368: Amazing That Any of This Works

Transcript from 368: Amazing That Any of This Works with Al Sweigart, Elecia White, and Christopher White.

EW (00:00:06):

Welcome to Embedded. I am Elecia White, here with Christopher White. Today we'll be talking about Python, no, not the snakes, with Al Sweigart.

CW (00:00:18):

Hey, Al. Thanks for joining us.

AS (00:00:19):

Hey, thanks for having me.

EW (00:00:21):

Could you tell us about yourself as though we met on the first day of PyCon at lunch?

AS (00:00:29):

Yeah. Hey, I'm Al. I'm mostly known for writing Python books. The most well-known is "Automate the Boring Stuff with Python."

AS (00:00:38):

And my 15 second description of that book is, it's sort of a programming book for complete beginners, who may or may not want to become software engineers, but they're just office workers who are dealing with Excel a lot.

AS (00:00:53):

But they'd like to learn how to program so they could write little scripts to automate all the little boring tasks that they have to do day to day.

EW (00:01:02):

Excellent. So I want to talk more about Python and the books you've written, but first we want to do lightning round where we ask you short questions, and we want short answers. Are you ready?

AS (00:01:12):

Yes.

CW (00:01:14):

Favorite Python module.

AS (00:01:17):

PyAutoGUI which is also my own module, but the more I've worked on it, the more I've come to really love it.

EW (00:01:25):

What Python module doesn't exist that you wish did?

AS (00:01:31):

Oh, something that can very intelligently scan for people's resumes and then convert it to a standardized electronic format.

CW (00:01:42):

Virtualenv, Conva, Dock - bleh. Virtualenv, Con - I can't save Conda after virtualenv. [Clears throat].

AS (00:01:49):

No one can.

CW (00:01:51):

Virtualenv, Conda, Docker, or other?

AS (00:01:54):

I tend to use virtualenv especially with the pipenv module. I think pipenv was finally the module that made virtual environments easy enough for me to actually use day to day.

EW (00:02:07):

Should Python be the first programming language that someone learns?

AS (00:02:12):

Absolutely. Python's a great programming language for beginners to learn, because it has such a gentle learning curve, and it hides away a lot of the complexities of programming. But at the same time, it's still a serious programming language that professionals use at software companies.

CW (00:02:28):

How often do you use the Python debugger?

AS (00:02:31):

Not as often as I should. I've written a blog post about this, where I know...just adding print function calls is probably not the best way to debug things, but it's quick and easy, so I tend to do it. But yeah, the Python debugger is really handy.

EW (00:02:47):

What's the worst part of the Python programming language?

AS (00:02:52):

I guess that Python is called the second best programming language for any application. Python does a lot of great things, but usually there are certain domains where there's going to be a better option.

AS (00:03:07):

But it's sort of like a pocket knife, where there's probably better knives or screwdrivers, but as far as just having a Swiss Army knife with all these tools, it's really portable and handy. Python is really useful for a lot of different areas.

CW (00:03:25):

Favorite fictional robot.

AS (00:03:28):

Johnny 5 from Short Circuit. Mostly because he's an actual robot and not just CGI.

EW (00:03:36):

Jupyter Notebooks, yay or nay.

AS (00:03:39):

Yay. I really like Jupyter notebooks, even though I haven't used them very often. But they're a great way, especially for people working in data science, or in academia, to share their code with other people and just get introduced to Python in general.

CW (00:03:57):

Do you have a tip everyone should know?

AS (00:04:00):

Yes. So the best way to use the Python debugger is with the pdb module. And it used to be that you would have to import pdb and then call the set_trace function, but now you can just call the breakpoint function, which is just a global built-in Python function now.

AS (00:04:22):

And being able to put that inside your source code and then rerunning your program to start the debugger at a certain point is really handy.

EW (00:04:31):

And then, kind of, maybe not a lightning round question, maybe, but how many versions of Python instances or versions of Python do you have on your computer right now?

AS (00:04:43):

I'm going to need some time to count this. 2.6, 2.7, and then probably three or four versions of Python 3, or five. So probably about six or seven.

CW (00:04:54):

But do you have a Python 4 on there?

EW (00:04:58):

Yes, it's my secret project that I'm working on.

EW (00:05:02):

Tell me about AutoGUI.

AS (00:05:05):

Oh, PyAutoGUI. It's a module that I developed that allows you to control the mouse and keyboard from a Python script. So there's a lot of software applications that don't really have APIs, but you can have a program just click on buttons automatically or fill in forms.

AS (00:05:24):

And so being able to use something like PyAutoGUI to do GUI automation, GUI as in graphical user interfaces, is really handy.

AS (00:05:35):

And I feel like a lot of computer users, especially, in addition to computer programmers, would really benefit from this when they have some boring, repetitive tasks that they just want to automate, just clicking a bunch of check boxes automatically, or filling in forms automatically.

AS (00:05:53):

PyAutoGUI can handle things like that. I specifically made it so that it runs on Windows, and Mac, and Linux, and also on Python 2 and 3, so that you don't have to dig up different instructions for different platforms.

EW (00:06:09):

When you said GUI, I actually skipped one of our lightening round questions, which was, what is the best module for making a graphical user interface? So maybe I should go back to that, because that comes up a lot.

AS (00:06:23):

Yeah. So I think of it as a tie between wxPython, which are the Python bindings for wxWidgets, mostly because they have such great documentation, and then there's also Tkinter, or Tk inter. And it's actually not great as a GUI framework, but the great thing about it is it comes with Python.

AS (00:06:49):

So if you have to just create some very basic, rudimentary Python GUI, then using Tkinter is actually great. Because then you can skip that step of having the user install an additional library.

CW (00:07:04):

Is that based on Tcl/Tk, which I was using -

AS (00:07:07):

Yes.

CW (00:07:07):

- 30 years ago?

AS (00:07:09):

Yes, exactly.

CW (00:07:10):

Oh, God. Okay.

AS (00:07:10):

And so it has a lot of legacy, leftover stuff from that era.

EW (00:07:16):

When I started looking at your books for this podcast, I picked up "Automate the Boring Stuff," and it was weirdly familiar. You have your books online for free?

AS (00:07:31):

Yes.

EW (00:07:32):

I must've read the free version and then just recently picked up, well, from Safari Online, from O'Reilly. But I was thoroughly confused, because I'm like, "Where's this happening?"

AS (00:07:45):

Yeah...That got started sort of by accident, but it was a very fortunate accident. I was working as a software engineer, and when I started writing books, I would just put them online for free under a Creative Commons license, which makes it free to share and distribute non-commercially.

AS (00:08:06):

And I didn't really consider writing or being an author as my career. I just thought, "Well, this is something that I like to do for fun as a side hobby." And people seemed to like it. So I eventually self-published the first few books I wrote and just kept doing that more and more.

AS (00:08:25):

And it turns out to be a really good thing, because having it free online allows people to build up that word-of-mouth promotion for it.

AS (00:08:36):

Otherwise...all of my books would have just been yet another self-published book sitting on an Amazon entry somewhere, and nobody really has ever heard about them, or nobody would really know about them.

AS (00:08:47):

But mostly my aim with my career is, with the success of "Automate the Boring Stuff," I'm able to have this as a career, but at the same time, I don't want to really maximize the amount of money I make from it as much as maximize the number of people who can access this, and just lower the barriers to access so that other people can learn how to program.

EW (00:09:15):

And you've written several Python books that I know about. "Automate the Boring Stuff," "Beyond the Basic Stuff," "Cracking Codes," and "Invent Your Own Computer Games." Are there others?

AS (00:09:27):

Yes. I wrote one book, called "Coding with Minecraft," that uses a Minecraft mod called ComputerCraft that adds these programmable robots into the Minecraft world. So...I joked, I called it "Automate the Minecraft Stuff," but that uses the Lua programming language to program the robots.

AS (00:09:51):

...Oh, and I've also written a Scratch programming book as well. Scratch is this programming environment made for kids. You can go to scratch.mit.edu, and it's completely free and runs in your web browser. And you can just easily make these simple little programs with their code blocks that you snapped together.

EW (00:10:16):

And it's a really cute language. It's easy to build stories.

AS (00:10:20):

Yeah.

EW (00:10:20):

Because you can do characters and build actions of people or characters. I like Scratch a lot.

AS (00:10:27):

Yeah. The more I use it, the more I realize they made a lot of really smart choices with that programming language. I read one paper about it, and it pointed out that there's no error messages in Scratch. They have some pretty sensible default behaviors, but kids most of the time will have a hard time typing out code.

AS (00:10:51):

And so with the snap-together blocks, they can mostly just use the mouse. But whenever I've tried to teach, especially young kids, programming Python, one hurdle that they always have trouble with is typing out code and then getting some error message.

AS (00:11:06):

And then they feel bad because they got the programming wrong, and then they feel like they're dumb, and they can't do programming. But by not having error messages, and just having unexpected, and sometimes really hilarious, weird behavior from their programs, then they're encouraged.

AS (00:11:24):

Like, "Okay, that didn't do what you expected. Why did it do that?" And you can look at it. It doesn't just shut them down immediately with some inscrutable error messages about missing a semicolon or something like that.

EW (00:11:35):

I mean, there are compiler errors, and it sounds like they've eliminated those. And there are runtime errors. And then there are the semantic errors, the ones that are based on a failure of understanding.

AS (00:11:47):

Yes.

EW (00:11:48):

Have they eliminated the runtime errors as well?

AS (00:11:51):

Exactly.

EW (00:11:52):

Oh, that's genius.

AS (00:11:54):

Yeah. I mean, it'll have some odd moments. I think if you try to divide something by zero, it'll...just evaluate it to zero, or some other strange message, but there's no part where your program will crash and just stop running.

AS (00:12:12):

It might start doing something you really didn't expect, but, yeah,...the Scratch programming environment has never told you that you have failed.

EW (00:12:25):

Yeah, I could get behind that. That would be nice. Okay. So one of the things people say about Python is that it takes a day to learn and years to master. Do you agree with that?

AS (00:12:43):

Oh yeah. I'd certainly agree with that. I gave one talk about the Python object model and all these low-level details about Python. And one of the things that I mentioned in that talk was that I've been using Python for over a decade.

AS (00:13:02):

I had even had jobs at companies where I'm programming Python professionally, and just a lot of these small details and tiny misconceptions, that really didn't affect the code I was producing, but at the same time, I was completely unaware of how everything in Python is an object.

AS (00:13:23):

So even integers and floating point numbers are still considered objects in Python and how this affects assignment and other small details. And these are things that you're almost certainly never going to encounter except in really weird, esoteric cases.

AS (00:13:42):

But I guess it's a credit to the language that Python can still be so useful, even with just a light understanding of it. And that's what I mean when I say that Python has such a gentle learning curve.

CW (00:13:56):

One of the things that I see a lot, and I've done myself, is consider myself a competent programmer in C or C++, or what have you. And then when I come to write a Python script, I just kind of throw all that to the wayside and write something that's not very well-written from a design standpoint and architecture standpoint.

CW (00:14:18):

I don't think it's the language's fault. Is it just that I'm writing a script so I don't have to care? Or is it a personal failing? Or is there something about Python that makes competent programmers just kind of?

AS (00:14:30):

Yeah. Python is a really versatile language and a really simple language. I'm really glad that they've added features such as type hints, so that if you're going to create massive production systems that have to be running 99.99% of the time,...the newer features of Python help you do all of that.

AS (00:14:53):

But for the most part, as Randall Munroe of xkcd, the webcomic, said, " 'Hello world' in Python is one line." And there's nothing like in Java, where you have to type, public class, hello, public static void main (String args bracket bracket).

AS (00:15:12):

And then, if you're learning to program, you have no idea what all of those words mean, and it's just sort of mysterious abracadabra. But with Python, it's fairly easy to get started and then just sort of knock something out.

CW (00:15:27):

Yeah.

AS (00:15:27):

And...I feel like that's a lot of what casual programmers and hobbyists really just sort of need from a programming language, is making the simple stuff simple.

EW (00:15:42):

But I've been using Python a lot in the last three years.

CW (00:15:50):

Professionally. Both of us.

EW (00:15:51):

Professionally. But I just found out about dunders about a month ago. There are so many things about the language that seem hidden. How do I learn those?

AS (00:16:07):

The precise answer to that, it's not a book that I wrote, but a book from O'Reilly, called "Fluent Python" by Luciano Ramalho. And it's about, I think a thousand pages maybe, or 800 pages.

AS (00:16:23):

But it's the book where once you've learned all the basics of Python, and you have a lot of knowledge and experience, but you want to learn all these tiny little details.

AS (00:16:39):

This is the book that goes into all of these details about the Python language, that sort of, most of the time, really only core Python devs know or people who have a decade of experience with Python will know.

AS (00:16:56):

Outside of that, probably the PyCon videos are a great way to learn about tiny little features of the Python language. So PyCon's talks are all recorded, and they put them online for free. And I find those are a really useful source.

AS (00:17:13):

But "Fluent Python," which I have on my shelf, and I've mostly read most of it, I sort of take bits and pieces of it at a time. But that one book is really sort of what takes you from intermediate to advanced Python programmer in my view.

EW (00:17:35):

One of my problems is that I am also in the same level of competency with TensorFlow, and I'm a little bit more confident with NumPy, since I've had a lot of MATLAB in my history, but sometimes I don't know which one I'm asking about.

EW (00:17:57):

Could you write a book that can, no, wait, what was my question there?

CW (00:18:02):

How do you tell when you're using NumPy, and how do you tell when you're using Python? I don't know.

EW (00:18:06):

How do you go from typing at it -

CW (00:18:11):

Yeah.

EW (00:18:11):

- and printing, to being relatively confident you are using the right, I don't want to say accent, being truly Pythonic.

CW (00:18:21):

Idiomatic?

EW (00:18:21):

Idiomatic Python.

AS (00:18:24):

I'll tell you as soon as I figure it out.

EW (00:18:28):

Okay. [Laughter].

AS (00:18:28):

So with the latest book that I wrote, "Beyond the Basic Stuff with Python," I wanted to write a book that was sort of bridging the gap between beginner and intermediate, as far as best practices, not just with Python, but with coding in general.

AS (00:18:47):

And so I write a little bit of how to write good comments and...the basics of just using Git, and source control, and why you would want to use that. And then I also go into sort of Python object-oriented programming, and why do we even have classes, and that sort of thing.

AS (00:19:04):

And throughout that book, I try to just hedge my bets, because whenever I give any advice, or say things are best practices, I just say, "Well, these are what I consider to be best practices."

AS (00:19:19):

Because in the back of my mind, while I was writing that book, every time I finished a paragraph, I kept thinking, "Okay, what are the one-star Amazon reviews going to say?"...Like, "Well, Al Sweigart says this, but really he's wrong because of some other things."

AS (00:19:33):

And I think the more experience I get with software development, the more I realize that nobody really quite knows what they're doing, and it's amazing that any of this works as well as it does.

AS (00:19:47):

I mean, yeah, you can even find the large corporations like Google or Amazon, and AWS will have some outage that causes half of the internet to just suddenly break for an hour or so.

AS (00:20:01):

And you begin to realize that software engineering involves a lot of duct tape, as well as a lot of very principled engineering, and careful analysis and construction. But then also a lot of times we're sort of just winging it.

EW (00:20:25):

I kind of hate that. I hate writing duct tape scripts. I mean, I guess part of it is, with working with embedded, and microcontrollers, and having to use every cycle or squeak out every nanoamp, or try to find the last 10 RAM bytes so that I can do something new.

EW (00:20:50):

I don't like the uncertainty. I like to have more confidence that I've done the best that I could do, which is at least in the top 10% of what anybody else could do.

AS (00:21:04):

Oh, yeah.

EW (00:21:05):

That didn't come out right. It's at least as good as most people.

AS (00:21:09):

Yeah. But I completely understand that. Because I feel that way all the time too.

AS (00:21:15):

Especially if I'm working on, I'm typing on some mobile app, and the screen doesn't refresh as fast as I'm typing, which seems like with all the computing power we have, the screen can't put letters on the screen as fast as I'm typing.

AS (00:21:32):

And that seems like a very 1980s sort of computer problem. And I just wish, "Oh, I wish things were just a lot more simpler, where we just had the text-only displays, and ASCII art boxes for the graphics, and things like that."

EW (00:21:53):

Gopher and Lynx forever.

AS (00:21:55):

Yeah, exactly,...but then I realized, "Well, that's probably just because I'm a product of my time," and it's like, "Well, I started learning programming in the 1990s, and that was the best time for programming."

AS (00:22:11):

But at the same time, yeah, I guess it's rather unfortunate that, just, technology and computing keep progressing. Some of the things that I say to myself to reassure myself that it's okay, "As long as it works, it works, and it's fine. And it doesn't have to be absolutely perfect."

AS (00:22:31):

I remind myself that this sentence that I'm speaking right now has taken up literally billions of nanoseconds. So why am I spending so much time worrying about getting every single nanosecond of performance out of my code?

AS (00:22:48):

There's certainly domains where this is very important, embedded being one of them, or if you're writing the code for a nuclear reactor on a Mars Rover, or something like that, you need to make sure it works. And you need make sure that it's performant.

AS (00:23:05):

But just coming from my point of where I am, where I'm writing Python code, and I'm writing desktop applications, you don't have to have everything work absolutely at the best level that it can.

AS (00:23:24):

And really when I'm programming, and I'm writing out code, I tell myself "It's likely that within two or three years, the code I'm writing right now will be forgotten or replaced. It's not like I'm building some cathedral that will last for centuries and centuries."

AS (00:23:43):

So really it just has to work, and it has to work correctly. And it has to work fast enough...There's the saying that software engineer time is so much more expensive than CPU time.

AS (00:23:59):

And if you can get your algorithm to run a few milliseconds faster, but it takes eight months to get there, have you really saved all that much time?

EW (00:24:12):

Yeah. And I mean, that definitely depends on what you're doing. And most of the time the answer is "No, it was not worth it." I picked up your "Beyond the Basic Stuff," and your description of it was right on. It was still for relatively new programmers.

EW (00:24:32):

It was a nice introduction to engineering as opposed to just Python. And I appreciate the beyond fluent, or the "Fluent Python" to look at, because I think that will be interesting.

EW (00:24:53):

One of the things that I finally untangled in my brain from your book was iterators and how they work. Can you describe that for everyone?

AS (00:25:05):

Okay. So this is another part of Python's gentle learning curve, is when you learn about for loops, and normally the syntax is for i in, and then you call the range function, and you pass 10 to the range function. And this will cause the for loop to loop 10 times.

AS (00:25:26):

And you just learn about that. And it's like, "Okay, if I have some code that I want to run a certain number of times, then I can use a for loop with this range function." And that's about it, you have that high-level understanding.

AS (00:25:38):

But technically, you can think of the for loop in Python as sort of a for each loop, where, in JavaScript, if you have an array of various values, and you want to run some code assigning that i variable to each of the values in that array, then you could use a for-each.

AS (00:25:59):

And that's sort of how Python's for loop works, but since it's not called for-each, you don't really think of it that way. You just remember range function. That's all I need to know.

AS (00:26:10):

So technically the range function returns an iterable, because it is an iterator?...Even I get the terminology confused, but it returns an iterable object. And this object basically says, "Okay, every time you want to get a value from me, I can give you the next value."

AS (00:26:36):

So this works for strings, which are just sort of one single character after another, or it can work for lists, where it's one element in that list after another. An iterable basically iterates over all of the objects, and it can return one object, or one element, from this collection of data at a time.

AS (00:26:59):

And it'll go through it once. And then when it reaches the end, it's exhausted itself, and you'd have to get a new Iterable object.

AS (00:27:06):

And so a range object returns this iterable object that can just go through the integers zero through nine, if you call a range 10, for example. And this is kind of nice, because in the old days of Python 2, I believe the range object would just return a list with the integers zero through nine.

AS (00:27:27):

And normally that was fine, because that's not that much data. But what if you want it to do something 1 billion times? And then the range object would return the integers from zero to one less than 1 billion, which would just take up gigabytes of memory.

AS (00:27:44):

And if you think about it, well, you don't really need to store all of that data in memory. You can just have the iterable object remember, "Oh, okay. I last returned the integer three and the next one I'll return is four. And also then I'm going to stop...after I return nine."

AS (00:28:03):

And so in Python 3, the range function was changed to have this behavior using iterables. And, again, all of this stuff is, these are details you don't necessarily have to know if you're a beginner or intermediate.

AS (00:28:23):

But once you do know this, then you can start thinking about, "Oh, and so I can also use a for loop to iterate over all the keys or values in a dictionary, because that can be an iterable object. And I could make my own iterables."

AS (00:28:44):

Sort of like if you had a function that for some reason you needed to iterate over prime numbers, starting with two, and then going to three, and five, and seven, and so on.

AS (00:28:53):

Instead of creating a list of these prime number integers, you could just have an iterable object, and save a lot of memory on that. And then use the standard for loop for this.

AS (00:29:07):

Or if you've come up with some sort of weird object, I don't know, let's say, robotic octopus, I might have...backed myself into a corner with this metaphor, and you wanted to run some code for each of its eight arms. And you don't want to write some weird custom function.

AS (00:29:26):

Well, you can just have the robotic octopus object return an iterable object that returns each of its eight arms in a sequence. And then, that way you can just use Python's regular standard for loop with your robotic octopus object.

EW (00:29:48):

For arms in octopus.

AS (00:29:52):

Yeah. And...the code that you can write ends up reusing a lot of Python's built-in syntax, things like for loops in this case.

AS (00:30:04):

And there's a lot of things where, once you start learning about iterables, or the dunder methods, you find that...you don't have to reinvent a lot of these things, because they already exist in Python.

AS (00:30:16):

Which not only keeps your code short and simple, but it also just establishes this sort of standard convention that other Python programmers can see. And instead of inventing your own conventions, that then your teammates, or other contributors to your project, would have to learn about.

EW (00:30:42):

One of the things that made it click for me, and I know iterators are in C++, and I've seen them there, but I've never thought about them other than you can run for loops over them. But you had a variable equal to the range(10).

EW (00:30:59):

And so the variable R equal range(n). Then you call R.next and it gives you zero. And you call R.next, and it gives you one. R.next gives you three. Until you get all the way to nine, and then when you call R.next, it throws an error.

EW (00:31:19):

And so inside the for loop, that's what it's doing. And I just had this little tiny bubble go off in my head that said, "Oh," and then it linked over to generators, which are a different thing, and made those make sense. Anyway.

AS (00:31:36):

Yeah, it's a lot of these things where you use it, I guess it's sort of like driving a car, and all you need to remember is to keep the gas tank full,...but then you can start learning about how everything starts working under the hood.

AS (00:31:53):

And Python is really great about hiding all of those details from you if you don't need to know about that stuff, say if you're just learning how to program.

AS (00:32:03):

But then later, once you do need to start paying attention to performance, or writing a quote, unquote, Pythonic code that the other Python programmers on your team will understand. That's when learning all of these details really starts coming together.

CW (00:32:21):

And that's one of the difficult things, because when I started out writing Python, I was just doing the things I knew from C, for loops and what have you. And they tended to be sort of slow.

CW (00:32:33):

Python offers a whole bunch of much more compact and advanced features for doing things to collections of objects. List comprehensions, and the map function.

CW (00:32:47):

...At what level do you think people need to know about those sorts of things? Can most people get by without ever learning those? Or are those things that everyone should really learn, because they're much faster and much more compact code?

AS (00:33:01):

I feel like everybody should learn them, but not today. And you can just keep telling yourself that day after day after day, probably. Where, yeah, it's something that you should eventually learn, but you shouldn't feel bad if you don't know about it.

AS (00:33:18):

And, like I said earlier, I was working at a professional software company as a Python programmer. And there were just all these tiny little details about Python that I actually didn't know. And it didn't really come up all that often.

AS (00:33:36):

But definitely having this deeper understanding of the tools that you're working with can allow you to skip a lot of basic mistakes. I remember one time when I was at my previous job, I was at a code review meeting with other members on my team.

AS (00:33:57):

And we were looking at a web application product for a router where you can set up different pass and block rules for the network traffic that was going through. And...you had all these fields, and we had set up a framework so that we had validators for this.

AS (00:34:15):

So if it says, you need to enter a number between zero and a hundred, and you filled in something else, it would show a little red mark and tell you to correct that.

AS (00:34:25):

And for coming up the rules, for these pass block rules for the network traffic, you could also type in a regular expression, just to find a pattern of traffic that you would want to block, say. And one of my coworkers said, "Hey, can we write a regular expression as a validator for regular expressions?"

AS (00:34:49):

And this tiny, untapped part of my brain from computer science undergrad just suddenly caused me to say, "Actually you can't, because regular expressions, the strings have nested parentheses."

AS (00:35:06):

"And in order to correctly find that, you can't use a regular language. You need a context-free grammar, and a stack data structure to keep track of all of that stuff and blah, blah, blah." And all of this just spilled out.

AS (00:35:17):

And it's like, "Is this the first time I've ever used my computer science degree for something?" And I realized, "Oh, okay," my coworker was just being flippant with that comment. But at the same time I realized, "What if we had thought, 'Oh, we should write a regular expression that can match regular expressions strings.' "

AS (00:35:38):

We could have spent weeks on that without realizing that actually, theoretically, no, you can't do that. It's impossible. You can probably get something really over-engineered, and spend a lot of time trying this out.

AS (00:35:53):

But in the end, technically, this is an impossible task. Regular expressions just aren't that expressive enough, I guess you could say.

AS (00:36:07):

And so it's these things where, don't have this foreboding sense of doom, but there are times when having a little bit of knowledge can save you a ton of work. So it is useful to do a deep dive into the tools that you're using.

EW (00:36:28):

Talking about limitations, you're going back to limitations. You have a GitHub repo, games by example?

AS (00:36:38):

Yes.

EW (00:36:40):

Tell us about that.

AS (00:36:42):

So this was something that I started, I think around 2018, maybe before, but it sort of came out of my first book, "Invent Your Own Computer Games with Python," where, instead of teaching programming concepts in that book, I just sort of started with the complete source code for a small game.

AS (00:37:07):

And then I would go line by line, explaining all the different parts of Python syntax that came together to make that game work. And I thought, "Well, this is really great."

AS (00:37:17):

Because I remember when I was trying to learn how to program, I would read some programming books that talk about syntax, and they have code snippets, which you can't actually run if you type them into the computer.

AS (00:37:32):

And it just helped me so much more to see a complete example of some program that did something, even if it was just something like a guess the number game. And so I thought it'd be really nice to have a large collection of programs that are like that.

AS (00:37:48):

And so I started creating a whole bunch of those. And over time I started developing all these rules for...how they should be constructed. And so...they should be short, they should be simple.

AS (00:38:04):

If there's a complicated way to do something and a simple way to do something, even if it's a little bit longer, just go with the simple way. Because you're writing code, not for performance, but to be understood by beginners.

AS (00:38:16):

And another one was to limit myself just to text as far as the input and output. So I'd use Python's print and input functions only. So these really wouldn't have graphics per se, unless I started creating some ASCII art kind of graphics, which would work surprisingly well for a lot of different things.

AS (00:38:39):

...I create a small blackjack game, and it has these tiny little cards that I draw on the screen with underscores, and the vertical pipe character. And so after a while, I just started growing this collection.

AS (00:38:55):

And it's actually going to be in a future book that's going to come out later this year. "The Big Book of Small Python Projects," where I didn't really want to go into teaching Python per se with this, but just say, "Hey, here's a giant collection of programs that are under 250 or 300 lines of code."

AS (00:39:17):

You can just copy them yourself, type them in yourself, start modifying them, see how that works. And my hope is that, even though it looks really old-school, because it's all just text, and it's kind of boring, but at the same time, it only takes about 20 or 30 minutes to type these in.

AS (00:39:38):

And if you amuse yourself for 10 minutes, before moving on to the next one, I'd still call that a win.

EW (00:39:45):

That sounds like your childhood, Christopher.

CW (00:39:45):

[Laughter].

AS (00:39:47):

Yeah, this is -

CW (00:39:48):

I mean, I just was about to say, "Well, this is a great idea." Because I haven't seen a lot of this kind of thing recently where-

AS (00:39:54):

Yeah.

CW (00:39:54):

- when I was a kid, you'd get a magazine or something, and it would have a game. And it would be on three pages and you'd type it in BASIC or something. And it was a great way to learn, because everything was self-contained.

CW (00:40:08):

And you didn't feel like you were spending hours trying to get something to work. And these bite-size things are just great.

AS (00:40:15):

Yeah. And part of me is really worried that I think this is great, because this is how I learned to code. And if you think about younger people today, they're probably not even really on laptops so much as on phones and tablets instead, and using social media websites and that sort of thing.

AS (00:40:36):

And so I wonder, "Oh,...am I just old?" And that's why I think this is a good way to learn that program.

CW (00:40:43):

No, you're absolutely 100% correct. This is a good way to program.

EW (00:40:48):

Do you know what a Speak and Spell is?

AS (00:40:52):

Yes, I do. Yeah. And I guess it touches on that problem with software today just being really complicated. I would teach these Saturday morning programming classes to kids, and I'd say, "Hey, okay, we're working in Scratch. What do you want to make?" And they would say, "Minecraft."

EW (00:41:13):

Sure.

AS (00:41:17):

We're going to need more than 45 minutes to do that...Software today is just so complicated, and has so many working parts. And you have to install these libraries, and then the libraries don't work. And you have to debug that and spend time on Stack Overflow.

AS (00:41:31):

Whereas these small text-based games, they're all text. They all exist in one file. You don't have to load other libraries, or have some virtual environment set up, or anything like that. In fact, since it's all in one file, you could just select all, copy, and paste it to pastebin, or to an email, and send it to your friends.

AS (00:41:58):

And I wanted to get rid of as many of the barriers to just getting started and getting something running, even if it was really tiny and not all that fun. But yeah, using just modern game design principles, I'm really pleased with what I've managed to come up with.

AS (00:42:19):

A lot of those old, like Byte magazine, listings of the source code in BASIC for some game, I've gone through a lot of those old books. And I realized that a lot of those games just aren't really all that fun.

CW (00:42:35):

No.

EW (00:42:35):

Blackjack. I mean, that's kind of fun, but also you videotaped yourself writing it.

AS (00:42:43):

Yeah.

CW (00:42:44):

Videotaped?

EW (00:42:45):

Oh, God. I'm so old.

CW (00:42:48):

Were you listening to a phonograph at the same time?

AS (00:42:51):

I don't even know what a videotape or DVD are.

CW (00:42:56):

Did you save it to a floppy disk? Sorry.

AS (00:43:01):

I think archeologists are just going to be digging up our bones, and then finding floppy disk icon that has just transcended millennia. And they'll be like, "We think this might've been part of some fertility cult back then."

CW (00:43:16):

They're going to still be using that icon 3000 years from now. And nobody will know what it means anymore, but it'll still be in all software.

AS (00:43:22):

Like, "Oh yeah, that's just the save symbol." Yeah.

CW (00:43:24):

Yeah. It's like a rune.

AS (00:43:29):

But yeah, I have videoed myself typing the code in for a lot of these really simple programs. And I call it the Calm Programming series.

AS (00:43:44):

Mostly I wanted something that was different from a lot of the other programming tutorials you would find on YouTube. I really like these channels that do furniture restoration, or metal tool restoration.

EW (00:43:58):

Building violins. That's the one for me. Building instruments that are just -

AS (00:44:02):

Yeah.

EW (00:44:02):

They're so amazing.

AS (00:44:03):

And especially the ones where they don't do a lot of talking. There's very little narration. There's one called Primitive Technology, where it's this guy just wearing gym shorts somewhere in Australia. And he makes flint stone axes, and starts fires, and builds mud huts, and everything. And there's no talking whatsoever.

AS (00:44:25):

You can turn on the closed captioning for limited explanation of what he's doing, but these are sort of casual things that I just like to have on when I'm just really tired, and I don't want to pay much attention to how things are working, and just something interesting to look at.

AS (00:44:41):

So, I started creating a video series, where I actually have the camera just on my laptop screen, which requires a whole lot of setup, just to get the camera angled correctly, and the brightness setting on the laptop screen working.

AS (00:44:59):

But you can just see me sort of typing out the code for these really simple 250-line programs. So the first episode was with covering the blackjack program. And the second episode now has me typing out the code for Conway's Game of Life. But you can sort of just see my screen as I'm typing this code out.

AS (00:45:24):

And I make sure that all the programs have a lot of comments to explain what's going on with the different parts. And I wanted something that was just sort of for people who just want to see some content that is very low-key and something that they don't have to really think too hard about.

AS (00:45:42):

And then, after the end of 20 or 30 minutes of this, you have the small little program. So it's a formula that I thought of where it's easy for me to create these videos.

AS (00:45:55):

Because I don't have to do a lot of narrating and talking, which is always a headache when it comes to editing it later. I have to take out every single "uh" and "um" and weird mouth noise that I make into the microphone. But this is just sort of rather soothing.

AS (00:46:09):

You just hear...the typing on the keyboard, and you can just watch as the code comes together. And I'll start running bits and pieces of it as I develop it so that you can see how all these different functions that I write come together to produce this final program.

AS (00:46:26):

And I have a lot of these programs, these text-based games, and animations, and things like that. So I have a lot of material for future videos.

EW (00:46:37):

And so...are you designing them as you're doing this, or are you just sitting down with the design you already have? You aren't just typing out of a already done sheet of paper, are you?

AS (00:46:50):

That is exactly what I'm doing almost.

EW (00:46:53):

Oh, okay.

AS (00:46:53):

...It's sort of like those cooking shows where they mix all the ingredients, but they've all been pre-measured and then...they put it into the oven, but then in the same shot, they pull out a already cooked -

EW (00:47:09):

Yes.

AS (00:47:09):

- thing from the oven to show you. And so it's sort of taking away all the boring parts, and trying to figure out weird bugs or things. But at the start of each video, you see me just setting up my laptop, and getting a cup of coffee, and putting all of that on the dining room table.

AS (00:47:32):

But, what you don't really see is that I have a second laptop that has the source code for the program that I'm writing. So it's a bit unnatural in that it's just me, straightforward, sitting down, and typing out this code. And technically, you could just sit down with the source code yourself and then just read through it.

AS (00:47:53):

But...these aren't so much tutorials as just calm, soothing videos that you can watch when you don't have anything else that you want to watch or have to focus on too much, but you want to see some program come together. Whether or not there's a large audience for this, I don't know, but I guess I'll find out.

EW (00:48:18):

You talked about being a developer. Are you primarily a technical book author now, or do you still do development?

AS (00:48:27):

Yeah, being an author is my full-time career now, as of, I forget, I think it's 2013, is when I left my job as a software developer. And originally I was thinking, "Well, I'll just spend a year to finish up this book I'm working on called 'Automate the Boring Stuff with Python.' I have no idea if this will take off or not."

AS (00:48:50):

But it's sort of like winning the lottery where this has really worked out well for me. Although I'm not really sure how I could give advice for other people to follow the same path.

AS (00:49:01):

But yeah, with the way that book took off, I just thought, "Well, I can spend another year, I have some other ideas for online courses, or videos, or other books that I want to write." And so I would just keep working on those.

AS (00:49:19):

And two years became three years, became four years, and now it's a really great scam, writing programming books. Hopefully I will never have to get a real job ever again.

CW (00:49:32):

It is a good way to become extremely wealthy. [Sarcasm]

EW (00:49:36):

Oh.

AS (00:49:36):

Oh, writing books. Yeah, that's where the big bucks are. [Sarcasm]

EW (00:49:38):

Writing technical books is definitely where the big bucks bucks are. [Sarcasm]

AS (00:49:42):

Yeah, I feel bad, because people will email me on occasion and say, "Well, hey, I have an idea for writing a technical book. Do you have any tips?" And my honest answer to that is, "Well, not really. A lot of it is privilege and luck that has worked out for me."

AS (00:50:00):

I was working as a software engineer at a company that I still had time after work. I wasn't working 60 or 80 hour weeks at a startup or something like that. I had my health, and I had free time, and "Automate the Boring Stuff with Python" was actually the fourth book I wrote.

AS (00:50:22):

I first got started in 2009, just sort of creating tutorials that turned into books. And people liked the first one so I wrote another one, and people liked that so I wrote another one.

AS (00:50:32):

And so I had all this practice writing books, and then also...early 2010s, mid-2010s, that's sort of when Python really started taking off as a programming language. So I had several years of experience with this programming language.

AS (00:50:52):

And I just had sort of, out of sheer luck, picked the right horse in this race that turned out really well. So I try to keep that in mind, before I let my ego grow too large, that my life could have easily taken a different path, and I would just be some software developer that nobody had ever heard of.

EW (00:51:22):

Which isn't a bad thing.

AS (00:51:24):

Oh yeah. Yeah. Oh, for sure. But whenever I go to PyCon, I kind of feel like a rock star, because people have heard of this book that I've written, and sometimes want my autograph, or take selfies with me. And at first I thought, "That's weird. What?" But then I learned, "No, no, just do it."

AS (00:51:44):

People get a kick out of it, I guess. But people also tend to think I'm a much better programmer than I actually am. They'll ask me for advice on machine learning, and AI, and I actually have no experience with that. But I could make stuff up and speak authoritatively on it if you want.

EW (00:52:07):

Have you had any experience with CircuitPython, or MicroPython, or other embedded Pythons?

AS (00:52:13):

Very little. I remember telling my friend a bunch of the hobbies that I have, which were just sort of writing software, and writing these tutorials, and things like origami or -

EW (00:52:27):

Wait, wait, origami? Let's just stop everything and talk about that.

AS (00:52:29):

Yeah.

CW (00:52:31):

Alright, I'll be back. Let you guys talk for a while.

AS (00:52:35):

Yeah...As a kid, I kind of liked doing origami folding. So my background is my father is white, and my mother is Japanese. And so she would show me how to make the crane, and I think a couple other really basic things.

AS (00:52:54):

And then I would find origami books, and I would not be able to do any of the models in there. Because the folding diagrams, there would always be, I'd be five steps in, and then there's some step that I just don't know how to make my piece of paper look like the thing in the book.

AS (00:53:12):

And at that point I was kind of just stuck, and I didn't know anybody else who was an origami master who could help me through this. And then, sometime in the 2010s, I think, I had the thought, "Hey, I bet there's a million origami folding videos on YouTube - "

EW (00:53:30):

Yes.

AS (00:53:30):

" - I can just look up."

EW (00:53:33):

So many.

AS (00:53:33):

And, yeah. And it's great, because now you can make all the really cool models. So I can make a cat, and a T-Rex, and a horse, and things like that,...because you can actually see how they fold each step, and you can rewind and put it in slow motion if you have to.

AS (00:53:55):

And so I've gotten more into that as well. And so, I was telling my friend, "Oh yeah, I like origami, and writing books, and software, and things like that." And she told me, "Well, you know, the one thing that all those things have in common is that they're cheap hobbies."

EW (00:54:13):

It's true. My origami bill is much less than an instrument bill.

AS (00:54:18):

Right.

CW (00:54:18):

Why are you looking at me?

EW (00:54:19):

[Laughter].

AS (00:54:22):

I don't even use origami paper most of the time. I'm just sort of getting stuff out recycling bin just to play around with. And so I feel like I never really got into hardware, because you have to spend money on these components, and on a multimeter, and an oscilloscope, and a bench, and all of these other things.

AS (00:54:45):

And writing books is nice because I can do that, and then put it online, and share it with tens of thousands of people. And I pay about $12 a month for web hosting to do that. It's really great. The same thing with videos, and other types of software, and content like that.

AS (00:55:06):

Whereas if I made a cool blinking light thing in hardware, being able to mass produce that and share it with people is just such a daunting task that I've never really gotten into it.

EW (00:55:20):

Well, let me recommend the Raspberry Pi Pico, which runs the different Pythons and costs $4.

AS (00:55:29):

That is very nice.

EW (00:55:31):

It's a nice little chip [err... board]. It's pretty easy to program, lots of support, lots of community. And $4 is hard to argue with.

AS (00:55:41):

That is so amazing. And it just sort of reminds me, because I hate telling people how I learned to code, because I was one of those kids that had a Commodore 64 kind of computer in the household. And so I started programming in the third grade.

AS (00:55:59):

And people hear that, and they think like, "Oh, programming is like being an Olympic gymnast, you have to start when you're really young. And you just have to have years of experience. And I can't possibly do that." But really at the same time, back in the nineties, when I was learning how to code there was next to no information.

AS (00:56:17):

And today you have Google, and Stack Overflow, and YouTube tutorials, and free books online. There's just so much information, where my head start really didn't count for all that much in the long run and these days, wow, you can get a Python programmable board for $4 and start creating things.

AS (00:56:40):

That's just incredible. I remember just as a young teenager, I was like, "I want to build robots." But I didn't know anything about building robots...I knew how to use a soldering iron, sort of, and I figured out, "Okay, connect batteries to this switch, to this motor."

AS (00:57:03):

...The housing, I just cut apart aluminum cans, but the quote, unquote, robot that I created, was just sort of roll around in not quite a straight line. And then could probably get stuck on the carpet or something like that. And I was like, "This is not as cool as Johnny 5 from the Short Circuit movie that I really loved."

AS (00:57:28):

And...I tried finding books on robotics, but they were just so far ahead of where I was, and I didn't really have anyone to explain things to me. So yeah, I just never really got into hardware or embedded programming at all.

EW (00:57:45):

Well, there's a lot more out there now, should you want to look at robotics. And origami, by the way, is one of the ways that I use Python regularly.

AS (00:57:57):

Oh, really?

EW (00:57:57):

I do curved crease origami, which requires more math in order to know where the curves -

AS (00:58:03):

Oh.

EW (00:58:03):

- are going to happen.

AS (00:58:05):

That's where you...sort of bend the paper instead of folding and creasing it?

EW (00:58:10):

You score it.

AS (00:58:11):

Oh.

EW (00:58:11):

And then you fold along the score, which is curved, which means that your result in the end has to be a piece of paper that's curved.

AS (00:58:25):

Oh, I see.

EW (00:58:27):

...I just finished a giant exploration of snails and how to make snail shells. And it was a whole generative Python thing, because should they be in a plane? Should they be conical? Should they curl up onto themselves just like nautiluses do? There are a lot of parameters. There really are a lot of parameters.

AS (00:59:00):

I'm definitely going to pick your brain after this, because there are some sort of origami software-related projects that I've been wanting to do.

AS (00:59:11):

Mostly as just sort of creating a CAD kind of setup for paper folding so that you can get these animations that you can view any angle. Because the main problem with all the YouTube origami videos is that you have somebody's big human hands blocking the camera shot most of the time.

EW (00:59:32):

Well, you need the SVG files. But there are some origami simulators online. There's one, Amanda Ghassaei, that's how I'm going to say that, has a really good one with some neat patterns, and some of what you're talking about. They don't do as much curved as I do because nobody likes curves as much as I do.

EW (01:00:01):

Yeah. I have a GitHub repository. I'm happy to share pictures.

AS (01:00:05):

That would be great.

EW (01:00:05):

Happy to share SVGs. Okay. I should probably let you go, or we're going to talk a lot more about origami when we probably should still be talking about Python. Al, it's been really fun to talk to you. Do you have any thoughts you'd like to leave us with?

AS (01:00:23):

Yeah. One thing that I really love to plug is just the Python community in general, and also PyCon, the Python conference, which unfortunately is going to be virtual and online again this year due to the pandemic. But PyCon was the first technical conference that I ever went to.

AS (01:00:44):

And I was just really amazed. It was great. And PyCon is also a conference where it's not $2,000 for a ticket, because they expect that your corporate employers are going to pay for it. It's something that ordinary people can go to.

AS (01:01:02):

And even if you're a complete beginner and new to Python or programming, it's a great way to just show up, and people are really friendly. And you can go see some of the talks, but you can also just sort of hang out in the hallways and start chatting with people.

AS (01:01:18):

PyCon has become this way that I see my friends from across the country or across the world every year, except unfortunately for the last couple. And so I really want to encourage people to look up PyCon just for themselves to attend, or even if they just want to watch some of the videos from previous conferences. Those are all online as well.

EW (01:01:45):

Our guest has been Al Sweigart, author of a number of books, including "Automate the Boring Stuff with Python," and "Beyond the Basic Stuff with Python." You can find his books on No Starch Press, where buying the physical copy also gets you an electronic one, or you can get it from one of the other booksellers.

EW (01:02:05):

Also check out Al's website, inventwithpython.com, if you want a preview of those books. And if you want to find his Udemy course, or videos, or whatevers, links will be in the show notes.

CW (01:02:19):

Thanks, Al.

AS (01:02:20):

Thank you very much.

EW (01:02:21):

Thank you to Christopher for producing and co-hosting. Thank you to most of our Patreon Slack group for questions about Python that I did not ask Al, but some of you, Monty Python was not appropriate.

EW (01:02:37):

Also, thank the rest of you for listening. You can always contact us at show@embedded.fm, or hit the contact link on embedded.fm. Now a quote to leave you with, from Nietzsche.

CW (01:02:47):

Geez. [Laughter].

EW (01:02:47):

"The snake which cannot cast its skin has to die. As well the minds which are prevented from changing their opinions; they cease to be mind."