Skip to content

Instantly share code, notes, and snippets.

View komuw's full-sized avatar

Komu Wairagu komuw

View GitHub Profile
@komuw
komuw / BreachHandler.py
Last active August 24, 2019 21:50
create a breach handler for python's standard logging library
import time
import logging
import collections
# Inspired by; https://tersesystems.com/blog/2019/07/28/triggering-diagnostic-logging-on-exception/
# Also see; https://docs.python.org/3.6/library/logging.handlers.html#logging.handlers.MemoryHandler
# Also see; https://github.com/komuw/naz/blob/e0666550396400f86b9a547932bb9075c520a5d9/naz/log.py#L159-L230
class BreachHandler(logging.StreamHandler):
"""
Log handler that buffers logs in an in-memory ring buffer until a trigger.
@komuw
komuw / instance_creator.py
Last active June 19, 2019 15:02
python create a new class instance inside same class
import copy
# see: https://stackoverflow.com/questions/13260557/create-new-class-instance-from-class-method
class Animal(object):
request_id = "Original_Animal"
def reproduce(self):
"""
gives birth to new instances of Animal
@komuw
komuw / print_array_list.zig
Created May 15, 2019 08:26
we can print a buffer but not ArrayList
const std = @import("std");
const warn = std.debug.warn;
pub fn main() !void {
// 1. This works
var buf = try std.Buffer.init(std.debug.global_allocator, "");
defer buf.deinit();
try buf.append("hello");
warn("buffer:\n\t {}", buf);
@komuw
komuw / read_file.zig
Last active August 11, 2024 04:44
read a file in zig
const std = @import("std");
const os = std.os;
const warn = std.debug.warn;
pub fn main() !void {
var file = try os.File.openRead("/path/to/file.txt");
defer file.close();
const file_size = try file.getEndPos();
// why cant I use?
@komuw
komuw / iterable.py
Created March 22, 2019 15:59
convert list to iterable
import asyncio
import collections
class Iterable:
def __init__(self, my_list):
self.pool = collections.deque(my_list, maxlen=None)
self.len = len(self.pool)
self.index = 0
@komuw
komuw / client.go
Last active January 4, 2019 14:13
client and server odd behaviour
package main
import (
"bytes"
"io"
"log"
"net"
"time"
)
@komuw
komuw / blocking_async.py
Created November 25, 2018 06:39
calling blocking code in async code
import asyncio
import requests
import concurrent
import functools
loop = asyncio.get_event_loop()
def reqq(timeout):
@komuw
komuw / python_named_pipe.py
Created November 1, 2018 15:23
python named pipe
import os
import errno
import time
"""
The linux pipe buffers are implemnted as circular buffers[1].
A consequence of the circular buffer is that when it is full and a subsequent write is performed:
(a) then it starts overwriting the oldest data[2].
(b) Alternatively, the routines that manage the buffer could prevent overwriting the data and return an error or raise an exception.
@komuw
komuw / leak_detector.go
Last active February 19, 2020 09:27
A Goroutines leak detector
package main
// or just use:
// 1. https://github.com/uber-go/goleak
// 2. github.com/cockroachdb/cockroach/pkg/util/leaktest
import (
// "bytes"
"fmt"
"os"