现在我一直在使用选择权,并想增加把论点从编织案上排出的能力。
迄今为止,我可以认为,最好用精彩的文字写法,用硬编码......似乎ky。
这样做的最合法方式是什么?
现在我一直在使用选择权,并想增加把论点从编织案上排出的能力。
迄今为止,我可以认为,最好用精彩的文字写法,用硬编码......似乎ky。
这样做的最合法方式是什么?
我同意S.Lott关于使用集束文件的想法,但我建议使用“装在ConfigParser(3.0配置的组件)模块,而不是“带回家”的解决办法。
这里有一份简短的文字说明,其中显示BigParser和。
import ConfigParser
from optparse import OptionParser
CONFIG_FILENAME = defaults.cfg
def main():
config = ConfigParser.ConfigParser()
config.read(CONFIG_FILENAME)
parser = OptionParser()
parser.add_option("-l",
"--language",
dest="language",
help="The UI language",
default=config.get("Localization", "language"))
parser.add_option("-f",
"--flag",
dest="flag",
help="The country flag",
default=config.get("Localization", "flag"))
print parser.parse_args()
if __name__ == "__main__":
main()
<<>Output>:
(<Values at 0x2182c88: { flag : japan.png , language : Japanese }>, [])
改为“parser.py -- 语句:
(<Values at 0x2215c60: { flag : japan.png , language : French }>, [])
Help is built in.
Run with "parser.py --help
":
Usage: parser.py [options]
Options:
-h, --help show this help message and exit
-l LANGUAGE, --language=LANGUAGE
The UI language
-f FLAG, --flag=FLAG The country flag
www.un.org/Depts/DGACM/index_spanish.htm The config file:
[Localization]
language=Japanese
flag=japan.png
我也存在同样的问题,但希望把“汇案”作为理由。 在S. Lott的回答中,我提出了以下法典。
例会:
$ python defaultconf.py # use hard-coded defaults False $ python defaultconf.py --verbose # verbose on command line True $ python defaultconf.py --loadconfig blah # load config with verbose :True True $ python defaultconf.py --loadconfig blah --quiet # Override configured value False
法典:
#!/usr/bin/env python2.6
import optparse
def getParser(defaults):
"""Create and return an OptionParser instance, with supplied defaults
"""
o = optparse.OptionParser()
o.set_defaults(**defaults)
o.add_option("--verbose", dest = "verbose", action="store_true")
o.add_option("--quiet", dest = "verbose", action="store_false")
o.add_option("--loadconfig", dest = "loadconfig")
return o
def main():
# Hard coded defaults (including non-command-line-argument options)
my_defaults = { verbose : False, config_only_variable : 42}
# Initially parse arguments
opts, args = getParser(my_defaults).parse_args()
if opts.loadconfig is not None:
# Load config from disk, update the defaults dictionary, and reparse
# Could use ConfigParser, simplejson, yaml etc.
config_file_values = { verbose : True} # the dict loaded from disk
my_defaults.update(config_file_values)
opts, args = getParser(my_defaults).parse_args()
print opts.verbose
if __name__ == __main__ :
main()
http://docs.python.org/library/optparse.html#optparse.OptionParser.set_defaults”rel=“nofollow noreferer” http://docs.python.org/library/optparse.html#optparse.OptionParser.set_defaults。
创建文件,记录违约值的字典。
{ arg1 : this ,
arg2 : that
}
然后读到该档案,将案文转换成字典,并将该词典作为论点提交set_defaults
。
如果你对<代码>eval表示真正担忧,那么该文档就使用JSON(或YAML)。 或者你甚至可以提出<代码>。 INI file out of it and use configparser
to Recaincies.
或者,你可以使用一份简单的清单,地址是exec
。
Config file.
arg1 = this
arg2 = that
阅读汇辑。
defaults= {}
with open( defaults.py , r ) as config
exec config in {}, defaults
将同一指挥系统格式的论据从档案(例如:@commands)中阅读,然后使用原教区来加以整理。
options, args = parser.parse_args()
if args[0][0] == @ : # script.py @optfile
with open(args[0][1:]) as f:
fa = [l.strip() for l in f]
fa = fa + args[1:] # put back any other positional arguments
# Use your original parser to parse the new options
options, args = parser.parse_args(args=fa, values=options)
Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...
I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...
Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...
Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...
I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...
Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...
Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...
I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...