Skip to main content

Prototype transformer

Codecov GitHub license

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.

Metadata

Release files for petit-interfacer 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for petit-interfacer 0.1.4
File Size Uploaded Provenance Actions
petit_interfacer-0.1.4.tar.gz 8.9 kB Without provenance Details for petit_interfacer-0.1.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for petit-interfacer 0.1.4
File Interpreter ABI Platform Actions
petit_interfacer-0.1.4-py2.py3-none-any.whl Python 2, Python 3 none any Details for petit_interfacer-0.1.4-py2.py3-none-any.whl

Total release size: 19.0 kB

Release files / petit_interfacer-0.1.4.tar.gz

Download URL petit_interfacer-0.1.4.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
aa1df10ecb5b18dbb8c706f933f9bcf4d127e3db591987699270fd0f5a6fb38b
BLAKE2b-256 checksum
How to use checksums
383686cdcd0e749451cef81b682080a5f6aebed2a12555df380f63244cc6a49c
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / petit_interfacer-0.1.4-py2.py3-none-any.whl

Download URL petit_interfacer-0.1.4-py2.py3-none-any.whl
Size 10.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
7cb3f262bd416d88946347d02a6092d0e5ee2644caf632b36fe6510ae3f54882
BLAKE2b-256 checksum
How to use checksums
34af24ca4c1b0783377129a1079cf01982b16e57b43a61b165e23841f439bf65
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for petit-interfacer 0.1.4

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page