From 8afa84a30d8b145272bf8039bc4e6387f2fda3a0 Mon Sep 17 00:00:00 2001 From: bakhtiyar-garashov Date: Tue, 5 Oct 2021 00:23:48 +0300 Subject: [PATCH] Add support for Python 3.x --- .travis.yml | 2 +- external/geonames/update_countries.py | 16 ++++++++-------- geodis/business.py | 6 +++--- geodis/city.py | 6 +++--- geodis/data/geonames2mysql.py | 4 ++-- geodis/geodis | 12 ++++++------ geodis/location.py | 6 +++--- geodis/provider/factual.py | 4 ++-- geodis/provider/geonames.py | 2 +- geodis/provider/ip2location.py | 4 ++-- geodis/provider/zipcodes.py | 2 +- geodis/us_states.py | 3 +-- test/benchmark.py | 8 ++++---- 13 files changed, 37 insertions(+), 38 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5f8df90..19ebdf8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ before_install: language: python python: - - 2.7 + - 3.6 services: - redis-server env: TEST_REDIS_PORT=6379 diff --git a/external/geonames/update_countries.py b/external/geonames/update_countries.py index 3e52403..e23f848 100644 --- a/external/geonames/update_countries.py +++ b/external/geonames/update_countries.py @@ -26,18 +26,18 @@ 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') @@ -45,12 +45,12 @@ 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)) diff --git a/geodis/business.py b/geodis/business.py index a2a856d..0f9f360 100644 --- a/geodis/business.py +++ b/geodis/business.py @@ -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 diff --git a/geodis/city.py b/geodis/city.py index 9c285e7..2f7c7c4 100644 --- a/geodis/city.py +++ b/geodis/city.py @@ -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: diff --git a/geodis/data/geonames2mysql.py b/geodis/data/geonames2mysql.py index d4674de..be991d8 100755 --- a/geodis/data/geonames2mysql.py +++ b/geodis/data/geonames2mysql.py @@ -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] @@ -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 diff --git a/geodis/geodis b/geodis/geodis index 0bd5e7a..aac486b 100755 --- a/geodis/geodis +++ b/geodis/geodis @@ -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) @@ -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) @@ -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) @@ -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) @@ -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__": diff --git a/geodis/location.py b/geodis/location.py index c43fe98..2866182 100644 --- a/geodis/location.py +++ b/geodis/location.py @@ -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 @@ -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]) diff --git a/geodis/provider/factual.py b/geodis/provider/factual.py index 9535c79..dbb6b31 100644 --- a/geodis/provider/factual.py +++ b/geodis/provider/factual.py @@ -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 \ No newline at end of file diff --git a/geodis/provider/geonames.py b/geodis/provider/geonames.py index d99d476..a598171 100644 --- a/geodis/provider/geonames.py +++ b/geodis/provider/geonames.py @@ -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 diff --git a/geodis/provider/ip2location.py b/geodis/provider/ip2location.py index 9ddb089..3561451 100644 --- a/geodis/provider/ip2location.py +++ b/geodis/provider/ip2location.py @@ -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() diff --git a/geodis/provider/zipcodes.py b/geodis/provider/zipcodes.py index 93ba848..120b9b3 100644 --- a/geodis/provider/zipcodes.py +++ b/geodis/provider/zipcodes.py @@ -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 diff --git a/geodis/us_states.py b/geodis/us_states.py index 4edeb10..134d167 100644 --- a/geodis/us_states.py +++ b/geodis/us_states.py @@ -124,7 +124,6 @@ def score(self, refLat, refLon): if __name__ == '__main__': - - print State.get('CA') + print(State.get('CA')) diff --git a/test/benchmark.py b/test/benchmark.py index a4e3a4d..5b99a74 100644 --- a/test/benchmark.py +++ b/test/benchmark.py @@ -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) @@ -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) @@ -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__":