HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Handling regional search requests

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
handlingregionalsearchrequests

Problem

The code works but I want to split it into functions and make it more reusable. The way it is now, the code is not reusable. I want the parts which replaces the query string and the part that populates with the number of items to be reusable for other classes, so ideally I want to make'em functions but then I suppose I must pass the template variable to those functions?

```
class RegionSearch(SearchBaseHandler):
"""Handles regional search requests."""

def get(self):
"""Handles a get request with a query."""
category = None
cityentity = None
country = ''
if self.request.host.find('hipheap') > -1: country = 'USA'
elif self.request.host.find('koolbusiness') > -1: country = 'India'
elif self.request.host.find('montao') > -1: country = 'Brasil'
regionID = 0
cityID = 0
categoryID = 0
regionname = None
cityname = None
categoryname = None
region = None
cursor = self.request.get("cursor")
uri = urlparse(self.request.uri)
query = ''
regionid = self.request.get("regionid")
cityid = self.request.get("cityid")
categoryid = self.request.get("category")
if uri.query:
query = parse_qs(uri.query)
try:
query = query['query'][0]
except KeyError, err:
query = ''
#logging.info('KeyError')

#Try find region ID and/or cityID and categoryID a.s.a.p.
if regionid or query.find('regionID') > -1:
regionID = re.sub("^regionID=(\d+).*", r'\1', query)
region = Region.get_by_id(long(regionID))
regionname = region.name

if regionid:
regionID = regionid
region = Region.get_by_id(long(regionID))
regionname = region.name

if cityid or query.find('cityID') > -1:
cityID = re.sub("^.cityID=(\d+).", r'\1', query)

Solution

Start with making a dictionary for the regionId-to-name part:

region_id_to_name = {'4694186': 'Andhra Pradesh', ... } #very long


And put it, along the form.w.choices list (which is actually exactly the same dictionary) outside the class, or as an instance variable. It should probably be read from a file, but that's a different story.

You can turn most of the local variables into instance variables.

The most basic thing is to ask "what am I doing here" in each part of this function. if you can answer, take this description, add a def before and arguments after, and you'll have a function/method.

Here's a simple example:

def mutate_query(self, query):
    query = query.replace(...) # whatever you are doing here
    query = re.sub("regionID=\d+", "", query)
    to_remove = ['category and', 'type=s', 'type=w', 'type=r','type=b','cityID and','and','regionID']
    for s in to_remove:
        query = query.replace(s,'')
    query = query.replace('=','%3D')
    query = re.sub("cityID%3D\d+", "", query)
    query = re.sub("category%3D\d+", "", query)
    query = query.replace('  ',' ')
    return query

Code Snippets

region_id_to_name = {'4694186': 'Andhra Pradesh', ... } #very long
def mutate_query(self, query):
    query = query.replace(...) # whatever you are doing here
    query = re.sub("regionID=\d+", "", query)
    to_remove = ['category and', 'type=s', 'type=w', 'type=r','type=b','cityID and','and','regionID']
    for s in to_remove:
        query = query.replace(s,'')
    query = query.replace('=','%3D')
    query = re.sub("cityID%3D\d+", "", query)
    query = re.sub("category%3D\d+", "", query)
    query = query.replace('  ',' ')
    return query

Context

StackExchange Code Review Q#27657, answer score: 6

Revisions (0)

No revisions yet.