subclass_dictable_namedtuple

privex.helpers.collections.subclass_dictable_namedtuple(named_type: type, typename=None, module=None, **kwargs)type[source]

Convert an existing collections.namedtuple() type into a dictable_namedtuple.

If you have an INSTANCE of a type (e.g. it has data attached), use convert_dictable_namedtuple()

Example:

>>> from collections import namedtuple
>>> from privex.helpers import subclass_dictable_namedtuple
>>> # Create a namedtuple type called 'Person'
>>> orig_Person = namedtuple('Person', 'first_name last_name')
>>> # Convert the 'Person' type into a dictable_namedtuple
>>> Person = subclass_dictable_namedtuple(orig_Person)
>>> john = Person('John', 'Doe')   # Create an instance of this dictable_namedtuple Person
>>> john['middle_name'] = 'Davis'
Parameters
  • named_type (type) – A NamedTuple type returned from collections.namedtuple()

  • typename (str) – Optionally, you can change the name of your type, e.g. if you provide a Person class type, but you set this to Man, then this will return a Man class type.

  • module (str) – Optionally, you can change the module that the type class belongs to. Otherwise it will inherit the module path from named_type.

Key bool read_only

(Default: False) If set to True, the outputted dictable_namedtuple type will not allow new fields to be created via attribute / item setting.

Return type dictable_namedtuple

Your named_type converted into a dictable_namedtuple type class.