Skip to main content

An advanced library for safe importing and scoped imports management.

Project description

SafeLib - Import Everything Safe

Safelib is an importer that supports fallback mechanism.

0.6.0 Changelog

  • Fixed several issues
  • Added safelib.valid instead importer.valid
  • Added search_builtins feature
  • Extended exception handling

Example Usage

Using Context Manager (sync/async)

import safelib

from safelib import Import
with Import('sqlalchemy', 'peewee', raises=False, search_builtins=True) as orm:
    from safelib import declarative_base # use traditional import
    Base = orm.declarative_base # use orm to access the declarative_base

    # declarative_base can be found entity or safelib.NotFound
    # because we set raises=False, it will not raise an exception if the entity is not found

    # when we search for `int` with orm.int, it will search first in the builtins module
    # and return the int type if found, or safelib.NotFound if not found.

    # to validate whether an entity is valid, use:
    if safelib.valid(orm.entity):
        do_something_with(orm.entity)

Using Classical Imports

from safelib import _main, typing, _fallback, typing_extensions

from safelib import Protocol, _no_raise

#> Protocol is safelib.NotFound, typing.Protocol or typing_extensions.Protocol

from safelib import _no_raise # catch errors by defaulting missing entities

No-Raise Statement

Import _no_raise sentinel to catch exceptions in current state.

from safelib import _main, typing
from safelib import _no_raise, Protocool
# Protocool is safelib.NotFound, otherwise raises exception

import safelib

with safelib.Import(raises=False) as foo:
    # or use context manager of Import with raises=False
    pass

Reset Statement

Import _reset sentinel to reset current state of safelib.

from safelib import _main, httpx
# after get method returned, state will be restored to initial state
from safelib import get, _reset 
from safelib import Import

async with Import('sqlalchemy', 'peewee') as importer:
    SafeEntity = importer.SafeEntity
    importer.reset_state()

For inquiries, feature request and bug reports, please contact me at contact@tomris.dev

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

safelib-0.6.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

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

safelib-0.6.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file safelib-0.6.0.tar.gz.

File metadata

  • Download URL: safelib-0.6.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.7

File hashes

Hashes for safelib-0.6.0.tar.gz
Algorithm Hash digest
SHA256 22765400fc41ef969baa0c76707fea95bc3f669b4f7434b91e7be45966617f5f
MD5 3365b521448bd255f6e7d71c3af60f55
BLAKE2b-256 a943bfbdd168e387825717cabfb1985000c5330590ca90def4fc0ea330643dd9

See more details on using hashes here.

File details

Details for the file safelib-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: safelib-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.7

File hashes

Hashes for safelib-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e0137cd19d26aaa1e32ab46c952e0ae6255ed8ca26d845bffdf957f1058b1d17
MD5 7ed36ae84ab59458cd1849e80fa0ba84
BLAKE2b-256 e681933f3b745c658cc47c7ec90db1ca188d0a6bb86370e2b5e55bd70570fa07

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