decorator

Changing class attributes in decorator

Changing class attributes in decorator Question: I’m using django as my framework for some web application. I implemented a modelview of my own because I have a few querysets and seriazliers in the same view. For this use, I needed to implement all of the CRUD functions myself: class Models1AndModel2View(mixins.CreateModelMixin, mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, mixins.ListModelMixin, GenericViewSet): …

Total answers: 1

decorator argument: unable to access instance attribute

decorator argument: unable to access instance attribute Question: Context class MyMockClient below has two methods: push_log: pushes one log from list self.logs and pops it push_event: pushes one event from list self.events and pops it Both methods are decorated using __iterate_pushes which essentially iterate push_event and push_log until their respective lists are entirely popped AND …

Total answers: 1

Automatically add methods to a class in Python

Automatically add methods to a class in Python Question: I’m trying to create a custom class to handle timeseries related to various objects. They all inherit from a base class called Timeseries, which looks like this class Timeseries: def __init__(self, t_max: int): self.t_max = t_max self.current_t = 0 def update(self): if self.current_t == self.t_max: raise …

Total answers: 1

Automatically add decorator to all inherited methods

Automatically add decorator to all inherited methods Question: I want in class B to automatically add the decorator _preCheck to all methods that have been inherited from class A. In the example b.double(5) is correctly called with the wrapper. I want to avoid to manually re-declare (override) the inherited methods in B but instead, automatically …

Total answers: 2

Error occuring when I try to run decorator with @ – Python

Error occuring when I try to run decorator with @ – Python Question: I am having a problem with the following programm. When I try to run decorator the easier way, using @ like this def decorator1(fun): def wrapper(): text = ‘——‘ return text + ‘n’ + fun + ‘n’ + text return wrapper() def …

Total answers: 2

Count how many times each function gets called

Count how many times each function gets called Question: I want to count how many times each function get called. I have a wrapper to do the counting and save it into a global variable def counter(f): global function_calls function_calls = 0 def wrapper(*args, **kwargs): global function_calls function_calls += 1 return f(*args, **kwargs) return wrapper …

Total answers: 2

`__set_name__` hook manually added to `functools.wraps()` descriptor instance never called

`__set_name__` hook manually added to `functools.wraps()` descriptor instance never called Question: I’m trying to add a __set_name__ hook to the descriptor produced by functools.wraps inside a decorator, but it is never called and I don’t see any error messages: import functools def wrap(fn): """Decorator.""" @functools.wraps(fn) def w(*args, **kwargs): return fn(*args, **kwargs) # This never gets …

Total answers: 1

python decorate class method sending a parameter to the decorator

python decorate class method sending a parameter to the decorator Question: I have the following class in python: class MatrixOfCarriers(DataLoader): def load_data(self): brands = self.brands try: data = self._load_data() print("’Carrier Matrix’ data loaded successfully.n") except Exception as e: print( "nCouldn’t load ‘Carier Matrix’ data due to the following " + "error: ‘{}’".format(str(e)) ) raise e …

Total answers: 1

How to avoid losing type hinting of decorated function

How to avoid losing type hinting of decorated function Question: I noticed that when wrapping a function or method that have some type hinting then the wrapped method loses the type hinting informations when I am coding using Visual studio code. For example with this code: from typing import Callable import functools def decorate(function: Callable): …

Total answers: 1

Strange python closure variable access

Strange python closure variable access Question: While trying to implement a decorator with a closure I ran into a somewhat strange behavior, where a variable can be read, but if try to assign it later, it becomes undefined even before the assignment. def run_once(fn): to_run = True def decorated(*args, **kwargs): if to_run: print(to_run) #this access …

Total answers: 1