pyramid_duh.params module

Utilities for request parameters

pyramid_duh.params.argify(*args, **type_kwargs)[source]

Request decorator for automagically passing in request parameters.

Notes

Here is a sample use case:

@argify(foo=dict, ts=datetime)
def handle_request(request, foo, ts, bar='baz'):
    # do request handling

No special type is required for strings:

@argify
def handle_request(request, foo, bar='baz'):
    # do request handling (both 'foo' and 'bar' are strings)

If any positional arguments are missing, it will raise a HTTPBadRequest exception. If any keyword arguments are missing, it will simply use whatever the default value is.

Note that unit tests should be unaffected by this decorator. This is valid:

@argify
def myview(request, var1, var2='foo'):
    return 'bar'

class TestReq(unittest.TestCase):
    def test_my_request(self):
        request = pyramid.testing.DummyRequest()
        retval = myview(request, 5, var2='foobar')
        self.assertEqual(retval, 'bar')
pyramid_duh.params.includeme(config)[source]

Add parameter utilities

pyramid_duh.params.is_request(obj)[source]

Check if an object looks like a request

pyramid_duh.params.param(request, name, default=<object object>, type=None, validate=None)[source]

Access a parameter and perform type conversion.

Parameters:

request : Request

name : str

The name of the parameter to retrieve

default : object, optional

The default value to use if none is found

type : object, optional

The type to convert the argument to. All python primitives are supported, as well as date and datetime. You may also pass in a factory function or an object that has a static __from_json__ method.

validate : callable, optional

Callable test to validate parameter value

Returns:

arg : object

Raises:

exc : HTTPBadRequest

If a parameter is requested that does not exist and no default was provided