@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

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! 🎉

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 this is neat... but it took me a bit a bit of staring at those letters to figure out what you were suggesting. So maybe too clever? 🤣 I'll think on this.

By the way, the "go grab the Unicode data and stick it in a database" bit of this app was heavily inspired by your code. 💗

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

Apparently unicode.party had 42,800 requests from 3,500 unique visitors over the past 30 days. 😮

I have no idea when this many people started using it or how they found out about it.

I assumed I was one of about 12 users up until now...

/cc @bmispelon

treyhunner, to python
@treyhunner@mastodon.social avatar

Even though we were returning in both the "if" and the "else", the body of the "if" and the body of the "else" didn't feel balanced to begin with.

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

ashleycollinge, to python
@ashleycollinge@mountains.social avatar

Anyone have any web frontend recommendations? I've only ever used HTML/JS, but I'd like to use a framework to maybe make things simpler/quicker(?) I come from a Python background if that helps!

treyhunner,
@treyhunner@mastodon.social avatar

@ashleycollinge I love the simplicity of Alpine.js for fully front end interactivity and HTMX for most server round trips.

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

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