python在工厂中的应用_python中的工厂方法
我正在做以下小項目:
https://github.com/AndreaCrotti/project-organizer
簡而言之,它旨在更容易地管理許多不同的項目.
其中一個有用的方法是自動檢測我正在處理的項目類型,以正確設置一些命令.
目前我正在使用classmethod“匹配”函數和一個迭代各種“匹配”的檢測函數.
我相信可能有更好的設計,但找不到它.
有任何想法嗎?
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()
總結
以上是生活随笔為你收集整理的python在工厂中的应用_python中的工厂方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mvc试图 下拉框不重复_面试前不巩固一
- 下一篇: 线上系统因为一个ThreadLocal直