Skip to main content

canonical_args

https://travis-ci.org/shonteag/canonical_args.svg?branch=master

canonical_args is a package designed to provide some certainty around abstract method calls. Consider, for instance, that we need to call one of many possible methods for a package we do not control. Each of these methods has the same arguments, but the potential values change depending on the function. We can write canonical_args arg specs for each of these methods, allowing us some clarity as to what each argument needs to be (types, values, etc.) when we execute dynamically:

{
        "args": [
                {
                        "name": "argument1",
                        "type": int,
                        "values": "range(0, 15)"
                },
                {
                        "name": "argument2",
                        "type": "one([int, float, str])",
                        "values": {
                                "int": ">0",
                                "float": ">0",
                                "str": ["A", "B", "C"]
                        }
                }
        ],
        "kwargs": {
                "loss_function": {
                        "type": str,
                        "values": ["quadratic", "0-1"]
                }
        }
}

We can associate this spec with a method, either by registering it (if we do not control the method source):

from canonical_args import register_spec

# associates the spec to the method
register_spec(somemethod, spec)

# method instance method returns the registered spec
print somemethod.get_spec()

or by decorating a method, if we do control it (let’s say for a dynamically imported method handler sub-method).

from canonical_args import argspec

@arg_spec(spec, register=True)
def ourmethod(argument1, argument2, loss_function="quadratic"):
        pass

print ourmethod.get_spec()

This could potentially be of great use to dynamically generate frontend code with type and value-checking code. The specs themselves could be stored in a file or database, allowing for fully dynamic method calls:

from canonical_args import check_args
import pymongo

conn = pymongo.MongoClient("localhost", 27017)

def handle(message_type, *args, **kwargs):
        spec = conn.somedatabase.arg_specs.find_one(
                {"message_type": message_type})
        subhandler = conn.somedatabase.handlers.find_one(
                {"message_type": message_type})

        # use canonical_args to check the unknown arguments
        # against the retrieved spec. will raise AssertionError
        # if fails.
        check_args(spec, *args, **kwargs)

        # if no errors raised, fire the retrieved handler method
        return subhandler(*args, **kwargs)

def get_handler_spec(message_type):
        """
        get the arg spec without executing the function. can
        be used at front end (eg. HTML) for generating an
        appropriate form for method calls.
        """
        return conn.somedatabase.handlers.find_one(
                {"message_type": message_type})

The code above does not register the spec directly to the subhandler method, as it may not always be desirable to do so. The choice is yours.

Full Documentation

https://shonteag.github.io/canonical_args/

Future Work

I aim to provide frontend code generation directly within the module, probably in a subpackage. At least to handle HTML <form> generation, possibly with Javascript type matching. Currently under development! (https://github.com/shonteag/canonical_args_frontend/)

Installation

Simple as:

pip install canonical_args

To run tests:

git clone https://github.com/shonteag/canonical_args
nosetests

Metadata

Release files for canonical-args 0.4.1

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

Source distribution (sdist)

Source distribution for canonical-args 0.4.1
File Size Uploaded Provenance
canonical_args-0.4.1.tar.gz 10.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for canonical-args 0.4.1
File Interpreter ABI Platform
canonical_args-0.4.1-py2-none-any.whl Python 2 none any Details

Total release size: 19.1 kB

Release files / canonical_args-0.4.1.tar.gz

Download URL canonical_args-0.4.1.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
01fc3263e7775c887800b570e41db4c6347d2bb3bc1943c88bbcfbfbede4a964
BLAKE2b-256 checksum
How to use checksums
e19fe0376ced3eff03f0e00f44c73e3a49b00ff2d790b64279e89fec4e0d9cd7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / canonical_args-0.4.1-py2-none-any.whl

Download URL canonical_args-0.4.1-py2-none-any.whl
Size 8.8 kB
Tags Python 2
SHA-256 checksum
How to use checksums
b9591ced0a08d59a0512fd3fce7584920a76fb898698bb975884f58895897f99
BLAKE2b-256 checksum
How to use checksums
0bafc8e0009c4fceeefd19f59e259de74733b69577188b36cca5923c712da0ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for canonical-args 0.4.1

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