Skip to main content

King Arthur's finest collection of table-like containers

Project description

King Arthur’s finest collection of table-like containers [for Python]

Table

Think of this as a list of namedtuples, except the elements of the namedtuples can be edited. The table is initialized with a list of column header names. Appending to the table adds new rows. Rows are created using a list (positional) or dict (column names). Unspecified column cells default to None.

Other list methods (insert, pop, remove, extend, reverse) work as expected. sort allows a string key to indicate a sort-by-column. Search methods (index, count, __contains__) accept either a Row object or a list or dict.

Additional methods useful for tables:

  • take(indexes_or_func) returns a new table based on passed in indexes Alternatively, applies a function to every row in the table and builds a new table out of rows that evaluate to True

  • column(colname) returns an iterator over values in a column

LookupTable

A lookup function or column is added to a basic Table. The lookup function is similar to a hash function, taking a Row object and returning a value. If a column is provided, the value in that column is the lookup value. A dict keeps track of these “lookup” values, allowing fast lookups on all seach methods (index, count, __contains__). These search methods accept either a Row object (which is run through the lookup function) or the lookup value. The lookup values do not need to be unique.

Example

from datetime import datetime, date, timedelta
from roundtable import Table
# Create empty table with column headers
tbl = Table(['Timestamp', 'Event', 'Root Cause', 'Due Date'])
# Add rows to the table
tbl.append((datetime(2013,1,2,12,30), 'Error code 129',
            'Short on board', date(2013,1,8)))
tbl.append({'Event': 'Pairwise testing',
            'Due Date': date(2013,1,7)}) # other columns default to None
# Build a sorted list of tasks due in the next week
task_list = tbl.take(lambda row: row['Due Date'] - date.today() < timedelta(days=7))
task_list.sort(col='Due Date', reverse=True)

Table access methods:

Code

Return Type

mytable[0]

Row object

mytable[-1]

Row object

mytable[0:10:2]

new Table object

mytable[0][0]

value in cell

mytable[0][-1]

value in cell

mytable[0, 0]

value in cell

mytable[0]['a column']

value in cell

mytable[0, 'a column']

value in cell

mytable[0].Col2

value in cell*

mytable.column(1)

iterator over values in column

mytable.column('a column')

iterator over values in column

* As a special case, if a column name is a valid Python variable name and starts with a capital letter then an attribute will be added to the Row object allowing access. The requirement to start with a capital letter avoids conflicts with other attributes and functions of the Row object.

Table object can be transformed into other table-like object if modules are available:

  • NumPy Array : mytable.as_array()

  • Pandas DataFrame: mytable.as_dataframe()

Note

Works for Python 2.6+, 3.1+

No dependencies except when using the as_array() or as_dataframe() methods.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

roundtable-0.3.zip (10.5 kB view details)

Uploaded Source

roundtable-0.3.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

roundtable-0.3.win32.exe (210.4 kB view details)

Uploaded Source

File details

Details for the file roundtable-0.3.zip.

File metadata

  • Download URL: roundtable-0.3.zip
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for roundtable-0.3.zip
Algorithm Hash digest
SHA256 b14fc18a8e59d75d8e5140cff288a0b566a0f5efca41d9533f365faec715caee
MD5 eb6ac0ef454cfd4d06d0e8daceea1ecc
BLAKE2b-256 d76c59f3b0a387301db51e747c12649faae72d57516f1b1d5b0a216d75343990

See more details on using hashes here.

File details

Details for the file roundtable-0.3.tar.gz.

File metadata

  • Download URL: roundtable-0.3.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for roundtable-0.3.tar.gz
Algorithm Hash digest
SHA256 3105261d47ca30d4de61e2ac5b123ba388b951d0d466d3ea4f1b8fd6210c9575
MD5 79a88acc08456d26e982dc211187d999
BLAKE2b-256 51fbc8ad7a7744c76aba04cb284c2d8b17a0096971c2f819bd176b6be34daf7d

See more details on using hashes here.

File details

Details for the file roundtable-0.3.win32.exe.

File metadata

  • Download URL: roundtable-0.3.win32.exe
  • Upload date:
  • Size: 210.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for roundtable-0.3.win32.exe
Algorithm Hash digest
SHA256 5bdd841ed03b41863e596fd6991623303c8eebd535b8bbef444c9273e12ba5bb
MD5 725a5f9825cea5f42bfd0aeb275da35d
BLAKE2b-256 829a38665c4df7fc9f40d93b1136dd40ac86ab6a4987d0be6bf7b5de96ff8a5a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page