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 a key-value pair to the Namespace.
Parameters: |
|
---|
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: | |
---|---|
Returns: | The value associated with key |
Just like the dict.items() function for the python dict.
Returns: | tuple of tuple s of all key, value pairs in the Namespace. |
---|
Just like the dict.keys() function for the python dict.
Returns: | tuple of all keys in the Namespace. |
---|
Remove a key from the Namespace.
Parameters: | key (str) – The key to remove. If it does not exist, it is ignored. |
---|
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