decorator

Determining if root logger is set to DEBUG level in Python?

If I set the logging module to DEBUG with a command line parameter like this: if (opt["log"] == "debug"): logging.basicConfig(level=logging.DEBUG) How can I later tell if the logger was set to DEBUG? I'm writing a decorator that will time a function if True flag is passed to it, and if no flag is given, it defaults to printing timi...

Why can @decorator not decorate a staticmethod or a classmethod?

Why can decorator not decorate a staticmethod or a classmethod? from decorator import decorator @decorator def print_function_name(function, *args): print '%s was called.' % function.func_name return function(*args) class My_class(object): @print_function_name @classmethod def get_dir(cls): return dir(cls) ...

What are Python metaclasses useful for?

What can be done with metaclasses that can't be in any other way? Alex Martelli told that there are tasks that can't be achieved without metaclasses here http://stackoverflow.com/questions/1779372/python-metaclasses-vs-class-decorators I'd like to know which are? ...

Python decorator with instantiation-time variable?

I want to make a decorator that creates a new function/method that makes use of an object obj. If the decorated object is a function, obj must be instantiated when the function is created. If the decorated object is a method, a new obj must be instantiated and bound to each instance of the class whose method is decorated. I can't put the...

Question about decorator pattern and the abstract decorator class?

This question was asked already here, but rather than answering the specific question, descriptions of how the decorator pattern works were given instead. I'd like to ask it again because the answer is not immediately evident to me just by reading how the decorator pattern works (I've read the wikipedia article and the section in the bo...

See What Line a Function Was Called From in Python Decorator

Given something like this: @my_decorator my_function(some, args) Is it possible for my_decorator to discover the file and line number my_function was called from? Thanks ...

Python: Very confused about decorators...

I thought I understood decorators but not anymore. Do decorators only work when the function is created? I wanted to create a series of functions that all have a required argument called 'ticket_params' that is a dictionary. and then decorate them with something like @param_checker(['req_param_1', 'req_param_2']) and then if 'req_param_...

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

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): ...

python parent class 'wrapping' child-class methods

Hello all, I have the following situation in my python code: class Parent(object): def run(self): print "preparing for run" self.runImpl() print "run done" class Child(Parent): def runImpl(self): print "child running" However, I have several such 'decorators', doing different setup/teardown ste...

Caching non-view returns

I have a dozen or so permission lookups on views that make sure users have the right permissions to do something on the system (ie make sure they're in the right group, if they can edit their profile, if they're group administrators, etc). A check might look like this: from django.contrib.auth.decorators import user_passes_test test_c...

Fine-grained decorator pattern

I understand the Decorator pattern, in it's simplest terms. The idea being that one class wraps another, where a decorator method wishes to run some other code before and/or after calling the same method on the decorated object. However, I have run into the situation where I cannot simply call the decorated method, as it has some undes...

Why can't Python decorators be chained across definitions?

Why arn't the following two scripts equivalent? (Taken from another question: Understanding Python Decorators) def makebold(fn): def wrapped(): return "<b>" + fn() + "</b>" return wrapped def makeitalic(fn): def wrapped(): return "<i>" + fn() + "</i>" return wrapped @makebold @makeitalic def hello(): ...

What side effects should one expect when method decorator replaces self?

I want to execute a method with a copy of the original self passed while execution. Here is the code I'm talking about: def protect_self(func): from copy import copy from functools import wraps @wraps(func) def decorated(self, *args, **kwargs): self_copy = copy(self) return func(self_copy, *args, **kwarg...

How do I use ViewScripts on Zend_Form File Elements?

I am using this ViewScript for my standard form elements: <div class="field" id="field_<?php echo $this->element->getId(); ?>"> <?php if (0 < strlen($this->element->getLabel())) : ?> <?php echo $this->formLabel($this->element->getName(), $this->element->getLabel());?> <?php endif; ?> <span class="value"><?php echo $this->...

zend form decorators

Having (more) issues with zend form decorators. I've got this so far: Reset overall form decorator: $this->clearDecorators(); $this->setDecorators(array('FormElements', 'Form')); I'm adding all my elements to a display group that i want to be inside a fieldset, within a DL $group->setDecorators(array( 'FormEle...

Can you remove a decorator?

Is it possible to remove a decorator from an object? Say I have the following code: abstract class Item { decimal cost(); } class Coffee : Item { decimal cost() { // some stuff } } abstract class CoffeeDecorator : Item { Item decoratedItem; } class Mocha : CoffeeDecorator { Item decoratedItem; public Mocha(...

How do I design classes in my role playing game to allow multi-classing?

I am programming a game as an exercise and I've run into a design problem. My role playing game will have the typical classes like Fighter, Wizard, Theif, Cleric. How do I design my classes so that players can multi-class? For example, one player might start off as a Fighter (and gain the related skills fighters have), then multi-class t...

How can I pack serveral decorators into one?

I have several decorators on each function, is there a way to pack them in to one instead? @fun1 @fun2 @fun3 def do_stuf(): pass change to: @all_funs #runs fun1 fun2 and fun3, how should all_funs look like? def do_stuf(): pass ...

Design considerations for temporarily transforming a player into an animal in a role playing game

I am working on a role playing game for fun and to practice design patterns. I would like players to be able to transform themselves into different animals. For example, a Druid might be able to shape shift into a cheetah. Right now I'm planning on using the decorator pattern to do this but my question is - how do I make it so that when ...

Zend_Form overriding element defaults for custom layout

I am currently trying to build a simple custom layer that I will extend instead of Zend_Form. For instance, My_Form. I want all my forms to look the same so I am setting this in My_Form. Here is what it is so far. class My_Form extends Zend_Form { protected $_elementDecorators = array( 'ViewHelper', 'Errors', ...