Skip to content

Instantly share code, notes, and snippets.

@bl4ck5un
bl4ck5un / py_descriptor.py
Last active August 29, 2015 14:01
Flavors of descriptor
#!/usr/bin/python
class simple_tracer_as_cls:
def __init__(self, func):
self.calls = 0
self.func = func
def __call__(self, *args, **kargs):
self.calls += 1
print('call %s to %s' % (self.calls, self.func.__name__))
@bl4ck5un
bl4ck5un / lister.py
Created May 10, 2014 16:28
A Python class-tree climber
#!/usr/share/python
class ListInstance:
def __str__(self):
return '<Instance of %s, address %s:\n%s>' % (
self.__class__.__name__,
id(self),
self.__attrnames())
def __attrnames(self):
result = ''
@bl4ck5un
bl4ck5un / miniCTex.tex
Created May 13, 2014 13:20
Minimum configuration for CTex on MacTex
%!TEX TS-program = xelatex
%!TEX encoding = UTF-8 Unicode
\documentclass[12pt]{article}
\usepackage[adobefonts]{ctex}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{amsmath}
\begin{document}
@bl4ck5un
bl4ck5un / v6.sh
Created June 12, 2014 22:13
setup isatap tunnel on ubuntu
#!/bin/bash
ipv4_addr=`ifconfig eth0 | grep 'inet addr' | cut -d : -f 2 | cut -d ' ' -f 1`
ipv6_base='2402:f000:1:1501'
ip tunnel add sit1 mode sit remote 166.111.21.1 local ${ipv4_addr}
ifconfig sit1 up
ifconfig sit1 add ${ipv6_base}::${ipv4_addr}/64
ip -6 route add ::/0 via ${ipv6_base}::1 metric 1 dev sit1
@bl4ck5un
bl4ck5un / insert_graphic.tex
Last active August 29, 2015 14:07
Latex: insert graphics
\usepackage{graphicx}
\begin{figure}[p]
\centering
\includegraphics[width=0.8\textwidth]{image.png}
\caption{Awesome Image}
\label{fig:awesome_image}
\end{figure}
@bl4ck5un
bl4ck5un / samplefrom2.py
Created September 24, 2015 21:59
Sample from two array coherently
# x = ...
# y = ...
idx = np.random.choice(np.arange(len(x)), 1000, replace=False)
x_sample = x[idx]
y_sample = y[idx]
"""
a : 1-D array-like or int
If an ndarray, a random sample is generated from its elements. If an int, the random sample is generated as if a was np.arange(n)
size : int or tuple of ints, optional
Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.
@bl4ck5un
bl4ck5un / palatino.tex
Created September 26, 2015 02:04
Using Palatino font in LaTeX
\usepackage{tgpagella} % text only
\usepackage{mathpazo} % math & text
wget https://aur.archlinux.org/cgit/aur.git/snapshot/package-query.tar.gz
tar xfz package-query.tar.gz
cd package-query && makepkg -rsi
wget https://aur.archlinux.org/cgit/aur.git/snapshot/yaourt.tar.gz
tar xfz yaourt.tar.gz
cd yaourt && makepkg -rsi
ffmpeg -i input.mp4 -c:v copy -c:a mp3 output.mp4
@bl4ck5un
bl4ck5un / python_argparse.py
Created October 7, 2015 19:51
Dead simple argparse example
# from http://stackoverflow.com/a/7427376/1616210
parser = argparse.ArgumentParser(description='Description of your program')
parser.add_argument('-f','--foo', help='Description for foo argument', required=True)
parser.add_argument('-b','--bar', help='Description for bar argument', required=True)
args = vars(parser.parse_args())
if args['foo'] == 'Hello':
# code here
if args['bar'] == 'World':