langfun 0.1.2.dev202510310805__py3-none-any.whl → 0.1.2.dev202511010804__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.

langfun/env/interface.py CHANGED
@@ -238,6 +238,11 @@ class Environment(pg.Object):
238
238
  f'No image ID found for feature {feature.name} in {self.image_ids}.'
239
239
  )
240
240
 
241
+ @property
242
+ @abc.abstractmethod
243
+ def event_handler(self) -> 'EventHandler':
244
+ """Returns the event handler for the environment."""
245
+
241
246
  @property
242
247
  @abc.abstractmethod
243
248
  def status(self) -> Status:
@@ -306,10 +311,6 @@ class Environment(pg.Object):
306
311
  Environment._ENV_STACK.pop()
307
312
  self.shutdown()
308
313
 
309
- def __del__(self):
310
- """Deletes the environment."""
311
- self.shutdown()
312
-
313
314
  @classmethod
314
315
  def current(cls) -> Optional['Environment']:
315
316
  """Returns the current environment."""
@@ -776,10 +777,6 @@ class Sandbox(pg.Object):
776
777
  return self.features[name]
777
778
  raise AttributeError(name)
778
779
 
779
- def __del__(self):
780
- """Deletes the sandbox."""
781
- self.shutdown()
782
-
783
780
 
784
781
  class Feature(pg.Object):
785
782
  """Interface for sandbox features."""
@@ -792,6 +789,11 @@ class Feature(pg.Object):
792
789
  def name(self) -> str:
793
790
  """Name of the feature, which will be used as key to access the feature."""
794
791
 
792
+ @property
793
+ @abc.abstractmethod
794
+ def environment(self) -> Environment:
795
+ """Returns the environment that the feature is running in."""
796
+
795
797
  @property
796
798
  @abc.abstractmethod
797
799
  def sandbox(self) -> Sandbox:
@@ -1008,3 +1010,337 @@ def log_sandbox_activity(name: str | None = None):
1008
1010
  return func(self, *args, **kwargs)
1009
1011
  return method_wrapper
1010
1012
  return decorator
1013
+
1014
+
1015
+ #
1016
+ # Interface for Environment event handlers.
1017
+ #
1018
+
1019
+
1020
+ class _SessionEventHandler:
1021
+ """Base class for session event handlers."""
1022
+
1023
+ def on_session_start(
1024
+ self,
1025
+ environment: Environment,
1026
+ sandbox: Sandbox,
1027
+ session_id: str,
1028
+ duration: float,
1029
+ error: BaseException | None
1030
+ ) -> None:
1031
+ """Called when a sandbox session starts.
1032
+
1033
+ Args:
1034
+ environment: The environment.
1035
+ sandbox: The sandbox.
1036
+ session_id: The session ID.
1037
+ duration: The time spent on starting the session.
1038
+ error: The error that caused the session to start. If None, the session
1039
+ started normally.
1040
+ """
1041
+
1042
+ def on_session_end(
1043
+ self,
1044
+ environment: Environment,
1045
+ sandbox: Sandbox,
1046
+ session_id: str,
1047
+ duration: float,
1048
+ lifetime: float,
1049
+ error: BaseException | None
1050
+ ) -> None:
1051
+ """Called when a sandbox session ends.
1052
+
1053
+ Args:
1054
+ environment: The environment.
1055
+ sandbox: The sandbox.
1056
+ session_id: The session ID.
1057
+ duration: The time spent on ending the session.
1058
+ lifetime: The session lifetime in seconds.
1059
+ error: The error that caused the session to end. If None, the session
1060
+ ended normally.
1061
+ """
1062
+
1063
+
1064
+ class _FeatureEventHandler:
1065
+ """Base class for feature event handlers."""
1066
+
1067
+ def on_feature_setup(
1068
+ self,
1069
+ environment: Environment,
1070
+ sandbox: Sandbox,
1071
+ feature: Feature,
1072
+ duration: float,
1073
+ error: BaseException | None
1074
+ ) -> None:
1075
+ """Called when a sandbox feature is setup.
1076
+
1077
+ Args:
1078
+ environment: The environment.
1079
+ sandbox: The sandbox.
1080
+ feature: The feature.
1081
+ duration: The feature setup duration in seconds.
1082
+ error: The error happened during the feature setup. If None,
1083
+ the feature setup performed normally.
1084
+ """
1085
+
1086
+ def on_feature_teardown(
1087
+ self,
1088
+ environment: Environment,
1089
+ sandbox: Sandbox,
1090
+ feature: Feature,
1091
+ duration: float,
1092
+ error: BaseException | None
1093
+ ) -> None:
1094
+ """Called when a sandbox feature is teardown.
1095
+
1096
+ Args:
1097
+ environment: The environment.
1098
+ sandbox: The sandbox.
1099
+ feature: The feature.
1100
+ duration: The feature teardown duration in seconds.
1101
+ error: The error happened during the feature teardown. If None,
1102
+ the feature teardown performed normally.
1103
+ """
1104
+
1105
+ def on_feature_teardown_session(
1106
+ self,
1107
+ environment: Environment,
1108
+ sandbox: Sandbox,
1109
+ feature: Feature,
1110
+ session_id: str,
1111
+ duration: float,
1112
+ error: BaseException | None
1113
+ ) -> None:
1114
+ """Called when a feature is teardown with a session.
1115
+
1116
+ Args:
1117
+ environment: The environment.
1118
+ sandbox: The sandbox.
1119
+ feature: The feature.
1120
+ session_id: The session ID.
1121
+ duration: The feature teardown session duration in seconds.
1122
+ error: The error happened during the feature teardown session. If
1123
+ None, the feature teardown session performed normally.
1124
+ """
1125
+
1126
+ def on_feature_setup_session(
1127
+ self,
1128
+ environment: Environment,
1129
+ sandbox: Sandbox,
1130
+ feature: Feature,
1131
+ session_id: str | None,
1132
+ duration: float,
1133
+ error: BaseException | None,
1134
+ ) -> None:
1135
+ """Called when a feature is setup with a session.
1136
+
1137
+ Args:
1138
+ environment: The environment.
1139
+ sandbox: The sandbox.
1140
+ feature: The feature.
1141
+ session_id: The session ID.
1142
+ duration: The feature setup session duration in seconds.
1143
+ error: The error happened during the feature setup session. If
1144
+ None, the feature setup session performed normally.
1145
+ """
1146
+
1147
+ def on_feature_housekeep(
1148
+ self,
1149
+ environment: Environment,
1150
+ sandbox: Sandbox,
1151
+ feature: Feature,
1152
+ counter: int,
1153
+ duration: float,
1154
+ error: BaseException | None,
1155
+ **kwargs,
1156
+ ) -> None:
1157
+ """Called when a sandbox feature is housekeeping.
1158
+
1159
+ Args:
1160
+ environment: The environment.
1161
+ sandbox: The sandbox.
1162
+ feature: The feature.
1163
+ counter: Zero-based counter of the housekeeping round.
1164
+ duration: The feature housekeeping duration in seconds.
1165
+ error: The error happened during the feature housekeeping. If None, the
1166
+ feature housekeeping normally.
1167
+ **kwargs: Feature-specific properties computed during housekeeping.
1168
+ """
1169
+
1170
+
1171
+ class _SandboxEventHandler(_FeatureEventHandler, _SessionEventHandler):
1172
+ """Base class for sandbox event handlers."""
1173
+
1174
+ def on_sandbox_start(
1175
+ self,
1176
+ environment: Environment,
1177
+ sandbox: Sandbox,
1178
+ duration: float,
1179
+ error: BaseException | None
1180
+ ) -> None:
1181
+ """Called when a sandbox is started.
1182
+
1183
+ Args:
1184
+ environment: The environment.
1185
+ sandbox: The sandbox.
1186
+ duration: The time spent on starting the sandbox.
1187
+ error: The error that caused the sandbox to start. If None, the sandbox
1188
+ started normally.
1189
+ """
1190
+
1191
+ def on_sandbox_status_change(
1192
+ self,
1193
+ environment: Environment,
1194
+ sandbox: Sandbox,
1195
+ old_status: 'Sandbox.Status',
1196
+ new_status: 'Sandbox.Status',
1197
+ span: float,
1198
+ ) -> None:
1199
+ """Called when a sandbox status changes.
1200
+
1201
+ Args:
1202
+ environment: The environment.
1203
+ sandbox: The sandbox.
1204
+ old_status: The old sandbox status.
1205
+ new_status: The new sandbox status.
1206
+ span: Time spent on the old status in seconds.
1207
+ """
1208
+
1209
+ def on_sandbox_shutdown(
1210
+ self,
1211
+ environment: Environment,
1212
+ sandbox: Sandbox,
1213
+ duration: float,
1214
+ lifetime: float,
1215
+ error: BaseException | None
1216
+ ) -> None:
1217
+ """Called when a sandbox is shutdown.
1218
+
1219
+ Args:
1220
+ environment: The environment.
1221
+ sandbox: The sandbox.
1222
+ duration: The time spent on shutting down the sandbox.
1223
+ lifetime: The sandbox lifetime in seconds.
1224
+ error: The error that caused the sandbox to shutdown. If None, the
1225
+ sandbox shutdown normally.
1226
+ """
1227
+
1228
+ def on_sandbox_activity(
1229
+ self,
1230
+ name: str,
1231
+ environment: Environment,
1232
+ sandbox: Sandbox,
1233
+ feature: Feature | None,
1234
+ session_id: str | None,
1235
+ duration: float,
1236
+ error: BaseException | None,
1237
+ **kwargs
1238
+ ) -> None:
1239
+ """Called when a sandbox activity is performed.
1240
+
1241
+ Args:
1242
+ name: The name of the sandbox activity.
1243
+ environment: The environment.
1244
+ sandbox: The sandbox.
1245
+ feature: The feature that is associated with the sandbox activity.
1246
+ session_id: The session ID.
1247
+ duration: The sandbox activity duration in seconds.
1248
+ error: The error that caused the sandbox activity to perform. If None,
1249
+ the sandbox activity performed normally.
1250
+ **kwargs: The keyword arguments of the sandbox activity.
1251
+ """
1252
+
1253
+ def on_sandbox_housekeep(
1254
+ self,
1255
+ environment: Environment,
1256
+ sandbox: Sandbox,
1257
+ counter: int,
1258
+ duration: float,
1259
+ error: BaseException | None,
1260
+ **kwargs
1261
+ ) -> None:
1262
+ """Called when a sandbox finishes a round of housekeeping.
1263
+
1264
+ Args:
1265
+ environment: The environment.
1266
+ sandbox: The sandbox.
1267
+ counter: Zero-based counter of the housekeeping round.
1268
+ duration: The sandbox housekeeping duration in seconds.
1269
+ error: The error that caused the sandbox to housekeeping. If None, the
1270
+ sandbox housekeeping normally.
1271
+ **kwargs: Sandbox-specific properties computed during housekeeping.
1272
+ """
1273
+
1274
+
1275
+ class EventHandler(_SandboxEventHandler):
1276
+ """Base class for event handlers of an environment."""
1277
+
1278
+ def on_environment_starting(self, environment: Environment) -> None:
1279
+ """Called when the environment is getting started.
1280
+
1281
+ Args:
1282
+ environment: The environment.
1283
+ """
1284
+
1285
+ def on_environment_start(
1286
+ self,
1287
+ environment: Environment,
1288
+ duration: float,
1289
+ error: BaseException | None
1290
+ ) -> None:
1291
+ """Called when the environment is started.
1292
+
1293
+ Args:
1294
+ environment: The environment.
1295
+ duration: The environment start duration in seconds.
1296
+ error: The error that failed the environment start. If None, the
1297
+ environment started normally.
1298
+ """
1299
+
1300
+ def on_environment_housekeep(
1301
+ self,
1302
+ environment: Environment,
1303
+ counter: int,
1304
+ duration: float,
1305
+ error: BaseException | None,
1306
+ **kwargs
1307
+ ) -> None:
1308
+ """Called when the environment finishes a round of housekeeping.
1309
+
1310
+ Args:
1311
+ environment: The environment.
1312
+ counter: Zero-based counter of the housekeeping round.
1313
+ duration: The environment start duration in seconds.
1314
+ error: The error that failed the housekeeping. If None, the
1315
+ housekeeping succeeded.
1316
+ **kwargs: Environment-specific properties computed during housekeeping.
1317
+ """
1318
+
1319
+ def on_environment_shutting_down(
1320
+ self,
1321
+ environment: Environment,
1322
+ offline_duration: float,
1323
+ ) -> None:
1324
+ """Called when the environment is shutting down.
1325
+
1326
+ Args:
1327
+ environment: The environment.
1328
+ offline_duration: The environment offline duration in seconds.
1329
+ """
1330
+
1331
+ def on_environment_shutdown(
1332
+ self,
1333
+ environment: Environment,
1334
+ duration: float,
1335
+ lifetime: float,
1336
+ error: BaseException | None
1337
+ ) -> None:
1338
+ """Called when the environment is shutdown.
1339
+
1340
+ Args:
1341
+ environment: The environment.
1342
+ duration: The environment shutdown duration in seconds.
1343
+ lifetime: The environment lifetime in seconds.
1344
+ error: The error that caused the environment to shutdown. If None, the
1345
+ environment shutdown normally.
1346
+ """
langfun/env/test_utils.py CHANGED
@@ -21,7 +21,6 @@ from langfun.env import base_environment
21
21
  from langfun.env import base_feature
22
22
  from langfun.env import base_sandbox
23
23
  from langfun.env import interface
24
- from langfun.env.event_handlers import base as event_handler_base
25
24
  import pyglove as pg
26
25
 
27
26
 
@@ -221,9 +220,7 @@ class TestingFeature(base_feature.BaseFeature):
221
220
  )
222
221
 
223
222
 
224
- class TestingEventHandler(
225
- pg.Object, event_handler_base.EventHandler
226
- ):
223
+ class TestingEventHandler(pg.Object, interface.EventHandler):
227
224
  """Testing environment event handler for unit tests."""
228
225
 
229
226
  log_sandbox_status: bool = False
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: langfun
3
- Version: 0.1.2.dev202510310805
3
+ Version: 0.1.2.dev202511010804
4
4
  Summary: Langfun: Language as Functions.
5
5
  Home-page: https://github.com/google/langfun
6
6
  Author: Langfun Authors
@@ -174,24 +174,26 @@ langfun/core/templates/demonstration.py,sha256=vCrgYubdZM5Umqcgp8NUVGXgr4P_c-fik
174
174
  langfun/core/templates/demonstration_test.py,sha256=SafcDQ0WgI7pw05EmPI2S4v1t3ABKzup8jReCljHeK4,2162
175
175
  langfun/core/templates/selfplay.py,sha256=yhgrJbiYwq47TgzThmHrDQTF4nDrTI09CWGhuQPNv-s,2273
176
176
  langfun/core/templates/selfplay_test.py,sha256=Ot__1P1M8oJfoTp-M9-PQ6HUXqZKyMwvZ5f7yQ3yfyM,2326
177
- langfun/env/__init__.py,sha256=VTjLmS_SkxtkBCmr6hBb4iACeLLPmcbWJkK7MEXsATA,1652
178
- langfun/env/base_environment.py,sha256=8Cpwb4D37zHj5AK4qgmGIPyxIMEDxTIPHXqxTSNkE_M,25030
179
- langfun/env/base_feature.py,sha256=JDEhL9LkbBHB0c603guchry7cy_zaIReg5vqExyQQgg,6902
180
- langfun/env/base_sandbox.py,sha256=0dduNozBtPqawvSX-pv6sEweDNTi89lrT_5qKk5FWak,30656
181
- langfun/env/base_test.py,sha256=6yHtExP3Soa-l0ug73ZSV9ZfS0B4vu-uaS_K_xvsDwk,74261
182
- langfun/env/interface.py,sha256=2X-gGD41cnejde5DIQ7QxgW3r85oKP4Gp0iWP-ABmLM,32126
177
+ langfun/env/__init__.py,sha256=EOKAU4CrlUAQtGORBaXXP1q_fAS6NHd6rIMOnRwWrjg,1688
178
+ langfun/env/base_environment.py,sha256=vU0IL6-Fmx7xAYeefRLPSjMfwuhCz5b23h5T1HeILTk,24742
179
+ langfun/env/base_environment_test.py,sha256=zXaCMA6JrWFtit68SESx_jGWzO6h7NQZpux4FqCMueQ,15432
180
+ langfun/env/base_feature.py,sha256=5vC-zXYRaImh_nAc0mkgV9vW4ObKYarLUepQeCVAEMw,8189
181
+ langfun/env/base_sandbox.py,sha256=-TZZ4JeQ5PaH0qhfF_HxPPosAHAJLfyD7rGETrbyYhQ,28015
182
+ langfun/env/base_sandbox_test.py,sha256=zs3HL90dyp2YybEhS7splyQNmODSO8J1B8BT8KUBlAY,59450
183
+ langfun/env/interface.py,sha256=zjmrXJcN2cV7a-Ja3V1b-JdpKA11ajWQqk46obHxfsI,41656
183
184
  langfun/env/interface_test.py,sha256=d8vdXL1PkrNQGwzfEI2r6esd6SBnTMgc-3GNArsnuj4,3295
184
185
  langfun/env/load_balancers.py,sha256=qRhCthqzjZIQBwta8qC1C0s0J-VQAVomJQqI7Nqv-r4,1948
185
186
  langfun/env/load_balancers_test.py,sha256=Bg0h-AL7LpWb_aixFXs_FpgQp4dWLvodcsQj-mys6zs,4125
186
- langfun/env/test_utils.py,sha256=9fqhxxKERiyAte9bbtwqXqZ1ihNGyuOdCiPPY8G0I8w,14171
187
- langfun/env/event_handlers/__init__.py,sha256=H34n-TbKSgtxqBhE-yAti8fY6weF2_v3yw59M9_zmGM,443
188
- langfun/env/event_handlers/base.py,sha256=eGdJ6N5em9kX-c9wzm1TdnRP5_5IAltX5JTHILdjzLM,10124
189
- langfun/env/event_handlers/event_logger.py,sha256=3dbPjBe53dBgntYHlyLlj_77hVecPSXkmKeiGXMxlO0,12699
190
- langfun/env/event_handlers/event_logger_test.py,sha256=ryupxaEP9D8wdtSsSwZRSZwqFaHCaSD-bFSea_T7QJo,9133
191
- langfun/env/event_handlers/metric_writer.py,sha256=F_Gk1lpJX5SZ6-Hyrf_-utf4gvSKvMmcov8VkKogZCU,19618
192
- langfun/env/event_handlers/metric_writer_test.py,sha256=sntUifTPHGixUshIgVBX4Q9vJL-xbeS0Cpd5X5hSQyQ,5955
193
- langfun-0.1.2.dev202510310805.dist-info/licenses/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
194
- langfun-0.1.2.dev202510310805.dist-info/METADATA,sha256=IKnFNK0k4M96hXuSz3sVo6QOFf9z36tKYsRaeJNvKqg,7522
195
- langfun-0.1.2.dev202510310805.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
196
- langfun-0.1.2.dev202510310805.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
197
- langfun-0.1.2.dev202510310805.dist-info/RECORD,,
187
+ langfun/env/test_utils.py,sha256=i6OadjYfLl5IBrDhEzWiJWsq_GWlplHnrBDD0LEB_HM,14090
188
+ langfun/env/event_handlers/__init__.py,sha256=EE3pV0BlhvYfTG3njhVTMqj88IO_gwjLqZDGSGL95DE,449
189
+ langfun/env/event_handlers/chain.py,sha256=elOAeY38rL0PzNKBq-jwsEqb6ZLE0aOI0LkRSQlQofs,7608
190
+ langfun/env/event_handlers/chain_test.py,sha256=FI0hnDdzmyPe6nEJDeVDrczM49SL9Mhch3STCtgNp7w,8657
191
+ langfun/env/event_handlers/event_logger.py,sha256=J5v9qPZk20ZscYU1Ko0cHaYaRa45Yurz9y8Yn-UATpY,12879
192
+ langfun/env/event_handlers/event_logger_test.py,sha256=HDSO09eFEKIdMYSM_8UHDKhNhSXuqM6Tif63d0kAGjs,9130
193
+ langfun/env/event_handlers/metric_writer.py,sha256=ABxvIkCJXtGpVb2KoaMqc61yMPhaHCoWB1gQraUqlEk,19793
194
+ langfun/env/event_handlers/metric_writer_test.py,sha256=bjdYXoXMPWpWz_-HUPM6vFP1ez5G386u0fmPfe-SR_M,5952
195
+ langfun-0.1.2.dev202511010804.dist-info/licenses/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
196
+ langfun-0.1.2.dev202511010804.dist-info/METADATA,sha256=GkWja_YtYyrdLzgc8XtzAn4aL8nSz01TSIdgxZIq7iA,7522
197
+ langfun-0.1.2.dev202511010804.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
198
+ langfun-0.1.2.dev202511010804.dist-info/top_level.txt,sha256=RhlEkHxs1qtzmmtWSwYoLVJAc1YrbPtxQ52uh8Z9VvY,8
199
+ langfun-0.1.2.dev202511010804.dist-info/RECORD,,