Skip to content

Commit fb58ab8

Browse files
refactor(elasticpy/): formatted code as per PEP8 guidelines
1 parent dd37984 commit fb58ab8

File tree

7 files changed

+216
-156
lines changed

7 files changed

+216
-156
lines changed

elasticpy/connection.py

+19-17
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
except ImportError:
1818
pass
1919

20+
2021
class ElasticConnection(object):
2122
if _use_gevent:
2223
session = None
2324
session_lock = RLock()
2425

25-
2626
def __init__(self, timeout=None, **params):
2727
self.status_code = 0
28-
self.timeout=timeout
28+
self.timeout = timeout
2929
self.encoding = None
30-
self.headers = {'Content-Type' : 'Application/json; charset=utf-8'}
30+
self.headers = {'Content-Type': 'Application/json; charset=utf-8'}
3131
if params.has_key('encoding'):
3232
self.encoding = 'utf8'
3333
del params['encoding']
@@ -37,40 +37,42 @@ def __init__(self, timeout=None, **params):
3737
ElasticConnection.session = requests.Session(**params)
3838
ElasticConnection.session_lock.release()
3939
else:
40-
self.session = requests.Session(timeout=timeout, **params)
40+
self.session = requests.Session(timeout=timeout, **params)
41+
4142
def get(self, url):
4243
try:
43-
response = self.session.get(url,headers=self.headers,timeout=self.timeout)
44+
response = self.session.get(url, headers=self.headers, timeout=self.timeout)
4445
except requests.ConnectionError as e:
4546
self.status_code = 0
46-
return {'error':e.message}
47+
return {'error': e.message}
4748
self.status_code = response.status_code
48-
return json.loads(response.content,encoding=self.encoding)
49+
return json.loads(response.content, encoding=self.encoding)
50+
4951
def post(self, url, data):
5052
body = json.dumps(data)
5153
try:
52-
response = self.session.post(url,data=body,headers=self.headers,timeout=self.timeout)
54+
response = self.session.post(url, data=body, headers=self.headers, timeout=self.timeout)
5355
except requests.ConnectionError as e:
5456
self.status_code = 0
55-
return {'error' : e.message}
57+
return {'error': e.message}
5658
self.status_code = response.status_code
57-
return json.loads(response.content,encoding=self.encoding)
59+
return json.loads(response.content, encoding=self.encoding)
5860

5961
def put(self, url, data):
6062
body = json.dumps(data)
6163
try:
62-
response = self.session.post(url,data=body,headers=self.headers,timeout=self.timeout)
64+
response = self.session.post(url, data=body, headers=self.headers, timeout=self.timeout)
6365
except requests.ConnectionError as e:
6466
self.status_code = 0
65-
return {'error' : e.message}
67+
return {'error': e.message}
6668
self.status_code = response.status_code
67-
return json.loads(response.content,encoding=self.encoding)
69+
return json.loads(response.content, encoding=self.encoding)
6870

69-
def delete(self,url):
71+
def delete(self, url):
7072
try:
71-
response = self.session.delete(url,headers=self.headers,timeout=self.timeout)
73+
response = self.session.delete(url, headers=self.headers, timeout=self.timeout)
7274
except requests.ConnectionError as e:
7375
self.status_code = 0
74-
return {'error' : e.message}
76+
return {'error': e.message}
7577
self.status_code = response.status_code
76-
return json.loads(response.content,encoding=self.encoding)
78+
return json.loads(response.content, encoding=self.encoding)

elasticpy/facet.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
'''
88

99

10-
1110
class ElasticFacet(dict):
11+
1212
'''
1313
Facets for search
1414
http://www.elasticsearch.org/guide/reference/api/search/facets/
@@ -31,7 +31,7 @@ def terms(self, facet_name, field, size=10, order=None, all_terms=False, exclude
3131
Regex Patterns: The terms API allows to define regex expression that will control which terms will be included in the faceted list.
3232
'''
3333

34-
self[facet_name] = dict(terms=dict(field=field,size=size))
34+
self[facet_name] = dict(terms=dict(field=field, size=size))
3535
if order:
3636
self[facet_name][terms]['order'] = order
3737
if all_terms:
@@ -45,7 +45,7 @@ def terms(self, facet_name, field, size=10, order=None, all_terms=False, exclude
4545

4646
return self
4747

48-
def range(self,facet_name, field, ranges=[]):
48+
def range(self, facet_name, field, ranges=[]):
4949
'''
5050
Range facet allow to specify a set of ranges and get both the number of docs (count) that fall within each range, and aggregated data either based on the field, or using another field.
5151
http://www.elasticsearch.org/guide/reference/api/search/facets/range-facet.html
@@ -66,7 +66,7 @@ def range(self,facet_name, field, ranges=[]):
6666
}
6767
'''
6868

69-
self[facet_name] = {'range' : { 'field' : field,'ranges' : [] }}
69+
self[facet_name] = {'range': {'field': field, 'ranges': []}}
7070
for s in ranges:
7171
if not isinstance(s, slice):
7272
continue
@@ -78,4 +78,3 @@ def range(self,facet_name, field, ranges=[]):
7878
self[facet_name]['range']['ranges'].append(entry)
7979

8080
return self
81-

elasticpy/filter.py

+46-40
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@
77
'''
88

99

10-
1110
class ElasticFilter(dict):
11+
1212
'''
1313
Wrapper for ElasticSearch filters
1414
'''
15-
15+
1616
@classmethod
1717
def and_filter(cls, query):
1818
'''
1919
http://www.elasticsearch.org/guide/reference/query-dsl/and-filter.html
2020
A filter that matches documents using AND boolean operator on other queries. This filter is more performant then bool filter. Can be placed within queries that accept a filter.
2121
'''
22-
return cls({'and':query})
22+
return cls({'and': query})
2323

2424
@classmethod
25-
def bool_filter(cls,query):
25+
def bool_filter(cls, query):
2626
'''
2727
http://www.elasticsearch.org/guide/reference/query-dsl/bool-filter.html
2828
A filter that matches documents matching boolean combinations of other queries. Similar in concept to Boolean query, except that the clauses are other filters. Can be placed within queries that accept a filter.
2929
'''
3030

31-
return cls({'bool':query})
32-
31+
return cls({'bool': query})
32+
3333
@classmethod
3434
def exists(cls, field):
3535
'''
@@ -39,17 +39,18 @@ def exists(cls, field):
3939
> filter.query()
4040
{'exists' : {'field' : 'user' } }
4141
'''
42-
return cls(exists={'field':field})
43-
42+
return cls(exists={'field': field})
43+
4444
@classmethod
4545
def ids(cls, values, itype=None):
4646
'''
4747
http://www.elasticsearch.org/guide/reference/query-dsl/ids-filter.html
4848
Filters documents that only have the provided ids. Note, this filter does not require the _id field to be indexed since it works using the _uid field.
4949
5050
'''
51-
instance = cls(ids={'values':values})
52-
if itype is not None: instance['ids']['type'] = itype
51+
instance = cls(ids={'values': values})
52+
if itype is not None:
53+
instance['ids']['type'] = itype
5354

5455
return instance
5556

@@ -59,16 +60,16 @@ def limit(cls, value):
5960
http://www.elasticsearch.org/guide/reference/query-dsl/limit-filter.html
6061
A limit filter limits the number of documents (per shard) to execute on.
6162
'''
62-
return cls(limit={'value':value})
63+
return cls(limit={'value': value})
6364

6465
@classmethod
65-
def type(cls,value):
66+
def type(cls, value):
6667
'''
6768
http://www.elasticsearch.org/guide/reference/query-dsl/type-filter.html
6869
Filters documents matching the provided document / mapping type. Note, this filter can work even when the _type field is not indexed (using the _uid field).
6970
'''
70-
return cls(type={'value':value})
71-
71+
return cls(type={'value': value})
72+
7273
@classmethod
7374
def geo_bounding_box(cls, field, top_left, bottom_right):
7475
'''
@@ -81,7 +82,7 @@ def geo_bounding_box(cls, field, top_left, bottom_right):
8182
> bounds = ElasticFilter().geo_bounding_box('pin.location', "drm3btev3e86", "drm3btev3e86")
8283
8384
'''
84-
return cls(geo_bounding_box={field:{'top_left':top_left, 'bottom_right':bottom_right}})
85+
return cls(geo_bounding_box={field: {'top_left': top_left, 'bottom_right': bottom_right}})
8586

8687
@classmethod
8788
def geo_distance(cls, field, center, distance, distance_type=None):
@@ -95,11 +96,11 @@ def geo_distance(cls, field, center, distance, distance_type=None):
9596
> bounds = ElasticFilter().geo_distance('pin.location', [40.73, -74.1], '300km')
9697
'''
9798

98-
instance = cls(geo_distance={'distance':distance, field:center})
99-
if distance_type is not None: instance['geo_distance']['distance_type'] = distance_type
99+
instance = cls(geo_distance={'distance': distance, field: center})
100+
if distance_type is not None:
101+
instance['geo_distance']['distance_type'] = distance_type
100102
return instance
101103

102-
103104
@classmethod
104105
def geo_distance_range(cls, field, center, from_distance, to_distance, distance_type=None):
105106
'''
@@ -108,8 +109,9 @@ def geo_distance_range(cls, field, center, from_distance, to_distance, distance_
108109
109110
110111
'''
111-
instance = cls(geo_distance_range={'from':from_distance, 'to':to_distance, field:center})
112-
if distance_type is not None: instance['geo_distance_range']['distance_type'] = distance_type
112+
instance = cls(geo_distance_range={'from': from_distance, 'to': to_distance, field: center})
113+
if distance_type is not None:
114+
instance['geo_distance_range']['distance_type'] = distance_type
113115
return instance
114116

115117
@classmethod
@@ -121,7 +123,7 @@ def geo_polygon(cls, field, points):
121123
122124
> filter = ElasticFilter().geo_polygon('pin.location', [[40, -70], [30, -80], [20, -90]])
123125
'''
124-
return cls(geo_polygon={field:{'points':points}})
126+
return cls(geo_polygon={field: {'points': points}})
125127

126128
@classmethod
127129
def has_child(cls, child_type, query):
@@ -134,8 +136,7 @@ def has_child(cls, child_type, query):
134136
135137
'''
136138

137-
return cls(has_child={'type':child_type, 'query':query})
138-
139+
return cls(has_child={'type': child_type, 'query': query})
139140

140141
@classmethod
141142
def match_all(cls):
@@ -154,7 +155,7 @@ def missing(cls, field):
154155
Filters documents where a specific field has no value in them.
155156
156157
'''
157-
return cls(missing={'field':field})
158+
return cls(missing={'field': field})
158159

159160
@classmethod
160161
def not_filter(cls, query):
@@ -163,19 +164,21 @@ def not_filter(cls, query):
163164
A filter that filters out matched documents using a query. This filter is more performant then bool filter. Can be placed within queries that accept a filter.
164165
165166
'''
166-
return cls({'not':query})
167+
return cls({'not': query})
167168

168169
@classmethod
169170
def numeric_range(cls, field, from_value, to_value, include_lower=None, include_upper=None):
170171
'''
171172
http://www.elasticsearch.org/guide/reference/query-dsl/numeric-range-filter.html
172173
Filters documents with fields that have values within a certain numeric range. Similar to range filter, except that it works only with numeric values, and the filter execution works differently.
173174
'''
174-
instance = cls(numeric_range={field:{'from':from_value, 'to':to_value}})
175-
if include_lower is not None: instance['numeric_range'][field]['include_lower'] = include_lower
176-
if include_upper is not None: instance['numeric_range'][field]['include_upper'] = include_upper
175+
instance = cls(numeric_range={field: {'from': from_value, 'to': to_value}})
176+
if include_lower is not None:
177+
instance['numeric_range'][field]['include_lower'] = include_lower
178+
if include_upper is not None:
179+
instance['numeric_range'][field]['include_upper'] = include_upper
177180
return instance
178-
181+
179182
@classmethod
180183
def or_filter(cls, query):
181184
'''
@@ -188,7 +191,7 @@ def or_filter(cls, query):
188191
> filter = ElasticFilter().or_filter([term1, term2])
189192
'''
190193

191-
return cls({'or':query})
194+
return cls({'or': query})
192195

193196
@classmethod
194197
def prefix(cls, field, pre):
@@ -197,16 +200,16 @@ def prefix(cls, field, pre):
197200
Filters documents that have fields containing terms with a specified prefix (not analyzed). Similar to phrase query, except that it acts as a filter. Can be placed within queries that accept a filter.
198201
'''
199202

200-
return cls(prefix={field:pre})
201-
203+
return cls(prefix={field: pre})
204+
202205
@classmethod
203206
def query(cls, query):
204207
'''
205208
http://www.elasticsearch.org/guide/reference/query-dsl/query-filter.html
206209
Wraps any query to be used as a filter. Can be placed within queries that accept a filter.
207210
'''
208211
return cls(query=query)
209-
212+
210213
@classmethod
211214
def range(cls, field, from_value=None, to_value=None, include_lower=None, include_upper=None):
212215
'''
@@ -215,11 +218,15 @@ def range(cls, field, from_value=None, to_value=None, include_lower=None, includ
215218
Filters documents with fields that have terms within a certain range. Similar to range query, except that it acts as a filter. Can be placed within queries that accept a filter.
216219
'''
217220

218-
instance = cls({'range':{field:{}}})
219-
if from_value is not None: instance['range'][field]['from'] = from_value
220-
if to_value is not None: instance['range'][field]['to'] = to_value
221-
if include_lower is not None: instance['range'][field]['include_lower'] = include_lower
222-
if include_upper is not None: instance['range'][field]['include_upper'] = include_upper
221+
instance = cls({'range': {field: {}}})
222+
if from_value is not None:
223+
instance['range'][field]['from'] = from_value
224+
if to_value is not None:
225+
instance['range'][field]['to'] = to_value
226+
if include_lower is not None:
227+
instance['range'][field]['include_lower'] = include_lower
228+
if include_upper is not None:
229+
instance['range'][field]['include_upper'] = include_upper
223230

224231
return instance
225232

@@ -241,5 +248,4 @@ def term(cls, field, value):
241248
Filters documents that have fields that contain a term (not analyzed). Similar to term query, except that it acts as a filter.
242249
'''
243250

244-
return cls(term={field:value})
245-
251+
return cls(term={field: value})

elasticpy/map.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,26 @@
88

99

1010
class ElasticMap(dict):
11+
1112
'''
1213
Mapping is the process of defining how a document should be mapped to the Search Engine, including its searchable characteristics such as which fields are searchable and if/how they are tokenized. In ElasticSearch, an index may store documents of different "mapping types". ElasticSearch allows one to associate multiple mapping definitions for each mapping type.
1314
1415
Explicit mapping is defined on an index/type level. By default, there isn't a need to define an explicit mapping, since one is automatically created and registered when a new type or new field is introduced (with no performance overhead) and have sensible defaults. Only when the defaults need to be overridden must a mapping definition be provided.
1516
'''
17+
1618
def __init__(self, field):
1719
self.field = field
1820
self[self.field] = dict()
1921

20-
def type(self,type_name):
22+
def type(self, type_name):
2123
'''
2224
Assigns a particular type to a field in the mapped properties.
2325
Available types are: string, integer/long, float/double, boolean and null
2426
'''
2527
self[self.field]['type'] = type_name
2628
return self
2729

28-
def analyzed(self,should=True):
30+
def analyzed(self, should=True):
2931
'''
3032
Specifies to the map that the field should be analyzed when indexed.
3133
'''
@@ -36,10 +38,10 @@ def ignore(self):
3638
'''
3739
Specifies that the field should be ignored in the index
3840
'''
39-
self[self.field] = {'index' : 'no'}
41+
self[self.field] = {'index': 'no'}
4042
return self
4143

42-
def null_value(self,value):
44+
def null_value(self, value):
4345
'''
4446
Specifies the null value to use when indexing.
4547
'''
@@ -99,4 +101,3 @@ def include_in_all(self, should=True):
99101
'''
100102
self[self.field]['include_in_all'] = should
101103
return self
102-

0 commit comments

Comments
 (0)