Contact: fumanchu@aminus.org

Log in as guest/dejavu to create tickets

I think I've seen this ORM somewhere before...

root/branches/ldap/__init__.py

Revision 400 (checked in by fumanchu, 6 years ago)

Marking 1.5.0RC1.

  • Property svn:eol-style set to native
Line 
1 """Dejavu is an Object-Relational Mapper.
2
3 Persisted objects are called "Units", and are served into Sandboxes within
4 an Arena. Each Unit instance has a class, which maintains its schema via
5 Unit Properties.
6
7 "Dejavu", to quote Flying Circus episode 16, means "that strange feeling
8 we sometimes get that we've lived through something before." What better
9 name for a persistence system? Our terminology reflects this cognitive bent:
10 sandboxes "memorize", "recall" and "forget" Units.
11
12 Most Unit lifecycles follow the same pattern:
13     aUnit = sandbox.unit(cls, ID=ID)
14     val = aUnit.propertyName
15     aUnit.propertyName = newValue
16     del aUnit # or otherwise release the reference, e.g. close the scope.
17
18 When creating new Units, a similar pattern would be:
19     newUnit = unit_class()
20     newUnit.propertyName = newValue
21     sandbox.memorize(newUnit)
22     del newUnit # or otherwise release the reference.
23
24 Using recall(), you get a list; using xrecall(), you get an iterator:
25     for unit in sandbox.recall(cls, expr):
26         do_something_with(unit)
27
28 You destroy a Unit via Unit.forget().
29
30 Applications only need to call Unit.repress() when they wish to stop
31 caching the object, returning it to storage. This is very rare, and
32 should really only be performed within dejavu code.
33 """
34
35 __version__ =  "1.5.0RC1"
36
37
38 import datetime as _datetime
39
40 from dejavu import analysis
41 sort = analysis.sort
42
43 from dejavu.arenas import *
44 from dejavu.schemas import *
45 from dejavu.units import *
46 from dejavu import logic
47
48
49 # Use this arena instance if you are deploying a single application per
50 # process. Otherwise, you should create your own instance per application.
51 dejavuarena = Arena()
52
53
54 ###########################################################################
55 ##                                                                       ##
56 ##                           Logic functions                             ##
57 ##                                                                       ##
58 ###########################################################################
59
60
61 def icontains(a, b):
62     """Case-insensitive test b in a. Note the operand order."""
63     return icontainedby(b, a)
64
65 def icontainedby(a, b):
66     """Case-insensitive test a in b. Note the operand order."""
67     if isinstance(b, basestring):
68         # Looking for text in a string.
69         if a is None or b is None:
70             return False
71         return a.lower() in b.lower()
72     else:
73         # Looking for field in (a, b, c).
74         # Force all args to lowercase for case-insensitive comparison.
75         if a is None or not b:
76             return False
77         return a.lower() in [x.lower() for x in b]
78
79 def istartswith(a, b):
80     """True if a starts with b (case-insensitive), False otherwise."""
81     if a is None or b is None:
82         return False
83     return a.lower().startswith(b.lower())
84
85 def iendswith(a, b):
86     """True if a ends with b (case-insensitive), False otherwise."""
87     if a is None or b is None:
88         return False
89     return a.lower().endswith(b.lower())
90
91 def ieq(a, b):
92     """True if a == b (case-insensitive), False otherwise."""
93     if a is None or b is None:
94         return False
95     return (a.lower() == b.lower())
96
97 def year(value):
98     """The year attribute of a date."""
99     if isinstance(value, (_datetime.date, _datetime.datetime)):
100         return value.year
101     else:
102         return None
103
104 def month(value):
105     """The month attribute of a date."""
106     if isinstance(value, (_datetime.date, _datetime.datetime)):
107         return value.month
108     else:
109         return None
110
111 def day(value):
112     """The day attribute of a date."""
113     if isinstance(value, (_datetime.date, _datetime.datetime)):
114         return value.day
115     else:
116         return None
117
118 def now():
119     """Late-bound datetime.datetime.now(). Taint this when early binding."""
120     return _datetime.datetime.now()
121 now.bind_late = True
122
123 def today():
124     """Late-bound datetime.date.today(). Taint this when early binding."""
125     return _datetime.date.today()
126 today.bind_late = True
127
128 def iscurrentweek(value):
129     """If value is in the current week, return True, else False."""
130     if isinstance(value, (_datetime.date, _datetime.datetime)):
131         return _datetime.date.today().strftime('%W%Y') == value.strftime('%W%Y')
132     else:
133         return False
134 iscurrentweek.bind_late = True
135
136 # Inject these functions into the logic module's globals.
137 class _Empty(object): pass
138 _d = _Empty()
139 for _name in ['icontains', 'icontainedby', 'istartswith', 'iendswith',
140               'ieq', 'year', 'month', 'now', 'today', 'iscurrentweek']:
141     setattr(_d, _name, globals()[_name])
142 logic.dejavu = _d
143 del _name, _d, _Empty
Note: See TracBrowser for help on using the browser.