Skip to main content

Extended Pathlib

Project description

Superpathlib

PyPI version Python version Operating system Coverage

Maximize your productivity using minimal code!

Superpathlib enhances file operations, making them more intuitive and easy to implement. This library extends Python's standard library pathlib module, offering an expansive range of added features without compromising performance.

Usage

from superpathlib import Path

path = Path(filename)

1) Use properties to read & write path content in different formats

  • text
  • byte_content
  • lines
  • yaml
  • json
  • numpy

examples:

path.json = {key: value}

for line in path.lines:
    if interesting(line):
        process(line)

2) Use instance properties to get/set file metadata:

  • get:
    • size: filesize
    • is_root: whether the owner of the file is a root user
    • has_children: whether a path has children
    • number_of_children: number of children in a folder
    • filetype: content type of a file
    • content_hash: a hash of the complete substructure found in a folder
  • get & set:
    • mtime: modified time
    • tag: can be used for alternative ordering or metadata

examples:

path_new.mtime = path_old.mtime

if path.tag != skip_keyword and path.filetype == "video":
    process(path)

3) Use class properties to access commonly used folders:

  • docs
  • assets
  • ..

example:

names_path = Path.assets / 'names'
names = names_path.lines

4) Use additional functionality

  • find(): recursively find all paths under a root that match a condition (extra options available for performance optimization)
  • rmtree(): remove directory recursively
  • copy_to(dest): copy content to dest
  • copy_properties_to(dest): recursively copy path properties (mtime, tag) to all n-level children of dest
  • tempfile(): create temporary file that can be used as context manager
  • unpack(): extract archive(zip, tar, ..) file to desired folder
  • pop_parent(): remove first parent from path in filesystem
  • from_uri(uri): create path object from uri string

examples:

with Path.tempfile() as tmp:
    do_work(logfile=tmp)
    log = tmp.text
process(log)

is_git_root = lambda p: (p / '.git').exists()
for git_path in root.find(is_git_root):
    process_git(git_path)

5) Enhance existing functionality

  • Automatically create parents when writing files, creating new files, renaming files, ..
  • Return default values when path does not exist (e.g. size = 0, lines=[])
  • Support replacing folders instead files only if specified

6) Inherit from superpathlib Path to define your own additional functionality:

example:

import superpathlib

class Path(superpathlib.Path):
    def count_children(self):
        return sum(1 for _ in self.iterdir())

This only works if you inherit from superpathlib Path, not pathlib Path

Installation

pip install superpathlib

or

pip install superpathlib[full]

Install the packages corresponding with the properties you want to use:

  • e.g. PyYaml for yaml property
  • Packages listed in pyproject.toml

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

superpathlib-2.0.0.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

superpathlib-2.0.0-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file superpathlib-2.0.0.tar.gz.

File metadata

  • Download URL: superpathlib-2.0.0.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for superpathlib-2.0.0.tar.gz
Algorithm Hash digest
SHA256 c442b2d6bf29e151b9a61c64253336e44dc2aa90d587fa6621d4cfc2066ab61d
MD5 b5c8f8d93c2210ade4a0d09b4da379f7
BLAKE2b-256 24399d9a0b5237e8c42b9d114fcc5b0317cd5512f1564175d6368152c65cd48b

See more details on using hashes here.

File details

Details for the file superpathlib-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: superpathlib-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for superpathlib-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2dd34b27ef19bc694ed56eae95e60e6528133f38c6822aef502fb1ff02b92387
MD5 ed6a12636bbc248321c3976f7970b968
BLAKE2b-256 39a5c1537ec35054818bbf3524fcc5d4c32b1dba25db693a60d07ad8c3fa09ab

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page