Info Class¶
pcapkit.corekit.infoclass
contains dict
like class
Info
only, which is originally
designed to work alike dataclasses.dataclass()
as introduced
in PEP 557.
- class pcapkit.corekit.infoclass.Info(dict_=None, **kwargs)[source]¶
Bases:
Mapping
[str
,VT
],Generic
[VT
]Turn dictionaries into
object
like instances.Info
objects are iterable, and support all functions asdict
typeInfo
objects are immutable, thus cannot set or delete attributes after initialisation
Important
Info
will attempt to rename keys with the same names as the class’s builtin methods, and store the mapping information in the__map__
and__map_reverse__
attributes. However, when accessing such renamed keys, the original key name should always be used, i.e., such renaming is totally transparent to the user.- Parameters:
*args – Arbitrary positional arguments.
**kwargs – Arbitrary keyword arguments.
- static __new__(cls, *args, **kwargs)[source]¶
Create a new instance.
The class will try to automatically generate
__init__
method with the same signature as specified in class variables’ type annotations, which is inspired by PEP 557 (dataclasses
).
- classmethod from_dict(dict_=None, **kwargs)[source]¶
Create a new instance.
If
dict_
is present and has a.keys()
method, then does:for k in dict_: self[k] = dict_[k]
.If
dict_
is present and has no.keys()
method, then does:for k, v in dict_: self[k] = v
.If
dict_
is not present, then does:for k, v in kwargs.items(): self[k] = v
.
- @pcapkit.corekit.infoclass.info_final(cls, *, _finalised=True)[source]¶
Finalise info class.
This decorator function is used to generate necessary attributes and methods for the decorated
Info
class. It can be useful to reduce runtime generation time as well as caching already generated attributes.Notes
The decorator should only be used on the final class, otherwise, any subclasses derived from a finalised info class will not be re-finalised.
Internal Definitions¶
- class pcapkit.corekit.infoclass.InfoMeta(name: str, bases: tuple[type, ...], attrs: dict[str, Any], **kwargs: Any)[source]¶
Bases:
ABCMeta
Meta class to add dynamic support to
Info
.This meta class is used to generate necessary attributes for the
Info
class. It can be useful to reduce runtime generation cost as well as caching already generated attributes.Info.__additional__
andInfo.__excluded__
are lists of additional and excluded field names, which are used to determine certain names to be included or excluded from the field dictionary. They will be automatically populated from the class attributes of theInfo
class and its base classes.Note
This is implemented thru the
__new__()
method, which will inherit the additional and excluded field names from the base classes, as well as populating the additional and excluded field from the subclass attributes.class A(Info): __additional__ = ['a', 'b'] class B(A): __additional__ = ['c', 'd'] class C(B): __additional__ = ['e', 'f'] print(A.__additional__) # ['a', 'b'] print(B.__additional__) # ['a', 'b', 'c', 'd'] print(C.__additional__) # ['a', 'b', 'c', 'd', 'e', 'f']
Type Variables¶
- pcapkit.corekit.infoclass.ST: Type[pcapkit.corekit.infoclass.Info]¶