Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
vlad-bezden / classic.ipynb
Created March 8, 2019 15:52
Factory Method Design Pattern in Python and it's Implementations
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@vlad-bezden
vlad-bezden / timer.py
Last active November 9, 2022 16:05
Function decorator to calculate function execution time for either sync or async functions
"""Helper module to calculate function execution time."""
import logging
from functools import wraps
from time import perf_counter
from typing import Any, Awaitable, Callable, Coroutine, ParamSpec, TypeVar
FORMAT = "%(asctime)s %(message)s"
logging.basicConfig(level=logging.DEBUG, format=FORMAT)
logger = logging.getLogger(__name__)
@vlad-bezden
vlad-bezden / add_user_to_group.py
Created April 29, 2016 20:50
Example on how to add user to the group in Windows using Python
#
# Example on how to add user to the group in Python
#
import win32net
def add_user_to_group(domain, group, user):
user_group_info = {
@vlad-bezden
vlad-bezden / deep_flatten.py
Created April 11, 2022 17:37
Flatten multi-level nested iterable (list, tuple, ...)
"""Flatten multy-level nested iterable."""
from typing import Any, Iterable
def deep_flatten(data: Iterable[Any]) -> Iterable[float]:
def inner(data):
if isinstance(data, (int, float)):
return [data]
elif not data:
@vlad-bezden
vlad-bezden / .drone.yml
Created March 17, 2022 18:58
Drone.io file for checking python code. It runs all steps/tasks in parallel, and not doing changes, just validate if code follows isort, black, flake8, mypy standards.
kind: pipeline
name: python_code_check
type: kubernetes
steps:
- name: isort
pull: if-not-exists
image: python:3.10-alpine
depends_on: [clone]
commands:
@vlad-bezden
vlad-bezden / custom_enum.py
Created February 15, 2022 10:27
Python Custom Enum. It inherits from str and Enum, so it will return value in the f-string instead of name
from enum import Enum
class Status(str, Enum):
STOPPED = "STOPPED"
RUNNING = "RUNNING"
COMPLETE = "COMPLETE"
status = Status.RUNNING
@vlad-bezden
vlad-bezden / zip_emulator.py
Created February 15, 2022 10:21
Custom Python Zip Implementation
def emulated_zip(*iterables):
lists = [list(iterable) for iterable in iterables]
while all(lists):
yield tuple(current_list.pop(0) for current_list in lists)
numbers = emulated_zip(["one", "two"], [1, 2])
for i in numbers:
print(i)
@vlad-bezden
vlad-bezden / string_joiner.py
Created October 24, 2021 14:48
Content manager for joining strings. Similar to "join" function,
from contextlib import contextmanager
from typing import List, Any, Iterator
class StringJoiner(List[str]):
def __init__(self, sep: str = " ", *args: str) -> None:
super().__init__(*args)
self.sep = sep
@vlad-bezden
vlad-bezden / minimum_change.py
Created October 17, 2021 20:13
Change Making Problem. The minimum number of coins to get a certain amount of money.
from collections import Counter
from fractions import Fraction
penny = Fraction(1, 100)
nickel = Fraction(5, 100)
dime = Fraction(10, 100)
quarter = Fraction(25, 100)
usd = [penny, nickel, dime, quarter]
@vlad-bezden
vlad-bezden / clearRealPython.js
Last active September 12, 2021 11:34
Remove navigation bar and status bar snippet from RealPython web page for better readability
// remove top navigation bar
document.querySelectorAll(".navbar").forEach(el => el.remove())
// remove status bar (bottom bar)
document.querySelectorAll(".drip-tab-container").forEach(el => el.remove())
// remove sign-in red bell
document.querySelectorAll(".onesignal-bell-container").forEach(el => el.remove())
// remove column on the right (ads)
document.querySelectorAll(".col-md-7, .col-lg-4").forEach(el => el.remove())
// remove chat button on the right-bottom corner
//document.getElementById("beacon-container-body").remove()