누구나 Class too big and hard to add new features에있는 사람들은 질문에 완전히 unphased되어 어떤 식 으로든 명령 행 옵션을 메소드에 연결하지만, 이에 대한 문서는 찾을 수 없습니다. optparse
또는 argparse
또는 sys.argv
이 아닙니다.이 질문은 메서드와 명령 줄 옵션간에 직접적인 관계가 있음을 의미합니다. 내가 뭘 놓치고 있니?Python 명령 행 인자는 메소드와 어떤 관련이 있습니까?
1
A
답변
1
나는 많은 명령을받은 후에는 유지하기가 매우 어렵 기 때문에이 클래스를 사용한다. 아주 좋은 생각이 아닌 것 같다.
class myprogram(object):
def __init__(self)
self.prepare()
def prepare(self):
# some initializations
self.prepareCommands()
def prepareCommands(self):
self.initCommand("--updateDatabase", self.updateDatabase)
self.initCommand("--getImages", self.getImages)
# and so on
def initCommand(self, cmd, func):
options = sys.argv
for option in options:
if option.find(cmd)!=-1:
return func()
# my commands
def updateDatabase(self):
#...
def getImages(self):
#...
if __name__ == "__main__":
p = myprogram()
EDIT1 : 여기 난 그냥 구현 청소기 방법 :
myprogram.py :
이from config import * # has settings
from commands import *
from logsys import log
import filesys
class myprogram(object):
def __init__(self):
log(_class=self.__name__, _func='__init__', _level=0)
log(_class=self.__name__, _func='__init__', text="DEBUG LEVEL %s" % settings["debug"], _level=0)
self.settings = settings
self.cmds = commands
def prepare(self):
log(_class=self.__name__, _func='prepare', _level=1)
self.dirs = {}
for key in settings["dir"].keys():
self.dirs[key] = settings["dir"][key]
filesys.checkDir(self.dirs[key])
def initCommands(self):
log(_class=self.__name__, _func='initCommands', _level=1)
options = sys.argv
for option in options:
for cmd in self.cmds.keys():
if option.find(cmd) != -1:
return self.cmds[cmd]()
if __name__ == '__main__':
p = myprogram()
p.prepare()
p.initCommands()
commands.py :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
commands = {}
#csv
import csvsys
commands["--getCSV"] = csvsys.getCSV
#commands["--getCSVSplitted"] = csvsys.getCSVSplitted
# update & insert
import database
commands["--insertProductSpecification"] = database.insertProductSpecification
# download
import download
commands["--downloadProductSites"] = download.downloadProductSites
commands["--downloadImages"] = download.downloadImages
# parse
import parse
commands["--parseProductSites"] = parse.parseProductSites
가 EDIT2 : 지금 업데이트 한 귀하의 질문에 더 완벽한 예제로 링크 된 내 질문 Class too big and hard to add new features
4
두 돌 사이에 돌이 링크가 없습니다. 링크 된 질문은 명령 줄 인수를 사용하여 여러 가지 다른 작업 중 하나를 수행 할 수있는 프로그램 인 것으로 보입니다. 이 은 프로그램에서 메소드를 사용하여 구현됩니다.
이들 사이에 접착제를 쓰려면 argparse
과 같은 것을 사용했음을 의미하는 것입니다. 그러나 메소드의 사용은 특정 프로그램의 구현 세부 사항 일뿐입니다.
관련 문제
- 1. Python 명령 행 - 다중 행 입력
- 2. 핸들은 스레드와 어떤 관련이 있습니까?
- 3. stacktrace.js의 숫자와 어떤 관련이 있습니까?
- 4. BCL은 CLS와 어떤 관련이 있습니까?
- 5. 카피 스트라 노는 레이크와 어떤 관련이 있습니까?
- 6. 전달 인자는
- 7. Rails의 alter_table 메소드와 어떤 관계가 있습니까?
- 8. 이 Perl 한 줄 필터는 가져온 XML과 어떤 관련이 있습니까?
- 9. 가변 인자는
- 10. Ruby - Python __str __() 메소드와 동일합니까?
- 11. Python 및 Ubuntu의 명령 행 도구 호출
- 12. 전달 인자는
- 13. jQuery는 함수형 프로그래밍과 어떤 관련이 있습니까?
- 14. 메시지 브리지는 Weblogic의 대기열과 어떤 관련이 있습니까?
- 15. armel은 무엇이며 armel은 arm과 어떤 관련이 있습니까?
- 16. ProcessInstanceInfo는 jBPM5의 SessionInfo와 어떤 관련이 있습니까?
- 17. 마말레이드가 iOS SDK와 어떤 관련이 있습니까
- 18. 어떤 굴절 된 광선이 관련이 있습니까?
- 19. 체크인 후 작업은 어떤 지점과 관련이 있습니까?
- 20. Cookie.setMaxAge (Integer.MAX_VALUE)와 어떤 관련이 있습니까?
- 21. 더블린이란 무엇이며 더블린은 WCF와 어떤 관련이 있습니까?
- 22. JavaScript '통화'란 무엇이며 효율성과 어떤 관련이 있습니까?
- 23. cURL은 웹 서비스와 어떤 관련이 있습니까?
- 24. My.Application.Log.WriteEntry()는 System.Diagnostics.Trace.WriteLine()과 어떤 관련이 있습니까?
- 25. ninject.extensions.interception을 통한 AOP는 ninject와 어떤 관련이 있습니까?
- 26. Google 다트는 Google 폐쇄와 어떤 관련이 있습니까?
- 27. ExecutionContext 클래스는 "컨텍스트"와 어떤 관련이 있습니까?
- 28. 파이썬의 GIL은 가비지 컬렉터와 어떤 관련이 있습니까?
- 29. GNU Classpath는 libgcj와 어떤 관련이 있습니까?
- 30. Angular JS는 Google Closure와 어떤 관련이 있습니까?
예를 들면, --max-val 5 또는 --treads = 5와 같은 인수에 값을 부여 할 수있는 방법이 없다는 것입니다. –
예 참으로 그렇습니다. 그러나 구체적인 인수는 필요 없습니다. 하나님의 계급 자체가 무엇을해야할지 알았 기 때문에 나의 "명령"입니다. ^^ –
와우, 그런 식으로 생각한 적 없어! 흥미 롭 군. – weronika