decorator

Django decorator generates SiteProfileNotAvailable error

拜拜、爱过 提交于 2019-12-25 01:08:04
问题 Newbie to Python (2.6) and Django (1.2) here, learning the ropes. Consider the following decorator that I want to use on methods, along with @login_required, that redirects users to a profile completion url if they try and do something that requires a "minimum information supplied" profile. The usage pattern is intended to be: @login_required @min_profile_required def my_view(request): # do whatever. My current defintion of the min_profile_required decorator is as follows: def min_profile

Creating a decorator / cache for checking global variable

狂风中的少年 提交于 2019-12-25 00:56:42
问题 I've quite a few functions that uses some global variables to hold an object to be reused throughout the library, e.g.: from some.other.lib import Object1, Object2, Object3, Object4, Object5 def function1(input): global _object1 try: _object1 except NameError: _object1 = Object1 return _object1.func1(input) def function2(input): global _object2 try: _object2 except NameError: _object2 = Object2 # Use function1 to do something to the input return _object2.func2(function1(input)) def function3

Class with a registry of methods based on decorators

♀尐吖头ヾ 提交于 2019-12-24 23:40:46
问题 I have a class that has several methods which each have certain properties (in the sense of quality). I'd like these methods to be available in a list inside the class so they can be executed at once. Note that the properties can be interchangeable so this can't be solved by using further classes that would inherit from the original one. In an ideal world it would look something like this: class MyClass: def __init__(): red_rules = set() blue_rules = set() hard_rules = set() soft_rules = set(

How to decorate property with ExpandableObject in C#

a 夏天 提交于 2019-12-24 12:17:06
问题 I am trying to display this class instance in the Xceed PropertyGrid per the instructions here: PG.SelectedObject = new Order() { ShipAddress = "Luisenstr. 48", ShipCountry = "Germany", ShipName = "Toms Spezialitaten", ShipPostalCode = "44087", chronology = new OrderChronology() { OrderDate = new DateTime(1996, 7, 5), ShippedDate = new DateTime(1996, 8, 16) } }; The Xceed example for behavior analogous to what I am trying to do says you must decorate your property with the ExpandableObject

template meta-programming OR operation

谁说我不能喝 提交于 2019-12-24 11:57:14
问题 I have a class that can be decorated with a set of add-on templates to provide additional functionality. Each add-on has an identifying addon_value that the base class needs to know. The code below is an example of what I would like to do. Obviously, the main() function fails to compile. The goal is for CBase::GetValueOfAddOns() to know the value of OR-ing the addon_value for each add-on. The calculation does not actually have to be performed in GetValueOfAddOns(), it just has to be able to

A timeout decorator class with multiprocessing gives a pickling error

喜欢而已 提交于 2019-12-24 04:36:05
问题 So on windows the signal and the thread approahc in general are bad ideas / don't work for timeout of functions. I've made the following timeout code which throws a timeout exception from multiprocessing when the code took to long. This is exactly what I want. def timeout(timeout, func, *arg): with Pool(processes=1) as pool: result = pool.apply_async(func, (*arg,)) return result.get(timeout=timeout) I'm now trying to get this into a decorator style so that I can add it to a wide range of

Decorator with parameters

南笙酒味 提交于 2019-12-24 02:40:50
问题 Can you explain me how the following decorator works: def set_ev_cls(ev_cls, dispatchers=None): def _set_ev_cls_dec(handler): if 'callers' not in dir(handler): handler.callers = {} for e in _listify(ev_cls): handler.callers[e] = _Caller(_listify(dispatchers), e.__module__) return handler return _set_ev_cls_dec @set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER) def _switch_features_handler(self, ev): datapath = ev.msg.datapath .... Please, don't go into details on what's going on

Is there a way to run a method automatically on the initialization of an instance without using __init__?

ぐ巨炮叔叔 提交于 2019-12-24 02:33:37
问题 I am writing some unit tests with Pytest. If I want them to be collected automatically, I have to avoid the __init__ constructor. (If there's a way to make Pytest collect tests with the __init__ constructor I'd take that as an alternate useful answer.) My unit tests have some variables and methods in common. Right now I have base test class TestFoo, and child test class TestBar(TestFoo), and grandchild test class TestBaz(TestBar). Since I can't have an init method, right now I'm calling a

What's the difference to use @staticmethod and global function in Python?

ぃ、小莉子 提交于 2019-12-23 20:47:29
问题 I have read What is the difference between @staticmethod and @classmethod in Python? Python @classmethod and @staticmethod for beginner? As staticmethod can't access the instance of that class, I don't know what's the difference betweent it and global function ? And when should use staticmethod ? Can give a good example? 回答1: Like global function, static method cannot access the instance of the containing class. But it conceptually belongs to the containing class. The other benefit is it can

Class Decorator in Typescript

牧云@^-^@ 提交于 2019-12-23 20:30:23
问题 I'm trying to understand how class decorators work in Typescript when we wish to replace the constructor. I've seen this demo: const log = <T>(originalConstructor: new(...args: any[]) => T) => { function newConstructor(... args) { console.log("Arguments: ", args.join(", ")); new originalConstructor(args); } newConstructor.prototype = originalConstructor.prototype; return newConstructor; } @log class Pet { constructor(name: string, age: number) {} } new Pet("Azor", 12); //Arguments: Azor, 12