Skip to content

Instantly share code, notes, and snippets.

Avatar

Diego Alonso etale-cohomology

View GitHub Profile
View timeit.py
@contextlib.contextmanager # Create factory function for *with* context managers
def timeit():
"""Time execution of code, based on time.clock(). Call using a *with* statement!
Examples:
with timeit():
a @ a
"""
start = time.clock()
yield # The decorated func must return a generator-iterator
@etale-cohomology
etale-cohomology / vispy0.py
Created Mar 11, 2016
A small working example of vispy, along with some notes
View vispy0.py
"""Vispy is based on OpenGL ES 2.0.
Vispy offers a Pythonic object-oriented interface to OpenGL, useful to those
who know OpenGL.
It is critical to use as few OpenGL draw calls as possible. Every draw incurs
a significant overhead. High performance is achieved by rendering all similar
primitive types at once (batch rendering).
The vertex shader is executed for EACH vertex that is given to the rendering
@etale-cohomology
etale-cohomology / save_ndarray.py
Created Mar 11, 2016
Save a NumPy array to disk with optional compression using 7z
View save_ndarray.py
def save_ndarray(ndarray, filename, compress=False):
"""Store a single NumPy array on an .npy binary file. Tries to keep allow_pickle at False.
Optional multithreaded compression using 7-zip (needs to be in the PATH).
Args:
ndarray (ndarray): Array to be stored on disk.
filename (str): The file's name on disk. No need for .npy extension
compress (bool, optional): Set to True to save array as .npy and then compress it to .7z
Returns:
@etale-cohomology
etale-cohomology / load_ndarray.py
Created Mar 11, 2016
Load a NumPy array from disk, either from a .npy file or from a .7z file. No need to include extension
View load_ndarray.py
def load_ndarray(filename):
"""Load a NumPy array from disk into memory, with extension .npy or .7z. If no extension is
included in the argument, first assume it's .npy, then .7z.
Args:
filename (str): Name of the NumPy array (in disk).
Returns:
ndarray
"""
@etale-cohomology
etale-cohomology / calculus0.py
Created Oct 11, 2017
The fundamental theorem of calculus, in discrete version!
View calculus0.py
# The fundamental theorem of calculus, in discrete version!
import numpy as np
np.set_printoptions(linewidth=200, precision=2)
print('This Python snippet shows how the sum of many differences is one difference of endpoinds!')
print('You can consider this the "discrete fundamental theorem of calculus"!')
# ----------------------------------------------------------------------
N_VALUES = 16
@etale-cohomology
etale-cohomology / add1.cu
Last active Oct 12, 2017
A minimal CUDA program that naively performs vector addition, component-wise!
View add1.cu
// Compile and run with:
// nvcc add1.cu -o add1 && ./add1
// This minimal CUDA program performs vector addition, component-wise!
#include <stdio.h>
#define N_ELEMENTS 8
@etale-cohomology
etale-cohomology / compress HTTP GET requests
Last active Oct 21, 2021
The 'Accept-Encoding' HTTP request header can be used to tell the HTTP server you want compressed data. The server responds with a `Content-Encoding` header.
View compress HTTP GET requests
curl https://api.binance.com/api/v3/exchangeInfo --header 'Accept-Encoding: gzip, deflate' | gunzip -