使用受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第三方库


热门话题
JavaPax考试:从非标准Maven存储库解析Karaf特性存储库(XML文件)   java Spring启动Bean创建异常   java中将ArrayList转换为数组的方法   Android Studio的java Unity插件。   java在CheckStyle中从方法计数中排除getter和setter   HibernateJava。sql。SQLSyntaxErrorException:表/视图“序列”不存在   与命令行程序Java vs C通信   java WebView膨胀异常   java在O(n)java8流中寻找两个列表的交集   java使用Gradle运行单元测试时,最大堆大小在哪里设置?   ssl加载java应用程序(CXF)内的jks文件   CI:Jenkins Git:Simple Java项目:希望在特定时间在脚本上发送消息   java根据位置更改数字   java按数值排序字符串数组   macos java版本“1.6.0_65”是否与java 6模棱两可?   Cassandra中的java时间戳