Query modules Python API
This is the API documentation for mgp.py
that contains definitions of the
public Python API provided by Memgraph. In essence, this is a wrapper around the
C API. This source file can be found in the Memgraph
installation directory, under /usr/lib/memgraph/python_support
.
For an example of how to implement query modules in Python, take a look at the example we provided.
If you install any Python modules after running Memgraph, you'll have to load them into Memgraph or restart Memgraph in order to use them.
You can also develop query modules in Python from Memgraph Lab (v2.0 and newer). Just navigate to Query Modules and click on New Module to start.
If you need an additional Python library not included with Memgraph, check out the guide on how to install it.
mgp.read_proc(func: Callable[…, mgp.Record])
Register func as a read-only procedure of the current module.
read_proc
is meant to be used as a decorator function to register module
procedures. The registered func needs to be a callable which optionally takes
ProcCtx
as the first argument. Other arguments of func will be bound to values
passed in the Cypher query. The full signature of func needs to be annotated with
types. The return type must be Record(field_name=type, …)
and the procedure must
produce either a complete Record
or None
. To mark a field as deprecated, use
Record(field_name=Deprecated(type), …)
. Multiple records can be produced by
returning an iterable of them. Registering generator functions is currently not
supported.
In the in-memory analytical storage mode, graph elements may be deleted by parallel transactions. If the return record contains any deleted values, that record is automatically skipped.
Example usage
import mgp
@mgp.read_proc
def procedure(context: mgp.ProcCtx,
required_arg: mgp.Nullable[mgp.Any],
optional_arg: mgp.Nullable[mgp.Any] = None
) -> mgp.Record(result=str, args=list):
args = [required_arg, optional_arg]
# Multiple rows can be produced by returning an iterable of mgp.Record
return mgp.Record(args=args, result='Hello World!')
The example procedure above returns 2 fields: args
and result
.
args
is a copy of arguments passed to the procedure.result
is the result of this procedure, a “Hello World!” string.
Any errors can be reported by raising an Exception
.
The procedure can be invoked in Cypher using the following calls:
CALL example.procedure(1, 2) YIELD args, result;
CALL example.procedure(1) YIELD args, result;
Naturally, you may pass in different arguments or yield less fields.
Install the mgp
Python module so your editor can use typing annotations
properly and suggest methods and classes it contains. You can install the module
by running pip install mgp
.
mgp.write_proc(func: Callable[…, mgp.Record])
Register func as a writeable procedure of the current module.
write_proc
is meant to be used as a decorator function to register module
procedures. The registered func needs to be a callable which optionally takes
ProcCtx
as the first argument. Other arguments of func will be bound to values
passed in the Cypher query. The full signature of func needs to be annotated with
types. The return type must be Record(field_name=type, …)
and the procedure must
produce either a complete Record
or None
. To mark a field as deprecated, use
Record(field_name=Deprecated(type), …)
. Multiple records can be produced by
returning an iterable of them. Registering generator functions is currently not
supported.
In the in-memory analytical storage mode, graph elements may be deleted by parallel transactions. If the return record contains any deleted values, that record is automatically skipped.
Example usage
import mgp
@mgp.write_proc
def procedure(context: mgp.ProcCtx,
required_arg: str,
optional_arg: mgp.Nullable[str] = None
) -> mgp.Record(result=mgp.Vertex):
vertex = context.graph.create_vertex()
vertex_properties = vertex.properties
vertex_properties[“required_arg”] = required_arg
if optional_arg is not None:
vertex_properties[“optional_arg”] = optional_arg
return mgp.Record(result=vertex)
The example procedure above returns a newly created vertex which has at most 2 properties:
required_arg
is always present and its value is the first argument of the procedure.optional_arg
is present if the second argument of the procedure is not null.
Any errors can be reported by raising an Exception
.
The procedure can be invoked in Cypher using the following calls:
CALL example.procedure(“property value”, “another one”) YIELD result;
CALL example.procedure(“single argument”) YIELD result;
Naturally, you may pass in different arguments.
mgp.add_batch_read_proc(func: Callable[…, mgp.Record], initializer: typing.Callable, cleanup: typing.Callable)
Register func
as a read-only batch procedure of the current module.
func
represents a function that is invoked through OpenCypher. Through OpenCypher user invokes func
. Memgraph invokes first the initializer
function. After the initializer
function, func
is called until it returns an empty result. Afterward, the cleanup
function is called, which can be used to clean up global resources. Only at that point is garbage collection invoked, so any dangling references to Python objects will be cleaned.
initializer
must define the same parameters as the main func
function, and will receive the same parameters as func
. The position of arguments and the type of arguments must be the same.
Otherwise, the same rules apply as in read_proc
. It's important to keep in mind that no Memgraph resources can be stored in init
and during batching. After initializer
and each func
call, every Memgraph-related object is invalidated and can't be used later on.
mgp.add_batch_write_proc(func: Callable[…, mgp.Record], initializer: typing.Callable, cleanup: typing.Callable)
Register func
as a writeable batch procedure of the current module.
The same rules for parameters and order of calls to functions apply for a writeable procedure as for the read-only batched procedure.
mgp.function(func: Callable[[…]])
Register func as a Memgraph function in the current module.
function
is meant to be used as a decorator function to register module
functions. The registered func needs to be a callable which optionally takes
FuncCtx
as the first argument. Other arguments of func will be bound to values
passed in the Cypher query. Only the funcion arguments need to be annotated with
types. The return type doesn't need to be specified, but it has to be supported
by mgp.Any
. Registering generator functions is currently not supported.
In the in-memory analytical storage mode, graph elements may be deleted by parallel transactions. If the return value is deleted, a null value is returned instead.
Example usage
import mgp
@mgp.function
def func_example(context: mgp.FuncCtx,
required_arg: str,
optional_arg: mgp.Nullable[str] = None
):
return_args = [required_arg]
if optional_arg is not None:
return_args.append(optional_arg)
# Return any kind of result supported by mgp.Any
return return_args
The example function above returns a list of provided arguments:
required_arg
is always present and its value is the first argument of the function.optional_arg
is present if the second argument of the function is notnull
.
Any errors can be reported by raising an Exception
.
The function can be invoked in Cypher using the following calls:
RETURN example.func_example("first argument", "second_argument");
RETURN example.func_example("first argument");
Naturally, you may pass in different arguments.
This module provides the API for usage in custom openCypher procedures.
Label Objects
class Label()
Label of a Vertex
.
name
@property
def name() -> str
Get the name of the label.
Returns:
A string that represents the name of the label.
Example:
label.name
Properties Objects
class Properties()
A collection of properties either on a Vertex
or an Edge
.
get()
def get(property_name: str, default=None) -> object
Get the value of a property with the given name or return default value.
Arguments:
property_name
- String that represents property name.default
- Default value return if there is no property.
Returns:
Any object value that property under property_name
has or default value otherwise.
Raises:
InvalidContextError
- Ifedge
orvertex
is out of context.UnableToAllocateError
- If unable to allocate amgp.Value
.DeletedObjectError
- If theobject
has been deleted.
Examples:
vertex.properties.get(property_name)
edge.properties.get(property_name)
set()
def set(property_name: str, value: object) -> None
Set the value of the property. When the value is None
, then the
property is removed.
Arguments:
property_name
- String that represents property name.value
- Object that represents value to be set.
Raises:
UnableToAllocateError
- If unable to allocate memory for storing the property.ImmutableObjectError
- If the object is immutable.DeletedObjectError
- If the object has been deleted.SerializationError
- If the object has been modified by another transaction.ValueConversionError
- Ifvalue
is vertex, edge or path.
Examples:
vertex.properties.set(property_name, value)
edge.properties.set(property_name, value)
set_properties()
def set_properties(props: dict) -> None
Updates the properties with the items from the given dictionary. For each item, the key-value pair becomes the property name and value.
Arguments:
props
- A dictionary of property names and values.
Raises:
UnableToAllocateError
- If unable to allocate memory for storing the property.ImmutableObjectError
- If the object is immutable.DeletedObjectError
- If the object has been deleted.SerializationError
- If the object has been modified by another transaction.
Examples:
vertex.properties.set_properties(props)
edge.properties.set_properties(props)
items()
def items() -> typing.Iterable[Property]
Iterate over the properties. Doesn’t return a dynamic view of the properties but copies the current properties.
Returns:
Iterable Property
of names and values.
Raises:
InvalidContextError
- If edge or vertex is out of context.UnableToAllocateError
- If unable to allocate an iterator.DeletedObjectError
- If the object has been deleted.
Examples:
items = vertex.properties.items()
for it in items:
name = it.name
value = it.value
items = edge.properties.items()
for it in items:
name = it.name
value = it.value
keys()
def keys() -> typing.Iterable[str]
Iterate over property names. Doesn’t return a dynamic view of the property names but copies the name of the current properties.
Returns:
Iterable list of strings that represent names/keys of properties.
Raises:
InvalidContextError
- If edge or vertex is out of context.UnableToAllocateError
- If unable to allocate an iterator.DeletedObjectError
- If the object has been deleted.
Examples:
graph.vertex.properties.keys()
graph.edge.properties.keys()
values()
def values() -> typing.Iterable[object]
Iterate over property values. Doesn’t return a dynamic view of the property values but copies the value of the current properties.
Returns:
Iterable list of property values.
Raises:
InvalidContextError
- If edge or vertex is out of context.UnableToAllocateError
- If unable to allocate an iterator.DeletedObjectError
- If the object has been deleted.
Examples:
vertex.properties.values()
edge.properties.values()
__len__
def __len__() -> int
Get the number of properties.
Returns:
A number of properties on vertex or edge.
Raises:
InvalidContextError
- If edge or vertex is out of context.UnableToAllocateError
- If unable to allocate an iterator.DeletedObjectError
- If the object has been deleted.
Examples:
len(vertex.properties)
len(edge.properties)
__iter__
def __iter__() -> typing.Iterable[str]
Iterate over property names.
Returns:
Iterable list of strings that represent names of properties.
Raises:
InvalidContextError
- If edge or vertex is out of context.UnableToAllocateError
- If unable to allocate an iterator.DeletedObjectError
- If the object has been deleted.
Examples:
iter(vertex.properties)
iter(edge.properties)
__getitem__
def __getitem__(property_name: str) -> object
Get the value of a property with the given name or raise KeyError.
Arguments:
property_name
- String that represents property name.
Returns:
Any value that property under property_name have.
Raises:
InvalidContextError
- If edge or vertex is out of context.UnableToAllocateError
- If unable to allocate a mgp.Value.DeletedObjectError
- If the object has been deleted.
Examples:
vertex.properties[property_name]
edge.properties[property_name]
__setitem__
def __setitem__(property_name: str, value: object) -> None
Set the value of the property. When the value is None
, then the
property is removed.
Arguments:
property_name
- String that represents property name.value
- Object that represents value to be set.
Raises:
UnableToAllocateError
- If unable to allocate memory for storing the property.ImmutableObjectError
- If the object is immutable.DeletedObjectError
- If the object has been deleted.SerializationError
- If the object has been modified by another transaction.ValueConversionError
- Ifvalue
is vertex, edge or path.
Examples:
vertex.properties[property_name] = value
edge.properties[property_name] = value
__contains__
def __contains__(property_name: str) -> bool
Check if there is a property with the given name.
Arguments:
property_name
- String that represents property name
Returns:
Bool value that depends if there is with a given name.
Raises:
InvalidContextError
- If edge or vertex is out of context.UnableToAllocateError
- If unable to allocate a mgp.Value.DeletedObjectError
- If the object has been deleted.
Examples:
if property_name in vertex.properties:
if property_name in edge.properties:
EdgeType Objects
class EdgeType()
Type of an Edge.
name
@property
def name() -> str
Get the name of EdgeType.
Returns:
A string that represents the name of EdgeType.
Example:
edge.type.name
Edge Objects
class Edge()
Edge in the graph database.
Access to an Edge is only valid during a single execution of a procedure in a query. You should not globally store an instance of an Edge. Using an invalid Edge instance will raise InvalidContextError.
is_valid()
def is_valid() -> bool
Check if edge
is in a valid context and may be used.
Returns:
A bool
value depends on if the edge
is in a valid context.
Examples:
edge.is_valid()
underlying_graph_is_mutable()
def underlying_graph_is_mutable() -> bool
Check if the graph
can be modified.
Returns:
A bool
value depends on if the graph
is mutable.
Examples:
edge.underlying_graph_is_mutable()
id
@property
def id() -> EdgeId
Get the ID of the edge.
Returns:
EdgeId
represents ID of the edge.
Raises:
InvalidContextError
- If edge is out of context.
Examples:
edge.id
type
@property
def type() -> EdgeType
Get the type of edge.
Returns:
EdgeType
describing the type of edge.
Raises:
InvalidContextError
- If edge is out of context.
Examples:
edge.type
from_vertex()
@property
def from_vertex() -> "Vertex"
Get the source vertex.
Returns:
Vertex
from where the edge is directed.
Raises:
InvalidContextError
- If edge is out of context.
Examples:
edge.from_vertex
to_vertex()
@property
def to_vertex() -> "Vertex"
Get the destination vertex.
Returns:
Vertex
to where the edge is directed.
Raises:
InvalidContextError
- If edge is out of context.
Examples:
edge.to_vertex
properties
@property
def properties() -> Properties
Get the properties of the edge.
Returns:
All Properties
of edge.
Raises:
InvalidContextError
- If edge is out of context.
Examples:
edge.properties
__eq__
def __eq__(other) -> bool
Raise InvalidContextError.
Vertex Objects
class Vertex()
Vertex in the graph database.
Access to a Vertex is only valid during a single execution of a procedure in a query. You should not globally store an instance of a Vertex. Using an invalid Vertex instance will raise InvalidContextError.
is_valid()
def is_valid() -> bool
Checks if Vertex
is in valid context and may be used.
Returns:
A bool
value depends on if the Vertex
is in a valid context.
Examples:
vertex.is_valid()
underlying_graph_is_mutable()
def underlying_graph_is_mutable() -> bool
Check if the graph
is mutable.
Returns:
A bool
value depends on if the graph
is mutable.
Examples:
vertex.underlying_graph_is_mutable()
id
@property
def id() -> VertexId
Get the ID of the Vertex.
Returns:
VertexId
represents ID of the vertex.
Raises:
InvalidContextError
- If vertex is out of context.
Examples:
vertex.id
labels
@property
def labels() -> typing.Tuple[Label]
Get the labels of the vertex.
Returns:
A tuple of Label
representing vertex Labels
Raises:
InvalidContextError
- If vertex is out of context.OutOfRangeError
- If some of the labels are removed while collecting the labels.DeletedObjectError
- IfVertex
has been deleted.
Examples:
vertex.labels
add_label()
def add_label(label: str) -> None
Add the label to the vertex.
Arguments:
label
- String label to be added.
Raises:
InvalidContextError
- IfVertex
is out of context.UnableToAllocateError
- If unable to allocate memory for storing the label.ImmutableObjectError
- IfVertex
is immutable.DeletedObjectError
- IfVertex
has been deleted.SerializationError
- IfVertex
has been modified by another transaction.
Examples:
vertex.add_label(label)
remove_label()
def remove_label(label: str) -> None
Remove the label from the vertex.
Arguments:
label
- String label to be deleted
Raises:
InvalidContextError
- IfVertex
is out of context.ImmutableObjectError
- IfVertex
is immutable.DeletedObjectError
- IfVertex
has been deleted.SerializationError
- IfVertex
has been modified by another transaction.
Examples:
vertex.remove_label(label)
properties
@property
def properties() -> Properties
Get the properties of the vertex.
Returns:
Properties
on a current vertex.
Raises:
InvalidContextError
- IfVertex
is out of context.
Examples:
vertex.properties
in_edges
@property
def in_edges() -> typing.Iterable[Edge]
Iterate over inbound edges of the vertex. When the first parameter to a procedure is a projected graph, iterating will start over the inbound edges of the given vertex in the projected graph. Doesn’t return a dynamic view of the edges but copies the current inbound edges.
Returns:
Iterable list of Edge
objects that are directed in towards the current vertex.
Raises:
InvalidContextError
- IfVertex
is out of context.UnableToAllocateError
- If unable to allocate an iterator.DeletedObjectError
- IfVertex
has been deleted.
Examples:
for edge in vertex.in_edges:
out_edges
@property
def out_edges() -> typing.Iterable[Edge]
Iterate over outbound edges of the vertex. When the first parameter to a procedure is a projected graph, iterating will start over the outbound edges of the given vertex in the projected graph.
Doesn’t return a dynamic view of the edges but copies the current outbound edges.
Returns:
Iterable list of Edge
objects that are directed out of the current vertex.
Raises:
InvalidContextError
- IfVertex
is out of context.UnableToAllocateError
- If unable to allocate an iterator.DeletedObjectError
- IfVertex
has been deleted.
Examples:
for edge in vertex.out_edges:
__eq__
def __eq__(other) -> bool
Raise InvalidContextError
Path Objects
class Path()
Path containing Vertex and Edge instances.
__init__
def __init__(starting_vertex_or_path: typing.Union[_mgp.Path, Vertex])
Initialize with a starting Vertex.
Raises:
InvalidContextError
- If passed in Vertex is invalid.UnableToAllocateError
- If cannot allocate a path.
is_valid()
def is_valid() -> bool
Check if Path
is in valid context and may be used.
Returns:
A bool
value depends on if the Path
is in a valid context.
Examples:
path.is_valid()
expand()
def expand(edge: Edge)
Append an edge continuing from the last vertex on the path.
The last vertex on the path will become the other endpoint of the given edge, as continued from the current last vertex.
Arguments:
edge
-Edge
that is added to the path
Raises:
InvalidContextError
- If using an invalidPath
instance or if passed inEdge
is invalid.LogicErrorError
- If the current last vertex in the path is not part of the given edge.UnableToAllocateError
- If unable to allocate memory for path extension.
Examples:
path.expand(edge)
pop()
def pop()
Remove the last node and the last relationship from the path.
Raises:
InvalidContextError
- If using an invalidPath
instance.OutOfRangeError
- If the path contains no relationships.
Examples:
path.pop()
vertices
@property
def vertices() -> typing.Tuple[Vertex, ...]
Vertices are ordered from the start to the end of the path.
Returns:
A tuple of Vertex
objects order from start to end of the path.
Raises:
InvalidContextError
- If using an invalid Path instance.
Examples:
path.vertices
edges
@property
def edges() -> typing.Tuple[Edge, ...]
Edges are ordered from the start to the end of the path.
Returns:
A tuple of Edge
objects order from start to end of the path
Raises:
InvalidContextError
- If using an invalidPath
instance.
Examples:
path.edges
Record Objects
class Record()
Represents a record of resulting field values.
__init__
def __init__(**kwargs)
Initialize with name=value fields in kwargs.
Vertices Objects
class Vertices()
Iterable over vertices in a graph.
is_valid()
def is_valid() -> bool
Check if Vertices
is in valid context and may be used.
Returns:
A bool
value depends on if the Vertices
is in valid context.
Examples:
vertices.is_valid()
__iter__
def __iter__() -> typing.Iterable[Vertex]
Iterate over vertices.
Returns:
Iterable list of Vertex
objects.
Raises:
InvalidContextError
- If context is invalid.UnableToAllocateError
- If unable to allocate an iterator or a vertex.
Examples:
for vertex in graph.vertices
__contains__
def __contains__(vertex)
Check if Vertices contain the given vertex.
Arguments:
vertex
-Vertex
to be checked if it is a part of graphVertices
.
Returns:
Bool value depends if there is Vertex
in graph Vertices
.
Raises:
UnableToAllocateError
- If unable to allocate the vertex.
Examples:
if vertex in graph.vertices:
__len__
def __len__()
Get the number of vertices.
Returns:
A number of vertices in the graph.
Raises:
InvalidContextError
- If context is invalid.UnableToAllocateError
- If unable to allocate an iterator or a vertex.
Examples:
len(graph.vertices)
Graph Objects
class Graph()
State of the graph database in current ProcCtx.
is_valid()
def is_valid() -> bool
Check if graph
is in a valid context and may be used.
Returns:
A bool
value depends on if the graph
is in a valid context.
Examples:
graph.is_valid()
get_vertex_by_id()
def get_vertex_by_id(vertex_id: VertexId) -> Vertex
Return the Vertex corresponding to the given vertex_id from the graph. When the first parameter to a procedure is a projected graph, the vertex must also exist in the projected graph.
Access to a Vertex is only valid during a single execution of a procedure in a query. You should not globally store the returned Vertex.
Arguments:
vertex_id
- Memgraph Vertex ID
Returns:
Vertex
corresponding to vertex_id
Raises:
IndexError
- If unable to find the given vertex_id.InvalidContextError
- If context is invalid.
Examples:
graph.get_vertex_by_id(vertex_id)
vertices
@property
def vertices() -> Vertices
Get all vertices in the graph.
Access to a Vertex is only valid during a single execution of a procedure in a query. You should not globally store the returned Vertex instances.
Returns:
Vertices
that contained in the graph.
Raises:
InvalidContextError
- If context is invalid.
Examples:
Iteration over all graph Vertices
.
graph = context.graph
for vertex in graph.vertices:
is_mutable()
def is_mutable() -> bool
Check if the graph is mutable. Thus it can be used to modify vertices and edges.
Returns:
A bool
value that depends if the graph is mutable or not.
Examples:
graph.is_mutable()
create_vertex()
def create_vertex() -> Vertex
Create an empty vertex. When the first parameter to a procedure is a projected graph, the vertex is also added to the projected graph view.
Returns:
Created Vertex
.
Raises:
ImmutableObjectError
- Ifgraph
is immutable.UnableToAllocateError
- If unable to allocate a vertex.
Examples
vertex = graph.create_vertex()
delete_vertex()
def delete_vertex(vertex: Vertex) -> None
Delete a vertex if there are no edges. When the first parameter to a procedure is a projected graph, the vertex must also exist in the projected graph.
Arguments:
vertex
-Vertex
to be deleted
Raises:
ImmutableObjectError
- Ifgraph
is immutable.LogicErrorError
- Ifvertex
has edges.SerializationError
- Ifvertex
has been modified by another transaction.
Examples:
graph.delete_vertex(vertex)
detach_delete_vertex()
def detach_delete_vertex(vertex: Vertex) -> None
Delete a vertex and all of its edges. When the first parameter to a procedure is a projected graph, such an operation is not possible.
Arguments:
vertex
-Vertex
to be deleted with all of its edges
Raises:
ImmutableObjectError
- Ifgraph
is immutable.SerializationError
- Ifvertex
has been modified by another transaction.
Examples:
graph.detach_delete_vertex(vertex)
create_edge()
def create_edge(from_vertex: Vertex, to_vertex: Vertex,
edge_type: EdgeType) -> Edge
Create an edge. When the first parameter is a projected graph, it will create a new directed edge with a specified label only if both vertices are a part of the projected graph.
Returns:
Created Edge
.
Arguments:
from_vertex
-Vertex
from where edge is directed.to_vertex
-Vertex
to where edge is directed.edge_type
-EdgeType
defines the type of edge.
Raises:
ImmutableObjectError
- Ifgraph
is immutable.UnableToAllocateError
- If unable to allocate an edge.DeletedObjectError
- Iffrom_vertex
orto_vertex
has been deleted.SerializationError
- Iffrom_vertex
orto_vertex
has been modified by another transaction.
Examples:
edge = graph.create_edge(from_vertex, vertex, edge_type)
delete_edge()
def delete_edge(edge: Edge) -> None
Delete an edge. When the first parameter to a procedure is a projected graph, the edge must also exist in the projected graph.
Arguments:
edge
-Edge
to be deleted
Raises:
- ImmutableObjectError: If
graph
is immutable. - Raise SerializationError: If
edge
, its source or destination vertex has been modified by another transaction.
Examples
graph.delete_edge(edge)
AbortError Objects
class AbortError(Exception)
Signals that the procedure was asked to abort its execution.
ProcCtx Objects
class ProcCtx()
Context of a procedure being executed.
Access to a ProcCtx is only valid during a single execution of a procedure in a query. You should not globally store a ProcCtx instance.
graph
@property
def graph() -> Graph
Access to Graph
object.
Returns:
Graph object.
Raises:
InvalidContextError
- If context is invalid.
Examples:
context.graph
Logger Objects
class Logger()
Class for logging.
info()
def info(out: str) -> None
Logs a message out
on INFO
log level.
Arguments:
out
-str
to be logged
Examples
logger.info("message")
debug()
def debug(out: str) -> None
Logs a message out
on DEBUG
log level.
Arguments:
out
-str
to be logged
Examples
logger.debug("message")
error()
def error(out: str) -> None
Logs a message out
on ERROR
log level.
Arguments:
out
-str
to be logged
Examples
logger.error("message")
trace()
def trace(out: str) -> None
Logs a message out
on TRACE
log level.
Arguments:
out
-str
to be logged
Examples
logger.trace("message")
warning()
def warning(out: str) -> None
Logs a message out
on WARNING
log level.
Arguments:
out
-str
to be logged
Examples
logger.warning("message")
critical()
def critical(out: str) -> None
Logs a message out
on CRITICAL
log level.
Arguments:
out
-str
to be logged
Examples
logger.critical("message")
UnsupportedTypingError Objects
class UnsupportedTypingError(Exception)
Signals a typing annotation is not supported as a _mgp.CypherType.
Deprecated Objects
class Deprecated()
Annotate a resulting Record's field as deprecated.
read_proc()
def read_proc(func: typing.Callable[..., Record])
Register func
as a read-only procedure of the current module.
The decorator read_proc
is meant to be used to register module procedures.
The registered func
needs to be a callable which optionally takes
ProcCtx
as its first argument. Other arguments of func
will be bound to
values passed in the cypherQuery. The full signature of func
needs to be
annotated with types. The return type must be Record(field_name=type, ...)
and the procedure must produce either a complete Record or None. To mark a
field as deprecated, use Record(field_name=Deprecated(type), ...)
.
Multiple records can be produced by returning an iterable of them.
Registering generator functions is currently not supported.
write_proc()
def write_proc(func: typing.Callable[..., Record])
Register func
as a writeable procedure of the current module.
The decorator write_proc
is meant to be used to register module
procedures. The registered func
needs to be a callable which optionally
takes ProcCtx
as the first argument. Other arguments of func
will be
bound to values passed in the cypherQuery. The full signature of func
needs to be annotated with types. The return type must be
Record(field_name=type, ...)
and the procedure must produce either a
complete Record or None. To mark a field as deprecated, use
Record(field_name=Deprecated(type), ...)
. Multiple records can be produced
by returning an iterable of them. Registering generator functions is
currently not supported.
InvalidMessageError Objects
class InvalidMessageError(Exception)
Signals using a message instance outside of the registered transformation.
Message Objects
class Message()
Represents a message from a stream.
is_valid()
def is_valid() -> bool
Return True if self
is in valid context and may be used.
source_type()
def source_type() -> str
Supported in all stream sources
Raise InvalidArgumentError if the message is from an unsupported stream source.
payload()
def payload() -> bytes
Supported stream sources:
- Kafka
- Pulsar
Raise InvalidArgumentError if the message is from an unsupported stream source.
topic_name()
def topic_name() -> str
Supported stream sources:
- Kafka
- Pulsar
Raise InvalidArgumentError if the message is from an unsupported stream source.
key()
def key() -> bytes
Supported stream sources:
- Kafka
Raise InvalidArgumentError if the message is from an unsupported stream source.
timestamp()
def timestamp() -> int
Supported stream sources:
- Kafka
Raise InvalidArgumentError if the message is from an unsupported stream source.
offset()
def offset() -> int
Supported stream sources:
- Kafka
Raise InvalidArgumentError if the message is from an unsupported stream source.
InvalidMessagesError Objects
class InvalidMessagesError(Exception)
Signals using a messages instance outside of the registered transformation.
Messages Objects
class Messages()
Represents a list of messages from a stream.
is_valid()
def is_valid() -> bool
Return True if self
is in valid context and may be used.
message_at()
def message_at(id: int) -> Message
Raise InvalidMessagesError if context is invalid.
total_messages()
def total_messages() -> int
Raise InvalidContextError if context is invalid.
TransCtx Objects
class TransCtx()
Context of a transformation being executed.
Access to a TransCtx is only valid during a single execution of a transformation. You should not globally store a TransCtx instance.
graph
@property
def graph() -> Graph
Raise InvalidContextError if context is invalid.
FuncCtx Objects
class FuncCtx()
Context of a function being executed.
Access to a FuncCtx is only valid during a single execution of a function in a query. You should not globally store a FuncCtx instance. The graph object within the FuncCtx is not mutable.
function()
def function(func: typing.Callable)
Register func
as a user-defined function in the current module.
The decorator function
is meant to be used to register module functions.
The registered func
needs to be a callable which optionally takes
FuncCtx
as its first argument. Other arguments of func
will be bound to
values passed in the Cypher query. Only the function arguments need to be
annotated with types. The return type doesn't need to be specified, but it
has to be supported by mgp.Any
. Registering generator functions is
currently not supported.
InvalidContextError Objects
class InvalidContextError(Exception)
Signals using a graph element instance outside of the registered procedure.
UnknownError Objects
class UnknownError(_mgp.UnknownError)
Signals unspecified failure.
UnableToAllocateError Objects
class UnableToAllocateError(_mgp.UnableToAllocateError)
Signals failed memory allocation.
InsufficientBufferError Objects
class InsufficientBufferError(_mgp.InsufficientBufferError)
Signals that some buffer is not big enough.
OutOfRangeError Objects
class OutOfRangeError(_mgp.OutOfRangeError)
Signals that an index-like parameter has a value that is outside its possible values.
LogicErrorError Objects
class LogicErrorError(_mgp.LogicErrorError)
Signals faulty logic within the program such as violating logical preconditions or class invariants and may be preventable.
DeletedObjectError Objects
class DeletedObjectError(_mgp.DeletedObjectError)
Signals accessing an already deleted object.
InvalidArgumentError Objects
class InvalidArgumentError(_mgp.InvalidArgumentError)
Signals that some of the arguments have invalid values.
KeyAlreadyExistsError Objects
class KeyAlreadyExistsError(_mgp.KeyAlreadyExistsError)
Signals that a key already exists in a container-like object.
ImmutableObjectError Objects
class ImmutableObjectError(_mgp.ImmutableObjectError)
Signals modification of an immutable object.
ValueConversionError Objects
class ValueConversionError(_mgp.ValueConversionError)
Signals that the conversion failed between python and cypher values.
SerializationError Objects
class SerializationError(_mgp.SerializationError)
Signals serialization error caused by concurrent modifications from different transactions.
AuthorizationError Objects
class AuthorizationError(_mgp.AuthorizationError)
Signals that the user doesn't have sufficient permissions to perform procedure call.