langfun 0.1.2.dev202509290805__py3-none-any.whl → 0.1.2.dev202509300805__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.

Potentially problematic release.


This version of langfun might be problematic. Click here for more details.

@@ -23,6 +23,13 @@ import pyglove as pg
23
23
  class EventLogger(pg.Object, base.EventHandler):
24
24
  """Event handler for logging debugger."""
25
25
 
26
+ colored: Annotated[
27
+ bool,
28
+ (
29
+ 'If True, log events with colors.'
30
+ )
31
+ ] = False
32
+
26
33
  regex: Annotated[
27
34
  str | list[str] | None,
28
35
  (
@@ -85,6 +92,13 @@ class EventLogger(pg.Object, base.EventHandler):
85
92
  self._regex_exps = [re.compile(x) for x in regex_exps]
86
93
  self._last_stats_report_time = None
87
94
 
95
+ def _maybe_colored(
96
+ self, message: str, color: str, styles: list[str] | None = None
97
+ ) -> str:
98
+ if self.colored:
99
+ return pg.colored(message, color, styles=styles)
100
+ return message
101
+
88
102
  def _format_message(
89
103
  self,
90
104
  message: str,
@@ -107,6 +121,18 @@ class EventLogger(pg.Object, base.EventHandler):
107
121
  return False
108
122
  return True
109
123
 
124
+ def on_environment_starting(
125
+ self,
126
+ environment: base.Environment,
127
+ ) -> None:
128
+ """Called when the environment is starting."""
129
+ self._print(
130
+ f'[{environment.id}] environment starting',
131
+ error=None,
132
+ color='green',
133
+ styles=['bold'],
134
+ )
135
+
110
136
  def on_environment_start(
111
137
  self,
112
138
  environment: base.Environment,
@@ -151,13 +177,14 @@ class EventLogger(pg.Object, base.EventHandler):
151
177
  def on_environment_shutdown(
152
178
  self,
153
179
  environment: base.Environment,
180
+ duration: float,
154
181
  lifetime: float,
155
182
  error: BaseException | None
156
183
  ) -> None:
157
184
  """Called when the environment is shutdown."""
158
185
  self._print(
159
186
  f'[{environment.id}] environment shutdown '
160
- f'(lifetime={lifetime:.2f} seconds)',
187
+ f'(duration={duration:.2f} seconds), lifetime={lifetime:.2f} seconds)',
161
188
  error=error,
162
189
  color='green',
163
190
  styles=['bold'],
@@ -199,13 +226,15 @@ class EventLogger(pg.Object, base.EventHandler):
199
226
  self,
200
227
  environment: base.Environment,
201
228
  sandbox: base.Sandbox,
229
+ duration: float,
202
230
  lifetime: float,
203
231
  error: BaseException | None
204
232
  ) -> None:
205
233
  if self.sandbox_status:
206
234
  self._print(
207
235
  f'[{sandbox.id}] sandbox shutdown '
208
- f'(lifetime={lifetime:.2f} seconds)',
236
+ f'(duration={duration:.2f} seconds), '
237
+ f'lifetime={lifetime:.2f} seconds)',
209
238
  error=error,
210
239
  color='white',
211
240
  styles=['bold'],
@@ -338,6 +367,7 @@ class EventLogger(pg.Object, base.EventHandler):
338
367
  environment: base.Environment,
339
368
  sandbox: base.Sandbox,
340
369
  session_id: str,
370
+ duration: float,
341
371
  lifetime: float,
342
372
  error: BaseException | None
343
373
  ) -> None:
@@ -345,7 +375,8 @@ class EventLogger(pg.Object, base.EventHandler):
345
375
  if self.session_status:
346
376
  self._print(
347
377
  f'[{sandbox.id}/{session_id}] session ended '
348
- f'(lifetime={lifetime:.2f} seconds)',
378
+ f'(duration={duration:.2f} seconds), '
379
+ f'lifetime={lifetime:.2f} seconds)',
349
380
  error=error,
350
381
  color='blue',
351
382
  )
@@ -394,22 +425,29 @@ class EventLogger(pg.Object, base.EventHandler):
394
425
  color: str | None = None,
395
426
  styles: list[str] | None = None,
396
427
  ):
428
+ message = self._maybe_colored(
429
+ message, color if error is None else 'red', styles=styles
430
+ )
397
431
  if error is not None:
398
- pg.logging.error(pg.colored(message, 'red', styles=styles))
432
+ pg.logging.error(message)
399
433
  else:
400
- pg.logging.info(pg.colored(message, color, styles=styles))
434
+ pg.logging.info(message)
401
435
 
402
436
 
403
437
  class ConsoleEventLogger(EventLogger):
404
438
  """Event handler for console debugger."""
405
439
 
440
+ colored = True
441
+
406
442
  def _write_log(
407
443
  self,
408
444
  message: str,
409
445
  error: BaseException | None,
410
446
  color: str | None = None,
411
- styles: list[str] | None = None,
447
+ styles: list[str] | None = None
412
448
  ):
413
449
  print(
414
- pg.colored(message, color if error is None else 'red', styles=styles)
450
+ self._maybe_colored(
451
+ message, color if error is None else 'red', styles=styles
452
+ )
415
453
  )
@@ -92,6 +92,7 @@ class EventLoggerTest(unittest.TestCase):
92
92
  return self._test_logger(
93
93
  test_name='test_all_flags_on',
94
94
  expected_substrings=[
95
+ 'environment starting',
95
96
  'environment started',
96
97
  'environment shutdown',
97
98
  'environment housekeeping',
@@ -122,6 +123,7 @@ class EventLoggerTest(unittest.TestCase):
122
123
  'RuntimeError',
123
124
  ],
124
125
  unexpected_substrings=[
126
+ 'environment starting',
125
127
  'environment started',
126
128
  'environment shutdown',
127
129
  'environment housekeeping',
@@ -139,6 +141,7 @@ class EventLoggerTest(unittest.TestCase):
139
141
  return self._test_logger(
140
142
  test_name='test_regex',
141
143
  expected_substrings=[
144
+ 'environment starting',
142
145
  'environment started',
143
146
  'environment shutdown',
144
147
  'environment housekeeping',
@@ -161,6 +164,7 @@ class EventLoggerTest(unittest.TestCase):
161
164
  return self._test_logger(
162
165
  test_name='test_sandbox_status_off',
163
166
  expected_substrings=[
167
+ 'environment starting',
164
168
  'environment started',
165
169
  'environment shutdown',
166
170
  'environment housekeeping',
@@ -185,6 +189,7 @@ class EventLoggerTest(unittest.TestCase):
185
189
  return self._test_logger(
186
190
  test_name='test_feature_status_off',
187
191
  expected_substrings=[
192
+ 'environment starting',
188
193
  'environment started',
189
194
  'environment shutdown',
190
195
  'environment housekeeping',
@@ -210,6 +215,7 @@ class EventLoggerTest(unittest.TestCase):
210
215
  return self._test_logger(
211
216
  test_name='test_session_status_off',
212
217
  expected_substrings=[
218
+ 'environment starting',
213
219
  'environment started',
214
220
  'environment shutdown',
215
221
  'environment stats',
@@ -235,6 +241,7 @@ class EventLoggerTest(unittest.TestCase):
235
241
  return self._test_logger(
236
242
  test_name='test_housekeep_status_off',
237
243
  expected_substrings=[
244
+ 'environment starting',
238
245
  'environment started',
239
246
  'environment shutdown',
240
247
  'environment stats',
@@ -260,6 +267,7 @@ class EventLoggerTest(unittest.TestCase):
260
267
  return self._test_logger(
261
268
  test_name='test_housekeep_status_off',
262
269
  expected_substrings=[
270
+ 'environment starting',
263
271
  'environment started',
264
272
  'environment shutdown',
265
273
  'environment housekeeping',