Skip to content

Instantly share code, notes, and snippets.

#### Start IPython, generate SHA1 password to use for IPython Notebook server
$ ipython
Python 2.7.5 |Anaconda 1.8.0 (x86_64)| (default, Oct 24 2013, 07:02:20)
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
@alexsavio
alexsavio / repo.py
Created June 25, 2014 14:19
GitHub to Gitlab repository version copier/control.
#content of the config file .python-gitlab.cfg:
#[global]
#default = local
#ssl_verify = true
#[local]
#url = http://***************
#private_token = **************
import colorsys
def hex_to_rgb(hex_str):
"""Returns a tuple representing the given hex string as RGB.
>>> hex_to_rgb('CC0000')
(204, 0, 0)
"""
if hex_str.startswith('#'):
hex_str = hex_str[1:]
@alexsavio
alexsavio / copy_subtitles_sync.py
Last active December 25, 2016 23:38
CLI command that reads the time data from one SRT file, the speech text from another, mixes them and saves the result into a new srt file.
#!/usr/bin/env python
import os
import os.path as path
import argparse
import logging
from chardet.universaldetector import UniversalDetector
from pysrt import SubRipFile, SubRipItem
logging.basicConfig(level=logging.INFO)
@alexsavio
alexsavio / upper_diagonal_indices
Last active August 29, 2015 14:05
One liner for loop on upper diagonal indices of a square matrix (n_elems X n_elems) (k=0 to include the diagonal)
import numpy as np
for x_idx, y_idx in zip(*np.triu_indices(n_elems, k=1)):
@alexsavio
alexsavio / ipynb_present.py
Created September 8, 2014 18:56
IPython notebook presentation launcher.
#!/usr/bin/env python
import os
import shutil
import tempfile
import logging
import argparse
import subprocess
'''
Non-parametric computation of entropy and mutual-information
Adapted by G Varoquaux for code created by R Brette, itself
from several papers (see in the code).
These computations rely on nearest-neighbor statistics
'''
import numpy as np
@alexsavio
alexsavio / embed-font-to-svg.py
Last active July 22, 2019 07:08
CLI to embed font-face base64 encoding to SVG files or clear text output for CSS importing
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import os.path as op
import base64
import argparse
import logging
from lxml import etree
@alexsavio
alexsavio / example_google.py
Created November 4, 2014 15:28
Docstring examples
# -*- coding: utf-8 -*-
"""Example Google style docstrings.
This module demonstrates documentation as specified by the `Google Python
Style Guide`_. Docstrings may extend over multiple lines. Sections are created
with a section header and a colon followed by a block of indented text.
Example:
Examples can be given using either the ``Example`` or ``Examples``
sections. Sections support any reStructuredText formatting, including
@alexsavio
alexsavio / mcmc
Last active October 2, 2020 22:35 — forked from osdf/gist:5133737
Simple MCMC sampling with Python
"""
Some python code for
Markov Chain Monte Carlo and Gibs sampling
by Bruce Walsh
"""
import numpy as np
import numpy.linalg as npla