Exemple #1
0
def compute_graph(edges, x, y, precision, tollerance):

    if booldebug: print(x, y)
    graph = Graph('image')
    q = Queue('nodes')
    p_node = Point(x, y)
    node = Node(p_node)
    graph.nodes.append(node)
    q.push(node)

    while not q.empty():
        node = q.top()
        q.pop()
        links = []

        p_node = node.get_point()
        x = p_node.get_x()
        y = p_node.get_y()
        cell = generate_cell(edges, p_node, precision)
        links = find_lines(cell, edges, x - precision, y - precision,
                           tollerance)
        #if booldebug: print(links)
        delete(edges, cell, x - precision, y - precision)

        for p in links:
            graph.nodes.append(Node(p))
            q.push(Node(p))

        graph.manage_links(node, links)

    if len(graph.nodes) > 2:
        return DPS(graph)
Exemple #2
0
    def compile(self,
                input_file_path,
                output_file_path,
                rendering_function=None):
        dsl_file = open(input_file_path)
        current_parent = self.root

        for token in dsl_file:
            token = token.replace(" ", "").replace("\n", "")

            if token.find(self.opening_tag) != -1:
                token = token.replace(self.opening_tag, "")

                element = Node(token, current_parent, self.content_holder)
                current_parent.add_child(element)
                current_parent = element
            elif token.find(self.closing_tag) != -1:
                current_parent = current_parent.parent
            else:
                tokens = token.split(",")
                for t in tokens:
                    element = Node(t, current_parent, self.content_holder)
                    current_parent.add_child(element)

        output_html = self.root.render(self.dsl_mapping,
                                       rendering_function=rendering_function)
        with open(output_file_path, 'w') as output_file:
            output_file.write(output_html)
    def compile(self,
                input_file_path,
                output_file_path,
                rendering_function=None):
        dsl_file = open(input_file_path)
        current_parent = self.root
        i = 0
        for token in dsl_file:
            token = token.replace(" ", "").replace('\t', '').replace("\n", "")

            if token.find(self.opening_tag) != -1:
                token = token.replace(self.opening_tag, "")
                element = Node(token, current_parent, self.content_holder,
                               getID(i))
                current_parent.add_child(element)
                current_parent = element
                i += 1
            elif token.find(self.closing_tag) != -1:
                current_parent = current_parent.parent
            else:
                tokens = token.split(",")
                for t in tokens:
                    element = Node(t, current_parent, self.content_holder,
                                   getID(i))
                    current_parent.add_child(element)
                    i += 1
        output_html = '<!DOCTYPE html>\n<html lang="en">\n  <head>\n    <meta charset="utf-8" />\n    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />\n    <meta name="viewport" content="width=device-width, initial-scale=1" />\n    <meta name="theme-color" content="#000000" />\n    <!-- Bootstrap core CSS -->\n    <link href="../compiled-bootstrap/css/bootstrap.css" rel="stylesheet">\n    \n    <!-- Custom styles for this template -->\n    <link href="../effect.css" rel="stylesheet">\n	<link href="../page.css" rel="stylesheet">\n\n <title>Generated Page</title>\n   </head>\n  <body>\n'
        output_html += self.root.render(self.dsl_mapping,
                                        rendering_function=rendering_function)
        output_html += '\n\t<script>\n\t\tvar x = document.getElementsByClassName("list-group-item");\n\t\tfor(var i = 0; i < x.length; i++) x[i].style.width = (parseInt(x[i].parentElement.offsetWidth) - 24) + "px";\n\t\tvar z = document.getElementsByClassName("card-header");\n\t\tfor(var i = 0; i < z.length; i++) z[i].style.width = (parseInt(z[i].parentElement.offsetWidth) - 24) + "px";\n\t</script>\n\n <script src="../jquery/jquery.min.js"></script>\n    <script src="../compiled-bootstrap/js/bootstrap.bundle.min.js"></script>\n  </body>\n</html>\n'
        with open(output_file_path, 'w') as output_file:
            output_file.write(output_html)
def parse_tree(cyk_matrix, current_symbol, i, j, errors, nonterminals):
    """Takes a Matrix, a symbol, a start location, an end location, the best
    error distance for the string, and a list of nonterminals and returns a
    parse tree for the individual characters in the string. This can be used
    to find I'.
    """
    if i == j - 1:
        tups = cyk_matrix.get(i, j)
        if current_symbol in tups:
            tup = tups[current_symbol]
            if tup[1] == errors:
                return Node(i, j, tup[2])
        raise LookupError('Could not find {} in cyk_matrix at {}'.format(
            current_symbol, (i, j)))
    A, B, q_1, q_2, dab, k = [None] * 6
    try:
        for k in range(i + 1, j):
            for rhs, dab in nonterminals[current_symbol].items():
                A, B = rhs.split()
                if A in cyk_matrix.get(i, k) and B in cyk_matrix.get(k, j):
                    q_1 = cyk_matrix.get(i, k)[A][1]
                    q_2 = cyk_matrix.get(k, j)[B][1]
                    if dab.errors + q_1 + q_2 == errors:
                        raise BreakIt
        raise LookupError(('Could not find match for right hand side of any '
                           'production of {} in cyk_matrix at {}').format(
                               current_symbol, (i, j)))
    except BreakIt:
        pass
    left = parse_tree(cyk_matrix, A, i, k, q_1, nonterminals)
    right = parse_tree(cyk_matrix, B, k, j, q_2, nonterminals)
    root = Node(i, j, dab)
    root.left = left
    root.right = right
    return root
Exemple #5
0
 def create_graph(self):
     """Creates a Huffman graph from self.heap"""
     while(len(self.heap.l) > 1):
         node1 = self.heap.pop()
         node2 = self.heap.pop()
         merged = Node(None, node1.freq + node2.freq)
         merged.left = node1
         merged.right = node2
         self.heap.push(merged)
Exemple #6
0
    def setUp(self):
        """
        Setup method for initializing nodes
        Returns:

        """
        self.first_node = Node(1)
        self.second_node = Node(2)
        self.first_node.next = self.second_node
Exemple #7
0
    def __init__(self, dsl_mapping_file_path):
        with open(dsl_mapping_file_path) as data_file:
            self.dsl_mapping = json.load(data_file)

        self.opening_tag = self.dsl_mapping["opening-tag"]
        self.closing_tag = self.dsl_mapping["closing-tag"]
        self.content_holder = self.opening_tag + self.closing_tag

        self.root = Node("body", None, self.content_holder)
Exemple #8
0
def MakeNewElem(list):
    tempNode = Node("cvor", list[0].freq + list[1].freq)
    list[0].parent = tempNode
    list[1].parent = tempNode
    tempNode.left = list[0]
    tempNode.right = list[1]
    list.pop(0)
    list.pop(0)
    list.append(tempNode)
    list.sort()
    def __init__(self, coordinates):
        """Constructof for IntersectionFinder."""
        self.found = []

        self.getDistance = GetGPSDistance.GetGPSDistance()
        # The coordinates are inverted here because search tree inverts path
        self.startPoint = Node.Node(-1, coordinates[2], coordinates[3])
        self.endPoint = Node.Node(-2, coordinates[0], coordinates[1])
        # [0] = Node ID, [1] = Distance from xPoint
        self.closestToStart = [sys.maxsize, 0]
        self.closestToEnd = [sys.maxsize, 0]
Exemple #10
0
def createNode():
    data = request.data
    data = json.loads(data)
    assert data["name"]

    node = Node()
    node.name = data["name"]

    getDomainRegistry().NodeRepository().save(node)

    return json.dumps(node.__dict__)
Exemple #11
0
def test():
    root = Node()
    root.right = Node()
    root.right.right = Node()
    root.right.right.right = Node()
    root.right.right.right.right = Node()
    root.right.left = Node()
    root.left = Node()
    root.left.right = Node()
    ibt = IsBalancedTree()
    print ibt.is_balanced(root)
Exemple #12
0
def main():
    selection = int(sys.argv[1])
    # Reads in the board from the random_board.py output
    board = list(map(int, sys.stdin.read().split()))
    board = [[board[0], board[1], board[2]], [board[3], board[4], board[5]],
             [board[6], board[7], board[8]]]
    state = State(board)

    # Setting the x and y position for the blank tile
    for i in range(0, 3):
        for j in range(0, 3):
            if board[i][j] == 0:
                state.setx(i)
                state.sety(j)

    # Initialize the relevent data structures
    curr_node = Node(state, h(state, selection))
    closed = Set()
    frontier = PriorityQueue()
    frontier.push(curr_node)
    V = 0

    # Loops through and creates the search tree
    while objective(curr_node.getstate()) == False:
        closed.add(curr_node.getstate())
        # Creates new states based on the valid_moves returned by the successor
        for valid_state in successor(curr_node.getstate()):
            V += 1
            # Checking that the state we are eveluating has not already been expanded. Only adds to the queue if false
            if closed.isMember(valid_state) == False:
                frontier.push(
                    Node(valid_state, h(valid_state, selection), curr_node))
        curr_node = frontier.pop()

    N = closed.length() + frontier.length()
    d = curr_node.getd()
    print("V=%d" % (V))
    print("N=%d" % (N))
    print("d=%d" % (d))
    print("b=%f" % (N**(1 / d)))
    print()

    # Create an empty list in order to retrieve the path from the goal node's parents
    stack = []
    while curr_node.getid() != 0:
        stack.append(curr_node)
        curr_node = curr_node.getparent()
    print(curr_node.getstate())
    # Prints out the solution path
    for node in reversed(stack):
        if node != None:
            print(node.getstate())
Exemple #13
0
class TestNode(unittest.TestCase):
    def setUp(self):
        self.test_node_0 = Node(0, 0.1, 1, 1, 1, 1, 1, 1)
        self.test_node_1 = Node(1, 0.1, 1, 1, 1, 1, 1, 1)

    def test_add_neighbour(self):
        strength: int = 20
        self.assertTrue(len(self.test_node_0.neighbours) == 0)
        self.assertTrue(len(self.test_node_1.neighbours) == 0)
        self.test_node_0.add_neighbour(self.test_node_1, strength)
        self.assertTrue(self.test_node_0.neighbours[1] == strength)
        self.assertTrue(1 in self.test_node_0.neighbours)
        self.assertTrue(len(self.test_node_1.neighbours) == 0)
Exemple #14
0
    def setUp(self):
        self.test_graph = Graph()
        self.test_node_0 = Node(0, 0.1, 1, 1, 1, 1, 1, 1)
        self.test_node_1 = Node(1, 0.2, 2, 2, 2, 2, 2, 2)
        self.test_node_2 = Node(2, 0.3, 1, 2, 3, 4, 5, 6)
        self.test_graph.add_vertex(self.test_node_0)
        self.test_graph.add_vertex(self.test_node_1)
        self.test_graph.add_vertex(self.test_node_2)
        self.test_graph.add_edge(self.test_node_0, self.test_node_1, 30)
        self.test_graph.add_edge(self.test_node_1, self.test_node_2, 50)

        self.test_sim = Simulation()
        self.test_sim.load_graph(self.test_graph)
Exemple #15
0
def test():
    root = Node(val=1)
    root.right = Node(val=2)
    root.right.right = Node(val=3)
    node1 = Node(val=4)
    root.right.right.right = node1
    root.right.right.right.right = Node(val=5)
    node2 = Node(val=6)
    root.right.left = node2
    root.left = Node(val=7)
    node3 = Node(8)
    root.left.right = node3
    cm = CommonAncestor()
    print cm.find_anc(root, node1, node3).val
    def test_push(self):
        """
        Unit-test for stack.push method
        Returns:

        """
        # Add new element by pushing into the stack, then check the value
        new_element = Node(4)
        self.stack.push(new_element)
        self.assertEqual(new_element.value, self.stack.peek())

        # Add another element with push operation to make sure ;)
        new_element = Node(5)
        self.stack.push(new_element)
        self.assertEqual(new_element.value, self.stack.peek())
Exemple #17
0
 def read_graph_as_matrix(self, filename):
     file = open(filename, 'r')
     self.nodes, self.edges = list(), list()
     f = False
     tag = "A"
     lines = file.readlines()
     for i in range(len(lines)):
         if lines[i] != '\n':
             if f:
                 # Создаем ребра
                 weights = [int(x) for x in lines[i].split(' ')]
                 for j in range(len(weights)):
                     if weights[j] != 0:
                         self.edges.append(
                             Edge.Edge(self.nodes[i - 1], self.nodes[j],
                                       weights[j]))
             else:
                 # Создаем нужное количество вершин
                 amount = int(lines[0])
                 # print(amount)
                 f = True
                 for w in range(amount):
                     self.nodes.append(Node.Node(tag))
                     tag = chr(ord(tag) + 1)
     self.normalize()
     pass
def main():
    """
    Main function for this file
    Returns:

    """
    # Initialize a plotter
    plotter = Plotter()

    # Create BigO object to observe for each runtime complexity
    enqueue_op = BigO('Enqueue operation')
    dequeue_op = BigO('Dequeue operation')
    peek_op = BigO('Peek operation')

    # Add BigO objects to compare the plots
    plotter.add_objects(enqueue_op, dequeue_op, peek_op)

    for i in range(1000):
        queue = create_queue(i + 1)

        # Time enqueue function with a queue where the size == i
        node = Node(np.random.randint(0, 1000))
        enqueue_op.time_function(enqueue_operation, node=node, queue=queue)

        # Time dequeue function with a dequeue where the size == 1
        dequeue_op.time_function(dequeue_operation, queue=queue)

        # Time peek function with a queue of size i.
        peek_op.time_function(peek_operaiton, queue=queue)

    plt.ylim((0, 3e-5))
    plotter.to_plot()
Exemple #19
0
 def heap_list(self, freq):
     """Takes a character-frequency dictionary, converts them into Nodes and sorts them into a heap"""
     for key in freq:
         node = Node(key, freq[key])
         self.heap.append(node)
     self.heap = Heap(self.heap)
     self.heap.sort()
Exemple #20
0
class Compiler:
    def __init__(self, dsl_mapping_file_path):
        with open(dsl_mapping_file_path) as data_file:
            self.dsl_mapping = json.load(data_file)

        self.opening_tag = self.dsl_mapping["opening-tag"]
        self.closing_tag = self.dsl_mapping["closing-tag"]
        self.content_holder = self.opening_tag + self.closing_tag

        self.root = Node("body", None, self.content_holder)

    def compile(self, dsl_file, rendering_function=None):
        # dsl_file = open(input_file_path)
        current_parent = self.root

        for token in dsl_file:
            token = token.replace(" ", "").replace("\n", "")

            if token.find(self.opening_tag) != -1:
                token = token.replace(self.opening_tag, "")

                element = Node(token, current_parent, self.content_holder)
                current_parent.add_child(element)
                current_parent = element
            elif token.find(self.closing_tag) != -1:
                current_parent = current_parent.parent
            else:
                tokens = token.split(",")
                for t in tokens:
                    element = Node(t, current_parent, self.content_holder)
                    current_parent.add_child(element)

        output_html = self.root.render(self.dsl_mapping,
                                       rendering_function=rendering_function)
        return output_html
Exemple #21
0
def ReadNodes(fileName, delimiter_=','):
    """ read node input file and set up node objects.
    
    This function will automatically create an internal node ID for each node. 
    Internal node IDs are consecutive non-negative integers starting from 0 as 
    required by initializations of dist_apsp and pred_apsp. 
    
    See CalculateAPSP(method='dij') for details.
    """
    global _dict_nodes
    global _map_uid_id

    with open(fileName) as f:
        # skip the header
        next(f)
        csvf = csv.reader(f, delimiter=delimiter_)
        # internal node id used for calculation
        nodeID = 0
        for r in csvf:
            # note that nodeUID here is STRING
            nodeUID = r[0].strip()
            _dict_nodes[nodeID] = Node(nodeID, nodeUID)
            if nodeUID not in _map_uid_id.keys():
                _map_uid_id[nodeUID] = nodeID
            else:
                raise Exception('DUPLICATE NODE ID FOUND: ' + nodeUID)
            nodeID += 1
Exemple #22
0
def loadDataSet(filename='data.xml'):
    tree = ET.parse(filename)
    root = tree.getroot()

    vehicles = []
    for vehicle in root.iter('vehicle'):
        vehicle_dict = {}
        vehicle_dict['id'] = int(vehicle.get('id'))
        vehicle_dict['dep_node'] = int(vehicle.find('departure_node').text)
        vehicle_dict['ari_node'] = int(vehicle.find('arrival_node').text)
        vehicle_dict['capacity'] = float(vehicle.find('capacity').text)
        vehicle_dict['early'] = float(vehicle.find('early').text)
        vehicle_dict['late'] = float(vehicle.find('late').text)
        vehicle_dict['velocity'] = float(vehicle.find('velocity').text)
        vehicles.append(vehicle_dict)

    nodes = NodeList(vehicles)

    for node, request in zip(root.iter('node'), root.iter('request')):
        id_ = int(node.get('id'))
        type_ = int(node.get('type'))
        x = float(node.find('cx').text)
        y = float(node.find('cy').text)
        position = np.array([x, y])
        early = float(node.find('early').text)
        late = float(node.find('late').text)
        service_time = float(node.find('s').text)
        demand = float(request.find('quantity').text)
        node_ = Node(id_, type_, position, early, late, service_time, demand)
        nodes.append(node_)

    return nodes
Exemple #23
0
class Compiler:
    def __init__(self, dsl_mapping_file_path):
        with open(dsl_mapping_file_path) as data_file:
            self.dsl_mapping = json.load(data_file)

        self.opening_tag = self.dsl_mapping["opening-tag"]
        self.closing_tag = self.dsl_mapping["closing-tag"]
        self.content_holder = self.opening_tag + self.closing_tag

        self.root = Node("body", None, self.content_holder)

    def compile(self, input_file_path, output_file_path, rendering_function=None):
        dsl_file = open(input_file_path)
        current_parent = self.root

        for token in dsl_file:
            token = token.replace(" ", "").replace("\n", "")

            if token.find(self.opening_tag) != -1:
                token = token.replace(self.opening_tag, "")

                element = Node(token, current_parent, self.content_holder)
                current_parent.add_child(element)
                current_parent = element
            elif token.find(self.closing_tag) != -1:
                current_parent = current_parent.parent
            else:
                tokens = token.split(",")
                for t in tokens:
                    element = Node(t, current_parent, self.content_holder)
                    current_parent.add_child(element)

        output_html = self.root.render(self.dsl_mapping, rendering_function=rendering_function)
        with open(output_file_path, 'w') as output_file:
            output_file.write(output_html)
Exemple #24
0
def list_sequences(node):
    """
    List the input group recursively.

    Sequences and groups are listed before their children. Sequences are unwrapped, but only
    listed once. Bitfield are NOT listed. This filter can be used to generate structs in C/C++.
    """
    sequences = []
    _list_sequences_recursively(node, sequences)

    names = set()
    list = []
    for seq in reversed(sequences):
        if seq.name not in names:
            names.add(seq.name)

            if hasattr(seq, 'nodes'):
                # NOTE: the sequence is unwrapped, lists all elements, we need only the defined ones
                for index in range(len(seq.nodes) / seq.number):
                    item = seq.nodes[index]
                    if item.is_sequence:
                        list.append(
                            Node(seq, {"number": item.number}, item.name))
#                        print "N", item.name, item.is_sequence, item.number
                    else:
                        list.append(item)
#                        print "R", item.name, item.is_register

            list.append(seq)


#            print "S", seq.name

    return list
Exemple #25
0
def parse_vrp(filepath):
    # read whole file
    s = open(filepath, "r").read()

    # get coordinates substring
    node_coord_section = extract_section(s, "NODE_COORD_SECTION",
                                         "DEMAND_SECTION").split('\n')[1:-1]
    # get demands substring
    demands_section = extract_section(s, "DEMAND_SECTION",
                                      "DEPOT_SECTION").split('\n')[1:-1]

    # extract client data from read strings
    number_of_clients = len(demands_section)
    nodes = []
    for i in range(0, number_of_clients):
        # parse client data
        x, y = node_coord_section[i].strip().split(" ")[1:]
        demand = demands_section[i].strip().split(" ")[-1]
        # create node and add to nodes list
        nodes.append(Node(i, demand, x, y))

    # extract capacity data from file
    capacity = int(
        extract_section(s, "CAPACITY :", "NODE_COORD_SECTION").strip())

    return nodes, capacity
Exemple #26
0
def main():
    """
    Main function for this file
    Returns:

    """
    # Initialize a plotter
    plotter = Plotter()

    # Create BigO object to observe for each runtime complexity
    push_op = BigO('Push operation')
    pop_op = BigO('Pop operation')
    peek_op = BigO('Peek operation')

    # Add BigO objects to compare the plots
    plotter.add_objects(push_op, pop_op, peek_op)

    for i in range(1000):
        stack = create_stack(i + 1)

        # Time push function with a stack where the size == i
        node = Node(np.random.randint(0, 1000))
        push_op.time_function(push_operation, node=node, stack=stack)

        # Time pop function with a stack where the size == 1
        pop_op.time_function(pop_operation, stack=stack)

        # Time peek function with a stack of size i.
        peek_op.time_function(peek_operaiton, stack=stack)

    plt.ylim((1e-7 - 1e-5, 1e-7 + 1e-5))
    plotter.to_plot()
Exemple #27
0
    def test_remove_vertex(self):
        n0, n1 = self.test_node_0, self.test_node_1
        n2 = Node(2, 0.1, 1, 1, 1, 1, 1, 1)
        self.test_graph.add_vertex(n0)
        self.test_graph.add_vertex(n1)
        self.test_graph.add_vertex(n2)
        self.test_graph.add_edge(n0, n1, 5)
        self.test_graph.add_edge(n1, n2, 15)
        self.test_graph.add_edge(n2, n0, 25)

        self.test_graph.remove_vertex(0)

        # test_node_0 no longer in neighbours dicts
        self.assertFalse(0 in n1.neighbours)
        self.assertFalse(0 in n2.neighbours)

        # neighbours edges intact
        self.assertTrue(2 in n1.neighbours)
        self.assertTrue(1 in n2.neighbours)

        self.assertFalse(0 in self.test_graph.vertices)

        # try to remove something that is removed
        with self.assertRaises(AssertionError):
            self.test_graph.remove_vertex(0)

        self.test_graph.remove_vertex(1)
        self.assertFalse(1 in n2.neighbours)
Exemple #28
0
def init_graph():
    """
	"""
    n = ['u', 'x', 'y', 'v', 'w', 'z']
    e = ['uv', 'uw', 'vw', 'vx', 'ux', 'wx', 'xy', 'wy', 'wz', 'yz']
    e_l_node = ['u', 'u', 'v', 'v', 'u', 'w', 'x', 'w', 'w', 'y']
    e_r_node = ['v', 'w', 'w', 'x', 'x', 'x', 'y', 'y', 'z', 'z']
    w = [2, 5, 3, 2, 1, 3, 1, 1, 5, 2]

    nb_node = len(n)
    nb_edge = len(e)

    E = {}
    for i in range(nb_edge):
        assert e[i] not in E.keys(), "{0} is in E".format(e[i])

        E[e[i]] = Edge(e[i], e_l_node[i], e_r_node[i], w[i])

    N = {}
    for i in range(nb_node):
        assert n[i] not in N.keys(), "{0} is in N".format(n[i])

        N[n[i]] = Node(n[i])

    for i in range(nb_edge):
        N[e_l_node[i]].insert_neigbors(e_r_node[i], w[i])
        N[e_r_node[i]].insert_neigbors(e_l_node[i], w[i])

    G = (N, E)
    return G
    def setUp(self):
        """
        Setup method to initialize a linked-list
        Returns:

        """
        # Create test nodes
        self.head_node = Node(1)
        self.second_node = Node(2)
        self.third_node = Node(3)

        # Create test linked-list
        self.list = LinkedList(self.head_node)

        # Append nodes such that the list is: 1->2->3
        self.list.append_right(self.second_node)
        self.list.append_right(self.third_node)
Exemple #30
0
    def setUp(self):
        """
        Setup method for unit-testing. This method will be called for each test case
        in this file
        Returns:

        """
        # Initialize a couple nodes to test the queue class
        self.first_node = Node(3)
        self.second_node = Node(2)
        self.third_node = Node(1)

        # Create a queue with elements created above
        self.queue = Queue()
        self.queue.enqueue(self.first_node)
        self.queue.enqueue(self.second_node)
        self.queue.enqueue(self.third_node)
    def setUp(self):
        """
        Setup method for unit-testing. This method will be called for each test case
        in this file
        Returns:

        """
        # Create dummy nodes for testing the stack class
        self.first_node = Node(1)
        self.second_node = Node(2)
        self.third_node = Node(3)

        # Push all nodes into a stack
        self.stack = Stack()
        self.stack.push(self.first_node)
        self.stack.push(self.second_node)
        self.stack.push(self.third_node)
Exemple #32
0
 def __read_nodes(self, nodes):
     count = len(nodes.index)
     counter = 1
     for (ind, row) in nodes.iterrows():
         if counter % 10000 == 0:
             print(int(counter / count * 100), '%')
         counter += 1
         self.nodes[row.id] = Node.Node(id_=row.id, x=row.lon, y=row.lat)
def shortest_path(source, target):
    """
    Returns the shortest list of (movie_id, person_id) pairs
    that connect the source to the target.

    If no possible path, returns None.
    """
    # create a node for the source
    start = Node(state=source, parent=None, action=None)

    # check if node is the solution and if not in put it into teh frontier
    if int(start.state) == int(target):
        return None
    else:
        frontier = QueueFrontier()
        frontier.add(start)

    # initialised an empty explored set() of nodes that have already been added
    explored = set()
    explored.add(int(start.state))

    # loop until solution is found
    while True:

        # if frontier is empty then no solution found
        if frontier.empty():
            return None

        # Choose a node from the frontier
        node = frontier.remove()

        # add  neighbour nodes to frontier and check if they are the solution
        neighbors = neighbors_for_person(node.state)
        for movie, person in neighbors:
            if not person == target and int(person) not in explored:
                new_node = Node(state=person, parent=node, action=movie)
                frontier.add(new_node)
            elif person == target:
                new_node = Node(state=person, parent=node, action=movie)
                path = []
                while new_node.parent is not None:
                    path.append((new_node.action, new_node.state))
                    new_node = new_node.parent
                path.reverse()
                return path
Exemple #34
0
    def __init__(self, dsl_mapping_file_path):
        with open(dsl_mapping_file_path) as data_file:
            self.dsl_mapping = json.load(data_file)

        self.opening_tag = self.dsl_mapping["opening-tag"]
        self.closing_tag = self.dsl_mapping["closing-tag"]
        self.content_holder = self.opening_tag + self.closing_tag

        self.root = Node("body", None, self.content_holder)
def parse_tree(cyk_matrix, current_symbol, i, j, errors, nonterminals):
    """Takes a Matrix, a symbol, a start location, an end location, the best
    error distance for the string, and a list of nonterminals and returns a
    parse tree for the individual characters in the string. This can be used
    to find I'.
    """
    if i == j - 1:
        tups = cyk_matrix.get(i, j)
        if current_symbol in tups:
            tup = tups[current_symbol]
            if tup[1] == errors:
                return Node(i, j, tup[2])
        raise LookupError('Could not find {} in cyk_matrix at {}'.format(
            current_symbol, (i, j)))
    A, B, q_1, q_2, dab, k = [None] * 6
    try:
        for k in range(i+1, j):
            for rhs, dab in nonterminals[current_symbol].items():
                A, B = rhs.split()
                if A in cyk_matrix.get(i, k) and B in cyk_matrix.get(k, j):
                    q_1 = cyk_matrix.get(i, k)[A][1]
                    q_2 = cyk_matrix.get(k, j)[B][1]
                    if dab.errors + q_1 + q_2 == errors:
                        raise BreakIt
        raise LookupError((
            'Could not find match for right hand side of any '
            'production of {} in cyk_matrix at {}').format(
                current_symbol, (i, j)))
    except BreakIt:
        pass
    left = parse_tree(cyk_matrix, A, i, k, q_1, nonterminals)
    right = parse_tree(cyk_matrix, B, k, j, q_2, nonterminals)
    root = Node(i, j, dab)
    root.left = left
    root.right = right
    return root
Exemple #36
0
def scan(locations,  ignores):
    """ Walks through the file system and analyzes the files and folders"""
    startTime = datetime.datetime.now()

    node_lists = []
    temp_filename_set = set()
    temp_foldername_set = set()
    dirs_analyzed = 0
    files_analyzed = 0

    # Search and mark the default folders found in the file system
    if sys.platform in ['Windows',  'win32']:
        default_locations = {os.path.join(HOME, folder) for folder in DEFAULT_FOLDERS["win"]}
    elif sys.platform in ['darwin']:
        default_locations = {os.path.join(HOME, folder) for folder in DEFAULT_FOLDERS["mac"]}
        apps_folder = [os.path.join(HOME, "Applications")]
        ignores.append(apps_folder)
    elif sys.platform in ['linux',  'linux2',  'linux3']:
        default_locations = {os.path.join(HOME, folder) for folder in DEFAULT_FOLDERS["linux"]}
    else:
        raise "Plattform not detected"

    for location in locations:
        norm_location = str(location)
        the_nodes = {}
        temp_nodes = {}
        node_id_counter = 0
        file_id_counter = 0

        def add_depths(node_id, depth):
            """ assigns depth to node, does same for children, their children... """
            temp_node = the_nodes[node_id]
            temp_node.depth = depth
            for child in temp_node.children:
                add_depths(child, depth + 1)
            the_nodes[node_id] = temp_node

        def is_hidden_file(root, f):
            """ checks to see if file is hidden (OS-sensitive) """
            if sys.platform in ['Windows',  'win32']:
                try:
                    full_path = os.path.join(root, f)
                    attrs = ctypes.windll.kernel32.GetFileAttributesW(full_path)
                    assert attrs != -1
                    result = bool(attrs & 2)
                except (AttributeError, AssertionError):
                    result = False
                return result
            else:  # there are other ways to hide files in MacOS but they are incredibly obscure and presumably infrequently used. also very 'expensive' to check.
                if str(f).startswith('.'):
                    return True
                else:
                    return False

        def is_symlink_file(root, f):
            """ checks to see if a file is a shortcut or symlink (OS-sensitive) """
            if sys.platform in ['Windows',  'win32']:
                if str(f)[-4:] == '.lnk':
                    return True
                else:
                    return False
            else:
                if os.path.islink(os.path.join(root, f)):
                    return True
                else:
                    return False

        # the actual walk process and the main observations made at each step
        for root, dirs, files in scandir(norm_location,  topdown=True,  onerror=None,  followlinks=False):
            this_node = Node()
            root_head_tail = os.path.split(root)
            node_id_counter += 1
            this_node.node_id = str(node_id_counter)
            this_node.name_length = len(root_head_tail[1])

            if root in default_locations:
                this_node.default = True
            #TO DO: check to see if it is the desktop, if so, mark the folder (will require appro. folder property)

            this_node.letters = sum(char.isalpha() for char in root_head_tail[1])
            this_node.numbers = sum(char.isdigit() for char in root_head_tail[1])

            this_node.white_spaces = sum(char.isspace() for char in root_head_tail[1])
            this_node.special_chars = sum(not char.isdigit() and not char.isalpha() and not char.isspace() for char in root_head_tail[1])

            if root_head_tail[1] in temp_foldername_set:
                this_node.name_duplicate = True
            else:
                temp_foldername_set.add(root_head_tail[1])

            try:
                root_stat_info = os.stat(root)
                this_node.m_time = datetime.datetime.fromtimestamp(root_stat_info.st_mtime).strftime("%Y-%m-%d %H:%M:%S")
                this_node.c_time = datetime.datetime.fromtimestamp(root_stat_info.st_ctime).strftime("%Y-%m-%d %H:%M:%S")

                if root_stat_info.st_nlink > 1:
                    this_node.hard_link_duplicate = True
            except:
                this_node.m_time = -2
                this_node.c_time = -2

            original_dirs = len(dirs)
            # Removes folders to be ignored from walk
            for ignore in ignores:
                norm_ignore = str(ignore)
                dirs[:] = [d for d in dirs if not ((os.path.join(root, d) in norm_ignore) and (norm_ignore in os.path.join(root, d)))]

            ignored_children = (original_dirs - len(dirs))
            dirs[:] = [d for d in dirs if os.access(os.path.join(root, d), os.W_OK)]

            inaccessible_children = (original_dirs - (len(dirs) + ignored_children))
            dirs[:] = [d for d in dirs if not d[0] == '.']

            this_node.hidden_children = (original_dirs - (len(dirs) + ignored_children + inaccessible_children))
            dirs[:] = [d for d in dirs if not (os.path.islink(os.path.join(root, d)))]

            # store number of dirs that are actually symlinks
            this_node.symlinks = (original_dirs - (len(dirs) + ignored_children + inaccessible_children + this_node.hidden_children))

            for d in dirs:
                dirs_analyzed += 1
                # print("Scanning directory #{}: {}".format(dirs_analyzed, os.path.join(root, d)))
                this_node.path_children.append(os.path.join(root, d))

            for f in files:
                files_analyzed += 1
                # print("Scanning file #{}: {}".format(files_analyzed, os.path.join(root, f)))

                if is_symlink_file(root, f):
                    # increment node.symlinks for each 'file' symlink found (counted in the same variable as folder symlinks)
                    this_node.symlinks += 1
                elif is_hidden_file(root, f):
                    this_node.hidden_files += 1
                else:
                    this_file = File()
                    file_id_counter += 1
                    this_file.file_id = file_id_counter
                    this_file.full_name_length = len(str(f))

                    try:
                        statinfo = os.stat(os.path.join(root, f))
                    except:
                        statinfo = False

                    if statinfo:
                        if statinfo.st_nlink > 1:
                            this_file.hard_link_duplicate = True

                        this_file.a_time = datetime.datetime.fromtimestamp(statinfo.st_atime).strftime("%Y-%m-%d %H:%M:%S")
                        this_file.m_time = datetime.datetime.fromtimestamp(statinfo.st_mtime).strftime("%Y-%m-%d %H:%M:%S")
                        this_file.c_time = datetime.datetime.fromtimestamp(statinfo.st_ctime).strftime("%Y-%m-%d %H:%M:%S")

                        this_file.file_size = statinfo.st_size

                        if this_file.file_size == 0:
                            try:
                                this_file.file_size = os.path.getsize(os.path.join(root, f))
                            except:
                                this_file.file_size = -2
                    else:
                        this_file.a_time = -2
                        this_file.m_time = -2
                        this_file.c_time = -2
                        this_file.file_size = -2

                    if str(f) in temp_filename_set:
                        this_file.name_duplicate = True
                    else:
                        temp_filename_set.add(str(f))

                    if '.' in f:
                        split_name = f.rsplit('.', 1)
                        this_file.extension = split_name[1]
                        this_file.letters = sum(c.isalpha() for c in split_name[0])
                        this_file.numbers = sum(c.isdigit() for c in split_name[0])
                        this_file.white_spaces = sum(c.isspace() for c in split_name[0])
                        this_file.special_chars = sum(not c.isdigit() and not c.isalpha() and not c.isspace() for c in split_name[0])
                    else:
                        this_file.letters = sum(c.isalpha() for c in f)
                        this_file.numbers = sum(c.isdigit() for c in f)
                        this_file.white_spaces = sum(c.isspace() for c in f)
                        this_file.special_chars = sum(not c.isdigit() and not c.isalpha() and not c.isspace() for c in f)
                    this_node.file_list.append(this_file)

            # this is done so that paths need not be permanently stored, but node relationships can be recorded
            for temp_id, temp_node in list(temp_nodes.items()):
                if (len(temp_node.path_children) < 1):
                    the_nodes[temp_id] = temp_node
                    del temp_nodes[temp_id]
                else:
                    for path_child in temp_node.path_children:
                        if ((root in path_child) and (path_child in root)):
                            temp_node.children.append(str(this_node.node_id))
                            temp_node.path_children.remove(path_child)

            if len(this_node.path_children) > 0:
                temp_nodes[this_node.node_id] = this_node
            else:
                the_nodes[this_node.node_id] = this_node

        while len(temp_nodes) > 0:
            for temp_id, temp_node in list(temp_nodes.items()):
                while len(temp_node.path_children) > 0:
                    for path_child in temp_node.path_children:
                        temp_node.unknown_children += 1
                        temp_node.path_children.remove(path_child)
                the_nodes[temp_id] = temp_node
                del temp_nodes[temp_id]

        node_lists.append(the_nodes)

        add_depths("1", 0)  # call the earlier defined function, starting from the top
    runtime = datetime.datetime.now() - startTime
    print("Walking runtime: {}".format(runtime))
    return node_lists