Skip to content

Commit c7a9cc9

Browse files
linwoodc3pegler
authored andcommitted
Reduces size of module bt 1/3. Achieves 94% code coverage, relating to #8. Closes #45. This is also an update to #46 and @seahawks code to include the files on the MANIFEST file..
1 parent 3d10a43 commit c7a9cc9

File tree

7 files changed

+59
-27752
lines changed

7 files changed

+59
-27752
lines changed

MANIFEST.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
include LICENSE
22
include README.md
3-
include tzwhere/tz_world.json
3+
include tzwhere/worldtest.json.gz
44
include tzwhere/tz_world_shortcuts.json

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
packages=['tzwhere'],
1313
package_data={
1414
'tzwhere': [
15-
'tz_world.json',
15+
'tz_world.json.gz',
1616
'tz_world_shortcuts.json'
1717
]
1818
},

tests/test_utilities.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
##################################
5+
# Standard library imports
6+
##################################
7+
8+
import os
9+
import datetime
10+
from unittest import TestCase
11+
12+
##################################
13+
# Custom library imports
14+
##################################
15+
16+
from tzwhere import tzwhere
17+
from tzwhere.tzwhere import BASE_DIR
18+
19+
20+
####################################
21+
22+
class TestTzwhereUtilities(TestCase):
23+
def test_preparemap_class(self):
24+
"""
25+
Tests the prepareMap class which writes shortcuts file. Test looks
26+
at modified date to see if that's equivalent to today's date
27+
28+
:return:
29+
Unit test response
30+
"""
31+
32+
a = tzwhere.prepareMap()
33+
location = os.path.join(BASE_DIR, 'tzwhere', 'tz_world_shortcuts.json')
34+
date = datetime.datetime.now().date()
35+
modified = datetime.datetime. \
36+
fromtimestamp(os.stat(location).st_mtime).date()
37+
return self.assertEqual(date, modified)

tzwhere/tz_world.json

-27,741
This file was deleted.

tzwhere/tz_world.json.gz

22.6 MB
Binary file not shown.

tzwhere/tz_world_shortcuts.json

+1-1
Large diffs are not rendered by default.

tzwhere/tzwhere.py

+19-8
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ class are instantiated and queried directly
88

99
import collections
1010
try:
11-
import json
12-
except ImportError:
13-
import simplejson as json
11+
import ujson as json # loads 2 seconds faster than normal json
12+
except:
13+
try:
14+
import json
15+
except ImportError:
16+
import simplejson as json
1417
import math
18+
import gzip
1519
import os
1620
import shapely.geometry as geometry
1721
import shapely.prepared as prepared
@@ -26,6 +30,9 @@ class are instantiated and queried directly
2630
WRAP = tuple
2731
COLLECTION_TYPE = tuple
2832

33+
# for navigation and pulling values/files
34+
this_dir, this_filename = os.path.split(__file__)
35+
BASE_DIR = os.path.dirname(this_dir)
2936

3037
class tzwhere(object):
3138

@@ -35,7 +42,7 @@ class tzwhere(object):
3542
DEFAULT_SHORTCUTS = os.path.join(os.path.dirname(__file__),
3643
'tz_world_shortcuts.json')
3744
DEFAULT_POLYGONS = os.path.join(os.path.dirname(__file__),
38-
'tz_world.json')
45+
'tz_world.json.gz')
3946

4047
def __init__(self, forceTZ=False):
4148
'''
@@ -149,7 +156,11 @@ def __forceTZ__(self, possibleTimezones, latTzOptions,
149156
class prepareMap(object):
150157

151158
def __init__(self):
152-
featureCollection = read_tzworld('tz_world.json')
159+
DEFAULT_SHORTCUTS = os.path.join(os.path.dirname(__file__),
160+
'tz_world_shortcuts.json')
161+
DEFAULT_POLYGONS = os.path.join(os.path.dirname(__file__),
162+
'tz_world.json.gz')
163+
featureCollection = read_tzworld(DEFAULT_POLYGONS)
153164
pgen = feature_collection_polygons(featureCollection)
154165
tzNamesToPolygons = collections.defaultdict(list)
155166
for tzname, poly in pgen:
@@ -164,7 +175,7 @@ def __init__(self):
164175
tzNamesToPolygons, tzwhere.SHORTCUT_DEGREES_LONGITUDE,
165176
tzwhere.SHORTCUT_DEGREES_LATITUDE)
166177

167-
with open('tz_world_shortcuts.json', 'w') as f:
178+
with open(DEFAULT_SHORTCUTS, 'w') as f:
168179
json.dump(
169180
(timezoneLongitudeShortcuts, timezoneLatitudeShortcuts), f)
170181

@@ -221,8 +232,8 @@ def read_tzworld(path):
221232

222233

223234
def read_json(path):
224-
with open(path, 'r') as f:
225-
featureCollection = json.load(f)
235+
with gzip.open(path, "rb") as f:
236+
featureCollection = json.loads(f.read().decode("utf-8"))
226237
return featureCollection
227238

228239

0 commit comments

Comments
 (0)