Skip to main content

A collection of utlities that can be used in other projects.

Project description

from tramp.as_completed import AsCompleted

Tramp

A collection of useful utilities that can be used in any project.

Installation

pip install tramp

Annotations

A utility for evaluating string annotations with support for forward references when a name cannot be evaluated. Forward references can be evaluated later by calling the forward reference's evaluate method. This is a similar API to PEP 649, although it is not fully compatible and only emulates the behavior of Format.FORWARDREF. Certain things are not possible (at least not without some serious hacking) such as using ForwardRef instances as that would break many annotation types from the typing module. To help with this limitation the tramp.annotations.ForwardRefMeta type overrides the isinstance check to return True when Tramp's version of the ForwardRef class is used in an instance check (isinstance or a match/case). The goal is to implement the most essential parts of the PEP to begin reaping the benefits of forward references now with the least necessary refactoring later.

On Python 3.14 Tramp falls through to the PEP 649 implementation.

from tramp.annotations import get_annotations


class Foo:
    bar: "Bar"


annotations = get_annotations(Foo)  # {'bar': <ForwardRef 'Bar'>}


class Bar:
    pass


annotations["bar"].evaluate()  # <class '__main__.Bar'>

It supports generic types, metadata, function calls/class instantiation, etc.

class Foo:
    bar: "list[int]"
    baz: "Callable[[int], str]"
    qux: "Annotated[int, Bar('baz')]"

As Completed

The AsCompleted type is a wrapper around asyncio.as_completed that adds an async iterator over the results from each task. This simplifies iterating over tasks, eliminating the need to await the next result.

from tramp.as_completed import AsCompleted
...
tasks = [...]
async for result in AsCompleted(*tasks):
    ...

Additionally it is possible to use AsCompleted in the same way that as_completed operates.

for next_result in AsCompleted(*tasks):
    result = await next_result

Containers

A container acts a reference to a changable value.

from tramp import Container

container = Container[int](0)
container.set(1)

print(container.value)  # 1

Modules

Helper functions for working with modules

from tramp import modules
from typing import Any

ns: dict[str, Any] = modules.get_module_namespace("some_module")

Optionals

An optional type that can be used with match statements.

from tramp.optionals import Optional

def foo(x: int) -> Optional[int]:
    if x > 0:
        return Optional.Some(x)
        
    return Optional.Nothing()

result = foo(1)
print(result.value) # 1

result = foo(-1)
print(result.value) # Raises an exception

result = foo(-1)
print(result.value_or(0)) # 0

...

match foo(1):
    case Optional.Some(x):
        print(x)

    case Optional.Nothing():
        print("Nothing")

# Output: 1

match foo(-1):
    case Optional.Some(x):
        print(x)

    case Optional.Nothing():
        print("Nothing")

# Output: Nothing

Results

A result type that can be used with match statements. Works the same as Optionals with an added error property.

from tramp.results import Result

with Result.build() as result:
    result.set(1)

print(result.value) # 1
print(result.error) # None

with Result.build() as result:
    raise Execption("Error")

print(result.value) # Raises an exception
print(result.value_or(0)) # 0
print(result.error) # Exception("Error")

Sentinel

A sentinel value that can be used to represent a unique value. Useful for creating NotSet types. Instantiating any sentinel type will always return the same singleton instance of that type allowing for is checks.

from tramp.sentinels import sentinel

NotSet = sentinel("NotSet")


def foo(x: int | NotSet = NotSet()) -> int:
    if x is NotSet():
        return 0

    return x

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

tramp-0.1.13.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

tramp-0.1.13-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file tramp-0.1.13.tar.gz.

File metadata

  • Download URL: tramp-0.1.13.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.5.0-1025-azure

File hashes

Hashes for tramp-0.1.13.tar.gz
Algorithm Hash digest
SHA256 3aa2a3b862876cd4ea44cc32ddf2619f471a5e557bfd17a675304eab8cd240ec
MD5 d82b2b7df24c11a18cbd3037f2906dbe
BLAKE2b-256 cd4dbcb093bba2361dbb34a38526e2b32325199d2f93dd59a51b0481b8ef19a2

See more details on using hashes here.

File details

Details for the file tramp-0.1.13-py3-none-any.whl.

File metadata

  • Download URL: tramp-0.1.13-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.5.0-1025-azure

File hashes

Hashes for tramp-0.1.13-py3-none-any.whl
Algorithm Hash digest
SHA256 558897411be3384ff67a26c364aabeb5614e26b93619b51b5317b08f42f730e8
MD5 ac55f37bdd318ee9690b805b24d9cc13
BLAKE2b-256 19832c89073d33defbfb97cc517c66d7e7ab1315449db0730f6c613de132aeba

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