Skip to content

Instantly share code, notes, and snippets.

View rednafi's full-sized avatar
🏠
Working from home

Redowan Delowar rednafi

🏠
Working from home
View GitHub Profile
@quad
quad / 0-interceptors-are-functions-too.md
Last active March 13, 2024 22:37
Interceptors Are Functions Too

Interceptors Are Functions Too

I could not agree more with my colleague and friend Travis Johnson's opinion that "[INTERCEPTORS ARE SO COOL][iasc]!" In that post, he succinctly describes the [Interceptor pattern][pattern] as used adroitly by [OkHttp][okhttp]. But, as is often the case, I believe a complicated object-oriented pattern obscures the simple functional gem within it.

What is an Interceptor?

I'll quote liberally from [OkHttp's documentation on the topic][okhttp-interceptor]:

Interceptors are a powerful mechanism that can monitor, rewrite, and retry calls. […] >

@msullivan
msullivan / view_patterns.py
Last active December 9, 2023 12:47
A few related schemes for implementing view patterns in Python. view_patterns_explicit.py is probably the least dodgy of them
"""Hacky implementation of "view patterns" with Python match
A "view pattern" is one that does some transformation on the data
being matched before attempting to match it. This can be super useful,
as it allows writing "helper functions" for pattern matching.
We provide a class, ViewPattern, that can be subclassed with custom
`match` methods that performs a transformation on the scructinee,
returning a transformed value or raising NoMatch if a match is not
possible.
/**
* @link https://raw.githubusercontent.com/NaturalCycles/js-lib/master/src/promise/pProps.ts
* Promise.all for Object instead of Array.
*
* Inspired by Bluebird Promise.props() and https://github.com/sindresorhus/p-props
*
* Improvements:
*
* - Exported as { promiseProps }, so IDE auto-completion works
@ZechCodes
ZechCodes / mutation_descriptors.py
Last active March 20, 2022 15:43
Apply Mutations on Dataclass Fields
from __future__ import annotations
import json
from dataclasses import dataclass, MISSING, field as _field
from typing import Any, Callable
class MutationDescriptor:
def __init__(self, mutator: Callable[[Any], Any]):
self._mutator = mutator
@benben233
benben233 / bplustree.py
Created May 17, 2021 14:19
Python b+ tree
import random # for demo test
splits = 0
parent_splits = 0
fusions = 0
parent_fusions = 0
class Node(object):
"""Base node object. It should be index node
@rednafi
rednafi / Makefile
Last active February 6, 2024 15:22
Applying Black, Flake8, Isort and typechecking with mypy
.PHONY: help
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
.PHONY: venvcheck ## Check if venv is active
venvcheck:
ifeq ("$(VIRTUAL_ENV)","")
@echo "Venv is not activated!"
@echo "Activate venv first."
@echo
@rednafi
rednafi / sqla_dict.py
Last active June 30, 2020 18:03
Python's dict-like custom data structure that can store data in any SQLAlchemy supported database. Uses transaction.
"""
This is a self contained custom data structure with dict like
key-value storage capabilities.
* Can store the key-value pairs in any sqlalchemy supported db
* Employs thread safe transactional scope
* Modular, just change the session_scope to use a different db
* This example uses sqlite db for demonstration purpose
The code is inspired by Raymond Hettinger's talk `Build powerful,
@gvx
gvx / transform_decorator.py
Created May 18, 2020 13:21
metadecorators to construct decorators that only transform function arguments or return values
from functools import wraps, partial
def transform_return(transformation, decorated=None):
if decorated is None:
return partial(transform_return, transformation)
@wraps(decorated)
def wrapper(*args, **kwargs):
return transformation(decorated(*args, **kwargs))
return wrapper
@Bobmajor
Bobmajor / gist:319e89e9c876cccdb1b77f1783e9a820
Created April 25, 2020 16:06
HOW TO INSTALL POSTMAN ON LINUX WITHOUT SNAP
1. Go to the Postman app download page at https://www.getpostman.com/apps. You can choose the os version from the drop-down. x64 for 64 bit Operating System and x84 for the 32 bit based Linux
2. Open the terminal and go to the directory where you have downloaded the tar file. If you have downloaded on the Downloads folder
cd ~/Downloads/
3. Run the following commands,
sudo rm -rf /opt/Postman/
@2minchul
2minchul / proxy.py
Created October 29, 2019 07:48
Python HTTPS proxy server with asyncio streams
import asyncio
import re
from asyncio.streams import StreamReader, StreamWriter
from contextlib import closing
from typing import Tuple, Optional
import async_timeout
StreamPair = Tuple[StreamReader, StreamWriter]