def check_speed_my_algor(): start_time = datetime.now() timezonefinder = TimezoneFinder() end_time = datetime.now() timezonefinder.timezone_at(13.3, 53.2) return end_time - start_time
def timezone(self): if self._timezone: return self._timezone tf = TimezoneFinder() coords = self.coordinates return tf.timezone_at(lng=coords[1], lat=coords[0])
class MainPackageTest(unittest.TestCase): # do the preparations which have to be made only once print("startup time:") if TimezoneFinder.using_numba(): print('Numba: ON (precompiled functions in use)') else: print('Numba: OFF (precompiled functions NOT in use)') start_time = datetime.now() timezone_finder = TimezoneFinder() end_time = datetime.now() my_time = end_time - start_time print_time(timezoefinder_time=my_time) print('\n') # create an array of points where timezone_finder finds something (realistic queries) print('collecting and storing', N, 'realistic points for the tests...') realistic_points = [] ps_for_10percent = int(N / 10) percent_done = 0 i = 0 while i < N: lng, lat = random_point() # a realistic point is a point where certain_timezone_at() finds something if timezone_finder.certain_timezone_at(lng=lng, lat=lat): i += 1 realistic_points.append((lng, lat)) if i % ps_for_10percent == 0: percent_done += 10 print(percent_done, '%') print("Done.\n") def setUp(self): self.timezone_finder = TimezoneFinder() def test_speed(self): print("\n\nSpeed Tests:\n-------------") def check_speed_of_algorithm(list_of_points): start_time = datetime.now() for point in list_of_points: self.timezone_finder.timezone_at(lng=point[0], lat=point[1]) end_time = datetime.now() return end_time - start_time def print_speed_test(type_of_points, list_of_points): my_time = check_speed_of_algorithm(list_of_points) print('\nrequired time for ', N, type_of_points) print_time(timezoefinder_time=my_time) if TimezoneFinder.using_numba(): print('Numba: ON (timezonefinder)') else: print('Numba: OFF (timezonefinder)') print_speed_test('realistic points', self.realistic_points) print_speed_test('random points', list_of_random_points(length=N)) def test_shortcut_boundary(self): # at the boundaries of the shortcut grid (coordinate system) the algorithms should still be well defined! assert self.timezone_finder.timezone_at(lng=-180.0, lat=90.0) is None assert self.timezone_finder.timezone_at(lng=180.0, lat=90.0) is None assert self.timezone_finder.timezone_at( lng=180.0, lat=-90.0) == 'Antarctica/McMurdo' assert self.timezone_finder.timezone_at( lng=-180.0, lat=-90.0) == 'Antarctica/McMurdo' with pytest.raises(ValueError): self.timezone_finder.timezone_at(lng=180.0 + INT2COORD_FACTOR, lat=90.0) self.timezone_finder.timezone_at(lng=-180.0 - INT2COORD_FACTOR, lat=90.0 + INT2COORD_FACTOR) self.timezone_finder.timezone_at(lng=-180.0, lat=90.0 + INT2COORD_FACTOR) self.timezone_finder.timezone_at(lng=180.0 + INT2COORD_FACTOR, lat=-90.0) self.timezone_finder.timezone_at(lng=180.0, lat=-90.0 - INT2COORD_FACTOR) self.timezone_finder.timezone_at(lng=-180.0 - INT2COORD_FACTOR, lat=-90.0) self.timezone_finder.timezone_at(lng=-180.0 - INT2COORD_FACTOR, lat=-90.01 - INT2COORD_FACTOR) def test_kwargs_only(self): # calling timezonefinder fcts without keyword arguments should raise an error with pytest.raises(TypeError): self.timezone_finder.timezone_at(23.0, 42.0) self.timezone_finder.timezone_at(23.0, lng=42.0) self.timezone_finder.timezone_at(23.0, lat=42.0) def test_correctness(self): no_mistakes_made = True template = '{0:20s} | {1:20s} | {2:20s} | {3:2s}' print('\nresults timezone_at()') print(template.format('LOCATION', 'EXPECTED', 'COMPUTED', '==')) print( '====================================================================' ) for (lat, lng, loc, expected) in TEST_LOCATIONS: computed = self.timezone_finder.timezone_at(lng=lng, lat=lat) if computed == expected: ok = 'OK' else: print(lat, lng) ok = 'XX' no_mistakes_made = False print(template.format(loc, str(expected), str(computed), ok)) print('\ncertain_timezone_at():') print(template.format('LOCATION', 'EXPECTED', 'COMPUTED', 'Status')) print( '====================================================================' ) for (lat, lng, loc, expected) in TEST_LOCATIONS_CERTAIN: computed = self.timezone_finder.certain_timezone_at(lng=lng, lat=lat) if computed == expected: ok = 'OK' else: print(lat, lng) ok = 'XX' no_mistakes_made = False print(template.format(loc, str(expected), str(computed), ok)) print('\nclosest_timezone_at():') print(template.format('LOCATION', 'EXPECTED', 'COMPUTED', 'Status')) print( '====================================================================' ) print( 'testing this function does not make sense any more, because the tz polygons do not follow the shoreline' ) for (lat, lng, loc, expected) in TEST_LOCATIONS_PROXIMITY: computed = self.timezone_finder.closest_timezone_at(lng=lng, lat=lat) if computed == expected: ok = 'OK' else: print(lat, lng) ok = 'XX' no_mistakes_made = False print(template.format(loc, str(expected), str(computed), ok)) assert no_mistakes_made def test_overflow(self): longitude = -123.2 latitude = 48.4 # make numpy overflow runtime warning raise an error import numpy as np np.seterr(all='warn') import warnings warnings.filterwarnings('error') # must not raise a warning self.timezone_finder.certain_timezone_at(lat=float(latitude), lng=float(longitude))
) print("time remaining:") while duration_idle_mem_test > 0: print(duration_idle_mem_test, 's') time.sleep(1) duration_idle_mem_test -= 1 print("package is now in use.") print( "Check the memory usage of python in your process list (Task Manager, Activity Manager)" ) seconds_registered = 0 start_time = datetime.now() print("seconds passed:") seconds_passed = 0 while seconds_passed < duration_in_use_mem_test: if seconds_passed > seconds_registered: print(seconds_passed) seconds_registered = seconds_passed point_list = list_of_random_points(100) for lng, lat in point_list: result = timezone_finder.timezone_at(lng=lng, lat=lat) seconds_passed = (datetime.now() - start_time).seconds ''' Peak RAM usage: timezonefinder: 16,4MB Numba off, in use 15,4MB numba off, idle '''