Skip to content

Instantly share code, notes, and snippets.

@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 / plot_interactive.py
Created February 18, 2015 18:42
Barebones manual animations in matplotlib
"""
Using matplotlib for manually creating animations. Works similar to FuncAnimation. Manual animation is a bit simpler than using the animations API in general since it is still capable of handling some events (like window resizing) automatically.
"""
import matplotlib.pyplot as plt
import numpy as np
# Create a figure and axis for plotting on
@tgarc
tgarc / goldensearch.py
Created February 27, 2015 21:59
Golden section search for finding the minimum of a function
# Example usage
# f = lambda x: 3*(x**2)+4*x+ 5
# xhat = findmin(f,-3,2)
from scipy.constants import golden
def goldensearch(f,a,b,tol=1e-6):
xl = b - (b-a)/golden
xu = a + (b-a)/golden
@tgarc
tgarc / opencv_cygwin
Last active August 29, 2015 14:16
Compiling opencv 2.4.10 for cygwin with ffmpeg 2.5.3
# download the opencv-2.4.10 for unix
# unzip the library to some temporary folder; let's say it's ~/opencv
# see http://hvrl.ics.keio.ac.jp/kimura/opencv/opencv-2.4.10.html
# for build patch
#build ffmpeg
$ cd /tmp/
$ tar xvjf ffmpeg-2.5.3.tar.bz2
$ cd ffmpeg-2.5.3/
$ ./configure --disable-yasm --disable-iconv
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# A short tutorial on pandas MultiIndexing with DataFrames"
]
},
{
@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 / 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 / libusb1.spec
Created December 15, 2015 23:04
libusb1.0.20.spec file for building RPM
# Modified version of libusb1.spec from libusb1-1.0.9-0.6.rc1.el6.rpm
# to create an RPM package for libusb-1.0.20
Summary: A library which allows userspace access to USB devices
Name: libusb1
Version: 1.0.20
Release: 0
Source0: libusb-1.0.20.tar.bz2
#Source0: http://downloads.sourceforge.net/libusb/libusb-%{version}.tar.bz2
License: LGPLv2+
@tgarc
tgarc / parse_fspec.py
Last active July 21, 2016 15:50
Parse the format specification (https://docs.python.org/2.7/library/string.html#format-specification-mini-language) from a .format style python string
import re
from string import Formatter
_fspecre = ("((?P<fill>{fill})?(?P<align>{align}))?"
"(?P<sign>{sign})?"
"(?P<prefix>#)?"
"(?P<zeropad>0)?"
"(?P<width>{width})?"
"(?P<thousands>,)?"
"(?:\.(?P<precision>{precision}))?"
@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