Package: input_reader
A python module to facilitate reading input filesinput_readeris used to define and read a general input file for a program.
ThisREADMEonly contains abriefsynopsis of whatinput_readercan
do.  For a more detailed description of the API, please see the documentation
athttp://packages.python.org/input_reader.  The API is inspired by that of theargparsemodule from the python standard library, so hopefully it will be
easy to learn.The ProblemLet’s say you have to write a program that cooks a meal.  You have the
following requirements: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.The user must specify a drink, and that drink can be water, milk,
OJ, beer, soda, or wine.The user can request an organic meal and/or gluten-free.How We Want the Input FilesLet’s say that someone wants a scrambled egg breakfast with syrupy pancakes
and bacon, and OJ for a drink.  Here is how we might define the input:drink oj

breakfast
    eggs 2 scrambled
    pancakes syrup
    bacon
endOr, what if someone with a gluten allergy wants dinner with wine. They want
a medium steak with salad, and they want the dessert.  Here is the input:nogluten

drink wine

dinner
    steak medium
    salad
    dessert
endTheinput_readerCodeTo define the above requirements, we would use the following code: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'))You can read in and analyze the file in a manner given below:# 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...AuthorSeth M. MortonHistory03-01-2014 v. 1.2.2Cleaned up import blocksAdded spaces between defs/classes (cosmetic)01-29-2014 v. 1.2.1aFixed bug where strings were compared to unicode instead of basestring in python2.x01-26-2014 v. 1.2.1All calls are completely python2/3 compatiblePython2 uses unicode literalsFiles use unicode encoding01-16-2014 v. 1.2.0Added input_reader.h to provide easy C interface to this python moduleAdded include_path attribute to input_reader module for C compilationsRemoved distribute_setup.py (cause install problems for some)Unit tests pass for both Python 2.7 and Python 3.xUpdated documentation04-13-2013 v. 1.1.1Added the filename attribute to the InputReader class01-25-2013 v. 1.1.0Increased code coverage of tests to ~98%Refactored code to reduce copy/paste and be open for future improvements01-14-2013 v. 1.0.2Added input_file attribute to InputReader classFixed typo in documentationUpdated version updating code12-22-2012 v. 1.0.1Fixed error in MANIFEST.in12-16-2012 v. 1.0.0Fixed bugs in unit testsFinished documentation with doctestsAdded a post_process method to InputReader that can be subclassedMade improvements to the setup process12-3-2012 v. 0.9.1Added unit testsAdded extra checks for bad input