opgee.utils¶
This module contains constants, functions, and classes that are used by various other modules of the opgee package.
API¶
- class opgee.utils.ParseCommaList(option_strings, dest, nargs=None, **kwargs)¶
- opgee.utils.coercible(value, pytype, raiseError=True)¶
Attempt to coerce a value to pytype and raise an error on failure. If the value is a pint.Quantity, the value is simply returned.
- Parameters:
value – any value coercible to pytype
pytype – any Python type or its string equivalent
raiseError – (bool) whether to raise errors when appropriate
- Returns:
(pytype) the coerced value, if it’s coercible, otherwise None if raiseError is False
- Raises:
OpgeeException – if the value is a pint.Quantity, it is returned unchanged. Otherwise, if not coercible and raiseError is True, error is raised.
- opgee.utils.filecopy(src, dst, removeDst=True)¶
Copy src file to dst, optionally removing dst first to avoid writing through symlinks
- opgee.utils.flatten(listOfLists)¶
Flatten one level of nesting given a list of lists. That is, convert [[1, 2, 3], [4, 5, 6]] to [1, 2, 3, 4, 5, 6].
- Parameters:
listOfLists – a list of lists, obviously
- Returns:
the flattened list
- opgee.utils.getBooleanXML(value)¶
Get a value from an XML file and convert it into a boolean True or False.
- Parameters:
value – any value (it’s first converted to a lower-case string)
- Returns:
True if the value is in [‘true’, ‘yes’, ‘1’], False if the value is in [‘false’, ‘no’, ‘0’, ‘none’]. An exception is raised if any other value is passed.
- Raises:
OpgeeException
- opgee.utils.getResource(relpath)¶
Extract a resource (e.g., file) from the given relative path in the pygcam package.
- Parameters:
relpath – (str) a path relative to the pygcam package
- Returns:
the file contents
- Raises:
OpgeeException if the resource isn’t found
- opgee.utils.loadModuleFromPath(module_path, raiseError=True)¶
Load a module from a ‘.py’ or ‘.pyc’ file from a path that ends in the module name, i.e., from “foo/bar/Baz.py”, the module name is ‘Baz’.
- Parameters:
module_path – (str) the pathname of a python module (.py or .pyc)
raiseError – (bool) if True, raise an error if the module cannot be loaded
- Returns:
(module) a reference to the loaded module, if loaded, else None.
- Raises:
OpgeeException
- opgee.utils.mkdirs(newdir, mode=504)¶
Try to create the full path newdir and ignore the error if it already exists.
- Parameters:
newdir – the directory to create (along with any needed parent directories)
- Returns:
nothing
- opgee.utils.parseTrialString(string)¶
Converts a comma-separated list of ranges into a list of numbers. Ex. 1,3,4-6,2 becomes [1,3,4,5,6,2]. Duplicates are deleted. This function is the inverse of
createTrialString().- Parameters:
string – (str) comma-separate list of ints or int ranges indicated by two ints separated by a hyphen.
- Returns:
(list) a list of ints
- opgee.utils.pushd(directory)¶
Context manager that changes to the given directory and then returns to the original directory. Usage is
with pushd('/foo/bar'): ...- Parameters:
directory – (str) a directory to chdir to temporarily
- Returns:
none
- opgee.xml_utils.merge_element(parent, new_elt)¶
Add an element if none of parent’s children has the same tag and attributes as element. If a match is found, add element’s children to those of the matching element.
- opgee.xml_utils.merge_elements(parent, elt_list)¶
Add each element in elt_list to parent if none of parent’s children has the same tag and attributes as elt. If a match is found, merge elt’s children with those of the the matching element, recursively.
- opgee.xml_utils.merge_siblings(elt1, elt2)¶
Merge elt2 into elt1.
- Returns:
none (elt1 is modified)