pyglove 0.4.5.dev202501140808__py3-none-any.whl → 0.4.5.dev202501150808__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.
@@ -56,6 +56,16 @@ class TimeIt:
56
56
  **kwargs,
57
57
  )
58
58
 
59
+ def merge(self, other: 'TimeIt.Status') -> 'TimeIt.Status':
60
+ """Merges the status of two `pg.timeit`."""
61
+ assert other.name == self.name, (self.name, other.name)
62
+ return TimeIt.Status(
63
+ name=self.name,
64
+ elapse=self.elapse + other.elapse,
65
+ has_ended=self.has_ended and other.has_ended,
66
+ error=self.error or other.error,
67
+ )
68
+
59
69
  @dataclasses.dataclass
60
70
  class StatusSummary(json_conversion.JSONConvertible):
61
71
  """Aggregated summary for repeated calls for `pg.timeit`."""
@@ -125,7 +135,7 @@ class TimeIt:
125
135
  self._name: str = name
126
136
  self._start_time: Optional[float] = None
127
137
  self._end_time: Optional[float] = None
128
- self._child_contexts: Dict[str, TimeIt] = {}
138
+ self._child_contexts: List[TimeIt] = []
129
139
  self._error: Optional[error_utils.ErrorInfo] = None
130
140
  self._parent: Optional[TimeIt] = None
131
141
 
@@ -137,13 +147,11 @@ class TimeIt:
137
147
  @property
138
148
  def children(self) -> List['TimeIt']:
139
149
  """Returns child contexts."""
140
- return list(self._child_contexts.values())
150
+ return self._child_contexts
141
151
 
142
152
  def add(self, context: 'TimeIt'):
143
153
  """Adds a child context."""
144
- if context.name in self._child_contexts:
145
- raise ValueError(f'`timeit` with name {context.name!r} already exists.')
146
- self._child_contexts[context.name] = context
154
+ self._child_contexts.append(context)
147
155
 
148
156
  def start(self):
149
157
  """Starts timing."""
@@ -206,11 +214,14 @@ class TimeIt:
206
214
  has_ended=self.has_ended, error=self._error,
207
215
  )
208
216
  }
209
- for child in self._child_contexts.values():
217
+ for child in self._child_contexts:
210
218
  child_result = child.status()
211
219
  for k, v in child_result.items():
212
220
  key = f'{self.name}.{k}' if self.name else k
213
- result[key] = v
221
+ if key in result:
222
+ result[key] = result[key].merge(v)
223
+ else:
224
+ result[key] = v
214
225
  return result
215
226
 
216
227
  def __enter__(self):
@@ -62,9 +62,9 @@ class TimeItTest(unittest.TestCase):
62
62
  self.assertFalse(r['node.child'].has_ended)
63
63
  self.assertTrue(r['node.child.grandchild'].has_ended)
64
64
 
65
- with self.assertRaisesRegex(ValueError, '.* already exists'):
66
- with timing.timeit('grandchild'):
67
- pass
65
+ with timing.timeit('grandchild') as t3:
66
+ time.sleep(0.5)
67
+ self.assertEqual(t1.children, [t2, t3])
68
68
 
69
69
  elapse2 = t.elapse
70
70
  self.assertTrue(t.has_ended)
@@ -72,17 +72,20 @@ class TimeItTest(unittest.TestCase):
72
72
  time.sleep(0.5)
73
73
  self.assertEqual(elapse2, t.elapse)
74
74
 
75
- statuss = t.status()
75
+ status = t.status()
76
76
  self.assertEqual(
77
- list(statuss.keys()),
77
+ list(status.keys()),
78
78
  ['node', 'node.child', 'node.child.grandchild']
79
79
  )
80
80
  self.assertEqual(
81
- sorted([v.elapse for v in statuss.values()], reverse=True),
82
- [v.elapse for v in statuss.values()],
81
+ status['node.child.grandchild'].elapse, t2.elapse + t3.elapse
82
+ )
83
+ self.assertEqual(
84
+ sorted([v.elapse for v in status.values()], reverse=True),
85
+ [v.elapse for v in status.values()],
83
86
  )
84
- self.assertTrue(all(v.has_ended for v in statuss.values()))
85
- self.assertFalse(any(v.has_error for v in statuss.values()))
87
+ self.assertTrue(all(v.has_ended for v in status.values()))
88
+ self.assertFalse(any(v.has_error for v in status.values()))
86
89
  status = t.status()
87
90
  json_dict = json_conversion.to_json(status)
88
91
  status2 = json_conversion.from_json(json_dict)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: pyglove
3
- Version: 0.4.5.dev202501140808
3
+ Version: 0.4.5.dev202501150808
4
4
  Summary: PyGlove: A library for manipulating Python objects.
5
5
  Home-page: https://github.com/google/pyglove
6
6
  Author: PyGlove Authors
@@ -146,8 +146,8 @@ pyglove/core/utils/text_color.py,sha256=xcCTCxY2qFNZs_jismMGus8scEXKBpYGAhpAgnz-
146
146
  pyglove/core/utils/text_color_test.py,sha256=3HtZpUB5XPr7A_5Fg4ZSMfNWeDRiQgSzmg9b1tctMI4,2801
147
147
  pyglove/core/utils/thread_local.py,sha256=i-CnyY3VREtLfAj4_JndBnsKuQLIgwG29ma8dAyRxbI,4839
148
148
  pyglove/core/utils/thread_local_test.py,sha256=AOqsdNsA-cYMvJckqxb25ql3Y5kDW0qLfBW1cl85Bnw,6757
149
- pyglove/core/utils/timing.py,sha256=fXdwEYXR84qZBC7hxbXbwztW2UKliJZLqCDvhz1qw_A,7241
150
- pyglove/core/utils/timing_test.py,sha256=OZxi551MJec9cKtpNbeep6jjzP68JUHhSmRHNKDFRMw,4928
149
+ pyglove/core/utils/timing.py,sha256=gnHCA2IJ9DZ3Y0vb8ItB1Ap8ss8j_zN0OMUAdhoGy2Y,7550
150
+ pyglove/core/utils/timing_test.py,sha256=t_awsYs5SqEO3_2u6HDbVxRcSxSfHsE4zMbFG-1ErZw,5011
151
151
  pyglove/core/utils/value_location.py,sha256=wAryIwQeEfrRddyGRk-KbLA7dnNDLhL-dSyFI9wIG5U,26756
152
152
  pyglove/core/utils/value_location_test.py,sha256=X6Gih3IoYugziwXWH8VGz5bPeb3Kq0CfZxwbNVIsZJo,21338
153
153
  pyglove/core/views/__init__.py,sha256=gll9ZBRYz4p_-LWOdzSR2a6UTWcJ8nR430trrP0yLCU,967
@@ -207,8 +207,8 @@ pyglove/ext/scalars/randoms.py,sha256=LkMIIx7lOq_lvJvVS3BrgWGuWl7Pi91-lA-O8x_gZs
207
207
  pyglove/ext/scalars/randoms_test.py,sha256=nEhiqarg8l_5EOucp59CYrpO2uKxS1pe0hmBdZUzRNM,2000
208
208
  pyglove/ext/scalars/step_wise.py,sha256=IDw3tuTpv0KVh7AN44W43zqm1-E0HWPUlytWOQC9w3Y,3789
209
209
  pyglove/ext/scalars/step_wise_test.py,sha256=TL1vJ19xVx2t5HKuyIzGoogF7N3Rm8YhLE6JF7i0iy8,2540
210
- pyglove-0.4.5.dev202501140808.dist-info/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
211
- pyglove-0.4.5.dev202501140808.dist-info/METADATA,sha256=_HkFXhfIYMiYBTTw47VFpp0lnLW5yojjf9NomI1d3II,7067
212
- pyglove-0.4.5.dev202501140808.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
213
- pyglove-0.4.5.dev202501140808.dist-info/top_level.txt,sha256=wITzJSKcj8GZUkbq-MvUQnFadkiuAv_qv5qQMw0fIow,8
214
- pyglove-0.4.5.dev202501140808.dist-info/RECORD,,
210
+ pyglove-0.4.5.dev202501150808.dist-info/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
211
+ pyglove-0.4.5.dev202501150808.dist-info/METADATA,sha256=djfHeOlrh9nRfIY2ZQghYSlCVa_bBXxNOpjHCXPcpwE,7067
212
+ pyglove-0.4.5.dev202501150808.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
213
+ pyglove-0.4.5.dev202501150808.dist-info/top_level.txt,sha256=wITzJSKcj8GZUkbq-MvUQnFadkiuAv_qv5qQMw0fIow,8
214
+ pyglove-0.4.5.dev202501150808.dist-info/RECORD,,