Convert your function prototypes to work faster
Project description
Prototype transformer
It's a module of the petite_stack
Whats is that for ?
You have a framework that executes a particular prototype of function but you don't want to bother yourself or the end developper with using the exact prototype, then this will help.
This lib will attempt to automatically build an interface layer, for your functions without using the **kwargs
magic and the dynamic nature of it.
Example :
from petit_interfacer import (BlindBind, Dataclass, RealOptional,
interface_binder_for, Dataclass)
def proto(
worker: RealOptional[Worker],
session: RealOptional[Session],
body: BlindBind[RealOptional[Body]],
d: Dataclass,
cookies: RealOptional[Cookies],
): -> Optional[Any]:
...
worker: Worker = ...
session: Session = ...
body: Body = ...
cookies: Cookies = ...
bind_interface = interface_binder_for(proto)
@bind_interface
def example1(body) -> Any: #notice here that, as body
# is BlindBind, we are not obligated to set it's type
...
# then you can do
# without worrying about the user having written the exact prototype
example1(worker=worker, session=session, body=body, cookies=cookies)
@bind_interface
def example2(w: Worker) -> None:
...
example2(worker=worker, session=session, body=body, cookies=cookies)
# here we have the same prototype in the end, and we are not using **kwargs, so everything is static
# and any error will raise a warning before your app starts, so no runtime error and less testing required
@bind_interface
def example3(w: Worker, BoDyButStangelyNamed) -> None:
...
example3(worker=worker, session=session, body=body, cookies=cookies)
# This will work
@bind_interface
def example3(w, BoDyButStangelyNamed) -> None:
...
example3(worker=worker, session=session, body=body, cookies=cookies)
# This will raise an Exception as it can't decide how to bind params together
So you see that you can make your life easier, by simply defining the prototype, you want to execute and reliying on static checking and binding.
Dataclass
As you may know dataclasses does not provide a way to check if a class is a dataclass with issubclass
, so this libs add the ClassProxyTest
class which can be inherited in order to add support for other classes.
The petit_interfacer lib provides the support for the dataclasses based on this technique. You can simply import Dataclass
from it.
If you need to add another custom class / function, then use ClassProxyTest
:
Example: How dataclasses is handled:
from petit_interfacer import ClassProxyTest
class Dataclass(ClassProxyTest):
"""As dataclasses does not provide a class to be used with issubclass, we use this proxy to handle it
"""
def is_correct_type(t: Any) -> bool:
return is_dataclass(t)
Using it with basic types:
Inherit the type you want to use it with, as without it, the app can't make the difference between two int
or two str
Next points :
Stop using a lambda for the interface and rather completly redefine and evaluate the function with a modified prototype.
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
Built Distribution
File details
Details for the file petit_interfacer-0.1.4.tar.gz
.
File metadata
- Download URL: petit_interfacer-0.1.4.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa1df10ecb5b18dbb8c706f933f9bcf4d127e3db591987699270fd0f5a6fb38b |
|
MD5 | e83adfebae8b899d030c6522cf75736e |
|
BLAKE2b-256 | 383686cdcd0e749451cef81b682080a5f6aebed2a12555df380f63244cc6a49c |
File details
Details for the file petit_interfacer-0.1.4-py2.py3-none-any.whl
.
File metadata
- Download URL: petit_interfacer-0.1.4-py2.py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7cb3f262bd416d88946347d02a6092d0e5ee2644caf632b36fe6510ae3f54882 |
|
MD5 | e316729ff92f890446ac3f2555fc208d |
|
BLAKE2b-256 | 34af24ca4c1b0783377129a1079cf01982b16e57b43a61b165e23841f439bf65 |