Skip to content

Instantly share code, notes, and snippets.

View asvetlov's full-sized avatar

Andrew Svetlov asvetlov

  • apolo.us
  • Gijon
View GitHub Profile
@asvetlov
asvetlov / gist:11343386
Created April 27, 2014 11:32
Control set of unhandled delayed calls
import asyncio
import functools
handles = set()
def call_later(delay, cb, *args, loop=None):
if loop is None:
@asvetlov
asvetlov / gist:ecbdd3ad75de156fd72e
Created May 16, 2014 19:41
Dump of elpy buffer when I try to work on python file with relative imports
There was an error in the Elpy backend.
The following lines were received from Python, and might help identifying
the problem.
Traceback (most recent call last):
File "/usr/lib/python3.4/runpy.py", line 170, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.4/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/andrew/.local/lib/python3.4/site-packages/elpy/__main__.py", line 15, in <module>
In [1]: import dis
In [2]: for i in range(10):
...: print(i)
...:
0
1
2
3
4
@asvetlov
asvetlov / gist:1a24f6f4a63c6c2f1932
Created October 22, 2014 17:56
Dependency Injection example
from inspect import getattr_static
class dep:
name = None
def __init__(self, type):
self.type = type
import asyncio
class CM:
async def __aenter__(self):
print(1)
await asyncio.sleep(0.01)
print(2)
return self
import asyncio
END = b'Bye-bye!\n'
async def echo_client():
reader, writer = await asyncio.open_connection('localhost', 8000)
writer.write(b'Hello, world\n')
writer.write(b'What a fine day it is.\n')
writer.write(END)
while True:
def f():
future = asyncio.Future()
future.set_result(5)
return future # yield from f() returns 5.
def g():
future = asyncio.Future()
asyncio.get_event_loop.call_later(1, future.set_result, 5)
return future # yield from g() also returns 5 but 1 second later
@asvetlov
asvetlov / gist:218a1d7d05a540d70a21
Last active August 29, 2015 14:26
aiohttp client with cookies
import aiohttp
import asyncio
class MyClass:
def __init__(self):
self.data = {'username': 'me', 'password': 'pw'}
self.login_url = 'http://example.com/login'
self.client = aiohttp.ClientSession()
import asyncio
import warnings
class Timeout:
def __init__(self, timeout, *, raise_error=False, loop=None):
self._timeout = timeout
if loop is None:
loop = asyncio.get_event_loop()
self._loop = loop
@asvetlov
asvetlov / gist:227af16ae9eb38db7f22
Created January 7, 2016 00:12
aiohttp memleaking test
import itertools
from aiohttp import CIMultiDict
import tracemalloc
def leaking(headers):
headers = ''.join(itertools.chain(
('status line',),
*((k, ': ', v, '\r\n') for k, v in headers.items())))