- Podcasts
- Python Bytes
- #180 Transactional file IO with Python and safer
#180 Transactional file IO with Python and safer
Python Bytes
- May 8, 2020
- 33 min
- Episode 180
About this episodeTopics covered in this episode: Ubuntu 20.04 is out ! Working with warnings in Python Safer file writer codespell Austin profiler Numbers…
- Ubuntu 20.04 is out !
- Working with warnings in Python
- Safer file writer
- codespell
- Austin profiler
- Numbers in Python
- Extras
- Joke
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
#180 Transactional file IO with Python and safer
Hello and welcome to Python Bytes, where we deliver news and headlines directly to your earbuds. This is episode 180, I can't believe it, recorded April 29th, 2020. It's almost Mayday. I am Brian Okken. And I'm Michael Kennedy. And this episode is brought to you by DigitalOcean, and we'll talk more about them a little later.
Well, I have some very timely news, because not very long ago, a couple days ago, Ubuntu 20.04 is out. What? That's cool, right? Yeah, the new Ubuntu. And it's, why is this big news? Well, there's a lot of releases of Ubuntu and whatnot. But this is the first new LTS long-term support version in two years.
So basically, this is the first real production-grade version of Ubuntu that's been out in two years. So that's a big deal, I think. Oh, yeah, really big deal. And it's got something special in it. It does. It hates legacy Python, but it loves modern Python. So one of the things that's bugged me about 1804, which is what I've been using for production, is it was stuck on 3.6.
Automatic. It is now. f-strings everywhere. That's right. Like, it's time to hit all your code with flint and auto F-string all the things. So I upgraded all of the servers for Python bytes. The servers are pretty small and simple. But if you look at all the stuff that I'm running, there's actually a ton of servers.
And I actually talked about that with Dan Bader on Talk Python episode 215. So people really want to look at what we're doing, what I'm doing here, what we're doing for Python bytes. In terms of infrastructure, they could do that. But upgraded a bunch of servers to 20.04. There's a bunch of stuff that kind of built up cruft.
And I'm like, oh, we could do this way better. Redid all that stuff over the weekend. And so now everything's on the shiny new versions of Python 3.8 and Ubuntu 20.04. And it went really well for me. So that's great. Yeah. And the kernel has been upgraded to 5.4, which adds support for WireGuard, VPNs, better support for like Raspberry Pis and Intel and AMD hardware.
Nice. Yeah. And actually, we've already had the kernel upgraded. The 5.4 kernel upgraded to like a new version. I just had to like apply some patches. So I guess it's pretty active. But here you go. So yeah, different topic. But for our servers, you have to like pay attention to kernel upgrades and stuff?
It says you got a reboot. It'll be like obvious that there's an update when you log in. And then I'm like, oh, I should just run that thing that upgrades all of them. Okay. Once I notice it. Yeah. So pretty much. Yeah. Okay. Neat. Yeah. Okay. Well, I'm going to switch hats. So I want to talk about warnings.
It's a good introduction, but he talks about exceptions and the class hierarchy and printouts and stuff. But if you want to like, if something goes wrong, you kind of want, you've got options of like printing out to the user or throwing an exception, but you also have warnings. And how should you treat those?
And I love what he wrote. He said, most of the time, warnings are aimed at developers rather than users. Warnings in Python are sort of like the service needed light on a car. The user might know that something is wrong, but only a qualified repair person will know what to do. Developers should avoid showing warnings to end users.
But one of the things that the warning system is used for is deprecation warnings. A lot of projects do this where they kind of want to get rid of a feature so they can refactor some stuff and or just doesn't fit in the API very well. So they'll deprecate it and they'll issue a deprecation warning when somebody uses it.
So it's an alert. It doesn't stop working, but it alerts people to that. There's a warning. One of the things I love about warnings is by default, pytest turns on warnings. And so you can see those and you can also make them make pytest so that it fails on warnings. So this is a good thing to pay attention to, but it doesn't stop your project.
It lets us indicate what kind of warning we're sending the user. So we have different types. It's like the exception hierarchy. You can have a warning. There's a warning hierarchy. You can create your own and you can filter on them so that you can screen out stuff that you don't care about, selectively fix things.
Anyway, it's a very powerful system. People should use it when they need it. The article goes on to give specifics on the syntax of how to use them, how to create custom warnings, and how to filter on them. And it's a good intro. Yeah, this looks super interesting and like something I should be paying more attention to than I have so far.
It says, well, this is going to be deprecated and now you've got to do this. I'm like, well, but I'm not doing that. I don't want to see this. But nonetheless, it looks like way simpler than maybe. I just haven't looked at it. It looks great. So I should use this more. One of the cool use cases that I heard recently is using pytest warnings or pytest's knowledge of warnings and testing your system when you're upgrading Python so that you can say, oh, when we're, because Python will deprecate things too.
DigitalOcean is very nice. And DigitalOcean just launched their virtual private cloud or VPC system and new trust platform. Ooh, a trust platform. Together, these make it easier to architect and run serious business applications with even stronger security and confidence. VPC allows you to create multiple private networks for your account or your team instead of having just one private network.
DigitalOcean can auto-generate your private networks, IP address range, or you can specify your own IPs. You can now configure droplets to have, to behave as internet gateways. That's cool. Yeah. It's like your own little baby internet. Yeah. That's neat. And a trust platform is a new microsite that provides one place to get all your security and privacy questions answered and download our available security certifications.
DigitalOcean is your trusted partner in the cloud. Visit pythonbytes.fm/DigitalOcean to get $100 credit for new users to build something awesome. Yeah. We love it. Like I just said at the outset, put Ubuntu 20.04 on there, and it's been working great for so many years. Now, one thing that I ran across, there's a few little libraries that are so simple, and yet when you come across them, you're like, oh yes, this is so cool.
One of those that I go on and on about is unsync, how that unifies all the different APIs that do asynchronous programming, like asyncio, threaded stuff, multiprocessing stuff, and whatnot. Right? So this is one I think that kind of is like that. It's not about unification, but it's about solving a problem in a way that's kind of transparent to the user, but is really, really awesome because it just adds some nice durability to your code.
So there's different levels of like exception handling if you look at it, right? So if you look at code, there's probably like the beginner level that has no try except blocks anywhere in the code. It's just like, I don't know what you call it. Is that optimistic programming? Like I don't need to do error handling.
Like maybe that's another level. I don't know what the making of these levels up a little bit, but even if you are catching an error, something could have gone terribly, terribly wrong and corrupted your data along the way. So there's like durable error handling and there's, it isn't technically crashing at the moment error handling, right?
So the durable error handling, I don't think a lot of people think about nearly as much. So simple example is what you would maybe use a transaction for in a database is like, I'm going to transfer money from this account to that account. But what happens if the transfer to the second account fails?
Yeah. So there's all these ways in which, like you still have a try accept. You still catch it. You still close the file pointer. It doesn't matter. It's corrupted, right? So there's like this another level of error handling of like kind of treating memory and files and whatnot as transaction, transactional type things, right?
File name as file pointer. And then otherwise all your code is identical. Okay. Okay. Here's what it actually does. So as you write to the file pointer, it's writing to a temporary file behind the scenes. And then if, you know, when you exit a width block, the width block, the exit, the dunder exit takes whether or not there was an error on the way out the door.
So it's basically adds a file move, which in an SSD is like nothing, right? Okay. It doesn't matter how big it is. It probably just like updates the, I don't know, like the table in the drive, whatever that means. Yeah. Isn't that cool? That is very cool. I like it. Yeah. So it seems so easy to use.
It looks like something that might be worth looking at. So I'm linking to a couple of things. I'm linking to an article that introduces this. And in the beginning, apparently there was like some edge case where something wasn't working quite right. If you passed like an integer representing a file handle or something funky like that, it didn't deal with that.
Do you have that open? I did. Click on that really quick. Cause I want to talk about a couple of really interesting patterns here. Like if you wanted to study 28 lines of code that took and brought together a bunch of interesting ideas, like, Whoa, this is pretty crazy. So it has a generator expression on an infinite sequence of numbers to find the temporary file, which is pretty interesting because it just says, I'm going to call it dot one, dot two, dot three.
And in case those exist, we're just going to go through all of them until one doesn't. Isn't this crazy? So that's pretty fun. And that uses shutil to copy the file over, which is pretty cool. It uses yield to automatically return the inner file pointer. So when you say with thing as whatever, even though you said safer dot open, it actually yields out the underlying pointer file pointer that came from open.
What's the next one? Okay. I'm on the other tab. So did I distract you? Oh, yeah, you did. And new article, new hat. So code spell. So I got this from Christian Klaus, that silly little project I play with on the side called Cards. I got a pull request against the project to add a pre-commit hook to run code spell.
And I had never heard of code spell. So I was excited to have a new topic for the podcast. Also, just it's as neat. So code spell. What it does is it fixes common misspellings in text files. And specifically, it's designed primarily for checking misspelled words in source code. But it can be used as other files as well.
When Christian applied this to the cards project, it noticed that in one of the documentation files I've got, one of the markdown files, I had spelled arguments with an extra U in the middle of it. And one of the problems with spelling, I mean, it's embarrassing to do and distracting to have spelling errors in your code or your comments or anything.
The GitHub repo has the entire dictionary so you can scan through it. And there's ways to ignore certain words if you're like, no, that's the correct spelling and it keeps doing stuff. You can ignore it. Nice. Well, that's a really good one. The most embarrassing misspelling I've ever done in code was I'd misspelled like a namespace or a class name or package name or something like that.
Yeah. I guess I just wasn't paying attention to like that. I kind of suck at spelling. That was like an extra bad case. Some new person came on the team and said, dude, why is this misspelled all over the place? And I'm like, oh, we got to fix it. But it was like other applications depended on that library and they used the misspelling.
Well, at least you didn't have like both of the spellings be valid symbols in your program and mean completely different things. That's true. Yeah. So one of the things that's awesome about this podcast is we'll say we'll find some random thing or maybe somebody will send it to us and we'll say, oh, did you even know that this was a thing?
I had never heard of this. And then like five other people shoot us a message and say, yeah, and this variation or this other thing. And that's cool. But there's also X, Y and Z. Right. Isn't that awesome? Yeah. Yeah. I learned so much by doing this. Yes, I know. We just got to throw something we vaguely know about and like people will correct us.
To me, Austin is either a guy's name or a town in Texas. I hadn't heard about Austin. Have you? I got a neighbor named Austin. Yeah. I don't think this is the same thing. So, this is like scaling is a frame stack sampler for CPython, meaning it doesn't have like this huge effect of once you run it on your code, it doesn't become 10 times slower as instrumenting.
You know, it just asks like, hey, what are you up to really quickly? So, that's cool. It's nice and fast. It also is just pure C code. There's no real dependencies like other than like the C runtime, which is in all the operating systems. So, it looks at running Python code at intervals and then it dumps out whatever it finds, which is cool.
So, it puts that out, but you can build other tools to analyze that or you could even make like a little player application that replays the execution of your application in like slow motion. Like replays that flame graph over time. Oh, that's neat. Isn't that cool? Yeah. So, now is where it gets really fun because there's a couple of user interfaces on top of this like simple output that can be interpreted.
But the top part of it shows the process information, the CPU it's using, the memory it's using, how long it's been running. And then a graph, an active like interactive flowing graph across the top of like the performance analysis. And then it has something that's a little bit like top maybe, showing you like what it's currently running, how much time it's using.
So, that's the TUI, which is going to be a popular one. But you may also want to be on the web. So, there's WebAustin, which is another example of making this for the web. So, you basically can log into wherever you're running it, connect to it, and it has a D3 flame graph that's like animated of what your web app or whatever process you're watching on that remote system is up to.
So, it's kind of the same thing, but like more visual, more graphical. Like the flame graph is there and whatnot. So, that's pretty cool. People can check that one out. You can even pause it and whatnot. Then finally, there's this other format called SpeedScope, which can be visualized in other tools.
And you can convert Austin output into the SpeedScope JSON format. And there's a sample for that in the repo. If you go look at that, you can load it into the SpeedScope visualizer type of things and have another way to view the data. So, this is really nice because so many of these profilers are like, we collected all this information.
How would you like it as a CSV? Or how would you like it as just like random columns in a terminal? And this is so much like, I would not like it that way. I really like the visualization because it's one thing to gather the information. It's another to go, oh, I see. Right there is actually where it's slow.
Let's go figure that out. Yeah. And the web one, the logo is awesome. It's good. It is really good. Yeah. It's like a 70s thing. It reminds me of Austin Powers. Yeah. A little bit, right? Yeah. Yeah. In a non-copyright infringing way. Anyway, that's it. If people are looking for a profiler, Austin looks pretty cool.
Check it out. Definitely. It seems like it's definitely one of the contenders. Anthony for sending that in. I want to talk about numbers. Does this fit in the screen? Oh, yeah. You got your mathematician hat on now or your wizard hat. I can't decide. I got this from a man. He writes two great stuff.
First name Mosh. Is the last name Zadka? Zadka? Mosh, you got to contact me and find out. Tell me how to pronounce your name. But numbers in Python. Really great article. In Python, you don't really have to think about numbers too much. They just sort of work. Yeah. But you do kind of need to think about them.
I never use the two slashes because that seems wrong. Anyway, so the implications are weird though. And the other thing, okay, so you got integers, they turn into floats if you divide them. You got floats, which are things with decimal points in them. They're not the only things with decimal points in them though.
Right. It looks right and it is so wrong. And I think just our training for so many years in theoretical mathematics means that it's hard to look at that and go, that's wrong. Yeah. Well, it's interesting that when you see it in numbers, you can, like, for instance, one of the examples is one plus two minus two minus one is zero.
So there's a decimals library that it's probably not surprising. One of the reasons it's in there is for financial transactions. They're set up to be correct with precision and do the right thing. And so I'm really glad it's there. Otherwise, we'd have like competing decimal third-party libraries or something like that.
And we probably do. But this one's built in. I'm glad the article was written, though, because something weird about decimals that I didn't know about was there's a global state variable called context that holds the precision that's being used for decimal division and stuff. It could be anywhere in your program that the precision gets changed.
Yeah. Or, I don't know, maybe there should be a minimum precision. This is interesting, though. Like, I didn't realize that you could even change the precision of decimals. So, like, in the docs it says, unlike hardware-based binary floating point numbers, the decimal module has a user-alterable precision defaulting to 28 places, which can be as large as needed for a given problem.
So, yeah, you can change it. The example in the Python document on the docs show, just globally changing it halfway through a calculation, which seems like a bad, let's kick them down the stairs instead of teaching them to hold on to the handrailing. But this is really cool, like, this local context, change it, you can set it really high.
That's cool. I had no idea that you could actually change that to grow as you need it, which is cool. Yeah, I guess you could still use the global context as long as you, maybe this isn't safe, but as long as you always remember to set it before you do decimal arithmetic. It's safe as long as you're not doing threading.
Oh, yeah. Okay. Yeah, because what if somebody, some other thread has the same idea and changes it? I think the idea is maybe, it seems to me like possibly it would be better if once set, it couldn't be set again. Like, you could set it at the beginning of your program, but it couldn't be altered and altered and altered.
Yeah. Like, right, something along those lines, like, okay, we set it, we're done, it's an exception if you try to set it again to something else and so on. I mean, it probably is a convenience. Yeah, this is the whole world I didn't even know about. This is cool. Yeah, I would probably set up some sort of, like, hook or something to make sure that you're only setting it one place if you're doing that.
Yeah. I don't know. Yeah, sounds good to me. Cool. Anyway, yeah, this is actually more interesting than I thought because, like, as usual, I've learned something, which is cool. Last thing on this, we're going to link to the standard library documentation for fractions and decimals because you may not have heard of them.
And it is available. And it has the peg parser that we talked with, I think, last week about a little bit. Yeah. Yeah. We was here and talked about that was really cool. The work he's been doing there. That's a big long-term upgrade, right? That's something that got written in the original version of Python.
It was unchanged and obviously can be better, right? Basically, the syntax was limited by the parser and how much it looked ahead and stuff. And so this should open up the language for more complex concepts or make it easier to add concepts to it. Yeah. All right. So I see we have some competing jokes here.
You want to go first? Yeah. I just put a call out on Twitter and said I need some more jokes. And boy, I got a whole bunch of great ones back. I'm going to pick one. This one's from James Zabel. If you put 1,000 monkeys at 1,000 computers, eventually one will write a Python program and the rest will write Perl.
That's right. I think maybe like 950 of them will write Perl. A couple of them are just going to be writing regular expressions like all on their own. Yeah. That's true. All right. I have one that's maybe in a similar vein here. So, you know, like we talked about Austin. It has all these different user interfaces and it's very user friendly.
All right. Fun as always. Great to be here with you. Bye. Bye. Thank you for listening to Python Bytes. Follow the show on Twitter at Python Bytes. That's Python Bytes as in B-Y-T-E-S. And get the full show notes at Python Bytes.fm. If you have a news item you want featured, just visit Python Bytes.fm and send it our way.
