Skip to content

Instantly share code, notes, and snippets.

View ykshatroff's full-sized avatar

Yuriy Shatrov ykshatroff

  • Wolt
  • Tallinn, Estonia
View GitHub Profile
@ykshatroff
ykshatroff / set_in_vs_not_in.py
Last active April 18, 2023 12:27
Python's set `in` vs `not in` benchmark
"""
Performance of set `in` vs `not in`
"""
import random
import time
import string
spec = [
"".join(random.sample(string.ascii_letters, 40) )
for _ in range(20100)
@ykshatroff
ykshatroff / Benchmark list removal in Python
Created April 3, 2023 07:39
Measure removal of an element from list with different methods.
"""
Measure removal of an element from list with different methods.
"""
import dataclasses
import time
@dataclasses.dataclass
class Item:
item_id: str
@ykshatroff
ykshatroff / get_size.py
Created November 4, 2022 17:56
Python: Get full size of an object
"""
Improved version of https://goshippo.com/blog/measure-real-size-any-python-object/
"""
import sys
def get_size(obj, seen=None):
"""Recursively finds size of objects"""
size = sys.getsizeof(obj)
if seen is None:
seen = set()
from datetime import datetime
import pytest
from prog import read_from_file, read_line
def test_file_operations(file_data):
assert read_line(file_data) == (datetime(2020, 1, 18, 13, 49, 47), "text")
from datetime import datetime
# get current date
now = datetime.now()
curtimestamp = now.strftime("%Y%m%d-%H:%M:%S")
def read_line(line: str) -> tuple:
"""
Line format:
import os
import tempfile
import pytest
@pytest.fixture()
def file_data():
yield "2020-01-18T13:49:47,text"
@ykshatroff
ykshatroff / 10-colored-prompt.sh
Created October 29, 2019 13:08
Bash colored prompt
# Set up a colored prompt in Bash with green (32) and red (31) colors for regular and root users, respectively,
# instead of the standard:
# PS1='[\u@\h \W]\$ '
#
# This prompt also allows Bash to take its length into consideration correctly
# for command line start offset calculation.
# Place this file in /etc/bashrc.d or wherever feasible for inclusion in ~/.bashrc .
_color='32'
_chr=':'
@ykshatroff
ykshatroff / aiohttp_getaddrinfo_error.py
Created February 5, 2019 21:38
Aiohttp error in getaddrinfo()
import aiohttp
import asyncio
async def request(session, url):
try:
async with session.get(url) as resp:
pass
except:
# raises: TypeError: getaddrinfo() argument 1 must be string or None