diff --git a/cereal/__init__.py b/cereal/__init__.py index a9b5398..0c2a9d8 100644 --- a/cereal/__init__.py +++ b/cereal/__init__.py @@ -370,6 +370,26 @@ class Integer(object): Int = Integer +class Float(object): + """ A type representing a float. + + The substructures of the :class:`cereal.Structure` that wraps this + type are ignored. + """ + def deserialize(self, struct, value): + try: + return float(value) + except Exception: + raise Invalid(struct, '%r is not a number' % value) + + def serialize(self, struct, value): + try: + return str(float(value)) + except Exception: + raise Invalid(struct, '%r is not a number' % value) + +Int = Integer + class Boolean(object): """ A type representing a boolean object. diff --git a/cereal/tests.py b/cereal/tests.py index 26e61a5..a5b71a5 100644 --- a/cereal/tests.py +++ b/cereal/tests.py @@ -565,6 +565,39 @@ class TestInteger(unittest.TestCase): result = typ.serialize(struct, val) self.assertEqual(result, '1') +class TestFloat(unittest.TestCase): + def _makeOne(self): + from cereal import Float + return Float() + + def test_deserialize_fails(self): + val = 'P' + struct = DummyStructure(None) + typ = self._makeOne() + e = invalid_exc(typ.deserialize, struct, val) + self.failUnless(e.msg) + + def test_deserialize_ok(self): + val = '1.0' + struct = DummyStructure(None) + typ = self._makeOne() + result = typ.deserialize(struct, val) + self.assertEqual(result, 1.0) + + def test_serialize_fails(self): + val = 'P' + struct = DummyStructure(None) + typ = self._makeOne() + e = invalid_exc(typ.serialize, struct, val) + self.failUnless(e.msg) + + def test_serialize_ok(self): + val = 1.0 + struct = DummyStructure(None) + typ = self._makeOne() + result = typ.serialize(struct, val) + self.assertEqual(result, '1.0') + class TestBoolean(unittest.TestCase): def _makeOne(self): from cereal import Boolean diff --git a/docs/api.rst b/docs/api.rst index 3835351..e42946a 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -37,6 +37,8 @@ Types .. autoclass:: Int + .. autoclass:: Float + .. autoclass:: Boolean .. autoclass:: Bool diff --git a/docs/index.rst b/docs/index.rst index 2ffd794..49c0b7c 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -29,7 +29,9 @@ objects, including: - An integer. -- A boolean value. +- A float. + +- A boolean. - An importable Python object (to a dotted Python object path). diff --git a/setup.py b/setup.py index df0b38e..b96bf59 100644 --- a/setup.py +++ b/setup.py @@ -25,16 +25,17 @@ requires = [] setup(name='cereal', version='0.0', - description='A schema-based serialization and deserialization library', + description=('A simple schema-based serialization and deserialization ' + 'library'), long_description=README + '\n\n' + CHANGES, classifiers=[ "Intended Audience :: Developers", "Programming Language :: Python", ], - keywords='serialize deserialize validate schema', + keywords='serialize deserialize validate schema validation', author="Agendaless Consulting", author_email="repoze-dev@lists.repoze.org", - url="http://www.repoze.org", + url="http://docs.repoze.org/cereal", license="BSD-derived (http://www.repoze.org/LICENSE.txt)", packages=find_packages(), include_package_data=True,