Skip to content

Instantly share code, notes, and snippets.

View juandesant's full-sized avatar

Juande Santander-Vela juandesant

View GitHub Profile
@juandesant
juandesant / PythonPaternsLoopOptimizationTesting.py
Last active August 29, 2015 13:55
This script is an implementation of the performance testing shown in the article "Python Patterns — An Optimization Anecdote" (http://python.org/doc/essays/list2str/). Compare with the article, and see how different functions are faster with different input sizes, and for different factors.
from pprint import pprint
import array
import time
# Ancillary functions
def timing(function=None, loops=50, input_data):
print function.__name__,
r = range(loops)
t1 = time.clock()
for i in r:
@juandesant
juandesant / primes_generator.py
Last active August 29, 2015 14:11
Rudimentary prime generator written in Python
known_primes = [ 2, 3, 5, 7, 11, 13]
#known_primes = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
# 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
import itertools as it
def give_me_primes(limit=100000000000000000000000L):
last_prime = 1
for n in it.ifilter(lambda x: x<limit, known_primes):
last_prime = n
yield last_prime
for p_candidate in it.takewhile(lambda x: x<limit,it.ifilter(lambda x: x%2==1, it.count(n+1))):
@juandesant
juandesant / fibonnaci_generator.py
Created December 17, 2014 15:25
Fibonacci series generator written in Python
def fibonacci():
a, b = 1, 1
while True:
yield a
a, b = b, a + b
# This function allows you to drag and drop a file to the terminal, and use cdf to cd to the path containing the file.
# cdf (cd to file)
cdf() {
dest_dir=`dirname "$1"`
cd "$dest_dir"
}
@juandesant
juandesant / jama_search.py
Last active August 29, 2015 14:15
Uses `jama` (python-jama) and the `webbrowser` module to open items in the web browser from search results
#!/usr/bin/env python
from jama import API as jama_api
import sys
from pprint import pprint
import webbrowser
# See the needs for the jama.API constructor in the project page:
# https://github.com/DynamicControls/python-jama
jama_client = jama_api()
@juandesant
juandesant / rot13.py
Last active August 29, 2015 14:15
Implementation of a command line rot13 using maketrans and a programmatic approach to construct the transform pairs.
#!/usr/bin/env python
# Trying to create the transformation from scratch
import sys # For getting stdin
from string import maketrans # For creating the transformation
# build the lowercase letters from a to z
letters = "".join([chr(ord("a")+x) for x in xrange(0,26)])
# rotate them by taking the last 13, and the first 13
rot13letters = letters[13:]+letters[:-13]
# concatenate the uppercase version of both strings after the lowercase
#!/bin/bash
# Encode a WAV to a finalized podcast MP3 with metadata, in the current directory
# Requires lame
# With Homebrew on Mac OS X: brew install lame
SHOW_AUTHOR="ATP"
EPISODE_NUMBER=104
EPISODE_TITLE="Minutiæ"
@juandesant
juandesant / dev_random.py
Last active August 29, 2015 14:20
Use NumPy to read random numbers as integers from /dev/random
import numpy as np
randomFile = open("/dev/random", "rb") # open for reading, binary
#define an integer type for reading
intType = np.dtype([('random', int)])
randomList = np.fromfile(randomFile,dtype=intType, count=10)
# Random generator
def random_int_generator(randomFile=randomFile):
yield np.fromfile(randomFile,dtype=intType, count=1)[0][0]
# -*- coding: utf-8 -*-
"""magics for using blockdiag.com modules with IPython Notebook
The module provides magics: %%actdiag, %%blockdiag, %%nwdiag, %%seqdiag
Sample usage (in IPython cell):
%%blockdiag
{
A -> B -> C;
@juandesant
juandesant / organigram_with_subgraphs.gv
Created July 30, 2015 20:29
This dot file represents an organigram using subgraphs
digraph Organigram {
/* General Layout */
rankdir = LR;
/* Layout Nodes */