decorator

Python get the total execution time of a function

Python get the total execution time of a function Question: Suppose that I have a function: def func(x): time.sleep(x) return x It will be called in every place when needed, for example, func_a in class A, func_b in class B… And all the modules start from a main function, main(). Now I want to statistic …

Total answers: 1

Decorator causing my function to not accept positional argument?

Decorator causing my function to not accept positional argument? Question: I am learning how to use decorators. Inspired by tqdm library (progress bars), I have cobbled together a decorator, heavily inspired by this code. The goal is to use multithreading to have a progress indicator blink/spin/count in stdout while a long-running function that does not …

Total answers: 2

Pass instance attribute as a decorator parameter

Pass instance attribute as a decorator parameter Question: I am creating a class Encuesta to create multiple surveys via user commands for a discord bot using discord.py. To achieve this, I have to pass the attribute "self.buttonid" as the ‘custom_id’ parameter for the decorator @discord.ui.button (already defined by discord.py). class Encuesta(discord.ui.View): def __init__(self, pregunta, buttonid): …

Total answers: 1

How can I update an attribute that's added to a method via a decorator?

How can I update an attribute that's added to a method via a decorator? Question: I’ve created a decorator that wraps tkinter’s after() method to make looping functions easier (i.e., having a function call itself periodically) import tkinter as tk from functools import wraps # decorator def after_loop(interval: int): """Loop the decorated method at the …

Total answers: 1

Why can't I pass a decorated function to scipy.integrate.ode?

Why can't I pass a decorated function to scipy.integrate.ode? Question: When I pass a decorated function to scipy.integrate.ode, the wrapper function gets called, but *args is empty. Why is this happening? This works: y0, t0 = 1, 0 def dydt(t, y): return y*0.5 r = scipy.integrate.ode(dydt) r.set_initial_value(y0) r.integrate(10) assert r.successful() This doesn’t: y0, t0 = …

Total answers: 2

Passing a python class constant to a decorator without self

Passing a python class constant to a decorator without self Question: I’m using the python ratelimit library and it uses a decorator to handle rate limiting of a function. I have a class constant I’d like to pass into the decorator but of course self wont work. Is there a way to reference the UNIVERSALIS_MAX_CALLS_PER_SECOND …

Total answers: 2

Python – create wrapper function for SQL queries

Python – create wrapper function for SQL queries Question: In my project I have lots of functions that carry out SQL queries. These queries include SELECT, UPDATE, INSERT etc… When writing functions for SELECT queries for example, I write the same structure of code in every single function. e.g. def generic_select_function(self): result = self.cursor.execute(""" SQL …

Total answers: 1

Clear list inside decorator from global scope

Clear list inside decorator from global scope Question: def outer(func): a = [] def inner(): func() a.append(‘work’) print(a) return inner @outer def test(): print(‘TEST’) test() test() # *1 test() # *2 *1 here I want clear list a inside a decorator *2 so here must be a new list a inside outer Is it posible …

Total answers: 3

How can I dynamically extend arguments of a method?

How can I dynamically extend arguments of a method? Question: The methods should be callable by assigning the specific parameters as well as considering the class’ attributes. So what I like to achieve is overwriting a method’s arguments with preset attributes. E.g.: class Obj: def __init__(self, cfg=None): self.cfg = {} if cfg: self.cfg = cfg …

Total answers: 2