decorator

Python decorator to determine order of execution of methods

こ雲淡風輕ζ 提交于 2020-01-15 10:11:33
问题 I have a basic class Framework with 3 methods that can be set by the user: initialize , handle_event and finalize . These methods are executed by the method run : class Framework(object): def initialize(self): pass def handle_event(self, event): pass def finalize(self): pass def run(self): self.initialize() for event in range(10): self.handle_event(event) self.finalize() I would like to create 3 decorators: on_initialize , on_event and on_finalize so that I could write such a class: class

Python decorator to determine order of execution of methods

穿精又带淫゛_ 提交于 2020-01-15 10:09:22
问题 I have a basic class Framework with 3 methods that can be set by the user: initialize , handle_event and finalize . These methods are executed by the method run : class Framework(object): def initialize(self): pass def handle_event(self, event): pass def finalize(self): pass def run(self): self.initialize() for event in range(10): self.handle_event(event) self.finalize() I would like to create 3 decorators: on_initialize , on_event and on_finalize so that I could write such a class: class

Adding properties to a class via decorators in TypeScript

眉间皱痕 提交于 2020-01-14 09:05:44
问题 On the TypeScript's Decorator reference page there is a code snipped that illustrates how to override the constructor with class decorator: function classDecorator<T extends {new(...args:any[]):{}}>(constructor:T) { return class extends constructor { newProperty = "new property"; hello = "override"; } } @classDecorator class Greeter { property = "property"; hello: string; constructor(m: string) { this.hello = m; } } console.log(new Greeter("world")); and in logs: class_1 { property: 'property

Angular | Inject service into decorator

你离开我真会死。 提交于 2020-01-14 08:09:24
问题 I am trying to inject a service into a decorator function in which I am creating, this is so I can get the contents that are stored inside of variables. I currently have a basic decorator setup. export function canEdit(editName: string, service: Service) { return function (constructor: any) { console.log(constructor); } } Where I am using this it is asking me for the second parameter, which obviously needs to be dependency injected. I have also tried the @Inject(Service) which isn't allowed

Celery task with multiple decorators not auto registering task name

给你一囗甜甜゛ 提交于 2020-01-14 07:29:12
问题 I'm having a task that looks like this from mybasetask_module import MyBaseTask @task(base=MyBaseTask) @my_custom_decorator def my_task(*args, **kwargs): pass and my base task looks like this from celery import task, Task class MyBaseTask(Task): abstract = True default_retry_delay = 10 max_retries = 3 acks_late = True The problem I'm running into is that the celery worker is registering the task with the name 'mybasetask_module.__inner' The task is registerd fine (which is the package+module

Error building angular+ngrx 8 for production when using createReducer function

点点圈 提交于 2020-01-14 07:03:10
问题 Currently I am trying to build an Angular + NgRX 8 application with the new NgRX creator functions. But when I am building this for production, there appears the following error: Function calls are not supported in decorators but 'createReducer' was called in 'reducers'. In development mode there is no problem at all. The request reducer looks like export interface State extends EntityState<Request> { loading: boolean; error: any; } export const initialState = adapter.getInitialState({

Pylons FormEncode @validate decorator pass parameters into re-render action

自作多情 提交于 2020-01-14 04:15:29
问题 I am attempting to use the validate decorator in Pylons with FormEncode and I have encountered an issue. I am attempting to validate a form on a controller action that requires parameters, and if the validation fails, the parameters aren't passed back in when the form is re-rendered. Here's an example. def question_set(self, id): c.question_set = meta.Session.query(QuestionSet).filter_by(id=id).first() c.question_subjects = meta.Session.query(QuestionSubject).order_by(QuestionSubject.name)

Python set docstring and get method name of dynamically generated classmethod

一笑奈何 提交于 2020-01-14 03:28:26
问题 I'm trying to get/set the name and docstring of dynamically created class methods as follows, but am having trouble figuring out exactly how to do it: import sys import inspect class test(object): pass @classmethod def genericFunc(cls, **kwargs): print "function:", (inspect.stack()[0][3]) print "kwargs:", kwargs function_list = ['myF1', 'myF2'] for func in function_list: setattr(test, func, genericFunc) #set docstring for func here? if __name__ == '__main__': x = test() print "docstring:", x

Google Style Guide properties for getters and setters

拥有回忆 提交于 2020-01-13 09:58:13
问题 I'm curious about one of the recommendations in the Google Python style guide concerning properties. In it, they give the following example: class Square(object): """A square with two properties: a writable area and a read-only perimeter. To use: >>> sq = Square(3) >>> sq.area 9 >>> sq.perimeter 12 >>> sq.area = 16 >>> sq.side 4 >>> sq.perimeter 16 """ def __init__(self, side): self.side = side def __get_area(self): """Calculates the 'area' property.""" return self.side ** 2 def ___get_area

How much overhead do decorators add to Python function calls

ぃ、小莉子 提交于 2020-01-13 07:31:09
问题 I've been playing around with a timing decorator for my pylons app to provide on the fly timing info for specific functions. I've done this by creating a decorator & simply attaching it to any function in the controller I want timed. It's been pointed out however that decorators could add a fair amount of overhead to the call, and that they run 2-3x slower than an undecorated function. Firstly, I would expect that executing a decorated function would take a smite longer than an undecorated