Skip to content

Instantly share code, notes, and snippets.

@gofer
Last active October 29, 2015 15:42
Show Gist options
  • Save gofer/ae593d6f3360b169dc03 to your computer and use it in GitHub Desktop.
Save gofer/ae593d6f3360b169dc03 to your computer and use it in GitHub Desktop.
import string
def all(pattern, argstr):
if isinstance(pattern, dict):
return exists(pattern, argstr)
else:
return reduce(lambda x, y : x and y,
map(lambda x : x >= 0,
map(argstr.find, filter(lambda x : isinstance(x, str), pattern))
) + map(lambda pattern : exists(pattern, argstr),
filter(lambda x : isinstance(x, dict), pattern)
)
)
def one(pattern, argstr):
if isinstance(pattern, dict):
return exists(pattern, argstr)
else:
return reduce(lambda x, y : x or y,
map(lambda x : x >= 0,
map(argstr.find, filter(lambda x : isinstance(x, str), pattern))
) + map(lambda pattern : exists(pattern, argstr),
filter(lambda x : isinstance(x, dict), pattern)
)
)
def exists(pattern, argstr):
if pattern.has_key('all'):
return all(pattern['all'], argstr)
elif pattern.has_key('one'):
return one(pattern['one'], argstr)
else:
return False
all(["a", "b", "c"], "abcdefg") # => True
all(["a", "s", "c"], "abcdefg") # => False
one(["a", "b", "t"], "abcdefg") # => True
one(["x", "y", "z"], "abcdefg") # => False
exists({'all': ["a", "b"], 'one': ["c", "s"]}, "abcdefg") # => True
exists({'all': ["a", "b", "c"]}, "abcdefg") # => True
exists({
'one': [
{'all': ["a", "b", "s"], 'one': ["c", "s"]},
{'all': ["a", "t", "c"]}
]
}, "abcdefg") # => True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment