Patches to support arrays of strings

51 views
Skip to first unread message

Wesley Emeneker

unread,
Dec 17, 2012, 10:51:47 AM12/17/12
to pyactive...@googlegroups.com
Dear pyactiveresource Devs,
  Thanks for the excellent tool. I have a couple of patches that allow us to work with our homegrown asset management system.
We encountered a problem when trying to get activeresource to encode a list of strings for storage in a MongoDB.

The to_dict() in activeresource.py and to_xml() in util.py could not handle the lists given to them.
It seems they expected either a list of dicts, or a list of ActiveResources.
I wasn't sure of right(TM) way to fix the problem, and a perusal of the way Ruby handles this indicates (to me) that lists of strings should be converted straightforwardly by simply returning each serialized string.

Below are the patches.
If these are acceptable, let me know how I can correctly submit them.

Thanks,
  Wesley


Index: src/util.py
===================================================================
--- src/util.py (revision 101)
+++ src/util.py (working copy)
@@ -291,10 +291,16 @@
     if isinstance(obj, list):
         root_element.set('type', 'array')
         for i in obj:
-            element = ET.fromstring(
+            if isinstance(obj, dict) or isinstance(obj,list):
+                element = ET.fromstring(
                     to_xml(i, root=singularize(root), header=False,
                            pretty=pretty, dasherize=dasherize))
-            root_element.append(element)
+                root_element.append(element)
+            else:
+                #If the object isn't a dict, a list, or empty, we should try to serialize it and add it to the element
+                #Note the SERIALIZERS list, maybe we should check that "i" is of a serializable type first?
+                element = ET.SubElement(root_element, 'item')
+                serialize(i, element)
     else:
         for key, value in obj.iteritems():
             key = dasherize and key.replace('_', '-') or key
Index: src/activeresource.py
===================================================================
--- src/activeresource.py (revision 101)
+++ src/activeresource.py (working copy)
@@ -717,7 +717,17 @@
                   if isinstance(i, dict):
                       new_value.append(i)
                   else:
-                      new_value.append(i.to_dict())
+                      try:
+                          new_value.append(i.to_dict())
+                      except AttributeError:
+                          #We couldn't convert the value "i" to a dictionary with to_dict
+                          #The AttributeError Exception means that "i" didn't have a method named to_dict.
+                          #That means that the given value is something other than a dict or ActiveResource
+                          #Maybe it is a string, integer, or list?
+                          #So, perhaps we just append it as-is. (I'm not certain what is the correct(TM) way
+                          #  to handle it)
+                          new_value.append(i)
+
                 values[key] = new_value
             elif isinstance(value, ActiveResource):
                 values[key] = value.to_dict()


Message has been deleted

Dylan Smith

unread,
Jan 7, 2013, 1:59:31 PM1/7/13
to pyactive...@googlegroups.com
I ran into the same problem as Wesley, however found that ActiveResource._update needed to also be fixed.

I have attached an alternate patch which includes tests.  Additionally, it simplifies util.to_xml and remove unnecessary serialization and deserialization.
0001-Support-array-attributes-of-non-resource-items.diff.txt
Reply all
Reply to author
Forward
0 new messages