Preferred way of defining properties in Python: property decorator or lambda?

For read-only properties I use the decorator, else I usually do something like this: class Bla(object): def sneaky(): def fget(self): return self._sneaky def fset(self, value): self._sneaky = value return locals() sneaky = property(**sneaky()) update: Recent versions of python enhanced the decorator approach: class Bla(object): @property def elegant(self): return self._elegant @elegant.setter def elegant(self, value): self._elegant = … Read more

Python class decorator arguments

@Cache(max_hits=100, timeout=50) calls __init__(max_hits=100, timeout=50), so you aren’t satisfying the function argument. You could implement your decorator via a wrapper method that detected whether a function was present. If it finds a function, it can return the Cache object. Otherwise, it can return a wrapper function that will be used as the decorator. class _Cache(object): … Read more

Decorators on abstract methods

I would code it as two different methods just like in standard method factory pattern description. https://www.oodesign.com/factory-method-pattern.html class Foo(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod @some_decorator def my_method(self, x): self.child_method() class SubFoo(Foo): def child_method(self, x): print x

Decorator classes in Python

A do-nothing decorator class would look like this: class NullDecl (object): def __init__ (self, func): self.func = func for name in set(dir(func)) – set(dir(self)): setattr(self, name, getattr(func, name)) def __call__ (self, *args): return self.func (*args) And then you can apply it normally: @NullDecl def myFunc (x,y,z): return (x+y)/z

How do I use the Decorator Pattern with Unity without explicitly specifying every parameter in the InjectionConstructor

Another approach, thanks to a suggestion from @DarkSquirrel42, is to use an InjectionFactory. The downside is that the code still needs updating every time a new constructor parameter is added to something in the chain. The advantages are much easier to understand code, and only a single registration into the container. Func<IUnityContainer,object> createChain = container … Read more

How to retry urllib2.request when fails?

I would use a retry decorator. There are other ones out there, but this one works pretty well. Here’s how you can use it: @retry(urllib2.URLError, tries=4, delay=3, backoff=2) def urlopen_with_retry(): return urllib2.urlopen(“http://example.com”) This will retry the function if URLError is raised. Check the link above for documentation on the parameters, but basically it will retry … Read more

How do __enter__ and __exit__ work in Python decorator classes?

the __exit__() method should accept information about exceptions that come up in the with: block. See here. The following modification of your code works: def __exit__(self, exc_type, exc_value, tb): if exc_type is not None: traceback.print_exception(exc_type, exc_value, tb) # return False # uncomment to pass exception through return True Then you can try raising an exception … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)