我在嵌套表中有值。我需要做一些计算并得到结果。这些课程分为script1.py
和script2.py
。
有超类Values
,它读取列表中的每一行并将它们转换为numpy数组。
还有子类Dimensions
,它从Values
继承数组并进行计算。
代码与表一起运行并获取结果,但不起作用。我收到错误消息:
init ()缺少1个必需的位置参数:' tbl'
script1.py
:
import numpy as np
class Values:
def __init__(self, tbl):
self.tbl = tbl
def get_val_a(self):
val_a = np.array(self.tbl[0])
return val_a
def get_val_b(self):
val_b = np.array(self.tbl[1])
return val_b
def get_val_c(self):
val_c = np.array(self.tbl[2])
return val_c
script2.py
:
from script1 import Values
val = Values()
class Dimensions(Values):
def __init__(self, tbl):
super().__init__(tbl)
def get_dim_a(self):
dim_a = 2 * val.get_val_a()
return dim_a
def get_dim_b(self):
dim_b = 2 * val.get_val_b()
return dim_b
def get_dim_c(self):
dim_c = 2 * val.get_val_c()
return dim_c
script3.py
:
from script2 import Dimensions
table = [[1, 2, 3],
[1, 4, 9],
[1, 8, 27]]
dim = Dimensions()
dim_1 = dim.get_dim_a()
dim_2 = dim.get_dim_b()
dim_3 = dim.get_dim_c()
print('Dimensions: ', dim_1, dim_2, dim_3)
答案 0 :(得分:2)
您的代码中有这些实例化尝试:
val = Values()
dim = Dimensions()
您需要将相关参数传递给它们才能实例化,因为在实例化时会隐式调用__init__
方法,并且在两种情况下都需要参数。
答案 1 :(得分:0)
Idos 已经说明失败的原因,我不再重复了。看看你的代码,我猜测你需要传递表格:
# script3.py
dim = Dimensions(table)
在 script2.py 中,我不知道为什么你在那里创建了一个Values
对象而且我不知道该猜什么。
答案 2 :(得分:0)
阅读你的答案,我已经遵循了传递论据的线索。我也意识到我的示例代码太简单,无法理解我的问题。 我正在寻找的是:
到目前为止,这是我最好的解决方案:
# csv file: table.csv
# -------------------
#a,b,c
1,2,3
1,4,9
1,8,27
# script1
# -------
import numpy as np
import csv
class ImportFileCSV:
'''
Read any csv file and transpose the rows to columns.
There are different csv files with different dimensions.
'''
def __init__(self, file_csv):
self.table_csv = []
self.file_csv = file_csv
def read_table_csv(self):
with open(self.file_csv, newline = '') as f_csv:
reader = csv.reader(f_csv)
for row in reader:
if row[0].startswith('#'): continue # Ignore the header.
self.table_csv.append(row)
self.table_arr = np.array(self.table_csv)
return self.table_arr.T
class Values(ImportFileCSV):
'''
Assign the values of each column to an array.
There would be different classes Values1, Values2... for each csv file.
'''
def __init__(self, file_csv):
super().__init__(file_csv)
import_file_csv = ImportFileCSV(self.file_csv)
self.table = import_file_csv.read_table_csv()
def get_val_a(self):
val_a = np.array(self.table[0], dtype = int)
return val_a
def get_val_b(self):
val_b = np.array(self.table[1], dtype = int)
return val_b
def get_val_c(self):
val_c = np.array(self.table[2], dtype = int)
return val_c
# script2
# -------
from script1 import Values
class Dimensions(Values):
'''
Do some basic calculations according to specific standards.
For each standard there would be different classes Dimensions1, Dimension2...
'''
def __init__(self, file_csv):
super().__init__(file_csv)
self.val = Values(self.file_csv)
def get_dim_a(self):
dim_a = self.val.get_val_a() + 2
return dim_a
def get_dim_b(self):
dim_b = self.val.get_val_b() * 2
return dim_b
def get_dim_c(self):
dim_c = self.val.get_val_c() ** 2
return dim_c
# script 3
# --------
# PROJECT
from script2 import Dimensions
dim = Dimensions('table.csv')
dim_a = dim.get_dim_a()
dim_b = dim.get_dim_b()
dim_c = dim.get_dim_c()
solution = dim_c - dim_a - dim_b
print('Dimensions:', dim_a, dim_b, dim_c)
print('Solution :', solution)
# Results
# -------
Dimensions: [3 3 3] [ 4 8 16] [ 9 81 729]
Solution : [ 2 70 710]