Skip to content

Instantly share code, notes, and snippets.

View sznurek's full-sized avatar

Łukasz Dąbek sznurek

View GitHub Profile
nfather :: Person -> Int -> Maybe Person
nfather p n = do
a <- father p
b <- father a
-- ... repeat n times
father xyz
@sznurek
sznurek / gist:3171575
Created July 24, 2012 18:09
PHP class constants - late binding
<?php
class First {
const X = 1;
const Y = self::X;
}
class Second extends First {
const X = 2;
}
@sznurek
sznurek / worker.py
Created July 26, 2012 14:25
Example worker
import sys
for line in sys.stdin.readlines():
parts = line.split()
uid = int(parts[0])
msg = ' '.join(parts[1:])
print("%d Hello user %d! Message: %s" % (uid, uid, msg))
@sznurek
sznurek / async.py
Created July 26, 2012 14:47
First try
# methods on_read and on_write was called when select confirmed that socket is ready to read from / write to
STATE_SEND_LOGIN = 1
STATE_RECEIVE_LOGIN = 2
STATE_SEND_PASS = 3
STATE_RECEIVE_PASS = 4
STATE_SEND_OK = 5
class Authenticator:
def __init__(self, sock):
@sznurek
sznurek / async.py
Created July 26, 2012 14:53
Second try
class Authenticator:
def __init__(self, sock):
self.sock = sock
self.login = None
self.pass = None
self.to_send = "LOGIN"
self.to_read = None
def on_write(self):
if self.to_send == "LOGIN":
@sznurek
sznurek / async.py
Created July 26, 2012 14:59
Third try
def auth(login, password):
return login == "1" and password == "test"
login_action = Write('LOGIN') >> \
Read('login') >> \
Write('PASS') >> \
Read('PASS') >> \
Guard(lambda v: auth(v['login'], v['pass']) >> \
Write('OK')
@sznurek
sznurek / fdmanager.py
Created July 30, 2012 07:20
FDManager
class FDManager:
def register_read(self, file_descriptor, callback):
# ...
def register_write(self, file_descriptor, callback):
# ...
def register_error(self, file_descriptor, callback):
# ...
@sznurek
sznurek / example.py
Created July 30, 2012 07:24
Example
class SendText(IOBase):
def __init__(self, fd, text=b"Hello!"):
self.fd = fd
self.text = text
def register(self, fdmanager):
fdmanager.register_write(self.fs, self.write)
def write(self):
os.write(self.fd, self.text)
@sznurek
sznurek / sendtext.py
Created July 30, 2012 07:32
SendText
class SendText(IOBase):
def __init__(self, fd, text=None):
self.fd = fd
self.text = text
if not self.text:
self.text = lambda: b"Hello!"
def register(self, fdmanager):
fdmanager.register_write(self.fs, self.write)
login_action = Write('LOGIN') >> \
Read('login') >> \
Write(lambda v: 'HELLO %s' % v['login'])