ntuple_utils¶
Extensions for namedtuple types.
This module provides utilities to derive an extended namedtuple from an existing one, adding new fields while preserving the original fields and their defaults.
extend(base_nt, name, fields, defaults=None)
¶
Create a new namedtuple type extending an existing one.
The new type preserves the base fields (in order, with their defaults) and appends the extra fields. Fields can be added with or without defaults; adding a defaulted field after fields without defaults is rejected, since the resulting namedtuple would not be constructible positionally.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_nt
|
Base namedtuple type to extend. |
required | |
name
|
Name of the new namedtuple type. |
required | |
fields
|
Field specification, as a space/comma separated string or an iterable of field names. |
required | |
defaults
|
Optional sequence of default values applied to the rightmost new fields, following the collections.namedtuple defaults semantics: the last len(defaults) fields get the given values in order. |
None
|
Returns:
| Type | Description |
|---|---|
|
A new namedtuple type with the base and the extra fields. |
Source code in py_misc_utils/ntuple_utils.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |