Merge ~cjwatson/lazr.restful:remove-manual-pretty-printing into lazr.restful:main
- Git
- lp:~cjwatson/lazr.restful
- remove-manual-pretty-printing
- Merge into main
Proposed by
Colin Watson
Status: | Merged |
---|---|
Merged at revision: | f47ef5f1dbe992f2203e25a32acf1d660a4ccdd0 |
Proposed branch: | ~cjwatson/lazr.restful:remove-manual-pretty-printing |
Merge into: | lazr.restful:main |
Diff against target: |
414 lines (+67/-90) 1 file modified
src/lazr/restful/docs/webservice-marshallers.rst (+67/-90) |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Guruprasad | Approve | ||
Review via email: mp+413798@code.launchpad.net |
Commit message
Remove most manual pretty-printing from webservice-
Description of the change
This was only needed for Python 2/3 support.
We still need `pprint_dict`, because dicts only became order-preserving in Python 3.6.
To post a comment you must log in.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | diff --git a/src/lazr/restful/docs/webservice-marshallers.rst b/src/lazr/restful/docs/webservice-marshallers.rst | |||
2 | index 0d18d2a..b32e0d2 100644 | |||
3 | --- a/src/lazr/restful/docs/webservice-marshallers.rst | |||
4 | +++ b/src/lazr/restful/docs/webservice-marshallers.rst | |||
5 | @@ -21,33 +21,11 @@ application root. | |||
6 | 21 | We also define some helpers to print values in a way that is unambiguous | 21 | We also define some helpers to print values in a way that is unambiguous |
7 | 22 | across Python versions. | 22 | across Python versions. |
8 | 23 | 23 | ||
9 | 24 | >>> import six | ||
10 | 25 | |||
11 | 26 | >>> def pformat_value(value): | ||
12 | 27 | ... """Pretty-format a single value.""" | ||
13 | 28 | ... if isinstance(value, six.text_type): | ||
14 | 29 | ... value = value.encode('unicode_escape').decode('ASCII') | ||
15 | 30 | ... if "'" in value and '"' not in value: | ||
16 | 31 | ... return '"%s"' % value | ||
17 | 32 | ... else: | ||
18 | 33 | ... return "'%s'" % value.replace("'", "\\'") | ||
19 | 34 | ... else: | ||
20 | 35 | ... return repr(value) | ||
21 | 36 | |||
22 | 37 | >>> def pprint_value(value): | ||
23 | 38 | ... """Pretty-print a single value.""" | ||
24 | 39 | ... print(pformat_value(value)) | ||
25 | 40 | |||
26 | 41 | >>> def pprint_list(lst): | ||
27 | 42 | ... print('[', end='') | ||
28 | 43 | ... print(', '.join(pformat_value(value) for value in lst), end='') | ||
29 | 44 | ... print(']') | ||
30 | 45 | |||
31 | 46 | >>> def pprint_dict(d): | 24 | >>> def pprint_dict(d): |
32 | 47 | ... print('{', end='') | 25 | ... print('{', end='') |
33 | 48 | ... print( | 26 | ... print( |
34 | 49 | ... ', '.join( | 27 | ... ', '.join( |
36 | 50 | ... '%s: %s' % (pformat_value(key), pformat_value(value)) | 28 | ... '%r: %r' % (key, value) |
37 | 51 | ... for key, value in sorted(d.items())), | 29 | ... for key, value in sorted(d.items())), |
38 | 52 | ... end='') | 30 | ... end='') |
39 | 53 | ... print('}') | 31 | ... print('}') |
40 | @@ -90,7 +68,7 @@ implementation, the value is returned unchanged. | |||
41 | 90 | 'foo' | 68 | 'foo' |
42 | 91 | >>> marshaller.marshall_from_json_data(4) | 69 | >>> marshaller.marshall_from_json_data(4) |
43 | 92 | 4 | 70 | 4 |
45 | 93 | >>> print(marshaller.marshall_from_json_data(u"unicode\u2122")) | 71 | >>> print(marshaller.marshall_from_json_data("unicode\u2122")) |
46 | 94 | unicode™ | 72 | unicode™ |
47 | 95 | >>> marshaller.marshall_from_json_data("") | 73 | >>> marshaller.marshall_from_json_data("") |
48 | 96 | '' | 74 | '' |
49 | @@ -114,16 +92,16 @@ string, the resulting value is passed on to marshall_from_json_data(). | |||
50 | 114 | >>> marshaller.marshall_from_request("false") | 92 | >>> marshaller.marshall_from_request("false") |
51 | 115 | False | 93 | False |
52 | 116 | >>> marshaller.marshall_from_request('["True", "False"]') | 94 | >>> marshaller.marshall_from_request('["True", "False"]') |
54 | 117 | [...'True', ...'False'] | 95 | ['True', 'False'] |
55 | 118 | >>> marshaller.marshall_from_request("1") | 96 | >>> marshaller.marshall_from_request("1") |
56 | 119 | 1 | 97 | 1 |
57 | 120 | >>> marshaller.marshall_from_request("-10.5") | 98 | >>> marshaller.marshall_from_request("-10.5") |
58 | 121 | -10.5 | 99 | -10.5 |
60 | 122 | >>> pprint_value(marshaller.marshall_from_request('"a string"')) | 100 | >>> marshaller.marshall_from_request('"a string"') |
61 | 123 | 'a string' | 101 | 'a string' |
63 | 124 | >>> pprint_value(marshaller.marshall_from_request('"false"')) | 102 | >>> marshaller.marshall_from_request('"false"') |
64 | 125 | 'false' | 103 | 'false' |
66 | 126 | >>> pprint_value(marshaller.marshall_from_request('"null"')) | 104 | >>> marshaller.marshall_from_request('"null"') |
67 | 127 | 'null' | 105 | 'null' |
68 | 128 | 106 | ||
69 | 129 | Invalid JSON-encoded strings are interpreted as string literals and | 107 | Invalid JSON-encoded strings are interpreted as string literals and |
70 | @@ -131,7 +109,7 @@ passed on directly to marshall_from_json_data(). That's for the | |||
71 | 131 | convenience of web clients, they don't need to encode string values in | 109 | convenience of web clients, they don't need to encode string values in |
72 | 132 | quotes, or can pass lists using multiple key-value pairs. | 110 | quotes, or can pass lists using multiple key-value pairs. |
73 | 133 | 111 | ||
75 | 134 | >>> pprint_value(marshaller.marshall_from_request(u"a string")) | 112 | >>> marshaller.marshall_from_request("a string") |
76 | 135 | 'a string' | 113 | 'a string' |
77 | 136 | >>> marshaller.marshall_from_request('False') | 114 | >>> marshaller.marshall_from_request('False') |
78 | 137 | 'False' | 115 | 'False' |
79 | @@ -212,9 +190,9 @@ None is passed through though. | |||
80 | 212 | Booleans are encoded using the standard JSON representation of 'true' or | 190 | Booleans are encoded using the standard JSON representation of 'true' or |
81 | 213 | 'false'. | 191 | 'false'. |
82 | 214 | 192 | ||
84 | 215 | >>> marshaller.marshall_from_request(u"true") | 193 | >>> marshaller.marshall_from_request("true") |
85 | 216 | True | 194 | True |
87 | 217 | >>> marshaller.marshall_from_request(u"false") | 195 | >>> marshaller.marshall_from_request("false") |
88 | 218 | False | 196 | False |
89 | 219 | 197 | ||
90 | 220 | >>> marshaller.marshall_from_request('True') | 198 | >>> marshaller.marshall_from_request('True') |
91 | @@ -250,7 +228,7 @@ Integers are encoded using strings when in a request. | |||
92 | 250 | 228 | ||
93 | 251 | >>> marshaller.marshall_from_request("4") | 229 | >>> marshaller.marshall_from_request("4") |
94 | 252 | 4 | 230 | 4 |
96 | 253 | >>> marshaller.marshall_from_request(u"-4") | 231 | >>> marshaller.marshall_from_request("-4") |
97 | 254 | -4 | 232 | -4 |
98 | 255 | 233 | ||
99 | 256 | It raises a ValueError if the value cannot be converted to an integer. | 234 | It raises a ValueError if the value cannot be converted to an integer. |
100 | @@ -269,15 +247,15 @@ Note that python octal and hexadecimal syntax isn't supported. | |||
101 | 269 | 247 | ||
102 | 270 | (This would 13 in octal notation.) | 248 | (This would 13 in octal notation.) |
103 | 271 | 249 | ||
105 | 272 | >>> marshaller.marshall_from_request(u"015") | 250 | >>> marshaller.marshall_from_request("015") |
106 | 273 | Traceback (most recent call last): | 251 | Traceback (most recent call last): |
107 | 274 | ... | 252 | ... |
109 | 275 | ValueError: got '...', expected int: ...'015' | 253 | ValueError: got 'str', expected int: '015' |
110 | 276 | 254 | ||
112 | 277 | >>> marshaller.marshall_from_request(u"0x04") | 255 | >>> marshaller.marshall_from_request("0x04") |
113 | 278 | Traceback (most recent call last): | 256 | Traceback (most recent call last): |
114 | 279 | ... | 257 | ... |
116 | 280 | ValueError: got '...', expected int: ...'0x04' | 258 | ValueError: got 'str', expected int: '0x04' |
117 | 281 | 259 | ||
118 | 282 | Float | 260 | Float |
119 | 283 | ----- | 261 | ----- |
120 | @@ -312,11 +290,11 @@ And integers are automatically converted to a float. | |||
121 | 312 | 290 | ||
122 | 313 | Floats are encoded using the standard JSON representation. | 291 | Floats are encoded using the standard JSON representation. |
123 | 314 | 292 | ||
125 | 315 | >>> marshaller.marshall_from_request(u"1.2") | 293 | >>> marshaller.marshall_from_request("1.2") |
126 | 316 | 1.2 | 294 | 1.2 |
128 | 317 | >>> marshaller.marshall_from_request(u"-1.2") | 295 | >>> marshaller.marshall_from_request("-1.2") |
129 | 318 | -1.2 | 296 | -1.2 |
131 | 319 | >>> marshaller.marshall_from_request(u"-1") | 297 | >>> marshaller.marshall_from_request("-1") |
132 | 320 | -1.0 | 298 | -1.0 |
133 | 321 | 299 | ||
134 | 322 | >>> marshaller.marshall_from_request('True') | 300 | >>> marshaller.marshall_from_request('True') |
135 | @@ -425,16 +403,16 @@ string. A ValueError is raised when that's not the case. | |||
136 | 425 | >>> verifyObject(IFieldMarshaller, marshaller) | 403 | >>> verifyObject(IFieldMarshaller, marshaller) |
137 | 426 | True | 404 | True |
138 | 427 | 405 | ||
140 | 428 | >>> pprint_value(marshaller.marshall_from_json_data(u"Test")) | 406 | >>> marshaller.marshall_from_json_data("Test") |
141 | 429 | 'Test' | 407 | 'Test' |
142 | 430 | >>> marshaller.marshall_from_json_data(1.0) | 408 | >>> marshaller.marshall_from_json_data(1.0) |
143 | 431 | Traceback (most recent call last): | 409 | Traceback (most recent call last): |
144 | 432 | ... | 410 | ... |
146 | 433 | ValueError: got 'float', expected ...: 1.0 | 411 | ValueError: got 'float', expected str: 1.0 |
147 | 434 | >>> marshaller.marshall_from_json_data(b'Test') | 412 | >>> marshaller.marshall_from_json_data(b'Test') |
148 | 435 | Traceback (most recent call last): | 413 | Traceback (most recent call last): |
149 | 436 | ... | 414 | ... |
151 | 437 | ValueError: got '...', expected ...: ...'Test' | 415 | ValueError: got 'bytes', expected str: b'Test' |
152 | 438 | 416 | ||
153 | 439 | None is passed through though. | 417 | None is passed through though. |
154 | 440 | 418 | ||
155 | @@ -444,13 +422,13 @@ None is passed through though. | |||
156 | 444 | When coming from the request, everything is interpreted as a unicode | 422 | When coming from the request, everything is interpreted as a unicode |
157 | 445 | string: | 423 | string: |
158 | 446 | 424 | ||
160 | 447 | >>> pprint_value(marshaller.marshall_from_request('a string')) | 425 | >>> marshaller.marshall_from_request('a string') |
161 | 448 | 'a string' | 426 | 'a string' |
163 | 449 | >>> pprint_value(marshaller.marshall_from_request(['a', 'b'])) | 427 | >>> marshaller.marshall_from_request(['a', 'b']) |
164 | 450 | "['a', 'b']" | 428 | "['a', 'b']" |
166 | 451 | >>> pprint_value(marshaller.marshall_from_request('true')) | 429 | >>> marshaller.marshall_from_request('true') |
167 | 452 | 'True' | 430 | 'True' |
169 | 453 | >>> pprint_value(marshaller.marshall_from_request('')) | 431 | >>> marshaller.marshall_from_request('') |
170 | 454 | '' | 432 | '' |
171 | 455 | 433 | ||
172 | 456 | Except that 'null' still returns None. | 434 | Except that 'null' still returns None. |
173 | @@ -460,11 +438,11 @@ Except that 'null' still returns None. | |||
174 | 460 | 438 | ||
175 | 461 | Line breaks coming from the request are normalized to LF. | 439 | Line breaks coming from the request are normalized to LF. |
176 | 462 | 440 | ||
178 | 463 | >>> pprint_value(marshaller.marshall_from_request('abc\r\n\r\ndef\r\n')) | 441 | >>> marshaller.marshall_from_request('abc\r\n\r\ndef\r\n') |
179 | 464 | 'abc\n\ndef\n' | 442 | 'abc\n\ndef\n' |
181 | 465 | >>> pprint_value(marshaller.marshall_from_request('abc\n\ndef\n')) | 443 | >>> marshaller.marshall_from_request('abc\n\ndef\n') |
182 | 466 | 'abc\n\ndef\n' | 444 | 'abc\n\ndef\n' |
184 | 467 | >>> pprint_value(marshaller.marshall_from_request('abc\r\rdef\r')) | 445 | >>> marshaller.marshall_from_request('abc\r\rdef\r') |
185 | 468 | 'abc\n\ndef\n' | 446 | 'abc\n\ndef\n' |
186 | 469 | 447 | ||
187 | 470 | Bytes | 448 | Bytes |
188 | @@ -480,9 +458,9 @@ a string, a ValueError is raised. | |||
189 | 480 | >>> verifyObject(IFieldMarshaller, marshaller) | 458 | >>> verifyObject(IFieldMarshaller, marshaller) |
190 | 481 | True | 459 | True |
191 | 482 | 460 | ||
193 | 483 | >>> pprint_value(marshaller.marshall_from_json_data(u"Test")) | 461 | >>> marshaller.marshall_from_json_data("Test") |
194 | 484 | b'Test' | 462 | b'Test' |
196 | 485 | >>> pprint_value(marshaller.marshall_from_json_data(u'int\xe9ressant')) | 463 | >>> marshaller.marshall_from_json_data('int\xe9ressant') |
197 | 486 | b'int\xc3\xa9ressant' | 464 | b'int\xc3\xa9ressant' |
198 | 487 | >>> marshaller.marshall_from_json_data(1.0) | 465 | >>> marshaller.marshall_from_json_data(1.0) |
199 | 488 | Traceback (most recent call last): | 466 | Traceback (most recent call last): |
200 | @@ -497,21 +475,20 @@ Again, except for None which is passed through. | |||
201 | 497 | When coming over the request, the value is also converted into a UTF-8 | 475 | When coming over the request, the value is also converted into a UTF-8 |
202 | 498 | encoded string, with no JSON decoding. | 476 | encoded string, with no JSON decoding. |
203 | 499 | 477 | ||
205 | 500 | >>> pprint_value(marshaller.marshall_from_request(u"Test")) | 478 | >>> marshaller.marshall_from_request("Test") |
206 | 501 | b'Test' | 479 | b'Test' |
208 | 502 | >>> pprint_value(marshaller.marshall_from_request(u'int\xe9ressant')) | 480 | >>> marshaller.marshall_from_request('int\xe9ressant') |
209 | 503 | b'int\xc3\xa9ressant' | 481 | b'int\xc3\xa9ressant' |
211 | 504 | >>> pprint_value(marshaller.marshall_from_request(b'1.0')) | 482 | >>> marshaller.marshall_from_request(b'1.0') |
212 | 505 | b'1.0' | 483 | b'1.0' |
214 | 506 | >>> pprint_value(marshaller.marshall_from_request(b'"not JSON"')) | 484 | >>> marshaller.marshall_from_request(b'"not JSON"') |
215 | 507 | b'"not JSON"' | 485 | b'"not JSON"' |
216 | 508 | 486 | ||
217 | 509 | Since multipart/form-data can be used to upload data, file-like objects | 487 | Since multipart/form-data can be used to upload data, file-like objects |
218 | 510 | are read. | 488 | are read. |
219 | 511 | 489 | ||
220 | 512 | >>> from io import BytesIO | 490 | >>> from io import BytesIO |
223 | 513 | >>> pprint_value( | 491 | >>> marshaller.marshall_from_request(BytesIO(b'A line of data')) |
222 | 514 | ... marshaller.marshall_from_request(BytesIO(b'A line of data'))) | ||
224 | 515 | b'A line of data' | 492 | b'A line of data' |
225 | 516 | 493 | ||
226 | 517 | Bytes field used in an entry are stored in the librarian, so their | 494 | Bytes field used in an entry are stored in the librarian, so their |
227 | @@ -547,12 +524,12 @@ ASCIILine is a subclass of Bytes but is marshalled like text. | |||
228 | 547 | 524 | ||
229 | 548 | Unicode objects remain Unicode objects. | 525 | Unicode objects remain Unicode objects. |
230 | 549 | 526 | ||
232 | 550 | >>> pprint_value(marshaller.marshall_from_json_data(u"Test")) | 527 | >>> marshaller.marshall_from_json_data("Test") |
233 | 551 | 'Test' | 528 | 'Test' |
234 | 552 | 529 | ||
235 | 553 | Note that the marshaller accepts character values where bit 7 is set. | 530 | Note that the marshaller accepts character values where bit 7 is set. |
236 | 554 | 531 | ||
238 | 555 | >>> print(marshaller.marshall_from_json_data(u'int\xe9ressant')) | 532 | >>> print(marshaller.marshall_from_json_data('int\xe9ressant')) |
239 | 556 | intéressant | 533 | intéressant |
240 | 557 | 534 | ||
241 | 558 | Non-string values like floats are rejected. | 535 | Non-string values like floats are rejected. |
242 | @@ -560,7 +537,7 @@ Non-string values like floats are rejected. | |||
243 | 560 | >>> marshaller.marshall_from_json_data(1.0) | 537 | >>> marshaller.marshall_from_json_data(1.0) |
244 | 561 | Traceback (most recent call last): | 538 | Traceback (most recent call last): |
245 | 562 | ... | 539 | ... |
247 | 563 | ValueError: got 'float', expected ...: 1.0 | 540 | ValueError: got 'float', expected str: 1.0 |
248 | 564 | 541 | ||
249 | 565 | None is passed through. | 542 | None is passed through. |
250 | 566 | 543 | ||
251 | @@ -570,17 +547,17 @@ None is passed through. | |||
252 | 570 | When coming from the request, everything is interpreted as a unicode | 547 | When coming from the request, everything is interpreted as a unicode |
253 | 571 | string: | 548 | string: |
254 | 572 | 549 | ||
256 | 573 | >>> pprint_value(marshaller.marshall_from_request('a string')) | 550 | >>> marshaller.marshall_from_request('a string') |
257 | 574 | 'a string' | 551 | 'a string' |
259 | 575 | >>> pprint_value(marshaller.marshall_from_request(['a', 'b'])) | 552 | >>> marshaller.marshall_from_request(['a', 'b']) |
260 | 576 | "['a', 'b']" | 553 | "['a', 'b']" |
262 | 577 | >>> pprint_value(marshaller.marshall_from_request('true')) | 554 | >>> marshaller.marshall_from_request('true') |
263 | 578 | 'True' | 555 | 'True' |
265 | 579 | >>> pprint_value(marshaller.marshall_from_request('')) | 556 | >>> marshaller.marshall_from_request('') |
266 | 580 | '' | 557 | '' |
268 | 581 | >>> print(marshaller.marshall_from_request(u'int\xe9ressant')) | 558 | >>> print(marshaller.marshall_from_request('int\xe9ressant')) |
269 | 582 | intéressant | 559 | intéressant |
271 | 583 | >>> pprint_value(marshaller.marshall_from_request('1.0')) | 560 | >>> marshaller.marshall_from_request('1.0') |
272 | 584 | '1.0' | 561 | '1.0' |
273 | 585 | 562 | ||
274 | 586 | But again, 'null' is returned as None. | 563 | But again, 'null' is returned as None. |
275 | @@ -666,7 +643,7 @@ display them the way we want. | |||
276 | 666 | ... try: | 643 | ... try: |
277 | 667 | ... callable(*args) | 644 | ... callable(*args) |
278 | 668 | ... except ValueError as e: | 645 | ... except ValueError as e: |
280 | 669 | ... print('ValueError:', six.text_type(e)) | 646 | ... print('ValueError:', str(e)) |
281 | 670 | 647 | ||
282 | 671 | 648 | ||
283 | 672 | Choice of EnumeratedTypes | 649 | Choice of EnumeratedTypes |
284 | @@ -858,41 +835,41 @@ For sequences, the only JSON representation for the collection itself is a | |||
285 | 858 | list, since that's the only sequence type available in JSON. Anything else | 835 | list, since that's the only sequence type available in JSON. Anything else |
286 | 859 | will raise a ValueError. | 836 | will raise a ValueError. |
287 | 860 | 837 | ||
289 | 861 | >>> pprint_list(list_marshaller.marshall_from_json_data([u"Test"])) | 838 | >>> list_marshaller.marshall_from_json_data(["Test"]) |
290 | 862 | ['Test'] | 839 | ['Test'] |
291 | 863 | 840 | ||
293 | 864 | >>> list_marshaller.marshall_from_json_data(u"Test") | 841 | >>> list_marshaller.marshall_from_json_data("Test") |
294 | 865 | Traceback (most recent call last): | 842 | Traceback (most recent call last): |
295 | 866 | ... | 843 | ... |
297 | 867 | ValueError: got '...', expected list: ...'Test' | 844 | ValueError: got 'str', expected list: 'Test' |
298 | 868 | 845 | ||
299 | 869 | For dicts, we support marshalling from sequences of (name, value) pairs as | 846 | For dicts, we support marshalling from sequences of (name, value) pairs as |
300 | 870 | well as from dicts or even strings which are interpreted as single element | 847 | well as from dicts or even strings which are interpreted as single element |
301 | 871 | lists. | 848 | lists. |
302 | 872 | 849 | ||
303 | 873 | >>> pprint_dict( | 850 | >>> pprint_dict( |
305 | 874 | ... dict_marshaller.marshall_from_json_data({u"foo": u"Vegetarian"})) | 851 | ... dict_marshaller.marshall_from_json_data({"foo": "Vegetarian"})) |
306 | 875 | {'foo': <Item Cuisine.VEGETARIAN, Vegetarian>} | 852 | {'foo': <Item Cuisine.VEGETARIAN, Vegetarian>} |
307 | 876 | 853 | ||
308 | 877 | >>> pprint_dict( | 854 | >>> pprint_dict( |
310 | 878 | ... dict_marshaller.marshall_from_json_data([(u"foo", u"Vegetarian")])) | 855 | ... dict_marshaller.marshall_from_json_data([("foo", "Vegetarian")])) |
311 | 879 | {'foo': <Item Cuisine.VEGETARIAN, Vegetarian>} | 856 | {'foo': <Item Cuisine.VEGETARIAN, Vegetarian>} |
312 | 880 | 857 | ||
314 | 881 | >>> pprint_dict(dict_marshaller.marshall_from_request(u"foo,Vegetarian")) | 858 | >>> pprint_dict(dict_marshaller.marshall_from_request("foo,Vegetarian")) |
315 | 882 | {'foo': <Item Cuisine.VEGETARIAN, Vegetarian>} | 859 | {'foo': <Item Cuisine.VEGETARIAN, Vegetarian>} |
316 | 883 | 860 | ||
317 | 884 | If we attempt to marshall something other than one of the above data formats, | 861 | If we attempt to marshall something other than one of the above data formats, |
318 | 885 | a ValueError will be raised. | 862 | a ValueError will be raised. |
319 | 886 | 863 | ||
321 | 887 | >>> dict_marshaller.marshall_from_json_data(u"Test") | 864 | >>> dict_marshaller.marshall_from_json_data("Test") |
322 | 888 | Traceback (most recent call last): | 865 | Traceback (most recent call last): |
323 | 889 | ... | 866 | ... |
325 | 890 | ValueError: got '...', expected dict: ...'Test' | 867 | ValueError: got 'str', expected dict: 'Test' |
326 | 891 | 868 | ||
328 | 892 | >>> dict_marshaller.marshall_from_request(u"Test") | 869 | >>> dict_marshaller.marshall_from_request("Test") |
329 | 893 | Traceback (most recent call last): | 870 | Traceback (most recent call last): |
330 | 894 | ... | 871 | ... |
332 | 895 | ValueError: got '[...'Test']', list of name,value pairs | 872 | ValueError: got '['Test']', list of name,value pairs |
333 | 896 | 873 | ||
334 | 897 | None is passed through though. | 874 | None is passed through though. |
335 | 898 | 875 | ||
336 | @@ -905,25 +882,25 @@ None is passed through though. | |||
337 | 905 | ValueError is also raised if one of the value in the list doesn't | 882 | ValueError is also raised if one of the value in the list doesn't |
338 | 906 | validate against the more specific marshaller. | 883 | validate against the more specific marshaller. |
339 | 907 | 884 | ||
341 | 908 | >>> list_marshaller.marshall_from_json_data([u'Text', 1, 2]) | 885 | >>> list_marshaller.marshall_from_json_data(['Text', 1, 2]) |
342 | 909 | Traceback (most recent call last): | 886 | Traceback (most recent call last): |
343 | 910 | ... | 887 | ... |
345 | 911 | ValueError: got 'int', expected ...: 1 | 888 | ValueError: got 'int', expected str: 1 |
346 | 912 | 889 | ||
347 | 913 | >>> show_ValueError(choice_list_marshaller.marshall_from_request, | 890 | >>> show_ValueError(choice_list_marshaller.marshall_from_request, |
349 | 914 | ... [u'Vegetarian', u'NoSuchChoice']) | 891 | ... ['Vegetarian', 'NoSuchChoice']) |
350 | 915 | ValueError: Invalid value "NoSuchChoice"... | 892 | ValueError: Invalid value "NoSuchChoice"... |
351 | 916 | 893 | ||
352 | 917 | ValueError is also raised if one of the keys or values in the dict doesn't | 894 | ValueError is also raised if one of the keys or values in the dict doesn't |
353 | 918 | validate against the more specific marshaller. | 895 | validate against the more specific marshaller. |
354 | 919 | 896 | ||
356 | 920 | >>> dict_marshaller.marshall_from_json_data({1: u"Vegetarian"}) | 897 | >>> dict_marshaller.marshall_from_json_data({1: "Vegetarian"}) |
357 | 921 | Traceback (most recent call last): | 898 | Traceback (most recent call last): |
358 | 922 | ... | 899 | ... |
360 | 923 | ValueError: got 'int', expected ...: 1 | 900 | ValueError: got 'int', expected str: 1 |
361 | 924 | 901 | ||
362 | 925 | >>> show_ValueError(dict_marshaller.marshall_from_request, | 902 | >>> show_ValueError(dict_marshaller.marshall_from_request, |
364 | 926 | ... {u'foo': u'NoSuchChoice'}) | 903 | ... {'foo': 'NoSuchChoice'}) |
365 | 927 | ValueError: Invalid value "NoSuchChoice"... | 904 | ValueError: Invalid value "NoSuchChoice"... |
366 | 928 | 905 | ||
367 | 929 | The return type is correctly typed to the concrete collection. | 906 | The return type is correctly typed to the concrete collection. |
368 | @@ -938,14 +915,14 @@ The return type is correctly typed to the concrete collection. | |||
369 | 938 | [<Item Cuisine.VEGETARIAN, Vegetarian>, <Item Cuisine.DESSERT, Dessert>] | 915 | [<Item Cuisine.VEGETARIAN, Vegetarian>, <Item Cuisine.DESSERT, Dessert>] |
370 | 939 | 916 | ||
371 | 940 | >>> result = choice_list_marshaller.marshall_from_request( | 917 | >>> result = choice_list_marshaller.marshall_from_request( |
373 | 941 | ... [u'Vegetarian', u'General']) | 918 | ... ['Vegetarian', 'General']) |
374 | 942 | >>> print(type(result).__name__) | 919 | >>> print(type(result).__name__) |
375 | 943 | list | 920 | list |
377 | 944 | >>> pprint_list(result) | 921 | >>> result |
378 | 945 | [<Item Cuisine.VEGETARIAN, Vegetarian>, <Item Cuisine.GENERAL, General>] | 922 | [<Item Cuisine.VEGETARIAN, Vegetarian>, <Item Cuisine.GENERAL, General>] |
379 | 946 | 923 | ||
380 | 947 | >>> marshalled_dict = dict_marshaller.marshall_from_json_data( | 924 | >>> marshalled_dict = dict_marshaller.marshall_from_json_data( |
382 | 948 | ... {u'foo': u'Vegetarian', u'bar': u'General'}) | 925 | ... {'foo': 'Vegetarian', 'bar': 'General'}) |
383 | 949 | >>> print(type(marshalled_dict).__name__) | 926 | >>> print(type(marshalled_dict).__name__) |
384 | 950 | dict | 927 | dict |
385 | 951 | >>> pprint_dict(marshalled_dict) | 928 | >>> pprint_dict(marshalled_dict) |
386 | @@ -958,16 +935,16 @@ underlying type are then followed. When marshalling dicts, the | |||
387 | 958 | list elements are name,value strings which are pulled apart and | 935 | list elements are name,value strings which are pulled apart and |
388 | 959 | used to populate the dict. | 936 | used to populate the dict. |
389 | 960 | 937 | ||
391 | 961 | >>> pprint_list(list_marshaller.marshall_from_request([u'1', u'2'])) | 938 | >>> list_marshaller.marshall_from_request(['1', '2']) |
392 | 962 | ['1', '2'] | 939 | ['1', '2'] |
394 | 963 | >>> pprint_list(list_marshaller.marshall_from_request('["1", "2"]')) | 940 | >>> list_marshaller.marshall_from_request('["1", "2"]') |
395 | 964 | ['1', '2'] | 941 | ['1', '2'] |
396 | 965 | 942 | ||
397 | 966 | >>> pprint_dict( | 943 | >>> pprint_dict( |
398 | 967 | ... dict_marshaller.marshall_from_request('["foo,Vegetarian"]')) | 944 | ... dict_marshaller.marshall_from_request('["foo,Vegetarian"]')) |
399 | 968 | {'foo': <Item Cuisine.VEGETARIAN, Vegetarian>} | 945 | {'foo': <Item Cuisine.VEGETARIAN, Vegetarian>} |
400 | 969 | 946 | ||
402 | 970 | >>> tuple_marshaller.marshall_from_request([u'1', u'2']) | 947 | >>> tuple_marshaller.marshall_from_request(['1', '2']) |
403 | 971 | (1, 2) | 948 | (1, 2) |
404 | 972 | 949 | ||
405 | 973 | Except that 'null' still returns None. | 950 | Except that 'null' still returns None. |
406 | @@ -985,7 +962,7 @@ single-element list. | |||
407 | 985 | >>> tuple_marshaller.marshall_from_request('1') | 962 | >>> tuple_marshaller.marshall_from_request('1') |
408 | 986 | (1,) | 963 | (1,) |
409 | 987 | 964 | ||
411 | 988 | >>> pprint_list(list_marshaller.marshall_from_request('test')) | 965 | >>> list_marshaller.marshall_from_request('test') |
412 | 989 | ['test'] | 966 | ['test'] |
413 | 990 | 967 | ||
414 | 991 | The unmarshall() method will return a list containing the unmarshalled | 968 | The unmarshall() method will return a list containing the unmarshalled |
I reviewed the diff and the changes look good to me. đź‘Ť