python在工厂中的应用_python中的工厂方法

博客围绕更便捷管理不同项目展开,提到自动检测项目类型并正确设置命令的方法。目前使用classmethod“match”函数和detect函数,但认为有更好设计却未找到,还给出了PythonProject、AutoconfProject等不同项目类型的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简而言之,它的目标是更容易地管理许多不同的项目。

其中一个有用的方法是自动检测我正在工作的项目类型,正确设置一些命令。在

目前我正在使用一个classmethod“match”函数和一个遍历各种“match”的detect函数。

我肯定有更好的设计,但是找不到。在

有什么想法吗?在class ProjectType(object):

build_cmd = ""

@classmethod

def match(cls, _):

return True

class PythonProject(ProjectType):

build_cmd = "python setup.py develop --user"

@classmethod

def match(cls, base):

return path.isfile(path.join(base, 'setup.py'))

class AutoconfProject(ProjectType):

#TODO: there should be also a way to configure it

build_cmd = "./configure && make -j3"

@classmethod

def match(cls, base):

markers = ('configure.in', 'configure.ac', 'makefile.am')

return any(path.isfile(path.join(base, x)) for x in markers)

class MakefileOnly(ProjectType):

build_cmd = "make"

@classmethod

def match(cls, base):

# if we can count on the order the first check is not useful

return (not AutoconfProject.match(base)) and \

(path.isfile(path.join(base, 'Makefile')))

def detect_project_type(path):

prj_types = (PythonProject, AutoconfProject, MakefileOnly, ProjectType)

for p in prj_types:

if p.match(path):

return p()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值