Skip to content

Instantly share code, notes, and snippets.

View bobfang1992's full-sized avatar
🏠
Working from home

Bob Fang bobfang1992

🏠
Working from home
View GitHub Profile
@bobfang1992
bobfang1992 / Alembic for different envs
Last active July 4, 2020 08:35 — forked from twolfson/README.md
Toggling between `alembic` databases
`alembic` is great but lacks an out of the box way to set up running migrations against a specific database (e.g. `development`, `test`, `production`). The following adjustments to its `env.py` and `alembic.ini` allow us to target a specific database:
**Example:**
```bash
alembic -x db=development upgrade head
```
**env.py:**
@bobfang1992
bobfang1992 / compile-time-vector.cpp
Created July 3, 2020 21:43
compile time vector
#include <iostream>
template <int...>
struct static_vector {
};
template <int n>
struct static_vector<n> {
static constexpr int value = n;
static_vector<> rest;

Things to check:

  1. It seems when creating indeces, the sqlalchemy will prepend table name when it is a Postgres database but does not do it if it is Sqlite Figure out if this is true. Pratically this implies that for PG you can have same index name across tables but not for Sqlite. Small issue but worth checking.
@bobfang1992
bobfang1992 / get_session.py
Created July 30, 2020 05:20
Sqlalchemy getsession
from contextlib import contextmanager
from typing import ContextManager
from sqlalchemy.engine.base import Engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm.session import Session
@contextmanager
def get_seesion(engine: Engine) -> ContextManager[Session]:

key - value mapping

{
  "name": "Bob"
  "age": 28,
  "sex": "male"
}
@bobfang1992
bobfang1992 / clear_db.sql
Created August 13, 2020 08:59
Delete every table in PG
DO $$ DECLARE
r RECORD;
BEGIN
FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = current_schema()) LOOP
EXECUTE 'DROP TABLE ' || quote_ident(r.tablename) || ' CASCADE';
END LOOP;
END $$;
from fastapi import FastAPI
import uuid
app = FastAPI()
COM_API_OBJECT = {}
@app.get("/create")
async def create():
@bobfang1992
bobfang1992 / 289.py
Created December 30, 2020 17:07
Leetcode 289
class Solution:
def gameOfLife(self, board: List[List[int]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
m = len(board)
if m == 0:
return
n = len(board[0])
@bobfang1992
bobfang1992 / 84.py
Created December 31, 2020 16:44
Leetcode 84
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
stack = [-1]
result = 0
heights.append(0)
for i in range(len(heights)):
while stack and heights[stack[-1]] > heights[i]:
h = heights[stack.pop()]
w = i - stack[-1] - 1
@bobfang1992
bobfang1992 / 1379.py
Created January 2, 2021 11:10
Leetcode 1379
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode:
val = target.val