python-decorators

adding __getitem__ accessor to Python class method

adding __getitem__ accessor to Python class method Question: I’m attempting to add an item getter (__getitem__, to provide the [] syntax) to a class method so that I can use some unique-ish syntax to provide types to functions outside the normal parentheses, like the following. The syntax on the last line (of this first snippet) …

Total answers: 5

running python script again based on user answer using decorator

running python script again based on user answer using decorator Question: Im trying to run the script again, based on user input(y/n). In my main file Im calling couple of functions from another file, with the decorator Ive seen its usage in older post here, but cant find why Im getting an error: " game() …

Total answers: 1

Create a generic logger for function start and finish time in Python

Create a generic logger for function start and finish time in Python Question: I want to create a logger decorator that will print before and after a function, in addition I want to add some information that if avaiable will appear in each line, for example most of our current logs look like: START: reading_data …

Total answers: 2

Type hinting decorator with bounded arguments

Type hinting decorator with bounded arguments Question: An example of what I’d like to do is as follows: @dataclass class Arg1: x = field() @dataclass class Arg2: x = field() @dataclass class Obj: y = field() T = TypeVar("T") R = TypeVar("R", bound=(Arg1 | Arg2)) C = TypeVar("C", bound=Callable[[Self, R], T]) @staticmethod def deco(f: C) …

Total answers: 2

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

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

Python decorate `class.method` that modify on `class.self`

Python decorate `class.method` that modify on `class.self` Question: How to access self of the decorated method? Based on this answer, the self refer to the decorator: class Decorator: def __init__(self, func): self.func = func def __call__(self, *args, **kwargs): print(self.func.__name__) self.counter += 1 return self.func(*args, **kwargs) class A: def __init__(self): self.counter = 0 @Decorator def method1(self): …

Total answers: 1

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