Python Input Adapter (Sending Data to a Sybase CEP Stream)

An example that demonstrates how a simple publisher writes messages to a Sybase CEP stream. This could be extended to make it an input adapter.

The stream schema is assumed to have exactly two fields ('Symbol' and 'Price') and the generated messages look like the following:

Row 1, 1

Row 2, 2

Row 3, 3

...

import sys
import time
from threading import Thread
from c8 import C8
# When you start this program, pass:
# 1) the URI of the stream to publish to, for example,              #    "ccl://localhost:6789/Stream/Default/Subscriber2/StreamIn1"
# 2-N) the column names of the stream
class C8Test(Thread):
def __init__ (self, mode, uri, column_names):
 Thread.__init__(self)
 self.mode = mode
 self.uri = uri
 self.column_names = column_names
 
def run(self):
 if self.mode == 'publish':
   self.publish()
   
def publish(self):
   publisher = C8.Publisher(self.uri)
   j = 0
   while True:
     tuple = C8.Tuple(self.column_names)
     for i in range(len(self.column_names)):
         tuple.setvalue(self.column_names[i], self.column_names[i] 
         + '-' + str(j))
     # print 'Pub Ts:  ' + repr(tuple.gettimestamp())
     # print 'Pub Sym: ' + tuple.getvalues()[self.column_names[0]]
     print 'Pub Csv: ' + tuple.getcsv()
     
     publisher.write_tuple(tuple)
     time.sleep(1)
     j = j + 1
def main():
try:
 if len(sys.argv) < 3:
   print 'Error: Wrong number of arguments, usage: ' + sys.argv[0] 
         + ' <uri> <column-name>, [<column-name>, ...]'
   return
 
 # Extract column names from the command-line arguments.
 columns = [ ] + sys.argv;
 # Ignore/remove the name of the program (sys.argv[0]) 
 columns.pop(0);
 # Ignore/remove the uri (sys.argv[1]) 
 columns.pop(0);    
 pub = C8Test('publish', sys.argv[1], columns)
 pub.start()
 # This example runs for 60 seconds.  A real 
 # adapter would probably run indefinitely.
 while True:
   time.sleep(60)
  
except IOError (errno, strerror):
 print "I/O error(%s): %s" % (errno, strerror)
else:    
 print 'bye!'
if __name__ == '__main__':
main()