langfun 0.1.2.dev202509290805__py3-none-any.whl → 0.1.2.dev202510010805__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,13 +92,22 @@ 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,
91
105
  error: BaseException | None,
92
106
  ) -> str:
93
107
  if error is not None:
94
- message = f'{message} with error: {error}'
108
+ message = (
109
+ f'{message} with error: {pg.utils.ErrorInfo.from_exception(error)}'
110
+ )
95
111
  return message
96
112
 
97
113
  def _keep(
@@ -107,6 +123,32 @@ class EventLogger(pg.Object, base.EventHandler):
107
123
  return False
108
124
  return True
109
125
 
126
+ def on_environment_starting(
127
+ self,
128
+ environment: base.Environment,
129
+ ) -> None:
130
+ """Called when the environment is starting."""
131
+ self._print(
132
+ f'[{environment.id}] environment starting',
133
+ error=None,
134
+ color='green',
135
+ styles=['bold'],
136
+ )
137
+
138
+ def on_environment_shutting_down(
139
+ self,
140
+ environment: base.Environment,
141
+ offline_duration: float,
142
+ ) -> None:
143
+ """Called when the environment is shutting down."""
144
+ self._print(
145
+ f'[{environment.id}] environment shutting down '
146
+ f'(offline_duration={offline_duration:.2f} seconds)',
147
+ error=None,
148
+ color='green',
149
+ styles=['bold'],
150
+ )
151
+
110
152
  def on_environment_start(
111
153
  self,
112
154
  environment: base.Environment,
@@ -127,13 +169,15 @@ class EventLogger(pg.Object, base.EventHandler):
127
169
  environment: base.Environment,
128
170
  counter: int,
129
171
  duration: float,
130
- error: BaseException | None
172
+ error: BaseException | None,
173
+ **kwargs
131
174
  ) -> None:
132
175
  """Called when the environment is housekeeping."""
133
176
  if self.housekeep_status:
134
177
  self._print(
135
- f'[{environment.id}] environment housekeeping complete'
136
- f'(counter={counter}, duration={duration:.2f} seconds)',
178
+ f'[{environment.id}] environment housekeeping complete '
179
+ f'(counter={counter}, duration={duration:.2f} seconds, '
180
+ f'housekeep_info={kwargs})',
137
181
  error=error,
138
182
  color='green',
139
183
  )
@@ -151,13 +195,14 @@ class EventLogger(pg.Object, base.EventHandler):
151
195
  def on_environment_shutdown(
152
196
  self,
153
197
  environment: base.Environment,
198
+ duration: float,
154
199
  lifetime: float,
155
200
  error: BaseException | None
156
201
  ) -> None:
157
202
  """Called when the environment is shutdown."""
158
203
  self._print(
159
204
  f'[{environment.id}] environment shutdown '
160
- f'(lifetime={lifetime:.2f} seconds)',
205
+ f'(duration={duration:.2f} seconds), lifetime={lifetime:.2f} seconds)',
161
206
  error=error,
162
207
  color='green',
163
208
  styles=['bold'],
@@ -199,13 +244,15 @@ class EventLogger(pg.Object, base.EventHandler):
199
244
  self,
200
245
  environment: base.Environment,
201
246
  sandbox: base.Sandbox,
247
+ duration: float,
202
248
  lifetime: float,
203
249
  error: BaseException | None
204
250
  ) -> None:
205
251
  if self.sandbox_status:
206
252
  self._print(
207
253
  f'[{sandbox.id}] sandbox shutdown '
208
- f'(lifetime={lifetime:.2f} seconds)',
254
+ f'(duration={duration:.2f} seconds), '
255
+ f'lifetime={lifetime:.2f} seconds)',
209
256
  error=error,
210
257
  color='white',
211
258
  styles=['bold'],
@@ -217,13 +264,15 @@ class EventLogger(pg.Object, base.EventHandler):
217
264
  sandbox: base.Sandbox,
218
265
  counter: int,
219
266
  duration: float,
220
- error: BaseException | None
267
+ error: BaseException | None,
268
+ **kwargs
221
269
  ) -> None:
222
270
  """Called when a sandbox feature is housekeeping."""
223
271
  if self.sandbox_status and self.housekeep_status:
224
272
  self._print(
225
273
  f'[{sandbox.id}] sandbox housekeeping complete '
226
- f'(counter={counter}, duration={duration:.2f} seconds)',
274
+ f'(counter={counter}, duration={duration:.2f} seconds, '
275
+ f'housekeep_info={kwargs})',
227
276
  error=error,
228
277
  color='white',
229
278
  )
@@ -305,13 +354,15 @@ class EventLogger(pg.Object, base.EventHandler):
305
354
  feature: base.Feature,
306
355
  counter: int,
307
356
  duration: float,
308
- error: BaseException | None
357
+ error: BaseException | None,
358
+ **kwargs
309
359
  ) -> None:
310
360
  """Called when a sandbox feature is housekeeping."""
311
361
  if self.feature_status and self.housekeep_status:
312
362
  self._print(
313
363
  f'[{sandbox.id}/<idle>/{feature.name}] feature housekeeping complete '
314
- f'(counter={counter}, (duration={duration:.2f} seconds)',
364
+ f'(counter={counter}, (duration={duration:.2f} seconds, '
365
+ f'housekeep_info={kwargs})',
315
366
  error=error,
316
367
  color='white',
317
368
  )
@@ -338,6 +389,7 @@ class EventLogger(pg.Object, base.EventHandler):
338
389
  environment: base.Environment,
339
390
  sandbox: base.Sandbox,
340
391
  session_id: str,
392
+ duration: float,
341
393
  lifetime: float,
342
394
  error: BaseException | None
343
395
  ) -> None:
@@ -345,7 +397,8 @@ class EventLogger(pg.Object, base.EventHandler):
345
397
  if self.session_status:
346
398
  self._print(
347
399
  f'[{sandbox.id}/{session_id}] session ended '
348
- f'(lifetime={lifetime:.2f} seconds)',
400
+ f'(duration={duration:.2f} seconds), '
401
+ f'lifetime={lifetime:.2f} seconds)',
349
402
  error=error,
350
403
  color='blue',
351
404
  )
@@ -394,22 +447,29 @@ class EventLogger(pg.Object, base.EventHandler):
394
447
  color: str | None = None,
395
448
  styles: list[str] | None = None,
396
449
  ):
450
+ message = self._maybe_colored(
451
+ message, color if error is None else 'red', styles=styles
452
+ )
397
453
  if error is not None:
398
- pg.logging.error(pg.colored(message, 'red', styles=styles))
454
+ pg.logging.error(message)
399
455
  else:
400
- pg.logging.info(pg.colored(message, color, styles=styles))
456
+ pg.logging.info(message)
401
457
 
402
458
 
403
459
  class ConsoleEventLogger(EventLogger):
404
460
  """Event handler for console debugger."""
405
461
 
462
+ colored = True
463
+
406
464
  def _write_log(
407
465
  self,
408
466
  message: str,
409
467
  error: BaseException | None,
410
468
  color: str | None = None,
411
- styles: list[str] | None = None,
469
+ styles: list[str] | None = None
412
470
  ):
413
471
  print(
414
- pg.colored(message, color if error is None else 'red', styles=styles)
472
+ self._maybe_colored(
473
+ message, color if error is None else 'red', styles=styles
474
+ )
415
475
  )
@@ -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,7 +123,9 @@ class EventLoggerTest(unittest.TestCase):
122
123
  'RuntimeError',
123
124
  ],
124
125
  unexpected_substrings=[
126
+ 'environment starting',
125
127
  'environment started',
128
+ 'environment shutting down',
126
129
  'environment shutdown',
127
130
  'environment housekeeping',
128
131
  'sandbox started',
@@ -139,7 +142,9 @@ class EventLoggerTest(unittest.TestCase):
139
142
  return self._test_logger(
140
143
  test_name='test_regex',
141
144
  expected_substrings=[
145
+ 'environment starting',
142
146
  'environment started',
147
+ 'environment shutting down',
143
148
  'environment shutdown',
144
149
  'environment housekeeping',
145
150
  ],
@@ -161,7 +166,9 @@ class EventLoggerTest(unittest.TestCase):
161
166
  return self._test_logger(
162
167
  test_name='test_sandbox_status_off',
163
168
  expected_substrings=[
169
+ 'environment starting',
164
170
  'environment started',
171
+ 'environment shutting down',
165
172
  'environment shutdown',
166
173
  'environment housekeeping',
167
174
  'feature setup complete',
@@ -185,7 +192,9 @@ class EventLoggerTest(unittest.TestCase):
185
192
  return self._test_logger(
186
193
  test_name='test_feature_status_off',
187
194
  expected_substrings=[
195
+ 'environment starting',
188
196
  'environment started',
197
+ 'environment shutting down',
189
198
  'environment shutdown',
190
199
  'environment housekeeping',
191
200
  'environment stats',
@@ -210,7 +219,9 @@ class EventLoggerTest(unittest.TestCase):
210
219
  return self._test_logger(
211
220
  test_name='test_session_status_off',
212
221
  expected_substrings=[
222
+ 'environment starting',
213
223
  'environment started',
224
+ 'environment shutting down',
214
225
  'environment shutdown',
215
226
  'environment stats',
216
227
  'environment housekeeping',
@@ -235,7 +246,9 @@ class EventLoggerTest(unittest.TestCase):
235
246
  return self._test_logger(
236
247
  test_name='test_housekeep_status_off',
237
248
  expected_substrings=[
249
+ 'environment starting',
238
250
  'environment started',
251
+ 'environment shutting down',
239
252
  'environment shutdown',
240
253
  'environment stats',
241
254
  'sandbox started',
@@ -260,7 +273,9 @@ class EventLoggerTest(unittest.TestCase):
260
273
  return self._test_logger(
261
274
  test_name='test_housekeep_status_off',
262
275
  expected_substrings=[
276
+ 'environment starting',
263
277
  'environment started',
278
+ 'environment shutting down',
264
279
  'environment shutdown',
265
280
  'environment housekeeping',
266
281
  ],