Skip to content

Instantly share code, notes, and snippets.

@qexat
qexat / bf_hello_world_one_line.py
Created September 2, 2022 22:23
One-liner hello world program written in Brainfuck but in a Python f-string
f'{(hw:="++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.")}{(bf:=lambda prgm:print(((pc:=0),(ti:=0),(t:=[0]*30000),(rs:=[]),(o:=""),(*(((char:=prgm[pc]),(((ti:=ti+1),(pc:=pc+1))if char==">"else(((ti:=ti-1),(pc:=pc+1))if char=="<"else(((t.__setitem__(ti,(t[ti]+1)&0xFF)),(pc:=pc+1))if char=="+"else(((t.__setitem__(ti,(t[ti]-1)&0xFF)),(pc:=pc+1))if char=="-"else(((o:=o+chr(t[ti])),(pc:=pc+1))if char=="."else(((t.__setitem__(ti,ord(input())%0xFF)),(pc:=pc+1))if char==","else(((rs.append(pc),(pc:=pc+1))if t[ti]!=0 else((bc:=1),(*(((pc:=pc+1),((bc:=bc+1)if prgm[pc]=="["else(bc:=bc-1)if prgm[pc]=="]"else...),(pc:=pc+1))for _ in iter(lambda:bool(bc),False)),)))if char=="["else((pc:=rs.pop())if char=="]"else...)))))))))for _ in iter(lambda:pc<len(prgm),False)),))[-1][-1][-1][0]))}{(bf(hw))}'
@qexat
qexat / typing_trick.py
Created November 16, 2022 14:41
the fact that it passes mypy and pyright completely lmao
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Generic, Literal, overload, TypeGuard, TypeVar
T = TypeVar("T")
HasPopd = TypeVar("HasPopd", bound=bool)
@dataclass
@qexat
qexat / silly_fib.py
Created January 16, 2023 01:25
silly fib
fib=(m:=globals(),(i:=lambda md:m.__setitem__(md,__import__(md)))("builtins"),i("sys"),J:=(U:="_","SIG")[1],j:=lambda *hx:U[True:].join(map(chr,hx)),p:=lambda v:v in {False,True},z:=builtins.__dict__[j(0x6C,0x65,0x6E)](()),t:=(s:=(m:=sys.modules)[U+j(115,105,103,110,97,108)],c:=m[U+j(99,111,100,101,99,115)],g:=m[j(98,117,105,108,116,105,110,115)].__dict__[j(105,110,116)],)[z].__dict__[J+g.__name__.upper()],_a:=lambda k:k.startswith(j(0o165,0o164,0o146)),tt:=list(filter(_a,c.__dict__.keys()))[(r:=~z)][t**t:t*(h:=r*(r+(z**z^r)))],ns:=t**getattr(s,j(95,95,100,105,99,116,95,95))[J+j(65,66,82,84)]+eval(tt),a:="".join(map(lambda n:chr(n+ns),x:=(eval("\x2A".join(tt)),h**t,t))),m.__setitem__(a,lambda n:n if p(n) else (y:=m[a])(n-t)+y(n+r)),l:=m[a],lambda n:print(l(n)))[-1]
for i in range(10):
fib(i)
@qexat
qexat / example.py
Last active February 23, 2023 09:16
Debug thingy imagined by @waffle-annie brought to life!
from waffle_debug import debug
debug("The answer is {x}!") @ 6 # debug at line 6
x = int(input("3 + 3 = "))
x = 6
@qexat
qexat / arrow_operator.py
Created February 23, 2023 09:15
Ah yes Python's arrow operator
class hackint(int):
def __neg__(self):
for key, value in globals().items():
if self is value:
globals()[key] = type(self)(self - 1)
return self
x = hackint(5)
@qexat
qexat / __main__.py
Last active March 12, 2024 14:07
Python implementation of Solution 3 from Let's Get Rusty's "Improve your Rust APIs with the type state pattern" video
"""
PoC of an equivalent Python implementation of the third solution borrowed from the following video:
<https://www.youtube.com/watch?v=_ccDqRTx-JU>
Important note: Python static type checkers are independent from the interpreter, and type checking failing
does not prevent from running code. Considering that, the following code does NOT perform runtime checks for
safety. `add_password()` is still technically accessible, even through a `PasswordManager` object in a locked
state (i.e. `PasswordManager[Literal[True]]`).
The program also requires the following dependencies:
@qexat
qexat / cursed_exception_divergence.py
Last active May 2, 2023 09:12
cursed exception divergence using impure-state tracker object
# requires: >= 3.10
from __future__ import annotations
from collections.abc import Callable
import functools
import inspect
from types import TracebackType
from typing import Concatenate, ParamSpec, TypeVar
@qexat
qexat / rust_vec.py
Created April 11, 2023 17:26
is this rust? is this python? the answer is: yes!
from collections.abc import Callable, Iterable, Iterator
from typing import Generic, Never, Self, TypeVar, overload
T = TypeVar("T")
class PrivacyError(TypeError):
@classmethod
def from_private_constructor(cls, type: type, /, *public_constructors: str) -> Self:
"""
@qexat
qexat / one4all.py
Last active May 7, 2023 22:34
One for all - one call, every method
from __future__ import annotations
from collections.abc import Callable, Iterable
import sys
from typing import (
Any,
Generic,
Literal,
LiteralString,
SupportsIndex,
@qexat
qexat / pycanvas_example.py
Last active June 15, 2023 09:21
pycanvas example
#!/usr/bin/env python3
import pycanvas
# We need this global constant to get the mouse position
WINDOW = pycanvas.get_window()
@pycanvas.init
async def init(canvas: pycanvas.Canvas):