Skip to content

Instantly share code, notes, and snippets.

View nosamanuel's full-sized avatar

Noah Seger nosamanuel

View GitHub Profile
@nosamanuel
nosamanuel / upsert.sql
Created April 24, 2014 16:26
Simple upsert in Postgres using a writeable CTE
create table foo (id serial primary key, data json);
with updated_foo as (
update foo set data = '"bar"' where id = 1
returning id
)
insert into foo (id, data)
select 1, '"foo"'
where not exists(select id from updated_foo);
@nosamanuel
nosamanuel / pep380.py
Created April 21, 2014 17:40
PEP 380
def foo():
yield 1
yield 2
yield 3
def bar():
yield 'a'
yield 'b'
yield 'c'
@nosamanuel
nosamanuel / destructure.py
Created February 9, 2014 00:04
Destructuring bind in Python
import ast
import inspect
class destructure(object):
def __init__(self, target):
self.target = target
def __enter__(self):
# 1. Get the stack frame that called `destructure`
@nosamanuel
nosamanuel / gist:8888183
Last active August 29, 2015 13:56
Python has a record of variables defined after the current frame
import inspect
frame = inspect.currentframe()
assert 'asdf' in frame.f_code.co_names
assert "asdf = 'asdf'" not in inspect.getsource(frame)
asdf = 'asdf'
<snippet>
<content><![CDATA[
@property
def ${1:name}(self):
${2:pass}
]]></content>
<tabTrigger>prop</tabTrigger>
<scope>source.python</scope>
<description>New Property Decorator</description>
</snippet>
@nosamanuel
nosamanuel / fail_in_exception.py
Created November 24, 2013 03:35
try/finally in a bare except block can safely re-raise the original error.
def do_something():
raise RuntimeError('do_something')
def clean_up():
raise RuntimeError('clean_up')
def fail_in_exception():
try:
import inspect
class Selfless(type):
def __new__(cls, name, bases, attrs):
# Detect any methods without `self` as the first argument
selfless_methods = {}
for k, v in attrs.items():
if callable(v):
try:
@nosamanuel
nosamanuel / gist:5180376
Created March 17, 2013 06:23
Curiosity about garbage collection and generator.send
state = {}
def generator():
state['generator'] = yield None
yield None
def main():
g = generator()
<A HREF="/HeartLand-Gaien/7211/kudos10/okurimono.html">
<P ALIGN="CENTER">
<LI>心からの贈り物J
</A>
</LI>
</P>
var session = FB.getSession();
var query = FB.Data.query("select uid2 from friend where uid1 = {0}", session.uid);
var friend_ids = new Array();
FB.Data.waitOn([query], function() {
FB.Array.forEach(query.value, function(row) {
friend_ids.push(row.uid2);
});
console.log(friend_ids);
});