[Python-ideas] Updated PEP 428 (pathlib)

Devin Jeanpierre jeanpierreda at gmail.com
Thu Mar 7 10:01:08 CET 2013


On Thu, Mar 7, 2013 at 3:23 AM, Charles-François Natali
<cf.natali at gmail.com> wrote:
> This looks really promising.
> Could one of you open an issue on the tracker and attach a patch?

The hard work is in making this a C extension module, which I don't
know much about. I could do it this weekend if there's any chance at
all it'd be accepted.

> Note that there's a problem with a current implementation:
> AssertionError: False is not true : expected 'abc' to match pattern '???*'

My bad. I always make that mistake. :(

---

def eps_closure(pattern, poses):
    for pos in poses:
        while pos < len(pattern) and pattern[pos] == '*':
            yield pos
            pos += 1
        yield pos

def fnmatch(name, pattern):
    positions = set(eps_closure(pattern, {0}))

    for char in name:
        new_positions = set()
        for pattern_pos in positions:
            if pattern_pos >= len(pattern):
                continue
            pattern_char = pattern[pattern_pos]
            if pattern_char == '*':
                if pattern_pos == len(pattern) - 1:
                    return True

                new_positions.update([pattern_pos, pattern_pos + 1])
            elif pattern_char == '?':
                new_positions.add(pattern_pos + 1)
            elif pattern[pattern_pos] == '[':
                negative = pattern[pattern_pos + 1] == "!"
                pattern_pos += 2 if negative else 1

                close_pos = pattern.index(']', pattern_pos)
                if (char in pattern[pattern_pos : close_pos]) != negative:
                    new_positions.add(close_pos + 1)
            elif char == pattern_char:
                new_positions.add(pattern_pos + 1)
        positions = set(eps_closure(pattern, new_positions))

    return len(pattern) in positions

-- Devin



More information about the Python-ideas mailing list