Skip to content

Instantly share code, notes, and snippets.

import urllib
import os
from time import gmtime, strftime
import subprocess
BASE_URL = "http://commondatastorage.googleapis.com/books/ngrams/books/";
downloading = False
def getCsvFileName(index):
return "googlebooks-eng-all-3gram-20090715-" + str(index) + ".csv"
import numpy as np
from math import ceil
def FFTCompress(vals, max_mse):
#normalize
norm = np.linalg.norm(vals)
vals = [v / norm for v in vals]
#calc fft and
fx = np.fft.fft(vals)
#only the first ceil(n/2) coefficients are required as the input is real
#Conversion
def TwoTagSymbolToBinaryWord(A, s):
idx = A.index(s)
return ('0' * idx) + '1' + '0' * (len(A) - idx - 1)
def TwoTagWordToBinaryWord(A, w):
t = ''
for i in xrange(len(w)):
t += TwoTagSymbolToBinaryWord(A, w[i])
return t
@barvinograd
barvinograd / simple_python_crawler.py
Created September 17, 2013 09:23
A simple python crawler. Produces a list wiki page view counts file dumps, but fairly generic.
#!/usr/bin/python
import urllib2
import urlparse
import re
from bs4 import BeautifulSoup
downloadQ = []
downloaded = set()
@barvinograd
barvinograd / yahoo_historical_prices.py
Last active December 23, 2015 06:09
Yahoo! Historical Stock Prices Crawler. Downloads historical prices of stocks. Ticker lists is available from http://www.nasdaq.com/screening/company-list.aspx
#!/usr/bin/python
import urllib2
import datetime
from os.path import isfile, join
from os import getcwd
tFile = open("tickers.txt")
eYear = datetime.datetime.now().year
eMonth = datetime.datetime.now().month
@barvinograd
barvinograd / RecursiveIterator.cs
Created June 3, 2013 18:10
A recursive simulator which uses an internal stack instead of the .NET stack. DFS traversing over the recursion.
/// <summary>
/// A recursive simulator which uses an internal stack instead of the .NET stack. DFS traversing over the recursion.
/// </summary>
/// <typeparam name="INPUT">The input of the recursive function</typeparam>
/// <typeparam name="OUTPUT">The output of the recursive function</typeparam>
/// <example>
/// var fibonachi = new RecursiveIterator<int, int>()
/// {
/// Branch = n => new int[] { n - 1, n - 2 },
/// TrySetTrivialResult = (n, s) =>
@barvinograd
barvinograd / CachedRecursiveIterator.cs
Last active December 18, 2015 00:58
A recursive simulator which uses an internal stack instead of the .NET stack. DFS traversing over the recursion. The simulator will cache intermediate results and avoid unnecessary calls.
/// <summary>
/// A recursive simulator which uses an internal stack instead of the .NET stack. DFS traversing over the recursion. The simulator will cache intermediate results and avoid unnecessary calls.
/// </summary>
/// <typeparam name="INPUT">The input of the recursive function</typeparam>
/// <typeparam name="OUTPUT">The output of the recursive function</typeparam>
/// <example>
/// var fibonachi = new CachedRecursiveIterator<int, BigInteger>()
/// {
/// Branch = n => new int[] { n - 1, n - 2 },
/// TrySetTrivialResult = (n, s) =>
@barvinograd
barvinograd / BufferQueueStream.cs
Created October 30, 2012 14:43
Buffer Queue Stream
public class BufferQueueStream : Stream, IDisposable
{
private const int INTERNAL_BUFFER_SIZE = 64 * 1024;
private System.ServiceModel.Channels.BufferManager bufferManager = System.ServiceModel.Channels.BufferManager.CreateBufferManager(INTERNAL_BUFFER_SIZE * 100, INTERNAL_BUFFER_SIZE);
private Queue<byte[]> _bufferQueue = new Queue<byte[]>();
private byte[] _currentReadingBuffer;
private int _currentBufferPosition;
private long _totalLengthToRead = 0;
private long _position = 0;