This is to extend @dpercy's answer @dpercy's answer a bit. If you make the required fields an attribute of your class you could override __getattr__
to give you access to all the functions without actually having to write them:
This is to extend @dpercy's answer a bit. If you make the required fields an attribute of your class you could override __getattr__
to give you access to all the functions without actually having to write them:
This is to extend @dpercy's answer a bit. If you make the required fields an attribute of your class you could override __getattr__
to give you access to all the functions without actually having to write them:
EDIT3: Now this code has no documentation of these functions. This can be remedied partially with:
class Chaos(object):
required = {"info": [], "change": ["broadband", "sim", "voip"]}
def __init__(self):
docstr = "Wrapper for '{}._command(command='{}', required={}, **kwargs)"
for name, required in Chaos.required.items():
setattr(self, name, partial(self._command, name, required))
getattr(self, name).__doc__ = docstr.format(self, name, required)
def __str__(self):
return "Chaos"
def _command(self, command, required, **kwargs):
print command, required, kwargs
This way, you can at least do:
>>> c.info.__doc__
"Wrapper for 'Chaos._command(command='info', required=[], **kwargs)"
But not:
>>> help(c.info)
<Help page of 'functools.partial'>
EDIT3: Now this code has no documentation of these functions. This can be remedied partially with:
class Chaos(object):
required = {"info": [], "change": ["broadband", "sim", "voip"]}
def __init__(self):
docstr = "Wrapper for '{}._command(command='{}', required={}, **kwargs)"
for name, required in Chaos.required.items():
setattr(self, name, partial(self._command, name, required))
getattr(self, name).__doc__ = docstr.format(self, name, required)
def __str__(self):
return "Chaos"
def _command(self, command, required, **kwargs):
print command, required, kwargs
This way, you can at least do:
>>> c.info.__doc__
"Wrapper for 'Chaos._command(command='info', required=[], **kwargs)"
But not:
>>> help(c.info)
<Help page of 'functools.partial'>
BeEDIT1:
Be aware, though, that the dictionary of required is shared between all instances of Chaos
, therefore this might lead to a bug if you have multiple instances and try to modify them on-the-fly just for that instance:
AsEDIT2:
As an alternative, to avoid the above problem and maybe speed things up a bit, you could assign these functions at instance creatincreation, using setattr
:
This should be faster (about 4 times on my machine), especially if you call the methods many times, because partial
is only called once and not every time. It also means that commands like vars(c)
and dir(c)
now contain the defined functions.
Be aware, though, that the dictionary of required is shared between all instances of Chaos
, therefore this might lead to a bug if you have multiple instances and try to modify them on-the-fly just for that instance:
As an alternative, to avoid the above problem and maybe speed things up a bit, you could assign these functions at instance creatin, using setattr
:
This should be faster, especially if you call the methods many times, because partial
is only called once and not every time. It also means that commands like vars(c)
and dir(c)
now contain the defined functions.
EDIT1:
Be aware, though, that the dictionary of required is shared between all instances of Chaos
, therefore this might lead to a bug if you have multiple instances and try to modify them on-the-fly just for that instance:
EDIT2:
As an alternative, to avoid the above problem and speed things up a bit, you could assign these functions at instance creation, using setattr
:
This should be faster (about 4 times on my machine), especially if you call the methods many times, because partial
is only called once and not every time. It also means that commands like vars(c)
and dir(c)
now contain the defined functions.