Skip to content

Instantly share code, notes, and snippets.

@jsbueno
jsbueno / raise_as_expression.py
Created September 13, 2024 12:14
Python, raise exception as an expression!
# Usually an exception can be raised with the `raise` statement
#
# however, occasionally it can be useful to be able to do so as part
# of an expression (even if the occasion is code golphing).
# A workaround for that is achievable through a generator's
# `.throw` method:
(_ for _ in ()).throw(Exception())
@jsbueno
jsbueno / sync_async.py
Created June 14, 2024 04:45
POC: wrapper for a function that can either run sychrnously or be awaited in an asyncio context:
# here, the function that can work "either way" is the nested "payload".
# The external ^morphix_in_thread" can be a decorator, and "payload" the decorated function
def morphix_in_thread():
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = None
def payload():
@jsbueno
jsbueno / pyproject.toml
Created January 23, 2024 15:33
Minimal pyproject.toml to use with setuptools and having a working, locally installable, package
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "mypackage"
version = "1.0"
[tool.setuptools]
packages = ["mypackage"]
@jsbueno
jsbueno / build.sh
Created November 22, 2023 16:35
Shell command sequence to compile and install mypaint from GIT, as of november 2023, with Python 3.11
# this is not a script - more like a curated command sequence from my history
# after succesfully installing mypaint.
export $PREFIX=/home/<user>/mypaint-git
sudo dnf install swig # fedora/redhat specific. One also will need Python3-dev and other Linux packages
git clone https://github.com/mypaint/mypaint.git
cd mypaint
~/.pyenv/versions/3.11.4/bin/python -m venv env311 # or use other cPython interpreter of preference
#original code written as an answer to https://stackoverflow.com/questions/75915644/call-decorator-only-once-in-a-nested-class-function/75915738#75915738
from functools import wraps
from threading import RLock
import contextvars
RUNONCE = contextvars.ContextVar("RUNONCE", default=None)
class RunOnceDecorator:
@jsbueno
jsbueno / README.MD
Last active October 1, 2024 03:08
"bicycle repair man"

"Bicycle Repair Man" script showing a "zope-esque" prototype in less than 200 LoC using multiple inheritance and mixins.

This prototype was presented in Python Brasil 2011 in Sao Paulo, explaining at once a bit about what Zope does (did), and showng the capabilities of multiple inheritance.

(and likely not touched since).

The video for this lecture is here: https://www.youtube.com/watch?v=SozHV-hR0Ug&amp;t=1s

@jsbueno
jsbueno / lgm_mazinator.py
Created March 11, 2023 06:28
Python + Pygame maze animation program - using generators as co-routines before it was trendy. (written in 2010, at LGM Brussels)
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: Jo?o S. O. Bueno
# Copyright: Jo?o 2010 -
# Created at LibreGraphicsMeeting 2010, Brussels
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
@jsbueno
jsbueno / gist:e4378521ead8f9dbb40565fb5cacd0b9
Created January 6, 2023 15:59
Snippets for advanced generator research I used when figuring out an answer for https://stackoverflow.com/questions/74922314/yield-from-vs-yield-in-for-loop
class VerboseItBase:
def __new__(cls, gen, method):
if method == "from":
cls = VerboseItFrom
elif method == "explicit":
cls = VerboseItExplicit
return super().__new__(cls)
def __init__(self, gen, method):
self.gen = gen
self.method = method
@jsbueno
jsbueno / day15_2022.py
Created December 16, 2022 18:06
advent_!5_2022
"""
SPOILER ALERT
Puzzle at: https://adventofcode.com/2022/day/15
Part 2 could be brute-forced with the use of "part1" in less
than 10 minutes, I guess.
I opted for a rectangle-splitting approach, and checking if
@jsbueno
jsbueno / advent_14_2022.py
Created December 14, 2022 17:47
Advent of code 2022 - day 14
"""
SPOILER ALERT!
Problem at: https://adventofcode.com/2022/day/14
I guess, no special comments this time.
Just that terminedia.V2 is truly great!
"""
import terminedia as TM