Skip to content

Instantly share code, notes, and snippets.

View yunwilliamyu's full-sized avatar

Yun William Yu yunwilliamyu

View GitHub Profile
@yunwilliamyu
yunwilliamyu / process_wrapper.py
Created January 4, 2017 19:14
Redirect Python stderr/stdout for a block
import sys
import contextlib
@contextlib.contextmanager
def output_wrapper():
save_stdout = sys.stdout
save_stderr = sys.stderr
sys.stdout = open('stdout.log', 'a')
sys.stderr = open('stderr.log', 'a')
yield
sys.stdout = save_stdout
@yunwilliamyu
yunwilliamyu / matrix_nonzero_numpy.py
Created January 2, 2017 19:25
Numpy 1.11.0 vs 1.8.2 matrix nonzero fix
import numpy as np
A = np.matrix([[1, 2], [3, 4]])
A[0,:].nonzero() # Returns (matrix([[0, 0]]), matrix([[0, 1]])) if np.__version__ == "1.8.2"
# Returns (array([0, 0]), array([0, 1])) if np.__version__ == "1.11.0"
np.asarray(A[0,:]).nonzero() # Returns (array([0, 0]), array([0, 1])) for both versions, so is backwards compatible
@yunwilliamyu
yunwilliamyu / stream_subprocess_stdin_stdout.py
Last active December 28, 2016 23:32
Streaming stdin and stdout for subprocess progress reports
# If you don't need streaming stdout, just use communicate to access stdout and stderr instead
import threading
import subprocess
import sys
pr = subprocess.Popen(['command'], env=my_env,
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
def pipe_writer():
pr.stdin.write(input)
pr.stdin.close()
set laststatus=2
set statusline=%<%F%=\ [%M%R%H%Y]\ (%(%l,%c%))
syntax on
set autoindent
set tabstop=4
set shiftwidth=4
set smarttab
set expandtab
set softtabstop=4
autocmd BufRead *.py set smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class