namedtuple_extend

emlib.misc.namedtuple_extend(name, orig, columns)[source]

Create a new namedtuple constructor with the added columns

It returns the class constructor and an ad-hoc constructor which takes as arguments an instance of the original namedtuple and the additional args

Parameters:
  • name (str) – new name for the type

  • orig – an instance of the original namedtuple or the constructor itself

  • columns (Union[str, Sequence[str]]) – the columns to add

Returns:

a tuple (newtype, newtype_from_old)

Example

>>> from collections import namedtuple
>>> Point = namedtuple("Point", "x y")
>>> p = Point(10, 20)
>>> Vec3, fromPoint = namedtuple_extend("Vec3", Point, "z")
>>> Vec3(1, 2, 3)
Vec3(x=1, y=2, z=3)
>>> fromPoint(p, 30)
Vec3(x=10, y=20, z=30)