I’m coding the final part of a clustering program, I want to parse a file like
JavaScript
x
6
1
--
2
COLOR
3
-
4
POINT COLOR
5
6
where
JavaScript
1
3
1
COLOR = (R,G,B)
2
POINT = (X,Y)
3
Example:
JavaScript
1
15
15
1
--
2
(255,0,4)
3
-
4
(0,0) (255,0,4)
5
(0,1) (255,0,4)
6
(0,2) (255,0,4)
7
(0,3) (255,0,4)
8
--
9
(32,32,12)
10
-
11
(1,0) (156,0,42)
12
(1,1) (156,0,42)
13
(1,2) (156,0,42)
14
(1,3) (156,0,42)
15
I want to save all this information in different classes to process the data easier.
Here is my code in python:
JavaScript
1
98
98
1
#!/usr/bin/env python3
2
3
import sys
4
5
class Point:
6
def __init__(self, x, y):
7
self.x = x
8
self.y = y
9
10
class Color:
11
def __init__(self, r, g, b):
12
self.r = r
13
self.g = g
14
self.b = b
15
16
class Pixel:
17
def __init__(self, point, color):
18
self.point = point
19
self.color = color
20
21
class Cluster:
22
def __init__(self, meanColor, pixels):
23
self.meanColor = meanColor
24
self.pixels = pixels
25
26
def printCluster(cl):
27
print("---------------")
28
print("(" + str(cl.meanColor.r) + "," + str(cl.meanColor.g) + "," + str(cl.meanColor.b) + ")")
29
print("I have " + str(len(pixels)) + " pixels.")
30
for px in pixels:
31
print("(" + str(px.point.x) + "," + str(px.point.y) + ") (" + str(px.color.r) + "," + str(px.color.g) + "," + str(px.color.b) + ")")
32
33
def printClusters(clusters):
34
for cl in clusters:
35
printCluster(cl)
36
37
def getColor(line):
38
r = int(line.split(',')[0][1:])
39
g = int(line.split(',')[1])
40
b = int(line.split(',')[2][:-1])
41
color = Color(r,g,b)
42
return (color)
43
44
45
file = open(sys.argv[1], 'r')
46
47
meanColor = None
48
clusters = []
49
cluster = ""
50
pixels = []
51
52
import copy
53
54
count = 0
55
file.readline()
56
line = file.readline().rstrip('n')
57
meanColor = getColor(line)
58
file.readline()
59
while True:
60
count += 1
61
line = file.readline().rstrip('n')
62
if (line == "--"):
63
pixelsCopy = list(pixels)
64
cluster = Cluster(meanColor, pixelsCopy)
65
printClusters(clusters)
66
printCluster(cluster)
67
clusters.append(cluster)
68
pixels = []
69
printCluster(cluster)
70
line = file.readline().rstrip('n')
71
meanColor = getColor(line)
72
file.readline()
73
elif (line and line != "--"):
74
print("-------------------------------")
75
printClusters(clusters)
76
print("line: ~" + line + "~")
77
x = int(line.split()[0].split(',')[0][1:])
78
y = int(line.split()[0].split(',')[1][:-1])
79
print("x: " + str(x))
80
print("y: " + str(y))
81
point = Point(x,y)
82
r = int(line.split()[1].split(',')[0][1:])
83
g = int(line.split()[1].split(',')[1])
84
b = int(line.split()[1].split(',')[2][:-1])
85
print("r: " + str(r))
86
print("g: " + str(g))
87
print("b: " + str(b))
88
color = Color(r,g,b)
89
pixels.append(Pixel(point, color))
90
if not line:
91
pixelsCopy = list(pixels)
92
cluster = Cluster(meanColor, pixelsCopy)
93
clusters.append(cluster)
94
#printCluster(cluster)
95
break
96
97
printClusters(clusters)
98
For some reason, after the while loop, when I print all clusters objects, I see that all contains the same pixels list, I try to do a copy with deepcopy, with [:] and with list() but makes no sense. I don’t know where my error is, also I print the cluster after reset to [] and I see that is save like a reference list but I don’t know why if I use list() to make a copy.
Thanks!
Advertisement
Answer
You need Cluster instance’s attribute named pixels:
JavaScript
1
7
1
def printCluster(cl):
2
print("---------------")
3
print("(" + str(cl.meanColor.r) + "," + str(cl.meanColor.g) + "," + str(cl.meanColor.b) + ")")
4
print("I have " + str(len(pixels)) + " pixels.")
5
for px in cl.pixels:
6
print("(" + str(px.point.x) + "," + str(px.point.y) + ") (" + str(px.color.r) + "," + str(px.color.g) + "," + str(px.color.b) + ")")
7