Skip to content

Instantly share code, notes, and snippets.

@ianlini
ianlini / test_set_default.py
Created October 26, 2020 10:36
Python dict.setdefault() performance
In [1]: def a(keys):
...: d = {}
...: for key in keys:
...: inner_d = d.setdefault(key, {})
...: return d
...:
In [2]: def b(keys):
...: d = {}
...: for key in keys:
@ianlini
ianlini / windows_terminal_quake.ahk
Last active July 2, 2020 02:36
AutoHotkey script for supporting Quake style for Windows Terminal (alt+1). Also support "always on top" and "transparent window".
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetBatchLines, -1
Process, Priority,, High
Opacity := 200 ; [0, 255] or off
Height := 0.4 ; [0., 1.]
@ianlini
ianlini / word_reader.py
Created June 23, 2019 04:39
Read input word by word in Python
class WordReader(Iterator):
def __init__(self, fp=sys.stdin):
self.fp = fp
def __next__(self):
word = ''
while True:
c = self.fp.read(1)
if c is None:
if word:
@ianlini
ianlini / motion_slack.sh
Last active January 28, 2020 00:00
Watch a log file and send a Slack message when there is new log.
#!/bin/bash
set -e
LOG_FILE='/var/log/motion/motion.log'
WEBHOOK_URL='https://hooks.slack.com/services/...'
USERNAME='motion'
CHANNEL='camera'
function send_slack_message {
DATA="payload={\"username\": \"${USERNAME}\", \"channel\": \"${CHANNEL}\", \"text\": \"$1\"}"
echo "data: ${DATA}"
@ianlini
ianlini / motion.conf
Last active March 21, 2021 14:38
motion config for motion 4.1.1 + Raspberry Pi 3 + Raspbian stretch
# Rename this distribution example file to motion.conf
#
# This config file was generated by motion 4.1.1
# Documentation: /usr/share/doc/motion/motion_guide.html
############################################################
# Daemon
############################################################
# Start in daemon (background) mode and release terminal (default: off)
@ianlini
ianlini / scipy_tensorflow.py
Created February 22, 2017 10:30
Using scipy sparse matrix as the input of tensorflow
import tensorflow as tf
import scipy.sparse as ss
import numpy as np
def scipy_run(sess, result, X, tf_indices, tf_id_values, tf_weight_values,
tf_dense_shape):
row_nnz = np.diff(X.indptr)
indices = np.asarray([[row_i, col_i]
for row_i, nnz in enumerate(row_nnz)