Skip to content

Instantly share code, notes, and snippets.

@zeffii
Created May 19, 2017 15:20
Show Gist options
  • Save zeffii/b63f3cc61f26109a8e50a732808186ba to your computer and use it in GitHub Desktop.
Save zeffii/b63f3cc61f26109a8e50a732808186ba to your computer and use it in GitHub Desktop.
dict_to_namedtuple.py
from collections import namedtuple
# one level named tuple
def dict_to_namedtuple(kwargs):
nt = namedtuple('props', kwargs.keys())
for field in nt._fields:
setattr(nt, field, kwargs[field])
return nt
@zeffii
Copy link
Author

zeffii commented May 19, 2017

and

from collections import namedtuple

def dict_to_namedtuple(kwargs):
    nt = namedtuple('props', kwargs.keys())
    for field in nt._fields:
        setattr(nt, field, kwargs[field])
    return nt

def broom():
    print('alder')


class NodeProxy():
    
    def __init__(self, *args, **kwargs):

        self.name = 'boo'
        self.order = 'backwards'

        if kwargs:
            nt = dict_to_namedtuple(kwargs)
            self.name = nt.name
            self.order = nt.order

    def pandora(self):
        print('yep')

    def __getattr__(self, name):
        print(name)

        if name == 'boom':
            return broom


NP = NodeProxy(name='gip', order='forward')
print(NP.name, NP.order)
NP.pandora()
NP.boom()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment