Skip to content

Commit 1558c7f

Browse files
authored
fixed xml parsing of text fields containing parentheses (#33)
1 parent b2350c9 commit 1558c7f

File tree

5 files changed

+67
-43
lines changed

5 files changed

+67
-43
lines changed

.github/workflows/python_package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ jobs:
7878
- name: Install test dependencies
7979
run: |
8080
python -m pip install --upgrade pip
81+
pip install setuptools wheel
8182
pip install flake8 pytest
8283
- name: Install package
8384
run: |

CHANGELOG.rst

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ All notable changes to this project will be documented in this file.
66
The format is based on `Keep a Changelog`_,
77
and this project adheres to `Semantic Versioning`_.
88

9-
`1.4.1`_
9+
`1.4.2`_ - 2020-11-3
10+
--------------------------
11+
Fixed
12+
'''''''
13+
- XML parsing text with parentheses.
14+
15+
`1.4.1`_ - 2020-10-13
1016
--------------------------
1117
Changed
1218
'''''''
1319
- Upgraded to pygmsh v7.0.2.
1420

15-
`1.4.0`_
21+
`1.4.0`_ - 2020-10-06
1622
--------------------------
1723
Added
1824
'''''''
@@ -25,19 +31,19 @@ Fixed
2531
- Color-by seed number in CLI TriMesh plot function.
2632
- Expansion of "~" in input filepaths.
2733

28-
`1.3.5`_ - 2020-09-20
34+
`1.3.5`_ - 2020-09-20
2935
--------------------------
3036
Fixed
3137
'''''''
3238
- Tetrahedral mesh maximum volume setting no longer ignored.
3339

34-
`1.3.4`_ - 2020-08-31
40+
`1.3.4`_ - 2020-08-31
3541
--------------------------
3642
Removed
3743
'''''''
3844
- Debug print statements from SeedList population fractions method.
3945

40-
`1.3.3`_ - 2020-08-31
46+
`1.3.3`_ - 2020-08-31
4147
--------------------------
4248
Added
4349
'''''
@@ -49,7 +55,7 @@ Fixed
4955
- XML tags in documentation on position distributions.
5056

5157

52-
`1.3.2`_ - 2020-07-11
58+
`1.3.2`_ - 2020-07-11
5359
--------------------------
5460
Added
5561
'''''
@@ -172,7 +178,8 @@ Added
172178

173179
.. LINKS
174180
175-
.. _`Unreleased`: https://github.com/kip-hart/MicroStructPy/compare/v1.4.1...HEAD
181+
.. _`Unreleased`: https://github.com/kip-hart/MicroStructPy/compare/v1.4.2...HEAD
182+
.. _`1.4.2`: https://github.com/kip-hart/MicroStructPy/compare/v1.4.1...v1.4.2
176183
.. _`1.4.1`: https://github.com/kip-hart/MicroStructPy/compare/v1.4.0...v1.4.1
177184
.. _`1.4.0`: https://github.com/kip-hart/MicroStructPy/compare/v1.3.5...v1.4.0
178185
.. _`1.3.5`: https://github.com/kip-hart/MicroStructPy/compare/v1.3.4...v1.3.5

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ matplotlib==3.0.2
33
pybind11==2.4.3
44
pygmsh==7.0.2
55
MeshPy==2018.2.1
6-
numpy==1.19.0
6+
numpy==1.19.4
77
pyquaternion==0.9.5
88
pyvoro-mmalahe==1.3.3
9-
scipy==1.5.0
9+
scipy==1.5.4
1010
xmltodict==0.12.0
1111
tox==3.14.0
1212
lsq-ellipse==2.0.1

src/microstructpy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
import microstructpy.seeding
55
import microstructpy.verification
66

7-
__version__ = '1.4.1'
7+
__version__ = '1.4.2'

src/microstructpy/_misc.py

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,42 +48,58 @@ def from_str(string):
4848
end_delims = (')', ']', '}', '>')
4949

5050
string = string.strip()
51-
if any([c in string for c in beg_delims + end_delims]) or ',' in string:
52-
if string[0] in beg_delims:
53-
string = string[1:]
54-
if string[-1] in end_delims:
55-
string = string[:-1]
56-
val = []
57-
n_beg = 0
58-
n_end = 0
59-
elem_str = ''
60-
for char in string:
61-
if char in beg_delims:
62-
n_beg += 1
63-
elif char in end_delims:
64-
n_end += 1
65-
66-
if (char == ',') and n_beg == n_end:
67-
val.append(from_str(elem_str.strip()))
68-
elem_str = ''
69-
else:
70-
elem_str += char
71-
val.append(from_str(elem_str.strip()))
72-
return val
51+
has_delims = False
52+
for beg, end in zip(beg_delims, end_delims):
53+
has_beg = string.startswith(beg)
54+
has_end = string.endswith(end)
55+
has_delims |= has_beg and has_end
56+
if has_delims or ',' in string:
57+
val = _list_from_str(string, beg_delims, end_delims)
7358
else:
59+
val = _single_from_str(string)
60+
return val
61+
62+
63+
def _list_from_str(string, beg_delims, end_delims):
64+
if string[0] in beg_delims:
65+
string = string[1:]
66+
if string[-1] in end_delims:
67+
string = string[:-1]
68+
val = []
69+
n_beg = 0
70+
n_end = 0
71+
elem_str = ''
72+
for char in string:
73+
if char in beg_delims:
74+
n_beg += 1
75+
elif char in end_delims:
76+
n_end += 1
77+
78+
if (char == ',') and n_beg == n_end:
79+
val.append(from_str(elem_str.strip()))
80+
elem_str = ''
81+
else:
82+
elem_str += char
83+
if elem_str == string and ',' in string:
84+
return _single_from_str(string)
85+
val.append(from_str(elem_str.strip()))
86+
return val
87+
88+
89+
def _single_from_str(string):
90+
try:
91+
val = int(string)
92+
except ValueError:
7493
try:
75-
val = int(string)
94+
val = float(string)
7695
except ValueError:
77-
try:
78-
val = float(string)
79-
except ValueError:
80-
if string.lower() in ('true', 'yes'):
81-
val = True
82-
elif string.lower() in ('false', 'no'):
83-
val = False
84-
else:
85-
val = str(string)
86-
return val
96+
if string.lower() in ('true', 'yes'):
97+
val = True
98+
elif string.lower() in ('false', 'no'):
99+
val = False
100+
else:
101+
val = str(string)
102+
return val
87103

88104

89105
# --------------------------------------------------------------------------- #

0 commit comments

Comments
 (0)