From 4f3b74ebecf9559a8cac861e67fdf864c6507167 Mon Sep 17 00:00:00 2001 From: rasbt Date: Fri, 26 May 2017 14:33:23 -0400 Subject: [PATCH 1/3] overwrite df safeguard message --- biopandas/mol2/pandas_mol2.py | 8 +++++++ biopandas/mol2/tests/test_pandas_mol2.py | 19 ++++++++++++++++ biopandas/pdb/pandas_pdb.py | 8 +++++++ biopandas/pdb/tests/test_assign_df.py | 28 ++++++++++++++++++++++++ biopandas/testutils/__init__.py | 2 ++ biopandas/testutils/testutils.py | 27 +++++++++++++++++++++++ docs/sources/CHANGELOG.md | 1 + 7 files changed, 93 insertions(+) create mode 100644 biopandas/pdb/tests/test_assign_df.py diff --git a/biopandas/mol2/pandas_mol2.py b/biopandas/mol2/pandas_mol2.py index 15e926d..032bd86 100644 --- a/biopandas/mol2/pandas_mol2.py +++ b/biopandas/mol2/pandas_mol2.py @@ -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: diff --git a/biopandas/mol2/tests/test_pandas_mol2.py b/biopandas/mol2/tests/test_pandas_mol2.py index d506eda..f6d56eb 100644 --- a/biopandas/mol2/tests/test_pandas_mol2.py +++ b/biopandas/mol2/tests/test_pandas_mol2.py @@ -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__)) @@ -55,3 +56,21 @@ 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) + diff --git a/biopandas/pdb/pandas_pdb.py b/biopandas/pdb/pandas_pdb.py index d97f0d4..3ac0e44 100644 --- a/biopandas/pdb/pandas_pdb.py +++ b/biopandas/pdb/pandas_pdb.py @@ -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 diff --git a/biopandas/pdb/tests/test_assign_df.py b/biopandas/pdb/tests/test_assign_df.py new file mode 100644 index 0000000..77d7140 --- /dev/null +++ b/biopandas/pdb/tests/test_assign_df.py @@ -0,0 +1,28 @@ +# BioPandas +# Author: Sebastian Raschka +# 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) diff --git a/biopandas/testutils/__init__.py b/biopandas/testutils/__init__.py index 64f5f57..ae2ad5f 100644 --- a/biopandas/testutils/__init__.py +++ b/biopandas/testutils/__init__.py @@ -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 \ No newline at end of file diff --git a/biopandas/testutils/testutils.py b/biopandas/testutils/testutils.py index ac58ea0..672e013 100644 --- a/biopandas/testutils/testutils.py +++ b/biopandas/testutils/testutils.py @@ -18,3 +18,30 @@ def assertMultiLineEqual(first, second, preserve_newline=True, msg=None): 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__) diff --git a/docs/sources/CHANGELOG.md b/docs/sources/CHANGELOG.md index 16ffb2a..72cecb2 100755 --- a/docs/sources/CHANGELOG.md +++ b/docs/sources/CHANGELOG.md @@ -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 From d2518e39a484dfff7c2f3856f4af63238f833e63 Mon Sep 17 00:00:00 2001 From: rasbt Date: Fri, 26 May 2017 14:37:06 -0400 Subject: [PATCH 2/3] style fixes --- biopandas/mol2/tests/test_pandas_mol2.py | 1 - biopandas/testutils/__init__.py | 2 +- biopandas/testutils/testutils.py | 4 ++-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/biopandas/mol2/tests/test_pandas_mol2.py b/biopandas/mol2/tests/test_pandas_mol2.py index f6d56eb..0d20b74 100644 --- a/biopandas/mol2/tests/test_pandas_mol2.py +++ b/biopandas/mol2/tests/test_pandas_mol2.py @@ -73,4 +73,3 @@ def overwrite(): assert_raises(AttributeError, expect, overwrite) - diff --git a/biopandas/testutils/__init__.py b/biopandas/testutils/__init__.py index ae2ad5f..19baa45 100644 --- a/biopandas/testutils/__init__.py +++ b/biopandas/testutils/__init__.py @@ -4,4 +4,4 @@ # Project Website: http://rasbt.github.io/biopandas/ # Code Repository: https://github.com/rasbt/biopandas -from .testutils import assert_raises \ No newline at end of file +from .testutils import assert_raises diff --git a/biopandas/testutils/testutils.py b/biopandas/testutils/testutils.py index 672e013..cda8f5d 100644 --- a/biopandas/testutils/testutils.py +++ b/biopandas/testutils/testutils.py @@ -9,8 +9,8 @@ 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), From d97595e02aa4f0cc7898a3439d2844476a7a4c0f Mon Sep 17 00:00:00 2001 From: rasbt Date: Fri, 26 May 2017 14:43:16 -0400 Subject: [PATCH 3/3] style fixes --- biopandas/testutils/testutils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/biopandas/testutils/testutils.py b/biopandas/testutils/testutils.py index cda8f5d..0b2af27 100644 --- a/biopandas/testutils/testutils.py +++ b/biopandas/testutils/testutils.py @@ -28,10 +28,10 @@ def assert_raises(exception_type, message, func, *args, **kwargs): 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. + 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`. + *args : positional arguments to `func` **kwargs : keyword arguments to `func` """