PortableTab.capnp_table module

class PortableTab.capnp_table.CapnpTable(tablename: str, db_dir=None)

Bases: CapnpManager

Class that represents a table containing Capnp records.

Parameters:
  • tablename (str) – Name of the table.

  • db_dir (Path-like, optional) – Base directory where the schema and tables are placed.

Notes

  • “db_dir” specifies the base directory where other tables will also be placed. Tables are placed in subdirectories with their tablename under the base directory.

append_records(records: Iterable[dict]) None

Appends a set of record to the end of the table.

Paramaters

records: Iterable[dict]

Iterable that returns the records in order.

count_records() int

Count the number of records in the table.

Returns:

The number of records.

Return type:

int

Notes

  • This method actually just reads the configuration file.

create(capnp_schema: str, record_type: str) Path

Create table.

Parameters:
  • capnp_schema (str) – Record definition written in Capnp schema format.

  • record_type (str) – Type name of the record (struct) defined in the schema.

Returns:

Directory path where the created tables and schema will be stored.

Return type:

Path

Notes

  • Even if multiple structure types are defined in the schema, the type passed as record_type is used as the record type of this table.

create_trie_on(attr: str, key_func: Callable | None = None, filter_func: Callable | None = None) None

Create TRIE index on the specified attribute.

Paramters

attr: str

The name of target attribute.

key_func: Callable, optional

A function that takes the attribute value as argument used to generate a set of index key strings from each record. If not specified, the ‘str’ function will be used.

filter_func: Callable, optional

A function that takes the record as argument to determine if the record should be added to the index or not. When the function returns true (in the context of Boolean operation), the record will be indexed. If not specified, all records will be indexed.

Notes

  • The created index is saved in the same directory as the page files with the file name “<attr>.trie”.

delete() None

Delete the table.

Notes

  • This method actually deletes the subtree containing the table.

delete_trie_on(attr: str)

Delete TRIE index on the specified attribute.

Paramters

attr: str

The name of target attribute.

Notes

  • Delete any file named “<attr>.trie” in the same directory as the page files.

  • If the index is already loaded, unload it.

get_config() Path

Get the contents of the config file of the table.

get_dir() Path

Get the directory where the table are placed.

get_list_type() Any

Get the list type.

Returns:

Capnp type name corresponding to a list of records.

Return type:

Any

Notes

  • The type is defined by the schema file name.

get_record(pos: int, as_dict: bool = False) Any

Get a record from the table at pos.

Parameters:
  • pos (int) – Position of the target record.

  • as_dict (bool [False]) – Specifies whether records are returned in dict format.

Returns:

When “as_dict” is set to True, it returns a dict object. Otherwise, it returns a record_type object.

Return type:

Any

get_record_type() Any

Get the record type.

Returns:

Capnp type name corresponding to a record.

Return type:

Any

Notes

  • The type is defined by the schema file name.

open_trie_on(attr: str) RecordTrie

Open TRIE index on the specified attribute.

Paramters

attr: str

The name of target attribute.

returns:

The TRIE index.

rtype:

RecordTrie

Notes

  • The index is mmapped from the file name “<attr>.trie”, in the same directory as the page files.

retrieve_records(limit: int | None = None, offset: int | None = None, as_dict: bool = False) Iterator[Any]

Get a iterator that retrieves records from the table.

Paramaters

limit: int, optional

Max number of records to be retrieved. If omitted, all records are retrieved.

offset: int, optional

Specifies the number of records to be retrieved from. If omitted, the retrieval is performed from the beginning.

as_dict: bool [False]

Specifies whether records are returned in dict format.

returns:

When “as_dict” is set to True, it returns a iterator of dict. Otherwise, it returns a iterator of record_type object.

rtype:

Iterator[Any]

search_records_on(attr: str, value: str, funcname: str = 'get') list

Search value from the table on the specified attribute.

Paramters

attr: str

The name of target attribute.

value: str

The target value.

funcname: str

The name of search method. - “get” searches for records that exactly match the value. - “prefixes” searches for records that contained in the value. - “keys” searches for records that containing the value.

returns:

List of records.

rtype:

List[Record]

Notes

  • TRIE index must be created on the column before searching.

  • The TRIE index file will be automatically opened if it exists.

set_config(config: dict) None

Set the contents of the config file of the table.

Notes

  • This method overwrite the config file completery.

unload() None

Release loaded resources manually, including the capnp module corresponding to this table.

update_records(updates: dict) bool

Updates records in the table that has already been output to a file.

Paramaters

updates: dict

A dict whose keys are the positions of records to be updated and whose values are the contents to be updated.

The format of the values are a dict of field name/value pairs to be updated.

Examples

>>> table.update_records({
    15: {
            "name": "Bernardo de la Paz",
            "job": "Professor",
    },
})

Notes

  • This process is very slow and should not be called if possible.