Skip to main content

A minimal Tableland Python SDK for creating, writing, and reading onchain tables

Project description

tableland.py

License: MIT AND Apache-2.0 standard-readme compliant PyPI version

A minimal Tableland Python SDK for creating, writing, and reading onchain tables

Background

This package is a simple Python SDK for the Tableland network. It's built around the web3.py library for onchain interactions and lets developers create, read, and write data to Tableland tables.

Install

You can install with pip:

pip install tableland

With pipx:

pipx install tableland

Or with poetry:

poetry add tableland

Usage

Start by importing the Database class and creating a new instance with an RPC provider for your desired chain and private key. You can then create a new table, write data to the table, and read data from the table.

from tableland import Database

private_key = "59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"  # Replace with your private key
db = Database(
    private_key=private_key,
    provider_uri="http://localhost:8545",  # Replace with your chain RPC provider URL
)

# Create a new table
statement = "create table my_table (id int, val text)"
create_event = db.create(statement)
table_name = create_event["table_name"]
table_id = create_event["table_id"]

# Check if there are any errors
if create_event["error"] is not None:
    print(f"Error: {create_event['error']}")

# Insert a row into the table
statement = f"insert into {table_name} (id, val) values (1, 'hello')"
write_event = db.write(statement)

# Check if there are any errors
if write_event["error"] is not None:
    print(f"Error: {write_event['error']}")

# Query the table
statement = f"select * from {table_name}"
data = db.read(statement)
print(data)
# [{'id': 1, 'val': 'hello'}]

The Database class also has a few other methods, including getting the transaction receipt for create or write events, getting the table's owner, signer's address, or various table information:

# Alternatively, manually get validator transaction receipt vs. checking `write_event["error"]` value
statement = f"insert into {table_name} (id, val) values (1, 'hello', 'an erroneous value')"
write_event = db.write(statement)
tx_hash = write_event["transaction_hash"]
receipt = db.get_receipt(tx_hash)
if receipt["error"] is not None:
    print(f"Error: {write_event['error']}")

# Get the chain ID
chain_id = db.get_chain_id()
# 31337

# Get table info
owner = db.get_owner(table_id)
print(owner)
# 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266

# Get table info
schema = db.get_table_info(table_id)["schema"]["columns"]
print(schema)
# [{"name": "id", "type": "int"}, {"name": "val", "type": "text}]

# Get the signer address
address = db.get_signer_address()
print(address)
# 0x70997970C51812dc3A010C7d01b50e0d17dc79C8

There are also a few helper functions, including getting table information, Tableland registry contract addresses, or validator URL information:

from tableland import get_registry_address, get_table_parts_from_name, get_validator_base_uri

# Get Tableland registry contract address
registry_address = get_registry_address(chain_id)
print(registry_address)
# 0xe7f1725e7734ce288f8367e1bb143e90bb3f0512

# Get the table prefix, chain ID, and table ID from a table name
parts = get_table_parts_from_name(table_name)
print(parts)
# {"prefix": "my_table", "chain_id": 31337, "table_id": 2}

# Get validator base URI for a given chain
chain_id = 31337  # Replace with your chain ID
base_uri = get_validator_base_uri(chain_id)
print(base_uri)
# http://localhost:8080/api/v1/

# Get the validator's poling settings
config = get_validator_polling_config(31337)
print(config)
# {"timeout": 5000, "interval": 1_500}

Development

This project uses poetry for dependency management. Make sure pipx is installed (e.g., brew install pipx on Mac) and then install poetry with pipx install poetry.

Once that's set up, you can install the project dependencies and run various tasks via poe or poetry run. You can view all of the available tasks by running poetry run poe --help or reviewing the [tool.poe.tasks] of the pyproject.toml file.

# Install dependencies
poetry install

# Setup pre-commit and pre-push hooks
poetry poe pre-commit

# Run linters
poetry poe lint

# Run tests
poetry poe test
poetry poe coverage
# Or
poetry run pytest

# Build the package
poetry build

Note: if you're using a Mac M1/M2 and the default poetry settings, you might run into issues with respect to the Python virtual environment's cache directory defined in the global poetry configuration. Check the path by running poetry config --list and looking at the cache-dir value. If that's the case, running poetry config cache-dir "$HOME/.local/share/virtualenvs" should fix it (i.e., the previous value is $HOME/Library/Caches/pypoetry). Alternatively, setting the virtualenvs.in-project config to true will use a local .venv instead.

Contributing

PRs accepted.

This package was created with Cookiecutter and the sourcery.ai project template. Small note: If editing the README, please conform to the standard-readme specification.

License

MIT AND Apache-2.0, © 2021-2024 Tableland Network Contributors

Project details


Download files

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

Source Distribution

tableland-0.0.2.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tableland-0.0.2-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file tableland-0.0.2.tar.gz.

File metadata

  • Download URL: tableland-0.0.2.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.1 Linux/6.5.0-1016-azure

File hashes

Hashes for tableland-0.0.2.tar.gz
Algorithm Hash digest
SHA256 295bff178f7e7f398a5d28393998d3ccb1946392c5b8627f622d08a54b694635
MD5 936b24e63f302c0ba29365976e6c59b4
BLAKE2b-256 4010f2ea27341c035e32aeb967b30a08e39463021721a06687d3c3d48e18ec69

See more details on using hashes here.

File details

Details for the file tableland-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: tableland-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.1 Linux/6.5.0-1016-azure

File hashes

Hashes for tableland-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9ac85ad64d762b9d235293b281f292cf4843a2c74f54881570ce7cc1f7143a7e
MD5 bf346e0a146e3834ff49e4814a2e8177
BLAKE2b-256 5889f6f3a980d99420d0962c4d3faf4fb79094ceced8848bb462d1d53642d832

See more details on using hashes here.

Supported by

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