Skip to content

Instantly share code, notes, and snippets.

@michalc
michalc / echo.hs
Last active February 26, 2017 07:35
Simple echo server in Haskell that accepts multiple connections
import Network.Socket hiding (send, sendTo, recv, recvFrom)
import Network.Socket.ByteString
import Control.Concurrent
port = 4242
incomingBufferSize = 4096
main = do
-- Listen
sock <- socket AF_INET Stream 0
@michalc
michalc / echoDelay.hs
Created February 26, 2017 07:45
Simple delayed echo server in Haskell that accepts multiple connections
import Network.Socket hiding (send, sendTo, recv, recvFrom)
import Network.Socket.ByteString
import Control.Concurrent
port = 4242
incomingBufferSize = 4096
delayMicroseconds = 500000
main = do
-- Listen
@michalc
michalc / sudoku.hs
Created December 31, 2017 14:32
Haskell Sudoku solver
import Control.Lens
import Control.Monad.Loops
import Control.Monad.State.Strict
import Data.List
import Data.List.Split
import Data.Maybe
data SudokuValue = S1 | S2 | S3 | S4 | S5 | S6 | S7 | S8 | S9 deriving (Eq, Enum)
instance Show SudokuValue where
show s = show $ fromJust (s `elemIndex` [S1 ..]) + 1
@michalc
michalc / aws_sig_v4_headers_aiohttp_s3_put.py
Created October 28, 2018 10:37
PUTting an object to S3 using aiohttp and aws_sig_v4_headers
import asyncio
import os
import aiohttp
from aws_sig_v4_headers import aws_sig_v4_headers
async def main():
method = 'PUT'
host = 's3-eu-west-1.amazonaws.com'
@michalc
michalc / path_lock_global_exclusive.py
Last active November 22, 2018 19:23
Path lock using a global exclusive lock
import asyncio
import contextlib
class PathLock():
def __init__(self):
self.lock = asyncio.Lock()
@contextlib.asynccontextmanager
async def __call__(self, read, write):
@michalc
michalc / path_lock_local_read_write_ancestor.py
Last active November 25, 2018 09:14
Path lock using a read/write/ancestor lock on each ancestor path
import asyncio
import contextlib
import weakref
from fifolock import FifoLock
class ReadAncestor(asyncio.Future):
@staticmethod
def is_compatible(holds):
return not holds[Write]
@michalc
michalc / path_lock_local_read_write.py
Last active November 25, 2018 09:15
Path lock using a read/write lock on each ancestor path
import asyncio
import contextlib
import weakref
from fifolock import FifoLock
class Read(asyncio.Future):
@staticmethod
def is_compatible(holds):
return not holds[Write]
@michalc
michalc / path_lock_global_read_write.py
Last active November 25, 2018 09:17
Path lock using a global read/write lock
import asyncio
import contextlib
from fifolock import FifoLock
class Read(asyncio.Future):
@staticmethod
def is_compatible(holds):
return not holds[Write]
@michalc
michalc / getaddrinfo_via_libc.py
Last active December 2, 2018 19:51
Own implementation of getaddrinfo by calling libc directly from Python + ctypes, supporting IPv4 and IPv6 (experimental)
from ctypes import (
byref,
c_char_p, c_uint32, c_uint16, c_ubyte,
CDLL,
POINTER,
pointer,
Structure, Union,
sizeof,
)
import platform
@michalc
michalc / table_csv_view.py
Created March 7, 2019 09:19
Django + gevent + psycopg2 download whole PostgreSQL table as CSV
import csv
import logging
import gevent
from psycopg2 import (
connect,
sql,
)
from django.conf import (