Skip to content

Instantly share code, notes, and snippets.

@tianhuil
tianhuil / README.md
Last active October 22, 2024 14:29
Migra + Supabase

Motivation

Supabase migrations are imperative. We want to declaratively write DB definitions declaratively and generate migrations as diffs. This tool accomplishes this.

Tool

This is a basic script to diff a set of schemas to generate new migrations for a supabase project. The expected file layout is

- supabase/baseline/baseline.sql
- supabase/migrations/[timestamp]_[name].sql
- supabase/schema/[order]_[name].sql
@tianhuil
tianhuil / settings.json
Last active June 26, 2024 03:26
Python Template
{
// Common
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit",
"source.organizeImports": "explicit",
},
// Python
"python.analysis.typeCheckingMode": "basic",
"files.exclude": {
@tianhuil
tianhuil / m1-deploy-heroku.md
Created May 20, 2023 18:43
Deploying to Heroku with a M1 Mac

From https://hackmd.io/@nirvana/deploy_python_m1

Build image with linux platform for heroku servers. Replace {NAME_OF_HEROKU_APP} with your own tag:

docker buildx build --platform linux/amd64 -t {NAME_OF_HEROKU_APP} .

Tag your app with the url for your apps registry. make sure to use the name of your Heroku app in the url and tag name:

docker tag {NAME_OF_HEROKU_APP} registry.heroku.com/{NAME_OF_HEROKU_APP}/web
@tianhuil
tianhuil / typeAssertions.ts
Created March 15, 2022 18:58
Type Assertions in typescript
// Type assertions, before I discovered https://www.npmjs.com/package/type-assertions
type AssertExtends<A, B> = A extends B ? true : false
export const assertExtends = <A, B>(_: AssertExtends<A, B>): void => {}
export const assertInstance = <B>(
a: any,
_: AssertExtends<typeof a, B>
): void => {}
assertExtends<'abc', string>(true)
@tianhuil
tianhuil / poetry_init.sh
Last active November 25, 2021 04:53
Poetry Install
# Poetry needs to be installed globally
poetry init
poetry add \
matplotlib \
pandas \
wordcloud \
tqdm \
python-dotenv \
@tianhuil
tianhuil / dnsovertls.md
Created September 20, 2021 13:11 — forked from uraimo/dnsovertls.md
Configure your Mac to use DNS over TLS
```
$ git push server main
+ export DOKKU_HOST_ROOT=/home/dokku
+ DOKKU_HOST_ROOT=/home/dokku
+ export DOKKU_DISTRO
++ . /etc/os-release
++ echo ubuntu
+ DOKKU_DISTRO=ubuntu
+ export DOCKER_BIN=docker
+ DOCKER_BIN=docker
@tianhuil
tianhuil / starbucks_us_locations.csv
Created June 10, 2020 01:16 — forked from dankohn/starbucks_us_locations.csv
8902 locations of US Starbucks with addresses, latitude, and longitude
We can't make this file beautiful and searchable because it's too large.
-149.8935557,61.21759217,Starbucks - AK - Anchorage 00001,"601 West Street_601 West 5th Avenue_Anchorage, Alaska 99501_907-277-2477"
-149.9054948,61.19533942,Starbucks - AK - Anchorage 00002,"Carrs-Anchorage #1805_1650 W Northern Lights Blvd_Anchorage, Alaska 99503_907-339-0500"
-149.7522,61.2297,Starbucks - AK - Anchorage 00003,"Elmendorf AFB_Bldg 5800 Westover Avenue_Anchorage, Alaska 99506"
-149.8643361,61.19525062,Starbucks - AK - Anchorage 00004,"Fred Meyer - Anchorage #11_1000 E Northern Lights Blvd_Anchorage, Alaska 995084283_907-264-9600"
-149.8379726,61.13751355,Starbucks - AK - Anchorage 00005,"Fred Meyer - Anchorage #656_2300 Abbott Road_Anchorage, Alaska 99507_907-365-2000"
-149.9092788,61.13994658,Starbucks - AK - Anchorage 00006,"Fred Meyer - Anchorage (Dimond) #71_2000 W Dimond Blvd_Anchorage, Alaska 995151400_907-267-6700"
-149.7364877,61.19533265,Starbucks - AK - Anchorage 00007,"Safeway-Anchorage #1817_7731 E Northern Lights Blvd_Anchorage, Alaska 99504_907-331-1700"
-149.8211,61.2156
@tianhuil
tianhuil / Solve.md
Last active June 1, 2020 19:45
Practically solving the shape

Suppose we wish to understand if a given X/Y pair falls within a set of non-intersecting arbitrary polygons. For example, we are trying to see if a given lat,long falls within a Michigan minor civil division. There are 1500+ such divisions each one of which could be a polygon with hundreds or thousands of points. The following is a practical solution that is fast enough to return to a user.

  1. Break up Michigan into $N$ box oriented tiles. We can easily calculate if x, y will only fall within a tile based on the value (x % WIDTH, y % HEIGHT). Select $N$ such that most (e.g. ~60%?) tiles are fully contained within a polygon, and the vast majority (e.g. 95%) are in no more than 4 polygons. I imagine this would happen easy at $N$ less than 100,000.
  2. Preprocess the polygon shape files using Shapely to determine a map
@tianhuil
tianhuil / shard_data.py
Last active November 12, 2019 19:02
Data Sharding (useful preprocessing for dask)
import gzip
import os
from itertools import islice
import argparse
# from https://stackoverflow.com/a/41333436/8930600
def grouper(iterable, n):
iterator = iter(iterable)
while True:
group = tuple(islice(iterator, n))