The attached patch allows one to use set/frozenset sets (available since 
python 2.4) or Set/ImmutableSet sets (available since python 2.3) as 
sequences passed to the IN operator (currently only lists and tuples can 
be used). In fact it'll accept a set of values everywhere a list/tuple of 
values is accepted.

The set classes will be ignored and the converters for them will not be 
registered if they are not present when sqlobject is imported, so it'll 
work with any version of python, regardless if it has the set data types 
or not.

The advantage of the patch is that it'll avoid the need to convert to a 
list before passing values to the IN operator if one already has the 
sequence of values in a set/frozenset/Set/ImmutableSet, since sets also 
implement the sequence protocol and can be iterated over exactly like 
list/tuples can.

The patch also registers the SequenceConverter for dictionaries as well. 
In python iterating over a dict, iterates over the dictionary keys. If we 
do that then passing a dictionary as a sequence for the IN operator for 
example, will use the dictionary keys, without the need to make a list of 
them with dict.keys()
It seems only natural to handle dictionaries in sqlobject the same way as 
they are handled in python, leading to a more intuitive behavior.

example usage:

  IN(field_list, tuple/list/dict/set/frozenset/Set/ImmutableSet)

-- 
Dan
Index: converters.py
===================================================================
--- converters.py	(revision 2240)
+++ converters.py	(working copy)
@@ -180,6 +180,21 @@
 
 registerConverter(type(()), SequenceConverter)
 registerConverter(type([]), SequenceConverter)
+registerConverter(type({}), SequenceConverter)
+try:
+    set, frozenset
+except NameError:
+    pass
+else:
+    registerConverter(set, SequenceConverter)
+    registerConverter(frozenset, SequenceConverter)
+try:
+    from sets import Set, ImmutableSet
+except ImportError:
+    pass
+else:
+    registerConverter(Set, SequenceConverter)
+    registerConverter(ImmutableSet, SequenceConverter)
 
 if hasattr(time, 'struct_time'):
     def StructTimeConverter(value, db):
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to