decorator

Angular condition in type provider with AOT

醉酒当歌 提交于 2019-12-23 19:15:09
问题 I have an Angular project which I compile with AOT. I want to be able to register ClassProvider that is resolved dynamically according to configuration. Simplified code I use is this: const isMock = Math.random() > 0.5; @NgModule({ // ... providers: [ { provide: MyServiceBase, useClass: (isMock) ? MyServiceMock : MyService }, ], bootstrap: [AppComponent] }) export class AppModule { } The problem is when I compile this with AOT I always get the same service. I would expect to get different

How does the Python setter decorator work

可紊 提交于 2019-12-23 16:49:21
问题 I am just starting Python, so bear with me if I am missing something obvious. I have read about the decorators and how they work, and I am trying to understand how this gets translated: class SomeObject(object): @property def test(self): return "some value" @test.setter def test(self, value): print(value) From what I have read, this should be turned into: class SomeObject(object): def test(self): return "some value" test = property(test) def test(self, value): print(value) test = test.setter

Override constructor with an class decorator?

醉酒当歌 提交于 2019-12-23 12:23:06
问题 How can I override a constructor with an ES7 class decorator? For example, I'd like to have something like: @injectAttributes({ foo: 42 }) class Bar { constructor() { console.log(this.foo); } } Where the injectAttributes decorator will inject attributes into new instances before they are created: > bar = new Bar(); 42 > bar.foo 42 The obvious solution – using a different constructor: function overrideConstructor(cls, attrs) { Object.assign(this, attrs); cls.call(this); } Does not work because

How to create a decorator function with arguments on python class?

老子叫甜甜 提交于 2019-12-23 12:02:09
问题 I want to create a decorator function to operate on a python class, with the ability to pass additional arguments. I want to do that before the class gets instantiated. Here is my approach: def register(x,a): print x,a @register(5) class Foo(object): pass with x being the class and a the additional argument. But I get a TypeError: register() takes exactly 2 arguments (1 given) What I want is some way to get hold of the class Foo and additional arguments at the time the class is defined,

Default Argument decorator python

被刻印的时光 ゝ 提交于 2019-12-23 11:48:32
问题 Python 3.6 I'm attempting to create a decorator that automatically assigns the string of the argument as the default value. such as: def example(one='one', two='two', three='three'): pass would be equivalent to: @DefaultArguments def example(one, two, three): pass Here is my attempt (doesn't work.. yet..) DefaultArguments : from inspect import Parameter, Signature, signature class DefaultArguments(object): @staticmethod def default_signature(signature): def default(param): if param.kind in

Class decorator to declare static member (e.g., for log4net)?

不问归期 提交于 2019-12-23 09:56:29
问题 I'm using log4net, and we have a lot of this in our code: public class Foo { private static readonly ILog log = LogManager.GetLogger(typeof(Foo)); .... } One downside is that it means we're pasting this 10-word section all over, and every now and then somebody forgets to change the class name. The log4net FAQ also mentions this alternative possibility, which is even more verbose: public class Foo { private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase

Class decorator to declare static member (e.g., for log4net)?

二次信任 提交于 2019-12-23 09:55:17
问题 I'm using log4net, and we have a lot of this in our code: public class Foo { private static readonly ILog log = LogManager.GetLogger(typeof(Foo)); .... } One downside is that it means we're pasting this 10-word section all over, and every now and then somebody forgets to change the class name. The log4net FAQ also mentions this alternative possibility, which is even more verbose: public class Foo { private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase

Dynamically add a decorator to class

三世轮回 提交于 2019-12-23 08:53:20
问题 I have a rather large and involved decorator to debug PyQt signals that I want to dynamically add to a class. Is there a way to add a decorator to a class dynamically? I might be approaching this problem from the wrong angle, so here is what I want to accomplish. Goal I have a decorator that will discover/attach to all pyqt signals in a class and print debug when those signals are emitted. This decorator is great for debugging a single class' signals. However, there might be a time when I

Angular 2 - Inject a dependency into a decorator factory

余生长醉 提交于 2019-12-23 07:39:22
问题 Is there a way to inject a dependency into a decorator factory, using Angular's DI? Let's take the following code as a simplified example: @Component({ selector: 'hello-component', template: '<div>Hello World</div>' }) export class HelloComponent { @PersonName() name: string; ngAfterViewInit() { console.log(`Hello, ${this.name}`); } } Here, the intended behaviour of the PersonName decorator is for it to access a Person dependency, and use it to set the name property of the class. Is it

Refresh decorator

有些话、适合烂在心里 提交于 2019-12-23 03:17:24
问题 I'm trying to write a decorator that 'refreshes' after being called, but where the refreshing only occurs once after the last function exits. Here is an example: @auto_refresh def a(): print "In a" @auto_refresh def b(): print "In b" a() If a() is called, I want the refresh function to be run after exiting a() . If b() is called, I want the refresh function to be run after exiting b() , but not after a() when called by b() . Here is an example of a class that does this: class auto_refresh