Extype stands for extensible types, a Python package that enables extending types.
pip install extypeAlternatively, 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/First, in your Python code, import the package:
import extypeThen, 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_allThis 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
extendfunction 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 | str |
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
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)) # TrueFirst, we'll need some tools:
from extype import extension, extend_type_withNext, 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 resultAfter 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)) # TrueWe 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.
- 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
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_editableWith this, you can start using the package in your code. Spawn shell within the build environment:
hatch shellIt'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 installBut note that any changes you make won't be reflected in the installed package.
To build the wheel, you can use:
hatch run dist:buildThis will build the wheel for all python versions, and put it in the dist folder.
To run tests for all python versions, run:
hatch run dist:testTo run tests for a specific python version, run:
hatch run +py=39 dist:testBoth commands will build, install the package into an isolated environment, and run the tests in it.
Note: All of the following
listextensions also exist ondict_keys,dict_valuesanddict_items.
list.all(self: List[T], fn: Callable[[T], bool] = bool) -> boolReturns true if all elements, mapped through the given fn, are True.
list.any(self: List[T], fn: Callable[[T], bool] = bool) -> boolReturns 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]) -> TReduces 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) -> UReduces 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 IndexErrorReturns the first element in the list, or raises an IndexError if the list is empty.
list.last(self: List[T]) -> T, raise IndexErrorReturns the last element in the list, or raises IndexError if the list is empty.
float.round(self: float) -> intRounds the floating point number to the nearest integer.
float.round(self: float, ndigits: int) -> int | floatRound 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) -> strReturns the hexadecimal representation of the integer.
int.oct(self: int) -> strReturns the octal representation of the integer.
int.bin(self: int) -> strReturns the binary representation of the integer.
str.to_int(self: str, base: int = 10, default: T = ...) -> int | TConverts 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 | TConverts 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,rangeandzip
.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.