<commit_before>from collections import defaultdict

from .users import Users

__all__ = [
    "Port",
    "Node",
    "Link",
    "Graph",
    "GraphModel",
    "NodeExists",
    "NoSuchNode",
    "NoSuchPort",
    "LinkExists",
    "NoSuchLink",
    "Merge",
    "Cancel",
    "Confirm",
    ]

class Port(object):
    def __init__(self, name, title):
        self._name = name
        self._title = title

    @property
    def name(self): 
        return self._name

    @property
    def title(self):
        return self._title

    def serialize(self):
        return dict(
            name=self._name,
            title=self._title,
            )

class State(object):
    def __init__(self):
        self._data = {}

    def serialize(self):
        return self._data

    def deserialize(self, data):
        self._data = data

class Node(object):
    state_factory = State
    port_factory = Port

    def __init__(self, id):
        self._id = id
        self._state = self.state_factory()
        self._ports = []
        self._ports_by_name = {}

    @property
    def id(self):
        return self._id

    def has_port(self, name):
        return name in self._ports_by_name

    def get_port(self, name):
        return self._ports_by_name.get(name)

    def add_port(self, port, index=None):
        if index is None:
            index = len(self._ports)
        self._ports.insert(index, port)
        self._ports_by_name[port.name] = port

    def remove_port(self, name):
        port = self._ports_by_name[name]
        self._ports.remove(port)

    def get_ports(self):
        return [port.serialize() for port in self._ports]

    def get_state(self):
        return self._state.serialize()

    def set_state(self, state):
        return self._state.deserialize(state)        

class Link(object):
    def __init__(self, start_id, start_name, end_id, end_name):
        self._start_id = start_id
        self._start_name = start_name
        self._end_id = end_id
        self._end_name = end_name

    @property
    def start_id(self): 
        return self._start_id

    @property
    def start_name(self): 
        return self._start_name

    @property
    def end_id(self): 
        return self._end_id

    @property
    def end_name(self): 
        return self._end_name

# ____________________________________________________________________________ #

class ResultException(Exception): pass
class NodeExists(ResultException):
    def __init__(self, id):
        self.id = id
class NoSuchNode(ResultException):
    def __init__(self, id):
        self.id = id
class NoSuchPort(ResultException):
    def __init__(self, id, name):
        self.id = id
        self.name = name
class LinkExists(ResultException):
    def __init__(self, start_id, start_name, end_id, end_name):
        self.start_id = start_id
        self.start_name = start_name
        self.end_id = end_id
        self.end_name = end_name
class NoSuchLink(ResultException):
    def __init__(self, start_id, start_name, end_id, end_name):
        self.start_id = start_id
        self.start_name = start_name
        self.end_id = end_id
        self.end_name = end_name

class ResultResolution(Exception): pass
class Merge(ResultResolution): pass
class Cancel(ResultException): pass
class Confirm(ResultException): pass

class Graph(object):
    node_factory = Node
    link_factory = Link

    def __init__(self):
        super().__init__()
        self._rev = 0
        self._nodes_by_name = {}
        self._links_by_key = {}
        self._links_by_start_id = defaultdict(dict)
        self._links_by_end_id = defaultdict(dict)

    @property
    def rev(self):
        old = self._rev
        self._rev += 1
        return old

    def has_node(self, id):
        return id in self._nodes_by_name

    def get_node(self, id):
        return self._nodes_by_name[id]

    def add_node(self, node):
        self._nodes_by_name[node.id] = node

    def remove_node(self, id):
        del self._nodes_by_name[id]

    def has_link(self, start_id, start_name, end_id, end_name):
        key = (start_id, start_name, end_id, end_name)
        return key in self._links_by_key

    def get_link(self, start_id, start_name, end_id, end_name):
        key = (start_id, start_name, end_id, end_name)
        return self._links_by_key[key]

    def add_link(self, link):
        key = (link.start_id, link.start_name, link.end_id, link.end_name)
        self._links_by_start_id[link.start_id][key] = link
        self._links_by_end_id[link.end_id][key] = link
        self._links_by_key[key] = link

    def remove_link(self, start_id, start_name, end_id, end_name):
        key = (start_id, start_name, end_id, end_name)
        del self._links_by_key[key]
        del self._links_by_start_id[start_id][key]
        del self._links_by_end_id[end_id][key]

    def find_links_startswith(self, start_id):
        yield from list(self._links_by_start_id[start_id].values())

    def find_links_endswith(self, end_id):
        yield from list(self._links_by_end_id[end_id].values())<commit_msg>some renamings<commit_after>from collections import defaultdict

from sentinels import Sentinel

from .users import Users

__all__ = [
    "Port",
    "Node",
    "Link",
    "Graph",
    "GraphModel",
    "NodeExists",
    "NoSuchNode",
    "NoSuchPort",
    "LinkExists",
    "NoSuchLink",
    "Merge",
    "Cancel",
    "Confirm",
    ]

class Port(object):
    def __init__(self, name, title):
        self._name = name
        self._title = title

    @property
    def name(self): 
        return self._name

    @property
    def title(self):
        return self._title

    def serialize(self):
        return dict(
            name=self._name,
            title=self._title,
            )

class State(object):
    def __init__(self):
        self._data = {}

    def serialize(self):
        return self._data

    def deserialize(self, data):
        self._data = data

class Node(object):
    state_factory = State
    port_factory = Port

    def __init__(self, id):
        self._id = id
        self._state = self.state_factory()
        self._ports = []
        self._ports_by_name = {}

    @property
    def id(self):
        return self._id

    def has_port(self, name):
        return name in self._ports_by_name

    def get_port(self, name):
        return self._ports_by_name.get(name)

    def add_port(self, port, index=None):
        if index is None:
            index = len(self._ports)
        self._ports.insert(index, port)
        self._ports_by_name[port.name] = port

    def remove_port(self, name):
        port = self._ports_by_name[name]
        self._ports.remove(port)

    def get_ports(self):
        return [port.serialize() for port in self._ports]

    def get_state(self):
        return self._state.serialize()

    def set_state(self, state):
        return self._state.deserialize(state)        

class Link(object):
    def __init__(self, start_id, start_name, end_id, end_name):
        self._start_id = start_id
        self._start_name = start_name
        self._end_id = end_id
        self._end_name = end_name

    @property
    def start_id(self): 
        return self._start_id

    @property
    def start_name(self): 
        return self._start_name

    @property
    def end_id(self): 
        return self._end_id

    @property
    def end_name(self): 
        return self._end_name

# ____________________________________________________________________________ #

class ResultException(Exception): pass
class NodeExists(ResultException):
    def __init__(self, id):
        self.id = id
class NoSuchNode(ResultException):
    def __init__(self, id):
        self.id = id
class NoSuchPort(ResultException):
    def __init__(self, id, name):
        self.id = id
        self.name = name
class LinkExists(ResultException):
    def __init__(self, start_id, start_name, end_id, end_name):
        self.start_id = start_id
        self.start_name = start_name
        self.end_id = end_id
        self.end_name = end_name
class NoSuchLink(ResultException):
    def __init__(self, start_id, start_name, end_id, end_name):
        self.start_id = start_id
        self.start_name = start_name
        self.end_id = end_id
        self.end_name = end_name

class ResultResolution(Exception): pass
class Merge(ResultResolution): pass
class Cancel(ResultException): pass
class Confirm(ResultException): pass

class Graph(object):
    node_factory = Node
    link_factory = Link

    def __init__(self):
        super().__init__()
        self._rev = 0
        self._nodes_by_id = {}
        self._links_by_key = {}
        self._links_by_start_id = defaultdict(dict)
        self._links_by_end_id = defaultdict(dict)

    @property
    def rev(self):
        old = self._rev
        self._rev += 1
        return old

    def has_node(self, id):
        return id in self._nodes_by_id

    def get_node(self, id):
        return self._nodes_by_id[id]

    def add_node(self, node):
        self._nodes_by_id[node.id] = node

    def remove_node(self, id):
        del self._nodes_by_id[id]

    def has_link(self, start_id, start_name, end_id, end_name):
        key = (start_id, start_name, end_id, end_name)
        return key in self._links_by_key

    def get_link(self, start_id, start_name, end_id, end_name):
        key = (start_id, start_name, end_id, end_name)
        return self._links_by_key[key]

    def add_link(self, link):
        key = (link.start_id, link.start_name, link.end_id, link.end_name)
        self._links_by_start_id[link.start_id][key] = link
        self._links_by_end_id[link.end_id][key] = link
        self._links_by_key[key] = link

    def remove_link(self, start_id, start_name, end_id, end_name):
        key = (start_id, start_name, end_id, end_name)
        del self._links_by_key[key]
        del self._links_by_start_id[start_id][key]
        del self._links_by_end_id[end_id][key]

    def find_links_startswith(self, start_id):
        yield from list(self._links_by_start_id[start_id].values())

    def find_links_endswith(self, end_id):
        yield from list(self._links_by_end_id[end_id].values())
{'fraction_non_alphanumeric': 0.09796799842937076, 'fraction_numerical': 0.0003926573083341514, 'mean_word_length': 2.5911173775114555, 'pattern_counts': {'":': 0, '<': 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': '479499', 'n_tokens_mistral': 3322, 'n_tokens_neox': 3246, 'n_words': 718}