Skip to content

Instantly share code, notes, and snippets.

View FanchenBao's full-sized avatar

Fanchen Bao FanchenBao

View GitHub Profile
@FanchenBao
FanchenBao / ggrep_recipe.sh
Created January 24, 2024 18:30
ggrep recipe
# ggrep is the GNU grep. On macOS, it is installed via `brew install grep`
# Using perl regex for lookahead and lookbehind
# This example greps the EXACT match between quotation marks
aws lambda publish-version --function-name foo --profile bar | ggrep -Po '(?<=")\d+(?=")'
# This example counts the number of occurrences of "prod" that is wedged bewteen string literals
# '"Name": "' and '"'
aws lambda list-aliases --function-name foo --profile bar | ggrep -Pc '(?<="Name":\s")prod(?=")'
@FanchenBao
FanchenBao / grouped_barplot.py
Created October 11, 2022 05:41
A function to generate grouped bar plot. Read the doc string for more information
def grouped_barplot(
ax,
df,
xlabel: str = '',
ylabel: str = '',
title: str = '',
width: float = 0.6,
loc: str = '',
bbox_to_anchor: List = [],
):
@FanchenBao
FanchenBao / timeit_benchmark.py
Last active August 18, 2022 22:56
An example of using timeit for bench marking
import timeit
setup = """
import pandas as pd
from dateutil import parser
from datetime import datetime
def test_datetime(timestamp):
try:
@FanchenBao
FanchenBao / script.py
Created August 15, 2022 18:59
Template for creating a Python script that accepts command line arguments
from argparse import ArgumentParser, RawDescriptionHelpFormatter
ef get_argument_parser() -> ArgumentParser:
"""Acquire command line arguments
:return: The argument parser
:rtype: ArgumentParser
"""
parser = ArgumentParser(
@FanchenBao
FanchenBao / test_foo.py
Last active August 15, 2022 18:42
Template for using the built-in unit test module in Python3
import unittest
from unittest.mock import MagicMock, patch
class TestFoo(unittest.TestCase):
"""blablabla
To run all the tests in TestFoo, run command
python3 -m unittest tests.test_foo.TestFoo
@FanchenBao
FanchenBao / chunk_read.py
Last active June 11, 2022 08:17
Generic function to read large structured file in chunks
def chunk_read(f_obj: TextIOBase, sentinel: str, max_sentinel: int):
"""Read a file object in chunks
Read the file object line by line. Each time a sentinel is detected, we increment
a count. Once the count reaches max_sentinel, we have gatherered the required
chunk and yield it.
The function is inspired by this SO answer:
https://stackoverflow.com/a/42964612/9723036
@FanchenBao
FanchenBao / .vimrc
Last active November 27, 2023 17:29
Vim Setup
" Set absolute line number for the current line and the rest is relative number
set number
set relativenumber
" Enable monokai theme
syntax enable
set background=dark
colorscheme monokai
" I do not remember what these do
@FanchenBao
FanchenBao / weighted_avg_std.md
Created February 21, 2022 20:17
PostgreSQL Weighted Average And Weighted Standard Deviation

Regarding Math Notation

You can copy and paste the LaTeX notation in a LaTeX editor to view the math notation. A good online LaTeX editor tool is HostMath.

Definition

Let $w_i$ be the weight of ith random variable $x_i$, then weighted average is expressed as

$$ \mu_x = \frac{\sum_{i=1}^n{w_ix_i}}{\sum_{i=1}^nw_i}

@FanchenBao
FanchenBao / multiprocess_benchmark.py
Created December 17, 2021 17:38
Simple benchmark for multiprocessing in Python2
#! /usr/bin/python
import multiprocessing as mp
import time
'''
A simple benchmark testing while learning about multiprocessing in python (2.7.10)
It is apparent from this test, conducted in MacBook Air OS 10.13.6, that using Pool
class of multiprocessing yields the best runtime performance. My guess is that in
the parallel() function, each new process is spawn up manually via a for loop, whereas
@FanchenBao
FanchenBao / ScreenshotTest.java
Created February 11, 2021 00:24
Source code 01 used in medium article "Make Fastlane Screengrab Work with React Native"
<!--For fastlane screengrab use-->
package com.your.app;
import androidx.test.rule.ActivityTestRule;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;