Trending

Latest Posts by Trey Hunner

concurrent.futures now includes an InterpreterPoolExecutor, which spawns a new Python interpreter in a separate thread, but in the same process

Read more 👉 https://trey.io/flfwr9

#Python

17 hours ago 4 0 0 0

Python Tip #97 (of 365):

When you need to read/write a whole file, use a pathlib.Path helper method.

Instead of:
with open(path) as f:
text = f.‍read()

Do:
text = path.‍read_text()

And instead of:
with open(path, "w") as f:
f.write(text)

Do:
path.write_text(text)

#Python #DailyPythonTip

18 hours ago 5 0 0 0
Preview
Python's pathlib module Python's pathlib module is the tool to use for working with file paths. See pathlib quick reference tables and examples.

If you need a os/os.path/glob to pathlib conversion cheat sheet, see pym.dev/pathlib-modu...

If you just want help adopting pathlib in general, use this cheat sheet instead:
pym.dev/pathlib-modu...

This week's tips are all about pathlib patterns & anti-patterns.

1 day ago 4 0 0 0
pathlib: Why and How to Use It Hello! This page includes resources related to my PyBeach 2025 talk, pathlib.Path: Why and how to use it.

Not convinced that pathlib is better than os.path and friends?

Well, unlike with strings:

1. It's hard to get yourself into trouble with pathlib.Path
2. When I see a pathlib.Path object, I know we're representing a path

I have a whole talk that might help convince you: trey.io/pathlib-talk

1 day ago 3 0 1 0

Python Tip #96 (of 365):

When manipulating file paths or asking questions about them, use pathlib 🧵

"Stringly typed code" uses a string to represent a value when a better type exists.

For representing file paths, pathlib.Path objects are that better-than-a-string type.

#Python #DailyPythonTip

1 day ago 5 0 1 0
Preview
Using a ~/.pdbrc file to customize the Python Debugger Did you know that you can customize the Python debugger (PDB) by creating custom aliases within a .pdbrc file in your home directory or Python’ …

Did you know that you can add aliases to PDB with a ~/.pdbrc file?

I made one over the weekend with some very helpful aliases.

I can now use these to inspect an object (note that "dir" and "vars" are like the dir() & vars() functions but better):

attrs x
dir x
vars x

Also these:

src func
loc

1 day ago 1 0 0 0

Just got my ticket to #PyConUS. I’m excited.

Last year was my first PyConUS. I hardly went to any talks; I mostly spent my time in the hallway. For me, a conference is about connecting with people. Being there in person is pure gold.

2 days ago 7 2 2 0

All escape sequences start with a backslash (\) in Python.

Read more 👉 https://trey.io/ca0qyn

#Python

2 days ago 1 0 0 0
Looking through one of the four windows of the Orion spacecraft, a tiny crescent Earth is illuminated against the blackness of space and grows smaller as the crew journeys closer to the Moon. Part of the window edge is visible; the rest is darkness. [Alt-text slightly revised from ESA/NASA version]

Looking through one of the four windows of the Orion spacecraft, a tiny crescent Earth is illuminated against the blackness of space and grows smaller as the crew journeys closer to the Moon. Part of the window edge is visible; the rest is darkness. [Alt-text slightly revised from ESA/NASA version]

We are so, so small. #Artemis

2 days ago 9098 1666 128 160
Advertisement
Preview
’Pale Blue Dot’ Revisited - NASA For the 30th anniversary of one of the most iconic views from the Voyager mission, NASA’s Jet Propulsion Laboratory in Pasadena, California, is publishing a

I use NASA's pale blue dot revisited as the background on all my devices to help remind me of this frequently.

The scale of our universe and of time is so much bigger than we can comfortably fathom.

2 days ago 6 1 0 0
Preview
re — Regular expression operations Source code: Lib/re/ This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings ( str) as well as 8-...

Update: apparently Python 3.15 is getting re.prefixmatch as an alias for re.match!

2 days ago 4 1 0 0
https://pym.dev/friendly-classes/

“Even if you don't use them directly, dataclasses are a good measuring stick for your class-based code.”

Read more 👉 pym.dev/friendly-cla...

#Python

2 days ago 3 0 0 0

re.match() is a weird middle ground between re.‍search() and re.fullmatch().

re.fullmatch() didn't used to exist but re.match() did... which is why fullmatch isn't called "match". This is all confusing due an accident of history.

Personally, I usually just stick with re.‍search().

2 days ago 3 0 1 0

Using re.fullmatch() is the same as using re.‍search() with ^ and $ used to anchor to the beginning and end of the string.

In other words, these are equivalent:

match = re.‍search(r"^\w+$", string)
match = re.fullmatch(r"\w+", string)

2 days ago 1 0 1 0

There are 2 main things we use regex matching for.

1. Finding a regex within a string
2. Matching a regex against a whole string

re.match() does neither of those.

To find a match WITHIN a string, you can use re.‍search().

To match an entire string against a regex, you can use re.fullmatch().

2 days ago 1 0 2 0

Python Tip #95 (of 365):

Don't use re.match(): it's confusing 🧵

I'm not sure I've ever seen re.match() used when it wasn't being used by mistake.

If you think you want re.match(), you probably want either re.‍search() or re.fullmatch() instead.

#Python #DailyPythonTip

2 days ago 8 1 1 0
Advertisement

This is something that I've wanted for a very long time. Very excited @savannah.dev made this!

Go give @coredispatch.xyz a follow to keep up with Python!

3 days ago 9 3 0 0

See you there!

3 days ago 0 0 0 0

See you there friend!

You going to the WebAssembly summit? I'd love to pick your brain about Python-in-the-browser stuff at some point.

3 days ago 2 0 1 0
Trey Hunner 🐍 (@treyhunner@mastodon.social) I'm getting an early start on my personal "list of folks on this social network who will be at @pycon@fosstodon.org this year". If you'll be at #PyConUS and would like to run into each other, 👋 reply...

I'm getting an early start on my personal "list of folks on bsky who will be at @pycon.us this year".

If you'll be at #PyConUS and want to run into each other, 👋 reply here (or like or something).

If you'll be on Mastodon during PyCon, respond there also/instead: mastodon.social/@treyhunner/...

3 days ago 7 1 4 0
https://pym.dev/unnecessary-parentheses/

“Python's if statements don't use parentheses.”

Read more 👉 pym.dev/unnecessary-...

#Python

3 days ago 4 0 0 0
Preview
Verbose mode to explain a non-capturing group. - Python Pastebin - Python Morsels Verbose mode to explain a non-capturing group.

That inner group isn't for capturing... it's just for grouping the alternatives with that | operator.

To make it non-capturing we can put ?: just after the opening paren:

>>> re.findall(r"`((?:\\`|[^`])+)`", text)
['[', ']', '\\`']

Find that hard to read?

I agree.

Remember re.VERBOSE (tip 92)!

3 days ago 3 0 0 0

We ended up with a list of 2-item tuples.

What's happening is that the re.findall() function only returns strings when there's either no capture group or 1 capture group... but when there are 2 or more capture groups, it returns a tuple of those group matches.

We need a non-capturing group.

3 days ago 1 0 1 0

We could use a group with an alternative, looking for a \ followed by a backtick OR any non-backtick character: (\\`|[^`])

We could quantify this with + to match it one or more times.

But this doesn't work:

>>> re.findall(r"`((\\`|[^`])+?)`", text)
[('(', '('), (')', ')'), ('\\`', '\\`')]

3 days ago 2 0 1 0
Advertisement

Python Tip #94 (of 365):

Improve your use of re.findall() with non-capturing groups 🧵

We're modifying our backtick searching regex (tip 93) to work with escaped backticks within backticks, to look for inline code like this:

>>> text = "`(`, `)`, and `\\`` are special."

#Python #DailyPythonTip

3 days ago 3 0 1 0
Preview
How to have a great first PyCon (updated for 2025) Update (2025): I’ve updated the links for PyCon US 2025. Update (2024): I’ve updated the links for PyCon US 2024. Also if this isn’ …

Here's a bunch of tips I wish I'd learned before my first one: trey.io/first-pycon

If you can, I'd recommend attending the newcomers orientation on Thursday night.

See you there!

3 days ago 1 0 0 0

If you're new to your Python journey, I recommend this.

PyCon US is big (~2,000 attendees) which might feel intimidating but it's very likely the friendliest tech conference of its size.

If you attend, come to the newcomers orientation on Thursday if you can.

See you there!

3 days ago 4 0 0 0
Preview
PyCon US 2026 PyCon US 2026

PyCon US 2026 is May 13th to 19th in Long Beach, California. If you've been meaning to learn to program, I'm doing a 3-hour tutorial (additional registration required) for absolute beginners who have never coded before. I hope to see you there! us.pycon.org/2026/

1 week ago 11 3 1 1

I did not realize I could purchase a PyLadies shirt through my registration ticket this year until I saw this post. It may not have been an option when I signed up or I may have missed it, but it's there now!

Check the dashboard page and add your shirt(s)! 👕👕

us.pycon.org/2026/account...

4 days ago 2 1 0 0

Note that you COULD argue that the above regex should actually match non-backticks:

>>> re.findall(r"`([^`]*)`", text)
['any()', 'all()']

I can't argue with that.

Reaching for a non-greedy quantifier IS often a sign that there might be a more accurate way to write your regex... but not always.

4 days ago 2 0 0 0