Skip to content

Instantly share code, notes, and snippets.

@TimSC
TimSC / file-soak-test.cpp
Created May 2, 2014 22:05
This program does test reads and writes to a file.
//Reading and writing tests
//Release under CC0 by Tim Sheerman-Chase, 2014
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <list>
#include <iostream>
#include <string>
using namespace std;
@TimSC
TimSC / file-soak-test.py
Last active August 29, 2015 14:00
This script does test reads and writes to a file.
#This script does test reads and writes to a file.
#Release under CC0 by Tim Sheerman-Chase, 2014
#This script fails on my computer but not on others. Weird.
import random, time, pickle, sys
import numpy as np
def MultiRead(handle, maxLen):
buff = []
@TimSC
TimSC / primes.py
Last active August 29, 2015 14:00
Find prime numbers. Good for giving the CPU some exercise!
import random, math
def FindPrimes(lowLimit, highLimit):
cand = list(range(lowLimit, highLimit))
for i in range(2,lowLimit):
current = i * int(math.floor(lowLimit / i))
while current < highLimit:
if current in cand:
cand.remove(current)
@TimSC
TimSC / compare-folders.py
Created May 11, 2014 12:27
Recursively compare files in two folders
#Recursively compare files in two folders
#by Tim Sheerman-Chase, 2014
#Released under the CC0 license
import sys, os, hashlib
def CheckFolderPair(fo1, fo2, stats):
fiList1 = os.listdir(fo1)
for fina in fiList1:
@TimSC
TimSC / iir.cpp
Last active August 29, 2015 14:04
IIR Filtering
void NaiveIir(const vector<float> &b, const vector<float> &a, const vector<float> &in, vector<float> &out)
{
//IIR filtering, unoptimised
//Call like scipy's lfilter
//http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.lfilter.html
out.resize(0);
out.resize(in.size());
for(int i=0; i < in.size(); i++)
@TimSC
TimSC / osmgrid.py
Created November 11, 2014 19:21
Draw OS grid squares in OSM format
#!/usr/bin/env python
#Draw OS grid squares in OSM format
from ostn02python.OSGB import *
from ostn02python.OSTN02 import *
pointStore = []
vertLines = {}
out = file("grid.osm", "wt")
@TimSC
TimSC / read-entire-file.cpp
Last active August 29, 2015 14:10
Read an entire file in C++
#include <string>
#include <fstream>
#include <vector>
int ReadFileContents(const char *filename, int binaryMode, std::string &contentOut)
{
contentOut = "";
std::ios_base::openmode mode = (std::ios_base::openmode)0;
if(binaryMode)
mode ^= std::ios::binary;
@TimSC
TimSC / hextostr.py
Created December 5, 2014 13:54
Convert binary data to hex representation and back
import string
def BinToHex(a):
return "".join(["%02x" % ord(ch) for ch in a])
def HexToBin(a):
out = []
for i in range(0, len(a), 2):
twoChrs = a[i:i+2]
v = string.atoi(twoChrs, 16)
@TimSC
TimSC / diffang.js
Created February 14, 2015 21:03
Difference between two angles
function relativeBearing(b1Rad, b2Rad)
{
b1y = Math.cos(b1Rad);
b1x = Math.sin(b1Rad);
b2y = Math.cos(b2Rad);
b2x = Math.sin(b2Rad);
crossp = b1y * b2x - b2y * b1x;
dotp = b1x * b2x + b1y * b2y;
if(crossp > 0.)
return Math.acos(dotp);
@TimSC
TimSC / ftp-list-files.py
Created May 30, 2015 10:57
Save FTP folder listing to a local file
import os
from ftplib import FTP
if __name__ == "__main__":
ftp = FTP('ftp.debian.org') # connect to host, default port
ftp.login() # user anonymous, passwd anonymous@
ftp.cwd('debian') # change into "debian" directory
outFile = open("out.txt", "wt")
ftp.retrlines('LIST', lambda x: outFile.write(x+os.linesep)) # list directory contents