On 12/22/2011 03:36 PM, Tony Asleson wrote:
>> if __name__ == '__main__':
>> c = get_class(class_name)
>> c = get_class(__name__ + '.' + class_name)
>> Given you know all the classes descended from IData, a little
>> string->class dict would achieve the same goal and be much simpler. If
>> that won't fly, then imho this conditional should at least be moved
>> inside get_class().
> Can you expand on the string->class dict some more? If I derive a new
> class from IData would I need to remember to update the string->class
> dict? I think I may be missing something here.
You would have to update the dict. If you control all IData-derived
classes it may be good enough. If not then maybe get_class is needed,
and the best we can do is make the code so that conditional is not
needed at every call site.
>> A cleaner way might be something like this:
>> class Callable(object):
>> def __init__(self, obj, name):
>> self.name = name
>> self.obj = obj
>> def __call__(self, *args):
>> self.obj.tp.rpc(self.name, args)
>> class Client(object):
>> def __getattr__(self, name):
>> if name in ("set_time_out", "get_time_out"): #etc...
>> return Callable(self, name)
>> raise AttributeError
>> __getattr__ lets us handle methods not actually in the class, and get
>> the name. Returning the Callable object then lets us handle the
>> parameters the way we want in __call__ (coalescing them into a list for
>> the call to tp.rpc()).
> The purist in me likes this :-). Do you have suggestions for the following:
> 1. Documentation
> How to handle this as it won't be as helpful or even exist right?
> import lsm.client
Override __dir__. Presumably you have a list of supported calls you are
proxying for, you can append this to the class __dir__ results, and it
should be correct.
> print lsm.client.Client.volume_create.__doc__
Make the supported call list a tuple of (name, docstring), pass both to
the Callable object's __init__, and set self.__doc__ = docstring?
OR, forget the whole thing and just put the each method's name and
params in each call to rpc() :-) Very readable and straightforward.
> 2. Parameters
> I am using locals() to get the parameters as a dict to be passed to the
> rpc call. How do I get the parameter names for the methods, they don't
> formally exist? Or am I missing something?
> Please note that I am passing the json-rpc method parameters as
> name:value pairs, they are not positional.
Dunno, offhand. See above, on second thought I suggest just coding this
straightforwardly, and then passing a dict of params is not an issue.
Off for the holidays -- Regards -- Andy