Skip to content

Instantly share code, notes, and snippets.

@dmwyatt
dmwyatt / path_walk.py
Created Mar 22, 2022
[breadth-first path walk] Walk a directory tree yielding all files and directories using a breadth-first algo.
View path_walk.py
def path_walk(path: str) -> Generator[str, None, None]:
"""Walks a directory tree yielding all files and directories.
This uses a breadth-first algorithm.
"""
queue = deque([path])
while queue:
current = queue.popleft()
for entry in os.scandir(current):
entry: os.DirEntry
@dmwyatt
dmwyatt / knownpaths.py
Created Jan 28, 2022 — forked from mkropat/knownpaths.py
Python wrapper around the SHGetKnownFolderPath Windows Shell function
View knownpaths.py
import ctypes, sys
from ctypes import windll, wintypes
from uuid import UUID
class GUID(ctypes.Structure): # [1]
_fields_ = [
("Data1", wintypes.DWORD),
("Data2", wintypes.WORD),
("Data3", wintypes.WORD),
("Data4", wintypes.BYTE * 8)
@dmwyatt
dmwyatt / read_dot_env.py
Created Dec 25, 2021
[read dot env] Read a .env file and set the env vars
View read_dot_env.py
with suppress(FileNotFoundError):
with open('.env') as f:
line = f.readline()
while line:
k, v = line.split('=', 1)
os.environ[k] = v.strip()
line = f.readline()
@dmwyatt
dmwyatt / setqueue.py
Created Oct 17, 2021
[SetQueue] A Queue that behaves like a Set #datastructure
View setqueue.py
class SetQueue(Queue):
def _init(self, maxsize):
self.queue = set()
def _put(self, item):
self.queue.add(item)
def _get(self):
return self.queue.pop()
@dmwyatt
dmwyatt / monitor.py
Created Oct 16, 2021
[monitor windows files] Monitors files and folders or directories. #system #windows
View monitor.py
import time
from pathlib import Path
import pywintypes
import win32con
import win32file
import winerror
WATCHED_DIR = str(Path("./").absolute())
@dmwyatt
dmwyatt / docstrings.py
Last active May 25, 2021
[Sphinx docs as markdown] Parse autodoc as markdown #sphinx
View docstrings.py
# based on https://stackoverflow.com/a/56428123/23972
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)
lines.clear()
@dmwyatt
dmwyatt / RequireOnlyOne.ts
Created Apr 12, 2021
[RequireOnlyOne] Type that requires one and only one of a set of properties #types
View RequireOnlyOne.ts
// https://stackoverflow.com/a/49725198/23972
// https://www.typescriptlang.org/play?#code/C4TwDgpgBAShCOBXAlgJwgQWAGQgQwGdgB5AOwgB4AVAGigGkIQCoIAPYCUgExYGsmAewBmUKlAC8UASBFiAfJICwAKCjqoABWQBjPtToBRNjoA2ibpRlzaDJgXnzVGqADIoAb2cuNAbXpQyKR2zAC6ALQA-ABcsAgo6NwU2noGDI5uWniowMh4psm6+rbGZhaUjMx09I5OahoAvv72oaqqQZyowng60ACyXIgAkpwAtp7eucCmELFEqEEA5gDc3jqCo2CC5KTAMVCkiKMARhCoq-VmRfuHJ2cX6rrbc8ALpCuqDW0qoJBQAMKmIrEVD-DZbHbASRxJBoTA4fBEMiUAaHEYQUZ0ADkVz0WKgAB8oDjwdsuMAsXVVOtSEQoAB3ZDAAAWYM2ZN2sUBwNBpMh0K89XUUxmsQARJwiGKaN51Ot2ZDYgBWABMMqFgRp4qepDFn2+NLpjJZ3L0XKBehBbIh5IFsqgItmUAlECl6pcuL4yoALO6NDrtTS9Sovipqds6WdUIJUAB1JnMgByEATZ3NPOtHKhUkFLkd4slwGl9oDzp1wdD4dpUNIgkMqGjcYTACFBCz05beQrbTn7fnnYXixrPT6-XK+eTYirfSWtWWg-qwypDVCozH4yzWyzY8yuMiABKEWPR95UcBO018K0T3Z2jX9l1u+0jqBKmfDm-AcUEZmCRCmbgoFOA4jlOVAoF3dAhxcUsxXLRdvl+aA4FhdAyFMEBkTSSoWHYTgeH4IRRHEKRrGIxQJHtFJiiMExzEsCgyLEap7Ece13A8KB-ECYIcIiGJ7RcFCEggJJqOwtiNRcdxNGyXJ8goOB1lQJJSnoip7GqeQ6EQHgIGEIJRMklwmj4xDzygdDMPIS9r27W8pGEuErKw1FhjGbFPXxIkSXsikqWXCMazrBt1wTFzyFiCKIFsrsbQ
@dmwyatt
dmwyatt / RequireAtLeastOne.ts
Created Apr 12, 2021
[RequireAtLeastOne] Type that requires at least one of it's properties #types
View RequireAtLeastOne.ts
// https://stackoverflow.com/a/49725198/23972
export type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<
T,
Exclude<keyof T, Keys>
> &
{
[K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
}[Keys];
@dmwyatt
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
// https://css-tricks.com/click-outside-detector/
// https://codepen.io/chriscoyier/project/editor/AxPzxn
import React, { useEffect, useRef, forwardRef } from 'https://cdn.skypack.dev/react';
const ClickOutsideDetector = forwardRef(function ClickOutsideDetector(
{ listen, onClickOutside, ignore, ...props },
ref
) {
@dmwyatt
dmwyatt / fakeBaseClass.ts
Created Mar 31, 2021
[fakeBaseClass] Make it so TypeScript can handle Proxy traps in a constructor
View fakeBaseClass.ts
// https://stackoverflow.com/a/51865579/23972
function fakeBaseClass<T>(): new () => Pick<T, keyof T> {
return class {} as any;
}
// USAGE:
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)