Skip to content

Instantly share code, notes, and snippets.

@showell
Created May 12, 2012 20:00
Show Gist options
  • Save showell/2668628 to your computer and use it in GitHub Desktop.
Save showell/2668628 to your computer and use it in GitHub Desktop.
wildcards from URL paths
def wildcards(path):
parts = path.split('/')
parts = filter(bool, parts)
def generate(parts):
if len(parts) == 0:
yield []
return
head = parts[0]
rest = parts[1:]
for rest in generate(rest):
yield [head] + rest
if rest and rest[0] == '%':
yield ['%'] + rest[1:]
else:
yield ['%'] + rest
variations = list(generate(parts))
def rejoin(parts):
return '/' + '/'.join(parts)
paths = map(rejoin, variations)
return paths
paths = [
'/foo/bar/baz/yo/',
'/yo/bar/whatever',
]
for path in paths:
for wildcard in wildcards(path):
print wildcard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment