Skip to content

Instantly share code, notes, and snippets.

View bnlucas's full-sized avatar

Nathan Lucas bnlucas

View GitHub Profile
@bnlucas
bnlucas / example.txt
Last active August 2, 2020 21:57
Full Text Searching with the Google App Engine Search API. Using with Flask to demonstrate.
When going to /search/artist/<query_string> using the documents below you get:
/search/artist/m - this returns both Macklemore and Against Me!
/search/artist/a - this returns both Against Me! and AWOLNATION
/search/artist/ma - this returns only Macklemore
/search/artist/aw - this returns only AWOLNATION
/search/artist/me - this returns only Against Me!
Adding 'AWOLNATION' to models.Artist produces:
@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 / fermat.py
Last active September 9, 2018 13:53
Fermat primality testing with Python.
def fermat(n):
if n == 2:
return True
if not n & 1:
return False
return pow(2, n-1, n) == 1
# benchmark of 10000 iterations of fermat(100**10-1); Which is not prime.
# 10000 calls, 21141 per second.
@bnlucas
bnlucas / miller_rabin.py
Last active April 11, 2024 16:43
Miller-Rabin primality testing with Python.
def miller_rabin(n, k=10):
if n == 2:
return True
if not n & 1:
return False
def check(a, s, d, n):
x = pow(a, d, n)
if x == 1:
return True
@bnlucas
bnlucas / solovay_strassen.py
Last active June 3, 2023 14:41
Solovay-Strassen primality testing with Python.
def solovay_strassen(n, k=10):
if n == 2:
return True
if not n & 1:
return False
def legendre(a, p):
if p < 2:
raise ValueError('p must not be < 2')
if (a == 0) or (a == 1):
@bnlucas
bnlucas / primes.py
Last active May 15, 2023 19:38
Working with primes in Python -- Updated.
from fractions import gcd
from random import randrange
PRIMES_LE_31 = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31)
PRIMONIAL_31 = 200560490130
def invmul(x, mod):
if mod <= 0:
raise ValueError('Modulus must be greater than zero.')
@bnlucas
bnlucas / isqrt.py
Last active September 9, 2016 21:51
The most common isqrt() method in Python (isqrt_1). The better isqrt() method in Python (isqrt_2).
import cProfile
from functools import wraps
from time import time
def benchmark(iterations=10000):
def wrapper(function):
@wraps(function)
def func(*args, **kwargs):
@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,
@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))