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

Issuing updates part 2 code python sql peewee

# peewee

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

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

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]

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

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)