Commits

Jonathan Eunice  committed 90e08d6

more complete ReMatch direct use; more tests; doc tweaks

  • Participants
  • Parent commits 9e06ac9

Comments (0)

Files changed (4)

     else:
         raise ValueError('YES IT IS')
 
+Partial Credit
+==============
+
+If you have existing code that already uses ``re`` regular expressions
+and you want to ease into using ``simplere``, ``ReMatch`` objects can
+simply wrap the ``_sre.SRE_Match`` objects that ``re`` returns.::
+
+    match = re.match(r'(?P<word>this)', 'this is a string')
+    match = ReMatch(match)
+    if match:
+        print match.group(1)    # still works
+        print match[1]          # same thing
+        print match.word        # same thing, with logical name
+   
+
 Notes
 =====
    
 
 setup(
     name='simplere',
-    version='0.25',
+    version='0.26',
     author='Jonathan Eunice',
     author_email='jonathan.eunice@gmail.com',
     description='Simpler, cleaner access to regular expressions. Globs too.',

File simplere/core.py

 from mementos import MementoMetaclass, with_metaclass    
 import sys, re, fnmatch
 
-_PY3 = sys.version_info[0] > 2
+_PY3 = sys.version_info[0] == 3
 if _PY3:
     basestring = str
-    
-
 
 class ReMatch(object):
     """
      
     def __init__(self, match):
         self._match = match
-        self._groupdict = match.groupdict()
+        if match is not None:
+            self._groupdict = match.groupdict()
+
+    def _bool(self):
+        """
+        Return True if the match was successful, and False if it failed.
+        """
+        return bool(self._match)
+
+    if _PY3:
+        __bool__ = _bool
+    else:
+        __nonzero__ = _bool
         
     def __getattr__(self, name):
         if name in self.__dict__:
     def split(self, *args, **kwargs):
         return self.re.split(*args, **kwargs)
     
-    def sub(self, repl, *args, **kwargs):
-        return self.re.sub(regrouped(repl), *args, **kwargs)
+    def sub(self, repl, string, *args, **kwargs):
+        return self.re.sub(regrouped(repl), string, *args, **kwargs)
     
     def subn(self, repl, *args, **kwargs):
-        return self.re.subn(regrouped(repl), *args, **kwargs)
+        return self.re.subn(regrouped(repl), string, *args, **kwargs)
     
     def escape(self, *args, **kwargs):
         return self.re.escape(*args, **kwargs)

File test/test.py

     assert repat.findall(tests) == ['some', 'string']
     
     assert 'ddd' not in repat
+    
+def test_direct_ReMatch():
+    
+    m = re.match(r'this', 'that')
+    assert not m
+    assert not ReMatch(m)
+    
+    m = re.match(r'this', 'this')
+    assert m
+    assert ReMatch(m)
+    
+def test_direct_ReMatch_easy_access():
+    m = re.match(r'this', 'this')
+    rm = ReMatch(m)
+    assert m.group(0) == 'this'
+    assert rm.group(0) == 'this'
+
+    match = re.match(r'(?P<word>this)', 'this is a string')
+    match = ReMatch(match)
+    assert match[1] == match.word
+    assert match.group(1) == match.word
+
 
 def test_Glob():
     assert "alpha" in Glob("a*")