Skip to content

Instantly share code, notes, and snippets.

@decatur
decatur / dataclass_sample.py
Created September 21, 2018 11:09
Use Python dataclass to autogenerate timestamp and sequence id on an object.
from dataclasses import dataclass, field
from itertools import count
from datetime import datetime
counter = count()
@dataclass
class Event:
message: str
@decatur
decatur / ipc.py
Last active June 18, 2023 16:55
Fast inter-process communication (ipc) for python on MS-Windows using shared memory and eventing.
# -*- coding: utf-8 -*-
"""
Copyright 2018-09-09, Wolfgang Kühn
Fast inter-process communication (ipc) for Python (CPython, PyPy, 2.7, 3.6+) on MS-Windows.
It uses shared memory and eventing.
Example rpc style synchronization:
Server (rpc provider):
@decatur
decatur / demo_rle
Last active August 18, 2017 13:25
Run-Length-Encode/Decode Parameters in GAMS (General Algebraic Modeling System)
$include rle.gms
Set T / 1*5 /;
* Test series without additional domain.
Parameter a(T), a_rle(T) / 1 1, 3 EPS, 5 2 /
a_expected(T) / 1 1, 2 1, 5 2 /;
a(T) = a_rle(T);
rle_decode_1(a, T);
@decatur
decatur / topological.py
Created August 1, 2017 14:44
Topological sort for python 3.6
def topological(nodes:set, get_successors) -> deque:
"""
Returs a topologically sorted queue using DFS and gray/black colors.
get_successors(node) an accessor function for the successors of each node.
Code based on https://gist.github.com/kachayev/5910538
"""
GRAY, BLACK = 0, 1
order, state = deque(), {}
@decatur
decatur / PythonCheatSheet.py
Created December 31, 2016 18:20
Python Cheat Sheet
# Python Cheat Sheet
# List Comprehension
some_items = [{'index':i, 'value':i*i} for i in range(0,10)]
filtered_items = [item for item in some_items if item['index']%2 == 1 ]
# Dict Comprehension