Commits

Jonathan Eunice committed e763852

updated and tested

Comments (0)

Files changed (3)

 #!/usr/bin/env python
 
 from setuptools import setup
-from decimal import Decimal
-import re
 
 def linelist(text):
     """
     Returns each non-blank line in text enclosed in a list.
     """
-    return [ l.strip() for l in text.strip().splitlines() if l.split() ]
+    return [ l.strip() for l in text.strip().splitlines() if l.strip() ]
     
-    # The double-mention of l.strip() is yet another fine example of why
-    # Python needs en passant aliasing.
 
 setup(
     name='simplere',
     packages=['simplere'],
     install_requires=['mementos'],
     tests_require = ['tox', 'pytest'],
-    zip_safe = False,
+    zip_safe = False,  # actually it is, but this apparently avoids setuptools hacks
     keywords='re regex regular expression glob',
     classifiers=linelist("""
-        Development Status :: 3 - Alpha
+        Development Status :: 4 - Beta
         Operating System :: OS Independent
         License :: OSI Approved :: BSD License
         Intended Audience :: Developers
 """
 
 from mementos import MementoMetaclass, with_metaclass    
-import re                               # for Re
-import fnmatch                          # for Glob
-import sys                              # for en passant operation in Re
+import sys, re, fnmatch
+
+_PY3 = sys.version_info[0] > 2
+if _PY3:
+    basestring = str
 
 class ReMatch(object):
     """
-    An easier-to-use proxy for regular expression match objects. Ideally this would be
-    a subclass of the re module's match object, but their type ``_sre.SRE_Match``
-    appears to be unsubclassable
+    An easier-to-use proxy for regular expression match objects. Ideally this
+    would be a subclass of the re module's match object, but their type
+    ``_sre.SRE_Match`` appears to be unsubclassable
     <http://stackoverflow.com/questions/4835352/subclassing-matchobject-in-python>`_.
     Thus, ReMatch is a proxy exposes the match object's numeric (positional) and
     named groups through indices and attributes. If a named group has the same
             return getattr(self._match, name)
         except AttributeError:
             return AttributeError("no such attribute '{}'".format(name))
-        
+
     def __getitem__(self, index):
         return self._match.group(index)
-        
+
 class Re(with_metaclass(MementoMetaclass, object)):
-        
+
     # convenience copy of re flag constants
     
     DEBUG      = re.DEBUG
         self.pattern = pattern
         self.flags   = flags
         self.re = re.compile(pattern, flags)
-        self.groups     = self.re.groups  
+        self.groups     = self.re.groups
         self.groupindex = self.re.groupindex
         self._       = None
         
     def _regroup(self, m):
         """
         Given an _sre.SRE_Match object, create and return a corresponding
-        ReMatch object. 
+        ReMatch object.
         """
         result = ReMatch(m) if m else m
         Re._ = result
         return result
 
     def __contains__(self, item):
-        # if not isinstance(item, basestring):
-        #     item = str(item)
+        if not isinstance(item, basestring):
+             item = str(item)
         return self._regroup(self.re.search(item))
     
-    def into(self, obj):
-        self.retobj = obj
-        return self
-    
     ### methods that return ReMatch objects
     
     def search(self, *args, **kwargs):
 
     def match(self, *args, **kwargs):
         return self._regroup(self.re.match(*args, **kwargs))
-        
+    
     def finditer(self, *args, **kwargs):
         for m in self.re.finditer(*args, **kwargs):
             yield self._regroup(m)
     
     def escape(self, *args, **kwargs):
         return self.re.escape(*args, **kwargs)
-    
+  
 
 class Glob(with_metaclass(MementoMetaclass, object)):
     """
         self.pattern = pattern
         
     def __contains__(self, item):
-        return fnmatch.fnmatch(item, self.pattern)
+        return fnmatch.fnmatch(str(item), self.pattern)
 
 from simplere import *
 
-def test_Re():
+class YesItIs(ValueError): pass
+
+def test_basic():
     tests = 'some string with things in it ok?'
     
-    testpat  = Re(r'\b(s\w*)\b')
-    testpat1 = Re(r'\b(s\w*)\b')
-    assert testpat is testpat1   # test memoization
+    sword  = Re(r'\b(s\w*)\b')    
     
-    
-    if tests in testpat:
+    if tests in sword:
         assert Re._[1] == 'some'
         assert Re._.end(1) == 4
-        assert _._match.group(1) == Re._[1]
+        assert Re._._match.group(1) == Re._[1]
     else:
-        raise ValueError('yes it is!')
+        raise YesItIs()
     
-    assert testpat.findall(tests) == ['some', 'string']
+def test_findall():
+    tests = 'some string with things in it ok?'
     
+    sword  = Re(r'\b(s\w*)\b')    
+
+    assert sword.findall(tests) == ['some', 'string']
+    
+    iterlist = [ m[1] for m in sword.finditer(tests) ]
+    assert iterlist == ['some', 'string']
+    
+def test_attributes():
+    
+    tests = 'some string with things in it ok?'
+    sword  = Re(r'\b(?P<word>s\w*)\b')
+    
+    if tests in sword:
+        assert Re._.word == 'some'
+    else:
+        raise YesItIs()
+    
+    iterlist = [ m.word for m in sword.finditer(tests) ]
+    assert iterlist == ['some', 'string']
+
     person = 'John Smith 48'
     if person in Re(r'(?P<name>[\w\s]*)\s+(?P<age>\d+)'):
         assert Re._.name == 'John Smith'
         assert Re._.name == Re._._match.group('name')
         assert Re._.age  == Re._._match.group('age')
     else:
-        raise ValueError('yes it is!!!')
+        raise YesItIs()
     
     for found in Re(r'pattern (\w+)').finditer('pattern is as pattern does'):
         assert isinstance(found, ReMatch)
     found = Re(r'pattern (\w+)').findall('pattern is as pattern does')
     assert found == 'is does'.split()
     
+def test_memoization():    
+    testpat  = Re(r'\b(s\w*)\b')
+    testpat1 = Re(r'\b(s\w*)\b')
+    assert testpat is testpat1   # test memoization
+    
+    assert Glob('a*') is Glob('a*')
+
 def test_Glob():
     assert "alpha" in Glob("a*")
     assert "beta" not in Glob("a*")