使用受argparse启发的api定义和读取输入文件

input_reader的Python项目详细描述


便于读取输入文件的python模块

input_reader用于定义和读取程序的常规输入文件。 这个README只包含brief什么input_reader可以 去吧。有关api的更详细描述,请参见文档 在http://packages.python.org/input_reader。API的灵感来自 argparse模块来自python标准库,因此希望它是 易学。

问题

假设你要写一个做饭的程序。你有 以下要求:

  1. The user must specify one and only one of breakfast, lunch, or dinner.

    1. 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.
    2. 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.
    3. 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.
  2. 用户必须指定一种饮料,该饮料可以是水、牛奶, 啤酒、苏打水或葡萄酒。

  3. 用户可以要求有机餐和/或无麸质。

我们希望输入文件如何

假设有人想要一份加糖浆煎饼的炒鸡蛋早餐 还有培根,还有一杯酒。下面是我们如何定义输入:

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

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

2012年3月12日第0.9.1节

  • Added unit tests
  • Added extra checks for bad input

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java迭代图形框架聚合达到内存限制的消息   反编译和开发人员向Android应用程序提供java安全密钥   活页夹中的java Vaadin上传组件,例如Crud编辑器   java如何定义0090当我有了每个字符   带有AOP的java截取私有注释方法   在Java中暂停并恢复动画(thread.sleep)   java在JavaFX中从不同的控制器向TableView添加行   java如何通过Spring使用脚本初始化inmemory HSQLDB   windows找到了绝对的java。通过编程从java代码获取exe路径   Java同步、信号量和队列的多线程性能   java异步任务回调未调用   java在迷宫中用坐标寻找最短路径   Java:并行处理数组,查找发生异常的位置   java我无法理解我收到的错误   如何调用。bat文件,并使用java中的ProcessBuilder发送字符串   java在mysql数据库中插入日期   将ArrayList的内容显示为格式正确的JSON for Java REST API   java@OneToMany注释SQLSyntaxErrorException:ORA000904无效标识符