仅导入类静态方法 [英] Import only a class static method

查看:125
本文介绍了仅导入类静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在基类中有以下装饰器:

I have the following decorator in a base class:

class BaseTests(TestCase):
    @staticmethod
    def check_time(self, fn):
        @wraps(fn)
        def test_wrapper(*args,**kwargs):
            # do checks ...
        return test_wrapper

以下类继承自BaseTests:

And the following class inheriting from BaseTests:

from path.base_posting import BaseTests
from path.base_posting.BaseTests import check_time  # THIS LINE DOES NOT WORK!

class SpecificTest(BaseTests):

    @check_time # use the decorator
    def test_post(self):
        # do testing ...

我想在上面的SpecificTest中使用装饰器,而不必使用BaseTests.check_time,因为在原始代码他们有很长的名字,我必须在很多地方使用它。任何想法?

I would like to use the decorator in SpecificTest as above, without having to use BaseTests.check_time, because in the original code they have long names, and I have to use it in many places. Any ideas?

编辑:
我决定将check_time作为BaseTests文件中的一个独立函数,只需导入

I decided to make check_time an independent function in BaseTests file, and simply import

from path.base_posting import BaseTests, check_time


推荐答案

简单地说

check_time = BaseTests.check_time

from module_paths.base_posting import BaseTests
check_time = BaseTests.check_time

class SpecificTest(BaseTests):

    @check_time # use the decorator
    def test_post(self):
        # do testing ...



< hr>

您可能还想重新考虑使用 check_time 静态方法,因为看起来您的用例更多地使用它作为支持 - 单独的功能而不是静态方法。


You may also want to reconsider making check_time a staticmethod, since it appears your use case employs it more as a stand-alone function than as a staticmethod.

这篇关于仅导入类静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆