<commit_before>"""Incomplete support for dump/load w/PyYAML."""

import yaml

from py2neo_compat import Node, Relationship, to_dict
# from py2neo.cypher.core import Record

from . import node, rel

node_tag = u'!gryaml.node'
rel_tag = u'!gryaml.rel'


def node_representer(dumper, data):
    # type: (yaml.BaseDumper, Node) -> yaml.SequenceNode
    """Represent a Neo4j node as YAML sequence node."""
    yaml_data = [
        {'labels': list(data.labels)},
    ]

    properties = to_dict(data)
    if properties:
        yaml_data.append({'properties': properties})

    return dumper.represent_sequence(node_tag, yaml_data,
                                     flow_style=False)


yaml.add_representer(Node, node_representer)


def node_constructor(loader, yaml_node):
    # type: (yaml.BaseLoader, yaml.Node) -> Node
    """Construct a Neo4j node from a YAML sequence."""
    return node(*(loader.construct_sequence(yaml_node, deep=True)))


yaml.add_constructor(node_tag, node_constructor)


def rel_representer(dumper, data):
    # type: (yaml.BaseDumper, Relationship) -> yaml.SequenceNode
    """Represent a Neo4j relationship as a YAML sequence node."""
    yaml_data = [
        data.start_node,
        data.type,
        data.end_node,
    ]

    properties = to_dict(data)
    if properties:
        yaml_data.append({'properties': properties})

    return dumper.represent_sequence(rel_tag, yaml_data, flow_style=False)


yaml.add_representer(Relationship, rel_representer)


def rel_constructor(loader, yaml_node):
    # type: (yaml.BaseLoader, yaml.Node) -> Relationship
    """Construct as Neo4j relationship from a YAML sequence."""
    return rel(*loader.construct_sequence(yaml_node, deep=True))


yaml.add_constructor(rel_tag, rel_constructor)

# py2neo and pyyaml have bugs that make this not straightforward:
# * py2neo assumes that a Record will only be compared to an iterable
# in `Record`'s `__eq__` method; it explicitly converts itself and "other"
# with `tuple()`, which fails if it is compared with `None`.
# * pyyaml has a bug in `representer.py`, function `ignore_aliases`,
# where it checks: `if data in [None, ()]:`, which performs an '=='
# comparison, not an 'is' comparison, which is correct for `None`.
#
# def record_representer(dumper, data):
#     """Represent."""
#     print('record rep called')
#     return dumper.represent_sequence(u'tag:yaml.org,2002:seq',
#         ['one', 'two'])
#         #[data[0], data[2], data[1]])
#
# yaml.add_representer(Record, record_representer)
<commit_msg>Exclude labels from dump if empty<commit_after>"""Incomplete support for dump/load w/PyYAML."""

import yaml

from py2neo_compat import Node, Relationship, to_dict
# from py2neo.cypher.core import Record

from . import node, rel

node_tag = u'!gryaml.node'
rel_tag = u'!gryaml.rel'


def node_representer(dumper, data):
    # type: (yaml.BaseDumper, Node) -> yaml.SequenceNode
    """Represent a Neo4j node as YAML sequence node."""
    yaml_data = []

    if data.labels:
        yaml_data.append({'labels': list(data.labels)})

    properties = to_dict(data)
    if properties:
        yaml_data.append({'properties': properties})

    return dumper.represent_sequence(node_tag, yaml_data,
                                     flow_style=False)


yaml.add_representer(Node, node_representer)


def node_constructor(loader, yaml_node):
    # type: (yaml.BaseLoader, yaml.Node) -> Node
    """Construct a Neo4j node from a YAML sequence."""
    return node(*(loader.construct_sequence(yaml_node, deep=True)))


yaml.add_constructor(node_tag, node_constructor)


def rel_representer(dumper, data):
    # type: (yaml.BaseDumper, Relationship) -> yaml.SequenceNode
    """Represent a Neo4j relationship as a YAML sequence node."""
    yaml_data = [
        data.start_node,
        data.type,
        data.end_node,
    ]

    properties = to_dict(data)
    if properties:
        yaml_data.append({'properties': properties})

    return dumper.represent_sequence(rel_tag, yaml_data, flow_style=False)


yaml.add_representer(Relationship, rel_representer)


def rel_constructor(loader, yaml_node):
    # type: (yaml.BaseLoader, yaml.Node) -> Relationship
    """Construct as Neo4j relationship from a YAML sequence."""
    return rel(*loader.construct_sequence(yaml_node, deep=True))


yaml.add_constructor(rel_tag, rel_constructor)

# py2neo and pyyaml have bugs that make this not straightforward:
# * py2neo assumes that a Record will only be compared to an iterable
# in `Record`'s `__eq__` method; it explicitly converts itself and "other"
# with `tuple()`, which fails if it is compared with `None`.
# * pyyaml has a bug in `representer.py`, function `ignore_aliases`,
# where it checks: `if data in [None, ()]:`, which performs an '=='
# comparison, not an 'is' comparison, which is correct for `None`.
#
# def record_representer(dumper, data):
#     """Represent."""
#     print('record rep called')
#     return dumper.represent_sequence(u'tag:yaml.org,2002:seq',
#         ['one', 'two'])
#         #[data[0], data[2], data[1]])
#
# yaml.add_representer(Record, record_representer)

{'fraction_non_alphanumeric': 0.12214634146341463, 'fraction_numerical': 0.005853658536585366, 'mean_word_length': 3.9193857965451055, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4962974', 'n_tokens_mistral': 1690, 'n_tokens_neox': 1568, 'n_words': 433}