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
@ktbarrett
ktbarrett / sync_fifo.v
Last active October 18, 2018 03:54
Synchronous FIFO implementation
`timescale 1ns / 1ps
`default_nettype none
module fifo #(
parameter WIDTH = 32,
parameter DEPTH = 1024,
parameter SKID = 0,
parameter GEN_EXCEPTIONS = 1,
parameter GEN_CONSENTS = 1
@ktbarrett
ktbarrett / ms_combinations.py
Last active January 19, 2019 04:24
Python Multiset combinations
from collections import Counter as Multiset
def multiset_combinations(ms, n):
assert sum(ms.values()) >= n
def f(ms_res, curr_val, ms_rem):
nonlocal n
if sum(ms_res.values()) == n: #ideally len would return the number of total elements
yield ms_res
else:
@ktbarrett
ktbarrett / parameterize.py
Created January 19, 2019 04:23
Python generalized type parameterization factory function
def Parameterize(cls, *mixins, **attributes):
mixin_prefix = ''.join((mixin.__name__ for mixin in mixins))
parameterize = (f"{k}={v}" for k,v in attributes.items())
parameter_suffix = f"({', '.join(parameterize)})"
bases = tuple((*mixins, cls))
base_types = tuple(frozenset(type(t) for t in bases))
base_typename = ''.join((t.__name__ for t in base_types))
base_type = type(base_typename, base_types, {})
return base_type(f"{mixin_prefix}{cls.__name__}{parameter_suffix}", bases, attributes)
@ktbarrett
ktbarrett / pyconf_loader.py
Created September 7, 2019 03:43
Emulate Lua's dofile
import itertools
def load_pyconf_file(filename, env=None):
f = '\t'.join(itertools.chain(("def __func():\n",), open(filename).readlines()))
if env:
exec(f, env)
return env['__func']()
else:
exec(f)
return __func()
@ktbarrett
ktbarrett / computation.py
Created October 14, 2019 06:52
monad stuff
from monad import Monad
class Computation(Monad):
__category = True
@staticmethod
def pure(value):
return Continue(value)
@ktbarrett
ktbarrett / stuff.hpp
Last active November 19, 2019 06:23
unique_handle and xtransform
struct no_delete {
template <typename T>
void operator() (T*) {}
};
template <typename T>
using unique_handle = std::unique_ptr<T, no_delete>;
template <typename T>
auto get_handle_to(T& a) {
@ktbarrett
ktbarrett / user_defined_conversion_constructors.hpp
Last active September 1, 2020 23:46
It works! User defined conversion constructors
// Library code
#include <stdexcept>
#include <type_traits>
#include <utility>
namespace library {
template <typename T>
struct to_positive {
@ktbarrett
ktbarrett / channel.py
Created August 3, 2021 13:09
Generic channel type for cocotb
from asyncio import QueueEmpty, QueueFull
from typing import Deque, Generic, TypeVar
from cocotb.triggers import Event
T = TypeVar("T")
class SendFailed(QueueFull):
...
def build_table(
field_spec: List[Tuple[str, str, Optional[int]]],
table: List[Dict[str, str]],
) -> str:
field_fmts = []
sep_line_fields = []
field_names = []
for name, alignment, width in field_spec:
if width is None:
width = max(len(name), *(len(row[name]) for row in table))
#include <stdnoreturn.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
typedef enum {
PUSH,
POP,
ADD,
SUB,