Skip to main content

Meaningful and safe wrapping types.

Project description

wraps

License Version Downloads Discord

Documentation Check Test Coverage

Meaningful and safe wrapping types.

Installing

Python 3.8 or above is required.

pip

Installing the library with pip is quite simple:

$ pip install wraps

Alternatively, the library can be installed from the source:

$ git clone https://github.com/nekitdev/wraps.git
$ cd wraps
$ python -m pip install .

poetry

You can add wraps as a dependency with the following command:

$ poetry add wraps

Or by directly specifying it in the configuration like so:

[tool.poetry.dependencies]
wraps = "^0.12.0"

Alternatively, you can add it directly from the source:

[tool.poetry.dependencies.wraps]
git = "https://github.com/nekitdev/wraps.git"

Examples

Option

Option[T] type represents an optional value: every option is either Some[T] and contains a value, or Null, and does not.

Here is an example of using wrap_option to catch any errors:

from typing import List, TypeVar
from wraps import wrap_option

T = TypeVar("T", covariant=True)


class Array(List[T]):
    @wrap_option
    def get(self, index: int) -> T:
        return self[index]


array = Array([1, 2, 3])

print(array.get(0).unwrap())  # 1
print(array.get(5).unwrap_or(0))  # 0

Result

Result[T, E] is the type used for returning and propagating errors. It has two variants, Ok[T], representing success and containing a value, and Error[E], representing error and containing an error value.

from enum import Enum

from wraps import Error, Ok, Result


class DivideError(Enum):
    DIVISION_BY_ZERO = "division by zero"


def divide(numerator: float, denominator: float) -> Result[float, DivideError]:
    return Ok(numerator / denominator) if denominator else Error(DivideError.DIVISION_BY_ZERO)

Early Return

Early return functionality (like the question-mark (?) operator in Rust) is implemented via early methods (for both Option[T] and Result[T, E] types) combined with the @early_option and @early_result decorators respectively.

from wraps import Option, early_option, wrap_option_on


@wrap_option_on(ValueError)
def parse(string: str) -> float:
    return float(string)


@wrap_option_on(ZeroDivisionError)
def divide(numerator: float, denominator: float) -> float:
    return numerator / denominator


@early_option
def function(x: str, y: str) -> Option[float]:
    return divide(parse(x).early(), parse(y).early())

Documentation

You can find the documentation here.

Support

If you need support with the library, you can send us an email or refer to the official Discord server.

Changelog

You can find the changelog here.

Security Policy

You can find the Security Policy of wraps here.

Contributing

If you are interested in contributing to wraps, make sure to take a look at the Contributing Guide, as well as the Code of Conduct.

License

wraps is licensed under the MIT License terms. See License for details.

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

wraps-0.12.0.tar.gz (31.2 kB view details)

Uploaded Source

Built Distribution

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

wraps-0.12.0-py3-none-any.whl (39.3 kB view details)

Uploaded Python 3

File details

Details for the file wraps-0.12.0.tar.gz.

File metadata

  • Download URL: wraps-0.12.0.tar.gz
  • Upload date:
  • Size: 31.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.10.12 Linux/6.5.0-1018-azure

File hashes

Hashes for wraps-0.12.0.tar.gz
Algorithm Hash digest
SHA256 354d84d04c9cc91842092ccee6584febe5f37af2cc9eccb6ab2446778826dd74
MD5 dd158ce023cd3ea82586e51b844642ba
BLAKE2b-256 8b9adaf15d46c96d53c0419aa947dc1828a39dae6918ff452669339114ade511

See more details on using hashes here.

File details

Details for the file wraps-0.12.0-py3-none-any.whl.

File metadata

  • Download URL: wraps-0.12.0-py3-none-any.whl
  • Upload date:
  • Size: 39.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.10.12 Linux/6.5.0-1018-azure

File hashes

Hashes for wraps-0.12.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0c10928dffefbd935cb3fd4acf9ac34a9ee2ed91f90dc8c7754039a5ed81b92c
MD5 56778fd517d15ba9974261b1d970ba36
BLAKE2b-256 8a0298e504ae742eb53868505f786e2be78b611a830ae466e0a7e81aea7c7558

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