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 hashlib | |
import shutil | |
from collections.abc import Iterator | |
from pathlib import Path | |
BUF_SIZE = 65536 | |
type H = dict[str, str] | |
type A = Iterator[tuple[str, str, str | None]] |
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
from string import ascii_letters as letters | |
def make_caesar(message: str, key: int) -> str: | |
return message.translate(str.maketrans(letters, letters[key:] + letters[:key])) |
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 random | |
import string | |
def generate_random_string(length=20, allowed_symbols=None): | |
allowed_symbols = allowed_symbols or string.ascii_letters + string.digits | |
return ''.join(random.sample(allowed_symbols, length)) | |
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
# https://cs50.harvard.edu/x/2020/psets/6/dna/ | |
import csv | |
import re | |
import sys | |
from pathlib import Path | |
def get_dna(filename): | |
return Path(filename).read_text() |
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
from . import db | |
from .exceptions import SaveObjectException | |
# From Mike Bayer's "Building the app" talk | |
# https://speakerdeck.com/zzzeek/building-the-app | |
class BaseMixin: | |
""" | |
A mixin that adds a surrogate integer 'primary key' column named ``id`` | |
to any declarative-mapped class and other usefull stuff. |
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
from functools import wraps | |
import warnings | |
def deprecated(func): | |
code = func.__code__ | |
warnings.warn_explicit( | |
func.__name__ + 'is deprecated', | |
category=DeprecationWarning, | |
filename=code.co_filename, |
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
flat = {} | |
make_tree_flat(tree, flat) | |
{'a': (None, ['b', 'e']), | |
'b': ('a', ['c', 'd']), | |
'c': ('b', []), | |
'd': ('b', []), | |
'e': ('a', ['f']), | |
'f': ('e', ['g']), | |
'g': ('f', [])} |
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
from functools import reduce | |
from operator import getitem | |
def walk(dictionary, keys): | |
return reduce(lambda d, key: d.get(key) if d else None, keys, dictionary) | |
def another_walk(dictionary, path): | |
return reduce(getitem, path, dictionary) | |
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
def create_chunks(products, size=7): | |
for i in range(0, len(products), size): | |
yield products[i: i + size] |