pyglove 0.4.5.dev202501050808__py3-none-any.whl → 0.4.5.dev202501060809__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.
- pyglove/core/__init__.py +24 -21
- pyglove/core/geno/base.py +53 -38
- pyglove/core/geno/base_test.py +2 -4
- pyglove/core/geno/categorical.py +36 -27
- pyglove/core/geno/custom.py +18 -15
- pyglove/core/geno/numerical.py +19 -16
- pyglove/core/geno/space.py +3 -4
- pyglove/core/hyper/base.py +6 -6
- pyglove/core/hyper/categorical.py +91 -52
- pyglove/core/hyper/custom.py +7 -7
- pyglove/core/hyper/custom_test.py +9 -10
- pyglove/core/hyper/derived.py +30 -22
- pyglove/core/hyper/derived_test.py +2 -4
- pyglove/core/hyper/dynamic_evaluation.py +3 -4
- pyglove/core/hyper/evolvable.py +57 -46
- pyglove/core/hyper/numerical.py +48 -24
- pyglove/core/hyper/numerical_test.py +9 -9
- pyglove/core/hyper/object_template.py +58 -46
- pyglove/core/logging_test.py +0 -2
- pyglove/core/patching/object_factory.py +4 -4
- pyglove/core/patching/pattern_based.py +4 -4
- pyglove/core/patching/rule_based.py +4 -3
- pyglove/core/symbolic/base.py +167 -131
- pyglove/core/symbolic/base_test.py +17 -19
- pyglove/core/symbolic/boilerplate.py +4 -5
- pyglove/core/symbolic/class_wrapper.py +9 -9
- pyglove/core/symbolic/compounding.py +2 -2
- pyglove/core/symbolic/compounding_test.py +2 -4
- pyglove/core/symbolic/dict.py +70 -54
- pyglove/core/symbolic/dict_test.py +117 -100
- pyglove/core/symbolic/diff.py +12 -12
- pyglove/core/symbolic/flags.py +1 -1
- pyglove/core/symbolic/functor.py +16 -15
- pyglove/core/symbolic/functor_test.py +2 -4
- pyglove/core/symbolic/inferred.py +2 -2
- pyglove/core/symbolic/list.py +70 -47
- pyglove/core/symbolic/list_test.py +117 -98
- pyglove/core/symbolic/object.py +42 -40
- pyglove/core/symbolic/object_test.py +95 -88
- pyglove/core/symbolic/origin.py +5 -7
- pyglove/core/symbolic/pure_symbolic.py +4 -3
- pyglove/core/symbolic/ref.py +12 -8
- pyglove/core/tuning/local_backend.py +2 -2
- pyglove/core/tuning/protocols.py +3 -3
- pyglove/core/typing/annotation_conversion.py +3 -3
- pyglove/core/typing/callable_ext.py +11 -13
- pyglove/core/typing/callable_signature.py +19 -18
- pyglove/core/typing/callable_signature_test.py +3 -5
- pyglove/core/typing/class_schema.py +48 -44
- pyglove/core/typing/class_schema_test.py +3 -5
- pyglove/core/typing/custom_typing.py +5 -4
- pyglove/core/typing/key_specs.py +5 -7
- pyglove/core/typing/key_specs_test.py +4 -4
- pyglove/core/typing/type_conversion.py +4 -5
- pyglove/core/typing/type_conversion_test.py +12 -12
- pyglove/core/typing/typed_missing.py +6 -7
- pyglove/core/typing/typed_missing_test.py +7 -8
- pyglove/core/typing/value_specs.py +210 -141
- pyglove/core/typing/value_specs_test.py +12 -13
- pyglove/core/utils/__init__.py +159 -0
- pyglove/core/{object_utils → utils}/common_traits_test.py +1 -3
- pyglove/core/{object_utils → utils}/docstr_utils_test.py +1 -3
- pyglove/core/{object_utils → utils}/error_utils.py +3 -3
- pyglove/core/{object_utils → utils}/error_utils_test.py +1 -1
- pyglove/core/{object_utils → utils}/formatting.py +1 -1
- pyglove/core/{object_utils → utils}/formatting_test.py +1 -2
- pyglove/core/{object_utils → utils}/hierarchical.py +23 -25
- pyglove/core/{object_utils → utils}/hierarchical_test.py +3 -5
- pyglove/core/{object_utils → utils}/json_conversion_test.py +1 -3
- pyglove/core/{object_utils → utils}/missing.py +2 -2
- pyglove/core/{object_utils → utils}/missing_test.py +2 -4
- pyglove/core/{object_utils → utils}/thread_local_test.py +1 -3
- pyglove/core/{object_utils → utils}/timing.py +3 -3
- pyglove/core/{object_utils → utils}/timing_test.py +2 -3
- pyglove/core/{object_utils → utils}/value_location.py +2 -2
- pyglove/core/{object_utils → utils}/value_location_test.py +2 -4
- pyglove/core/views/base.py +25 -29
- pyglove/core/views/html/base.py +14 -15
- pyglove/core/views/html/controls/base.py +5 -5
- pyglove/core/views/html/controls/progress_bar.py +3 -5
- pyglove/core/views/html/tree_view.py +37 -35
- {pyglove-0.4.5.dev202501050808.dist-info → pyglove-0.4.5.dev202501060809.dist-info}/METADATA +1 -1
- {pyglove-0.4.5.dev202501050808.dist-info → pyglove-0.4.5.dev202501060809.dist-info}/RECORD +90 -90
- {pyglove-0.4.5.dev202501050808.dist-info → pyglove-0.4.5.dev202501060809.dist-info}/WHEEL +1 -1
- pyglove/core/object_utils/__init__.py +0 -161
- /pyglove/core/{object_utils → utils}/common_traits.py +0 -0
- /pyglove/core/{object_utils → utils}/docstr_utils.py +0 -0
- /pyglove/core/{object_utils → utils}/json_conversion.py +0 -0
- /pyglove/core/{object_utils → utils}/thread_local.py +0 -0
- {pyglove-0.4.5.dev202501050808.dist-info → pyglove-0.4.5.dev202501060809.dist-info}/LICENSE +0 -0
- {pyglove-0.4.5.dev202501050808.dist-info → pyglove-0.4.5.dev202501060809.dist-info}/top_level.txt +0 -0
@@ -18,7 +18,7 @@ import sys
|
|
18
18
|
import typing
|
19
19
|
import unittest
|
20
20
|
|
21
|
-
from pyglove.core import
|
21
|
+
from pyglove.core import utils
|
22
22
|
from pyglove.core.typing import annotation_conversion # pylint: disable=unused-import
|
23
23
|
from pyglove.core.typing import callable_signature
|
24
24
|
from pyglove.core.typing import class_schema
|
@@ -36,11 +36,10 @@ class ValueSpecTest(unittest.TestCase):
|
|
36
36
|
"""Base class for value spec test."""
|
37
37
|
|
38
38
|
def assert_json_conversion(self, v):
|
39
|
-
self.assertEqual(
|
39
|
+
self.assertEqual(utils.from_json(v.to_json()), v)
|
40
40
|
|
41
41
|
def assert_json_conversion_key(self, v, key):
|
42
|
-
self.assertEqual(
|
43
|
-
v.to_json()[object_utils.JSONConvertible.TYPE_NAME_KEY], key)
|
42
|
+
self.assertEqual(v.to_json()[utils.JSONConvertible.TYPE_NAME_KEY], key)
|
44
43
|
|
45
44
|
|
46
45
|
class BoolTest(ValueSpecTest):
|
@@ -1039,9 +1038,9 @@ class ListTest(ValueSpecTest):
|
|
1039
1038
|
self.assertEqual(vs.List(vs.Int().noneable()).apply([1, None]), [1, None])
|
1040
1039
|
# Automatic conversion: str -> KeyPath is a registered conversion.
|
1041
1040
|
# See 'type_conversion.py'.
|
1042
|
-
l = vs.List(vs.Object(
|
1043
|
-
self.assertIsInstance(l[0],
|
1044
|
-
self.assertEqual(l, [
|
1041
|
+
l = vs.List(vs.Object(utils.KeyPath)).apply(['a.b.c'])
|
1042
|
+
self.assertIsInstance(l[0], utils.KeyPath)
|
1043
|
+
self.assertEqual(l, [utils.KeyPath.parse('a.b.c')])
|
1045
1044
|
self.assertEqual(
|
1046
1045
|
vs.List(vs.Int()).apply(
|
1047
1046
|
typed_missing.MISSING_VALUE, allow_partial=True),
|
@@ -2046,7 +2045,7 @@ class DictTest(ValueSpecTest):
|
|
2046
2045
|
x = vs.Dict([
|
2047
2046
|
('a', int, 'field 1', dict(x=1)),
|
2048
2047
|
]).freeze(dict(a=1))
|
2049
|
-
y =
|
2048
|
+
y = utils.from_json(x.to_json())
|
2050
2049
|
self.assert_json_conversion(
|
2051
2050
|
vs.Dict([
|
2052
2051
|
('a', int, 'field 1', dict(x=1)),
|
@@ -2089,7 +2088,7 @@ class ObjectTest(ValueSpecTest):
|
|
2089
2088
|
class C(A):
|
2090
2089
|
pass
|
2091
2090
|
|
2092
|
-
class D(C,
|
2091
|
+
class D(C, utils.MaybePartial):
|
2093
2092
|
|
2094
2093
|
def missing_values(self):
|
2095
2094
|
return {'SOME_KEY': 'SOME_VALUE'}
|
@@ -2378,7 +2377,7 @@ class CallableTest(ValueSpecTest):
|
|
2378
2377
|
|
2379
2378
|
def test_value_type(self):
|
2380
2379
|
self.assertIsNone(vs.Callable().value_type)
|
2381
|
-
self.assertEqual(vs.Functor().annotation,
|
2380
|
+
self.assertEqual(vs.Functor().annotation, utils.Functor)
|
2382
2381
|
|
2383
2382
|
def test_forward_refs(self):
|
2384
2383
|
self.assertEqual(vs.Callable().forward_refs, set())
|
@@ -2584,7 +2583,7 @@ class CallableTest(ValueSpecTest):
|
|
2584
2583
|
|
2585
2584
|
def test_apply_on_functor(self):
|
2586
2585
|
|
2587
|
-
class FunctorWithRegularArgs(
|
2586
|
+
class FunctorWithRegularArgs(utils.Functor):
|
2588
2587
|
|
2589
2588
|
__signature__ = Signature(
|
2590
2589
|
callable_type=callable_signature.CallableType.FUNCTION,
|
@@ -2630,7 +2629,7 @@ class CallableTest(ValueSpecTest):
|
|
2630
2629
|
|
2631
2630
|
def test_apply_on_functor_with_varargs(self):
|
2632
2631
|
|
2633
|
-
class FunctorWithVarArgs(
|
2632
|
+
class FunctorWithVarArgs(utils.Functor):
|
2634
2633
|
|
2635
2634
|
__signature__ = Signature(
|
2636
2635
|
callable_type=callable_signature.CallableType.FUNCTION,
|
@@ -2781,7 +2780,7 @@ class CallableTest(ValueSpecTest):
|
|
2781
2780
|
)
|
2782
2781
|
)
|
2783
2782
|
x = vs.Callable([vs.Int()], default=lambda x: x + 1).noneable()
|
2784
|
-
y =
|
2783
|
+
y = utils.from_json(x.to_json())
|
2785
2784
|
self.assert_json_conversion(
|
2786
2785
|
vs.Callable([vs.Int()], default=lambda x: x + 1).noneable()
|
2787
2786
|
)
|
@@ -0,0 +1,159 @@
|
|
1
|
+
# Copyright 2022 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
|
+
# pylint: disable=line-too-long
|
15
|
+
"""Utility library that provides common traits for objects in Python.
|
16
|
+
|
17
|
+
Overview
|
18
|
+
--------
|
19
|
+
|
20
|
+
``pg.utils`` sits at the bottom of all PyGlove modules and empowers other
|
21
|
+
modules with the following features:
|
22
|
+
|
23
|
+
+---------------------+--------------------------------------------+
|
24
|
+
| Functionality | API |
|
25
|
+
+=====================+============================================+
|
26
|
+
| Formatting | :class:`pg.Formattable`, |
|
27
|
+
| | |
|
28
|
+
| | :func:`pg.format`, |
|
29
|
+
| | |
|
30
|
+
| | :func:`pg.print`, |
|
31
|
+
| | |
|
32
|
+
| | :func:`pg.utils.kvlist_str`, |
|
33
|
+
| | |
|
34
|
+
| | :func:`pg.utils.quote_if_str`, |
|
35
|
+
| | |
|
36
|
+
| | :func:`pg.utils.message_on_path` |
|
37
|
+
+---------------------+--------------------------------------------+
|
38
|
+
| Serialization | :class:`pg.JSONConvertible`, |
|
39
|
+
| | |
|
40
|
+
| | :func:`pg.registered_types`, |
|
41
|
+
| | |
|
42
|
+
| | :func:`pg.utils.to_json`, |
|
43
|
+
| | |
|
44
|
+
| | :func:`pg.utils.from_json`, |
|
45
|
+
+---------------------+--------------------------------------------+
|
46
|
+
| Partial construction| :class:`pg.MaybePartial`, |
|
47
|
+
| | |
|
48
|
+
| | :const:`pg.MISSING_VALUE`. |
|
49
|
+
+---------------------+--------------------------------------------+
|
50
|
+
| Hierarchical key | :class:`pg.KeyPath` |
|
51
|
+
| representation | |
|
52
|
+
+---------------------+--------------------------------------------+
|
53
|
+
| Hierarchical object | :func:`pg.utils.traverse` |
|
54
|
+
| traversal | |
|
55
|
+
+---------------------+--------------------------------------------+
|
56
|
+
| Hierarchical object | :func:`pg.utils.transform`, |
|
57
|
+
| transformation | |
|
58
|
+
| | :func:`pg.utils.merge`, |
|
59
|
+
| | |
|
60
|
+
| | :func:`pg.utils.canonicalize`, |
|
61
|
+
| | |
|
62
|
+
| | :func:`pg.utils.flatten` |
|
63
|
+
+---------------------+--------------------------------------------+
|
64
|
+
| Docstr handling | :class:`pg.docstr`, |
|
65
|
+
+---------------------+--------------------------------------------+
|
66
|
+
| Error handling | :class:`pg.catch_errors`, |
|
67
|
+
+---------------------+--------------------------------------------+
|
68
|
+
"""
|
69
|
+
# pylint: enable=line-too-long
|
70
|
+
# pylint: disable=g-bad-import-order
|
71
|
+
# pylint: disable=g-importing-member
|
72
|
+
|
73
|
+
# Handling JSON conversion.
|
74
|
+
from pyglove.core.utils.json_conversion import Nestable
|
75
|
+
from pyglove.core.utils.json_conversion import JSONValueType
|
76
|
+
|
77
|
+
from pyglove.core.utils.json_conversion import JSONConvertible
|
78
|
+
from pyglove.core.utils.json_conversion import from_json
|
79
|
+
from pyglove.core.utils.json_conversion import to_json
|
80
|
+
from pyglove.core.utils.json_conversion import registered_types
|
81
|
+
|
82
|
+
# Handling formatting.
|
83
|
+
from pyglove.core.utils.formatting import Formattable
|
84
|
+
from pyglove.core.utils.formatting import format # pylint: disable=redefined-builtin
|
85
|
+
from pyglove.core.utils.formatting import printv as print # pylint: disable=redefined-builtin
|
86
|
+
from pyglove.core.utils.formatting import kvlist_str
|
87
|
+
from pyglove.core.utils.formatting import quote_if_str
|
88
|
+
from pyglove.core.utils.formatting import maybe_markdown_quote
|
89
|
+
from pyglove.core.utils.formatting import comma_delimited_str
|
90
|
+
from pyglove.core.utils.formatting import camel_to_snake
|
91
|
+
from pyglove.core.utils.formatting import auto_plural
|
92
|
+
from pyglove.core.utils.formatting import BracketType
|
93
|
+
from pyglove.core.utils.formatting import bracket_chars
|
94
|
+
from pyglove.core.utils.formatting import RawText
|
95
|
+
|
96
|
+
# Context managers for defining the default format for __str__ and __repr__.
|
97
|
+
from pyglove.core.utils.formatting import str_format
|
98
|
+
from pyglove.core.utils.formatting import repr_format
|
99
|
+
|
100
|
+
# Value location.
|
101
|
+
from pyglove.core.utils.value_location import KeyPath
|
102
|
+
from pyglove.core.utils.value_location import KeyPathSet
|
103
|
+
from pyglove.core.utils.value_location import StrKey
|
104
|
+
from pyglove.core.utils.value_location import message_on_path
|
105
|
+
|
106
|
+
# Value markers.
|
107
|
+
from pyglove.core.utils.missing import MissingValue
|
108
|
+
from pyglove.core.utils.missing import MISSING_VALUE
|
109
|
+
|
110
|
+
# Handling hierarchical.
|
111
|
+
from pyglove.core.utils.hierarchical import traverse
|
112
|
+
from pyglove.core.utils.hierarchical import transform
|
113
|
+
from pyglove.core.utils.hierarchical import flatten
|
114
|
+
from pyglove.core.utils.hierarchical import canonicalize
|
115
|
+
from pyglove.core.utils.hierarchical import merge
|
116
|
+
from pyglove.core.utils.hierarchical import merge_tree
|
117
|
+
from pyglove.core.utils.hierarchical import is_partial
|
118
|
+
from pyglove.core.utils.hierarchical import try_listify_dict_with_int_keys
|
119
|
+
|
120
|
+
# Common traits.
|
121
|
+
from pyglove.core.utils.common_traits import MaybePartial
|
122
|
+
from pyglove.core.utils.common_traits import Functor
|
123
|
+
|
124
|
+
from pyglove.core.utils.common_traits import explicit_method_override
|
125
|
+
from pyglove.core.utils.common_traits import ensure_explicit_method_override
|
126
|
+
|
127
|
+
# Handling thread local values.
|
128
|
+
from pyglove.core.utils.thread_local import thread_local_value_scope
|
129
|
+
from pyglove.core.utils.thread_local import thread_local_has
|
130
|
+
from pyglove.core.utils.thread_local import thread_local_set
|
131
|
+
from pyglove.core.utils.thread_local import thread_local_get
|
132
|
+
from pyglove.core.utils.thread_local import thread_local_del
|
133
|
+
from pyglove.core.utils.thread_local import thread_local_increment
|
134
|
+
from pyglove.core.utils.thread_local import thread_local_decrement
|
135
|
+
from pyglove.core.utils.thread_local import thread_local_push
|
136
|
+
from pyglove.core.utils.thread_local import thread_local_pop
|
137
|
+
from pyglove.core.utils.thread_local import thread_local_peek
|
138
|
+
|
139
|
+
# Handling docstrings.
|
140
|
+
from pyglove.core.utils.docstr_utils import DocStr
|
141
|
+
from pyglove.core.utils.docstr_utils import DocStrStyle
|
142
|
+
from pyglove.core.utils.docstr_utils import DocStrEntry
|
143
|
+
from pyglove.core.utils.docstr_utils import DocStrExample
|
144
|
+
from pyglove.core.utils.docstr_utils import DocStrArgument
|
145
|
+
from pyglove.core.utils.docstr_utils import DocStrReturns
|
146
|
+
from pyglove.core.utils.docstr_utils import DocStrRaises
|
147
|
+
from pyglove.core.utils.docstr_utils import docstr
|
148
|
+
|
149
|
+
# Handling exceptions.
|
150
|
+
from pyglove.core.utils.error_utils import catch_errors
|
151
|
+
from pyglove.core.utils.error_utils import CatchErrorsContext
|
152
|
+
from pyglove.core.utils.error_utils import ErrorInfo
|
153
|
+
|
154
|
+
# Timing.
|
155
|
+
from pyglove.core.utils.timing import timeit
|
156
|
+
from pyglove.core.utils.timing import TimeIt
|
157
|
+
|
158
|
+
# pylint: enable=g-importing-member
|
159
|
+
# pylint: enable=g-bad-import-order
|
@@ -11,10 +11,8 @@
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
|
-
"""Tests for pyglove.object_utils.common_traits."""
|
15
|
-
|
16
14
|
import unittest
|
17
|
-
from pyglove.core.
|
15
|
+
from pyglove.core.utils import common_traits
|
18
16
|
|
19
17
|
|
20
18
|
class ExplicitlyOverrideTest(unittest.TestCase):
|
@@ -11,11 +11,9 @@
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
|
-
"""Tests for pyglove.object_utils.docstr_utils."""
|
15
|
-
|
16
14
|
import inspect
|
17
15
|
import unittest
|
18
|
-
from pyglove.core.
|
16
|
+
from pyglove.core.utils import docstr_utils
|
19
17
|
|
20
18
|
|
21
19
|
class DocStrTest(unittest.TestCase):
|
@@ -21,8 +21,8 @@ import sys
|
|
21
21
|
import traceback
|
22
22
|
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Type, Union
|
23
23
|
|
24
|
-
from pyglove.core.
|
25
|
-
from pyglove.core.
|
24
|
+
from pyglove.core.utils import formatting
|
25
|
+
from pyglove.core.utils import json_conversion
|
26
26
|
|
27
27
|
|
28
28
|
@dataclasses.dataclass(frozen=True)
|
@@ -102,7 +102,7 @@ def catch_errors(
|
|
102
102
|
|
103
103
|
Examples::
|
104
104
|
|
105
|
-
with pg.
|
105
|
+
with pg.utils.catch_errors(
|
106
106
|
[
|
107
107
|
RuntimeErrror,
|
108
108
|
(ValueError, 'Input is wrong.')
|
@@ -18,7 +18,7 @@ import enum
|
|
18
18
|
import io
|
19
19
|
import sys
|
20
20
|
from typing import Any, Callable, ContextManager, Dict, List, Optional, Sequence, Set, Tuple
|
21
|
-
from pyglove.core.
|
21
|
+
from pyglove.core.utils import thread_local
|
22
22
|
|
23
23
|
|
24
24
|
_TLS_STR_FORMAT_KWARGS = '_str_format_kwargs'
|
@@ -11,11 +11,10 @@
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
|
-
"""Tests for pyglove.object_utils.formatting."""
|
15
14
|
import inspect
|
16
15
|
import unittest
|
17
16
|
|
18
|
-
from pyglove.core.
|
17
|
+
from pyglove.core.utils import formatting
|
19
18
|
|
20
19
|
|
21
20
|
class Foo(formatting.Formattable):
|
@@ -14,9 +14,9 @@
|
|
14
14
|
"""Operating hierarchical object."""
|
15
15
|
|
16
16
|
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
|
17
|
-
from pyglove.core.
|
18
|
-
from pyglove.core.
|
19
|
-
from pyglove.core.
|
17
|
+
from pyglove.core.utils import common_traits
|
18
|
+
from pyglove.core.utils.missing import MISSING_VALUE
|
19
|
+
from pyglove.core.utils.value_location import KeyPath
|
20
20
|
|
21
21
|
|
22
22
|
def traverse(value: Any,
|
@@ -33,7 +33,7 @@ def traverse(value: Any,
|
|
33
33
|
print(path)
|
34
34
|
|
35
35
|
tree = {'a': [{'c': [1, 2]}, {'d': {'g': (3, 4)}}], 'b': 'foo'}
|
36
|
-
pg.
|
36
|
+
pg.utils.traverse(tree, preorder_visit)
|
37
37
|
|
38
38
|
# Should print:
|
39
39
|
# 'a'
|
@@ -48,10 +48,10 @@ def traverse(value: Any,
|
|
48
48
|
|
49
49
|
Args:
|
50
50
|
value: A maybe hierarchical value to traverse.
|
51
|
-
preorder_visitor_fn: Preorder visitor function.
|
52
|
-
|
53
|
-
postorder_visitor_fn: Postorder visitor function.
|
54
|
-
|
51
|
+
preorder_visitor_fn: Preorder visitor function. Function signature is (path,
|
52
|
+
value) -> should_continue.
|
53
|
+
postorder_visitor_fn: Postorder visitor function. Function signature is
|
54
|
+
(path, value) -> should_continue.
|
55
55
|
root_path: The key path of the root value.
|
56
56
|
|
57
57
|
Returns:
|
@@ -111,7 +111,7 @@ def transform(value: Any,
|
|
111
111
|
'e': 'bar',
|
112
112
|
'f': 4
|
113
113
|
}
|
114
|
-
output = pg.
|
114
|
+
output = pg.utils.transform(inputs, _remove_int)
|
115
115
|
assert output == {
|
116
116
|
'a': {
|
117
117
|
'c': ['bar'],
|
@@ -123,11 +123,11 @@ def transform(value: Any,
|
|
123
123
|
Args:
|
124
124
|
value: Any python value type. If value is a list of dict, transformation
|
125
125
|
will occur recursively.
|
126
|
-
transform_fn: Transform function in signature
|
127
|
-
|
128
|
-
If new value is MISSING_VALUE, key will be deleted.
|
126
|
+
transform_fn: Transform function in signature (path, value) -> new value If
|
127
|
+
new value is MISSING_VALUE, key will be deleted.
|
129
128
|
root_path: KeyPath of the root.
|
130
129
|
inplace: If True, perform transformation in place.
|
130
|
+
|
131
131
|
Returns:
|
132
132
|
Transformed value.
|
133
133
|
"""
|
@@ -186,7 +186,7 @@ def flatten(src: Any, flatten_complex_keys: bool = True) -> Any:
|
|
186
186
|
'b': 'hi',
|
187
187
|
'c': None
|
188
188
|
}
|
189
|
-
output = pg.
|
189
|
+
output = pg.utils.flatten(inputs)
|
190
190
|
assert output == {
|
191
191
|
'a.e': 1,
|
192
192
|
'a.f[0].g': 2,
|
@@ -200,9 +200,9 @@ def flatten(src: Any, flatten_complex_keys: bool = True) -> Any:
|
|
200
200
|
Args:
|
201
201
|
src: source value to flatten.
|
202
202
|
flatten_complex_keys: if True, complex keys such as 'x.y' will be flattened
|
203
|
-
|
204
|
-
{'a
|
205
|
-
|
203
|
+
as 'x'.'y'. For example: {'a': {'b.c': 1}} will be flattened into
|
204
|
+
{'a.b.c': 1} if this flag is on, otherwise it will be flattened as
|
205
|
+
{'a[b.c]': 1}.
|
206
206
|
|
207
207
|
Returns:
|
208
208
|
For primitive value types, `src` itself will be returned.
|
@@ -464,7 +464,7 @@ def merge(value_list: List[Any],
|
|
464
464
|
'f': 10
|
465
465
|
}
|
466
466
|
}
|
467
|
-
output = pg.
|
467
|
+
output = pg.utils.merge([original, patch])
|
468
468
|
assert output == {
|
469
469
|
'a': 1,
|
470
470
|
# b is updated.
|
@@ -486,14 +486,12 @@ def merge(value_list: List[Any],
|
|
486
486
|
value. The merge process will keep input values intact.
|
487
487
|
merge_fn: A function to handle value merge that will be called for updated
|
488
488
|
or added keys. If a branch is added/updated, the root of branch will be
|
489
|
-
passed to merge_fn.
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
If
|
494
|
-
|
495
|
-
If final_value is MISSING_VALUE for a path, it will be removed from its
|
496
|
-
parent collection.
|
489
|
+
passed to merge_fn. the signature of function is: `(path, left_value,
|
490
|
+
right_value) -> final_value` If a key is only present in src dict,
|
491
|
+
old_value is MISSING_VALUE; If a key is only present in dest dict,
|
492
|
+
new_value is MISSING_VALUE; otherwise both new_value and old_value are
|
493
|
+
filled. If final_value is MISSING_VALUE for a path, it will be removed
|
494
|
+
from its parent collection.
|
497
495
|
|
498
496
|
Returns:
|
499
497
|
A merged value.
|
@@ -11,12 +11,10 @@
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
|
-
"""Tests for pyglove.object_utils.hierarchical."""
|
15
|
-
|
16
14
|
import unittest
|
17
|
-
from pyglove.core.
|
18
|
-
from pyglove.core.
|
19
|
-
from pyglove.core.
|
15
|
+
from pyglove.core.utils import common_traits
|
16
|
+
from pyglove.core.utils import hierarchical
|
17
|
+
from pyglove.core.utils import value_location
|
20
18
|
|
21
19
|
|
22
20
|
class TraverseTest(unittest.TestCase):
|
@@ -11,13 +11,11 @@
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
|
-
"""Tests for pyglove.object_utils.json_conversion."""
|
15
|
-
|
16
14
|
import abc
|
17
15
|
import typing
|
18
16
|
import unittest
|
19
|
-
from pyglove.core.object_utils import json_conversion
|
20
17
|
from pyglove.core.typing import inspect as pg_inspect
|
18
|
+
from pyglove.core.utils import json_conversion
|
21
19
|
|
22
20
|
|
23
21
|
class X:
|
@@ -14,8 +14,8 @@
|
|
14
14
|
"""Representing missing value for a field."""
|
15
15
|
|
16
16
|
from typing import Any, Dict
|
17
|
-
from pyglove.core.
|
18
|
-
from pyglove.core.
|
17
|
+
from pyglove.core.utils import formatting
|
18
|
+
from pyglove.core.utils import json_conversion
|
19
19
|
|
20
20
|
|
21
21
|
class MissingValue(formatting.Formattable, json_conversion.JSONConvertible):
|
@@ -11,11 +11,9 @@
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
|
-
"""Tests for pyglove.object_utils.missing."""
|
15
|
-
|
16
14
|
import unittest
|
17
|
-
from pyglove.core.
|
18
|
-
from pyglove.core.
|
15
|
+
from pyglove.core.utils import json_conversion
|
16
|
+
from pyglove.core.utils import missing
|
19
17
|
|
20
18
|
|
21
19
|
class MissingValueTest(unittest.TestCase):
|
@@ -11,13 +11,11 @@
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
|
-
"""Tests for pyglove.object_utils.thread_local."""
|
15
|
-
|
16
14
|
import threading
|
17
15
|
import time
|
18
16
|
import unittest
|
19
17
|
|
20
|
-
from pyglove.core.
|
18
|
+
from pyglove.core.utils import thread_local
|
21
19
|
|
22
20
|
|
23
21
|
class ThreadLocalTest(unittest.TestCase):
|
@@ -18,9 +18,9 @@ import dataclasses
|
|
18
18
|
import time
|
19
19
|
from typing import Any, Dict, List, Optional
|
20
20
|
|
21
|
-
from pyglove.core.
|
22
|
-
from pyglove.core.
|
23
|
-
from pyglove.core.
|
21
|
+
from pyglove.core.utils import error_utils
|
22
|
+
from pyglove.core.utils import json_conversion
|
23
|
+
from pyglove.core.utils import thread_local
|
24
24
|
|
25
25
|
|
26
26
|
class TimeIt:
|
@@ -11,12 +11,11 @@
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
|
-
|
15
14
|
import time
|
16
15
|
import unittest
|
17
16
|
|
18
|
-
from pyglove.core.
|
19
|
-
from pyglove.core.
|
17
|
+
from pyglove.core.utils import json_conversion
|
18
|
+
from pyglove.core.utils import timing
|
20
19
|
|
21
20
|
|
22
21
|
class TimeItTest(unittest.TestCase):
|
@@ -17,7 +17,7 @@ import abc
|
|
17
17
|
import copy as copy_lib
|
18
18
|
import operator
|
19
19
|
from typing import Any, Callable, Iterable, Iterator, List, Optional, Union
|
20
|
-
from pyglove.core.
|
20
|
+
from pyglove.core.utils import formatting
|
21
21
|
|
22
22
|
|
23
23
|
class KeyPath(formatting.Formattable):
|
@@ -822,7 +822,7 @@ class StrKey(metaclass=abc.ABCMeta):
|
|
822
822
|
|
823
823
|
Example::
|
824
824
|
|
825
|
-
class MyKey(pg.
|
825
|
+
class MyKey(pg.utils.StrKey):
|
826
826
|
|
827
827
|
def __init__(self, name):
|
828
828
|
self.name = name
|
@@ -11,11 +11,9 @@
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
|
-
"""Tests for pyglove.object_utils.value_location."""
|
15
|
-
|
16
14
|
import unittest
|
17
|
-
from pyglove.core.
|
18
|
-
from pyglove.core.
|
15
|
+
from pyglove.core.utils import formatting
|
16
|
+
from pyglove.core.utils import value_location
|
19
17
|
|
20
18
|
|
21
19
|
KeyPath = value_location.KeyPath
|