Skip to content

Instantly share code, notes, and snippets.

View mhconradt's full-sized avatar
🎯
Locked in

Max mhconradt

🎯
Locked in
  • Codebased
  • San Francisco, CA
  • 14:27 (UTC -08:00)
View GitHub Profile
@mhconradt
mhconradt / pprint.py
Last active September 17, 2024 03:24
Tiktoken Pretty Printer
import textwrap
import tiktoken
from colorama import Back
def pprint(enc: tiktoken.Encoding, text: str) -> None:
tokens = enc.encode(text)
colors = [Back.CYAN, Back.GREEN, Back.LIGHTRED_EX, Back.RED, Back.BLUE]
current = []
i, j = 0, 0
@mhconradt
mhconradt / dictionary.py
Created May 2, 2022 00:10
PyArrow High Cardinality Dictionary Example
from string import ascii_uppercase
import pandas as pd
import pyarrow as pa
strings = pd.Categorical([a + b for a in ascii_uppercase for b in
ascii_uppercase])
print(strings)
"""
@mhconradt
mhconradt / ml.py
Created January 21, 2022 20:27
Fun Functions for Preparing Datasets for ML
from typing import Tuple
import numpy as np
import polars as pl
from numba import njit
from pyarrow import fs
pl.toggle_string_cache(True)
PERPETUALS = {'ALT-PERP', 'HUM-PERP', 'BSV-PERP', 'BNB-PERP', 'LEO-PERP',
@mhconradt
mhconradt / brute_force.py
Last active January 13, 2022 05:11
Clickhouse Arrow Stream
import subprocess
from io import BytesIO
import pyarrow as pa
def append_format(query: str) -> str:
sans_semicolon = query.rstrip(" ;") # trim spaces + semi-colons
return sans_semicolon + " FORMAT ArrowStream;"
@mhconradt
mhconradt / switch.c
Created January 2, 2022 00:30
Lox Switch Statement
static void switchStatement() {
consume(TOKEN_LEFT_PAREN, "Expect '(' after 'switch'.");
expression();
consume(TOKEN_RIGHT_PAREN, "Expect ')' after expression.");
consume(TOKEN_LEFT_BRACE, "Expect '{' before cases.");
int jumpAfter = -1;
int jumpBefore = -1;
@mhconradt
mhconradt / ema.py
Created December 23, 2021 22:45
Candles EMA Polars
import polars as pl
from pyarrow import fs
trades = pl.read_parquet('crypto-exchange-pds/ftx-trades/',
filesystem=fs.S3FileSystem(),
filters=[[('ds', '>=', '2021-10-24'), ('ds', '<=', '2021-10-31')]],
columns=['market', 'time', 'id', 'price', 'size'])
trades['market'] = trades.market.cast(pl.datatypes.Categorical)
@mhconradt
mhconradt / cat.py
Last active December 7, 2021 22:37
Attempting to write pl.Categorical to Parquet
from string import ascii_uppercase
import numpy as np
import polars as pl
N = 5 # N > 4 to break to_parquet/read_parquet
# for smaller N, categories need to be multiple characters
cats = [a + b for a in ascii_uppercase for b in ascii_uppercase][:N]
@mhconradt
mhconradt / polygon.lox
Created December 5, 2021 00:03
Polygon + Tessellation in Lox
class Point { init(x, y) { this.x = x; this.y = y; } }
class LinkedList {
init(item, tail) {
this.item = item;
this.tail = tail;
}
class prepend(item, list) {
return LinkedList(item, list);
@mhconradt
mhconradt / sym_diff.py
Created November 22, 2021 18:48
Symmetric Difference
def symmetric_difference(a: set, b: set) -> set:
return {elt for elt in a | b if (elt in a) ^ (elt in b)}
symmetric_difference({'bread', 'butter'}, {'bread', 'jam'})
# {'butter', 'jam'}
{'bread', 'butter'} ^ {'bread', 'jam'}
# {'butter', 'jam'}
@mhconradt
mhconradt / set_lt.py
Created November 22, 2021 18:46
Set <
{'bread'} < {'bread', 'butter'}
# True
{'bread'} < {'bread'}
# False