Skip to content

Instantly share code, notes, and snippets.

View sashgorokhov's full-sized avatar
💭
🐍

Alexander Gorokhov sashgorokhov

💭
🐍
View GitHub Profile
#!/usr/bin/env python3
import json
import os
import re
import sys
import eyed3
import requests
import tqdm
@sashgorokhov
sashgorokhov / download.js
Created June 8, 2017 15:24
Vkontakte music download from browser
/**
* Created by sashgorokhov on 08.06.17.
*/
function get_url_list() {
var audio_list = document.getElementById('au_search_items');
var audio_item_list = audio_list.getElementsByClassName('audio_item');
var url_list = [];
import time
import contextlib
def retries(timeout=30, sleep_time=0.5, exception=AssertionError):
timeout_at = time.time() + timeout
state = {'fails': 0, 'give_up': False, 'success': False}
@contextlib.contextmanager
def handler():
try:
@sashgorokhov
sashgorokhov / attrdict.py
Last active October 11, 2016 10:14
Python AttrDict implementation with attribute defaults.
"""
AttrDict is useful for declarative class data mapping.
>>> class Foo(AttrDict):
... bar = 'bar'
>>> f = Foo()
>>> f
{'bar': 'bar'}
>>> f.bar
'bar'
>>> Foo(bar='baz', foo='foo') == {'bar': 'baz', 'foo': 'foo'}
@sashgorokhov
sashgorokhov / singleton.py
Created September 17, 2016 15:53
Python singleton class decorator
def singleton(cls):
"""
Class decorator. Everyone knows what it does.
>>> class Foo: pass
>>> Foo = singleton(Foo) # cant use class decorators in doctests ;(
>>> f1 = Foo()
>>> f2 = Foo()
>>> f1 == f2
@sashgorokhov
sashgorokhov / extract.py
Last active October 11, 2016 10:12
Extract parameters passed to caller function.
import inspect
def extract_parameters(deep=1, flattern=True):
"""
Return caller function arguments and values.
:param int deep: how much to go up with callers. 1 is for original caller, 2 is for caller of a caller, etc
:param bool flattern: *args arguments are not supported. Adds all keys from kwargs to original data.
:rtype: dict