我有一个几乎没有静态字段的类,它是从iterable初始化的(比如csvreader
的输出)。 __init__
执行从字符串到数字的类型转换:
class PerformanceTestResult(object):
def __init__(self, csv_row):
# csv_row[0] is just an ordinal number of the test - skip that
self.name = csv_row[1] # Name of the performance test
self.samples = int(csv_row[2]) # Number of measurement samples taken
self.min = int(csv_row[3]) # Minimum runtime (ms)
self.max = int(csv_row[4]) # Maximum runtime (ms)
self.mean = int(csv_row[5]) # Mean (average) runtime (ms)
self.sd = float(csv_row[6]) # Standard deviation (ms)
我正在考虑将其转换为namedtuple
,因为它没有太多其他内容。但我想在初始化期间保持类型转换。有没有办法用namedtuple
执行此操作? (我没有在__init__
工厂方法的详细输出中注意到namedtuple
方法,这让我暂停了默认初始化程序的工作方式。)
答案 0 :(得分:1)
您可以使用解包运算符csv_row
解压缩,而不是按原样传入*
。例如:
>>> def f(a, b):
... return a + b
...
>>> csv_row = [1, 2]
>>> f(*csv_row) # Instead of your current f(csv_row)
这也适用于namedtuple
,因为参数的顺序将在解包时保留:
>>> from collections import namedtuple
>>> PerformanceTestResult = namedtuple('PerformanceTestResult', [
... 'name',
... 'samples',
... 'min',
... 'max',
... 'mean',
... 'sd',
... ])
>>> test_row = ['test', '123', 2, 5, 3, None] # from your csv file
>>> ptr = PerformanceTestResult(*test_row)
>>> ptr
PerformanceTestResult(name='test', samples='123', min=2, max=5, mean=3, sd=None)
这不仅允许您使用namedtuple
,这似乎是一个非常好的主意,但它也使您的PerformanceTestResult
无需了解CSV文件!抽象是好的,因为现在你可以使用同一个类,无论数据来自何处以及采用何种格式。
如果您需要int()
和float()
转换,则必须编写单独的转换功能。您可以通过子类化将其构建到PerformanceTestResult
:
_PerformanceTestResult = namedtuple('PerformanceTestResult', [...])
class PerformanceTestResult(_PerformanceTestResult):
@classmethod
def from_csv(cls, row):
return cls(
row[0],
int(row[1]),
int(row[2]),
int(row[3]),
int(row[4]),
int(row[5]),
float(row[6])
)
可以这样使用:
>>> ptr = PerformanceTestResult.from_csv(your_csv_row)
或者您可以创建单独的转换功能:
def parse_csv_row(row):
return (row[0], int(row[1]), ...)
现在使用它在解包之前转换行:
>>> ptr = PerformanceTestResult(*parse_csv_row(your_csv_row))