2,518 questions
1
vote
3
answers
110
views
How to get traceback from a module using a decorator?
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
...
1
vote
0
answers
83
views
Avoid using global variables whilst allowing a wrapper to modify these variables
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 ...
0
votes
2
answers
112
views
Adding decorator to a python class
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 ...
3
votes
1
answer
230
views
Timefold Python project terminates on encountering Timefold decorators
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 ...
2
votes
1
answer
60
views
Wrap class method with argument(s) only once
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 +...
3
votes
2
answers
182
views
How to add function parameter kwargs during runtime?
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 ...
0
votes
1
answer
182
views
Using python decorators associated with an instance level object
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 ...
1
vote
1
answer
58
views
Django decorator import issue: 'cannot import name' when used in views.py
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 ...
0
votes
0
answers
68
views
Writing decorator adding callables taking objects of generic type to instance list
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 ...
0
votes
1
answer
91
views
Why does inspect identify decorated methods as functions instead of methods in Python?
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 ...
0
votes
1
answer
178
views
How to gracefully skip pytest tests if optional dependencies are not installed?
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 ...
0
votes
0
answers
38
views
Profile selected functions in Python
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 ...
0
votes
2
answers
36
views
How to dynamically choose between two methods with parameters using a property in Python
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 ...
0
votes
1
answer
310
views
How can I convert a TypeVarTuple to a ParamSpec?
(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
...
0
votes
1
answer
172
views
Internal function call in FastAPI / Parameter that I can set only inside the FastAPI app
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'...
0
votes
2
answers
115
views
How to combine multiple @staticmethod decorators into a single one
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 ...
0
votes
2
answers
104
views
Is there a way to find undecorated classes/functions in a Python module?
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()...
-1
votes
3
answers
101
views
Re-decorate a python (class) decorator [duplicate]
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.
...
0
votes
1
answer
157
views
Optimizing dynamic programming solution for Abbreviation problem- Hackerrank
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, ...
1
vote
1
answer
180
views
Python annotate adding class members in __get__
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 ...
0
votes
1
answer
79
views
How to get response content from a Response() object in a view decorator?
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, ...
1
vote
1
answer
235
views
How to split python-decorated API router methods in different files
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 ...
1
vote
1
answer
187
views
How to deduce whether a classmethod is called on an instance or on a class
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 ...
1
vote
1
answer
52
views
Having trouble seeing the motive for decorators in *simple* examples
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 ...
0
votes
1
answer
272
views
How do I type hint for class wrapper or class with method assigned elsewhere than definition in Python3?
In some cases, it is required to inject the same method to multiple classes. To avoid repeated code, I can use a class decorator or class wrapper to assign new attributes and methods to specific class....