omlish 0.0.0.dev417__py3-none-any.whl → 0.0.0.dev419__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.
@@ -1,99 +1,95 @@
1
+ import abc
1
2
  import typing as ta
2
3
 
3
4
  from .. import check
4
5
  from .. import defs
5
6
  from .. import lang
6
- from .base import Lifecycle
7
+ from .base import AnyLifecycle # noqa
8
+ from .base import Lifecycle # noqa
7
9
  from .states import LifecycleState
8
10
  from .states import LifecycleStates
9
11
  from .transitions import LifecycleTransition
10
12
  from .transitions import LifecycleTransitions
11
13
 
12
14
 
15
+ R = ta.TypeVar('R')
16
+
17
+ AnyLifecycleT = ta.TypeVar('AnyLifecycleT', bound='AnyLifecycle')
18
+
13
19
  LifecycleT = ta.TypeVar('LifecycleT', bound='Lifecycle')
14
20
 
15
21
 
16
22
  ##
17
23
 
18
24
 
19
- class LifecycleListener(ta.Generic[LifecycleT]):
20
- def on_starting(self, obj: LifecycleT) -> None:
21
- pass
25
+ class AnyLifecycleListener(ta.Generic[AnyLifecycleT, R]):
26
+ def on_starting(self, obj: AnyLifecycleT) -> R | None:
27
+ return None
22
28
 
23
- def on_started(self, obj: LifecycleT) -> None:
24
- pass
29
+ def on_started(self, obj: AnyLifecycleT) -> R | None:
30
+ return None
25
31
 
26
- def on_stopping(self, obj: LifecycleT) -> None:
27
- pass
32
+ def on_stopping(self, obj: AnyLifecycleT) -> R | None:
33
+ return None
28
34
 
29
- def on_stopped(self, obj: LifecycleT) -> None:
30
- pass
35
+ def on_stopped(self, obj: AnyLifecycleT) -> R | None:
36
+ return None
31
37
 
32
38
 
33
- class LifecycleController(Lifecycle, ta.Generic[LifecycleT]):
39
+ class AnyLifecycleController(AnyLifecycle[R], lang.Abstract, ta.Generic[AnyLifecycleT, R]):
34
40
  def __init__(
35
41
  self,
36
- lifecycle: LifecycleT,
37
- *,
38
- lock: lang.DefaultLockable = None,
42
+ lifecycle: AnyLifecycleT,
39
43
  ) -> None:
40
44
  super().__init__()
41
45
 
42
- self._lifecycle: LifecycleT = check.isinstance(lifecycle, Lifecycle) # type: ignore
43
- self._lock = lang.default_lock(lock, False)
46
+ self._lifecycle: AnyLifecycleT = check.isinstance(lifecycle, AnyLifecycle) # type: ignore
44
47
 
45
48
  self._state = LifecycleStates.NEW
46
- self._listeners: list[LifecycleListener[LifecycleT]] = []
49
+ self._listeners: list[AnyLifecycleListener[AnyLifecycleT, R]] = []
47
50
 
48
51
  defs.repr('lifecycle', 'state')
49
52
 
50
53
  @property
51
- def lifecycle(self) -> LifecycleT:
54
+ def lifecycle(self) -> AnyLifecycleT:
52
55
  return self._lifecycle
53
56
 
54
57
  @property
55
58
  def state(self) -> LifecycleState:
56
59
  return self._state
57
60
 
58
- def add_listener(self, listener: LifecycleListener[LifecycleT]) -> 'LifecycleController':
59
- self._listeners.append(check.isinstance(listener, LifecycleListener))
61
+ def add_listener(self, listener: AnyLifecycleListener[AnyLifecycleT, R]) -> ta.Self:
62
+ self._listeners.append(check.isinstance(listener, AnyLifecycleListener))
60
63
  return self
61
64
 
65
+ @abc.abstractmethod
62
66
  def _advance(
63
67
  self,
64
68
  transition: LifecycleTransition,
65
- lifecycle_fn: ta.Callable[[], None],
66
- pre_listener_fn: ta.Callable[[LifecycleListener[LifecycleT]], ta.Callable[[LifecycleT], None]] | None = None, # noqa
67
- post_listener_fn: ta.Callable[[LifecycleListener[LifecycleT]], ta.Callable[[LifecycleT], None]] | None = None, # noqa
68
- ) -> None:
69
- with self._lock():
70
- if pre_listener_fn is not None:
71
- for listener in self._listeners:
72
- pre_listener_fn(listener)(self._lifecycle)
73
- check.state(self._state in transition.old)
74
- self._state = transition.new_intermediate
75
- try:
76
- lifecycle_fn()
77
- except Exception:
78
- self._state = transition.new_failed
79
- raise
80
- self._state = transition.new_succeeded
81
- if post_listener_fn is not None:
82
- for listener in self._listeners:
83
- post_listener_fn(listener)(self._lifecycle)
69
+ lifecycle_fn: ta.Callable[[], R | None],
70
+ pre_listener_fn: ta.Callable[
71
+ [AnyLifecycleListener[AnyLifecycleT, R]],
72
+ ta.Callable[[AnyLifecycleT], R | None],
73
+ ] | None = None,
74
+ post_listener_fn: ta.Callable[
75
+ [AnyLifecycleListener[AnyLifecycleT, R]],
76
+ ta.Callable[[AnyLifecycleT], R | None],
77
+ ] | None = None,
78
+ ) -> R | None:
79
+ raise NotImplementedError
84
80
 
85
81
  ##
86
82
 
87
83
  @ta.override
88
- def lifecycle_construct(self) -> None:
89
- self._advance(
84
+ def lifecycle_construct(self) -> R | None:
85
+ return self._advance(
90
86
  LifecycleTransitions.CONSTRUCT,
91
87
  self._lifecycle.lifecycle_construct,
92
88
  )
93
89
 
94
90
  @ta.override
95
- def lifecycle_start(self) -> None:
96
- self._advance(
91
+ def lifecycle_start(self) -> R | None:
92
+ return self._advance(
97
93
  LifecycleTransitions.START,
98
94
  self._lifecycle.lifecycle_start,
99
95
  lambda l: l.on_starting,
@@ -101,8 +97,8 @@ class LifecycleController(Lifecycle, ta.Generic[LifecycleT]):
101
97
  )
102
98
 
103
99
  @ta.override
104
- def lifecycle_stop(self) -> None:
105
- self._advance(
100
+ def lifecycle_stop(self) -> R | None:
101
+ return self._advance(
106
102
  LifecycleTransitions.STOP,
107
103
  self._lifecycle.lifecycle_stop,
108
104
  lambda l: l.on_stopping,
@@ -110,8 +106,59 @@ class LifecycleController(Lifecycle, ta.Generic[LifecycleT]):
110
106
  )
111
107
 
112
108
  @ta.override
113
- def lifecycle_destroy(self) -> None:
114
- self._advance(
109
+ def lifecycle_destroy(self) -> R | None:
110
+ return self._advance(
115
111
  LifecycleTransitions.DESTROY,
116
112
  self._lifecycle.lifecycle_destroy,
117
113
  )
114
+
115
+
116
+ ##
117
+
118
+
119
+ LifecycleListener: ta.TypeAlias = AnyLifecycleListener[LifecycleT, None]
120
+
121
+
122
+ class LifecycleController(
123
+ AnyLifecycleController[LifecycleT, None],
124
+ Lifecycle,
125
+ ta.Generic[LifecycleT],
126
+ ):
127
+ def __init__(
128
+ self,
129
+ lifecycle: LifecycleT,
130
+ *,
131
+ lock: lang.DefaultLockable = None,
132
+ ) -> None:
133
+ super().__init__(lifecycle)
134
+
135
+ self._lock = lang.default_lock(lock, False)
136
+
137
+ def _advance(
138
+ self,
139
+ transition: LifecycleTransition,
140
+ lifecycle_fn: ta.Callable[[], None],
141
+ pre_listener_fn: ta.Callable[
142
+ [LifecycleListener[LifecycleT]],
143
+ ta.Callable[[LifecycleT], None],
144
+ ] | None = None,
145
+ post_listener_fn: ta.Callable[
146
+ [LifecycleListener[LifecycleT]],
147
+ ta.Callable[[LifecycleT], None],
148
+ ] | None = None,
149
+ ) -> None:
150
+ with self._lock():
151
+ if pre_listener_fn is not None:
152
+ for listener in self._listeners:
153
+ pre_listener_fn(listener)(self._lifecycle)
154
+ check.state(self._state in transition.old)
155
+ self._state = transition.new_intermediate
156
+ try:
157
+ lifecycle_fn()
158
+ except Exception:
159
+ self._state = transition.new_failed
160
+ raise
161
+ self._state = transition.new_succeeded
162
+ if post_listener_fn is not None:
163
+ for listener in self._listeners:
164
+ post_listener_fn(listener)(self._lifecycle)
@@ -16,7 +16,19 @@ See:
16
16
  - https://github.com/Fatal1ty/mashumaro
17
17
  - https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#custom-serializers
18
18
  """
19
- from .. import lang as _lang
19
+ from .. import dataclasses as _dc # noqa
20
+
21
+
22
+ _dc.init_package(
23
+ globals(),
24
+ codegen=True,
25
+ )
26
+
27
+
28
+ ##
29
+
30
+
31
+ from .. import lang as _lang # noqa
20
32
 
21
33
 
22
34
  with _lang.auto_proxy_init(globals()) as _api_cap:
@@ -39,6 +51,14 @@ with _lang.auto_proxy_init(globals()) as _api_cap:
39
51
  UnhandledTypeError,
40
52
  )
41
53
 
54
+ from .base.funcs import ( # noqa
55
+ FuncMarshaler,
56
+ FuncUnmarshaler,
57
+
58
+ FuncMarshalerFactory,
59
+ FuncUnmarshalerFactory,
60
+ )
61
+
42
62
  from .base.options import ( # noqa
43
63
  Option,
44
64
  )
@@ -63,8 +83,7 @@ with _lang.auto_proxy_init(globals()) as _api_cap:
63
83
  MarshalerFactory,
64
84
  UnmarshalerFactory,
65
85
 
66
- MarshalerFactory_,
67
- UnmarshalerFactory_,
86
+ Marshaling,
68
87
  )
69
88
 
70
89
  from .base.values import ( # noqa
@@ -115,11 +134,6 @@ with _lang.auto_proxy_init(globals()) as _api_cap:
115
134
  TypeCacheUnmarshalerFactory,
116
135
  )
117
136
 
118
- from .factories.func import ( # noqa
119
- FuncMarshaler,
120
- FuncUnmarshaler,
121
- )
122
-
123
137
  from .factories.recursive import ( # noqa
124
138
  RecursiveMarshalerFactory,
125
139
  RecursiveUnmarshalerFactory,
@@ -241,11 +255,3 @@ with _lang.auto_proxy_init(globals()) as _api_cap:
241
255
 
242
256
  install_standard_factories,
243
257
  )
244
-
245
-
246
- ##
247
-
248
-
249
- from .. import lang as _lang
250
-
251
- _lang.trigger_conditional_imports(__package__)
@@ -0,0 +1,57 @@
1
+ import dataclasses as dc
2
+ import typing as ta
3
+
4
+ from ... import lang
5
+ from ... import reflect as rfl
6
+ from ...funcs import match as mfs
7
+ from .contexts import MarshalContext
8
+ from .contexts import UnmarshalContext
9
+ from .types import Marshaler
10
+ from .types import MarshalerFactory
11
+ from .types import MarshalerMaker
12
+ from .types import Unmarshaler
13
+ from .types import UnmarshalerFactory
14
+ from .types import UnmarshalerMaker
15
+ from .values import Value
16
+
17
+
18
+ ##
19
+
20
+
21
+ @dc.dataclass(frozen=True)
22
+ class FuncMarshaler(Marshaler, lang.Final):
23
+ fn: ta.Callable[[MarshalContext, ta.Any], Value]
24
+
25
+ def marshal(self, ctx: MarshalContext, o: ta.Any) -> Value:
26
+ return self.fn(ctx, o)
27
+
28
+
29
+ @dc.dataclass(frozen=True)
30
+ class FuncUnmarshaler(Unmarshaler, lang.Final):
31
+ fn: ta.Callable[[UnmarshalContext, Value], ta.Any]
32
+
33
+ def unmarshal(self, ctx: UnmarshalContext, v: Value) -> ta.Any:
34
+ return self.fn(ctx, v)
35
+
36
+
37
+ ##
38
+
39
+
40
+ @dc.dataclass(frozen=True)
41
+ class FuncMarshalerFactory(MarshalerFactory): # noqa
42
+ guard: ta.Callable[[MarshalContext, rfl.Type], bool]
43
+ fn: ta.Callable[[MarshalContext, rfl.Type], Marshaler]
44
+
45
+ @lang.cached_property
46
+ def make_marshaler(self) -> MarshalerMaker:
47
+ return mfs.simple(self.guard, self.fn)
48
+
49
+
50
+ @dc.dataclass(frozen=True)
51
+ class FuncUnmarshalerFactory(UnmarshalerFactory): # noqa
52
+ guard: ta.Callable[[UnmarshalContext, rfl.Type], bool]
53
+ fn: ta.Callable[[UnmarshalContext, rfl.Type], Unmarshaler]
54
+
55
+ @lang.cached_property
56
+ def make_unmarshaler(self) -> UnmarshalerMaker:
57
+ return mfs.simple(self.guard, self.fn)
@@ -53,6 +53,7 @@ class Registry(ta.Generic[RegistryItemT]):
53
53
  self._dct: dict[ta.Any, _KeyRegistryItems[RegistryItemT]] = {}
54
54
  self._id_dct: ta.MutableMapping[ta.Any, _KeyRegistryItems[RegistryItemT]] = col.IdentityKeyDict()
55
55
 
56
+ self._version = 0
56
57
  self._sealed = False
57
58
 
58
59
  #
@@ -84,6 +85,9 @@ class Registry(ta.Generic[RegistryItemT]):
84
85
  *items: RegistryItemT,
85
86
  identity: bool = False,
86
87
  ) -> ta.Self:
88
+ if not items:
89
+ return self
90
+
87
91
  with self._lock:
88
92
  if self._sealed:
89
93
  raise RegistrySealedError(self)
@@ -93,6 +97,8 @@ class Registry(ta.Generic[RegistryItemT]):
93
97
  sr = dct[key] = _KeyRegistryItems(key)
94
98
  sr.add(*items)
95
99
 
100
+ self._version += 1
101
+
96
102
  return self
97
103
 
98
104
  #
@@ -1,5 +1,4 @@
1
1
  import abc
2
- import dataclasses as dc
3
2
  import typing as ta
4
3
 
5
4
  from ... import lang
@@ -53,27 +52,6 @@ class UnmarshalerFactory(lang.Abstract):
53
52
  ##
54
53
 
55
54
 
56
- @dc.dataclass(frozen=True)
57
- class MarshalerFactory_(MarshalerFactory): # noqa
58
- fn: MarshalerMaker
59
-
60
- @property
61
- def make_marshaler(self) -> MarshalerMaker:
62
- return self.fn
63
-
64
-
65
- @dc.dataclass(frozen=True)
66
- class UnmarshalerFactory_(UnmarshalerFactory): # noqa
67
- fn: UnmarshalerMaker
68
-
69
- @property
70
- def make_unmarshaler(self) -> UnmarshalerMaker:
71
- return self.fn
72
-
73
-
74
- ##
75
-
76
-
77
55
  class Marshaling(lang.Abstract):
78
56
  @abc.abstractmethod
79
57
  def config_registry(self) -> ConfigRegistry:
@@ -1,5 +1,6 @@
1
1
  """
2
2
  TODO:
3
+ - clean up yeesh
3
4
  - tangled with objects - Field/ObjectMetadata defined over there but unused
4
5
  """
5
6
  import typing as ta
@@ -158,11 +159,17 @@ def get_dataclass_field_infos(
158
159
  return FieldInfos(ret)
159
160
 
160
161
 
161
- def _make_field_obj(ctx, ty, obj, fac):
162
+ def _make_field_obj(
163
+ ctx,
164
+ ty,
165
+ obj,
166
+ fac,
167
+ fac_attr,
168
+ ):
162
169
  if obj is not None:
163
170
  return obj
164
171
  if fac is not None:
165
- return fac(ctx, ty)
172
+ return getattr(fac, fac_attr)(ctx, ty)
166
173
  return ctx.make(ty)
167
174
 
168
175
 
@@ -210,7 +217,16 @@ class DataclassMarshalerFactory(AbstractDataclassFactory, SimpleMarshalerFactory
210
217
  fis = self._get_field_infos(ty, ctx.options)
211
218
 
212
219
  fields = [
213
- (fi, _make_field_obj(ctx, fi.type, fi.metadata.marshaler, fi.metadata.marshaler_factory))
220
+ (
221
+ fi,
222
+ _make_field_obj(
223
+ ctx,
224
+ fi.type,
225
+ fi.metadata.marshaler,
226
+ fi.metadata.marshaler_factory,
227
+ 'make_marshaler',
228
+ ),
229
+ )
214
230
  for fi in fis
215
231
  if fi.name not in dc_md.specials.set
216
232
  ]
@@ -262,7 +278,16 @@ class DataclassUnmarshalerFactory(AbstractDataclassFactory, SimpleUnmarshalerFac
262
278
  ret.extend(e_ns)
263
279
 
264
280
  else:
265
- tup = (fi, _make_field_obj(ctx, fi.type, fi.metadata.unmarshaler, fi.metadata.unmarshaler_factory))
281
+ tup = (
282
+ fi,
283
+ _make_field_obj(
284
+ ctx,
285
+ fi.type,
286
+ fi.metadata.unmarshaler,
287
+ fi.metadata.unmarshaler_factory,
288
+ 'make_unmarshaler',
289
+ ),
290
+ )
266
291
 
267
292
  for pfx in prefixes:
268
293
  for un in fi.unmarshal_names:
omlish/math/__init__.py CHANGED
@@ -1,83 +1,89 @@
1
- from .bits import ( # noqa
2
- get_bit,
3
- get_bits,
4
- set_bit,
5
- set_bits,
6
- )
7
-
8
- from .c import ( # noqa
9
- cdiv,
10
- cmod,
11
- )
12
-
13
- from .fixed import ( # noqa
14
- CheckedFixedWidthIntError,
15
- OverflowFixedWidthIntError,
16
- UnderflowFixedWidthIntError,
17
-
18
- FixedWidthInt,
19
-
20
- SignedInt,
21
- UnsignedInt,
22
-
23
- CheckedInt,
24
- ClampedInt,
25
-
26
- AnyInt8,
27
- AnyInt16,
28
- AnyInt32,
29
- AnyInt64,
30
- AnyInt128,
31
-
32
- CheckedInt8,
33
- CheckedInt16,
34
- CheckedInt32,
35
- CheckedInt64,
36
- CheckedInt128,
37
-
38
- CheckedUint8,
39
- CheckedUint16,
40
- CheckedUint32,
41
- CheckedUint64,
42
- CheckedUint128,
43
-
44
- ClampedInt8,
45
- ClampedInt16,
46
- ClampedInt32,
47
- ClampedInt64,
48
- ClampedInt128,
49
-
50
- ClampedUint8,
51
- ClampedUint16,
52
- ClampedUint32,
53
- ClampedUint64,
54
- ClampedUint128,
55
-
56
- WrappedInt8,
57
- WrappedInt16,
58
- WrappedInt32,
59
- WrappedInt64,
60
- WrappedInt128,
61
-
62
- WrappedUint8,
63
- WrappedUint16,
64
- WrappedUint32,
65
- WrappedUint64,
66
- WrappedUint128,
67
- )
68
-
69
- from .floats import ( # noqa
70
- isclose,
71
- float_to_bytes,
72
- bytes_to_float,
73
- )
74
-
75
- from .stats import ( # noqa
76
- get_quantile,
77
-
78
- Stats,
79
- )
80
-
81
- from .histogram import ( # noqa
82
- SamplingHistogram,
83
- )
1
+ from .. import lang as _lang
2
+
3
+
4
+ with _lang.auto_proxy_init(globals()):
5
+ ##
6
+
7
+ from .bits import ( # noqa
8
+ get_bit,
9
+ get_bits,
10
+ set_bit,
11
+ set_bits,
12
+ )
13
+
14
+ from .c import ( # noqa
15
+ cdiv,
16
+ cmod,
17
+ )
18
+
19
+ from .fixed import ( # noqa
20
+ CheckedFixedWidthIntError,
21
+ OverflowFixedWidthIntError,
22
+ UnderflowFixedWidthIntError,
23
+
24
+ FixedWidthInt,
25
+
26
+ SignedInt,
27
+ UnsignedInt,
28
+
29
+ CheckedInt,
30
+ ClampedInt,
31
+
32
+ AnyInt8,
33
+ AnyInt16,
34
+ AnyInt32,
35
+ AnyInt64,
36
+ AnyInt128,
37
+
38
+ CheckedInt8,
39
+ CheckedInt16,
40
+ CheckedInt32,
41
+ CheckedInt64,
42
+ CheckedInt128,
43
+
44
+ CheckedUint8,
45
+ CheckedUint16,
46
+ CheckedUint32,
47
+ CheckedUint64,
48
+ CheckedUint128,
49
+
50
+ ClampedInt8,
51
+ ClampedInt16,
52
+ ClampedInt32,
53
+ ClampedInt64,
54
+ ClampedInt128,
55
+
56
+ ClampedUint8,
57
+ ClampedUint16,
58
+ ClampedUint32,
59
+ ClampedUint64,
60
+ ClampedUint128,
61
+
62
+ WrappedInt8,
63
+ WrappedInt16,
64
+ WrappedInt32,
65
+ WrappedInt64,
66
+ WrappedInt128,
67
+
68
+ WrappedUint8,
69
+ WrappedUint16,
70
+ WrappedUint32,
71
+ WrappedUint64,
72
+ WrappedUint128,
73
+ )
74
+
75
+ from .floats import ( # noqa
76
+ isclose,
77
+ float_to_bytes,
78
+ bytes_to_float,
79
+ )
80
+
81
+ from .stats import ( # noqa
82
+ get_quantile,
83
+
84
+ Stats,
85
+ )
86
+
87
+ from .histogram import ( # noqa
88
+ SamplingHistogram,
89
+ )
@@ -4,7 +4,7 @@ import abc
4
4
  import sys
5
5
  import typing as ta
6
6
 
7
- from ..lite.maysyncs import make_maysync
7
+ from ..lite.maysync import make_maysync
8
8
  from .asyncs import AbstractAsyncSubprocesses
9
9
  from .run import SubprocessRun
10
10
  from .run import SubprocessRunOutput
omlish/text/templating.py CHANGED
@@ -9,16 +9,18 @@ import string
9
9
  import typing as ta
10
10
 
11
11
  from .. import lang
12
- from .minja import MinjaTemplate
13
- from .minja import MinjaTemplateParam
14
- from .minja import compile_minja_template
15
12
 
16
13
 
17
14
  if ta.TYPE_CHECKING:
18
15
  import jinja2
16
+
17
+ from . import minja
18
+
19
19
  else:
20
20
  jinja2 = lang.proxy_import('jinja2')
21
21
 
22
+ minja = lang.proxy_import('.minja', __package__)
23
+
22
24
 
23
25
  ##
24
26
 
@@ -97,7 +99,7 @@ pep292_templater = Pep292Templater.from_string
97
99
 
98
100
  @dc.dataclass(frozen=True)
99
101
  class MinjaTemplater(Templater):
100
- tmpl: MinjaTemplate
102
+ tmpl: 'minja.MinjaTemplate'
101
103
 
102
104
  ENV_IDENT: ta.ClassVar[str] = 'env'
103
105
 
@@ -110,12 +112,12 @@ class MinjaTemplater(Templater):
110
112
  src: str,
111
113
  **ns: ta.Any,
112
114
  ) -> 'MinjaTemplater':
113
- tmpl = compile_minja_template(
115
+ tmpl = minja.compile_minja_template(
114
116
  src,
115
117
  [
116
- MinjaTemplateParam(cls.ENV_IDENT),
118
+ minja.MinjaTemplateParam(cls.ENV_IDENT),
117
119
  *[
118
- MinjaTemplateParam.new(k, v)
120
+ minja.MinjaTemplateParam.new(k, v)
119
121
  for k, v in ns.items()
120
122
  ],
121
123
  ],