(cls_name: str, field_names: FieldNames)
| 35 | FieldNames = Union[str, Iterable[str]] # <1> |
| 36 | |
| 37 | def 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 | |
| 67 | def parse_identifiers(names: FieldNames) -> tuple[str, ...]: |
nothing calls this directly
no test coverage detected