swat.cas.datamsghandlers.CASDataMsgHandler

class swat.cas.datamsghandlers.CASDataMsgHandler(vars, nrecs=1000, reclen=None, locale=None, transformers=None)

Bases: object

Base class for all CAS data message handlers

All CAS data message handlers should inherit from this class. The communication between the client and CAS server requires several steps and error handling that is implemented in this class.

When subclassing CASDataMsgHandler, you only need to implement two pieces: __init__ (the constructor) and getrow. The constructor must create the vars= parameter for the table.addtable CAS action and store it in the vars instance attribute. The getrow method, must return a single row of data values to be added to the data buffer.

Parameters:
vars : list-of-dicts

The list of variables to upload. This has the same format as the vars= argument to the table.addtable action. Each dict should at least have the keys: name, rtype, and length.

nrecs : int, optional

The number of records in the buffer.

reclen : int, optional

The length of each record in the buffer.

locale : string, optional

The locale to use for messages.

transformers : dict-of-functions

Transformers to use for variables. Keys are the column names. Values are the function that does the transformation.

Returns:
CASDataMsgHandler object

Examples

The example below creates a custom data message handler with hard-coded data and variable definitions. The getrow method is defined to simply return the requested row in the data array.

>>> conn = swat.CAS()
>>> import swat.cas.datamsghandlers as dmh
>>> class MyDMH(dmh.CASDataMsgHandler):
...
...     def __init__(self):
...         self.data = [
...             ('A', 1, 100.2),
...             ('B', 2, 234.5),
...             ('C', 3, 999.0)
...         ]
...
...         vars = [
...             dict(name='name', label='Name', length=16,
...                  type='varchar', rtype='char', offset=0),
...             dict(name='index', label='Index', length=4,
...                  type='int32', rtype='numeric', offset=16),
...             dict(name='value', label='Value', length=8,
...                  type='sas', rtype='numeric', offset=20),
...         ]
...
...         super(MyDMH, self).__init__(vars)
...
...     def getrow(self, row):
...         try:
...             return self.data[row]
...         except IndexError:
...             return
...
>>> mydmh = MyDMH()
>>> out = conn.addtable(table='mytable', **mydmh.args.addtable)
>>> tbl = out.casTable
>>> print(tbl.head())
__init__(vars, nrecs=1000, reclen=None, locale=None, transformers=None)

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__(vars[, nrecs, reclen, locale, …]) Initialize self.
finish(connection) Finish the data sending operation
getone(connection, **kwargs) Get a single response from the server
getrow(row) Return the list of values for the requested row
send(connection, nrecs) Send the records to the connection
write(row, values) Write the value to the row and column specified in the buffer