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
@rednafi
rednafi / execute_tagged.py
Created April 12, 2021 13:53
Run a tagged function in python
import argparse
import functools
import another
def tag(*name):
def outer(func):
func.tag = name
@functools.wraps(func)
@rednafi
rednafi / go.md
Last active February 28, 2021 21:03
Go

Update & Installation

#!/usr/bin/env bash

# Added bash strict mode.
set -euo pipefail

# Remove old golang versions.
whereis go | xargs -n1 sudo rm -rf
@rednafi
rednafi / update.py
Last active February 14, 2021 16:22
Update python poetry dependencies
#!/bin/python3
import toml
import subprocess
import sys
class UpdateDeps:
@rednafi
rednafi / add_slot.py
Last active January 6, 2021 18:10
add_slot.py
""" Classes and metaclasses for easier ``__slots__`` handling. """
from itertools import tee
import dis
__version__ = "2021.1.6"
__all__ = ("Slots",)
def self_assignemts(method) -> set:
@rednafi
rednafi / redis_queue.py
Last active June 27, 2022 11:40
Simple FIFO queue with Redis to run tasks asynchronously in Python.
"""Simple FIFO queue with Redis to run tasks asynchronously.
===========
Description
===========
This script implements a rudimentary FIFO task queue using Redis's list data
structure. I took a peek under Celery and RQ's source code to understand how
they've implemented the async task queue — harnessing the features of Redis and
Python's multiprocessing paradigm.
@rednafi
rednafi / logger.py
Last active December 15, 2020 19:21
Colorful logging in Python
"""Custom logger with colorized output.
MODULE_NAME: LINE_NUMBER: LOG_LEVEL: LOG_MESSAGE
Usage
======
from common.logger import logger
logger.debug(f"{__name__}: This is a debug message.")
logger.info(f"{__name__}: This is an info message.")
@rednafi
rednafi / Dockerfile
Created September 5, 2020 09:52
A rudimentary Dockerfile & docker-compose.yml for Python projects
#!/bin/bash
docker-compose down
# Remove dangling images (-q=quiet, -f=without prompt)
docker rmi $(docker images -q -f dangling=true)
# Remove all stopped containers
docker rm -v $(docker ps -a -q -f status=exited)
@rednafi
rednafi / composed_inheritance.py
Last active August 22, 2020 06:38
Inheritance-like auto delegation via composition in Python
from typing import Dict, Any
class Engine:
def __init__(self, name: str, sound: str) -> None:
self.name = name
self.sound = sound
def noise(self) -> str:
return f"Engine {self.name} goes {self.sound}!"
@rednafi
rednafi / prefix_meta.py
Created August 6, 2020 15:27
Add prefixes before dataclass attributes dynamically
from dataclasses import dataclass
class PrefixMeta(type):
def __new__(cls, name, bases, attrs):
try:
prefix = attrs["Config"].prefix
except (KeyError, AttributeError):
prefix = None
if prefix:
@rednafi
rednafi / dataclasses_singledispatch.py
Last active August 6, 2020 04:18
A callable that takes a dataclass object and applies appropriate target-methods based on the types of the attributes
from dataclasses import dataclass
from functools import singledispatchmethod
from typing import List, TypeVar
T = TypeVar("T")
class Process:
@singledispatchmethod
def _process(self, arg: T) -> None: