@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 python
@treyhunner@mastodon.social avatar

What's been your favorite learning resource so far?

Whether book, video course, or something else.

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

In Python we tend to use ALL_CAPS for constant variables.

Keep in mind that this is just a convention though. Python doesn't actually have constants.

Python DOES have immutable objects, but those are different from constants.

Remember, has 2 types of change: mutations change objects and assignments change variables. A true constant variable (not Python's) cannot be re-assigned while an immutable object cannot be mutated.

https://m.youtube.com/watch?v=0yrlE_XCMEE&feature=youtu.be

treyhunner, to python
@treyhunner@mastodon.social avatar

Need to concatenate a list of strings into a single string in ?

words = ["Python", "is", "fun"]

Don't use += in a "for" loop.

Use the string join method!

>>> " ".join(words)
'Python is fun'

Note that 's join method is backward from what you'd use in JavaScript.

JavaScript:

> list_of_strings.join(" ")

Python:

>>> " ".join(list_of_strings)

The reason? Duck typing!

The string join method doesn't just work on lists: it works on ANY iterable.

https://pym.dev/duck-typing/

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 django
@treyhunner@mastodon.social avatar

My Django students and clients often ask questions that make me realize I have Django opinion that might be of interest to someone else (a fact that's challenging to recognize in isolation)!

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

treyhunner, to python
@treyhunner@mastodon.social avatar

My usual use for Python's sets: de-duplicating collections of stuff (when I don't care about the order).

But 's sets are quite a bit more than just de-duplicators.

You can also use set arithmetic to ask questions of sets.

Given two sets of strings:

>>> a = {"blue", "green", "purple"}
>>> b = {"purple", "white", "blue"}

You can union them to see all their values:

>>> a | b
{'green', 'blue', 'white', 'purple'}

Or intersect them to see the common values:

>>> a & b
{'purple', 'blue'}

treyhunner, to python
@treyhunner@mastodon.social avatar

How do you stay up-to-date with the latest developments in the community?

treyhunner, to random
@treyhunner@mastodon.social avatar

You know you've been adulting too much when you realize you have strong opinions about the best and worst pieces of hold music. 😬☎️

treyhunner, to python
@treyhunner@mastodon.social avatar

The ruff linter can now replace all my favorite linting & auto-formatting tools.

For many months, I've used these commands:

• ruff check . # lint
• ruff check --fix . # auto-fix

Now I also use:

• ruff format . # A Black equivalent

Instead of flake8 or pylint, I either use the ruff defaults (similar to flake8) or I enable the "PL" rules for pylint.

The --fix argument will auto-fix many linting errors too (similar to autopep8, autoflake, isort, pyupgrade, etc.).

treyhunner, to random
@treyhunner@mastodon.social avatar

When my models end up with LOTS of methods & properties, I ponder a refactor.

I've considered:

  1. Move features to functions in their own module
  2. Make another class & use multiple inheritance
  3. Composition with a cached property

I often like option 3.

Let's briefly take a look at all 3 options.

treyhunner, to random
@treyhunner@mastodon.social avatar

@bmispelon I just ran across a slidedeck of yours from 2013!

I searched Kagi for "python -m pyclbr" and there were only about 6 results. Yours was one of them.

https://speakerdeck.com/bmispelon/stdlib-safari-exotic-animal-edition?slide=91

treyhunner, to random
@treyhunner@mastodon.social avatar

Guess which Norwegian, needleworking, library-indexing, BeeWare contributing folks are now on Mastodon in preparation for a future Python conference. 🇳🇴🧵📚🐝

@yngvem & @marieroald!

#PyConUS

treyhunner, to random
@treyhunner@mastodon.social avatar

If you're not using a "service layer" where should you business logic live in Django projects?

Views, templates, models, forms, serializers, somewhere else?

Well... it depends on the logic!

Business logic doesn't live in just one place in projects.

treyhunner, to random
@treyhunner@mastodon.social avatar

What tips do you have for folks thinking of trying out Mastodon during this year?

3 of my tips:

  1. Follow the hashtag (yes you can follow a hashtag just like following a profile!)
  2. If you post and realize you forgot the hashtag, don't delete your post. Just edit it!
  3. Consider following the hashtag also. Many folks use it without realizing that is the official one.

What tips do you have?

treyhunner, to random
@treyhunner@mastodon.social avatar

What are some of your favorite @pycon tips? 💡

Reply and tag . 💖

treyhunner, to python
@treyhunner@mastodon.social avatar

Heads up: many Python-related Cyber Monday sales end today. 🐍⏰

Many courses, books, and skill-building subscriptions are still on sale.

Check whether any of these are things you want soon!

https://treyhunner.com/2023/11/python-black-friday-and-cyber-monday-sales-2023/

treyhunner, to random
@treyhunner@mastodon.social avatar

Hey friiends! 👋

If we haven't met at and you'd like to meet, please message me and we'll find a time and place to meet.

I'll be doing a lot of hallway track-ing today and I'd love to meet you before you leave!

I also still have lots of Python Morsels stickers, if you're interested in stickers but not in meeting. 😅

treyhunner, to python
@treyhunner@mastodon.social avatar

For single-line comments, Python uses the octothorpe character (#), also known as pound, number sign, crunch, and of course, the hashtag character

Read the full article: Multiline comments in Python
https://trey.io/a5O8qe

treyhunner, to python
@treyhunner@mastodon.social avatar

The singleton design pattern involves making a class that has just a single instance.

It's possible to make singletons in , though you may not need to.

Python's module objects are often used for the same shared state that singletons offer.

https://youtu.be/sppHANksoG4

treyhunner, to random
@treyhunner@mastodon.social avatar

I just spent 2 hours debugging my ~/.zshrc, ~/.tmux.conf, and ~/.profile configurations because every new terminal/tmux window I opened resulted in 5 seconds of waiting time.

I spent 2 hours to save myself 5 seconds... though at the rate that those 5 seconds happen those 2 hours should break even within a matter of months.

Plus my brain perceives that my machine is much faster now. 😜

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

I stumbled upon an interesting yesterday. 🐍 🤔

Can you spot the bug in this code?

values = ["Trey", "Hunner", "4"]
contents_string = (
"First,Last,Number\r\n"
",".join(values) + "\r\n"
)

treyhunner, to random
@treyhunner@mastodon.social avatar

I had "I am the very model of a modern Django model form" stuck in my head this morning. 🎶

And now I'm watching old lightning talks. I forgot how much I love lightning talks. ⚡

Thanks @freakboy3742 & @glasnt! 😊

treyhunner, to Economics
@treyhunner@mastodon.social avatar

A while back I said I'd occasionally share thoughts here... then I didn't!

Here are some concepts I feel are far too often undervalued or overlooked when discussing .

Externalities: we do discuss a subset negative ones (e.g. pollution) but we overlook so many, especially positive externalities!

Moral hazard: sometimes govt intervention fixes this, sometimes it causes this, and sometimes both at once!

Opportunity cost: hard at the personal level, but harder govt-wide!

treyhunner, to random
@treyhunner@mastodon.social avatar

Let's say a attendee didn't attend the sprints this year but they plan to next year. They'd like to prepare themselves over the next year.

Thoughts? Advice?

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

You're about to start a new pip-installable project.

What do you do?

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