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 (VT) – Arbitrary positional arguments.

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

Return type

Info

__map__: dict[str, str]

Mapping of name conflicts with builtin methods (original names to transformed names).

__map_reverse__: dict[str, str]

Mapping of name conflicts with builtin methods (transformed names to original names).

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

Info

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
  • dict_ (Optional[Mapping[str, VT] | Iterable[tuple[str, VT]]]) – Source data.

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

Return type

Info[VT]

to_dict()[source]

Convert Info into dict.

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.

Return type

dict[str, VT]