Blurbs ansible coffeescript django dvcs erlang es6 hg javascript json lua mercurial peewee postgresql python scope sql sqlalchemy yaml

Iterating part 2 code javascript es6

let a = [1, 2, 3, null, 5];
a.six = 6;
a.seven = 7;

for (let i = 0; i < a.length; i++) {
    console.log(a[i]); // 1, 2, 3, null, 5
}

for (let v of a) {
    console.log(v); // 1, 2, 3, null, 5
}

for (let k in a) {
    console.log(a[k]); // 1, 2, 3, null, 5, 6, 7
}

Iterating part 1 code lua

local a = { 1, 2, 3, nil, 5, six = 6, seven = 7 };

for i = 1, #a do
    print(a[i]) -- 1, 2, 3, nil, 5
end

for k, v in ipairs(a) do
    print(v) -- 1, 2, 3
end

for k, v in pairs(a) do
    print(v) -- 1, 2, 3, 5, 7, 6
end

Ansible task styles code ansible yaml

- name: Eh
  apt: pkg=foo state=latest

- name: Eww
  apt: >
    pkg=foo
    state=latest

- name: Yeah
  apt:
    pkg: foo
    state: latest

Parens part 2 code lua

local model = {
    status = 'active',

    get = function(self, key) return self[key] end
}

local a = model:get 'status' == 'active' -- true

Issuing updates part 2 code python sql peewee

# peewee

query = Entry.update(counter=Entry.counter + 1)
query.execute()

Parens code coffeescript javascript

# coffeescript
a = model.get 'status' is 'active'
// generated javascript
var a;
a = model.get('status' === 'active');

FILTER clause code postgresql sql

SELECT COUNT(*) AS total, COUNT(*) FILTER (WHERE i % 2 != 0) AS odd
FROM generate_series(1, 10) AS s(i);

--  total | odd
-- -------+-----
--     10 |   5
-- (1 row)

Globals and locals part 3 code python scope

a = 1

def g():
    print(a)
    print([a for a in range(3)])

g()

# Python 2 only (leaky comprehensions):
# >>> UnboundLocalError: local variable 'a' referenced before assignment

Globals and locals part 2 code python scope

a = 1

def f():
    a = a + 1
    # or even shorter!
    a += 1

f()

# >>> UnboundLocalError: local variable 'a' referenced before assignment

hg merge branch code hg mercurial dvcs

hg update stable; hg merge default # oops, we did that backwards
hg branch default; hg commit

HAVING COUNT(*) code postgresql sql

SELECT section_id
FROM section_relations
GROUP BY section_id
HAVING COUNT(*) > 500;

UPDATE FROM code postgresql sql

UPDATE users u SET hobby = (SELECT hobby FROM hobbies h WHERE u.id = h.user_id);

UPDATE users u SET hobby = h.hobby FROM hobbies h WHERE u.id = h.user_id;

Sort list of tuples code python erlang

a = [(1, 2), (3, 4), (5, 6)]
sorted(a, cmp=lambda a, b: b[1] - a[1])
# [(5, 6), (3, 4), (1, 2)]

from operator import itemgetter
sorted(a, key=itemgetter(1), reverse=True)
# [(5, 6), (3, 4), (1, 2)]
A = [{1, 2}, {3, 4}, {5, 6}].
lists:sort(fun({_, A}, {_, B}) -> A >= B end, A).
% [{5,6},{3,4},{1,2}]

lists:reverse(lists:keysort(1, A)).
% [{5,6},{3,4},{1,2}]

Stuttering list code python erlang

a = [1, 2, 3, 4]
[i for i in a for _ in range(i)]
# [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
A = [1, 2, 3, 4].
[I || I <- A, _ <- lists:seq(1, I)].
% [1,2,2,3,3,3,4,4,4,4]

DISTINCT ON code postgresql sql

SELECT DISTINCT ON (genre) artist, genre, count_by_genre
FROM artists_and_genres
ORDER BY genre, count_by_genre DESC;

RETURNING code postgresql sql json

UPDATE questions SET bounty = bounty * 2 WHERE id = 5432
RETURNING bounty;

WITH upd AS (
  UPDATE employees SET sales_count = sales_count + 1 WHERE id = 1
  RETURNING *
)
INSERT INTO elog SELECT row_to_json(upd), current_timestamp FROM upd;

CTE code postgresql sql

WITH busytables AS (
  SELECT relname, (seq_scan + idx_scan) AS scans
  FROM pg_stat_user_tables
)
SELECT relname, scans
FROM busytables
WHERE scans > 100
ORDER BY scans DESC;

pg_stats code postgresql sql

SELECT attname, n_distinct, most_common_vals, most_common_freqs
FROM pg_stats
WHERE tablename = 'my_books';

pg_stat_user code postgresql sql

SELECT relname, indexrelname, idx_scan, idx_tup_read, idx_tup_fetch
FROM pg_stat_user_indexes
ORDER BY relname;

SELECT relname, seq_scan, seq_tup_read, idx_scan, idx_tup_fetch
FROM pg_stat_user_tables
ORDER BY relname;

array_to_json code postgresql json sql

SELECT array_to_json(array_agg(books))
FROM (SELECT isbn, author, title FROM my_books) AS books;

Globals and locals code python scope

item = 'spam'

def p():
    print item # <<<
    for item in ['foo', 'bar']:
        print item

p()

# >>> UnboundLocalError: local variable 'item' referenced before assignment

# variable global to a scope that you reassign within that scope
# is marked local to that scope by the compiler.

http://stackoverflow.com/questions/404534/python-globals-locals-and-unboundlocalerror

Defaults binding code python scope

for i in range(10):
    def callback(i=i):
        print "clicked button", i
    UI.Button("button %s" % i, callback)

# The “i=i” part binds the parameter “i” (a local variable)
# to the current value of the outer variable “i”.

http://effbot.org/zone/default-values.htm

Closure without return code javascript scope

var bar;
function foo(x) {
    bar = function() {
        alert(x);
    }
};
foo(5);
bar();

Issuing updates code python sql sqlalchemy django

# sqlalchemy

from sqlalchemy import update

stmt = update(entry).values(counter=entry.c.counter + 1)
conn.execute(stmt)


# sqlalchemy.orm

session.query(Entry).update({'counter': Entry.counter + 1})


# django

from django.db.models import F

Entry.objects.update(counter=F('counter') + 1)