This file contains hidden or 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
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)] |
This file contains hidden or 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
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 |
This file contains hidden or 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 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): |
This file contains hidden or 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
# 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") |
This file contains hidden or 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
"""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") |
This file contains hidden or 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 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" | |
) | |
This file contains hidden or 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
"""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: |
This file contains hidden or 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 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 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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 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 |