Commits

Jonathan Eunice committed 24b4e06

improved Text.replace()

Comments (0)

Files changed (3)

         lines = self._data_to_lines(data)
         self._lines.extend(lines)
         return self
+    
+    def __contains__(self, other):
+        other = str(other)
+        return other in self.text
 
     def append(self, line, callframe=None, interpolate=True):
         if interpolate:
         Works in situ, contra str.replace().
         """
         if target and isinstance(target, dict) and replacement is None:
+            replacement = ''
             for (targ, repl) in target.items():
-                self.replace(targ, repl)
+                repl = str(repl)
+                for i, line in enumerate(self._lines):
+                    self._lines[i] = line.replace(targ, repl)
+                    replacement += repl # for newline check below
         else:
+            replacement = str(replacement)
             for i, line in enumerate(self._lines):
                 self._lines[i] = line.replace(target, replacement)
+                
+        #  recalibrate lines, if new lines included in replacement
+        if replacement and '\n' in replacement:
+            self._lines = self._data_to_lines(self.text)
             
-        # TODO: should lines be recalibrated, if there are any \n in replacement? or just assume there won't be?
+        return self
+    
+    # TODO: add doc note about caution re dict-based replacements; order is base on dict iteration order, and subsequent can overwrite previous
+            
     
     def re_replace(self, target, replacement):
         """
         target = Re(target) if not isinstance(target, Re) else target
         for i, line in enumerate(self._lines):
             self._lines[i] = target.sub(replacement, line)
+        return self
             
     # TODO: for some reason, replacement not getting to be a simplere.ReMatch obj
     # TODO: extend dict-style invocation to re_replace
 
 setup(
     name='say',
-    version=verno("0.962"),
+    version=verno("0.968"),
     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

    assert t.lines == 'a b c d'.split()
    
    t.replace('b', 'B')
-   t.replace('d', 'D')
+   tres = t.replace('d', 'D')
    assert t.lines ==  'a B c D'.split()
+   assert tres == t
    
 def test_replace_many():
    t = Text('a\nb\nc\nd')
    assert t.lines == 'a b c d'.split()
    
-   t.replace({'b': 'BEE', 'd': 'DEE'})
+   tres = t.replace({'b': 'BEE', 'd': 'DEE'})
    assert t.lines ==  'a BEE c DEE'.split()
+   assert tres == t
+
+   
+def test_replace_with_text():
+    t = Text()
+    t += 'this is\nsome\ntext here'
+    
+    middle = Text("some incredible\nawesome\nlively")
+    t.replace('some', middle)
+    assert t.text == 'this is\nsome incredible\nawesome\nlively\ntext here'
+    assert len(t) == 5
+
    
 @pytest.mark.xfail
 def test_re_replace():