This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from argparse import ArgumentParser | |
| from math import sqrt | |
| from urllib import urlencode | |
| import sys | |
| MAX_SIZE = 300000 | |
| def main(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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())) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fabric(fnum): | |
| def add(num): | |
| return fnum + num | |
| return add | |
| def addition(num): | |
| return lambda x, num=num: x+num | |
| if __name__ == '__main__': |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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/' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |