Last active
January 6, 2016 22:17
-
-
Save rudifa/5908d9c93b100f512f59 to your computer and use it in GitHub Desktop.
Create a set of named values with a minimum of fuss (a python namedtuple)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Here is my variation on theme 'Create a set of named values with a minimum of fuss' | |
# Based on idea seen in https://gist.github.com/href/1319371 | |
# Rudi Farkas 6 Jan 2016 | |
from collections import namedtuple | |
def MakeNamedTuple(name='NamedTuple', **kwargs): | |
""" | |
Returns a namedtuple instance. | |
>>> nt1 = MakeNamedTuple(reclen=1000, numrec=5, samprate=1e6) | |
>>> nt1 | |
NamedTuple(numrec=5, reclen=1000, samprate=1000000.0) | |
>>> nt1.reclen | |
1000 | |
>>> sp1 = MakeNamedTuple(name='Setup', reclen=2000, numrec=55, samprate=1.6e6) | |
>>> sp1 | |
Setup(numrec=55, reclen=2000, samprate=1600000.0) | |
>>> sp1.samprate | |
1600000.0 | |
""" | |
dt = dict(**kwargs) | |
return namedtuple(name, sorted(dt))(**dt) | |
if __name__ == "__main__" : | |
import doctest | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment