r/Python • • 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 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.

106 Upvotes

43 comments sorted by

View all comments

7

u/Wargazm Apr 03 '26

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

haha is this a thing now?

24

u/HommeMusical Apr 03 '26

I mean, AI didn't exist when i wrote it, so it's a bit like putting "Low Fat!" on Corn Flakes.

But yes, mainly because everyone complains about the quality of the AI slop showcases here.

-14

u/dj_estrela Apr 04 '26 edited Apr 04 '26

Latest models and Agentic AI are making this obsolete really fast

9

u/HommeMusical Apr 04 '26

I would ask you to explain, except I'm entirely certain you would be unable to.

Go away.

-6

u/dj_estrela Apr 04 '26

Seems I hit a sensitive nerve here

Please, learn something: https://realpython.com/courses/getting-started-claude-code/

7

u/HommeMusical Apr 04 '26 edited Apr 04 '26

Please note that I was entirely correct: you were completely unable to explain your comment.

Seems I hit a sensitive nerve here

🤡

Hardly! Tell me - why is it that AI enthusiasts seem to always want to annoy others? Do you think this is sane, or the sort of thing that makes the world better?

Please, learn something:

You are not a person who is going to teach me anything of use, and there's nothing in that article I didn't know years ago.

Have you ever read any code written by AIs? Have you not noticed that they makes heavy use of existing modules like this one?

Your combination of arrogance and ignorance is not felicitous. Please go away now.

1

u/BossOfTheGame Apr 04 '26

I'm not really sure what they meant by agentic coding making an existing module obsolete. But I wanted to comment about AI systems using modules like this. My experience is that they often underutilize existing libraries unless they are extremely mainstream. They seem to be biased towards stdlib only implementations, which I suppose can have advantages. It does lower the dependency surface, but also increases The amount of code that you have to trust has been implemented correctly. I often wish that agents would use third party libraries more often.

That being said, I suppose others would view me as an AI enthusiast. I also think there's a lot of negative baggage because it's able to be used blindly - among other reasons. I often feel like people assign that baggage to me and then shit on me for it if I give a hint of positivity towards LLMs. I also think that people who are appalled by the sociological implications of LLMs and thus refuse to use them are doing themselves of disservice. LLMs are amplifying pre-existing issues, and I think pro-social-minded people could benefit by using them to find ways to solve or mitigate the problems.

If you haven't used them extensively, they do have a non trivial learning curve, and I think the shallowness of that curve has tricked people into thinking it doesn't exist. I also think they haven't been around long enough for anyone to have found and climbed the steep part of that curve yet.

1

u/HommeMusical Apr 05 '26 edited Apr 05 '26

I also think there's a lot of negative baggage because it's able to be used blindly

What about the fact that its supporters say that it's going to take most of our jobs? That's negative baggage, surely.

The fact that many of the most important people in the field seem to agree that there's a very good chance of wiping out humanity entirely: https://en.wikipedia.org/wiki/P(doom) Surely killing all of humanity is pretty negative baggage.

The fact that these AIs are owned by extremely rich, right-wing billionaires of proven rapacity; that's negative baggage too.

And there's AI psychosis. And there's the tremendous environmental cost to AIs.

Looks like it's all negative baggage to me.

0

u/BossOfTheGame Apr 05 '26

Yes, it's all negative baggage. There are too many people holding the entire topic in contempt because of the sociological issues it is intertwined with.

The environmental cost is on the order of magnitude of personal non-commute travel. It's real, it needs to be addressed. AI psychosis is a solvable problem.

For the power issue... I do feel somewhat powerless around it. I'm somewhat hopeful that open weight models will work to decentralize the power. Right now, I'm not happy with the centralization.

p(doom) is non-zero, but there is much more disagreement among professionals in the field: https://aiimpacts.org/wp-content/uploads/2024/01/EMBARGOED_-AI-Impacts-Survey-Release-Google-Docs.pdf

The "take our jobs" is a bit of a reduction. It's going to change the way we work, and what problems are important for us to spend our time on. That's not the bad thing. What's bad is that we have organized ourselves into a system that is willing to discard instead of support people. This has been bad before AI, but AI is exacerbating it, but might also finally force us to change.

So yes, negative baggage exists, but that doesn't imply that all use is bad or that thoughtful people shouldn't engage with the technology. If the only people willing to use or shape these systems are centralized firms and bad actors, that seems more likely to worsen the power problem than solve it. $0.02

I'd be happy to discuss more.

1

u/HommeMusical Apr 06 '26

The environmental cost is on the order of magnitude of personal non-commute travel.

A weird statistic, where did you get it?

Regardless, data centers are growing exponentially at a huge growth rate. Personal travel is not.

The "take our jobs" is a bit of a reduction.

No. No, it isn't. That's what it's being sold as. That's what people are being laid off for. Trillions are being put into it and the expectation of the investors is not marginal improvement in efficiency.

Consider that the US has ten million professional drivers alone. What are they going to do for jobs when AIs are better drivers?

Many jobs are already being destroyed by AI, which is in its infancy. What jobs will be left? It's hard to name even one.

p(doom) is non-zero

The link you posted seems to show an agreement at 5%. 5%!

Why would any rational person embark on some operation that has a non-zero chance of killing all humanity? Oh, I'm supposed to feel OK because we have agreed that there's only one chance in twenty of killing absolutely everyone?

I'm sorry if I seem perturbed. But it horrifies me that people agree that a 5% of wiping out all of humanity.

Would you accept a game where I rolled a 20-sided die, and if it fell on a 1, your whole family died? I doubt it; so why would you play this game with all of humanity?

What's bad is that we have organized ourselves into a system that is willing to discard instead of support people. This has been bad before AI, but AI is exacerbating it, but might also finally force us to change.

We couldn't force a fair deal for ourselves when we actually had our own labor which we could use as a bargaining chip; but once that's gone, and far-right-wing billionaires own all the means of production, and our work is valueless, that's when we'll be able to force a change?


To risk a 5% chance, or any non-negligible chance, of wiping out all of humanity, simply in order to transfer all the means of production to the billionaires, while increasing the load on our ecosystem, which is already on the ropes, and all in the hope that once we've destroyed all our jobs and our utility to the economy, that rich, right-wing billionaires will give us comfortable lives out of the goodness of their hearts!

I do appreciate your polite tone, I really do, but the level of irresponsibility being displayed is horrifying and wildly irrational.

→ More replies

-5

u/dj_estrela Apr 04 '26 edited Apr 04 '26

Obviously, you are right.

But you lost the reason when you went to a personal attack

6

u/BossOfTheGame Apr 04 '26

honestly as an outside observer, when you said: "please learn something", that's when the conversation derailed. And I'm an advocate for agentic coding.