Commits

Max Noel committed 34235d3

* _python_to_dynamodb no longer needs an explicit schema_type argument (it's inferred from the value's type).

Comments (0)

Files changed (2)

dynamodb_mapper/model.py

     return schema_type()
 
 
-def _python_to_dynamodb(schema_type, value):
+def _python_to_dynamodb(value):
     """Convert a Python object to a representation suitable to direct storage
     in DynamoDB, according to a type from a DynamoDBModel schema.
 
     If value should be represented as a missing value in DynamoDB
     (empty string or set), None is returned.
 
-    ``_dynamodb_to_python(t, _python_to_dynamodb(t, v)) == v`` for any v.
-
-    :param schema_type: A type supported by the mapper (TODO Clearly list those).
+    ``_dynamodb_to_python(t, _python_to_dynamodb(v)) == v`` for any v.
 
     :param value: The Python object to convert.
 
     :return: ``value``, serialized to DynamoDB, or ``None`` if ``value`` must
         be represented as a missing attribute.
     """
-    if schema_type in _JSON_TYPES:
+    if isinstance(value, tuple(_JSON_TYPES)):
         # json serialization hooks for json_* data types.
         return json.dumps(value, sort_keys=True)
 
-    if schema_type == datetime.datetime:
+    if isinstance(value, datetime.datetime):
         # datetime instances are stored as UTC in the DB itself.
         # (that way, they become sortable)
         # datetime objects without tzinfo are not supported.
     place), a default value is returned (empty string or set, or 0, depending
     on what the type is).
 
-    ``_dynamodb_to_python(t, _python_to_dynamodb(t, v)) == v`` for any v.
+    ``_dynamodb_to_python(t, _python_to_dynamodb(v)) == v`` for any v.
 
     :param schema_type: A type supported by the mapper (TODO Clearly list those).
 
         """
         table = ConnectionBorg().get_table(cls.__table__)
         # Convert the keys to DynamoDB values.
-        h_type = cls.__schema__[cls.__hash_key__]
-        h_value = _python_to_dynamodb(h_type, hash_key_value)
+        h_value = _python_to_dynamodb(hash_key_value)
         if cls.__range_key__:
-            r_type = cls.__schema__[cls.__range_key__]
-            r_value = _python_to_dynamodb(r_type, range_key_value)
+            r_value = _python_to_dynamodb(range_key_value)
         else:
             r_value = None
 
         borg = ConnectionBorg()
         table = borg.get_table(cls.__table__)
         # Convert all the keys to DynamoDB values.
-        h_type = cls.__schema__[cls.__hash_key__]
         if cls.__range_key__:
-            r_type = cls.__schema__[cls.__range_key__]
             dynamo_keys = [
                 (
-                    _python_to_dynamodb(h_type, h),
-                    _python_to_dynamodb(r_type, r)
+                    _python_to_dynamodb(h),
+                    _python_to_dynamodb(r)
                 ) for (h, r) in keys
             ]
         else:
-            dynamo_keys = [_python_to_dynamodb(h_type, h) for h in keys]
+            dynamo_keys = map(_python_to_dynamodb, keys)
 
         batch_list = borg.new_batch_list()
         batch_list.add_batch(table, dynamo_keys)
         :rtype: generator
         """
         table = ConnectionBorg().get_table(cls.__table__)
-        h_type = cls.__schema__[cls.__hash_key__]
-        h_value = _python_to_dynamodb(h_type, hash_key_value)
+        h_value = _python_to_dynamodb(hash_key_value)
         return (
             cls.from_dict(d)
             for d in table.query(
         schema, suitable for direct storage in DynamoDB.
         """
         out = {}
-        for (name, type_) in type(self).__schema__.iteritems():
-            value = _python_to_dynamodb(type_, getattr(self, name))
+        for name in type(self).__schema__:
+            value = _python_to_dynamodb(getattr(self, name))
             if value is not None:
                 # DynamoDB can't store empty sets/strings, so we must
                 # represent them as missing attributes
                 expected_db_values = {}
                 schema = type(self).__schema__
                 for (name, value) in expected_values.iteritems():
-                    type_ = schema[name]
-                    db_value = _python_to_dynamodb(type_, value)
+                    db_value = _python_to_dynamodb(value)
                     if db_value is None:
                         # Empty strings/sets must be represented as missing values
                         expected_db_values[name] = False

dynamodb_mapper/tests/test_model.py

 
 class TestTypeConversions(unittest.TestCase):
     def test_python_to_dynamodb_number(self):
-        self.assertEquals(0, _python_to_dynamodb(int, 0))
-        self.assertEquals(0.0, _python_to_dynamodb(float, 0.0))
-        self.assertEquals(10, _python_to_dynamodb(int, 10))
-        self.assertEquals(10.0, _python_to_dynamodb(float, 10.0))
+        self.assertEquals(0, _python_to_dynamodb(0))
+        self.assertEquals(0.0, _python_to_dynamodb(0.0))
+        self.assertEquals(10, _python_to_dynamodb(10))
+        self.assertEquals(10.0, _python_to_dynamodb(10.0))
 
 
     def test_dynamodb_to_python_number(self):
         self.assertEquals(10.0, _dynamodb_to_python(float, 10.0))
 
     def test_python_to_dynamodb_unicode(self):
-        self.assertEquals(u"hello", _python_to_dynamodb(unicode, u"hello"))
+        self.assertEquals(u"hello", _python_to_dynamodb(u"hello"))
         # Empty strings become missing attributes
-        self.assertIsNone(_python_to_dynamodb(unicode, u""))
+        self.assertIsNone(_python_to_dynamodb(u""))
 
     def test_dynamodb_to_python_unicode(self):
         self.assertEquals(u"hello", _dynamodb_to_python(unicode, u"hello"))
         self.assertEquals(u"", _dynamodb_to_python(unicode, None))
 
     def test_python_to_dynamodb_set(self):
-        self.assertEquals(set([1, 2, 3]), _python_to_dynamodb(set, set([1, 2, 3])))
+        self.assertEquals(set([1, 2, 3]), _python_to_dynamodb(set([1, 2, 3])))
         self.assertEquals(
             set(["foo", "bar", "baz"]),
-            _python_to_dynamodb(set, set(["foo", "bar", "baz"])))
+            _python_to_dynamodb(set(["foo", "bar", "baz"])))
         # Empty sets become missing attributes
-        self.assertIsNone(_python_to_dynamodb(set, set()))
+        self.assertIsNone(_python_to_dynamodb(set()))
 
     def test_dynamodb_to_python_set(self):
         self.assertEquals(set([1, 2, 3]), _dynamodb_to_python(set, set([1, 2, 3])))
 
         self.assertEquals(
             json.dumps(attacks, sort_keys=True),
-            _python_to_dynamodb(list, attacks))
-        self.assertEquals("[]", _python_to_dynamodb(list, []))
+            _python_to_dynamodb(attacks))
+        self.assertEquals("[]", _python_to_dynamodb([]))
 
     def test_dynamodb_to_python_list(self):
         attacks = [
 
         self.assertEquals(
             json.dumps(monsters, sort_keys=True),
-            _python_to_dynamodb(dict, monsters))
-        self.assertEquals("{}", _python_to_dynamodb(dict, {}))
+            _python_to_dynamodb(monsters))
+        self.assertEquals("{}", _python_to_dynamodb({}))
 
     def test_dynamodb_to_python_dict(self):
         monsters = {
     def test_python_to_dynamodb_datetime(self):
         self.assertEquals(
             "2012-05-31T12:00:00.000000+0000",
-            _python_to_dynamodb(datetime.datetime, datetime.datetime(2012, 05, 31, 12, 0, 0, tzinfo=utc_tz)))
+            _python_to_dynamodb(datetime.datetime(2012, 05, 31, 12, 0, 0, tzinfo=utc_tz)))
 
     def test_python_to_dynamodb_datetime_notz(self):
         self.assertRaises(
             ValueError,
-            _python_to_dynamodb, datetime.datetime, datetime.datetime(2012, 05, 31, 12, 0, 0))
+            _python_to_dynamodb, datetime.datetime(2012, 05, 31, 12, 0, 0))
 
 
 class TestDynamoDBModel(unittest.TestCase):