5. py2neo.cypher – Cypher Utilities

This module provides a set of Cypher language utility functions that can be useful when building Cypher statements and expressions.

py2neo.cypher.cypher_escape(identifier, **kwargs)[source]

Return a Cypher identifier, with escaping if required.

Simple Cypher identifiers, which just contain alphanumerics and underscores, can be represented as-is in expressions. Any which contain more esoteric characters, such as spaces or punctuation, must be escaped in backticks. Backticks themselves are escaped by doubling.

>>> cypher_escape("simple_identifier")
'simple_identifier'
>>> cypher_escape("identifier with spaces")
'`identifier with spaces`'
>>> cypher_escape("identifier with `backticks`")
'`identifier with ``backticks```'

Identifiers are used in Cypher to denote named values, labels, relationship types and property keys. This function will typically be used to construct dynamic Cypher queries in places where parameters cannot be used.

>>> "MATCH (a:{label}) RETURN id(a)".format(label=cypher_escape("Employee of the Month"))
'MATCH (a:`Employee of the Month`) RETURN id(a)'
Parameters:identifier – any non-empty string
py2neo.cypher.cypher_repr(value, **kwargs)[source]

Return the Cypher representation of a value.

This function attempts to convert the supplied value into a Cypher literal form, as used in expressions.

py2neo.cypher.cypher_str(value, **kwargs)[source]

Convert a Cypher value to a Python Unicode string.

This function converts the supplied value into a string form, as used for human-readable output. This is generally identical to cypher_repr() except for with string values, which are returned as-is, instead of being enclosed in quotes with certain characters escaped.