Skip to content

Commit

Permalink
Continue to improve option docs
Browse files Browse the repository at this point in the history
  • Loading branch information
arbennett committed May 8, 2019
1 parent befe1d2 commit a9cd648
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 22 deletions.
7 changes: 0 additions & 7 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ Ensemble
.. automodule:: pysumma.ensemble
:members:

File Manager
=========
.. automodule:: pysumma.file_manager
:show-inheritance:
:members:
:inherited-members:

Decisions
=========
.. automodule:: pysumma.decisions
Expand Down
2 changes: 1 addition & 1 deletion pysumma/decisions.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def set_option(self, key, value):
else:
raise

def get_constructor_args(self, line):
def _get_constructor_args(self, line):
decision, *value = line.split('!')[0].split()
if isinstance(value, list):
value = " ".join(value).replace("'", "")
Expand Down
19 changes: 17 additions & 2 deletions pysumma/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,32 @@ def __str__(self):
class FileManager(OptionContainer):
"""
The FileManager object provides an interface to
a SUMMA file manager file.
a SUMMA file manager file. Generally, this object
is not meant to be instantiated on its own (though
it can be), but rather as a member of a simulation
object.
Example usage::
import pysumma as ps
s = ps.Simulation('summa.exe', 'file_manager.txt')
print(s.manager)
Parameters
----------
path (string):
The path to the file manager file.
"""

def __init__(self, path):
super().__init__(path, FileManagerOption)

def set_option(self, key, value):
"""Change the value of an option"""
o = self.get_option(key)
o.set_value(value)

def get_constructor_args(self, line):
def _get_constructor_args(self, line):
return (OPTION_NAMES[self.opt_count],
line.split('!')[0].replace("'", "").strip())

Expand Down
2 changes: 1 addition & 1 deletion pysumma/force_file_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def set_option(self, key, value):
o = self.get_option(key)
o.set_value(value)

def get_constructor_args(self, line):
def _get_constructor_args(self, line):
file_name = line.replace("'", "")
return (os.path.join(self.prefix, file_name.strip()), )

Expand Down
4 changes: 2 additions & 2 deletions pysumma/local_param_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ def read(self, path):
self.header.append(line)
elif not line.startswith('!'):
self.options.append(self.OptionType(
*self.get_constructor_args(line)))
*self._get_constructor_args(line)))
self.opt_count += 1

def get_constructor_args(self, line):
def _get_constructor_args(self, line):
param, *value = line.split('|')
default, low, high = map(lambda x: float(x.strip().replace('d', 'e')),
value)
Expand Down
59 changes: 52 additions & 7 deletions pysumma/option.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __str__(self):
return "{} : {}".format(self.name, self.value)


class OptionContainer(object):
class OptionContainer():
"""
Base implementation for representing text based configuration
files for SUMMA. This class is meant to be extended rather than
Expand All @@ -39,7 +39,7 @@ def set_option(self):
"""This has to be implemented by subclasses"""
raise NotImplementedError()

def get_constructor_args(self, line):
def _get_constructor_args(self, line):
"""
This has to be implemented by subclasses
Expand All @@ -62,11 +62,20 @@ def read(self, path):
self.header.append(line)
elif not line.startswith('!'):
self.options.append(self.OptionType(
*self.get_constructor_args(line)))
*self._get_constructor_args(line)))
self.opt_count += 1

def write(self, path=None):
"""Write the configuration given the values of the options"""
"""
Write the configuration based on the string
representation of the OptionType of the instance.
Parameters
----------
path: (string, optional)
Where to write the file. If not provided
the original file will be overwritten.
"""
self.validate()
if not path:
path = self.original_path
Expand All @@ -75,7 +84,18 @@ def write(self, path=None):
f.writelines((str(o) + '\n' for o in self.options))

def get_option(self, name, strict=False):
"""Retrieve an option"""
"""
Retrieve an option
Parameters
----------
name: (string)
The name of the option to search for
strict: (bool, optional)
Whether to raise an error if the option
is not found. Defaults to ``False``, which
does not raise exceptions.
"""
for o in self.options:
if name == o.name:
return o
Expand All @@ -84,7 +104,19 @@ def get_option(self, name, strict=False):
return None

def get_value(self, name, strict=False):
"""Retrieve the value of a given option"""
"""
Retrieve the value of a given option
Parameters
----------
name: (string)
The name of the option whose value
to search for
strict: (bool, optional)
Whether to raise an error if the option
is not found. Defaults to ``False``, which
does not raise exceptions.
"""
for o in self.options:
if name == o.name:
return o.value
Expand All @@ -93,7 +125,19 @@ def get_value(self, name, strict=False):
return None

def remove_option(self, name, strict=False):
"""Remove an option"""
"""
Remove an option
Parameters
----------
name: (string)
The name of the option whose value
to remove
strict: (bool, optional)
Whether to raise an error if the option
is not found. Defaults to ``False``, which
does not raise exceptions.
"""
for i, o in enumerate(self.options):
if name == o.name:
return self.options.pop(i)
Expand All @@ -102,6 +146,7 @@ def remove_option(self, name, strict=False):
return None

def clear(self):
"""Clear all options"""
self.options = []

def validate(self):
Expand Down
3 changes: 1 addition & 2 deletions pysumma/output_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ def set_option(self, name=None, period=None, sum=0, instant=1,
"""
try:
o = self.get_option(name, strict=True)
print(o)
o.period = period
o.sum = sum
o.instant = instant
Expand All @@ -110,5 +109,5 @@ def set_option(self, name=None, period=None, sum=0, instant=1,
else:
raise

def get_constructor_args(self, line):
def _get_constructor_args(self, line):
return [l.strip() for l in line.split('!')[0].split('|')]

0 comments on commit a9cd648

Please sign in to comment.