pyglove 0.4.5.dev20240319__py3-none-any.whl → 0.4.5.dev202501132210__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.
Files changed (145) hide show
  1. pyglove/core/__init__.py +54 -20
  2. pyglove/core/coding/__init__.py +42 -0
  3. pyglove/core/coding/errors.py +111 -0
  4. pyglove/core/coding/errors_test.py +98 -0
  5. pyglove/core/coding/execution.py +309 -0
  6. pyglove/core/coding/execution_test.py +333 -0
  7. pyglove/core/{object_utils/codegen.py → coding/function_generation.py} +10 -4
  8. pyglove/core/{object_utils/codegen_test.py → coding/function_generation_test.py} +5 -7
  9. pyglove/core/coding/parsing.py +153 -0
  10. pyglove/core/coding/parsing_test.py +150 -0
  11. pyglove/core/coding/permissions.py +100 -0
  12. pyglove/core/coding/permissions_test.py +93 -0
  13. pyglove/core/geno/base.py +54 -41
  14. pyglove/core/geno/base_test.py +2 -4
  15. pyglove/core/geno/categorical.py +37 -28
  16. pyglove/core/geno/custom.py +19 -16
  17. pyglove/core/geno/numerical.py +20 -17
  18. pyglove/core/geno/space.py +4 -5
  19. pyglove/core/hyper/base.py +6 -6
  20. pyglove/core/hyper/categorical.py +94 -55
  21. pyglove/core/hyper/custom.py +7 -7
  22. pyglove/core/hyper/custom_test.py +9 -10
  23. pyglove/core/hyper/derived.py +30 -22
  24. pyglove/core/hyper/derived_test.py +2 -4
  25. pyglove/core/hyper/dynamic_evaluation.py +5 -6
  26. pyglove/core/hyper/evolvable.py +57 -46
  27. pyglove/core/hyper/numerical.py +48 -24
  28. pyglove/core/hyper/numerical_test.py +9 -9
  29. pyglove/core/hyper/object_template.py +58 -46
  30. pyglove/core/io/__init__.py +1 -0
  31. pyglove/core/io/file_system.py +17 -7
  32. pyglove/core/io/file_system_test.py +2 -0
  33. pyglove/core/io/sequence.py +299 -0
  34. pyglove/core/io/sequence_test.py +124 -0
  35. pyglove/core/logging_test.py +0 -2
  36. pyglove/core/patching/object_factory.py +4 -4
  37. pyglove/core/patching/pattern_based.py +4 -4
  38. pyglove/core/patching/rule_based.py +17 -5
  39. pyglove/core/patching/rule_based_test.py +27 -4
  40. pyglove/core/symbolic/__init__.py +2 -7
  41. pyglove/core/symbolic/base.py +320 -183
  42. pyglove/core/symbolic/base_test.py +123 -19
  43. pyglove/core/symbolic/boilerplate.py +7 -13
  44. pyglove/core/symbolic/boilerplate_test.py +25 -23
  45. pyglove/core/symbolic/class_wrapper.py +48 -45
  46. pyglove/core/symbolic/class_wrapper_test.py +2 -2
  47. pyglove/core/symbolic/compounding.py +9 -15
  48. pyglove/core/symbolic/compounding_test.py +2 -4
  49. pyglove/core/symbolic/dict.py +154 -110
  50. pyglove/core/symbolic/dict_test.py +238 -130
  51. pyglove/core/symbolic/diff.py +199 -10
  52. pyglove/core/symbolic/diff_test.py +226 -0
  53. pyglove/core/symbolic/flags.py +1 -1
  54. pyglove/core/symbolic/functor.py +29 -26
  55. pyglove/core/symbolic/functor_test.py +102 -50
  56. pyglove/core/symbolic/inferred.py +2 -2
  57. pyglove/core/symbolic/list.py +81 -50
  58. pyglove/core/symbolic/list_test.py +119 -97
  59. pyglove/core/symbolic/object.py +225 -113
  60. pyglove/core/symbolic/object_test.py +320 -108
  61. pyglove/core/symbolic/origin.py +17 -14
  62. pyglove/core/symbolic/origin_test.py +4 -2
  63. pyglove/core/symbolic/pure_symbolic.py +4 -3
  64. pyglove/core/symbolic/ref.py +108 -21
  65. pyglove/core/symbolic/ref_test.py +93 -0
  66. pyglove/core/symbolic/symbolize_test.py +10 -2
  67. pyglove/core/tuning/local_backend.py +2 -2
  68. pyglove/core/tuning/protocols.py +3 -3
  69. pyglove/core/tuning/sample_test.py +3 -3
  70. pyglove/core/typing/__init__.py +14 -5
  71. pyglove/core/typing/annotation_conversion.py +43 -27
  72. pyglove/core/typing/annotation_conversion_test.py +23 -0
  73. pyglove/core/typing/callable_ext.py +241 -3
  74. pyglove/core/typing/callable_ext_test.py +255 -0
  75. pyglove/core/typing/callable_signature.py +510 -66
  76. pyglove/core/typing/callable_signature_test.py +619 -99
  77. pyglove/core/typing/class_schema.py +229 -154
  78. pyglove/core/typing/class_schema_test.py +149 -95
  79. pyglove/core/typing/custom_typing.py +5 -4
  80. pyglove/core/typing/inspect.py +63 -0
  81. pyglove/core/typing/inspect_test.py +39 -0
  82. pyglove/core/typing/key_specs.py +10 -11
  83. pyglove/core/typing/key_specs_test.py +7 -4
  84. pyglove/core/typing/type_conversion.py +4 -5
  85. pyglove/core/typing/type_conversion_test.py +12 -12
  86. pyglove/core/typing/typed_missing.py +6 -7
  87. pyglove/core/typing/typed_missing_test.py +7 -8
  88. pyglove/core/typing/value_specs.py +604 -362
  89. pyglove/core/typing/value_specs_test.py +328 -90
  90. pyglove/core/utils/__init__.py +164 -0
  91. pyglove/core/{object_utils → utils}/common_traits.py +3 -67
  92. pyglove/core/utils/common_traits_test.py +36 -0
  93. pyglove/core/{object_utils → utils}/docstr_utils.py +23 -0
  94. pyglove/core/{object_utils → utils}/docstr_utils_test.py +36 -4
  95. pyglove/core/{object_utils → utils}/error_utils.py +78 -9
  96. pyglove/core/{object_utils → utils}/error_utils_test.py +61 -5
  97. pyglove/core/utils/formatting.py +464 -0
  98. pyglove/core/utils/formatting_test.py +453 -0
  99. pyglove/core/{object_utils → utils}/hierarchical.py +23 -25
  100. pyglove/core/{object_utils → utils}/hierarchical_test.py +3 -5
  101. pyglove/core/{object_utils → utils}/json_conversion.py +177 -52
  102. pyglove/core/{object_utils → utils}/json_conversion_test.py +97 -16
  103. pyglove/core/{object_utils → utils}/missing.py +3 -3
  104. pyglove/core/{object_utils → utils}/missing_test.py +2 -4
  105. pyglove/core/utils/text_color.py +128 -0
  106. pyglove/core/utils/text_color_test.py +94 -0
  107. pyglove/core/{object_utils → utils}/thread_local_test.py +1 -3
  108. pyglove/core/utils/timing.py +236 -0
  109. pyglove/core/utils/timing_test.py +154 -0
  110. pyglove/core/{object_utils → utils}/value_location.py +275 -6
  111. pyglove/core/utils/value_location_test.py +707 -0
  112. pyglove/core/views/__init__.py +32 -0
  113. pyglove/core/views/base.py +804 -0
  114. pyglove/core/views/base_test.py +580 -0
  115. pyglove/core/views/html/__init__.py +27 -0
  116. pyglove/core/views/html/base.py +547 -0
  117. pyglove/core/views/html/base_test.py +830 -0
  118. pyglove/core/views/html/controls/__init__.py +35 -0
  119. pyglove/core/views/html/controls/base.py +275 -0
  120. pyglove/core/views/html/controls/label.py +207 -0
  121. pyglove/core/views/html/controls/label_test.py +157 -0
  122. pyglove/core/views/html/controls/progress_bar.py +183 -0
  123. pyglove/core/views/html/controls/progress_bar_test.py +97 -0
  124. pyglove/core/views/html/controls/tab.py +320 -0
  125. pyglove/core/views/html/controls/tab_test.py +87 -0
  126. pyglove/core/views/html/controls/tooltip.py +99 -0
  127. pyglove/core/views/html/controls/tooltip_test.py +99 -0
  128. pyglove/core/views/html/tree_view.py +1517 -0
  129. pyglove/core/views/html/tree_view_test.py +1461 -0
  130. {pyglove-0.4.5.dev20240319.dist-info → pyglove-0.4.5.dev202501132210.dist-info}/METADATA +18 -4
  131. pyglove-0.4.5.dev202501132210.dist-info/RECORD +214 -0
  132. {pyglove-0.4.5.dev20240319.dist-info → pyglove-0.4.5.dev202501132210.dist-info}/WHEEL +1 -1
  133. pyglove/core/object_utils/__init__.py +0 -154
  134. pyglove/core/object_utils/common_traits_test.py +0 -82
  135. pyglove/core/object_utils/formatting.py +0 -234
  136. pyglove/core/object_utils/formatting_test.py +0 -223
  137. pyglove/core/object_utils/value_location_test.py +0 -385
  138. pyglove/core/symbolic/schema_utils.py +0 -327
  139. pyglove/core/symbolic/schema_utils_test.py +0 -57
  140. pyglove/core/typing/class_schema_utils.py +0 -202
  141. pyglove/core/typing/class_schema_utils_test.py +0 -194
  142. pyglove-0.4.5.dev20240319.dist-info/RECORD +0 -185
  143. /pyglove/core/{object_utils → utils}/thread_local.py +0 -0
  144. {pyglove-0.4.5.dev20240319.dist-info → pyglove-0.4.5.dev202501132210.dist-info}/LICENSE +0 -0
  145. {pyglove-0.4.5.dev20240319.dist-info → pyglove-0.4.5.dev202501132210.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,99 @@
1
+ # Copyright 2024 The PyGlove 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
+ """Html tooltip."""
15
+
16
+ from typing import Optional, Union
17
+ from pyglove.core import typing as pg_typing
18
+ from pyglove.core.symbolic import object as pg_object
19
+ # pylint: disable=g-importing-member
20
+ from pyglove.core.views.html.base import Html
21
+ from pyglove.core.views.html.controls.base import HtmlControl
22
+ # pylint: disable=g-importing-member
23
+
24
+
25
+ @pg_object.use_init_args(
26
+ ['content', 'for_element', 'id', 'css_classes', 'styles']
27
+ )
28
+ class Tooltip(HtmlControl):
29
+ """A tooltip control.
30
+
31
+ Attributes:
32
+ content: The content of the tooltip. It could be a string or a HTML object.
33
+ id: The id of the tooltip.
34
+ css_classes: The CSS classes for the tooltip.
35
+ for_element: The CSS selector for the element to attach the tooltip to.
36
+ e.g. '.my-element' or '#my-element'.
37
+ """
38
+
39
+ content: Union[str, Html]
40
+ for_element: Optional[str] = None
41
+
42
+ def _to_html(self, **kwargs):
43
+ if self.for_element is None:
44
+ raise ValueError(
45
+ 'CSS selector `for_element` is required for tooltip to display.'
46
+ )
47
+ content = self.content
48
+ if isinstance(self.content, str):
49
+ content = Html.escape(self.content)
50
+ return Html.element(
51
+ 'span',
52
+ [content],
53
+ id=self.element_id(),
54
+ css_classes=[
55
+ 'tooltip',
56
+ 'html-content' if isinstance(content, Html) else None,
57
+ ] + self.css_classes,
58
+ styles=self.styles,
59
+ ).add_style(
60
+ """
61
+ span.tooltip {
62
+ visibility: hidden;
63
+ white-space: pre-wrap;
64
+ font-weight: normal;
65
+ background-color: #484848;
66
+ color: #fff;
67
+ padding: 10px;
68
+ border-radius: 6px;
69
+ position: absolute;
70
+ z-index: 1;
71
+ }
72
+ span.tooltip:hover {
73
+ visibility: visible;
74
+ }
75
+ .tooltip.html-content {
76
+ white-space: inherit;
77
+ background-color: white;
78
+ color: inherit;
79
+ box-shadow: rgba(0, 0, 0, 0.16) 0px 1px 4px;
80
+ }
81
+ """,
82
+ f"""
83
+ {self.for_element}:hover + .tooltip {{
84
+ visibility: visible;
85
+ }}
86
+ """
87
+ )
88
+
89
+ def update(self, content: Union[str, Html]) -> None:
90
+ self._sync_members(content=self._update_content(content))
91
+ if isinstance(content, Html):
92
+ self._add_css_class('html-content')
93
+ else:
94
+ self._remove_css_class('html-content')
95
+
96
+
97
+ # Register converter for automatic conversion.
98
+ pg_typing.register_converter(str, Tooltip, Tooltip)
99
+ pg_typing.register_converter(Html, Tooltip, Tooltip)
@@ -0,0 +1,99 @@
1
+ # Copyright 2024 The PyGlove 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
+ import inspect
15
+ import unittest
16
+
17
+ from pyglove.core import symbolic # pylint: disable=unused-import
18
+ from pyglove.core.views.html import base
19
+ from pyglove.core.views.html.controls import tooltip as tooltip_lib
20
+
21
+
22
+ class TooltipTest(unittest.TestCase):
23
+
24
+ def assert_html_content(self, control, expected):
25
+ expected = inspect.cleandoc(expected).strip()
26
+ actual = control.to_html().content.strip()
27
+ if actual != expected:
28
+ print(actual)
29
+ self.assertEqual(actual, expected)
30
+
31
+ def test_basic(self):
32
+ tooltip = tooltip_lib.Tooltip('foo')
33
+ with self.assertRaisesRegex(
34
+ ValueError, 'CSS selector `for_element` is required'
35
+ ):
36
+ tooltip.to_html()
37
+
38
+ tooltip = tooltip_lib.Tooltip('foo', for_element='.bar')
39
+ self.assertEqual(tooltip.for_element, '.bar')
40
+ self.assert_html_content(
41
+ tooltip,
42
+ '<span class="tooltip">foo</span>'
43
+ )
44
+ self.assertIn(
45
+ inspect.cleandoc(
46
+ """
47
+ .bar:hover + .tooltip {
48
+ visibility: visible;
49
+ }
50
+ """
51
+ ),
52
+ tooltip.to_html().style_section,
53
+ )
54
+
55
+ def test_update(self):
56
+ tooltip = tooltip_lib.Tooltip('foo', for_element='.bar', interactive=True)
57
+ self.assertIn('id="control-', tooltip.to_html_str(content_only=True))
58
+ with tooltip.track_scripts() as scripts:
59
+ tooltip.update('normal text')
60
+ self.assertEqual(tooltip.content, 'normal text')
61
+ self.assertEqual(
62
+ scripts,
63
+ [
64
+ inspect.cleandoc(
65
+ f"""
66
+ elem = document.getElementById("{tooltip.element_id()}");
67
+ elem.textContent = "normal text";
68
+ """
69
+ ),
70
+ inspect.cleandoc(
71
+ f"""
72
+ elem = document.getElementById("{tooltip.element_id()}");
73
+ elem.classList.remove("html-content");
74
+ """
75
+ ),
76
+ ]
77
+ )
78
+ with tooltip.track_scripts() as scripts:
79
+ tooltip.update(base.Html('<b>bold text</b>'))
80
+ self.assertEqual(
81
+ scripts,
82
+ [
83
+ inspect.cleandoc(
84
+ f"""
85
+ elem = document.getElementById("{tooltip.element_id()}");
86
+ elem.innerHTML = "<b>bold text</b>";
87
+ """
88
+ ),
89
+ inspect.cleandoc(
90
+ f"""
91
+ elem = document.getElementById("{tooltip.element_id()}");
92
+ elem.classList.add("html-content");
93
+ """
94
+ ),
95
+ ]
96
+ )
97
+
98
+ if __name__ == '__main__':
99
+ unittest.main()