MCPcopy Index your code
hub / github.com/fluentpython/example-code-2e / record_factory

Function record_factory

24-class-metaprog/factories.py:37–64  ·  view source on GitHub ↗
(cls_name: str, field_names: FieldNames)

Source from the content-addressed store, hash-verified

35FieldNames = Union[str, Iterable[str]] # <1>
36
37def record_factory(cls_name: str, field_names: FieldNames) -> type[tuple]: # <2>
38
39 slots = parse_identifiers(field_names) # <3>
40
41 def __init__(self, *args, **kwargs) -> None: # <4>
42 attrs = dict(zip(self.__slots__, args))
43 attrs.update(kwargs)
44 for name, value in attrs.items():
45 setattr(self, name, value)
46
47 def __iter__(self) -> Iterator[Any]: # <5>
48 for name in self.__slots__:
49 yield getattr(self, name)
50
51 def __repr__(self): # <6>
52 values = ', '.join(f'{name}={value!r}'
53 for name, value in zip(self.__slots__, self))
54 cls_name = self.__class__.__name__
55 return f'{cls_name}({values})'
56
57 cls_attrs = dict( # <7>
58 __slots__=slots,
59 __init__=__init__,
60 __iter__=__iter__,
61 __repr__=__repr__,
62 )
63
64 return type(cls_name, (object,), cls_attrs) # <8>
65
66
67def parse_identifiers(names: FieldNames) -> tuple[str, ...]:

Callers

nothing calls this directly

Calls 1

parse_identifiersFunction · 0.70

Tested by

no test coverage detected