Skip to content

Commit

Permalink
Add type hints to the HinetPy/utils.py file
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Mar 27, 2024
1 parent 2e5fdd4 commit 683a84a
Showing 1 changed file with 41 additions and 32 deletions.
73 changes: 41 additions & 32 deletions HinetPy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Utility functions.
"""

from __future__ import annotations

import math
import shutil
from datetime import date, datetime
Expand All @@ -11,20 +13,20 @@
from packaging.version import Version


def split_integer(number, maxn):
def split_integer(number: int, maxn: int) -> list[int]:
"""
Split an integer into evenly sized chunks.
Parameters
----------
number: int
number
An interger that to be split into chunks.
maxn: int
maxn
The maximum number in each chunk.
Returns
-------
list
chunks
List of integers.
Examples
Expand All @@ -42,29 +44,29 @@ def split_integer(number, maxn):


def point_inside_box(
latitude,
longitude,
minlatitude=None,
maxlatitude=None,
minlongitude=None,
maxlongitude=None,
):
latitude: float,
longitude: float,
minlatitude: float | None = None,
maxlatitude: float | None = None,
minlongitude: float | None = None,
maxlongitude: float | None = None,
) -> bool:
"""
Check if a point is inside a box region.
Parameters
----------
latitude: float
latitude
Latitude of the point.
longitude: float
longitude
Longitude of the point.
minlatitude: float
minlatitude
Minimum latitude of the box region.
maxlatitude: float
maxlatitude
Maximum latitude of the box region.
minlongitude: float
minlongitude
Minimum longitude of the box region.
maxlongitude: float
maxlongitude
Maximum longitude of the box region.
Returns
Expand Down Expand Up @@ -100,7 +102,7 @@ def point_inside_box(
return True


def haversine(lat1, lon1, lat2, lon2):
def haversine(lat1: float, lon1: float, lat2: float, lon2: float) -> float:
"""
Calculate the great circle distance between two points on the earth (specified in
decimal degrees) using haversine formula.
Expand All @@ -125,23 +127,30 @@ def haversine(lat1, lon1, lat2, lon2):
return 2.0 * math.degrees(math.asin(math.sqrt(delta)))


def point_inside_circular(lat1, lon1, lat2, lon2, minradius=None, maxradius=None):
def point_inside_circular(
lat1: float,
lon1: float,
lat2: float,
lon2: float,
minradius: float | None = None,
maxradius: float | None = None,
) -> bool:
"""
Check if a point is inside a circular region.
Parameters
----------
lat1: float
lat1
Latitude of the point.
lon1: float
lon1
Longitude of the point.
lat2: float
lat2
Latitude of center of the circular region.
lon2: float
lon2
Longitude of center of the circular region.
minradius: float
minradius
Minimum radius in degrees of the circular region.
maxradius: float
maxradius
Maximum radius in degrees of the circular region.
Returns
Expand All @@ -160,18 +169,18 @@ def point_inside_circular(lat1, lon1, lat2, lon2, minradius=None, maxradius=None
return True


def to_datetime(value):
def to_datetime(value: str | datetime | date) -> datetime:
"""
Convert a datetime from :class:`str` to :class:`datetime.datetime` in a hard way.
Parameters
----------
value: str
A datetime as a string.
value
A :class:`datetime.datetime` object or a datetime string.
Returns
-------
datetime.datetime
datetime
A datetime as :class:`datetime.datetime`.
Examples
Expand Down Expand Up @@ -213,13 +222,13 @@ def to_datetime(value):
return datetime.strptime(value, strfmt)


def check_cmd_exists(cmd):
def check_cmd_exists(cmd: str) -> bool:
"""
Check if a command exists in PATH and is executable.
Parameters
----------
cmd: str
cmd
Name of the command.
Returns
Expand All @@ -235,7 +244,7 @@ def check_cmd_exists(cmd):
return bool(fullpath)


def check_package_release():
def check_package_release() -> bool:
"""
Check whether HinetPy has a new release.
Expand Down

0 comments on commit 683a84a

Please sign in to comment.