このように動作させるラッパークラスを作成するにはどうすればよいですか?numpy配列をkwarg-splattingする
def foo(a, b):
print a
data = np.empty(20, dtype=[('a', np.float32), ('b', np.float32)])
data = my_magic_ndarray_subclass(data)
foo(**data[0])
いくつかのより多くの背景:
は私がベクトル化したかった。このような関数のペアを持っていた:
def start_the_work(some_arg):
some_calculation = ...
something_else = ...
cost = some_calculation * something_else
return cost, dict(
some_calculation=some_calculation,
some_other_calculation=some_other_calculation
)
def finish_the_work(some_arg, some_calculation, some_other_calculation):
...
start_the_work
はの束と呼ばれることを意図して異なる議論が行われ、最低原価項目が完成する。同じ計算の多くは両方の機能によって使用されているので、辞書やkwarg-スプラッティングは、その結果に渡すために使用される次のように私はそれらをベクトル化することができ
def run():
best, best_cost, continuation = min(
((some_arg,) + start_the_work(some_arg)
for some_arg in [1, 2, 3, 4]),
key=lambda t: t[1] # cost
)
return finish_the_work(best, **continuation)
一つの方法は次のとおりです。
def start_the_work(some_arg):
some_calculation = ...
something_else = ...
cost = some_calculation * something_else
continuation = np.empty(cost.shape, dtype=[
('some_calculation', np.float32),
('some_other_calculation', np.float32)
])
continuation['some_calculation'] = some_calculation
continuation['some_other_calculation'] = some_other_calculation
return cost, continuation
しかし、辞書のように見えても、continuation
はkwarg-splattedできません。
'FOO(*データ[0])'ので動作します構造化配列のレコードは、タプルのように(反復の目的で)動作します。 – hpaulj
クラスは '** kwarg'として動作するためにどのような振る舞いをする必要がありますか? – Eric
その質問に対する答えは 'keys()'と '__getitem__'です。 – Eric