Skip to content

Python Client

Kyle Hanson edited this page Feb 27, 2022 · 11 revisions

Details

CrissCrossPy is a very thin wrapper around the Redis client. It uses elixir_py to encode between Python and BERT format automatically.

For a full list of commands available see https://github.com/SoCal-Software-Labs/CrissCross/wiki/Redis-API

It is important to always use the raw binary forms of clusters, trees and other values, not their Base58 representation.

Basic Usage

pip install crisscross-py
>>> client = CrissCross(host=host, port=port, username=username, password=password)
>>> client = CrissCross()
>>> location = client.put_multi("", [(("wow", 1.2), {1: (True, None)})])
>>> client.get_multi(location, [("wow", 1.2)])
{1: (True, None)}
>> new_location = client.put_multi(location, [("cool", 12345)], ttl=1000 * 60 * 60 * 60 * 24 * 30)
>> client.get(new_location, "cool")
12345

Files

Upload and download python file objects (Or BytesIO, etc)

with open("somefile.txt", "b") as f:
    tree = r.upload(f)

with open("mypath.txt", "wb") as f:
    r.download(tree, f)

tree = r.upload_dir("", "./my/dir")
r.download_dir(tree, "./other_dir")

Remote

To connect to a remote cluster prefix read commands with remote, you can optionally specify num for the number of peers to use and/or cache of whether or not to write to and use your local data.

client.remote_get_multi(cluster, tree, ["hello"])
client.remote_sql_read(cluster, tree, ["CREATE TABLE MyCrissCrossTable (id INTEGER);"])

See the Redis Docs for more information about what commands are allowed.

Using Variables

client.var_set("myvar", tree)
client.var_get("myvar")

Use the prefix var_with on commands to have the var be replaced with the tree in the command.

client.get_multi("^myvar", ["hello"])
client.put_multi("^myvar", ["hello", 123)])

Sql

Execute SQL write statements with sql to get a new hash back and SQL read statements with sql_read to not modify the tree.

>> client = CrissCross()
>>> location, ret = client.sql("", "CREATE TABLE MyCrissCrossTable (id INTEGER);")
>>> print(ret) # Get the result of the execution
[(Atom(b'ok'), b'Create')]
>>> location2, _ = client.sql(location, "INSERT INTO MyCrissCrossTable VALUES (100);")
>>> location3, _ = client.sql(location2, "INSERT INTO MyCrissCrossTable VALUES (200);")
# When reading from a table, use the read variant to avoid setting TTL on nodes
>>> ret = client.sql_read(location3, "SELECT * FROM MyCrissCrossTable WHERE id > 100;")
>>> print(ret[0][1])
{b'Select': {b'labels': [b'id'], b'rows': [[{b'I64': 200}]]}}

Execute many statements at once:

>>> location, sqlreturns = client.sql("", "CREATE TABLE MyCrissCrossTable (id INTEGER);", "INSERT INTO MyCrissCrossTable VALUES (100);", "INSERT INTO MyCrissCrossTable VALUES (200);", "SELECT * FROM MyCrissCrossTable WHERE id > 100;")
>>> print(sqlreturns)
[(Atom(b'ok'), b'Create'), (Atom(b'ok'), {b'Insert': 1}), (Atom(b'ok'), {b'Insert': 1}), (Atom(b'ok'), {b'Select': {b'labels': [b'id'], b'rows': [[{b'I64': 200}]]}})]

Clone this wiki locally