Skip to content

Instantly share code, notes, and snippets.

View FanchenBao's full-sized avatar

Fanchen Bao FanchenBao

View GitHub Profile
@FanchenBao
FanchenBao / .zshrc
Last active March 8, 2024 18:16
iTerm2 and Oh My Zsh Setup
# If you come from bash you might have to change your $PATH.
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
# Path to your oh-my-zsh installation.
export ZSH="/Users/fanchenbao/.oh-my-zsh"
# Set default editor
export EDITOR='nano'
# Set name of the theme to load --- if set to "random", it will
@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 / .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 / my_queue.py
Last active October 24, 2023 04:20
A custom MyQueue class that avoids `NotImplementedError` when calling queue.qsize() method.
from multiprocessing.queues import Queue
import multiprocessing
# The following implementation of custom MyQueue to avoid NotImplementedError
# when calling queue.qsize() in MacOS X comes almost entirely from this github
# discussion: https://github.com/keras-team/autokeras/issues/368
# Necessary modification is made to make the code compatible with Python3.
class SharedCounter(object):
@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 / accumulate_vnstat_output.py
Last active April 6, 2022 23:45
A quick script to accumulate the "total" column of `vnstat -5hdm` output
import re
from typing import List
"""
This scripts accumulates the 'total' column of vnstat output in this format:
wlan0 / hourly
hour rx | tx | total | avg. rate