Skip to content

Commit 7dfb3b3

Browse files
committed
fixed string parsing errors, bumped version
1 parent d37f298 commit 7dfb3b3

File tree

4 files changed

+14
-58
lines changed

4 files changed

+14
-58
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ 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-
`Unreleased`_
9+
`1.4.6`_ - 2021-02-07
1010
--------------------------
1111
Fixed
1212
'''''''
13+
- String parsing errors.
1314
- Logo example failing on ReadTheDocs.
1415
- 3D gmsh with variable mesh sizes.
1516

@@ -207,8 +208,9 @@ Added
207208

208209
.. LINKS
209210
210-
.. _`Unreleased`: https://github.com/kip-hart/MicroStructPy/compare/v1.4.5...HEAD
211-
.. _`1.4.5`: https://github.com/kip-hart/MicroStructPy/compare/v1.4.5...v1.4.5
211+
.. _`Unreleased`: https://github.com/kip-hart/MicroStructPy/compare/v1.4.6...HEAD
212+
.. _`1.4.6`: https://github.com/kip-hart/MicroStructPy/compare/v1.4.5...v1.4.6
213+
.. _`1.4.5`: https://github.com/kip-hart/MicroStructPy/compare/v1.4.4...v1.4.5
212214
.. _`1.4.4`: https://github.com/kip-hart/MicroStructPy/compare/v1.4.3...v1.4.4
213215
.. _`1.4.3`: https://github.com/kip-hart/MicroStructPy/compare/v1.4.2...v1.4.3
214216
.. _`1.4.2`: https://github.com/kip-hart/MicroStructPy/compare/v1.4.1...v1.4.2

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.5'
7+
__version__ = '1.4.6'

src/microstructpy/_misc.py

Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
33
This private module contains miscellaneous functions.
44
"""
5+
import ast
6+
57
import numpy as np
68

79
__author__ = 'Kenneth (Kip) Hart'
@@ -44,61 +46,11 @@ def from_str(string):
4446
The value in the string.
4547
4648
"""
47-
beg_delims = ('(', '[', '{', '<')
48-
end_delims = (')', ']', '}', '>')
49-
50-
string = string.strip()
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)
58-
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):
49+
s = string.strip()
9050
try:
91-
val = int(string)
92-
except ValueError:
93-
try:
94-
val = float(string)
95-
except ValueError:
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)
51+
val = ast.literal_eval(s)
52+
except (ValueError, SyntaxError):
53+
val = s
10254
return val
10355

10456

src/microstructpy/seeding/seed.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ def __str__(self):
211211
str_str += str(self.geometry) + '\n'
212212
str_str += 'Phase: ' + str(self.phase) + '\n'
213213
bkdwn_str = ', '.join([str(tuple(b)) for b in self.breakdown])
214+
if len(self.breakdown) == 1:
215+
bkdwn_str += ',' # breakdowns will be a tuple of length 1
214216
str_str += 'Breakdown: (' + bkdwn_str + ')\n'
215217
str_str += 'Position: (' + ', '.join([str(x) for x in self.position])
216218
str_str += ')'

0 commit comments

Comments
 (0)