mooonpy.tools.file_utils module

class mooonpy.tools.file_utils.Path(string: str | Path)[source]

Bases: str

As computational scientists, half our jobs is file management and manipulation, the Path class contains several aliases for the os.path and glob.glob modules to make processing data easier. All mooonpy functions internally use this class for inputs of files or folders. Relevant strings are converted to path on entering functions

Not sure what class level docs should look like. I’ll fill out the methods for now

Todo

__truediv__ __bool__ __abs__ and __iter__ docstrings in config?

basename() Path[source]

Split Path to filename and extention.

Alias for os.path.basename

Returns:

Path of file

Return type:

Path

Example:
>>> from mooonpy.tools import Path
>>> MyPath = Path('Project/Monomers/DETDA.mol')
>>> print(MyPath.basename())
'DETDA.mol'
dir() Path[source]

Split Path to directory.

Alias for os.path.dirname.

Returns:

Path to directory

Return type:

Path

Example:
>>> from mooonpy.tools import Path
>>> MyPath = Path('Project/Monomers/DETDA.mol')
>>> print(MyPath.dir())
'Project\Monomers'
ext() Path[source]

Split Path to just extention.

Alias for os.path.basename and os.path.splitext.

Returns:

extention as Path

Return type:

Path

Example:
>>> from mooonpy.tools import Path
>>> MyPath = Path('Project/Monomers/DETDA.mol')
>>> print(MyPath.ext())
'.mol'
matches() List[Path][source]

Finds matching paths with a * (asterisk) wildcard character.

Returns:

List of matching Paths

Return type:

List[Path]

Example:
>>> from mooonpy.tools import Path
>>> MyWildcard = Path('*.mol')
>>> print(Path.matches(MyWildcard))
[Path('DETDA.mol'), Path('DEGBF.mol')]
new_ext(ext: str | Path) Path[source]

Replace extension on a Path with a new extension.

Parameters:

ext (str or Path) – new extension including delimeter.

Returns:

replaced Path

Return type:

Path

Example:
>>> from mooonpy.tools import Path
>>> MyPath = Path('Project/Monomers/DETDA.mol')
>>> print(MyPath.new_ext('.data'))
'Project/Monomers/DETDA.data'
open(mode='r', encoding='utf-8')[source]

Open path with smart_open

Parameters:
  • filename (Path or str) – Path to file

  • mode (str) – Open mode, usually ‘r’ or ‘a’

  • encoding (str) – File encoding

Returns:

opened file as object

Return type:

File Object

Example:
>>> from mooonpy.tools import smart_open
>>> MyFileObj = smart_open('Project/Monomers/DETDA.data.gz')
recent(oldest: bool = False) Path | None[source]

Find wildcard matches and return the Path of the most recently modified file.

Parameters:

oldest (bool) – Reverses direction and finds least recently modified file.

Returns:

Path of most recently modified file

Return type:

Path

Example:
>>> from mooonpy.tools import Path
>>> MyWildcard = Path('Template_*.lmpmol')
>>> print(Path.recent())
'Template_1_v10_final_realthistime.lmpmol'
>>> print(Path.recent(oldest=True))
'Template_1.lmpmol'
root() Path[source]

Split Path to filename with no extention.

Alias for os.path.basename and os.path.splitext.

Returns:

Path of filename

Return type:

Path

Example:
>>> from mooonpy.tools import Path
>>> MyPath = Path('Project/Monomers/DETDA.mol')
>>> print(MyPath.root())
'DETDA'
mooonpy.tools.file_utils.smart_open(filename, mode='r', encoding='utf-8')[source]

Open file with appropriate decompression based on extension

Supported extensions: Use substring in filename
  • .gz: Uses gzip module

  • .bz2: Uses bzip2 module

  • .xz: Uses lzma module

  • .lzma: Uses lzma module

  • Other extensions use the builtin open function

Parameters:
  • filename (Path or str) – Path to file

  • mode (str) – Open mode, usually ‘r’ or ‘a’

  • encoding (str) – File encoding

Returns:

opened file as object

Return type:

File Object

Example:
>>> from mooonpy.tools import smart_open
>>> MyFileObj = smart_open('Project/Monomers/DETDA.data.gz')