Skip to content

Instantly share code, notes, and snippets.

View evtn's full-sized avatar
🍞
პური

Dmitry Gritsenko evtn

🍞
პური
View GitHub Profile
from typing import Callable, Iterable, TypeVar
# In Python, for loops (and, by extension, comprehensions)
# actually have the ability to define variables and write into keys and attributes of objects.
#
# This ability is unintuitive but also creates a weird set of things possible with loops
T = TypeVar("T")
R = TypeVar("R")
@evtn
evtn / pie.md
Created December 6, 2022 04:32
The Pie

This is a story of Pie, an apple pie who would find himself a living being.
He would find friends and obstacles on his way to find out who is he and what he wants.

The story is developing in a chat between the Narrator and the Pie.
There was no editing of chat text at all, so there was one take.

Cast

OpenAI ChatGPT as Pie
Me as Narrator

@evtn
evtn / matrix.py
Last active March 10, 2022 09:51
Simple matrix calculator
from typing import Tuple, Callable, List, Dict, Union
Number = Union[float, int]
class MatrixIter:
def __init__(self, matrix):
self.matrix = matrix
self.index = 0
def __iter__(self):
@evtn
evtn / example.py
Created November 20, 2021 15:37
full-fledged one-line 381 chars XML builder with text escaping, attribute name cleaning and self-closing empty tags
from wtfisthat import r
svg = {
"n": "svg",
"c": [
{
"n": "rect",
"a": {
"width": "100%",
"height": "100%",
/* ==UserStyle==
@name VK unread dot
@namespace github.com/evtn
@version 1.0.2
@description Adds a dot to unread messages
@author evtn
==/UserStyle== */
@-moz-document domain("vk.com") {
[dir] .im-mess.im-mess_unread:not(.im-mess_light):not(.im-mess_selected) {
def combinations(letters, length):
current = [0] * length
result = []
while True:
i = len(current) - 1
result.append([letters[x] for x in current])
current[i] += 1
while current[i] == len(letters):
current[i] = 0
@evtn
evtn / pretty_print.py
Created June 14, 2021 09:47
Simple pretty print
def add_tab(s, tab_char=" "):
return (tab_char + s).replace("\n", "\n" + tab_char)
container_parens = {
dict: "{}",
list: "[]",
tuple: "()",
set: "{}"
}
@evtn
evtn / sign.py
Created November 8, 2020 18:58
Better sign check example (works in Python 3.6+)
from base64 import b64encode
from hashlib import sha256
from hmac import HMAC
from urllib.parse import urlparse, parse_qsl, urlencode
def sign_checker(query: dict, secret: str) -> bool:
"""Check VK Apps signature"""
vk_subset = filter(
lambda key: key.startswith("vk_"),
query