iNTERFACEWARE Products Manual > Installing and Using Chameleon > Using Python Scripting > Python Scripting Examples > Retrieving a Table and Adding or Deleting Rows |
|
Looking for Iguana v.5 or v.6? Learn More or see the Help Center.
If you have defined a table definition in your VMD file, you can use the Environment Variable object's get_table() function to retrieve the contents of the table. get_table() can be called from the Global Inbound Parse-complete Script or the Global Inbound Post Process Script. get_table() returns a Table object. One element of the object is defined for each row of the table. Each row of the table is a Row object, whose behaviour is similar to that of a Python dictionary, as it consists of a series of keys and values. The keys are the column names of the table, and the values are the values stored in each field of the row. Here is an example of how to use get_table() to retrieve table data:
This example calls get_table() to retrieve the contents of the Patients table. It then calls len() to retrieve the number of rows in the table. It then displays the contents of each row in the table, repeatedly calling print_column() to retrieve and display each field in each row. When you have retrieved a table with get_table(), you can use the append_row() function, included in the Chameleon Python library, to add a row to the table:
This code creates a new entry in the Patients table for patient John Doe. (See Converting a Date from String to Double for information on the date_time_to_double() function.) You can also add a row to the middle of the table:
This creates a row and sets it to be the fifth row of the table. Existing rows are moved down to make room: in this example, the old fifth row now becomes the new sixth row, and so on. You can also delete one or more rows:
If an index is negative, indexing starts from the end of the table:
To assign a present but null value to a field in a Row Object, assign None to it:
If a field has the value None, row.has_key() will return true.
|