Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Python 3.x #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ before_install:

language: python
python:
- 2.7
- 3.6
services:
- redis-server
env: TEST_REDIS_PORT=6379
Expand Down
16 changes: 8 additions & 8 deletions external/geonames/update_countries.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,31 @@
source = itertools.dropwhile(lambda l: l.startswith('#'), f)
reader = csv.DictReader(source, fields, delimiter='\t')

print 'import collections'
print 'Country = collections.namedtuple(\'Country\', {})'.format(fields)
print 'countries = ['
print('import collections')
print('Country = collections.namedtuple(\'Country\', {})'.format(fields))
print('countries = [')

for line in reader:
for field in fields:
t = types.get(field, str)
attr = line[field].strip()
line[field] = t(attr) if attr else None
print ' {},'.format(Country(**line))
print (' {},'.format(Country(**line)))

print ']'
print (']')

# Generate getters (i.e: getIdByName, get2LetterCodeById)
_attrs = ('Id', 'id'), ('Name', 'name'), ('2LetterCode', 'ISO'), ('3LetterCode', 'ISO3')
for attr in _attrs:
print
print
lookup = 'countriesBy{}'.format(attr[0])
print '{} = {{c.{}: c for c in countries}}'.format(lookup, attr[1])
print ('{} = {{c.{}: c for c in countries}}'.format(lookup, attr[1]))

others = set(_attrs) - {attr}
for other in others:
print
print
print '''def get{other[0]}By{attr[0]}({attr[1]}):
print ('''def get{other[0]}By{attr[0]}({attr[1]}):
"""Get country {other[0]} by {attr[0]}"""
return {lookup}[{attr[1]}].{other[1]}'''.format(other=other, attr=attr, lookup=lookup)
return {lookup}[{attr[1]}].{other[1]}'''.format(other=other, attr=attr, lookup=lookup))
6 changes: 3 additions & 3 deletions geodis/business.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ def getByRadius(cls, lat, lon, radius, redisConn, text = None):
nodes = Business.getByRadius(lat, lon, 10, r, 'mcdonalds')
#nodes.sort(lambda x,y: cmp(y.score(lat, lon), x.score(lat, lon)))
et = time.time()
print len(nodes)
print(len(nodes))
for n in nodes:
print n.name, ',', n.address, Location.getLatLonDistance((lat, lon), (n.lat, n.lon)), "km"
print 1000*(et-st)
print(n.name, ',', n.address, Location.getLatLonDistance((lat, lon), (n.lat, n.lon)), "km")
print(1000*(et-st))
# for city in cities:
# print city.name, city.country, Location.getLatLonDistance((lat, lon), (city.lat, city.lon))
#import redis
Expand Down
6 changes: 3 additions & 3 deletions geodis/city.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ def getByRadius(cls, lat, lon, radius, redisConn, text = None, limit = 5):
#cities = City.getByRadius(lat, lon, d, r, "haifa")
cities = City.getByName('tel aviv', r, lat, lon)
et = time.time()
print 1000*(et - st),"ms"
print "Found %d cities!" % len(cities)
print "\n".join(["%s %s, %s %.02fkm pop %s score %s" % (c.country, c.name, c.state, Location.getLatLonDistance((lat, lon), (c.lat, c.lon)), c.population, c.score(lat, lon)) for c in cities])
print(1000*(et - st),"ms")
print("Found %d cities!" % len(cities))
print("\n".join(["%s %s, %s %.02fkm pop %s score %s" % (c.country, c.name, c.state, Location.getLatLonDistance((lat, lon), (c.lat, c.lon)), c.population, c.score(lat, lon)) for c in cities]))


# for city in cities:
Expand Down
4 changes: 2 additions & 2 deletions geodis/data/geonames2mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def load(self, allCountires, alternates, hierarchy):
(r[Cols.feature_code]!='TERR' or int(r[Cols.gtopo3])>=0):

if fc == 'TERR' and r[Cols.country_code] == 'AU':
print r
print(r)
continue

ft = featureCodes[fc]
Expand Down Expand Up @@ -220,7 +220,7 @@ def dump(self):
record.insert(4, '0')
record.insert(5, '')

print json.dumps(record, ensure_ascii=False)
print(json.dumps(record, ensure_ascii=False))

return True

Expand Down
12 changes: 6 additions & 6 deletions geodis/geodis
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def importGeonames(fileName):
global redis_host, redis_port, redis_db
importer = GeonamesImporter(fileName, redis_host, redis_port, redis_db)
if not importer.runImport():
print "Could not import geonames database..."
print("Could not import geonames database...")
sys.exit(1)


Expand All @@ -63,7 +63,7 @@ def importBusinesses(fileName):
global redis_host, redis_port, redis_db
importer = BusinessImporter(fileName, redis_host, redis_port, redis_db)
if not importer.runImport():
print "Could not import business database..."
print("Could not import business database...")
sys.exit(1)


Expand All @@ -72,7 +72,7 @@ def importIP2Location(fileName):
global redis_host, redis_port, redis_db
importer = IP2LocationImporter(fileName, redis_host, redis_port, redis_db)
if not importer.runImport(True):
print "Could not import geonames database..."
print("Could not import geonames database...")
sys.exit(1)


Expand All @@ -81,7 +81,7 @@ def importZIPCode(fileName):
global redis_host, redis_port, redis_db
importer = ZIPImporter(fileName, redis_host, redis_port, redis_db)
if not importer.runImport():
print "Could not import geonames database..."
print("Could not import geonames database...")
sys.exit(1)


Expand All @@ -90,14 +90,14 @@ def resolveIP(ip):
r = redis.Redis(host = redis_host, port = redis_port, db = redis_db)

loc = IPRange.getCity(ip, r)
print loc
print(loc)


def resolveCoords(lat, lon):
global redis_host, redis_port, redis_db
r = redis.Redis(host = redis_host, port = redis_port, db = redis_db)
loc = City.getByLatLon(lat, lon, r)
print loc
print(loc)


if __name__ == "__main__":
Expand Down
6 changes: 3 additions & 3 deletions geodis/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ def getDistance(geoHash1, geoHash2):
return Location.getLatLonDistance(coords1, coords2)
#return math.sqrt(math.pow(coords1[0] - coords2[0], 2) +
#math.pow(coords1[1] - coords2[1], 2))
except Exception, e:
print e
except Exception as e:
print(e)
return None


Expand Down Expand Up @@ -238,7 +238,7 @@ def getByGeohash(cls, geoKey, redisConn):
if not candidates :
return None

for i in xrange(len(candidates)):
for i in range(len(candidates)):

gk = long(candidates[i][1])

Expand Down
4 changes: 2 additions & 2 deletions geodis/provider/factual.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ def runImport(self):

i += 1
if i % 1000 == 0:
print i
print(i)
pipe.execute()
pipe.execute()

logging.info("Imported %d businesses, failed %d times" , i, fails)
print "Finished!"
print("Finished!")
return True
2 changes: 1 addition & 1 deletion geodis/provider/geonames.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ def runImport(self):
pipe.execute()

logging.info("Imported %d cities, failed %d times" , i, fails)
print "Finished!"
print("Finished!")
return True
4 changes: 2 additions & 2 deletions geodis/provider/ip2location.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ def runImport(self, reset = False):
return False

if reset:
print "Deleting old ip data..."
print("Deleting old ip data...")
self.redis.delete(IPRange._indexKey)

print "Starting import..."
print("Starting import...")

reader = csv.reader(fp, delimiter=',', quotechar='"')
pipe = self.redis.pipeline()
Expand Down
2 changes: 1 addition & 1 deletion geodis/provider/zipcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def runImport(self):
state['cities'][city['name']] = city['cityId']


print features.keys()
print(features.keys())
pipe = self.redis.pipeline()
i = 0
fails = 0
Expand Down
3 changes: 1 addition & 2 deletions geodis/us_states.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ def score(self, refLat, refLon):


if __name__ == '__main__':

print State.get('CA')
print(State.get('CA'))


8 changes: 4 additions & 4 deletions test/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def benchResolveIPs(num):
#ips = ['166.205.138.92', '62.0.18.221', '69.147.125.65', '188.127.241.156', '79.178.26.33']
r = redis.Redis()
nips = len(ips)
for i in xrange(num):
for i in range(num):
ip = ips[i % nips]
loc = IPRange.getCity(ip, r)

Expand All @@ -274,7 +274,7 @@ def benchResolveCoords(num):
coords = [(-3.03333,53.47778), (40.7226,-74.66544), (31.78199,35.2196), (0,0),(45,45)]
r = redis.Redis()
ncoords = len(coords)
for i in xrange(num):
for i in range(num):
lat,lon = coords[i % ncoords]
loc = City.getByLatLon(lat,lon, r)

Expand All @@ -283,12 +283,12 @@ def benchResolveCoords(num):

def benchSingleProc(func, num):

print "Running benchmark %s for %d times..." % (func.__name__, num)
print("Running benchmark %s for %d times..." % (func.__name__, num))
st = time.time()
num = func(num)
et = time.time()

print "time: %.03fsec, rate: %.03fq/s" % (et - st, (float(num) / (et-st)))
print("time: %.03fsec, rate: %.03fq/s" % (et - st, (float(num) / (et-st))))

if __name__ == "__main__":

Expand Down