Contact: fumanchu@aminus.org

Log in as guest/geniusql to create tickets

root/trunk/geniusql/logicfuncs.py

Revision 206 (checked in by fumanchu, 4 years ago)

New builtin logic function alias(table.attr, 'newkey').

  • Property svn:eol-style set to native
Line 
1 """Logic functions for Geniusql."""
2
3 import datetime as _datetime
4
5
6 def icontains(a, b):
7     """Case-insensitive test b in a. Note the operand order."""
8     return icontainedby(b, a)
9
10 def icontainedby(a, b):
11     """Case-insensitive test a in b. Note the operand order."""
12     if isinstance(b, basestring):
13         # Looking for text in a string.
14         if a is None or b is None:
15             return False
16         return a.lower() in b.lower()
17     else:
18         # Looking for field in (a, b, c).
19         # Force all args to lowercase for case-insensitive comparison.
20         if a is None or not b:
21             return False
22         return a.lower() in [x.lower() for x in b]
23
24 def istartswith(a, b):
25     """True if a starts with b (case-insensitive), False otherwise."""
26     if a is None or b is None:
27         return False
28     return a.lower().startswith(b.lower())
29
30 def iendswith(a, b):
31     """True if a ends with b (case-insensitive), False otherwise."""
32     if a is None or b is None:
33         return False
34     return a.lower().endswith(b.lower())
35
36 def ieq(a, b):
37     """True if a == b (case-insensitive), False otherwise."""
38     if a is None or b is None:
39         return False
40     return (a.lower() == b.lower())
41
42 def year(value):
43     """The year attribute of a date."""
44     if isinstance(value, (_datetime.date, _datetime.datetime)):
45         return value.year
46     else:
47         return None
48
49 def month(value):
50     """The month attribute of a date."""
51     if isinstance(value, (_datetime.date, _datetime.datetime)):
52         return value.month
53     else:
54         return None
55
56 def day(value):
57     """The day attribute of a date."""
58     if isinstance(value, (_datetime.date, _datetime.datetime)):
59         return value.day
60     else:
61         return None
62
63 def now():
64     """Late-bound datetime.datetime.now(). Taint this when early binding."""
65     return _datetime.datetime.now()
66 now.irreducible = True
67
68 def utcnow():
69     """Late-bound datetime.datetime.utcnow(). Taint this when early binding."""
70     return _datetime.datetime.utcnow()
71 utcnow.irreducible = True
72
73 def today():
74     """Late-bound datetime.date.today(). Taint this when early binding."""
75     return _datetime.date.today()
76 today.irreducible = True
77
78 def iscurrentweek(value):
79     """If value is in the current week, return True, else False."""
80     if isinstance(value, (_datetime.date, _datetime.datetime)):
81         return _datetime.date.today().strftime('%W%Y') == value.strftime('%W%Y')
82     else:
83         return False
84 iscurrentweek.irreducible = True
85
86 def count(values):
87     """Return a count of the given values."""
88     return len(values)
89
90 def alias(value, key):
91     """Output the given value using the given key (instead of the default key)."""
92     # TODO: This probably isn't correct, but it's not supported in non-DB
93     # stores yet. Once that support is written, this will probably change.
94     return (value, key)
95 alias.irreducible = True
96
97
98 def init():
99     """Inject this module's functions into the logic module's globals."""
100     from geniusql import logic
101     for name, obj in globals().iteritems():
102         if isinstance(obj, type(today)):
103             logic.builtins[name] = obj
Note: See TracBrowser for help on using the browser.