Skip to content

Commit

Permalink
Meaningful error message if attempting to overwrite the df attribute (
Browse files Browse the repository at this point in the history
  • Loading branch information
rasbt committed May 26, 2017
1 parent 5655016 commit 377d11e
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 2 deletions.
8 changes: 8 additions & 0 deletions biopandas/mol2/pandas_mol2.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def df(self):
"""Acccesses the pandas DataFrame"""
return self._df

@df.setter
def df(self, value):
"""Assign a new value to the pandas DataFrame"""
raise AttributeError('Please use `PandasMol2.df_ = ... ` instead\n'
'of `PandasMol2.df = ... ` if you are sure that\n'
'you want to overwrite the `df` attribute.')
# self._df = value

def _load_mol2(self, mol2_lines, mol2_code, columns):
"""Load mol2 contents into assert_raise_message instance"""
if columns is None:
Expand Down
18 changes: 18 additions & 0 deletions biopandas/mol2/tests/test_pandas_mol2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
from biopandas.mol2 import PandasMol2
from biopandas.mol2.mol2_io import split_multimol2
from biopandas.testutils import assert_raises

this_dir = os.path.dirname(os.path.realpath(__file__))

Expand Down Expand Up @@ -55,3 +56,20 @@ def test_distance():

pdmol = PandasMol2().read_mol2(data_path)
assert round(pdmol.distance().values[0], 3) == 31.185


def test_overwrite_df():
data_path = os.path.join(this_dir, 'data', '1b5e_1.mol2')
pdmol = PandasMol2().read_mol2(data_path)

def overwrite():
pdmol.df = pdmol.df[(pdmol.df['atom_type'] != 'H')]

expect = ('Please use `PandasMol2.df_ = ... `'
' instead\nof `PandasMol2.df = ... `'
' if you are sure that\nyou want'
' to overwrite the `df` attribute.')

assert_raises(AttributeError,
expect,
overwrite)
8 changes: 8 additions & 0 deletions biopandas/pdb/pandas_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ def df(self):
"""Acccess dictionary of pandas DataFrames for PDB record sections."""
return self._df

@df.setter
def df(self, value):
"""Assign a new value to the pandas DataFrame"""
raise AttributeError('Please use `PandasPdb.df_ = ... ` instead\n'
'of `PandasPdb.df = ... ` if you are sure that\n'
'you want to overwrite the `df` attribute.')
# self._df = value

def read_pdb(self, path):
"""Read PDB files (unzipped or gzipped) from local drive
Expand Down
28 changes: 28 additions & 0 deletions biopandas/pdb/tests/test_assign_df.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# BioPandas
# Author: Sebastian Raschka <mail@sebastianraschka.com>
# License: BSD 3 clause
# Project Website: http://rasbt.github.io/biopandas/
# Code Repository: https://github.com/rasbt/biopandas

from biopandas.pdb import PandasPdb
from biopandas.testutils import assert_raises
import os


TESTDATA_FILENAME = os.path.join(os.path.dirname(__file__), 'data', '3eiy.pdb')


def test_overwrite_df():
data_path = os.path.join(os.path.dirname(__file__), 'data', '3eiy.pdb')
pdb = PandasPdb().read_pdb(data_path)

def overwrite():
pdb.df = 'bla'

expect = ('Please use `PandasPdb.df_ = ... ` instead\n'
'of `PandasPdb.df = ... ` if you are sure that\n'
'you want to overwrite the `df` attribute.')

assert_raises(AttributeError,
expect,
overwrite)
2 changes: 2 additions & 0 deletions biopandas/testutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
# License: BSD 3 clause
# Project Website: http://rasbt.github.io/biopandas/
# Code Repository: https://github.com/rasbt/biopandas

from .testutils import assert_raises
31 changes: 29 additions & 2 deletions biopandas/testutils/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,39 @@

def assertMultiLineEqual(first, second, preserve_newline=True, msg=None):
"""Assert that two multi-line strings are equal."""
assert isinstance(first, str) == True, 'First argument is not a string'
assert isinstance(second, str) == True, 'Second argument is not a string'
assert isinstance(first, str), 'First argument is not a string'
assert isinstance(second, str), 'Second argument is not a string'

if first != second:
message = ''.join(difflib.ndiff(first.splitlines(preserve_newline),
second.splitlines(preserve_newline)))
if msg:
message += " : " + msg
raise AssertionError("Multi-line strings are unequal:\n" + message)


def assert_raises(exception_type, message, func, *args, **kwargs):
"""Check that an exception is raised with a specific message
Parameters
----------
exception_type : exception
The exception that should be raised
message : str (default: None)
The error message that should be raised. Ignored if False or None
func : callable
The function that raises the exception
*args : positional arguments to `func`
**kwargs : keyword arguments to `func`
"""
try:
func(*args, **kwargs)
except exception_type as e:
error_message = str(e)
if message and message not in error_message:
raise AssertionError("Error message differs from the expected"
" string: %r. Got error message: %r" %
(message, error_message))
else:
raise AssertionError('%s not raised.' % exception_type.__name__)
1 change: 1 addition & 0 deletions docs/sources/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The CHANGELOG for the current development version is available at

- The `amino3to1` method of `biopandas.pdb.PandasPDB` objects now returns a pandas `DataFrame` instead of a pandas `Series` object. The returned data frame has two columns, `'chain_id'` and `'residue_name'`, where the former contains the chain ID of the amino acid and the latter contains the 1-letter amino acid code, respectively.
- Significant speed improvements of the `distance` method of both `PandasPdb` and `PandasMol2` (now about 300 percent faster than previously).
- Add meaningful error message if attempting to overwrite the `df` attributes of `PandasMol2` and `PandasPdb` directly.

##### Bug Fixes

Expand Down

0 comments on commit 377d11e

Please sign in to comment.