Skip to content

Instantly share code, notes, and snippets.

@tpitale
tpitale / group_and_count_to_date.sql
Last active August 29, 2015 13:56
Group count by creation date and sum of count-to-date.
SELECT created_on, note_count, sum(note_count) OVER (ORDER BY created_on ASC)
FROM (
SELECT date_trunc('day', created_at) AS created_on, count(1) AS note_count
FROM notes
GROUP BY created_on
ORDER BY created_on DESC
) AS note_counting;
-- WHERE created_on >= '2013-01-01'
@tpitale
tpitale / too_clever.rb
Created March 29, 2014 18:10
Chained enumerable and block arg destructuring
[1,2,3,4,5].each_cons(2).each_with_index do |(a, b), i|
p a
p b
p i
end
# connection and profile selection skipped
class Pageview
extend Legato::Model
metrics :pageviews
dimensions :pagePath, :date
filter :for_path, &lambda { |path| contains(:pagePath, path) }
end
@tpitale
tpitale / wait_for_notify.rb
Last active August 29, 2015 14:01
Implementation of wait_for_notify with PG and Celluloid::IO
def wait_for_notify(&block)
io = pg_connection.socket_io
while @listening do
Celluloid::IO.wait_readable(io) # blocks execution, but unblocks this actor
pg_connection.consume_input # fetch any input on this connection
# read the NOTIFY messages off the socket
while notification = pg_connection.notifies do
block.call(
@tpitale
tpitale / data.tsv
Created May 29, 2014 15:23
Minimally responsive (flexible width) D3 Bar Chart
letter frequency
A .08167
B .01492
C .02782
D .04253
E .12702
F .02288
G .02015
H .06094
I .06966
@tpitale
tpitale / gist:93428db182dd44ec52e5
Created June 11, 2014 03:50
SideBarEnhancement for ST2
import os; path=sublime.packages_path(); (os.makedirs(path) if not os.path.exists(path) else None); window.run_command('exec', {'cmd': ['git', 'clone', 'https://github.com/titoBouzout/SideBarEnhancements', 'SideBarEnhancements'], 'working_dir': path})
import os; path=sublime.packages_path(); window.run_command('exec', {'cmd': ['git', 'checkout', '37429739a0452a2dc36343fb7875ba7fcbeb88a9'], 'working_dir': os.path.join(path, 'SideBarEnhancements')})
@tpitale
tpitale / errors_test.js
Created July 6, 2014 18:38
Failing test for conflict between Errors.content and a model attribute named content
// Added to the bottom of tests/unit/model/errors_test.js
var env, store, Person, Dog;
module("unit/model/errors - model.save() triggers errors", {
setup: function() {
Person = DS.Model.extend({
firstName: DS.attr(),
lastName: DS.attr()
});
@tpitale
tpitale / convert_sf_id.js
Last active August 29, 2015 14:05
Javascript from salesforce id converter to add suffix characters.
function convertId(id) {
if (id == null) return id;
id = id.replace(/\"/g, '');
if (id.length != 15) {
return null;
}
var suffix = "";
for (var i = 0; i < 3; i++) {
var flags = 0;
for (var j = 0; j < 5; j++) {
@tpitale
tpitale / always_center.py
Created August 27, 2014 17:42
Center Sublime Text 2 on current line when changing
import sublime_plugin
class AlwaysCenterCommand(sublime_plugin.EventListener):
def on_modified(self, view):
sel = view.sel()
region = sel[0] if len(sel) == 1 else None
if region != None:
view.show_at_center(region)
@tpitale
tpitale / one_row.sql
Last active August 29, 2015 14:13
One row for each association id SQL
CREATE OR REPLACE VIEW next_episode_ids AS (
SELECT id, ROW_NUMBER() OVER(PARTITION BY show_id ORDER BY airs_on) AS r
FROM episodes
WHERE airs_on >= NOW()
);
CREATE OR REPLACE VIEW next_episodes AS (
SELECT * FROM episodes WHERE id IN (
SELECT id FROM next_episode_ids WHERE r = 1
)