n6sdk.pyramid_commons.renderers¶
Note
To learn how to implement your own renderer, please analyze the source code of the classes defined in this module.
Note
To learn how to register your own renderer (to make it usable with in
your n6sdk-based application), please consult the
n6sdk.pyramid_commons.register_stream_renderer()
documentation.
-
class
n6sdk.pyramid_commons.renderers.
BaseStreamRenderer
(data_generator, request)[source]¶ Bases:
object
The base class for renderers.
-
content_type
= None¶
-
-
class
n6sdk.pyramid_commons.renderers.
StreamRenderer_sjson
(data_generator, request)[source]¶ Bases:
n6sdk.pyramid_commons.renderers.BaseStreamRenderer
The class of the renderer registered as the
json
one.-
content_type
= 'text/plain'¶
-
-
class
n6sdk.pyramid_commons.renderers.
StreamRenderer_json
(data_generator, request)[source]¶ Bases:
n6sdk.pyramid_commons.renderers.BaseStreamRenderer
The class of the renderer registered as the
sjson
one.-
content_type
= 'application/json'¶
-
-
n6sdk.pyramid_commons.renderers.
dict_with_nulls_removed
(d, _container_with_nulls_removed=<function _container_with_nulls_removed>, _isinstance=<built-in function isinstance>, _jsonable_container=(<type 'dict'>, <type 'list'>, <type 'tuple'>), _dict_items=<method 'iteritems' of 'dict' objects>)[source]¶ Get a copy of the given dictionary with empty-or-
None
items removed recursively.(A helper function used by the
StreamRenderer_json
andStreamRenderer_sjson
renderers.)Note
Values equal to 0 (including
False
) are not removed. Other false values – such as empty sequences (including strings) orNone
– are removed.>>> d = { ... 'a': 'A', 'b': '', 'c': [], 'd': (), 'e': {}, 'f': [''], 'g': ['x'], ... 'h': { ... 'a': 'A', 'b': '', 'c': [], 'd': (), 'e': {}, 'f': [''], 'g': ['x'], ... }, ... 'i': ['A', '', 0, [], (), {}, [None], [0.0], ['x']], ... 'j': ['', [{}], ([{}]), {'x': ()}, ['']], ... 'k': [None], ... 'l': {'x': None}, ... 'm': None, ... 'x': [0], ... 'y': {'x': False}, ... 'z': 0, ... } >>> d2 = dict_with_nulls_removed(d) >>> d2 == { ... 'a': 'A', 'g': ['x'], ... 'h': {'a': 'A', 'g': ['x']}, ... 'i': ['A', 0, [0.0], ['x']], ... 'x': [0], ... 'y': {'x': False}, ... 'z': 0, ... } True
>>> dict_with_nulls_removed({}) {}
-
n6sdk.pyramid_commons.renderers.
data_dict_to_json
(data, **kwargs)[source]¶ Serialize the given data dictionary to JSON (using any additional keyword arguments as argument for func:`json.dumps), applying
dict_with_nulls_removed()
and converting containeddatetime.datetime
instances (if any) to strings. Onlydatetime.datetime
instances that are “naive”, i.e. not aware of timezone, can be used (effects of using timezone-aware ones are undefined).>>> import copy, datetime, json >>> d = { ... 'a': 'A', 'b': '', 'c': [], 'd': (), 'e': {}, 'f': [''], 'g': ['x'], ... 'h': { ... 'a': 'A', 'b': '', 'c': [], 'd': (), 'e': {}, 'f': [''], 'g': ['x'], ... }, ... 'i': ['A', '', 0, [], (), {}, [None], [0.0], ['x']], ... 'j': ['', [{}], ([{}]), {'x': ()}, ['']], ... 'k': [None], ... 'l': {'x': None}, ... 'm': None, ... 'x': [0], ... 'y': {'x': False}, ... 'z': 0, ... 'dt': datetime.datetime(2015, 6, 19, 10, 22, 42, 123), ... 'dt_seq': [ ... datetime.datetime(2015, 6, 19, 10, 22, 42), ... [], ... [datetime.datetime(2015, 6, 19, 10, 22, 42, 987654)], ... ], ... } >>> dcopy = copy.deepcopy(d) >>> json1 = data_dict_to_json(d) >>> json2 = data_dict_to_json(d, indent=4) >>> '\n' not in json1 True >>> '\n' in json2 True >>> len(json2) > len(json1) True >>> json.loads(json1) == json.loads(json2) == { ... 'a': 'A', 'g': ['x'], ... 'h': {'a': 'A', 'g': ['x']}, ... 'i': ['A', 0, [0.0], ['x']], ... 'x': [0], ... 'y': {'x': False}, ... 'z': 0, ... 'dt': '2015-06-19T10:22:42.000123Z', ... 'dt_seq': [ ... '2015-06-19T10:22:42Z', ... ['2015-06-19T10:22:42.987654Z'], ... ], ... } True >>> dcopy == d # the given dictionary has not been modified True