Skip to content

runsascoded/stdlb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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("Python version: {version}\n")

Install

pip install stdlb

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.

Importing seems to take a few milliseconds (on my Macbook Air):

%%time
from stdlb import *
# CPU times: user 914 µs, sys: 397 µs, total: 1.31 ms
# Wall time: 1.6 ms

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

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.