Created
May 12, 2012 20:00
-
-
Save showell/2668628 to your computer and use it in GitHub Desktop.
wildcards from URL paths
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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