Skip to content

Instantly share code, notes, and snippets.

class SimplyDataloader(DataLoader):
def batch_load_fn(self, keys):
"""
batch_load_fn accept list of keys and should return list of promises.
assert len(keys) == len(promises)
It calls only one time, with all the keys from the .load(key) calls.
"""
# heavy calculations will be here.
ret = 5
return Promise.resolve([ret])
@Zagrebelin
Zagrebelin / gist:00f28caecea05d38d5dd97642b340409
Last active January 15, 2020 07:58
multilanguage transliteration
from unidecode import unidecode
unidecode('жопа негра') -> 'zhopa negra'
def transform(value):
return {'True':True, 'False':False}.get(value, value)
d = {'str1': 'True','str2':'string','str3':10}
d2 = {key:transform(value) for key, value in d.items()}
assert d2 == {'str1': True,'str2':'string','str3':10}
print('OK')
@Zagrebelin
Zagrebelin / markov.py
Created August 16, 2017 17:10
simplest markov chain realisation
import random
from collections import defaultdict, Counter
def build_chain(words):
""" build Markov chain from the list of words """
chain = defaultdict(Counter)
for idx, current_word in enumerate(words[:-1]):
next_word = words[idx+1]
chain[current_word].update([next_word])
return chain
@Zagrebelin
Zagrebelin / classmethod.py
Created March 2, 2017 11:57
classmethod.py
class Foo:
x = 2
@classmethod
def multy_cls(cls, y):
return cls.x * y
def multy(self, y):
return self.x * y
f1 = Foo()
@Zagrebelin
Zagrebelin / create dja
Created January 24, 2017 03:43
create django project on windows
Microsoft Windows [Version 6.1.7601]
Zagrebelin-PI@ZAGREBELIN-PC C:\Users\zagrebelin-pi
> cd %temp%
Zagrebelin-PI@ZAGREBELIN-PC C:\Users\ZAGREB~1\AppData\Local\Temp
> mkdir dja
Zagrebelin-PI@ZAGREBELIN-PC C:\Users\ZAGREB~1\AppData\Local\Temp
> cd Dja
@Zagrebelin
Zagrebelin / taneja.py
Last active January 13, 2017 09:42 — forked from vaxyzek/taneja.py
#! /usr/bin/python3.6
import datetime
import itertools
def show_progress(start, idx, total):
if idx == 0:
return
now = datetime.datetime.now()
speed = idx / (now - start).total_seconds()
import random
"""
Дан словарь английского языка (несколько прилагательных, глаголов, существительных, наречий).
Нужно сформировать несколько случайных предложений, выбрав для каждого один шаблон из двух:
"the прилагатльное существительное глагол" или
"the прилагатльное существительное наречие глагол"
🌟 доп. условие: список шаблонов фраз сколько угодно большой, каждая фраза может включать несколько существительных и прилагательных.
🌟🌟 доп. условие 2: список частей речи может меняться.
"""
import dis
dis.dis("""
try:
print(2)
except ValueError as e:
print(1)""")
2 0 SETUP_EXCEPT 14 (to 17)
3 3 LOAD_NAME 0 (print)
@Zagrebelin
Zagrebelin / .py
Created June 8, 2016 08:26 — forked from anonymous/.py
def vake(list_of_keys,list_of_values):
if len(list_of_values) >= len(list_of_keys):
return {x[0]:x[1] for x in zip(list_of_keys,list_of_values)}
else:
list_of_values = list_of_values + [None]*(len(list_of_keys) - len(list_of_values))
return {x[0]:x[1] for x in zip(list_of_keys,list_of_values)}