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
from typing import Awaitable, Generic, TypeVar
from cocotb.triggers import Event
T = TypeVar("T")
class Signal(Generic[T]):
def __init__(self, __init_value: T) -> None:
from collections import deque
from typing import Deque, Generic, Optional, TypeVar
from cocotb.triggers import Event
T = TypeVar("T")
class RecvFailed(Exception):
...
@ktbarrett
ktbarrett / main.py
Created February 24, 2022 03:33 — forked from mypy-play/main.py
Shared via mypy Playground
from typing import *
K_contra = TypeVar("K_contra", contravariant=True)
K_co = TypeVar("K_co", covariant=True)
V_co = TypeVar("V_co", covariant=True)
V = TypeVar("V")
T = TypeVar("T")
class ContravariantMapping(Protocol[K_contra, V_co]):
def __getitem__(self, __key: K_contra) -> V_co: ...
@ktbarrett
ktbarrett / generic_FF.vhd
Last active December 13, 2021 16:33
VHDL type-generic entities and subprograms
-- package containing a type-generic D Flip Flop
-- may not be 100% valid VHDL code, contact ktbarrett on gitter
-- non-generic version does synthesize correctly
package generic_pkg is
procedure generic_FF
generic (
constant T: type)
paramater (
signal q : out T;
* simulator control (enums or separate functions?)
* SHUTDOWN
* RESTART (new addition)
* logging
* levels
* TRACE
* DEBUG
* INFO
@ktbarrett
ktbarrett / alert.py
Created October 25, 2021 04:10
recurring event type
from typing import Awaitable
from cocotb.triggers import Trigger, _Event
class Alert(Awaitable[None]):
def __init__(self) -> None:
self._pending = []
def set(self) -> None:
@ktbarrett
ktbarrett / channel.py
Last active October 26, 2021 15:32
Broadcasting channels with inline stream processing and events in Python
from abc import ABCMeta, abstractmethod, abstractproperty
from asyncio import QueueEmpty
from collections import deque
from typing import (
Callable,
Deque,
Generic,
Protocol,
Set,
Type,
#include <stdnoreturn.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
typedef enum {
PUSH,
POP,
ADD,
SUB,
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))
@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):
...