langfun 0.0.2.dev20240608__py3-none-any.whl → 0.0.2.dev20240610__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- langfun/core/__init__.py +3 -0
- langfun/core/repr_utils.py +83 -0
- langfun/core/repr_utils_test.py +58 -0
- {langfun-0.0.2.dev20240608.dist-info → langfun-0.0.2.dev20240610.dist-info}/METADATA +1 -1
- {langfun-0.0.2.dev20240608.dist-info → langfun-0.0.2.dev20240610.dist-info}/RECORD +8 -6
- {langfun-0.0.2.dev20240608.dist-info → langfun-0.0.2.dev20240610.dist-info}/LICENSE +0 -0
- {langfun-0.0.2.dev20240608.dist-info → langfun-0.0.2.dev20240610.dist-info}/WHEEL +0 -0
- {langfun-0.0.2.dev20240608.dist-info → langfun-0.0.2.dev20240610.dist-info}/top_level.txt +0 -0
langfun/core/__init__.py
CHANGED
@@ -121,6 +121,9 @@ from langfun.core.memory import Memory
|
|
121
121
|
# Utility for console output.
|
122
122
|
from langfun.core import console
|
123
123
|
|
124
|
+
# Helpers for implementing _repr_xxx_ methods.
|
125
|
+
from langfun.core import repr_utils
|
126
|
+
|
124
127
|
# Utility for event logging.
|
125
128
|
from langfun.core import logging
|
126
129
|
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# Copyright 2024 The Langfun Authors
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
"""Helpers for implementing _repr_xxx_ methods."""
|
15
|
+
|
16
|
+
import collections
|
17
|
+
import contextlib
|
18
|
+
import io
|
19
|
+
from typing import Iterator
|
20
|
+
|
21
|
+
from langfun.core import component
|
22
|
+
|
23
|
+
|
24
|
+
@contextlib.contextmanager
|
25
|
+
def share_parts() -> Iterator[dict[str, int]]:
|
26
|
+
"""Context manager for defining the context (scope) of shared content.
|
27
|
+
|
28
|
+
Under the context manager, call to `lf.write_shared` with the same content
|
29
|
+
will be written only once. This is useful for writing shared content such as
|
30
|
+
shared style and script sections in the HTML.
|
31
|
+
|
32
|
+
Example:
|
33
|
+
```
|
34
|
+
class Foo(pg.Object):
|
35
|
+
def _repr_html_(self) -> str:
|
36
|
+
s = io.StringIO()
|
37
|
+
lf.repr_utils.write_shared_part(s, '<style>..</style>')
|
38
|
+
lf.repr_utils.write_shared_part(s, '<script>..</script>')
|
39
|
+
return s.getvalue()
|
40
|
+
|
41
|
+
with lf.repr_utils.share_parts() as share_parts:
|
42
|
+
# The <style> and <script> section will be written only once.
|
43
|
+
lf.console.display(Foo())
|
44
|
+
lf.console.display(Foo())
|
45
|
+
|
46
|
+
# Assert that the shared content is attempted to be written twice.
|
47
|
+
assert share_parts['<style>..</style>'] == 2
|
48
|
+
```
|
49
|
+
|
50
|
+
Yields:
|
51
|
+
A dictionary mapping the shared content to the number of times it is
|
52
|
+
attempted to be written.
|
53
|
+
"""
|
54
|
+
context = component.context_value(
|
55
|
+
'__shared_parts__', collections.defaultdict(int)
|
56
|
+
)
|
57
|
+
with component.context(__shared_parts__=context):
|
58
|
+
try:
|
59
|
+
yield context
|
60
|
+
finally:
|
61
|
+
pass
|
62
|
+
|
63
|
+
|
64
|
+
def write_maybe_shared(s: io.StringIO, content: str) -> bool:
|
65
|
+
"""Writes a maybe shared part to an string stream.
|
66
|
+
|
67
|
+
Args:
|
68
|
+
s: The string stream to write to.
|
69
|
+
content: A maybe shared content to write.
|
70
|
+
|
71
|
+
Returns:
|
72
|
+
True if the content is written to the string. False if the content is
|
73
|
+
already written under the same share context.
|
74
|
+
"""
|
75
|
+
context = component.context_value('__shared_parts__', None)
|
76
|
+
if context is None:
|
77
|
+
s.write(content)
|
78
|
+
return True
|
79
|
+
written = content in context
|
80
|
+
if not written:
|
81
|
+
s.write(content)
|
82
|
+
context[content] += 1
|
83
|
+
return not written
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# Copyright 2024 The Langfun Authors
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
"""Tests for langfun.core.repr_utils."""
|
15
|
+
|
16
|
+
import io
|
17
|
+
import unittest
|
18
|
+
|
19
|
+
from langfun.core import repr_utils
|
20
|
+
|
21
|
+
|
22
|
+
class SharingContentTest(unittest.TestCase):
|
23
|
+
|
24
|
+
def test_sharing(self):
|
25
|
+
s = io.StringIO()
|
26
|
+
|
27
|
+
self.assertTrue(repr_utils.write_maybe_shared(s, '<hr>'))
|
28
|
+
self.assertTrue(repr_utils.write_maybe_shared(s, '<hr>'))
|
29
|
+
|
30
|
+
with repr_utils.share_parts() as ctx1:
|
31
|
+
self.assertTrue(repr_utils.write_maybe_shared(s, '<style></style>'))
|
32
|
+
self.assertFalse(repr_utils.write_maybe_shared(s, '<style></style>'))
|
33
|
+
|
34
|
+
with repr_utils.share_parts() as ctx2:
|
35
|
+
self.assertIs(ctx2, ctx1)
|
36
|
+
self.assertFalse(repr_utils.write_maybe_shared(s, '<style></style>'))
|
37
|
+
self.assertTrue(repr_utils.write_maybe_shared(s, '<style>a</style>'))
|
38
|
+
self.assertFalse(repr_utils.write_maybe_shared(s, '<style>a</style>'))
|
39
|
+
self.assertTrue(repr_utils.write_maybe_shared(s, '<style>b</style>'))
|
40
|
+
|
41
|
+
with repr_utils.share_parts() as ctx3:
|
42
|
+
self.assertIs(ctx3, ctx1)
|
43
|
+
self.assertFalse(repr_utils.write_maybe_shared(s, '<style></style>'))
|
44
|
+
self.assertFalse(repr_utils.write_maybe_shared(s, '<style>a</style>'))
|
45
|
+
self.assertFalse(repr_utils.write_maybe_shared(s, '<style>a</style>'))
|
46
|
+
self.assertFalse(repr_utils.write_maybe_shared(s, '<style>b</style>'))
|
47
|
+
|
48
|
+
self.assertEqual(
|
49
|
+
s.getvalue(),
|
50
|
+
'<hr><hr><style></style><style>a</style><style>b</style>'
|
51
|
+
)
|
52
|
+
self.assertEqual(ctx1['<style></style>'], 4)
|
53
|
+
self.assertEqual(ctx1['<style>b</style>'], 2)
|
54
|
+
self.assertEqual(ctx1['<style>a</style>'], 4)
|
55
|
+
|
56
|
+
|
57
|
+
if __name__ == '__main__':
|
58
|
+
unittest.main()
|
@@ -1,5 +1,5 @@
|
|
1
1
|
langfun/__init__.py,sha256=P62MnqA6-f0h8iYfQ3MT6Yg7a4qRnQeb4GrIn6dcSnY,2274
|
2
|
-
langfun/core/__init__.py,sha256=
|
2
|
+
langfun/core/__init__.py,sha256=Mdp1a2YnXdSmfTfbUwuAnEWYbjA3rXXGtbxl5fljZyg,4812
|
3
3
|
langfun/core/component.py,sha256=Icyoj9ICoJoK2r2PHbrFXbxnseOr9QZZOvKWklLWNo8,10276
|
4
4
|
langfun/core/component_test.py,sha256=q15Xn51cVTu2RKxZ9U5VQgT3bm6RQ4638bKhWBtvW5o,8220
|
5
5
|
langfun/core/concurrent.py,sha256=TRc49pJ3HQro2kb5FtcWkHjhBm8UcgE8RJybU5cU3-0,24537
|
@@ -19,6 +19,8 @@ langfun/core/modality.py,sha256=Tla4t86DUYHpbZ2G7dy1r19fTj_Ga5XOvlYp6lbWa-Q,3512
|
|
19
19
|
langfun/core/modality_test.py,sha256=HyZ5xONKQ0Fw18SzoWAq-Ob9njOXIIjBo1hNtw-rudw,2400
|
20
20
|
langfun/core/natural_language.py,sha256=3ynSnaYQnjE60LIPK5fyMgdIjubnPYZwzGq4rWPeloE,1177
|
21
21
|
langfun/core/natural_language_test.py,sha256=LHGU_1ytbkGuSZQFIFP7vP3dBlcY4-A12fT6dbjUA0E,1424
|
22
|
+
langfun/core/repr_utils.py,sha256=HrN7FoGUvpTlv5aL_XISouwZN84z9LmrB6_2jEn1ukc,2590
|
23
|
+
langfun/core/repr_utils_test.py,sha256=-XId1A72Vbzo289dYuxC6TegNXuZhI28WbNrm1ghiwc,2206
|
22
24
|
langfun/core/sampling.py,sha256=vygWvgC8MFw0_AKNSmz-ywMXJYWf8cl0tI8QycvAmyI,5795
|
23
25
|
langfun/core/sampling_test.py,sha256=U7PANpMsl9E_pa4_Y4FzesSjcwg-u-LKHGCWSgv-8FY,3663
|
24
26
|
langfun/core/subscription.py,sha256=euawEuSZP-BHydaT-AQpfYFL0m5pWPGcW0upFhrojqc,10930
|
@@ -115,8 +117,8 @@ langfun/core/templates/demonstration.py,sha256=vCrgYubdZM5Umqcgp8NUVGXgr4P_c-fik
|
|
115
117
|
langfun/core/templates/demonstration_test.py,sha256=SafcDQ0WgI7pw05EmPI2S4v1t3ABKzup8jReCljHeK4,2162
|
116
118
|
langfun/core/templates/selfplay.py,sha256=yhgrJbiYwq47TgzThmHrDQTF4nDrTI09CWGhuQPNv-s,2273
|
117
119
|
langfun/core/templates/selfplay_test.py,sha256=rBW2Qr8yi-aWYwoTwRR-n1peKyMX9QXPZXURjLgoiRs,2264
|
118
|
-
langfun-0.0.2.
|
119
|
-
langfun-0.0.2.
|
120
|
-
langfun-0.0.2.
|
121
|
-
langfun-0.0.2.
|
122
|
-
langfun-0.0.2.
|
120
|
+
langfun-0.0.2.dev20240610.dist-info/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
|
121
|
+
langfun-0.0.2.dev20240610.dist-info/METADATA,sha256=IB3ZKHYSs9TGLiJ8_ZEUZGX-dvh_SI8D2RD0ikQYvro,3550
|
122
|
+
langfun-0.0.2.dev20240610.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
123
|
+
langfun-0.0.2.dev20240610.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
|
124
|
+
langfun-0.0.2.dev20240610.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|