Skip to content

Commit

Permalink
Convert os.path to pathlib based Paths
Browse files Browse the repository at this point in the history
  • Loading branch information
oerc0122 committed Jan 28, 2025
1 parent df2fb58 commit 5859a20
Show file tree
Hide file tree
Showing 43 changed files with 379 additions and 517 deletions.
34 changes: 21 additions & 13 deletions MDANSE/Src/MDANSE/Chemistry/Databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#

import copy
import os
from typing import Union, ItemsView
from pathlib import Path

import json

Expand All @@ -30,8 +30,8 @@ class _Database(metaclass=Singleton):
Base class for all the databases.
"""

_DEFAULT_DATABASE: str
_USER_DATABASE: str
_DEFAULT_DATABASE: Path
_USER_DATABASE: Path

def __init__(self):
"""
Expand Down Expand Up @@ -65,7 +65,11 @@ def __iter__(self):
for v in self._data.values():
yield copy.deepcopy(v)

def _load(self, user_database: str = None, default_database: str = None) -> None:
def _load(
self,
user_database: Union[Path, str, None] = None,
default_database: Union[Path, str, None] = None,
) -> None:
"""
Load the database. This method should never be called elsewhere than __init__ or unit testing.
Expand All @@ -77,10 +81,14 @@ def _load(self, user_database: str = None, default_database: str = None) -> None
"""
if user_database is None:
user_database = self._USER_DATABASE
else:
user_database = Path(user_database)
if default_database is None:
default_database = self._DEFAULT_DATABASE
else:
default_database = Path(default_database)

if os.path.exists(user_database):
if user_database.exists():
database_path = user_database
else:
database_path = default_database
Expand Down Expand Up @@ -164,10 +172,10 @@ class AtomsDatabase(_Database):
>>> atoms = ATOMS_DATABASE.atoms()
"""

_DEFAULT_DATABASE = os.path.join(os.path.dirname(__file__), "atoms.json")
_DEFAULT_DATABASE = Path(__file__).parent / "atoms.json"

# The user path
_USER_DATABASE = os.path.join(PLATFORM.application_directory(), "atoms.json")
_USER_DATABASE = PLATFORM.application_directory() / "atoms.json"

# The python types supported by the database
_TYPES = {"str": str, "int": int, "float": float, "list": list}
Expand Down Expand Up @@ -616,10 +624,10 @@ class MoleculesDatabaseError(Error):


class MoleculesDatabase(_Database):
_DEFAULT_DATABASE = os.path.join(os.path.dirname(__file__), "molecules.json")
_DEFAULT_DATABASE = Path(__file__).parent / "molecules.json"

# The user path
_USER_DATABASE = os.path.join(PLATFORM.application_directory(), "molecules.json")
_USER_DATABASE = PLATFORM.application_directory() / "molecules.json"

def __getitem__(self, item: str):
"""
Expand Down Expand Up @@ -697,10 +705,10 @@ class NucleotidesDatabaseError(Error):


class NucleotidesDatabase(_Database):
_DEFAULT_DATABASE = os.path.join(os.path.dirname(__file__), "nucleotides.json")
_DEFAULT_DATABASE = Path(__file__).parent / "nucleotides.json"

# The user path
_USER_DATABASE = os.path.join(PLATFORM.application_directory(), "nucleotides.json")
_USER_DATABASE = PLATFORM.application_directory() / "nucleotides.json"

def __getitem__(
self, item: str
Expand Down Expand Up @@ -798,10 +806,10 @@ class ResiduesDatabaseError(Error):


class ResiduesDatabase(_Database):
_DEFAULT_DATABASE = os.path.join(os.path.dirname(__file__), "residues.json")
_DEFAULT_DATABASE = Path(__file__).parent / "residues.json"

# The user path
_USER_DATABASE = os.path.join(PLATFORM.application_directory(), "residues.json")
_USER_DATABASE = PLATFORM.application_directory() / "residues.json"

def __getitem__(
self, item: str
Expand Down
4 changes: 2 additions & 2 deletions MDANSE/Src/MDANSE/Chemistry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
import os
from pathlib import Path
import json

from MDANSE.Chemistry.Databases import (
Expand All @@ -31,5 +31,5 @@

NUCLEOTIDES_DATABASE = NucleotidesDatabase()

with open(os.path.join(os.path.dirname(__file__), "residues_alt_names.json"), "r") as f:
with (Path(__file__).parent / "residues_alt_names.json").open("r") as f:
RESIDUE_ALT_NAMES = json.load(f)
Loading

0 comments on commit 5859a20

Please sign in to comment.