class - Create hierarchical python object where arguments to methods are from hierarchy -


there existing module use containing class has methods string arguments take form:

existing_object.existing_method("arg1") 

or

existing_object.existing_method("arg1:arg2") 

the arguments in hierarchical structure. create module objectifies arguments , makes them methods of class of imported module such use this:

my_object.arg1.my_method() 

or

my_object.arg1.arg2.my_method() 

my_method() call existing_method() while passing "arg1:arg2" argument.

if point me in right direction started i'd appreciate it.

you can custom __getattr__ returns special method caller instances:

class methodcaller(object):     def __init__(self, args, parent):         self.args = args         self.parent = parent     def __getattr__(self, name):         return methodcaller(self.args + (name,), self.parent)     def my_method(self):         return self.parent.existing_method(':'.join(self.args))  class myclass(object):     def __getattr__(self, name):         return methodcaller((name,), self)     def existing_method(self, arg):         print arg 

example:

>>> myclass().arg1.my_method() arg1 >>> myclass().arg1.arg2.my_method() arg1:arg2 >>> myclass().foo.bar.my_method() foo:bar 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -