Skip to content

Instantly share code, notes, and snippets.

@iafisher
iafisher / oop.md
Last active May 6, 2024 12:19
Some notes on object-oriented programming techniques and their practical applications

Notes on object-oriented programming

Overview of object-oriented techniques

Inheritance

Inheritance is a mechanism by which a class is populated with fields and methods not only from its explicit definition, but also from a set of one or more parent classes. Languages with inheritance (among them Java, C++ and Python) typically allow classes to declare that some methods cannot be redefined by child classes (final in Java), some methods must be redefined, and that some fields may not be used by child classes (private versus protected).

Although inheritance was the marquee feature of influential object-oriented languages like Java and C++, its reputation has suffered considerably in recent years. Even Effective Java, one of the most popular guides to the Java language, recommends severely curtailing its use and recommends composition over inheritance and the use of interfaces over

from iafisher_precommit import Precommit, Problem, checks
def main():
precommit = Precommit()
# Generic checks
precommit.register(checks.NoStagedAndUnstagedChanges())
precommit.register(checks.NoWhiteSpaceInFilePath())
@iafisher
iafisher / trycatch.c
Created June 10, 2019 14:35
Exception handling with try, catch and throw statements, implemented in pure C99
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
const char* e = NULL;
#define EXCEPTION_DEPTH 128
jmp_buf* iafisher_trycatch_stack[EXCEPTION_DEPTH];
@iafisher
iafisher / mystery-lambda.rkt
Last active April 3, 2019 19:31
Minimal example of local-expand not turning lambda into #%plain-lambda in Racket
#lang racket
(define-syntax (show stx)
(syntax-case stx ()
[(_ x)
(let ([fully (local-expand #'x 'expression #f)])
(displayln (syntax->datum fully))
#'x)]))
; expect to see (#%plain-lambda (x) x), but get (lambda (x) x) instead
@iafisher
iafisher / bookmarks_from_sql.py
Created March 9, 2019 22:54
Programmatically access your Firefox bookmarks
"""
A script to automatically export bookmarks from Firefox's SQLite database.
There does not seem to be a programmatic way to get Firefox to export its bookmarks in
the conventional HTML format. However, you can access the bookmark information directly
in Firefox's internal database, which is what this script does.
Always be careful when working with the internal database! If you delete data, you will
likely not be able to recover it.
@iafisher
iafisher / pratt.py
Last active April 23, 2020 05:09
Small working example of a Pratt parser for infix expressions
"""Small but complete example of a Pratt recursive-descent parser for the following
grammar:
start := expr
expr := expr op expr | call | LPAREN expr RPAREN | MINUS expr | INT | SYMBOL
call := SYMBOL LPAREN arglist? RPAREN
op := PLUS | ASTERISK | MINUS | SLASH
arglist := (expr COMMA)* expr
@iafisher
iafisher / bookmarks.py
Last active March 9, 2019 22:40
DEPRECATED (see my bookmarks_from_sql.py gist): A small Python utility to parse bookmarks exports from Firefox
"""
DEPRECATED: see my bookmarks_from_sql.py gist.
A short script to parse bookmark exports from Firefox so they can be
manipulated with Python.
Author: Ian Fisher (iafisher@protonmail.com)
Version: November 2018
"""
from collections import namedtuple
@iafisher
iafisher / Enclosing.java
Created June 26, 2018 16:21
Minimal example of instantiating and using objects of a static inner class
import lombok.Data;
public class Enclosing {
@Data
public static class Inner {
private String value;
}
public static void main(String[] args) {
Inner inner1 = new Inner();
@iafisher
iafisher / testhelper_demo.py
Last active June 23, 2018 16:11
A demo of a testing utility for the Python shell that automatically generates unit tests based on your interactive session
>>> import mylib, testhelper as th
>>> fibonacci = th.register(mylib.fibonacci)
>>> fib(12)
144
[testhelper] Is this the expected result (y[es]/n[o]/c[ancel])? y
>>> fib(-1)
Traceback (most recent call last):
...
ValueError
[testhelper] Is this the expected result (y[es]/n[o]/c[ancel])? y
@iafisher
iafisher / crypto.py
Last active April 5, 2018 17:07
Minimal working crypto example with Diffie-Helman
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from cryptography.hazmat.primitives.padding import PKCS7
from cryptography.hazmat.primitives.ciphers import Cipher, modes, algorithms
import os