Skip to content

Instantly share code, notes, and snippets.

dmwyatt /
Created Oct 17, 2021
[SetQueue] A Queue that behaves like a Set #datastructure
class SetQueue(Queue):
def _init(self, maxsize):
self.queue = set()
def _put(self, item):
def _get(self):
return self.queue.pop()
dmwyatt /
Created Oct 16, 2021
[monitor windows files] Monitors files and folders or directories. #system #windows
import time
from pathlib import Path
import pywintypes
import win32con
import win32file
import winerror
WATCHED_DIR = str(Path("./").absolute())
dmwyatt /
Last active May 25, 2021
[Sphinx docs as markdown] Parse autodoc as markdown #sphinx
# based on
import commonmark
def docstring(app, what, name, obj, options, lines):
if len(lines) > 1 and lines[0] == "@&ismd":
md = "\n".join(lines[1:])
ast = commonmark.Parser().parse(md)
rst = commonmark.ReStructuredTextRenderer().render(ast)
dmwyatt / RequireOnlyOne.ts
Created Apr 12, 2021
[RequireOnlyOne] Type that requires one and only one of a set of properties #types
View RequireOnlyOne.ts
dmwyatt / RequireAtLeastOne.ts
Created Apr 12, 2021
[RequireAtLeastOne] Type that requires at least one of it's properties #types
View RequireAtLeastOne.ts
export type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<
Exclude<keyof T, Keys>
> &
[K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
dmwyatt / ClickOutsideDectector.js
Created Apr 2, 2021
[clickOutsideDector] React component to detect if the component it's wrapping is clicked outside of. #React #ui
View ClickOutsideDectector.js
import React, { useEffect, useRef, forwardRef } from '';
const ClickOutsideDetector = forwardRef(function ClickOutsideDetector(
{ listen, onClickOutside, ignore, ...props },
) {
dmwyatt / fakeBaseClass.ts
Created Mar 31, 2021
[fakeBaseClass] Make it so TypeScript can handle Proxy traps in a constructor
View fakeBaseClass.ts
function fakeBaseClass<T>(): new () => Pick<T, keyof T> {
return class {} as any;
class FooProxy extends fakeBaseClass<Foo>(){
private foo: Foo; // I would make this private as it is not really accessible on what the constructor of FooProxy returns (maybe remove it as I see no use for it)
dmwyatt / TailwindConstants.js
Last active Mar 23, 2021
Extract color value from tailwind theme.
View TailwindConstants.js
import resolveConfig from "tailwindcss/resolveConfig";
// Update next import to the location of your tailwind config. If you're using CRA
// and your `tailwind.config.js` is outside of `src`, you might have to do
// Shenanigans™ to import it. (You can use craco and
import tailwindConfig from "../../tailwind.config";
const THEME = resolveConfig(tailwindConfig).theme;
dmwyatt / nestedGet.js
Created Mar 23, 2021
[nestedGet] Given list of strings, access nested properties in object.
View nestedGet.js
* Given list of stings, access nested properties in object.
* @param {Object<string, *>} object
* @param {Array<string>} accessors
* @returns {*}
function nestedGet(object, accessors) {
return accessors.reduce((acc, prop) => {
return acc?.[prop];
dmwyatt / lazyProperty.js
Created Feb 11, 2021
[lazyProperty] Lazy getters on JS classes.
View lazyProperty.js
* Lazily-evaluated getters.
* ```js
* class Person {
* get name() {
* lazyProperty(this, 'name', calculateSomeValueForName())
* return
* }
* }