Skip to main content
Image

r/Python pythonLogo pythonLogo

Pycon US 2025 starts next week!


`safer`: a tiny utility to avoid partial writes to files and streams
`safer`: a tiny utility to avoid partial writes to files and streams
Showcase

What My Project Does

In 2020, I broke a few configuration files, so I wrote something to help prevent breaking a lot the next time, and turned it into a little library: https://github.com/rec/safer

It's a drop-in replacement for open that only writes the file when everything has completed successfully, like this:

with safer.open(filename, 'w') as fp:
    fp.write('oops')
    raise ValueError
 # File is untouched

By default, the data is cached in memory, but for large files, there's a flag to allow you to cache it as a file that is renamed when the operation is complete.

You can also use it for file sockets and other streams:

try:
    with safer.writer(socket.send) as send:
          send_bytes_to_socket(send)
except Exception:
     # Nothing has been sent
     send_error_message_to_socket(socket.send)

Target Audience

This is a mature, production-quality library for any application where partial writes are possible. There is extensive testing and it handles some obscure edge cases.

It's tested on Linux, MacOS and Windows and has been stable and essentially unchanged for years.

Comparison

There doesn't seem to be another utility preventing partial writes. There are multiple atomic file writers which solve a different problem, the best being this: https://github.com/untitaker/python-atomicwrites

Note

#noAI was used in the writing or maintenance of this program.


[SPOILER] Top-down twin-stick action inspired by Hotline Miami!
Image [SPOILER] Top-down twin-stick action inspired by Hotline Miami!
media poster



I built a civic transparency platform with FastAPI that aggregates 40+ government APIs
I built a civic transparency platform with FastAPI that aggregates 40+ government APIs
Showcase

What My Project Does:

WeThePeople is a FastAPI application that pulls data from 40+ public government APIs to track corporate lobbying, government contracts, congressional stock trades, enforcement actions, and campaign donations across 9 economic sectors. It serves 3 web frontends and a mobile app from a single backend.

Target Audience:

Journalists, researchers, and citizens who want to understand corporate influence on government. Also useful as a reference for anyone building a multi-connector API aggregation platform in Python.

How Python Relates:

The entire backend is Python. FastAPI, SQLAlchemy, and 36 API connectors that each wrap a different government data source.

The dialect compatibility layer (utils/db_compat.py) abstracts SQLite, PostgreSQL, and Oracle differences behind helper functions for date arithmetic, string aggregation, and pagination. The same queries run on all three without changes.

The circuit breaker (services/circuit_breaker.py) is a thread-safe implementation that auto-disables failing external APIs after N consecutive failures, with half-open probe recovery.

The job scheduler uses file-lock based execution to prevent SQLite write conflicts across 35+ automated sync jobs running on different intervals (24h, 48h, 72h, weekly).

All 36 API connectors follow the same pattern. Each wraps a government API (Senate LDA, USASpending, FEC, Congress.gov, SEC EDGAR, Federal Register, OpenFDA, EPA, FARA, and more) with retry logic, caching, and circuit breaker integration.

The claims verification pipeline extracts assertions from text and matches them against 9 data sources using a multi-matcher architecture.

Runs on a $4 monthly Hetzner ARM server. 4.1GB SQLite database in WAL mode. Let's Encrypt TLS via certbot.

Source code: github.com/Obelus-Labs-LLC/WeThePeople

Live: wethepeopleforus.com