~barry/mailman/lp1423756

« back to all changes in this revision

Viewing changes to src/mailman/rest/docs/helpers.rst

  • Committer: Barry Warsaw
  • Date: 2015-01-05 01:20:33 UTC
  • mfrom: (7264.4.66 py3)
  • Revision ID: barry@list.org-20150105012033-zdrw9c2odhpf22fz
Merge the Python 3 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
    >>> resource = dict(geddy='bass', alex='guitar', neil='drums')
46
46
    >>> json_data = etag(resource)
47
47
    >>> print(resource['http_etag'])
48
 
    "96e036d66248cab746b7d97047e08896fcfb2493"
 
48
    "6929ecfbda2282980a4818fb75f82e812077f77a"
49
49
 
50
50
For convenience, the etag function also returns the JSON representation of the
51
51
dictionary after tagging, since that's almost always what you want.
58
58
    >>> dump_msgdata(data)
59
59
    alex     : guitar
60
60
    geddy    : bass
61
 
    http_etag: "96e036d66248cab746b7d97047e08896fcfb2493"
 
61
    http_etag: "6929ecfbda2282980a4818fb75f82e812077f77a"
62
62
    neil     : drums
63
63
 
64
64
 
69
69
converting their values.
70
70
::
71
71
 
 
72
    >>> import six
72
73
    >>> from mailman.rest.validator import Validator
73
 
    >>> validator = Validator(one=int, two=unicode, three=bool)
 
74
    >>> validator = Validator(one=int, two=six.text_type, three=bool)
74
75
 
75
76
    >>> class FakeRequest:
76
77
    ...     params = None
81
82
    >>> def print_request(one, two, three):
82
83
    ...     print(repr(one), repr(two), repr(three))
83
84
    >>> print_request(**validator(FakeRequest))
84
 
    1 u'two' True
 
85
    1 'two' True
85
86
 
86
87
On invalid input, an exception is raised.
87
88
 
119
120
However, if optional keys are missing, it's okay.
120
121
::
121
122
 
122
 
    >>> validator = Validator(one=int, two=unicode, three=bool,
 
123
    >>> validator = Validator(one=int, two=six.text_type, three=bool,
123
124
    ...                       four=int, five=int,
124
125
    ...                       _optional=('four', 'five'))
125
126
 
128
129
    >>> def print_request(one, two, three, four=None, five=None):
129
130
    ...     print(repr(one), repr(two), repr(three), repr(four), repr(five))
130
131
    >>> print_request(**validator(FakeRequest))
131
 
    1 u'two' True 4 5
 
132
    1 'two' True 4 5
132
133
 
133
134
    >>> del FakeRequest.params['four']
134
135
    >>> print_request(**validator(FakeRequest))
135
 
    1 u'two' True None 5
 
136
    1 'two' True None 5
136
137
 
137
138
    >>> del FakeRequest.params['five']
138
139
    >>> print_request(**validator(FakeRequest))
139
 
    1 u'two' True None None
 
140
    1 'two' True None None
140
141
 
141
142
But if the optional values are present, they must of course also be valid.
142
143