Skip to content

Instantly share code, notes, and snippets.

View thejchap's full-sized avatar

justin thejchap

View GitHub Profile
@thejchap
thejchap / pydantic_examples.py
Created September 4, 2025 16:59
pydantic_examples.py
# /// script
# dependencies = ["pydantic"]
# ///
from typing import Self
from pydantic import BaseModel, computed_field
class A(BaseModel):
@thejchap
thejchap / nginx.conf
Last active January 4, 2025 21:21
jchap.me blogging platform
events {}
http {
access_log /usr/local/openresty/nginx/logs/access.log;
error_log /usr/local/openresty/nginx/logs/error.log;
lua_shared_dict posts 1m;
init_by_lua_block {
local lfs = require"lfs"
local markdown = require "markdown"
local posts_root = "/usr/local/openresty/nginx/html/templates/posts"
for file in lfs.dir(posts_root) do
@thejchap
thejchap / tinypk.py
Last active September 18, 2023 01:24
tiny smol primary keys
"""
utility for creating tiny primary keys
with a prefix (think type info)
keys are 32 bit integers:
- 5 bits: single-character prefix
- 27 bits: random
~134 million keys per prefix
for example:
U93152957 (user) or P42163488 (post)
"""
@thejchap
thejchap / matching_engine.py
Created January 1, 2021 00:21
matching_engine.py
from __future__ import annotations
from typing import Optional, NamedTuple, List
from dataclasses import dataclass, field
from enum import Enum
class Order(NamedTuple):
"""
public limit order interface
"""
@thejchap
thejchap / jdb_transaction.py
Last active October 8, 2020 15:31
jdb_transaction.py
from jdb.db import DB
db = DB()
with db.transaction() as txn:
 txn.read(b'foo')
 txn.write(b'bar', b'baz')
@thejchap
thejchap / misort.py
Last active June 13, 2020 19:01
misort.py
"""
https://en.wikipedia.org/wiki/Merge-insertion_sort
"""
from typing import List, Callable, Tuple
from bisect import bisect
# max of pair, min of pair
Pair = Tuple[int, int]
@thejchap
thejchap / rule90.py
Created November 26, 2019 18:44
Rule 90 Implementation
#!/usr/bin/env python3
"""
rule 90 implementation
"""
def rule90(starting):
grid, m = [starting], len(starting)
for i in range(1, m):
@thejchap
thejchap / checkout.js
Created August 22, 2018 21:52
checkout
export default Component.extend({
didInsertElement() {
document.addEventListener('message', this._handleMessage.bind(this));
},
willDestroyElement() {
document.removeEventListener('message', this._handleMessage.bind(this));
}
_handleMessage(message) {
@thejchap
thejchap / cardreadermanager.swift
Created August 22, 2018 21:38
cardreadermanager.swift
@objc(CardReaderManager)
class CardReaderManager: RCTEventEmitter {
func cardDataReceived(data: Dictionary<String, String>, sender: AnyObject) {
StripeUtil.createToken(data) { (token, error) in
self.sendEvent(
withName: "stripeToken",
body: token
)
}
}
@thejchap
thejchap / checkout.js
Last active August 22, 2018 21:34
Checkout
class Checkout extends Component {
constructor(props) {
super(props);
this.subscriptions = [];
}
componentDidMount() {
const { cardReader: { emitter } } = global;