<commit_before>"""
Remote method registry
"""<commit_msg>Implement schema_from_function<commit_after>"""
Remote method registry
"""

from inspect import getargspec

BASE_SCHEMA = {
    "additionalProperties": False,
    "required": ["args", "kwargs"],
    "properties": {
        "args": {
            "type": "array",
            "items": [],
            "additionalItems": False,
        },
        "kwargs": {
            "type": "object",
            "additionalProperties": False,
            "properties": {},
        },
    },
}

def schema_from_function(func):
    """
    Return a json schema derived from
    the provided function's signature
    """

    (args, _, _, defaults) = getargspec(func)

    args = args or []
    defaults = defaults or []

    if defaults:
        defaults = args[-len(defaults):]
        args = args[:-len(defaults)]
    else:
        defaults = []

    schema = BASE_SCHEMA

    schema["properties"]["args"]["items"] = [{}] * len(args)
    schema["properties"]["kwargs"]["properties"] = {
        key: {}
        for key
        in defaults
    }

    return schema

class Registry(object):
    """
    Generate json schema for each registered function
    Store functions by a name string
    Retrieve a schema and function by a name string
    """

    def __init__(self):
        """
        Initialise the store
        """

        self.methods = {}
        self.schemas = {}

    def put(self, name, func):
        """
        Generate a JSON schema for `func`
        Store the function and the schema
        using `name` as the key
        """

    def get(self, name):
        """
        Return a tuple containing the schema and function
        using `name` as the key

        Throw a KeyError if it doesn't exist
        """
{'fraction_non_alphanumeric': 0.12478920741989882, 'fraction_numerical': 0.0, 'mean_word_length': 2.0272108843537415, 'pattern_counts': {'":': 11, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7510853', 'n_tokens_mistral': 487, 'n_tokens_neox': 476, 'n_words': 148}