Skip to content

Commit

Permalink
feat(types): Add type hints and code optimizations
Browse files Browse the repository at this point in the history
Python3.8 is almost end-of-life and backwards compatibility
to versions before type hints is not a priority anymore.

This commit is a start and by no means complete or correct
for all instances.

Furthermore I included code optimizations. These mainly regard
raising of exceptions, pytest style tests and some minor
refactoring.
  • Loading branch information
benatouba committed Jul 15, 2024
1 parent c87cad1 commit 3d991fa
Show file tree
Hide file tree
Showing 16 changed files with 3,892 additions and 2,548 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ requires = [
]

[tool.setuptools_scm]
fallback_version = "0.3.8"
fallback_version = "0.3.8"
89 changes: 56 additions & 33 deletions salem/__init__.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
"""
Salem package
"""
from __future__ import division
"""Salem package."""

from os import path
from os import makedirs
import sys
from collections.abc import Callable
from functools import wraps
from pathlib import Path

import pyproj

from .version import __version__


def lazy_property(fn):
"""Decorator that makes a property lazy-evaluated."""

def lazy_property(fn: Callable) -> Callable:
"""Lazy-evaluate a property (Decorator)."""
attr_name = '_lazy_' + fn.__name__

@property
@wraps(fn)
def _lazy_property(self):
def _lazy_property(self: object) -> object:
if not hasattr(self, attr_name):
setattr(self, attr_name, fn(self))
return getattr(self, attr_name)
Expand All @@ -32,44 +28,71 @@ def _lazy_property(self):
wgs84 = pyproj.Proj(proj='latlong', datum='WGS84')

# Path to the cache directory
cache_dir = path.join(path.expanduser('~'), '.salem_cache')
if not path.exists(cache_dir):
makedirs(cache_dir)
download_dir = path.join(cache_dir, 'downloads')
if not path.exists(download_dir):
makedirs(download_dir)
cache_dir = Path.home() / '.salem_cache'
cache_dir.mkdir(exist_ok=True)
download_dir = cache_dir / 'downloads'
download_dir.mkdir(exist_ok=True)

sample_data_gh_commit = '454bf696324000d198f574a1bf5bc56e3e489051'
sample_data_dir = path.join(cache_dir, 'salem-sample-data-' +
sample_data_gh_commit)
sample_data_dir = cache_dir / f'salem-sample-data-{sample_data_gh_commit}'

# python version
python_version = 'py3'
if sys.version_info.major == 2:
python_version = 'py2'

# API
from salem.gis import *
from salem.datasets import *
from salem.sio import read_shapefile, read_shapefile_to_grid, grid_from_dataset
from salem.sio import (open_xr_dataset, open_metum_dataset,
open_wrf_dataset, open_mf_wrf_dataset)
from salem.sio import DataArrayAccessor, DatasetAccessor
from salem.datasets import (
WRF,
EsriITMIX,
GeoDataset,
GeoNetcdf,
GeoTiff,
GoogleCenterMap,
GoogleVisibleMap,
)
from salem.gis import (
Grid,
check_crs,
googlestatic_mercator_grid,
mercator_grid,
proj_is_latlong,
proj_is_same,
proj_to_cartopy,
transform_geometry,
transform_geopandas,
transform_proj,
)
from salem.sio import (
DataArrayAccessor,
DatasetAccessor,
grid_from_dataset,
open_metum_dataset,
open_mf_wrf_dataset,
open_wrf_dataset,
open_xr_dataset,
read_shapefile,
read_shapefile_to_grid,
)
from salem.utils import get_demo_file, reduce

try:
from salem.graphics import get_cmap, DataLevels, Map
from salem.graphics import DataLevels, Map, get_cmap
except ImportError as err:
if 'matplotlib' not in str(err):
raise
if 'matplotlib' not in str(err):
raise

def get_cmap() -> None:
msg = 'requires matplotlib'
raise ImportError(msg)

def get_cmap():
raise ImportError('requires matplotlib')
def DataLevels() -> None:
msg = 'requires matplotlib'
raise ImportError(msg)

def DataLevels():
raise ImportError('requires matplotlib')
def Map() -> None:
msg = 'requires matplotlib'
raise ImportError(msg)

def Map():
raise ImportError('requires matplotlib')

from salem.wrftools import geogrid_simulator
Loading

0 comments on commit 3d991fa

Please sign in to comment.