<commit_before>__author__ = 'Alex'


class JSONxVisitor(object):
    def visit(self, node):
        return node.accept(self)


class Node(object):
    def __eq__(self, other):
        return isinstance(other, Node)

    def __repr__(self):
        strings = [repr(item) for item in self.__dict__.itervalues()]
        return "{}({})".format(self.__class__.__name__, ', '.join(strings))

    def __str__(self):
        return self.__class__.__name__ + '()'

    def accept(self, visitor):
        pass


class NumberNode(Node):
    def __init__(self, value):
        self.value = value

    def accept(self, visitor):
        return self.value

    def __eq__(self, other):
        return super(NumberNode, self).__eq__(other) \
               and self.value == other.value


class StringNode(Node):
    def __init__(self, value):
        self.value = value

    def accept(self, visitor):
        return self.value

    def __eq__(self, other):
        return super(StringNode, self).__eq__(other) \
               and self.value == other.value


class TrueNode(Node):
    def accept(self, visitor):
        return True

    def __eq__(self, other):
        return isinstance(other, TrueNode)


class FalseNode(Node):
    def accept(self, visitor):
        return False

    def __eq__(self, other):
        return isinstance(other, FalseNode)


class NullNode(Node):
    def accept(self, visitor):
        return None

    def __eq__(self, other):
        return isinstance(other, NullNode)


class ReferenceNode(Node):
    def __init__(self, file_path, object_path):
        self.file = file_path
        self.path = object_path

    def accept(self, visitor):
        return {"$ref": {"file": self.file, "path": self.path}}

    def __eq__(self, other):
        return super(ReferenceNode, self).__eq__(other) \
               and (self.file, self.path) == (other.file, other.path)


class ArrayNode(Node):
    def __init__(self, nodes):
        self.children = nodes

    def accept(self, visitor):
        result = []
        for child in self.children:
            result += visitor.visit(child),
        return result

    def __eq__(self, other):
        return super(ArrayNode, self).__eq__(other) \
               and self.children == other.children


class ObjectNode(Node):
    def __init__(self, pairs):
        self.children = pairs

    def accept(self, visitor):
        result = {}
        for child in self.children:
            key, value = visitor.visit(child)
            result[key] = value
        return result

    def __eq__(self, other):
        return super(ObjectNode, self).__eq__(other) \
               and self.children == other.children


class PairNode(Node):
    def __init__(self, key, value):
        self.key = key
        self.value = value

    def accept(self, visitor):
        return visitor.visit(self.key), visitor.visit(self.value)

    def __eq__(self, other):
        return super(PairNode, self).__eq__(other) \
               and (self.key, self.value) == (other.key, other.value)<commit_msg>visitor refactoring<commit_after>__author__ = 'Alex'


# noinspection PyMethodMayBeStatic
class JSONxVisitor(object):
    def visit(self, node):
        method_name = 'visit_' + node.__class__.__name__
        method = getattr(self, method_name, self.visit_generic)
        return method(node)

    def visit_generic(self, node):
        print '[Warning]: Unknown node ' + node.__class__.__name__

    def visit_NumberNode(self, node):
        return node.value

    def visit_StringNode(self, node):
        return node.value

    def visit_TrueNode(self, node):
        return True

    def visit_FalseNode(self, node):
        return False

    def visit_NullNode(self, node):
        return None

    def visit_PairNode(self, node):
        return self.visit(node.key), self.visit(node.value)

    def visit_ReferenceNode(self, node):
        return {"$ref": {"file": node.file, "path": node.path}}

    def visit_ObjectNode(self, node):
        result = {}
        for child in node.children:
            key, value = self.visit(child)
            result[key] = value
        return result

    def visit_ArrayNode(self, node):
        result = [self.visit(child) for child in node.children]
        return result


class Node(object):
    def __eq__(self, other):
        return isinstance(other, Node)

    def __repr__(self):
        strings = [repr(item) for item in self.__dict__.itervalues()]
        return "{}({})".format(self.__class__.__name__, ', '.join(strings))

    def __str__(self):
        return self.__class__.__name__ + '()'


class NumberNode(Node):
    def __init__(self, value):
        self.value = value

    def __eq__(self, other):
        return super(NumberNode, self).__eq__(other) \
               and self.value == other.value


class StringNode(Node):
    def __init__(self, value):
        self.value = value

    def __eq__(self, other):
        return super(StringNode, self).__eq__(other) \
               and self.value == other.value


class TrueNode(Node):
    def __eq__(self, other):
        return isinstance(other, TrueNode)


class FalseNode(Node):
    def __eq__(self, other):
        return isinstance(other, FalseNode)


class NullNode(Node):
    def __eq__(self, other):
        return isinstance(other, NullNode)


class ReferenceNode(Node):
    def __init__(self, file_path, object_path):
        self.file = file_path
        self.path = object_path

    def __eq__(self, other):
        return super(ReferenceNode, self).__eq__(other) \
               and (self.file, self.path) == (other.file, other.path)


class ArrayNode(Node):
    def __init__(self, nodes):
        self.children = nodes

    def __eq__(self, other):
        return super(ArrayNode, self).__eq__(other) \
               and self.children == other.children


class ObjectNode(Node):
    def __init__(self, pairs):
        self.children = pairs

    def __eq__(self, other):
        return super(ObjectNode, self).__eq__(other) \
               and self.children == other.children


class PairNode(Node):
    def __init__(self, key, value):
        self.key = key
        self.value = value

    def __eq__(self, other):
        return super(PairNode, self).__eq__(other) \
               and (self.key, self.value) == (other.key, other.value)
{'fraction_non_alphanumeric': 0.11529933481152993, 'fraction_numerical': 0.0, 'mean_word_length': 2.5083333333333333, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '594830', 'n_tokens_mistral': 1880, 'n_tokens_neox': 1819, 'n_words': 478}