Field¶
- class psynet.field.PythonClass(*args, **kwargs)[source]¶
Bases:
PythonObject
- property python_type¶
Return the Python type object expected to be returned by instances of this type, if known.
Basically, for those types which enforce a return type, or are known across the board to do such for all common DBAPIs (like
int
for example), will return that type.If a return type is not defined, raises
NotImplementedError
.Note that any type also accommodates NULL in SQL which means you can also get back
None
from any type in practice.
- class psynet.field.PythonObject(*args, **kwargs)[source]¶
Bases:
TypeDecorator
- impl¶
alias of
String
- process_bind_param(value, dialect)[source]¶
Receive a bound parameter value to be converted.
Custom subclasses of
_types.TypeDecorator
should override this method to provide custom behaviors for incoming data values. This method is called at statement execution time and is passed the literal Python data value which is to be associated with a bound parameter in the statement.The operation could be anything desired to perform custom behavior, such as transforming or serializing data. This could also be used as a hook for validating logic.
- Parameters:
value – Data to operate upon, of any type expected by this method in the subclass. Can be
None
.dialect – the
Dialect
in use.
See also
types_typedecorator
_types.TypeDecorator.process_result_value()
- process_literal_param(value, dialect)[source]¶
Receive a literal parameter value to be rendered inline within a statement.
Note
This method is called during the SQL compilation phase of a statement, when rendering a SQL string. Unlike other SQL compilation methods, it is passed a specific Python value to be rendered as a string. However it should not be confused with the
_types.TypeDecorator.process_bind_param()
method, which is the more typical method that processes the actual value passed to a particular parameter at statement execution time.Custom subclasses of
_types.TypeDecorator
should override this method to provide custom behaviors for incoming data values that are in the special case of being rendered as literals.The returned string will be rendered into the output string.
- process_result_value(value, dialect)[source]¶
Receive a result-row column value to be converted.
Custom subclasses of
_types.TypeDecorator
should override this method to provide custom behaviors for data values being received in result rows coming from the database. This method is called at result fetching time and is passed the literal Python data value that’s extracted from a database result row.The operation could be anything desired to perform custom behavior, such as transforming or deserializing data.
- Parameters:
value – Data to operate upon, of any type expected by this method in the subclass. Can be
None
.dialect – the
Dialect
in use.
See also
types_typedecorator
_types.TypeDecorator.process_bind_param()
- property python_type¶
Return the Python type object expected to be returned by instances of this type, if known.
Basically, for those types which enforce a return type, or are known across the board to do such for all common DBAPIs (like
int
for example), will return that type.If a return type is not defined, raises
NotImplementedError
.Note that any type also accommodates NULL in SQL which means you can also get back
None
from any type in practice.
- class psynet.field.VarStore(owner)[source]¶
Bases:
BaseVarStore
A repository for arbitrary variables which will be serialized to JSON for storage into the database, specifically in the
details
field. Variables can be set with the following syntax:participant.var.my_var_name = "value_to_set"
. The variable can then be accessed withparticipant.var.my_var_name
. See the methods below for an alternative API.TIP 1: the standard setter function is unavailable in lambda functions, which are otherwise convenient to use when defining e.g.
CodeBlock
objects. Usepsynet.field.VarStore.set()
instead, for example:from psynet.timeline import CodeBlock CodeBlock(lambda participant: participant.var.set("my_var", 3))
TIP 2: by convention, the
VarStore
object is placed in an object’svar
slot. You can add aVarStore
object to a custom object (e.g. a DallingerNode
) as follows:from dallinger.models import Node from psynet.field import VarStore class CustomNode(Node): __mapper_args__ = {"polymorphic_identity": "custom_node"} @property def var(self): return VarStore(self)
TIP 3: avoid storing large objects here on account of the performance cost of converting to and from JSON.