Skip to content

Instantly share code, notes, and snippets.

from datetime import datetime, timezone
non_naive_timestamp = datetime.now(timezone.utc)
iso_timestamp = non_naive_timestamp.isoformat("T") + "Z"
filename_safe_timestamp = f"{non_naive_timestamp:%Y%m%d-%H%M%S%Z}"
@djoek
djoek / toolio.py
Last active October 16, 2024 13:03
A function that quickly creates the schema for a tool, to be used in OpenAI's Tool Calling API
from inspect import signature, Parameter
from typing import Annotated, Callable, get_origin
# Incomplete, should import jsonschema
python_to_json_schema_types = {
str: "string",
int: "integer",
float: "number",
bool: "boolean",
from PIL import Image, ImageFilter
def drop_shadow(layer, color="#000", radius=0, distance=0):
radius = radius or layer.size[0] // 64
distance = distance or layer.size[0] // 64
shadow = Image.new(layer.mode, layer.size, color=color)
shadow.putalpha(layer.effect_spread(distance).getchannel('A'))
import csv
class Invisible(csv.Dialect):
delimiter = chr(0x1f) # US
escapechar = None
lineterminator = chr(0x1e) # RS
quoting = csv.QUOTE_NONE
@djoek
djoek / iluminize.yaml
Last active September 3, 2023 13:50
iluminize rgb remote zigbee
blueprint:
name: iluminize ZG2819S-RGBW
source_url: https://gist.github.com/djoek/ddabd8c94057a86aad1734e0ab513886/raw/bee58d171bc79b7151a65456cc7da670646282c7/iluminize.yaml
description: Control lights with a iluminize 511.344 RGBW Remote
domain: automation
input:
remote:
name: Remote
description: iluminize 511.344 remote to use
selector:
from functools import wraps
def memoized_property(fget):
attr_name = '_{0}'.format(fget.__name__)
@wraps(fget)
def fget_memoized(self):
if not hasattr(self, attr_name):
setattr(self, attr_name, fget(self))
.progress-donut {
background: conic-gradient(
from -135deg,
rgba(255, 0, 0, 1.0) 80deg, /* change me for value set */
rgba(255, 0, 0, 0.3) 0deg 270deg,
rgba(255, 0, 0, 0.0) 270deg
);
mask-image: radial-gradient(
rgba(0, 0, 0, 0) 25%,
rgba(0, 0, 0, 1) 25%,
@djoek
djoek / 00_README.md
Last active September 27, 2024 12:53
FizzBuzz collection

djoek's FizzBuzz Collection

A collection of FizzBuzz implementations in python I've written and/or collected over the years. No need to comment that most of these are silly, I'm well aware and this is intentional.

from typing import Union, Awaitable, AsyncIterable
def aiter(o: Union[AsyncIterable, Awaitable], sentinel=None):
if isinstance(o, AsyncIterable):
async for item in o:
yield item
elif isinstance(o, Awaitable):
while True:
val = await o
if (sentinel is None and val is sentinel) or val == sentinel:
from numpy import arange, ceil, sqrt
candidates = arange(100)
candidates[1] = 0 # 1 is not a prime
upto = ceil(sqrt(candidates.size)).astype(candidates.dtype)
for c in candidates[2:upto]:
candidates[2*c::c].fill(0) if c else ...
print(candidates[candidates > 0])