decorator

Creating a namedtuple object using only a subset of arguments passed

我是研究僧i 提交于 2019-12-19 10:26:18
问题 I am pulling rows from a MySQL database as dictionaries (using SSDictCursor) and doing some processing, using the following approach: from collections import namedtuple class Foo(namedtuple('Foo', ['id', 'name', 'age'])): __slots__ = () def __init__(self, *args): super(Foo, self).__init__(self, *args) # ...some class methods below here class Bar(namedtuple('Bar', ['id', 'address', 'city', 'state']): __slots__ = () def __init__(self, *args): super(Bar, self).__init__(self, *args) # some class

setter method of property decorator not being called

試著忘記壹切 提交于 2019-12-19 06:03:25
问题 I am trying to use a property method to set the status of a class instance, with the following class definition: class Result: def __init__(self,x=None,y=None): self.x = float(x) self.y = float(y) self._visible = False self._status = "You can't see me" @property def visible(self): return self._visible @visible.setter def visible(self,value): if value == True: if self.x is not None and self.y is not None: self._visible = True self._status = "You can see me!" else: self._visible = False raise

setter method of property decorator not being called

天大地大妈咪最大 提交于 2019-12-19 06:00:29
问题 I am trying to use a property method to set the status of a class instance, with the following class definition: class Result: def __init__(self,x=None,y=None): self.x = float(x) self.y = float(y) self._visible = False self._status = "You can't see me" @property def visible(self): return self._visible @visible.setter def visible(self,value): if value == True: if self.x is not None and self.y is not None: self._visible = True self._status = "You can see me!" else: self._visible = False raise

Method overloading decorator

烈酒焚心 提交于 2019-12-19 05:59:15
问题 I'm trying to write a decorator that provides method overloading functionality to python, similar to the one mentioned in PEP 3124. The decorator I wrote works great for regular functions, but I can't get it to work for methods in a class. Here is the decorator: class Overload(object): def __init__(self, default): self.default_function = default self.type_map = {} self.pos = None def __call__(self, *args, **kwargs): print self try: if self.pos is None: pos = kwargs.get("pos", 0) else: pos =

Method overloading decorator

 ̄綄美尐妖づ 提交于 2019-12-19 05:59:08
问题 I'm trying to write a decorator that provides method overloading functionality to python, similar to the one mentioned in PEP 3124. The decorator I wrote works great for regular functions, but I can't get it to work for methods in a class. Here is the decorator: class Overload(object): def __init__(self, default): self.default_function = default self.type_map = {} self.pos = None def __call__(self, *args, **kwargs): print self try: if self.pos is None: pos = kwargs.get("pos", 0) else: pos =

How to implement a wrapper decorator in Java?

陌路散爱 提交于 2019-12-18 21:19:00
问题 The problem is to create a dynamic enhanced version of existing objects. I cannot modify the object's Class . Instead I have to: subclass it wrap the existing object in the new Class delegate all the original method calls to the wrapped object implement all methods that are defined by another interface The interface to add to existing objects is: public interface EnhancedNode { Node getNode(); void setNode(Node node); Set getRules(); void setRules(Set rules); Map getGroups(); void setGroups

How to implement a wrapper decorator in Java?

∥☆過路亽.° 提交于 2019-12-18 21:18:32
问题 The problem is to create a dynamic enhanced version of existing objects. I cannot modify the object's Class . Instead I have to: subclass it wrap the existing object in the new Class delegate all the original method calls to the wrapped object implement all methods that are defined by another interface The interface to add to existing objects is: public interface EnhancedNode { Node getNode(); void setNode(Node node); Set getRules(); void setRules(Set rules); Map getGroups(); void setGroups

Python: How do I access an decorated class's instance from inside a class decorator?

此生再无相见时 提交于 2019-12-18 16:50:59
问题 Here's an example of what I mean: class MyDecorator(object): def __call__(self, func): # At which point would I be able to access the decorated method's parent class's instance? # In the below example, I would want to access from here: myinstance def wrapper(*args, **kwargs): return func(*args, **kwargs) return wrapper class SomeClass(object): ##self.name = 'John' #error here name="John" @MyDecorator() def nameprinter(self): print(self.name) myinstance = SomeClass() myinstance.nameprinter()

Python: How do I access an decorated class's instance from inside a class decorator?

爱⌒轻易说出口 提交于 2019-12-18 16:50:01
问题 Here's an example of what I mean: class MyDecorator(object): def __call__(self, func): # At which point would I be able to access the decorated method's parent class's instance? # In the below example, I would want to access from here: myinstance def wrapper(*args, **kwargs): return func(*args, **kwargs) return wrapper class SomeClass(object): ##self.name = 'John' #error here name="John" @MyDecorator() def nameprinter(self): print(self.name) myinstance = SomeClass() myinstance.nameprinter()

How to Create a Simple Typescript Metadata Annotation

亡梦爱人 提交于 2019-12-18 15:16:16
问题 I have some fields that need to be formatted before sent to server-side. So, I would like to serialize some fields of my typescript classes using custom serializers, something like that would be ideal: export class Person { @serializeWith(MyDateSerializer) private date: Date; } post(url, value) { this.http.post(url, JSON.stringfy(value, (key, val) => { if (//value has serializeWith annotation) { return //serialize with custom serializer } })); } Anything close to this would be acceptable, any