Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds.
This is episode 83, recorded June 20th, 2018. I'm Michael Kennedy.
And I'm Brian Okken.
And Brian, we have a special guest, don't we?
Yes, we do.
Yeah, so Chris Medina is here to join in the fun and share his perspective on things.
Hey, Chris, welcome to the show.
Hey, guys, how's it going? Good to be here.
Yeah, it's good to have you.
And I also want to say thank you to DigitalOcean.
They are a major, major sponsor of the show, and they're sponsoring this one as well.
I'll tell you more about them, but the short version is pythonbytes.fm/DigitalOcean.
Get $100 credit for new users.
Brian, speaking of new users, people learning Python, they might want to set up a server at some point.
Set up a server?
Well, you know, eventually, they'll have a website or something, right, once they learn to code.
But you've got a really nice way for them to get started.
I've always been reluctant to try to look or play with some of these beginning code editors,
because part of learning how to code is learning an editor.
But I'm kind of warming to the idea of some of these specialized editors.
And there's one that I wasn't familiar with, which is Mu.
I think it's Mu or Mu.
I don't know.
Emu.
Code with Mu.
It's got a whole bunch of people on board.
If I go to the...
I can't remember where it is, but there's a bunch of people working on this.
And it's kind of a neat little editor that has...
I was playing with it this morning.
And it has some...
You just open it up, and it has built-in right off the bat.
It asks you if you're going to do it for Python 3, or if you're targeting, like, an Adafruit chip or a micro bit.
So it has built-in targeting of those things, like, right away.
And there's, like...
Along the top, there's some icons for loading and saving files, running it, debugging it, popping up the REPL, interacting with the plotter.
And then there's a...
Even there's a theme button, so you can jump back and forth between dark theme and light theme.
And it even has, like, a linter built-in where you can check things.
And it's all...
Like, you don't have to memorize any key sequences, or there's not really any menus around.
It just is all up there.
And I think this would actually be great for teaching people how to code.
So I'm going to start using it for, like, demos and stuff.
It's pretty cool, yeah.
So as you type, basically, it pulls up both the completion for the arguments that go to a method or something,
but also the documentation, like, while you're typing.
So, you know, you might get annoyed with that if you know what you're doing.
But if you're new, it's really cool to show you, like, hey, you can call these various things.
And here's the arguments.
Here's what it means.
And it even has a debug.
It's pretty cool.
So it's a separate app?
Yeah, it's a separate app.
But you can...
There's an installer available.
So you can just...
You don't even have to tell people about pip right away, because you can just install it with an installer.
But you can install it with pip, because it's just a Python thing.
I think it's a Qt application.
I've been corrected.
It's a Qt application.
Oh, that's really cool.
It's cute.
Okay.
Yeah, I installed it with pip.
And then one of the things that it has in the installation instructions is instructions on how to use...
So when you install it with pip, you get, like, a command line invocation to invoke it.
But it recommends using another package called shortcut, which you just...
If you pip install shortcut, you get shortcut moo editor, for instance.
And it'll create...
On my Mac, it created a...
It just went ahead and created a shortcut in my launchpad to launch it.
So I didn't...
Oh, that's pretty cool.
So you can attach that to any Python script.
You can just launch it from there.
So...
Nice.
Oh, but...
It looks like it has different modes, too.
Like, you can set it up to work for writing code for Adafruit or for Pygame or stuff like that.
That looks kind of neat.
Yeah, and then when it has...
The built-in REPL, if you pop open the REPL, it's not just your normal REPL.
It's a Jupyter notebook or Jupyter REPL, which is pretty cool.
Yeah, that's really cool.
I love this.
Yeah, it's very neat.
And there's some...
Like, on the tutorials link, there's a whole bunch of tutorials that aren't quite there yet.
So I'm guessing they could use some people to help out with this project.
Oh, right.
Yeah, and that'd be a great, easy way to get...
Sort of gets your feet wet and open source is to write some tutorials.
Right?
You don't have too much to depend upon or you probably won't break anybody's code by writing one.
Yeah.
Yeah, nice.
So, Chris, one of the things I find that's pretty interesting is we love to talk about super advanced topics,
but a lot of times it's really what people need or want, especially if you're helping or mentoring someone else,
is like some more fundamental stuff, right?
Yep, I found this kind of an intro topic into how the Python parentheses system works.
It's pretty interesting also for folks that come from other languages,
so you can get kind of like a quick translation from one place to another and how things operate.
It sounds pretty simple.
I mean, it's about Python parentheses, right?
The Python parentheses primer from Reuben Lerner.
Shout out to him on that.
But there's some pretty advanced things going on and a pretty cool feature coming at the end.
Great.
So, tell us about it.
So, the fun stuff is it goes into all the different ways that you actually use parentheses
while you're doing your Python syntax and what you can use it for.
So, you get a couple of the simple topics like, you know, usually a parentheses is used in a callable,
like to call a function and pass in parameters or to call a class that implements callable.
Things like prioritization of operations or conditionals.
You also use parentheses for making tuples.
But you'll also get a little bit into generator expressions and the advantages of that.
And a couple of little quirks you can use it for securing some of the indentation rules that Python has that some people kind of hate.
But other people like me love.
So, I'm special.
You also get into square brackets and curly braces.
And square brackets, which are typically used for lists and for indexing.
For somebody that's new to the topic, you'll want to look at the slices section.
Because slices are a pretty powerful little tool that you get out of Python.
If you haven't used it before, how to get specific sections of a list or iterable.
And there's also, while not in the article itself, but some of the folks that commented on the article also mentioned that some of the new type hint stuff also uses square brackets for identifying,
like the variables, like the variables, like the data type of floats, the data type that it's implementing.
So, if you have a list of floats, you would say list curly bracket floats when you're specifying what type it is.
Yeah, that's really interesting.
It's almost like generics in like C# or templates in C++ where you would say this thing takes a list of strings.
You can say it's just a list.
But then, like, you don't get any help on what's contained within it.
But you could say, you know, import the list thing and say list square bracket int square bracket.
Then it has to be a homogeneous list of ends, not just any random list, which is pretty cool.
Exactly.
I was looking through here, and down at the bottom, they're talking about some of the ways to, like, future-proof your code.
So, like, you can say from Dunder future import division.
And there's a proposal for braces, huh?
Uh-huh.
If you have a Python REPL open right now and you kind of want to try that, you'll get a neat little message of what the future will look like if you ever actually import braces.
Right.
So, if you can see, like, the coming braces feature, you can say from Dunder future import braces and see what support Python's probably going to be adding for braces soon.
Which, you know, not everyone's typing.
They're probably driving this up.
So, basically, it throws a syntax exception error and says not a chance.
We're never adding braces.
It's beautiful.
I knew about anti-gravity.
We were joking about that earlier.
But I did know about the import braces.
That's pretty sweet.
One more interesting thing to mention, obviously, goes into curly braces and how you use that as well for set comprehensions and dictionaries.
The other thing to remind folks is Python 3.6+.
There is f-strings.
And with f-strings, you can use braces inside a string that you prefix with f.
And the interpreter replaces any code that you put inside those braces, actually executes it, and replaces the result into the string.
Yeah.
Very nice.
f-strings are great.
And it's way more efficient.
Fastest and the shortest.
Cleanest.
Very nice.
So, Brian, you mentioned that Mew Editor is a Qt application.
And Qt is kind of making a bit of a resurgence these days.
At least I've been paying more attention to it.
Yeah.
Well, it had gone through some fairly dark times.
I mean, obviously, we've been on our GUI kick for a while.
We're going through all these different options.
And there's still tons of great options.
Like, WX Python had their Phoenix release, which was sort of a rebuilding of that whole thing, which is awesome.
A little while ago, we talked about that.
So, the Qt company, Qt company, is the company that is behind the Qt framework.
And they just announced the official release for Python for Qt, which is taking these various version mismatch, PySide 2, PyQt, all those things, and, like, rebuilding it, like, on a two-year project into a fresh, new, shiny version.
So, that's pretty awesome that that's more or less out.
Oh, that's exciting.
We heard recent, not too long ago, that it was coming.
But having it be released, wonderful.
Yeah.
So, if you're messing with this, it's version 5.11.
And I think it's still in a tech preview in terms of the execution bits.
But I suspect that this means the APIs are frozen.
So, you can go and check that out.
Yeah.
So, it started two years ago.
They've been working on this.
And one thing that's really nice is the way you get it is you just pip install.
And right now, it's not coming out of PyPI.org itself.
It's coming out of a different place.
So, they show you the command, like, to pass their particular sort of dev, their dev index URL, which I put in the show notes.
So, people can check that out.
So, that looks like they have their own PyPI implementation there.
Yeah.
Something like that, right?
Like, a little bit like what you guys are doing that we talked about over on Talk Python, right, Chris?
Yep.
Exactly.
But this one goes outside the firewall, it looks like.
Nice.
So, you know, I just want to give a shout out.
People who are looking, waiting for Qt to become more important to get revitalized.
I feel like, you know, this is the announcement we've been waiting for.
Hopefully, this means lots of good things.
We'll see what happens.
Yeah.
Pretty cool.
Yeah.
For sure.
So, speaking of good things, DigitalOcean, they're very good.
They do all sorts of awesome stuff, right?
They're giving you guys tons of credit if you want to get started there.
And I always try to talk about different ways we are using them in our various bits of infrastructure.
And that's still true.
Still are.
Still enjoying that.
But one of the things that they have that's really cool is when you go create a virtual machine, you can say, I want a one-click app, which really means a virtual machine configured to run something.
And they recently set up a machine learning virtual machine for you.
So, you can go there, create a droplet.
That's their VM terminology.
Click on one-click apps.
Pick machine learning in AI.
And you automatically get a machine with Python 3, R, Jupyter, Notebooks, SensorFlow, PyTorch, all that stuff.
And you can pick, like, a CPU-optimized CPU for your virtual machine.
All sorts of good stuff.
So, if you want to check that out, you can do it real quick and affordably.
Just play around with those tools, which is pretty awesome.
So, check them out over at pythonbytes.fm/digitalocean.
And like I said, if you go there and you're a new user, you'll get $100 credit, which is also pretty sweet.
So, Brian, iteration is a key element of Python, right?
Yeah, we often deal with lots of iteration and generators and iterators and all sorts of things.
Exactly.
We've got generators.
Like, the primary for loop is not for I equals zero, I plus plus, but it's for thing in collection.
It's beautiful, right?
But it goes even deeper.
Developing your own iterators and generators is a fun thing to learn about.
But I have to admit that I don't often do that very much.
And one of the things that I've – it's always been on my to-do list is to learn more about the iter tools module.
And I've not really ever explored it too much.
And real Python has a blog post called iter tools in Python 3 by example.
And I really enjoyed this.
It's a nice introduction as to what is the problem that iterators and generators are dealing with.
And then, like, for example, it starts off with just how does zip work to take two lists and create another list with, like, each element.
And then what if you need, like, not quite the same zip?
What if they're not the same length and things like that?
And some of the problems that can happen and why lazy evaluation is a good thing.
And actually, in the discussion, it used the time function, the time shell function.
I guess this is, like, a Unix-y kind of thing with a dash f, which I've never used before.
And that showed the memory used also.
Didn't know that was a thing.
Oh, that's cool.
But this example of using, like, a big example in the memory usage blew up to, like, huge.
And so just talk through, like, some of the different things that are in iter tools and why that helps you.
I pulled out a couple.
Like, a lot of these things, if you don't know they're there, you'll just come up with it on your own.
Like, doing combinatorial sets.
Those are already in there.
Coming up with permutations, you don't have to do, like, a random index.
You can just use their iterate, the ones that are built in.
I think that's a good example of, like, being fluent in Python versus being able to make the code work but not really deeply knowing it, right?
Like, if somebody said, hey, I want you to take this thing and generate, like, a set of permutations, all the permutations of, say, this word or whatever, right?
You might attempt to implement the fairly complicated algorithm.
Or you might just call iter tools, you know, permute sort of thing, right?
And then, boom, it's just there.
So there's so many things like that that are just, like, at your fingertips if you just import them.
Yeah.
I mean, a lot of people are smart enough to, like, if you come up with all the combinations, you can just have a nested for loop and figure out how to do that.
But you don't have to.
It's already there.
And one of the things I didn't know, there's some fun things like some fun uses of count and cycle.
I didn't know about cycle.
That's pretty cool.
It just kind of cycles through a set and then keeps giving you more of the same.
And the tutorial also talks about, in the tutorial, it's using the next function, which is for iterators.
You don't have to use them in a loop.
You can even just call the next function, which is kind of fun.
And then something that's iSliced.
I didn't know this was a thing.
So if you want to take a slice of a list or a slice of some other iterator, you can do that.
But if you want that to be an iterable also, this does it for you.
And so there's a whole bunch of goodies in here.
Yeah, one of my favorites is iSliced because, like Chris already said, slicing is awesome.
But if you try to do, say, slice on a generator, it'll just go fail.
Like, nope, we don't slice generators.
Well, you can iSliced them, and it works just fine.
It's beautiful, right?
So you sort of make things sliceable that otherwise would not be.
It's really great.
Yeah, so this is definitely a good read to beef up on the iter tools.
Yeah, I recommend everybody that writes any Python code to just go look at iter tools and all the things you can do.
There's lots of stuff online, and this is a good resource.
Every once in a while, every time there's an article that's something about iter tools, I always go just at least skim it.
Because there's always some little magic in there.
You're like, oh, I implemented that myself the other day.
Let me go delete that code and actually do something that's maintained by somebody that knows something more than that.
Yeah, and maybe even in C.
So it's a little faster, right?
Down in the internals.
Nice.
So speaking of sort of working with collections and iterables and things like that, Chris, I feel like your item dovetails really well in with this.
Right.
So the next item is about Python sets and set theory.
It's a nice, also kind of a primer on how sets work in Python, how you divide them, how you work with them, and just a tiny bit on set theory.
So you can build sets different ways.
The most common way is by calling the set function to build an empty set.
But you can also use curly braces, which we were talking about earlier, to have set comprehensions.
And it's a great way of, say, deduping items in a list.
So if you have a list of a bunch of integers or something like that, you can use the comprehension to iterate through the list and just grab the items that you care about, and you only get the unique items out of it.
The interesting part about sets as well is that they're all of one.
So when you're making a membership test, it's fast.
It's considerably faster than going through, say, a list where you have to iterate through the entire list to see if the item is in it.
Yeah, that's cool.
I definitely like the distinct aspect.
That's what I use a lot of sets for.
It's like, I have all these things.
There might be duplication.
I just want them one of each.
You know, if there's duplicate, just don't add it.
Right.
So you can just, like, you could even say set of some list and then, you know, just get that back as here's the distinct stuff in the list, which is great.
Also, for simplifying conditionals.
So you have the example, well, Python had the example, and the stuff you covered about parentheses of if item equals x or item equals y or item equals z or whatever.
Right.
Like that becomes like kind of a nasty multiline looking thing.
Maybe a more Pythonic way of you say if x is in, you know, curly set x, y, z.
Right.
You can do stuff like that.
It's always another point to keep in mind is sets are of immutable values.
So you can't put, like, say, a normal set inside of a set.
There's other value types and stuff like that.
You can't just, like, stick in a set.
Right.
So it has to be hashable, basically same as dictionary keys.
Yeah.
Right.
So it can't change.
But it's always, always interesting to remember once you've made a set that you're looking for distinct values and things like that.
I always try to look through my code if there's some sort of, if I'm checking if item is in list one and also in list two, because you can always have two sets and do an intersection between them.
And then you don't have to do those for loops.
And the code is kind of optimized to do that for you, which I'm sure will not be all of n squared when you're writing your code.
Right.
Yeah, exactly.
And then just to quickly look back on immutability, if you want to make a set of sets, there's also such a thing as a frozen set where the set itself is immutable.
And you can stick that into a set.
So stuff like that.
That's very meta.
Brian, do you use sets very often?
I always have, like, a tutorial like this or something to look up because I forget what the operators are.
Because it does have union, intersection, difference, symmetric difference, things like that.
And remembering all of that stuff from your college days is good to try to make some elegant code.
But it has, like, for instance, the union operator is you can just use the bar function for that.
And looking up all those is a good thing.
Although, since I know I have to look it up, I usually put a comment in there to tell anybody else what I'm doing to say this is a set operation going on here.
Yeah, it makes sense.
I feel like sometimes knowing the right data structure cannot, like, really open people's eyes or really change the way they're doing something.
And sets and dictionaries definitely seem to fall into that space where, like, you don't use them that much.
But when you do, you're like, whoa, that really is better.
So many ways.
Yeah, and it's also one of those things of, like, it's a new hammer.
Try to be sure that you don't see everything as a set problem.
Because everything isn't a set problem.
Yeah, for sure.
All right.
So last item that I wanted to cover on this episode is a little bit of a look ahead, maybe to next week.
But to put this on people's radar so when it launches, you can, like, do maybe a backflip or something if you've been practicing.
So coming up, we just had the release of Python 3.7 Release Candidate 1, which was on June 12th.
And expected or planned for June 27th, which is seven days from now, is the final release of Python 3.7 official.
How about that?
I'm so excited.
Do you know what makes me most excited about this?
There's certain things in here like, oh, okay, cool.
Breakpoint.
That's kind of nice.
Data classes, kind of nice.
But what really makes me most excited is this is the categorically fastest Python period.
So all the people that say, no, we're using legacy Python because under this particular use case, it's faster than Python 3.7.
And there may still be some very narrow use case.
But generally speaking, this is the fastest Python period.
It's much faster than Python 3, certainly up to 3.3.
But it's even quite a bit faster than 3.6.
So, for example, from 3.6 to 3.7, when you're calling methods, especially methods that are part of classes, not standalone functions, but, like, bound methods to classes, it's, like, 20% faster.
That's a reason right there to switch.
Yeah, and what's cool, like, all you got to do is just run on the new version, right?
There's no, like, oh, I switched to PyPy or something.
It's just I now have 3.7, so life is better.
Especially if you're using a framework.
Yeah.
Anybody that's using a framework, remember, there's a bunch of levels of abstraction around it.
So there's a bunch of function calls.
Exactly.
Yeah, there's tons of function calls.
And so this is really, really good news.
So just to run through some of the features, there's improvements to coupling that type annotations or type hints added to the system.
So previously, if I wanted to say, here's a function, and it returns this type of thing, right?
It returns, like, this object or some type I've defined.
I have to literally import that type at the top of that file.
And what that means is at import time, I pay an extra import processing that file, which I might not have otherwise loaded, potentially, or at least not right then.
The other one is circular dependencies are super annoying in that.
Like, so, for example, I've got, like, a repository class, and it returns some kind of object, right?
But that object, when it's defined, has to know about the repository.
So it says it returned, like, it uses this somewhere else.
Like, that basically will fail and break the import because it's circular, right?
So with this new thing, this change is instead of actually evaluating these type hints and having to import them, it's just evaluated as if you had typed them as strings.
So they're not actually evaluating the true types when you do type annotations.
So that gives you more flexibility.
The circular dependencies are a problem, and it's faster.
Yeah.
That's neat.
Yeah, the type annotations thing that makes me the most crazy is I have a class, and it's got, like, some function that returns an instance of itself.
You can't say that in the previous type hints as concrete types because you can't say the name of the class until it's fully defined.
But you're halfway through the class trying to say, this method returns one of me, you know?
You can get around it by putting quotes, but that's effectively, like, what this does without the syntax to put in the quotes.
So it looks more natural.
So you can do that now with the 3-side?
Yeah, but you have to put quotes, and you have to, like, say the name out exactly as a string.
Oh, okay.
Yeah, yeah.
Let's take a look at that.
Yeah, it's pretty cool.
All right, you also have data classes, which are really nice.
They're kind of like tuples.
They have a lot of interesting hashing and equality operators on them as well as representation ones.
They can be frozen, so they can be mutable or immutable, all sorts of things.
So kind of like enumeration, name tuple, tuple, it's stuff all brought together.
It's sort of a derivative of the adders project, but they decided not to move adders into the framework because it's evolving quickly, and they don't want to, like, freeze it.
So data classes, kind of the same thing.
I just want to point out that data classes has a back port for 3.6, so you can go ahead and start using those in 3.6 as long as you pip install them first.
Yeah, how do I get that?
Pip install data classes?
Pip install data classes.
Oh, that's pretty killer.
I didn't know about that.
All right, so there's a new breakpoint function, which we talked about.
We don't use that much because, like, if you just fire a PyCharm, who cares?
But there's some pretty interesting – it's got to be clicking the gutters.
But there's actually some pretty interesting ways to, like, say, hook a remote debugger to your code, things like that.
There's really interesting stuff going on there.
AsyncIO got better.
Chris, you should be happy about that.
Did you see these?
Some of this is pretty cool.
Tell me about it.
This asyncIO run function is a way of running a coroutine from synchronous code, which, I mean, that existed before, but you had to do, like, 50 million things to set your stuff up correctly and make sure you didn't break other –
You just call run, and it will internally, like, create the loop and do it for you?
Yep.
Just call run, starts coroutine.
Oh, that is sweet.
It starts an event loop, runs your coroutine, and closes the event loop at the end.
So you're going to have to be careful when you use this.
This is pretty cool.
And there's some of the task management stuff that's also a little bit better.
So this is pretty cool.
I'll have to – maybe I'll write a post on this.
Yeah, that's awesome.
Yeah, definitely cool.
Another one that's really nice is the time module.
And a bunch of other functions, as well, have become more accurate, especially over long durations, and they've taken on nanosecond resolution.
So you can import time and say time – you just call time, and it will give you a floating point value of the number of seconds since the epoch, like 1970.
But this – you can also now in 3.7 say time underscore ns, and it will give you the nanoseconds as an integer.
And because it's an integer, not a float, that means there's no drift or anything in sort of the floating point operations.
It's like truly the nanoseconds, which is awesome.
Yep.
All right.
And finally, I just want to close out.
Give it like – what would Python Bytes show be without?
A shout-out to Anthony Shaw.
Right, Brian?
Yeah, he's been a part of the show a lot.
For sure.
So he actually did a brand-new course on Pluralsight called What's New in Python 3.7.
So I put a link to his course there as well.
And actually, it's only about an hour long, but it goes really in-depth into like the advantages or all these various things.
So quite cool.
If that's easy for you to check out, go ahead and drop over there and check out Anthony's course.
Very nice.
Yeah, so I'm really excited for when this ships.
I think it's going to be great.
There always seems to be like some latency on the Linux distributions and stuff before I can really make use of it.
But, you know, pretty soon I'll be happy to be running this.
Yeah, definitely.
Yeah.
Do you guys have any extra news that you want to cover?
That's it for our official items.
I've been doing a lot of going back, went through all of the code that was in the pytest book to make sure it's 3.7 compatible,
which I was happy to find out that it's fine.
Nice.
So that's good.
Yeah, very cool.
I saw this really cool tutorial series about Mongo.
Do you know anything about this?
I may have spent an incredible amount of time working on this project.
Yeah, so I did a three-part, three-hour webcast series in partnership with Mongo.
And basically from concept to actually deploying it on DigitalOcean, actually, I rebuilt the PyPI.org website in MongoDB and then published it to the internet, which I then deleted.
So I don't have to pay to keep it running because there's a real one.
But yeah, there's like it's recorded.
People can go check it out.
You got the link in the show notes.
So that's awesome.
Yeah.
Nice.
It looks pretty fun.
Yeah.
Thanks for the shout-out.
Chris, anything you want to let people know about while you're here?
Nope.
I don't really have anything new.
Maybe just keep an eye out for a few new posts coming up and try to step past in my new blog.
Awesome.
Yeah.
There might be one about this asyncio.run, huh?
Yeah.
Awesome.
Yeah, you called it out.
You got to write it now.
So, yeah.
Remember, the internet is written in ink.
And so this is permanent now.
My next post was going to be about one of my modules, which uses asyncio a bunch.
So maybe I'm going to go back and experiment with 3.7 and what changes I would have to do there to write it up.
So this is pretty cool.
Yeah, awesome.
Well, Chris, thanks for being on the show.
And Brian, thank you as always.
Yeah, thank you.
Thanks for having me.
Thank you for listening to Python Bytes.
Follow the show on Twitter via at Python Bytes.
That's Python Bytes as in B-Y-T-E-S.
And get the full show notes at pythonbytes.fm.
If you have a news item you want featured, just visit pythonbytes.fm and send it our way.
We're always on the lookout for sharing something cool.
On behalf of myself and Brian Okken, this is Michael Kennedy.
Thank you for listening and sharing this podcast with your friends and colleagues.