This commit is contained in:
Jing Ling
2020-08-29 17:14:07 +08:00
parent 93991fb66c
commit f4ee39b7c2
4 changed files with 29 additions and 22 deletions
+4 -4
View File
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
import os import os
from sys import stdout from sys import stdout
from collections import OrderedDict from collections import OrderedDict
@@ -121,7 +119,8 @@ class RecordCollection(object):
yield self[i] yield self[i]
else: else:
# Throws StopIteration when done. # Throws StopIteration when done.
# Prevent StopIteration bubbling from generator, following https://www.python.org/dev/peps/pep-0479/ # Prevent StopIteration bubbling from generator,
# following https://www.python.org/dev/peps/pep-0479/
try: try:
yield next(self) yield next(self)
except StopIteration: except StopIteration:
@@ -234,7 +233,8 @@ class RecordCollection(object):
try: try:
self[1] self[1]
except IndexError: except IndexError:
return self.first(default=default, as_dict=as_dict, as_ordereddict=as_ordereddict) return self.first(default=default, as_dict=as_dict,
as_ordereddict=as_ordereddict)
else: else:
raise ValueError('RecordCollection contained more than one row. ' raise ValueError('RecordCollection contained more than one row. '
'Expects only one row when using ' 'Expects only one row when using '
+12 -9
View File
@@ -1,7 +1,7 @@
""" Tablib - JSON Support
"""
import decimal import decimal
import json import json
import csv
from io import StringIO
from uuid import UUID from uuid import UUID
from . import tablib from . import tablib
@@ -16,7 +16,12 @@ def serialize_objects_handler(obj):
return obj return obj
class JSONFormat: """
Tablib - JSON Support
"""
class JSONFormat(object):
title = 'json' title = 'json'
extensions = ('json',) extensions = ('json',)
@@ -58,14 +63,11 @@ class JSONFormat:
return False return False
""" Tablib - *SV Support. """ Tablib - CSV Support.
""" """
import csv
from io import StringIO
class CSVFormat(object):
class CSVFormat:
title = 'csv' title = 'csv'
extensions = ('csv',) extensions = ('csv',)
@@ -114,7 +116,8 @@ class CSVFormat:
def detect(cls, stream, delimiter=None): def detect(cls, stream, delimiter=None):
"""Returns True if given stream is valid CSV.""" """Returns True if given stream is valid CSV."""
try: try:
csv.Sniffer().sniff(stream.read(1024), delimiters=delimiter or cls.DEFAULT_DELIMITER) csv.Sniffer().sniff(stream.read(1024),
delimiters=delimiter or cls.DEFAULT_DELIMITER)
return True return True
except Exception: except Exception:
return False return False
+1 -1
View File
@@ -4,7 +4,7 @@ from collections import OrderedDict
from .format import JSONFormat, CSVFormat from .format import JSONFormat, CSVFormat
class Registry: class Registry(object):
_formats = OrderedDict() _formats = OrderedDict()
def register(self, key, format_or_path): def register(self, key, format_or_path):
+12 -8
View File
@@ -2,6 +2,7 @@ from collections import OrderedDict
from io import BytesIO, StringIO from io import BytesIO, StringIO
from .registry import registry from .registry import registry
def normalize_input(stream): def normalize_input(stream):
""" """
Accept either a str/bytes stream or a file-like object and always return a Accept either a str/bytes stream or a file-like object and always return a
@@ -13,6 +14,7 @@ def normalize_input(stream):
return BytesIO(stream) return BytesIO(stream)
return stream return stream
def import_set(stream, format=None, **kwargs): def import_set(stream, format=None, **kwargs):
"""Return dataset of given stream (file-like object, string, or bytestring).""" """Return dataset of given stream (file-like object, string, or bytestring)."""
@@ -288,7 +290,8 @@ class Dataset:
if self.headers: if self.headers:
if dicts: if dicts:
data = [dict_pack(list(zip(self.headers, data_row))) for data_row in _data] data = [dict_pack(list(zip(self.headers, data_row)))
for data_row in _data]
else: else:
data = [list(self.headers)] + list(_data) data = [list(self.headers)] + list(_data)
else: else:
@@ -319,8 +322,8 @@ class Dataset:
def _get_dict(self): def _get_dict(self):
"""A native Python representation of the :class:`Dataset` object. If headers have """A native Python representation of the :class:`Dataset` object. If headers have
been set, a list of Python dictionaries will be returned. If no headers have been set, been set, a list of Python dictionaries will be returned. If no headers have been
a list of tuples (rows) will be returned instead. set, a list of tuples (rows) will be returned instead.
A dataset object can also be imported by setting the `Dataset.dict` attribute: :: A dataset object can also be imported by setting the `Dataset.dict` attribute: ::
@@ -464,7 +467,7 @@ class Dataset:
def add_formatter(self, col, handler): def add_formatter(self, col, handler):
"""Adds a formatter to the :class:`Dataset`. """Adds a formatter to the :class:`Dataset`.
.. versionadded:: 0.9.5 .. version added:: 0.9.5
:param col: column to. Accepts index int or header str. :param col: column to. Accepts index int or header str.
:param handler: reference to callback function to execute against :param handler: reference to callback function to execute against
@@ -488,7 +491,8 @@ class Dataset:
"""Removes all duplicate rows from the :class:`Dataset` object """Removes all duplicate rows from the :class:`Dataset` object
while maintaining the original order.""" while maintaining the original order."""
seen = set() seen = set()
self._data[:] = [row for row in self._data if not (tuple(row) in seen or seen.add(tuple(row)))] self._data[:] = [row for row in self._data if
not (tuple(row) in seen or seen.add(tuple(row)))]
def wipe(self): def wipe(self):
"""Removes all content and headers from the :class:`Dataset` object.""" """Removes all content and headers from the :class:`Dataset` object."""
@@ -519,12 +523,12 @@ registry.register_builtins()
class InvalidDimensions(Exception): class InvalidDimensions(Exception):
"Invalid size" """Invalid size"""
class InvalidDatasetIndex(Exception): class InvalidDatasetIndex(Exception):
"Outside of Dataset size" """Outside of Dataset size"""
class UnsupportedFormat(NotImplementedError): class UnsupportedFormat(NotImplementedError):
"Format is not supported" """Format is not supported"""