This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Measure removal of an element from list with different methods. | |
""" | |
import dataclasses | |
import time | |
@dataclasses.dataclass | |
class Item: | |
item_id: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import tempfile | |
import pytest | |
@pytest.fixture() | |
def file_data(): | |
yield "2020-01-18T13:49:47,text" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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=':' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |