I was wondering if there was a way to ensure some function was called in all Flask API endpoints without manually calling the function? This is to prevent repetition in making sure the functions get called, and to avoid mistakes of accidentally forgetting to call it. Maybe with a library or decorator? So i want Thanks in advance! Answer You
Tag: decorator
How to obtain parameters from decorated imported function?
i’d like to obtain the parameter from the decorated function. It’s a lil bit different from the usual one. Here is the code output: i need fetch: context, 1,2,3(parameters in test_function) in remotable_timer i know the code below can be a potential way to achieve it, but i have to decorate remotable_cache in line anyway. Dose anyone have any idea,
Passing a method of self to decorator as an argument
I have a class A, which has a lot of old complex methods, that I have re-written. I want to gradually replace a.old_func with `a.new_func. but first, I want to make sure that the new method always works the same way as the old one. So I’ve written a decorator to check that: And I wanted to call it like
How to pytest that a decorator is assigned to multiple functions
I read somewhere on SO that you should not test the decorator but the functionality of the wrapped function. Nevertheless there might be a shorter way of testing whether a certain decorator is assigned to multiple functions. I have this decorator: I have some tests to test the decorator function itself, e.g.: Now I have about 20 FastAPI endpoints where
Pylint throws not-callable on decorator defined within class
Im trying to create a class that has a time remaining property and executing functions within it reduces this time. Here is the code Then when i instantiaite the class and use do_something like so: It works as expected. However when i run pylint on the code i get an error: Is this a false positive or is there a
How to pass self attributes to a decorator?
I have a decorator for an object method: I want to add another level of wrapping around this to allow parameters: Now, what I want to be able to do is pass a self attribute as a wrapper parameter, i.e. call it with something like this I see 2 options: make self._foo global don’t pass self._foo, and make the decorator
How to wrap a decorator around another classes method?
I have created a decorator which I am using to manage logging. I want logging to occur before and after the decorated function runs. The function works fine when interacting with very basic functions, however, when interacting with methods that are a part of other classes, things break. I suspect the issue is a result of there being 2 self
How to reassign a class method after calling another method?
I am currently working on a Python project with a couple class methods that are each called tens of thousands of times. One of the issues with these methods is that they rely on data being populated via another method first, so I want to be able to raise an error if the functions are called prior to populating the
Good way to “wrap” the opening and closing of a database around functions in Python?
I’ve looked at a few related questions on StackOverflow and at some documentation/guides regarding wrappers, all of which tells me “no,” but this doesn’t seem right. That said, I’m very new to programming, so 🤷♂️ Problem: Opening and closing a database (using python/sqlite3) requires a tedious amount of repeated code (as I understand it): So, I tried to write a
Python: How to pass variables into decorators AND decorators pass decorator variables back into function as well?
I am always looping through the files of a directory to perform various kinds of data manipulations. Thus, I always use the following code Instead of keep writing these lines of code for every function, I was wondering if I can make it better by using decorators. Like the following: I have found many resources out there which focus on