Understanding NumPy’s einsum

(Note: this answer is based on a short blog post about einsum I wrote a while ago.) What does einsum do? Imagine that we have two multi-dimensional arrays, A and B. Now let’s suppose we want to… multiply A with B in a particular way to create new array of products; and then maybe sum this new array along particular axes; and then maybe transpose the axes of the new … Read more

Dictionary in a numpy array?

You have a 0-dimensional array of object dtype. Making this array at all is probably a mistake, but if you want to use it anyway, you can extract the dictionary by indexing the array with a tuple of no indices: or by calling the array’s item method:

How do I print colored output to the terminal in Python?

Would the Python termcolor module do? This would be a rough equivalent for some uses. The example is right from this post, which has a lot more. Here is a part of the example from docs A specific requirement was to set the color, and presumably other terminal attributes, so that all following prints are that way. While I … Read more

Asyncio.gather vs asyncio.wait

Although similar in general cases (“run and get results for many tasks”), each function has some specific functionality for other cases: asyncio.gather() Returns a Future instance, allowing high level grouping of tasks: All tasks in a group can be cancelled by calling group2.cancel() or even all_groups.cancel(). See also .gather(…, return_exceptions=True), asyncio.wait() Supports waiting to be stopped after the first … Read more