Skip to main content

Wildcard-import the Python standard library

Project description

stdlb

Wildcard-import the Python standard library

PyPI badge: "stdlb" library

from stdlb import *

# Most of the standard library is now available:
print(f"Current directory: {getcwd()}")
stderr.write(f"Python version: {version}\n")
print(f"Today: {dt.now()}")
data = {"key": "value"}
dumps(data)  # json.dumps

Features:

Install

pip install stdlb

Coverage

stdlb now includes 79+ stdlib modules, including:

Core utilities: os, sys, pathlib, subprocess, tempfile, shutil, glob, fnmatch

Data structures & algorithms: collections, itertools, heapq, bisect, array, queue

Text processing: re, string, textwrap, difflib, unicodedata

Data formats: json, csv, configparser, pickle, base64, binascii

Math & numbers: math, cmath, decimal, fractions, statistics, random, secrets

Date & time: datetime, time, calendar, timeit

Functional programming: functools, operator, itertools

Type hints: typing, types, dataclasses, enum

Concurrency: threading, asyncio, subprocess, signal

Networking: socket, urllib, http, html, mimetypes

Cryptography: hashlib, hmac, secrets

Compression: zlib, zipfile

And more: logging, warnings, traceback, pprint, platform, locale, etc.

Version-specific modules

  • Python 3.9+: graphlib, zoneinfo
  • Python 3.11+: tomllib

Platform-specific symbols

Some symbols vary by platform, particularly in socket, signal, and os:

  • Socket constants: AF_*, IPPROTO_*, SO_*, etc. (vary by OS)
  • Signal constants: SIG* (vary by OS)
  • Clock constants: CLOCK_* (vary by OS)

The library exports whatever is available on your platform. Snapshot tests are lenient about these platform-specific symbols.

Notes

I've found this especially useful in Jupyter notebooks, where I don't have an easy "add import statements as I add code" setup.

Import time is reasonable (~57ms) for the comprehensive coverage provided. See scripts/benchmark_import.py for detailed measurements.

Collision Resolution

__builtins vs. module members

stdlb avoids overwriting __builtins__ with conflicting module members, e.g.:

  • open vs. os.open
  • compile vs. re.compile
  • pow vs. math.pow
  • copyright vs. sys.copyright
  • BlockingIOError vs. io.BlockingIOError

test.ipynb is executed as part of ci.yml to verify there are no __builtins__ are unexpectedly shadowed.

Module/Members

In a few cases, a top-level standard library module also contains a member with the same name (e.g. datetime, shlex, time). stdlb makes an effort to ensure the module "wins" in this case:

from stdlb import *

datetime  # <module 'datetime' from '$PYTHON_HOME/lib/python3.9/datetime.py'>
shlex     # <module 'shlex' from '$PYTHON_HOME/lib/python3.9/shlex.py'>
time      # <module 'time' (built-in)>

A few names are disambiguated with the most sensible-seeming defaults:

path     # resolves to os.path, not sys.path
join     # os.path.join, not shlex.join
Path     # pathlib.Path, not zipfile.Path
error    # re.error, not zlib.error
compress # itertools.compress, not zlib.compress
repeat   # itertools.repeat, not timeit.repeat

Use scripts/compare_versions.py to compare exports between versions and identify collisions.

Aliases

For convenience, datetime.datetime is also exposed as dt, and a few of its members are exported directly:

dt.now()       # datetime.datetime(2023, 8, 3, 10, 9, 43, 981458)
fromtimestamp  # datetime.datetime.fromtimestamp
fromisoformat  # datetime.datetime.fromisoformat

Custom cached_property

One additional bit of functionality is this custom cached_property decorator, which omits an unnecessary/unserializable lock found in functools.cached_property. cpython#87634 has more info, seems like a fix is coming in Python 3.12.

Development

This project uses uv for development.

# Install dependencies
uv sync

# Run tests
uv run pytest tests/ -v

# Test across multiple Python versions
for v in .venv/3.*/bin/python; do $v scripts/quick_test.py; done

# Regenerate __init__.py (if needed)
python scripts/generate_init.py > src/stdlb/__init__.py

# Update exports snapshot
python scripts/snapshot_exports.py

# Compare versions to check for regressions
python scripts/compare_versions.py v0.0.4 HEAD

Helper Scripts

Code Generation

  • scripts/discover_stdlib.py: Analyze the stdlib and identify useful modules to include
  • scripts/generate_init.py: Generate src/stdlb/__init__.py from configuration
    • Handles version-specific imports, module preservation, collision resolution
    • Configuration in VERSION_REQUIREMENTS, PRESERVE_MODULE, COLLISION_PREFERENCES

Testing & Quality Assurance

  • scripts/quick_test.py: Quick functionality test across Python versions
  • scripts/benchmark_import.py: Measure import time performance
  • scripts/snapshot_exports.py: Generate JSON snapshot of all exported symbols
    • Output: tests/exports_snapshot.json (2127 symbols tracked)
    • Used by tests/test_exports_snapshot.py for regression detection
  • scripts/compare_versions.py: Compare exports between git refs
    # Compare any two versions
    python scripts/compare_versions.py v0.0.4 v0.1.0
    python scripts/compare_versions.py <old-ref> <new-ref>
    
    # Show only FQN changes (collisions)
    python scripts/compare_versions.py v0.0.4 HEAD 2>/dev/null | grep -A 50 "Changed FQN"
    

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

stdlb-0.1.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

stdlb-0.1.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file stdlb-0.1.1.tar.gz.

File metadata

  • Download URL: stdlb-0.1.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for stdlb-0.1.1.tar.gz
Algorithm Hash digest
SHA256 45dbebf9eefa5c4363d5e40e980ad86b25849561f0e33a788d3f7640413a677c
MD5 ab5805eebb031257435931626f4b0c5c
BLAKE2b-256 7fe26efe231933a33bd9d1af2b6d0f3649f199ae797c3865159c55c509995da8

See more details on using hashes here.

File details

Details for the file stdlb-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: stdlb-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for stdlb-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 122b21f17ec10b20e4804898bf7e590607506182696822e698db2c6ea9b92125
MD5 777526536718e2254b04aea94382ee9e
BLAKE2b-256 f0d9b8e183026e24b2911d674c8f12af06564e57d5e9e53a332140ad96517b48

See more details on using hashes here.

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