From 2b5818598a87875aa77712bb8e733eea9d4e1f93 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 12 May 2011 16:44:39 -0400 Subject: [PATCH 1/2] compat module --- tablib/__init__.py | 2 +- tablib/compat.py | 23 +++++++++++++++++++---- tablib/core.py | 8 +++----- tablib/formats/_xls.py | 10 +--------- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/tablib/__init__.py b/tablib/__init__.py index 24d5fdb..c7ae7c0 100644 --- a/tablib/__init__.py +++ b/tablib/__init__.py @@ -1,7 +1,7 @@ """ Tablib. """ -from tablib.compat import ( +from tablib.core import ( Databook, Dataset, detect, import_set, InvalidDatasetType, InvalidDimensions, UnsupportedFormat ) diff --git a/tablib/compat.py b/tablib/compat.py index 85ae9b4..09a56b6 100644 --- a/tablib/compat.py +++ b/tablib/compat.py @@ -10,9 +10,24 @@ Tablib compatiblity module. import sys +is_py3 = (sys.version_info[0] > 2) + + + +try: + from collections import OrderedDict +except ImportError: + from tablib.packages.ordereddict import OrderedDict + + +if is_py3: + from io import BytesIO + import tablib.packages.xlwt3 as xlwt + from tablib.packages import markup3 as markup + +else: + from cStringIO import StringIO as BytesIO + import tablib.packages.xlwt as xlwt + from tablib.packages import markup -from tablib.core import ( - Databook, Dataset, detect, import_set, - InvalidDatasetType, InvalidDimensions, UnsupportedFormat -) diff --git a/tablib/core.py b/tablib/core.py index 896dfcc..55d057b 100644 --- a/tablib/core.py +++ b/tablib/core.py @@ -15,10 +15,8 @@ from operator import itemgetter from tablib import formats import collections -try: - from collections import OrderedDict -except ImportError: - from tablib.packages.ordereddict import OrderedDict + +from tablib.compat import OrderedDict __title__ = 'tablib' @@ -795,7 +793,7 @@ def import_set(stream): format.import_set(data, stream) return data - except AttributeError as e: + except AttributeError: return None diff --git a/tablib/formats/_xls.py b/tablib/formats/_xls.py index d820250..48dcc0b 100644 --- a/tablib/formats/_xls.py +++ b/tablib/formats/_xls.py @@ -5,15 +5,7 @@ import sys - -if sys.version_info[0] > 2: - from io import BytesIO - import tablib.packages.xlwt3 as xlwt - -else: - from cStringIO import StringIO as BytesIO - import tablib.packages.xlwt as xlwt - +from tablib.compat import BytesIO, xlwt title = 'xls' From 11bca4f7a28bffb90c67a89ae6069995ad0ee180 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 12 May 2011 17:20:22 -0400 Subject: [PATCH 2/2] callable check fix --- tablib/core.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tablib/core.py b/tablib/core.py index 55d057b..4cef83c 100644 --- a/tablib/core.py +++ b/tablib/core.py @@ -13,7 +13,6 @@ from copy import copy from operator import itemgetter from tablib import formats -import collections from tablib.compat import OrderedDict @@ -276,7 +275,8 @@ class Dataset(object): else: header = [] - if len(col) == 1 and isinstance(col[0], collections.Callable): + if len(col) == 1 and hasattr(col[0], '__call__'): + col = list(map(col[0], self._data)) col = tuple(header + col) @@ -547,7 +547,7 @@ class Dataset(object): col = list(col) # Callable Columns... - if len(col) == 1 and isinstance(col[0], collections.Callable): + if len(col) == 1 and hasattr(col[0], '__call__'): col = list(map(col[0], self._data)) col = self._clean_col(col)