pattern-based string expansion

Andrew Dalke dalke at acm.org
Mon Apr 17 05:27:31 EDT 2000


Justin Sheehy wrote:
>In fact, if you retain the expressivity of glob, the function you
>describe is not practically possible.  What should it return if given
>the glob "*"?


A generator for all possible strings?

Here's an outline for a very inefficient solution, assuming the
input is an re instead of a glob.  And, um, if there are only
a finite number of strings, this will still never terminate
until the integer counts overflow or you run out of memory.  :)

class MatchGenerator:
  def __init__(self, pattern):
    self.pattern = pattern
    self.size = 0
    self.letters = []
    self.offset = 0
    self._n = 0
  def __getitem__(self, i):
    assert self._n == i, "forward iteration only"
    x = self.next()
    if x is None:
      raise IndexError, i
    return x
  def next(self):
    self._n = self._n + 1
    while 1:
      s = string.join(self.letters)
      if self.pattern.match(s):
        break
        # advance the value of the current offset letter one character
        # if it's the end, advance the offset character
        # if *that's* at the end, try the next larger size string
    return s

That was only in jest, but it's a reasonable result.  If the
glob has an infinite number of matches, __len__ could raise some
sort of exception.  For a finite number, the getitem might even
be random access.

Finally, as an exercise for the student, it should take an optional
list of finite strings in the constructor, for backward compatibility
to glob :)

                    Andrew
                    dalke at acm.org






More information about the Python-list mailing list