Package#

the contents of the pathchain package.

Concept#

A dropin replacement of pathlib.Path to be chained together. Inspired by the xarray.Dataset.pipe() mechanism.

Example

from pathchain import Path

path_file = Path.home()\
            .joinpath("test")\
            .mkdir(exist_ok)\
            .touch()

Reference#

class pathchain.Path(*args, **kwargs)#

Subclass of pathlib.Path that adds method chaining support. Can be used as a drop-in replacement

mkdir(mode=511, parents=False, exist_ok=False)#

Create a new directory at this path and return self for chaining. Wraps pathlib.Path.mkdir().

rmdir()#

Remove this directory and return self for chaining. Wraps pathlib.Path.rmdir().

chmod(mode)#

Change the mode of this path and return self for chaining. Wraps pathlib.Path.chmod().

touch(mode=438, exist_ok=True)#

Create a new file at this path (if it does not exist) and return self for chaining. Wraps pathlib.Path.touch().

rename(target)#

Rename this path to the given target and return a new Path instance. Wraps pathlib.Path.rename().

replace(target)#

Rename this path to the given target, overwriting if necessary, and return a new Path instance. Wraps pathlib.Path.replace().

Create a symbolic link pointing to the target and return self for chaining. Wraps pathlib.Path.symlink_to().

Create a hard link pointing to the target and return self for chaining. Wraps pathlib.Path.hardlink_to().

Remove this file or symbolic link and return self for chaining. Wraps pathlib.Path.unlink().

write_text(data, encoding=None, errors=None, newline=None)#

Write string data to this file and return self for chaining. Wraps pathlib.Path.write_text().

Warning

Unlike pathlib.Path.write_text(), this method does not return the number of characters written. It returns self to support method chaining.

write_bytes(data)#

Write bytes to this file and return self for chaining. Wraps pathlib.Path.write_bytes().

Warning

Unlike pathlib.Path.write_bytes(), this method does not return the number of characters written. It returns self to support method chaining.

assert_exists()#

Assert that this path exists and return self for chaining.

Returns:

self

Raises:

AssertionError – if the path does not exist

pipe(func: Callable, *args, **kwargs)#

Apply a function to this Path object and return the result. This method enables a functional programming style similar to pandas DataFrame.pipe(), allowing you to chain operations on Path objects by passing them through custom functions.

Parameters:
  • func – A callable that accepts this Path object as its first argument

  • args – Positional arguments to pass to func after the Path object

  • kwargs – Keyword arguments to pass to func

Returns:

The result of applying func to this Path object