Conditions | 6 |
Total Lines | 78 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # -*- coding: utf-8 -*- |
||
34 | @expose('json') |
||
35 | def index(self, address=None, *args, **kwargs): |
||
36 | if not address: |
||
37 | return dict(results=[]) |
||
38 | |||
39 | search = model.Geocomplete().search() |
||
40 | |||
41 | address_query = Q() |
||
42 | postal_code_query = Q() |
||
43 | |||
44 | (address, postal_code) = self.geocomplete_town_input_parser(address) |
||
45 | |||
46 | if address: |
||
47 | address_query = Q('match', name=address) |
||
48 | |||
49 | if postal_code: |
||
50 | postal_code_query = Q('match', postal_code=postal_code) |
||
51 | |||
52 | weight_scoring_function = SF( |
||
53 | 'field_value_factor', |
||
54 | factor=1, |
||
55 | modifier='none', |
||
56 | field='weight' |
||
57 | ) |
||
58 | |||
59 | search.query = Q( |
||
60 | 'function_score', |
||
61 | query=address_query & postal_code_query, |
||
62 | functions=[weight_scoring_function] |
||
63 | ) |
||
64 | |||
65 | dedup_docs = A( |
||
66 | 'top_hits', |
||
67 | size=1, |
||
68 | sort={'postal_code.raw': 'asc'} |
||
69 | ) |
||
70 | |||
71 | dedup = A( |
||
72 | 'terms', |
||
73 | field='name.raw', |
||
74 | size=5, |
||
75 | order={'score_sort': 'desc'} |
||
76 | ) |
||
77 | |||
78 | score_sort = A( |
||
79 | 'max', |
||
80 | script=dict(lang='expression', script='_score') |
||
81 | ) |
||
82 | |||
83 | dedup.bucket('dedup_docs', dedup_docs) |
||
84 | dedup.bucket('score_sort', score_sort) |
||
85 | search.aggs.bucket('dedup', dedup) |
||
86 | |||
87 | # Do not compute the results, we are only interested by the aggregations |
||
88 | raw_res = search[0:0].execute() |
||
89 | |||
90 | res = list() |
||
91 | for bucket in raw_res.aggregations.dedup.buckets: |
||
92 | for source_doc in bucket['dedup_docs']['hits']['hits']: |
||
93 | fields = source_doc['_source'] |
||
94 | |||
95 | name = fields['name'] |
||
96 | complement = fields['complement'] |
||
97 | postal_code = fields['postal_code'] |
||
98 | country = 'France' |
||
99 | |||
100 | geoloc = fields['geolocation'] |
||
101 | coordinates = dict(lat=geoloc['lat'], lon=geoloc['lon']) |
||
102 | |||
103 | res.append(dict( |
||
104 | name=name, |
||
105 | complement=complement, |
||
106 | postal_code=postal_code, |
||
107 | country=country, |
||
108 | coordinates=coordinates |
||
109 | )) |
||
110 | |||
111 | return dict(results=res) |
||
112 |