Ejemplo n.º 1
0
    def move_vertex(self, v_coords1, v_coords2):
        '''
        Moves a vertex from one position to another by deleting it and then
        recreating it with the same connectivity.

        ARGUMENTS:
        v_coords1 (tuple)       --  Coordinates for the original vertex
        v_coords2 (tuple)       --  New coordinates for the vertex

        RAISES:
        KeyError                --  if v_coords1 or v_coords2 don't exist
        '''
        # Make sure input is tuple of length 2 containing numbers and convert
        # numbers to floats
        v_coords1 = tuple2_check(v_coords1)
        v_coords2 = tuple2_check(v_coords2)

        try:
            connections = self.vertices[v_coords1].connections
        except KeyError:
            raise KeyError('{} is not an existing vertex'.v_coords1)

        # Remove the vertex and its connections
        self.remove(v_coords1)

        # Add the vertex back but at the new coordinates
        self.add(v_coords2)

        # Add back the connections
        for vertexID in connections:
            self.connect(v_coords2, vertexID)
Ejemplo n.º 2
0
    def disconnect(self, vertex1, vertex2):
        '''
        Disconnect a vertex to another vertex

        ARGUMENTS:
        vertex1 (str)           --  First vertex to be disconnected
        vertex2 (str)           --  Second vertex to be disconnected

        RAISES:
        TypeError               --  if vertexes are not tuples converted to
                                    strings.
        KeyError                --  if vertex has not yet been added
        '''
        # Check that input is correct and convert for consistency
        vertex1 = tuple2_check(vertex1)
        vertex2 = tuple2_check(vertex2)

        # Remove vertex 2 from list of connections for vertex 1
        try:
            self.vertices[vertex1].discon(vertex2)
        except KeyError as inst:
            raise

        # Remove vertex 1 from list of connections for vertex 2
        try:
            self.vertices[vertex2].discon(vertex1)
        except KeyError:
            raise RuntimeError('''Vertex connection not symmetric: {} does not
                    contain connection info for {}'''.format(vertex2, vertex1))
Ejemplo n.º 3
0
    def connect(self, vertex1, vertex2):
        '''
        Connect a vertex to another vertex

        ARGUMENTS:
        vertex1 (str)           --  First vertex to be connected
        vertex2 (str)           --  Second vertex to be connected

        RAISES:
        TypeError               --  if vertexes are not tuples converted to
                                    strings.
        KeyError                --  if vertex has not yet been added
        '''
        # Check that input is correct and convert for consistency
        vertex1 = tuple2_check(vertex1)
        vertex2 = tuple2_check(vertex2)

        try:
            self.vertices[vertex1].con(vertex2)
        except KeyError as inst:
            raise KeyError('{} is not an existing vertex'.format(vertex1))
        except RuntimeError as inst:
            raise

        try:
            self.vertices[vertex2].con(vertex1)
        except KeyError as inst:
            self.vertices[vertex1].discon(vertex2)
            raise KeyError('{} is not an existing vertex'.format(vertex2))
Ejemplo n.º 4
0
    def con(self, vertexID):
        '''
        Records a vertex as connected to the current vertex

        ARGUMENTS:
        vertexID (str)      --  vertexID to connect to the current vertex

        RAISES:
        TypeError           --  if vertexID is not a tuple
        RuntimeError        --  if vertexID is the same as this vertex's ID
        '''
        # Check to make sure the ID is a two-number tuple
        try:
            vertexID = tuple2_check(vertexID)
        except (TypeError, IndexError) as e:
            raise ('''vertex to be connected must be a length 2 tuple not 
                  {}'''.format(vertexID))
        except ValueError:
            msg = '''Cannot connect vertex \'{}\' because is does not contain 
            numbers'''.format(vertexID)
            raise TypeError(msg)
        if vertexID == self.id:
            raise RuntimeError('Cannot connect a vertex to itself')
        else:
            self.connections.add(vertexID)
Ejemplo n.º 5
0
    def remove(self, v_coords):
        '''
        Removes a given vertex from the set of vertices

        ARGUMENTS:
        v_coords (tuple)        --  Tuple of coordinates defining the vertex

        RAISES:
        KeyError                -- if v_coords is not an existing vertex
        '''
        # Make sure input is tuple of length 2 containing numbers and convert
        # numbers to floats
        v_coords = tuple2_check(v_coords)

        # Try to remove the vertex and throw exception if it fails
        try:
            self.coordinates.remove(v_coords)
        except KeyError:
            raise KeyError('{} is not an existing vertex'.format(v_coords))

        # Now figure out the connectivity
        connections = self.vertices[v_coords].connections.copy()

        # Remove all of the connections between this vertex and others
        for vertexID in connections:
            self.disconnect(v_coords, vertexID)

        # Now finally delete the vertex in the dict
        del self.vertices[v_coords]
Ejemplo n.º 6
0
    def discon(self, vertexID):
        '''
        Removes a vertexID from the set of connected vertices

        ARGUMENTS:
        vertexID (str)      --  vertexID to disconnect to the current vertex

        RAISES:
        KeyError            --  If vertexID is not part of the set of connected
                                vertices
        TypeError           --  If vertexID is not a tuple
        '''
        # Check to make sure the ID is a two-number tuple
        try:
            vertexID = tuple2_check(vertexID)
        except (TypeError, IndexError) as e:
            raise ('''vertex to be connected must be a length 2 tuple not 
                  {}'''.format(vertexID))
        except ValueError:
            msg = '''Cannot connect vertex \'{}\' because is does not contain 
            numbers'''.format(vertexID)
            raise TypeError(msg)

        # Raise error if not connected
        try:
            self.connections.remove(vertexID)
        except KeyError:
            raise KeyError('Vertex {} is not connected to {}'.format(
                vertexID, self.id))
Ejemplo n.º 7
0
    def __init__(self, coords):
        '''
        ARGUMENTS:
        coords (tuple)      --  The x- and y-coordinates of the vertex

        RAISES:
        TypeError           --  if vertex is not a tuple
        IndexError          --  if vertex tuple is not length 2
        ValueError          --  if vertex tuple does not contain numbers
        '''
        # Make sure input is tuple of length 2 containing numbers and convert
        # numbers to floats
        coords = tuple2_check(coords)

        self.x = coords[0]
        self.y = coords[1]
        self.id = coords
        self.connections = set([])
Ejemplo n.º 8
0
    def add(self, v_coords):
        '''
        Adds a vertex to the list of all vertices

        ARGUMENTS:
        v_coords (tuple)        --  Tuple pair of coordinates for vertex
        '''
        # Make sure input is tuple of length 2 containing numbers and convert
        # numbers to floats
        v_coords = tuple2_check(v_coords)

        # Check current length of vertex set
        initial_len = len(self.coordinates)
        # Try to add the new vertex to the set
        self.coordinates.add(v_coords)
        new_len = len(self.coordinates)

        # Check if the vertex was added. If it wasn't, that means it's already
        # in the set so it shouldn't be added
        if new_len > initial_len:
            self.vertices[v_coords] = Vertex(v_coords)