Elektrine lite

← Feed

Trey Hunner 🐍

treyhunner@mastodon.social

<p><a href="https://mastodon.social/tags/Python" class="mention hashtag" rel="tag">#<span>Python</span></a> &amp; <a href="https://mastodon.social/tags/Django" class="mention hashtag" rel="tag">#<span>Django</span></a> educator &amp; team trainer</p><p>I help folks sharpen their Python skills with <a href="https://PythonMorsels.com" target="_blank" rel="nofollow noopener" translate="no"><span class="invisible">https://</span><span class="">PythonMorsels.com</span><span class="invisible"></span></a>🐍🍪</p><p><a href="https://mastodon.social/tags/pythonoddity" class="mention hashtag" rel="tag">#<span>pythonoddity</span></a></p><p>Also: ostrovegan, sentientist, YIMBY, and I think economics is highly underrated. I don&#39;t post about any of those topics very often.</p><p>he/him</p>

Posts

  • Post #4350726

    Python Tip #214 (of 365): Use truthiness for integers with caution. I DO rely on truthiness checks for integers, but not always. I would recommend only using them when they seem to improve readability. I find this: if hours: ... More readable than this: if hours &gt; 0: ... But I find this: if number % 2 != 0: ... More readable than this: if number % 2: ... 🧵 (1/2) #Python #DailyPythonTip

  • Post #4306059

    Did you know that map()/filter() were added to Python 6 years before comprehensions? Also range() was added 9 years before zip() which was added 3 years before enumerate(). And strings methods weren&#39;t methods for the first 9 years... they lived in the string module! For years I&#39;ve wanted an easier way to look up facts like those. Now I have a way! https://pym.dev/since It&#39;s also available as a CLI tool via PyPI uvx sincewhen &#39;sum()&#39; #Python #programming

  • Post #2133278

    RE: https://fosstodon.org/@pycon/116516549603890416 Coming to #PyConUS? Register for my tutorial to learn about generators &amp;amp; iterators with me. See you there!

  • Post #2013686

    @willingc gave a great keynote at #PyBeach today on #Python, AI, and communication.

  • Post #1956193

    A new ridiculously long but VERY packed video by @hynek on using uv for your local #Python project workflow... 😮 https://youtu.be/TiBIjouDGuI?feature=shared It&amp;#39;s secretly actually an infomercial for just files and direnv... which is great because this video might finally have convinced me to just give just a try (after months of @webology telling me I should be using it already). I will definitely be rewatching parts of this video 2-3 times. 👏

  • Post #1882702

    Python Tip #119 (of 365): Don&amp;#39;t just rely on the built-in types for argparse&amp;#39;s type parameter You can create your own argparse &amp;quot;type&amp;quot; with a callable that accepts a string and returns the parsed object, raising a ValueError (with a user-facing message) if parsing failed. If you wanted to accept a YYYY-MM-DD date for an argument, you could make a date function: import datetime as dt def date(string): return dt.‍date.strptime(string, &amp;quot;%Y-%m-%d&amp...

  • Post #1821653

    Python Tip #118 (of 365): Instead of argparse.FileType, use pathlib.Path The argparse module&amp;#39;s FileType is deprecated (as of Python 3.14). It has known issues: no newline argument accepted, encourages not using a context manager, etc. Instead of argparse.FileType you should use pathlib.Path. Using &amp;quot;type=Path&amp;quot; will auto-convert given arguments to pathlib.Path objects. That way all command-line-provided file paths will turn into pathlib.Path objects automatically!...

  • Post #1589232

    T-strings are primarily useful for the maintainers of libraries that are used by other Python developers. Read more 👉 https://trey.io/n4mlgb #Python

  • Post #1588436

    I made a thing last week and Earth Day seems like a great day to announce it. 🌏 https://Whereabouts.Earth is a fun (for me at least) app for learning the location of every country on Earth. It uses a very effective learning technique (spaced repetition), but you don&amp;#39;t need to know how that part works. It will work. My geography knowledge has improved a LOT over the past week. Join me in learning about the only home we&amp;#39;ll ever have. (And please share your feedback if you actu...

  • Post #1553067

    Python 3.14 is now out! 🐍🥧🎉 My favorite new features include: • All the color (REPL &amp;amp; PDB syntax highlighting, argparse help, unittest, etc.) • pathlib&amp;#39;s copy &amp;amp; move methods • date.strptime • uuid7 • argparse choice typo suggestions • t-strings • concurrent subinterpreters • import tab completion And that list doesn&amp;#39;t even include free-threading, JIT, the external debugger interface, or asyncio introspection! A 6 minute demo of all my favorite features: https:...

  • Post #1350658

    RE: https://mastodon.social/@hugovk/116370025617675394 Big news (for me at least): pprint on Python 3.15 is going to allow MUCH friendlier output! 🎉 Instead of: pprint.pprint(some_object) If you do this: pprint.pprint(some_object, indent=4, expand=True) You&amp;#39;ll see output that looks like the most commonly used modern Python line-wrapping style in actual code! I rarely use pprint because its output style is so bizarre. I may start using it on Python 3.15 (even though the version I&...

  • Post #1350657

    Python Tip #104 (of 365): Use an _ to represent a placeholder variable. This can be handy when you need to use range to loop N times but you don&amp;#39;t care about the current iteration number in the loop: matrix = [[0] * 3 for _ in range(3)] Or when you&amp;#39;re unpacking a tuple and one or more tuple values are unimportant: [(item, _)] = collections.Counter(items).most_common(1) When I see _ I think &amp;quot;I don&amp;#39;t need to care about that value&amp;quot;. #Python #DailyPyt...

  • Post #1350656

    “But del is a statement, not a function.” Read more 👉 https://pym.dev/unnecessary-parentheses/ #Python

  • Post #1350655

    “Python typically prints unhandled exceptions and warning messages to standard error.” Read more 👉 https://pym.dev/standard-error/ #Python

  • Post #1350653

    Python Tip #105 (of 365): When breaking a function call or list literal over multiple lines, use trailing commas. For example, take this list: fruits = [ &amp;quot;lemons&amp;quot;, &amp;quot;apples&amp;quot;, &amp;quot;watermelon&amp;quot;, ] Moving &amp;quot;lemons&amp;quot; to the end is easy when there&amp;#39;s a trailing comma: just cut and paste. Without trailing commas, moving &amp;quot;lemons&amp;quot; to the end means adding a comma after &amp;quot;watermelon&amp;quot;...

  • Post #1350652

    “Parentheses are used for 3 things in Python: calling callables, creating empty tuples, and grouping.” Read more 👉 https://pym.dev/unnecessary-parentheses/ #Python

  • Post #1350651

    Python Tip #106 (of 365): Name ambiguous unicode characters with &amp;quot;\N{...}&amp;quot; When you need to represent a non-ASCII character, you can either: 1. Copy-paste the character directly into your file 2. Use a &amp;quot;\u&amp;quot; or &amp;quot;\U&amp;quot; prefix to represent the character by its code point 3. Use &amp;quot;\N&amp;quot; to represent the character by its name 🧵 (1/3) #Python #DailyPythonTip

  • Post #1350650

    Python Tip #107 (of 365): Use the walrus operator to simplify &amp;quot;while&amp;quot; loops. This generator function reads a file in chunks: def read_in_chunks(path): with open(path, mode=&amp;quot;rb&amp;quot;) as file: chunk = file.‍read(8192) while chunk: yield chunk chunk = file.‍read(8192) Notice that &amp;quot;chunk = file.‍read(8192)&amp;quot; appears twice: once before the loop and once at the end. This is a somewhat common pattern with &am...

  • Post #1350649

    Python Tip #108 (of 365): Don&amp;#39;t use semicolons. Want to put 2 statements on one line? from rich.traceback import install; install(show_locals=True) Don&amp;#39;t. Just use 2 lines: from rich.traceback import install install(show_locals=True) It&amp;#39;s more readable. That&amp;#39;s the entirety of today&amp;#39;s tip. 🤷 #Python #DailyPythonTip

  • Post #1190448

    RE: https://fosstodon.org/@ThePSF/116403474490412403 Stay at The Westin and play Cabo and other card games and board games in the lobby in the evenings! All are welcome to join. You&amp;#39;ll be able to spot us because we&amp;#39;ll look like PyCon attendees. Come over, say hi, play games, and chat.

  • Post #1089204

    “Even if you don&amp;#39;t use them directly, dataclasses are a good measuring stick for your class-based code.” Read more 👉 https://pym.dev/friendly-classes/ #Python

  • Post #1065481

    Python Tip #97 (of 365): When you need to read/write a whole file all at once, use the appropriate pathlib.Path helper method Instead of: with open(path) as file: contents = file.‍read() Do this: contents = path.‍read_text() And instead of: with open(path, mode=&amp;quot;wt&amp;quot;) as file: file.write(contents) Do this: path.write_text(contents) #Python #DailyPythonTip

  • Post #1065480

    @pythonbytes some interesting things you may want to consider in an upcoming episode: 1. Savannah Ostrowski&amp;#39;s new Core Dispatch newsletter: https://coredispatch.xyz 2. my article on pdbrc files: https://treyhunner.com/2026/04/customizing-pdb-with-pdbrc/ or Ned&amp;#39;s *much* earlier article that I discovered after I published mine: https://nedbatchelder.com/blog/200704/my_pdbrc 3. I can&amp;#39;t remember whether you&amp;#39;ve covered this and it might not be time yet (it&amp;#39;s o...

  • Post #1065479

    Python Tip #98 (of 365): Don&amp;#39;t use open method on pathlib.Path objects 🧵 Instead of this: with path.‍open() as file: ... Prefer this: with open(path) as file: ... Since Python 3.6, pathlib.Path objects can be passed to the built-in open function. pathlib.Path&amp;#39;s open method is a relic from an earlier version of pathlib that wasn&amp;#39;t yet fully embraced by Python&amp;#39;s various path-handling features. I recommend using Python&amp;#39;s built-in open() functio...

  • Post #1065478

    $ docker volume prune ... Total reclaimed space: 224.4GB Yikes...

  • Post #1065477

    “While splitting on a line feed character will often work, I recommend using the string splitlines method instead.” Read more 👉 https://pym.dev/splitlines/ #Python

  • Post #1065476

    I just discovered a &amp;quot;Speaker Card&amp;quot; button on my PyCon dashboard page.

  • Post #1065475

    I&amp;#39;ve updated my blog post on how to have a great first #PyConUS: https://trey.io/first-pycon The only TBD links are the volunteering link and the open spaces links. I&amp;#39;m considering making an audio recording of this post for folks to listen to during the travels to Long Beach. Anyone interested in that?

  • Post #1065474

    Have a date string and want to find the format string needed to parse it? Read more 👉 https://trey.io/l4b84k #Python

  • Post #1065473

    Python Tip #99 (of 365): Don&amp;#39;t convert pathlib.Path objects to strings Using a pathlib.Path object in an f-string or a print call is fine and I do this often. But if you THINK you need to convert a pathlib.Path object to a string to pass it off to some other path-handling utility, you probably don&amp;#39;t need to. Most path-handling utilities in Python support pathlib.Path objects just fine. #Python #DailyPythonTip