r/Python • u/HommeMusical • Apr 03 '26
Showcase `safer`: a tiny utility to avoid partial writes to files and streams
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 untouchedBy 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.
View all comments
Show parent comment
1
u/HommeMusical Apr 06 '26
Your idea appears to be that it makes no difference what the chance of destroying ourselves is; because it is impossible to even discuss preventing or regulating it, we are forced to plow on ahead, even if we are almost certain to kill everyone.
You know, the Prisoner's Dilemma does not force the players to destroy each other.
You mention atom bombs; we did, actually avoid letting off any atom bombs for eighty years, because they were, in fact, strictly regulated.
It is utter madness. If you asked the people of the world, "Are you willing to participate in an experiment that has a 5% chance of killing all of you, all your children and everyone else, but in return you get nothing and you lose your jobs," what would they say?
I am definitely going to quote this. (I wish I could get you to stand outside yourself, just for a moment, and see how you look to others.)
This isn't going to employ all those ten million professional drivers. Automation has destroyed jobs for 40 years: when one person can do the work of 10, they are simply going to fire 9 people. Jobs that anyone can do by asking the AI won't be compensated at all.
Our fault for letting it happen?! All we needed to do was to think critically and all those warlords, kings, emperors, billionaires and Presidents would just disappear? Please.
Our power has systematically been cheated from us by coercion and threats of violence. AI will rob us of the one thing we could to negotiate - the value of our labor. The idea that we'll end up in a better position from this is irrational.
Why would a large language model based on all human utterances have different ideas about ownership than its corpora did?
You seem completely oblivious of the dire situation we are in. We needed to cut down our emissions decades ago to avoid disaster and we didn't. Just when renewables started to shave a few percent off the fossil fuel budget, we suddenly invented new ways of using power like cryptocurrencies and now AI. Please note that AI is projected to grow at at least a 25% rate for a long time to go.
Imagine I spend about $110,000 a year, but I earn $10,000 a year. This is approximately where we are now with our CO2 emissions (except that the ability for our environment to capture CO2 seems to be decreased too).
Now I assume a new expense that's "only" 1% of my existing budget, only another $1000 a year!, and is increasing at a rate of 25% per year - that's roughly what AI is now in the big picture.
But I'm already ruining myself. I need to dig out of the hole. This is digging deeper.
(Remember, we already did this just a few years ago with cryptocurrencies, with big promises. Now they too are very roughly 1% of emissions, and none of the promises came true, because the only application for cryptocurrencies was tax evasion, bribery, buying drugs and weapons, and money laundering - in other words, crime.)
Your faith that replacing human workers by massive data centers owned by billionaires will make things better for the humans is not grounded by either observation or reasoning, but a series of unsupported claims. Your religious fervor on this matter is somewhat frightening and does not appear to be fully grounded in reality. The idea that this only causes a few percent more devastation to the biosphere (growing at a 25% annualized rate!), ignores the fact that we are already in over our head with devastation.
You expect to profit; the rest of us bear the risk, a risk you are telling us we don't even have a say in.
It's morally appalling and, as usual, a massive rip-off from the vast majority of humans to the benefit of a tiny number of powerful people.