ExType is a Python package that allows you to externally extend types.
Project description
ExType
Extype stands for extensible types, a Python package that enables extending types.
Installation
pip install extype
Alternatively, you can install through git (make sure to have pip 22.0 or higher):
python -m pip install --upgrade pip
pip install git+https://github.com/xpodev/extype/
Usage
First, in your Python code, import the package:
import extype
Then, you can use the built-in extensions for the builtin types. In order to apply these extensions,
import the extend_all
module from the extype.builtin_extensions
package:
from extype.builtin_extensions import extend_all
This will apply the extensions to all builtins we support, as a side-effect of the import (to extend only some builtins, you can use the dedicated modules, see below).
Sometimes you don't want to apply all the extensions that we provide, but only for some specific type(s).
Say, for example, we only want to apply the provided extensions for list
. We'll need to manually
apply them like so:
from extype.builtin_extensions import list_ext
list_ext.extend()
Note: All built-in extension modules have an
extend
function which will apply the extensions in the module to the relevant type.
Currently, we provide the following extensions:
file | extended types |
---|---|
dict_ext.py | dict_keys, dict_values, dict_items |
float_ext.py | float |
function_ext.py | FunctionType, LambdaType |
int_ext.py | int |
list_ext.py | list |
seq_ext.py | map, filter, range, zip |
str_ext.py | to_int, to_float |
Then you can use these extensions. Here's an example of using the list.map
extension:
print([1, 2, 3].map(lambda x: x + 1)) # [2, 3, 4]
There's a list of all the built-in extensions here
Creating your own extensions
You can create your own extension methods, with which you can extend any type you want! (not only builtins)
For example, let's make our own tofloat
function in the int
type.
What we want to have at the end is:
x = 10
print(isinstance(x.tofloat(), float)) # True
First, we'll need some tools:
from extype import extension, extend_type_with
Next, we'll define our class which will hold the extension method. Note that this class will not get instantiated. It is also recommended to make this class inherit the type you want to extend, so you get better typing support.
class IntExtension(int): # inheriting `int` for typing
@extension # marks this method to be added as an extension
def tofloat(self): # self will be of the same type we extend, which, in this case, is `int`
return float(self) # convert the int to float and return the result
After we create the class which will contain the extension methods, we need to apply them to the types we want to extend:
extend_type_with(int, IntExtension)
Now, we can run the code from above:
x = 10
print(isinstance(x.tofloat(), float)) # True
We can also apply multiple extensions to the same type or even the same extension to multiple types.
Only methods marked with @extension
will be added as extension methods.
Note:
Extending a type will extend it in all modules, not just the one that called the extend_type_with
,
so make sure you don't override an existing function, unless, of course, it is what you want.
Features
- Exteranlly extend type via another type
- Basic support for magic method extensions
- Number protocol
- Mapping protocol
- Sequence protocol
- Add support for reverse methods (e.g.
__radd__
) - Make this features/todo list look nicer
- Add support for the rich comparison function
Maintainers
Build & Installation
We use Hatch to manage the build environment, and mesonpy to build the package.
Note: Currently, we use unreleased mesonpy features, so we install it from git.
First, install Hatch: https://hatch.pypa.io/latest/install/. We recommend using pipx.
After you've installed Hatch, you can build the package with the following command:
hatch run install_editable
With this, you can start using the package in your code. Spawn shell within the build environment:
hatch shell
It'll rebuild the package every time you import it, so you can test your changes. If you don't want to rebuild the package every time you import it, you can install it with:
hatch run install
But note that any changes you make won't be reflected in the installed package.
To build the wheel, you can use:
hatch run dist:build
This will build the wheel for all python versions, and put it in the dist
folder.
Testing
To run tests for all python versions, run:
hatch run dist:test
To run tests for a specific python version, run:
hatch run +py=39 dist:test
Both commands will build, install the package into an isolated environment, and run the tests in it.
Built-in Extensions
Note: All of the following
list
extensions also exist ondict_keys
,dict_values
anddict_items
.
list.all(self: List[T], fn: Callable[[T], bool] = bool) -> bool
Returns true if all elements, mapped through the given fn
, are True
.
list.any(self: List[T], fn: Callable[[T], bool] = bool) -> bool
Returns true if any of the elements, mapped through the given fn
, is True
.
list.map(self: List[T], fn: Callable[[T], U]) -> List[U]
Returns a new list whose elements are the result of applying the given function on each element in the original list.
list.reduce(self: List[T], fn: Callable[[T, T], T]) -> T
Reduces the list to a single value, using the given function as the reduction (combination) function.
Raises TypeError
if the list is empty.
list.reduce(self: List[T], fn: Callable[[U, T], U], initial_value: U) -> U
Reduces the list to a single value, using the given function as the reduction (combination) function and the initial value.
list.filter(self: List[T], fn: Callable[[T], bool]) -> List[T]
Returns a new list containing all the elements that match the given predicate fn
.
list.first(self: List[T]) -> T, raise IndexError
Returns the first element in the list, or raises an IndexError
if the list is empty.
list.last(self: List[T]) -> T, raise IndexError
Returns the last element in the list, or raises IndexError
if the list is empty.
float.round(self: float) -> int
Rounds the floating point number to the nearest integer.
float.round(self: float, ndigits: int) -> int | float
Round the floating point number to the nearest float with ndigits
fraction digits.
# function @ functioin
function.__matmul__(self: Callable[[T], U], other: Callable[..., T]) -> Callable[..., U]
Compose 2 functions such that doing (foo @ bar)(*args, **kwargs)
will have the same result as calling foo(bar(*args, **kwargs))
.
int.hex(self: int) -> str
Returns the hexadecimal representation of the integer.
int.oct(self: int) -> str
Returns the octal representation of the integer.
int.bin(self: int) -> str
Returns the binary representation of the integer.
str.to_int(self: str, base: int = 10, default: T = ...) -> int | T
Converts the given string to an int with the given base. If it can't be
converted and default
is given, it is returned. Otherwise, a ValueError
is thrown.
str.to_float(self: str, default: T = ...) -> float | T
Converts the given string to a float. If it can't be
converted and default
is given, it is returned. Otherwise, a ValueError
is thrown.
- The following extensions are valid for
map
,filter
,range
andzip
.tolist(self: Iterable[T]) -> List[T]
Exhausts the iterble and creates a list from it.
.map(self: Iterable[T], fn: Callable[[T], U]) -> Iterable[U]
Maps the iterable with the given function to create a new iterable.
This does not iterates through the original iterable.
.filter(self: Iterable[T], fn: Callable[[T], bool]) -> Iterable[T]
Filters the iterable with the given function as the predicate function.
This does not iterates through the original iterable.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for extype-1.1.1-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec7d01bc389ac67bcc0fd90b5864c510c776d8fc7048f4aa8d5f66c39e7ddd00 |
|
MD5 | f75015d2d55e3d9ef833eb6602760ead |
|
BLAKE2b-256 | a9fa0de7844e2937d291345e3bcb1c2dcebd0ffec93234dbd3d0cd5a8f5b8f43 |
Hashes for extype-1.1.1-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57bafc804e1345d2fa5551f38dcaf1c1664860297385f6ef2b2fc545dab2a218 |
|
MD5 | 429426af26ae12a353eb3efef19cdadb |
|
BLAKE2b-256 | 6b73855ffde61f9a166b7e3c3eb044ef81b1cd2c691239e32daaf980ca954829 |
Hashes for extype-1.1.1-cp311-cp311-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd6e4b85e2f8c16148caade006a21bce95d85a0847307ea55862f142eebf9299 |
|
MD5 | 6124238d669545e620d0c867ad7ffba8 |
|
BLAKE2b-256 | 45a3d7ccae65948f34691d6bf3fe9cec52a78e0860d21e4a400d23f255d0070d |
Hashes for extype-1.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 98004161743c05ef5335189abeb1ebefcefb7525fef3559f9df392ebc8bd89c3 |
|
MD5 | f2e1116cf554724be3791454d0a98b5f |
|
BLAKE2b-256 | f90a68a064a5fc40bcce7a1876883e8280379cec6b0b161040c53a683a320cd4 |
Hashes for extype-1.1.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d516888af1e3d6a96d24df0705c71f51911078c1e014dcb2bb0e46f6022c23fd |
|
MD5 | 7ceb47e6d0fe5ee0a5175f00184ab0fb |
|
BLAKE2b-256 | 279bf4dac5c6d0d8980ccda9c9a0c1793136e070625be210d1b0a1b5c084574d |
Hashes for extype-1.1.1-cp311-cp311-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2320e3d4737e67bb420a8a1f45935b78ac3ebc4790bd9fee6334fd6af25f091e |
|
MD5 | 5b04f6050189ee110b275fd5c0a0b7fb |
|
BLAKE2b-256 | 2302710038871f41a7f42afbe98b59d90a85d41ccbc2d50d927435cc594ccca8 |
Hashes for extype-1.1.1-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5e7cc198904117aabe7adcf9a4ed481f6f5f8ab637ab47f7af241e0f3ebd708a |
|
MD5 | 871deb9fee7ab06cf8dfceddd6de89ac |
|
BLAKE2b-256 | 27511fb0b976f4973b6ec9abcc59d047c8651f18daa0991d96fb9b6762ff5f65 |
Hashes for extype-1.1.1-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4366ab86f901bfb18699e7e42aae205558c06cae400ce5d5b4987d5c65a89a3f |
|
MD5 | 8cf2876e7e5ff197be389fa68496a7eb |
|
BLAKE2b-256 | ee6da9c649ace60ffd81f710acde6d85f8b63bb4e1a4ddf96d1eafa89caf1115 |
Hashes for extype-1.1.1-cp310-cp310-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 447579fee53de3e75a72864439c25684cd74f090e73789c10bba0d25b1a2af68 |
|
MD5 | 48253e72f9b3cd23998aeb36505af121 |
|
BLAKE2b-256 | c7f7f78b08d4be5ad846d03b7ba8833285464fdb557e93bb12a8409303fc2f8f |
Hashes for extype-1.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca5b3d6f5ae51b97f2d74aa6062303e3fe6007ee36dfad14f67c3746878cd513 |
|
MD5 | c2c80ea36546f4829dd77d7f6e91fe54 |
|
BLAKE2b-256 | c0be5f518db9909501f7d1fd04c59b88e5216a1ff3e3c8a1ca0a803e558585ba |
Hashes for extype-1.1.1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6b05cb11aa3c1ea50bc63cd4f0679e7fc227796a2f0f0cc2bedefe7b1fec9fe |
|
MD5 | 29920072f7620ee733b08ffcdb70e127 |
|
BLAKE2b-256 | e914c27263f64878f349ca40593d3dbb1282566177364ce1f056ffbcd72a9c11 |
Hashes for extype-1.1.1-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5eb40d46c514be8bdf4fb96ad58b488f73406debd3f3d15420482991a4ed2d3c |
|
MD5 | 3c61fca6be957760359b3ba1a11d7aae |
|
BLAKE2b-256 | 913af728821744c3bb4df369ec9080bc5e38aeecfe557fd5ecbb4ec1722e9de0 |
Hashes for extype-1.1.1-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f1b1dff9e49eec36776cc5077225bf4a7e47303686a43f4dd2b29c0544d4121 |
|
MD5 | fada04d2694edf6e10c5ed6451bad149 |
|
BLAKE2b-256 | 6230cb5f13445e5e591f84a74da8a17be5b7709004de0c183a53130cffbb62d8 |
Hashes for extype-1.1.1-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ee9c80ab8b7737b6d624b30176981409ac089e1779992e0c895de6d172668ad |
|
MD5 | ae9bc5307d60c6e3df82f5143715f40c |
|
BLAKE2b-256 | cb1c871fff91784246c5baa9c4b0e22f92021bde80ffc2fc3ebebc369e9b9e56 |
Hashes for extype-1.1.1-cp39-cp39-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 316e4620bdb5cf63c5de109777ae8103fc647874c9d9f24f8dc16fd2bacb96f0 |
|
MD5 | 00e3ddc6fafc5164a247d6038a18c216 |
|
BLAKE2b-256 | b1814b2a8c7610d527928f656b6f89328c645b94e8d0c917acb2c6aa6872405c |
Hashes for extype-1.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fae6b80bb07b639b78de6bc8363592737095622374bb9005bd3d7e66f8cb8613 |
|
MD5 | fb20069d42245fe2172bdea3aa51d8c9 |
|
BLAKE2b-256 | 007911d4b6eb218a5407e5a934e38bf9d13c15ba943b45edc9fb4488a85315e6 |
Hashes for extype-1.1.1-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | be1c5b07b22238bd3b63158e0c62ca0eecab3e17b8ae165d0d50c9b88485c903 |
|
MD5 | bdab2dccbb301866212a55c196f3139b |
|
BLAKE2b-256 | efd1a4a7f54dd6f46f972c026f5486844059590439aae8f67194dc624f206843 |
Hashes for extype-1.1.1-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 430246fc6f27f5f4179155939e8d1d886174fd98202a81769d5fdd4b3dec5ecd |
|
MD5 | d3a30450397636e00b97c0240671b485 |
|
BLAKE2b-256 | 4eab5eaed12979bec66a81d089218bf9eb43b39cabcbdceb4c684f6a5fd8146f |
Hashes for extype-1.1.1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8c0c3bd0f499b9ee982ca928c29299ab07199d8f36eb8850f7ac70f65877f2b |
|
MD5 | b32ea17c5c455ee9819f19d7a5ff00f3 |
|
BLAKE2b-256 | 2ce24e1575ea06ca1678c2185f294150e32582cb8f1e67cc7ec2ec0daf247fbe |
Hashes for extype-1.1.1-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 899d5b6a4a7e762a41932ed2f5fc8b83042cdace23855f979fd61b58208bb9eb |
|
MD5 | 2bce9e7e9750f3dad2b7c583e8bc5585 |
|
BLAKE2b-256 | f8b1d1b3606a5d48feb67ab4cb728e72dc642112b6053b2345d796293eecd6bc |
Hashes for extype-1.1.1-cp38-cp38-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2369275c77ad2c6f4f311fcf9944e530f108ff7318b7c535be3dbd0cf8ccf0f6 |
|
MD5 | f3462768140ee6c481bdf2a388fa2f31 |
|
BLAKE2b-256 | 74e6da4269186e7de58db4f43eb619cba6b30d36d39bc99c385b887f74b52fca |
Hashes for extype-1.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 689f8f4eae5d1948ecf898179e2c5831f74403de17d5f13687bf62530ce0f8fb |
|
MD5 | 92a88d1f4d8a270c046245e9993f70eb |
|
BLAKE2b-256 | 07dbdc9d687bb6d756c852846c0ebb1312391b3e66823a54e7b67638a80714f5 |
Hashes for extype-1.1.1-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7fef27f209f5cd0cae0f7ea7bd36d176b4f6d5a8c0f52c64f4b5f251c8d0bb41 |
|
MD5 | 89525f3eb539189e890a4a6d60f4d6c0 |
|
BLAKE2b-256 | adee2e3359d5f73482595483f6e76bf8b042c3aaa0179fe2515cf67de3a5e012 |
Hashes for extype-1.1.1-cp38-cp38-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ac48c0a655d7b3f4a37a77e03f434bba6bed7a6d575b69744470f4f95cba4e1 |
|
MD5 | a17edf55003c7afcc2f416af5684f95e |
|
BLAKE2b-256 | 6a15ac557970acefd49de71e0b474f13e357ad677e6e6605c57844d46c3019de |
Hashes for extype-1.1.1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d908f3f26b413086fd87479e2011c6121e77f59838703c5bccb6eac984e2fd9 |
|
MD5 | 3654d2f0445d15214bb36da2692f32ab |
|
BLAKE2b-256 | 0a62c86ff3f94d5c8b2050f7ed663c3d3e2dbf9e1ee6d4563b30d47d4acbcc40 |
Hashes for extype-1.1.1-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dba0cfc58ccb6b3339da5e3edf797dad058b21ec4fb3112ff9bbb1e0ae669d07 |
|
MD5 | 8db6965d0b2227081ff2013e418d1cb6 |
|
BLAKE2b-256 | 69e023a90be4aa34b4718005b175251b1131009678912fb9c088d3579265ab2e |
Hashes for extype-1.1.1-cp37-cp37m-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b0caf5415e6c70e5e4092c7760edb8d125c9a28777a02520a25880181150102 |
|
MD5 | 33d1a9d679a83c7e7cd06d9746ea68a2 |
|
BLAKE2b-256 | cbd06c14c8a893dd93b9ee9be311d149be3d67c60736a12c0e8005057b4d1482 |
Hashes for extype-1.1.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 978a33b58b170ca20a88d9d331d43821eff1052766587ff1988a371f7cc5c908 |
|
MD5 | c9a42242ac747eb129c81debf4ab37e8 |
|
BLAKE2b-256 | 5b8a9999fc5369d728e04eef1ce6b08fa7d46c4728a64e360c7d5198777b5c34 |
Hashes for extype-1.1.1-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 865200852dce4c6ccf054c905d147db4b9a28d25de70d5fecdbd0fe15b7cbcb5 |
|
MD5 | bb0cee40c21b6ac7133f2253bd337591 |
|
BLAKE2b-256 | ab034d56714488a35a6138d2724a999b0836f7aba1ac338f90f5df381d55c9ca |
Hashes for extype-1.1.1-cp37-cp37m-macosx_11_0_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d3c9563ae57786d27888d45f33cc5e11f8ae63232bae0eb26b5684764d530de |
|
MD5 | b05ff0dc808e42ab6598a2f65e485530 |
|
BLAKE2b-256 | 7eedee67209fc11558c70abe62245cfc488449ccdfcf2dabada3fb45937ab00f |