Skip to content

Instantly share code, notes, and snippets.

View asvetlov's full-sized avatar

Andrew Svetlov asvetlov

  • constructor.org
  • Gijon
View GitHub Profile
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:
import asyncio
class CM:
async def __aenter__(self):
print(1)
await asyncio.sleep(0.01)
print(2)
return self
@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
In [1]: import dis
In [2]: for i in range(10):
...: print(i)
...:
0
1
2
3
4
@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>
@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:
import asyncio
@asyncio.coroutine
def sender(queue):
print('sender started')
try:
yield from asyncio.sleep(3)
while True:
data = yield from queue.get()
import asyncio
class A:
def __init__(self, reader, writter):
self.reader, self.writer = reader, writter
@asyncio.coroutine
def read_packet(self):
bsize = yield from self.reader.readexactly(4)
[user]
name = Andrew Svetlov
email = andrew.svetlov@gmail.com
[color]
ui = true
[alias]
lg = log --graph --pretty=format:'%Cred%h%Creset%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
st = status -sb
co = checkout
ci = commit
@asvetlov
asvetlov / gist:9420657
Created March 7, 2014 21:35
Running subprocess from nonmain thread
from asyncio import *
@coroutine
def coro():
proc = yield from create_subprocess_exec('true')
yield from proc.wait()
print('subprocess returncode', proc.returncode)
def thr():