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
from sys import stdout
from collections import OrderedDict
@@ -121,7 +119,8 @@ class RecordCollection(object):
yield self[i]
else:
# 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:
yield next(self)
except StopIteration:
@@ -234,7 +233,8 @@ class RecordCollection(object):
try:
self[1]
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:
raise ValueError('RecordCollection contained more than one row. '
'Expects only one row when using '
+12 -9
View File
@@ -1,7 +1,7 @@
""" Tablib - JSON Support
"""
import decimal
import json
import csv
from io import StringIO
from uuid import UUID
from . import tablib
@@ -16,7 +16,12 @@ def serialize_objects_handler(obj):
return obj
class JSONFormat:
"""
Tablib - JSON Support
"""
class JSONFormat(object):
title = 'json'
extensions = ('json',)
@@ -58,14 +63,11 @@ class JSONFormat:
return False
""" Tablib - *SV Support.
""" Tablib - CSV Support.
"""
import csv
from io import StringIO
class CSVFormat:
class CSVFormat(object):
title = 'csv'
extensions = ('csv',)
@@ -114,7 +116,8 @@ class CSVFormat:
def detect(cls, stream, delimiter=None):
"""Returns True if given stream is valid CSV."""
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
except Exception:
return False
+1 -1
View File
@@ -4,7 +4,7 @@ from collections import OrderedDict
from .format import JSONFormat, CSVFormat
class Registry:
class Registry(object):
_formats = OrderedDict()
def register(self, key, format_or_path):
+11 -7
View File
@@ -2,6 +2,7 @@ from collections import OrderedDict
from io import BytesIO, StringIO
from .registry import registry
def normalize_input(stream):
"""
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 stream
def import_set(stream, format=None, **kwargs):
"""Return dataset of given stream (file-like object, string, or bytestring)."""
@@ -288,7 +290,8 @@ class Dataset:
if self.headers:
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:
data = [list(self.headers)] + list(_data)
else:
@@ -319,8 +322,8 @@ class Dataset:
def _get_dict(self):
"""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,
a list of tuples (rows) will be returned instead.
been set, a list of Python dictionaries will be returned. If no headers have been
set, a list of tuples (rows) will be returned instead.
A dataset object can also be imported by setting the `Dataset.dict` attribute: ::
@@ -488,7 +491,8 @@ class Dataset:
"""Removes all duplicate rows from the :class:`Dataset` object
while maintaining the original order."""
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):
"""Removes all content and headers from the :class:`Dataset` object."""
@@ -519,12 +523,12 @@ registry.register_builtins()
class InvalidDimensions(Exception):
"Invalid size"
"""Invalid size"""
class InvalidDatasetIndex(Exception):
"Outside of Dataset size"
"""Outside of Dataset size"""
class UnsupportedFormat(NotImplementedError):
"Format is not supported"
"""Format is not supported"""