Skip to content

Instantly share code, notes, and snippets.

const std = @import("std");
const io = std.io;
const assert = std.debug.assert;
// This example shows one possible way to approach Polymorphism in Zig
// using dynamic dispatch.
//
// Largely inspired by:
// - std/mem/Allocator.zig
// https://github.com/ziglang/zig/blob/77836e08a2384450b5e7933094511b61e3c22140/lib/std/mem/Allocator.zig#L55
@andreadipersio
andreadipersio / huffman.rb
Created September 2, 2019 20:46
Huffman Encoder/Decoder
# Huffman Encoder/Decoder
#
# usage:
# To encode a string:
# > encode_string("foo foo bar")
# It returns an encoding table and the encoded text (TODO)
#
# https://engineering.purdue.edu/ece264/17au/hw/HW13?alt=huffman
TreeValue = Struct.new(:character, :frequency)
@andreadipersio
andreadipersio / amazon-prod-ad-api.go
Created December 15, 2013 14:53
Some scratch code I wrote time ago to get amazon product information through Product Advertisement API. Use as a starting point.
package amazon
import (
"net/url"
"strings"
"fmt"
"regexp"
"time"
"crypto/hmac"
"crypto/sha256"
@andreadipersio
andreadipersio / metaclass-example1-py2.py
Last active December 30, 2015 11:59
An example on how to use metaclasses, difference between python 2 and python 3 and using six to write code supported on both.
METACLASS_TXT = """
Hello, I am {} metaclass!
I am now creating '{}' class, child of '{}',
having the following class attributes:
{}
"""
class OCPRobotMeta(type):
def __new__(cls, name, bases, attrs):
"""new is called for the creation of a new class"""
@andreadipersio
andreadipersio / switch-nginx-binary.sh
Last active January 20, 2022 14:14
How to switch nginx binary at runtime.
# inspired by
# Master NGINX - Dimitri Aivaliotis
# http://www.amazon.com/Mastering-NGINX-Dimitri-Aivaliotis-ebook/dp/B00B90PJR4
# Chapter 8 - Switching binaries at runtime
# We get the pid of the 'to be replaced' nginx master process
export nginx_old_pid=`cat /var/run/nginx.pid`
# we send an USR2 signal to tell 'to be replaced' process
# to start a new master process.
package main
import (
"fmt"
"log"
"flag"
"strings"
import tornado.ioloop
import tornado.web
import tornado.auth
import tornado.gen
from tornado import escape, httpclient
from tornado.options import parse_command_line, define, options
try:
import urllib.parse as urllib_parse # py3
import tornado.ioloop
import tornado.web
import tornado.auth
import tornado.gen
from tornado.options import parse_command_line, define, options
define("port", default=8888)
define("debug", default=False)
import tornado.ioloop
import tornado.web
from tornado.options import parse_command_line, define, options
define("port", default=8888)
define("debug", default=False)
class AuthHandler(tornado.web.RequestHandler):
def get(self):
import tornado.ioloop
import tornado.web
from tornado.options import parse_command_line, define, options
define("port", default=8888)
define("debug", default=False)
class AuthHandler(tornado.web.RequestHandler):
def get(self):