使用受argparse启发的api定义和读取输入文件
input_reader的Python项目详细描述
便于读取输入文件的python模块
input_reader用于定义和读取程序的常规输入文件。 这个README只包含brief什么input_reader可以 去吧。有关api的更详细描述,请参见文档 在http://packages.python.org/input_reader。API的灵感来自 argparse模块来自python标准库,因此希望它是 易学。
问题
假设你要写一个做饭的程序。你有 以下要求:
The user must specify one and only one of breakfast, lunch, or dinner.
- For breakfast, the user must specify scrambled or poached eggs and how many eggs. Also, the user specifies waffles or pancakes, and specifies if they want butter and/or syrup. The user may optionally request bacon.
- For lunch, the user must specify if they want a sandwich (BLT, ham, or turkey) or soup (vegetable or chili). The user also specifies if they want bread.
- For dinner, the user specifies steak (rare, medium or well), salmon, or pasta (red or white sauce). There is a choice of soup or salad. The user may also choose to have dessert.
用户必须指定一种饮料,该饮料可以是水、牛奶, 啤酒、苏打水或葡萄酒。
用户可以要求有机餐和/或无麸质。
我们希望输入文件如何
假设有人想要一份加糖浆煎饼的炒鸡蛋早餐 还有培根,还有一杯酒。下面是我们如何定义输入:
drink oj breakfast eggs 2 scrambled pancakes syrup bacon end
或者,如果一个面筋过敏的人想和酒一起吃晚餐怎么办。他们想要 中号牛排加色拉,他们要甜点。输入如下:
nogluten drink wine dinner steak medium salad dessert end
input_reader代码
为了定义上述要求,我们将使用以下代码:
importsysfrominput_readerimportInputReader,ReaderErrorreader=InputReader()# Gluten free or organic meal? These are simple booleansreader.add_boolean_key('nogluten')reader.add_boolean_key('organic')# The drink has an argument, and accepts a specific list of valuesreader.add_line_key('drink',type=('water','milk','oj','beer','soda','wine'))# We are allowed to specify breakfast, lunch or dinner, so we use# a mutually exclusive group. We need one of these, so we call this# required.meal=reader.add_mutually_exclusive_group(required=True)# We define the breakfast blockbfast=meal.add_block_key('breakfast')# Eggs, number of eggs then the stylebfast.add_line_key('eggs',type=[int,('scrambled','poached')],required=True)# Pancakes OR waffles. Syrup and/or butter is optionalwp=bfast.add_mutually_exclusive_group(required=True)wp.add_line_key('waffles',type=None,glob={'len':'*','type':('syrup','butter')})wp.add_line_key('pancakes',type=None,glob={'len':'*','type':('syrup','butter')})# BACON!bfast.add_boolean_key('bacon')# The lunch blocklunch=meal.add_block_key('lunch')# Bread?lunch.add_boolean_key('bread')# Sandwitch or soupss=lunch.add_mutually_exclusive_group(required=True)ss.add_line_key('sandwich',type=('blt','ham','turkey'))ss.add_line_key('soup',type=('vegetable','chili'))# The dinner blockdinner=meal.add_block_key('dinner')# Dessert?dinner.add_boolean_key('dessert')# Soup or salad?ss=dinner.add_mutually_exclusive_group()ss.add_boolean_key('soup')ss.add_boolean_key('salad')# Main coursemcourse=dinner.add_mutually_exclusive_group(required=True)mcourse.add_line_key('steak',type=('rare','medium','well'))mcourse.add_boolean_key('salmon')mcourse.add_line_key('pasta',type=('red','white'))
您可以按以下方式读入和分析文件:
# Assuming the input file is in argv[1], read in the input filetry:inp=reader.read_input(sys.argv[1])exceptReaderErrorase:sys.exit(str(e))# Is the meal gluten free?ifinp.nogluten:...# Is lunch served?ifinp.lunch:# If so, what type of soup?ifinp.lunch.soup=='chili':...# Etc...
历史记录
2014年1月3日第1.2.2节
- Cleaned up import blocks
- Added spaces between defs/classes (cosmetic)
2014年1月29日第1.2.1a节
- Fixed bug where strings were compared to unicode instead of basestring in python2.x
2014年1月26日第1.2.1节
- All calls are completely python2/3 compatible
- Python2 uses unicode literals
- Files use unicode encoding
2014年1月16日第1.2.0节
- Added input_reader.h to provide easy C interface to this python module
- Added include_path attribute to input_reader module for C compilations
- Removed distribute_setup.py (cause install problems for some)
- Unit tests pass for both Python 2.7 and Python 3.x
- Updated documentation
2013年4月13日第1.1.1节
- Added the filename attribute to the InputReader class
2013年1月25日第1.1.0版
- Increased code coverage of tests to ~98%
- Refactored code to reduce copy/paste and be open for future improvements
- Increased code coverage of tests to ~98%
- Refactored code to reduce copy/paste and be open for future improvements
2013年1月14日第1.0.2节
- Added input_file attribute to InputReader class
- Fixed typo in documentation
- Updated version updating code
2012年12月22日第1.0.1节
- Fixed error in MANIFEST.in
2012年12月16日第1.0.0版
- Fixed bugs in unit tests
- Finished documentation with doctests
- Added a post_process method to InputReader that can be subclassed
- Made improvements to the setup process
- Fixed bugs in unit tests
- Finished documentation with doctests
- Added a post_process method to InputReader that can be subclassed
- Made improvements to the setup process
2012年3月12日第0.9.1节
- Added unit tests
- Added extra checks for bad input