Skip to content

Commit

Permalink
Reduces size of module bt 1/3. Achieves 94% code coverage, relating to
Browse files Browse the repository at this point in the history
…#8.  Closes #45. This is also an update to #46 and @seahawks code to include the files on the MANIFEST file..
  • Loading branch information
linwoodc3 authored and pegler committed Aug 22, 2017
1 parent 3d10a43 commit c7a9cc9
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 27,752 deletions.
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include LICENSE
include README.md
include tzwhere/tz_world.json
include tzwhere/worldtest.json.gz
include tzwhere/tz_world_shortcuts.json
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
packages=['tzwhere'],
package_data={
'tzwhere': [
'tz_world.json',
'tz_world.json.gz',
'tz_world_shortcuts.json'
]
},
Expand Down
37 changes: 37 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

##################################
# Standard library imports
##################################

import os
import datetime
from unittest import TestCase

##################################
# Custom library imports
##################################

from tzwhere import tzwhere
from tzwhere.tzwhere import BASE_DIR


####################################

class TestTzwhereUtilities(TestCase):
def test_preparemap_class(self):
"""
Tests the prepareMap class which writes shortcuts file. Test looks
at modified date to see if that's equivalent to today's date
:return:
Unit test response
"""

a = tzwhere.prepareMap()
location = os.path.join(BASE_DIR, 'tzwhere', 'tz_world_shortcuts.json')
date = datetime.datetime.now().date()
modified = datetime.datetime. \
fromtimestamp(os.stat(location).st_mtime).date()
return self.assertEqual(date, modified)
27,741 changes: 0 additions & 27,741 deletions tzwhere/tz_world.json

This file was deleted.

Binary file added tzwhere/tz_world.json.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion tzwhere/tz_world_shortcuts.json

Large diffs are not rendered by default.

27 changes: 19 additions & 8 deletions tzwhere/tzwhere.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ class are instantiated and queried directly

import collections
try:
import json
except ImportError:
import simplejson as json
import ujson as json # loads 2 seconds faster than normal json
except:
try:
import json
except ImportError:
import simplejson as json
import math
import gzip
import os
import shapely.geometry as geometry
import shapely.prepared as prepared
Expand All @@ -26,6 +30,9 @@ class are instantiated and queried directly
WRAP = tuple
COLLECTION_TYPE = tuple

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

class tzwhere(object):

Expand All @@ -35,7 +42,7 @@ class tzwhere(object):
DEFAULT_SHORTCUTS = os.path.join(os.path.dirname(__file__),
'tz_world_shortcuts.json')
DEFAULT_POLYGONS = os.path.join(os.path.dirname(__file__),
'tz_world.json')
'tz_world.json.gz')

def __init__(self, forceTZ=False):
'''
Expand Down Expand Up @@ -149,7 +156,11 @@ def __forceTZ__(self, possibleTimezones, latTzOptions,
class prepareMap(object):

def __init__(self):
featureCollection = read_tzworld('tz_world.json')
DEFAULT_SHORTCUTS = os.path.join(os.path.dirname(__file__),
'tz_world_shortcuts.json')
DEFAULT_POLYGONS = os.path.join(os.path.dirname(__file__),
'tz_world.json.gz')
featureCollection = read_tzworld(DEFAULT_POLYGONS)
pgen = feature_collection_polygons(featureCollection)
tzNamesToPolygons = collections.defaultdict(list)
for tzname, poly in pgen:
Expand All @@ -164,7 +175,7 @@ def __init__(self):
tzNamesToPolygons, tzwhere.SHORTCUT_DEGREES_LONGITUDE,
tzwhere.SHORTCUT_DEGREES_LATITUDE)

with open('tz_world_shortcuts.json', 'w') as f:
with open(DEFAULT_SHORTCUTS, 'w') as f:
json.dump(
(timezoneLongitudeShortcuts, timezoneLatitudeShortcuts), f)

Expand Down Expand Up @@ -221,8 +232,8 @@ def read_tzworld(path):


def read_json(path):
with open(path, 'r') as f:
featureCollection = json.load(f)
with gzip.open(path, "rb") as f:
featureCollection = json.loads(f.read().decode("utf-8"))
return featureCollection


Expand Down

0 comments on commit c7a9cc9

Please sign in to comment.