July 2nd, 2024

Python Modern Practices

Python development best practices involve using tools like mise or pyenv for multiple versions, latest Python version, pipx for app running. Project tips include src layout, pyproject.toml, virtual environments, Black, flake8, pytest, wheel, type hinting, f-strings, datetime, enum, Named Tuples, data classes, breakpoint(), logging, TOML config for efficiency and maintainability.

Read original articleLink Icon
Python Modern Practices

Python development best practices include installing Python with tools supporting multiple versions like mise or pyenv, using the latest Python version, and running developer applications with pipx. Developing projects involves avoiding Poetry, using pyproject.toml files, creating a src layout directory structure, and utilizing virtual environments with requirements.txt files. Code formatting with tools like Black, linting with flake8 or Ruff, and testing with pytest are recommended. Packaging applications with wheel packages or container images is advised. Language syntax suggestions include using type hinting, f-strings for formatting, datetime objects with time zones, and enum or Named Tuples for key-value pairs. Data classes and collections.abc for custom types, as well as using breakpoint() for debugging, are also highlighted. Lastly, logging is preferred over print() for diagnostic messages, and the TOML format is recommended for configuration. These practices aim to enhance Python project development efficiency and maintainability.

Related

Software Engineering Practices (2022)

Software Engineering Practices (2022)

Gergely Orosz sparked a Twitter discussion on software engineering practices. Simon Willison elaborated on key practices in a blog post, emphasizing documentation, test data creation, database migrations, templates, code formatting, environment setup automation, and preview environments. Willison highlights the productivity and quality benefits of investing in these practices and recommends tools like Docker, Gitpod, and Codespaces for implementation.

What's up Python? Django get background tasks, a new REPL, bye bye gunicorn

What's up Python? Django get background tasks, a new REPL, bye bye gunicorn

Several Python updates include Django's background task integration, a new lightweight Python REPL, Uvicorn's multiprocessing support, and PyPI blocking outlook.com emails to combat bot registrations, enhancing Python development and security.

Elixir for Python Developers

Elixir for Python Developers

This comparison delves into Python and Elixir, emphasizing syntax disparities and distinctive traits. Elixir prioritizes immutability and pattern matching, while Python focuses on object-oriented elements. Both share list comprehensions and parallel processing, yet Elixir's functional approach and pattern matching distinguish it. Elixir's Stream module supports lazy operations for big data, contrasting Python's I/O streams. Python developers can benefit from exploring Elixir.

Elixir for Humans Who Know Python

Elixir for Humans Who Know Python

The article explores transitioning from Python to Elixir, emphasizing Elixir's concurrency, Phoenix framework, LiveView feature, immutability, and pattern matching. It compares Elixir's functionalism and control flow to Python, showcasing Elixir's efficiency for web development.

Python grapples with Apple App Store rejections

Python grapples with Apple App Store rejections

Python 3.12 faced rejections in Apple's App Store due to the "itms-services" string. Python developers discussed solutions, leading to a consensus for Python 3.13 with an "--with-app-store-compliance" option to address the issue.

Link Icon 16 comments
By @jiripospisil - 4 months
I'm surprised the post doesn't mention uv, which did quite well on HN a few months ago. Any experience?

https://github.com/astral-sh/uv

https://news.ycombinator.com/item?id=39387641

By @helsinki - 4 months
I've not had any issues using mamba environments and vanilla pip. Not sure why I would need to change that? I have packages that thousands of people use, and others that help manage billions of dollars. I've managed to keep things simple without upgrading tooling. This article feels like it's suggesting flavor of the month technologies without explaining why the status quo is bad. Also, you can't really use virtualenvs easily when you have packages that depend on C libraries.
By @throwadobe - 4 months
For what it's worth, I still prefer to use pip, python -m venv and virtualenvwrapper. Solves 99% of the issues with 1% of the complexity
By @kebman - 4 months
For many sections I'd ask myself "Why?" Sure, I know you can click on the links and do deeper research, but I'd love a quick "why" blurb for each section. Just a short "Why this is nice? Well, because..."
By @thomasahle - 4 months
> Use requirements.txt Files to Install Packages Into Environments

Isn't this obsoleted by the previous section on using pyproject.toml?

By @martinky24 - 4 months
Overall, generally reasonable. Nothing is a hard and fast rule, if you know what you're doing and have a good reason for going down an alternative path, that's great. But if on a new project you generally follow these you'll be setting yourself up for success.
By @skeledrew - 4 months
I've been using conda+poetry as my goto combo for years now and it's served me very well. Sure it's said poetry doesn't follow certain standards, but it just abstracts away so much that I don't see myself needing anything else really. If I want to share a non-poetry artifact, it's a simple `poetry build` to create a wheel.

I did come across a weird dependency confusion issue recently when I tried using it on a server setup with piku though, but haven't gotten around to find what exactly is happening.

Also don't think I'll ever give up on YAML for it's easy read+write, and I love the simplicity of fire for CLI things. And pathlib.Path().glob() for directory listing.

By @ctoth - 4 months
Sad to not see Attrs mentioned. Dataclasses are cool, I guess, but Attrs is seriously great.
By @wanderingmind - 4 months
Curious why there is no mention of conda and its fast alternative mamba (micromamba). In my experience, conda is much more robust in managing. dependencies conflict (thanks to AWS and especially boto3) compared to pip.
By @diimdeep - 4 months
Slapped together gossip turned cargo cult use this practices.
By @jw887c - 4 months
>Avoid using Poetry for new projects. Poetry predates many standards for Python tooling. This means that it uses non-standard implementations of key features, such as the dependency resolver and configuration formats in pyproject.toml files.

What? This is the first I've heard of this.

By @behnamoh - 4 months
Has anyone tried orchestrating Python code using Elixir/Erlang? I feel like the true concurrency can only be achieved in the actor model of BEAM, and async still just does concurrent code, not parallel.
By @joshuamcginnis - 4 months
I like miniconda for managing personal python projects.
By @lolpanda - 4 months
what are the pros and cons with pydantic models vs data classes? i like the runtime validation in pydantic models. the pattern i landed on is that 1) only use primitive data types such as str, int, pydantic models and pandas dataframes to present data. 2) no classes with methods, no polymorphism, only use module functions.