decorator

How to write a custom decorator in django?

一曲冷凌霜 提交于 2019-12-17 08:18:48
问题 The problem - @is_premium_user def sample_view: ....... ...... I want certain views accesible to only the premium users of the website. And how can I use this decorator across various applications in my project? 回答1: You don't have to write your own decorator for this as user_passes_test is already included in Django. And there's a snippet (group_required_decorator) that extends this decorator and which should be pretty appropriate for your use case. If you really want to write your own

How to get all methods of a python class with given decorator

六眼飞鱼酱① 提交于 2019-12-17 07:02:29
问题 How to get all methods of a given class A that are decorated with the @decorator2? class A(): def method_a(self): pass @decorator1 def method_b(self, b): pass @decorator2 def method_c(self, t=5): pass 回答1: Method 1: Basic registering decorator I already answered this question here: Calling functions by array index in Python =) Method 2: Sourcecode parsing If you do not have control over the class definition , which is one interpretation of what you'd like to suppose, this is impossible

How to get all methods of a python class with given decorator

喜欢而已 提交于 2019-12-17 07:02:11
问题 How to get all methods of a given class A that are decorated with the @decorator2? class A(): def method_a(self): pass @decorator1 def method_b(self, b): pass @decorator2 def method_c(self, t=5): pass 回答1: Method 1: Basic registering decorator I already answered this question here: Calling functions by array index in Python =) Method 2: Sourcecode parsing If you do not have control over the class definition , which is one interpretation of what you'd like to suppose, this is impossible

Decorator execution order

无人久伴 提交于 2019-12-17 06:33:46
问题 def make_bold(fn): return lambda : "<b>" + fn() + "</b>" def make_italic(fn): return lambda : "<i>" + fn() + "</i>" @make_bold @make_italic def hello(): return "hello world" helloHTML = hello() Output: "<b><i>hello world</i></b>" I roughly understand about decorators and how it works with one of it in most examples. In this example, there are 2 of it. From the output, it seems that @make_italic executes first, then @make_bold . Does this mean that for decorated functions, it will first run

Python memoising/deferred lookup property decorator

房东的猫 提交于 2019-12-17 02:03:28
问题 Recently I've gone through an existing code base containing many classes where instance attributes reflect values stored in a database. I've refactored a lot of these attributes to have their database lookups be deferred, ie. not be initialised in the constructor but only upon first read. These attributes do not change over the lifetime of the instance, but they're a real bottleneck to calculate that first time and only really accessed for special cases. Hence they can also be cached after

Python memoising/deferred lookup property decorator

♀尐吖头ヾ 提交于 2019-12-17 02:03:11
问题 Recently I've gone through an existing code base containing many classes where instance attributes reflect values stored in a database. I've refactored a lot of these attributes to have their database lookups be deferred, ie. not be initialised in the constructor but only upon first read. These attributes do not change over the lifetime of the instance, but they're a real bottleneck to calculate that first time and only really accessed for special cases. Hence they can also be cached after

How to implement a typescript decorator?

杀马特。学长 韩版系。学妹 提交于 2019-12-16 22:39:11
问题 TypeScript 1.5 now has decorators. Could someone provide a simple example demonstrating the proper way to implement a decorator and describe what the arguments in the possible valid decorator signatures mean? declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void; declare type PropertyDecorator = (target: Object, propertyKey: string | symbol) => void; declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor:

How do the Proxy, Decorator, Adapter, and Bridge Patterns differ?

天大地大妈咪最大 提交于 2019-12-16 22:19:31
问题 I was looking at the Proxy Pattern, and to me it seems an awful lot like the Decorator, Adapter, and Bridge patterns. Am I misunderstanding something? What's the difference? Why would I use the Proxy pattern versus the others? How have you used them in the past in real world projects? 回答1: Proxy, Decorator, Adapter, and Bridge are all variations on "wrapping" a class. But their uses are different. Proxy could be used when you want to lazy-instantiate an object, or hide the fact that you're

Is there a mechanism for something like python decorators in GNU Makefiles?

▼魔方 西西 提交于 2019-12-14 03:52:23
问题 I find myself a little torn between two possibilities to declare GNU make tragets phony in Makefiles. One is declaring all phonies in one go: .PHONY: targ1 targ2 targ3 targ1: ... targ2: ... targ3: ... Which has the advantage of being more readable (to me) and more tidy. But one can't see quickly which targets are phony. The other possibility is declaring the phoniness along with the rule (directly in front or behind): .PHONY: targ1 targ1: ... .PHONY: targ2 targ2: ... targ3: ... .PHONY: targ3

scala: trait for a Function object with variable length arguments?

五迷三道 提交于 2019-12-14 03:48:16
问题 i'm writing some scala code to emulate a python decorator. i'm considering implementing this by having the decorator extend a Function trait. the issue is that i want this decorator to extend a Function that accepts any number of arguments, and the only Function traits i can find only allow a specific number of arguments, e.g. Function1, Function2, etc. does such a trait exist? alternatively, is there a better way to implement such a decorator? Edit: I recast this question to be more clear at