Skip to content

Commit

Permalink
Add support for deleting table rows.
Browse files Browse the repository at this point in the history
  • Loading branch information
mherrmann committed Nov 3, 2015
1 parent e4622e1 commit 710e6c4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
12 changes: 9 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,17 @@ given by the table's ``.fields`` property:
OrderedDict([('field1', <pypxlib.LongField object at 0x1072c6710>),
('field2', <pypxlib.AlphaField object at 0x10731ffd0>)]
>>> table.insert((50, 'Some text'))
97
>>> table[97]
2
>>> table[2]
Row(field1=50, field2='Some text')
Don't forget to close the table when you are done!
Deleting a row can be done via the ``del`` keyword:
.. code:: python
>>> del table[2]
Finally, don't forget to close the table when you are done!
.. code:: python
Expand Down
16 changes: 12 additions & 4 deletions pypxlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ def fields(self):
Field.from_type(ord(field.px_ftype), i, self.encoding)
return self._fields_cached
def __getitem__(self, rownum):
if not isinstance(rownum, int):
raise TypeError(
'Table indices must be integers, not %s.' % type(rownum)
)
self._check_rownum(rownum)
_len = len(self)
if rownum < 0:
rownum %= _len
Expand All @@ -60,6 +57,17 @@ def __getitem__(self, rownum):
(rownum, self.file_path)
)
return Row(self, rownum, pxvals)
def _check_rownum(self, rownum):
if not isinstance(rownum, int):
raise TypeError(
'Table indices must be integers, not %s.' % type(rownum)
)
def __delitem__(self, rownum):
self._check_rownum(rownum)
if PX_delete_record(self.pxdoc, rownum) == -1:
raise PXError(
'Could not delete row %d of file %s.' % (rownum, self.file_path)
)
def __len__(self):
return self.pxdoc.contents.px_head.contents.px_numrecords
def __iter__(self):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
setup(
name='pypxlib',

version='1.4',
version='1.5',

description=description,
long_description=
Expand Down

0 comments on commit 710e6c4

Please sign in to comment.