- Podcasts
- Python Bytes
- #115 Dataclass CSV reader and Nina drops by
#115 Dataclass CSV reader and Nina drops by
Python Bytes
- February 2, 2019
- 29 min
- Episode 115
About this episodeTopics covered in this episode: [play:1:03] Great Expectations [play:5:01] Using CircuitPython and MicroPython to write Python for…
- [play:1:03] Great Expectations
- [play:5:01] Using CircuitPython and MicroPython to write Python for wearable electronics and embedded platforms
- [play:11:58] How to Rock Python Packaging with Poetry and Briefcase
- [play:14:59] awesome-python-security 🕶🐍🔐 , a collection of tools, techniques, and resources to make your Python more secure
- [play:18:20] pydbg
- Extras
- Joke
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
#115 Dataclass CSV reader and Nina drops by
Hey, everyone. I'm very proud of you for pronouncing my last name correctly. Thank you. Brian and I, we specialize in mangling people's last names on this show, but we try. This time you nailed it. Thank you. For those who don't know me, I'm a senior cloud developer advocate at Microsoft, and my focus is on Python. You can find me on Twitter at NNJA. That's like ninja, but without the I.
Yeah, that's a pretty cool Twitter handle. Awesome. And also, this episode is brought to you by Datadog. Check them out at pythonbytes.fm/Datadog. Tell you more about that. You know, Brian, I expect quite a bit of stuff out of Datadog. I would say I have great expectations when I go use their stuff. What do you think? How about you?
Yeah, yeah. I do too. Yes. For our first item, we have great expectations. And it is a package that was shared by a listener. And I don't know the listener because they shared it with you, Michael, and you didn't tell me who it was. I just sent you over the link and I forgot. But thank you for sending it in. Next time, we'll do a better job.
Well, we're just also trying to make it so that sharing information with everybody else is not an ego-boosting exercise because we won't remember your name. Or we'll mispronounce it. Unless it's important to you, then let us know. Yeah. Yeah. No, great expectations. It's kind of cool. It's this idea that we have a lot of tools out, for instance, pytest, to test your code. But there's in a lot of stuff, the data that you're running your code through, like in data science or a lot of data pipelines, the data is important too.
And being able to check to make sure your data fits what you expect it to fit, what look like, is important. So these are some really cool expect calls. They're a bunch of functions that start with expect. They aren't assertions, so they aren't going to throw an exception. But what happens is you, like maybe on some of them, you pass them a data frame.
And it has like a valid, and you can say whether or not, you know, if it failed or passed the test. But it also shows you the parts where it didn't. So this is, there's a little video, actually, demo video that they have, where you can see it in action using a Jupyter notebook. And it's kind of cool. It shows you exactly where it's failing.
So I imagine doing this interactively to look at your data would be helpful. But also you could probably put this in place in some data cleaning steps to make sure things are around. Like making sure there aren't any nulls in a column. There's a whole bunch of different things you can assert on or expect on your data.
It's pretty fun. Yeah, it's pretty cool. Some of them are totally straightforward. Like expect these values to be in the set. Others, a little more data science, mathematical focused, like expect the chi-squared test p-value to be greater than such and such, right? I honestly haven't done a lot of chi-squared lately, but I don't do that much data science, you know, on the web.
It's more we use addition and like stuff like that. Yeah, but some of the fun things, like it was in the video example, he expected some data from a column to be in a particular set. And it was either male or female. And first off, like how binary of this. But anyway, the males, a lot of them came back with spaces in them.
So doing some of this exploratory thing might tell you where you need to add some cleaning steps or dealing with nulls or things like that. The follow-up, I'd like to hear how people might be able to use this, how it's used exploratory-wise. But how, I'd like to see somebody using it in their pipeline stages and how that works.
Whether you, I guess I imagine if you had it in production, you'd have some code that would, if it failed an expectation, you'd write a log entry or something? Or I don't know what you'd do. Yeah, or maybe return, if you were accepting data over a web method, right? Somebody's doing ETL and they're like, here, we're submitting this new set of data.
You could return like 400 bad requests to indicate, no, no, something's wrong with the data you sent me. Things like that, possible. Yeah. You know, what do you think about this? I think it's very cool and is probably going to be pretty helpful. I like it as well. What I like about it is it lets you take what would be a little algorithm you'd have to write to, say, go through and test all, say, the chi-square values and then compare them and then assert on that.
Now you just do one line and it does it on the whole data set, the whole data frame. That's pretty sweet. Yeah, I'm going to be watching the video demo after the show. Yeah, right on. So the thing that you are going to cover next, the timing is incredible, Nina. I literally got a notification that I had ordered one of these and it shipped today and it's on its way to my house.
So I'm very excited. Tell people what is on its way. Would that be the Circuit Playground Express? It would be. Yeah. Awesome. Yeah. So I wanted to chat a little bit about using CircuitPython and MicroPython to write Python for wearable electronics and embedded platforms. I've been playing with electronics projects as a hobby for probably about the past two years now.
Okay. So what I know I've heard of MicroPython. And I think I've heard of CircuitPython. Are they the same? Are they different? Tell everyone about it. Yeah, there's definitely a little bit of confusion about that. So MicroPython is the original. It's a lean and efficient implementation of Python 3 that can run on these tiny little microcontrollers.
Yeah. Yeah. And some of these, the devices that Adafruit sells, they're as small as a quarter. That would be the trinket. But of course, my favorite Python hardware platform for beginners is that the Adafruit Circuit Playground Express. It has everything you need to get started with programming hardware without even needing to learn how to solder.
All you need is some alligator clips for the conductive pads. And the board has a ring of neopixel LEDs. It has buttons, switches, temperature sensors, motion detectors, sound sensors, a tiny little speaker, and a lot more stuff. Like you can even use it to control servos, which are those tiny little motor arms.
Wow. That's really awesome. Yeah. It's a tiny little thing. Yeah. What I really like about this and the reason that I ordered it was I can go to Adafruit and look around and I'm just like, this is too low level for me. I don't really know what I need. I don't even know if I have a power supply, if I get this little chip or that.
Like how to put together. I'm like, okay, give me like one thing that has all the stuff to do little projects like you're describing. And I think it's awesome. And doing it in Python is super cool. It only costs $25. Yeah. It's not too expensive. No. And then if you don't want to use Python to program this, there's a tool that you can use called Microsoft MakeCode.
And it lets you program these little devices with a drag and drop style kind of scratch like interface. So that's perfect for kids. And you'll find a lot of examples on their site. Oh, that's awesome that you point that out because I might get my daughter to do some kind of little game, you know, like a Simon Says or something with the LEDs.
Who knows? That'd be fun. Yeah. The Mu editor has some tutorials on using these as well. Right. That's awesome. Super cool. All right. And I like you threw in the link. Yeah. To your code. So people can do the earrings as well. Yeah. And like you said earlier, there are tons of guides for Python projects on the Adafruit website.
Stuff from making your own synthesizers to jewelry to silly little robots. So definitely make sure to check that out. Cool. Yeah. Well, I can almost recommend it. It looks really, really good. And when I get it, I'm going to play with it and let everyone know what I think. But yeah, it seems like a great little package that you can get.
25 bucks. You know, it's like, you know, as software developers, you could just try it, right? It's not that big of a risk. It's not like you're getting a new MacBook or something. Mm-hmm. And Adafruit has a Python for microcontrollers mailing list. And they always drop kind of hot news and interesting new things.
So there's a link to sign up for that in the show notes. Awesome. That's a great one. So this next one, I think, is a really interesting use of Python 3.7. So when you think of the main features of Python 3.7, certainly data classes has to be one of them. Have either of you found any use for data classes yet?
You import CSV and then you create a dict reader based on a file stream. And it'll read the header and figure out the columns. And then it gives you little dictionaries based on the column names. And that's pretty sweet. But what you get back is a whole bunch of strings corresponding to those values.
Right? And with this, what you can do is you can actually define a data class that maps the schema of your CSV file. Right? So I could define a data class that maybe has an ID and a name and a value or price. Right? Maybe it's like products. So like the ID could be an integer defined in the data class.
Yeah. So you can just define these things. You get autocomplete in, you know, PyCharm or Visual Studio Code for your types, for your rows, because they come back as these data classes. And you get validation. And on top of that, you can actually do cool stuff. Like you can say, with data classes, you can specify either just the type or the type and a value.
And that value becomes the default value. So if like only sometimes the price is there, you could put zero or minus one. And it'll just go through and substitute that value. So a lot of cool little nice touches here. Nice. Yeah. I'm definitely going to check this out the next time that I have to do some sort of CSV parsing because it's way better to let this thing do the validation and the type conversion and all that and just, you know, not worry.
So before we get to our next item, let me tell you about them. This show is brought to you by Datadog. They're a cloud scale monitoring platform that brings like metrics and logs, distributed traces all together. You can do auto instrumenting of frameworks like Django and Flask and PostgreSQL, which means you can track requests across service boundaries, across machines, things like that, which is awesome.
There's a fun article called How to Rock Python Packaging with Poetry in Briefcase. Plus, it has the phrase how to rock something, and I'm a sucker for that. It's actually kind of a nice tutorial on packaging. So for those of you that just joined us and haven't learned all of our discussions on packaging, this is a nice introduction to packaging and how it fits in Python and also kind of how the changing of things, changing of packaging, like where Flit, Pipenv, and Poetry sort of fit in with all of this.
It's kind of a nice run through of that. Poetry is one of those things for packaging, and it uses the pyproject.toml file. And of course, we talked about poetry in episode 100. And the nitty-gritty details of pyproject.toml was in testing code 52 with Brett Cannon. But the neat thing, one of the neat things why I picked this is it also talks about Briefcase.
And we haven't talked about Briefcase yet. And Briefcase is one of those tools from Py, the Beware project. And it's something that you can, it packages your, a Python application as a standalone native application for lots of stuff. It claims Mac, Windows, Linux, even iOS and Android, which is interesting.
And Briefcase is part of that whole set of small independent tools from Beware, which also is pretty cool. And it's nice to see them working together. Also, we got Cookie Cutter doing some magic in here as well. Definitely. And then one of the things that I like at the end of this, there's several tutorials on publishing, like how to push your new package to PyPI.
Of course. Yeah, of course. Why don't more of them do that? I don't know. But please, everybody, like save the world and push it here first. Yeah. The packages on PyPI cannot be changed when they're uploaded. You can only add newer ones. So although you've got to admire the rate at which you'll increase your version of your package if you screw it up a few times trying to publish it.
Yeah. That's productivity, right? That's right. So Nina, this next one that you found for us is one of these awesome lists. And I think these awesome lists are coming along really faster and faster these days, right? We've got awesome Python, awesome Python applications. What's the next one that's in that category?
And this is a new one I came across called Awesome Python Security. It's a collection of tools, techniques, and resources to make your Python more secure. Oh, that's cool. Yeah. And it's got a lot of stuff for web apps like the secure.py, which is awesome. We've covered that. And the Flask, Flask Talesman, Django sessions, all kinds of stuff, right?
But not just the web. There's a whole bunch of other ones. I think, and hopefully all of you agree, that all of your production and client-facing code should be written with security in mind. And this list features a few resources that I'd come across before, like Anthony Shaw's excellent 10 common security gotchas article that highlights problems like input ejection and depending on assert statements in production.
Whoopsie. Oops. Oopsie doops. We'll just check it in again without that in there. I'm sure it'll be fine. There's no history. And something I really like about this list in particular is it also includes resources for learning about security concepts like cryptography. Yeah. You know, out of that cryptography section, they listed one of my all-time favorite packages, which is PassLib.
So if you're going to store user secrets and you want to hash them, like passwords are probably the most common, but there could be other things as well that you don't want to store directly, but you want to accept from user and see if you have it. All right. Like you can hash it and that's a good idea.
Yep. To make sure your password doesn't end up on have I been pwned. Exactly. Exactly. So basically you can say, I want it to take, you know, 0.2 seconds to determine, to brute force or to check each version of the password. And it automatically, because of that, will slow down dictionary attacks against your site because you can only do them point, you know, it'd take only five per second, right?
So, and then there's another one called let's be bad guys. That's interesting. So yeah, a lot of cool stuff here. It's a great project name. I know. It's like a hacker playground. Yeah. So check out the full list on GitHub. And then if there's something missing that you think should be there, maybe open a pull request.
It looks pretty interesting to me, but the basic idea of this DBG macro is instead of just printing out, like I'm here, I'm here, the value is, you know, printing X as the value, it'll actually give you a higher level statement without doing more work. So if you're trying to debug something through print statements and that kind of thing, this makes it a lot easier.
So you can go and say, like, I have a equals two, b equals three. If I could say DBG of a plus b, the output is the file of the line a plus b equals five. Things like that. Really, really nice. It sort of shows you in your message where you are in the file, what thing is you're actually printing without having to like come up with elaborate print statements.
Like I don't primarily use print statements for debugging, but sometimes I do when I'm kind of exploring that I want it to run, but I kind of want to see what's happening. I'm like, Oh, what am I getting back from that API? What is this value? Things like that. Have either of you used watch statements?
I usually do those in a graphical debugger. Yeah, me too. Yeah, I definitely have used those. I was, I was thinking something different, but so what, where, where I'm going to use this DBG, PyDBG thing is a lot of times I've got, we've got test code that generates huge amounts of data, like trace data.
This is just here till I figure out what's going on. And then we're going to stop this. Yeah. But cool. People can check it out. Thanks for sending that in to our listeners. All right. So I guess that's it for all of our main topics, but looking at our show notes here, Brian, we all like kind of had a second round in the extra.
So maybe we'll do like a lightning round one more time. What do you got for us? This is just a quickie that pytest has temporary directories and temperature factory fixtures for dealing with temporary files. But they've added, as of pytest 3.9, there's path versions that return pathlib path objects.
And those are just quite fun. And I'll drop a link in the show notes. Okay. That's great. So I want to bring your attention to something, let's say, non-standard in terms of conference presentations. So this is something at PyCon US. So in May, Cleveland, 2019, there's a project called The Art of Python, which is a miniature arts festival focusing on narrative performance and visual art around programming and Python.
And basically showcase novel art that helps us share our emotionally charged programming experiences, particularly to do with Python. So it's like five to 20 minute presentations in a separate little track. And the call for papers are open. So if you've always been a theater fan and you program, here you go.
So everyone should go out there and represent for Python and fill out the 2019 Stack Overflow survey. So that's good. And then finally, this gets a little bit back to your pick, Nina. NumPy is awaiting a fix for a critical remote, remote code execution bug. That's bad. That doesn't sound super good.
Oh, boy. Yeah, yeah, that's not so, that's not so good. So this goes up to at least version 110 through 116, which at least is January 14th. This hadn't been fixed. So hopefully it's been fixed. But more importantly, if you're using NumPy and you're accepting user input through the load function, you want to upgrade and you want to be a little careful around that.
But the problem is if you use like Google Drive, what ends up on your hard drive is like a hyperlink back to the actual sheet, right? So how do you back that stuff up? It turns out if you go to takeout.google.com slash settings slash takeout, that's a lot of repetition. Anyway, you go there, you can say, give me all my document format, all my documents, and it'll give them to you in Microsoft Office format.
Like it'll convert the sheets to Excel, it'll convert the docs to Word docs, and then let you download them so you have like a sort of permanent version. Anyway, I thought that was cool and people might find that useful too. Cool. Yeah. All right, Nina, you got some as well. You're teaching a class that looks really interesting.
And it has an in-person component if you happen to be, where is it, Minneapolis? In Minneapolis, that's right. You can come to the class. Will Minneapolis be thought out by then? The class size is about 20 people. That I cannot promise. Hopefully in March it's a little warmer. Yeah, so up to 20 people could drop in in person.
That'd be really cool. The next thing, I recently recorded a series of videos with Carlton Gibson. He's a Django maintainer, maintains a lot of other projects on developing Django web apps with VS Code, deploying them to Azure with just a few clicks, setting up continuous integration and continuous delivery, as well as creating serverless applications.
You can watch that video series at aka.ms.com.au. Python-virus.com.au. Check that out, too. Yeah, it's great. We got to film in the Microsoft Channel 9 studio. And it's a very well-done series. All the bright lights and everything, huh? Not just screencasts. That's cool. Yeah, we feel like newscasters.
I'm also planning on being a mentor at a brand new hatchery event at PyCon US 2019. That's going to be mentored sprints for diverse beginners organized by Tanya Allard. The goal is to help underrepresented folks at PyCon contribute to open source in a supportive environment. The details aren't out yet, but I dropped a link to where they'll be when they're finalized.
Oh, that's super cool. And there's also things like scholarships or something like that to help folks get actually physically to the event if they need some help as well, right? They can apply for that at the PyCon site. Yeah, PyCon US offers a lot of financial aid. Lastly, if you're interested in Python for hardware, like we talked about earlier, you can catch my talk about electronics projects in Python with LEDs at PyCascades in Seattle on February 24th.
Currently, tickets for that are still on sale. Excellent. Yeah, and Brian and I are definitely going to be there. We're all going to PyCascades, so we're going to catch it. Hopefully, everyone else does as well. Great. I'm excited to see you there. And I do have one last thing to sneak in, and that is if you haven't tried the Python extension for VS Code yet, now is a really good time.
The December release included some really killer features like remote Jupyter support and exporting Python files as Jupyter notebooks. And if you're interested in keeping up with future releases, I dropped a link to the Python at Microsoft blog. Nice. And didn't you as a group, I'm speaking to you as a Microsoft, as VS Code, didn't you guys just release like an AI-powered autocomplete backend for Python as well?
Follow the show on Twitter via at Python Bytes. That's Python Bytes as in B-Y-T-E-S. And get the full show notes at pythonbytes.fm. If you have a news item you want featured, just visit pythonbytes.fm and send it our way. We're always on the lookout for sharing something cool. On behalf of myself and Brian Okken, this is Michael Kennedy.
