
[英]How to add additional arguments to existing decorator with custom decorator
我知道我的问题听起来有点傻,但是图书馆里有一个现有的装饰器,它可以接受也可以不接受 kwarg。 我想在达到特定条件时传递此参数。 所以我最初的想法是写一个装饰器来包装这个 function。但我不知道这在 Python 中是否可行。 例如,这是原始装饰器: 如果某个条件被调用,则需要像这样调用它 ...
[英]How to add additional arguments to existing decorator with custom decorator
我知道我的问题听起来有点傻,但是图书馆里有一个现有的装饰器,它可以接受也可以不接受 kwarg。 我想在达到特定条件时传递此参数。 所以我最初的想法是写一个装饰器来包装这个 function。但我不知道这在 Python 中是否可行。 例如,这是原始装饰器: 如果某个条件被调用,则需要像这样调用它 ...
[英]Decorating with arguments a Class Declaration
我正在尝试创建一个 Class 表示要为我的应用程序执行的正则表达式。 对于每个正则表达式,我都有一个指向 regex101.com 页面的链接,用户可以在其中找到单元测试。 我想使用这个解决方案在 class 声明附近有这个链接,但没有在 class 代码中。 我想到的代码必须如下所示:class ...
[英]Reuse function as pytest fixture
我的代码中有一个 function,fastapi 使用它向端点提供一个 db session: 我想使用与 pytest 夹具相同的 function。 如果我执行以下操作,则无法识别灯具: test_item_create抛出一个关于db not being a fixture: fixtur ...
[英]decorating method using instance method
我正在尝试用另一个实例方法装饰来装饰我的实例 car_details 但我无法弄清楚为什么会出现错误。 当我像下面这样调用 call car_details 方法时 然后它可以正常工作,但是当我当时用@decorator 尝试它时,我遇到了问题。 类型错误:decor() 缺少 1 个必需的位置参数 ...
[英]Decorator's return is different from the actual return
代码为什么我得到不同的结果?from time import time def speed_test(func): def wrapper(*args, **kwargs): start = time() func(*args, **kwargs) ...
[英]Using decorators that are aware of `self`'s state
简而言之,我有一个 DataFormatter class 有两种可能的状态: train或infer ,它应该类似于许多具有fit和transform功能的 sklearn 库:如果模式是train我想在self.metadata中存储一个列表已进行的 function 调用和参数,以便可以简单 ...
[英]Decorator/package to save all intermediate variables in a function, should the function fail
我发现自己经常遇到这类问题。 我有一个 function 之类的def compute(input): result = two_hour_computation(input) result = post_processing(result) return result 和 ...
[英]How to type hint a function, added to class by class decorator in Python
我有一个 class 装饰器,它为装饰的 class 添加了一些函数和字段。@mydecorator @dataclass class A: a: str = "" 添加(通过setattr() )是一个.save() function 和一组数据类字段的信息作为单独的字典。 我希望 V ...
[英]How to add decorator to dynamically create class
我想将此代码转换为动态代码: 我可以通过这种方式动态创建 class: 但是我不知道如何添加装饰器@external_decorator 。 谢谢 ...
[英]Defered invocation of a python classmethod obtained in a decorator
我有一个装饰器用来包装这样的类方法:class Class(object): @register_classmethod @classmethod def my_class_method(cls): ... 我的装饰器得到一个classmethod当我 ...
[英]How to have typing support for a static property (using a decorator)
给定一个 static 属性装饰器:class static_property: def __init__(self, getter): self.__getter = getter def __get__(self, obj, objtype): ...
[英]Saving the arguments of a function each time it is called, using a decorator defined in a parent class
我有一系列的父子类 A(),B(A),......可能在不同的模块中。 用户将导入其中一个类并定义自己的子类 class Z(X) 派生自 X = A, B,...。然后他将使用在父类 class X 中定义的初始化方法初始化他的实例,或者在他在 Z 中进行了重写初始化。我希望初始化 functio ...
[英]How to typecheck a property with a class decorator
我正在努力让这段代码正确地使用mypy进行类型检查: 这给了我这个错误,即使它正确运行打印42 (注意我在运行reveal_type时取消了 reveal_type 的mypy ): 此代码正确地使用mypy进行类型检查: 但这显然不能正确运行: 请注意,如果我使用 function 装饰器而不是使 ...
[英]How to pass self into a decorated method?
我正在尝试使用装饰器将隐私修改器实现到 python 中。 我的问题是,每当我装饰一个以 self 作为参数的方法时,当使用点表示法调用该方法时,它不会自动传递 self 。 公共装潢师class:class Public: def __init__(self, method, *args ...
[英]How to type annotate a multi-level decorator
我正在尝试注释一个注入器装饰器,它在调用 function 时将全局字典中的值作为关键字参数注入到装饰的 function 中。 任何有使用参数注释装饰器经验的人都可以帮助我吗? 尝试注释但陷入以下错误:import functools import inspect from typing impo ...
[英]Declaring a function parameter inside a decorator
我需要在装饰器内部创建参数,以免在多个方法中重复代码,但是,我遇到了一个错误,而且我不确定这种使用命名空间的方式是如何正确的。 我想知道这种结构在程序中的可接受程度如何,是否值得使用它? 我的尝试是这样的: | NameError: 名称 'config' 未定义此外,如果我们直接放置 func ...
[英]How to inject arguments from a custom decorator to a command in discord.py?
我正在开发一个机器人,它可以跟踪不同渠道中的各种基于文本的游戏。 在运行相关游戏的通道之外使用的命令当然应该什么也不做,并且它们也不应该在游戏未运行时激活(例如,当新游戏即将开始时)。 因此,几乎我所有的命令都以相同的几行代码开头 我宁愿只装饰所有这些方法。 Discord.py 方便地提供了一 ...
[英]Track invocations of methods and functions
我正在寻找允许跟踪方法和函数调用的库。 将其视为提供called和call_count属性的Mock 。 所需最终结果示例:s = MagicProxyLib() @s class MyClass: def not_called(self): print("This is ...
[英]How can I implement LRU cache in my maya scene using python?
我正在尝试在我的 python 程序中实现 LRU 缓存,我想将 object 名称及其存在于我的 Maya 场景中的类型存储到 LRU 缓存中,然后我想创建两个将返回名称和对象类型的函数。 ...
[英]Why is my decorator not working when using the alternative to @ syntax?
我正在为我的命令行程序制作一个工具,我不会在这个问题上争论它的使用。 事实上,我使用装饰器将功能分配给“令牌”。 当使用 @decorator 语法时,我的代码可以完美地工作,但是当我使用替代语法时却无法执行任何操作(请原谅我,因为我不知道名字)。 下面是装饰器的相关函数:def __add_co ...