Skip to content

Instantly share code, notes, and snippets.

@gubser
gubser / AsyncBoundedCache.fs
Last active January 31, 2019 19:46
A bounded cache for async operations in F#
module AsyncBoundedCache
open System
open System.Collections.Concurrent
open System.Threading
open System.Threading.Tasks
let startTime = DateTime.UtcNow.Ticks / 10000000L
let now () = int ((DateTime.UtcNow.Ticks / 10000000L) - startTime)
@gubser
gubser / AsyncBoundedCacheTests.fs
Created January 30, 2019 21:04
Unit tests for bounded caching. Needs more.
module AsyncBoundedCacheTests
open System
open Xunit
[<Fact>]
let ``insert items`` () = async {
let f = fun x -> async {
do! Async.Sleep(1)
return x
@gubser
gubser / space-delimited-int-array-column.py
Created January 30, 2019 10:03
This python snippet parses the `vector` column as a list of integers delimited by space and adds it as a new column
"""
| frame | which | center-x | center-y | vector | vector_array |
| 44 | 0 | 55.5 | 29.3 | "49 44 31 22 26 22 44 ..." | [49, 44, 31, 22, 26, 22, 44, ...] |
"""
import pandas as pd
df = pd.read_csv(r"C:\Users\egubser\Downloads\blobs.csv", sep=';')
df['vector_array'] = df.vector.apply(lambda v: [int(k) for k in v.split(' ')])
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<title>TextSymbol with Halo - 4.9</title>
<style>
html,
body,
#viewDiv {
@gubser
gubser / TaskResultBind.fs
Last active November 6, 2018 21:19
Helper function to chain TaskResult producing functions
type TaskResult<'a,'b> = Task<Result<'a,'b>>
module TaskResult =
let bind (f: 'a -> TaskResult<'b,'e>) (x: TaskResult<'a,'e>): TaskResult<'b,'e> = task {
match! x with
| Ok success -> return! f success
| Error failure -> return Error failure
}
// should it be called bindResult or bindTask?
@gubser
gubser / pathScanContext.fsx
Created October 11, 2018 13:36
A variant of pathScan that also passes HttpContext to the handler
#r "<path/to/Suave.dll>"
open Suave
open Suave.Successful
open Suave.RequestErrors
/// <summary>
/// Same as pathScan but also passes the HttpContext to the handler h.
/// See pathScan: https://github.com/SuaveIO/suave/blob/0524552a876526ae6623ba6fdf4ca3a06feaf510/src/Suave/Combinators.fs#L413
/// </summary>
let pathScanContext (pf : PrintfFormat<_,_,_,_,'t>) (h : HttpContext -> 't -> WebPart) : WebPart =
@gubser
gubser / AutoReloadConfig.py
Last active May 17, 2018 05:54
License: MIT
import json
import time
import logging
from collections import UserDict
class AutoReloadConfig(UserDict):
"""
A dictionary that automatically reloads itself from a file.
On each call to __getitem__(), check if config is older than reload_period and if yes then reload from file.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
The following generators are available on this platform:
Unix Makefiles = Generates standard UNIX makefiles.
Ninja = Generates build.ninja files (experimental).
Watcom WMake = Generates Watcom WMake makefiles.
CodeBlocks - Ninja = Generates CodeBlocks project files.
CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
CodeLite - Ninja = Generates CodeLite project files.
CodeLite - Unix Makefiles = Generates CodeLite project files.
Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 - Unix Makefiles