Skip to content

Instantly share code, notes, and snippets.

View richard-to's full-sized avatar

Richard To richard-to

View GitHub Profile
@richard-to
richard-to / csv_to_sqlite3.py
Last active August 29, 2015 14:01
Random snippet to insert csv into sqlite3. Mainly just want to remember date time conversion to unix timestamp and associated issues with timezones, ie strptime seems to use local timezone and then mktime will be utc
import csv
import time
import sqlite3
from datetime import datetime
readings = []
with open('data.csv', 'r') as csvfile:
data = csv.reader(csvfile)
next(data)
for line in data:
@richard-to
richard-to / gist:0fcd4d5251a80121a731
Created June 6, 2014 00:59
Numpy slicing to get last column and everything but last column
data = np.array([
[1.0, 1.1, 1],
[1.0, 1.0, 1],
[0.0, 0.0, 2],
[0.0, 0.1, 2],
])
labels = data[:,-1]
points = data[0:,:-1]
def datetime_to_timestamp(date, date_format):
"""
Converts date/time string to unix timestamp. Note that
the date/time string will interpreted using the local
timezone on your system.
The resulting unix timestamp will be in GMT time.
"""
dt = datetime.strptime(date, date_format)
return int(time.mktime(dt.timetuple()))
@richard-to
richard-to / gist:eaa10f06f550a7ae4bf8
Created July 2, 2014 23:45
Quick and dirty script to generate fake sensor data for testing
function r(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var type = 's';
var start_time = 1404313860;
var interval = 2;
var readings = 10;
for (var g = 0; g < 10; g++)
{
@richard-to
richard-to / burn_image_to_sd_card.sh
Last active August 29, 2015 14:03
Burning image to SD Card on OSX using Terminal
#!/bin/bash
# First unzip the disc image with Keka: Right click on the xz file and then do "Open With" and pick Keka
# Next make sure you have an empty microSD card. 4GB minimum will be required.
# List disks on system and find location of microSD card. Look for the 4GB one
diskutil list
# WARNING: Make sure you selected the correct disk. You do not want to overwrite your hard drive,
@richard-to
richard-to / BridgeSensorD.ino
Last active August 29, 2015 14:03
6 BridgeSensor Tests with Full Kit
/*
Bridge Sensor with GSM, SDCard, and RTC
Sketch used by ARTF Sensors platform.
Created 14 6 2014
Modified 13 7 2014
*/
#include <LowPower.h>
@richard-to
richard-to / Gruntfile.js
Created July 19, 2014 09:30
Gruntfile vs. gulpfile...
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
clean: ['js', 'css', 'fonts'],
copy: {
main: {
files: [
{
@richard-to
richard-to / gist:0fb1e384084c20ffaa42
Created July 28, 2014 15:32
Random snippet to zip separate columns into list and then turn each row into dict.
keys = ('FacilityID', 'FacilityName', 'City', 'Zip', 'Score', 'InspDate')
facilities_data = [data[key].split('|') for key in keys]
facilities = [dict(zip(keys, row)) for row in zip(*facilities_data)]
#! /usr/bin/env python
import argparse
from datetime import datetime
from serial import Serial
from sqlite3 import ProgrammingError
from time import sleep
from core import DataParser, SensorReadingsDataStore, MessageFormatError
from utils import get_config, create_logger_from_config, parse_sensor_types_config
@richard-to
richard-to / router.js
Last active August 29, 2015 14:05
Simple HTML5 history router. No dependencies. No fallback if HTML5 history is not supported. Just ignored. Work in progress. Untested.
(function() {
// Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}
var _delim = '/';
var _regexDelim = "\\/";