Python Bytes - #116 So you want Python in a 3D graphics engine?
Episode Date: February 6, 2019Topics covered in this episode: [play:0:25] Inside python dict — an explorable explanation [play:2:37] Embed Python in Unreal Engine 4 [play:4:32]* Redirecting stdout with contextlib* [play:7:56...] Panda3D [play:10:32] Why PyPI Doesn't Know Your Projects Dependencies [play:13:58]* PyGame series* Extras Joke See the full show notes for this episode on the website at pythonbytes.fm/116
Transcript
Discussion (0)
Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds.
This is episode 116, recorded January 29th, 2019.
I'm Michael Kennedy.
And I'm Brian Ocken.
And this episode is brought to you by DigitalOcean.
Check them out at pythonbytes.fm slash DigitalOcean.
For now, I would like to get out there and explore a dictionary.
What do we got here, Brian?
This is totally cool.
I don't even know what to call this.
I've got a link.
It's called Inside Python Dict,
an Explorable Explanation.
But it's really like a four-page different tutorial.
It starts with lists and searching efficiently in a list.
And then it kind of builds up to Dict.
So it has like a description of what hash tables
are, why are they called hash tables, and then creates a Python-like, almost Python dictionary
structure. And it's sort of a deep dive into, and then at the end, it does actually show you how
Python dictionaries really work. It's a pretty deep dive, but what I really want to highlight is the interactiveness
of this. So if you go take a look at this, it shows a lot of the examples in playable code.
So the code's like auto-playing with like a little, it looks like a debugger where it walks
you through different lines and then shows you the output. And it's this very visual way to learn. And it's
pretty darn cool. And I don't even know how they're doing this, but you can figure it out if
you want to, because all of the code for this website is up on GitHub. This is a cool example.
And you're right, the little visual played, highlight the code, highlight the visual data
structures, animation is really great, I think. Yeah, it's a neat option.
And then occasionally some of the code has like fill in the blank stuff where you can type in stuff and change what the code does on the back end.
And it's just, I don't even know how this is doing it, but it's fun.
And actually really getting a handle on how dictionaries work in Python
is one of the, like the superpowers. To get really good at Python, you kind of have to really grok dictionaries work in Python is one of the superpowers.
To get really good at Python, you kind of have to really grok dictionaries.
So this is a good thing.
Yeah, they're certainly used more than regular, let's say,
everyday programming languages like C, C++, C Sharp, Java.
They have dictionaries or hash tables, but they are not built on top of them.
The variables you look up come out of the local dictionary, right?
Your classes and their fields and values and attributes are based on dictionaries.
And yeah, understanding them is super important.
This is a cool example.
I like it.
Yeah.
So neither of us are super big gamers, but there's a lot of people out there who are.
And then there's also a lot of people building visualizations or 3d interactions and python itself is also used a lot in creating pipelines for creating games and
movies and stuff like that so i kind of have a theme through this whole episode that i put
together i ran across some stuff kind of all at once and so the first one i want to talk about is
the unreal 4 engine which is one
of the big game engines you know built on c++ things like that so there's like fortnite save
the world gears of war 4 marvel versus capcom system shock not the old one which is an awesome
game from the 90s but the new one that's being remade all these are being done on unreal engine 4 and the news is you can now use a plugin to embed
python 3 entirely inside of the unreal engine both for controlling editing like automating
a scripting say loading a bunch of stuff and auto generating characters or stuff like that
and also runtime like even characters in the games yeah that's neat that's cool right so yeah
so basically this is like a meta plugin.
Like you take this plugin, you stick it in Unreal
and then you can create Python plugins to do stuff
and even write automated tests against like the AI
of your game and stuff like that.
Oh, wow.
Yeah, it's pretty interesting.
And the GitHub repo is super detailed
in like walkthroughs and steps.
So there's an example usage on how to create this character
from a mesh and from textures and stuff in Python
in the editor.
And there's also one about how to create gameplay elements
that run around in a game.
So it looks really cool if anyone needs to do
3D interactive stuff and they want to use Unreal 4 Engine
or even plug into a game that exists
like some,
there's a whole bunch more
than the ones I listed here.
This is a cool option here.
Yeah, nice.
Cool.
Yeah.
So this next one you got for us
is on Context Lab.
I'm a big fan of
context managers
in the West David.
Tell us what you found.
I was actually slow
to come to context managers,
but they definitely,
once you get a,
get your head around them,
they're really handy
and i saw this reference and i didn't know existed there's a lot of functions that just
print stuff like they print stuff to standard out or to standard error and i am used to testing that
i i know how to test that with pytest there's a the capsys fixture that you can use that to
capture the output of something and check what it is and read it whatever but there's the capsys fixture that you can use that to capture the output of something
and check what it is and read it, whatever.
But there's a lot of times where you might be using something that prints to standard out
and you don't want it to, or you want to be able to get that output programmatically.
And ContextLib has a redirect standard out function that you just give it a new target
and then using the with statement, run whatever code you're going to run and you can capture the output.
And didn't know that was there.
And it's very cool.
I'm going to drop a little tiny code snippet in the show notes to show people how to use it.
Yeah, and it'd be great for testing and something like that without trying to like do too much.
Right.
What's cool that I like about this is because it's a with statement, a little bit of code
is being redirected, then it goes back to normal, right?
It's not like you're permanently changing what it is.
It's just maybe during a test or during one function call or something, right?
I didn't put this in the show notes, but in the link to the context lib documentation,
the output I showed in our example,
just capturing it with string IO,
but you can pass it in like a file.
So if you've got part of your system
that usually just prints the standard out,
you can redirect all that to a file
if you want to save it somewhere.
Yeah, nice.
And there's a standard error addition as well, right?
Yep.
Sweet.
You know, I love this pattern.
I've done stuff like this with colored output.
So use something like Colorama and say for this output, I would like it to go to green,
but then I want it to go back to whatever it was before. So really nice way to do that as well as
to put it into one of these, create a little context manager, throw it in there. And just
the stuff like that interacts with a console or terminal in the width block is great, right?
That's pretty cool.
I like this pattern.
Very nice.
Before we move on to the next one, let me tell you about DigitalOcean.
Of course, a lot of our hardware is running on DigitalOcean.
They're super, super nice.
One thing I don't know much about, Brian, I don't know how you're doing, is Kubernetes.
Have you done a lot of Kubernetes?
I have not.
I have not either.
But it definitely seems like one of the most widely adopted ways to use
Docker containers and orchestrate them and really take more than one Docker container and make it
work as a whole, do live zero downtime upgrades, things like that. So you should check out Digital
Ocean's Kubernetes DOK8 service, which is a managed Kubernetes service. It's out officially now. It's
been around in beta and alpha for a while.
So you can deploy faster,
get your Kubernetes clusters in seconds,
provision and access it in a few minutes,
easily scale persistent storage
based on incoming traffic
with block storage and load balancers and whatnot.
And it costs 2.4 times less
or better performance per price
compared to the other providers.
So on top of that,
if you go to pythonbytes.fm slash digital ocean as a new user, you'll get $100 credit to Kubernetes,
all the things. So it sounds like a lot of fun and definitely check it out. It supports the show.
So to continue on with my theme, it's all about the 3d and the Python. So the next one that I
want to cover is something called Panda 3d and now it may seem
very similarly named to pandas but it has nothing to do with that it's just an open source completely
free to use game engine for real-time 3d games visualizations simulation simulations experiments
all that kind of stuff this one comes from kalia lubitz hopefully that was close enough thank you
for sending that in.
That's great.
One of the things I thought was cool,
I don't think everyone's going to be building a game,
but I think a lot of people out there are doing science
and experiments and visualizations and stuff like that.
And I think this is a great little 3D engine
for simulations that just happen to be in 3D
and you can do them in Python.
Oh, that's cool.
Yeah, isn't that nice?
So this is basically a C++ 3D engine that has a bunch of library plugins and stuff that
make it a lot easier to work with.
So it runs on all the platforms.
It has very little overhead because you basically get your meshes, you load them into the system,
and then you just sort of poke at them and control them from python but
they run like on your gpu in c++ so really good performance as well oh nice yeah so some of the
features platform portability runs on mac linux and windows maybe raspberry i can't remember where
else other possibly other places but the main places it has a lot of tools for handling assets
like i don't know if you've ever done anything with 3d, but like it's these library files
and you know, this Adobe file format for that other thing.
And there's just all that kind of stuff.
So there's like a lot of tools for handling these assets, which is pretty awesome.
So you can automate either your pipeline or even the code.
It also has library bindings which is cool so you can do things like
plug in the bullet physics engine or the as a map loader or open al or all these different things
that you know just because you have 3d doesn't mean you have like physics or interaction and
stuff like that right yeah pretty cool and then finally it comes with this thing called p stance
for panda 3d profiling so you can actually get a visual representation of exactly where your program is spending its time.
Like this frame or this mesh or whatever takes that amount of time to run.
So you get like super detailed runtime profiling and performance analysis.
So not bad for a completely free Python enabled thing.
Yeah, and it's like completely free.
You can even use it for commercial use if you want.
Yeah, exactly.
And I don't think like Unreal 4, for example, is.
So this is a nice one.
Nice.
Yep.
What's the next thing you got?
I have an interesting article and I should have written down.
It's from Dustin Ingram called Why PyPI Doesn't Know Your Project's Dependencies.
And I guess it's something I never really thought about before. But if you go look
on packages on PyPI, it would be kind of cool if you could see all the dependencies of a particular
package. Yeah, you can do it on GitHub as a whole project, but you can't do it as like a single,
you can't go, this package I'm going to get has exactly. That's nice. It would be nice.
He's trying to answer, he says he gets a lot of questions like how can i produce a dependency graph for python packages man why doesn't pypi show that
the project dependencies right out on the page that should that would be neat and also just how
can i get all the dependencies without downloading it first and uh the answer is you can't which is
interesting and the reason is because of setup tools, then setup.py is dynamic.
It's code.
It runs.
And he gives an example, which is sort of funny.
He gives an example of building a setup.py package that does a dependency with random
choice.
So randomly pulls in different packages.
Fun.
Just to make a point.
Yeah. So that would be evil, but it could be done. And I'm hoping that a listener will do this and
push it up just for the heck of it. But there's some real reasons to do that. And a lot of them
have to do with different packages being available on Windows versus Linux versus Mac or whatever.
So operating system dependencies are something that often makes you have to
pull one in versus another.
Also, different versions of Python.
So if you're using Python different, if you want to use data classes
and you're using an older Python, you need to actually depend on data classes,
for instance.
Yeah.
So this is an interesting article that talks about that,
but also points out why wheels are awesome.
So wheels are not something that are dynamic.
They are more predictable.
So this is, I think, also a little bit of a plug for wheels.
And I wanted to highlight that.
Wheels, they have dependencies in them also, but they're not as dynamic.
So there'll be a different wheel up there for different operating systems. And while I was poking around on here, I also wanted to go on a
little bit of a tangent. There's a website called pythonwheels.com that I'll just drop a link in
here too, and it talks about some of the advantages of wheels. It's nice. It installs faster, doesn't run setup PUI, doesn't require a compiler.
So basically kind of a binary version instead of a source version, which is
cool. For some packages that have wheels up there, it is kind of cool
if you've got hard operating system dependencies, then
have some sort of build step that puts those up for the different operating systems.
Yeah, cool. I like it. And it's a totally reasonable question.
Like, I'm considering installing this thing. What does it use?
That should be obvious, and I'm not sure it is.
And one of the things I also, I don't know why people are,
if you really want to know on my operating system, what do I need?
PIP includes a command called download.
So you can say pip download something and point it
to a directory and it'll download something and all of the dependencies that are valid for your
operating system. So if you're really curious, that's one way to do it. Very cool. All right,
for the last one, I want to talk about building games and other stuff, other stuff with Python.
Now, this one is different than the other. The first two, there's about 3d engines, one, a big commercial one and one,
a free open source one that you can plug in and control and automate and work with, with Python.
But what this is actually, this one makes me super excited. It makes me want to go along with it. It's
a, at least six part series on pie game. And they're really quite detailed and in depth and interactive.
So I'll just give you a sense. It starts out with building a simple dice game,
then building a game framework so you can build on top of it, adding players,
moving your characters around, having heroes and villains that fight. And then the last one,
at least the time of the recording, is putting platforms in a Python game with Pygame. And then the last one, at least the time of the recording is putting platforms in a
Python game with Pygame. And what they mean by that is, you know, think of Mario Brothers or
Joust or something like one of these old 2D side-scroller games. Like how do you create a
game with that? And it's pretty elaborate and pretty nice. Oh, that's cool. Yeah. It's like
basically the six-part series. So if you wanted to kind of get your Python better or you wanted to learn Pygame,
either of those, this would be a really cool set of series to go through
because you're going to come up with like a 2D side-scroller game at the end,
and that seems pretty awesome.
Man, I haven't thought about Joust in a long time.
I miss that game.
That was cool.
I know.
I can't even remember the sounds.
But, yeah, I remember it for sure. Being in the arcade there, that was fun.
Yeah, that one in Dungeon, that Dungeon game, that was sort of just a video more or less.
But those were fun times.
Now, one other thing that I want to throw in here, like, so this is the series and that was kind of what I want to talk about.
I'm linking to all the pieces.
But something I ran across recently as well that I thought was pretty cool, I haven't gotten it, I haven't played with it yet,
but it gave me the same feeling of like,
oh, this looks so cool,
is this thing called Mission Python,
a code space adventure game.
So similarly, it's like a whole learn Python
and learn Pygame by building like a space adventure game
with Pygame.
And it just looks so fun.
I just, I don't want to do it,
but haven't done it yet.
Haven't ordered the book.
Oh yeah, it does look fun.
Yeah.
Yeah.
So if you're interested in learning Pygame
or you want to use Pygame for some kind of simulation type thing,
either of these would be real helpful.
Yeah.
Sorry.
I'm already giggling about the joke.
Yeah, it's coming.
So those are our main items.
You got anything extra to share with folks
before we unleash an amazing joke on them?
No, nothing extra this week.
How about you?
Not really, but I do want to say I'm looking forward to being at Pi Cascades with you and
everyone else up in Seattle in the February.
So if there's still tickets available by the time this goes out and people want to make
it there, then definitely check it out.
It was a fun conference last year. Definitely. Absolutely. All right. I guess we got
our friend Dan Bader to thank for this, don't we? I guess. I think that, I'm not sure. Somebody at
RealPython. Yeah, somebody at RealPython. All right, well, hit us with a joke. This comes from
the RealPython Twitter and I linked to the joke. It's got a little graphic to it, but you can do
without. Yeah. So why do pythons live on land? I don't know. They're snakes. I don't know why. Because they're above sea level. The letter C, capital dash level,
of course. Yeah. Because pythons are higher level language. It gets worse as you explain it more.
That's right. See, because you don't compile it. No. Yeah, it's bad.
It's a good joke though and very very funny.
Thanks for sharing. Yeah definitely.
So thank you. Yeah thanks for doing the episode as well
and goodbye everyone. Bye. 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.