Skip to content

Commit

Permalink
fixed xml parsing of text fields containing parentheses (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
kip-hart committed Nov 5, 2020
1 parent b2350c9 commit 1558c7f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 43 deletions.
1 change: 1 addition & 0 deletions .github/workflows/python_package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ jobs:
- name: Install test dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel
pip install flake8 pytest
- name: Install package
run: |
Expand Down
21 changes: 14 additions & 7 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ All notable changes to this project will be documented in this file.
The format is based on `Keep a Changelog`_,
and this project adheres to `Semantic Versioning`_.

`1.4.1`_
`1.4.2`_ - 2020-11-3
--------------------------
Fixed
'''''''
- XML parsing text with parentheses.

`1.4.1`_ - 2020-10-13
--------------------------
Changed
'''''''
- Upgraded to pygmsh v7.0.2.

`1.4.0`_
`1.4.0`_ - 2020-10-06
--------------------------
Added
'''''''
Expand All @@ -25,19 +31,19 @@ Fixed
- Color-by seed number in CLI TriMesh plot function.
- Expansion of "~" in input filepaths.

`1.3.5`_ - 2020-09-20
`1.3.5`_ - 2020-09-20
--------------------------
Fixed
'''''''
- Tetrahedral mesh maximum volume setting no longer ignored.

`1.3.4`_ - 2020-08-31
`1.3.4`_ - 2020-08-31
--------------------------
Removed
'''''''
- Debug print statements from SeedList population fractions method.

`1.3.3`_ - 2020-08-31
`1.3.3`_ - 2020-08-31
--------------------------
Added
'''''
Expand All @@ -49,7 +55,7 @@ Fixed
- XML tags in documentation on position distributions.


`1.3.2`_ - 2020-07-11
`1.3.2`_ - 2020-07-11
--------------------------
Added
'''''
Expand Down Expand Up @@ -172,7 +178,8 @@ Added

.. LINKS
.. _`Unreleased`: https://github.com/kip-hart/MicroStructPy/compare/v1.4.1...HEAD
.. _`Unreleased`: https://github.com/kip-hart/MicroStructPy/compare/v1.4.2...HEAD
.. _`1.4.2`: https://github.com/kip-hart/MicroStructPy/compare/v1.4.1...v1.4.2
.. _`1.4.1`: https://github.com/kip-hart/MicroStructPy/compare/v1.4.0...v1.4.1
.. _`1.4.0`: https://github.com/kip-hart/MicroStructPy/compare/v1.3.5...v1.4.0
.. _`1.3.5`: https://github.com/kip-hart/MicroStructPy/compare/v1.3.4...v1.3.5
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ matplotlib==3.0.2
pybind11==2.4.3
pygmsh==7.0.2
MeshPy==2018.2.1
numpy==1.19.0
numpy==1.19.4
pyquaternion==0.9.5
pyvoro-mmalahe==1.3.3
scipy==1.5.0
scipy==1.5.4
xmltodict==0.12.0
tox==3.14.0
lsq-ellipse==2.0.1
2 changes: 1 addition & 1 deletion src/microstructpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
import microstructpy.seeding
import microstructpy.verification

__version__ = '1.4.1'
__version__ = '1.4.2'
82 changes: 49 additions & 33 deletions src/microstructpy/_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,42 +48,58 @@ def from_str(string):
end_delims = (')', ']', '}', '>')

string = string.strip()
if any([c in string for c in beg_delims + end_delims]) or ',' in string:
if string[0] in beg_delims:
string = string[1:]
if string[-1] in end_delims:
string = string[:-1]
val = []
n_beg = 0
n_end = 0
elem_str = ''
for char in string:
if char in beg_delims:
n_beg += 1
elif char in end_delims:
n_end += 1

if (char == ',') and n_beg == n_end:
val.append(from_str(elem_str.strip()))
elem_str = ''
else:
elem_str += char
val.append(from_str(elem_str.strip()))
return val
has_delims = False
for beg, end in zip(beg_delims, end_delims):
has_beg = string.startswith(beg)
has_end = string.endswith(end)
has_delims |= has_beg and has_end
if has_delims or ',' in string:
val = _list_from_str(string, beg_delims, end_delims)
else:
val = _single_from_str(string)
return val


def _list_from_str(string, beg_delims, end_delims):
if string[0] in beg_delims:
string = string[1:]
if string[-1] in end_delims:
string = string[:-1]
val = []
n_beg = 0
n_end = 0
elem_str = ''
for char in string:
if char in beg_delims:
n_beg += 1
elif char in end_delims:
n_end += 1

if (char == ',') and n_beg == n_end:
val.append(from_str(elem_str.strip()))
elem_str = ''
else:
elem_str += char
if elem_str == string and ',' in string:
return _single_from_str(string)
val.append(from_str(elem_str.strip()))
return val


def _single_from_str(string):
try:
val = int(string)
except ValueError:
try:
val = int(string)
val = float(string)
except ValueError:
try:
val = float(string)
except ValueError:
if string.lower() in ('true', 'yes'):
val = True
elif string.lower() in ('false', 'no'):
val = False
else:
val = str(string)
return val
if string.lower() in ('true', 'yes'):
val = True
elif string.lower() in ('false', 'no'):
val = False
else:
val = str(string)
return val


# --------------------------------------------------------------------------- #
Expand Down

0 comments on commit 1558c7f

Please sign in to comment.