Skip to content

Instantly share code, notes, and snippets.

@OddBloke
Created October 4, 2012 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OddBloke/3833554 to your computer and use it in GitHub Desktop.
Save OddBloke/3833554 to your computer and use it in GitHub Desktop.
# For extracting user and pass
preds = map(eq_fact, ['user_name', 'password'])
[[user_name], [password]] = pred_list_partition(preds, kwargs, False)
def eq_fact(name):
def wrapped(x):
return x == name
return wrapped
def pred_list_partition(predicate_list, in_list, with_else=True):
"""
Partitions the list based on predicate_list.
This creates len(predicate_list) lists or +1 if with_else is True
and inserts each element of in_list to the list of the first predicate
that returns true or to the last one if with else is True otherwise drops
the element
"""
if with_else:
predicate_list.append(lambda x: True)
pcount = len(predicate_list)
ret = [[] for _ in xrange(pcount)]
for el in in_list:
for i in xrange(pcount):
if predicate_list[i](el):
ret[i].append(el)
break
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment