Skip to content

Instantly share code, notes, and snippets.

"""
http://stackoverflow.com/questions/7632963/numpy-find-first-index-of-value-fast
First you need to run:
f2py -c -m search search.f90
"""
from time import time
from matplotlib.pyplot import subplots, show
!!
!! f2py -c -m search search.f90
!!
subroutine find_first(needle, haystack, haystack_length, index)
!!
!! Find the first index of `needle` in `haystack`.
!!
implicit none
# python 3 + numpy
from numpy import iinfo, int64
q = iinfo(int64).max // 2**10
q_lim = q + 3
while q < q_lim:
print('hello')
@mverleg
mverleg / quiz_02.py
Last active November 23, 2016 20:32
def mygen():
for k in range(5):
a = yield
yield a**2
return k
r = mygen()
for k, x in enumerate(r):
# python 3 + numpy
from numpy import iinfo, int64
# iinfo(int64).max will give the maximum value of an 8-byte precision integer,
# which should be 2^63 - 1 = 9223372036854775807
# (there is one bit for the sign, and a shift by 1 because of zero)
# The trick is that a 8-byte float uses 11 bits for the exponent
# This creates a generator.
def mygen():
for k in range(5):
# This yields None, and assigns to `a` the value that's sent in after.
a = yield
# This yields `a**2` and discards any value sent it.
yield a**2
# This returns the final value of k, which will be set as the StopIteration
# `err.value`, which in this case is not used. (It's 4)
@mverleg
mverleg / extract-clementine-playlist-files.py
Last active April 23, 2017 18:16
Copy all the track files from a Clementine playlist to a separate directory.
"""
Reads a Clementine playlist, and copies all the song files to a directory.
arg 1: path to the playlist
arg 2: optionally, the output directory
You can ignore the BeautifulSoup parser warning (or fi you insist, follow it's directions)
"""
from bs4 import BeautifulSoup, Tag # pip install beautifulsoup4
from shutil import copy2
@mverleg
mverleg / pycharm_jump_to_stracktrace_location.py
Created February 26, 2017 12:05
Copy a python stacktrace, then laucnh this script to open the appropriate file in PyCharm (v1)
"""
Get stacktrace from the clipboard, and open the relevant file/line in the
current PyCharm project (useful when using terminal instead of PyCharm run).
Arguments:
1. The path to the PyCharm launcher script (e.g. "~/pycharm/bin/pycharm.sh")
2. [Optional] A json dictionary of path substitutions (e.g '{"/remote/userA": "/home/userA"}')
https://www.jetbrains.com/help/pycharm/2016.3/opening-files-from-command-line.html
@mverleg
mverleg / quiz_03.java
Last active March 16, 2017 09:31
Java inheritane of attributes and methods
/*
The question is simply: what is the output?
Extra: what is `instance.z` after line 8?
*/
public class TestQuestion1 {
public static void main(String[] args) {
SuperCls instance = new SubCls();
}
}
/*
For the question, see
https://gist.github.com/mverleg/1088fbb5f21590da6691a6e84547bada
OUTPUT:
super constructor
super b
x = 1, y = 3
sub b
x = null, y = 3