From ec2ceacd2b36b238ca4ce204802dc81a16c31c82 Mon Sep 17 00:00:00 2001 From: Oliver Beckstein Date: Fri, 22 Jan 2016 11:12:34 -0700 Subject: [PATCH] added __add__ and friends to NamedStream + test cases for add and radd - Makes ns + "something" work (= add) but "something" + ns still fails because it uses the str.__add__() and not the ns.__radd__() method - I don't quite understand why I had to add __add__ in the first place because the getattr() hack can get at the self.name.__add__ but the add test failed and passes with the explicit NamedStream.__add__ added. --- package/MDAnalysis/lib/util.py | 8 +++++++ testsuite/MDAnalysisTests/test_streamio.py | 25 +++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/package/MDAnalysis/lib/util.py b/package/MDAnalysis/lib/util.py index e829d0efbea..3bc2cd7ab68 100644 --- a/package/MDAnalysis/lib/util.py +++ b/package/MDAnalysis/lib/util.py @@ -730,6 +730,14 @@ def __lt__(self, x): def __len__(self): return len(self.name) + def __add__(self, x): + return self.name + x + + def __mul__(self, x): + return self.name * x + + __rmul__ = __mul__ + def __format__(self, format_spec): return self.name.format(format_spec) diff --git a/testsuite/MDAnalysisTests/test_streamio.py b/testsuite/MDAnalysisTests/test_streamio.py index e72f075fac5..5160a1aaca4 100644 --- a/testsuite/MDAnalysisTests/test_streamio.py +++ b/testsuite/MDAnalysisTests/test_streamio.py @@ -160,9 +160,12 @@ class TestNamedStream_filename_behavior(object): # note: no setUp() because classes with generators would run it # *for each generated test* and we need it for the generator method - def test_ospath_funcs(self): + def create_NamedStream(self): obj = cStringIO.StringIO() - ns = util.NamedStream(obj, self.textname) + return util.NamedStream(obj, self.textname) + + def test_ospath_funcs(self): + ns = self.create_NamedStream() funcs = ("abspath", "basename", "dirname", "normpath", "relpath", "split", "splitext") @@ -174,7 +177,7 @@ def _test_func(funcname, fn=self.textname, ns=ns): err_msg=("os.path.{0}() does not work with " "NamedStream").format(funcname)) # join not included because of different call signature - def _test_join(func="join", fn=self.textname, ns=ns, path="/tmp/MDAnalysisTests"): + def _test_join(funcname="join", fn=self.textname, ns=ns, path="/tmp/MDAnalysisTests"): reference = os.path.join(path, fn) value = os.path.join(path, ns) assert_equal(value, reference, @@ -184,6 +187,22 @@ def _test_join(func="join", fn=self.textname, ns=ns, path="/tmp/MDAnalysisTests" yield _test_func, func yield _test_join, "join" + def test_add(self): + ns = self.create_NamedStream() + try: + assert_equal(ns + "foo", self.textname + "foo") + except TypeError: + raise AssertionError("NamedStream does not support " + "string concatenation, NamedStream + str") + + def test_radd(self): + ns = self.create_NamedStream() + try: + assert_equal("foo" + ns, "foo" + self.textname) + except TypeError: + raise AssertionError("NamedStream does not support right " + "string concatenation, str + NamedStream") + class _StreamData(object): """Data for StreamIO functions."""