Skip to content

Commit 578d5ac

Browse files
Add timezone grid caching to CLI (#17) (#21)
* add cli caching, will read from timezone.json or --grid argument * update output in test_executable_without_args * update find_sun to use cached timezone_grid.json * Bump minor version number --------- Co-authored-by: Galen Reich <54807169+GalenReich@users.noreply.github.com>
1 parent e5f636b commit 578d5ac

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "ShadowFinder"
3-
version = "0.4.0"
3+
version = "0.5.0"
44
description = "Find possible locations of shadows."
55
authors = ["Bellingcat"]
66
license = "MIT License"

src/shadowfinder/cli.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def find(
4141
date: str,
4242
time: str,
4343
time_format: str = "utc",
44+
grid: str = "timezone_grid.json",
4445
) -> None:
4546
"""
4647
Find the shadow length of an object given its height and the date and time.
@@ -59,14 +60,15 @@ def find(
5960
shadow_finder = ShadowFinder(
6061
object_height, shadow_length, date_time, time_format
6162
)
62-
shadow_finder.quick_find()
63+
shadow_finder.quick_find(grid)
6364

6465
@staticmethod
6566
def find_sun(
6667
sun_altitude_angle: float,
6768
date: str,
6869
time: str,
6970
time_format: str = "utc",
71+
grid: str = "timezene_grid.json",
7072
) -> None:
7173
"""
7274
Locate a shadow based on the solar altitude angle and the date and time.
@@ -86,4 +88,17 @@ def find_sun(
8688
time_format=time_format,
8789
sun_altitude_angle=sun_altitude_angle,
8890
)
89-
shadow_finder.quick_find()
91+
shadow_finder.quick_find(grid)
92+
93+
@staticmethod
94+
def generate_timezone_grid(
95+
grid: str = "timezone_grid.json",
96+
) -> None:
97+
"""
98+
Generate a timezone grid file.
99+
:param grid: File path to save the timezone grid.
100+
"""
101+
102+
shadow_finder = ShadowFinder()
103+
shadow_finder.generate_timezone_grid()
104+
shadow_finder.save_timezone_grid(grid)

src/shadowfinder/shadowfinder.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,13 @@ def set_details(
9393
# Lengths and angle are None and we use the same values as before
9494
pass
9595

96-
def quick_find(self):
97-
self.generate_timezone_grid()
96+
def quick_find(self, timezone_grid="timezone_grid.json"):
97+
# try to load timezone grid from file, generate if not found
98+
try:
99+
self.load_timezone_grid(timezone_grid)
100+
except FileNotFoundError:
101+
self.generate_timezone_grid()
102+
98103
self.find_shadows()
99104
fig = self.plot_shadows()
100105

tests/test_executable.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ def test_executable_without_args():
2424
2525
find_sun
2626
Locate a shadow based on the solar altitude angle and the date and time.
27+
28+
generate_timezone_grid
29+
Generate a timezone grid file.
2730
"""
2831

2932
# WHEN

0 commit comments

Comments
 (0)