360
361
utcnow.override_time = None
363
def strtime(at=None, fmt=PERFECT_TIME_FORMAT):
364
"""Returns formatted utcnow."""
364
def isotime(at=None):
365
"""Returns iso formatted utcnow."""
367
return at.strftime(fmt)
370
def parse_strtime(timestr, fmt=PERFECT_TIME_FORMAT):
371
"""Turn a formatted time back into a datetime."""
372
return datetime.datetime.strptime(timestr, fmt)
375
def isotime(at=None):
376
"""Returns iso formatted utcnow."""
377
return strtime(at, ISO_TIME_FORMAT)
368
return at.strftime(TIME_FORMAT)
380
371
def parse_isotime(timestr):
381
372
"""Turn an iso formatted time back into a datetime."""
382
return parse_strtime(timestr, ISO_TIME_FORMAT)
373
return datetime.datetime.strptime(timestr, TIME_FORMAT)
385
376
def parse_mailmap(mailmap='.mailmap'):
516
def to_primitive(value, convert_instances=False, level=0):
517
"""Convert a complex object into primitives.
519
Handy for JSON serialization. We can optionally handle instances,
520
but since this is a recursive function, we could have cyclical
523
To handle cyclical data structures we could track the actual objects
524
visited in a set, but not all objects are hashable. Instead we just
525
track the depth of the object inspections and don't go too deep.
527
Therefore, convert_instances=True is lossy ... be aware.
530
if inspect.isclass(value):
531
return unicode(value)
536
# The try block may not be necessary after the class check above,
537
# but just in case ...
539
if type(value) is type([]) or type(value) is type((None,)):
542
o.append(to_primitive(v, convert_instances=convert_instances,
545
elif type(value) is type({}):
547
for k, v in value.iteritems():
548
o[k] = to_primitive(v, convert_instances=convert_instances,
551
elif isinstance(value, datetime.datetime):
553
elif hasattr(value, 'iteritems'):
554
return to_primitive(dict(value.iteritems()),
555
convert_instances=convert_instances,
557
elif hasattr(value, '__iter__'):
558
return to_primitive(list(value), level)
559
elif convert_instances and hasattr(value, '__dict__'):
560
# Likely an instance of something. Watch for cycles.
561
# Ignore class member vars.
562
return to_primitive(value.__dict__,
563
convert_instances=convert_instances,
568
# Class objects are tricky since they may define something like
569
# __iter__ defined but it isn't callable as list().
570
return unicode(value)
507
def to_primitive(value):
508
if type(value) is type([]) or type(value) is type((None,)):
511
o.append(to_primitive(v))
513
elif type(value) is type({}):
515
for k, v in value.iteritems():
516
o[k] = to_primitive(v)
518
elif isinstance(value, datetime.datetime):
520
elif hasattr(value, 'iteritems'):
521
return to_primitive(dict(value.iteritems()))
522
elif hasattr(value, '__iter__'):
523
return to_primitive(list(value))
573
528
def dumps(value):