From 32257f96ff95f4b61270317654ee58a62ac6a127 Mon Sep 17 00:00:00 2001 From: Paul Morelle Date: Mon, 18 Jan 2016 23:12:19 +0100 Subject: [PATCH] Remove specific params from ds.freeze() signature Add documentation to retrieve them more easily. References pudo/dataset#150 --- dataset/freeze/app.py | 42 ++++++++++++++++++++++++++++------ dataset/freeze/format/fjson.py | 11 +++++---- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/dataset/freeze/app.py b/dataset/freeze/app.py index 01efc28..7f32a26 100644 --- a/dataset/freeze/app.py +++ b/dataset/freeze/app.py @@ -25,8 +25,7 @@ def create_parser(): def freeze(result, format='csv', filename='freeze.csv', fileobj=None, - prefix='.', meta={}, indent=2, mode='list', wrap=True, - callback=None, **kw): + prefix='.', mode='list', **kw): """ Perform a data export of a given result set. This is a very flexible exporter, allowing for various output formats, metadata @@ -80,18 +79,47 @@ def freeze(result, format='csv', filename='freeze.csv', fileobj=None, Tabson is a smart combination of the space-efficiency of the CSV and the parsability and structure of JSON. + You can pass additional named parameters specific to the used format. + + As an example, you can freeze to minified JSON with the following: + + dataset.freeze(res, format='json', indent=4, wrap=False, + filename='output.json') + + *json* and *tabson* + *callback*: + if provided, generate a JSONP string using the given callback + function, i.e. something like `callback && callback({...})` + + *indent*: + if *indent* is a non-negative integer (it is ``2`` by default + when you call `dataset.freeze`, and ``None`` via the + ``datafreeze`` command), then JSON array elements and object + members will be pretty-printed with that indent level. + An indent level of 0 will only insert newlines. + ``None`` is the most compact representation. + + *meta*: + if *meta* is not ``None`` (default: ``{}``), it will be included + in the JSON output (for *json*, only if *wrap* is ``True``). + + *wrap* (only for *json*): + if *wrap* is ``True`` (default), the JSON output is an object + of the form ``{"count": 2, "results": [...]}``. + if ``meta`` is not ``None``, a third property ``meta`` is added + to the wrapping object, with this value. """ kw.update({ 'format': format, 'filename': filename, 'fileobj': fileobj, 'prefix': prefix, - 'meta': meta, - 'indent': indent, - 'callback': callback, - 'mode': mode, - 'wrap': wrap + 'mode': mode }) + + # Special cases when freezing comes from dataset.freeze + if format in ['json', 'tabson'] and 'indent' not in kw: kw['indent'] = 2 + records = result.all() if isinstance(result, Table) else result return freeze_export(Export({}, kw), result=records) diff --git a/dataset/freeze/format/fjson.py b/dataset/freeze/format/fjson.py index 814d30a..dd75068 100644 --- a/dataset/freeze/format/fjson.py +++ b/dataset/freeze/format/fjson.py @@ -25,7 +25,7 @@ class JSONSerializer(Serializer): def wrap(self, result): if self.mode == 'item': result = result[0] - if self.export.get_bool('wrap'): + if self.export.get_bool('wrap', True): result = { 'count': len(result), 'results': result @@ -50,10 +50,11 @@ class JSONSerializer(Serializer): data = json.dumps(result, cls=JSONEncoder, indent=self.export.get_int('indent')) - if self.export.get('callback'): - data = "%s && %s(%s);" % (self.export.get('callback'), - self.export.get('callback'), - data) + + callback = self.export.get('callback') + if callback: + data = "%s && %s(%s);" % (callback, callback, data) + fh.write(data) if self.fileobj is None: fh.close()