Permalink
Browse files

Use cElementTree if it's available.

git-svn-id: https://pysolr.googlecode.com/svn/trunk@13 13ae9d4a-4d43-0410-997b-81b7443f7ec1
  • Loading branch information...
1 parent 56abdc6 commit e641a9233f1eb75c9e06ce14dd17c2a6314365e3 @jkocherhans committed Mar 15, 2008
Showing with 15 additions and 10 deletions.
  1. +15 −10 pysolr.py
View
@@ -2,7 +2,8 @@
"""
All we need to create a Solr connection is a url.
->>> conn = Solr('http://127.0.0.1:8983/solr/')
+>>> #conn = Solr('http://127.0.0.1:8983/solr/')
+>>> conn = Solr('http://127.0.0.1:8080/solr/default/')
First, completely clear the index.
@@ -107,9 +108,13 @@
from time import strptime
try:
# for python 2.5
- from xml.etree import ElementTree
+ from xml.etree import cElementTree as ET
except ImportError:
- from elementtree import ElementTree
+ try:
+ # use cElementTree if available
+ import cElementTree as ET
+ except ImportError:
+ from elementtree import ElementTree as ET
__all__ = ['Solr']
@@ -162,7 +167,7 @@ def _extract_error(self, response):
Extract the actual error message from a solr response. Unfortunately,
this means scraping the html.
"""
- et = ElementTree.parse(response)
+ et = ET.parse(response)
return et.findtext('body/pre')
# Converters #############################################################
@@ -244,7 +249,7 @@ def search(self, q, sort=None, start=None, rows=None):
# TODO: make result retrieval lazy and allow custom result objects
# also, this has become rather ugly and definitely needs some cleanup.
- et = ElementTree.parse(response)
+ et = ET.parse(response)
result = et.find('result')
hits = int(result.get('numFound'))
docs = result.findall('doc')
@@ -270,23 +275,23 @@ def add(self, docs, commit=True):
"""Adds or updates documents. For now, docs is a list of dictionaies
where each key is the field name and each value is the value to index.
"""
- message = ElementTree.Element('add')
+ message = ET.Element('add')
for doc in docs:
- d = ElementTree.Element('doc')
+ d = ET.Element('doc')
for key, value in doc.items():
# handle lists, tuples, and other iterabes
if hasattr(value, '__iter__'):
for v in value:
- f = ElementTree.Element('field', name=key)
+ f = ET.Element('field', name=key)
f.text = self._from_python(v)
d.append(f)
# handle strings and unicode
else:
- f = ElementTree.Element('field', name=key)
+ f = ET.Element('field', name=key)
f.text = self._from_python(value)
d.append(f)
message.append(d)
- m = ElementTree.tostring(message)
+ m = ET.tostring(message)
response = self._update(m)
if response.status != 200:
raise SolrError(self._extract_error(response))

0 comments on commit e641a92

Please sign in to comment.