Values¶
Values are instances of types. Value is the base class with factory methods
for creating and converting values.
When to use: Use values to hold typed data. Primitives are immutable; containers (Vector, Map, Set, XArray) are mutable.
Quick Start¶
>>> from dsviper import Value, ValueString, ValueInt64, TypeVector, Type
Primitives construct directly; containers go through Value.create:
>>> name = ValueString("Alice")
>>> count = ValueInt64(42)
>>> numbers = Value.create(TypeVector(Type.INT64), [1, 2, 3])
Coming back out to Python objects is Value.dumps for anything, and
encoded() for a primitive:
>>> Value.dumps(name)
'Alice'
>>> name.encoded()
'Alice'
>>> Value.dumps(numbers)
[1, 2, 3]
Reading an element of a container already gives a native:
>>> numbers[0]
1
>>> name.type()
string
A Value is not the Python object it wraps — str() gives the DSM
representation, quotes included:
>>> isinstance(name, str)
False
>>> str(name)
"'Alice'"
Choosing the Right Pattern¶
Pattern |
When to Use |
Example |
|---|---|---|
|
Direct primitive construction |
|
|
Generic factory with type |
|
|
Deserialize from binary |
|
|
Type casting |
|
|
Back out to Python objects |
|
|
Back out a primitive |
|
Base Class¶
Creates values, and converts between them and Python objects. |
Primitive Values¶
A value of type void. |
|
A value of type bool. |
|
A value of type uint8. |
|
A value of type uint16. |
|
A value of type uint32. |
|
A value of type uint64. |
|
A value of type int8. |
|
A value of type int16. |
|
A value of type int32. |
|
A value of type int64. |
|
A value of type float. |
|
A value of type double. |
|
A value of type string. |
|
A value of type blob. |
|
A value of type blob_id. |
|
A value of type commit_id. |
|
A value of type uuid. |
Container Values¶
A value of type vector<element_type>. |
|
Iterator for ValueVector elements. |
|
A value of type set<element_type>. |
|
Iterator for ValueSet elements. |
|
A value of type map<key_type, element_type>. |
|
Iterator for ValueMap keys. |
|
Iterator for ValueMap values. |
|
Iterator for ValueMap items (key-value pairs). |
|
A value of type xarray<element_type>. |
|
A value of type optional<element_type>. |
Algebraic Values¶
A value of type tuple<T0, ...>. |
|
Iterator for ValueTuple elements. |
|
A value of type vec<numeric_type, size>. |
|
A value of type mat<numeric_type, columns, rows>, stored column-major. |
|
A value of type variant<T0, ...>. |
|
A value of any type. |
User-Defined Values¶
A value of type struct. |
|
A value of type enum. |
|
A value of type key<element_type>. |
Value Program¶
A ValueProgram is a sequence of opcodes that describe mutations to values.
Each opcode represents an atomic operation (set, update, union, subtract, etc.).
The sequence of opcodes a commit carries. |
|
What an opcode applies to: an attachment, an instance, and that instance's concept. |
|
The base of the opcode hierarchy. |
|
Writes a whole document at a key - the only opcode without a path. |
|
Writes a value at a path inside a document. |
|
Adds entries to the map at a path, replacing the keys already there. |
|
Removes from the map at a path the entries whose key it carries. |
|
Writes entries over the keys the map at a path already holds, inserting none. |
|
Adds elements to the set at a path. |
|
Removes elements from the set at a path. |
|
Inserts a position into the xarray at a path, before another one. |
|
Removes the element at a position of the xarray at a path. |
|
Writes a value at a position of the xarray at a path. |
Executing a program leaves a trace of the opcodes it ran, in the same vocabulary:
The execution of one program: whether it ran, and the opcodes it applied. |
|
One step of a traced execution: the opcode, and the exception it raised if it did. |