Skip to main content
Filter by
Sorted by
Tagged with
1 vote
2 answers
78 views

I want to add a decorator on a FastAPI route like this: /admin/verification.py from functools import wraps import json def admin_required(admin_function): @wraps(admin_function) def ...
gtj520's user avatar
  • 383
-2 votes
3 answers
127 views

I need to set class attributes by calling a function during class definition. A very simplified example would look like the following. class Mammal(): # Base class provided by internal library ...
bflynt's user avatar
  • 11
2 votes
2 answers
143 views

I use the "decorator" idea as way to parameterize functions, but ran into pickling errors when passing these functions as targets to the multiprocessing module. I found a work-around by ...
Donna's user avatar
  • 1,692
1 vote
3 answers
126 views

I have a decorator in the root of a package that should log out information about unexpected exceptions. from logging import getLogger from os.path import basename from traceback import extract_tb ...
ludovico's user avatar
  • 135
1 vote
3 answers
107 views

I'm using Python decorators to implement common functionality across multiple classes. The problem comes when I want the decorated classes to inherit from each other: super() doesn't seem to delegate ...
jezza's user avatar
  • 522
1 vote
0 answers
93 views

I am attempting to create a decorator which prints the current call chain into the terminal in a similar format to the windows tree command. An object of my class type is tied to each function as they ...
lily's user avatar
  • 11
2 votes
1 answer
58 views

I am working on a decorator. To avoid duplicating code I am inheriting a parent decorator which has a nonlocal variable defined. I am not able to get the exact behaviour in my child decorator as I am ...
Rohit Pathak's user avatar
1 vote
2 answers
123 views

I have a few methods that require a certain condition to be applied to function properly. To avoid code repetition I've created a decorator to check if this condition is applied and only then execute ...
good user's user avatar
5 votes
2 answers
181 views

I have a decorator factory which just multiplies some given numbers. from functools import wraps def multiply(numbers): def decorator(f): def wrapper(*args, **kwargs): num1 = numbers[0] ...
Aadvik's user avatar
  • 1,532
3 votes
1 answer
262 views

I have been working with Timefold in Python for a while and wrote a setup manual for a Timefold project. When testing this setup manual on another device we ran into a problem. It doesn't seem to ...
Steven's user avatar
  • 41
2 votes
1 answer
785 views

I'm experiencing a type checking error with Pyright when using a decorator with a method that overrides another method. The error occurs only when the method has two parameters (self and another ...
usan's user avatar
  • 189
2 votes
1 answer
66 views

There are two classes and I want to wrap item.foo method only once, to prevent cache = {param1: 'param_value'} being reinited class Foo: _count = 0 def foo(self, param2): self._count +...
Aleksandr Lekontsev's user avatar
1 vote
1 answer
125 views

I want to design a decorator that will allow the wrapped method to take a float or a numpy array of floats. If the passed argument was a float then a float should be returned and if it was a numpy ...
Nukesub's user avatar
  • 227
3 votes
2 answers
209 views

I have hundreds of functions test1, test2, ... Function testX may or may not have kwargs: def test1(x, z=1, **kwargs): pass def test2(x, y=1): pass def test3(x, y=1, z=1): pass ... and I ...
lei zhang's user avatar
  • 223
0 votes
1 answer
88 views

Type infer error for decorator class with __get__ not properly identified type with Generic lose Type[] The following are the comparisons of several scenarios. from typing import TypeVar, Callable, ...
lei zhang's user avatar
  • 223
0 votes
1 answer
189 views

I am trying to create a decorator which enables me to register callback functions to an instance of ClassB. I would like to use this decorator in other classes within my program (eg. ClassA) but want ...
bchiddy's user avatar
  • 11
1 vote
1 answer
523 views

I have this decorator in python 3.11 that sends a signal encoding the arguments to a function call that it decorates, here: def register_action(signal=None): def decorator(func): def ...
KBriggs's user avatar
  • 1,498
1 vote
1 answer
73 views

when trying to start the server it cannot find the decorator from django.contrib.auth.decorators import login_required, user_passes_test, staff_member_required ... @staff_member_required def ...
Alex's user avatar
  • 12
0 votes
1 answer
103 views

I am trying to do something like SpringBoot @Component scan to search for classes that are decorated with Temporal @worfklow.defn and @activity.defn so I can automatically register them to the worker ...
Archimedes Trajano's user avatar
0 votes
0 answers
75 views

I want to create a custom Collection (FnColl) that is specific for an ObjectType T and holds a list of Callables, each taking an object of type T and returning any value. class FnColl[T]: def ...
DblDpl's user avatar
  • 1
0 votes
1 answer
100 views

I'm working on a Python project where I'm using decorators to wrap functions and methods with some logic. However, I've encountered an issue where inspect identifies the decorated methods as functions ...
Niemand's user avatar
  • 25
0 votes
0 answers
36 views

I'm implementing custom JWT authentication in Django and I'm using middleware to decode the JWT token and assign request.user to the authenticated user. However, when I try to access request.user ...
Nasir uddin Sabbir's user avatar
0 votes
1 answer
233 views

I have a Python package with optional dependencies defined in pyproject.toml. I want to run pytest while ensuring that specific tests are executed only if the relevant optional dependencies are ...
Sam's user avatar
  • 1
0 votes
0 answers
65 views

I have a python decorator, apply_suffix which adds a suffix keyword argument to a python function. import functools def add_suffix(func): @functools.wraps(func) def wrapper(*args, suffix: str =...
illusional's user avatar
0 votes
0 answers
43 views

I can profile and visualize my entire application with: python -m cProfile -o program.prof my_program.py snakeviz program.prof Is it possible to write a decorator that will profile only some ...
Ohumeronen's user avatar
  • 2,156
3 votes
1 answer
151 views

This question is related to Calling a static method with self vs. class name but I'm trying to understand the behavior when you wrap a static method so I can fix my wrapper. For example: import ...
aiguofer's user avatar
  • 2,195
0 votes
2 answers
60 views

I have a Python class that needs to dynamically choose between two methods based on a condition. Both methods take parameters. I want to use a property to determine which method to call. How can I ...
Mikhail Goussarov's user avatar
1 vote
1 answer
835 views

I feel I'm not understanding something and don't know the correct way to typehint this. I've got a parent class and a child class. I'm trying to create a decorator with ParamSpec so I gain access to ...
AndyMac's user avatar
  • 840
0 votes
1 answer
348 views

(I am almost certain the wording of the question does not make sense, but I have yet to find a better one.) I have the following code that I want to type-check: from collections.abc import Callable ...
bers's user avatar
  • 6,371
0 votes
2 answers
61 views

I have a class that performs some useful job in my project class Test: def __init__(self): self.__value = None def set_value(self, value): print(f"set_value(): value={...
Oleksandr Masliuchenko's user avatar
0 votes
1 answer
204 views

I wrote my own session logic and use the following decorator to check the request: def require_session(func): @wraps(func) async def wrapper(*args, **kwargs): request = kwargs['request'...
kos1posha's user avatar
0 votes
4 answers
130 views

I want to write a decorator that will count the number of swaps in a sorting algorithm. I created a swap function to swap numbers and a decorator that counts each call to this function. The issues I'...
UsernameTHC's user avatar
0 votes
2 answers
149 views

I want to combine multiple @statimethod decorators (let's call them decorator_1, decorator_2, etc.) into a single @staticmethod combined_decorator. For my use case, I need the decorators to be able to ...
VRichardJP's user avatar
1 vote
0 answers
129 views

What is the correct way to specify a type annotation for a decorator that can be applied to both sync and async functions? I specify the type for the received function as type FuncType[**P, R] = ...
bahladamos's user avatar
0 votes
2 answers
140 views

I'm trying to write a tool which will help me find implementation bugs in a large existing python code base that makes heavy use of decorators. Lets say I have a file, rules.py: manager = RuleManager()...
ijustlovemath's user avatar
0 votes
0 answers
90 views

I have a class to preload data into cache when an event happens. I want to register loader functions with a decorator and an event key, so that when the event fires, I can execute the functions. from ...
cmsommerville's user avatar
-1 votes
3 answers
118 views

I'd like to create a decorator that basically wraps an already existing decorator that has parameters, such that the new decorator acts like the old one with some of the arguments supplied. ...
VY_CMa's user avatar
  • 395
0 votes
0 answers
70 views

I was working on my project about matrixes. I write method that multiply every element on number and function that sums matrices: class SquareMatrix(): def __init__(self, rows, random=False, *args)...
priant's user avatar
  • 1
0 votes
1 answer
183 views

So I personally favor coding dynamic programming solutions using a top-down approach. Especially in python because it lends itself to a rather easy recursive implementation using the cache decorator, ...
BBenyani's user avatar
  • 123
1 vote
1 answer
63 views

I would like to be able to decorate instance methods with a class Step such that the methods are replaced by a Step object. At the same time, I'd like to have the option be able to instantiate a step ...
mauro's user avatar
  • 11
1 vote
1 answer
205 views

I want to create a decorator that adds a member to the decorated class that is instance of outer class. In this case, decorator memberclass adds attribute self to the instance of class y that is of ...
Superior's user avatar
  • 885
-1 votes
2 answers
172 views

I’m learning about decorators in Python and would like to use them to modify the behaviour of several more complex functions in my code. Specifically, I want to: Log the execution time for each ...
Ieleniayu's user avatar
0 votes
1 answer
92 views

Let's say I have a decorator that can be applied to a DRF class based View. A basic version of the decorator i have is as follows:- `class LogRequest: def __init__(self, log_success=True, ...
Adhishraya Sharma's user avatar
2 votes
2 answers
108 views

I have two files t.py: import functools import traceback def wrapper(func): @functools.wraps(func) def wrapped(*args, **kwargs): try: return func(*args, **kwargs) ...
hovnatan's user avatar
  • 1,395
1 vote
1 answer
266 views

I cannot find a clean way to structure a python API project that splits the routes of a module (/invoices for this example) into different files, when using a decorator approach for the routing ...
victorperezpiqueras's user avatar
0 votes
2 answers
224 views

I am using python click options that are shared by multiple commands, as described at https://stackoverflow.com/a/77732441. Is there a simple way to customize the help= text of the list option in the ...
zapta's user avatar
  • 135
1 vote
1 answer
226 views

I'm writing a logger decorator that can be applied to, among others, classmethods (and theoretically any kind of function or method). My problem is that their parametrization changes according to ...
Gyula Sámuel Karli's user avatar
1 vote
1 answer
2k views

I guess most people familiar with jax have seen this example in the documentation and know that it does not work: import jax.numpy as jnp from jax import jit class CustomClass: def __init__(self, x:...
Stackerexp's user avatar
1 vote
1 answer
56 views

I've surfed some tutorials online about decorators. I'm having trouble seeing their benefit for simple examples. Here is a common example of a function crying out to be decorated, taken from this ...
user2153235's user avatar
  • 1,295
2 votes
1 answer
76 views

Q: Is there a better way to do this, or the idea itself is wrong I have a processing class that creates something with multiple construction steps, such that the next function depends on the previous ...
Electron X's user avatar

1
2 3 4 5
51