Skip to main content

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

petit_interfacer-0.1.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

petit_interfacer-0.1.0-py2.py3-none-any.whl (9.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file petit_interfacer-0.1.0.tar.gz.

File metadata

  • Download URL: petit_interfacer-0.1.0.tar.gz
  • Upload date:
  • Size: 8.3 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

Hashes for petit_interfacer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 25a9b733b687681cf7b5d30dd8a4026d84f6765ba8b25e76b0d36d0cd1f8dc90
MD5 012eb1d66ef356b66f7f7595d79df8c5
BLAKE2b-256 05802586cb5fa14009d984c9cc3e7de98710ee638202c56857fbe988ed88e6d9

See more details on using hashes here.

File details

Details for the file petit_interfacer-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: petit_interfacer-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.3 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

Hashes for petit_interfacer-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6629a5c64dd3b50f31507a4c72f06b28e21e61365f9450d488b58f3bdb9f0632
MD5 a41fd4b49c9c2fd9023c3805907087d7
BLAKE2b-256 6c2ab86af8f75dff9453b84542f5bd189257d3b8beaa974c926d79851fcfe564

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page