Types and casting
TinyChain's Python client uses type declarations in an unusual way. Here's what to expect.
import os
import tinychain as tc
# use the demo host at demo.tinychain.net, unless overridden by the environment variable `TC_HOST`
HOST = tc.host.Host(os.getenv("TC_HOST", "http://demo.tinychain.net"))
# this endpoint will attempt to resolve whatever state you send it, without committing any changes
ENDPOINT = "/transact/hypothetical"
# this assumes that `x` is of type `tc.tensor.Tensor`
def average(x):
return x.sum() / x.size
if __name__ == "__main__":
cxt = tc.Context() # initialize a new Op context
cxt.x = tc.tensor.Dense.ones([3]) # initialize a new Dense tensor
cxt.result = average(cxt.x) # call our custom function
actual = HOST.post(ENDPOINT, cxt) # execute the `Op` defined by `cxt`
assert actual == 1 # verify the resultLast updated
Was this helpful?