@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

The REPL will automatically print any result that isn't None.

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

treyhunner, to random
@treyhunner@mastodon.social avatar

I am SO excited for the new Python REPL for 3.13 that I made the PR branch for this the default Python on my machine today so that I can test it out in the coming weeks.

If you use pyenv and you'd like to do the same, here the (AI-generated but seemingly correct) instructions I used: https://treyhunner.com/2024/05/installing-a-custom-python-build-with-pyenv/

Also listen to the core.py podcast with @ambv and Pablo Galindo Salgado. That's how I found out about this new feature months ago.

yantor3d, to python
@yantor3d@mograph.social avatar

A dutiful dev writes a test for both sides of the if/else for 100% coverage.

A lazy dev re-writes the if/else as a ternary operator for 100% coverage.

treyhunner,
@treyhunner@mastodon.social avatar

@yantor3d this is why I always enable branch coverage 😬

treyhunner, to python
@treyhunner@mastodon.social avatar

If you'd like to serve up a different directory besides the current one (which is the default) you can provide a -d argument specifying the directory to serve.

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

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

@_KevinTrainor unless your website uses completely relative linking everywhere (../thing.html and dir/thing.html) then you'll end up with broken links everywhere. Many static site builders use absolute links (with a leading /).

Also if you use subdirectories with index.html files you'll end up navigating the the directory listing instead of the index file repeatedly. That one's a slightly more minor annoyance though.

treyhunner,
@treyhunner@mastodon.social avatar

@Ertain you're welcome!

carapace, to python
@carapace@mastodon.social avatar

(In )

x = 1
while True:
print(bin(x)[2:])
x = x ^ (x << 1)

1
11
101
1111
10001
110011
1010101
11111111
100000001
1100000011
10100000101
111100001111
1000100010001
11001100110011
101010101010101
1111111111111111
10000000000000001
110000000000000011
1010000000000000101
11110000000000001111
100010000000000010001
1100110000000000110011
10101010000000001010101
111111110000000011111111
1000000010000000100000001
11000000110000001100000011
...

https://en.wikipedia.org/wiki/Sierpi%C5%84ski_triangle

treyhunner,
@treyhunner@mastodon.social avatar

@carapace I turned this into a slightly more traditional version of the triangle, partly inspired by @AlSweigart's scrollart.org

https://pym.dev/p/3cmma/

treyhunner, to random
@treyhunner@mastodon.social avatar

Variables in Python are NOT like buckets that contain objects ❌🪣

Variables are "pointers". Imagine an arrow pointing from a variable to an object.

my_name ⇨ "Trey Hunner"

https://pym.dev/variables-are-pointers/?watch

CodenameTim, to django
@CodenameTim@fosstodon.org avatar

Coming back at update_or_create for round 2!

If you call update_or_create where the instance already exists and the defaults passed in are already the values on the instance, do you ever want it to actually re-save the instance?

I'm running into the case where I'd prefer it to not update the instance if it doesn't need to. I'm guessing I'm overlooking some race condition problem though.

treyhunner,
@treyhunner@mastodon.social avatar

@webology @ryanhiebert @CodenameTim I thought some folks added functionality to simple history to control whether/when a history snapshot happens.

treyhunner,
@treyhunner@mastodon.social avatar

@CodenameTim @webology @ryanhiebert
It was skip_history_when_saving that I was thinking of. I had thought of using that with django-lifecycle in the past. My data duplication issue didn't end up being too big of a problem though, so I never got around to it.

The biggest upside of django-simple-history is also it's biggest upside: it's automatic. 😬

treyhunner,
@treyhunner@mastodon.social avatar

@ryanhiebert @CodenameTim @webology I actually made FieldTracker because I was inspired by django-simple-history and thought django-model-utils might be a good home for that. 😜

Honestly I use django-lifecycle's dirty-checking more often now since it's good enough for my use cases.

treyhunner, to python
@treyhunner@mastodon.social avatar

With the "else", this code looks to me a bit like a balance scale, with an "if" and an "else" on either side.

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

treyhunner, to random
@treyhunner@mastodon.social avatar

@buttondown will you have a booth at @pycon? I'm curious to know how Buttondown compares to my current email/newsletter setup and would love to chat with folks in-the-know.

Regardless of whether you'll have a booth, thank you for sponsoring! 💖

chrisyxlee, to python
@chrisyxlee@hachyderm.io avatar

TIL in my adventures, the proper way to set up a class that returns itself is to use the Self typing annotation and return values by calling self.__class__ on the new value

from typing import Self  
class ImActuallyAString(str):  
 def one_longer(self) -&gt; Self:  
 return self.__class__(self + "one")  

ᕕ( ᐛ )ᕗ (please tell me if I'm wrong lol)

treyhunner,
@treyhunner@mastodon.social avatar

@chrisyxlee I always forget about typing.Self 👏

I'd call type(self)(...) on the new item. I prefer to avoid dunder attributes when there's a higher-level way to access the same thing.

This is not a universally agreed upon convention though, as there are parts of CPython's source code that use self.class and parts that use type(self). Most of the self.class parts are older and may have predated type(...) being usable (old-style classes didn't work well with type back in Python 2).

carlton, to random
@carlton@fosstodon.org avatar

We’re on 314 now. You’re all lovely 🥰

From: @carlton
https://fosstodon.org/@carlton/112359422380857316

treyhunner,
@treyhunner@mastodon.social avatar
treyhunner, to random
@treyhunner@mastodon.social avatar

I subscribed to an open source PR months ago for a feature I really want to see happen. It sat dormant for months and activity started up again this weekend. I've been excitedly watching commit pushes over the last few days. I'll be silently cheering on the sidelines and looking forward to enthusiastically alpha/beta-testing this feature once it's ready.

💖 @ambv: cheering you and Pablo on 👏

treyhunner, to random
@treyhunner@mastodon.social avatar

I've had Moonage Daydream playing in my head since I woke up. Looks like that'll be my brain's background track while teaching today. 🎶

treyhunner,
@treyhunner@mastodon.social avatar

During each of the breaks during today's sessions I'm finding that Mr. Mastodon Farm is stuck in my head. 🎶 One weird song to another. 🤷

treyhunner, to python
@treyhunner@mastodon.social avatar

The index method is for making integer-like objects.

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

#Python

treyhunner, to python
@treyhunner@mastodon.social avatar

So before you remove that "else" statement, think about whether it makes your code more readable or not.

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

treyhunner,
@treyhunner@mastodon.social avatar

@bugroar It's an option! Though in this case the "blah" would be pretty long.

treyhunner, to python
@treyhunner@mastodon.social avatar

This week's Python tip email will be my 100th one! 🎉

I just realized this while planning the email this morning. I'm glad I thought to check when 100 would be! 💯

That's 2 base-10-based anniversaries within a couple weeks (over the weekend I also shared that I just passed my 10 year anniversary of attending Python conferences). 🔟

This week's tip will be related to an activity that got me into this whole Python community thing. 💖

If you're into newsletters & : https://pym.dev/newsletter 💌

ehmatthes, to random
@ehmatthes@fosstodon.org avatar

Python people, do you make short-lived intermediate variables to make your return statements readable?

For example, which of these would you tend to prefer?

def get_project_name(output_str): """Get project name from output of

treyhunner,
@treyhunner@mastodon.social avatar

@ehmatthes this is pretty much the example I give for when I remove that extra variable.

When the function is named "get_something" and the last line is "return something", I'd rather remove the "something" variable when possible.

If the function name isn't the same as the variable name or the variable is assigned somewhere that isn't immediately before the last "return", I'd leave it.

Otherwise, I always remove it.

treyhunner,
@treyhunner@mastodon.social avatar

@ehmatthes since I readability isn't an issue in this case (IMO) because the function name is the same as the variable name, the main reason I usually hear argued for keeping it is for the sake of ease of debugging.

I might add the variable back temporarily when debugging (just as I might unwrap a comprehension into a loop). Though PDB has a "retval" command for this purpose.
https://docs.python.org/3/library/pdb.html#pdbcommand-retval

bbelderbos, to github
@bbelderbos@fosstodon.org avatar

A cool way to keep your profile up2date is by creating a self-updating Readme, a trick I picked up from Simon Willison.

Here is how I made mine pulling in different feeds:
https://pybit.es/articles/how-to-create-a-self-updating-github-readme/

I hope it inspires you to make your profile more engaging.

treyhunner,
@treyhunner@mastodon.social avatar

@bbelderbos Neat trick! I may end up doing some variation of this. Thanks for sharing Bob. 💗

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