io
Module¶
Module for performing input/output operations.
Written by Jesse Bloom.
Functions¶
ParseInfile : reads key / value pairs from an input file.
ParseBoolValue : reads Boolean values from input dictionary.
ParseIntValue : reads integer values from input dictionary.
ParseFloatValue : reads float values from input dictionary.
ParseStringValue : reads string values from input dictionary.
ParseSeqValue : reads sequence from file specified by input dictionary.
ParseFileList : reads file list specified by input dictionary.
-
src.io.
ParseBoolValue
(d, key)¶ Gets Boolean argument from input dictionary.
d is a dictionary of key/value string pairs as returned by ParseInfile.
key is a string in this dictionary that specifies a value that should be True or False.
Returns the Boolean truth value specified by key. Raises a ValueError if key does not exist in d, and a ValueError if it specifies something other than True or False.
>>> d = {'gzipped':'True', 'applyfilter':'False', 'a1':'a1.txt'} >>> ParseBoolValue(d, 'gzipped') True
>>> ParseBoolValue(d, 'applyfilter') False
>>> ParseBoolValue(d, 'a1') Traceback (most recent call last): ... ValueError: value a1.txt for a1 is not True/False
>>> ParseBoolValue(d, 'otherkey') Traceback (most recent call last): ... ValueError: did not find a key for otherkey
-
src.io.
ParseFileList
(d, key)¶ Gets list of files from input dictionary.
d is a dictionary of key/value string pairs as returned by ParseInfile.
key is a string in this dictionary that specifies a value that should give a list of one or more file names.
Returns a list of the file names specified by key. If one or more of these files does not exist, raises and IOError. If key does not exist in d, raises and KeyError.
-
src.io.
ParseFloatValue
(d, key)¶ Gets float argument from input dictionary.
d a dictionary of key/value string pairs as returned by ParseInfile.
key is a string in this dictionary that specifies a value that should be convertible to a float.
Returns the float specified by key. Raises a ValueError if key does not exist in d, and a ValueError if it specifies something other than an integer.
>>> d = {'maxn':'2', 'minq':'20.5', 'string':'hello'} >>> print "%.1f" % ParseFloatValue(d, 'maxn') 2.0
>>> print "%.1f" % ParseFloatValue(d, 'minq') 20.5
>>> ParseFloatValue(d, 'string') Traceback (most recent call last): ... ValueError: value hello for string is not float
>>> ParseFloatValue(d, 'otherkey') Traceback (most recent call last): ... ValueError: did not find a key for otherkey
-
src.io.
ParseInfile
(f)¶ Reads key / value pairs from an input file.
f should be a readable file-like object.
Starting at the current position in f, reads all remaining lines until the end of the file-like object. Any blank line or line with a first character of # is disregarded (# indicates a comment line). All other lines should contain exactly two entries, the first being the key and the second being the value. The key is construed to be the first word, and cannot have any spaces. The value is all of the text that follows the key up to the newline. The key / value pairs are returned in a dictionary, as illustrated in the example below. If there is a duplicate key, and exception is raised.
Example of successfully reading two key/value pairs
>>> f = cStringIO.StringIO() >>> f.write('# comment line followed by blank line\n\n') >>> f.write('key1 first_value\n') >>> f.write('# now another key with two values\nkey2 2 value2') >>> f.seek(0) >>> ParseInfile(f) == {'key1':'first_value', 'key2':'2 value2'} True
Example of duplicate key name
>>> f = cStringIO.StringIO() >>> f.write('# comment line followed by blank line\n\n') >>> f.write('key1 first_value\n') >>> f.write('# now another key\nkey2 2') >>> f.write('\nkey1 1') >>> f.seek(0) >>> ParseInfile(f) == {'key1':'first_value', 'key2':'2'} Traceback (most recent call last): ... ValueError: duplicate key: key1
-
src.io.
ParseIntValue
(d, key)¶ Gets integer argument from input dictionary.
d is a dictionary of key/value string pairs as returned by ParseInfile.
key is a string in this dictionary that specifies a value that should be an integer.
Returns the integer specified by key. Raises a ValueError if key does not exist in d, and a ValueError if it specifies something other than an integer.
>>> d = {'maxn':'2', 'minq':'20.5'} >>> ParseIntValue(d, 'maxn') 2
>>> ParseIntValue(d, 'minq') Traceback (most recent call last): ... ValueError: value 20.5 for minq is not integer
>>> ParseIntValue(d, 'otherkey') Traceback (most recent call last): ... ValueError: did not find a key for otherkey
-
src.io.
ParseSeqValue
(d, key)¶ Reads sequence from FASTA file specified by input dictionary.
d is a dictionary of key/value string pairs as returned by ParseInfile.
key is a string in this dictionary that specifies a value that is a filename of a readable file that contains exactly one sequence in FASTA format.
Returns a string corresponding to the sequence specified in the FASTA file. Raises a ValueError if the filename does not exist, or does not specify exactly one sequence.
-
src.io.
ParseStringValue
(d, key)¶ Reads string argument specified by input dictionary.
d is a dictionary of the key/value string pairs as returned by ParseInfile.
key is a string in this dictionary that specifies a value that is a string.
Returns string corresponding to key. Raises a ValueError if the key does not exist in d.
>>> d = {'outfileprefix':'test_example', 'samplename':'test example'} >>> ParseStringValue(d, 'outfileprefix') 'test_example'
>>> ParseStringValue(d, 'samplename') 'test example'
>>> ParseStringValue(d, 'otherkey') Traceback (most recent call last): ... ValueError: did not find a key for otherkey