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: python-decorators
Using a class decorator to automatically run a method with a child process
I was asked to develop a consistent way to run(train, make predictions, etc.) any ML model from the command line. I also need to periodically check the DB for requests related to training, like abort requests. To minimize the effect checking the DB has on training, I want to create a separate process for fetching requests from the DB. So
Why am I unable to get docstring printed out using sphinx python package?
I am using Sphinx to document out my functions. However, it does not read my doc strings for function with a decorator. Sphinx just replace the function doc strings with <The magic happens here> My functions are written in this manner with a decorator. How do i make Sphinx detect the doc strings in the function. Folder is in this
Args do not unpack
I’m learning flask and i want to unpack args in inner function. That’s my code: I’m getting an arror “tuple index out of range” even if I add next asterix in Can I unpack it anywhere else? Answer If you print args and kwargs, you will find that user is passed in kwargs (by calling func(user=xxx)). The problem is that
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 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
Integration test for decorated function
I have a problem in writing integration test for some function that calls other function which is decorated. Suppose the following definitions: I need to write test to ensure that any exception raised inside problematic_func will not be propagated to func_under_test. For this reason, I used mocking like below: Problem is that I can’t pass this test. Patching problematic_func caused
Function wrapper for multiple code lines in Python
I would like to wrap some code around a function (e.g. print timing information) and reuse this function for multiple code blocks: Is it possible to wrap multiple lines of code into a wrapper function, without defining functions for each code blocks? One way would be to define a function and use a decorator but I would like to avoid
How to avoid name collisions in python decorators functions
I would like to write a python decorator so that a function raising an exception will be run again until either it succeeds, or it reaches the maximum number of attempts before giving up. Like so : My problem is that I want a guarantee that no matter what names the kwargs contain, there cannot be a collision with the
Using the @property decorator for interrelated attributes within a Python class
This question relates to using the @property decorator within a class in Python. I want to create a class with two public attributes (say a and b). I also want the class to have a further public attribute (say c) that is always equal to some calculation based on the other attributes within the class (say a + b =