Previous topic

2. The InputReader Class

Next topic

4. The SUPPRESS class

This Page

3. The Namespace class

class input_reader.Namespace(**defaults)

A simple class to hold the keys and arguments found from the input file. This is a copy-and-paste job from the argparse module with some minor additions.

You can populate the Namespace at initialization with a series of key-value pairs. These are considered the defaults.

add(key, val)

Add a key-value pair to the Namespace.

Parameters:
  • key (str) – The key to add
  • val (anything) – The value of the key
finalize()

Any defaults not yet added with the add() are added to the Namespace.

get(key, default=None)

Get the value of a key. If the key does not exist, default is returned. This is alternative to the namespace.key syntax that does not raise an error when key does not exist.

Parameters:
  • key (str) – The key whose value you wish to retrieve.
  • default (anything (default is None)) –
Returns:

The value associated with key

items()

Just like the dict.items() function for the python dict.

Returns:tuple of tuple s of all key, value pairs in the Namespace.
keys()

Just like the dict.keys() function for the python dict.

Returns:tuple of all keys in the Namespace.
remove(key)

Remove a key from the Namespace.

Parameters:key (str) – The key to remove. If it does not exist, it is ignored.
values()

Just like the dict.values() function for the python dict.

Returns:tuple of all values in the Namespace.

In general, it is unlikely you need to use many of these methods unless you have to manually modify the Namespace. In general you will likely be using the namespace as given below:

# Typically, you won't populate the Namespace yourself.  It will be done
# for you with the read_input method.  Don't worry about the next three
# lines
from input_reader import Namespace
inp = Namespace(red=True, blue=False, green=True)
inp.finalize()

# Here is where the typical use case
if inp.red: # True
    print 'red'
if inp.blue: # False
    print 'blue'
if 'green' in inp: # True
    print 'Maybe green'
if 'yellow' in inp: # False
    print 'Maybe yellow'

# You may need to add a key, since the Namespace can act as a safe way to
# pass around global variables
inp.add('yellow', True)
if 'yellow' in inp: # True
    print 'Maybe yellow'

# Of course you can iterate over the keys if you want
for key in inp.keys():
    print key, key in inp