Why do all objects get populated with the same coordinates? - python

I'm creating a program for a robot to map out a labyrinth. I'm doing this in Python and the problem is when I'm trying to create a 3D array with my objects by looping through it, somehow every object gets populated with [9, 9, 2].
class Field:
coordinates = {}
def __init__(self, x_arg, y_arg, z_arg = None):
self.coordinates['x'] = x_arg
self.coordinates['y'] = y_arg
if z_arg is not None:
self.coordinates['z'] = z_arg
else:
self.coordinates['z'] = 0
def update_location(self, x_arg, y_arg, z_arg = None):
del self.coordinates['x']
del self.coordinates['y']
self.coordinates['x'] = x_arg
self.coordinates['y'] = y_arg
if z_arg is not None:
del self.coordinates['z']
self.coordinates['z'] = z_arg
def __repr__(self):
return "[" + str(self.coordinates['x']) + ", " + str(self.coordinates['y']) + ", " + str(self.coordinates['z']) + "]"
def __str__(self):
return "[" + str(self.coordinates['x']) + ", " + str(self.coordinates['y']) + ", " + str(self.coordinates['z']) + "]"
map = [[[Field(current_x, current_y, current_z) for current_z in xrange(3)] for current_y in xrange(10)] for current_x in xrange(10)]

You've defined coordinates at class level so that property is shared between all Field instances. To fix this, try defining coordinates in the __init__ method:
class Field:
def __init__(self, x_arg, y_arg, z_arg = None):
self.coordinates = {}
self.coordinates['x'] = x_arg
self.coordinates['y'] = y_arg
...

You have the line coordinates = {} outside of any method. That means that dictionary is a class object, the same for all instances of that class.
Since you want that to vary for each instance, you need to put it in a method: the __init__ method to be precise. Assign that empty dictionary to self so each object instance has its own dictionary. So use something like this:
class Field:
def __init__(self, x_arg, y_arg, z_arg = None):
self.coordinates = {}
self.coordinates['x'] = x_arg
self.coordinates['y'] = y_arg
if z_arg is not None:
self.coordinates['z'] = z_arg
else:
self.coordinates['z'] = 0

Related

Is there a way to have a object type of your choice (i.e. LinkedEdge) hash as part of a 'union.set()' processing?

I have the following code and it works until it gets to the 'union.set()' part. It says, "unhashable type: 'LinkedEdge' " . I am not sure why this is the case since I have looked at other sources on the web and in reference books to know that the 'g.addVertex()' method and the 'g.addEdge()' method as well as the arguments being passed should lead correctly to an output like this:
5 Vertices: A C B E D
5 Edges: A>B:3 A>C:2 B>D:1 C>D:1 D>E:2
class LinkedEdge(object):
def __init__(self, fromVertex, toVertex, weight = None):
self._vertex1 = fromVertex
self._vertex2 = toVertex
self._weight = weight
self._mark = False
def clearMark(self):
self._mark = False
def __eq__(self, other):
if self is other: return True
if type(self) != type(other):
return False
return self._vertex1 == other._vertex1 and self._vertex2 == other._vertex2
def getOtherVertex(self, thisVertex):
if thisVertex == None or thisVertex == self._vertex2:
return self._vertex1
else:
return self._vertex2
def getToVertex(self):
return self._vertex2
def getWeight(self):
return self._weight
def isMarked(self):
return self._mark
def setMark(self):
self._mark = True
def setWeight(self, weight):
self._weight = weight
def __str__(self):
return str(self._vertex1) + ">" + str(self._vertex2) + ":" + str(self._weight)
class LinkedVertex(object):
def __init__(self, label):
self._label = label
self._edgeList = []
self._mark = False
def clearMark(self):
self._mark = False;
def getLabel(self):
return self._label
def isMarked(self):
return self._mark
def setLabel(self, label, g):
g._vertices.pop(self._label, None)
g._vertices[label] = self
self._label = label
def setMark(self):
self._mark = True
def __str__(self):
return str(self._label)
def addEdgeTo(self, toVertex, weight):
edge = LinkedEdge(self, toVertex, weight)
self._edgeList.append(edge)
def getEdgeTo(self, toVertex):
edge = LinkedEdge(self, toVertex)
try:
return self._edgeList[self._edgeList.index(edge)]
except:
return None
def incidentEdges(self):
return iter(self._edgeList)
def neighboringVertices(self):
vertices = []
for edge in self._edgeList:
vertices.append(edge.getOtherVertex(self))
return iter(vertices)
def removeEdgeTo(self, toVertex):
edge = LinkedEdge(self, toVertex)
if edge in self._edgeList:
self._edgeList.remove(edge)
return True
else:
return False
class LinkedDirectedGraph(object):
def __init__(self, collection = None):
self._vertexCount = 0
self._edgeCount = 0
self._vertices = {}
if collection != None:
for label in collection:
self.addVertex(label)
# Methods for clearing, marks, sizes, string rep
def clear(self):
self._vertexCount = 0
self._edgeCount = 0
self._vertices = {}
def clearEdgeMarks(self):
for edge in self.edges():
edge.clearMark()
def clearVertexMarks(self):
for vertex in self.vertices():
vertex.clearMark()
def isEmpty(self):
return self._vertexCount == 0;
def sizeEdges(self):
return self._edgeCount
def sizeVertices(self):
return self._vertexCount
def __str__(self):
result = str(self.sizeVertices()) + " Vertices: "
for vertex in self._vertices:
result += " " + str(vertex)
result += "\n";
result += str(self.sizeEdges()) + " Edges: "
for edge in self.edges():
result += " " + str(edge)
return result
def addVertex(self, label):
self._vertices[label] = LinkedVertex(label)
self._vertexCount += 1
def containsVertex (self, label):
return label in self._vertices
def getVertex(self, label):
return self._vertices[label]
def removeVertex(self, label):
removedVertex = self._vertices.pop(label, None)
if removedVertex is None:
return False
# Examine all vertices
for vertex in self.vertices():
if vertex.removeEdgeTo(removedVertex):
self._edgeCount -= 1
self._vertexCount -= 1
return True
def addEdge(self, fromLabel, toLabel, weight):
fromVertex = self.getVertex(fromLabel)
toVertex = self.getVertex(toLabel)
fromVertex.addEdgeTo(toVertex, weight)
self._edgeCount += 1
def containsEdge(self, fromLabel, toLabel):
return self.getEdge(fromLabel, toLabel) != None
def getEdge(self, fromLabel, toLabel):
fromVertex = self._vertices[fromLabel]
toVertex = self._vertices[toLabel]
return fromVertex.getEdgeTo(toVertex)
def removeEdge (self, fromLabel, toLabel):
fromVertex = self.getVertex(fromLabel)
toVertex = self.getVertex(toLabel)
edgeRemovedFlg = fromVertex.removeEdgeTo(toVertex)
if edgeRemovedFlg:
self._edgeCount -= 1
return edgeRemovedFlg
# Iterators
def edges(self):
result = set()
for vertex in self.vertices():
edges = vertex.incidentEdges()
result = result.union(set(edges))
return iter(result)
def vertices(self):
return iter(self._vertices.values())
def incidentEdges(self, label):
return self._vertices[label].incidentEdges()
def neighboringVertices(self, label):
return self._vertices[label].neighboringVertices
g = LinkedDirectedGraph()
# Insert vertices
g.addVertex("John")
g.addVertex("Sam")
g.addVertex("Megan")
g.addVertex("Jennifer")
g.addVertex("Rick")
# Insert weighted edges
g.addEdge("John", "Sam", 3)
g.addEdge("John", "Megan", 2)
g.addEdge("Sam", "Jennifer", 1)
g.addEdge("Megan", "Jennifer", 1)
g.addEdge("Jennifer", "Rick", 2)
print(g)
If you override __eq__, then Python intentionally makes your class unhashable, since there is no longer a guarantee that the default hashing algorithm (based on the object's location in memory) is compatible with your __eq__ algorithm. "Compatible" here just means that equal objects must have equal hashes. By default, nothing is equal, so when you make some things equal using an __eq__ method, you impose a requirement on what a proper hash function must do.
If you want a custom class with a custom __eq__ method to be hashable, you must implement a __hash__ method yourself.
It could be as simple as being based on the hash of the corresponding tuple:
def __hash__(self):
return hash((type(self), self._vertex1, self._vertex2))
The Python docs explain this here.

Can't print __str__ from the class

I wondered how can I print the class contents of a particular index. I made a class that has all the values of a certain seismic movement and stores each in its own data type.
Here is the class:
import re
class Txt_data:
def __init__(self, result):
self.date = result[0]
self.time = result[1]
self.latit = result[2]
self.long = result[3]
self.depth = result[4]
self.md = result[5]
self.ml = result[6]
self.mw = result[7]
self.region = result[8]
self.method = result[9]
def date(self):
return self._date
def time(self):
return self._time
def latit(self):
return self._latit
def long(self):
return self._long
def depth(self):
return self._depth
def md(self):
return self._md
def ml(self):
return self._ml
def mw(self):
return self._mw
def region(self):
return self._region
def method(self):
return self._method
# This does not work
def __str__(self):
return ('MAG: ' + float(self.ml()) + ' DEPTH: ' + int(self.region()) + ' DATE/TIME: ' + str(self.date()) + ' ' + str(self.time()) + ' LAT: ' + float(self.latit()) + ' LON: ' + float(self.long()))
result = [('2021.12.02', '22:29:24', '36.9605', '28.1775', '13.0', '-.-', '1.5', '-.-', 'KARACA-MARMARIS (MUGLA)', ' Quick')]
print(Txt_data(result))
I was trying to print the data using str method but it doesn't work.
Here is the error:
Traceback (most recent call last):
File "/Users/seyfalsultanov/Documents/uni comp 100/comp100-2021f-ps5-seyfalku/main.py", line 73, in <module>
print(Txt_data(result))
File "/Users/seyfalsultanov/Documents/uni comp 100/comp100-2021f-ps5-seyfalku/main.py", line 60, in __str__
print('MAG: ' + float(self.ml()) + ' DEPTH: ' + int(self.region()) + ' DATE/TIME: ' + str(self.date()) + ' ' + str(self.time()) + ' LAT: ' + float(self.latit()) + ' LON: ' + float(self.long()))
AttributeError: Txt_data instance has no __call__ method
My question is how to print the string i tried to print using str method in the class.
Thanks very much beforehand.
Your immediate problem is that you shadowed all your methods with instance attributes, instead of using _-prefixed names for the attributes that the method expect.
def __init__(self, result):
self._date = result[0]
self._time = result[1]
self._latit = result[2]
self._long = result[3]
self._depth = result[4]
self._md = result[5]
self._ml = result[6]
self._mw = result[7]
self._region = result[8]
self._method = result[9]
However, none of those getters are necessary; just use the instance attributes directly. In __str__, use an f-string to perform any necessary type conversions (which you are currently doing wrong; non-str values need to be converted to str values, not the other way around).
class Txt_data:
def __init__(self, result):
self.date = result[0]
self.time = result[1]
self.latit = result[2]
self.long = result[3]
self.depth = result[4]
self.md = result[5]
self.ml = result[6]
self.mw = result[7]
self.region = result[8]
self.method = result[9]
def __str__(self):
return f'MAG: {self.ml} DEPTH: {self.region} DATE/TIME: {self.date} {self.time} LAT: {self.latit} LON: {self.long}'
result = ('2021.12.02', '22:29:24', '36.9605', '28.1775', '13.0', '-.-', '1.5', '-.-', 'KARACA-MARMARIS (MUGLA)', ' Quick')
print(Txt_data(result))
Finally, I would recommend making __init__ not be responsible for splitting up a list. Have it simply take 10 different arguments, and use a dedicated class method to parse a list in a fixed format.
class Txt_data:
def __init__(self, date, time, latitude, long, depth, md, ml, mw, region, method):
self.date = date
self.time = time
self.latit = latit
self.long = long
self.depth = depth
self.md = md
self.ml = ml
self.mw = mw
self.region = region
self.method = method
#classmethod
def from_list(cls, x):
if len(x) != 10:
raise ValueError("Wrong number of elements in list")
return cls(*x)
def __str__(self):
return f'MAG: {self.ml} DEPTH: {self.region} DATE/TIME: {self.date} {self.time} LAT: {self.latit} LON: {self.long}'

Python - Parent method don't acess the value of variable children

Hi I'm having a problem in this classes I created the parent class extracao_nia with the method aplica_extracao for having the similar part of the execution that I use in others class and the diferent part is in the transform method definined in the children class
but I'm having an issue that the variables that I defined as list() are Null variable when I execute the code:
AttributeError: 'NoneType' object has no attribute 'append'
class extracao_nia:
def __init__(self, d=1, h=1, m=15):
self._data_base = "database"
self.UM_DIA = datetime.timedelta(days=d)
self.UMA_HORA = datetime.timedelta(hours=h)
self.INTERVALO = datetime.timedelta(minutes=m)
#property
def data_base(self):
return self._data_base
def aplica_extracao(self, SQL):
fim_intervalo = self.inicio + self.INTERVALO#
pbar = self.cria_prog_bar(SQL)#
while (fim_intervalo <= self.FIM):#
self.connector.execute(SQL,(self.inicio.strftime('%Y-%m-%d %H:%M'),fim_intervalo.strftime('%Y-%m-%d %H:%M')))#
for log in self.connector:#
self.transforma(log)
self.inicio = fim_intervalo
fim_intervalo = self.inicio + self.INTERVALO
class usuarios_unicos(extracao_nia):
def __init__(self, d=1, h=1, m=15, file='nodes.json'):
self._data_base = "database"
self.UM_DIA = datetime.timedelta(days=d)
self.UMA_HORA = datetime.timedelta(hours=h)
self.INTERVALO = datetime.timedelta(minutes=m)
self.file = file
self.ids = list()
self.nodes = list()
self.list_cpf = list()
def transforma(self, log):
context = json.loads(log[0])['context']
output = json.loads(log[0])['output']
try:
nr_cpf = context['dadosDinamicos']['nrCpf']
conversation_id = context['conversation_id']
nodes_visited = output['output_watson']['nodes_visited']
i = self.ids.index(conversation_id)
atual = len(self.nodes[i])
novo = len(nodes_visited)
if novo > atual:
nodes[i] = nodes_visited
except KeyError:
pass
except ValueError:
self.ids.append(conversation_id)
self.nodes = self.nodes.append(nodes_visited)
self.list_cpf = self.list_cpf.append(nr_cpf)
list.append returns None since it is an in-place operation, so
self.nodes = self.nodes.append(nodes_visited)
will result in self.nodes being assigned None. Instead you can just use
self.nodes += nodes_visited

Python - TypeError: object of type '...' has no len()

Here is a class:
class CoordinateRow(object):
def __init__(self):
self.coordinate_row = []
def add(self, input):
self.coordinate_row.append(input)
def weave(self, other):
result = CoordinateRow()
length = len(self.coordinate_row)
for i in range(min(length, len(other))):
result.add(self.coordinate_row[i])
result.add(other.coordinate_row[i])
return result
This is a part of my program:
def verwerk_regel(regel):
cr = CoordinateRow()
coordinaten = regel.split()
for coordinaat in coordinaten:
verwerkt_coordinaat = verwerk_coordinaat(coordinaat)
cr.add(verwerkt_coordinaat)
cr2 = CoordinateRow()
cr12 = cr.weave(cr2)
print cr12
def verwerk_coordinaat(coordinaat):
coordinaat = coordinaat.split(",")
x = coordinaat[0]
y = coordinaat[1]
nieuw_coordinaat = Coordinate(x)
adjusted_x = nieuw_coordinaat.pas_x_aan()
return str(adjusted_x) + ',' + str(y)
But I'm geting an error at "cr12 = cr.weave(cr2)":
for i in range(min(length, len(other))):
TypeError: object of type 'CoordinateRow' has no len()
You need to add a __len__ method, then you can use len(self) and len(other):
class CoordinateRow(object):
def __init__(self):
self.coordinate_row = []
def add(self, input):
self.coordinate_row.append(input)
def __len__(self):
return len(self.coordinate_row)
def weave(self, other):
result = CoordinateRow()
for i in range(min(len(self), len(other))):
result.add(self.coordinate_row[i])
result.add(other.coordinate_row[i])
return result
In [10]: c = CoordinateRow()
In [11]: c.coordinate_row += [1,2,3,4,5]
In [12]: otherc = CoordinateRow()
In [13]: otherc.coordinate_row += [4,5,6,7]
In [14]:c.weave(otherc).coordinate_row
[1, 4, 2, 5, 3, 6, 4, 7]
Iterating over a range of len(something) is very much an anti-pattern in Python. You should be iterating over the contents of the containers themselves.
In your case, you can just zip the lists together and iterate over that:
def weave(self, other):
result = CoordinateRow()
for a, b in zip(self.coordinate_row, other.coordinate_row):
result.add(a)
result.add(b)
return result
Here is the complete solution for the Cart and Item class implementation.
class Item:
def __init__(self, name, price):
self.name = name
self.price = price
def getPrice(self):
return self.price
def getName(self):
return self.name
class ShoppingCart:
def __init__(self):
self.list = []
def __len__(self):
return len(self.list)
def add(self, item):
self.list.append(item)
def total(self):
total = 0
for item in self.list:
total = total + item.getPrice()
return total
other is of type CoordinateRow, which does not have a length. Use len(other.coordinate_row) instead. This is the list that does have the length property.

Why do I keep getting the error: AttributeError: 'int' object has no attribute 'subject'

So I am creating a code what are the co requisites and which courses students can take next by specifying a specific course. For example, if the user inputs "ECE 256", then co requisites should be ECE 256L and the next class they can take is ECE 304.
List.print_list()
File /Users/marleneterrones/Dropbox/ECE 480 Group/linked list/node.py, line 45, in,
print_list
result = result + str(dataObj.subject)
AttributeError: 'str' object has no attribute 'subject'
class Node:
def __init__(self, subject=None, corec=[] , next_class=[]):
self.subject = subject
self.corec = corec
self.next_class = next_class
class LinkedList:
def __init__(self):
self.firstNode = Node(None, None, None)
self.lastNode = self.firstNode
self.size = 0
def add(self, subject,corec):
"""Add a node to the list"""
node = Node(subject , corec ,None)
node.subject = subject;
node.corec = corec;
if self.firstNode.subject == None:
self.firstNode = node
self.lastNode = node
else:
self.lastNode.next_class = node
self.lastNode = node
self.size += 1
def print_list(self):
"""prints whats ever in the array """
result = ""
currentNode = Node( None,None, None)
currentNode = self.firstNode
i = 0
result = result + "("
while currentNode != None:
if i > 0:
result = result + ","
dataObj = currentNode.subject
dataObj2 = currentNode.corec
if dataObj != None:
result = result + str(dataObj.subject)
if dataObj2 != None:
result = result + str(dataObj2.corec)
currentNode = currentNode.next_class
i = i + 1
result = result + ")"
return result
dataObj = currentNode.subject makes dataObj maybe an int, then dataObj.subject causes such an error. You may just want
result = result + str(dataObj)
there are some needless code in you snippet, e.g.:
node = Node(subject , corec ,None)
node.subject = subject;
node.corec = corec;
the 2 latter lines are not necessary since you've already initialized node with subject and corec.

Categories