Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / 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 / papertrail_logging.py
Created April 3, 2019 06:27
Send log to Papertrail with Python logging dictconfig
# Code is modified from Papertrail's official examples:
# https://help.papertrailapp.com/kb/configuration/configuring-centralized-logging-from-python-apps/#examples
import logging
import logging.config
import os
import socket
class ContextFilter(logging.Filter):
"""
@gyli
gyli / join_nullable.py
Created April 9, 2019 18:28
Join list of strings with None
def join_nullable(l: List[str], sep: str) -> str:
"""
Filter out None in a list and join the remaining strings.
Example:
Input: l=['a', None, 'c'], sep=' '
Output: 'a c'
:param l: list of strings
:param sep: separator
"""
return sep.join(filter(lambda i: bool(i), l))
@gyli
gyli / pandas_multiprocessing.py
Last active April 13, 2019 03:32
Simple version of processing large CSV file with Pandas and multiprocessing
import pandas as pd
import multiprocessing as mp
import traceback
def processing_chunk(chunk):
for row in chunk.iterrows():
pass
time.sleep(5)