Skip to content

Instantly share code, notes, and snippets.

View bnlucas's full-sized avatar

Nathan Lucas bnlucas

View GitHub Profile
@bnlucas
bnlucas / float_formating.py
Last active August 29, 2015 14:22
A better way to print formatted floats using the `format()` specs.
def float_spec(fill, align, sign, width, separator, precision):
if len(fill) > 1:
raise ValueError('The fill character can only contain 1 character.')
if align not in ['left', 'right', 'center', 'padded']:
raise ValueError('{} is not an allowed alignment.'.format(align))
if sign not in ['+', '-', ' ']:
raise ValueError('{} is not a valid sign.'.format(sign))
@bnlucas
bnlucas / extended_gcd.py
Created June 11, 2015 07:04
Extended Euclidean Algorithm extended_gcs(a, b) - With benchmarks.
'''
Extended Euclidean Algorithm extended_gcd(a, b) methods found online.
'''
from functools import wraps
from time import time
def benchmark(iterations=10000):
def wrapper(function):
@wraps(function)
'''
def test1(stop=100):
for i in range(stop):
pass
def test2(stop=100):
for i in xrange(stop):
pass
compare = Compare('Testing the use of range and xrange.')
@bnlucas
bnlucas / which.py
Last active August 29, 2015 14:23
Implement the Linux `which` command in Python that works in Windows!
import os
def which(executable):
for path in os.environ['PATH'].split(os.pathsep):
path = path.strip('"')
fpath = os.path.join(path, executable)
if os.path.isfile(fpath) and os.access(fpath, os.X_OK):
return fpath
@bnlucas
bnlucas / imports.py
Created June 22, 2015 06:04
Find all .py files that import a certain module or just supplied methods of the module. I use http://click.pocoo.org/ for interacting with the command line, but this can be changed.
import os
import re
from subprocess import PIPE, Popen
import click
def which(executable):
for path in os.environ['PATH'].split(os.pathsep):
@bnlucas
bnlucas / Paginated.py
Last active October 17, 2015 22:54
Move seamlessly through paginated API results without loading all pages at once. Only loads the pages needed.
import math
def mceil(n, m):
'''
returns ceil(n) the multiple m.
example: mceil(21, 10) -> 30.0 which is the next multiple of 10.
:param n: value to ceil.
@bnlucas
bnlucas / 0_reuse_code.js
Created October 31, 2015 21:11
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@bnlucas
bnlucas / demo.py
Last active December 18, 2015 16:48
Using foreign IDs with pyechonest Song instantiations due to their issue with Song.id and Song.track_id.
from pyechonest import config
from pyechonest.song import Song
# Loading Bangarang by Skrillex...
song = Song('id:rdio-US:track:t14313937')
song = Song('id:spotify-WW:track:6VRhkROS2SZHGlp0pxndbJ')
song = Song('TRVOJIZ13A9CFBB670')
@bnlucas
bnlucas / ghget.py
Last active December 20, 2015 13:19
Download GitHub repository, not a GIT clone command! This was the answer to a problem I was having, creating a skeleton app structure and easily setting up a local file structure. For example, obtaining the src directory from https://github.com/kamalgill/flask-appengine-template. This is a work in progress, not the prettiest of things.
'''
c:\local\dev>python ghget.py -h
usage: ghget.py [-h] -u USER -r REPO [-b BRANCH] [-s SUB] [-p PATH] [-v]
GitHub repository downloader.
optional arguments:
-h, --help show this help message and exit
-u USER, --user USER github user (required)
-r REPO, --repo REPO github repository (required)
chain = [ 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, 101, 103, 107, 109, 113,
127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
467, 479, 487, 491, 499, 503, 509, 521, 523, 541,