@bbelderbos@fosstodon.org
@bbelderbos@fosstodon.org avatar

bbelderbos

@bbelderbos@fosstodon.org

Software developer and entrepreneur. Helping people grow their Python + programming skills through https://fosstodon.org/@pybites

This profile is from a federated server and may be incomplete. Browse more on the original instance.

bbelderbos, to random
@bbelderbos@fosstodon.org avatar

How to find the last change to a particular line of code? 💡

  1. Use git annotate (or blame)
  2. Scroll to the specific line
  3. Use git show with the commit hash

bbelderbos, to rust
@bbelderbos@fosstodon.org avatar

I shipped my first crate (= package) to https://crates.io (= package index) 🎉

It's a little command line tool to search @pybites content:

$ psearch
Usage: search <search_term> [<content_type>] [--title-only]

bbelderbos,
@bbelderbos@fosstodon.org avatar

Here is how, including automating the process with GitHub Actions: https://apythonistalearningrust.com/shipped-first-crate/ 📈

I hope it helps you ship some code too, Cargo makes it easy + painless. 😍

bbelderbos, to python
@bbelderbos@fosstodon.org avatar

@treyhunner does a deep dive of #Python's many command-line utilities. 🙏 🔥

First of all it's good to know that you can use -m to run a Python module as if it were a script 💡

There are some nifty ones like inspect / json.tool / zipapp. You'll surely pick up new ones ... 💪

bbelderbos, to rust
@bbelderbos@fosstodon.org avatar

Are you looking to create your own tech blog? 💡

A static site generator + GitHub Pages is a great combo. 😍

I started to use Zola to document my learning (Zola is written in Rust) and it's easy to use, has nice themes and is super fast. 🏃 📈

bbelderbos,
@bbelderbos@fosstodon.org avatar
bbelderbos, to python
@bbelderbos@fosstodon.org avatar

Need to check if a resource exists? 💡

Use requests.head() instead of requests.get() for a faster response! 🏃

It only fetches the headers, saving time and bandwidth. 🕒

I used it the other day to quickly check if a transcript was added to a podcast episode. 😎

bbelderbos, to python
@bbelderbos@fosstodon.org avatar

I was in the debugger the other day and I was wondering how to write a sorted dictionary to a file while there. 💪

And then I learned that pprint's PrettyPrinter can stream to a file, nice! 😎

Here is how:

bbelderbos, to python
@bbelderbos@fosstodon.org avatar

Ever wondered how to debug a hanging test in ?

pip install pytest-timeout
pytest --timeout=N_SECONDS

To dump the whole stack trace you can add --timeout_method=thread as well.

Here is an example:

bbelderbos,
@bbelderbos@fosstodon.org avatar
bbelderbos, to python
@bbelderbos@fosstodon.org avatar

Did you know about this optimization tip? 💡

Integers from -5 to 256 are cached, meaning they are preallocated and reused, boosting memory efficiency. 📈

See below where I use "is" to show the ints that are preallocated to the same object = optimized:

bbelderbos,
@bbelderbos@fosstodon.org avatar

@hugovk hm weird, I use Typefully and always add it to images, so it seems it did not come across then :(

bbelderbos,
@bbelderbos@fosstodon.org avatar

@fabianegli not really, I shared it more like an interesting Python fact. On the other hand I am learning Rust atm and that has me reflect more on performance.

bbelderbos, to python
@bbelderbos@fosstodon.org avatar

🚀 Tip: automate version population in your package's init.py file using importlib.metadata. 📦

✅ Ensures compatibility across Python versions

✅ Automatically sets version from package metadata

bbelderbos, to django
@bbelderbos@fosstodon.org avatar

Interesting: 5.1 ships with the new LoginRequiredMiddleware which "redirects all unauthenticated requests to a login page." 💡

This makes sense if most of your views require authentication. Now you can add login_not_required() to the few that don't require auth. 📈

bbelderbos, to random
@bbelderbos@fosstodon.org avatar

Yesterday I used maturin/PyO3 to get some Rust code working in Python showing a major speed up. 💡💪

It was just a toy example, but this opens up opportunities when I know more Rust (learning it atm) and want to optimize certain parts of my Python code - pretty cool! 😎 📈

bbelderbos, to random
@bbelderbos@fosstodon.org avatar

Clean code tip: break long functions into smaller ones.

A function should do one thing, adhering to the `Single-responsibility Principle'.

This will make your code more modular and easier to test 🛠️

A hint that a function is doing too many things are inline comments.💡

bbelderbos,
@bbelderbos@fosstodon.org avatar

@slott56 haha

bbelderbos, to python
@bbelderbos@fosstodon.org avatar

🔍 Terminal Tip: Instant Source Viewer! 🐍

Below I defined a function called pys (you can add it to your .zshrc).

Now you can peek at Python source code from the command line like this:

$ pys module [object]

E.g.
$ pys collections
$ pys collections Counter

bbelderbos,
@bbelderbos@fosstodon.org avatar

To do this in code, loading a module from a string, you can use importlib like this:

bbelderbos,
@bbelderbos@fosstodon.org avatar

@adamchainz cool, thanks

bbelderbos, to python
@bbelderbos@fosstodon.org avatar

How do you automate pushing 🐍 projects to PyPI? 💡

One useful way is to use a GitHub Action, see here:
https://pybit.es/articles/5-ways-i-use-github-actions/

Adding the condition: if: startsWith(github.ref, 'refs/tags')

When I do:

git tag x.y.z
git push --tags

It pushes a new release 😎 📈

bbelderbos, to Podcast
@bbelderbos@fosstodon.org avatar

And we're back with a new @pybites #podcast episode debriefing Pycon US 2024: favorite moments, takeaways / lessons, inspiration, and more. 😍 📈 🔥

And as usual we geek out over #books 😄 💡

Check it out here:
https://www.youtube.com/watch?v=-JR9QbXCF1U

#python #conference #pycon #developer

bbelderbos, to python
@bbelderbos@fosstodon.org avatar

Tip: you can create an entry point to your package by adding a main.py module to it.

This makes it callable using: python -m my_package.

Example:

bbelderbos,
@bbelderbos@fosstodon.org avatar

Related video:

How to add an entry point to your package
https://www.youtube.com/watch?v=ERRIJUvK5Q0

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