Skip to content

Instantly share code, notes, and snippets.

@gyli
gyli / slice_iterator.py
Last active April 20, 2019 20:09
Slice an iterator into chunks
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:
@gyli
gyli / unique_dict_in_list.py
Created July 9, 2018 19:51
Find unique dict in a list in Python
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'))
@gyli
gyli / asyncio_aiohttp_requests.py
Created January 8, 2018 22:03
Making requests with asyncio and aiohttp
import aiohttp
import asyncio
NUMBERS = range(12)
URL = 'http://httpbin.org/get?a={}'
async def fetch_async(a):
async with aiohttp.ClientSession() as session:
@gyli
gyli / sort_nested_dictionary.py
Last active January 8, 2024 22:07
Sort Nested Dictionary By Key in Python
# 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}
@gyli
gyli / generate_fontawesome_mapping.py
Last active November 21, 2017 16:44
Generate a Font Awesome icon mapping into a Python script
# 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
@gyli
gyli / ceil_and_floor_without_math.py
Last active October 5, 2019 23:30
Division with Ceil and floor in Python without math module
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)),
@gyli
gyli / lazy_dict.py
Last active November 30, 2022 20:21
Lazy Dict in Python
# 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):
@gyli
gyli / lazy_db_connection.py
Last active June 23, 2017 01:57
Lazy DB Connection in Python
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):
@gyli
gyli / long_table_to_wide_table_in_python_pandas.py
Last active May 29, 2017 05:47
Convert long table to wide table in Python Pandas
# 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
@gyli
gyli / jsonformatter.py
Created March 2, 2017 18:15
Format JSON with Slack slash command and Flask
#!/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