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
@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
@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,
@skrawcz
skrawcz / PyBayFormatter.py
Last active October 8, 2021 22:46
JSON Structured Logger
import json
import logging
import sys
logger = logging.getLogger(__name__)
class PyBayFormatter(logging.Formatter):
"""Implementation of JSON structured logging that works for most handlers."""
@0xpizza
0xpizza / async_tcp_scan.py
Last active November 20, 2021 15:25
TCP Network scanner using asyncio for Python 3.7
#!/usr/bin/python3.7
import asyncio
import ipaddress
import re
import sys
MAX_NUMBER_WORKERS = 200
@daltonmatos
daltonmatos / mocktrue.py
Created August 7, 2012 02:36
Hack to mock the python True object
import mock
class AlmostAlwaysTrue(object):
def __init__(self, total_iterations=1):
self.total_iterations = total_iterations
self.current_iteration = 0
def __nonzero__(self):
if self.current_iteration < self.total_iterations:
@omarish
omarish / node.py
Created March 26, 2011 20:33
Simple JSON-Friendly Python Tree
We couldn’t find that file to show.
@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
/**
* @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
@diosmosis
diosmosis / example.py
Created August 15, 2011 22:41
Python decorator that catches exceptions and logs a traceback that includes every local variable of each frame.
import os
from log_exceptions import log_exceptions
def throw_something(a1, a2):
raise Exception('Whoops!')
@log_exceptions(log_if = os.getenv('MYAPP_DEBUG') is not None)
def my_function(arg1, arg2):
throw_something(arg1 + 24, arg2 - 24)
@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