Skip to content

Commit

Permalink
Several small improvements and bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mherrmann committed Oct 20, 2015
1 parent 7fc5df0 commit 47ec1b6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
20 changes: 16 additions & 4 deletions pypxlib/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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):
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 47ec1b6

Please sign in to comment.