Skip to content

Commit 0e003f0

Browse files
authored
Merge pull request #33 from paulovcmedeiros/main
Fix problems with type='' in string padding syntax
2 parents ea6ab17 + 9258c24 commit 0e003f0

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

trollsift/parser.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,14 @@ def convert_field(self, value, conversion):
163163
spec_regexes['E'] = spec_regexes['f']
164164
spec_regexes['g'] = spec_regexes['f']
165165
spec_regexes['X'] = spec_regexes['x']
166-
allow_multiple = ['c', 'd', 'o', 's', 'x', 'X']
166+
spec_regexes[''] = spec_regexes['s']
167+
allow_multiple = ['c', 'd', 'o', 's', '', 'x', 'X']
167168
fixed_point_types = ['f', 'e', 'E', 'g']
168169
# format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]
169170
# https://docs.python.org/3.4/library/string.html#format-specification-mini-language
170171
fmt_spec_regex = re.compile(
171172
r'(?P<align>(?P<fill>.)?[<>=^])?(?P<sign>[\+\-\s])?(?P<pound>#)?(?P<zero>0)?(?P<width>\d+)?'
172-
r'(?P<comma>,)?(?P<precision>.\d+)?(?P<type>[bcdeEfFgGnosxX%])')
173+
r'(?P<comma>,)?(?P<precision>.\d+)?(?P<type>[bcdeEfFgGnosxX%]?)')
173174

174175

175176
def _get_fixed_point_regex(regex_dict, width, precision):
@@ -294,7 +295,7 @@ def format_spec_to_regex(field_name, format_spec):
294295
if fill is None:
295296
if width is not None and width[0] == '0':
296297
fill = '0'
297-
elif ftype in ['s', 'd']:
298+
elif ftype in ['s', '', 'd']:
298299
fill = ' '
299300

300301
char_type = spec_regexes[ftype]
@@ -304,7 +305,7 @@ def format_spec_to_regex(field_name, format_spec):
304305
width=width,
305306
precision=precision
306307
)
307-
if ftype == 's' and align and align.endswith('='):
308+
if ftype in ('s', '') and align and align.endswith('='):
308309
raise ValueError("Invalid format specification: '{}'".format(format_spec))
309310
final_regex = char_type
310311
if ftype in allow_multiple and (not width or width == '0'):
@@ -387,19 +388,15 @@ def _get_number_from_fmt(fmt):
387388

388389
def _convert(convdef, stri):
389390
"""Convert the string *stri* to the given conversion definition *convdef*."""
390-
is_fixed_point = any([ftype in convdef for ftype in fixed_point_types])
391391
if '%' in convdef:
392392
result = dt.datetime.strptime(stri, convdef)
393-
elif 'd' in convdef or 's' in convdef or is_fixed_point:
394-
stri = _strip_padding(convdef, stri)
395-
if 'd' in convdef:
396-
result = int(stri)
397-
elif is_fixed_point:
398-
result = float(stri)
399-
else:
400-
result = stri
401393
else:
402-
result = stri
394+
result = _strip_padding(convdef, stri)
395+
if 'd' in convdef:
396+
result = int(result)
397+
elif any(float_type_marker in convdef for float_type_marker in fixed_point_types):
398+
result = float(result)
399+
403400
return result
404401

405402

trollsift/tests/unittests/test_parser.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,19 @@ def test_parse(self):
126126
'time': dt.datetime(2014, 2, 12, 14, 12),
127127
'orbit': 12345})
128128

129+
def test_parse_string_padding_syntax_with_and_without_s(self):
130+
"""Test that, in string padding syntax, '' is equivalent to 's'.
131+
132+
From <https://docs.python.org/3.4/library/string.html#format-specification-mini-language>:
133+
* Type 's': String format. This is the default type for strings and may be omitted.
134+
* Type None: The same as 's'.
135+
"""
136+
result = parse('{foo}/{bar:_<8}', 'baz/qux_____')
137+
expected_result = parse('{foo}/{bar:_<8s}', 'baz/qux_____')
138+
self.assertEqual(expected_result["foo"], "baz")
139+
self.assertEqual(expected_result["bar"], "qux")
140+
self.assertEqual(result, expected_result)
141+
129142
def test_parse_wildcards(self):
130143
# Run
131144
result = parse(

0 commit comments

Comments
 (0)