Patch for data conversion to handle datetime.date fields and values in lists

13 views
Skip to first unread message

Etienne Posthumus

unread,
Jan 22, 2009, 10:44:06 AM1/22/09
to solrpy
Here is a svn diff to revision 18 to fix some issues with the data
conversions when adding data.
Two things are changed:

1) The basic data conversions are moved inside the list handing loop,
so that it also converts items inside the list
2) Add support for datetime.date objects which dont have times
attached.

Index: solr.py
===================================================================
--- solr.py (revision 18)
+++ solr.py (working copy)
@@ -671,13 +671,7 @@
def __add(self, lst, fields):
lst.append(u'<doc>')
for field, value in fields.items():
- # Do some basic data conversion
- if isinstance(value, datetime.datetime):
- value = utc_to_string(value)

- elif isinstance(value, bool):
- value = value and 'true' or 'false'
-
# Handle multi-valued fields if values
# is passed in as a list/tuple
if not isinstance(value, (list, tuple)):
@@ -686,6 +680,14 @@
values = value

for value in values:
+ # Do some basic data conversion
+ if isinstance(value, datetime.date):
+ value = datetime.datetime.combine(value,
datetime.time(tzinfo=UTC()))
+ if isinstance(value, datetime.datetime):
+ value = utc_to_string(value)
+ elif isinstance(value, bool):
+ value = value and 'true' or 'false'
+
lst.append('<field name=%s>%s</field>' % (
(quoteattr(field),
escape(unicode(value)))))

Leonardo Santagada

unread,
Jan 22, 2009, 2:08:21 PM1/22/09
to sol...@googlegroups.com
I would put an "else: raise ValueError" or something in case something
unexpected did show up.


ps: How is this project doing, why this code isn't in solr repository?

--
Leonardo Santagada
santagada at gmail.com

Ed Summers

unread,
Jan 31, 2009, 4:28:01 PM1/31/09
to sol...@googlegroups.com
Ok, I've applied the patch, added some tests for it, committed and
pushed a v0.3 to PyPi:

http://pypi.python.org/pypi/solrpy/0.3

Sorry for the delay, and thanks for the patch!

//Ed

Reply all
Reply to author
Forward
0 new messages