Skip to content

Commit

Permalink
removed inused imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Paro committed Feb 2, 2012
1 parent 701319b commit 607dbdf
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 40 deletions.
10 changes: 5 additions & 5 deletions pyes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def version_with_meta():
except ImportError:
pass

try:
#useful for additional features for django users
from djangoutils import *
except ImportError:
pass
#try:
# #useful for additional features for django users
# from djangoutils import *
#except ImportError:
# pass
3 changes: 2 additions & 1 deletion pyes/connection_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def execute(self, request):
"""
Execute a request and return a response
"""
response = requests.request(method=Method._VALUES_TO_NAMES[request.method],
s = requests.session()
response = s.request(method=Method._VALUES_TO_NAMES[request.method],
url="http://%s:%s%s" % (self.host, self.port, request.uri), params=request.parameters,
data=request.body, headers=request.headers)
return RestResponse(status=response.status_code, body=response.content, headers=response.headers)
Expand Down
55 changes: 34 additions & 21 deletions pyes/es.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
__all__ = ['ES', 'file_to_attachment', 'decode_json']

try:
# For Python < 2.6 or people using a newer version of simplejson
import simplejson
json = simplejson
except ImportError:
# For Python >= 2.6
import json
except ImportError:
# For Python < 2.6 or people using a newer version of simplejson
import simplejson as json

import logging
import random
Expand All @@ -21,7 +20,6 @@
from urlparse import urlunsplit
import base64
import time
from StringIO import StringIO
from decimal import Decimal
from urllib import quote
import threading
Expand Down Expand Up @@ -62,30 +60,45 @@ def __deepcopy__(self, memo):

class ElasticSearchModel(DotDict):
def __init__(self, *args, **kwargs):
self.meta = DotDict()
self._meta = DotDict()
self.__initialised = True
if len(args) == 2 and isinstance(args[0], ES):
item = args[1]
self.update(item.pop("_source", DotDict()))
self.update(item.pop("fields", {}))
self.meta = DotDict([(k.lstrip("_"), v) for k, v in item.items()])
self.meta.parent = self.pop("_parent", None)
self.meta.connection = args[0]
self._meta = DotDict([(k.lstrip("_"), v) for k, v in item.items()])
self._meta.parent = self._meta.pop("_parent", None)
self._meta.connection = args[0]
else:
self.update(dict(*args, **kwargs))

def __setattr__(self, key, value):
if not self.__dict__.has_key('_ElasticSearchModel__initialised'): # this test allows attributes to be set in the __init__ method
return dict.__setattr__(self, key, value)
elif self.__dict__.has_key(key): # any normal attributes are handled normally
dict.__setattr__(self, key, value)
else:
self.__setitem__(key, value)

def __repr__(self):
return repr(self)

def get_meta(self):
return self._meta

def delete(self, bulk=False):
"""
Delete the object
"""
meta = self.meta
meta = self._meta
conn = meta['connection']
conn.delete(meta.index, meta.type, meta.id, bulk=bulk)

def save(self, bulk=False, id=None, parent=None, force=False):
"""
Save the object and returns id
"""
meta = self.meta
meta = self._meta
conn = meta['connection']
id = id or meta.get("id", None)
parent = parent or meta.get('parent', None)
Expand All @@ -95,14 +108,14 @@ def save(self, bulk=False, id=None, parent=None, force=False):
res = conn.index(dict([(k, v) for k, v in self.items() if k != "meta"]),
meta.index, meta.type, id, parent=parent, bulk=bulk, version=version, force_insert=force)
if not bulk:
self.meta.id = res._id
self.meta.version = res._version
self._meta.id = res._id
self._meta.version = res._version
return res._id
return id

def get_id(self):
""" Force the object saveing to get an id"""
_id = self.meta.get("id", None)
_id = self._meta.get("id", None)
if _id is None:
_id = self.save()
return _id
Expand All @@ -113,17 +126,17 @@ def get_bulk(self, create=False):
op_type = "index"
if create:
op_type = "create"
meta = self.meta
meta = self._meta
cmd = { op_type : { "_index" : meta.index, "_type" : meta.type}}
if meta.parent:
cmd[op_type]['_parent'] = meta.parent
if meta.version:
cmd[op_type]['_version'] = meta.version
if meta.id:
cmd[op_type]['_id'] = meta.id
result.append(json.dumps(cmd, cls=self.meta.connection.encoder))
result.append(json.dumps(cmd, cls=self._meta.connection.encoder))
result.append("\n")
result.append(json.dumps(self.store, cls=self.meta.connection.encoder))
result.append(json.dumps(self.store, cls=self._meta.connection.encoder))
result.append("\n")
return ''.join(result)

Expand Down Expand Up @@ -1150,11 +1163,11 @@ def factory_object(self, index, doc_type, data=None, id=None, vertex=False):
"""
data = data or {}
obj = ElasticSearchModel()
obj.meta.index = index
obj.meta.type = doc_type
obj.meta.connection = self
obj._meta.index = index
obj._meta.type = doc_type
obj._meta.connection = self
if id:
obj.meta.id = id
obj._meta.id = id
if data:
obj.update(data)
if vertex:
Expand Down
8 changes: 4 additions & 4 deletions pyes/tests/test_esmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ def test_ElasticSearchModel_init(self):
obj.name = "aaa"
self.assertEqual(obj.name, "aaa")
self.assertEqual(obj.val, 1)
self.assertEqual(obj.meta.id, None)
obj.meta.id = "dasdas"
self.assertEqual(obj.meta.id, "dasdas")
self.assertEqual(obj._meta.id, None)
obj._meta.id = "dasdas"
self.assertEqual(obj._meta.id, "dasdas")
self.assertEqual(sorted(obj.keys()), ["meta", "name", "val"])
obj.save()
obj.name = "test2"
obj.save()

reloaded = self.conn.get(self.index_name, self.document_type, obj.meta.id)
reloaded = self.conn.get(self.index_name, self.document_type, obj._meta.id)
self.assertEqual(reloaded.name, "test2")

def test_DotDict(self):
Expand Down
12 changes: 6 additions & 6 deletions pyes/tests/test_highlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ def test_QueryHighlight(self):
q.add_highlight("name")
resultset = self.conn.search(q, indices=self.index_name)

print resultset[0].meta.highlight
print resultset[0]._meta.highlight

self.assertEquals(resultset.total, 2)
self.assertNotEqual(resultset[0].meta.highlight, None)
self.assertNotEqual(resultset[0]._meta.highlight, None)

self.assertEquals(resultset[0].meta.highlight[u"parsedtext"][0],
self.assertEquals(resultset[0]._meta.highlight[u"parsedtext"][0],
u'<em>Joe</em> Testere nice guy ')

def test_QueryHighlightWithHighLighter(self):
Expand All @@ -59,12 +59,12 @@ def test_QueryHighlightWithHighLighter(self):
q.add_highlight("name")
resultset = self.conn.search(q, indices=self.index_name)

print resultset[0].meta.highlight
print resultset[0]._meta.highlight

self.assertEquals(resultset.total, 2)
self.assertNotEqual(resultset[0].meta.highlight, None)
self.assertNotEqual(resultset[0]._meta.highlight, None)

self.assertEquals(resultset[0].meta.highlight[u"parsedtext"][0],
self.assertEquals(resultset[0]._meta.highlight[u"parsedtext"][0],
u'<b>Joe</b> Testere nice guy ')

if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion pyes/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def testVersion(self):
*({"name":"Joe Test2"}, self.index_name, self.document_type, 1), **{"version":2})
item = self.conn.get(self.index_name, self.document_type, 1)
self.assertEqual(item["meta"]["version"], 3)
self.assertEqual(item.meta.version, 3)
self.assertEqual(item._meta.version, 3)

if __name__ == "__main__":
unittest.main()
4 changes: 2 additions & 2 deletions pyes/tests/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ def test_CustomScoreQueryMvel(self):
))
resultset = self.conn.search(query=q, indices=self.index_name, doc_types=[self.document_type])
self.assertEquals(resultset.total, 3)
self.assertEquals(resultset[0].meta.score, 8.0)
self.assertEquals(resultset[1].meta.score, 7.0)
self.assertEquals(resultset[0]._meta.score, 8.0)
self.assertEquals(resultset[1]._meta.score, 7.0)
self.assertEquals(resultset.max_score, 8.0)

def test_CustomScoreQueryJS(self):
Expand Down

0 comments on commit 607dbdf

Please sign in to comment.