Skip to content

Instantly share code, notes, and snippets.

!ls
import subprocess, sys
!pip install {library}
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'pkg_name'])
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
import requests
import lxml.html as lh
import pandas as pd
header = { "User-Agent"; #....
r = requests.get(url, headers=header)
df = pd.read_html(r.text)[0]
######################################
def fib(n):
v1, v2, v3 = 1, 1, 0 # initialise a matrix [[1,1],[1,0]]
for rec in bin(n)[3:]: # perform fast exponentiation of the matrix (quickly raise it to the nth power)
calc = v2*v2
v1, v2, v3 = v1*v1+calc, (v1+v3)*v2, calc+v3*v3
if rec=='1': v1, v2, v3 = v1+v2, v1, v2
return v2
@mbadros
mbadros / conda_revisions
Created August 29, 2019 13:48
Roll back your Anaconda distribution to an earlier version
#
# Source: http://blog.rtwilson.com/conda-revisions-letting-you-rollback-to-a-previous-version-of-your-environment/
# Changes to Anaconda distribution and packages
conda list --revisions
# Revert to a previous revision
import numpy
def sieve8(n):
"""Return an array of the primes below n."""
prime = numpy.ones(n//3 + (n%6==2), dtype=numpy.bool)
for i in range(3, int(n**.5) + 1, 3):
if prime[i // 3]:
p = (i + 1) | 1
@mbadros
mbadros / shift_func
Created January 10, 2018 19:32
The function takes as input the vector x and a specified integer lag, and shifts the input series by that amount. NA’s are added to either the beginning or the end (depending on the sign of lag) to pad the shifted vector to be the same length as the input. Note that lag is defined so that a positive lag shifts x “to the right”, i.e. moves values…
# R Function to Shift Vectors
# http://clarkrichards.org/r/timeseries/2016/02/09/a-function-to-shift-vectors/
#
shift <- function(x, lag) {
n <- length(x)
xnew <- rep(NA, n)
if (lag < 0) {
xnew[1:(n-abs(lag))] <- x[(abs(lag)+1):n]
} else if (lag > 0) {
### Keybase proof
I hereby claim:
* I am mbadros on github.
* I am mbadros (https://keybase.io/mbadros) on keybase.
* I have a public key ASAw2MSDq8rPTnECkZ4ZbR5BGa-SWaPvKrvxtllX0kVnogo
To claim this, I am signing this object:
@mbadros
mbadros / 0_reuse_code.js
Created August 19, 2014 19:26
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