diff --git a/excel_handler/__init__.py b/excel_handler/__init__.py index 85efa8c..f6d71ed 100644 --- a/excel_handler/__init__.py +++ b/excel_handler/__init__.py @@ -1,4 +1,5 @@ -from handler import ExcelHandler +from __future__ import absolute_import +from .handler import ExcelHandler assert ExcelHandler version = "0.2.3" diff --git a/excel_handler/fields.py b/excel_handler/fields.py index bf9d4f8..0d7776d 100644 --- a/excel_handler/fields.py +++ b/excel_handler/fields.py @@ -1,3 +1,6 @@ +from builtins import str +from past.builtins import basestring +from builtins import object import xlrd import datetime @@ -46,16 +49,16 @@ def cast(self, value, book, row_data): except: try: return self.choices_inv[value] - except ValueError, error: + except ValueError as error: error.args += (self.name,) raise ValueError(error) - except KeyError, error: + except KeyError as error: error.args += (self.name,) raise KeyError(error) try: return self.cast_method(value) - except ValueError, error: + except ValueError as error: error.args += (self.name, value) raise ValueError(error) @@ -69,12 +72,12 @@ def write(self, workbook, sheet, row, value): if self.choices: try: value = self.choices[value] - except KeyError, error: + except KeyError as error: if value is not None: raise KeyError(error) if hasattr(value, 'translate'): - value = unicode(value) + value = str(value) sheet.write(row, self.col, value) @@ -104,7 +107,7 @@ def __init__(self, col, *args, **kwargs): class CharField(Field): def __init__(self, col, *args, **kwargs): super(CharField, self).__init__(col, *args, **kwargs) - self.cast_method = unicode + self.cast_method = str class DateTimeField(Field): diff --git a/excel_handler/handler.py b/excel_handler/handler.py index c2d513f..87cc077 100644 --- a/excel_handler/handler.py +++ b/excel_handler/handler.py @@ -1,10 +1,15 @@ """ This document defines the excel_handler module """ +from __future__ import print_function +from __future__ import absolute_import +from builtins import str +from builtins import object import xlrd import xlsxwriter import datetime -from fields import Field +from .fields import Field from collections import namedtuple +from future.utils import with_metaclass class FieldNotFound(Exception): @@ -27,7 +32,7 @@ def __new__(cls, name, bases, attrs): cols = {} - for k, v in attrs.items(): + for k, v in list(attrs.items()): if isinstance(v, Field): field = attrs.pop(k) field.name = k @@ -47,7 +52,7 @@ def __new__(cls, name, bases, attrs): attrs['fieldname_to_field'] = fieldname_to_field attrs['fields'] = sorted( - fieldname_to_field.values(), + list(fieldname_to_field.values()), key=lambda field: field.col ) @@ -56,7 +61,7 @@ def __new__(cls, name, bases, attrs): this = sup.__new__(cls, name, bases, attrs) field_count = len(fieldname_to_field) - for field_name, field in fieldname_to_field.items(): + for field_name, field in list(fieldname_to_field.items()): try: if field._distance_from_last < 0: field.col = field_count + field._distance_from_last @@ -66,12 +71,10 @@ def __new__(cls, name, bases, attrs): return this -class ExcelHandler(): +class ExcelHandler(with_metaclass(ExcelHandlerMetaClass, object)): """ ExcelHandler is a class that is used to wrap common operations in excel files """ - __metaclass__ = ExcelHandlerMetaClass - def __init__(self, path=None, excel_file=None, mode='r', on_demand=False): if path is None and excel_file is None: raise Exception("path or excel_file requried") @@ -224,7 +227,7 @@ def read(self, skip_titles=False, failfast=False, ignore_blank_rows=True, if not err.args: err.args = ('', ) msg = u'Cannot read row "{}" : Column {}, {}'.format( - row + 1, unicode(field.verbose_name), err.args[0]) + row + 1, str(field.verbose_name), err.args[0]) err.args = (msg,) + err.args[1:] if failfast: raise @@ -240,7 +243,7 @@ def read(self, skip_titles=False, failfast=False, ignore_blank_rows=True, ) ) else: - print msg + print(msg) continue_while = True break @@ -333,22 +336,22 @@ def write(self, data, set_titles=False): formt = self.workbook.add_format() self.set_title_format(formt) - for field_name, field in self.fieldname_to_field.items(): + for field_name, field in list(self.fieldname_to_field.items()): self.sheet.write( 0, field.col, - unicode(field.verbose_name), + str(field.verbose_name), formt ) row = 1 # set format and prepare the write for each field - for field_name, field in self.fieldname_to_field.items(): + for field_name, field in list(self.fieldname_to_field.items()): field.set_column_format(self) field.prepare_write() for row_data in data: - for field_name, value in row_data.items(): + for field_name, value in list(row_data.items()): try: field = self.fieldname_to_field[field_name] except KeyError: diff --git a/setup.py b/setup.py index 87a0e36..ef268d7 100644 --- a/setup.py +++ b/setup.py @@ -22,10 +22,12 @@ # 'mimeparse', 'xlutils(>=1.6.0)', 'XlsxWriter(>=0.5.7)', + 'future(>=0.18.2)', ], install_requires=[ 'xlutils >= 1.6.0', 'XlsxWriter >= 0.5.7', + 'future >=0.18.2', ], package_data={ }, diff --git a/test/test.py b/test/test.py index 373c022..14d4291 100644 --- a/test/test.py +++ b/test/test.py @@ -1,3 +1,4 @@ +from builtins import object from excel_handler import ExcelHandler from excel_handler import fields @@ -5,7 +6,7 @@ import datetime -class Query: +class Query(object): def values_list(*args, **kwargs): return [( 'one', 1 @@ -16,13 +17,13 @@ def values_list(*args, **kwargs): )] -class Meta: +class Meta(object): object_name = 'model' -class Model(): +class Model(object): - class Objects: + class Objects(object): def all(self): return Query() @@ -128,10 +129,10 @@ def test_read_rows(self): self.assertEqual(len(second_row), 2) self.assertEqual(len(second_row[0]), 4) - for key, value in data[0].items(): + for key, value in list(data[0].items()): self.assertEqual(first_row[0][key], value) - for key, value in data[1].items(): + for key, value in list(data[1].items()): self.assertEqual(second_row[0][key], value) def test_write_rows(self): @@ -160,7 +161,7 @@ def test_read(self): self.assertEqual(len(expected_data), len(data)) for i, obj in enumerate(expected_data): - for k, expected_value in obj.items(): + for k, expected_value in list(obj.items()): read_value = data[i][k] self.assertEqual(read_value, expected_value) @@ -185,7 +186,7 @@ def test_read(self): self.assertEqual(len(expected_data), len(data)) for i, obj in enumerate(expected_data): - for k, expected_value in obj.items(): + for k, expected_value in list(obj.items()): read_value = data[i][k] self.assertEqual(read_value, expected_value) @@ -207,7 +208,7 @@ def test_read_empty(self): self.assertEqual(len(expected_data), len(data)) for i, obj in enumerate(expected_data): - for k, expected_value in obj.items(): + for k, expected_value in list(obj.items()): read_value = data[i][k] self.assertEqual(read_value, expected_value) @@ -252,7 +253,7 @@ def test_read(self): self.assertEqual(len(expected_data), len(data)) for i, obj in enumerate(expected_data): - for k, expected_value in obj.items(): + for k, expected_value in list(obj.items()): read_value = data[i][k] self.assertEqual(read_value, expected_value) @@ -290,7 +291,7 @@ def test_write(self): self.assertEqual(len(in_data), 3) for i, obj in enumerate(data): - for k, v in obj.items(): + for k, v in list(obj.items()): self.assertEqual(in_data[i][k], v) # testing the choices valus @@ -339,7 +340,7 @@ def test_read(self): self.assertEqual(len(expected_data), len(data)) for i, obj in enumerate(expected_data): - for k, expected_value in obj.items(): + for k, expected_value in list(obj.items()): read_value = data[i][k] self.assertEqual(read_value, expected_value)