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
1diff --git a/src/lazr/restful/docs/webservice-marshallers.rst b/src/lazr/restful/docs/webservice-marshallers.rst
2index 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 We also define some helpers to print values in a way that is unambiguous
7 across Python versions.
8
9- >>> import six
10-
11- >>> def pformat_value(value):
12- ... """Pretty-format a single value."""
13- ... if isinstance(value, six.text_type):
14- ... value = value.encode('unicode_escape').decode('ASCII')
15- ... if "'" in value and '"' not in value:
16- ... return '"%s"' % value
17- ... else:
18- ... return "'%s'" % value.replace("'", "\\'")
19- ... else:
20- ... return repr(value)
21-
22- >>> def pprint_value(value):
23- ... """Pretty-print a single value."""
24- ... print(pformat_value(value))
25-
26- >>> def pprint_list(lst):
27- ... print('[', end='')
28- ... print(', '.join(pformat_value(value) for value in lst), end='')
29- ... print(']')
30-
31 >>> def pprint_dict(d):
32 ... print('{', end='')
33 ... print(
34 ... ', '.join(
35- ... '%s: %s' % (pformat_value(key), pformat_value(value))
36+ ... '%r: %r' % (key, value)
37 ... for key, value in sorted(d.items())),
38 ... end='')
39 ... print('}')
40@@ -90,7 +68,7 @@ implementation, the value is returned unchanged.
41 'foo'
42 >>> marshaller.marshall_from_json_data(4)
43 4
44- >>> print(marshaller.marshall_from_json_data(u"unicode\u2122"))
45+ >>> print(marshaller.marshall_from_json_data("unicode\u2122"))
46 unicode™
47 >>> marshaller.marshall_from_json_data("")
48 ''
49@@ -114,16 +92,16 @@ string, the resulting value is passed on to marshall_from_json_data().
50 >>> marshaller.marshall_from_request("false")
51 False
52 >>> marshaller.marshall_from_request('["True", "False"]')
53- [...'True', ...'False']
54+ ['True', 'False']
55 >>> marshaller.marshall_from_request("1")
56 1
57 >>> marshaller.marshall_from_request("-10.5")
58 -10.5
59- >>> pprint_value(marshaller.marshall_from_request('"a string"'))
60+ >>> marshaller.marshall_from_request('"a string"')
61 'a string'
62- >>> pprint_value(marshaller.marshall_from_request('"false"'))
63+ >>> marshaller.marshall_from_request('"false"')
64 'false'
65- >>> pprint_value(marshaller.marshall_from_request('"null"'))
66+ >>> marshaller.marshall_from_request('"null"')
67 'null'
68
69 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 convenience of web clients, they don't need to encode string values in
72 quotes, or can pass lists using multiple key-value pairs.
73
74- >>> pprint_value(marshaller.marshall_from_request(u"a string"))
75+ >>> marshaller.marshall_from_request("a string")
76 'a string'
77 >>> marshaller.marshall_from_request('False')
78 'False'
79@@ -212,9 +190,9 @@ None is passed through though.
80 Booleans are encoded using the standard JSON representation of 'true' or
81 'false'.
82
83- >>> marshaller.marshall_from_request(u"true")
84+ >>> marshaller.marshall_from_request("true")
85 True
86- >>> marshaller.marshall_from_request(u"false")
87+ >>> marshaller.marshall_from_request("false")
88 False
89
90 >>> marshaller.marshall_from_request('True')
91@@ -250,7 +228,7 @@ Integers are encoded using strings when in a request.
92
93 >>> marshaller.marshall_from_request("4")
94 4
95- >>> marshaller.marshall_from_request(u"-4")
96+ >>> marshaller.marshall_from_request("-4")
97 -4
98
99 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
102 (This would 13 in octal notation.)
103
104- >>> marshaller.marshall_from_request(u"015")
105+ >>> marshaller.marshall_from_request("015")
106 Traceback (most recent call last):
107 ...
108- ValueError: got '...', expected int: ...'015'
109+ ValueError: got 'str', expected int: '015'
110
111- >>> marshaller.marshall_from_request(u"0x04")
112+ >>> marshaller.marshall_from_request("0x04")
113 Traceback (most recent call last):
114 ...
115- ValueError: got '...', expected int: ...'0x04'
116+ ValueError: got 'str', expected int: '0x04'
117
118 Float
119 -----
120@@ -312,11 +290,11 @@ And integers are automatically converted to a float.
121
122 Floats are encoded using the standard JSON representation.
123
124- >>> marshaller.marshall_from_request(u"1.2")
125+ >>> marshaller.marshall_from_request("1.2")
126 1.2
127- >>> marshaller.marshall_from_request(u"-1.2")
128+ >>> marshaller.marshall_from_request("-1.2")
129 -1.2
130- >>> marshaller.marshall_from_request(u"-1")
131+ >>> marshaller.marshall_from_request("-1")
132 -1.0
133
134 >>> marshaller.marshall_from_request('True')
135@@ -425,16 +403,16 @@ string. A ValueError is raised when that's not the case.
136 >>> verifyObject(IFieldMarshaller, marshaller)
137 True
138
139- >>> pprint_value(marshaller.marshall_from_json_data(u"Test"))
140+ >>> marshaller.marshall_from_json_data("Test")
141 'Test'
142 >>> marshaller.marshall_from_json_data(1.0)
143 Traceback (most recent call last):
144 ...
145- ValueError: got 'float', expected ...: 1.0
146+ ValueError: got 'float', expected str: 1.0
147 >>> marshaller.marshall_from_json_data(b'Test')
148 Traceback (most recent call last):
149 ...
150- ValueError: got '...', expected ...: ...'Test'
151+ ValueError: got 'bytes', expected str: b'Test'
152
153 None is passed through though.
154
155@@ -444,13 +422,13 @@ None is passed through though.
156 When coming from the request, everything is interpreted as a unicode
157 string:
158
159- >>> pprint_value(marshaller.marshall_from_request('a string'))
160+ >>> marshaller.marshall_from_request('a string')
161 'a string'
162- >>> pprint_value(marshaller.marshall_from_request(['a', 'b']))
163+ >>> marshaller.marshall_from_request(['a', 'b'])
164 "['a', 'b']"
165- >>> pprint_value(marshaller.marshall_from_request('true'))
166+ >>> marshaller.marshall_from_request('true')
167 'True'
168- >>> pprint_value(marshaller.marshall_from_request(''))
169+ >>> marshaller.marshall_from_request('')
170 ''
171
172 Except that 'null' still returns None.
173@@ -460,11 +438,11 @@ Except that 'null' still returns None.
174
175 Line breaks coming from the request are normalized to LF.
176
177- >>> pprint_value(marshaller.marshall_from_request('abc\r\n\r\ndef\r\n'))
178+ >>> marshaller.marshall_from_request('abc\r\n\r\ndef\r\n')
179 'abc\n\ndef\n'
180- >>> pprint_value(marshaller.marshall_from_request('abc\n\ndef\n'))
181+ >>> marshaller.marshall_from_request('abc\n\ndef\n')
182 'abc\n\ndef\n'
183- >>> pprint_value(marshaller.marshall_from_request('abc\r\rdef\r'))
184+ >>> marshaller.marshall_from_request('abc\r\rdef\r')
185 'abc\n\ndef\n'
186
187 Bytes
188@@ -480,9 +458,9 @@ a string, a ValueError is raised.
189 >>> verifyObject(IFieldMarshaller, marshaller)
190 True
191
192- >>> pprint_value(marshaller.marshall_from_json_data(u"Test"))
193+ >>> marshaller.marshall_from_json_data("Test")
194 b'Test'
195- >>> pprint_value(marshaller.marshall_from_json_data(u'int\xe9ressant'))
196+ >>> marshaller.marshall_from_json_data('int\xe9ressant')
197 b'int\xc3\xa9ressant'
198 >>> marshaller.marshall_from_json_data(1.0)
199 Traceback (most recent call last):
200@@ -497,21 +475,20 @@ Again, except for None which is passed through.
201 When coming over the request, the value is also converted into a UTF-8
202 encoded string, with no JSON decoding.
203
204- >>> pprint_value(marshaller.marshall_from_request(u"Test"))
205+ >>> marshaller.marshall_from_request("Test")
206 b'Test'
207- >>> pprint_value(marshaller.marshall_from_request(u'int\xe9ressant'))
208+ >>> marshaller.marshall_from_request('int\xe9ressant')
209 b'int\xc3\xa9ressant'
210- >>> pprint_value(marshaller.marshall_from_request(b'1.0'))
211+ >>> marshaller.marshall_from_request(b'1.0')
212 b'1.0'
213- >>> pprint_value(marshaller.marshall_from_request(b'"not JSON"'))
214+ >>> marshaller.marshall_from_request(b'"not JSON"')
215 b'"not JSON"'
216
217 Since multipart/form-data can be used to upload data, file-like objects
218 are read.
219
220 >>> from io import BytesIO
221- >>> pprint_value(
222- ... marshaller.marshall_from_request(BytesIO(b'A line of data')))
223+ >>> marshaller.marshall_from_request(BytesIO(b'A line of data'))
224 b'A line of data'
225
226 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
229 Unicode objects remain Unicode objects.
230
231- >>> pprint_value(marshaller.marshall_from_json_data(u"Test"))
232+ >>> marshaller.marshall_from_json_data("Test")
233 'Test'
234
235 Note that the marshaller accepts character values where bit 7 is set.
236
237- >>> print(marshaller.marshall_from_json_data(u'int\xe9ressant'))
238+ >>> print(marshaller.marshall_from_json_data('int\xe9ressant'))
239 intéressant
240
241 Non-string values like floats are rejected.
242@@ -560,7 +537,7 @@ Non-string values like floats are rejected.
243 >>> marshaller.marshall_from_json_data(1.0)
244 Traceback (most recent call last):
245 ...
246- ValueError: got 'float', expected ...: 1.0
247+ ValueError: got 'float', expected str: 1.0
248
249 None is passed through.
250
251@@ -570,17 +547,17 @@ None is passed through.
252 When coming from the request, everything is interpreted as a unicode
253 string:
254
255- >>> pprint_value(marshaller.marshall_from_request('a string'))
256+ >>> marshaller.marshall_from_request('a string')
257 'a string'
258- >>> pprint_value(marshaller.marshall_from_request(['a', 'b']))
259+ >>> marshaller.marshall_from_request(['a', 'b'])
260 "['a', 'b']"
261- >>> pprint_value(marshaller.marshall_from_request('true'))
262+ >>> marshaller.marshall_from_request('true')
263 'True'
264- >>> pprint_value(marshaller.marshall_from_request(''))
265+ >>> marshaller.marshall_from_request('')
266 ''
267- >>> print(marshaller.marshall_from_request(u'int\xe9ressant'))
268+ >>> print(marshaller.marshall_from_request('int\xe9ressant'))
269 intéressant
270- >>> pprint_value(marshaller.marshall_from_request('1.0'))
271+ >>> marshaller.marshall_from_request('1.0')
272 '1.0'
273
274 But again, 'null' is returned as None.
275@@ -666,7 +643,7 @@ display them the way we want.
276 ... try:
277 ... callable(*args)
278 ... except ValueError as e:
279- ... print('ValueError:', six.text_type(e))
280+ ... print('ValueError:', str(e))
281
282
283 Choice of EnumeratedTypes
284@@ -858,41 +835,41 @@ For sequences, the only JSON representation for the collection itself is a
285 list, since that's the only sequence type available in JSON. Anything else
286 will raise a ValueError.
287
288- >>> pprint_list(list_marshaller.marshall_from_json_data([u"Test"]))
289+ >>> list_marshaller.marshall_from_json_data(["Test"])
290 ['Test']
291
292- >>> list_marshaller.marshall_from_json_data(u"Test")
293+ >>> list_marshaller.marshall_from_json_data("Test")
294 Traceback (most recent call last):
295 ...
296- ValueError: got '...', expected list: ...'Test'
297+ ValueError: got 'str', expected list: 'Test'
298
299 For dicts, we support marshalling from sequences of (name, value) pairs as
300 well as from dicts or even strings which are interpreted as single element
301 lists.
302
303 >>> pprint_dict(
304- ... dict_marshaller.marshall_from_json_data({u"foo": u"Vegetarian"}))
305+ ... dict_marshaller.marshall_from_json_data({"foo": "Vegetarian"}))
306 {'foo': <Item Cuisine.VEGETARIAN, Vegetarian>}
307
308 >>> pprint_dict(
309- ... dict_marshaller.marshall_from_json_data([(u"foo", u"Vegetarian")]))
310+ ... dict_marshaller.marshall_from_json_data([("foo", "Vegetarian")]))
311 {'foo': <Item Cuisine.VEGETARIAN, Vegetarian>}
312
313- >>> pprint_dict(dict_marshaller.marshall_from_request(u"foo,Vegetarian"))
314+ >>> pprint_dict(dict_marshaller.marshall_from_request("foo,Vegetarian"))
315 {'foo': <Item Cuisine.VEGETARIAN, Vegetarian>}
316
317 If we attempt to marshall something other than one of the above data formats,
318 a ValueError will be raised.
319
320- >>> dict_marshaller.marshall_from_json_data(u"Test")
321+ >>> dict_marshaller.marshall_from_json_data("Test")
322 Traceback (most recent call last):
323 ...
324- ValueError: got '...', expected dict: ...'Test'
325+ ValueError: got 'str', expected dict: 'Test'
326
327- >>> dict_marshaller.marshall_from_request(u"Test")
328+ >>> dict_marshaller.marshall_from_request("Test")
329 Traceback (most recent call last):
330 ...
331- ValueError: got '[...'Test']', list of name,value pairs
332+ ValueError: got '['Test']', list of name,value pairs
333
334 None is passed through though.
335
336@@ -905,25 +882,25 @@ None is passed through though.
337 ValueError is also raised if one of the value in the list doesn't
338 validate against the more specific marshaller.
339
340- >>> list_marshaller.marshall_from_json_data([u'Text', 1, 2])
341+ >>> list_marshaller.marshall_from_json_data(['Text', 1, 2])
342 Traceback (most recent call last):
343 ...
344- ValueError: got 'int', expected ...: 1
345+ ValueError: got 'int', expected str: 1
346
347 >>> show_ValueError(choice_list_marshaller.marshall_from_request,
348- ... [u'Vegetarian', u'NoSuchChoice'])
349+ ... ['Vegetarian', 'NoSuchChoice'])
350 ValueError: Invalid value "NoSuchChoice"...
351
352 ValueError is also raised if one of the keys or values in the dict doesn't
353 validate against the more specific marshaller.
354
355- >>> dict_marshaller.marshall_from_json_data({1: u"Vegetarian"})
356+ >>> dict_marshaller.marshall_from_json_data({1: "Vegetarian"})
357 Traceback (most recent call last):
358 ...
359- ValueError: got 'int', expected ...: 1
360+ ValueError: got 'int', expected str: 1
361
362 >>> show_ValueError(dict_marshaller.marshall_from_request,
363- ... {u'foo': u'NoSuchChoice'})
364+ ... {'foo': 'NoSuchChoice'})
365 ValueError: Invalid value "NoSuchChoice"...
366
367 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 [<Item Cuisine.VEGETARIAN, Vegetarian>, <Item Cuisine.DESSERT, Dessert>]
370
371 >>> result = choice_list_marshaller.marshall_from_request(
372- ... [u'Vegetarian', u'General'])
373+ ... ['Vegetarian', 'General'])
374 >>> print(type(result).__name__)
375 list
376- >>> pprint_list(result)
377+ >>> result
378 [<Item Cuisine.VEGETARIAN, Vegetarian>, <Item Cuisine.GENERAL, General>]
379
380 >>> marshalled_dict = dict_marshaller.marshall_from_json_data(
381- ... {u'foo': u'Vegetarian', u'bar': u'General'})
382+ ... {'foo': 'Vegetarian', 'bar': 'General'})
383 >>> print(type(marshalled_dict).__name__)
384 dict
385 >>> pprint_dict(marshalled_dict)
386@@ -958,16 +935,16 @@ underlying type are then followed. When marshalling dicts, the
387 list elements are name,value strings which are pulled apart and
388 used to populate the dict.
389
390- >>> pprint_list(list_marshaller.marshall_from_request([u'1', u'2']))
391+ >>> list_marshaller.marshall_from_request(['1', '2'])
392 ['1', '2']
393- >>> pprint_list(list_marshaller.marshall_from_request('["1", "2"]'))
394+ >>> list_marshaller.marshall_from_request('["1", "2"]')
395 ['1', '2']
396
397 >>> pprint_dict(
398 ... dict_marshaller.marshall_from_request('["foo,Vegetarian"]'))
399 {'foo': <Item Cuisine.VEGETARIAN, Vegetarian>}
400
401- >>> tuple_marshaller.marshall_from_request([u'1', u'2'])
402+ >>> tuple_marshaller.marshall_from_request(['1', '2'])
403 (1, 2)
404
405 Except that 'null' still returns None.
406@@ -985,7 +962,7 @@ single-element list.
407 >>> tuple_marshaller.marshall_from_request('1')
408 (1,)
409
410- >>> pprint_list(list_marshaller.marshall_from_request('test'))
411+ >>> list_marshaller.marshall_from_request('test')
412 ['test']
413
414 The unmarshall() method will return a list containing the unmarshalled

Subscribers

People subscribed via source and target branches