Skip to main content

Extended Pathlib

Project description

PyPI version

Superpathlib

Maximize your productivity using minimal code!

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

Usage

from plib 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)

condition = lambda p: (p / '.git').exists()
for git_path in root.find(condition):
    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 plib Path to define your own additional functionality:

example:

import plib

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

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

Installation

pip install superpathlib

Install the packages corresponding with the properties you want to use

  • e.g. PyYaml for yaml property
  • Packages listed in requirements.txt

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-1.1.12.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

superpathlib-1.1.12-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for superpathlib-1.1.12.tar.gz
Algorithm Hash digest
SHA256 9186e9dadbdd466a534f788b84fc178fc2566c59902210983b0d04e9b27ac634
MD5 d2523b52acf4f5e38daa12783f694d7f
BLAKE2b-256 896eecacedaa6cf4633db8b2481f9ea85c79219a1289fb765a22b2473c772fb3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for superpathlib-1.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 ddcf59d1ca5222f2cc2bd1f5dfd52ead042dc493d4d3226a73c68c9e17c0d3a7
MD5 a3148b6af4a446e8cab75812c8b2887c
BLAKE2b-256 f063ae135e3ea4f006964625c357d0b942521ab5afeee318716719fff0152a83

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