How can I write a wrapper class that makes this work?
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])
Some more background:
I had a pair of functions like this that I wanted to vectorize:
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):
...
With the intent that start_the_work
is called with a bunch of different arguments, and then the lowest cost item is complete. A lot of the same calculations are used by both functions, so a dictionary and kwarg-splatting is used to pass on those results:
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)
One way I can vectorize them is as follows:
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
But despite looking like a dictionary, continuation
cannot be kwarg-splatted.