2012-03-31 1 views
1

누구나 Class too big and hard to add new features에있는 사람들은 질문에 완전히 unphased되어 어떤 식 으로든 명령 행 옵션을 메소드에 연결하지만, 이에 대한 문서는 찾을 수 없습니다. optparse 또는 argparse 또는 sys.argv이 아닙니다.이 질문은 메서드와 명령 줄 옵션간에 직접적인 관계가 있음을 의미합니다. 내가 뭘 놓치고 있니?Python 명령 행 인자는 메소드와 어떤 관련이 있습니까?

답변

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

+0

예를 들면, --max-val 5 또는 --treads = 5와 같은 인수에 값을 부여 할 수있는 방법이 없다는 것입니다. –

+0

예 참으로 그렇습니다. 그러나 구체적인 인수는 필요 없습니다. 하나님의 계급 자체가 무엇을해야할지 알았 기 때문에 나의 "명령"입니다. ^^ –

+0

와우, 그런 식으로 생각한 적 없어! 흥미 롭 군. – weronika

4

두 돌 사이에 돌이 링크가 없습니다. 링크 된 질문은 명령 줄 인수를 사용하여 여러 가지 다른 작업 중 하나를 수행 할 수있는 프로그램 인 것으로 보입니다. 이 은 프로그램에서 메소드를 사용하여 구현됩니다.

이들 사이에 접착제를 쓰려면 argparse과 같은 것을 사용했음을 의미하는 것입니다. 그러나 메소드의 사용은 특정 프로그램의 구현 세부 사항 일뿐입니다.

관련 문제