Пример #1
0
def cypher_escape(identifier, **kwargs):
    """ 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)'

    :param identifier: any non-empty string
    """
    if not isinstance(identifier, string_types):
        raise TypeError(type(identifier).__name__)
    encoder = CypherEncoder(**kwargs)
    return encoder.encode_key(identifier)
Пример #2
0
def cypher_repr(value, **kwargs):
    """ Return the Cypher representation of a value.

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

    """
    encoder = CypherEncoder(**kwargs)
    return encoder.encode_value(value)
Пример #3
0
 def __repr__(self):
     args = [repr(self.nodes[0]), repr(self.nodes[-1])]
     kwargs = OrderedDict()
     d = dict(self)
     for key in sorted(d):
         if CypherEncoder.is_safe_key(key):
             args.append("%s=%r" % (key, d[key]))
         else:
             kwargs[key] = d[key]
     if kwargs:
         args.append("**{%s}" % ", ".join("%r: %r" % (k, kwargs[k]) for k in kwargs))
     return "%s(%s)" % (self.__class__.__name__, ", ".join(args))
Пример #4
0
 def __repr__(self):
     args = list(map(repr, sorted(self.labels)))
     kwargs = OrderedDict()
     d = dict(self)
     for key in sorted(d):
         if CypherEncoder.is_safe_key(key):
             args.append("%s=%r" % (key, d[key]))
         else:
             kwargs[key] = d[key]
     if kwargs:
         args.append("**{%s}" % ", ".join("%r: %r" % (k, kwargs[k]) for k in kwargs))
     return "Node(%s)" % ", ".join(args)