Skip to content

Instantly share code, notes, and snippets.

@harryscholes
harryscholes / asyncio_grpc_client.py
Created April 13, 2022 12:51
Python asyncio gRPC client
import asyncio
import grpc
from recommender_pb2_grpc import RecommenderStub
from recommender_pb2 import RecommendRequest
async def run() -> None:
async with grpc.aio.insecure_channel('localhost:50051') as channel:
client = RecommenderStub(channel)
@harryscholes
harryscholes / sem.py
Created April 11, 2022 18:15
Python asyncio with semaphore
import asyncio
async def foo(sem, i):
async with sem:
print("acquired", i)
await asyncio.sleep(1)
print("released", i)
@harryscholes
harryscholes / terra.graphql
Last active November 25, 2021 10:30
Terra graphql query for tx log event KV pairs
{
tx {
byHeight(height: 6708762) {
tx {
body {
messages
}
}
logs {
events {
@harryscholes
harryscholes / clone.rs
Created November 5, 2021 09:44
Rust: how to clone a type that is not Clone
use std::sync::Arc;
struct Foo();
fn main() {
let x = Foo();
// x.clone(); // `Foo` is not `Clone`
let x = Arc::new(x); // Wrap in `Arc`
x.clone(); // Works because `Arc` is `Clone`
@harryscholes
harryscholes / option.rs
Created November 4, 2021 15:35
Rust custom Option type
enum MyOption<T> {
MySome(T),
MyNone,
}
use MyOption::{MyNone, MySome};
fn main() {
let mut x = MySome(1i32);
x = MyNone;
@harryscholes
harryscholes / price_holder.rs
Last active October 31, 2021 19:59
Rust threadsafe hashmap with notification of state changes via the observer pattern
use std::collections::HashMap;
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
use std::sync::{Arc, Mutex};
use num::Unsigned;
struct Price<T> {
value: Option<T>,
waiters: Option<Vec<SyncSender<T>>>,
}
@harryscholes
harryscholes / terracli_config.sh
Created September 13, 2021 08:20
terracli node config
terracli config node http://public-node.terra.dev:26657
terracli config chain-id columbus-4
terracli config trust-node false
@harryscholes
harryscholes / lib.rs
Last active August 11, 2021 15:50
Thread safe price holder in Rust
use std::{
collections::HashMap,
sync::{
mpsc::{self, RecvError, Sender},
Arc, Mutex,
},
};
struct Price {
value: Option<u128>,
@harryscholes
harryscholes / error_conversion.rs
Last active July 24, 2021 08:50
Rust automatic error conversion
use thiserror::Error;
#[derive(Debug, Error)]
enum A {
#[error("")]
ErrorA {},
}
#[derive(Debug, Error)]
enum B {
@harryscholes
harryscholes / terra_core_apple_silicon.sh
Created July 19, 2021 15:34
Install terra core on Apple Silicon
# On an Apple Silicon computer, install Rosetta 2 and the amd64 version of Go, then
arch -x86_64 make install