|
6 | 6 |
|
7 | 7 | def send_array(socket, A, flags=0, copy=False, track=False, metadata=None): |
8 | 8 | """send a numpy array with metadata over zmq""" |
9 | | - md = dict( |
10 | | - dtype=str(A.dtype), |
11 | | - shape=A.shape, |
12 | | - timestamp=datetime.datetime.now().isoformat() |
13 | | - ) |
| 9 | + |
| 10 | + # create a metadata dictionary for the message |
| 11 | + md = {} |
| 12 | + # always add a timestamp |
| 13 | + md['timestamp'] = datetime.datetime.now().isoformat() |
| 14 | + |
| 15 | + # copy extra metadata |
| 16 | + if metadata: |
| 17 | + md.update(metadata) |
| 18 | + |
| 19 | + # if we don't have an array |
| 20 | + if A is None: |
| 21 | + # send only json |
| 22 | + socket.send_json(md, flags) |
| 23 | + # and we're done |
| 24 | + return |
| 25 | + |
| 26 | + # add array metadata |
| 27 | + md['dtype'] = str(A.dtype) |
| 28 | + md['shape'] = A.shape |
14 | 29 | try: |
| 30 | + # If an array has a fill value assume it's an array with missings |
| 31 | + # store the fill_Value in the metadata and fill the array before sending. |
15 | 32 | md['fill_value'] = A.fill_value |
16 | 33 | A = A.filled() |
17 | 34 | except AttributeError: |
18 | 35 | # no masked array, nothing to do |
19 | 36 | pass |
20 | 37 |
|
21 | | - if metadata: |
22 | | - md.update(metadata) |
23 | | - if A is None: |
24 | | - # send only json |
25 | | - socket.send_json(md, flags) |
26 | | - else: |
27 | | - # send json, followed by array |
28 | | - socket.send_json(md, flags | zmq.SNDMORE) |
| 38 | + # send json, followed by array |
| 39 | + socket.send_json(md, flags | zmq.SNDMORE) |
29 | 40 | # Make a copy if required and pass along the buffer |
30 | 41 | msg = buffer(np.ascontiguousarray(A)) |
31 | 42 | socket.send(msg, flags, copy=copy, track=track) |
|
0 commit comments