Sign in

Trey Hunner

@trey.io
4K followers 216 following 2.2K posts

Python & Django team trainer I help folks sharpen their Python skills with PythonMorsels.com 🐍🍪 I also blog at treyhunner.com & run Whereabouts.Earth PSF Fellow. YIMBY. 95% vegan.

PostsRepliesMedia
Trey Hunner @trey.io · 1h
🗺️ September 30, 2026 5/5 countries in 6/13 guesses 🟢🟢🟢🟡🟢 whereabouts.earth/daily/ #Whereabouts
whereabouts.earth
Whereabouts Daily
How many of today's five countries can you guess?
110
Trey Hunner @trey.io · 15h
Python Tip #272 (of 365): Do something or return something, but not both. 🧵 The list sort method returns None: >>> numbers = [4, 2, 7, 1, 5] >>> x = numbers.sort() >>> print(x) None >>> numbers [1, 2, 4, 5, 7] #Python #DailyPythonTip
120
Trey Hunner @trey.io · 15h
Python Tip #273 (of 365): Avoid needless wrapper functions. 🧵 This lambda does nothing but call len: sorted(words, key=lambda s: len(s)) So pass len itself: sorted(words, key=len) #Python #DailyPythonTip
120
Trey Hunner @trey.io · 20h
“A loop that goes on forever is called an infinite loop.” Read more 👉 pym.dev/while-loops/ #Python
pym.dev
https://pym.dev/while-loops/
010
Trey Hunner @trey.io · 21h
🗺️ September 29, 2026 5/5 countries in 7/13 guesses 🟡🟡🟢🟢🟢 whereabouts.earth/daily/ #Whereabouts
whereabouts.earth
Whereabouts Daily
How many of today's five countries can you guess?
121
Trey Hunner @trey.io · 29/09/2026
Python Tip #271 (of 365): Return, don't print. 🧵 If your function only seems to "work" in the REPL, it may be printing when it should be RETURNING. #Python #DailyPythonTip
140
Trey Hunner @trey.io · 28/09/2026
“It has nothing to do with binary numbers or ones and zeroes.” Read more 👉 pym.dev/binary-search/ #Python
pym.dev
Binary search in Python with bisect
Python's bisect module implements binary search for you. Here's how bisect_left, bisect_right, and insort work, plus recipes for finding the closest match or all values in a range.
030
Trey Hunner @trey.io · 28/09/2026
In early April this year I only knew the location of 2 of these 5 countries. 🗺️ September 28, 2026 5/5 countries in 5/13 guesses 🟢🟢🟢🟢🟢 whereabouts.earth/daily/ #Whereabouts
whereabouts.earth
Whereabouts Daily
How many of today's five countries can you guess?
130
Trey Hunner @trey.io · 28/09/2026
Python Tip #270 (of 365): Move and copy files with pathlib. 🧵 In Python 3.14, Path objects gained move and copy methods, and copy works on whole directories: >>> from pathlib import Path >>> taxes = Path("taxes") >>> taxes.copy("taxes-backup") PosixPath('taxes-backup') #Python #DailyPythonTip
150
Trey Hunner @trey.io · 27/09/2026
“The reproducibility of pseudorandom numbers is useful for automated tests and for debugging.” Read more 👉 pym.dev/random-numbe... #Python
pym.dev
Selecting random values in Python
Python's random module provides utilities for generating pseudorandom numbers. For cryptographically-secure randomness, use the secrets module instead.
130
Trey Hunner @trey.io · 27/09/2026
🗺️ September 27, 2026 5/5 countries in 5/13 guesses 🟢🟢🟢🟢🟢 whereabouts.earth/daily/ #Whereabouts
whereabouts.earth
Whereabouts Daily
How many of today's five countries can you guess?
120
Trey Hunner @trey.io · 26/09/2026
Python Tip #269 (of 365): Walk directory trees with Path.walk. 🧵 The walk method (Python 3.12+) is the pathlib version of os.walk. For each directory in the tree, you get the directory's path, a list of its subdirectory names, and a list of its file names. #Python #DailyPythonTip
110
Trey Hunner @trey.io · 26/09/2026
“When exactly parentheses improve readability is a bit fuzzy and it's definitely subjective.” Read more 👉 pym.dev/unnecessary-... #Python
pym.dev
https://pym.dev/unnecessary-parentheses/
010
Trey Hunner @trey.io · 26/09/2026
🗺️ September 26, 2026 5/5 countries in 8/13 guesses 🟡🟢🟠🟢🟢 whereabouts.earth/daily/ #Whereabouts
whereabouts.earth
Whereabouts Daily
How many of today's five countries can you guess?
120
Trey Hunner @trey.io · 26/09/2026
Python Tip #268 (of 365): Show relative paths with relative_to. 🧵 Absolute paths are noisy: >>> print(path) /home/trey/taxes/archived/2017/invoices >>> taxes PosixPath('/home/trey/taxes') >>> print(path.relative_to(taxes)) archived/2017/invoices #Python #DailyPythonTip
131
Reposted by Trey Hunner
Grace @gracekind.net · 26/09/2026
“Improve model for everyone,” aka “allow my images to be posted on public websites”
“Improve model for everyone” setting in chatgpt
714610
Trey Hunner @trey.io · 25/09/2026
“A queue is often called a FIFO data structure: first in, first out.” Read more 👉 pym.dev/stacks-and-q... #Python
pym.dev
Stacks and queues in Python
Use a Python list for stack operations (last-in, first-out) and a deque from the collections module for queue operations (first-in, first-out).
040
Trey Hunner @trey.io · 25/09/2026
🗺️ September 25, 2026 5/5 countries in 5/13 guesses 🟢🟢🟢🟢🟢 whereabouts.earth/daily/ #Whereabouts
whereabouts.earth
Whereabouts Daily
How many of today's five countries can you guess?
120
Reposted by Trey Hunner
San Diego Python @sandiegopython.org · 24/09/2026
Our monthly meetup is tonight 7pm at @qualcomm.bsky.social in the usual spot. We have a great speaker lineup including @trey.io. See you there or on the livestream. www.meetup.com/pythonsd/eve...
001
Trey Hunner @trey.io · 24/09/2026
“If an object quacks like a duck, it may be used as if it's a duck.” Read more 👉 pym.dev/all-iteratio... #Python
pym.dev
https://pym.dev/all-iteration-is-the-same/
120
Trey Hunner @trey.io · 24/09/2026
Python Tip #267 (of 365): Change file extensions with with_suffix. 🧵 A ".rst" file path that needs to turn into a ".md" file path: >>> from pathlib import Path >>> old_path = Path("uvrs/readme.rst") >>> old_path.with_suffix(".md") PosixPath('uvrs/readme.md') #Python #DailyPythonTip
140
Trey Hunner @trey.io · 24/09/2026
🗺️ September 24, 2026 5/5 countries in 8/13 guesses 🟢🟠🟢🟡🟢 whereabouts.earth/daily/ #Whereabouts
whereabouts.earth
Whereabouts Daily
How many of today's five countries can you guess?
120
Trey Hunner @trey.io · 24/09/2026
Keep in mind that because the loops in a comprehension must be in the same order as equivalent for loops, it does read a little funny. Read more 👉 trey.io/aeo9fg #Python
030
Trey Hunner @trey.io · 23/09/2026
Python Tip #266 (of 365): Reread a file with seek(0). 🧵 Try to .read() a file twice, and you'll get nothing the second time: >>> f = open("my_file.txt") >>> f‍.read() 'This is my file 👋\nThis is line 2\n' >>> f‍.read() '' #Python #DailyPythonTip
130
Trey Hunner @trey.io · 23/09/2026
🗺️ September 23, 2026 5/5 countries in 5/13 guesses 🟢🟢🟢🟢🟢 whereabouts.earth/daily/ #Whereabouts
whereabouts.earth
Whereabouts Daily
How many of today's five countries can you guess?
120
Trey Hunner @trey.io · 23/09/2026
“For true randomness, you can use the secrets module, which generates cryptographically secure random numbers.” Read more 👉 pym.dev/random-numbe... #Python
pym.dev
Selecting random values in Python
Python's random module provides utilities for generating pseudorandom numbers. For cryptographically-secure randomness, use the secrets module instead.
020
Trey Hunner @trey.io · 22/09/2026
Python Tip #265 (of 365): Use next() to get the first line in a file. 🧵 #Python #DailyPythonTip
130
Trey Hunner @trey.io · 22/09/2026
🗺️ September 22, 2026 5/5 countries in 12/13 guesses 🟡🟢🟡🟠🔵 whereabouts.earth/daily/ #Whereabouts
whereabouts.earth
Whereabouts Daily
How many of today's five countries can you guess?
130
Trey Hunner @trey.io · 22/09/2026
“To navigate block-by-block, you can use Ctrl+P to go to the previous block and Ctrl+N to go to the next block.” Read more 👉 pym.dev/repl-features/ #Python
pym.dev
https://pym.dev/repl-features/
020
Trey Hunner @trey.io · 21/09/2026
Python Tip #264 (of 365): Don't use the file readlines method. 🧵 Do this: lines = list(file) Not this: lines = file.readlines() Both give the same list of lines. So why prefer list? #Python #DailyPythonTip
130
Trey Hunner @trey.io · 21/09/2026
🗺️ September 21, 2026 5/5 countries in 7/13 guesses 🟢🟡🟡🟢🟢 whereabouts.earth/daily/ #Whereabouts
whereabouts.earth
Whereabouts Daily
How many of today's five countries can you guess?
110
Trey Hunner @trey.io · 20/09/2026
Python Tip #263 (of 365): Parse messy dates with the third-party dateutil library. 🧵 >>> from dateutil.parser import parse >>> parse("September 20, 2026") datetime.datetime(2026, 9, 20, 0, 0) >>> parse("20 Sep 2026 14:30") datetime.datetime(2026, 9, 20, 14, 30) #Python #DailyPythonTip
110
Trey Hunner @trey.io · 20/09/2026
🗺️ September 20, 2026 5/5 countries in 5/13 guesses 🟢🟢🟢🟢🟢 whereabouts.earth/daily/ #Whereabouts
whereabouts.earth
Whereabouts Daily
How many of today's five countries can you guess?
110
Trey Hunner @trey.io · 19/09/2026
“Pretty much every time I make a temporary file, I need to close the file without actually deleting it so that I can then pass the filename off to other code that actually uses the file.” Read more 👉 pym.dev/temporary-fi... #Python
pym.dev
Creating temporary files in Python
How to create temporary files and directories in Python using the tempfile module's NamedTemporaryFile and TemporaryDirectory.
011
Trey Hunner @trey.io · 19/09/2026
Python Tip #262 (of 365): Parse dates with date.strptime. 🧵 In Python 3.14+, the date class got its own strptime class method: >>> import datetime as dt >>> dt.date.strptime("2026-08-19", "%Y-%m-%d") datetime‍.date(2026, 8, 19) #Python #DailyPythonTip
120
Trey Hunner @trey.io · 19/09/2026
🗺️ September 19, 2026 5/5 countries in 6/13 guesses 🟢🟢🟡🟢🟢 whereabouts.earth/daily/ #Whereabouts
whereabouts.earth
Whereabouts Daily
How many of today's five countries can you guess?
120
Reposted by Trey Hunner
PyBeach @pybeach.bsky.social · 18/09/2026
🐍🏖️ #pybeach2026 Speaker Highlight: Trey Hunner @trey.io "Everything You Don't Need to Know About Classes in Python" 🔗 https:// 2026.pybeach.org/speakers#hunner 📅 October 24th, 2026 🎫 https:// ti.to/pybeach/pybeach2026
An image containing the speaker's portrait, their talk title, and the name, logo, and URL of the conference.
043
Trey Hunner @trey.io · 19/09/2026
“Of course, at some point in your Python coding journey, you will probably find some opportunities to make classes.” Read more 👉 pym.dev/friendly-cla... #Python
pym.dev
https://pym.dev/friendly-classes/
020
Trey Hunner @trey.io · 18/09/2026
Python Tip #261 (of 365): Use Python's zoneinfo module when working with time zones. 🧵 >>> from zoneinfo import ZoneInfo >>> pacific = ZoneInfo("America/Los_Angeles") >>> meeting = datetime(2027, 3, 13, 9, 0, tzinfo=pacific) >>> print(meeting) 2027-03-13 09:00:00-08:00 #Python #DailyPythonTip
120
Trey Hunner @trey.io · 18/09/2026
🗺️ September 18, 2026 5/5 countries in 6/13 guesses 🟢🟢🟢🟡🟢 whereabouts.earth/daily/ #Whereabouts
whereabouts.earth
Whereabouts Daily
How many of today's five countries can you guess?
120
Trey Hunner @trey.io · 18/09/2026
I got 16/17 on a quiz about Python's datetime module. How many can you get? (And yes I still got one of them wrong despite the fact that I made this quiz and have been testing it for days) www.pythonmorsels.com/quiz/datetime/
pythonmorsels.com
How well do you know Python's datetime module?
Test your knowledge of parsing and formatting dates and times, format codes, and date arithmetic. 17 questions, answers revealed as you go. No account needed.
150
Trey Hunner @trey.io · 18/09/2026
“Note that the utilities in Python's random module generate pseudorandom numbers, not true random numbers.” Read more 👉 pym.dev/random-numbe... #Python
pym.dev
Selecting random values in Python
Python's random module provides utilities for generating pseudorandom numbers. For cryptographically-secure randomness, use the secrets module instead.
030
Trey Hunner @trey.io · 17/09/2026
Python Tip #260 (of 365): Bookmark a date format cheat sheet. 🧵 Is "%M" month or minute? Is "%H" 12 or 24 hour time? Which one shows a long month name? And a short one? You COULD memorize all of Python's many date/time format codes, but is that worth it? #Python #DailyPythonTip
130
Trey Hunner @trey.io · 17/09/2026
🗺️ September 17, 2026 5/5 countries in 6/13 guesses 🟢🟢🟢🟢🟡 whereabouts.earth/daily/ #Whereabouts
whereabouts.earth
Whereabouts Daily
How many of today's five countries can you guess?
130
Trey Hunner @trey.io · 17/09/2026
Python Tip #259 (of 365): Use datetime arithmetic and comparisons to check if a date is within a range. 🧵 Sputnik launched before the moon landing: >>> sputnik_launch = datetime(1957, 10, 4) >>> moon_landing = datetime(1969, 7, 20) >>> sputnik_launch < moon_landing True #Python #DailyPythonTip
130
Trey Hunner @trey.io · 16/09/2026
Running a #Whereabouts poll on Mastodon. Feel free to to vote over there or reply here if you'd like to let me know what you do/use. I'd love to know. I want to know, do you: 1. Play Whereabouts Daily 2. Use the main Whereabouts learning mode 3. Do both (This is about whereabouts.earth) Thanks!
mastodon.social
Trey Hunner 🐍 (@treyhunner@mastodon.social)
I suspect there are quite a few folks who use the Whereabouts learning mode but don't play the daily game or who play the daily game but don't use it to learn. Do you use Whereabouts.Earth to learn c...
120
Trey Hunner @trey.io · 16/09/2026
“The syntax for adding a new key-value pair to a dictionary is actually the same as the syntax for updating the value for an existing key.” Read more 👉 pym.dev/using-dictio... #Python
pym.dev
Using dictionaries in Python
Python's dictionaries act as lookup tables which map keys to their values.
150
Trey Hunner @trey.io · 16/09/2026
🗺️ September 16, 2026 5/5 countries in 8/13 guesses 🟢🟢🟢🟢🔵 whereabouts.earth/daily/ #Whereabouts
whereabouts.earth
Whereabouts Daily
How many of today's five countries can you guess?
130
Trey Hunner @trey.io · 16/09/2026
“The last plate that's added to the top of the stack will be the first plate removed from the top of the stack.” Read more 👉 pym.dev/stacks-and-q... #Python
pym.dev
Stacks and queues in Python
Use a Python list for stack operations (last-in, first-out) and a deque from the collections module for queue operations (first-in, first-out).
030
Trey Hunner @trey.io · 15/09/2026
Python Tip #258 (of 365): Format dates with f-strings. 🧵 datetime objects support the same format codes in f-string format specifiers that the strftime method uses: >>> deadline = datetime(2046, 10, 31, 15, 46) >>> f"{deadline:%m/%d/%Y %I:%M %p}" '10/31/2046 03:46 PM' #Python #DailyPythonTip
140