如何解决pylint 的非自用有多重要?
举个例子,我有一个 Django 自定义管理命令,它会定期(APScheduler + CronTrigger)向 Dramatiq 发送任务。
为什么以下代码具有单独的功能:
def get_crontab(options):
"""Returns crontab whether from options or settings"""
crontab = options.get("crontab")
if crontab is None:
if not hasattr(settings,"REMOVE_TOO_OLD_CRONTAB"):
raise ImproperlyConfigured("Whether set settings.REMOVE_TOO_OLD_CRONTAB or use --crontab argument")
crontab = settings.REMOVE_TOO_OLD_CRONTAB
return crontab
def add_cron_job(scheduler: BaseScheduler,actor,crontab):
"""Adds cron job which triggers Dramatiq actor"""
module_path = actor.fn.__module__
actor_name = actor.fn.__name__
trigger = CronTrigger.from_crontab(crontab)
job_path = f"{module_path}:{actor_name}.send"
job_name = f"{module_path}.{actor_name}"
scheduler.add_job(job_path,trigger=trigger,name=job_name)
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()
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()
add_cron_job(scheduler,tasks.remove_too_old_publications,get_crontab(options))
run_scheduler(scheduler)
比带有方法的代码更好吗?
class Command(BaseCommand):
help = "Periodically removes too old publications from the RSS feed"
def add_arguments(self,type=str)
def get_crontab(self,options):
"""Returns crontab whether from options or settings"""
crontab = options.get("crontab")
if crontab is None:
if not hasattr(settings,"REMOVE_TOO_OLD_CRONTAB"):
raise ImproperlyConfigured(
"Whether set settings.REMOVE_TOO_OLD_CRONTAB or use --crontab argument"
)
crontab = settings.REMOVE_TOO_OLD_CRONTAB
return crontab
def handle(self,**options):
scheduler = BlockingScheduler()
self.add_cron_job(scheduler,self.get_crontab(options))
self.run_scheduler(scheduler)
def add_cron_job(self,scheduler: BaseScheduler,crontab):
"""Adds cron job which triggers Dramatiq actor"""
module_path = actor.fn.__module__
actor_name = actor.fn.__name__
trigger = CronTrigger.from_crontab(crontab)
job_path = f"{module_path}:{actor_name}.send"
job_name = f"{module_path}.{actor_name}"
scheduler.add_job(job_path,name=job_name)
def run_scheduler(self,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()
此代码仅在一处使用,不会重复使用。
StackOverflow 需要更多详细信息,因此:
第二个代码是我原来写的版本。之后,我使用 Pylint 运行了 Prospector,除了其他有用的消息之外,我还收到了 pylint: no-self-use / Method could be a function (col 4)
。为了解决这个问题,我像第一个例子一样重写了我的代码。但我还是不明白为什么这样更好。
解决方法
至少,在这种情况下,并不是更好。 Pylint 会通知您有关“self”未使用的信息,就像它会通知您有关变量或导入未使用的信息一样。
修复 pylint 消息的其他几个选项是在函数中实际使用“self”或添加静态方法(或类方法)装饰器。两者的例子都在水平线之后。 Here are the docs for staticmethod 和 here's the difference between staticmethod and classmethod。
由于这是一个 Django 命令,并且您可能不会有该类的多个实例或其他继承 Command 的类(即重载函数)或可以从类中的函数中受益的东西,选择您认为最易读/最容易更改的内容。
为了完整起见,StackExchange Code Review 可以进一步了解哪个是最好的(如果有的话)。
使用 self 的示例,主要区别在于调度程序是在 __init__ 中创建的,而不是作为参数传递给使用它的函数:
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()
使用静态方法的示例,其中唯一的区别是静态方法装饰器和带有装饰器的函数没有自参数:
class Command(BaseCommand):
help = "Periodically removes too old publications from the RSS feed"
def add_arguments(self,**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)
scheduler.start()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。