Skip to content

Instantly share code, notes, and snippets.

View ricky-lim's full-sized avatar

Ricky Lim ricky-lim

View GitHub Profile
import math
import typing
import hashlib
from dataclasses import dataclass, field
from pathlib import Path
@dataclass(order=True)
class ReferenceGenome:
@ricky-lim
ricky-lim / grade.py
Created March 25, 2020 12:45
Composing classes with dataclass, instead of deep-nested dictionary
# From the book, effective python 2nd edition item 37
from collections import defaultdict
from dataclasses import dataclass, field
from typing import List, Dict
@dataclass
class Grade:
weight: int
@ricky-lim
ricky-lim / gist:dca855169e315b5faa9e67efe80cc942
Created June 20, 2020 05:58
Flask DB migrate to drop column with sqlite
Example to drop a column title in a posts table:
# Generate by Alembic
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_posts_title'), table_name='posts')
op.drop_column('posts', 'title')
$ flask db downgrade
....
@ricky-lim
ricky-lim / example-video-audio-youtube-dl.txt
Last active January 10, 2021 22:58
Combine m4a and mp4 from youtube
pipx install tubex
tubex --outdir foo download-mp4 https://www.youtube.com/watch?v=LABGimhsEys
@ricky-lim
ricky-lim / many-to-many.py
Created November 28, 2020 03:34
Questions
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import sessionmaker, relationship, backref
engine = sqlalchemy.create_engine('sqlite:///tmp.db')
Base = declarative_base()
class Answer(Base):
@ricky-lim
ricky-lim / vuetify-template-form.py
Created December 2, 2020 13:08
template form for vuetify
import ipyvuetify as v
import traitlets
import json
class MyForm(v.VuetifyTemplate):
email = traitlets.Unicode('').tag(sync=True)
password = traitlets.Unicode('').tag(sync=True)
isValid = traitlets.Bool(False).tag(sync=True)
result = traitlets.Dict({}).tag(sync=True)
template = traitlets.Unicode('''
@ricky-lim
ricky-lim / club_member.py
Last active December 19, 2020 09:39
club-member
import pytest
from sqlalchemy import (
create_engine,
Column,
Integer,
String,
Table,
ForeignKey,
PrimaryKeyConstraint,
)
@ricky-lim
ricky-lim / generate_bins.py
Created January 7, 2021 18:21
generate binning with a size, that accumulates to the count
#!/usr/bin/env python
import math
def generate_bins(size: int, count: int):
"""
>>> list(generate_bins(size=100, count=300))
[100, 100, 100]
>>> list(generate_bins(size=100, count=301))
[100, 100, 100, 1]
@ricky-lim
ricky-lim / autoreload-ipython.sh
Created January 10, 2021 08:52
Add autoreload to ipython config
# Create config file
$ ipython profile create
$ vim <ipython_config_file>
c.InteractiveShellApp.extensions = ['autoreload']
.InteractiveShellApp.exec_lines = ['%autoreload 2']
@ricky-lim
ricky-lim / palindromChecker.java
Last active March 30, 2022 11:16
Palindrom checker
import java.util.stream.IntStream;
class Scratch {
public static boolean palindromeChecker(String s) {
String normalizedString = s.trim().toLowerCase();
return IntStream.range(0, normalizedString.length() / 2)
.allMatch(i ->
normalizedString.charAt(i) ==
normalizedString.charAt(normalizedString.length() - i - 1));