Skip to content

Instantly share code, notes, and snippets.

View zelark's full-sized avatar

Aleksandr Zhuravlёv zelark

View GitHub Profile
# Try it out. It works!
x = [[1,2], [3,4], [5,6]]
x = [x for x in x for x in x]
# x = [1, 2, 3, 4, 5, 6]
a = [[1, 2], [3, 4], [5, 6]]
b = [x for l in a for x in l]
# b = [1, 2, 3, 4, 5, 6]
@zelark
zelark / debug_foo.py
Created March 25, 2014 06:41
extract traceback stack
import traceback
def foo():
x = [1, 2, 3]
print(traceback.extract_stack())
if __name__ == '__main__':
foo()
@zelark
zelark / python_magic.same_variable.00.py
Last active August 29, 2015 13:57
The same variable through a list comprehension.
def foo():
x0 = [[1, 2], [3, 4], [5, 6]]
y = [x2 for x1 in x0 for x2 in x1]
print y
def bar():
x = [[1, 2], [3, 4], [5, 6]]
x = [x for x in x for x in x]
print x
create table tmp_t1
(
amount number,
txt varchar2(50 char)
);
insert into tmp_t1
(amount, txt)
values
(1, 'text 1');
select regexp_replace(str, '(foo|foos|fooes)', 'bar', 1, 1, 'i') as val
from (select 'aa_fOo_bb' str from dual);
>>> my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> my_list
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
@zelark
zelark / vm.py
Created April 4, 2014 14:41
python virtual machine
# http://tech.blog.aknin.name/2010/04/02/pythons-innards-introduction/
# http://tech.blog.aknin.name/category/my-projects/pythons-innards/
# http://akaptur.github.io/blog/2013/08/14/python-bytecode-fun-with-dis/
# from vm import VirtualMachine
# vm = VirtualMachine()
# vm.byte_LOAD_CONST('a')
# vm.byte_LOAD_CONST('b')
# vm.byte_BUILD_LIST(2)
# vm.byte_BUILD_LIST(0)
@zelark
zelark / rpg.py
Created April 9, 2014 14:35
Пишем простую RPG на Clojure (https://www.youtube.com/watch?v=r0TWL5L7RE0)
from random import randint
def kill_negative(n):
if n < 0:
n = 0
return n
def calc_attack(level):
return level*6
@zelark
zelark / avg_nullif.sql
Created April 14, 2014 07:36
Calculate the avarage without non-zero values and given zero values in the next column.
with data as (
select 50 as col1, 0 as col2 from dual union all
select 0 as col1, 20 as col2 from dual union all
select 20 as col1, 10 as col2 from dual union all
select 20 as col1, 10 as col2 from dual
)
select avg(nullif(col1, 0)) as col1_avg_no_zero,
avg(nullif(case when col2 = 0 then 0 else col1 end, 0)) as col1_avg_no_zero_with_col2
from data;