Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 36b3ede

Browse files
committed
separate single-date parsing from walking extraction
makes code more readable
1 parent 3b87a51 commit 36b3ede

2 files changed

Lines changed: 28 additions & 16 deletions

File tree

IPython/parallel/client/client.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
from IPython.utils.capture import RichOutput
3939
from IPython.utils.coloransi import TermColors
40-
from IPython.utils.jsonutil import rekey, extract_dates
40+
from IPython.utils.jsonutil import rekey, extract_dates, parse_date
4141
from IPython.utils.localinterfaces import localhost, is_local_ip
4242
from IPython.utils.path import get_ipython_dir
4343
from IPython.utils.py3compat import cast_bytes, string_types, xrange, iteritems
@@ -675,7 +675,7 @@ def _extract_metadata(self, msg):
675675
if 'date' in parent:
676676
md['submitted'] = parent['date']
677677
if 'started' in msg_meta:
678-
md['started'] = extract_dates(msg_meta['started'])
678+
md['started'] = parse_date(msg_meta['started'])
679679
if 'date' in header:
680680
md['completed'] = header['date']
681681
return md
@@ -1580,7 +1580,7 @@ def result_status(self, msg_ids, status_only=True):
15801580
)
15811581
md.update(self._extract_metadata(md_msg))
15821582
if rec.get('received'):
1583-
md['received'] = extract_dates(rec['received'])
1583+
md['received'] = parse_date(rec['received'])
15841584
md.update(iodict)
15851585

15861586
if rcontent['status'] == 'ok':
@@ -1843,12 +1843,12 @@ def db_query(self, query, keys=None):
18431843
has_rbufs = result_buffer_lens is not None
18441844
for i,rec in enumerate(records):
18451845
# unpack datetime objects
1846-
for dtkey in ('header', 'result_header',
1847-
'submitted', 'started',
1848-
'completed', 'received',
1849-
):
1846+
for hkey in ('header', 'result_header'):
1847+
if hkey in rec:
1848+
rec[hkey] = extract_dates(rec[hkey])
1849+
for dtkey in ('submitted', 'started', 'completed', 'received'):
18501850
if dtkey in rec:
1851-
rec[dtkey] = extract_dates(rec[dtkey])
1851+
rec[dtkey] = parse_date(rec[dtkey])
18521852
# relink buffers
18531853
if has_bufs:
18541854
blen = buffer_lens[i]

IPython/utils/jsonutil.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,34 @@ def rekey(dikt):
6262
dikt[nk] = dikt.pop(k)
6363
return dikt
6464

65+
def parse_date(s):
66+
"""parse an ISO8601 date string
67+
68+
If it is None or not a valid ISO8601 timestamp,
69+
it will be returned unmodified.
70+
Otherwise, it will return a datetime object.
71+
"""
72+
if s is None:
73+
return s
74+
m = ISO8601_PAT.match(s)
75+
if m:
76+
# FIXME: add actual timezone support
77+
# this just drops the timezone info
78+
notz = m.groups()[0]
79+
return datetime.strptime(notz, ISO8601)
80+
return s
6581

6682
def extract_dates(obj):
6783
"""extract ISO8601 dates from unpacked JSON"""
6884
if isinstance(obj, dict):
69-
obj = dict(obj) # don't clobber
85+
new_obj = {} # don't clobber
7086
for k,v in iteritems(obj):
71-
obj[k] = extract_dates(v)
87+
new_obj[k] = extract_dates(v)
88+
obj = new_obj
7289
elif isinstance(obj, (list, tuple)):
7390
obj = [ extract_dates(o) for o in obj ]
7491
elif isinstance(obj, string_types):
75-
m = ISO8601_PAT.match(obj)
76-
if m:
77-
# FIXME: add actual timezone support
78-
# this just drops the timezone info
79-
notz = m.groups()[0]
80-
obj = datetime.strptime(notz, ISO8601)
92+
obj = parse_date(obj)
8193
return obj
8294

8395
def squash_dates(obj):

0 commit comments

Comments
 (0)