Python Bytes - #83 from __future__ import braces
Episode Date: June 22, 2018Topics covered in this episode: Code with Mu: a simple Python editor for beginner programmers. Python parenthesis primer Python for Qt Released Itertools in Python 3, By Example Python Sets and Set... Theory Python 3.7 is coming soon! Extras Joke See the full show notes for this episode on the website at pythonbytes.fm/83
Transcript
Discussion (0)
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 Atkins.
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 slash DigitalOcean.
Get $100 credit for new users.
Brian, speaking of new users,
people are into 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 have been always been
reluctant to try to look or play with some of these uh beginning beginning code editors because
part of learning how to code is learning the learning an editor but i'm kind of warming to
the idea of some of these uh specialized editors and there's one that that i
wasn't familiar with which is moo i think it's moo or mu i don't know mu code with moo it's got
a whole bunch of people on board if i go to the uh i can't remember where it is but there's a bunch
of people working on this and it's a kind of a neat little editor that has i was playing with
it this morning and it has uh some bill 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 theme button so you can jump back and forth between dark theme and light theme.
And it even has like a linter built in so 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 how to code so i'm gonna start using it for like demos and stuff it's pretty cool yeah
so as you type basically it pulls up both the the completion for the arguments that go to a method or something,
but also the documentation while you're typing.
So 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.
Is it 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, or Qt, I've been corrected.
It's a Qt application.
Oh, that's really cool. It's Qt? 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 when you install it with pip, you get like a command line invocation to invoke it.
But it recommends using a another package called shortcut, which you just if you pip install shortcut you get shortcut move editor for
instance and it'll create on my mac it created a uh i just went ahead and created a shortcut in my
launch pad to launch it so i didn't that's pretty cool attach that to any python script you can just
launch it from there so nice oh but looks like it has uh different modes too, you can just launch it from there. Nice. Oh, but... 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 get your feet wet in 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. way to get uh sort of gets your feet wet in open source is to write some tutorials right it's you
don't have to too much to depend upon or you probably won't break anybody's code by by writing
one yeah yeah nice so chris one of the things i find it'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 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 on how things operate.
It sounds pretty simple.
I mean, it's about Python parentheses, right?
The Python parentheses primer from Reuven 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 little
quirks you can use it for security, 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 and square brackets
which are typically used for lists and for indexing the for somebody that's new to the topic
you'll want to look at the slices section because slices are pretty 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 variable.
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, if you have a variable that's, say, a list or a container class of some sort,
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 C Sharp 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 you don't get any help on what's contained within it.
But you could say, import the list thing,
say list, square bracket, int, square bracket.
Then it has to be a homogeneous list of ints,
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 future-proof your code.
So 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 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 didn't 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 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.
Yeah.
Well, it had gone through some fairly dark times.
I mean, obviously, we've been on our gooey kick for a while.
We're going through all these different options, and there's still tons of great options, like WXPython 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 cute which is taking these
various version mismatch pi side 2 pi cute all those things and like rebuilding it like 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 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.
Well, 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 TalkPython, 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 to 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 and AI, and you automatically get a machine with Python 3, R, Jupyter, notebooks,
TensorFlow, 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
slash 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.
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 RealPython 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 uh and then like for example
it starts off with just um how does zip work to take you know two lists and uh create another
list with like each element and then what if you need what if you need like uh 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 as this example of using like like a big example in the like the
memory usage blew up to like huge and so is 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 uh a lot of these things
you if you don't think 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 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 islice.
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 i slice 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 that nope we don't slice generators
well you can i slice them and it works just fine it's beautiful right so it's you sort of make
things sliceable that otherwise would not be it That's really great. Yeah. So there's definitely a, definitely a good read to
beef up on the, the iter tools. So, 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, 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 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 intervals
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 define 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, dedubing 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 one so when you're making a membership test you can 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 is 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. 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 conditional.
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 multi-line looking thing.
Maybe a more Pythonic way would be 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 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 yeah right so it can't change and 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 four loops and the code is kind
of optimized to do that for you which i'm sure sure will not be 11 squared, I think, 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 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, for instance, the union operator.
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 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 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 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 one, 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.
Break point.
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, uh, it's like 20%
faster. That's the 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 pipe pipe or something.
It's just, I now have three, seven. So life is better. Especially if you're using a framework.
Yeah. Anybody who'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 defined, it 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 would type them as strings.
So they're not actually evaluating
the true types
when you do type annotation. So that gives you more more flexibility the circular dependencies aren't problem and it's
faster no yeah that's neat yeah the one that this 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 ins as a 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 can get around it by putting quotes,
but that's effectively what this does
without the syntax of putting the quotes,
so it looks more natural.
So you can do that now with 3.7?
Yeah, but you have to put quotes
and you have to say the name out exactly as a string.
Oh, okay.
Yeah, yeah.
I'll still take a look at that. Yeah, it's pretty cool put quotes and you have to say the name out exactly as a string. Oh, okay. Yeah, yeah. I'll have to 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 async io got better chris you should be happy about that did you see these
some of this is pretty cool tell me about it this async io run function is a way of running a code
routine from synchronous code which i mean that 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 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 type import time and say time, you just call time, and it'll 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'll 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.
What would a 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, Plurals site, 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 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
some latency on the Linux
distributions and stuff before I can really make
use of it, but 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.
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 MongoDB.
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, 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 accept pass
in my new blog.
Awesome.
Yeah, there might be one about this asyncio.run, huh?
Yeah.
Yeah, you called it out.
You got to write it now.
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 Auchin, this is Michael Kennedy. Thank you for listening and sharing this podcast with
your friends and colleagues.