Skip to content

Commit d898ad3

Browse files
committed
Merge branch 'develop' into feature/sherman/consolidateVerbosityApproach
2 parents 79caec5 + bb75e46 commit d898ad3

File tree

9 files changed

+117
-121
lines changed

9 files changed

+117
-121
lines changed

pygeos_package/pygeos/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
2-
1+
"""@package pygeos
2+
A python module that enables advanced xml features for GEOSX.
3+
"""

pygeos_package/pygeos/__main__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1+
"""Command line tools for pygeos"""
12

23
import argparse
34
from pygeos import xml_processor
45

56

6-
# Entry point for the pygeos console script
77
def main():
8+
"""Entry point for the pygeos console script
9+
10+
@arg input Input file name
11+
@arg -o/--output Output filename (default = randomly generated string)
12+
@arg -s/--schema GEOSX schema to use for validating the generated xml
13+
@arg -v/--verbose Verbosity level
14+
"""
15+
816
# Parse the user arguments
917
parser = argparse.ArgumentParser()
1018
parser.add_argument('input', type=str, help='Input file name')

pygeos_package/pygeos/format_xml.py

Lines changed: 0 additions & 87 deletions
This file was deleted.

pygeos_package/pygeos/regex_tools.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Tools for managing regular expressions in pygeos"""
2+
13
import re
24

35

@@ -26,9 +28,11 @@
2628
symbolic_format = '%1.6e'
2729

2830

29-
# This function is used to evaluate symbolic expressions that are identified
30-
# using the patterns['symbolic'] regex
3131
def SymbolicMathRegexHandler(match):
32+
"""Evaluate symbolic expressions that are identified using the regex_tools.patterns['symbolic'].
33+
34+
@param match A matching string identified by the regex.
35+
"""
3236
k = match.group(1)
3337
if k:
3438
# Sanitize the input
@@ -43,13 +47,22 @@ def SymbolicMathRegexHandler(match):
4347
return
4448

4549

46-
# This class is used to substitute matched values with those stored in a dict
4750
class DictRegexHandler():
51+
"""This class is used to substitute matched values with those stored in a dict."""
52+
4853
def __init__(self):
49-
# Substitution definitions
54+
"""Initialize the handler with an empty target list.
55+
The key/value pairs of self.target indicate which values
56+
to look for and the values they will replace with.
57+
"""
5058
self.target = {}
5159

5260
def __call__(self, match):
61+
"""Replace the matching strings with their target.
62+
63+
@param match A matching string identified by the regex.
64+
"""
65+
5366
k = match.group(1)
5467
if k:
5568
if (k not in self.target.keys()):

pygeos_package/pygeos/table_generator.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
"""Tools for reading/writing GEOSX ascii tables"""
12

23
import numpy as np
34

45

5-
# Write an GEOS-compatible ascii table
6-
# axes_values = A list of the axes values in order
7-
# properties = A dictionary of properties given as an ndarray
8-
# The shape of the array must be consistent with the axes
9-
# axes_names = A list of axes names (optional)
10-
# string_format = Format for table values
116
def write_GEOS_table(axes_values, properties, axes_names=['x', 'y', 'z', 't'], string_format='%1.5e'):
7+
"""Write an GEOS-compatible ascii table.
8+
9+
@param axes_values List of arrays containing the coordinates for each axis of the table.
10+
@param properties Dict of arrays with dimensionality/size defined by the axes_values
11+
@param axes_names Names for each axis (default = ['x', 'y', 'z', 't'])
12+
@param string_format Format for output values (default = %1.5e)
13+
"""
14+
1215
# Check to make sure the axes/property files have the correct shape
1316
axes_shape = tuple([len(x) for x in axes_values])
1417
for k in properties.keys():
@@ -25,11 +28,13 @@ def write_GEOS_table(axes_values, properties, axes_names=['x', 'y', 'z', 't'], s
2528
np.savetxt('%s.geos' % (k), tmp, fmt=string_format, delimiter=',')
2629

2730

28-
# Read an GEOS-compatible ascii table
29-
# axes_files = A list of the axes file names in order
30-
# property_files = A list of the property file names
3131
def read_GEOS_table(axes_files, property_files):
32-
# Open spatial files
32+
"""Read an GEOS-compatible ascii table.
33+
34+
@param axes_files List of the axes file names in order.
35+
@param property_files List of property file names
36+
@return List of axis definitions, dict of property values
37+
"""
3338
axes_values = []
3439
for f in axes_files:
3540
axes_values.append(np.loadtxt('%s.geos' % (f), unpack=True, delimiter=','))
@@ -44,8 +49,9 @@ def read_GEOS_table(axes_files, property_files):
4449
return axes_values, properties
4550

4651

47-
# Example of how to read/write GEOS tables using the above functions
4852
def write_read_GEOS_table_example():
53+
"""Table read / write example."""
54+
4955
# Define table axes
5056
a = np.array([0.0, 1.0])
5157
b = np.array([0.0, 0.5, 1.0])

pygeos_package/pygeos/tests/generate_test_xml.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
1+
"""Tool for generating test xml files for processing."""
12

23
from lxml import etree as ElementTree
34
import os
45

56

6-
# Format the xml files to enable comparison
77
def pretty_print_xml(init, final):
8+
"""Format the xml files to enable comparison
9+
10+
@param init The input file name.
11+
@param final The output file name.
12+
"""
813
parser = ElementTree.XMLParser(remove_comments=True, remove_blank_text=True)
914
tree = ElementTree.parse(init, parser)
1015
tree.write(final, pretty_print=True)
1116
os.remove(init)
1217

1318

14-
# Build example input/output xml files, which can be used to test the parser
15-
# These are derived from a GEOSX integrated test xml
1619
def generate_test_xml_files(root_dir):
20+
"""Build example input/output xml files, which can be used to test the parser.
21+
These are derived from a GEOSX integrated test xml.
22+
23+
@param root_dir The folder to write the example xml files.
24+
"""
25+
1726
# Build segments of an xml file that can be compiled to form a test
1827
# File header/footer
1928
xml_header = """<Problem>"""

pygeos_package/pygeos/unit_manager.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1+
"""Tools for managing units in GEOSX"""
12

23
import re
34
from pygeos import regex_tools
45

56

6-
# A structure for creating and applying unit definitions
77
class UnitManager():
8+
"""This class is used to manage unit definitions."""
9+
810
def __init__(self):
11+
"""Initialize the class by creating an instance of the dict regex handler, building units."""
912
self.units = {}
1013
self.unitMatcher = regex_tools.DictRegexHandler()
1114
self.buildUnits()
1215

13-
# Evaluate the symbolic expression
1416
def __call__(self, unitStruct):
17+
"""Evaluate the symbolic expression for matched strings.
18+
19+
@param unitStruct A list containing the variable scale and the unit definition.
20+
"""
21+
1522
# Replace all instances of units in the string with their scale defined in self.units
1623
symbolicUnits = re.sub(regex_tools.patterns['units_b'], self.unitMatcher, unitStruct[1])
1724

@@ -26,14 +33,18 @@ def __call__(self, unitStruct):
2633
str_value = re.sub(regex_tools.patterns['strip_trailing_b'], '', str_value)
2734
return str_value
2835

29-
# This function is called when the regex identifies unit definitions in a string
3036
def regexHandler(self, match):
37+
"""Split the matched string into a scale and unit definition.
38+
39+
@param match The matching string from the regex.
40+
"""
3141
# The first matched group includes the scale of the value (e.g. 1.234)
3242
# The second matches the string inside the unit definition (e.g. m/s**2)
3343
return self.__call__([match.group(1), match.group(2)])
3444

35-
# Build the unit definitions
3645
def buildUnits(self):
46+
"""Build the unit definitions."""
47+
3748
# Long, short names for SI prefixes
3849
prefixes = {'giga': {'value': 1e9, 'alt': 'G'},
3950
'mega': {'value': 1e6, 'alt': 'M'},

pygeos_package/pygeos/xml_processor.py

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Tools for processing xml files in GEOSX"""
2+
13
from lxml import etree as ElementTree
24
from lxml.etree import XMLSyntaxError
35
import re
@@ -10,9 +12,14 @@
1012
parameterHandler = regex_tools.DictRegexHandler()
1113

1214

13-
# Merge nodes in an included file into the current
14-
# structure level by level
1515
def merge_xml_nodes(existingNode, targetNode, level):
16+
"""Merge nodes in an included file into the current structure level by level.
17+
18+
@param existingNode The current node in the base xml structure.
19+
@param targetNode The node to insert.
20+
@param level The xml file depth.
21+
"""
22+
1623
# Copy attributes on the current level
1724
for tk in targetNode.attrib.keys():
1825
existingNode.set(tk, targetNode.get(tk))
@@ -51,8 +58,15 @@ def merge_xml_nodes(existingNode, targetNode, level):
5158
existingNode.insert(-1, target)
5259

5360

54-
# Recursively merge included files into the current structure
5561
def merge_included_xml_files(root, fname, includeCount, maxInclude=100):
62+
"""Recursively merge included files into the current structure.
63+
64+
@param root The root node of the base xml structure.
65+
@param fname The name of the target xml file to merge.
66+
@param includeCount The current recursion depth.
67+
@param maxInclude The maximum number of xml files to include (default = 100)
68+
"""
69+
5670
# Expand the input path
5771
pwd = os.getcwd()
5872
includePath, fname = os.path.split(os.path.abspath(os.path.expanduser(fname)))
@@ -88,9 +102,13 @@ def merge_included_xml_files(root, fname, includeCount, maxInclude=100):
88102
os.chdir(pwd)
89103

90104

91-
# Apply regexes that handle parameters, units, and symbolic math to
92-
# each xml attribute in the structure
93105
def apply_regex_to_node(node):
106+
"""Apply regexes that handle parameters, units, and symbolic math to each
107+
xml attribute in the structure.
108+
109+
@param node The target node in the xml structure.
110+
"""
111+
94112
for k in node.attrib.keys():
95113
value = node.get(k)
96114

@@ -126,8 +144,12 @@ def apply_regex_to_node(node):
126144
apply_regex_to_node(subNode)
127145

128146

129-
# If the target name is not specified, generate a random name for the compiled xml
130147
def generate_random_name(prefix='', suffix='.xml'):
148+
"""If the target name is not specified, generate a random name for the compiled xml
149+
150+
@param prefix The file prefix (default = '').
151+
@param suffix The file suffix (default = '.xml')
152+
"""
131153
from hashlib import md5
132154
from time import time
133155
from os import getpid
@@ -136,8 +158,16 @@ def generate_random_name(prefix='', suffix='.xml'):
136158
return '%s%s%s' % (prefix, md5(tmp.encode('utf-8')).hexdigest(), suffix)
137159

138160

139-
# Process an xml file
140161
def process(inputFile, outputFile='', schema='', verbose=0, keep_parameters=True, keep_includes=True):
162+
"""Process an xml file
163+
164+
@param inputFile Input file name.
165+
@param outputFile Output file name (if not specified, then generate randomly).
166+
@param schema Schema file name to validate the final xml (if not specified, then do not validate).
167+
@param verbose Verbosity level.
168+
@param keep_parameters If True, then keep parameters in the compiled file (default = True)
169+
@param keep_includes If True, then keep includes in the compiled file (default = True)
170+
"""
141171
if verbose:
142172
print('\nReading input xml parameters and parsing symbolic math...')
143173

@@ -206,8 +236,13 @@ def process(inputFile, outputFile='', schema='', verbose=0, keep_parameters=True
206236
return outputFile
207237

208238

209-
# Validate an xml file, and parse the warnings
210239
def validate_xml(fname, schema, verbose):
240+
"""Validate an xml file, and parse the warnings.
241+
242+
@param fname Target xml file name.
243+
@param schema Schema file name.
244+
@param verbose Verbosity level.
245+
"""
211246
if verbose:
212247
print('Validating the xml against the schema...')
213248
try:

0 commit comments

Comments
 (0)