Skip to content

Instantly share code, notes, and snippets.

View LemonPi's full-sized avatar

Johnson Zhong LemonPi

View GitHub Profile
@LemonPi
LemonPi / typeddict.py
Last active November 9, 2016 10:39
python dictionary with restricted key type, given at compile time
# -*- coding: utf-8 -*-
class TypedDict(dict):
"""Dictionary with keys restricted to a specific type, given at creation time"""
def __init__(self, key_type):
print("initializing with", key_type)
self.key_type = key_type
def __getstate__(self):
return self.key_type, dict(self)
@LemonPi
LemonPi / g11
Last active February 23, 2017 14:24
Quick c++11 compile script for bash; put in user path (e.g. /usr/bin/)
filename=$(basename "$1")
outfile="${filename%.*}"
{ g++ -o $outfile $@ -std=c++11 2>&1; } | tee log.txt
@LemonPi
LemonPi / mode_docs_spreadsheet
Created February 24, 2017 10:38
Google Docs query for listing rows by occurance
=query(index(if({1,1},upper(D3:D24)&"")),"select Col1,count(Col2) where Col1<>'' group by Col1 order by count(Col2) desc",0)
@LemonPi
LemonPi / clear_terminal
Created March 15, 2017 15:37
Clear terminal for real rather than printing new lines; enter on bash
printf "\033c"
@LemonPi
LemonPi / grape
Created March 30, 2017 16:21
Search recursively for patterns in C++ source files
#!/bin/bash
grep -rn --color=always --include=\*.{cpp,hpp,c,h} $@
@LemonPi
LemonPi / .clang-format
Last active October 2, 2017 17:36
Clang format options
# put in project root
---
# We'll use defaults from the LLVM style, but with 4 columns indentation.
BasedOnStyle: LLVM
IndentWidth: 4
---
Language: Cpp
# Force pointers to the type for C++.
DerivePointerAlignment: false
@LemonPi
LemonPi / pre-commit.hook
Created January 18, 2018 17:50
pre-commit hook for checking source files are clang-formatted, giving command to format any unformatted files
#!/bin/bash
#
# Check that all source files follow a fixed format.
# Called by "git commit" with no arguments.
# Commit is blocked if any file does not follow format and outputs
# command to format them.
# Enable this by copying this file into .git/hooks/pre-commit
ROOT=$(git rev-parse --show-toplevel)
FORMATTER="clang-format"
# For a complete list of available commands, see http://bit.ly/jLtj
# Message to display in the status line when activity is detected in a
# monitored window.
activity "activity in %n (%t) [%w:%s]~"
# Detach session on hangup instead of terminating screen completely.
autodetach on # default: on
# When a bell character is sent to a background window, screen displays a
@LemonPi
LemonPi / vocab.txt
Created August 16, 2018 03:21
GRE vocabulary map
repeated too often as to lose meaning
- banal
- trite
- platitude (a trite remark)
- hackneyed (lacking significance from overuse)
- bromide (a trite or obvious remark)
dull
- insipid (lacking flavour or interest)
- prosaic (dull and lacking imagination)
@LemonPi
LemonPi / watch_sync.py
Last active November 3, 2018 20:42
Automatic rsync on file change (alternative for systems without inotifywait)
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import RegexMatchingEventHandler
from subprocess import call
import argparse
class RsyncHandler(RegexMatchingEventHandler):
def __init__(self, path, target):