Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pylint'有多重要;这不是自用吗?_Python_Django_Pylint - Fatal编程技术网

Python pylint'有多重要;这不是自用吗?

Python pylint'有多重要;这不是自用吗?,python,django,pylint,Python,Django,Pylint,例如,我有一个Django自定义管理命令,它定期(APScheduler+CronTrigger)将任务发送到戏剧化 为什么以下代码具有单独的功能: def get_crontab(选项): “”“从选项或设置返回crontab”“” crontab=options.get(“crontab”) 如果crontab为None: 如果不是hasattr(设置,“删除旧的\u-TOO\u-CRONTAB”): raise配置不当(“是否设置设置。删除\u太旧\u CRONTAB或使用--CRONTA

例如,我有一个Django自定义管理命令,它定期(APScheduler+CronTrigger)将任务发送到戏剧化

为什么以下代码具有单独的功能:

def get_crontab(选项):
“”“从选项或设置返回crontab”“”
crontab=options.get(“crontab”)
如果crontab为None:
如果不是hasattr(设置,“删除旧的\u-TOO\u-CRONTAB”):
raise配置不当(“是否设置设置。删除\u太旧\u CRONTAB或使用--CRONTAB参数”)
crontab=settings.REMOVE\u TOO\u OLD\u crontab
返回crontab
def add_cron_作业(调度程序:BaseScheduler、actor、crontab):
“”“添加触发戏剧演员的cron作业”“”
模块\路径=actor.fn.\模块__
actor\u name=actor.fn.\u name__
触发器=CronTrigger.from_crontab(crontab)
作业路径=f“{module\u path}:{actor\u name}.send”
job_name=f“{module_path}.{actor_name}”
添加作业(作业路径,触发器=触发器,名称=作业名称)
def运行计划程序(计划程序):
“”“以阻止方式运行计划程序”“”
def关闭(信号、帧):
scheduler.shutdown()
信号。信号(信号。信号,关机)
信号.signal(signal.SIGTERM,关机)
scheduler.start()
类命令(BaseCommand):
help=“定期从RSS源中删除太旧的发布”
def add_参数(self,解析器:argparse.ArgumentParser):
add_参数(“--crontab”,type=str)
def句柄(自身、*参数、**选项):
调度程序=阻塞调度程序()
添加\u cron\u作业(调度程序,任务。删除\u too\u旧发布,获取\u crontab(选项))
运行调度程序(调度程序)
这比带方法的代码好吗

class命令(BaseCommand):
help=“定期从RSS源中删除太旧的发布”
def add_参数(self,解析器:argparse.ArgumentParser):
add_参数(“--crontab”,type=str)
def get_crontab(自身,选项):
“”“从选项或设置返回crontab”“”
crontab=options.get(“crontab”)
如果crontab为None:
如果不是hasattr(设置,“删除旧的\u-TOO\u-CRONTAB”):
提高配置不当(
“是设置settings.REMOVE\u TOO\u OLD\u CRONTAB还是使用--CRONTAB参数”
)
crontab=settings.REMOVE\u TOO\u OLD\u crontab
返回crontab
def句柄(自身、*参数、**选项):
调度程序=阻塞调度程序()
self.add_cron_作业(调度程序,任务。删除旧发布,self.get_crontab(选项))
self.run_调度程序(调度程序)
def add_cron_作业(self,scheduler:BaseScheduler,actor,crontab):
“”“添加触发戏剧演员的cron作业”“”
模块\路径=actor.fn.\模块__
actor\u name=actor.fn.\u name__
触发器=CronTrigger.from_crontab(crontab)
作业路径=f“{module\u path}:{actor\u name}.send”
job_name=f“{module_path}.{actor_name}”
添加作业(作业路径,触发器=触发器,名称=作业名称)
def run_计划程序(自身、计划程序):
“”“以阻止方式运行计划程序”“”
def关闭(信号、帧):
scheduler.shutdown()
信号。信号(信号。信号,关机)
信号.signal(signal.SIGTERM,关机)
scheduler.start()
此代码只在一个地方使用,不会被重用

StackOverflow需要更多详细信息,因此:


第二个代码是我最初编写的版本。在那之后,我用Pylint运行了Prospector,除了其他有用的消息之外,我还得到了
Pylint:no self-use/Method可能是函数(col4)
。为了解决这个问题,我重新编写了第一个示例中的代码。但我仍然不明白为什么这样更好。

至少,在这种情况下,这样并不更好。Pylint通知您“self”未使用,就像它通知您变量或导入未使用一样

修复pylint消息的两个其他选项是在函数中实际使用“self”或添加staticmethod(或classmethod)装饰器。两者的示例都位于水平线之后。和

由于这是一个Django命令,并且您可能不会有继承该命令的类或其他类的多个实例(即重载函数)或从类中的函数中获益的类,选择您认为最可读/最容易更改的一个

为了完整性,我们可以进一步了解哪一个是最好的,如果有的话


使用self的示例,主要区别在于调度器是在_init_uuu中创建的,而不是作为参数传递给使用它的函数:

class Command(BaseCommand):
    help = "Periodically removes too old publications from the RSS feed"

    def __init__(self):
        super().__init__()
        self.scheduler = BlockingScheduler()

    def add_arguments(self, parser: argparse.ArgumentParser):
        parser.add_argument("--crontab", type=str)

    def handle(self, *args, **options):
        self.add_cron_job(tasks.remove_too_old_publications, self.get_crontab(options))
        self.run_scheduler()

    # ...

    def run_scheduler(self):
        """Runs scheduler in a blocking way"""

        def shutdown(signum, frame):
            self.scheduler.shutdown()

        signal.signal(signal.SIGINT, shutdown)
        signal.signal(signal.SIGTERM, shutdown)

        self.scheduler.start()
使用staticmethod的示例,其中唯一的区别是staticmethod decorator和具有decorator的函数没有自参数:

class Command(BaseCommand):
    help = "Periodically removes too old publications from the RSS feed"

    def add_arguments(self, parser: argparse.ArgumentParser):
        parser.add_argument("--crontab", type=str)

    def handle(self, *args, **options):
        scheduler = BlockingScheduler()
        self.add_cron_job(scheduler, tasks.remove_too_old_publications, self.get_crontab(options))
        self.run_scheduler(scheduler)

    # ...

    @staticmethod
    def run_scheduler(scheduler):
        """Runs scheduler in a blocking way"""

        def shutdown(signum, frame):
            scheduler.shutdown()

        signal.signal(signal.SIGINT, shutdown)
        signal.signal(signal.SIGTERM, shutdown)

        scheduler.start()

是的,你可以把方法变成静态的,这不是一个巨大的重构。