Trey Hunner
@trey.io
2.9K followers 180 following 550 posts
Python & Django team trainer I help folks sharpen their Python skills with https://PythonMorsels.com 🐍🍪 YIMBY. 95% vegan.
Posts Media Videos Starter Packs
trey.io
If you've seen the various string strip methods, you might be wondering, how are removeprefix and removesuffix any different from lstrip and rstrip? Read more �‘‰ https://trey.io/dley4p #Python
trey.io
uv supports an exclude-newer feature as a sort of "pin to versions available as of a specific date" feature!

I'm wondering whether I should add support for that OR simply enable it by default. 🤔

If you have thoughts, I'd love to hear them: github.com/treyhunner/u...
Consider pinning requirements to the current timestamp · Issue #5 · treyhunner/uvrs
uv supports an exclude-newer field in a tool.uv section to improve reproducibility of scripts. I didn't know about this and know I'm considering adding it to all of my scripts as a sort of very sim...
github.com
trey.io
Yes. Although "init" adds a shebang line that uv doesn't add and also sets an executable bit on the file.
trey.io
Tomorrow's weekly Python tip email will include em dashes.

Homegrown em dashes.
trey.io
Have a #Python script that needs a third-party library to run?

uv can help! And I made a small wrapper to makes things even easier.

Install it:
uv tool install uvrs

And add a shebang + dependency:
uvrs fix ./name.py
uvrs add ./name.py requests

Then you can run it, no venv-worry needed:
./name.py
GitHub - treyhunner/uvrs: Create and run uv scripts with POSIX standardized shebang line
Create and run uv scripts with POSIX standardized shebang line - treyhunner/uvrs
github.com
trey.io
Neat little Python library.

If you have uv installed and want try out any of the demos:

uv run --with buttonpad python -m buttonpad
alsweigart.bsky.social
I've finished a presentable version of ButtonPad, but simple GUI framework for Python desktop apps. Built on tkinter so it has no major dependencies, it lets you make a grid of buttons/labels/images/text boxes (like a streamdeck or drum machine). pypi.org/project/Butt...
trey.io
So when writing a file using Python's csv module, you're expected to always specify newline="" when opening the file you're writing to, to make sure that you don't accidentally double up your carriage returns on Windows. Read more �‘‰ https://trey.io/ca0qyn #Python
trey.io
I've been happily using Kagi for over a year.

I used Ecosia before it and Duck Duck Go for years before that.

If you're looking for a search engine where your eyes aren't the product, try Kagi out.

This is not an ad.
trey.io
It became much more possible when 3.13 was released last year.

I've really enjoyed being able to tweak things to better fit my REPL usage needs.
trey.io
Last week I wrote about how I customize my #Python REPL to add additional keyboard shortcuts.

It's hacky, but I've been really satisfied with the result.
Handy Python REPL Modifications
I find myself in the Python REPL a lot. I open up the REPL to play with an idea, to use Python as a calculator or quick and dirty text parsing tool, …
treyhunner.com
Reposted by Trey Hunner
hugovk.dev
Some stats from core dev @hauntsaninja.bsky.social showing Python 3.14 had more usable packages on day 1 than other recent releases.

New releases of #cibuildwheel adding 3.14 as optional and then default also helped.

discuss.python.org/t/stats-on-p...

#Python #Python314 #wheels #stats #PyPI
Python adoption chart showing number of packages definitely supporting 3.11 to 3.14 against time since final release, starting at -400, going to +1000 days, where available. All curves follow a similar trend, but newer ones have more available earlier. The trend is fairly flat at -400, but around -300 start picking up, and at day zero all increase a lot. Similar to the previous chart, but Python adoption amongst packages with version specific wheels. Here, the new versions have much greater adoption than earlier ones.
trey.io
Same goes for:

• "None" in an explicit "return None"
• "start=" in enumerate(..., start=1)
• "mode=" in open(... mode="wt")
• "else" in an if-else that returns, assuming it's clearer that way (pym.dev/unnecessary-...)

Many bits of #Python's syntax are "technically unnecessary" but useful.
Unnecessary else statements
When your function ends in an else block with a return statement in it, should you remove that else?
pym.dev
trey.io
Compare these 2:

for item in counts.keys(): ...
for item in counts: ...

The first one is more explicit and more readable. Yes, the keys method call is technically redundant, but it's clearer, especially to new Pythonistas.

This #Python tip (gripe?) brought to you by an overeager linter rule.
trey.io
Also, if you've ever wished that Python's unittest module included nicely colored output like pytest, it does now. Read more �‘‰ https://trey.io/flfwr9 #Python
trey.io
trey.io
I made a uvrs tool as a wrapper around uv's "init --script", "run --script", "add --script", and "remove --script".

It adds a suitable shebang line and sets an executable bit automatically. There are open issues in uv noting why this isn't the default behavior yet. Hopefully it will be eventually!
GitHub - treyhunner/uvrs: Create and run uv scripts with POSIX standardized shebang line
Create and run uv scripts with POSIX standardized shebang line - treyhunner/uvrs
github.com
trey.io
In this new blog post, I explain why my PYTHONSTARTUP file has grown into a PyPI package.

Let's use sys.append on REPL startup to access a wrapper around an undocumented/private #Python library which monkey patches Python to add custom keyboard shortcuts and customize our syntax highlighting.
Handy Python REPL Modifications
I find myself in the Python REPL a lot. I open up the REPL to play with an idea, to use Python as a calculator or quick and dirty text parsing tool, …
treyhunner.com
trey.io
I'm always in the REPL, so the syntax highlighting was worth it on its own for me.

I treated as an infomercial for Python 3.14, since everyone wished they had the same colored syntax that my REPL had. 🌈
trey.io
For more on Python's pathlib module, see my recent talk from #PyBeach 2025: trey.io/PyBeach2025

And also some pathlib cheat sheet-style tables in this article: pym.dev/pathlib-module
pathlib: Why and How to Use It
trey.io
trey.io
Do it!

I've been using it to run my courses for months now. Only hit a weird bug one time (and it had already been fixed by the time I hit it).
trey.io
One of my favorite features in Python 3.14 is that pathlib.Path objects now have copy and move methods. Read the full article: Python 3.14's best new features �¸ https://trey.io/flfwr9 #Python
trey.io
trey.io
Due to the laziness of reversed(), if we were to pass it lots of items (say 100,000 items) we won't end up copying the iterable that we gave it. Read more �‘‰ https://trey.io/ur7l4g #Python
trey.io
trey.io
Not just on newer versions, but all supported versions since Python 2.7 and 3.5 were end-of-life'd in 2020.

If f-strings are valid Python syntax, dictionaries are ordered.
trey.io
I guess it's October because this office hour was scary... 👻

We discussed:

• Infinitely recursive data structures 🧟
• __new__ vs. metaclass's __call__ vs. class decorators vs. function with __mro_entries__ 👹
• ways to create a context manager 🔮
• pth files 🧌
• modifying sys.path in PYTHONSTARTUP 🦹🏻
trey.io
My #Python office hours start in less than 1 hour.

It's casual and you can participate as much or as little as you like.

Come as you are and hopefully you'll learn something by the end.
Python Morsels Office Hours
Join Trey for 13 weekly office hour sessions. Free for everyone!
pym.dev