130
145
variable_class = None
132
147
def __init__(self, name=None, primary=False, **kwargs):
149
@param name: The name of this property.
150
@param primary: A boolean indicating whether this property is a
152
@param default: The initial value of this variable. The default
153
behavior is for the value to stay undefined until it is
155
@param default_factory: If specified, this will immediately be
156
called to get the initial value.
157
@param allow_none: A boolean indicating whether None should be
158
allowed to be set as the value of this variable.
159
@param validator: Validation function called whenever trying to
160
set the variable to a non-db value. The function should
161
look like validator(object, attr, value), where the first and
162
second arguments are the result of validator_object_factory()
163
(or None, if this parameter isn't provided) and the value of
164
validator_attribute, respectively. When called, the function
165
should raise an error if the value is unacceptable, or return
166
the value to be used in place of the original value otherwise.
167
@param kwargs: Other keyword arguments passed through when
168
constructing the underlying variable.
133
170
kwargs["value"] = kwargs.pop("default", Undef)
134
171
kwargs["value_factory"] = kwargs.pop("default_factory", Undef)
135
172
Property.__init__(self, name, primary, self.variable_class, kwargs)
138
175
class Bool(SimpleProperty):
178
This accepts integer, L{float}, or L{decimal.Decimal} values, and stores
139
181
variable_class = BoolVariable
141
184
class Int(SimpleProperty):
187
This accepts integer, L{float}, or L{decimal.Decimal} values, and stores
142
190
variable_class = IntVariable
144
193
class Float(SimpleProperty):
196
This accepts integer, L{float}, or L{decimal.Decimal} values, and stores
197
them as floating-point values.
145
199
variable_class = FloatVariable
147
202
class Decimal(SimpleProperty):
205
This accepts integer or L{decimal.Decimal} values, and stores them as
206
text strings containing their decimal representation.
148
208
variable_class = DecimalVariable
150
211
class Bytes(SimpleProperty):
214
This accepts L{bytes}, L{buffer} (Python 2), or L{memoryview} (Python 3)
215
objects, and stores them as byte strings.
217
Deprecated aliases: L{Chars}, L{RawStr}.
151
219
variable_class = BytesVariable
153
222
# OBSOLETE: Bytes was Chars in 0.9. This will die soon.
155
224
# DEPRECATED: Bytes was RawStr until 0.22.
158
228
class Unicode(SimpleProperty):
231
This accepts L{unicode} (Python 2) or L{str} (Python 3) objects, and
232
stores them as text strings. Note that it does not accept L{str}
159
235
variable_class = UnicodeVariable
161
238
class DateTime(SimpleProperty):
239
"""Date and time property.
241
This accepts aware L{datetime.datetime} objects and stores them as
242
timestamps; it also accepts integer or L{float} objects, converting them
243
using L{datetime.utcfromtimestamp}. Note that it does not accept naive
244
L{datetime.datetime} objects (those that do not have timezone
162
247
variable_class = DateTimeVariable
164
250
class Date(SimpleProperty):
253
This accepts L{datetime.date} objects and stores them as datestamps; it
254
also accepts L{datetime.datetime} objects, converting them using
255
L{datetime.datetime.date}.
165
257
variable_class = DateVariable
167
260
class Time(SimpleProperty):
263
This accepts L{datetime.time} objects and stores them as datestamps; it
264
also accepts L{datetime.datetime} objects, converting them using
265
L{datetime.datetime.time}.
168
267
variable_class = TimeVariable
170
270
class TimeDelta(SimpleProperty):
271
"""Time delta property.
273
This accepts L{datetime.timedelta} objects and stores them as time
171
276
variable_class = TimeDeltaVariable
173
279
class UUID(SimpleProperty):
282
This accepts L{uuid.UUID} objects and stores them as their text
174
285
variable_class = UUIDVariable
176
288
class Pickle(SimpleProperty):
291
This accepts any object that can be serialized using L{pickle}, and
292
stores it as a byte string containing its pickled representation.
177
294
variable_class = PickleVariable
179
297
class JSON(SimpleProperty):
300
This accepts any object that can be serialized using L{json}, and stores
301
it as a text string containing its JSON representation.
180
303
variable_class = JSONVariable
183
306
class List(SimpleProperty):
309
This accepts iterable objects and stores them as a list where each
310
element is an object of the given value type.
184
312
variable_class = ListVariable
186
314
def __init__(self, name=None, **kwargs):
316
@param name: The name of this property.
317
@param type: An instance of L{Property} defining the type of each
318
element of this list.
319
@param default_factory: If specified, this will immediately be
320
called to get the initial value.
321
@param validator: Validation function called whenever trying to
322
set the variable to a non-db value. The function should
323
look like validator(object, attr, value), where the first and
324
second arguments are the result of validator_object_factory()
325
(or None, if this parameter isn't provided) and the value of
326
validator_attribute, respectively. When called, the function
327
should raise an error if the value is unacceptable, or return
328
the value to be used in place of the original value otherwise.
329
@param kwargs: Other keyword arguments passed through when
330
constructing the underlying variable.
187
332
if "default" in kwargs:
188
333
raise ValueError("'default' not allowed for List. "
189
334
"Use 'default_factory' instead.")