Skip to content

Instantly share code, notes, and snippets.

@tgarc
tgarc / startup.py
Last active August 22, 2023 15:52
Launch ipython terminal magic
'''
Adds %ipython magic command to launch a bash terminal and connect to the current jupyter session because coding in jupyter sucks.
To be placed in ~/.ipython/profile_default/startup/
'''
import os, sys
import ipykernel
from IPython.core.magic import register_line_magic
@register_line_magic
def ipython(line):
@tgarc
tgarc / Makefile
Created February 17, 2023 15:07
C++ Makefile template
# Thanks to Job Vranish (https://spin.atomicobject.com/2016/08/26/makefile-c-projects/)
TARGET_EXEC := a
BUILD ?= release
BUILD_DIR ?= ./build
SRC_DIRS ?= ./src
CC ?= gcc
CXX ?= g++
@tgarc
tgarc / example_usage
Last active March 25, 2022 13:44
Jekyll IPython notebook converter
ipython nbconvert --to markdown <notebook>.ipynb --config jekyll.py
@tgarc
tgarc / save_user_variables.md
Last active December 20, 2021 08:25
automatically save ipython sessions

I often like to start my ipython session from where I last left off - similar to saving a firefox browsing session. IPython already automatically saves your input history so that you can look up commands in your history, but it doesn't save your variables. Here are the steps to save the state of your variables on exit and have them loaded on startup:

  1. Add the save_user_variables.py script below to your ipython folder (by default $HOME/.ipython). This script takes care of saving user variables on exit.

  2. Add this line to your profile's ipython startup script (e.g., $HOME/.ipython/profile_default/startup/startup.py):
    get_ipython().ex("import save_user_variables;del save_user_variables")

  3. In your ipython profile config file (by default $HOME/.ipython/profile_default/ipython_config.py) find the following line:
    # c.StoreMagics.autorestore = False
    Uncomment it and set it to true. This automatically reloads stored variables on startup. Alternatively you can use reload the last session manually us

@tgarc
tgarc / playrec.py
Last active January 14, 2020 13:55
Simultaneous record and play using pysoundfile and pyaudio libraries.
#!/usr/bin/env python2
"""
Simultaneous record and play using libsndfile and portaudio libraries.
You should choose a stream rate that is compatible with your device HW,
or you could get unreliable results.
"""
import pyaudio as pa
import soundfile as sf
import wave
@tgarc
tgarc / be8.patch
Last active July 14, 2019 07:07
Patch GCC ARM Embedded Toolchain for big-endian targets (simply git apply be8.patch in top directory)
From 9ba6f4c25c50ce80c9195c45df7fab9d1e1c8152 Mon Sep 17 00:00:00 2001
From: "tdos.apone" <toemossgarcia@gmail.com>
Date: Thu, 10 Dec 2015 11:03:09 -0600
Subject: [PATCH 1/2] initial big-endian attempt
---
build-toolchain.sh | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/build-toolchain.sh b/build-toolchain.sh
@tgarc
tgarc / mask2runs.py
Last active November 6, 2018 17:57
find runs of constant value, returning their indices
import numpy as np
def mask2runs(mask):
'''
mask2runs
Turns a boolean array into an array of indices indicating the start and end indices of each run of 1's.
'''
runflip = np.nonzero(mask[1:] ^ mask[:-1])[0]+1
runflip[1::2] -= 1 # Note that the prior step returns end indices as the end of a run plus 1
@tgarc
tgarc / merge_load.py
Last active April 28, 2017 00:21
Load a yaml stream (src) and merge it with another yaml stream (dst) allowing *dst* to use aliases defined in *src*.
import sys
import yaml
def merge_load(src, dst, loadfunc=yaml.load, **kwargs):
'''\
Load a yaml stream (src) and merge it with another yaml stream
(dst) allowing *dst* to use aliases defined in *src*.
Other Parameters
----------------
@tgarc
tgarc / .asoundrc
Last active February 5, 2017 21:38
portaudio/libsndfile for half and full duplex audio streaming
# sets up a full duplex alsa loopback device
pcm.loophw00 {
type hw
card Loopback
device 0
subdevice 0
format S32_LE
rate 48000
channels 8
}
@tgarc
tgarc / findfile.py
Created February 2, 2017 21:06
python file finder
import os
import glob
import errno
try:
from itertools import ifilter as filter
except ImportError:
pass
def findfile(filepath, find_all, search_path='', recursive=False, allowglob=False, rel_path='.', follow_links=False):
filelist = []