@treyhunner@mastodon.social
@treyhunner@mastodon.social avatar

treyhunner

@treyhunner@mastodon.social

#Python & #Django educator & team trainer

I help folks sharpen their Python skills with https://PythonMorsels.com🐍🍪

#pythonoddity

Also a #humanist #YIMBY who is attempting more ethical eating (#vegetarian, not yet #vegan) and thinks #economics is highly underrated, but I don't post about those topics very often.

he/him

This profile is from a federated server and may be incomplete. Browse more on the original instance.

treyhunner, to django
@treyhunner@mastodon.social avatar

A controversial topic:

class-based views (CBVs) or function-based views (FBVs)? 🤔

I started with function-based views, but after a few years of using Django I pretty much moved fully to class-based views. Both have pros & cons (and vocal advocates).

The Django docs introduce FBVs first and then discusses CBVs as well as Django's built-in "generic class-based views" (GCBVs).

Django-REST-Framework also makes heavy use of CBVs.

But class-based views CAN be confusing.

treyhunner, to python
@treyhunner@mastodon.social avatar

I wish 's string replace method allowed specifying "count" as a keyword argument.

>>> text = "Hello? Python?"
>>> text.replace("?", "!", count=1)

I find that more explicit than this:

>>> text.replace("?", "!", 1)
'Hello! Python?'

But the replace only allows positional arguments 🐍😢

treyhunner, to python
@treyhunner@mastodon.social avatar

What feature would you have trouble explaining to a new programmer?

(If you think you CAN explain that feature, feel free to reply to replies with your own new-programmer-oriented explanation)

treyhunner, to python
@treyhunner@mastodon.social avatar

What's a #Python feature you wish existed but doesn't? 🤔

It can even be something others would think is absurd. 🛸

Dream big! 💭

treyhunner, to random
@treyhunner@mastodon.social avatar

I'm an HTML First convert.

I've found a lot of value in Alpine.js and HTMX for front end interactivity and recently I've been really enjoying the ease of maintaining Tailwind styles.

I've also noticed that I'm able to easily throw a tiny bit of custom HTML with Alpine attributes in my markdown-powered CMS-ish system and it just works. No need to make something custom for small one-off situations.

https://html-first.com

treyhunner, to python
@treyhunner@mastodon.social avatar

Self-taught programmers: what made the idea of "time complexity" really click for you? 🧮

Was there a video, blog post, book chapter, etc. that really acted as an ah-ha moment? 💡

treyhunner, to python
@treyhunner@mastodon.social avatar

My students often ask me whether they can see a list of every dunder method that has. I've been meaning to compile a categorized list for years.

I finally did it.

Here's every dunder method in Python... all 115 of them! 🤯

https://pym.dev/every-dunder-method/

treyhunner, (edited ) to django
@treyhunner@mastodon.social avatar

What idiom do you wish you'd known sooner?

Or, what's a Django feature that seems underused?

treyhunner, to random
@treyhunner@mastodon.social avatar

friends who aren't attending the auction: come play Cabo with me tonight. I'd to teach you!

After the lightning talks, I plan to quickly eat tacos at Condados and then hang out in the Westin lobby and play Cabo. I won't be up too late though. See you tonight!

treyhunner, to python
@treyhunner@mastodon.social avatar

If you were to mentor a beginner coder, what would be your first lesson?

treyhunner, to python
@treyhunner@mastodon.social avatar

What are your favorite one liners?

treyhunner, (edited ) to random
@treyhunner@mastodon.social avatar

I've just updated my ~/.config/pip/pip.conf & my dotfiles repo to disallow pip installing outside virtual environments! 🎉

TIL 2 things about 's pip:

  1. pip has a config file. If I ever knew this, I'd forgotten.

  2. pip has an option that stops it from working outside of a virtual environment!

This is great for me because I use pipx and NEVER do global pip installs.

Info: https://unix.stackexchange.com/questions/492041/is-there-a-way-to-disable-pip-outside-of-a-virtual-environment

Thanks to @hynek's excellent uv video for this semi-related TIL 💜 https://youtu.be/_FdjW47Au30?si=5iRmMuWMdewRubNt

treyhunner, (edited ) to random
@treyhunner@mastodon.social avatar

It may be obvious that these are classes:

• tuple
• list
• dict
• set
• str
• int
• float
• bool

But did you know these "functions" are also classes?

• enumerate
• range
• zip
• reversed
• map
• filter
• super
• type
• property
• classmethod

Classes are callable, just as functions are: Python has no "new" keyword!

Callables may be classes or functions. And it's acceptable to refer to function-like callables as "functions", even if they're actually classes.

https://www.pythonmorsels.com/callables/

treyhunner, to python
@treyhunner@mastodon.social avatar

Ever seen a variable with two underscores on either side (like_this)?

These variables define a contract between Python devs and the interpreter.

Officially called special attributes, they're often called "dunder variables" colloquially.

Each dunder variable is typically used for one of two purposes:

  1. Conveing information (metadata) to Python devs
  2. Empowering Python devs to convey information to the Python interpreter

name is an example of 1.

repr is an example of 2.

treyhunner, (edited ) to django
@treyhunner@mastodon.social avatar

I just watched @pythonbynight's #DjancoCon talk on the current state of #HTMX in #Django and now I'm turn between trying out django-render-block, forge-htmx, and @carlton's django-template-partials.

https://github.com/clokep/django-render-block
https://github.com/forgepackages/forge-htmx
https://github.com/carltongibson/django-template-partials/

Anyone written a blog post or made a repository attempting to write the same HTMX-powered view(s) using all 3 of those packages? I'd love to compare and contrast them more directly.

Here's the talk:

https://www.youtube.com/watch?v=J0xES5wGjik

treyhunner, to python
@treyhunner@mastodon.social avatar

What meetups are still either meeting remotely or hosting remote talks?

treyhunner, (edited ) to python
@treyhunner@mastodon.social avatar

Python 3.13 beta 1 just landed.

My favorite new feature is the new REPL.

Honestly, this may be my favorite feature since f-strings.

My write up on the differences between old and new REPLs:
https://treyhunner.com/2024/05/my-favorite-python-3-dot-13-feature/

treyhunner, to random
@treyhunner@mastodon.social avatar

Let's start , by talking Django learning 🏫

I often recommend the Django Girls tutorial followed by the official Django documentation.

Why Django Girls first? https://tutorial.djangogirls.org

The official Django tutorial is great, but it explains some features that aren't essential on your first day with Django. https://djangoproject.com/start/

The Django docs also assume Python knowledge & a general understanding of web development. The Django Girls tutorial makes few assumptions.

treyhunner, to python
@treyhunner@mastodon.social avatar

What's your favorite one-liner?

treyhunner, to python
@treyhunner@mastodon.social avatar

What's a bit of syntax that is technically unnecessary and yet you prefer to leave it in for readability's sake?

What about a bit of unnecessary syntax that you always prefer to remove?

treyhunner, to random
@treyhunner@mastodon.social avatar

I started a project for searching Unicode characters from your terminal, similar to unicode.party and @sethmlarson's utf8.xyz (but as a TUI instead of a web app).

I initially thought I'd call the project "utf", but that has a name conflict on PyPI (due to similarity to utf7, utf9, etc. likely).

What would you suggest I name this project?

https://github.com/treyhunner/utf/issues/17

treyhunner, to random
@treyhunner@mastodon.social avatar

The Python Morsels REPL (https://pym.dev/repl) now properly supports using Ctrl+D to stop standard input! A user recently tested out code that relied on sys.stdin.read() and ended up stuck without a way to send EOF.

Thanks @notsolonecoder for a 1.5 year old unmerged CPython PR that contained the keys to fixing this issue!

https://github.com/python/cpython/pull/97665

treyhunner, to python
@treyhunner@mastodon.social avatar

Flattening a list-of-lists (or iterable-of-iterables) in ?

Take your pick.

1️⃣ nested loops

names = []
for g in groups:
for n in g:
names.append(n)

2️⃣ extend in a loop

names = []
for g in groups:
names.extend(g)

or +=:
names += g

3️⃣ comprehension or generator:

names = [
n
for g in groups
for n in g
]

4️⃣ chain.from_iterable

from itertools import chain
names = chain.from_iterable(groups)

More details plus one "bad" way: https://pym.dev/flatten-a-list-in-python/

treyhunner, to python
@treyhunner@mastodon.social avatar

Knowing when to use a set in can be VERY handy sometimes.

no_neighbors = [
n
for n in nums
if n-1 not in nums
and n+1 not in nums
]

If "nums" is a list of 500,000 numbers, this takes 54 minutes.

Convert "nums" to a set and this taken less than 1 second.

Why?

It's all about that "in" operator.

Checking for containment in a list requires looping over the whole list.

Checking for containment in a set uses the magic of hashing to return an answer without looping.

treyhunner, to random
@treyhunner@mastodon.social avatar

Not one project is holding their sprint on the rooftop (4th floor). I feel like that's a missed opportunity here. #PyConUS

Tables outside on the fourth floor roof with a yellow bridge in the background

  • All
  • Subscribed
  • Moderated
  • Favorites
  • megavids
  • kavyap
  • DreamBathrooms
  • thenastyranch
  • magazineikmin
  • tacticalgear
  • khanakhh
  • Youngstown
  • mdbf
  • slotface
  • rosin
  • everett
  • ngwrru68w68
  • Durango
  • JUstTest
  • InstantRegret
  • cubers
  • GTA5RPClips
  • cisconetworking
  • ethstaker
  • osvaldo12
  • modclub
  • normalnudes
  • provamag3
  • tester
  • anitta
  • Leos
  • lostlight
  • All magazines