tensorneko-util 0.3.15__py3-none-any.whl → 0.3.17__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.
@@ -14,7 +14,7 @@ class JsonWriter:
14
14
  json.dump(obj, file, indent=indent, ensure_ascii=ensure_ascii)
15
15
  else:
16
16
  import orjson
17
- with open(path, "w", encoding=encoding) as file:
17
+ with open(path, "wb") as file:
18
18
  if indent == 4:
19
19
  warnings.warn("orjson does not support indent 4, will use indent 2 instead.")
20
20
  option = orjson.OPT_INDENT_2
@@ -241,7 +241,8 @@ def load_py(path: str) -> ModuleType:
241
241
  return module
242
242
 
243
243
 
244
- def try_until_success(func: Callable[[Any], T], *args, max_trials: Optional[int] = None, sleep_time: int = 0,
244
+ def try_until_success(func: Callable[..., T], *args, max_trials: Optional[int] = None, sleep_time: int = 0,
245
+ exception_callback: Optional[Callable[[Exception], None]] = None,
245
246
  exception_type: Union[Type[T_E], Tuple[T_E, ...]] = Exception, **kwargs
246
247
  ) -> T:
247
248
  """
@@ -251,6 +252,7 @@ def try_until_success(func: Callable[[Any], T], *args, max_trials: Optional[int]
251
252
  func (``(...) -> T``): The function to run.
252
253
  max_trials (``int``, optional): The max try times. None for unlimited. Default: None.
253
254
  sleep_time (``int``, optional): The sleep time between each try. Default: 0.
255
+ exception_callback (``(Exception) -> None``, optional): The callback function when exception occurs.
254
256
  exception_type (``Type[Exception] | (Type[Exception], ...)``, optional): The exception types to catch.
255
257
  Default: Exception.
256
258
  *args: The args for the function.
@@ -268,6 +270,8 @@ def try_until_success(func: Callable[[Any], T], *args, max_trials: Optional[int]
268
270
  return func(*args, **kwargs)
269
271
  except exception_type as e:
270
272
  trials += 1
273
+ if exception_callback is not None:
274
+ exception_callback(e)
271
275
  if max_trials is not None and trials > max_trials:
272
276
  raise e
273
277
  if sleep_time > 0:
@@ -28,7 +28,7 @@ class Registry(Generic[T]):
28
28
  """
29
29
 
30
30
  def __init__(self):
31
- self._registry: dict[str, T] = {}
31
+ self._registry: dict[str, Type[T]] = {}
32
32
 
33
33
  def register(self, name: str) -> Callable[[Type[T]], Type[T]]:
34
34
  def wrapper(cls):
@@ -37,7 +37,7 @@ class Registry(Generic[T]):
37
37
 
38
38
  return wrapper
39
39
 
40
- def __getitem__(self, name: str) -> T:
40
+ def __getitem__(self, name: str) -> Type[T]:
41
41
  return self._registry[name]
42
42
 
43
43
  def items(self):
@@ -1 +1 @@
1
- 0.3.15
1
+ 0.3.17