Skip to content

Instantly share code, notes, and snippets.

View CodeByAidan's full-sized avatar
💻
i love HPC/DL

aidan CodeByAidan

💻
i love HPC/DL
View GitHub Profile
@CodeByAidan
CodeByAidan / point.py
Created June 14, 2024 14:30
yet another point example in python, but with ctypes, and heavy type annotations, since who doesn't like them? 😅
from __future__ import annotations
import ctypes as ct
class Point(ct.Structure):
_fields_: list[tuple[str, ct._SimpleCData]] = [("x", ct.c_int), ("y", ct.c_int)]
def __repr__(self) -> str:
return f"({self.x}, {self.y})"
@CodeByAidan
CodeByAidan / get_param_var_name.py
Created June 13, 2024 19:59
Get a variable's name that was passed into a function in Python using this little snippet!
import inspect
def foo(bar) -> str | None:
bar_name: str = [
name
for name, obj in inspect.currentframe().f_back.f_locals.items()
if obj is bar
][0]
return bar_name
@CodeByAidan
CodeByAidan / type-annotate-checker.py
Created June 13, 2024 19:12
A decorator using only built-in libraries in Python, to type-hint/annotate parameters.
import functools
import sys
from typing import Any, Callable, TypeVar
T = TypeVar("T")
def reveal_type[T](obj: T, /) -> T:
print(f"Runtime type is {type(obj).__name__!r}", file=sys.stderr)
return obj
@CodeByAidan
CodeByAidan / out.txt
Created June 13, 2024 17:49
`python -m py_compile .\temp2.py` then `python .\view_pyc_file.py .\__pycache__\temp2.cpython-312.pyc`
0 0 RESUME 0
1 2 LOAD_CONST 0 (0)
4 LOAD_CONST 1 (None)
6 IMPORT_NAME 0 (functools)
8 STORE_NAME 0 (functools)
2 10 LOAD_CONST 0 (0)
12 LOAD_CONST 1 (None)
14 IMPORT_NAME 1 (sys)
@CodeByAidan
CodeByAidan / fib.ml
Created June 12, 2024 16:03
another fibonacci function, but this time, OCAML! Pretty simple!
let fib n =
let rec aux acc n2 n1 = function
| 1 -> acc
| c -> aux ((n2 + n1) :: acc) n1 (n2 + n1) (c - 1)
in
List.rev(aux [1; 0] 0 1 (n - 1))
;;
let () =
let fibs = fib 50 in
@CodeByAidan
CodeByAidan / time-command-execution.ps1
Created June 12, 2024 13:28
Found a neat one-liner way of timing the last command you ran using PowerShell! For example, run a python file, so like `python regex_playground.py` then run the one-liner and it returns `00:00:00.3471487`!
(Get-History -Count 1 | Select-Object -Property @{Name = 'ts'; Expression = { $_.EndExecutionTime - $_.StartExecutionTime} }).ts.ToString()
@CodeByAidan
CodeByAidan / threadpool.py
Last active June 11, 2024 19:01
asyncio.ThreadPool prototype implementation and demo
import asyncio
import concurrent.futures
import functools
import os
import threading
import time
import weakref
from typing import Any, Callable, Optional, TypeVar
_threads_queues = weakref.WeakKeyDictionary()
@CodeByAidan
CodeByAidan / manipulating_bits_in_vector.py
Last active June 11, 2024 18:37
Simple and easy class that helps manipulating bits in a vector in Python, using overloaded methods.
from typing import TypeVar
T = TypeVar("T", int, slice)
class BitVector:
"""A class for manipulating bits in a vector."""
_val: int
@CodeByAidan
CodeByAidan / thread-lock-type-safe-decorator.py
Created June 10, 2024 15:39
fun example of using a type-safe decorator providing a threading.Lock
from collections.abc import Callable
from threading import Lock
from typing import Concatenate
my_lock = Lock()
def with_lock[**P, R](f: Callable[Concatenate[Lock, P], R]) -> Callable[P, R]:
"""A type-safe decorator which provides a lock."""
@CodeByAidan
CodeByAidan / memory_manager.c
Created June 9, 2024 22:24
memory manager example in C, no external libs.
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BLOCKS 10
#define BLOCK_SIZE 1024
typedef struct {
char *block;