From 47ec1b64ea91e4f0d0cdfad86701c955f5d95159 Mon Sep 17 00:00:00 2001 From: Michael Herrmann Date: Tue, 20 Oct 2015 16:37:39 +0200 Subject: [PATCH] Several small improvements and bug fixes. --- pypxlib/__init__.py | 20 ++++++++++++++++---- setup.py | 5 +++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/pypxlib/__init__.py b/pypxlib/__init__.py index f1dc258..a0a23e7 100644 --- a/pypxlib/__init__.py +++ b/pypxlib/__init__.py @@ -1,10 +1,7 @@ from collections import OrderedDict -from ctypes import c_int, c_long, byref -from datetime import date from pypxlib.pxlib_ctypes import * import atexit -import sys PX_boot() atexit.register(PX_shutdown) @@ -23,6 +20,8 @@ def next(self): self.i += 1 return result raise StopIteration() + # Python 3: + __next__ = next def __init__(self, file_path, encoding='cp850'): self.file_path = file_path @@ -47,6 +46,15 @@ def _field_indices(self): self._field_indices_cached[field_name] = i return self._field_indices_cached def __getitem__(self, rownum): + if not isinstance(rownum, int): + raise TypeError( + 'Table indices must be integers, not %s.' % type(rownum) + ) + _len = len(self) + if rownum < 0: + rownum %= _len + elif rownum > _len: + raise IndexError('table index out of range.') pxvals = PX_retrieve_record(self.pxdoc, rownum) if not pxvals: raise PXError( @@ -64,6 +72,10 @@ def close(self): PX_close(self.pxdoc) PX_delete(self.pxdoc) self._field_indices_cached = None + def __repr__(self): + return '%s(%r)' % (self.__class__.__name__, self.file_path) + def __str__(self): + return repr(self) class Row(object): def __init__(self, pxvals, field_indices, encoding): @@ -74,7 +86,7 @@ def __getattr__(self, item): try: return self[item] except KeyError: - return AttributeError(item) + raise AttributeError(item) def __getitem__(self, item): pxval_index = self.field_indices[item] return self._parse_pxval(self.pxvals[pxval_index].contents) diff --git a/setup.py b/setup.py index da6bed3..33b67df 100644 --- a/setup.py +++ b/setup.py @@ -13,10 +13,11 @@ setup( name='pypxlib', - version='1.2', + version='1.3', description=description, - long_description=description + '\n\nHome page: https://github.com/mherrmann/pypxlib', + long_description= + description + '\n\nHome page: https://github.com/mherrmann/pypxlib', url='https://github.com/mherrmann/pypxlib', author='Michael Herrmann',