| Home | Trees | Index | Help |
|
|---|
| Package picalo :: Package base :: Module Table :: Class Table |
|
The primary data structure used in Picalo modules. This is the first object a user should learn about since it's used everywhere. Tables are the input into most functions and are also the return records of most functions.
See the Picalo manual for more information on tables.| Method Summary | |
|---|---|
| Table |
Creates a Picalo Table. |
| Table |
Adds two tables together. |
| returns |
Returns a copy of this table. |
Removes a record from the table. | |
Returns whether this table is equal to another table (or list of lists). | |
| Record or Column |
Retrieves a record or a full column of the table. |
Creates a new Table populated with records from i to j. | |
This is essentially the extend method. | |
| iterator |
Returns an interator to the Records in this Table. |
| int |
Return the number of records in the table. |
| str |
For debugging. |
Replaces an existing Record with the given data. | |
__sub__(self,
other)
| |
Internal method to inesrt a column once the Column object is created | |
| Record |
Inserts a new record at the end of this table. |
| Column |
Adds a new, calculated column with records given by expression. |
| Column |
Adds a new, calculated column with records given by expression. |
| Column |
Adds a new column to the table, optionally setting records of the new cells. |
Clears any active filter on this table, restoring the view to all records in the table | |
| Column |
Returns a single column of the table. |
| int |
Retrieves the number of columns in a table. |
Removes a column from the table and discards the records. | |
| int |
Dereferences the col name to its index (if it is a name). |
Appends the records in the given table to the end of this table. | |
Filters the table with the given expression. | |
| list |
Finds the record indices with given key=record pairs. |
| list |
Returns the column names of this table. |
| list |
Returns the column objects of this table. |
Inspects the first num_records in each column and automatically sets the type of each column. | |
Returns an index on this table for the given column name(s). | |
| returns |
Inserts a new record at the given index location. |
| Column |
Inserts a new, calculated column with records given by expression at the given index location. |
| Column |
Inserts a new, calculated column with records given by expression at the given index location. |
| Column |
Inserts a new column in the table at the given index location. |
Returns whether the table has been changed since loading | |
Returns True if this table has an active filter | |
| bool |
Returns whether this table is read only. |
| iterator |
Returns an iterator to the Records in thistTable. |
Moves a column to another location in the table. | |
Pretty prints the table to the given fp. | |
| Record |
Retrieves the record at the given index. |
| int |
Returns the number of records in this table. |
Reorders the columns according to the given list. | |
Saves this table in native Picalo format. | |
Saves this table to a Comma Separated Values (CSV) text file. | |
Saves this table to a delimited text file. | |
Saves this table to a Microsoft Excel 97+ file. | |
Saves this table to a fixed width text file. | |
Saves this table to a Tab Separated Values (TSV) text file. | |
Saves this table to an XML file using a pre-defined schema. | |
Sets whether the class has been changed since loading. | |
Sets the format of this column. | |
Changes the name of an existing column. | |
Sets the read only status of this table. | |
Sets the type of a column. | |
Sorts this table with optional arguments. | |
| Table |
Returns the structure of this table, including column names, input and output types, and general statistics. |
Opens a spreadsheet-view of the table if Picalo is being run in GUI mode. | |
Adds a listener to the table. | |
Calculates the columns map based upon the current columns. | |
Checks the current page size to see if we need to split it into two pages | |
Returns the filtered index of the given index, if a filter is in place. | |
Invalidates the indices already calculated on this table. | |
Notifies listeners that we've had a change | |
Populates a record with data. | |
Removes a listener from the table. | |
Internal function to save according to a specific dialect (see the csv module docs) | |
Sets the given cursor as the datasource for this table. | |
Swaps the current page for a new one | |
Swaps the current page to the one including the given record | |
Updates the filter index. | |
| Method Details |
|---|
__init__(self,
columns=3,
data=[])
|
__add__(self,
other)
|
__copy__(self)Returns a copy of this table. Use this method to make a full copy of all data.
|
__delitem__(self,
index)
Removes a record from the table. The proper use to call this method
is 'del table[i]' where i is the record number to remove. You can also
specify columns to delete with 'del table["colname"]' where
colname is the column name you want to delete.
|
__eq__(self,
other)
Returns whether this table is equal to another table (or list of
lists). Only the records of the table are compared -- not the column
names or column definitions.
|
__getitem__(self,
index)
Retrieves a record or a full column of the table. If the given index
is a column name, the column object is returned. If the given index is
an integer, the given row object is returned.
|
__getslice__(self,
i,
j)
Creates a new Table populated with records from i to j. This method
is deprecated, but included since the super (list) still includes
__getslice__.
|
__iadd__(self, other)This is essentially the extend method. I included a += method directly to increase efficiency (so no intermediate table needs to be created) |
__iter__(self)Returns an interator to the Records in this Table. This is normally achieved through:
>>> for record in mytable:
>>> # do something with each record object
|
__len__(self)
Return the number of records in the table. The method respects any
active filters on the table.
|
__repr__(self)
For debugging. Use view() for a formatted printout.
|
__setitem__(self,
index,
record)
|
_insert_column_(self, index, coldef, records=None, expression=None)Internal method to inesrt a column once the Column object is created |
append(self, *a, **k)Inserts a new record at the end of this table. This is the primary way to add new data to a table. If you need to insert a row in the middle of a table, use the insert() method. Records can be added in any of the following ways: Format 1:
|
append_calculated(self, name, expression)Adds a new, calculated column with records given by expression. Calculated columns act as regular columns in all ways. Their records are 'active' meaning their records change when the result of the expression. In other words, they are recalculated each time they are used rather than being stored statically. This is similar to the way Excel functions always reflect the most updated data records. Example:>>> table = Table([('id', int)], [[1],[2],[4]]) >>> table.append_calculated('plusone', "col000+1") >>> table.view() +--------+---------+ | col000 | plusone | +--------+---------+ | 1 | 2 | | 2 | 3 | | 4 | 5 | +--------+---------+
|
append_calculated_static(self, name, column_type, expression)Adds a new, calculated column with records given by expression. The records are calculated immediately using expression, and then they are static. In other words, this method calculates a new, regular column. The records are not 'active' in the sense that append_calculated() columns are active. When this method returns, the new column is the same as any other, non-calculated column. Example:>>> table = Table([('id', int)], [[1],[2],[4]]) >>> table.append_calculated('plusone', int, "col000+1") >>> table.view() +--------+---------+ | col000 | plusone | +--------+---------+ | 1 | 2 | | 2 | 3 | | 4 | 5 | +--------+---------+
|
append_column(self, name, column_type, records=None)Adds a new column to the table, optionally setting records of the new cells.
|
clear_filter(self)Clears any active filter on this table, restoring the view to all records in the table |
column(self, col)Returns a single column of the table. Column records can be read and modified but not deleted. This is a useful method when you want to work with a single column of a table. The returned Column object is essentially a list of records and can be treated as such. Any function that takes a list can take a Column object. This method is used often in analyses.
|
column_count(self)Retrieves the number of columns in a table. Note that since Picalo lists are zero-based, you access individual columns starting with 0. So although column_count() may report 3 columns, you access them via table.column(0), table.column(1), and table.column(2). However, using column names rather than direct indices is easier and more readable. table['colname'] returns the given column.
|
delete_column(self, column)Removes a column from the table and discards the records. Remaining column indices are decremented to reflect the new table structure. Column names (columns) are not modified. This action is permanent and cannot be undone.
|
deref_column(self, col_name)Dereferences the col name to its index (if it is a name). For example, if the column name "id" is given, it returns the index of this column (such as 0, 1, 2, etc.). The column can be specified as the column index, the column name, or even a negative index (from the last column backward).
|
extend(self, table)Appends the records in the given table to the end of this table. The two tables must have the same number of columns to be merged. Example:
>>> mytable3.extend(mytable2) # appends mytable2 to the end of mytable3
|
filter(self, expression=None)Filters the table with the given expression. Until the filter is either replaced or cleared, only the records matching the filter will be available in the table. All Picalo functions will see only this limited view of the table in their analyses. In other words, it is as if the filtered record is not in the table at all until the filter is removed. Filters are transient. They do not save with the table and they do not carry over if you copy a table. They are simply temporary filters that you can use to restrict Picalo analyses to a few records. Only one filter can be active at any time. Setting a new filter will replace the existing one. In creating your expression, use the standard record['col'] notation to access individual records in the table.
|
find(self, **pairs)Finds the record indices with given key=record pairs. This method is not normally used directly -- use Simple.select_by_record instead. This method is different than Simple.select_by_record in that it does not create a new table. Simple.select_by_record creates a new table consisting of copies of all matching records. In contrast, this method simply returns the record indices of the matching records. This method *is* efficient and can be used often. It calculates indices as needed and should select very fast. Example:>>> table = Table([ ... ('col001', int), ... ('col002', int), ... ('col003', unicode), ... ],[ ... [5,6,'flo'], ... [3,2,'sally'], ... [4,6,'dan'], ... [4,7,'stu'], ... [4,7,'ben'], ... [4,6,'benny'], ... ]) >>> results = table.find(col001=6, col000=4) >>> results [2, 5]
|
get_column_names(self)Returns the column names of this table.
|
get_columns(self)Returns the column objects of this table. Columns are Column objects, which contain the column name, calculated expression (if any), type if set, etc. Most users should call get_column_names() instead as it only returns the names of the columns. Only call this method if you need to access the internal column objects.
|
guess_types(self, num_records=-1)Inspects the first num_records in each column and automatically sets the type of each column. This method is not perfect, but it is fairly robust. It is a good method to call after importing from TSV/CSV if you don't want to set types manually. If num_records is less than 1, all records in the table are inspected (this can take a long time for huge tables but should be done for all other tables). If a column is a calculated column, its type is not set (columns with expressions do not have an explicit type).
|
index(self, *col_names)Returns an index on this table for the given column name(s). This method allows you to find the record indices that match specific keys. If only one column name is specified, the index will have as many records as there are unique records in the column. If multiple columns are specified, the index will have as many records as there are unique combinations of the records in the columns. Indices are used throughout Picalo internally and are not normally accessed by users. However, many analyses need to calculate indices directly, and exposing this method allows this behavior. This method is efficient -- if the table data have not been modified since the last time a specific index was asked for, it uses the previously calculated index.
|
insert(self, *a, **k)Inserts a new record at the given index location. The first parameter *must* be the index location to insert the record. The remaining parameters are the same as the append() method. Possible formats are (assuming you want to place the new record in the second row and push all existing records down one): Format 1:
|
insert_calculated(self, index, name, expression)Inserts a new, calculated column with records given by expression at the given index location. Calculated columns act as regular columns in all ways. Their records are 'active' meaning their records change when the result of the expression. In other words, they are recalculated each time they are used rather than being stored statically. This is similar to the way Excel functions always reflect the most updated data records. Example:>>> table = Table([('id', int)], [[1],[2],[4]]) >>> table.insert_calculated(0, 'plusone', "col000+1") >>> table.view() +---------+--------+ | plusone | col000 | +---------+--------+ | 2 | 1 | | 3 | 2 | | 5 | 4 | +---------+--------+
|
insert_calculated_static(self, index, name, column_type, expression)Inserts a new, calculated column with records given by expression at the given index location. The records are calculated immediately using expression, and then they are static. In other words, this method calculates a new, regular column. The records are not 'active' in the sense that append_calculated() columns are active. When this method returns, the new column is the same as any other, non-calculated column. Example:>>> table = Table([('id', int)], [[1],[2],[4]]) >>> table.append_calculated('plusone', int, "col000 + 1") >>> table.view() +--------+---------+ | col000 | plusone | +--------+---------+ | 1 | 2 | | 2 | 3 | | 4 | 5 | +--------+---------+
|
insert_column(self, index, name, column_type, records=None)Inserts a new column in the table at the given index location.
|
is_changed(self)Returns whether the table has been changed since loading |
is_filtered(self)Returns True if this table has an active filter |
is_readonly(self)Returns whether this table is read only.
|
iterator(self, respect_filter=True)Returns an iterator to the Records in thistTable. This is normally achieved through:
>>> for record in mytable:
>>> # do something with each record object
This method is provided to allow you to ignore the filter if you
want. The regular iterator syntax (for record in mytable) always
respects any active filters.
|
move_column(self, column, new_index)Moves a column to another location in the table. A column can be moved in front of other columns or behind other columns with this method. The column parameter is the name of the column to be moved, the index of the column to be moved, or the column object itself. The new_index parameter is the new index for this column. This can be seen as the insertion point, or the column the moved column will be placed in front of. It can be specified as a column index, a column name, or a column object.
|
prettyprint(self, fp=None, center_columns=True, space_before=' ', space_after=' ', col_separator_char='|', row_separator_char='-', join_char='+', line_ending='\n', none='<N>', respect_filter=True)Pretty prints the table to the given fp. Note that the preferred way to print a table is to call "table.view()", which opens the table in the Picalo GUI if possible, or uses view() if in console mode. In other words, you should normally use view() rather than this method.
|
record(self, index, respect_filter=True)Retrieves the record at the given index. This is one of the most-used methods in the Picalo toolkit as it gives you access to records. In keeping with most computer languages, Picalo indices are always zero-based. This may require a slight adjustment for some users, but it makes mathematical calculations much easier and has other implications. This means that record 1 is table[0], record 2 is table[1], and so forth. Note that the shortcut way to access this method is the simple [n] notation, as in table[1] to access the second record. table.record() is rarely called as the shortcut is preferred instead. For advanced users: Index can also be a slice, as in table[2:5] to return a new Picalo table including only records 2, 3, and 4. See the Python documentation for more information on slices. The method respects any filters on the table by default. This can be overridden to ignore the filter. Example:
>>> table = Table([('id', int)], [[1],[2],[3],[4]])
>>> table2 = table[1]
>>> # table2 is now a Record object pointing at [2]
Example 2:
>>> table = Table([('id', int)], [[1],[2],[3],[4]]) >>> print table[1]['col000'] 2
|
record_count(self, respect_filter=True)Returns the number of records in this table. The method respects any active filters on the table by default.
|
reorder_columns(self, columns)Reorders the columns according to the given list. This is an alternative to move_column. If you know the exact order you want the columns in, use this method to explicitly set them. The columns parameter is a list giving the new order. Its items can be current column indices, names, or columb objects.
|
save(self, filename, respect_filter=False)Saves this table in native Picalo format. This is the preferred format to save tables in because all column types, formulas, and so forth are saved.
|
save_csv(self, filename, line_ending='\n', none='', encoding='utf-8', respect_filter=False)Saves this table to a Comma Separated Values (CSV) text file. CSV is an industry-standard way of transferring data between applications. This is the preferred way of exporting data from Picalo. Note that although this is the preferred export method, it has some limitations. These are limitations of the format rather than limitations of Picalo:
|
save_delimited(self, filename, delimiter=',', qualifier='"', line_ending='\n', none='', encoding='utf-8', respect_filter=False)Saves this table to a delimited text file. This method allows the specification of different types of delimiters and qualifiers. This method is for advanced users. Most users should call save_tsv, save_csv, or save_fixed to save using one of the accepted text formats. See these methods for more information.
|
save_excel(self, filename, none='', respect_filter=False)Saves this table to a Microsoft Excel 97+ file.
|
save_fixed(self, filename, line_ending='\n', none='', respect_filter=False)Saves this table to a fixed width text file. Fixed width files pad column records with extra spaces so they are easier to read. You should normally prefer CSV and TSV export formats to fixed as they have less limitations. Fixed format was widely used in the early days of computers, and some servers still use the format.
|
save_tsv(self, filename, line_ending='\n', none='', encoding='utf-8', respect_filter=False)Saves this table to a Tab Separated Values (TSV) text file. TSV is an industry-standard way of transferring data between applications. Note that although this is the preferred export method, it has some limitations. These are limitations of the format rather than limitations of Picalo:
|
save_xml(self, filename, line_ending='\n', indent='\t', compact=False, none='', respect_filter=False)Saves this table to an XML file using a pre-defined schema. If you need to save to a different schema, use the xml.dom.minidom class directly.
|
set_changed(self, changed)Sets whether the class has been changed since loading. This is not normally called by users. |
set_format(self, column, format=None)
Sets the format of this column. The format is used for printing the record
and showing the record in the Picalo GUI.
The format should be a Picalo expression that evaluates to a string. Use the
'record' variable for the record of the current cell.
Note that this is not an input mask. It doesn't affect the internal record of
the field records. It only affects how it is displayed on the screen.
Example:
# shows the current record in uppercase
table.set_format('Salary', "record.upper()")
@param column: The column name or index to set the type of
@type column: str
@param format: A Picalo expression that evaluates to a string
@type format: str
|
set_name(self, column, name)Changes the name of an existing column. The column name must be a valid Picalo name and must be unique to other column names in the table. |
set_readonly(self, readonly_flag=False)Sets the read only status of this table. Tables that are read only cannot be modified. Normally, tables are initially not read only (i.e. can be modified). The only exception is tables loaded from databases, which are read only.
|
set_type(self, column, column_type=None, format=None, expression=None)Sets the type of a column. The type must be a valid <type> object, such as int, float, str, unicode, DateTime, etc. All records in this column will be converted to this new type. The format is an optional format to be used for printing the record and showing the record in the Pialo GUI.
|
sort(self, cmp=None, key=None, reverse=False)Sorts this table with optional arguments.
|
structure(self)Returns the structure of this table, including column names, input and output types, and general statistics.
|
view(self)Opens a spreadsheet-view of the table if Picalo is being run in GUI mode. If Picalo is being run in console mode, it redirects to prettyprint(). This is the preferred way of viewing the data in a table. |
_add_listener(self, listener)Adds a listener to the table. Will be notified when data changes occur. The listener should be a callable/function of form callback(table). For efficiency reasons and because we don't need it right now, the col and row is not reported. |
_calculate_columns_map(self)Calculates the columns map based upon the current columns. This method should not be called externally. |
_check_page_size(self)Checks the current page size to see if we need to split it into two pages |
_get_filtered_index(self, index)Returns the filtered index of the given index, if a filter is in place. This method is not thread-safe. |
_invalidate_indexes(self)Invalidates the indices already calculated on this table. This occurs anytime data are modified in this table |
_notify_listeners(self, level=1)Notifies listeners that we've had a change |
_populate_record(self, rec, *a, **k)Populates a record with data. Examples:
You cannot mix formats in the same call. This method is used internall and should not be called by user code. |
_remove_listener(self, listener)Removes a listener from the table. |
_save_dialect(self, filename, dialect, none='', encoding='utf-8', respect_filter=False)Internal function to save according to a specific dialect (see the csv module docs) |
_set_cursor_datasource(self, cursor)Sets the given cursor as the datasource for this table. |
_swap_page(self, new_pageindex)Swaps the current page for a new one |
_swap_page_to_index(self, key)Swaps the current page to the one including the given record |
_update_filter_index(self)Updates the filter index. |
| Home | Trees | Index | Help |
|
|---|
| Generated by Epydoc 2.1 on Mon Aug 20 05:38:17 2007 | http://epydoc.sf.net |