ducktools-classbuilder 0.2.0__py3-none-any.whl → 0.3.0__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 ducktools-classbuilder might be problematic. Click here for more details.
- ducktools/classbuilder/__init__.py +27 -41
- ducktools/classbuilder/__init__.pyi +2 -3
- ducktools/classbuilder/prefab.py +7 -7
- ducktools/classbuilder/prefab.pyi +4 -4
- {ducktools_classbuilder-0.2.0.dist-info → ducktools_classbuilder-0.3.0.dist-info}/METADATA +1 -1
- ducktools_classbuilder-0.3.0.dist-info/RECORD +10 -0
- ducktools_classbuilder-0.2.0.dist-info/RECORD +0 -10
- {ducktools_classbuilder-0.2.0.dist-info → ducktools_classbuilder-0.3.0.dist-info}/LICENSE.md +0 -0
- {ducktools_classbuilder-0.2.0.dist-info → ducktools_classbuilder-0.3.0.dist-info}/WHEEL +0 -0
- {ducktools_classbuilder-0.2.0.dist-info → ducktools_classbuilder-0.3.0.dist-info}/top_level.txt +0 -0
|
@@ -19,34 +19,12 @@
|
|
|
19
19
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
20
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
# SOFTWARE.
|
|
22
|
-
__version__ = "v0.
|
|
22
|
+
__version__ = "v0.3.0"
|
|
23
23
|
|
|
24
24
|
# Change this name if you make heavy modifications
|
|
25
25
|
INTERNALS_DICT = "__classbuilder_internals__"
|
|
26
26
|
|
|
27
27
|
|
|
28
|
-
def get_internals(cls):
|
|
29
|
-
"""
|
|
30
|
-
Utility function to get the internals dictionary
|
|
31
|
-
or return None.
|
|
32
|
-
|
|
33
|
-
As generated classes will always have 'fields'
|
|
34
|
-
and 'local_fields' attributes this will always
|
|
35
|
-
evaluate as 'truthy' if this is a generated class.
|
|
36
|
-
|
|
37
|
-
Generally you should use the helper get_flags and
|
|
38
|
-
get_fields methods.
|
|
39
|
-
|
|
40
|
-
Usage:
|
|
41
|
-
if internals := get_internals(cls):
|
|
42
|
-
...
|
|
43
|
-
|
|
44
|
-
:param cls: generated class
|
|
45
|
-
:return: internals dictionary of the class or None
|
|
46
|
-
"""
|
|
47
|
-
return getattr(cls, INTERNALS_DICT, None)
|
|
48
|
-
|
|
49
|
-
|
|
50
28
|
def get_fields(cls, *, local=False):
|
|
51
29
|
"""
|
|
52
30
|
Utility function to gather the fields dictionary
|
|
@@ -71,7 +49,9 @@ def get_flags(cls):
|
|
|
71
49
|
return getattr(cls, INTERNALS_DICT)["flags"]
|
|
72
50
|
|
|
73
51
|
|
|
74
|
-
def
|
|
52
|
+
def _get_inst_fields(inst):
|
|
53
|
+
# This is an internal helper for constructing new
|
|
54
|
+
# 'Field' instances from existing ones.
|
|
75
55
|
return {
|
|
76
56
|
k: getattr(inst, k)
|
|
77
57
|
for k in get_fields(type(inst))
|
|
@@ -105,7 +85,7 @@ class MethodMaker:
|
|
|
105
85
|
self.code_generator = code_generator
|
|
106
86
|
|
|
107
87
|
def __repr__(self):
|
|
108
|
-
return f"<MethodMaker for {self.funcname} method>"
|
|
88
|
+
return f"<MethodMaker for {self.funcname!r} method>"
|
|
109
89
|
|
|
110
90
|
def __get__(self, instance, cls):
|
|
111
91
|
local_vars = {}
|
|
@@ -122,7 +102,7 @@ class MethodMaker:
|
|
|
122
102
|
return method.__get__(instance, cls)
|
|
123
103
|
|
|
124
104
|
|
|
125
|
-
def init_maker(cls, *, null=NOTHING):
|
|
105
|
+
def init_maker(cls, *, null=NOTHING, extra_code=None):
|
|
126
106
|
fields = get_fields(cls)
|
|
127
107
|
flags = get_flags(cls)
|
|
128
108
|
|
|
@@ -150,8 +130,17 @@ def init_maker(cls, *, null=NOTHING):
|
|
|
150
130
|
assignments.append(assignment)
|
|
151
131
|
|
|
152
132
|
args = ", ".join(arglist)
|
|
153
|
-
assigns = "\n ".join(assignments)
|
|
154
|
-
code =
|
|
133
|
+
assigns = "\n ".join(assignments) if assignments else "pass\n"
|
|
134
|
+
code = (
|
|
135
|
+
f"def __init__(self, {args}):\n"
|
|
136
|
+
f" {assigns}\n"
|
|
137
|
+
)
|
|
138
|
+
# Handle additional function calls
|
|
139
|
+
# Used for validate_field on fieldclasses
|
|
140
|
+
if extra_code:
|
|
141
|
+
for line in extra_code:
|
|
142
|
+
code += f" {line}\n"
|
|
143
|
+
|
|
155
144
|
return code, globs
|
|
156
145
|
|
|
157
146
|
|
|
@@ -300,7 +289,7 @@ class Field:
|
|
|
300
289
|
:param kwargs: Additional keyword arguments for subclasses
|
|
301
290
|
:return: new field subclass instance
|
|
302
291
|
"""
|
|
303
|
-
argument_dict = {**
|
|
292
|
+
argument_dict = {**_get_inst_fields(fld), **kwargs}
|
|
304
293
|
|
|
305
294
|
return cls(**argument_dict)
|
|
306
295
|
|
|
@@ -407,6 +396,14 @@ def slotclass(cls=None, /, *, methods=default_methods, syntax_check=True):
|
|
|
407
396
|
return cls
|
|
408
397
|
|
|
409
398
|
|
|
399
|
+
def _field_init_func(cls):
|
|
400
|
+
# Fields need a different Nothing for their __init__ generation
|
|
401
|
+
# And an extra call to validate_field
|
|
402
|
+
field_nothing = _NothingType()
|
|
403
|
+
extra_calls = ["self.validate_field()"]
|
|
404
|
+
return init_maker(cls, null=field_nothing, extra_code=extra_calls)
|
|
405
|
+
|
|
406
|
+
|
|
410
407
|
def fieldclass(cls):
|
|
411
408
|
"""
|
|
412
409
|
This is a special decorator for making Field subclasses using __slots__.
|
|
@@ -417,18 +414,7 @@ def fieldclass(cls):
|
|
|
417
414
|
:return: Modified subclass
|
|
418
415
|
"""
|
|
419
416
|
|
|
420
|
-
|
|
421
|
-
# So append it to the code from __init__.
|
|
422
|
-
def field_init_func(cls_):
|
|
423
|
-
code, globs = init_maker(cls_, null=field_nothing)
|
|
424
|
-
code += " self.validate_field()\n"
|
|
425
|
-
return code, globs
|
|
426
|
-
|
|
427
|
-
field_nothing = _NothingType()
|
|
428
|
-
field_init_desc = MethodMaker(
|
|
429
|
-
"__init__",
|
|
430
|
-
field_init_func,
|
|
431
|
-
)
|
|
417
|
+
field_init_desc = MethodMaker("__init__", _field_init_func)
|
|
432
418
|
field_methods = frozenset({field_init_desc, repr_desc, eq_desc})
|
|
433
419
|
|
|
434
420
|
cls = builder(
|
|
@@ -6,13 +6,11 @@ _py_type = type # Alias for type where it is used as a name
|
|
|
6
6
|
__version__: str
|
|
7
7
|
INTERNALS_DICT: str
|
|
8
8
|
|
|
9
|
-
def get_internals(cls) -> dict[str, typing.Any] | None: ...
|
|
10
|
-
|
|
11
9
|
def get_fields(cls: type, *, local: bool = False) -> dict[str, Field]: ...
|
|
12
10
|
|
|
13
11
|
def get_flags(cls:type) -> dict[str, bool]: ...
|
|
14
12
|
|
|
15
|
-
def
|
|
13
|
+
def _get_inst_fields(inst: typing.Any) -> dict[str, typing.Any]: ...
|
|
16
14
|
|
|
17
15
|
class _NothingType:
|
|
18
16
|
...
|
|
@@ -32,6 +30,7 @@ def init_maker(
|
|
|
32
30
|
cls: type,
|
|
33
31
|
*,
|
|
34
32
|
null: _NothingType = NOTHING,
|
|
33
|
+
extra_code: None | list[str] = None
|
|
35
34
|
) -> tuple[str, dict[str, typing.Any]]: ...
|
|
36
35
|
def repr_maker(cls: type) -> tuple[str, dict[str, typing.Any]]: ...
|
|
37
36
|
def eq_maker(cls: type) -> tuple[str, dict[str, typing.Any]]: ...
|
ducktools/classbuilder/prefab.py
CHANGED
|
@@ -398,7 +398,7 @@ def get_asdict_maker():
|
|
|
398
398
|
vals = ", ".join(
|
|
399
399
|
f"'{name}': self.{name}"
|
|
400
400
|
for name, attrib in fields.items()
|
|
401
|
-
if attrib.
|
|
401
|
+
if attrib.serialize and not attrib.exclude_field
|
|
402
402
|
)
|
|
403
403
|
out_dict = f"{{{vals}}}"
|
|
404
404
|
code = f"def as_dict(self): return {out_dict}"
|
|
@@ -428,7 +428,7 @@ class Attribute(Field):
|
|
|
428
428
|
compare=True,
|
|
429
429
|
iter=True,
|
|
430
430
|
kw_only=False,
|
|
431
|
-
|
|
431
|
+
serialize=True,
|
|
432
432
|
exclude_field=False,
|
|
433
433
|
)
|
|
434
434
|
|
|
@@ -450,7 +450,7 @@ def attribute(
|
|
|
450
450
|
compare=True,
|
|
451
451
|
iter=True,
|
|
452
452
|
kw_only=False,
|
|
453
|
-
|
|
453
|
+
serialize=True,
|
|
454
454
|
exclude_field=False,
|
|
455
455
|
doc=None,
|
|
456
456
|
type=NOTHING,
|
|
@@ -466,7 +466,7 @@ def attribute(
|
|
|
466
466
|
:param compare: Include this attribute in the class __eq__
|
|
467
467
|
:param iter: Include this attribute in the class __iter__ if generated
|
|
468
468
|
:param kw_only: Make this argument keyword only in init
|
|
469
|
-
:param
|
|
469
|
+
:param serialize: Include this attribute in methods that serialize to dict
|
|
470
470
|
:param exclude_field: Exclude this field from all magic method generation
|
|
471
471
|
apart from __init__ signature
|
|
472
472
|
and do not include it in PREFAB_FIELDS
|
|
@@ -484,7 +484,7 @@ def attribute(
|
|
|
484
484
|
compare=compare,
|
|
485
485
|
iter=iter,
|
|
486
486
|
kw_only=kw_only,
|
|
487
|
-
|
|
487
|
+
serialize=serialize,
|
|
488
488
|
exclude_field=exclude_field,
|
|
489
489
|
doc=doc,
|
|
490
490
|
type=type,
|
|
@@ -906,7 +906,7 @@ def is_prefab_instance(o):
|
|
|
906
906
|
|
|
907
907
|
def as_dict(o):
|
|
908
908
|
"""
|
|
909
|
-
Get the valid fields from a prefab respecting the
|
|
909
|
+
Get the valid fields from a prefab respecting the serialize
|
|
910
910
|
values of attributes
|
|
911
911
|
|
|
912
912
|
:param o: instance of a prefab class
|
|
@@ -927,5 +927,5 @@ def as_dict(o):
|
|
|
927
927
|
return {
|
|
928
928
|
name: getattr(o, name)
|
|
929
929
|
for name, attrib in flds.items()
|
|
930
|
-
if attrib.
|
|
930
|
+
if attrib.serialize and not attrib.exclude_field
|
|
931
931
|
}
|
|
@@ -6,7 +6,7 @@ from collections.abc import Callable
|
|
|
6
6
|
from . import (
|
|
7
7
|
INTERNALS_DICT, NOTHING,
|
|
8
8
|
Field, MethodMaker, SlotFields as SlotFields,
|
|
9
|
-
builder, fieldclass,
|
|
9
|
+
builder, fieldclass, get_flags, get_fields, slot_gatherer
|
|
10
10
|
)
|
|
11
11
|
|
|
12
12
|
# noinspection PyUnresolvedReferences
|
|
@@ -63,7 +63,7 @@ class Attribute(Field):
|
|
|
63
63
|
compare: bool
|
|
64
64
|
iter: bool
|
|
65
65
|
kw_only: bool
|
|
66
|
-
|
|
66
|
+
serialize: bool
|
|
67
67
|
exclude_field: bool
|
|
68
68
|
|
|
69
69
|
def __init__(
|
|
@@ -78,7 +78,7 @@ class Attribute(Field):
|
|
|
78
78
|
compare: bool = True,
|
|
79
79
|
iter: bool = True,
|
|
80
80
|
kw_only: bool = False,
|
|
81
|
-
|
|
81
|
+
serialize: bool = True,
|
|
82
82
|
exclude_field: bool = False,
|
|
83
83
|
) -> None: ...
|
|
84
84
|
|
|
@@ -97,7 +97,7 @@ def attribute(
|
|
|
97
97
|
compare: bool = True,
|
|
98
98
|
iter: bool = True,
|
|
99
99
|
kw_only: bool = False,
|
|
100
|
-
|
|
100
|
+
serialize: bool = True,
|
|
101
101
|
exclude_field: bool = False,
|
|
102
102
|
) -> Attribute: ...
|
|
103
103
|
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
ducktools/classbuilder/__init__.py,sha256=Vw5RjzzIkXGYXKOmFWTxPsIAM4ceg74Q1Oku4fwF0ek,13505
|
|
2
|
+
ducktools/classbuilder/__init__.pyi,sha256=hgcEylMAHgfonOt2fH9OCE3gU60mYkiOUe7e8mTwNVs,2857
|
|
3
|
+
ducktools/classbuilder/prefab.py,sha256=gJJCtTAbQLgIoo79jRWVp09r3u8kJjcqXp4G4FTu9j8,29887
|
|
4
|
+
ducktools/classbuilder/prefab.pyi,sha256=vcaEpBxyVsOTwkGBkVgmbpn8DPqQqFnAU4xTKPnpOd8,3977
|
|
5
|
+
ducktools/classbuilder/py.typed,sha256=la67KBlbjXN-_-DfGNcdOcjYumVpKG_Tkw-8n5dnGB4,8
|
|
6
|
+
ducktools_classbuilder-0.3.0.dist-info/LICENSE.md,sha256=6Thz9Dbw8R4fWInl6sGl8Rj3UnKnRbDwrc6jZerpugQ,1070
|
|
7
|
+
ducktools_classbuilder-0.3.0.dist-info/METADATA,sha256=sAWw76LgN3HruCS-SXURrrAj2A6MmhsyDJwMSvnWW2w,9280
|
|
8
|
+
ducktools_classbuilder-0.3.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
|
9
|
+
ducktools_classbuilder-0.3.0.dist-info/top_level.txt,sha256=uSDLtio3ZFqdwcsMJ2O5yhjB4Q3ytbBWbA8rJREganc,10
|
|
10
|
+
ducktools_classbuilder-0.3.0.dist-info/RECORD,,
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
ducktools/classbuilder/__init__.py,sha256=OOVk-E6UBi8-68bEy9pqCiKbn9oQ1y4tRGnZ3LGsGYg,13747
|
|
2
|
-
ducktools/classbuilder/__init__.pyi,sha256=-QHJhPn4EjI4XW4OI74MKNg2tGKJiM3w0ELSOvPstAw,2877
|
|
3
|
-
ducktools/classbuilder/prefab.py,sha256=yUa_yoPKU1hzI4klqWJOwTZEXtNOmUU1fQnLuHkVACw,29871
|
|
4
|
-
ducktools/classbuilder/prefab.pyi,sha256=4cyXYqTXtCJv4gPOSNtkBrjORj2jt1bvX6p_sCOzoXw,3963
|
|
5
|
-
ducktools/classbuilder/py.typed,sha256=la67KBlbjXN-_-DfGNcdOcjYumVpKG_Tkw-8n5dnGB4,8
|
|
6
|
-
ducktools_classbuilder-0.2.0.dist-info/LICENSE.md,sha256=6Thz9Dbw8R4fWInl6sGl8Rj3UnKnRbDwrc6jZerpugQ,1070
|
|
7
|
-
ducktools_classbuilder-0.2.0.dist-info/METADATA,sha256=LmHMIhj4jJ9WlmHOac4lBVgW4l6cdNUjG8cEvIULVko,9280
|
|
8
|
-
ducktools_classbuilder-0.2.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
|
9
|
-
ducktools_classbuilder-0.2.0.dist-info/top_level.txt,sha256=uSDLtio3ZFqdwcsMJ2O5yhjB4Q3ytbBWbA8rJREganc,10
|
|
10
|
-
ducktools_classbuilder-0.2.0.dist-info/RECORD,,
|
{ducktools_classbuilder-0.2.0.dist-info → ducktools_classbuilder-0.3.0.dist-info}/LICENSE.md
RENAMED
|
File without changes
|
|
File without changes
|
{ducktools_classbuilder-0.2.0.dist-info → ducktools_classbuilder-0.3.0.dist-info}/top_level.txt
RENAMED
|
File without changes
|