def func_factory(method): try: func = getattr(math, method) except AttributeError: return def inner(arg1, arg2=None): try: return func(arg1, arg2) except TypeError: return func(arg1) inner.__name__ = method doc = func.__doc__.splitlines() if len(doc) > 1 and not doc[1]: doc = doc[2:] inner.__doc__ = '\n'.join(doc) if method.startswith('is'): return comparison(inner) return filter(function(inner))
import re from native_tags.decorators import comparison, function def matches(pattern, text): 'String comparison. True if string ``text`` matches regex ``pattern``' return re.compile(str(pattern)).match(text) matches = comparison(matches) def substitute(search, replace, text): 'Regex substitution function. Replaces regex ``search`` with ``replace`` in ``text``' return re.sub(re.compile(str(search)), replace, text) substitute = function(substitute) def search(pattern, text): 'Regex pattern search. Returns match if ``pattern`` is found in ``text``' return re.compile(str(pattern)).search(str(text)) search = function(search)
users = get_people_i_follow(viewed_user, count) return {"viewed_user":viewed_user, "element_id": element_id, "callback": callback, "count": count, "users": users} register.inclusion_tag("profiles/show_contact_select.html")(show_contact_select) def are_friends(user, user2): if user.is_authenticated(): are_friend = Friendship.objects.are_friends(user, user2) return are_friend else: return False are_friends = comparison(are_friends) def received_friendship_request_from_user(user, from_user): if from_user.is_authenticated(): are_friend = FriendshipInvitation.objects.invitations(to_user=user, from_user=from_user) return are_friend return False received_friendship_request_from_user = comparison(received_friendship_request_from_user) def sent_friendship_request_to_user(user, to_user): if to_user.is_authenticated(): are_friend = FriendshipInvitation.objects.invitations(to_user=to_user,
import operator from native_tags.decorators import comparison, function # Comparison operators def lt(a, b): return operator.lt(a, b) lt = comparison(lt, doc=operator.lt.__doc__) def le(a, b): return operator.le(a, b) le = comparison(le, doc=operator.le.__doc__) def eq(a, b): return operator.eq(a, b) eq = comparison(eq, doc=operator.eq.__doc__) def ne(a, b): return operator.ne(a, b) ne = comparison(ne, doc=operator.ne.__doc__) def ge(a, b): return operator.ge(a, b) ge = comparison(ge, doc=operator.ge.__doc__) def gt(a, b): return operator.gt(a, b) gt = comparison(gt, doc=operator.gt.__doc__) def not_(a): return operator.not_(a)
import re from native_tags.decorators import comparison, function def matches(pattern, text): 'String comparison. True if string ``text`` matches regex ``pattern``' return re.compile(str(pattern)).match(text) matches = comparison(matches) matches.test = {'args': ('\d', '_'), 'result': None} def substitute(search, replace, text): 'Regex substitution function. Replaces regex ``search`` with ``replace`` in ``text``' return re.sub(re.compile(str(search)), replace, text) substitute = function(substitute) substitute.test = {'args': ('w', 'f', 'wtf'), 'result': 'ftf'} def search(pattern, text): 'Regex pattern search. Returns match if ``pattern`` is found in ``text``' return re.compile(str(pattern)).search(str(text)) search = function(search) search.test = {'args': ('\d', 'asdfasdfwe'), 'result': None}
def dynamic(*a, **kw): return list(a) + sorted(kw.items()) dynamic = function(dynamic) def no_render(*a, **kw): return list(a) + sorted(kw.items()) no_render = function(no_render, resolve=False) def myfilter(value, arg):# a, b, c): return value + arg myfilter = filter(myfilter) def adder(x, y): return x + y adder = function(adder, name='add') def cmp_kwargs(**kw): return len(kw) cmp_kwargs = comparison(cmp_kwargs) def myinc(noun): return 'unittest.html', {'noun': noun} myinc = function(myinc, inclusion=True) def ifsomething(): return True ifsomething = comparison(ifsomething) def date(): return datetime.now() date = function(date, cache=3600)
myfilter = filter(myfilter, test={'args': (1, 1), 'result': 2}) def adder(x, y): return x + y adder = function(adder, name='add', test={'args': (1, 1), 'result': 2}) def cmp_kwargs(**kw): return len(kw) cmp_kwargs = comparison(cmp_kwargs) def myinc(noun): return 'unittest.html', {'noun': noun} myinc = function(myinc, inclusion=True) def ifsomething(): return True ifsomething = comparison(ifsomething)
Comparison tags """ try: set except NameError: from sets import Set as set from django.conf import settings from native_tags.decorators import comparison def less(x, y): 'True if x is less than y' return x < y less = comparison(less, test={'args': (0, 1)}) def less_or_equal(x, y): 'True if x is less than or equal to y' return x <= y less_or_equal = comparison(less_or_equal, test={'args': (1, 1)}) def greater_or_equal(x, y): 'True if x is greater than or equal to y' return x >= y
""" Comparison tags """ try: set except NameError: from sets import Set as set from django.conf import settings from native_tags.decorators import comparison def less(x,y): 'True if x is less than y' return x < y less = comparison(less, test={'args':(0,1)}) def less_or_equal(x,y): 'True if x is less than or equal to y' return x <= y less_or_equal = comparison(less_or_equal, test={'args':(1,1)}) def greater_or_equal(x,y): 'True if x is greater than or equal to y' return x >= y greater_or_equal = comparison(greater_or_equal, test={'args':(2,1)}) def greater(x,y): 'True if x is greater than y' return x > y greater = comparison(greater, test={'args':(2,1)}) def startswith(x,y):
""" Comparison tags """ try: set except NameError: from sets import Set as set from django.conf import settings from native_tags.decorators import comparison def less(x,y): 'True if x is less than y' return x < y less = comparison(less) def less_or_equal(x,y): 'True if x is less than or equal to y' return x <= y less_or_equal = comparison(less_or_equal) def greater_or_equal(x,y): 'True if x is greater than or equal to y' return x >= y greater_or_equal = comparison(greater_or_equal) def greater(x,y): 'True if x is greater than y' return x > y greater = comparison(greater) def startswith(x,y):
import operator from native_tags.decorators import comparison, function # Comparison operators def lt(a, b): return operator.lt(a, b) lt = comparison(lt, doc=operator.lt.__doc__) def le(a, b): return operator.le(a, b) le = comparison(le, doc=operator.le.__doc__) def eq(a, b): return operator.eq(a, b) eq = comparison(eq, doc=operator.eq.__doc__) def ne(a, b): return operator.ne(a, b)