Skip to content

Commit

Permalink
added __add__ and friends to NamedStream + test cases for add and radd
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
orbeckst committed Jan 22, 2016
1 parent 3ad14f9 commit ec2ceac
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
8 changes: 8 additions & 0 deletions package/MDAnalysis/lib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
25 changes: 22 additions & 3 deletions testsuite/MDAnalysisTests/test_streamio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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,
Expand All @@ -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."""
Expand Down

0 comments on commit ec2ceac

Please sign in to comment.