GithubHelp home page GithubHelp logo

mapleccc / importall Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 379 KB

🚀✨Import Everything!✨🚀 A lightweight and robust library to import every available names from standard libraries to the current namespace, i.e., a Python equivalent to C++'s <bits/stdc++.h>

License: MIT License

Python 99.28% Makefile 0.72%
python python3 import library python-equivalent convenient importall

importall's Introduction

Importall - A Python Equivalent to C++'s <bits/stdc++.h>

License LOC Code style: black pre-commit

Table of Content

Overview

importall is a lightweight and robust library to import every available names from standard libraries to the current namespace, i.e., a Python equivalent to C++'s <bits/stdc++.h>.

It's definitely not intended for serious software engineering situations. It's useful and convenient for interactive consoles, and some niche scenarios, such as competitive programming contests, under which situation saving some typing strokes and hence precious time is highly desirable. Also convenient when just prototyping, testing back-of-envelop thoughts, or tinkering with ideas on playground. Save the time and tedious chores spent on typing all the necessary modules, which could be annoying, boring and typo-prone.

Quick Start

Call the importall() function, with the globals() passed in as argument, then all names are imported to the current namespace.

from importall import importall

importall(globals())

list(combinations("ABCD", 2))
# [("A", "B"), ("A", "C"), ("A", "D"), ("B", "C"), ("B", "D"), ("C", "D")]

nlargest(4, [48, 5, 21, 38, 65, 12, 27, 18])
# [65, 48, 38, 27]

Note that local() should not be passed to importall(), as locals() is intended as readonly per doc.

The importall() function also provides several parameters for finer-grained configuration, enabling more flexible and customizable options.

More than likely, names imported from different standard libraries might collides. The name collision is resolvable by tuning the prioritized and ignore parameters.

Say, a user finds that he wants to use compress from the lzma module instead of that from the zlib module. He could either set higher priority for the lzma module through the prioritized parameter, or ignore the zlib module altogether through the ignore parameter.

importall(globals())

compress.__module__
# "zlib"

importall(globals(), prioritized=["lzma"])
# Alternatives:
# importall(globals(), prioritized={"lzma": 1, "zlib": -1})
# importall(globals(), ignore=["zlib"])

compress.__module__
# "lzma"

If one prefers getting all importable names stored as a variable instead of importing them into the current namespace, so as to avoid cluttering the globals() namespace, there is also a programmatic interface for doing so:

from importall import get_all_symbols

symbol_table = get_all_symbols()

symbol_table["python_implementation"]()
# "CPython"

symbol_table["getrecursionlimit"]()
# 1000

symbol_table["log2"](2)
# 1.0

To recover the globals() and de-import all imported names, use the deimportall() function:

from importall import deimportall, importall

importall(globals())

log2(2)
# 1.0

deimportall(globals())

log2(2)
# NameError

The doc and API of the importall() function:

def importall(
    globals: SymbolTable,
    *,
    protect_builtins: bool = True,
    include_deprecated: bool = False,
    prioritized: Union[Iterable[str], Mapping[str, int]] = (),
    ignore: Iterable[str] = (),
) -> None:
    """
    Import every available names from standard libraries to the current namespace.
    Python equivalent to C++'s <bits/stdc++.h>.

    Name collision is likely. One can resolve name collisions by tuning the `prioritized`
    and/or the `ignore` parameter. Names from the module with higher priority value will
    override names from the module with lower priority value.

    The `globals` parameter accepts a symbol table to populate. Usually the caller passes
    in `globals()`.

    By default, built-in names are protected from overriding. The protection can be switched
    off by setting the `protect_builtins` parameter to `False`.

    By default, deprecated modules and deprecated names are not imported. It is designed
    so because deprecated modules and names hopefully should not be used anymore,
    their presence only for easing the steepness of API changes and providing a progressive
    cross-version migration experience. If you are sure you know what you are doing, override
    the default behavior by setting the `include_deprecated` parameter to `True` (**not recommended**).

    The `prioritized` parameter accepts either an iterable of strings specifying modules
    whose priorities are set to 1, or a mapping object with string keys and integer values,
    specifying respective priority values for corresponding modules. Valid priority value
    is always integer. All modules default to 0 priority values. It's possible to specify
    negative priority value.

    The `ignore` parameter accepts an iterable of strings specifying modules that should
    be skipped and not imported.

    Despite imported, features in the `__future__` module are not enabled, as they are
    not imported in the form of future statements (See the production rule for the
    nonterminal `future_stmt` in https://docs.python.org/3/reference/simple_stmts.html#future-statements).
    """

    ...

Development

$ git clone https://github.com/MapleCCC/importall.git

$ cd importall

# Optionally create a virtual environment for isolation purpose
$ python3 -m virtualenv .venv
$ source .venv/bin/activate

# Install dependenies
$ python3 -m pip install -r requirements.txt
# Install dev dependenies
$ python3 -m pip install -r requirements-dev.txt

# Deploy pre-commit hooks
$ pre-commit install

$ python3 -m pip install -e .

Testing

The importall project uses pytest for unit testing.

# Install test dependenies
$ python3 -m pip install -r requirements-test.txt

$ make test

Miscellaneous

We use the lists maintained by the stdlib-list library instead of that by the isort library or that of sys.stdlib_module_names, for they don't include sub-packages and sub-modules, such as concurrent.futures.

One can compare the two lists:

  1. List maintained by the isort library.

  2. List maintained by the stdlib-list library.

Contribution

Contributions are welcome. Open issues or pull requests.

Other Similar Projects

License

The source code of the importall library is currently licensed under the terms of the MIT license. Feel free to contribute, fork, modify, or redistribute.

importall's People

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.