Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
vlad-bezden / group_by_sequence.py
Created May 3, 2020 16:23
Sometimes, we'll have raw data that is a flat list of values that we'd like to bunch up into subgroups.
SIZE = 5
def group_by_sequence_even(n, sequence):
"""Groups sequence by n number of items
It will only return number of rows divided evenly.
Remaining items will not be generated
"""
data_iter = iter(sequence)
return [tuple(next(data_iter) for _ in range(n)) for _ in range(len(sequence) // n)]
@vlad-bezden
vlad-bezden / dynamic_func_invocation.py
Created April 16, 2020 12:09
Example of how dynamically invoke function in Python
class Cleaner:
@classmethod
def clean(cls, type, *args, **kwargs):
getattr(cls, f"_clean_{type}")(*args, **kwargs)
@classmethod
def _clean_email(cls, *args, **kwargs):
print("invoked _clean_email function")
@classmethod
@vlad-bezden
vlad-bezden / asynio_blocking_code_default_executor.py
Created March 18, 2020 21:20
Example of running blocking code in asyncio using default executor. There are four tasks each of them sleep from 1 to 4 seconds corresponding. If it were concurrent call then they would run for 1 + 2+ 3 + 4 = 10 seconds. However, running them in executor executes each task on separate thread and the total time to execute all of them took 4 secon…
import asyncio
from time import sleep
import logging
logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s %(thread)s %(funcName)s %(message)s"
)
def long_task(t):
@vlad-bezden
vlad-bezden / db_connection.py
Created February 29, 2020 10:40
MODULE-SCOPED CODE TO CONFIGURE DEPLOYMENT ENVIRONMENTS.
# db_connection.py
from __main__ import TESTING
class TestingDatabase:
def __init__(self):
print("Connecting to DEV database")
class RealDatabase:
def __init__(self):
print("Connecting to PROD database")
@vlad-bezden
vlad-bezden / filterfalse_remove_elements.py
Last active February 22, 2020 15:47
Remove more than one element from the iterable
"""Remove more than one element from the iterable"""
import random
from itertools import filterfalse
random.seed(42)
data = [random.randrange(5) for _ in range(10)]
clean = [*filterfalse(lambda i: i == 0, data)]
print(f"Remove 0s\n{data=}\n{clean=}\n")
@vlad-bezden
vlad-bezden / asynio_blocking_code.py
Last active March 3, 2020 15:08
Run blocking code on the separate executor. Use asyncio to execute blocking io operation without blocking execution thread using "run_in_executor" function
import asyncio
from concurrent.futures.thread import ThreadPoolExecutor
from time import sleep
import logging
logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s %(thread)s %(funcName)s %(message)s"
)
@vlad-bezden
vlad-bezden / iterable_static_type_checking.py
Created January 20, 2020 20:02
Example on how to use Iterable type instead of List to make mypy happy :)
"""Static type checking using Iterable
show function receives List[Base]. However, if I specify type as List[Base]
mypy will return following error:
error: Argument 1 to "show" has incompatible type "List[Double]"; expected "List[Base]"
To fix it use Iterable[Base] instead
Output:
@vlad-bezden
vlad-bezden / find_and_index_performance_test.py
Created January 18, 2020 20:06
Performance tests of 'find' and 'index' Python functions
"""Performance tests of 'find' and 'index' functions.
Results:
using_index t = 0.0259 sec
using_index t = 0.0290 sec
using_index t = 0.6851 sec
using_find t = 0.0301 sec
using_find t = 0.0282 sec
using_find t = 0.6875 sec
@vlad-bezden
vlad-bezden / treadmill_speed_calculation.ipynb
Created January 16, 2020 09:33
Calculates how many seconds, or minutes to take to run one circle (1/4 of mile) on the treadmill, and difference in seconds between with increasing speed
Loading
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 / numpy_diff_and_formatting.py
Created November 28, 2019 10:20
Here is a powerful example of numpy diff function. In this example, I chunk of memory and I need to find start, end, and length of the memory. 1 means allocated and 0 means free. using numpy.diff I was able to find start and the end of the allocated memory. Another example thing I use here is dynamic size of the column col_size = 7 and the power…
import numpy as np
# Memory allocation:
# '1' means allocated, '0' means free
memory = np.array([0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0])
# contiguous memory allocations
diff = np.diff(memory)
# starts indices
starts = np.nonzero(diff == 1)[0] + 1
# ends indices