Skip to content

Instantly share code, notes, and snippets.

View ktbarrett's full-sized avatar

Kaleb Barrett ktbarrett

  • Hudson River Trading
  • Boulder, CO
View GitHub Profile
class TaskManager(Mapping[str, Task[Any]]):
"""Associative container of names to child Tasks.
Tasks created with this object are not allowed to leak;
child tasks will be cancelled if the TaskManager is collected.
Typically this is used as an async context manager,
forcing all child Tasks to finish before returning.
@ktbarrett
ktbarrett / cached_method.py
Last active August 14, 2024 21:53
For caching method calls on instances
from functools import update_wrapper, wraps
class cached_method:
def __init__(self, method):
self._method = method
update_wrapper(self, method)
def __get__(self, instance, objtype=None):
if instance is None:
@ktbarrett
ktbarrett / sum.cpp
Created November 15, 2023 20:35
sum metafunction C++17
#include <variant>
template <typename ...Args>
struct sum_;
template <typename Last>
struct sum_<Last> {
using type = Last;
};
@ktbarrett
ktbarrett / logic_array.pyi
Last active September 21, 2023 10:33
LogicArray mock up
from functools import cache, cached_property
from typing import (
Collection,
Iterable,
Optional,
Reversible,
Sized,
TypeAlias,
TypeVar,
overload,
@ktbarrett
ktbarrett / c3.py
Created May 17, 2023 22:29
Python implementation of the C3 algorithm used in Python for MRO
from functools import lru_cache
from typing import Dict, Hashable, List, Optional, TypeVar
T = TypeVar("T", bound=Hashable)
def c3_linearize(dep_tree: Dict[T, List[T]], root: T) -> List[T]:
"""Linearize a dependency tree"""
@lru_cache(None)
from typing import Optional, Tuple, Union, overload
_time_unit_map = {
"ps": -12,
"ns": -9,
"us": -6,
"ms": -3,
"s": 0,
}
@ktbarrett
ktbarrett / logerr.sh
Created July 29, 2022 04:25
Like tee, but just on stderr
function logerr {
file="$1"
shift 1
"$@" 2> >(tee "$file" >&2)
}
@ktbarrett
ktbarrett / generic_ff_pack.vhd
Created July 26, 2022 16:43
Generic FF procedure
library ieee;
use ieee.std_logic_1164.all;
package generic_ff_pack is
procedure FF
generic (
type T)
parameter (
signal q : out T;
constant d : in T;
@ktbarrett
ktbarrett / example.vhd
Created July 22, 2022 16:11
Example initial block
entity piecewise_function is
generic (
NODE_MAP : NodeMapType;
NODE_ID : string;
NUM_SEGMENTS : natural;
THRESHOLD_SIZE_RES : sfixed;
BIAS_SIZE_RES : sfixed;
GAIN_SIZE_RES : sfixed);
port (
@ktbarrett
ktbarrett / example1.py
Last active April 10, 2023 19:48
Join blocks
async def test(dut):
with TaskManager() as tm:
@tm.fork
async def stimulate():
pass # stimulate an interface
@tm.fork