<commit_before>from __future__ import division, with_statement
from BinPy.config import *
from BinPy.connectors.linker import *


"""
Contains
========

* Connector
* Bus

"""


class Connector(object):

    """
    This class is the primary medium for data transfer. Objects of this
    class can be connected to any digital object.

    Example
    =======

    >>> from BinPy import *
    >>> conn = Connector(1)  #Initializing connector with initial state = 1
    >>> conn.get_logic()
    1
    >>> gate = OR(0, 1)
    >>> conn.tap(gate, 'output')  #Tapping the connector

    METHODS
    =======

    set_logic         :      To set the logic state of the connector.
    get_logic         :      To get the logic state of the connector.
    set_voltage       :      To set the floating point volatage of the connector.
    get_voltage       :      To get the floating point volatage of the connector.
    is_input_of       :      To check whether this connector is the input of the given element.
    is_output_of      :      To check whether this connector is the output of the given element.
    tap               :      Tap this connector as input / output of another element
    untap             :      Untap this connector from another element



    PROPERTIES
    ==========

    index             :      The index of the connector instance registered with the BinPyIndexer
    name              :      Get the name of the connector
    state             :      [ Currently read / write supported. In future versions this will be read-only ]
                             To get ( and set ) the logic state of the connector.
    enabled           :      Return True if the connector is enabled.
    disabled          :      Return True if the connector is not enabled.

    """

    def __init__(self, state=None, voltage=None, analog=False, name=""):
        self.__dict__["_enable"] = True  # To bypass the __setattr__

        self.connections = {"output": [], "input": []}
        # To store the all the taps onto this connection
        self.set_logic(state)
        self.oldstate = None

        self.analog = analog
        # voltage for analog components
        if voltage is not None:
            self.set_voltage(voltage)
            self.analog = True

        self.oldvoltage = 0.0
        self._name = name
        self.name_set = (name != "")
        self._index = BinPyIndexer.index(self)

    @property
    def index(self):
        return self._index

    def tap(self, element, mode):
        # Can't serve output for multiple devices
        if mode == "output":
            self.connections["output"] = []

        if element not in self.connections[mode]:
            self.connections[mode].append(
                element)  # Add an element to the connections list

    def untap(self, element, mode):
        if element in self.connections[mode]:
            self.connections[mode].remove(
                element)  # Delete an element from the connections list
        else:
            raise Exception(
                "ERROR:Connector is not the %s of the passed element" %
                mode)

    def set_logic(self, val):

        if not self._enable:
            return

        if type(val) in [int, type(None), bool]:
            self.state = val if val is not None else None
            self.voltage = constants.LOGIC_HIGH_VOLT if self.state == constants.LOGIC_HIGH_STATE else constants.LOGIC_LOW_VOLT
            self.trigger()

        elif isinstance(val, Connector):
            self.state = val.get_logic()

        else:
            raise Exception("ERROR: Invalid input type")

        self.trigger()
        # All set functions ultimately call this. So one trigger here should
        # suffice.

    def get_logic(self):
        return self.state

    def set_voltage(self, val):
        if not self._enable:
            return

        if type(val) in [float, int]:
            self.voltage = float(val)
        elif isinstance(val, Connector):
            self.voltage = val.get_voltage()

        else:
            raise Exception("ERROR: Voltage must be a float or int")

        self.state = constants.LOGIC_HIGH_STATE if self.voltage > constants.LOGIC_THRESHOLD_VOLT else constants.LOGIC_LOW_STATE

    def get_voltage(self):
        return self.voltage

    def is_input_of(self, element):
        return element in self.connections["input"]

    def is_output_of(self, element):
        return element in self.connections["output"]

    # This function is called when the value of the connection changes
    def trigger(self):
        for i in self.connections["input"]:
            i.trigger()

    def __call__(self):
        return self.state

    def set_name(self, name):
        if (self.name is None) and (not self.name_set):
            for k, v in list(globals().iteritems()):
                if (id(v) == id(self)) and (k != "self"):
                    self.name = k
            self.name_set = True

    @property<commit_msg>Fixed connector naming<commit_after>from __future__ import division, with_statement
from BinPy.config import *
from BinPy.connectors.linker import *


"""
Contains
========

* Connector
* Bus

"""


class Connector(object):

    """
    This class is the primary medium for data transfer. Objects of this
    class can be connected to any digital object.

    Example
    =======

    >>> from BinPy import *
    >>> conn = Connector(1)  #Initializing connector with initial state = 1
    >>> conn.get_logic()
    1
    >>> gate = OR(0, 1)
    >>> conn.tap(gate, 'output')  #Tapping the connector

    METHODS
    =======

    set_logic         :      To set the logic state of the connector.
    get_logic         :      To get the logic state of the connector.
    set_voltage       :      To set the floating point volatage of the connector.
    get_voltage       :      To get the floating point volatage of the connector.
    is_input_of       :      To check whether this connector is the input of the given element.
    is_output_of      :      To check whether this connector is the output of the given element.
    tap               :      Tap this connector as input / output of another element
    untap             :      Untap this connector from another element



    PROPERTIES
    ==========

    index             :      The index of the connector instance registered with the BinPyIndexer
    name              :      Get the name of the connector
    state             :      [ Currently read / write supported. In future versions this will be read-only ]
                             To get ( and set ) the logic state of the connector.
    enabled           :      Return True if the connector is enabled.
    disabled          :      Return True if the connector is not enabled.

    """

    def __init__(self, state=None, voltage=None, analog=False, name=""):
        self.__dict__["_enable"] = True  # To bypass the __setattr__

        self.connections = {"output": [], "input": []}
        # To store the all the taps onto this connection
        self.set_logic(state)
        self.oldstate = None

        self.analog = analog
        # voltage for analog components
        if voltage is not None:
            self.set_voltage(voltage)
            self.analog = True

        self.oldvoltage = 0.0
        self._name = name
        self.name_set = (name != "")
        self._index = BinPyIndexer.index(self)

    @property
    def index(self):
        return self._index

    def tap(self, element, mode):
        # Can't serve output for multiple devices
        if mode == "output":
            self.connections["output"] = []

        if element not in self.connections[mode]:
            self.connections[mode].append(
                element)  # Add an element to the connections list

    def untap(self, element, mode):
        if element in self.connections[mode]:
            self.connections[mode].remove(
                element)  # Delete an element from the connections list
        else:
            raise Exception(
                "ERROR:Connector is not the %s of the passed element" %
                mode)

    def set_logic(self, val):

        if not self._enable:
            return

        if type(val) in [int, type(None), bool]:
            self.state = val if val is not None else None
            self.voltage = constants.LOGIC_HIGH_VOLT if self.state == constants.LOGIC_HIGH_STATE else constants.LOGIC_LOW_VOLT
            self.trigger()

        elif isinstance(val, Connector):
            self.state = val.get_logic()

        else:
            raise Exception("ERROR: Invalid input type")

        self.trigger()
        # All set functions ultimately call this. So one trigger here should
        # suffice.

    def get_logic(self):
        return self.state

    def set_voltage(self, val):
        if not self._enable:
            return

        if type(val) in [float, int]:
            self.voltage = float(val)
        elif isinstance(val, Connector):
            self.voltage = val.get_voltage()

        else:
            raise Exception("ERROR: Voltage must be a float or int")

        self.state = constants.LOGIC_HIGH_STATE if self.voltage > constants.LOGIC_THRESHOLD_VOLT else constants.LOGIC_LOW_STATE

    def get_voltage(self):
        return self.voltage

    def is_input_of(self, element):
        return element in self.connections["input"]

    def is_output_of(self, element):
        return element in self.connections["output"]

    # This function is called when the value of the connection changes
    def trigger(self):
        for i in self.connections["input"]:
            i.trigger()

    def __call__(self):
        return self.state

    def set_name(self, name):
        if (self.name is None) and (not self.name_set):
            for k, v in list(globals().iteritems()):
                if (id(v) == id(self)) and (k != "self"):
                    self.name = k
            self.name_set = True

        elif self.name is not None:
            self._name = name

    @property
{'fraction_non_alphanumeric': 0.08454058344909704, 'fraction_numerical': 0.0013891645167692002, 'mean_word_length': 1.9557184750733139, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 35, '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': '5368864', 'n_tokens_mistral': 2797, 'n_tokens_neox': 2593, 'n_words': 948}