Skip to content

Instantly share code, notes, and snippets.

View komuw's full-sized avatar

Komu Wairagu komuw

View GitHub Profile
@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 / 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 / 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 / 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 / go_errors_with_stacktrace.go
Created November 9, 2019 15:36
Golang errors with stack traces
package main
import (
"fmt"
"runtime"
"strconv"
"strings"
)
/*
@komuw
komuw / panic_handler.go
Last active December 16, 2020 16:58
global panic handler in Go
package main
import "fmt"
// Usage:
//
// func main() {
// defer panicHandler()
// }
//
@komuw
komuw / fdatasync_vs_fsync.go
Last active July 7, 2021 15:32
Benchmark fdatasync vs fsync
package main
import (
"log"
"math/rand"
"os"
"syscall"
"testing"
"time"
@komuw
komuw / complex_types.go
Last active October 29, 2020 13:29
pointer receiver versus value receiver benchmarks
package main
import (
"fmt"
"math"
"testing"
)
/* ---------- package ---------- */
type h struct {
@komuw
komuw / variable_shadowing_race.go
Created November 5, 2020 13:24
Golang variable shadowing can mask race conditions
package main
import "time"
type cool struct{}
func (c *cool) add() error {
return nil
}