Issuing updates part 2 code
# peewee
query = Entry.update(counter=Entry.counter + 1)
query.execute()
# peewee
query = Entry.update(counter=Entry.counter + 1)
query.execute()
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
a = 1
def f():
a = a + 1
# or even shorter!
a += 1
f()
# >>> UnboundLocalError: local variable 'a' referenced before assignment
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}]
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]
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
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”.
# 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)