Code: Alles auswählen
class F(functools.partial):
"""Adds function composition to functools.partial.
E.g. F(map, f) * F(filter, g) ->
lambda iter: map(f, filter(g, iter))"""
def __mul__(left, right):
return F(lambda *args, **kwargs: left(right(*args, **kwargs)))
def flip(f):
"Takes a function that takes two arguments and returns a function "
"with the arguments reversed."
return lambda x, y: f(y, x)
def maybe(value, just_f, none_f):
"returns just_f(value) if value is not None else none_f()."
return none_f() if value is None else just_f(value)
def maybe_args(just_f, none_f, *args, **kwargs):
"returns just_f with all additional arguments if none of them are None "
"else none_f()"
return none_f() if (
any(arg is None for arg in args) or
any(arg is None for arg in kwargs.values())
) else just_f(*args, **kwargs)
Code: Alles auswählen
get_session_id = F(string.rstrip, chars='L') * F(string.lstrip, chars='0x') * F(hex) * F(random.getrandbits, 128)