pytweezer.servers.clients module

Handels interaction with the datachannel in a convenient way

Currently there are 4 clients:

  • CommandClient (used for controlling drivers and processes)

  • DataClient (used for small data like 1d curves)

  • ImageClient (used for larger chunks of 2d data, mainly images)

  • MessageClient (used for error,warning,info and debug messages)

Examples

Receiving image data from a stream:

from pytweezer.servers import ImageClient
self.imageq=ImageClient(name)       #the name only has an effect if sending data
self.imageq.subscribe('teststream')

msg=self.imageq.recv()
    if msg!= None:
        msgstr,dict_info,array_data=msg

Sending data over a stream:

from pytweezer.servers import DataClient
self.dataq=DataClient('teststream')
self.dataq.send(info_dictionary,data_array,'substream1_')

Classes

class pytweezer.servers.clients.CommandClient(name, **kwargs)[source]

Bases: DataClient

send(cmd='', data=None, A=None, flags=0, copy=True, track=False, prefix=None)[source]

distribute over ZMQ

Parameters:
  • datadict (dict) – data to be send (must be json serializable)

  • A (np.array) – optional array of data

  • channel (string) – subchannel (will be appended to name when sending)

  • flags (int) – see ZMQ send flags in the ZMQ doku

class pytweezer.servers.clients.DataClient(name, **kwargs)[source]

Bases: GenericClient

class pytweezer.servers.clients.GenericClient(name='noname', recvtimeout=1000, subscribe=None)[source]

Bases: object

has_new_data()[source]

check whether there is unprocessed data in the stream

Returns:

Data is available. (the next recv call will not have to wait or timeout)

Return type:

bool

recv(flags=0, copy=True, track=False)[source]

recv a dictionary (and a numpy array)

Parameters:
  • flags (bitmap)

  • copy (bool)

  • track (bool) – ZMQ recieving options (leave as defaults unless you RTFM of ZMQ)

send(datadict, A=None, channel='', flags=0, copy=True, track=False, prefix=None)[source]

distribute ove ZMQ

Parameters:
  • datadict (dict) – data to be send (must be json serializable)

  • A (np.array) – optional array of data

  • channel (string) – subchannel (will be appended to name when sending)

  • flags (int) – see ZMQ send flags in the ZMQ doku

subscribe(channel)[source]

subscribe to a channel

Parameters:
  • channel ([string]) – string channel to subscribe to

  • channel – you can alternatively give a list of channels

unsubscribe(channel=None)[source]

unsubscribe from specific channel

Parameters:

channel (string)

class pytweezer.servers.clients.ImageClient(name, **kwargs)[source]

Bases: DataClient

send(header, data, channel='', flags=0, copy=True, track=False)[source]

distribute ove ZMQ

Parameters:
  • header (dict) – dictionary with additional info about the data (must be json serializable)

  • data (np.array) – data array to be send (must be numpy array, will be converted to bytes and reconstructed on the other side according to the header info)

  • channel (string) – subchannel (will be appended to name when sending)

  • flags (int) – see ZMQ send flags in the ZMQ doku

class pytweezer.servers.clients.NpEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: JSONEncoder

default(obj)[source]

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return super().default(o)