Nir Soffer has posted comments on this change.
Change subject: utils ......................................................................
Patch Set 1:
(9 comments)
.................................................... Commit Message Line 3: AuthorDate: 2014-01-10 14:18:10 +0200 Line 4: Commit: Liron Aravot laravot@redhat.com Line 5: CommitDate: 2014-01-10 14:22:06 +0200 Line 6: Line 7: utils Explain what this does and show a usage example. Line 8: Line 9: Change-Id: Icbd049360a02ae98fa21756dc734c1da8ac3d11a
.................................................... File lib/vdsm/utils.py Line 643: except: Line 644: return False Line 645: Line 646: Line 647: def traced(on=""): Add a docstring that explain what this does and show a usage example. Line 648: Line 649: def func_decorator(func, log): Line 650: def wrapper(self, *a, **kw): Line 651: log.debug("executing %s with a= %s,kw= %s", func.func_name, a, kw)
Line 645: Line 646: Line 647: def traced(on=""): Line 648: Line 649: def func_decorator(func, log): You mark wrapper as function that wraps func:
@functools.wraps(func) Line 650: def wrapper(self, *a, **kw): Line 651: log.debug("executing %s with a= %s,kw= %s", func.func_name, a, kw) Line 652: return func(self, *a, **kw) Line 653: return wrapper
Line 647: def traced(on=""): Line 648: Line 649: def func_decorator(func, log): Line 650: def wrapper(self, *a, **kw): Line 651: log.debug("executing %s with a= %s,kw= %s", func.func_name, a, kw) I think *a=%s, **kw=%s would be more clear. Line 652: return func(self, *a, **kw) Line 653: return wrapper Line 654: Line 655: def decorator(cls):
Line 651: log.debug("executing %s with a= %s,kw= %s", func.func_name, a, kw) Line 652: return func(self, *a, **kw) Line 653: return wrapper Line 654: Line 655: def decorator(cls): Should be class_decorator, to make it easier to understand. Line 656: log = logging.getLogger(on) Line 657: log.debug("aasd") Line 658: for name in dir(cls): Line 659: value = getattr(cls, name)
Line 653: return wrapper Line 654: Line 655: def decorator(cls): Line 656: log = logging.getLogger(on) Line 657: log.debug("aasd") ?! Line 658: for name in dir(cls): Line 659: value = getattr(cls, name) Line 660: log.debug(name) Line 661: log.debug(type(value))
Line 657: log.debug("aasd") Line 658: for name in dir(cls): Line 659: value = getattr(cls, name) Line 660: log.debug(name) Line 661: log.debug(type(value)) Can be on the same line: log.debug("name: %s type: %s", name, value) Line 662: Line 663: if inspect.ismethod(value): Line 664: setattr(cls, name, func_decorator(value, log)) Line 665: log.debug("name = %s , value = %s was wrapped", name, value)
Line 660: log.debug(name) Line 661: log.debug(type(value)) Line 662: Line 663: if inspect.ismethod(value): Line 664: setattr(cls, name, func_decorator(value, log)) If we define func_decorator inside class_decorator, we can remove the log parameter, which can be accessed from the class_decorator context. Line 665: log.debug("name = %s , value = %s was wrapped", name, value) Line 666: Line 667: return cls Line 668:
Line 661: log.debug(type(value)) Line 662: Line 663: if inspect.ismethod(value): Line 664: setattr(cls, name, func_decorator(value, log)) Line 665: log.debug("name = %s , value = %s was wrapped", name, value) name=%s, value=%s would be more clear. Line 666: Line 667: return cls Line 668: Line 669: return decorator