Merge ~cjwatson/lazr.restful:remove-manual-pretty-printing into lazr.restful: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)
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-marshallers.rst

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.
Revision history for this message
Guruprasad (lgp171188) wrote :

I reviewed the diff and the changes look good to me. đź‘Ť

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/src/lazr/restful/docs/webservice-marshallers.rst b/src/lazr/restful/docs/webservice-marshallers.rst
index 0d18d2a..b32e0d2 100644
--- a/src/lazr/restful/docs/webservice-marshallers.rst
+++ b/src/lazr/restful/docs/webservice-marshallers.rst
@@ -21,33 +21,11 @@ application root.
21We also define some helpers to print values in a way that is unambiguous21We also define some helpers to print values in a way that is unambiguous
22across Python versions.22across Python versions.
2323
24 >>> import six
25
26 >>> def pformat_value(value):
27 ... """Pretty-format a single value."""
28 ... if isinstance(value, six.text_type):
29 ... value = value.encode('unicode_escape').decode('ASCII')
30 ... if "'" in value and '"' not in value:
31 ... return '"%s"' % value
32 ... else:
33 ... return "'%s'" % value.replace("'", "\\'")
34 ... else:
35 ... return repr(value)
36
37 >>> def pprint_value(value):
38 ... """Pretty-print a single value."""
39 ... print(pformat_value(value))
40
41 >>> def pprint_list(lst):
42 ... print('[', end='')
43 ... print(', '.join(pformat_value(value) for value in lst), end='')
44 ... print(']')
45
46 >>> def pprint_dict(d):24 >>> def pprint_dict(d):
47 ... print('{', end='')25 ... print('{', end='')
48 ... print(26 ... print(
49 ... ', '.join(27 ... ', '.join(
50 ... '%s: %s' % (pformat_value(key), pformat_value(value))28 ... '%r: %r' % (key, value)
51 ... for key, value in sorted(d.items())),29 ... for key, value in sorted(d.items())),
52 ... end='')30 ... end='')
53 ... print('}')31 ... print('}')
@@ -90,7 +68,7 @@ implementation, the value is returned unchanged.
90 'foo'68 'foo'
91 >>> marshaller.marshall_from_json_data(4)69 >>> marshaller.marshall_from_json_data(4)
92 470 4
93 >>> print(marshaller.marshall_from_json_data(u"unicode\u2122"))71 >>> print(marshaller.marshall_from_json_data("unicode\u2122"))
94 unicode™72 unicode™
95 >>> marshaller.marshall_from_json_data("")73 >>> marshaller.marshall_from_json_data("")
96 ''74 ''
@@ -114,16 +92,16 @@ string, the resulting value is passed on to marshall_from_json_data().
114 >>> marshaller.marshall_from_request("false")92 >>> marshaller.marshall_from_request("false")
115 False93 False
116 >>> marshaller.marshall_from_request('["True", "False"]')94 >>> marshaller.marshall_from_request('["True", "False"]')
117 [...'True', ...'False']95 ['True', 'False']
118 >>> marshaller.marshall_from_request("1")96 >>> marshaller.marshall_from_request("1")
119 197 1
120 >>> marshaller.marshall_from_request("-10.5")98 >>> marshaller.marshall_from_request("-10.5")
121 -10.599 -10.5
122 >>> pprint_value(marshaller.marshall_from_request('"a string"'))100 >>> marshaller.marshall_from_request('"a string"')
123 'a string'101 'a string'
124 >>> pprint_value(marshaller.marshall_from_request('"false"'))102 >>> marshaller.marshall_from_request('"false"')
125 'false'103 'false'
126 >>> pprint_value(marshaller.marshall_from_request('"null"'))104 >>> marshaller.marshall_from_request('"null"')
127 'null'105 'null'
128106
129Invalid JSON-encoded strings are interpreted as string literals and107Invalid JSON-encoded strings are interpreted as string literals and
@@ -131,7 +109,7 @@ passed on directly to marshall_from_json_data(). That's for the
131convenience of web clients, they don't need to encode string values in109convenience of web clients, they don't need to encode string values in
132quotes, or can pass lists using multiple key-value pairs.110quotes, or can pass lists using multiple key-value pairs.
133111
134 >>> pprint_value(marshaller.marshall_from_request(u"a string"))112 >>> marshaller.marshall_from_request("a string")
135 'a string'113 'a string'
136 >>> marshaller.marshall_from_request('False')114 >>> marshaller.marshall_from_request('False')
137 'False'115 'False'
@@ -212,9 +190,9 @@ None is passed through though.
212Booleans are encoded using the standard JSON representation of 'true' or190Booleans are encoded using the standard JSON representation of 'true' or
213'false'.191'false'.
214192
215 >>> marshaller.marshall_from_request(u"true")193 >>> marshaller.marshall_from_request("true")
216 True194 True
217 >>> marshaller.marshall_from_request(u"false")195 >>> marshaller.marshall_from_request("false")
218 False196 False
219197
220 >>> marshaller.marshall_from_request('True')198 >>> marshaller.marshall_from_request('True')
@@ -250,7 +228,7 @@ Integers are encoded using strings when in a request.
250228
251 >>> marshaller.marshall_from_request("4")229 >>> marshaller.marshall_from_request("4")
252 4230 4
253 >>> marshaller.marshall_from_request(u"-4")231 >>> marshaller.marshall_from_request("-4")
254 -4232 -4
255233
256It raises a ValueError if the value cannot be converted to an integer.234It raises a ValueError if the value cannot be converted to an integer.
@@ -269,15 +247,15 @@ Note that python octal and hexadecimal syntax isn't supported.
269247
270(This would 13 in octal notation.)248(This would 13 in octal notation.)
271249
272 >>> marshaller.marshall_from_request(u"015")250 >>> marshaller.marshall_from_request("015")
273 Traceback (most recent call last):251 Traceback (most recent call last):
274 ...252 ...
275 ValueError: got '...', expected int: ...'015'253 ValueError: got 'str', expected int: '015'
276254
277 >>> marshaller.marshall_from_request(u"0x04")255 >>> marshaller.marshall_from_request("0x04")
278 Traceback (most recent call last):256 Traceback (most recent call last):
279 ...257 ...
280 ValueError: got '...', expected int: ...'0x04'258 ValueError: got 'str', expected int: '0x04'
281259
282Float260Float
283-----261-----
@@ -312,11 +290,11 @@ And integers are automatically converted to a float.
312290
313Floats are encoded using the standard JSON representation.291Floats are encoded using the standard JSON representation.
314292
315 >>> marshaller.marshall_from_request(u"1.2")293 >>> marshaller.marshall_from_request("1.2")
316 1.2294 1.2
317 >>> marshaller.marshall_from_request(u"-1.2")295 >>> marshaller.marshall_from_request("-1.2")
318 -1.2296 -1.2
319 >>> marshaller.marshall_from_request(u"-1")297 >>> marshaller.marshall_from_request("-1")
320 -1.0298 -1.0
321299
322 >>> marshaller.marshall_from_request('True')300 >>> marshaller.marshall_from_request('True')
@@ -425,16 +403,16 @@ string. A ValueError is raised when that's not the case.
425 >>> verifyObject(IFieldMarshaller, marshaller)403 >>> verifyObject(IFieldMarshaller, marshaller)
426 True404 True
427405
428 >>> pprint_value(marshaller.marshall_from_json_data(u"Test"))406 >>> marshaller.marshall_from_json_data("Test")
429 'Test'407 'Test'
430 >>> marshaller.marshall_from_json_data(1.0)408 >>> marshaller.marshall_from_json_data(1.0)
431 Traceback (most recent call last):409 Traceback (most recent call last):
432 ...410 ...
433 ValueError: got 'float', expected ...: 1.0411 ValueError: got 'float', expected str: 1.0
434 >>> marshaller.marshall_from_json_data(b'Test')412 >>> marshaller.marshall_from_json_data(b'Test')
435 Traceback (most recent call last):413 Traceback (most recent call last):
436 ...414 ...
437 ValueError: got '...', expected ...: ...'Test'415 ValueError: got 'bytes', expected str: b'Test'
438416
439None is passed through though.417None is passed through though.
440418
@@ -444,13 +422,13 @@ None is passed through though.
444When coming from the request, everything is interpreted as a unicode422When coming from the request, everything is interpreted as a unicode
445string:423string:
446424
447 >>> pprint_value(marshaller.marshall_from_request('a string'))425 >>> marshaller.marshall_from_request('a string')
448 'a string'426 'a string'
449 >>> pprint_value(marshaller.marshall_from_request(['a', 'b']))427 >>> marshaller.marshall_from_request(['a', 'b'])
450 "['a', 'b']"428 "['a', 'b']"
451 >>> pprint_value(marshaller.marshall_from_request('true'))429 >>> marshaller.marshall_from_request('true')
452 'True'430 'True'
453 >>> pprint_value(marshaller.marshall_from_request(''))431 >>> marshaller.marshall_from_request('')
454 ''432 ''
455433
456Except that 'null' still returns None.434Except that 'null' still returns None.
@@ -460,11 +438,11 @@ Except that 'null' still returns None.
460438
461Line breaks coming from the request are normalized to LF.439Line breaks coming from the request are normalized to LF.
462440
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')
464 'abc\n\ndef\n'442 'abc\n\ndef\n'
465 >>> pprint_value(marshaller.marshall_from_request('abc\n\ndef\n'))443 >>> marshaller.marshall_from_request('abc\n\ndef\n')
466 'abc\n\ndef\n'444 'abc\n\ndef\n'
467 >>> pprint_value(marshaller.marshall_from_request('abc\r\rdef\r'))445 >>> marshaller.marshall_from_request('abc\r\rdef\r')
468 'abc\n\ndef\n'446 'abc\n\ndef\n'
469447
470Bytes448Bytes
@@ -480,9 +458,9 @@ a string, a ValueError is raised.
480 >>> verifyObject(IFieldMarshaller, marshaller)458 >>> verifyObject(IFieldMarshaller, marshaller)
481 True459 True
482460
483 >>> pprint_value(marshaller.marshall_from_json_data(u"Test"))461 >>> marshaller.marshall_from_json_data("Test")
484 b'Test'462 b'Test'
485 >>> pprint_value(marshaller.marshall_from_json_data(u'int\xe9ressant'))463 >>> marshaller.marshall_from_json_data('int\xe9ressant')
486 b'int\xc3\xa9ressant'464 b'int\xc3\xa9ressant'
487 >>> marshaller.marshall_from_json_data(1.0)465 >>> marshaller.marshall_from_json_data(1.0)
488 Traceback (most recent call last):466 Traceback (most recent call last):
@@ -497,21 +475,20 @@ Again, except for None which is passed through.
497When coming over the request, the value is also converted into a UTF-8475When coming over the request, the value is also converted into a UTF-8
498encoded string, with no JSON decoding.476encoded string, with no JSON decoding.
499477
500 >>> pprint_value(marshaller.marshall_from_request(u"Test"))478 >>> marshaller.marshall_from_request("Test")
501 b'Test'479 b'Test'
502 >>> pprint_value(marshaller.marshall_from_request(u'int\xe9ressant'))480 >>> marshaller.marshall_from_request('int\xe9ressant')
503 b'int\xc3\xa9ressant'481 b'int\xc3\xa9ressant'
504 >>> pprint_value(marshaller.marshall_from_request(b'1.0'))482 >>> marshaller.marshall_from_request(b'1.0')
505 b'1.0'483 b'1.0'
506 >>> pprint_value(marshaller.marshall_from_request(b'"not JSON"'))484 >>> marshaller.marshall_from_request(b'"not JSON"')
507 b'"not JSON"'485 b'"not JSON"'
508486
509Since multipart/form-data can be used to upload data, file-like objects487Since multipart/form-data can be used to upload data, file-like objects
510are read.488are read.
511489
512 >>> from io import BytesIO490 >>> from io import BytesIO
513 >>> pprint_value(491 >>> marshaller.marshall_from_request(BytesIO(b'A line of data'))
514 ... marshaller.marshall_from_request(BytesIO(b'A line of data')))
515 b'A line of data'492 b'A line of data'
516493
517Bytes field used in an entry are stored in the librarian, so their494Bytes field used in an entry are stored in the librarian, so their
@@ -547,12 +524,12 @@ ASCIILine is a subclass of Bytes but is marshalled like text.
547524
548Unicode objects remain Unicode objects.525Unicode objects remain Unicode objects.
549526
550 >>> pprint_value(marshaller.marshall_from_json_data(u"Test"))527 >>> marshaller.marshall_from_json_data("Test")
551 'Test'528 'Test'
552529
553Note that the marshaller accepts character values where bit 7 is set.530Note that the marshaller accepts character values where bit 7 is set.
554531
555 >>> print(marshaller.marshall_from_json_data(u'int\xe9ressant'))532 >>> print(marshaller.marshall_from_json_data('int\xe9ressant'))
556 intéressant533 intéressant
557534
558Non-string values like floats are rejected.535Non-string values like floats are rejected.
@@ -560,7 +537,7 @@ Non-string values like floats are rejected.
560 >>> marshaller.marshall_from_json_data(1.0)537 >>> marshaller.marshall_from_json_data(1.0)
561 Traceback (most recent call last):538 Traceback (most recent call last):
562 ...539 ...
563 ValueError: got 'float', expected ...: 1.0540 ValueError: got 'float', expected str: 1.0
564541
565None is passed through.542None is passed through.
566543
@@ -570,17 +547,17 @@ None is passed through.
570When coming from the request, everything is interpreted as a unicode547When coming from the request, everything is interpreted as a unicode
571string:548string:
572549
573 >>> pprint_value(marshaller.marshall_from_request('a string'))550 >>> marshaller.marshall_from_request('a string')
574 'a string'551 'a string'
575 >>> pprint_value(marshaller.marshall_from_request(['a', 'b']))552 >>> marshaller.marshall_from_request(['a', 'b'])
576 "['a', 'b']"553 "['a', 'b']"
577 >>> pprint_value(marshaller.marshall_from_request('true'))554 >>> marshaller.marshall_from_request('true')
578 'True'555 'True'
579 >>> pprint_value(marshaller.marshall_from_request(''))556 >>> marshaller.marshall_from_request('')
580 ''557 ''
581 >>> print(marshaller.marshall_from_request(u'int\xe9ressant'))558 >>> print(marshaller.marshall_from_request('int\xe9ressant'))
582 intéressant559 intéressant
583 >>> pprint_value(marshaller.marshall_from_request('1.0'))560 >>> marshaller.marshall_from_request('1.0')
584 '1.0'561 '1.0'
585562
586But again, 'null' is returned as None.563But again, 'null' is returned as None.
@@ -666,7 +643,7 @@ display them the way we want.
666 ... try:643 ... try:
667 ... callable(*args)644 ... callable(*args)
668 ... except ValueError as e:645 ... except ValueError as e:
669 ... print('ValueError:', six.text_type(e))646 ... print('ValueError:', str(e))
670647
671648
672Choice of EnumeratedTypes649Choice of EnumeratedTypes
@@ -858,41 +835,41 @@ For sequences, the only JSON representation for the collection itself is a
858list, since that's the only sequence type available in JSON. Anything else835list, since that's the only sequence type available in JSON. Anything else
859will raise a ValueError.836will raise a ValueError.
860837
861 >>> pprint_list(list_marshaller.marshall_from_json_data([u"Test"]))838 >>> list_marshaller.marshall_from_json_data(["Test"])
862 ['Test']839 ['Test']
863840
864 >>> list_marshaller.marshall_from_json_data(u"Test")841 >>> list_marshaller.marshall_from_json_data("Test")
865 Traceback (most recent call last):842 Traceback (most recent call last):
866 ...843 ...
867 ValueError: got '...', expected list: ...'Test'844 ValueError: got 'str', expected list: 'Test'
868845
869For dicts, we support marshalling from sequences of (name, value) pairs as846For dicts, we support marshalling from sequences of (name, value) pairs as
870well as from dicts or even strings which are interpreted as single element847well as from dicts or even strings which are interpreted as single element
871lists.848lists.
872849
873 >>> pprint_dict(850 >>> pprint_dict(
874 ... dict_marshaller.marshall_from_json_data({u"foo": u"Vegetarian"}))851 ... dict_marshaller.marshall_from_json_data({"foo": "Vegetarian"}))
875 {'foo': <Item Cuisine.VEGETARIAN, Vegetarian>}852 {'foo': <Item Cuisine.VEGETARIAN, Vegetarian>}
876853
877 >>> pprint_dict(854 >>> pprint_dict(
878 ... dict_marshaller.marshall_from_json_data([(u"foo", u"Vegetarian")]))855 ... dict_marshaller.marshall_from_json_data([("foo", "Vegetarian")]))
879 {'foo': <Item Cuisine.VEGETARIAN, Vegetarian>}856 {'foo': <Item Cuisine.VEGETARIAN, Vegetarian>}
880857
881 >>> pprint_dict(dict_marshaller.marshall_from_request(u"foo,Vegetarian"))858 >>> pprint_dict(dict_marshaller.marshall_from_request("foo,Vegetarian"))
882 {'foo': <Item Cuisine.VEGETARIAN, Vegetarian>}859 {'foo': <Item Cuisine.VEGETARIAN, Vegetarian>}
883860
884If we attempt to marshall something other than one of the above data formats,861If we attempt to marshall something other than one of the above data formats,
885a ValueError will be raised.862a ValueError will be raised.
886863
887 >>> dict_marshaller.marshall_from_json_data(u"Test")864 >>> dict_marshaller.marshall_from_json_data("Test")
888 Traceback (most recent call last):865 Traceback (most recent call last):
889 ...866 ...
890 ValueError: got '...', expected dict: ...'Test'867 ValueError: got 'str', expected dict: 'Test'
891868
892 >>> dict_marshaller.marshall_from_request(u"Test")869 >>> dict_marshaller.marshall_from_request("Test")
893 Traceback (most recent call last):870 Traceback (most recent call last):
894 ...871 ...
895 ValueError: got '[...'Test']', list of name,value pairs872 ValueError: got '['Test']', list of name,value pairs
896873
897None is passed through though.874None is passed through though.
898875
@@ -905,25 +882,25 @@ None is passed through though.
905ValueError is also raised if one of the value in the list doesn't882ValueError is also raised if one of the value in the list doesn't
906validate against the more specific marshaller.883validate against the more specific marshaller.
907884
908 >>> list_marshaller.marshall_from_json_data([u'Text', 1, 2])885 >>> list_marshaller.marshall_from_json_data(['Text', 1, 2])
909 Traceback (most recent call last):886 Traceback (most recent call last):
910 ...887 ...
911 ValueError: got 'int', expected ...: 1888 ValueError: got 'int', expected str: 1
912889
913 >>> show_ValueError(choice_list_marshaller.marshall_from_request,890 >>> show_ValueError(choice_list_marshaller.marshall_from_request,
914 ... [u'Vegetarian', u'NoSuchChoice'])891 ... ['Vegetarian', 'NoSuchChoice'])
915 ValueError: Invalid value "NoSuchChoice"...892 ValueError: Invalid value "NoSuchChoice"...
916893
917ValueError is also raised if one of the keys or values in the dict doesn't894ValueError is also raised if one of the keys or values in the dict doesn't
918validate against the more specific marshaller.895validate against the more specific marshaller.
919896
920 >>> dict_marshaller.marshall_from_json_data({1: u"Vegetarian"})897 >>> dict_marshaller.marshall_from_json_data({1: "Vegetarian"})
921 Traceback (most recent call last):898 Traceback (most recent call last):
922 ...899 ...
923 ValueError: got 'int', expected ...: 1900 ValueError: got 'int', expected str: 1
924901
925 >>> show_ValueError(dict_marshaller.marshall_from_request,902 >>> show_ValueError(dict_marshaller.marshall_from_request,
926 ... {u'foo': u'NoSuchChoice'})903 ... {'foo': 'NoSuchChoice'})
927 ValueError: Invalid value "NoSuchChoice"...904 ValueError: Invalid value "NoSuchChoice"...
928905
929The return type is correctly typed to the concrete collection.906The return type is correctly typed to the concrete collection.
@@ -938,14 +915,14 @@ The return type is correctly typed to the concrete collection.
938 [<Item Cuisine.VEGETARIAN, Vegetarian>, <Item Cuisine.DESSERT, Dessert>]915 [<Item Cuisine.VEGETARIAN, Vegetarian>, <Item Cuisine.DESSERT, Dessert>]
939916
940 >>> result = choice_list_marshaller.marshall_from_request(917 >>> result = choice_list_marshaller.marshall_from_request(
941 ... [u'Vegetarian', u'General'])918 ... ['Vegetarian', 'General'])
942 >>> print(type(result).__name__)919 >>> print(type(result).__name__)
943 list920 list
944 >>> pprint_list(result)921 >>> result
945 [<Item Cuisine.VEGETARIAN, Vegetarian>, <Item Cuisine.GENERAL, General>]922 [<Item Cuisine.VEGETARIAN, Vegetarian>, <Item Cuisine.GENERAL, General>]
946923
947 >>> marshalled_dict = dict_marshaller.marshall_from_json_data(924 >>> marshalled_dict = dict_marshaller.marshall_from_json_data(
948 ... {u'foo': u'Vegetarian', u'bar': u'General'})925 ... {'foo': 'Vegetarian', 'bar': 'General'})
949 >>> print(type(marshalled_dict).__name__)926 >>> print(type(marshalled_dict).__name__)
950 dict927 dict
951 >>> pprint_dict(marshalled_dict)928 >>> pprint_dict(marshalled_dict)
@@ -958,16 +935,16 @@ underlying type are then followed. When marshalling dicts, the
958list elements are name,value strings which are pulled apart and935list elements are name,value strings which are pulled apart and
959used to populate the dict.936used to populate the dict.
960937
961 >>> pprint_list(list_marshaller.marshall_from_request([u'1', u'2']))938 >>> list_marshaller.marshall_from_request(['1', '2'])
962 ['1', '2']939 ['1', '2']
963 >>> pprint_list(list_marshaller.marshall_from_request('["1", "2"]'))940 >>> list_marshaller.marshall_from_request('["1", "2"]')
964 ['1', '2']941 ['1', '2']
965942
966 >>> pprint_dict(943 >>> pprint_dict(
967 ... dict_marshaller.marshall_from_request('["foo,Vegetarian"]'))944 ... dict_marshaller.marshall_from_request('["foo,Vegetarian"]'))
968 {'foo': <Item Cuisine.VEGETARIAN, Vegetarian>}945 {'foo': <Item Cuisine.VEGETARIAN, Vegetarian>}
969946
970 >>> tuple_marshaller.marshall_from_request([u'1', u'2'])947 >>> tuple_marshaller.marshall_from_request(['1', '2'])
971 (1, 2)948 (1, 2)
972949
973Except that 'null' still returns None.950Except that 'null' still returns None.
@@ -985,7 +962,7 @@ single-element list.
985 >>> tuple_marshaller.marshall_from_request('1')962 >>> tuple_marshaller.marshall_from_request('1')
986 (1,)963 (1,)
987964
988 >>> pprint_list(list_marshaller.marshall_from_request('test'))965 >>> list_marshaller.marshall_from_request('test')
989 ['test']966 ['test']
990967
991The unmarshall() method will return a list containing the unmarshalled968The unmarshall() method will return a list containing the unmarshalled

Subscribers

People subscribed via source and target branches