Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor / style: Type hint and style improvements #35

Merged
merged 4 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions python/hypy/hydrolocation/hydrolocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ def realized_nexus(self) -> str:
return self._realized_nexus

@property
def geometry(self) -> Point | Tuple:
def geometry(self) -> Point | tuple[float, float]:
"""
Geometric coordinates of the location as a `Point` or two-Tuple
Geodetic coordinates of the location as a `Point` or tuple of two floats
"""
return self._shape

Expand Down
25 changes: 15 additions & 10 deletions python/hypy/hydrolocation/nwis_location.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Union, Tuple, TYPE_CHECKING
from __future__ import annotations

from typing import Tuple, TYPE_CHECKING
from hydrotools import nwis_client

from hypy.hydrolocation import HydroLocation, HydroLocationType
Expand All @@ -8,21 +10,24 @@
from numpy import datetime64
from datetime import datetime
from shapely.geometry import Point

class NWISLocation(HydroLocation):
"""
An NWIS subclass of HydroLocation
An NWIS subclass of HydroLocation
"""
__slots__ = ("_station_id",)

def __init__(self,
station_id: str,
realized_nexus: str,
shape: Union['Point', Tuple] = None,
shape: Point | Tuple = None,
referenced_position = None, #TODO implement indirect position?
):
"""

Parameters
----------
shape: Union[Point, Tuple]
shape: Point | Tuple
a shapely point geometry or two-tuple defining the coordinates of this location
station_id: str
NWIS station identifier
Expand All @@ -33,19 +38,19 @@ def __init__(self,
"""
super().__init__(realized_nexus, shape, HydroLocationType.hydrometricStation, referenced_position)
self._station_id = station_id
#self._nwis = nwis_client.IVDataService()

@property
def station_id(self) -> str:
"""
NWIS station identifier
NWIS station identifier
"""
return self._station_id

def get_data(self,
start: Union[ str, 'datetime', 'datetime64', 'Timestamp', None ] = None,
end: Union[ str, 'datetime', 'datetime64', 'Timestamp', None ] = None
) -> 'DataFrame':
start: str | datetime | datetime64 | Timestamp | None = None,
end: str | datetime | datetime64 | Timestamp | None = None
) -> DataFrame:
"""
Get observation data from nwis
Get observation data from NWIS
"""
return nwis_client.IVDataService().get(self._station_id, startDT=start, endDT=end)
76 changes: 41 additions & 35 deletions python/hypy/nexus.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,31 @@
from typing import List, Optional, Tuple, TYPE_CHECKING, Union
from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from .catchment import Catchment, Catchments_Collection
from .hydrolocation.hydrolocation import HydroLocation

class Nexus():
class Nexus:
"""
Implementation of the HY Features Nexus concept.
"""

@classmethod
def _convert_collection_to_tuple(cls, collection: 'Catchments_Collection') -> Tuple['Catchment', ...]:
"""
Convenience method to accept a ``Catchments_Collection``, which is a union of several possible types, and to
output a tuple of catchments (or an empty tuple).
Parameters
----------
collection: Catchments_Collection
A collection of catchment objects, which could be objects within certain containers, an empty container, or
a single catchment object itself.
Returns
-------
Tuple['Catchment', ...]
A tuple containing the catchments included directly or in the container parameter object.
"""
if isinstance(collection, list):
return tuple(collection)
elif isinstance(collection, tuple):
return collection
# Assuming type hinting is followed, the only thing this should leave is the single catchment
# TODO: consider whether any cases outside of type hint need to be addressed
else:
return (collection,)

__slots__ = ["_id", "_receiving_catchments", "_contributing_catchments", "_hydro_location"]
__slots__ = ("_id", "_receiving_catchments", "_contributing_catchments", "_hydro_location")

def __init__(self,
nexus_id: str,
hydro_location,
receiving_catchments: 'Catchments_Collection' = tuple(),
contributing_catchments: 'Catchments_Collection' = tuple()):
hydro_location: HydroLocation,
receiving_catchments: Catchments_Collection = tuple(),
contributing_catchments: Catchments_Collection = tuple()):
"""
Set the nexus identity and params

Parameters
----------
nexus_id: str
The identifier string for this nexus
hydro_location: HydroLocation
HydroLocation associated with this nexus
receiving_catchments: Catchments_Collection
The catchment object(s) that receive(s) water from this nexus
contributing_catchments: Catchments_Collection
Expand All @@ -69,23 +49,49 @@ def id(self) -> str:
return self._id

@property
def receiving_catchments (self) -> Tuple['Catchment', ...]:
def receiving_catchments (self) -> tuple[Catchment, ...]:
"""Tuple of Catchment object(s) receiving water from nexus

Returns
-------
Tuple['Catchment', ...]
tuple[Catchment, ...]
Tuple of Catchment object(s) receiving water from nexus
"""
return self._receiving_catchments

@property
def contributing_catchments (self) -> Tuple['Catchment', ...]:
def contributing_catchments (self) -> tuple[Catchment, ...]:
"""Tuple of Catchment object(s) contributing water to nexus

Returns
-------
Tuple['Catchment', ...]
tuple[Catchment, ...]
Tuple of Catchment object(s) contributing water to nexus
"""
return self._contributing_catchments

@staticmethod
def _convert_collection_to_tuple(collection: Catchments_Collection) -> tuple[Catchment, ...]:
"""
Convenience method to accept a ``Catchments_Collection``, which is a union of several possible types, and to
output a tuple of catchments (or an empty tuple).
Parameters
----------
collection: Catchments_Collection
A collection of catchment objects, which could be objects within certain containers, an empty container, or
a single catchment object itself.
Returns
-------
tuple[Catchment, ...]
A tuple containing the catchments included directly or in the container parameter object.
"""
# avoid circular import
from .catchment import Catchment
if isinstance(collection, list):
return tuple(collection)
elif isinstance(collection, tuple):
return collection
elif isinstance(collection, Catchment):
return (collection,)
else:
raise AssertionError("unreachable")
Loading