decorator

Python decorator with Flask

让人想犯罪 __ 提交于 2019-12-22 04:13:13
问题 I need to add a python decorator to Flask route functions, (basically I edited the code from here) def requires_admin(f): def wrapper(f): @wraps(f) def wrapped(*args, **kwargs): #if not admin: #return render_template('error.html') return f(*args, **kwargs) return wrapped return wrapper and use it like this will be OK: @app.route('/admin/action') @requires_admin def AdminAction(): #NO error if NO parameter But use it like this will have error: @app.route('/admin/action/<int:id>') @requires

Javascript Decorator Pattern - Prototype or single function?

独自空忆成欢 提交于 2019-12-22 01:24:30
问题 I'm running through Addy Osmani's tutorial on The Decorator Pattern (found here http://addyosmani.com/blog/decorator-pattern/) and I'm a little confused on how to implement the most simplistic Decorator in Javascript. It seems that some examples use the obj.prototype pattern to add functionality to an existing object and some create a standalone function and pass an object. // Decorator Pattern ? function coffee(size, flavors) { this._size = size || "medium"; this._flavors = flavors || [];

Python decorate a class to change parent object type

偶尔善良 提交于 2019-12-21 20:44:56
问题 Suppose you have two classes X & Y. You want to decorate those classes by adding attributes to the class to produce new classes X1 and Y1. For example: class X1(X): new_attribute = 'something' class Y1(Y): new_attribute = 'something' new_attribute will always be the same for both X1 and Y1. X & Y are not related in any meaningful way, except that multiple inheritance is not possible. There are a set of other attributes as well, but this is degenerate to illustrate. I feel like I'm

TypeScript Decorators and Circular Dependencies

假如想象 提交于 2019-12-21 20:19:35
问题 Consider the sample of inter-dependent code (below) that makes use of decorators. Now consider the following workflow (yes, I do want to pass the actual exported classes since I need to use them later): App imports and runs Parent.ts @Test(Child) causes the app to import Child.ts while decorating Note: the class Parent has not yet been reached by the code In Child.ts , the @Test(Parent) decorator is executed At this point, Parent is undefined and cannot be passed to the decorator. As you see,

Decorating method (class methods overloading)

a 夏天 提交于 2019-12-21 17:28:17
问题 Inspired by Muhammad Alkarouri answer in What are good uses for Python3's "Function Annotations" , I want to do this multimethod for methods, not regular functions. However, when I do this registry = {} class MultiMethod(object): def __init__(self, name): self.name = name self.typemap = {} def __call__(self, *args): types = tuple(arg.__class__ for arg in args) # a generator expression! function = self.typemap.get(types) if function is None: raise TypeError("no match") return function(*args)

Python decorator with options

夙愿已清 提交于 2019-12-21 12:57:51
问题 I have a module that has a function whose prototype is similar to that of the thread class. def do(fn, argtuple=(), kwargdict={}, priority=0, block=False, timeout=0, callback=None, daemon=False) # do stuff fn is a callable, and argtuple and kwargdict are positional and dictionary arguments that will be passed to the fn callable when it gets called. I'm now trying to write a decorator for this, but I'm confused. I've never really had a very good grasp on decorators. Is there a way to make a

How to combine multiple property decorators in Typescript?

≯℡__Kan透↙ 提交于 2019-12-21 12:42:16
问题 I have a class Template with a property _id which has decorators from class-transformer and typed-graphql import {classToPlain, Exclude, Expose, plainToClass, Type } from 'class-transformer'; import { ExposeToGraphQL } from '../../decorators/exposeToGraphQL'; import { Field, ID, MiddlewareInterface, NextFn, ObjectType, ResolverData } from 'type-graphql'; import { getClassForDocument, InstanceType, prop, Typegoose } from 'typegoose'; /** * Class * @extends Typegoose */ @Exclude() @ObjectType()

How can I decorate an instance of a callable class?

守給你的承諾、 提交于 2019-12-21 12:40:11
问题 def decorator(fn): def wrapper(*args, **kwargs): print 'With sour cream and chives!', return fn(*args, **kwargs) return wrapper class Potato(object): def __call__(self): print 'Potato @ {} called'.format(id(self)) spud = Potato() fancy_spud = decorator(Potato()) With this code we have two instances of callable class, one is decorated and one is plain: >>> spud() Potato @ 140408136280592 called >>> fancy_spud() With sour cream and chives! Potato @ 140408134310864 called I wonder if it is

Python decorators on class members fail when decorator mechanism is a class

前提是你 提交于 2019-12-21 12:26:19
问题 When creating decorators for use on class methods, I'm having trouble when the decorator mechanism is a class rather than a function/closure. When the class form is used, my decorator doesn't get treated as a bound method. Generally I prefer to use the function form for decorators but in this case I have to use an existing class to implement what I need. This seems as though it might be related to python-decorator-makes-function-forget-that-it-belongs-to-a-class but why does it work just fine

Python decorating functions before call

a 夏天 提交于 2019-12-21 09:26:31
问题 I have a rather complex decorator written by someone else. What I want to do is call a decorated version of the function one time based on a descision or call the original function (not decorated) another time. Is this possible? 回答1: With: decorator(original_function)() Without: original_function() A decorator is just a function which takes a function as an argument and returns another one. The @ syntax is totally optional. Perhaps a sift through some documentation might help clarify things.