Skip to content

Instantly share code, notes, and snippets.

View sprytnyk's full-sized avatar
🪖
Fighting for the bright future of 🇺🇦

Vladyslav Krylasov sprytnyk

🪖
Fighting for the bright future of 🇺🇦
View GitHub Profile
@sprytnyk
sprytnyk / mock_function.py
Created April 11, 2020 15:57
How to mock a function.
from unittest.mock import patch
from core.utils import list_dirs_only
# specify what should be patched
# i.e. if one wants to mock datetime.datetime but it used by your module x.py
# then it required to mock x.datetime but not datetime.datetime
patcher = patch('__main__.list_dirs_only')
# attach the original function to the variable because it will be modified
# when the patcher will start
@sprytnyk
sprytnyk / mock_module.py
Created April 11, 2020 16:07
How to mock module.
from unittest.mock import patch
from core.utils import list_dirs_only
# specify what should be patched
# i.e. if one wants to mock datetime.datetime but it used by your module x.py
# then it required to mock x.datetime but not datetime.datetime
patcher = patch('__main__.list_dirs_only')
# attach the original function to the variable because it will be modified
# when the patcher will start
@sprytnyk
sprytnyk / install-srm.sh
Last active January 6, 2025 18:16
A dummy script that installs srm util.
#!/usr/bin/env bash
# A dummy script incorporated to a gist for the sake of simplicity to install srm.
# Credit: http://techies-world.com/install-srm-secure-remove-command/
cd "$(mktemp -d)" || exit
echo "Starting to download srm file..."
sudo apt install build-essential
wget http://downloads.sourceforge.net/project/srm/1.2.15/srm-1.2.15.tar.gz
tar -zxvf srm-1.2.15.tar.gz
cd srm-1.2.15 || exit
import re
import subprocess
from collections import defaultdict
from dataclasses import dataclass, field
from pprint import pprint
TEMPLATE = """## {code} ({text_code})
### :x: Problematic code:
@sprytnyk
sprytnyk / main.go
Last active June 24, 2020 22:57
How pointers works in Golang.
package main
import "fmt"
func main() {
/*
i initial value is 0
j initial value is 0
i initial address is 0xc0000b4010
j initial address is 0xc0000b4018
@sprytnyk
sprytnyk / cap_vs_len.go
Last active June 25, 2020 16:14
How `cap` built-in differs from `len` one in Golang.
package main
import "fmt"
func main() {
/*
array address is 0xc000016420.
array values are [5]int{1, 2, 3, 4, 5}.
array type is [5]int.
array length is 5.
@sprytnyk
sprytnyk / mem.py
Last active March 18, 2021 16:25
A dummy decorator function to measure memory usage of a Python function, method, etc.
import resource
from functools import wraps
def mem_it(func):
@wraps(func)
def wrapper(*args, **kwargs):
pre = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024
print(f'Pre-memory usage: {pre} (mb)')
f = func(*args, **kwargs)
@sprytnyk
sprytnyk / time.py
Created July 15, 2020 13:21
A dummy function to measure time consumed by a function, method, etc.
import resource
from functools import wraps
from time import time
def time_it(func):
@wraps(func)
def wrapper(*args, **kwargs):
start = time()
f = func(*args, **kwargs)
@sprytnyk
sprytnyk / standalone.py
Last active December 7, 2021 11:28
An example of Django standalone script
#!/usr/bin/env python3
import os
import pathlib
import sys
import django
# Set a project root to a const, this file should be placed in a project root
# as well
PROJECT_ROOT = pathlib.Path(__file__).resolve().parent
@sprytnyk
sprytnyk / docker-compose.yml
Created August 10, 2021 11:34
Postgis container with PGAdmin4 interface for nice and easy local development when a DB needed.
version: "3.7"
services:
postgresql:
image: postgis/postgis
environment:
POSTGRES_DB: "geo"
POSTGRES_USER: "geo"
POSTGRES_PASSWORD: "geo"