Exemplo n.º 1
0
def prefix(transformers, default=None):
    """Returns a different transformer depending on the prefix at the end of the requested URL.
       If none match and no default is given no transformation takes place.

       should pass in a dict with the following format:

            {'[prefix]': transformation_action,
             ...
            }
    """
    transformers = {
        prefix: auto_kwargs(transformer) if transformer else transformer
        for prefix, transformer in transformers.items()
    }
    default = default and auto_kwargs(default)

    def transform(data, request=None, response=None):
        path = request.path
        transformer = default
        for prefix_test, prefix_transformer in transformers.items():
            if path.startswith(prefix_test):
                transformer = prefix_transformer
                break

        return transformer(data) if transformer else data

    return transform
Exemplo n.º 2
0
def content_type(transformers, default=None):
    """Returns a different transformer depending on the content type passed in.
       If none match and no default is given no transformation takes place.

       should pass in a dict with the following format:

            {'[content-type]': transformation_action,
             ...
            }
    """
    transformers = {
        content_type: auto_kwargs(transformer) if transformer else transformer
        for content_type, transformer in transformers.items()
    }
    default = default and auto_kwargs(default)

    def transform(data, request):
        transformer = transformers.get(
            request.content_type.split(';')[0], default)
        if not transformer:
            return data

        return transformer(data)

    return transform
Exemplo n.º 3
0
def content_type(transformers, default=None):
    """Returns a different transformer depending on the content type passed in.
       If none match and no default is given no transformation takes place.

       should pass in a dict with the following format:

            {'[content-type]': transformation_action,
             ...
            }
    """
    transformers = {content_type: auto_kwargs(transformer) if transformer else transformer
                    for content_type, transformer in transformers.items()}
    default = default and auto_kwargs(default)

    def transform(data, request):
        transformer = transformers.get(request.content_type.split(';')[0], default)
        if not transformer:
            return data

        return transformer(data)
    return transform
Exemplo n.º 4
0
def prefix(transformers, default=None):
    """Returns a different transformer depending on the prefix at the end of the requested URL.
       If none match and no default is given no transformation takes place.

       should pass in a dict with the following format:

            {'[prefix]': transformation_action,
             ...
            }
    """
    transformers = {prefix: auto_kwargs(transformer) if transformer else transformer
                    for prefix, transformer in transformers.items()}
    default = default and auto_kwargs(default)

    def transform(data, request=None, response=None):
        path = request.path
        transformer = default
        for prefix_test, prefix_transformer in transformers.items():
            if path.startswith(prefix_test):
                transformer = prefix_transformer
                break

        return transformer(data) if transformer else data
    return transform
Exemplo n.º 5
0
def all(*transformers):
    """Returns the results of applying all passed in transformers to data

       should pass in list of transformers

            [transformer_1, transformer_2...]
    """
    transformers = tuple(auto_kwargs(transformer) for transformer in transformers)

    def transform(data, request=None, response=None):
        for transformer in transformers:
            data = transformer(data, request=request, response=response)

        return data
    return transform
Exemplo n.º 6
0
def all(*transformers):
    """Returns the results of applying all passed in transformers to data

       should pass in list of transformers

            [transformer_1, transformer_2...]
    """
    transformers = tuple(
        auto_kwargs(transformer) for transformer in transformers)

    def transform(data, request=None, response=None):
        for transformer in transformers:
            data = transformer(data, request=request, response=response)

        return data

    return transform
def my_method(name, request=None):
    pass


def my_method_with_kwargs(name, request=None, **kwargs):
    pass


with Timer('generate_kwargs'):
    accept_kwargs = generate_accepted_kwargs(my_method, ('request', 'response', 'version'))

    for test in range(100000):
        my_method(test, **accept_kwargs(DATA))


with Timer('auto_kwargs'):
    wrapped_method = auto_kwargs(my_method)

    for test in range(100000):
        wrapped_method(test, **DATA)


with Timer('native_kwargs'):
    for test in range(100000):
        my_method_with_kwargs(test, **DATA)


with Timer('no_kwargs'):
    for test in range(100000):
        my_method(test, request=None)
Exemplo n.º 8
0
def my_method(name, request=None):
    pass


def my_method_with_kwargs(name, request=None, **kwargs):
    pass


with Timer("generate_kwargs"):
    accept_kwargs = generate_accepted_kwargs(my_method, ("request", "response", "version"))

    for test in range(100000):
        my_method(test, **accept_kwargs(DATA))


with Timer("auto_kwargs"):
    wrapped_method = auto_kwargs(my_method)

    for test in range(100000):
        wrapped_method(test, **DATA)


with Timer("native_kwargs"):
    for test in range(100000):
        my_method_with_kwargs(test, **DATA)


with Timer("no_kwargs"):
    for test in range(100000):
        my_method(test, request=None)