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.
Metadata
Release files for pewter 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pewter-0.1.0.tar.gz | 3.2 kB | Without provenance | Details for pewter-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pewter-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for pewter-0.1.0-py3-none-any.whl |
Total release size: 6.9 kB
Release files / pewter-0.1.0.tar.gz
| Download URL | pewter-0.1.0.tar.gz |
|---|---|
| Size | 3.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a2fab7b25e14a1d22cf20531bc02e728529c5810f66b1bc2b7b9fbb0f671a375
|
|
BLAKE2b-256 checksum How to use checksums |
c3a26c4a954866e475417d4570f4d53a932cdaf03b0a989bbde0cf9175b621fb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.8.10 Linux/5.4.0-196-generic
|
Release files / pewter-0.1.0-py3-none-any.whl
| Download URL | pewter-0.1.0-py3-none-any.whl |
|---|---|
| Size | 3.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
157ba03d7112e35f9e06301cdb48a72e82298cbca4d3009faedf5b35c65c6667
|
|
BLAKE2b-256 checksum How to use checksums |
c7edfb7f46edda4e40625fbd8eea330c48c3a784a6ea8927b12f62ddf0f403ca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.8.10 Linux/5.4.0-196-generic
|
Security for pewter 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.