Skip to content

Commit

Permalink
Move mikeio1d.open() into top_level module
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-kipawa committed Dec 12, 2024
1 parent 9b2d16b commit a62efd1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 56 deletions.
37 changes: 2 additions & 35 deletions mikeio1d/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,45 +47,12 @@

from .res1d import Res1D
from .xns11 import Xns11
from .top_level import open

from .various import allow_nested_autocompletion_for_ipython

allow_nested_autocompletion_for_ipython(Res1D)
allow_nested_autocompletion_for_ipython(Xns11)

from .various import open as open_impl


def open(file_name: str | Path, **kwargs) -> Res1D | Xns11:
"""Open a file type supported by MIKE IO 1D file.
Parameters
----------
file_name : str or Path
Path to the file to read.
**kwargs
Additional keyword arguments to pass to the relevant constructor.
See Also
--------
mikeio1d.Res1D
mikeio1d.Xns11
Returns
-------
Res1D or Xns11
The object representing the 1D file.
Examples
--------
>>> import mikeio1d
>>> res = mikeio1d.open("results.res1d")
>>> res.nodes.read()
>>> xs = mikeio1d.open("cross_section.xns11")
>>> xs
"""
return open_impl(file_name, **kwargs)


__all__ = ["Res1D", "Xns11"]
__all__ = ["Res1D", "Xns11", "open"]
52 changes: 52 additions & 0 deletions mikeio1d/top_level.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Module containing top level functions for MIKE IO 1D."""

from __future__ import annotations

from pathlib import Path

from .res1d import Res1D
from .xns11 import Xns11


def open(file_name: str | Path, **kwargs) -> Res1D | Xns11:
"""Open a file type supported by MIKE IO 1D file.
Parameters
----------
file_name : str or Path
Path to the file to read.
**kwargs
Additional keyword arguments to pass to the relevant constructor.
See Also
--------
mikeio1d.Res1D
mikeio1d.Xns11
Returns
-------
Res1D or Xns11
The object representing the 1D file.
Examples
--------
>>> import mikeio1d
>>> res = mikeio1d.open("results.res1d")
>>> res.nodes.read()
>>> xs = mikeio1d.open("cross_section.xns11")
>>> xs
"""
if isinstance(file_name, str):
file_name = Path(file_name)

if not file_name.exists():
raise FileNotFoundError(f"File not found: {file_name}")

suffix = file_name.suffix.lower()
file_name = str(file_name)

if suffix in Res1D.get_supported_file_extensions():
return Res1D(file_name, **kwargs)
elif suffix in Xns11.get_supported_file_extensions():
return Xns11(file_name, **kwargs)
21 changes: 0 additions & 21 deletions mikeio1d/various.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,3 @@ def allow_nested_autocompletion_for_ipython(cls: Type):
from IPython.core import guarded_eval

guarded_eval.EVALUATION_POLICIES["limited"].allowed_getattr.add(cls)


def open(file_name: str | Path, **kwargs) -> Res1D | Xns11:
"""Open a file type supported by MIKE IO 1D file."""
# import here to avoid circular imports
from mikeio1d.res1d import Res1D
from mikeio1d.xns11 import Xns11

if isinstance(file_name, str):
file_name = Path(file_name)

if not file_name.exists():
raise FileNotFoundError(f"File not found: {file_name}")

suffix = file_name.suffix.lower()
file_name = str(file_name)

if suffix in Res1D.get_supported_file_extensions():
return Res1D(file_name, **kwargs)
elif suffix in Xns11.get_supported_file_extensions():
return Xns11(file_name, **kwargs)

0 comments on commit a62efd1

Please sign in to comment.