Skip to main content

Extends Python with useful features.

Project description

🧰 Toolbox

Toolbox is a set of tools that expands on the Python Standard Library.

Installing

pip install toolbox

Documentation

Documentation can be found here. PDF version of docs can be found here.

Tools

Toolbox follows the same pattern as of the Python Standard Library (PSL) which means that all tools are found inside package names that corresponds to that of the PSL (e.g. asyncio, collections, etc.) with only one exception (config).

The following packages and tools are included:

Code Examples

Check out documentation for function definitions and more details.

asyncio

async to_thread

Asynchronously run function func in a separate thread.

from toolbox import to_thread
import asyncio
import time

def func():
    time.sleep(2)
    return "Hello world"

asyncio main():
    await to_thread(func)

asyncio.run(main())

awaitable

Decorator that converts a synchronous function into an asynchronous function by leveraging to_thread and sending function to a new thread on execution.

from toolbox import awaitable
import asyncio
import time

@awaitable
def func():
    time.sleep(2)
    return "Hello world"

async def main():
    await func()

asyncio.run(func())

builtins

classproperty

Combines a property and a classmethod into one, creating a class property. Allows access to computed class attributes.

from toolbox import classproperty

class Animal:
    @classproperty
    def dog(cls):
        return "whoof!"

print(Animal.dog) # >>> 'whoof!'

collections

BidirectionalDict

Dictionary with two-way capabilities.

from toolbox import BidirectionalDict

d = BidirectionalDict({"hello": "world"})
print(d) # >>> {'hello': 'world', 'world': 'hello'}

ObjectDict

Dictionary that can be accessed as though it was an object.

from toolbox import ObjectDict

d = ObjectDict({"hello": "world"})
print(d.hello) # >>> 'world'

OverloadedDict

Dictionary that can be added or subtracted.

from toolbox import OverloadedDict

d1 = OverloadedDict({"hello": "world"})
d2 = OverloadedDict({"ola": "mundo"})

d1 += d2
print(d1) # >>> {'hello': 'world', 'ola': 'mundo'}

d1 -= d2
print(d1) # >>> {'hello': 'world'}

nestednamedtuple

nt = nestednamedtuple({"hello": {"ola": "mundo"}})
print(nt) # >>> namedtupled(hello=namedtupled(ola='mundo'))

fdict

d = {"hello": "world"}
nt = nestednamedtuple({"forced": fdict(d), "notforced": d})

print(nt.notforced)    # >>> namedtupled(hello='world')
print(nt.forced)       # >>> {'hello': 'world'}

config

make_config

Creates global configuration.

from toolbox import make_config

make_config(hello="world")

conf

Access global configuration as a nestednamedtuple.

from toolbox import conf

print(conf().hello) # >>> 'world'

config

Access global configuration as a dictionary.

from toolbox import config

print(config()['hello']) # >>> 'world'

functools

timeout

Wait for time before quitting func run and returning None.

from toolbox import timeout

@timeout(seconds=5)
def func():
    time.wait(15)

func()

experimental

asyncdispatch

Decorator for adding dispatch functionality between async and sync functions.

from toolbox import asyncdispatch
import asyncio

@asyncdispatch
def func():
    return "sync"

@func.register
async def _():
    return "async"

async def main():
    print(func())          # >>> sync
    print(await func())    # >>> async

asyncio.run(main())

string

Comes out of the box with built-in ANSI formats that allows one to do the following:

from toolbox import bold, red

print(red("This text is red!"))
print(bold("This text is bolded!"))

Check documentation here for further information on all built-in formats.

Format

Persistent ANSI format container that allows custom ANSI code.

from toolbox import Format

bold = Format(code=1)
print(bold("hello world"))

Style

Persistent ANSI format container that allows custom ANSI code.

from toolbox import Style, red, bold

error = Style(red, bold)
print(error("This is red & bolded error."))

supports_color

Returns bool that indicates whether or not the users terminal supports color.

from toolbox import supports_color

print(supports_color())

strip_ansi

Removes ANSI codes from string.

from toolbox import strip_ansi

print(strip_ansi("\x1b[1mhello world\x1b[0m")) # >>> hello world

textwrap

unindent

Unident triple quotes and removes any white spaces before or after text.

from toolbox import unident

def test():
    return unindent(
        '''
        hello world
        this is a test
        of this functionality
        '''
    )

print(test()) 
# >>> hello world
# >>> this is a test
# >>> of this functionality

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

toolbox-1.1.1.tar.gz (22.6 kB view hashes)

Uploaded Source

Built Distribution

toolbox-1.1.1-py3-none-any.whl (16.2 kB view hashes)

Uploaded Python 3

Supported by

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