Skip to content

Instantly share code, notes, and snippets.

View jaycosaur's full-sized avatar
🐦

Jacob Richter jaycosaur

🐦
  • Sydney, Australia
View GitHub Profile
@jaycosaur
jaycosaur / fanout_queue.py
Last active July 18, 2022 19:26
Async and Sync queue message multicasting to multiple queues. This is the implementation of a message fanout strategy for worker threads and processes. Note this doesn't create worker threads / processes, it only manages (in a blocking way) multicasting messages.
from typing import Type, Set, Any
from multiprocessing import Queue
import asyncio
class MulticastQueue:
def __init__(self, queue_constructor: Type[Queue] = Queue) -> None:
self.subscribers: Set[Queue] = set()
self.constructor = queue_constructor
def register(self) -> Queue:
@jaycosaur
jaycosaur / queue_pipe.py
Created February 1, 2020 11:30
QueuePipe joins forwards messages from one theading queue to another, can be stopped by invoking the stop() method.
from queue import Queue
from threading import Thread
class KillSignal:
pass
class QueuePipe:
def __init__(self, queue_in: Queue, queue_out: Queue) -> None:
@jaycosaur
jaycosaur / ticker.py
Created February 1, 2020 11:32
Emulation of Ticker class in golang. Emits the time at intervals of 'time_to_wait' seconds on internal queue. Can be stopped by calling 'stop' method.
from queue import Queue, Empty
from threading import Thread
from typing import Any
import time
class Ticker:
_alive = True
_q = Queue()
_stop_q = Queue()
@jaycosaur
jaycosaur / multiplex_queue.py
Last active February 1, 2020 12:11
Multiplex class joins messages from multiple queues together using QueuePipe's. Useful for fan-in jobs.
from queue import Queue
from threading import Thread
from typing import Any, List, Iterator
class PipeClose:
pass
class QueuePipe:
@jaycosaur
jaycosaur / person.ts
Last active August 9, 2020 13:06
Implicit vs Explicit typing [typescript] - Typescript to Python field guide
interface Person {
name: string;
age: number;
height: number;
friends: Person[];
}
// Jane implicitly implements the person type
const jane = {
name: "Jane",
@jaycosaur
jaycosaur / person.py
Created August 9, 2020 11:42
Implicit vs Explicit typing [python] - Typescript to Python field guide
from typing import Sequence
from dataclasses import dataclass
@dataclass
class Person:
name: str
age: int
height: float
friends: Sequence[Person]
# in Python, unless using Protocols, variables must implement types explicitly
@jaycosaur
jaycosaur / union.ts
Created August 9, 2020 11:44
Union types [typescript] - Typescript to Python field guide
type StringOrNumber = string | number;
const somethingGood: StringOrNumber = "123"; // ok!
const somethingBad: StringOrNumber = {}; // not ok!
@jaycosaur
jaycosaur / union.py
Created August 9, 2020 11:45
Union types [python] - Typescript to Python field guide
from typing import Union
StringOrNumber = Union[str, int]
something_good: StringOrNumber = "123" # ok!
something_bad: StringOrNumber = {} # not ok!
@jaycosaur
jaycosaur / intersection.ts
Created August 9, 2020 11:46
Intersection types [typescript] - Typescript to Python field guide
type HasAge = { age: number };
type HasName = { name: string };
type HasNameAndAge = HasName & HasAge;
const correct: HasNameAndAge = { name: "Tim", age: 23 };
@jaycosaur
jaycosaur / intersection.py
Created August 9, 2020 11:47
Intersection types [python] - Typescript to Python field guide
from dataclasses import dataclass
@dataclass
class HasAge:
age: int
@dataclass
class HasName:
name: str