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 itertools import islice | |
from typing import List, Iterator | |
def chunks(iterator: Iterator, step: int) -> List: | |
""" | |
Yield successive step-sized chunks from iterator | |
""" | |
iterable = iter(iterable) | |
while True: |
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 hashlib | |
def dict_hash(d: dict): | |
""" | |
Hash dictionary's key and value in string as the hash value of the dict | |
""" | |
out = hashlib.md5() | |
for key, value in d.items(): | |
out.update(str(key).encode('utf-8')) |
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 aiohttp | |
import asyncio | |
NUMBERS = range(12) | |
URL = 'http://httpbin.org/get?a={}' | |
async def fetch_async(a): | |
async with aiohttp.ClientSession() as session: |
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
# In CPython implementation of Python 3.6, dictionary keeps the insertion order. | |
# From Python 3.7, this will become a language feature. | |
# In order to sort a dictionary by key including nested dictionary inside, we can do: | |
def sort_dict(item: dict): | |
""" | |
Sort nested dict | |
Example: | |
Input: {'a': 1, 'c': 3, 'b': {'b2': 2, 'b1': 1}} | |
Output: {'a': 1, 'b': {'b1': 1, 'b2': 2}, 'c': 3} |
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
# This script generates a .py file with a dictionary of Font Awesome icon mapping, like '500px': '\uf26e' | |
# Run: | |
# python generate_fontawesome_mapping.py > fontawesome_mapping.py | |
import yaml | |
import requests | |
import sys | |
INDENT = ' ' * 4 |
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 division(x: int, y: int, method: str = 'float') -> Union[int, float]: | |
""" | |
:param x: dividend | |
:param y: divisor | |
:param method: {'float', 'round', 'ceil', 'floor'} | |
""" | |
method_mapping = { | |
'float': lambda a, b: a / b, | |
'nearest': lambda a, b: round(a / b), | |
'ceil': lambda a, b: int(a // b + bool(a % b)), |
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
# Lazy dict allows storing function and argument pairs when initializing the dictionary, | |
# it calculates the value only when fetching it. | |
# In this examole, if the key starts with '#', it would accept a (function, args) tuple as value and | |
# returns the calculated result when fetching the values. | |
from collections.abc import Mapping | |
class LazyDict(Mapping): | |
def __init__(self, *args, **kw): |
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 dictmysql # or whatever db library | |
class LazyConnector: | |
def __init__(self, *args, **kwargs): | |
self.__dict__['__factory'] = dictmysql.DictMySQL | |
self.__dict__['__con'] = None | |
self.__dict__['__args'] = args | |
self.__dict__['__kwargs'] = kwargs | |
def _add_conn(self): |
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
# Convert long table like | |
# key long_column other_column_1 | |
# ------------------------------------ | |
# 0 Ann 1 A | |
# 1 Ann 3 A | |
# 2 Ann 5 A | |
# 3 John 3 B | |
# 4 John 5 B | |
# 5 George 1 A | |
# 6 George 3 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/python | |
# -*-coding: utf-8 -*- | |
# This is a Flask app that accepts raw JSON from Slack with /json command and returns indented JSON | |
from flask import Flask, request, jsonify, abort | |
import json | |
import demjson | |
import requests |