1. Jonathan Eunice
  2. intensional

Source

intensional / more docs.txt


  * A nicer way to use regular expressions
  * A ``switch`` statement (actually, function) for Python


Usage
=====

Instead of::

    import re
    
    match = re.search(pattern, some_string)
    if match:
        print match.group(1)

You can do an *en passant* test::

    from intensional import Re
    
    if some_string in Re(pattern):
        print _[1]
        
There are two things to note here: First,
this turns the sense of the matching around, asking "is a given string *in*
the set of items this pattern describes?" The ``Re`` pattern is an intensionally
defined set (namely "all strings matching the pattern"). This order often makes
excellent sense whey you have a clear intent for the test. For example, "is the
given string within the set of *all legitimate commands*?"

Second, the ``in`` test had the side effect of setting the underscore
name ``_`` to the result. Python doesn't support *en passant* assignment, so
you can't both test and collect results in the same motion, even though that's
sometimes exactly appropriate. ``Re`` use introspection to get around this
difficulty and provide neat results.

If you prefer the more traditional ``re`` calls, you can still use them with the
convenient *en passant* style.::

    if Re(pattern).search(some_string):
        print _[1]
        
``Re`` works even better with named pattern components, such as::

    person = 'John Smith 48'
    if person in Re(r'(?P<name>[\w\s]*)\s+(?P<age>\d+)'):
        print _.name, "is", _.age, "years old"
    else:
        print "don't understand '{}'".format(person)
        
        
It's possible also to loop over the results, as in::

    for found in Re(r'(pattern\w*)').findall('pattern is as pattern does'):
        print found
        
``Re`` objects are `memoized <http://en.wikipedia.org/wiki/Memoization>`_ for efficiency, so they
are only compiled once, regardless of how many times they're mentioned in the program.


    
Notes
=====

It's entirely possible to implement an intensional set like idea with
other Python mechanisms, such as ``lambda`` expressions, test functions,
and list comprehensions. Many (most?) Python programs already *de facto*
use this implicit intensional set approach. Making intensional sets
explicit, however, has the benefit of making what's being done more
clear. It provides a mechanism for cleaning up several sort of ucky
parts of Python:

 *  Messy, guts-exposed regular expression tests
 *  Overly verbose lambda expressions
 *  Long, non-DRY ``if``/``elif``/``else`` constructions (with ``switch``)
 *  A mess of mechanisms for collection filtering