PyCon Australia 2013

Nunca hagas esto con Python

Richard Jones  · 

Transcripción

Extracto de la transcripción automática del vídeo realizada por YouTube.

welcome to the plenary Hall for this early afternoon session our first presenter for this block is Richard Jones he's a Python program with nearly 15 years experience he's an occasional core developer of Python itself he's a member of the Python

Software Foundation as a co-founder and chair of a program committee of Python Australia which means he picked his own talk this time he's learned a few things about what you should not ever you should absolutely not ever ever ever do please welcome Richard

Jones thanks Benno ok so this is just going to be the fun little talk well at least I thought it'd be fun I hope you get a bit of fun a bit too and basically what I'm gonna do in this talk is is poke around some some strange corners of Python and perhaps

show you some things you can do with Python that you it probably shouldn't do but maybe start something sorry yeah well we'll get to that you might learn something new which will be cool we'll see how we go the first thing I'm gonna look at

is a couple of corner cases of the Python grammar that kind of surprised me I thought they were kind of neat we'll see how we go so let's say we have serialization right turning data into text for storage or transfer or whatever we have some mixing

classes that perform serialization we have our preferred mixing class which might not be available so serializing to JSON is preferred over serializing using Marshall but we want things to keep on going and Rocking regardless of whether you know a preferred

one is available so did you know that the class is in the basis clause of a class definition can actually be an expression because in inheritance doesn't really cut it in this modern world of rapidly changing serialization protocols we need fallback atones

okay who knew that it the accept clauses of exam can be expressions as well whatever the expression evaluates to had better be an exception class or you know it won't work but as long as it is the exception type that you end up with will be the thing that's

caught okay so keep that in mind generators are cool right when you pluck the double-under next out of a generator you can just keep calling it and it just keeps generating stuff they're like little happy spewing machines that make your program awesomer

just keep generating they're awesome let's modify our generator to generate exception classes instead of strings so now we have some stupid code that generates some stupid assertion errors but about half the time and we can restrict our program so

that it just captures some of them and after five we stop squashing them so that's we've done this is an actual line of code from MongoDB it isn't exactly the same thing but it's kind of similar which is basically squashing the logging of an

exception certain a roughly 90% of cases randomly so that's fun okay so that's just a couple of bizarro things in in the grammar which I thought pretty cool pythons also quite famously mutable so let's look at some things in pythons runtime that

are perhaps a little bit more mutable than you might have previously thought let's start off with a little function okay we'll call it F and it does a thing the thing it does is in its code object and the code object is attached to the function object

as the dunder code attribute you can exact code objects that's fun we'll come back to that so we'll make another function now let's call that G it does a thing as well so how many of you knew that the dunder code attribute was mutable you can

take the code object from one function and assign it to another function so that's fun the code objects is not unique to functions the code in modules is also encapsulated in a code object module objects have a dunder code object the pyc files they're

written by Python to cache module code is actually that code object marshaled using the built-in marshal module and we can unmarshal that object and I think you probably know where I'm heading with this so that's you know that's that's fun

too that's some mutability that you might not have known about you can also create code objects by hand this is actually an unrelated conversation but it's similar this is where we start going off the rails and of course you can start with you know

Python code to create code objects by hand but that's the obvious way right we're not talking about the obvious way of doing things we're talking about XML the beautiful elegance of XML Python XML we all short will call this file adder dot pi XML

isn't it beautiful and elegant I mean I've got a whole bunch of XML stuff here I'm looking to enjoy so what we're going to do is we're going to parse this XML using Elementary which is nice and we're going to construct a thing called

an abstract syntax tree which is a built-in thing in Python and we can take that syntax tree that we build and we can turn that into code using compile and it basically consists of a bunch of this I have it on good authority from someone close to the ast code

that this really isn't done very often I I even managed to provoke a seg fault from deep inside AST which kind of worried him but I'll get back to you know I'll try and figure out exactly how I did that it's pretty straightforward really we're

[ ... ]

Nota: se han omitido las otras 2.627 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.