Skip to content

Instantly share code, notes, and snippets.

View h2rd's full-sized avatar

Igor Skrynkovskyy h2rd

  • Route4Me
  • Lviv, Ukraine
View GitHub Profile
@h2rd
h2rd / qr.py
Created October 24, 2013 16:00
from argparse import ArgumentParser
from math import sqrt
from urllib import urlencode
import sys
MAX_SIZE = 300000
def main():
l = dict(a=1, b=2, c=3,
d=4, f=5)
dict(((v, k) for k, v in l.iteritems()))
{v:k for k, v in l.iteritems()}
dict(zip(l.values(), l.keys()))
import heapq
class PriorityQueue:
def __init__(self):
self._queue = []
self._index = 0
def push(self, item, priority):
heapq.heappush(self._queue, (-priority, self._index, item))
self._index += 1
@h2rd
h2rd / fabric_add.py
Last active December 28, 2015 07:38
function
def fabric(fnum):
def add(num):
return fnum + num
return add
def addition(num):
return lambda x, num=num: x+num
if __name__ == '__main__':
@h2rd
h2rd / list_fabrics.py
Created November 14, 2013 11:43
functions
def addition_range(f, t):
l = []
for n in range(f, t):
l.append(lambda x, n=n: x+n)
return l
if __name__ == '__main__':
additionals = addition_range(0, 3)
assert additionals[0](1), 1
@h2rd
h2rd / cl.py
Created November 15, 2013 21:22
class Observable(object):
def __init__(self, **kwargs):
for key, val in kwargs.items():
setattr(self, key, val)
def __format(self):
attrs = ['%s=%s' % (key, val)
for key, val in self.__dict__.iteritems() if not key.startswith('_')]
return 'Observable(%s)' % (', '.join(attrs))
@h2rd
h2rd / dict_attr.py
Last active December 28, 2015 11:18
class DictAttr(object):
def __init__(self, attrs):
self.attrs = dict(attrs)
def __getitem__(self, key):
if key not in self.attrs:
raise AttributeError
return self.attrs[key]
a = [1,2,6,3]
b = [4,2,1,5,3]
list(set(a).symmetric_difference(set(b)))
# 4, 5, 6
# (a - b) + (b - a)
@h2rd
h2rd / dc.py
Last active December 30, 2015 13:59
#!/usr/bin/env python
# encode: utf8
from __future__ import print_function
import os
import shutil
import sys
DROPBOX_USER_ID = ''
DROPBOX_PUBLIC_FOLDER = '/Users/h2rd/Dropbox/Public/'
from pprint import pprint
import mysql.connector
class MySQLCursorDict(mysql.connector.cursor.MySQLCursor):
def _row_to_python(self, rowdata, desc=None):
row = super(MySQLCursorDict, self)._row_to_python(rowdata, desc)
if row:
return dict(zip(self.column_names, row))
return None