Python Bytes - #18 Python 3 has some amazing types and you can now constructively insult your shell!
Episode Date: March 21, 2017See the full show notes for this episode on the website at pythonbytes.fm/18...
Transcript
Discussion (0)
Hello and welcome to Python Bytes, Python news and headlines delivered directly to your earbuds.
I'm Michael Kennedy. And I'm Brian Ocken. We're here to share the Python news with you and show,
basically share the cool stuff that Brian and I found for the week. Brian, you know what I think
is cool that I can open up, I can basically go to any Python object or module or even function or
whatever and just say, tell me what is is in you dir this thing and out pops all
of its capabilities mostly anyway yeah i love that do you know what repl stands for ripple
ripple read val print loop just the okay just what you get me in type python yeah that like
most of the time now the only time i open that is to try out something little or to actually to use
the dir command you know what I do do that as well.
I'm like, I'm gonna open this up and just do a dir here. Okay, back to work. Yeah, exactly. Just
to look at the interface for something. So I was excited to see a new project from Leica 9M on
GitHub called pdir2. And when you use it, so it's pdir2 when you're installing it, but when you use it, so it's PDIR2 when you're installing it, but when you use it, it's just PDIR.
And it's just a replacement.
And it's cool because it's all colorized and it splits it out and splits it up into one line per item.
And it pulls the dock string out of each item so that you can see that.
And for what I'm using it for when I pull up the REPL,
it does what it should do automatically, and I like it.
Yeah, it's cool.
I mean, it's kind of like if you typed help, a thing,
but it gives you meaningful stuff even if there's no real help to be had.
And it'll categorize things like here are all the classes,
here are all the functions, here's the stuff that I have no idea what it is,
but it's also there, so we'll put it over here.
And if there's doc strings, it'll put the doc strings there for like here's the stuff that i have no idea what it is but it's also there so we'll put it over here and if there's doc strings it'll put the doc strings there for like here's
the description of each thing it's it's really nice yeah and it does um it like puts all the
dunder functions out of the way so those aren't in the middle of everything too yeah yeah those
land in the other yeah anyway it's just it's part of my uh repertoire now i'm gonna i'm never gonna
let it go one of the things i would like to ask somebody to do is to help out with the
project and convert the read me to whatever that other thing is.
Restructured.
Yeah.
Restructured so that it works.
It looks good on pipe PI because it's in the read me is in Markdown and pipe
PI that still doesn't like Markdown.
So I wonder,
did you look at it on PyPI.org?
No, I haven't.
Versus the older one?
It's worth looking at that one.
I have no idea what it'll look like there either, but that's the new one.
Maybe it understands Markdown.
Who knows?
I would also like to recommend that people pair this with PT Python.
Do you know PT Python?
I don't.
Oh my God, PT Python is awesome.
Like, I don't use the REPL very much.
Everyone knows I love PyCharm, and I kind of live there. But when I do go into the REPL,
it really annoys me that I need to completely type everything that is there, that it doesn't
autocomplete, especially when I'm exploring something new, which is like why we said often
we go into the REPL. Well, PT Python is basically, I think it's a little bit based on Emacs, but it's like
a different version of the REPL that has code completion and a little bit of a UI over the
terminal. It's really nice. So these days, if I want Python, I type PTPython. Okay, well, I'll
have to check that out. And just to let people know I did. Okay, so I'll take back that request
because I looked it up on PyPI.org,
and it looks great.
It even has that animated GIF on there.
Oh, yeah, so there's a little animated GIF that shows you how it works,
which is quite cool.
So, yeah, check out this project.
It's easy to use.
The one thing to notice is you've got a pip install pdr2 versus pdr,
but then the module's just pdir, right?
So there's a little bit of a nomenclature funkiness but it is cool and you know what else go ahead i just uh yeah just want people to
remember that and speaking of memory speaking of memory absolutely so more this has happened to me
once in my life i have not used these tools to solve it. But here's the deal, like suppose that you somehow through probably your fault, but it could have been like a hard drive failure or
something, somehow you've lost your source code. So there's a guy who was using a source control
wrong and wiped away all of his files off of his directory, but he still had the program running in memory.
It was a Python program. So he used two really interesting packages that I'd never heard of.
Had you heard of these? No, I haven't. One of them is Pyrocyte, like parasite, but for Python,
P-Y-ro-cyte. And the other one is Uncompile 6, because apparently it's one of the few that supports both python 2 and python 3
and multiplication is awesome so uncompile six all right so the idea is with pyrocyte you can
attach to any running python process and you basically get this repl like we were talking
about to just explore its memory to see what its values are. You can even like force it to load other scripts and run them.
You can go to like a in memory,
like singleton object and like work with its data,
all sorts of stuff you can do in real time while it's still running around.
That's pretty cool.
So basically what he did is he said,
I'm going to hook up to it with pyro site.
I'm going to force the thing to load and compile,
and I'm going to call and compile on itself and get my code back. Wow.
But mostly the, mostly the core, it worked. Yes, it worked. There are people, there's like a whole
bunch of people. It's a, I get them just at the bottom of the gist or like, oh my God, I just had
to try this because I couldn't believe it would work. Oh, that's so awesome. Yeah, it works. And
they, I think there's either a video or a little GIF that basically shows you how it works if you go to Pyrocyte.com, which that's the coolest takeaway for me on this whole one.
That's awesome.
Good job.
Cool.
Yeah.
All right.
You know what I think?
You and I were on a mission, right, to make sure that Python is modern Python, not legacy Python, right?
Yes, definitely.
And I was really surprised.
They're still adding quite interesting and new data types to Python 3. Even stuff that's been around for a little while that I just haven't
like heard of. Because of this show, I've learned about the simple namespace as a, wait, no, that's
not it. The name tuple class, the capitals, but let's get into this. There's an article called
New Interesting Data Types in Python 3,
and it just goes through three that I really like. And the three types are types.mappingproxetype,
types.simplenamespace, and typing.namedtuple. And the mapping proxy type is like really cool.
It takes like something like a dictionary or it says,
I can't think of anything other than dictionary. It says it only takes mapping types, but.
Yeah, it's really cool, but it's like a read only dictionary basically, right?
Yeah. So you take a normal, like maybe a normal dictionary and like if you had an API or something,
you wanted to expose that dictionary to through the API, you could wrap it in this mapping proxy type and return that. And I tried it out. And so for instance, one part of your project could use
the mapping proxy type version and read everything. And the part of your system that can have,
that still has write access, that is synchronized. So it isn't a copy. You can,
when one part writes to it, it gets updated in the other place too.
Oh, it's like a view into an existing dictionary, huh?
Yeah, it's a read-only view.
Sweet.
So that's nice.
And then not too long ago, I started using where I really wanted classes or something,
and I was using things like tuples or lists where there really should have been something more structured.
I've been using the lowercase named tuple.
Right, out of the collections module, right?
Yeah, out of collections.
And there is this thing called types.simpleNamespace
that uses a similar sort of functionality.
You can just, if you define a type to be that type,
you can just, the parameters, when you instantiate it,
those just become elements inside the object.
It's pretty convenient.
Yeah, that's cool.
So you can do a little more organization.
And then, of course, the typing.nameTupil class is really nice.
I hadn't been using it.
I've always just used collections.nameTupil, but I think I'm going to be switching.
Now, one word of caution, I suppose, is a lot of these are, they require pretty modern Python.
Some of the techniques they show require 3.6.1.
Like 3.6.0 is not enough.
So just be aware that some of these require pretty new versions.
Yeah, and that's, I guess it's hard for me to remember because I'm usually trying to stay.
I don't go into the in-development Python, but i try to use the latest released one i do as
well you know where that bites me though is on my digital ocean servers it's running the version of
ubuntu that only has three five two or something like that so if it's literally a three six feature
i'll put it in my app and i'll ship it like oh it doesn't work what was i thinking? Yeah. And I forget that, that 3.5 is really, for version 3.x, 3.5 is being used more than 3.6 right now.
Yeah, just because of the distributions, I think.
In my mind, that's like the minimum bar for 3 stuff is 3.5.
Yeah.
Okay.
But I think these are great things to start adding to your repertoire.
If you have control over where it's going to run, those are good things.
Yeah.
And some of them come from like Python 3.3 or 3.4. They're fine. There's just a few
techniques there that are brand new. And what I like about this, one of the things I like about
this article is it's pretty short and it gives examples of how to use these pretty concisely.
So that's good. Yeah. Very nice. Very nice. Speaking of how do I use things,
wouldn't it be awesome if I could just go to my bash shell and just type how do I and type a thing?
Like I'd like to say, maybe I want to know how to print a stack trace or trace back in Python.
I could say, how do I print stack trace Python and just type that?
Yeah, that'd be cool.
It'd be cool if my computer knew that, right?
Well, it turns out we ran across a project called How Do I? And if you install How Do I, if you pip install it,
it will give you instant coding and shell answers on the command line.
So I can say, how do I connect SQL Alchemy?
And it'll give me the Python code to connect for SQL Alchemy.
I can say, how do I Python save dictionary?
That's three words.
And it'll show me how to use the JSON load s dump functionality
right there in the command line. Now, sometimes it gives me like a stupid answer. Like if you type
that, how do I Python save? How do I Python load JSON, it just shows you JSON, which is stupid,
but you cannot do a dash a and it'll give you all the answers it's found on the internet
related to that usually somewhere in that list is something decent.
It has some sources like Stack Overflow,
and I don't know where they all come from,
but they're all over the place.
It's cool.
I can say, how do I debug Python?
It'll show me the PDB commands.
I can say, how do I change my path in macOS?
It'll show me how to do that, assuming I'm running Bash,
things like that.
Okay.
I'm going to try it out.
Yeah.
How do I put a worm on a hook?
Exactly.
Yeah,
it doesn't work.
Yeah.
It's got to be something technical,
I think.
So the other,
there's another one that I want to point out that I thought was really
funny,
but actually it's kind of useful.
Oh,
what's that?
It starts with a command.
It starts,
it starts actually starts with a cartoon.
And basically the idea is if you type in a command that you think,
you know,
on the shell,
but you get it wrong, you can curse at your shell and it will potentially fix it for you.
Yeah. I love this.
So this thing is called the F. I'm not going to complete it so we can keep our clean rating in iTunes, but the link is in the show notes. And basically you type that after you've made a
mistake on the shell and it goes, oh, you probably meant this. Were you trying to do this get checkout branch thing? You did a little bit wrong. The F. Oh,
here's the command. Enter. And it'll just automatically fix it for you. It's beautiful.
Yeah. Nice.
So plus you get a little, a little fresh. Yeah. You get to let some frustration out on the thing,
right?
Yeah.
All right. What you got for us? What's next?
Oh yeah. I got to go back to that tab. We have a, yeah, we're at a, a listener. So,
you know, it just seems like we just did the last episode.
Just a few days ago.
We had a listener commented on the last episode.
Last episode, we did mention some feedback from a listener about Async speeding up their, some work.
And we have another one from a comment this time.
And this is a project that a
listener shared with us and we've got a link in the show notes but it's a project called py marcis
and and it um and i i wasn't familiar with emarcis i guess it doesn't really matter but it's a cr
it's a cloud-based crm and it's python interface but i'll just read this. The gist of it is it's bedded up.
And this is from Diego. Thanks, Diego. Another awesome episode. Thanks, Michael and Brian.
And if you say that we do something awesome, we'll totally put you on the show. Okay. About
async IO being awesome. I had my own experience. I had to send information to about a hundred
information about hundreds of thousands of users to a CRM through their
public API daily.
And with the synchronous stuff, it would have taken 50 hours a day, which you can't do that.
And they added async and it went down to 20 minutes.
So that's 150 times faster, which is awesome.
Yeah, they were using requests before.
And in order to update their CRM, it took 50 hours per day problem.
So they switched to the AIO HTTP client stuff that has async and await and 20 minutes.
In fact, he told us an interesting story.
He said, the first time I ran it, it like opened so many connections that it crashed
his Mac.
And so he actually had to implement a semaphore limiting system, which is pretty cool.
Yeah.
And I did like that he added that, that he added the semaphores.
But the GitHub link that he put up, I didn't see any semaphore usage in there.
So I've also in our show notes linked another article that talks about using asynchronous
IO with semaphores to limit how many connections you're making.
Yeah.
No, thanks, Diego, for putting that up there.
Anybody who wants to have a comment on the show,
you can contact us on Twitter where we're at, Python Bytes.
But also, every episode has a discuss section at the end.
So if you want to talk about an episode, share a link or whatever, drop in there.
So what's the link for this episode?
Pythonbytes.fm slash 18.
Yeah, yeah, there you go. Cool.
Yeah, I made it super easy to find.
And do you know what would be super easy is if somebody would make an easy way for me to do a Pyramid installation.
That's right.
So the way I made that slash 18 work, and you can still go to like slash episode slash list or whatever, is I use constraints in the routing infrastructure. And so one of the things that I've been doing a lot of work with lately is with building Pyramid web apps and trying to really help people
jumpstart what they're up to, right? Like I can go to Pyramid and create a web app and I get
basically a web app that runs and here's the view function and here's a template. Go, go write the
web app from scratch. And I kind of wanted to help the world not start from scratch
but start from near the end so i've been doing a lot with cookie cutter more on that later but
i decided to create a cookie cutter template that is an opinionated cookie cutter template
for creating pyramid web apps that have most of what you need pre-built and pre-configured and
factored the way you would for a large professional app, not for like a one file starter thing.
That's great.
So I created a cookie cutter thing called cookie cutter dash pyramids dash dash
talk Python starter.
And so this came out of my Python for entrepreneurs course,
but you don't have to have anything to do with that to play with this template.
So what you do is it comes out with like, when you just say,
create the website, it'll create like a shared layout.
So there's one place where all your general navigation and look and feel goes. It has
handlers for class management. It has like secure user management using things like pass lib and
other techniques, bootstrap, logbook, rollbar integration, MailChimp integration, outbound
email integration, power for static file management, all just the list goes on and on. But it's very much closer to where you need to be for a
professional pyramid app to get started, in my opinion, rather than like, basically, here's one
file now create the whole thing from scratch. So people want to check that out. That's on GitHub,
you can just cookie cutter space the GitHub URL and you're off to the races. It needs a little
bit more documentation. I'm going to work on that this week, but it's really close.
But it's a good start. And I'm actually pretty excited about it. And you've got one,
your course is one of the very few that I've seen that really teaches people how to do Pyramid as
well. Yeah, thanks. Yeah, it's definitely one of the few ones on Pyramid, which is great. So here's
like one more resource. Basically, this is going to be part of the end of that course. And, you know, the first time I heard about Cookie Cutter, I
really kind of thought it was a little silly, but it's actually turning out to be quite a useful
tool for a lot of spaces. Yeah, I definitely, yeah, the more I use it, the more I like it. So
I've definitely been putting some effort into it. It's great. Well, that's the end of our six. So, um,
do you have anything to share with us, Michael? No, nothing. Uh, nothing other than this template
thing. That's, that's really all I have to share. I've been just cranking on my classes. I hear that
you're probably still writing on your book. That's getting close though, isn't it?
Yeah, it is. It's getting a lot closer and I wish it was closer than it is, but I'm, I'm, uh,
I've got a whole bunch of, actually, I'm kind of overwhelmed
with both work and with gratitude that I've had a lot of great people, incredible programmers and
good writers help me out with the technical reviews on the first four chapters and a lot
of great feedback. And now I got to go rewrite everything. Thanks a lot, guys.
Thanks a lot. No, that's awesome. Yeah. I'm looking forward to the day where we can announce
your book on here. That'd be great great any day now with that book huh any hopefully we the goal still is
to have something to sell by pycon beautiful all right well thanks brian for sharing sharing news
and thank you everyone for listening we'll catch you next week all right thank you 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.