Skip to content

Commit

Permalink
Merge pull request #651 from DHI/collections
Browse files Browse the repository at this point in the history
Use collections.abc
  • Loading branch information
ecomodeller authored Feb 16, 2024
2 parents 2893d48 + f619b4f commit e4f8c2d
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 30 deletions.
2 changes: 1 addition & 1 deletion mikeio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations
from pathlib import Path
from platform import architecture
from typing import Sequence
from collections.abc import Sequence

# PEP0440 compatible formatted version, see:
# https://www.python.org/dev/peps/pep-0440/
Expand Down
3 changes: 2 additions & 1 deletion mikeio/_spectral.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations
from typing import Sequence, Tuple, Literal
from collections.abc import Sequence
from typing import Literal, Tuple
import numpy as np
from matplotlib.axes import Axes
from matplotlib.projections.polar import PolarAxes
Expand Down
9 changes: 6 additions & 3 deletions mikeio/_time.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations
from datetime import datetime
from dataclasses import dataclass
from typing import List, Iterable
from typing import List
from collections.abc import Iterable

import pandas as pd

Expand All @@ -14,7 +15,9 @@ class DateTimeSelector:

def isel(
self,
x: int | Iterable[int] | str | datetime | pd.DatetimeIndex | slice | None = None,
x: (
int | Iterable[int] | str | datetime | pd.DatetimeIndex | slice | None
) = None,
) -> List[int]:
"""Select time steps from a pandas DatetimeIndex
Expand Down Expand Up @@ -64,4 +67,4 @@ def isel(
if isinstance(x, Iterable):
return [self.isel(t)[0] for t in x]

return indices
return indices
5 changes: 3 additions & 2 deletions mikeio/_track.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations
from pathlib import Path
from typing import Callable, Sequence, Tuple
from collections.abc import Sequence
from typing import Callable, Tuple

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -50,7 +51,7 @@ def _extract_track(

times = df.index
coords = df.iloc[:, 0:2].to_numpy(copy=True)

elif isinstance(track, Dataset):
times = track.time
coords = np.zeros(shape=(len(times), 2))
Expand Down
14 changes: 6 additions & 8 deletions mikeio/dataset/_data_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations
from datetime import datetime
import re
from typing import Iterable, Sized, List
from typing import List
from collections.abc import Iterable, Sized

import pandas as pd

Expand All @@ -19,7 +20,10 @@ def _n_selected_timesteps(x: Sized, k: slice | Sized) -> int:
return len(k)


def _get_time_idx_list(time: pd.DatetimeIndex, steps: int | Iterable[int] | str | datetime | pd.DatetimeIndex | slice) -> List[int] | slice:
def _get_time_idx_list(
time: pd.DatetimeIndex,
steps: int | Iterable[int] | str | datetime | pd.DatetimeIndex | slice,
) -> List[int] | slice:
"""Find list of idx in DatetimeIndex"""

# indexing with a slice needs to be handled differently, since slicing returns a view
Expand All @@ -30,9 +34,3 @@ def _get_time_idx_list(time: pd.DatetimeIndex, steps: int | Iterable[int] | str

dts = DateTimeSelector(time)
return dts.isel(steps)






12 changes: 3 additions & 9 deletions mikeio/dataset/_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@
from pathlib import Path
from datetime import datetime
from functools import cached_property
from typing import (
Any,
from collections.abc import (
Iterable,
Sized,
Sequence,
Tuple,
Mapping,
Union,
Sized,
Literal,
TYPE_CHECKING,
overload,
MutableMapping,
Callable,
)
from typing import Any, Union, Literal, TYPE_CHECKING, overload, Callable, Tuple


import numpy as np
Expand Down
8 changes: 3 additions & 5 deletions mikeio/dfs/_dfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import warnings
from abc import abstractmethod
from dataclasses import dataclass
from datetime import datetime
from datetime import datetime, timedelta
from typing import List, Tuple, Sequence
import numpy as np
import pandas as pd
Expand Down Expand Up @@ -705,10 +705,8 @@ def time(self) -> pd.DatetimeIndex:
if not self._is_equidistant:
raise NotImplementedError("Not implemented for non-equidistant files")

# TODO using 'S' works in Python 3.8, but is deprecated, 's' doesn't work in 3.8
return pd.date_range(
start=self.start_time, periods=self.n_timesteps, freq=f"{self.timestep}S"
)
dt = timedelta(seconds=self.timestep)
return pd.date_range(start=self.start_time, periods=self.n_timesteps, freq=dt)

@property
def projection_string(self):
Expand Down
4 changes: 3 additions & 1 deletion mikeio/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from copy import deepcopy
from datetime import datetime, timedelta
from shutil import copyfile
from typing import Iterable, List, Sequence, Tuple, Union
from collections.abc import Iterable, Sequence
from typing import Union, List, Tuple


import numpy as np
import pandas as pd
Expand Down

0 comments on commit e4f8c2d

Please sign in to comment.