#stupidpythontricks — Public Fediverse posts
Live and recent posts from across the Fediverse tagged #stupidpythontricks, aggregated by home.social.
-
I'm gonna pay for it when I have to be up in a few hours but the dopamine hit from fixing composable, nested, self-referential inventories is going to sing me to sleep.
-
#Python #StupidPythonTricks
```python
class X:
def __init__(self): self.n = 0
def __getattr__(self, key):
self.n += len(key)
return self
def __str__(self):
return f'{self.n} characters'print(X().moby.fubar) # 9 characters
```
-
CW: stupid python tricks
So my audio streamer was having an issue where on track boundaries players would occasionally stop decoding the stream. icecast didn't log any errors but about 40% of the time when the stream switched to a new track, audio would just die.
The fix, apparently, was to buffer reads on the input stream with a buffer size 2x the size of the chunks written to the output stream.
https://github.com/evilchili/dnd-music-console/blob/main/croaker/streamer.py#L98
-
muahaha. Adding the music controller to my DM Shell was a piece of cake, just a few lines and a bunch of keybindings.
https://github.com/evilchili/dmsh/blob/main/dmsh/croaker.py
Now I can just hit m followed by a number to switch to a new playlist!
-
CW: ttrpgs:: DM's tools, stupid python tricks
Last campaign I built a set of tools to make it easier to DM my homebrew world with the level of detail and specificity I prefer. They worked, but they were increasingly hacky as I added things, to the point where I couldn't remember how to invoke the particular tool I needed in the moment.
So this time around I invested significant effort in building an interactive shell that I run while I'm running the game, which gives me hot keys for things like generating NPCs, loot tables, wild magic tables, and so on. Today I refactored out my calendaring code into its own repo, and wired it up to the DM Shell.
https://github.com/evilchili/dnd-calendar
Now I can hit F4 to get the current date, type 'date inc' to move the campaign date forward one day, 'date set' to move the campaign to a specific date, 'date season' to print a calendar, and so on. No more getting lost in my DM scribble notes!
the ux bindings: https://github.com/evilchili/dnd/blob/main/deadsands/site_tools/shell/interactive_shell.py#L76
-
CW: Groove on Demand
Only did a little bit tonight but was pleased that the interactive shell turns out to be very testable with a single mock. Still haven't settled on a final UX but whenever I do it'll just be a matter of tweaking inputs and expected outputs.
#StupidPythonTricks #GrooveOnDemand
https://github.com/evilchili/grooveondemand/blob/main/test/test_shell.py
-
Made some entertaining progress on #GrooveOnDemand -- now have multithreaded autocompletion working in an interactive shell, so you can quickly build playlists with a few keystrokes.
-
Putting the pieces together...
The #DeadSands 5e campaign prominently features a vast desert, broken up into individual regions, in which magic has gone awry. Simply surviving overland travel in the desert is a systemic implementation of one of the campaign's main themes, so I wanted to be able to crank out unique and interesting regions to explore without a whole lot of work or lead time.
Since I'm building the static website with pelican, I wrote a python tool to generate random roll tables. The site generator now uses said tool to automatically populate new region articles with a bunch of stuff.
Here's an example:
-
Part of my plan for the new #DnD campaign involves weird weather effects that are unique to individual locations. I want to be able to generate this stuff quickly but also make it persistent, so that over time the players can predict weather patterns and anyway I wrote a tool to generate roll tables of arbitrary size and populate it with a random distribution of selections, weighted by frequency of occurrence.