Skip to content

Instantly share code, notes, and snippets.

@davidism
Created June 13, 2014 21:27
Show Gist options
  • Save davidism/54b4cdfc8124583cec0f to your computer and use it in GitHub Desktop.
Save davidism/54b4cdfc8124583cec0f to your computer and use it in GitHub Desktop.
Build "path" by accessing attributes and calling
class NestMeta(type):
def __getattr__(cls, key):
return cls(cls, key)
def __str__(cls):
return cls.__name__
__repr__ = __str__
class Nest(object):
__metaclass__ = NestMeta
parent = None
name = None
def __init__(self, parent, name):
self.parent = parent
self.name = name
self.args = []
self.kwargs = {}
def __str__(self):
base = '' if self.parent is None else str(self.parent)
parts = [self.name]
parts.extend(str(item) for item in self.args)
parts.extend('{}={}'.format(key, str(value)) for key, value in self.kwargs.iteritems())
return '{}/{}'.format(base, '.'.join(parts))
__repr__ = __str__
def __call__(self, *args, **kwargs):
self.args.extend(args)
self.kwargs.update(kwargs)
return self
def __getattr__(self, key):
return self.__class__(self, key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment