Jonathan Eunice avatar Jonathan Eunice committed c66d294

added +,&,| operations for adding Text objects to each other

Comments (0)

Files changed (3)

             if prelen:
                 lines = [ line[prelen:] for line in lines ]
         return lines
+    
+    
+    def __add__(self, data):
+        """
+        Add the contents of the self with the contents of the other item, returning
+        a next text. 
+        """
+        result = self.copy()
+        lines = self._data_to_lines(data)
+        callframe = inspect.currentframe().f_back
+        result.extend(lines, callframe, interpolate=True, dedent=True)
+        return result
 
+    def __or__(self, data):
+        """
+        Add the contents of the self with the contents of the other item, returning
+        a next text. 
+        """
+        result = self.copy()
+        lines = self._data_to_lines(data)
+        callframe = inspect.currentframe().f_back
+        result.extend(lines, callframe, interpolate=True, dedent=False)
+        return result
+
+    def __and__(self, data):
+        """
+        Add the contents of the self with the contents of the other item, returning
+        a next text.
+        """
+        result = self.copy()
+        lines = self._data_to_lines(data)
+        result._lines.extend(lines)
+        return result
+    
+    # TODO: Add the final 'yes dedent, no interpret' operation
+    
     def __iadd__(self, data):
         """
         In-place add the text or lines contained in data, with auto-dedent.
         return iter(self._lines)
     
     def _data_to_lines(self, data):
-        if isinstance(data, list):
+        if isinstance(data, Text):
+            return data.lines
+        elif isinstance(data, list):
             return [ line.rstrip('\n') for line in data ]
         else:
             return data.splitlines()
 
 setup(
     name='say',
-    version=verno("0.954"),
+    version=verno("0.962"),
     author='Jonathan Eunice',
     author_email='jonathan.eunice@gmail.com',
     description='Super-simple templated printing. E.g.: say("Hello, {whoever}!", indent=1)',

test/test_text.py

 
 import six, os
 from say import Text
+import pytest
 
 def test_text_basic():
    t = Text()
             # with one indented line
     """
     assert t2.text == "# this is a script\n# which should be left aligned and compact\n    # with one indented line"
- 
+    
+
+def test_adding_other_text():
+    
+    t = Text("this is good")
+    u = Text("this too!")
+    
+    tu = t + u
+    assert tu.text == "this is good\nthis too!"
+    
+    t += u
+    assert t.text == "this is good\nthis too!"
+
+     
 def test_indexing():
     t = Text('this\nis\nsomething')
     assert t[0] == 'this'
    t.replace({'b': 'BEE', 'd': 'DEE'})
    assert t.lines ==  'a BEE c DEE'.split()
    
+@pytest.mark.xfail
 def test_re_replace():
    t = Text('a\nb\nc\nd')
    assert t.lines == 'a b c d'.split()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.