Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unique testing and serialization support for datetimes
- Loading branch information
1 parent
7b2b40b
commit 6487f0e
Showing
4 changed files
with
118 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import datetime | ||
import json | ||
|
||
''' | ||
Adds the ability to send date and datetime objects to the API | ||
The date and datetime formats from the API are automatically supported and parsed | ||
''' | ||
|
||
def _datetime_encoder(obj): | ||
if isinstance(obj, (datetime.datetime, datetime.date)): | ||
return obj.isoformat() | ||
|
||
|
||
def _datetime_decoder(dict_): | ||
for key, value in dict_.iteritems(): | ||
# The built-in `json` library will `unicode` strings, except for empty | ||
# strings which are of type `str`. `jsondate` patches this for | ||
# consistency so that `unicode` is always returned. | ||
if value == '': | ||
dict_[key] = u'' | ||
continue | ||
|
||
if value is not None and isinstance(value, basestring): | ||
try: | ||
# The api always returns times like this 2014-07-25T09:12:24.735 | ||
datetime_obj = datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S.%f" ) | ||
dict_[key] = datetime_obj | ||
except (ValueError, TypeError): | ||
try: | ||
# The api always returns times like this 2014-07-25T09:12:24.735 | ||
datetime_obj = datetime.datetime.strptime(value, "%Y-%m-%d" ) | ||
dict_[key] = datetime_obj.date() | ||
except (ValueError, TypeError): | ||
continue | ||
return dict_ | ||
|
||
|
||
def dumps(*args, **kwargs): | ||
kwargs['default'] = _datetime_encoder | ||
return json.dumps(*args, **kwargs) | ||
|
||
|
||
def dump(*args, **kwargs): | ||
kwargs['default'] = _datetime_encoder | ||
return json.dump(*args, **kwargs) | ||
|
||
|
||
def loads(*args, **kwargs): | ||
kwargs['object_hook'] = _datetime_decoder | ||
return json.loads(*args, **kwargs) | ||
|
||
def load(*args, **kwargs): | ||
kwargs['object_hook'] = _datetime_decoder | ||
return json.load(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters