A library jam for Python.
Project description
libjam
A library jam for Python.
Installing
libjam is available on PyPI, and can be installed using pip.
pip install libjam
To install the latest bleeding edge:
pip install git+https://github.com/philippkosarev/libjam.git
Modules
Captain
Makes creating command line interfaces easy.
Example CLI project:
example.py:
#! /usr/bin/env python3
from libjam import Captain
def shout(text: str):
'Shouts the given text back'
if options.get('world'):
text += ' world'
print(text + '!')
captain = Captain(shout, program='shout')
captain.add_option(
'world', ['world', 'w'],
"Adds ' world' before the exclamation mark",
)
global options
args, options = captain.parse()
shout(*args)
Usage:
$ ./example.py
shout: missing argument <TEXT>
$ ./example.py Hello
Hello!
$ ./example.py Hello --world
Hello world!
$ ./example.py --help
Usage:
shout [OPTION]... <TEXT>
Description:
Shouts the given text back.
Options:
-w, --world - Adds ' world' before the exclamation mark.
-h, --help - Prints this page.
Drawer
Responsible for file operations. Accepts / as the file separator regardless the operating system.
Example CLI for calculating filesizes
mass.py:
#! /usr/bin/env python3
from libjam import Captain, drawer, typewriter
import sys
def print_progress(todo, done):
typewriter.print_progress('Calculating file size', todo, done)
def mass(*paths):
'Prints human-readable size of files'
if len(paths) == 0:
paths = ['./']
n_paths = len(paths)
results = {}
for i, path in enumerate(paths):
if n_paths > 1:
print_progress(i+1, n_paths)
if not drawer.exists(path):
typewriter.clear_lines(0)
print(f'{path}: No such file or directory.', file=sys.stderr)
continue
size = drawer.get_filesize(path)
results[path] = size
results = dict(sorted(results.items(), key=lambda item: item[1], reverse=True))
typewriter.clear_lines(0)
for file in results:
size = results.get(file)
size, units, _ = drawer.get_readable_filesize(size)
size = round(size, 1)
print(f'{file}: {size} {units.upper()}')
captain = Captain(mass)
def main():
args = captain.parse()
try:
return mass(*args)
except KeyboardInterrupt:
print()
sys.exit(1)
if __name__ == '__main__':
sys.exit(main())
Usage:
$ ./mass.py
./: 100.9 MB
$ ./mass.py downloads pictures videos
videos: 34.7 GB
downloads: 12.6 GB
pictures: 2.2 GB
$ ./mass.py -h
Synopsis:
mass.py [OPTION]... [PATHS]...
Description:
Prints human-readable size of files.
Options:
-h, --help - Prints this page.
Typewriter
Transforms text and prints to the terminal.
Notebook
Allows reading and writing .toml, .ini and .json files and provides an application configuration management system with the Notebook.Ledger class.
Example of an app configured through Notebook.Ledger:
# Imports
from libjam import notebook, drawer
# Defining default values
default_downloads_dir = drawer.absolute_path('~/Downloads')
if not drawer.is_folder(default_downloads_dir):
default_downloads_dir = None
default_values = {
'downloads-directory': default_downloads_dir,
}
# Config template
template = '''\
# An override for the default downloads directory
# downloads-directory = ''
'''
# Initialising config
ledger = notebook.Ledger('download-manager')
config_obj = ledger.init_config('config', default_values, template)
config_dict = config_obj.read()
# Checking values
downloads_dir = config_dict.get('downloads-directory')
if downloads_dir is None:
config_obj.on_error(
"Could not automatically find an existing Downloads directory. "
"Please specify 'downloads-directory' in the configuration manually."
)
if not drawer.exists(downloads_dir):
config_obj.on_error("The specified 'downloads-directory' does not exist")
download_manager.py:
#! /usr/bin/env python3
# Imports
import requests
# Getting downloads_dir from config
from config import downloads_dir
# The rest of the program...
Example output:
$ ./download_manager.py
Configuration error(s):
/home/philipp/.config/download-manager/config.toml:
- Could not automatically find an existing Downloads directory. Please specify 'downloads-directory' in the configuration manually.
Clipboard
Provides a few list operations such as deduplicate.
Flashcard
Provides the prompt_yn function.
Cloud
Provides the download function.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file libjam-0.1.9.tar.gz.
File metadata
- Download URL: libjam-0.1.9.tar.gz
- Upload date:
- Size: 24.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
246315d14b8dde8c1ea0cc6a2fc3febc396257868c2a3cc5938d357aedc24437
|
|
| MD5 |
6500f2c88d3d73d03750c33f323d00ef
|
|
| BLAKE2b-256 |
85bc45d317ef5ee5bc067c3c89cf2e58cf46fe888e799d60b3ea025149336d35
|
File details
Details for the file libjam-0.1.9-py3-none-any.whl.
File metadata
- Download URL: libjam-0.1.9-py3-none-any.whl
- Upload date:
- Size: 25.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
81bcbbeb2b281f698fc35cabd712829dac9bce227991ec36b36d4853e25f475d
|
|
| MD5 |
27421b5e4745a15671c52711e4a1518c
|
|
| BLAKE2b-256 |
bfd9b30a28af92963ef89bba95744009caf351f5e45fb1b3ce97d3a541262f67
|