Skip to content

Commit

Permalink
Use cElementTree if it's available.
Browse files Browse the repository at this point in the history
git-svn-id: https://pysolr.googlecode.com/svn/trunk@13 13ae9d4a-4d43-0410-997b-81b7443f7ec1
  • Loading branch information
jkocherhans committed Mar 15, 2008
1 parent 56abdc6 commit e641a92
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions pysolr.py
Expand Up @@ -2,7 +2,8 @@
""" """
All we need to create a Solr connection is a url. 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. First, completely clear the index.
Expand Down Expand Up @@ -107,9 +108,13 @@
from time import strptime from time import strptime
try: try:
# for python 2.5 # for python 2.5
from xml.etree import ElementTree from xml.etree import cElementTree as ET
except ImportError: 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'] __all__ = ['Solr']


Expand Down Expand Up @@ -162,7 +167,7 @@ def _extract_error(self, response):
Extract the actual error message from a solr response. Unfortunately, Extract the actual error message from a solr response. Unfortunately,
this means scraping the html. this means scraping the html.
""" """
et = ElementTree.parse(response) et = ET.parse(response)
return et.findtext('body/pre') return et.findtext('body/pre')


# Converters ############################################################# # Converters #############################################################
Expand Down Expand Up @@ -244,7 +249,7 @@ def search(self, q, sort=None, start=None, rows=None):


# TODO: make result retrieval lazy and allow custom result objects # TODO: make result retrieval lazy and allow custom result objects
# also, this has become rather ugly and definitely needs some cleanup. # also, this has become rather ugly and definitely needs some cleanup.
et = ElementTree.parse(response) et = ET.parse(response)
result = et.find('result') result = et.find('result')
hits = int(result.get('numFound')) hits = int(result.get('numFound'))
docs = result.findall('doc') docs = result.findall('doc')
Expand All @@ -270,23 +275,23 @@ def add(self, docs, commit=True):
"""Adds or updates documents. For now, docs is a list of dictionaies """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. 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: for doc in docs:
d = ElementTree.Element('doc') d = ET.Element('doc')
for key, value in doc.items(): for key, value in doc.items():
# handle lists, tuples, and other iterabes # handle lists, tuples, and other iterabes
if hasattr(value, '__iter__'): if hasattr(value, '__iter__'):
for v in value: for v in value:
f = ElementTree.Element('field', name=key) f = ET.Element('field', name=key)
f.text = self._from_python(v) f.text = self._from_python(v)
d.append(f) d.append(f)
# handle strings and unicode # handle strings and unicode
else: else:
f = ElementTree.Element('field', name=key) f = ET.Element('field', name=key)
f.text = self._from_python(value) f.text = self._from_python(value)
d.append(f) d.append(f)
message.append(d) message.append(d)
m = ElementTree.tostring(message) m = ET.tostring(message)
response = self._update(m) response = self._update(m)
if response.status != 200: if response.status != 200:
raise SolrError(self._extract_error(response)) raise SolrError(self._extract_error(response))
Expand Down

0 comments on commit e641a92

Please sign in to comment.