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 inherit from dict type

  • Info objects are iterable, and support all functions as dict type

  • Info 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.

__post_init__()[source]#

Customisation method to be called after initialisation.

Return type:

None

__additional__: list[str]#

List of additional built-in names.

__excluded__: list[str]#

List of names to be excluded from dict conversion.

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).

Parameters:
  • *args (TypeVar(VT)) – Arbitrary positional arguments.

  • **kwargs (TypeVar(VT)) – Arbitrary keyword arguments.

Return type:

Self

__post_init__()[source]#

Customisation method to be called after initialisation.

Return type:

None

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.

Parameters:
Return type:

Self

to_dict()[source]#

Convert Info into dict. :rtype: dict[str, TypeVar(VT)]

Important

We only convert nested Info objects into dict if they are the direct value of the Info object’s attribute. Should such Info objects be nested within other data, types, such as list, tuple, set, etc., we shall not convert them into dict and remain them intact.

@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.

Parameters:
  • cls (TypeVar(ST, bound= Type[Info])) – Info class.

  • _finalised (bool) – Whether to make the info class as finalised.

Return type:

TypeVar(ST, bound= Type[Info])

Returns:

Finalised info class.

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__ and Info.__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 the Info 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.VT: Any#
pcapkit.corekit.infoclass.ST: Type[pcapkit.corekit.infoclass.Info]#