Skip to content

Commit 260fcc3

Browse files
authored
Merge pull request #294 from dartmouth-pbs/bf-pretty-json-dates
Fixing up the abomination -- the regex to remove multiple spaces before numbers
2 parents 9237fb2 + 7b2e8c5 commit 260fcc3

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

heudiconv/tests/test_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,10 @@ def test_json_dumps_pretty():
5252
== '{\n "a": -1,\n "b": "123",\n "c": [1, 2, 3],\n "d": ["1.0", "2.0"]\n}'
5353
assert pretty({'a': ["0.3", "-1.9128906358217845e-12", "0.2"]}) \
5454
== '{\n "a": ["0.3", "-1.9128906358217845e-12", "0.2"]\n}'
55+
# original, longer string
56+
tstr = 'f9a7d4be-a7d7-47d2-9de0-b21e9cd10755||' \
57+
'Sequence: ve11b/master r/50434d5; ' \
58+
'Mar 3 2017 10:46:13 by eja'
59+
# just the date which reveals the issue
60+
# tstr = 'Mar 3 2017 10:46:13 by eja'
61+
assert pretty({'WipMemBlock': tstr}) == '{\n "WipMemBlock": "%s"\n}' % tstr

heudiconv/utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,16 @@ def json_dumps_pretty(j, indent=2, sort_keys=True):
217217
# uniform no spaces before ]
218218
js_ = re.sub(" *\]", "]", js_)
219219
# uniform spacing before numbers
220-
js_ = re.sub(' *("?[-+.0-9e]+"?)(?P<space> ?)[ \n]*',
221-
r' \1\g<space>', js_)
220+
# But that thing could screw up dates within strings which would have 2 spaces
221+
# in a date like Mar 3 2017, so we do negative lookahead to avoid changing
222+
# in those cases
223+
#import pdb; pdb.set_trace()
224+
js_ = re.sub(
225+
'(?<!\w{3})' # negative lookbehind for the month
226+
' *("?[-+.0-9e]+"?)'
227+
'(?! [123]\d{3})' # negative lookahead for a year
228+
'(?P<space> ?)[ \n]*',
229+
r' \1\g<space>', js_)
222230
# no spaces after [
223231
js_ = re.sub('\[ ', '[', js_)
224232
# the load from the original dump and reload from tuned up

0 commit comments

Comments
 (0)