Skip to main content

A lightweight Python package for easy reading and writing of JSON, JSONL, and Pickle files.

Project description

whywhytools

whywhytools is a lightweight Python package for reading and writing JSON, JSON Lines (.jsonl), and Pickle files. It provides simple and intuitive APIs for handling these common data formats.

Installation

The package can be installed via pip:

pip install whywhytools

For extended functionalities, optional dependencies are available.

To install dependencies for specific features:

pip install "whywhytools[torch]"
pip install "whywhytools[safetensors]"

To install all optional dependencies simultaneously:

pip install "whywhytools[all]"

Quickstart

JSON Lines (.jsonl)

Handle .jsonl files, supporting read, write, and append operations.

Write JSONL File
from whywhytools import write_jsonl

data = [
    {'id': 'data-1', 'text': 'hello world'},
    {'id': 'data-2', 'text': 'whywhytools is awesome'}
]

# Write to file
write_jsonl(data, 'output.jsonl') # [INFO] save to output.jsonl

# If file exists
write_jsonl(data, 'output.jsonl') # [INFO] output.jsonl already exists.

# Force overwrite existing file
write_jsonl(data, 'output.jsonl', force=True) # [INFO] save to output.jsonl

# Silent mode (not print any message)
write_jsonl(data, 'output.jsonl', force=True, silent=True)
Append to JSONL File
from whywhytools import append_jsonl

new_data = [{'id': 'data-3', 'text': 'new line'}]
append_jsonl(new_data, 'output.jsonl')
Read JSONL File
from whywhytools import read_jsonl

data = read_jsonl('output.jsonl')
print(data)
# [{'id': 'data-1', 'text': 'hello world'}, {'id': 'data-2', 'text': 'whywhytools is awesome'}, {'id': 'data-3', 'text': 'new line'}]

JSON (.json)

Handle standard .json files.

Write JSON File
from whywhytools import write_json

data = {'project': 'whywhytools', 'version': '0.1.0'}

# Write to file
write_json(data, 'config.json')
Read JSON File
from whywhytools import read_json

config = read_json('config.json')
print(config)

Pickle (.pkl)

Handle Python's pickle serialization format.

Save Pickle File
from whywhytools import save_pickle

model_data = {'weights': [0.1, 0.5, 0.9], 'bias': 0.01}
save_pickle(model_data, 'model.pkl')
Load Pickle File
from whywhytools import load_pickle

data = load_pickle('model.pkl')
print(data)

Plain Text (.txt or general text files)

Handle plain text files, supporting direct string reading or line-by-line list loading, as well as write and append operations.

Write Text File
from whywhytools import write_file

lines = [
    "First line of the text file.",
    "Second line is here."
]

# Write a list of strings to a file
write_file(lines, "document.txt")

# Write a single string
write_file("Just one line here.", "single_doc.txt")
Append to Text File
from whywhytools import append_file

# Append single or multiple lines
append_file("This is a newly appended line.", "document.txt")
Read Text File
from whywhytools import read_file

# Read entire file as a single string
content = read_file("document.txt")
print(content)

# Read file line by line into a list
lines = read_file("document.txt", lines=True)
print(lines)

PyTorch (.pt or .pth)

Handle PyTorch files. Compared to the original torch.save and torch.load, whywhytools provides built-in path type checking, automatic parent directory creation, and safety guards like force argument to prevent accidental overwrites.

Save PyTorch File
import torch
from whywhytools import save_pt

model_data = {'weights': torch.tensor([0.1, 0.5, 0.9])}
save_pt(model_data, 'model.pt')
Load PyTorch File
from whywhytools import load_pt

data = load_pt('model.pt')
print(data)

# Load safely with weights_only=True
safe_data = load_pt('model.pt', weights_only=True)

License

MIT

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

whywhytools-1.1.0.tar.gz (74.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

whywhytools-1.1.0-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file whywhytools-1.1.0.tar.gz.

File metadata

  • Download URL: whywhytools-1.1.0.tar.gz
  • Upload date:
  • Size: 74.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for whywhytools-1.1.0.tar.gz
Algorithm Hash digest
SHA256 f03abd51e020a336966f0c3f257e71786db398e8a94fdafc2ecc972a25745984
MD5 74c338a0b804a63b2b0da3cf5b5a9e5d
BLAKE2b-256 6518d646085f9a3be20b83e81bc912df63e0331a339f84b56b8a0270301f2b4b

See more details on using hashes here.

Provenance

The following attestation bundles were made for whywhytools-1.1.0.tar.gz:

Publisher: publish_to_pypi.yaml on whywhybug/whywhytools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file whywhytools-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: whywhytools-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for whywhytools-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 99f2de412a6d9ceb2dd3aefb3d0d0f700324620a33907f74ca5c65d62ec7ced9
MD5 f21e65e2998a3e4f56deef8a12c1d14a
BLAKE2b-256 08034f7d5d0b7125dda45aae2ae69c39a7611932b21a4c5675693cf0347049a2

See more details on using hashes here.

Provenance

The following attestation bundles were made for whywhytools-1.1.0-py3-none-any.whl:

Publisher: publish_to_pypi.yaml on whywhybug/whywhytools

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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