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.1rc3.tar.gz (16.6 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.1rc3-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file tableland-0.0.1rc3.tar.gz.

File metadata

  • Download URL: tableland-0.0.1rc3.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.8 Darwin/23.3.0

File hashes

Hashes for tableland-0.0.1rc3.tar.gz
Algorithm Hash digest
SHA256 c6b5fe4a965a767bb207f2d2633a9c70daafd1bc1921ea625243d57a179b61bc
MD5 2e32a3c68ddee3b34cea0a0f1cf8b40d
BLAKE2b-256 2ce2180edb96ade2d55a992819a04cc476dd453b7495ce3e2b61d13a6f606861

See more details on using hashes here.

File details

Details for the file tableland-0.0.1rc3-py3-none-any.whl.

File metadata

  • Download URL: tableland-0.0.1rc3-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.11.8 Darwin/23.3.0

File hashes

Hashes for tableland-0.0.1rc3-py3-none-any.whl
Algorithm Hash digest
SHA256 ba672cfc2029f8ab12df00fd6681666a2fb12bfd1d735fe3ac83f6eb77761bdd
MD5 86228bcea6248e78d3cc23cdcda91c3d
BLAKE2b-256 28734c25747d3ccdddc832c2cb85c75bb1a8b6a9502d9a93bc8473f0c9444a5f

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