Skip to content

Instantly share code, notes, and snippets.

@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
@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 / 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 / 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 / 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 / test_pythonflow.py
Created December 4, 2018 15:36
Use case save/restore with Pythonflow for long running data flow operations.
"""
Use case save/restore with Pythonflow for long running data flow operations.
Pythonflow is a very lighter alternative to tensorflow's graphs.
Prerequisite:
$ pip install pythonflow
In this example, we model round(a+b) with two save points.
Our file-database will have the structure:
@decatur
decatur / generateQuarterHoursForLocalDate.js
Created January 28, 2019 12:30
JavaScript Date Pitfalls
/**
* In a daylight-saving time locale, this will generate 92, 96 or 100 values.
* This function sideways some of JavaScript Date pitfalls
* @param {string} localDateString
* Example:
* generateQuarterHoursForLocalDate('2019-03-31').map(function(date) { return date.toJSON() })
* -> 0: "2019-03-30T23:00:00.000Z"
* 1: "2019-03-30T23:15:00.000Z"
* ...
* 91: "2019-03-31T21:45:00.000Z"
@decatur
decatur / pandas_with_typing.py
Last active September 18, 2020 09:32
typed pandas
import numpy as np
class Plant:
"""
KISS implementation of a time series data class with a ndarray backing.
Think of Pandas with typing.
Properties can be also set by scalar values.
"""
def __init__(self, dim: int):
@decatur
decatur / README.md
Last active April 1, 2021 21:25
Chrome extension to make yammer feed pages more user friendly.

This a Chrome extension to make yammer feed pages more user friendly. As I personally do not trust extensions from the Chrome Web Store, this extension is only offered through this gist and must be installed in Developer Mode.

Setup

  1. Download this gist to some folder my_yammer_extension_folder
  2. Add this Chrome Extension according to Getting started - Chrome Developers:
    1. Open the Extension Management page by navigating to chrome://extensions.
    2. Enable Developer Mode by clicking the toggle switch next to Developer mode.
  3. Click the LOAD UNPACKED button and select the my_yammer_extension_folder
@decatur
decatur / Python_App-Definition.md
Last active July 26, 2021 17:54
A definition of what constitutes a Python Application

What is a Python Application

A Python Application describes required packages, entry point and constraints for the operation and development process.

Operation

  1. An entry point, e.g. my_app.my_module:App
  2. Constraints for required packages AppR, e.g. {my_app=1.2.3, my_util=0.1.2, pandas=1.2.5, ...}

Development