Commit 281d3774 authored by Yusei Tahara's avatar Yusei Tahara

slapos/recipe/librecipe/generic.py: GenericBaseRecipe's _ws is a lazy attribute now.

getWorkingSet() is slow and it is not always needed. So, it is better not to call it in __init__.
parent 68fd0fda
......@@ -61,7 +61,13 @@ class GenericBaseRecipe(object):
self._options(options) # Options Hook
self.options = options.copy() # Updated options dict
self._ws = self.getWorkingSet()
@property
def _ws(self):
# getWorkingSet() is slow and it is not always needed.
# So _ws should be a lazy attribute.
if getattr(self, '__ws', None) is None:
self.__ws = self.getWorkingSet()
return self.__ws
def update(self):
"""By default update method does the same thing than install"""
......
  • Beware of __ magic:

    >>> class Foo(object):
    ...   def foo(self):
    ...     if getattr(self, '__bar', None) is None:
    ...       print 'miss'
    ...       self.__bar = 1
    ...     else:
    ...       print 'hit'
    ...
    >>> a = Foo()
    >>> a.foo()
    miss # As expected
    >>> a.foo()
    miss # Not expected !
    >>> a.__dict__
    {'_Foo__bar': 1}
    >>> setattr(a, '__bar', 2)
    >>> a.__dict__
    {'__bar': 2, '_Foo__bar': 1}
    >>> a.foo()
    hit # And here it is, but we have two property with apparently the same name now, and depending on how they are accessed, one or the other will be returned.

    IOW, in a class, a.__b != getattr(a, '__b').

    The same code ran outside of a class would work though:

    >>> b = Foo()
    >>> b.__bar = 1
    >>> b.foo()
    hit
    >>> b.__dict__
    {'__bar': 1}
    Edited by Vincent Pelletier
  • mentioned in commit 6d662699

    Toggle commit list
  • Thank you. I fixed it.

Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment