10 Topics

Member Avatar for
Member Avatar for vegaseat
Member Avatar for vegaseat
4
679
Member Avatar for vegaseat

With the advent of Python33 the module time has received a high resolution timer/counter. Here we use time.perf_counter() in a decorator to conveniently time two test functions.

3
2K
Member Avatar for vegaseat
Member Avatar for vegaseat

If you decorate a function, then some of the information is lost during the debugging process. Python supplies another decorator @wraps to help out. A decorator for a decorator, very interesting.

1
431
Member Avatar for falek.marcin

I came across a problem that I can't solve and it's associated with multiprocessing and use it inside the decorator. When I'm calling the method **run_in_parallels** using multiprocessing I 'm getting the error: `Can't pickle <function run_testcase at 0x00000000027789C8>: it's not found as __main__.run_testcase` The call takes place inside the …

Member Avatar for falek.marcin
0
1K
Member Avatar for Gribouillis

This snippet defines a decorator @mixedmethod similar to @classmethod, which allows the method to access the calling instance when it exists. The decorated functions have two implicit arguments [i]self, cls[/i], the former having a None value when there is no instance in the call. Using python's descriptor protocol, the implementation …

Member Avatar for TrustyTony
1
405
Member Avatar for Gribouillis

This snippet defines a simple decorator to post-process a function's return value through a sequence of filters. As a first application, the decorator can transform a generator function into a function returning a sequence type (list, tuple, dict).

Member Avatar for Gribouillis
2
569
Member Avatar for Gribouillis

This snippet defines a decorator to automate attributes setting in class instances at creation time. It can be seen as a gadget feature but it can also be handy especially for fast class prototyping.

Member Avatar for Gribouillis
0
254
Member Avatar for TrustyTony

Here is simple math program to list number whose all factors are in given set of factors. I have included my timing decorator, which uses my timestring function, so I included that and imports for decorator in the decorator function, bit against the rules. Of course I could have used …

0
271
Member Avatar for Purkinje

I have a question about function-based decorators. When I make the first call to sqrt(), I enter the decorator function, which I understand. However, every subsequent call to sqrt() only calls the temp_func, not the actual decorator again. I thought decorators were called every time the method is called, but …

Member Avatar for Purkinje
0
188

The End.