thanosql.resources._table

Module Contents

Classes

BaseColumn

Column

Unique

PrimaryKey

ForeignKey

Constraints

BaseTable

TableObject

IfExists

Create a collection of name/value pairs.

TableService

Service layer for table methods.

Table

Extends the BaseTable class, which has name, schema,

TableTemplate

TableTemplateService

Service layer for table template methods.

class thanosql.resources._table.BaseColumn

Bases: thanosql.resources._model.BaseModel

default: str | None
is_nullable: bool | None = True
type: str
name: str
class thanosql.resources._table.Column

Bases: thanosql.resources._model.BaseModel

id: int | None
default: str | None
is_nullable: bool | None = True
type: str
name: str
class thanosql.resources._table.Unique

Bases: thanosql.resources._model.BaseModel

name: str | None
columns: List[str] | None = []
class thanosql.resources._table.PrimaryKey

Bases: thanosql.resources._model.BaseModel

name: str | None
columns: List[str] | None = []
class thanosql.resources._table.ForeignKey

Bases: thanosql.resources._model.BaseModel

name: str | None
reference_schema: str = 'public'
reference_column: str
reference_table: str
column: str
class thanosql.resources._table.Constraints

Bases: thanosql.resources._model.BaseModel

unique: List[Unique] | None
primary_key: PrimaryKey | None
foreign_keys: List[ForeignKey] | None
class thanosql.resources._table.BaseTable

Bases: thanosql.resources._model.BaseModel

name: str | None
table_schema: str | None
columns: List[BaseColumn] | None
constraints: Constraints | None
class thanosql.resources._table.TableObject

Bases: thanosql.resources._model.BaseModel

columns: List[BaseColumn] | None
constraints: Constraints | None
class thanosql.resources._table.IfExists(*args, **kwds)

Bases: enum.Enum

Create a collection of name/value pairs.

Example enumeration:

>>> class Color(Enum):
...     RED = 1
...     BLUE = 2
...     GREEN = 3

Access them by:

  • attribute access:

    >>> Color.RED
    <Color.RED: 1>
    
  • value lookup:

    >>> Color(1)
    <Color.RED: 1>
    
  • name lookup:

    >>> Color['RED']
    <Color.RED: 1>
    

Enumerations can be iterated over, and know how many members they have:

>>> len(Color)
3
>>> list(Color)
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

Methods can be added to enumerations, and members can have their own attributes – see the documentation for details.

FAIL = 'fail'
APPEND = 'append'
REPLACE = 'replace'
class thanosql.resources._table.TableService(client: thanosql._client.ThanoSQL)

Bases: thanosql._service.ThanoSQLService

Service layer for table methods.

client

The ThanoSQL client used to make requests to the engine.

Type:

ThanoSQL

template

The table template service layer to access methods involving table templates.

Type:

TableTemplateService

list(schema: str | None = None, verbose: bool | None = None, offset: int | None = None, limit: int | None = None) List[Table]

Lists tables stored in the workspace.

Parameters:
  • schema (str, optional) – The schema where the listed tables should reside in. If not set, all tables from all schemas will be included.

  • verbose (bool, optional) – Whether to include the table columns and constraints in the results. By default, or if set to False, only retrieves the names and schemas of stored tables.

  • offset (int, optional) – When set to n, skips the first n results and excludes them from the output list. Otherwise, starts the list from the first result stored. Must be greater than 0.

  • limit (int, optional) – When set to n, limits the number of results listed to n. Otherwise, lists up to 100 results per call. Must range between 0 to 100.

Returns:

A list of Table objects.

Return type:

List[Table]

Raises:

ThanoSQLValueError – If offset is less than 0 or if limit is not between 0 to 100 (inclusive).

get(name: str, schema: str | None = None) Table

Shows the details of the specified table.

Parameters:
  • name (str) – The name of the table to be retrieved.

  • schema (str, optional) – The schema where the table to be retrieved is in. If not specified, this method will look for the table in “public”.

Returns:

A Table object.

Return type:

Table

update(name: str, schema: str | None = None, table: BaseTable | None = None) Table

Updates the specified table.

Parameters:
  • name (str) – The name of the table to be updated.

  • schema (str, optional) – The schema where the table to be updated is in. If not specified, this method will look for the table in “public”.

  • table (BaseTable, optional) –

    BaseTable object containing changed details of the table to be updated. Any attribute of BaseTable can be modified, and if left unset, the current value will be maintained after update. The attributes are as follows: - name: new name to rename the table to - schema: new schema to move the table to - columns: new columns of the updated table. All new columns

    must be in this object, including columns that already exist in the original table. If this attribute is set but some original columns are not included, they will be removed from the table.

    • constraints: new constraints of the updated table. All new

      constraints must be in this object, including constraints that already exist in the original table. If this attribute is set but some original constraints are not included, they will be removed from the table.

Returns:

Table object of the new table after update.

Return type:

Table

Raises:

ThanoSQLValueError – If the table object contains invalid formatting.

create(name: str, table: TableObject, schema: str | None = None) Table

Creates a new table.

Parameters:
  • name (str) – The name of the table to be created.

  • table (TableObject) – TableObject containing the columns and constraints of the table to be created. In order to create an empty table, pass in an empty object (TableObject()).

  • schema (str, optional) – The schema to save the created table in. If not specified, the table will be saved to “public”.

Returns:

Table object of the created table.

Return type:

Table

Raises:

ThanoSQLValueError – If the table object contains invalid formatting.

upload(name: str, file: str | os.PathLike | None = None, df: pandas.DataFrame | None = None, schema: str | None = None, table: TableObject | None = None, if_exists: str = 'fail') Table

Uploads the contents of a CSV or Excel-like file or Pandas DataFrame into the specified table.

Either a CSV or Excel-like (.xls, .xlsx, .xlsm, .xlsb, .odf, .ods, .odt) file or DataFrame must be specified. However, both should not be used at the same time.

Parameters:
  • name (str) – The name of the table created from the file or DataFrame.

  • file (str or PathLike, optional) – CSV or Excel-like file containing tabulated data to be uploaded to the specified table.

  • df (DataFrame, optional) – Pandas DataFrame containing data to be uploaded to the specified table.

  • schema (str, optional) – The schema to save the created table in. If not specified, the table will be saved to “public”.

  • table (TableObject, optional) – TableObject containing the columns and constraints of the table to be created. If specified, the created table will follow the object format and no type inference is conducted. Otherwise, type inference will be performed and the table will be created to match the columns from source.

  • if_exists (str, default "fail") –

    What to do if table of the same name already exists. There are only three available values: - fail: fails (throws an error) if the same table exists - append: appends records into an existing table (columns must match

    in order to not make an error)

    • replace: deletes existing table and creates a new one with the

      given name

Returns:

Table object of the uploaded table.

Return type:

Table

Raises:

ThanoSQLValueError

  • If if_exists is not one of “fail”, “append”, or “replace”.

  • If neither file nor df is used, or if both are used at the same time.

  • If file is used but it is neither CSV nor Excel-like.

  • If the file or df contains badly-formatted contents.

  • If a table body is specified but does not match the contents of the file or df.

  • If if_exists is set to “append” but the new contents does not match the format of the existing table.

delete(name: str, schema: str | None = None) dict

Deletes the specified table.

Parameters:
  • name (str) – The name of the table to be deleted.

  • schema (str, optional) – The schema where the table to be deleted is in. If not specified, this method will look for the table in “public”.

Returns:

A dictionary containing a success message, table name, and schema in the format of:

{
    "message": "string",
    "table_name": "string",
    "schema": "string"
}

Return type:

dict

class thanosql.resources._table.Table

Bases: BaseTable

Extends the BaseTable class, which has name, schema, columns, and constraints as attributes, with a table service layer to allow connection to the ThanoSQL engine.

service: TableService | None

The table service layer to access the ThanoSQL client.

get_records(offset: int | None = None, limit: int | None = None) thanosql.resources._record.Records

Lists the records of the table.

Parameters:
  • offset (int, optional) – When set to n, skips the first n results and excludes them from the output list. Otherwise, starts the list from the first result stored. Must be greater than 0.

  • limit (int, optional) – When set to n, limits the number of results listed to n. Otherwise, lists up to 100 results per call. Must range between 0 to 100.

Returns:

A Records object.

Return type:

Records

Raises:

ThanoSQLValueError – If offset is less than 0 or if limit is not between 0 to 100 (inclusive).

get_records_as_csv(timezone_offset: int | None = None) None

Downloads the records of the table as a CSV file.

Parameters:

timezone_offset (int, optional) – Timezone offset from Coordinated Universal Time (UTC). If not set, this value is 9, following the timezone in Seoul. This value is used to determine the time used in the file name.

insert(records: List[dict]) Table

Inserts records to the specified table.

Parameters:

records (list of dict) – The records to be inserted in the format of a list of column-value pairs.

Returns:

A Table object.

Return type:

Table

Raises:

ThanoSQLValueError – If the records are in an invalid format or contain invalid contents.

class thanosql.resources._table.TableTemplate

Bases: thanosql.resources._model.BaseModel

name: str
table_template: TableObject
version: str | None
compatibility: str | None
created_at: datetime.datetime | None
class thanosql.resources._table.TableTemplateService(client: thanosql._client.ThanoSQL)

Bases: thanosql._service.ThanoSQLService

Service layer for table template methods.

client

The ThanoSQL client used to make requests to the engine.

Type:

ThanoSQL

list(search: str | None = None, order_by: str | None = None, latest: bool | None = None) List[TableTemplate]

Lists table templates in the workspace.

Parameters:
  • search (str, optional) – Search keywords that the table template names in the results must contain. If not set, all table templates are returned by default.

  • order_by (str, optional) – How to order the results. There are only three possible values: - recent: based on the date of creation, from most recent to oldest - name_asc: based on the name of the template, from A to Z - name_desc: based on the name of the template, from Z to A

  • latest (bool, optional) – Whether to return only the latest version of each table template. By default, or if set to False, all versions of table templates are included in the results.

Returns:

A list of TableTemplate objects.

Return type:

List[TableTemplate]

Raises:

ThanoSQLValueError – If order_by is not one of “recent”, “name_asc”, or “name_desc”.

get(name: str, version: str | None = None) dict

Shows the details of the specified table template.

Parameters:
  • name (str) – The name of the table template to be retrieved.

  • version (str, optional) – The version of the table template to be retrieved. The value can either be a specific version such as “1.0”, or “latest”. If “latest” is specified, only the latest version of the table template will be shown. If version is not set, all versions will be shown.

Returns:

A dictionary of matching table template(s) in the format of:

{
    "table_templates": ["TableTemplate"],
    "versions": ["string"]
}

Return type:

dict

create(name: str, table_template: TableObject, version: str | None = None, compatibility: str | None = None) TableTemplate

Creates a new table template.

Parameters:
  • name (str) – The name of the table template to be created.

  • table_template (TableObject) – TableObject containing the columns and constraints of the table template to be created. In order to create an empty table template, pass in an empty object (TableObject()).

  • version (str, optional) – The version of the table template to be created. It must be in the format of “[1-9].[0-9]”. If not set, it will default to “1.0”.

  • compatibility (str, optional) – The compatibility setting of the table template to be created. If not set, it will default to “ignore” (no compatibility checks).

Returns:

TableTemplate object of the created table template.

Return type:

TableTemplate

Raises:

ThanoSQLValueError

  • If the template name contains invalid characters or is too long.

  • If version is specified but is not in the right format.

  • If the table template contains formatting errors.

delete(name: str, version: str | None = None) dict

Deletes the specified table template.

Parameters:
  • name (str) – The name of the table template to be removed.

  • version (str, optional) – The version of the table template to be removed. If not specified, all versions of the table template will be removed.

Returns:

A dictionary containing a success message and the name of the table template in the format of:

{
    "message": "string",
    "table_template_name": "string"
}

Return type:

dict