decorator

TypeScript class decorator that modifies object instance

感情迁移 提交于 2019-12-09 05:39:48
问题 I'm making a plugin for Aurelia and need a class decorator that adds attributes to the new object instance, and calls an external function with the new object as an argument. I've looked through examples, and so far I've put together ("pseudo-ish" code) return function addAndCall(target: any): any { var original = target; var newConstructor = function (...args) { original.apply(this, args); this.newAttribute = "object instance value"; ExternalModule.externalFunction(this); }; newConstructor

TypeScript class decorators - add class method

岁酱吖の 提交于 2019-12-08 23:30:54
问题 How to define property with TypeScript and decorators? For example I have this class decorator: function Entity<TFunction extends Function>(target: TFunction): TFunction { Object.defineProperty(target.prototype, 'test', { value: function() { console.log('test call'); return 'test result'; } }); return target; } And use it: @Entity class Project { // } let project = new Project(); console.log(project.test()); I have this console log: test call entity.ts:5 test result entity.ts:18 This code

Python 2.6.4 property decorators not working

*爱你&永不变心* 提交于 2019-12-08 19:28:21
问题 I've seen many examples online and in this forum of how to create properties in Python with special getters and setters . However, I can't get the special getter and setter methods to execute, nor can I use the @property decorator to transform a property as readonly . I'm using Python 2.6.4 and here is my code. Different methods to use properties are employed, but neither work. class PathInfo: def __init__(self, path): self.setpath(path) def getpath(self): return self.__path def setpath(self,

Change an attribute of a function inside its own body?

房东的猫 提交于 2019-12-08 17:34:24
问题 I'm attempting to create a function that keeps count of the times it has been called, and I want the information to stay inside the function itself. I tried creating a wrapper like so: def keep_count(f): f.count = 0 @functools.wraps(f) def wrapped_f(*args, **kwargs): f(*args, **kwargs) f.count += 1 return wrapped_f @keep_count def test_f(*args, **kwargs): print(args, kwargs) I thought it'd work, but I got an AttributeError saying 'function' object has no attribute 'count' . I already figured

dynamically adding functions to a Python module

。_饼干妹妹 提交于 2019-12-08 16:41:19
问题 Our framework requires wrapping certain functions in some ugly boilerplate code: def prefix_myname_suffix(obj): def actual(): print 'hello world' obj.register(actual) return obj I figured this might be simplified with a decorator: @register def myname(): print 'hello world' However, that turned out to be rather tricky, mainly because the framework looks for a certain pattern of function names at module level. I've tried the following within the decorator, to no avail: current_module = _

How can I pass additional arguments to a property decorator in TypeScript?

最后都变了- 提交于 2019-12-08 15:46:43
问题 I have this simple class with a property that has a property decorator applied to it: class MyClass { @collectionMember public myProperty: number[]; // ... } And the decorator function: function collectionMember(target: Object, propertyKey: string | symbol): void { // ... } How can I pass additional arguments to the decorator function? I tried doing the following with no avail: class MyClass { @collectionMember("MyProp") public myProperty: number[]; // ... } Obviously, this yields the error

Strangeness with a decorator

拜拜、爱过 提交于 2019-12-08 14:48:54
问题 I want to make a decorator which will catch exceptions and adequately logged their. def logger(foo): try: print foo() except Exception as e: print e @logger def d(): return 2/2 if __name__ == '__main__': d() Thats right i think, but then I run it and I have an exception like this: 1 Traceback (most recent call last): File "log.py", line 14, in <module> d() TypeError: 'NoneType' object is not callable Why interpreter tells me that the function has None type, but call it and print answer? 回答1:

Decorating a String in Java

流过昼夜 提交于 2019-12-08 12:28:33
问题 Suppose I want to use the Decorator pattern to add functionality to the java.lang.String class. (Just for example, to add a toRussian() method.) String is a final class. Is the following the only way to do it? class DecoratedString implements Serializable, CharSequence, Comparable<DecoratedString> { private final String str; public DecoratedString (String str) { this.str = str; } public DecoratedString toRussian() { ... } public String toString() { return str; } public int hashCode() { return

Use a Descriptor (EDIT: Not a single decorator) for multiple attributes?

爷,独闯天下 提交于 2019-12-08 11:24:35
问题 Python 2.5.4. Fairly new to Python, brand new to decorators as of last night. If I have a class with multiple boolean attributes: class Foo(object): _bool1 = True _bool2 = True _bool3 = True #et cetera def __init__(): self._bool1 = True self._bool2 = False self._bool3 = True #et cetera Is there a way to use a single decorator to check that any setting of any of the boolean attributes must be a boolean, and to return the boolean value for any requested one of these variables? In other words,

Decorator Pattern with multiple generics

时光总嘲笑我的痴心妄想 提交于 2019-12-08 10:28:12
问题 I am currently doing some code refactoring. So I came up replacing an existing inheritance design by a decorator design. But I am struggling with multiple generics (maybe it is simply not possible). I have the above design at the moment. There is the IConstraint which check s a class against an implemented constraint. The concrete realization of those constraints are SimpleConstraintA and SimpleConstraintB both of them are checking some values from ClassA . The Decorator enhances the