Skip to content

Commit

Permalink
Support for datetime.date
Browse files Browse the repository at this point in the history
  • Loading branch information
rhettg committed Dec 12, 2013
1 parent 3b1d06b commit c8261a7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
blueox (0.6.0)

* Introduce new utility, oxingest
* Add support for datetime.date serialization

-- Rhett Garber <[email protected]> Mon, 2 Dec 2013 18:04:00 -0800

Expand Down
2 changes: 2 additions & 0 deletions blueox/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def msgpack_encode_default(obj):
return str(obj)
if isinstance(obj, datetime.datetime):
return time.mktime(obj.utctimetuple())
if isinstance(obj, datetime.date):
return obj.strftime("%Y-%m-%d")

raise TypeError("Unknown type: %r" % (obj,))

Expand Down
19 changes: 15 additions & 4 deletions tests/network_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import random
import struct
import decimal
import datetime

from testify import *
import zmq
Expand Down Expand Up @@ -67,19 +68,29 @@ class SerializeContextTestCase(TestCase):
def build_context(self):
self.context = context.Context('test', 1)

def test_decimal(self):
def test_types(self):
with self.context:
self.context.set('value', decimal.Decimal("6.66"))
self.context.set('decimal_value', decimal.Decimal("6.66"))
self.context.set('date_value', datetime.date(2013, 12, 10))
self.context.set('datetime_value', datetime.datetime(2013, 12, 10, 12, 12, 12))

meta_data, context_data = network._serialize_context(self.context)
data = msgpack.unpackb(context_data)
assert_equal(data['body']['value'], "6.66")
assert_equal(data['body']['decimal_value'], "6.66")
assert_equal(data['body']['date_value'], "2013-12-10")
assert_equal(
datetime.datetime.fromtimestamp(float(data['body']['datetime_value'])),
datetime.datetime(2013, 12, 10, 12, 12, 12))

def test_decimal(self):
def test_exception(self):
with self.context:
self.context.set('value', Exception('hello'))

meta_data, context_data = network._serialize_context(self.context)
data = msgpack.unpackb(context_data)

# The serialization should fail, but that just means we don't have any
# data.
assert_equal(data['body'], None)


Expand Down

0 comments on commit c8261a7

Please sign in to comment.