Skip to content

Instantly share code, notes, and snippets.

View waveform80's full-sized avatar

Dave Jones waveform80

View GitHub Profile
@waveform80
waveform80 / rgb_colors.py
Created June 28, 2015 17:42
A quick demo of using PiRGBAnalysis for (semi) rapid analysis of RGB data
from __future__ import division
import picamera
import picamera.array
import numpy as np
class MyAnalysis(picamera.array.PiRGBAnalysis):
def __init__(self, camera):
super(MyAnalysis, self).__init__(camera)
self.frame_num = 0
@waveform80
waveform80 / gist:4124157
Created November 21, 2012 10:29
LEAST alternative
CASE WHEN ContractPrice < DiscountPrice
THEN
CASE WHEN ContractPrice < ListPrice THEN ContractPrice ELSE ListPrice END
ELSE
CASE WHEN DiscountPrice < ListPrice THEN DiscountPrice ELSE ListPrice END
END
@waveform80
waveform80 / gist:4124953
Created November 21, 2012 13:54
Example of recursive date generation in SQL
CREATE VIEW some_dates AS
WITH RECURSIVE dates(i, d) AS (
VALUES (1, DATE('1970-01-01'))
UNION ALL
SELECT i + 1, d + 1 DAY FROM dates WHERE d < DATE('2070-12-31')
)
SELECT
d.d AS a_date,
YEAR(d.d) AS a_year,
MONTH(d.d) AS a_month,
from gpiozero import RyanteckRobot, Button
from signal import pause
robot = RyanteckRobot()
button_actions = {
Button(26): robot.forward,
Button(16): robot.left,
Button(21): robot.right,
Button(20): robot.backward,
@waveform80
waveform80 / gist:4657034
Last active December 11, 2015 20:39
Pivotting columns in SQL...
WITH qdps_with_rows AS (
SELECT
ROW_NUMBER() OVER (PARTITION BY q.product_code) AS row_num,
q.*
FROM qdps q
)
SELECT
p.product_code,
SUM(CASE WHEN q.row_num = 1 THEN q.value END) AS qdp1,
SUM(CASE WHEN q.row_num = 2 THEN q.value END) AS qdp2,
@waveform80
waveform80 / getladata.py
Created May 2, 2013 17:17
Converting flat SQL output to a JSON array of hashes in Python
#!/usr/bin/env python
import json
from itertools import izip, cycle, groupby
from operator import itemgetter, attrgetter
# Imagine this is the result of some DB query like:
# SELECT region, year, value FROM some_table ORDER BY region, year
data = [
('Southwark', 2009, 71.89),
def process_iis_directory(d):
print('Processing IIS directory %s' % d)
for entry in os.listdir(d):
entry = os.path.join(d, entry)
if entry.endswith('.gz'):
print('Processing %s into CSV' % entry)
with gzip.open(entry, 'rb') as uncompressed, \
io.BufferedReader(uncompressed) as buffered, \
io.TextIOWrapper(buffered, encoding='latin1') as infile, \
io.open(os.path.splitext(entry)[0] + '.csv', 'wb') as outfile, \
@waveform80
waveform80 / factorial.py
Last active December 25, 2015 19:48
Factorial in a functional style
def factorial(i):
if i > 1:
return factorial(i - 1) * i
elif i == 1:
return 1
else:
raise ValueError('Cannot take factorial of %d' % i)
from gpiozero import TrafficLights
from time import sleep
from itertools import cycle, chain
from signal import pause
lights = TrafficLights(12, 16, 19, pwm=True)
def slow(gen):
for item in gen:
yield item
@waveform80
waveform80 / cacachart.py
Last active September 28, 2016 08:36
ASCII art charts!
from __future__ import (
unicode_literals,
absolute_import,
print_function,
division,
)
from itertools import tee
from collections import deque
from time import sleep