Latest / Talk Python To Me / #227: Maintainable data science: Tips for non-developers
Transcript
- 0:00Did you come to software development outside the traditional computer science path?
- 0:04This is common, and it's even how I got into programming myself.
- 0:07I think it's especially true for data scientists and folks doing scientific computing.
- 0:12That's why I'm thrilled to bring you an episode with Daniel Chen
- 0:15about maintainable data science tips and techniques.
- 0:18This is Talk Python To Me, episode 227, recorded August 6th, 2019.
- 0:23Welcome to Talk Python To Me, a weekly podcast on Python,
- 0:39the language, the libraries, the ecosystem, and the personalities.
- 0:43This is your host, Michael Kennedy.
- 0:44Follow me on Twitter, where I'm @mkennedy.
- 0:47Keep up with the show and listen to past episodes at talkpython.fm,
- 0:50and follow the show on Twitter via at Talk Python.
- 0:54This episode is sponsored by Indeed and Rollbar.
- 0:56Please check out what they're offering during their segments.
- 0:59It really helps support the show.
- 1:00Dan, welcome to Talk Python To Me.
- 1:03Hi, Mike. Nice to meet you.
- 1:04It's great to meet you as well.
- 1:05I'm so glad that we got a chance to run into each other at PyCon this year
- 1:09and learn about what you're up to because we're going to have a good time talking about it.
- 1:13Yeah, and this year was the first year I was at PyCon,
- 1:16and I typically live in the data science world,
- 1:18so it was one super cool to be at pretty much a convention of Python users.
- 1:23And I almost forgotten how Python is outside of data science.
- 1:29Django is a thing.
- 1:31It was a thing that was repeated back to me.
- 1:35Yeah, exactly.
- 1:36That's pretty interesting.
- 1:37What was your take on it?
- 1:39Do you recommend people, especially data scientists, attend PyCon?
- 1:43You're happy you went?
- 1:44Yeah.
- 1:44I mean, it was super cool.
- 1:46I mean, data science is sort of one of the growing parts of Python as a language.
- 1:51And I think a lot of people have said it's sort of the reason why Python has picked up in popularity recently.
- 1:58And so, yeah, it was super cool just to see all the booths.
- 2:02I personally gave a pandas tutorial there, so it is becoming more and more of a thing.
- 2:08And I think there were two or at least three pandas-related tutorials during the session.
- 2:15Yeah, I know Kevin Markham gave one as well, I'm pretty sure.
- 2:20At least something on data science there.
- 2:22So, yeah, there was definitely some interest.
- 2:23I think I met one other person who's doing one.
- 2:25So, yeah, it's pretty incredible, right?
- 2:27Yeah, yeah.
- 2:28And then, again, there's this whole web stack of things that I almost never really use,
- 2:35but a lot of people do use it as well.
- 2:38So, it's super cool just to see it and be reminded what Python can do as a language.
- 2:45Yeah, that's cool.
- 2:45And for me, it's exactly the opposite, right?
- 2:47Like, I spend a lot of my days writing web apps and APIs and things.
- 2:50And then to see the data science stuff, it really reminds me, like,
- 2:53there's a really different way to work and other things to optimize than, you know, scalable web apps.
- 2:59Yeah, yeah.
- 3:00For sure.
- 3:00So, totally recommended to go.
- 3:02And, like, you know, there was, like, some talk on Twitter, like,
- 3:05don't always try to do the hallway track if you can, because, like, speakers,
- 3:10sometimes speakers would like people in their audience.
- 3:12But I tried to go to the talks that I can.
- 3:16But then there were a few, like, education-related hallway track groupings or meetups.
- 3:21And that's what I attended to.
- 3:23So, it was just nice seeing other, like, Python educators,
- 3:26which I also went to SciPy a couple weeks later.
- 3:30So, it was some of the people I saw again for the second time.
- 3:32And I was like, oh, cool.
- 3:33Yeah, I definitely love going to Python.
- 3:35As people know, I talk about it all the time.
- 3:37And it's a really great experience.
- 3:39And what I think is interesting is a lot of people feel like they have to be experts to go.
- 3:42I met a lot of people who are fairly beginner in their career.
- 3:45And it was really valuable to them to be there.
- 3:47So, I just want to throw that out there for people.
- 3:49Yeah, super welcoming.
- 3:50I mean, that's sort of the reason why I stuck around with Python.
- 3:54I'm also, like, pretty active in the R community as well.
- 3:57And between Python and R, a lot of people join for whatever reason.
- 4:01But again, like, the saying, as the saying goes, like, they stay because of the community.
- 4:05And everyone's, like, just super nice and helpful and super beginner-friendly.
- 4:09Absolutely, absolutely.
- 4:10All right, well, I haven't got a chance to ask you the opening questions.
- 4:13Let's start there.
- 4:14So, before we get into all the techniques and tips and stuff you have for data scientists
- 4:21to bring in more structured programming stuff to make their data science techniques and tools better,
- 4:27let's talk about you.
- 4:29How did you get into programming in Python?
- 4:31So, I was pretty much always surrounded by computers as a kid.
- 4:34I always had, like, the hand-me-down computer when I was a kid from, like, my parents when they were working.
- 4:41I guess it sort of does help that my dad is a software engineer.
- 4:45But it wasn't really, like, a thing when I was at home.
- 4:47Other than, like, hey, dad does things on computers.
- 4:49That's kind of cool.
- 4:51I was always tinkering around with computers.
- 4:53So, like, I do remember, like, the first thing I would do every time, like, I open a new app is, like,
- 4:57hey, let's go to edit and preferences and just see, like, what I can change.
- 5:00And it was sort of just tinkering.
- 5:01I grew up in New York City.
- 5:03I'm from Queens.
- 5:04And I went to one of the specialized math and science high schools in New York City.
- 5:10And so, for us, sophomore year, it was actually mandated that every student take one semester of computer science and one semester of technical drawing or drafting.
- 5:20That's pretty cool.
- 5:21I think drafting is less valuable than people imagined it.
- 5:24Because I remember I had a drafting class as well and I don't really see it.
- 5:28But the software thinking and tools and ideas are certainly is.
- 5:32What language was that in?
- 5:34Pen and paper.
- 5:35And then CAD towards the end.
- 5:36Yeah.
- 5:37So, yeah, we were, we were, like, in, like, a room and we were drawing, like, isometrics by pen or pencil and ruler.
- 5:44Those big slanted tables.
- 5:46And the programming one, what was that?
- 5:48What technologies did you all cover?
- 5:49It was, like, it was only towards the end.
- 5:51And it was, like, in some CAD program that I don't remember.
- 5:54Oh, okay.
- 5:55Yeah.
- 5:55And it's, like, interesting.
- 5:56Like, that was a thing I never, I thought it was super cool.
- 5:59And then, like, now that 3D printing is a thing, it's sort of, like, oh, wait, I used to kind of, I've done this once.
- 6:05But, like, I just haven't done it in many years.
- 6:07So, it's, like, kind of interesting.
- 6:09Yeah.
- 6:09How interesting.
- 6:10Yeah.
- 6:11Yeah.
- 6:11How cool.
- 6:11Well, that's a great introduction.
- 6:13And then did you study computer science in college?
- 6:16So, I didn't.
- 6:17And that was part of it was I didn't notice until I was in college.
- 6:21But when we had to take computer science in high school, it was sort of, man, all of these other people.
- 6:27And by, like, other people, it's, like, just a handful.
- 6:29It was, like, man, they're really good at this.
- 6:31There's no way I'm going to be able to study this in the future or, like, for a career and whatnot.
- 6:37We did the one semester of computer science.
- 6:39I didn't go for the AP or anything because, like, originally I was going to, like, go down and be a medical doctor.
- 6:46That was my original plan.
- 6:47So, then in college, I ended up, how do I make my, like, medical education as strong as possible?
- 6:53Let's do, like, neuroscience and sort of, like, a bio-heavy program.
- 6:57But, like, that's where I sort of took my first set of statistics courses.
- 7:02And I was, oh, yeah, like, we hear about mean and standard deviation.
- 7:05But to finally understand it in the context of, oh, yeah, here's the exam scores for the previous exam.
- 7:13Like, how do you actually rank?
- 7:15Just something like that, like, get some meaningful understanding of, like, where do you rank in the class?
- 7:20And it's, like, oh, maybe this is how the curve is going to be.
- 7:22Or, like, did I didn't do very well in the exam, but, like, I'm actually kind of okay.
- 7:26So, that was cool.
- 7:28And then because I ended up switching into neuroscience my second year, I had to stay, like, a fifth year in college.
- 7:36And so, like, my last two years was, like, oh, you only need four classes to do, like, a computer science minor.
- 7:40So, I was, like, yeah, I've done this in high school.
- 7:42Let's, like, just pick this up for fun.
- 7:44And then so, it was that first intro computer science class when we got to, like, the actual Python programming portion where I was, like, wait, this is actually not as terrible.
- 7:55And I would see the other students, like, in which case, like, they would be freshmen and I would have been, like, already a junior.
- 8:00And I would see the freshmen, like, they've never seen this before.
- 8:03Their struggles were essentially my struggles back in high school.
- 8:06And then I realized, oh, it's literally because, like, I saw it before.
- 8:10And, like, even though, like, not much of it got retained, it was just thinking about things procedurally, just doing it once.
- 8:17Now I can actually think about, like, syntax errors versus, like, doing everything at once.
- 8:22That's sort of when I was, like, huh, maybe I could have done this as, like, a career choice.
- 8:25But, nope, nope.
- 8:28Let's keep going down the medicine route.
- 8:30So, I ended up doing a master's in public health and epidemiology just to stack on more research skills.
- 8:37The thought being was, hey, research in medicine was super cool, but I'm pretty sure if I ever start medical school, I'm never going to learn this stuff again.
- 8:44So, let's just learn everything and then go to medical school.
- 8:47So, I did my master's in epidemiology, and that's when I took my first, like, intro to data science course.
- 8:53And that is probably the most life-changing moment in my life.
- 8:58When I was doing my master's, I was already just learning about all of these other basic statistical techniques.
- 9:03I've never heard of logistic regression before.
- 9:05And that's, like, the type of analysis you do when you have a binary outcome.
- 9:09So, for us, it was like, did this person die?
- 9:10Yes or no.
- 9:11Or did this person get cancer?
- 9:13Yes or no.
- 9:14And I've never seen that before.
- 9:15And it was just like, wow, this is amazing.
- 9:17And then I take my data science class and I was like, what is this random forest thing?
- 9:21This is amazing.
- 9:22Or, like, what is this, like, ridge and lasso regression?
- 9:25And, like, I can just, like, condense, like, thousands of variables into, like, something meaningful.
- 9:30Like, that's super cool.
- 9:32And so, that sort of started this whole trajectory down to where I am now.
- 9:36Because it wasn't until, like, that data science course I, during that semester, because there was so much learning to do, the instructors set up a software carpentry workshop.
- 9:46And so, I was an attendee for software carpentry.
- 9:49I think software carpentry is a really cool project for folks with the background exactly like you described.
- 9:54I actually had Jonah Duckles on the show way back in episode 93 talking about software carpentry.
- 10:01So, it's been a really long time since I've spoken about it.
- 10:03Maybe just tell the listeners out there what software carpentry workshop is about.
- 10:08Because it'd be good for a lot of folks who are in the data science and sort of science in the programming space.
- 10:13So, yeah, it's sort of expanded over the past, like, couple years.
- 10:18But software carpentry and their sister program, Data Carpentry, they're housed under this one umbrella called the Carpentries.
- 10:25And, essentially, they're this nonprofit organization.
- 10:28And their goal is simply to teach researchers or scientists the skills that they need for, in the sense of software carpentry, like, programming skills.
- 10:37And then, in the case of Data Carpentry, like, working with data.
- 10:40So, like, data skills.
- 10:42And the two really just go hand in hand.
- 10:43So, you'll mix and match.
- 10:45They have a lot of overlap.
- 10:46And, essentially, there's these two-day workshops where they cover Bash for the shell.
- 10:52And the whole premise of that is to, like, show you about, like, what is a working directory?
- 10:56And programs do one thing and one thing really well.
- 11:00And you can pipe them into one another to chain things together.
- 11:03So, that's, like, what you're supposed to take away from Bash.
- 11:06And then they go through Git for version control, which it's really hard to get an understanding of Git in three hours.
- 11:12But it's just to show you that, like, there are better ways than naming your files final, final, final, et cetera, et cetera.
- 11:19Putting the date on the end.
- 11:21No, like, really, final.
- 11:23Yeah, and putting the date.
- 11:24And then there's a section on Python or some of, like, R or any of the other programming languages.
- 11:29And it used to be that they also had a fourth section on SQL.
- 11:34But then, usually, SQL gets bumped out for, like, a longer Python or R session.
- 11:40So, it's a two-day workshop that covers those skills.
- 11:43And it's really to give, like, researchers a primer because we go into science not thinking that we're going to program.
- 11:51And so, like, a lot of this stuff is just like, oh, I picked it up on my own.
- 11:55And it's just a bunch of stuff hobbled together.
- 11:58And that's how we learned it.
- 11:59And actually, that's how, like, a lot of people in data science, like, that's how they learn programming.
- 12:04And then this is, like, the first time, like.
- 12:05I feel like, yeah, I feel like this is actually really common, as you're saying.
- 12:09And I think it's also a little bit why Python is growing a lot in the data science space.
- 12:15Is it's, like, what can I do that's an easy step to do just enough computation to solve my problem so I can go back to what I actually care about?
- 12:22Because I don't want to be a programmer.
- 12:23I want to be a biologist or a doctor or whatever.
- 12:26But then you slowly find yourself six months later with, like, a lot of scripts.
- 12:31And you're running code and you're using pandas or NumPy.
- 12:35And you're like, well, I have no qualification for this.
- 12:38But here I am, like, in it somehow, even though I swore I would never do this because I hated math or something like that, right?
- 12:43Yeah.
- 12:43So that's the whole premise of the carpentries is, like, okay, let's take one step back.
- 12:46You learn how to do this on your own.
- 12:48And let's, like, refresh, like, the actual basics and, like, kind of, like, steer you in the correct way.
- 12:53That's the general lowdown of what the carpentries are.
- 12:57That's cool.
- 12:57And you started as a student, but you became an instructor, right?
- 13:00I was a student, like, fall of 2013.
- 13:03And then it was, like, just at the cusp of, wait, I can actually teach this stuff.
- 13:08It wasn't, like, that much a leap and bounce.
- 13:11Like, I already knew a little bit about Python programming.
- 13:13So, and then the Bash stuff.
- 13:15I was, like, one of those people in college that was, like, I'm just going to install Linux and see what happens.
- 13:20Deal with, like, problems that come from that.
- 13:22I've been saying to myself, like, it's the year of the Linux desktop since, like, 2010 or something.
- 13:29It's almost here.
- 13:30It's almost here.
- 13:31So I ended up signing up to, like, go help out.
- 13:35You end up realizing that, like, for a lot of newcomers, a lot of the problems that they have aren't actually that complicated.
- 13:42And then just to go into, like, education theory a little bit, it's, they don't have a lot of nodes to make connections with.
- 13:49And so a lot of their problems is also, like, just they'd made a typing mistake, right?
- 13:53Like, they're just not used to hitting tab to tab complete things.
- 13:55So, like, everything is mainly a typo.
- 13:57So I started off helping out a few workshops.
- 14:01And then I matriculated into, like, their next, like, instructor class where I was, like, certified to be an instructor where it was mainly, like, getting familiar with the material and, like, learning how to teach the material.
- 14:13That's cool.
- 14:14Yeah.
- 14:14And then I was an instructor.
- 14:15And my first couple of years as an instructor, that was, like, right on the border of, I was still in grads.
- 14:21I was, like, finishing up my master's program.
- 14:23And also, like, I had a job.
- 14:26But I ended up working so much during my job that my boss was pretty much, like, please go home.
- 14:31And so I spent a lot of time going home.
- 14:34But it was really just, like, go teach, like, other workshops.
- 14:36And it was, like, super nice being in the New York City area because, like, going to a university or any place was pretty much local for me.
- 14:43So I got a lot of teaching experience out of that.
- 14:45And I didn't know at the time, but I say it now, like, teaching is, like, one of the best ways to learn something.
- 14:50So Bash and Git and Python and later on R, like, I just got more familiar with it just because I was teaching it all the time.
- 14:59And then, you know, once you have some foundation, like, learning the next small bit of information is, it becomes easier and easier.
- 15:06And then it just snowballs into something.
- 15:09That's cool.
- 15:10Yeah.
- 15:10And then, like, all of that teaching knowledge ended up being, like, the foundation for, like, the book that I ended up writing or was tasked to write or call pandas for everyone.
- 15:20I mean, it's really, like, an honor that, like, I got recommended to write this thing.
- 15:24So I should frame it in that sense.
- 15:26I've done a lot of training as well.
- 15:28And I feel like once you kind of go through a couple of cycles of that, you just get so good at learning something with enough depth to present it that it becomes, like, this really great power.
- 15:39And it's kind of addicting, right?
- 15:41You're like, oh, what's the next thing I can learn?
- 15:42What's the next research project I can go on?
- 15:44And, yeah.
- 15:45So it sounds like you did the software carpentry thing and it kind of somehow sucked you down this pandas for everyone hole of writing this book, which is Addison Wesley, which is pretty cool.
- 15:55Even writing the book, like, now you're just like, oh, I just can't write, like, really janky code anymore.
- 15:59Like, this actually needs to be, like, quote, unquote, like, the better way of doing things.
- 16:03So, like, there was, like, still, even though I was, like, writing a book and I was supposed to be the expert in this, like, a lot of it was also, like, I should probably read this part of the documentation just to make sure.
- 16:13Because, like, I also learned this, like, on my own.
- 16:16Right. Well, that's the thing about the difference of practicing as a programmer or as a data scientist versus an author or an instructor, right?
- 16:25Like, as a practicing person, you have a problem.
- 16:28You're like, I need to figure out how to make pandas do this.
- 16:30Like, it doesn't matter how it happens.
- 16:32But if you can make it happen, you're done.
- 16:33Like, that's the end of the research.
- 16:35You're done.
- 16:35This part is solved.
- 16:37What's the next problem?
- 16:38But as an instructor, like, well, but there's these other two ways.
- 16:40And but if somebody says, well, why not this way versus that way?
- 16:44What's the difference?
- 16:44All of a sudden, like, all these cases that would you would never go down, like, you have to start going down those now, which is I think is awesome, actually.
- 16:51But it's definitely a different way of thinking.
- 16:53It's super cool because, like, now it becomes its own, like, learning path.
- 16:56Like, you see other people have problems and you see how they think about it.
- 16:59And, like, it sort of adapts how you present material.
- 17:02For me, when I was originally, when I first started off teaching workshops out of the book, I pretty much went in the order that I presented the chapters in.
- 17:12And then more and more recently, like, I realized, like, wait, like, tidy data principles is actually, like, one of the most important things in, like, data science and data cleaning.
- 17:20After we load our first data set, I pretty much just jumped to, like, that chapter.
- 17:24Because if you can really understand that, everything else becomes way easier, quote unquote easier.
- 17:29Yeah, sure.
- 17:30Well, if you're trying to do operations on bad data and it keeps crashing, like, that's no fun.
- 17:35Like, why does it say none?
- 17:37Is that invalid?
- 17:38You know, it doesn't have this attribute.
- 17:39I don't understand.
- 17:40Like, well, let's talk about that.
- 17:43This portion of Talk Python To Me is brought to you by Indeed Prime.
- 17:47Are you putting your Python skills to good use?
- 17:50Find your dream role with Indeed Prime and start doing more of what you love every day.
- 17:55Whether you're a developer, data scientist, or anything in between, one application puts you in front of hundreds of companies like PayPal and VRBO in over 90 cities.
- 18:04Indeed Prime showcases your experience and tech skills to match you with great fit roles that meet and exceed your salary, location, and career goals.
- 18:13And when you start a one-on-one conversation with one of their career coaches, you'll get resume reviews and personalized advice to help you get what you deserve.
- 18:21So, if filling out countless job applications isn't your thing, let top tech companies apply to you.
- 18:28Join Indeed Prime for free at talkpython.fm/indeed.
- 18:32That's talkpython.fm/indeed.
- 18:35The reason I wanted to talk a little bit about software carpentry, other than just like you have been doing in his school,
- 18:42is I think it's a really good segue into this larger topic of how do you take the average data scientist and the work that they're doing
- 18:51and help bring in these more computer science, maybe not even computer science, let's say software engineering principles,
- 18:58to help them basically be more effective, right?
- 19:01So, maybe we start at the beginning.
- 19:03We've got some idea.
- 19:05We probably found out we can open up a Jupyter notebook, load something into Pandas,
- 19:10and poke around with it with a Matplotlib or something, right?
- 19:15Maybe that's it, right?
- 19:16Maybe we've seen a lot of Matlab code as well, where it's like, well, I got this does this thing,
- 19:22but it's like there's no functions.
- 19:23Maybe there's loops, maybe not, right?
- 19:26It's just like all crammed in there.
- 19:28And those are PhDs writing that.
- 19:30So, like really brilliant people, but they just don't have the software engineering skills.
- 19:34So, where do we start with that?
- 19:37There's a few papers I would direct people to sort of get a sense of where I'm coming from.
- 19:43So, there's this one paper by William Noble called A Quick Guide to Organizing Computational Biology Projects.
- 19:48And that's sort of the premise of how, I guess, I would present, how do we introduce software skills?
- 19:56And in that paper, he literally talks about you should have a folder structure.
- 19:59And maybe this is one way you should set up your folders for your analysis projects.
- 20:03And I'll talk a little bit about that in a bit.
- 20:05But yeah.
- 20:06Yeah.
- 20:07So, it's called A Quick Guide for Organizing Computational Biology Projects.
- 20:10And, you know, it's probably focused on biologists, but I'm sure that it's like pretty generally applicable.
- 20:14Yeah.
- 20:15Yeah.
- 20:15Other than like maybe the sequence.py file, like replace that name with whatever you need.
- 20:21Right.
- 20:21It's Hubble.py or whatever.
- 20:24Yeah.
- 20:25And the other two papers, the first author is by Greg Wilson, who restarted Starforce Carpentry, like back in the 2000s.
- 20:33And he wrote two papers, one in 2014 called like Best Practices for Scientific Computing.
- 20:38And then in 2017, the paper is called Good Enough Practices in Scientific Computing.
- 20:43If you just look at the papers, it almost seems like, hey, we're presenting like the ideal case.
- 20:48And then we almost realize like that's impossible in the real world.
- 20:51But they're both like pretty good papers.
- 20:53And they talk about like different things.
- 20:55Right.
- 20:55What would we have if we had like the perfect adaptation of software engineering to this world?
- 21:00Like, okay, well, what can we reasonably ask people to do that will make their life better?
- 21:05It sounds like.
- 21:05Yeah.
- 21:06And the way I approach it is like, just like when I teach data science skills, I pretty much make a beeline to tidy data and tidy data principles.
- 21:15In this case, it's almost like a beeline towards project organization.
- 21:20Just having some kind of structure to your analysis project.
- 21:25That will snowball into all of the cool tools that you probably heard of and don't know how people end up there.
- 21:33But if you take slow steps, I found that project organization is the fundamental thing where it's sort of like the gateway to everything else.
- 21:42Right.
- 21:43Because a lot of what you need, it sounds like, is code organization.
- 21:47Right.
- 21:48It's like the architecture and functions, classes, different modules, the concept of I'm going to pass data to this thing and make it reusable.
- 21:56All of that stuff really seems to be like natural follow-ons of like, well, how do we organize this project by function or by purpose?
- 22:05And like, just really think through that, right?
- 22:07Yeah.
- 22:07And it doesn't even have to be as complicated as, oh, we're doing like proper software engineering and like we need to create a Python package.
- 22:15Like that can all be deferred to much later.
- 22:18Because usually what ends up happening, you mentioned like, hey, I'm a scientist.
- 22:21I found out about Jupyter Notebooks.
- 22:22It's a really cool tool.
- 22:24Taking pictures of black holes out, like using them.
- 22:27So, yeah, you have all these tools.
- 22:29And like the scenario is like, hey, it's great that you're using a programming language to work with data.
- 22:35Excel is a great GUI for data, but it has its limitations.
- 22:39Cool.
- 22:40You are now using a programming language.
- 22:42Where can we go from there?
- 22:44And like when you are in that beginning state, just to make everything work, like you dump everything in one folder.
- 22:51You have like your Jupyter Notebooks.
- 22:52You have all your scripts, all your data.
- 22:55Your data files.
- 22:56Yeah.
- 22:56Yeah.
- 22:56If I say load this, I just want to say the file name.
- 23:00I don't want to have to think about like where that's relative to the other on some like server or something like that, right?
- 23:05Yeah.
- 23:05And then like as an academic, you might have like a Word doc in there or maybe a LaTeX file.
- 23:09And then you compile that thing and it very quickly becomes this folder with hundreds of files and you can't find anything.
- 23:17And that's when you just start end up, you know, maybe the word final comes into like the beginning of the file name just so like you can find things, right?
- 23:24Yeah.
- 23:25I was going to say it already sounds bad.
- 23:26And then if you start trying to do version control by like having multiple files named the same thing, then you're really pushing your luck.
- 23:32Yeah.
- 23:32So the most important thing, I think like if you're at that point, where can you go next, right?
- 23:38It's always like trying to do things incrementally.
- 23:40Like how do you make your life like 10% better each time?
- 23:43And then it's like a nice way, especially if you're like brand new grad student or you're in science, but like you've never really learned programming.
- 23:52Like where can you go from there?
- 23:53It's useful to have some kind of guide or path that you can follow or think about to like make yourself better and do these things more efficiently.
- 24:01Yeah.
- 24:01So let's talk about some of the programming things that you can think about.
- 24:05One of the ones that you have is like try to make your code easy to read.
- 24:10Oh, yes.
- 24:11So one of the things I talk about in programming is like make things easier to read.
- 24:15Do things in steps.
- 24:17Don't try to like write one for loop that have a whole bunch of like side effects going on, right?
- 24:23Like things should just be incremental just to take like a cue from like education.
- 24:27Like we as human beings can only carry, I think the number is like four plus or minus three objects in our mind at the same time, like roughly seven.
- 24:34You should pretty much follow that too.
- 24:37When you're programming, you shouldn't have to have, I mean yourself or like potentially another reader try to carry like 10 different things going on at the same time.
- 24:47It's just not helpful for.
- 24:48Like maybe an example is I'm trying to go through a loop.
- 24:51I'm really trying to do three things.
- 24:52Like as I get the data and I compute something with the first step and then I do some other filtering and I do another thing.
- 24:59I could try to cram that into like one giant loop or maybe it should be three separate little loops.
- 25:04One that like cleans the data, one that like does that computation, another that then filters it, right?
- 25:10Three loops sounds like a better step than one giant loop trying to do it all.
- 25:13Yeah.
- 25:13Or you can more in like education framework, like, or you can like group things together.
- 25:18And in programming, the way we group things together is like write functions.
- 25:21So then you end up with one giant loop and it's really just making three function calls, but that's easier to keep track of than like, let's say we didn't write the function.
- 25:30Now we have like three different things like scattered in our code and you end up with a loop that's 150 lines long.
- 25:38And that's like scary because like I see a loop and like before I even look at this thing, like I'm already like, oh man, we are in for a ride.
- 25:47Right.
- 25:47So let me just give my perspective from like the software development, web dev, you know, more application side of things.
- 25:54It's like, if I see a function that's more than 10 lines long, it starts to make me nervous.
- 26:00I'm like, there is something going on here that's probably bad unless there's like a lot of error handling and like response, like even 10 is a lot in the typical scientific computing bits that I've at least seen a while ago.
- 26:14There was more than 10 lines.
- 26:15There's more than 10 lines.
- 26:16And so like Jenny, Brian, like from the R world has like this talk about like code smells.
- 26:23And it's like, that's like one of those like code smells of like, Hey, why does it look like this?
- 26:27Or like, at least when you're working with data or in the PI data stack, usually you shouldn't have to write for loops in the sense of like, if you're trying to operate on a data frame, they should be an apply call to a function.
- 26:39Even like sometimes when I see loops, it's like, yes, I will write them just because something broke.
- 26:44And I'm just trying to figure out like where my data frame, like I have a bad value, but like the final result ends up being like an apply call or something.
- 26:51Yeah.
- 26:52It's interesting because a lot of the libraries, NumPy, Pandas and whatnot, can they do the looping?
- 26:57They do it much faster and more efficient than you will in Python.
- 27:00One of the cool things that I like teach during the data science part is like when we go over like applying functions, if you're doing numerical computations, like just the NumPy decorator for like vectorize or the number decorator for vectorize, like just wrap the decorator around your computation function.
- 27:19It like pretty much for free gives you order of magnitude speed improvements.
- 27:24And so it's like, it's way better than just you trying to like optimize this thing yourself.
- 27:28Right.
- 27:29And that's like one of the other programming things.
- 27:30It's premature optimization is like the bane of all evil or whatever.
- 27:34Just write the thing you want, especially if they're like loops.
- 27:37Python has many mechanisms to like help you with that and make it faster pretty much for free.
- 27:42That's definitely cool.
- 27:43I love this idea of code smells.
- 27:45I'm fascinated by it.
- 27:46I want to come back to it.
- 27:46But another thing I want to throw in that I kind of, I feel like is in this realm is like the idea of reusability.
- 27:53You can write code so that it's easily reusable or that it's not so much.
- 27:57So like I could write a function, but maybe I have a bunch of global variables that I'm still using and it makes the function like it moves the code away.
- 28:04So I understand that it's like it's more compact and more readable, but it doesn't necessarily make it reusable.
- 28:09So thinking about like how do I parameterize these things and make them something that I can use in other situations or once you solve this problem in this way, like I never have to think about this again.
- 28:21I just now use this in the other part and that was rough, but that was Friday and I don't have to think about it ever again.
- 28:26Like that's a pretty good principle, I think, here as well.
- 28:28Yeah.
- 28:29And even when you're writing your functions, you can write your function for your use case now.
- 28:33And, you know, for example, it's like a function that is a regular expression parser for like a US telephone number, which is, if you try to write one of those, it's like way more complicated than ever it needs to be.
- 28:43But it's like final exam in like regular expression 101 or something.
- 28:48Like it's really like way worse than it should be.
- 28:50Yeah.
- 28:51You'll write your function with that in.
- 28:52And like one of the things I end up doing is even if I have hard coded things within the function and then I realize later on like, oh, wait, I pretty much need to run that function again.
- 29:02But instead of like the second index, I need like the fourth index or whatever.
- 29:06You can make backwards compatible functions or code by like saying like, oh, I'm just going to create a default parameter in my function.
- 29:15It's going to default to the one that already works.
- 29:16But then now I can just reuse that function later on and like just change that value.
- 29:20Simple things like that, that you don't have to rewrite the function just for your second use case.
- 29:26I like, I talk about like, if you ever hit control C on your computer, you better be paying attention when and how many times you're hitting control V, right?
- 29:35And if it's like more than three times, you're probably doing something wrong.
- 29:39Yeah, for sure.
- 29:40For sure.
- 29:41One of the things I think would be nice, you talked about premature optimization and all these performance stuff.
- 29:46What is your recommendation around like how you structure your code?
- 29:52So a lot of times I imagine that the data science stuff has pretty much like there's a Jupyter notebook and most of the code, like the supporting functions are kind of the beginning.
- 30:03And then later on, like they're kind of using them and so on.
- 30:06When do you tell folks to break out like separate Python modules that you could load into your notebooks?
- 30:12And like, what's the, how do you think about like different module files versus notebooks and things like you can apply refactoring tools really easily to a bunch of files using PyCharm or things like VS Code, not so easily in Jupyter, right?
- 30:27So where's the balance there?
- 30:29Yeah, so the thing with Jupyter notebooks is, yes, there was like a talk at JupyterCon about why Jupyter notebooks are bad.
- 30:36And I have this love hate relationship with Jupyter notebooks.
- 30:39But one of the things I can say, so Rachel Tappman from Kaggle, she gave like an Our Ladies Meetup talk in 2018 about like putting together a data science portfolio.
- 30:51And one of the things in there is like the Jupyter notebook is great, but most of the time you probably are just interested in like the figures or tables that's being generated, especially if you're taking this into a meeting, right?
- 31:04Like no one wants to like scroll forever to get to the bottom of the notebook because the first like three quarters is cleaning code.
- 31:12Right.
- 31:12I sort of like got into this sort of workflow of like I'll use the Jupyter notebook to like test things out in like data cleaning pipeline.
- 31:21But the actual data cleaning stuff all go into like Python scripts.
- 31:25So like what ends up at the end of the day, what happens in the Jupyter notebook is like pretty much load the libraries I want, load the data I want.
- 31:33Maybe there's like a few functions that's specific to like the figures I need and then just the figures and tables I need.
- 31:39So my Jupyter notebooks are pretty small and that down the line in terms of like other software engineering practices that just makes the diffs in through Git just way more manageable if I start making changes.
- 31:51So if you end up with massive Jupyter notebooks that are a lot of it is just data cleaning code, you would think about like moving that out to other notebooks or other files just so you have more files.
- 32:04I'm in the camp of pretty much in a lot of academic or scientific use cases, maybe not in like physics when they're working with like sensor data, but file IO is not that big of a bottleneck.
- 32:16So like I will have more scripts and more files that just write out data just to have another script and file read it back in.
- 32:24But that just breaks up my thought processes into smaller manageable pieces.
- 32:28That's interesting.
- 32:29It's like a little bit of a cache as well, right?
- 32:31Like you can take the step N and go to N plus one and like iterate on how that happens without rerunning all the stuff, right?
- 32:39Because you just reload that file that you save.
- 32:40Yeah, yeah, exactly.
- 32:41So like this goes down into like project template world where like I'll have a data folder in our data folder, you know, we'll have like an original data folder.
- 32:49That is the data that we download stuff in, never make changes to your raw data.
- 32:54And then everything else gets modified with a script.
- 32:57I'll have like, for example, a script that reads in one of my original data sets.
- 33:01I'll do my first set of processing.
- 33:03Like maybe it's like, oh, fixing missing values.
- 33:05And then I'll immediately write it out to somewhere in like under data and processing because it's now a process data set.
- 33:13And I want to distinguish between data sets.
- 33:14I can, I should just pretty much lock as read only versus things that I could like potentially modify and delete later on.
- 33:21I'll have a whole series of these scripts that pretty much just like you'll see it.
- 33:25I rarely these days have scripts that are like more than a hundred lines long because it's pretty much read in, do this one task, write it out.
- 33:33And especially if you have like one step that just takes a really long time.
- 33:38Yeah, it serves as pretty much as a cache where you just save out your temporary results and then you can deal with it later without like accidentally rerunning the part of your code that you didn't mean to run because now you're stuck for an hour.
- 33:50And that's sort of like what happens with Jupyter Notebooks as well.
- 33:53When we first started programming, like when I first started programming, it was just like, I just need this stuff to run.
- 33:58So I'll run cell one and then jump to cell 10 and I'll run cell one again and then jump to cell 15.
- 34:04And then I can like scroll all the way down and get my plot.
- 34:07Right.
- 34:07And then it's like, how am I supposed to run?
- 34:09How do you document something like that?
- 34:10Right.
- 34:11And that's sort of one of the drawbacks with the Jupyter Notebook is, yeah, the execution order isn't guaranteed in what was written.
- 34:18It's a little bit like a go-to.
- 34:20Yeah.
- 34:20It's pretty much like a go-to.
- 34:21Yeah.
- 34:22Which is kind of bad.
- 34:22Except it's not even like documented, right?
- 34:24Yeah.
- 34:25At least it doesn't even say go to 20.
- 34:26It's just like they went to 20.
- 34:28Yeah.
- 34:28And then when you execute it, it turns to 21, right?
- 34:31So like it doesn't even, like you don't even know what 20 is, right?
- 34:34So if you end up in a situation where you're running bits and pieces of a code all over the place, that's a sign of like, wait, let's fix this now.
- 34:42It's pretty cheap to create a new file.
- 34:45And let's do all the data cleaning or the parts I need for this figure.
- 34:48Maybe that can just be in one thing.
- 34:51And then more and more as you find pieces that need to be reused, you'll, oh, maybe I can turn this into a function.
- 34:56Then you'll put that as a module.
- 34:58And I would say like even if it's a module, just leave it in.
- 35:01If the folder structure is pretty much you have a data folder and an analysis folder and an output folder where output is like your figures and stuff.
- 35:08At first, it's okay.
- 35:10You can have your modules in your analysis folder.
- 35:12And so you can still say import something and it'll still import properly in that sense.
- 35:17You don't have to like just go and make a Python package right away because at least in what I've seen is sometimes your analysis, it's not really going to be reused across projects.
- 35:28You don't need the overhead of writing a Python package.
- 35:31It's when you, for example, if you're querying, if you're doing some study on like code in GitHub, for example, and you write your own GitHub querying API call stuff, and then you realize this is part of one giant grant with many different analyses that need to happen.
- 35:46Maybe like your GitHub querying code will turn into a package because you're actually reusing it.
- 35:51You don't have to turn everything into a Python project.
- 35:54You don't have to do that to do it like quote unquote correctly.
- 35:57Yeah, and I feel like the value from going from like just some huge notebook or some huge script file and then moving that into modules that have functions you can import and run and whatever, that's like 85% of the way, right?
- 36:09Whether or not you can pip install the thing, it doesn't matter.
- 36:11You know, there's a lot of overhead to make something super reusable to make it documented.
- 36:16Like maybe if you're in academics, maybe that's a cool project for like a senior undergraduate person.
- 36:23Like, hey, you know what?
- 36:23You know Python.
- 36:24Why don't we take this and turn this into an open source project?
- 36:27And that can be your project, right?
- 36:28Like, I'm not sure it's a great research time and energy in general.
- 36:33Yeah.
- 36:33Well, so more and more, there's very recently PyOpenSci is an organization that sprung up and it's trying to mimic our OpenSci.
- 36:42And it's essentially like supposed to be a repository of Python packages made towards making science better for some scientific use case.
- 36:53And all of those are going to be reviewed by somebody and it fast tracks you for if you want to write a paper based off of that software package, it'll fast track you into Joss, which is a journal of open source software.
- 37:08Yeah.
- 37:08And I had them on the show as well.
- 37:10Yeah.
- 37:10So now you have at least like the incentives are more or less lined up, right?
- 37:15Because before, like if you were just maintaining a software package, you know, what are your academic incentives?
- 37:20Because a lot of that is still around publishing and grants.
- 37:24So at least now there's the incentives are now lined up where like even though you are writing a software package, you can now write a paper about it.
- 37:31Yeah.
- 37:32It may generate a paper which might help you with your tenure and so on.
- 37:35But I guess let me take a step back really quick on my statement.
- 37:38Like it might not help you in your academic career directly to spend the software engineering time, but it may help you significantly in your research.
- 37:46If you can publish something and then you get other researchers to start using it, right?
- 37:51It becomes a package that you have more contributors to, right?
- 37:54Maybe you have one student you could fund part time.
- 37:57Now all of a sudden there's 20 institutions like all working.
- 37:59Like that could be a huge benefit, but I think a lot of stuff is so specialized, so tied to your data and your particular problem.
- 38:07Like you say, your first thought shouldn't be how do I open source this as a package?
- 38:10It's like, how do I just like make this a decent software project?
- 38:13Yeah.
- 38:13And that's a pretty lofty first goal too.
- 38:16Like how do I make this work properly for myself?
- 38:18Right.
- 38:19Because then that you go into the route of like, okay, I should write tests for this just to make sure it's like at least behaving correctly.
- 38:25There's a bunch of incentives as well for just having an open source project and trying to get other people to play with it because you'll build out the functionality for the thing you built.
- 38:34And as functionality expands, you'll sort of get more and more people in.
- 38:38And it sort of ties back to like the Python community is great.
- 38:44And so like now you are embracing the broader Python community and now you have more and more resources or people you've met to help you with your own project.
- 38:53If you're at like PyCon or SciPy, you can have your own sprint for your software project just to have other people try this out.
- 39:01You end up building your own community off of your little software project, which is, it makes you feel good.
- 39:08And it's still also advancing science.
- 39:10And a lot of science is also communication.
- 39:12And you built this stuff to help other people.
- 39:15So like you might as well try to make it easier for other people to help you as well.
- 39:19Yeah.
- 39:20It could definitely help your career as well.
- 39:22I mean, people like Wes McKinney, Jake VanderPlas, Travis Oliphant, like folks like that, like they're legitimate big names in the whole Python space in general.
- 39:31And a lot of that came from, you know, these academic projects and whatnot.
- 39:34So that's pretty cool.
- 39:35This portion of Talk Python To Me is brought to you by Rollbar.
- 39:40Got a question for you.
- 39:41Have you been outsourcing your bug discovery to your users?
- 39:44Have you been making them send you bug reports?
- 39:46You know, there's two problems with that.
- 39:48You can't discover all the bugs this way.
- 39:50And some users don't bother reporting bugs at all.
- 39:53They just leave, sometimes forever.
- 39:55The best software teams practice proactive error monitoring.
- 39:58They detect all the errors in their production apps and services in real time and debug important errors in minutes or hours, sometimes before users even notice.
- 40:07Teams from companies like Twilio, Instacart, and CircleCI use Rollbar to do this.
- 40:12With Rollbar, you get a real-time feed of all the errors so you know exactly what's broken in production.
- 40:18And Rollbar automatically collects all the relevant data and metadata you need to debug the errors so you don't have to sift through logs.
- 40:25If you aren't using Rollbar yet, they have a special offer for you, and it's really awesome.
- 40:30Sign up and install Rollbar at talkpython.fm/Rollbar, and Rollbar will send you a $100 gift card to use at the Open Collective, where you can donate to any of the 900-plus projects listed under the Open Source Collective or to the Women Who Code organization.
- 40:46Get notified of errors in real time and make a difference in Open Source.
- 40:50Visit talkpython.fm/Rollbar today.
- 40:52Before we move off, I don't want to drop this idea of code smells because, first of all, I love this concept.
- 41:01It's just such a good visualization of what can be wrong with software, but not broken with software.
- 41:09Because a lot of times you think of, well, my code now works, but what should I do?
- 41:14And I think the code smells is a very practical thing.
- 41:17Just for folks listening, like, code smells, the idea is the code is working.
- 41:23It's not broken, but when you look at it, you try to read it, like, your nose literally could kind of curl up.
- 41:29You're like, ew, there's something wrong with this.
- 41:31I guess it works, but I guess it's not good.
- 41:34It's really not good, right?
- 41:35Like a 300-line function, not good.
- 41:37Like, it works, but there's something wrong.
- 41:40And I knew this mostly from Martin Fowler's work back in 1999 when he wrote refactoring.
- 41:46And this was sort of the introduction to, like, how do you know when to refactor?
- 41:50Well, you look for the places that make your nose turn up.
- 41:54You go, ew, what do we do with this, right?
- 41:56Like, oh, there's a 300-line function.
- 41:58That's bad.
- 41:59What can we do about that?
- 42:01Or here's a function taking 20 parameters.
- 42:03That's really horrible.
- 42:04You know, it's really easy to switch this integer for that integer.
- 42:07And how do you know when that happens?
- 42:09So what could you do to make that better?
- 42:10And there's just a bunch of them.
- 42:11But I only know this through the sort of software engineering side of things.
- 42:17And this presentation that you talked about here, which was Jenny Bryan, right?
- 42:24She has some really interesting tips from the data science perspective, right?
- 42:28Yeah.
- 42:29Yeah, so the first one is do not comment or uncomment sections of your code to alter behavior
- 42:33because you want to try different stuff out.
- 42:35Yeah, and that's, like, a very common thing, right?
- 42:37Like, the easiest case where that happens is if you are in a collaboration environment,
- 42:43you have five people.
- 42:45You have five comments of data loading because everyone hardcoded, like, a data path, right?
- 42:52And so, like, there's literally, like, you comment in your code just to, like, load the data set
- 42:58across, like, depending on who you are, right?
- 43:00And then, like, you end up, like, if you end up using, like, some kind of version control system,
- 43:05like, the vast majority of your commits are just, like, it's my turn that ran it.
- 43:10And you just have this one bit of these couple of lines that are just, like, committing.
- 43:15Just cycling.
- 43:16Just cycling back and forth.
- 43:17Yeah, so, I mean, what is the fix, right?
- 43:19The fix would be to do something where you have this proper structure, as you already talked about,
- 43:24and then you use something like os.path or pathlib, and you compute the relative path over to that,
- 43:29and then you generate an absolute path.
- 43:32That would work for everybody as long as they all check out the same general structure, which sounds like Git.
- 43:37Yeah, and they dealt with this in the R world with these two packages called Rprojroot, like, for the root of an R project,
- 43:45and here, here as in, like, find this file using here as, like, the root path or something.
- 43:52And that's sort of, like, my contribution to all of this.
- 43:55I tried to pretty much, I wrote a package called PyPrideRoot that tries to mimic, like, the same functionality as well,
- 44:02because it works if you are working with scripts and stuff.
- 44:07But the second you have, like, some kind of folder structure where you have a Jupyter notebook,
- 44:12you'll sort of realize that, like, the Jupyter notebook doesn't care that you have a folder structure.
- 44:17Like, the second you're in it, like, the working directory is now wherever the Jupyter notebook is,
- 44:21not whatever folder structure you've, like, very carefully pieced together.
- 44:25And so this was, like, an attempt.
- 44:28It's not a very complicated function.
- 44:30It literally takes, like, oh, what is your working directory?
- 44:32And I'll recursively go up by its parent and checking for, like, special files like .git or a .here file.
- 44:40And then I'll pre-pen that to whatever path, just so, like, you can now use relative paths in a Jupyter notebook,
- 44:45just like you would in a script.
- 44:47So you can avoid that problem as well, the commenting in and out.
- 44:51Yeah, yeah, that's cool.
- 44:52And I'll definitely link to that project that you built.
- 44:54Tip two, use if and else in moderation, which seems pretty good.
- 44:59Number three is pretty straightforward.
- 45:00Use functions.
- 45:01I mean, just do.
- 45:03It's a good idea.
- 45:05You should do this.
- 45:05Yes, and, like, even when you're writing a function, it's okay to have a very complex function.
- 45:12And even complex functions don't need to be written all in one go, right?
- 45:16Like, you can break up your function, even though it does, like, a very complicated task.
- 45:21There's probably small subtasks.
- 45:23And your function can call other helper functions.
- 45:26It's not just like, oh, this is a really complicated thing.
- 45:30Let me just write a function for it.
- 45:31As you're writing the function for it, like, that's one of the other code smells as well.
- 45:35Like, if I have a hundred line function, like, that's kind of scary.
- 45:38You couldn't break this down into smaller pieces?
- 45:40Like, that's kind of weird.
- 45:41And so, like, having helper functions that feed into, like, a larger function is also how you fix that code smell.
- 45:49Yeah, absolutely.
- 45:50And obviously, that makes testing way easier because you test little bits.
- 45:54And then, you know, you test the kind of orchestration of them, and you're good.
- 45:57Another one that I'm a huge fan of, it's like a serious pet peeve of mine,
- 46:01is to have quick returns near the top or guarding clauses or guard clauses.
- 46:07If you've got a function that's, like, indented, and then it's got a loop, and then it's got an if,
- 46:13and then another if, and then another if, and it's just, like, way to the right.
- 46:17If you're scrolling to the right, you're doing it wrong.
- 46:20Yeah, yeah.
- 46:21And during PyCon, I actually, like, just bought your entire encyclopedia of training.
- 46:26And I forgot which one.
- 46:28I think it was the how to write your Python code, like, an experience.
- 46:31Pythonic code or something.
- 46:32Yeah, yeah, that one, huh?
- 46:33Yeah.
- 46:34So, like, I remember that chapter.
- 46:36Yeah, like, don't write nested if statements.
- 46:38Like, essentially, write them inside out so, like, it's flat.
- 46:42Yeah, exactly.
- 46:42Do them backwards.
- 46:43Yeah.
- 46:44Yeah.
- 46:44So, that was something that was just, like, that's what you should do.
- 46:48It makes it so clear.
- 46:50And, like, it's not very commonly taught, I don't believe.
- 46:54So, these are called guarding clauses.
- 46:55And the idea is instead of testing for a good condition and then another good condition and
- 47:00another good condition and then doing the thing, which puts everything way on the inside, you
- 47:04test for all the bad conditions first and you just bail out.
- 47:07And then what you're left with is a non-indented simple bit of code, which is what you're actually
- 47:11after.
- 47:12So, it's really clear what you're testing against.
- 47:14And then once you're past that, here's the simple thing we do.
- 47:17I love it.
- 47:18So, that was one of her tips as well.
- 47:19It's a nice one.
- 47:20Yeah.
- 47:21She's got some great little examples there.
- 47:23Some stuff on object orienting and so on.
- 47:26But, yeah, these are really good.
- 47:27I, you know, switch, which doesn't apply as much to Python.
- 47:29I actually wrote a switch language extension for Python using the context management with
- 47:35block.
- 47:35That's pretty awesome.
- 47:36But I'm not going to get into that because that's a whole different debate.
- 47:38But I do think this idea of code smells is really interesting.
- 47:41And you should think about them for data science because I'm sure there are different.
- 47:45It sounds like, it looks like there are different data standout smells that are more common
- 47:49than, say, standard software engineering.
- 47:51If you're doing database programming or whatever, you get like a different style there.
- 47:54Yeah.
- 47:54And just for like other programming related things and how you can like structure your projects,
- 48:00Jenny Bryan also has this talk about like, how do you name your files?
- 48:03It's kind of interesting because like I, if you think about these common problems long enough,
- 48:08everyone pretty much just converges to like the same set of solutions.
- 48:12I remember like coming up with, yeah, I should just name things this way or like set up my
- 48:17folder this way.
- 48:18And then like all of a sudden, Jenny Bryan like gives a talk at like,
- 48:21big R conference.
- 48:22Like, wait, that was like, I feel validated that like I didn't come up with something like
- 48:25nonsensical.
- 48:26Other people as well, like they write packages sort of like a cookie cutter, just like set
- 48:31up projects.
- 48:31And it's pretty much like the same way.
- 48:33And one of them is like, oh, how do you name your files?
- 48:36Right.
- 48:36Like, and especially in analytics, there's clearly an order you should run this stuff
- 48:40in.
- 48:41So one of the ways of like, how do you name your files is pre-pen a number to them.
- 48:47Right.
- 48:47So like you can say like one dash and then like the script and that's the order you write
- 48:51it in.
- 48:51If you want to do better, you say zero one.
- 48:54So like 10 and one doesn't get sorted in properly.
- 48:56Yeah.
- 48:57And then if you really want to go one step further, I started this habit of like having
- 49:02a three digit number.
- 49:03So like zero one zero, and that gives you a buffer room to like insert something in
- 49:08the middle.
- 49:09Or if you like forget something or like you realize that.
- 49:12That's like the 10, 20, 30 in basic.
- 49:14Yeah.
- 49:15Like, what if you got to put a line in between that?
- 49:17You got to go to 30 still.
- 49:18Well, you could do 19.
- 49:20Yeah.
- 49:20Whatever.
- 49:20Yeah.
- 49:21And I found that out because like, that's how sort of some of the files in Linux in the
- 49:26order of like how it loads up like services or something.
- 49:29It's like defined in like those three digit numbers.
- 49:32And I was like, oh, this is interesting.
- 49:34I should do that.
- 49:35It just saves me from like renumbering like a whole bunch of stuff.
- 49:37Yeah.
- 49:38That's cool.
- 49:38I mean, just thinking about the structure is quite interesting.
- 49:40At the end of the day, even though you have all this structure for your analytics project,
- 49:45because everything is like nice and in some kind of order, if you do, for example, want
- 49:51to create a Python package, like it's already there for you, right?
- 49:54Like you can create another folder.
- 49:56That's the name of your module.
- 49:58Put a setup that PY file.
- 50:00You could have the ability to set that up.
- 50:02And now you can like pip install dash E.
- 50:04And then anytime you edit that file, like your analysis will still work.
- 50:08And that's pretty cool.
- 50:10The other thing with project structure related stuff, like if you have things numbered at
- 50:14the end of the day, everything comes down to like a DAG compute system.
- 50:19And so like, because you have your stuff in order and there's properly defined inputs and
- 50:23outputs, you can use like a make file or like a simple script as like a poor man's make file.
- 50:28But then you end up in like the situation like, oh, that's where Luigi and Airflow come into
- 50:33play.
- 50:34They're pretty much just DAG executors.
- 50:36Like I said, at the very beginning, setting up your project is pretty much like the gateway
- 50:41drug into like all of this other cool technology.
- 50:43Cause like you've, you would have set everything up in such a way that you then use those tools
- 50:48when you hit that point where you need it.
- 50:51And it's like a nice way to like slowly improve, do self-improvement stuff.
- 50:55And then you also like end up using all the cool stuff that you see at like these big conferences
- 51:00as well.
- 51:00Yeah, that's, that's really cool.
- 51:02And of course the structure gets you just that much closer to trying it out.
- 51:05Now, what do you think about Papermill and some of these concepts?
- 51:10Are you familiar with Papermill?
- 51:11Yeah.
- 51:11Papermill is, I think that's the Netflix.
- 51:14Yes.
- 51:14It lets you basically turn a Jupyter notebook into something that can receive inputs and then
- 51:19have outputs almost like a function or a module or something like that.
- 51:22So I personally haven't used it.
- 51:25That's mainly because when I was started working, like Papermill wasn't really a thing at the
- 51:30point.
- 51:30So like I had migrated out into like, let's just make everything a Python script because
- 51:35that has no dependency and we can just execute things that way.
- 51:40And then the notebook itself just becomes like, Hey, this is the report.
- 51:44In some sense, I can see if I, for me, I guess like the next time I start an analysis project,
- 51:49like I probably will use Papermill just because it's like, Oh, it's this cool technology.
- 51:53And I've like set up my folder structures in such a way where like I can now use it.
- 51:58Right.
- 51:58So I've heard of it, but I personally haven't used it yet.
- 52:01Yeah.
- 52:01I haven't used it either, but it sounds pretty interesting.
- 52:03Like it sounds like Netflix, like you said, is doing really interesting stuff to me.
- 52:07One of the things that sounded special, it made me go, okay, well maybe that is worth considering,
- 52:12even though it's like not necessarily my style, right?
- 52:15is if you have a big, long sort of pipeline of operations and each one is its own Jupyter
- 52:20notebook.
- 52:21If it fails, you can save, you basically keep the notebook as it was computed laying around.
- 52:28So you can just open it up and you have basically a history of what happened and then what failed,
- 52:32which sounds like a pretty interesting way.
- 52:34Cause if you switch it to scripts, which I'm all for, but you end up with, you know,
- 52:38it exited without, with like not code zero.
- 52:41Oh, that's bad.
- 52:42Right.
- 52:43Like, what does that mean?
- 52:44Like, I forgot, I don't even have logging or any of these things, right?
- 52:46Like what happened?
- 52:47Like, why did it not work?
- 52:48So I do think there's some interesting stuff happening around there, but I do also
- 52:52feel like the software engineering tools you have apply really well to modules, right?
- 52:59Like it's easy to run that through pytest.
- 53:01It's easy to run that through a profiler, the refactoring tools work on those.
- 53:07Not that you can't do some of that stuff with notebooks, but it's easier to use them on files.
- 53:13Yeah.
- 53:13And especially if you're checking things into version control, that's sort of like the one
- 53:18thing.
- 53:18My main gripe with the notebooks is like, every time I make a change, like I have no
- 53:24idea what's going on in diff and it's just like, yeah, just add and commit.
- 53:28Like, I think it's right.
- 53:29Let's see.
- 53:31Do you accept their changes or your changes?
- 53:33my changes.
- 53:34Or like, or like if I just want to open the notebook.
- 53:38So there's this program called enter act, which at least is like a, a desktop version.
- 53:43So I don't have to like open up fire a server and then open a notebook.
- 53:48That way.
- 53:48But yeah, like sometimes like, I just want to double click this thing just to see it.
- 53:52I don't like want to open up a terminal and like launch everything just to see something.
- 53:56So it was like little things like that, where I was like, I'll try to do as much as I can
- 54:00in a script.
- 54:01And then like everything else goes into a notebook.
- 54:03And then in the notebook, I still save out the things I want just so like I have an easier
- 54:08way to like access figures or tables without having to like look at the entire notebook.
- 54:13Yeah.
- 54:13I guess that is one of the challenges is the whole diff thing.
- 54:16Maybe we could talk.
- 54:17We're kind of getting long on time, but there's a lot of interesting stuff to cover.
- 54:19So I'll ask you a few more questions.
- 54:20Let's think a little bit about collaboration.
- 54:23Like you talked about the anti-pattern of having like Sarah's path, Dan's path, Michael's path,
- 54:32whatever, like, and just commenting them out which one is active at the moment.
- 54:36But there's probably some other stuff for collaboration, like are you using Git?
- 54:40Are you using some online shared notebook that's kind of like Google Docs?
- 54:46Like what are your thoughts around that kind of stuff?
- 54:48So Google has something called like the Co-Laboratory Notebook, which is essentially like Google Docs,
- 54:53but gives you a Jupyter Notebook system.
- 54:56That's pretty cool in the sense that like, yeah, we won't have this commenting out of like random
- 55:02lines because everyone's really just working on the same place.
- 55:04Like that's really nice for collaboration.
- 55:06I still think that you need some form of version control.
- 55:10Like that is, I think like at this day and age, like it's pretty much required, especially
- 55:15when programs start to get more and more complex.
- 55:18Like you need a way to fall back on.
- 55:20The nicest feature I use in Git is like I write something, everything is broken and I just say
- 55:26Git reset and I just pretend I never did that and I just start over.
- 55:30Yes, exactly.
- 55:32Like that was a really bad idea.
- 55:34Please revert that.
- 55:34Okay.
- 55:35Now we're good.
- 55:35And it lets you be more exploratory.
- 55:38It lets you be more aggressive and trying to change it.
- 55:41Like this might not work, but if it works, it's going to be awesome.
- 55:43And try it.
- 55:45Actually, that didn't work.
- 55:46Revert.
- 55:46Or, you know, maybe it's a little more forethought.
- 55:49You create a feature branch to explore it.
- 55:50You do it there.
- 55:52And they're like, forget that.
- 55:53That was a bad branch.
- 55:54We're just going back here.
- 55:55Like, let's not do that.
- 55:56Right.
- 55:56Yeah.
- 55:56But it's a really great feature.
- 55:57Yeah.
- 55:57Yeah.
- 55:57And like, just along the lines of like collaboration stuff, like make small incremental changes.
- 56:03And that's like the actual stuff.
- 56:04That's the code that will actually get reviewed.
- 56:07Right.
- 56:07Like no one will review a code base where you're like at the end of the paper, the entire like submission relies on this code base.
- 56:15And you're like, I need someone to review this thing.
- 56:17Right.
- 56:17Like there's no way that's going to get a proper review.
- 56:21And so just in general for like, doesn't even have to be in like research or science.
- 56:26It's a good habit to like make small incremental changes.
- 56:29And like, maybe that's what your weekly meeting is about.
- 56:32It's just like, this is what I did this week.
- 56:34Someone press the green button to merge this in because that will actually be reviewed.
- 56:40And then you'll have a discussion around that point.
- 56:42Like all of that stuff.
- 56:43For me, I personally, I'm not in a managerial position.
- 56:47So like, those are the types of meetings I find like productive where I can actually talk about.
- 56:51This is why I did.
- 56:52This was the implementation.
- 56:53This is what I'm thinking about next.
- 56:55And then have a conversation around that because it can still be productive and you can still have like talks about longer goals.
- 57:01But like you also now have the benefit of like someone else looking at your work to make sure it doesn't have like a bad code smell.
- 57:08You know, maybe like you, you, you're off by like a factor of 10 and no one's going to notice that in like 900 lines of code.
- 57:14But they will if it's just like 20 lines of code, like a change like that is much easier to find.
- 57:19Yeah, that's definitely good advice.
- 57:21I definitely recommend working in small little, little bits and changes and, you know, make some small change.
- 57:27Do a git commit.
- 57:27Make another small change.
- 57:29A little git commit, right?
- 57:30Like don't wait until the end of the week or like until the end of the paper and like, all right, time to check it in.
- 57:34Like, no, not a good idea.
- 57:36One of the things I wish like exists more in academia is just having more resources to do pair programming.
- 57:43Because usually people are assigned one project and there isn't like two people assigned to the exact same bit, which is what you really need pair programming for.
- 57:51When I was co-instructing like the summer program in my previous lab, I would sit down next to students and I would pair program them through some kind of data related work.
- 58:02And it's super valuable for them because they actually get to see how I'm thinking about like this problem.
- 58:08And I'll say like, you're doing a join of two tables.
- 58:11Yeah, make sure that like the keys don't have duplicates if you're expecting duplicates, right?
- 58:16That's like one of those things of like, yeah, the code ran, so I'm just going to keep going, right?
- 58:19And you don't realize that you just did, you just did a Cartesian product and now you have a million rows and you don't know why, but you're just going to keep going.
- 58:28Why is it taking so long?
- 58:29Yeah.
- 58:29So pair programming, yeah, it's super valuable.
- 58:32And even now during my internship, it's I'm on the receiving end of pair programming, but this is more on the software engineering side.
- 58:38It's super valuable just to see like, oh yeah, this is how you write good code or like, this is how they're thinking about it.
- 58:45And it's even stuff like I talk about, yeah, be careful where you're hitting like control V a bunch of times.
- 58:50And it's like, oh yeah, like this is in two different places.
- 58:53Like, let's just refactor this out.
- 58:55And it's like, oh yeah, I didn't catch that.
- 58:56And like, when you refactor it out, you can actually have more guarding clauses just to make this like an even better check.
- 59:04That's one of the things I wish, like at least in research, like there was more budget and time for is just pair programming.
- 59:11And that just makes collaboration easier because you're now just talking with a person back and forth.
- 59:16It just makes that whole process like way nicer and smoother.
- 59:20Yeah.
- 59:20I mean, we certainly have the tools these days for it, right?
- 59:23You talked about Google Colaboratory, which has like live multiple editor features, kind of like Google Docs.
- 59:30You've got obviously screen sharing, you've got like VS Codes, ways to like watch somebody else's system on two sets of Visual Studio code.
- 59:40And there's some really interesting options.
- 59:41But yeah, it's got to, it's like also a cultural thing.
- 59:44And also you've got to have people to collaborate with on that part, right?
- 59:48Right.
- 59:49And in the sense of, hey, maybe like when you, even though you're in this small world and you write your package, like now you have someone to collaborate with, right?
- 59:56And that's sort of like socially motivating that you have other people using your stuff.
- 1:00:01Yeah.
- 1:00:01It definitely feels good to have someone looking at it, interacting with what you're building because building software completely in isolation just for yourself.
- 1:00:11It's kind of a weird place to be.
- 1:00:12It's not as much fun as it could be.
- 1:00:13Yeah.
- 1:00:13It's fun.
- 1:00:14Like when you're just in the sense of like, I got to get something like that minimum viable product, like that's fun.
- 1:00:20And then it's just like, as soon as you hit maintenance mode, it's like, who am I maintaining this for?
- 1:00:26Yeah.
- 1:00:26Or just all like a lot of the projects, you know, you're going to be working on it and you kind of get the happy path, mostly working and you feel like you're mostly done.
- 1:00:35But then there's all these little loose ends, the documentation you got to write for the other people involved, all the little tests and the edge cases.
- 1:00:44And just, it can just go on and on and on.
- 1:00:46It feels like, I thought I was done a month ago with this and I'm still working on it.
- 1:00:49How is this not still not done yet?
- 1:00:51Like I've definitely had that feeling in software and I'm sure it's just the same, you know, that was actually in a semi-research context.
- 1:00:58I'm thinking back to it.
- 1:00:59Yeah.
- 1:00:59Final thought on this collaboration bit.
- 1:01:03What do you think about GitHub?
- 1:01:04Like creating either a private or a public repo, using that for your work to share with people?
- 1:01:10I love it right now.
- 1:01:12Like pretty much if I have a thought, I just make a GitHub repo.
- 1:01:15So like my personal GitHub account has a bunch of projects where like they're pretty much empty, but they have a name.
- 1:01:21And it's just because like I thought of something one day and I just made a repo out of it.
- 1:01:26It's even really good for simple stuff.
- 1:01:28Like if you're at a conference and you just want a place to take notes, that doesn't matter what machine you're on.
- 1:01:34I've taken just notes and markdown as a GitHub repository.
- 1:01:38And then like during like a lightning talk, just be like, hey, I just started putting up my notes.
- 1:01:42And then maybe some people will like add, hey, wait, this is my talk.
- 1:01:46Let me put my talk in there.
- 1:01:47And you end up collaborating on like some kind of notes for like a conference, which is pretty cool.
- 1:01:53And for me, like I try to in lines of that 10% improvement, like every time, like originally, like I just made everything in Git just because I needed more practice with it.
- 1:02:03And it was just like a nice safe place for me to like, oh yeah, like add and commit.
- 1:02:07Like if you do it a couple of hundred times, that part doesn't become scary anymore.
- 1:02:11And so that's right.
- 1:02:13It just becomes so natural.
- 1:02:15Like, oh yeah, when I first learned Git, it's like, why am I doing this?
- 1:02:17This is so tedious.
- 1:02:19And then it's like, now it's like, okay, whatever.
- 1:02:20But then like you can do other stuff with Git, which is like super cool.
- 1:02:24So GitHub is like a great way to practice using Git and then also gives you the ability to practice or get ready for collaboration.
- 1:02:33Right.
- 1:02:34So even for me, even if I'm working on personal projects, sometimes like I will do branches for myself, push branches to GitHub by myself, and I will submit pull requests to myself.
- 1:02:47Just to document it and make it really clear.
- 1:02:49Like this is the reason for it here.
- 1:02:51The files that changed and all that.
- 1:02:52Right.
- 1:02:52And like, I was doing that for a couple of years.
- 1:02:54And like now, like during my internship, like that has become so second nature that like I can actually do Git things and it doesn't hinder collaborating in like the real world.
- 1:03:04Yeah.
- 1:03:04So it was a lot of like just practice that like, I just thought it was cool.
- 1:03:07Like, I didn't realize until now that was like, wait, like this is actually just like years of practicing on my own.
- 1:03:13And so like, in that sense, like, and like Microsoft essentially saved GitHub and like, it's just as good as ever.
- 1:03:20So like, yeah, plus plus one for GitHub all the way.
- 1:03:24Yeah.
- 1:03:25Awesome.
- 1:03:26I totally agree.
- 1:03:27I totally agree.
- 1:03:27Okay.
- 1:03:28This is really interesting.
- 1:03:30I think there's a lot of concrete advice here.
- 1:03:32I'll link to the papers.
- 1:03:34I'll link to your PyRoot project.
- 1:03:37The code smells thing, all that.
- 1:03:39We'll put all this up there and people can come back and definitely dig into the details if that's useful for them.
- 1:03:46So before we get to the final bit of the show, though, I've got to ask you the two questions, Dan.
- 1:03:51First of all, if you're going to write some Python code, what editor do you use?
- 1:03:55So I used to use Emacs with LPy and now I am now a VS Code convert.
- 1:04:01They've brought you over.
- 1:04:03You know, I would say like the last four shows that I've had, everyone has said VS Code, which is pretty interesting.
- 1:04:08Yeah.
- 1:04:08I was pretty reluctant until like I had to write some Python code and I was on, I switched over to my Windows machine and I was like, I don't have any way to edit code right now.
- 1:04:21Let's just try this thing.
- 1:04:23And, you know, it worked.
- 1:04:25And so like I was pretty happy with it.
- 1:04:27So I sort of just hung around.
- 1:04:29What's actually really cool is the screen sharing ability in VS Code that does pair programming.
- 1:04:36Yes.
- 1:04:37That live, I think it's called Live Share.
- 1:04:39I've never had a good chance to use it, but I've seen it and it looks amazing.
- 1:04:42Yeah, I've used it with one of the other interns and it's like, this is really cool.
- 1:04:47And they also have like a voice communication mechanism.
- 1:04:49So like yet another way to like do voice chat, but at least the screen, like the live coding part, like that was super cool.
- 1:04:57Very nice.
- 1:04:58Yeah.
- 1:04:58Okay, great.
- 1:04:59Definitely a good answer for the editor.
- 1:05:01Packages, some notable ones.
- 1:05:02The package that notable that I haven't heard on the show yet is one called Pie Janitor by Eric Ma.
- 1:05:08And he works at Novartis.
- 1:05:11And this is pretty much his consolidation of pretty common data cleaning stuff in pandas.
- 1:05:19And that ties to another package by Zachary Saylor called Pandas Flavor, which is a wrapper around your ability to extend pandas.
- 1:05:28And the benefit of that is, you know, if you want pandas to have a method that you don't already have, like you might think like, oh, let me create another class.
- 1:05:36I'll inherit pandas and I'll release a package.
- 1:05:38But no one's really going to use that because it's not a pandas data frame object.
- 1:05:42It's like some weird class that you created yourself.
- 1:05:44And so like this is sort of like a mechanism for you to inject your own methods into a pandas data frame object, but still have a pandas data frame object without having to re-extend the class.
- 1:05:57So it's super cool.
- 1:05:58Yeah, that's really great.
- 1:05:59And yeah, the Pie Janitor, I really like that one.
- 1:06:02It takes a whole bunch of imperative data frame operations and turns it into a really nice fluent API like data frame dot from dictionary dot remove columns dot drop not a number drop, you know, rename call and just boom, just flows it all together.
- 1:06:18It's really nice.
- 1:06:19I haven't covered on the show, but we did talk about it over on Python bytes that podcast.
- 1:06:23So, yeah, it's definitely a cool one.
- 1:06:25It's been on my radar as well.
- 1:06:26Nice.
- 1:06:27All right.
- 1:06:27Well, final call to action.
- 1:06:28People who are out there, maybe they're in science, data science.
- 1:06:31It's something like that.
- 1:06:33And they want to make their code take, you know, that 10% step you're talking about towards the more proper engineering structured world.
- 1:06:42What do they do?
- 1:06:43For me, like I was lucky enough to be in New York City, which is a big city.
- 1:06:46So it was always like local meetups were always like a thing that were very busy and you learn a lot from there.
- 1:06:53But even if you don't live in a very big city, you can either start one yourself because chances are you are not alone.
- 1:07:01And the Python community is super supportive.
- 1:07:04You can always if you say something on Twitter, someone will give you the ways of how to start something.
- 1:07:10And if you're at a university, you can always have meetings in like a classroom or something.
- 1:07:16So don't worry.
- 1:07:17Right.
- 1:07:17Maybe it has an interdisciplinary, right?
- 1:07:20Like maybe there's not that many people in your department.
- 1:07:21But if you go across, you could probably find a decent number of folks you want to attend.
- 1:07:26Yeah.
- 1:07:26And so meetups are a great way to like learn or meet other people or at least just like ask questions about stuff.
- 1:07:33And if you can make it to like any of the Python conferences or like attend a sprint like that is probably like going to a sprint was like the fastest way that I've became a better Python programmer.
- 1:07:46Or even if it was something as like editing a piece of documentation, like just seeing the mechanism of how other people collaborate on such a large scale and then still seeing your work like in one of these major projects like that's super motivating and like cool.
- 1:08:02Yeah, that's really cool.
- 1:08:02Yeah, it's a great opportunity.
- 1:08:04And it's also a great opportunity to, you know, rub shoulders with really prominent people in something that you're working with, right?
- 1:08:12The maintainers of this probably important project who are there and, you know, what better chance to get to know them a little bit than to sit down and like add a feature with them or spend a day in the room with them.
- 1:08:23Something like that, right?
- 1:08:24That really can build some connections that, you know, especially if you're in a small town somewhere and not meeting them in person, that could be a challenge.
- 1:08:32Yeah, and a lot of people stay within Python because of the community.
- 1:08:36So like, I guess my final call to action comes from Greg Wilson in his book called Teaching Tech Together.
- 1:08:43He talks about the rules of teaching how to program or like building community.
- 1:08:48And the first rule is be kind, all else is details.
- 1:08:52Yeah, that's great.
- 1:08:53Be kind to all else's details.
- 1:08:54I agree.
- 1:08:55It's definitely right up there is one of the most important ones.
- 1:08:58All right, Dan, thank you for being on the show.
- 1:09:00It's been really great to talk about these ideas with you.
- 1:09:03I think there's a lot of good advice people can take away.
- 1:09:05Yeah, it's been great talking with you, Michael, as well.
- 1:09:07You bet.
- 1:09:08Bye.
- 1:09:09This has been another episode of Talk Python To Me.
- 1:09:11Our guest on this episode was Daniel Chen and has been brought to you by Indeed and Rollbar.
- 1:09:16With Indeed Prime, one application puts you in front of hundreds of companies like PayPal and VRBO in over 90 cities.
- 1:09:24Get started at talkpython.fm/Indeed.
- 1:09:28Rollbar takes the pain out of errors.
- 1:09:30They give you the context and insight you need to quickly locate and fix errors that might have gone unnoticed until users complain, of course.
- 1:09:38Track a ridiculous number of errors for free as Talk Python To Me listeners at talkpython.fm/rollbar.
- 1:09:44Want to level up your Python?
- 1:09:46If you're just getting started, try my Python Jumpstart by Building 10 Apps course.
- 1:09:51Or if you're looking for something more advanced, check out our new async course that digs into all the different types of async programming you can do in Python.
- 1:09:59And of course, if you're interested in more than one of these, be sure to check out our Everything Bundle.
- 1:10:04It's like a subscription that never expires.
- 1:10:06Be sure to subscribe to the show.
- 1:10:08Open your favorite podcatcher and search for Python.
- 1:10:11We should be right at the top.
- 1:10:12You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the direct RSS feed at /rss on talkpython.fm.
- 1:10:21This is your host, Michael Kennedy.
- 1:10:23Thanks so much for listening.
- 1:10:24I really appreciate it.
- 1:10:25Now get out there and write some Python code.
- 1:10:27I'll see you next time.
- 1:10:47Bye.