1
I am trying to create a data collector of an experiment I am doing. So far I have the following code:
in an archive experiments.py
has the class:
import csv
class Experimento:
def __init__(self, material: str, num_points: int, time: float=20.0, expType: str='varredura'):
if expType not in ('varredura','ponto fixo'):
raise ValueError('Tipo de experimento inválido. Selecione entre Varredura ou Ponto Fixo. \n NOTA: não utilize letras maiúsculas!')
self.num_points = num_points
self.time = time
self.material = material
self.expType = expType
And the subclass:
class Ponto_Fixo(Experimento):
def __init__(self, material: str, num_points: int, x: int, z: int, time: float=20.0):
Experimento.__init__(material, num_points, time, 'ponto fixo')
self.x = x
self.z = z
self.expID = material + ' x' + str(self.x) + ' z' + str(self.z) + ' pt=' + str(num_points)
header = ['data','material','ponto','x','z','contagem','tempo(s)']
csv_title = '\%s.csv' %(self.expID)
with open('resultados'+ csv_title,'w') as file:
writer = csv.writer(file)
writer.writerow(header)
In the main.py
, I run this code like this:
import experiments
if __name__ == "__main__":
material = 'agua+CaCO3'
expType = 'ponto fixo'
tempo = 20.0
num_points = 6
x = 2
z = 2
def new_experiment(material, num_points, tempo, x, z, expType):
if expType == 'ponto fixo':
experiments.Ponto_Fixo(material, num_points, x, z, tempo)
novo_exp = new_experiment(material, num_points, tempo, x, z, expType)
However, by doing this execution I get the title error
File "C:\Adam\Doutorado\Laboratorio\TARG\programs\data collection\main.py", line 46, in <module>
novo_exp = new_experiment(material, num_points, tempo, x, z, expType)
File "C:\Adam\Doutorado\Laboratorio\TARG\programs\data collection\main.py", line 37, in new_experiment
experiments.Ponto_Fixo(material, num_points, x, z, tempo)
File "C:\Adam\Doutorado\Laboratorio\TARG\programs\data collection\experiments.py", line 113, in __init__
Experimento.__init__(material, num_points, time, 'ponto fixo')
File "C:\Adam\Doutorado\Laboratorio\TARG\programs\data collection\experiments.py", line 37, in __init__
self.num_points = num_points
AttributeError: 'str' object has no attribute 'num_points'
The line that the program accuses the error is this (which is within the Experiment class):
self.num_points = num_points
Also, the line that changes the type of the variable num_point
of int
for str
is after this line of error. Therefore, I am not able to see where and why is taking place the change of type of int
for str
in the num_point
.
Can anyone shed a light? Thank you
while doing what you suggested, at first gave the following error
ValueError: __init__() requires a code object with 0 free vars, not 1
However, insisting a little on the execution of the code, the error stopped happening and everything went well. I still don’t really understand why of this, but your help solved the problem. Thank you very much.– Adam Basílio
@Adambasil here his code executed perfectly: https://ideone.com/Vg3MCh -- I only removed the part of CSV pq that cannot read or write files in this online test.
– Guilherme Nascimento
@Adambasil must be because you are using jupyter, or anything with autoreloader, sometimes occurs.
– Guilherme Nascimento