diff options
| author | Peyman Salehi <peymanslh@pm.me> | 2019-10-19 16:43:21 +0200 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2019-10-19 16:57:14 +0200 |
| commit | bdc84255a83c1d3f88202503d4a4ace8e0275a44 (patch) | |
| tree | 2410cbedd48797a82511004e9348b1278611e2cf | |
| parent | b3c7145c4079fe3d0c76c45421c02b96cc74b624 (diff) | |
| download | tablib-bdc84255a83c1d3f88202503d4a4ace8e0275a44.tar.gz | |
Fix some linting errors
| -rw-r--r-- | src/tablib/core.py | 17 | ||||
| -rw-r--r-- | src/tablib/formats/_dbf.py | 8 | ||||
| -rw-r--r-- | src/tablib/formats/_df.py | 4 | ||||
| -rw-r--r-- | src/tablib/formats/_html.py | 2 | ||||
| -rw-r--r-- | src/tablib/formats/_ods.py | 1 | ||||
| -rw-r--r-- | src/tablib/formats/_tsv.py | 1 | ||||
| -rw-r--r-- | src/tablib/formats/_xls.py | 2 | ||||
| -rw-r--r-- | src/tablib/formats/_xlsx.py | 2 | ||||
| -rw-r--r-- | src/tablib/packages/dbfpy/fields.py | 41 | ||||
| -rw-r--r-- | src/tablib/packages/dbfpy/header.py | 28 | ||||
| -rw-r--r-- | src/tablib/packages/dbfpy/record.py | 18 | ||||
| -rw-r--r-- | src/tablib/packages/dbfpy/utils.py | 1 | ||||
| -rwxr-xr-x | tests/test_tablib.py | 3 |
13 files changed, 66 insertions, 62 deletions
diff --git a/src/tablib/core.py b/src/tablib/core.py index 46e8444..23903eb 100644 --- a/src/tablib/core.py +++ b/src/tablib/core.py @@ -63,7 +63,8 @@ class Row: return slots def __setstate__(self, state): - for (k, v) in list(state.items()): setattr(self, k, v) + for (k, v) in list(state.items()): + setattr(self, k, v) def rpush(self, value): self.insert(0, value) @@ -93,7 +94,7 @@ class Row: def has_tag(self, tag): """Returns true if current row contains tag.""" - if tag == None: + if tag is None: return False elif isinstance(tag, str): return (tag in self.tags) @@ -172,7 +173,7 @@ class Dataset: def __getitem__(self, key): if isinstance(key, str): if key in self.headers: - pos = self.headers.index(key) # get 'key' index from each data + pos = self.headers.index(key) # get 'key' index from each data return [row[pos] for row in self._data] else: raise KeyError @@ -820,7 +821,7 @@ class Dataset: if isinstance(col, str): if col in self.headers: - col = self.headers.index(col) # get 'key' index from each data + col = self.headers.index(col) # get 'key' index from each data else: raise KeyError @@ -986,13 +987,13 @@ class Dataset: if cols is None: cols = list(self.headers) - #filter out impossible rows and columns + # filter out impossible rows and columns rows = [row for row in rows if row in range(self.height)] cols = [header for header in cols if header in self.headers] _dset = Dataset() - #filtering rows and columns + # filtering rows and columns _dset.headers = list(cols) _dset._data = [] @@ -1072,8 +1073,8 @@ class Databook: for dset in self._datasets: collector.append(dict_pack( - title = dset.title, - data = dset._package(ordered=ordered) + title=dset.title, + data=dset._package(ordered=ordered) )) return collector diff --git a/src/tablib/formats/_dbf.py b/src/tablib/formats/_dbf.py index fecb214..2a6df32 100644 --- a/src/tablib/formats/_dbf.py +++ b/src/tablib/formats/_dbf.py @@ -2,7 +2,6 @@ """ import io import os -import struct import tempfile from tablib.packages.dbfpy import dbf @@ -15,6 +14,7 @@ extensions = ('csv',) DEFAULT_ENCODING = 'utf-8' + def export_set(dataset): """Returns DBF representation of a Dataset""" new_dbf = dbfnew.dbf_new() @@ -45,6 +45,7 @@ def export_set(dataset): os.remove(temp_uri) return stream.getvalue() + def import_set(dset, in_stream, headers=True): """Returns a dataset from a DBF stream.""" @@ -55,13 +56,14 @@ def import_set(dset, in_stream, headers=True): row = [_dbf[record][f] for f in _dbf.fieldNames] dset.append(row) + def detect(stream): """Returns True if the given stream is valid DBF""" - #_dbf = dbf.Table(StringIO(stream)) + # _dbf = dbf.Table(StringIO(stream)) try: if type(stream) is not bytes: stream = bytes(stream, 'utf-8') - _dbf = dbf.Dbf(io.BytesIO(stream), readOnly=True) + dbf.Dbf(io.BytesIO(stream), readOnly=True) return True except Exception: return False diff --git a/src/tablib/formats/_df.py b/src/tablib/formats/_df.py index dd319a3..372bf7d 100644 --- a/src/tablib/formats/_df.py +++ b/src/tablib/formats/_df.py @@ -1,9 +1,6 @@ """ Tablib - DataFrame Support. """ -import sys -from io import BytesIO - try: from pandas import DataFrame except ImportError: @@ -15,6 +12,7 @@ import tablib title = 'df' extensions = ('df', ) + def detect(stream): """Returns True if given stream is a DataFrame.""" if DataFrame is None: diff --git a/src/tablib/formats/_html.py b/src/tablib/formats/_html.py index 952caf2..bb31128 100644 --- a/src/tablib/formats/_html.py +++ b/src/tablib/formats/_html.py @@ -2,11 +2,9 @@ """ import codecs -import sys from io import BytesIO from MarkupPy import markup -import tablib BOOK_ENDINGS = 'h3' diff --git a/src/tablib/formats/_ods.py b/src/tablib/formats/_ods.py index cd58cd1..43a2cfb 100644 --- a/src/tablib/formats/_ods.py +++ b/src/tablib/formats/_ods.py @@ -10,6 +10,7 @@ extensions = ('ods',) bold = style.Style(name="bold", family="paragraph") bold.addElement(style.TextProperties(fontweight="bold", fontweightasian="bold", fontweightcomplex="bold")) + def export_set(dataset): """Returns ODF representation of Dataset.""" diff --git a/src/tablib/formats/_tsv.py b/src/tablib/formats/_tsv.py index 8015bff..380b4e6 100644 --- a/src/tablib/formats/_tsv.py +++ b/src/tablib/formats/_tsv.py @@ -12,6 +12,7 @@ extensions = ('tsv',) DELIMITER = '\t' + def export_set(dataset): """Returns TSV representation of Dataset.""" return export_set_wrapper(dataset, delimiter=DELIMITER) diff --git a/src/tablib/formats/_xls.py b/src/tablib/formats/_xls.py index 71c8245..ca086dd 100644 --- a/src/tablib/formats/_xls.py +++ b/src/tablib/formats/_xls.py @@ -1,7 +1,6 @@ """ Tablib - XLS Support. """ -import sys from io import BytesIO import tablib @@ -80,6 +79,7 @@ def import_set(dset, in_stream, headers=True): else: dset.append(sheet.row_values(i)) + def import_book(dbook, in_stream, headers=True): """Returns databook from XLS stream.""" diff --git a/src/tablib/formats/_xlsx.py b/src/tablib/formats/_xlsx.py index 27757f4..0a947b8 100644 --- a/src/tablib/formats/_xlsx.py +++ b/src/tablib/formats/_xlsx.py @@ -1,7 +1,6 @@ """ Tablib - XLSX Support. """ -import sys from io import BytesIO import openpyxl @@ -27,6 +26,7 @@ def detect(stream): except Exception: return False + def export_set(dataset, freeze_panes=True): """Returns XLSX representation of Dataset.""" diff --git a/src/tablib/packages/dbfpy/fields.py b/src/tablib/packages/dbfpy/fields.py index bc39d57..c763e1e 100644 --- a/src/tablib/packages/dbfpy/fields.py +++ b/src/tablib/packages/dbfpy/fields.py @@ -28,7 +28,7 @@ TODO: __version__ = "$Revision: 1.14 $"[11:-2] __date__ = "$Date: 2009/05/26 05:16:51 $"[7:-2] -__all__ = ["lookupFor",] # field classes added at the end of the module +__all__ = ["lookupFor"] # field classes added at the end of the module import datetime import struct @@ -36,7 +36,8 @@ import sys from . import utils -## abstract definitions +# abstract definitions + class DbfFieldDef: """Abstract field definition. @@ -56,8 +57,7 @@ class DbfFieldDef: """ - __slots__ = ("name", "decimalCount", - "start", "end", "ignoreErrors") + __slots__ = ("name", "decimalCount", "start", "end", "ignoreErrors") # length of the field, None in case of variable-length field, # or a number if this field is a fixed-length field @@ -73,13 +73,12 @@ class DbfFieldDef: defaultValue = None def __init__(self, name, length=None, decimalCount=None, - start=None, stop=None, ignoreErrors=False, - ): + start=None, stop=None, ignoreErrors=False): """Initialize instance.""" assert self.typeCode is not None, "Type code must be overridden" assert self.defaultValue is not None, "Default value must be overridden" - ## fix arguments - if len(name) >10: + # fix arguments + if len(name) > 10: raise ValueError("Field name \"%s\" is too long" % name) name = str(name).upper() if self.__class__.length is None: @@ -87,13 +86,12 @@ class DbfFieldDef: raise ValueError("[%s] Length isn't specified" % name) length = int(length) if length <= 0: - raise ValueError("[%s] Length must be a positive integer" - % name) + raise ValueError("[%s] Length must be a positive integer" % name) else: length = self.length if decimalCount is None: decimalCount = 0 - ## set fields + # set fields self.name = name # FIXME: validate length according to the specification at # http://www.clicketyclick.dk/databases/xbase/format/data_types.html @@ -144,7 +142,7 @@ class DbfFieldDef: return ( _name + self.typeCode + - #data address + # data address chr(0) * 4 + chr(self.length) + chr(self.decimalCount) + @@ -171,7 +169,7 @@ class DbfFieldDef: """Return decoded field value from the record string.""" try: return self.decodeValue(self.rawFromRecord(record)) - except: + except Exception: if self.ignoreErrors: return utils.INVALID_VALUE else: @@ -194,7 +192,8 @@ class DbfFieldDef: """ raise NotImplementedError -## real classes +# real classes + class DbfCharacterFieldDef(DbfFieldDef): """Definition of the character field.""" @@ -257,11 +256,13 @@ class DbfNumericFieldDef(DbfFieldDef): % (self.name, _rv, self.length)) return _rv + class DbfFloatFieldDef(DbfNumericFieldDef): """Definition of the float field - same as numeric.""" typeCode = "F" + class DbfIntegerFieldDef(DbfFieldDef): """Definition of the integer field.""" @@ -277,6 +278,7 @@ class DbfIntegerFieldDef(DbfFieldDef): """Return string containing encoded ``value``.""" return struct.pack("<i", int(value)) + class DbfCurrencyFieldDef(DbfFieldDef): """Definition of the currency field.""" @@ -292,6 +294,7 @@ class DbfCurrencyFieldDef(DbfFieldDef): """Return string containing encoded ``value``.""" return struct.pack("<q", round(value * 10000)) + class DbfLogicalFieldDef(DbfFieldDef): """Definition of the logical field.""" @@ -338,7 +341,7 @@ class DbfMemoFieldDef(DbfFieldDef): def decodeValue(self, value): """Return int .dbt block number decoded from the string object.""" - #return int(value) + # return int(value) raise NotImplementedError def encodeValue(self, value): @@ -347,7 +350,7 @@ class DbfMemoFieldDef(DbfFieldDef): Note: this is an internal method. """ - #return str(value)[:self.length].ljust(self.length) + # return str(value)[:self.length].ljust(self.length) raise NotImplementedError @@ -423,6 +426,7 @@ class DbfDateTimeFieldDef(DbfFieldDef): _fieldsRegistry = {} + def registerField(fieldCls): """Register field definition class. @@ -454,11 +458,12 @@ def lookupFor(typeCode): # forget to look to the same comment in ``registerField`` return _fieldsRegistry[chr(typeCode)] -## register generic types +# register generic types + for (_name, _val) in list(globals().items()): if isinstance(_val, type) and issubclass(_val, DbfFieldDef) \ - and (_name != "DbfFieldDef"): + and (_name != "DbfFieldDef"): __all__.append(_name) registerField(_val) del _name, _val diff --git a/src/tablib/packages/dbfpy/header.py b/src/tablib/packages/dbfpy/header.py index cb0050a..3eff673 100644 --- a/src/tablib/packages/dbfpy/header.py +++ b/src/tablib/packages/dbfpy/header.py @@ -51,13 +51,12 @@ class DbfHeader: """ __slots__ = ("signature", "fields", "lastUpdate", "recordLength", - "recordCount", "headerLength", "changed", "_ignore_errors") + "recordCount", "headerLength", "changed", "_ignore_errors") - ## instance construction and initialization methods + # instance construction and initialization methods def __init__(self, fields=None, headerLength=0, recordLength=0, - recordCount=0, signature=0x03, lastUpdate=None, ignoreErrors=False, - ): + recordCount=0, signature=0x03, lastUpdate=None, ignoreErrors=False): """Initialize instance. Arguments: @@ -111,7 +110,7 @@ class DbfHeader: _data = bytes(first_32, sys.getfilesystemencoding()) _data = first_32 (_cnt, _hdrLen, _recLen) = struct.unpack("<I2H", _data[4:12]) - #reserved = _data[12:32] + # reserved = _data[12:32] _year = _data[1] if _year < 80: # dBase II started at 1980. It is quite unlikely @@ -119,10 +118,10 @@ class DbfHeader: _year += 2000 else: _year += 1900 - ## create header object + # create header object _obj = cls(None, _hdrLen, _recLen, _cnt, _data[0], - (_year, _data[2], _data[3])) - ## append field definitions + (_year, _data[2], _data[3])) + # append field definitions # position 0 is for the deletion flag _pos = 1 _data = stream.read(1) @@ -135,7 +134,7 @@ class DbfHeader: return _obj fromStream = classmethod(fromStream) - ## properties + # properties year = property(lambda self: self.lastUpdate.year) month = property(lambda self: self.lastUpdate.month) @@ -156,7 +155,7 @@ class DbfHeader: """) - ## object representation + # object representation def __repr__(self): _rv = """\ @@ -173,7 +172,7 @@ Version (signature): 0x%02x ) return _rv - ## internal methods + # internal methods def _addField(self, *defs): """Internal variant of the `addField` method. @@ -197,8 +196,7 @@ Version (signature): 0x%02x else: (_name, _type, _len, _dec) = (tuple(_def) + (None,) * 4)[:4] _cls = fields.lookupFor(_type) - _obj = _cls(_name, _len, _dec, - ignoreErrors=self._ignore_errors) + _obj = _cls(_name, _len, _dec, ignoreErrors=self._ignore_errors) _recordLength += _obj.length _defs.append(_obj) # and now extend field definitions and @@ -206,7 +204,7 @@ Version (signature): 0x%02x self.fields += _defs return _recordLength - ## interface methods + # interface methods def addField(self, *defs): """Add field definition to the header. @@ -251,7 +249,7 @@ Version (signature): 0x%02x self.recordCount, self.headerLength, self.recordLength) + (b'\x00' * 20) - #TODO: figure out if bytes(utf-8) is correct here. + # TODO: figure out if bytes(utf-8) is correct here. def setCurrentDate(self): """Update ``self.lastUpdate`` field with current date value.""" diff --git a/src/tablib/packages/dbfpy/record.py b/src/tablib/packages/dbfpy/record.py index 90dc468..db79e86 100644 --- a/src/tablib/packages/dbfpy/record.py +++ b/src/tablib/packages/dbfpy/record.py @@ -52,7 +52,7 @@ class DbfRecord: __slots__ = "dbf", "index", "deleted", "fieldData" - ## creation and initialization + # creation and initialization def __init__(self, dbf, index=None, deleted=False, data=None): """Instance initialization. @@ -82,7 +82,7 @@ class DbfRecord: # XXX: validate self.index before calculating position? position = property(lambda self: self.dbf.header.headerLength + \ - self.index * self.dbf.header.recordLength) + self.index * self.dbf.header.recordLength) def rawFromStream(cls, dbf, index): """Return raw record contents read from the stream. @@ -137,10 +137,10 @@ class DbfRecord: """ return cls(dbf, index, string[0]=="*", - [_fd.decodeFromRecord(string) for _fd in dbf.header.fields]) + [_fd.decodeFromRecord(string) for _fd in dbf.header.fields]) fromString = classmethod(fromString) - ## object representation + # object representation def __repr__(self): _template = "%%%ds: %%s (%%s)" % max([len(_fld) @@ -155,7 +155,7 @@ class DbfRecord: _rv.append(_template % (_fld, _val, type(_val))) return "\n".join(_rv) - ## protected methods + # protected methods def _write(self): """Write data to the dbf stream. @@ -171,7 +171,7 @@ class DbfRecord: self._validateIndex(False) self.dbf.stream.seek(self.position) self.dbf.stream.write(bytes(self.toString(), - sys.getfilesystemencoding())) + sys.getfilesystemencoding())) # FIXME: may be move this write somewhere else? # why we should check this condition for each record? if self.index == len(self.dbf): @@ -179,7 +179,7 @@ class DbfRecord: # we should write SUB (ASCII 26) self.dbf.stream.write(b"\x1A") - ## utility methods + # utility methods def _validateIndex(self, allowUndefined=True, checkRange=False): """Valid ``self.index`` value. @@ -195,9 +195,9 @@ class DbfRecord: raise ValueError("Index can't be negative (%s)" % self.index) elif checkRange and self.index <= self.dbf.header.recordCount: raise ValueError("There are only %d records in the DBF" % - self.dbf.header.recordCount) + self.dbf.header.recordCount) - ## interface methods + # interface methods def store(self): """Store current record in the DBF. diff --git a/src/tablib/packages/dbfpy/utils.py b/src/tablib/packages/dbfpy/utils.py index ee4396e..b3388a7 100644 --- a/src/tablib/packages/dbfpy/utils.py +++ b/src/tablib/packages/dbfpy/utils.py @@ -164,6 +164,7 @@ class _InvalidValue: def __repr__(self): return "<INVALID>" + # invalid value is a constant singleton INVALID_VALUE = _InvalidValue() diff --git a/tests/test_tablib.py b/tests/test_tablib.py index 943e1e7..04cba69 100755 --- a/tests/test_tablib.py +++ b/tests/test_tablib.py @@ -4,7 +4,6 @@ import datetime import doctest import json -import sys import unittest from uuid import uuid4 @@ -533,7 +532,7 @@ class HTMLTests(BaseTestCase): html.table.close() html = str(html) - headers = ['foo', None, 'bar']; + headers = ['foo', None, 'bar'] d = tablib.Dataset(['foo', None, 'bar'], headers=headers) self.assertEqual(html, d.html) |
