whisker_python_demo.py

Top  Previous  Next

#!/usr/bin/python

# whisker_python_demo.py

# 7 Feb 2010

# ---------------------------

# Syntax:    whisker_perl_demo.pl [server [port]]

#

# Note that Python comments are preceded by a hash (#)

# Python quick guides and reference:

#    http://www.intuit-symbiosis.org/computing/?uid=9

#    http://docs.python.org/tutorial/

# Standard conventions:

# - 4 spaces, not tabs

# - CamelCase for classes, lower_case_underscore for functions

 

import whisker # read in whisker.py from current directory or PYTHONPATH

# If you don't have it, fetch it from http://www.whiskercontrol.com/examples/whisker.py

 

# -----------------------------------------------------------

# Main program

# -----------------------------------------------------------

 

# Fetch command-line options.

from optparse import OptionParser

parser = OptionParser()

parser.add_option("-s", "--server",

                 action="store", type="string", dest="server", default="localhost",

                 help="connect to Whisker server SERVER", metavar="SERVER")

parser.add_option("-p", "--port",

                 action="store", type="string", dest="port", default="3233",

                 help="use TCP/IP port PORT", metavar="PORT")

parser.add_option("-v", "--verbosenetwork",

                 action="store_true", dest="verbosenetwork", default=True,

                 help="show verbose network messages")

parser.add_option("-q", "--quietnetwork",

                 action="store_false", dest="verbosenetwork",

                 help="suppress network messages")

parser.add_option("-d", "--verbosedatabase",

                 action="store_true", dest="verbosedatabase", default=True,

                 help="show verbose database messages")

parser.add_option("-x", "--quietdatabase",

                 action="store_false", dest="verbosedatabase",

                 help="suppress database messages")

(options, args) = parser.parse_args()

# We now have what we want in (e.g.) options.server and options.port

# Pass info to the whisker namespace.

whisker.verbosenetwork = options.verbosenetwork

whisker.verbosedatabase = options.verbosedatabase

 

print "Whisker demo in Python"

print "----------------------"

 

# Open log file.

print "\n--- Let's open a log file."

logfilename = whisker.ask_user("Text results file", "whiskertemp.txt")

logfile = open(logfilename, "w")

 

# Open database connection.

print "\n--- Let's open a database connection."

dbh = whisker.connect_to_database()

 

# Open network connection.

print "\n--- Let's connect to Whisker."

whisker.connect_both_ports(options.server, options.port)

 

# Initial commands to server: claiming lines and setting up the task.

print "\n--- We're connected. Let's set up a task."

whisker.send("ReportName Whisker python demo program")

whisker.send("ReportStatus Absolutely fine.")

whisker.send("WhiskerStatus")

reply = whisker.send_immediate("TimerSetEvent 1000 9 TimerFired")

reply = whisker.send_immediate("TimerSetEvent 12000 0 EndOfTask")

whisker.send("TestNetworkLatency")

 

# Enter a loop to listen to messages from the server.

print "\n--- Let's listen to the server and process its events."

for line in whisker.getlines_mainsock():

   if whisker.verbosenetwork:

       print "SERVER: " + line # For info only.

   if line == "Ping":

       # If the server has sent us a Ping, acknowledge it.

       whisker.send("PingAcknowledged")

   if line[:7] == "Event: ":

       # The server has sent us an event.

       event = line[7:]

       if whisker.verbosenetwork:

           print "EVENT RECEIVED: " + event # For info only.

       # Event handling for the behavioural task is dealt with here.

       if event == "EndOfTask":

           break # Exit the while loop.

 

# Create some sample data.

# For arrays of arrays, see http://perldoc.perl.org/perldsc.html#ARRAYS-OF-ARRAYS

# For passing array references to functions, see http://www.cs.cf.ac.uk/Dave/PERL/node61.html

table = "table1"

fields = ["field1","field2","field3"]

values = [

          [ "data1","data2","data3" ],

          [ "data4","data5","data6" ],

          [ "data7","data8","data9" ],

        ]

 

# Write data to disk/database. Do these separately, disk first, in case there are database problems.

print "\n--- Let's write to the log file."

whisker.produce_csv_output(logfile, fields, values)

 

print "\n--- Let's write to the database."

whisker.sql_insert_multiple_records(dbh, table, fields, values)

 

print "\n--- Let's log out from the Whisker server."

whisker.log_out()

print "----------------"

print "Finished."