Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added '_to_python' method for converting data back to its native Pyth…

…on type. Backward compatible (requires manually calling).

git-svn-id: https://pysolr.googlecode.com/svn/trunk@18 13ae9d4a-4d43-0410-997b-81b7443f7ec1
  • Loading branch information...
commit 5902599c2d656f08c3bc1ccc117cb0b7a705a806 1 parent cfd608f
@toastdriven toastdriven authored
Showing with 43 additions and 3 deletions.
  1. +42 −2 pysolr.py
  2. +1 −1  setup.py
View
44 pysolr.py
@@ -104,7 +104,7 @@
from urllib import urlencode
from urlparse import urlsplit
from datetime import datetime, date
-from time import strptime
+import re
try:
# for python 2.5
from xml.etree import cElementTree as ET
@@ -128,14 +128,20 @@
except ImportError:
# For Python >= 2.6
import json
+try:
+ set
+except NameError:
+ from sets import Set as set
__author__ = 'Joseph Kocherhans, Jacob Kaplan-Moss, Daniel Lindsley'
__all__ = ['Solr']
-__version__ = (2, 0, 0)
+__version__ = (2, 0, 1)
def get_version():
return "%s.%s.%s" % __version__
+DATETIME_REGEX = re.compile('^(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})T(?P<hour>\d{2}):(?P<minute>\d{2}):(?P<second>\d{2}).\d+Z$')
+
class SolrError(Exception):
pass
@@ -220,6 +226,40 @@ def _from_python(self, value):
else:
value = unicode(value)
return value
+
+ def _to_python(self, value):
+ """
+ Converts values from Solr to native Python values.
+ """
+ if value == 'true':
+ return True
+ elif value == 'false':
+ return False
+
+ possible_datetime = DATETIME_REGEX.search(value)
+
+ if possible_datetime:
+ date_values = possible_datetime.groupdict()
+
+ for dk, dv in date_values.items():
+ date_values[dk] = int(dv)
+
+ return datetime(date_values['year'], date_values['month'], date_values['day'], date_values['hour'], date_values['minute'], date_values['second'])
+
+ try:
+ # This is slightly gross but it's hard to tell otherwise what the
+ # string's original type might have been. Be careful who you trust.
+ converted_value = eval(value)
+
+ # Try to handle most built-in types.
+ if isinstance(converted_value, (list, tuple, set, dict, int, float, long, complex)):
+ return converted_value
+ except:
+ # If it fails (SyntaxError or its ilk) or we don't trust it,
+ # continue on.
+ pass
+
+ return value
# API Methods ############################################################
View
2  setup.py
@@ -2,7 +2,7 @@
setup(
name = "pysolr",
- version = "2.0.0",
+ version = "2.0.1",
description = "Lightweight python wrapper for Apache Solr.",
author = 'Joseph Kocherhans',
author_email = 'jkocherhans@gmail.com',
Please sign in to comment.
Something went wrong with that request. Please try again.