Skip to main content

A library that provides a simple way to type cast in Python.

Project description

Pewter

Pewter is a simple library for type casting / type coercion. Its purpose is to let users avoid boilerplate type conversion code.

Installation

Install Pewter using pip:

pip install pewter

Usage example

This example will use a simple user-defined class:

from dataclasses import dataclass

@dataclass
class LipschitzQuaternion:
    x: int
    i: int
    j: int
    k: int

Before Pewter can convert objects to different classes, it needs to be told how to. To do this use the add_cast function. In this example we tell Pewter our class can be used as an int if its attributes i, j, k are all zero:

from pewter import add_cast

def quaternion_to_int(value: LipschitzQuaternion) -> int:
    if value.i != 0 or value.j != 0 or value.k != 0:
        raise ValueError()
    return value.x

add_cast(LipschitzQuaternion, int, quaternion_to_int)

Now converting is easy:

from pewter import cast

quarternion = LipschitzQuaternion(3, 0, 0, 0)
# Result is 3
cast(quarternion, int)
# Result is 5
cast(5, int)

Advanced usage

Type checkng

For parameters which support many castable classes, the type signature with MyPy can get awkward. Pewter can simplify this by using generic types:

Without generic types:

from typing import Union
from pewter import add_cast, cast

class LikeThree:
    pass

class LikeFour:
    pass

add_cast(LikeThree, int, lambda value: 3)
add_cast(LikeFour, int, lambda value: 4)

def triple(value: Union[int, LikeThree, LikeFour]) -> int:
    return cast(value, int) * 3

With generic types:

from pewter import Castable, CastableValue, add_cast, cast

class LikeThree(Castable[int]):
    pass

class LikeFour(Castable[int]):
    pass

add_cast(LikeThree, int, lambda value: 3)
add_cast(LikeFour, int, lambda value: 4)

def triple(value: CastableValue[int]) -> int:
    return cast(value, int) * 3

Subclasses and inheritance

Pewter supports inheritance for the original_class, but not for the target_class. To find the appropriate converter method, Pewter looks for a suitable add_cast call. The value passed to cast must be an instance of the original_class in add_cast (respecting inheritance). The target_class in cast and add_cast must match exactly (not using inheritance).

There may be more than one (original_class, target_class) pair that matches for a cast call. This can happen when casts have been defined with add_cast for origin classes where one is a subclass of another. In this case, the origin_class that is the nearest ancestor of the class of the value will be used. Precisely, the first match in the value Method Resolution Order will be used.

Rationale

Type conversion using a constructor is a common and effective idiom in Python. For example the int constructor: int("2"). A problem can arise when it is not possible to modify the constructor to allow a new type, such as when the class is defined in a different project. Pewter provides functionality similar to to_str, but it can be used for any class.

Pewter supports inheritance in the original_class, but not in the target_class for efficiency and simplicity. Pewter is intended to reduce boilerplate when passing values, which is why it can accept subclasses in the original_class. The caller does not need to know that Pewter is being used. The method using cast is always aware of Pewter, and can make sure to choose the precise class it wants. If appropriate casts are available, but do not give the exact class desired, the method can implement a conversion, which would not require code changes for any number of supported castable classes.

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

pewter-0.1.0.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

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

pewter-0.1.0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file pewter-0.1.0.tar.gz.

File metadata

  • Download URL: pewter-0.1.0.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.8.10 Linux/5.4.0-196-generic

File hashes

Hashes for pewter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a2fab7b25e14a1d22cf20531bc02e728529c5810f66b1bc2b7b9fbb0f671a375
MD5 30062814dc54b2a7858ed80b1109dd50
BLAKE2b-256 c3a26c4a954866e475417d4570f4d53a932cdaf03b0a989bbde0cf9175b621fb

See more details on using hashes here.

File details

Details for the file pewter-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pewter-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.8.10 Linux/5.4.0-196-generic

File hashes

Hashes for pewter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 157ba03d7112e35f9e06301cdb48a72e82298cbca4d3009faedf5b35c65c6667
MD5 9d242a3a1a06f6c64b888040c5891743
BLAKE2b-256 c7edfb7f46edda4e40625fbd8eea330c48c3a784a6ea8927b12f62ddf0f403ca

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