Skip to content

Commit

Permalink
Add timezone grid caching to CLI (#17) (#21)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
borisnezlobin and GalenReich authored Aug 28, 2024
1 parent e5f636b commit 578d5ac
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ShadowFinder"
version = "0.4.0"
version = "0.5.0"
description = "Find possible locations of shadows."
authors = ["Bellingcat"]
license = "MIT License"
Expand Down
19 changes: 17 additions & 2 deletions src/shadowfinder/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def find(
date: str,
time: str,
time_format: str = "utc",
grid: str = "timezone_grid.json",
) -> None:
"""
Find the shadow length of an object given its height and the date and time.
Expand All @@ -59,14 +60,15 @@ def find(
shadow_finder = ShadowFinder(
object_height, shadow_length, date_time, time_format
)
shadow_finder.quick_find()
shadow_finder.quick_find(grid)

@staticmethod
def find_sun(
sun_altitude_angle: float,
date: str,
time: str,
time_format: str = "utc",
grid: str = "timezene_grid.json",
) -> None:
"""
Locate a shadow based on the solar altitude angle and the date and time.
Expand All @@ -86,4 +88,17 @@ def find_sun(
time_format=time_format,
sun_altitude_angle=sun_altitude_angle,
)
shadow_finder.quick_find()
shadow_finder.quick_find(grid)

@staticmethod
def generate_timezone_grid(
grid: str = "timezone_grid.json",
) -> None:
"""
Generate a timezone grid file.
:param grid: File path to save the timezone grid.
"""

shadow_finder = ShadowFinder()
shadow_finder.generate_timezone_grid()
shadow_finder.save_timezone_grid(grid)
9 changes: 7 additions & 2 deletions src/shadowfinder/shadowfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,13 @@ def set_details(
# Lengths and angle are None and we use the same values as before
pass

def quick_find(self):
self.generate_timezone_grid()
def quick_find(self, timezone_grid="timezone_grid.json"):
# try to load timezone grid from file, generate if not found
try:
self.load_timezone_grid(timezone_grid)
except FileNotFoundError:
self.generate_timezone_grid()

self.find_shadows()
fig = self.plot_shadows()

Expand Down
3 changes: 3 additions & 0 deletions tests/test_executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def test_executable_without_args():
find_sun
Locate a shadow based on the solar altitude angle and the date and time.
generate_timezone_grid
Generate a timezone grid file.
"""

# WHEN
Expand Down

0 comments on commit 578d5ac

Please sign in to comment.