Konfigurationsdatei als Argument zum Konfigurieren von Parse Modul übergebenPython

Python-Programme
Anonymous
 Konfigurationsdatei als Argument zum Konfigurieren von Parse Modul übergeben

Post by Anonymous »

Mein Skript sollte so genannt werden: < /pt>

# python start.py --config=start.cfg
< /code>

Ich habe also drei Dateien: < /p>

start.py

import argparse
import options

parser = argparse.ArgumentParser(prog='mystart')
parser.add_argument("-f", "--file", dest="filename",
help="write report to FILE", metavar="FILE")

args = parser.parse_args()
ConfigFile = args.filename

test = options.getTest()

print test
< /code>

options.py

from ConfigReader import getConfigValue as getVal

def getTest():
Test = getVal('main', 'test')
return Test
< /code>

configreader.py

import os
import ConfigParser

def config():
cfg = ConfigParser.ConfigParser()
for f in ('default.cfg',):
if os.path.exists(f):
cfg.read(f)
return cfg
return None

def getConfigValue(Section, Option):
cfg = config()
if cfg.has_option(Section, Option):
return cfg.get(Section, Option)
else:
return None
< /code>

Wie Sie sehen können, ist meine Konfigurationsdatei in meinem Modul Configreader.py. p>

Dies ist nur ein einfaches Beispiel. Ich habe eine Lochhaufen von Dateien, die auch auf diese Variable zugreifen sollten. < /P>

Ist eine globale Variable eine Option? Gibt es einen anderen Weg?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post