From 509c5d723dd4f25cb0ac553e67a18b900d55a2be Mon Sep 17 00:00:00 2001 From: lda Date: Sun, 23 Aug 2026 21:29:58 +0700 Subject: [PATCH] fmt + script --- cf-ep.py | 2 +- ls_sessions.py | 6 ++-- pynput_demo.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++ requests_demo.py | 11 +++++++- turing_machine.py | 1 - 5 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 pynput_demo.py diff --git a/cf-ep.py b/cf-ep.py index d4dd718..180e271 100644 --- a/cf-ep.py +++ b/cf-ep.py @@ -5,4 +5,4 @@ def decode(string: str, start: int = 0): if __name__ == "__main__": - decode("f09c9491b09c949c9491de939f9d", 0) # lda@ldlda.com + decode("f09c9491b09c949c9491de939f9d", 0) # lda@ldlda.com diff --git a/ls_sessions.py b/ls_sessions.py index 2d6357c..7374895 100644 --- a/ls_sessions.py +++ b/ls_sessions.py @@ -28,7 +28,7 @@ def parse_session_filename(name: str): return None started = datetime.strptime( - f'{match.group("date")} {match.group("time")}', + f"{match.group('date')} {match.group('time')}", "%Y-%m-%d %H-%M-%S", ) @@ -61,10 +61,10 @@ def format_session_line(file: Path) -> str | None: size = format_size(stat.st_size) return ( - f'{parsed["started"]:%Y-%m-%d %H:%M:%S} | ' + f"{parsed['started']:%Y-%m-%d %H:%M:%S} | " f"updated {modified:%Y-%m-%d %H:%M:%S} | " f"{size:>8} | " - f'codex resume {parsed["uuid"]}' + f"codex resume {parsed['uuid']}" ) diff --git a/pynput_demo.py b/pynput_demo.py new file mode 100644 index 0000000..b3b2506 --- /dev/null +++ b/pynput_demo.py @@ -0,0 +1,71 @@ +# /// script +# dependencies = [ +# "pynput", +# "pyautogui", +# ] +# /// + +# uv likes this block + +from collections.abc import Callable + +import pynput + +controller = pynput.keyboard.Controller() + + +type Key = pynput.keyboard.Key | pynput.keyboard.KeyCode | None +type ListenerFn = Callable[[Key], bool | None] + + +def h() -> tuple[ListenerFn, ListenerFn]: + a_first = True + held = False + + A = pynput.keyboard.KeyCode.from_char("a") + D = pynput.keyboard.KeyCode.from_char("d") + TRIGGER = pynput.keyboard.KeyCode.from_char("\\") + FLIP = pynput.keyboard.KeyCode.from_char("/") + QUIT = pynput.keyboard.KeyCode.from_char("'") + + def order(do): + if a_first: + do(A) + do(D) + else: + do(D) + do(A) + + def funny(key: Key, press: bool) -> bool | None: + nonlocal a_first, held + + if key == TRIGGER: + if press and not held: + order(controller.press) + held = True + + elif not press and held: + order(controller.release) + held = False + + elif key == FLIP and press: + a_first = not a_first + print(f"flipped a_first to {a_first}") + + elif key == QUIT and press: + return False + + return ( + lambda key: funny(key, True), + lambda key: funny(key, False), + ) + + +def main(): + press, release = h() + with pynput.keyboard.Listener(on_press=press, on_release=release) as listener: + listener.join() + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/requests_demo.py b/requests_demo.py index 66760a8..d9e1484 100644 --- a/requests_demo.py +++ b/requests_demo.py @@ -1,3 +1,12 @@ +# /// script +# dependencies = [ +# "requests", +# "beautifulsoup4", +# "pypdf", +# ] +# /// + + import io from collections.abc import Iterable from functools import partial @@ -61,7 +70,7 @@ def ts_pdf(s: requests.Session, url: str) -> bytes: def main(): u, p = input("Username: "), getpass("Password: ") - pdfs = ( # example + pdfs = ( # example "https://moodle.usth.edu.vn/pluginfile.php/91136/mod_resource/content/1/Chapter%201%20v1.pdf", "https://moodle.usth.edu.vn/pluginfile.php/91137/mod_resource/content/1/Chapter%202.pdf", "https://moodle.usth.edu.vn/pluginfile.php/91138/mod_resource/content/1/Chapter%203%20%20.pdf", diff --git a/turing_machine.py b/turing_machine.py index 3872ede..fac9d4e 100644 --- a/turing_machine.py +++ b/turing_machine.py @@ -112,4 +112,3 @@ tm = TuringMachine( final_state, final_tape = tm.run("00110") print("Final State:", final_state) print("Final Tape:", "".join(final_tape).strip()) -