Skip to content

Instantly share code, notes, and snippets.

View JamesTheAwesomeDude's full-sized avatar

James E. A. JamesTheAwesomeDude

View GitHub Profile
@JamesTheAwesomeDude
JamesTheAwesomeDude / _udp_listen.py
Last active March 8, 2024 17:21
Python socket.recv() work with KeyboardInterrupt example
from contextlib import contextmanager
import select
import signal
import socket
try:
from resource import getpagesize
except ImportError:
import mmap
def getpagesize():
@JamesTheAwesomeDude
JamesTheAwesomeDude / safe_tk.py
Last active March 7, 2024 15:27
Python tkinter make exceptions fatal
from contextlib import contextmanager
import tkinter as tk # https://stackoverflow.com/questions/66734619/no-module-named-tkinter#66735147
import tkinter.messagebox
# Two different approaches to handling mainloop errors in tkinter.
# 1. A context manager that will display errors in callbacks and initializers (RECOMMENDED)
# 2. A subclass of Tk that will only display errors in callbacks, not initializers
@contextmanager
@JamesTheAwesomeDude
JamesTheAwesomeDude / _cachedir.py
Last active March 11, 2024 19:40
Python get "user cache directory" on any OS
# Usage:
# import _cachedir
# CACHEDIR = _cachedir.cachedir('my_application@company.com')
# CACHEDIR = _cachedir.cachedir('my_packagename@pypi.org')
# NOTE:
# This does not create a secure enclave of any kind.
# Obviously, malicious applications running on the same
# user account could read or modify the cache directory.
@JamesTheAwesomeDude
JamesTheAwesomeDude / lockscreen.py
Last active February 15, 2024 19:56
Perfectly random Android lockscreen pattern
from itertools import combinations, permutations
import re
MIN_LENGTH = 4
ILLEGAL_PATTERN_RE = re.compile('(%s)' % '|'.join(
# Simulate negative lookbehind via negative lookahead
# h/t https://learnbyexample.github.io/py_regular_expressions/lookarounds.html#negated-groups
f'^(?!.*{b}.*{a}{c}).*{a}{c}' for a,b,c, in
['123', '159', '147', '258', '369', '357', '321', '456', '654', '789', '741', '753', '852', '987', '951', '963']
@JamesTheAwesomeDude
JamesTheAwesomeDude / multipop.py
Created February 7, 2024 19:03
Remove multiple indices at once from Python list
"""https://github.com/python/cpython/issues/53464"""
def simultaneous_remove(l, indices):
##assert isinstance(l, Collection) and not isinstance(l, Mapping)
_len = len(l)
to_remove = sorted(((i if i >= 0 else _len + i) for i in indices), reverse=True)
for i in to_remove:
del l[i]
@JamesTheAwesomeDude
JamesTheAwesomeDude / axiom_of_choice.py
Created December 20, 2023 14:39
get arbitrary element from set in Python
def peek1(collection):
"Get an arbitrary element from *collection*, non-destructively"
try:
return next(iter(collection))
except StopIteration:
raise ValueError("axiom of choice on empty collection")
@JamesTheAwesomeDude
JamesTheAwesomeDude / pypy310_system.reg
Last active January 23, 2024 22:05
add PyPy to "py" launcher
Windows Registry Editor Version 5.00
; py -V:PyPy/3.10
[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PyPy\3.10]
"SupportUrl"="https://www.pypy.org/"
"DisplayName"="PyPy3.10"
"SysVersion"="3.10"
"SysArchitecture"="64bit"
"Version"="7.3.15"
@JamesTheAwesomeDude
JamesTheAwesomeDude / 00.LICENSE.txt
Last active November 3, 2023 19:35
CC0 but without patent limitation
Double-Zero 1.0 Universal
THE TEXT OF THIS DOCUMENT ("00") IS THAT OF THE CREATIVE COMMONS "CC0
1.0 UNIVERSAL", EXCEPT WITH THE LIMITATION OF PATENT RIGHTS REMOVED.
IT HAS NOT BEEN REVIEWED BY, OR PRODUCED IN COLLABORATION WITH, THE
CREATIVE COMMONS ORGANIZATION.
THE AUTHOR(S) OF THIS DOCUMENT PROVIDE THIS INFORMATION ON AN "AS-IS"
BASIS. THE AUTHOR(S) MAKE NO WARRANTIES REGARDING THE USE OF THIS
DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER, AND DISCLAIM
@JamesTheAwesomeDude
JamesTheAwesomeDude / shell.bat
Last active October 9, 2023 17:28
Open a CMD window directly in a Python venv
@echo off
REM After creating a venv via py -m venv "%USERPROFILE%\.venvs\MYVENVNAMEHERE"
REM Save this as %USERPROFILE%\.venvs\MYVENVNAMEHERE\Scripts\shell.bat
REM This script is OK to call from another folder, such as %USERPROFILE%
@echo on
REM Try "%%ProgramFiles(x86)%%\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat" && start python -m "idlelib"
cmd /k "%~dp0\activate.bat"
@JamesTheAwesomeDude
JamesTheAwesomeDude / teeinto_lockstep.py
Last active January 12, 2024 18:07
tee into multiple "for" loops in parallel without async, threading, or subprocess
import greenlet
def teeinto_lockstep(it, *consumer_callables):
"""tee iterator *it* into multiple non-`async`, non-Generator consumers, in constant memory, without `threading`.
The behavior of each passed iterable is UNDEFINED after its respective consumer exits.
If a consumer function raises an exception, that exception will bubble out of teeinto_lockstep.