@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.

scerruti, to random
@scerruti@csed.social avatar

@treyhunner are you aware of any way to deal with IEEE 745 Single Precision floats in Python on a bit level? I'm trying to do something stupid as a demo for class and it works but it's slow.

https://github.com/e3-cerruti/jupyter-calculus/blob/main/Applicatons%20of%20Newton's%20Method.ipynb

treyhunner,
@treyhunner@mastodon.social avatar

@scerruti working with bytes instead of strings should be faster. Anything implement in Python itself will be slower, but working with the actual bytes instead of strings should remove some overhead.

struct may or may not be useful (I haven't implemented this myself, at least not in many years).

I'd ask Claude, MS Copilot/GPT 4, and Gemini for a better solution that's lower level and see what they come up with. Could be a start or might not work, but might have some time.

treyhunner, to python
@treyhunner@mastodon.social avatar

Python's "next" function is designed to be used with iterators.

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

hynek, to python
@hynek@mastodon.social avatar

In my quest to produce a shorter video, I’ve produced my longest so far:

tox vs Nox: What are they for & How do you choose?: https://www.youtube.com/watch?v=ImBvrDvK-1U

In which I introduce those two indispensable tools and reflect my own usage of BOTH. Enjoy!

treyhunner,
@treyhunner@mastodon.social avatar

@hynek another video that taught me something that wasn't the primary purpose of the video!

I'm now using Python version classifiers to control GitHub actions & nox versions. ✅

https://github.com/treyhunner/countdown-cli/pull/208/commits/d33716b471a028ee1c56e35672e94e896dc5f360

Thanks! 💗

treyhunner, to random
@treyhunner@mastodon.social avatar

Coming to @pycon next week?

If this is your first , join us for the Newcomer's Orientation before the opening reception on Thursday!

https://us.pycon.org/2024/events/newcomer-orientation/

Also see my post on how to have a great first PyCon as well @brainwane's post for folks who've already attended a conference before.

https://treyhunner.com/2018/04/how-to-make-the-most-of-your-first-pycon/
https://www.harihareswara.net/posts/2023/your-first-pycon-but-not-your-first-convention/

treyhunner, to python
@treyhunner@mastodon.social avatar

The bisect module has an implementation of binary search for you.

Read the full article: Python Big O: the time complexities of different data structures in Python
https://trey.io/d8D57O

treyhunner, to random
@treyhunner@mastodon.social avatar

@pycon just a thought, but you may want to put in your bio so folks know that's the official hashtag.

I often see folks assuming it's when it should be .

treyhunner, to python
@treyhunner@mastodon.social avatar

What are your favorite one liners?

treyhunner,
@treyhunner@mastodon.social avatar

@rochacbruno nice one.

In case you're unaware of it:

export PYTHONBREAKPOINT="ipdb.set_trace"

https://kortina.nyc/notes/use-ipdb-as-the-default-python-debugger-with-breakpoint-and-pythonbreakpoint/

treyhunner,
@treyhunner@mastodon.social avatar

@iris Great ones!

Here's an even shorter equivalent to locals():

vars()

Relatedly: dir() (for just the names)

Also vars(an_object) or dir(an_object).

Since vars(an_object) only looks at dict they're a bit different. Sometimes I'll do vars(an_object).keys() to see just the attributes that live directly on an object.

bmispelon, to django
@bmispelon@mastodon.social avatar

New article on my blog: a ORM technique I found for combining JSONObject and Subquery to build model instances: https://blog.bmispelon.rocks/articles/2024/2024-05-09-django-getting-a-full-model-instance-from-a-subquery.html

treyhunner,
@treyhunner@mastodon.social avatar

@bmispelon This is so clever! 🎉And a great write up! 👏

I appreciate your "attempt 1", "attempt 2", style write ups in general. It makes me feel like I'm suffering through searching for the solution along with you. 😆

treyhunner, to python
@treyhunner@mastodon.social avatar

You may be wondering, why is the start index included, but the stop index is excluded?

Read the full article: List slicing in Python
https://trey.io/ZEuawA

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

@minkiu The Venn diagram overlap would be a circle within a circle.

Then new REPL isn't meant to replace bpython, ptpython, or IPython. But it does borrow some of my favorite features.

treyhunner,
@treyhunner@mastodon.social avatar

@bobmagicii when you type "exit" it... Exits! 🎉

treyhunner,
@treyhunner@mastodon.social avatar

@chrysn @fohrloop I imagine someone motivated to make that change would need to do it.

I assume "-m asyncio" makes up a fairly small portion of all REPL usage. That doesn't mean it is unimportant to those who use it of course!

tonybaloney, to random
@tonybaloney@fosstodon.org avatar

Spot what's new 🧐

treyhunner,
@treyhunner@mastodon.social avatar

@tonybaloney the fact that your exit doesn't need parenthesis after it 💡

treyhunner, to python
@treyhunner@mastodon.social avatar

Which means we can use "next" to get the first item from any iterable as long as we pass that iterable to the built-in "iter" function first

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

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

@pg @notsolonecoder yes it's fully in-browser, running on CPython compiled to WebAssembly via Emscripten.

The Python Morsels exercise automated tester is powered by this as well, so user solutions run right in the browser.

Also this pastebin tool runs on the same system: https://pym.dev/p

I'm not using pyodide because I don't want to allow arbitrary JS code execution. A pyodide dev told me that I could have put a wrapper/sandbox around pyodide to accomplish something similar as well.

treyhunner,
@treyhunner@mastodon.social avatar

@pg Yup! @notsolonecoder has given a number of talks on WebAssembly and CPython: https://www.youtube.com/results?search_query=katie+bell+webassembly

I understand the concern of load time. Since the running on Python Morsels is triggered by the user, the wasm file is often loaded by the time the user hits the run button and when it's not it typically only adds a couple seconds delay.

I'm hoping smart folks find a way to make the initial wasm file that's sent considerably smaller.

treyhunner,
@treyhunner@mastodon.social avatar

@notsolonecoder I think it would be!

I've also thought of trying to at least make a better demo for folks that lives outside of CPython's repo. I'd be happy to collaborate on and/or beta test either/both of these things.

Completely separately: support for WebAssembly support for readline and curses one day would be incredible. Both are required for the brand new Python REPL in CPython's main branch, which I'm pretty sure means no new-style REPL in WASM land. This seems far less trivial though.

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

@sethmlarson These responses are making me think of a couple other directions for the name:

chargrep
charfind
findchar
strfind

treyhunner,
@treyhunner@mastodon.social avatar

@zecege nice! That's pretty much the same purpose but without the UI.

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

@_KevinTrainor folks keep noting the one exit point coding style in regards to this video and I'm surprised at how common this style is in Python.

I've never been a convert to this style of coding and I suspect that it no longer makes sense in Python, but I need to do some research to make my case.

if I put together some challenges that will attempt to poke holes in this style, would you be interested in trying to rebut those challenges?

I'd like to understand the mindset of folks using this.

treyhunner,
@treyhunner@mastodon.social avatar

@_KevinTrainor 👍 I'll give this some more thought after PyCon. Thanks!

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