In my previous post, I showed a class that allowed a dictionary to be accessed as object attributes - very useful when connecting to arbitrary web APIs.
To use this, one would still need to wrap the various API methods, such as com.foobar.services.status().
With the code below, you no longer need to write wrapper code for arbitrary hierarchical structures. A single __call__ method will be provided with the full path to the function (in this case "com.foobar.services.status":
class api():
def __init__(self, parents="", parent=""):
if parents:
self.parents = parents + "." + parent
else:
self.parents = parent
def __call__(self, **params):
print self.parents, params
def __getattr__(self, name):
return api(self.parents, name)
a = api()
a.com.foobar.services.status(device_id=6544)
This should show simply:
com.foobar.services.status {'device_id': 6544}