Skip to content

Instantly share code, notes, and snippets.

@jtallieu
Last active September 12, 2016 18:39
Show Gist options
  • Save jtallieu/083acd01ce9e46a3258860f159d74657 to your computer and use it in GitHub Desktop.
Save jtallieu/083acd01ce9e46a3258860f159d74657 to your computer and use it in GitHub Desktop.
Eagle Eye Networks Inc. :: Python screening sample - Optimize
"""
::Eagle Eye Networks Inc.:::
::Python screening::
The following script is purposley written poorly.
The City class depends on an external service that is really
slow. Improve the code below so that 'test1' is more performant
but preserves:
- the ability to get a City as a dict or string.
- the City class interface.
Hint: You should get below 10s
"""
"""
!!!YOU CANNOT TOUCH THIS PART OF THE SAMPLE!!!
Imagine this as an external service that provides
a geolocation hash for an address/business/city/etc
"""
import time
class GeoService(object):
def location_hash(self, location):
time.sleep(5)
return str(abs(hash(location)))
"""
End of the external service definition
"""
"""
You are free to modify the code below
"""
import cProfile
class City(object):
def __init__(self, name):
self.__geoservice = GeoService()
self.name = name
@property
def location(self):
return self.__geoservice.location_hash(self.name)
def city_to_dict(city):
return {'name': city.name, 'location': city.location}
def city_str(city):
return "%s is @ %s" % (city.name, city.location)
def test1():
austin = City("Austin")
print city_to_dict(austin)
print city_str(austin)
if __name__ == "__main__":
cProfile.run("test1()")
@jtallieu
Copy link
Author

jtallieu commented Sep 9, 2016

         17 function calls in 10.005 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000   10.005   10.005 sample_2.py:51(test1)

@thartley
Copy link

misspelled "purposley"

other than that, 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment