 Hello everyone Let the gym wire it's late as usual. All right. I Found this font on the internet. Helvetica. It looks really great on slides. You should check it out All right, I'm Chris Wastroff That's me eating it enough of it up here, and I'm going to talk today about Python right Evil Python But why why would we want to talk about Python? Well, I'm just bored of Ruby, right? This is a tweet if you can't read it I have to find a new hobby since Ruby became my day job Right, so what am I going to do in the hallway here? I can't write Ruby and Actually, I work at a company called github and we see lots of other languages I didn't used to be like a polyglot pipe person, but since working there I've met people in other languages doing different things and I've gotten interested in trying out other languages So it's kind of my hobby now is picking up a new language writing something in it And then pretending that I know a lot about the language after I done that And I'm going to talk about the times I've done that which has been a lot the JavaScript Turns out it's a really awesome language with really horrible users. I have this library called Facebook's It's a modal pop-up box a light box and has a huge mailing list It is the biggest mailing list of any of my open-source projects, and it's like the worst I don't even want to open it sometimes just the the conversation and the level of discourse there just So if you need help on Facebook's I there's a good mailing list. You should check it out I write I write emacs list weekly. Well, I write emacs list every week. It's not feeble. I don't think but It's cool. And how else are you gonna write list really if you're not writing emacs list There's no other real list really super closure. So object to see and Mac Ruby. I've played with double This is actually a Mac Ruby app, but I didn't have any other screenshots for object to see IO which used to be really cool OOC which is really cool Erlang and then a whole bunch of other ones. Those are just the cool ones that I was trying to get like a book deal on so I put Them in the slides So Python this is my favorite slide by the way doesn't get any better than this Python. Whoops. You still hear me It's a cool language, and I've worked on a couple projects in it as well the first one is called pinder it's a fork or a rewrite of a Ruby library called tinder, which is the campfire API that you see this a lot in Python They'll take a library and they'll put a P in the front of it for Python. It's genius But this is cool because it was a project that someone had posted they weren't working on anymore I put it on github. I added some features someone forked it and it works pretty well And this was kind of my first foray into serious Python development And it was really useful because I'm familiar with the tinder gem So this is a rewrite and I was able to kind of do side-by-side and get a feel for the language that way Which if you're looking at any new language, I think it's a good thing to do I had a talk at the Ruby hoedown Seven years ago at this point it feels like where I was trying to encourage people to do side projects and learn a new language And that sort of thing and I said, you know Just take rake or something that you're intimately familiar with and try writing that in your new language in closure Or Python because then you don't need to worry about figuring out what the program does and said you can just focus on the language In the syntax, so this was a great way for me to do it Is that for me? The reason I was doing a campfire thing in Python is I wrote this bridge called well You can't really pronounce it IR camp or something so it's an IRC campfire bridge and Instead of writing a campfire mode in emacs. I decided I was going to write a campfire IRC bridge that would put campfire in IRC So then I could keep using my IRC client emacs. That's the kind of person. I am It worked really well and that was fun it gave me some exposure to twisted which is their version of event machine Which I'll talk about in a little bit and then the other Look This is how you know I'm like becoming a true Pythonista is because I took a library and I put P y in front of it Right boom. This is pie stash, which is the Python version of mustache Which is my most recent project and I actually hope people will use it that are Python developers And it's not just like for me in my emacs And then the final thing is leafy chat Which we made me and Leah Colver and Alex Gainer for the Django dash, which is like the rails rumble Except you have to use subversion so it was Sorry, it was it was 48 hours. We had to build Django app. It was a lot of fun I was new to Django, but I wasn't new to Python at this point I knew twisted and I'd done the IRC camp and twisted and so we wrote like a real-time Comet IRC client in your browser, which is lightning fast and it was awesome and It was fun because I learned a lot of Python idioms I already knew the language but then working with two Python developers and seeing them rewrite my commits really open my eyes to some of the idioms Alright, so Python is interesting to me more so than something like objective C because it's so similar to Ruby Which for a long time I thought that was a reason to not like Python It was a way to make fun of it and you know pearl too, but it's it's really similar to Ruby in a lot of ways first of all The style is actually one of the things I love about Ruby is the way that they do the style The way that the code is written and Python does it really similarly so all the things I like they do especially this one it also has a really modern and Innovative web framework, which we're all kind of know a thing or two about and a really awesome event-driven network programming framework called twisted Which is very similar to a vent machine They're both created by foreigners as far as I'm concerned So they both have really similar roles you can do web stuff in Python and Ruby you system automation You can use it to write scripts for your Unix machines People work on it full-time Some people work on it part-time alongside a language like C++ people do it just as a hobby some people do it for serious stuff and What that means is really they have very similar problems the Python people were in the same things we do But they have different solutions It's like if all of a sudden you double the amount of rubius You have this whole new pool of people to pick their brains And so I've kind of been doing that and I'm not the only one in fact I wasn't even my idea rack is some of you may know is Basically influenced by this thing of Python called whiskey WS GI and it's rack in Python It's a little bit different, but it's mostly the same and it's what they use for all their web frameworks Python even has a problem. It's too easy to write a web framework in Python So there's like a thousand of them which we're getting to that point. Thanks to rack. So thanks Christian, but It's really helped the servers you can play with any web framework in Python You can run it on your server mod Python mod whiskey, and it'll just work Which is now what we're experiencing with rack Rip is a project that I worked on over the summer and it's taking a break right now But it will soon come back and it's influenced by Virtual M and PIP. So what I did is it took PIP and I changed the first letter to an R for Ruby and It's awesome So we're gonna talk about some things that we can steal from Python some good ideas They have just some different ideas and what can we bring back to our community? And you know, maybe they can look at our community and take some things to theirs as well like rake All right, so we'll talk about the language a little bit I'm gonna give it a high-level overview of the cool things. So the VM space One of the things I initially liked about Python when I was checking it out is there's there's there's VMs that you would expect And then there's the wacky VMs because the syntax is pretty straightforward and they're very open about the development So it's super easy for someone to just have a crazy idea and work on a VM get it Most of the way working and then just forget about it So there's this history of like corpses of VMs and Python which is really interesting to see all the ideas that didn't work out So Jython is their version of J Ruby. It started in 1997 I think son hired some people to work on it full-time or they did I know but I don't know if they work on it anymore and It was started by the same guy who started iron Python, which is interesting So they have the iron Python just like iron Ruby runs on the DLR You guys probably all know a lot about the iron Ruby because if you didn't you'd be in that talk right now So I'm not gonna say anything else about it runs on silver light, right? Pi Pi is Python and Python and it's you know It's the self-posting dream the kind of the rabbinius pie in the sky and it's cool Because what they do is they have a strict subset of Python called our Python Which is restricted Python and so it takes out a lot of the dynamic parts of the language And it makes it really easy for them to compile our Python to see to Java bytecode or to whatever net has and So that's how that project works. It's also interesting because it's Funded by the European Union. I guess you can get grants from that And what they do is they're targeting see Python compatibility just like we're trying to do So stackless Python is a cool one because it's like a VM or a fork that has a very specific Need a very specific target a goal is trying to solve and for them. It's concurrency So stackless Python has these things called micro threads or tasklets Which are similar to Erlang processes or go routines if we're gonna be putting the first letter thing and They're cool because they're lightweight. You can have tons of them You can serialize them to disk you can take them up a disk later and can continue with processing with them And it's interesting because it's not just a toy There's a big multiplayer online game called Eve online where it's kind of like space trading and all that sort of thing And all their servers are written using stackless Python. I used to play that game That's there's nothing else there, but Stackless Python is almost all the way compatible with normal Python except for the parts that are stackless All right, so unladen swallow is one that's been in the news lately It's a Python is a Monty Python reference, so there's they go overboard with it I think and sometimes but unladen swallow is it's it's not a totally separate VM It's a fork or no, it's not a fork. That's exactly what I didn't want to say It is not a fork of the VM of see Python. It's a branch So the idea here is they're going to make Python four times faster much faster and then they're going to contribute all their patches upstream and It's it's run by people who work at Google, but it's not officially a Google project It's just sponsored by Google and they're doing quarterly releases and it's very open development It's on Google code and it's pretty cool because they're they're very interested in making Python fast And then there's see Python which is our MRI. It's the reference implementation It was started in 1989 and it's kind of what all the other VMs strive to be compatible with That's the overview of their VM world. It's very similar Ecosystem it's green because of yeah anyway, so the Python ecosystem This is a kind of the some of the things in the community ideas idioms that they have that I think are interesting And aside from that VM part the rest of the stuff is things I've picked out that I don't think we have So we have gython, you know and all that Python does have things that we have but I've left those out So these are the cool like Python specific things that are kind of new to me starting with documentation They're really good about documentation. They're about as obsessed at about documentation as we are about testing So they still have Libraries that a full test coverage they have test frameworks, but not every library has that and they're not nearly as obsessed about it It's all about documentation a really really good Python project is one that has awesome documentation Example of that is Django and from people I've talked to one of the reasons that they love Django is because of the documentation Maybe they are already Python developers or they were coming from PHP with the documentation from day one has just always been phenomenal This is the Django book if you've ever seen it. It's their a-press book. It's community comments or something like that So they can creative commons. It's all open source You can buy a physical copy, but they wrote it all in the open and what they have along the side there The blurry little yellow dots those are little comet bubbles and you can see in there the number of comments for each paragraph So if you've ever used github and you've ever seen a comment on a commit We stole that from this book so so it's cool because it was written in the open people discuss things and the book was kind of a Community project and it's free. You can go check it out today. They're actually working on Version of the book for Django 1.0 that this is a screenshot from Aside from the book the Django site itself is has really good documentation And the screenshot doesn't really capture it and it's kind of hard to know what that means But when you look at this Django documentation page and you scroll down Everything is there and it's not like this deep tree structure. It's just the model layer the view layer the template layer Deploying a Django application and everything so it's kind of like the rails guides But they've just been polishing it for years and it's very concise and if I were doing hardcore Django I think that this would probably be like my home page. I would just go there I would find it and it's all there. It's open source people contribute to it. It's really well done This is a project called fabric. It's a similar to Capistrano and this uses Sphinx, which I'm going to talk about a little bit It's not the search engine Sphinx a different Sphinx generates documentation But this is sort of the standard documentation format You'll see in Python including the Python standard libraries documentation and it's laid out not as an API reference You know, it's not class module method It's actual documentation about installation and so that's kind of where it's different from us where what we'll do is We'll have a read me we'll have a couple other pages and we'll run our doc on it We'll get a nice theme and we'll publish that but what that really is is some explanation and then an API reference whereas in Python what you tend to see is just like Essays on the code how to use it how do you use it problems you're going to run into so I think that's something We can really take away from there from their culture and it's something like us at github in our open source We are just recently trying to do we're trying to do like documentation driven Development and stuff like that where you write documentation first and then you make the code and the test match that so you kind of see How you would explain it and if that makes sense then it's probably a good idea and if it doesn't then you're doing it wrong But yeah, I think better documentation tools like YARV are really awesome. I've sprung up in the past couple years weeks And if you guys have open source and you're not doing a lot of documentation you should give it a shot All right, so pet is another cool thing that the Python people have so we used to have this I remember when I first became a Rubyist there was the Ruby version of pet So this is the Python enhancement proposal and what this means is they have a website and a submission process and you can Submit an idea to pet and it could be one of three things. It's either a new feature or implementation detail for Python itself It's a Python design issue or like a guideline for the community like all projects have to start with a P That's not a real pet, but that could be a pet And a process pet which describes You know how it's a meta pet. Let's say you can do process for submitting a patch to Python itself Honing the process of submitting a pet itself and that sort of thing So this is cool because there are some peps now that are like legendary That are old and when you're new to Python They basically hand you the manual and a bunch of them are peps one of which is pep 8 Which is something that we should really think about pep 8 is the Python style guide So should you use tabs or spaces? Should we use camel case? Where should we use it? Should we use underscores? That's all formalized in the Python community under pep 8 Google doesn't follow it with their Python code, but everyone else seems to do a pretty good job of it and You know for me, that's always one of the things I wonder what I'm writing JavaScript Should I do camel cases that I should I do underscores for method names? It's like I don't want to I don't what I want to do first is figure out how everyone else does it So I don't look like an outsider right so pep 8 does that you just go it shows you how to import stuff cleanly It shows you how to structure your code, and it's a really nice read pep 20 is another fun one It's the Zen of Python if you've ever heard this it's basically 19 or so single line statements that try to describe the Python culture and I think some of them are really good like Explicit is better than implicit. I know we don't always subscribe to that as rubious But that's kind of how the Python people think and once you read this this pep You can really start to understand their code and get into the into the mood Flat is better than nested it's another example of one and if the implementation is hard to explain It's a bad idea which I need to remind myself of all the time So I know oh, yeah, there's an Easter egg to if you do their version of require is called import If you do import this it'll print out pep 20 in Python So two to three is a cool tool, and I've not seen this before but it might be a thing so there's Python 2 right now they're on like Python 2 6 and then there's Python 3 and You can deploy your code on each of them or both of them But as you can guess Python 3 is backwards incompatible with 2 so there are syntax that you will be using 2 that'll just break it 3 So what 2 to 3 is is it's a command line script you run on code on Python 2 code And it'll print out a diff of what you need to change to make it compatible with Python 3 it's not perfect, but it's extensible people can submit their own fixers as they're called and It'll help you find out what parts of your code are gonna choke on Python 3 and it's apparently pretty good so The Python community is really interested in making the upgrade process Seamless and getting people to do it. They don't want to get into the situation where like people are still running PHP 3 Which is true. And so on that note there is pep 3003 so this is a recent one. This was on you know the news sites hacker news and reddit and whatnot This is the moratorium So what Python decided to do is they're not gonna add any syntactic changes to the language for two years And this pep describes what that means exactly what's a syntactic change? What's an implementation change? What's a new feature what's okay to add what's not okay to add and the length of time? And the reason they're doing that is because they're giving the other Python VMs and implementations a chance to catch up So they want pi pi They want the other one stack list and all those guys to be able to catch up to current Python And they don't want to keep adding features and you know making it harder for them What they also want to do is they want to make it easier for people to convert to Python 3 So Python 3 isn't advancing very quickly. It's easier for you to be like alright I'm gonna get 2 to 3 and I'm gonna run it and I'm gonna do that this weekend And it won't be an old version of 2 to 3 or it won't be new changes that aren't documented yet You have a long runway for upgrading to Python 3 without worrying about if the Python 3 you're upgrading to is the one That you know about and all that sort of thing So it's a pretty cool idea to not add stuff for two years And it's a good way for them to kind of embrace the community and that sort of thing All right, so Python a language. It's actually a really cool language You might not believe me. I really like JavaScript JavaScript is one of my favorite languages I've always thought that if Ruby is like Lisp then JavaScript is like scheme Of course Ruby is much prettier than JavaScript But there are core ideas in Ruby that are obviously from Lisp and there are core ideas in JavaScript That are different because they're obviously from Scream from scheme and this isn't one of those ideas But that's just my preference. All right, so the attributes in Python. I really like this I don't like public and private and protected. I'm a little bit vocal about that I'm just one of those people and in Python. There's none of that So an object in Python is similar to an object in JavaScript where it's just like a bag with a bunch of slots in it And some of those slots have functions in them and we call those methods and some of those slots don't So it has some cool implications and some horrible ones as well But what that means is there's no method missing in Python. There's attribute missing So when you call a method that doesn't exist, you're actually trying to access an attribute and then Invoke the function. So if that attribute doesn't exist, you can play with it You can give it back a function. You can give it back a number or that sort of thing So you can kind of get not as nice as Ruby But you can get DSLs in Python and you can get chaining and that sort of thing going without Perenn's because in your attribute missing you can invoke a function and then return the value of it So it's pretty extensible and cool and JavaScript needs that. The Firefox guys added no such method to JavaScript Which basically is method missing. Bad. They should add no such attribute So you can decide if you want it to be an attribute or a function call. But all right, so where this falls down everything's in one namespace functions are just methods are just functions thrown in there is instance variables it turns out that having instance variables distinct from methods in two different namespaces like in Lisp is pretty cool Because let's say you have for me. I had did some Python stuff with Redis So I have a Redis Python attribute and I want to store the Redis object somewhere But I also want to use the attribute as a function call or I want it to be customizable What would you end up doing is you have an underscore Redis Attribute which stores your your your back end and then you have your Redis function, which is your front end Same thing for configuration if you have like a template that the first time you access it You want it to compile but you want people to be able to set strings or something like we would do in Ruby Or something would get cached. It's a little bit harder in Python So you end up doing the same attribute name with an underscore in front of it. Cool All right, this is another cool thing. So in Python They have things like you know less than greater than and all that and it's not methods like it is in Ruby Instead though the way that they let you customize them is you can write hook functions hook attributes that the Python interpreter will call so let's say you have an object and you want it to respond to a greater than all right what you would do is you would define a double underscore like GT are Double underscore and Python will call that function passing it You know be and you compare to get yourself and you return the value. So it still looks like a Normal greater than call or a less than call or a double equals or whatever and the way that you override the function is with one of these hooks another example is there's a double underscore str double underscore and Instead of calling two string on objects in Python There's an str function so you pass something in and that str function will call double underscore str double underscore That's kind of how you do it. It's not how we would do it where you directly just override the method instead There's hooks you have to kind of learn about but once you do it. It lets them get pretty interesting Another one is double underscore call double underscore So someone tries to invoke your object as if it were a function Python will invoke that double underscore calm hook If you have it so you can do whatever you want there and then there's lots of parts of the language That use these hooks and expose these hooks for instance in Python You can change the class of an object on the fly by changing what the double underscore class double underscore attribute is Think about that for a second. All right so Future this is another cool thing. They have this this future module. You can do from future import X The canonical example right now is from future import division So in the newer pythons, they're changing the way division works the division operator It used to be that if you gave it integers It would like do some rounding and give you the floor and so it would work with integers or floats So sometimes if you actually got an integer in there, it would totally screw you So what they're doing is I'm making it strict and they're adding another lazy division operator, which is two slashes What you can do in your code is you can do from future import division And you can today start using that feature which will be introduced in the future in your code So you can test your code you can write your code against it even though your current version of Python doesn't support it You can still import things that will be supported in the future that you want to depend on just inside your one file So you can have code that is incompatible with other codes syntactically Running together in different modules using this trick And so like I said, it's a cool way to kind of prepare your code to be upgraded in the future Okay, decorators. These are neat, too They can be emulated in Ruby and I did them in rip, but it's not always a great idea This is how they work basically it's syntactic support for like higher order functions So I have a modifier and I pass it a random string and I have an out sign in front of modifier And then below that I define a function as I normally would and so what happens then is Python Takes the actual function object that I define unescaped here and it passes it into the decorator So modifiers somewhere is a function and so what you can do then is you can change the function the unescape function You can wrap it you can do things like add logging disable logging change what it does entirely do type checking on it You can do all sorts of things using decorators What I'm doing here, which I didn't even realize is I'm actually This modifier is a function. That's calling that's being called and it's returning a function That's then having escaped passed into it. Anyway, it's cool You can do some crazy stuff with it but what I'm doing here what I'm accomplishing is in In my in my templating thing py stash you have different tags that do different things And so I map the actual trigger to the function using decorators So I say if you see this guy call this function underneath it And so that way I can just add stuff at at will and not have to worry about like a dictionary mapping it I just use a decorator that will do it for me and Yeah, you can do decorators in Ruby, but sometimes it helps to have syntactic support for a feature for instance Blocks in Ruby you can do them in JavaScript, but one is nicer than the other. I won't tell you which Metaclasses okay Python has metaclasses, but they are not eigen classes They are different metaclasses in Python and I think most of the world are classes that write classes classes that create other classes So what you can do is you can give a class a metaclass that when the class definition is finished That metaclass gets past the class you're defining and then it can do crazy things to it So an example is in Django. They don't have Migrations well, they have they have migrations, but they don't do it the way rails does it where you define your database schema And then the active record object picks up what the attributes are and columns and whatnot instead In Python in Django models you define the attributes and you tell what type they're gonna be this is gonna be a string This is gonna be an integer and all that in your model and what you literally do is you create attributes and you set them you set types to them so like string column integer column and whatnot and the Models have a metaclass that goes through it finds those attributes It finds out the type that you want them to be and then it turns them into methods that know how to pull themselves from the database And format themselves correctly and whatnot So you still get a nice DSL and you just say explicitly this this attribute is going to be this type of database column And then you get enhanced functionality using a metaclass. So it's a trick just to do nicer DSLs add functionality and that sort of thing Yes, I don't know I Don't know how the migration stuff works all that well. There's a that's interesting. They don't have migrations in Django proper There's a plug-in called south that uses it and I haven't worked with it extensively. I don't do a lot of sequel these days I Only put this in here because I was so happy to see it they have args splat. It works the same way as it does in ruby It's an awesome feature. I would have been sad if they didn't have it But they they they raised the the anti so to speak and they have this thing called quarks So these are keyword arguments. These are awesome. This is one of the best features of Python All right, I have some I have some awesome code. It's all syntax highlighted nice I promise myself at one point. I would never put code on the slide again. So I broke that promise I also promise. I've never do Python. All right, so here's a normal method Normal method call any and same as ruby similar to ruby and I call it like this right So I have name and age and it's not going to do anything but I pass in information So what keyword arguments are in Python? At the simplest level is this so even though name was the first parameter I define and age was the second if I pass in a dictionary and I have The names of the dictionary slots match Parameters it'll work. I mean basically this will work the way you think it's gonna work age Then in your code will be set to 24 and name will be set to Chris in this method definition So that's neat We have methods in in github sometimes that are old and maybe they take four parameters And it should really be like an option task that you passed in with keys because when you're calling it It's a little you have to like check the method definition What you can do if you're lazy if we had these things is we could just put the names of the variables right in front of what we're Passing in and it's kind of a little bit self-documenting. It ends up looking really nice at times But that's just the beginning. So what you can also do is this quarks thing With the double double-splat and what that means is the quarks object objects in your method will be a dictionary of whatever keyword arguments whatever hash stuff was passed into the function that it didn't recognize so Here we're removing the age parameter, and I'm still passing a name, but I'm specifying age is 24 And so that quarks object now will be a dictionary or hash with a slot of age that maps 24 So I could put anything I want in there and quarks will hold it for me I can also pass a dictionary in and I could put two stars in front of it And it'll expand it just like you do with an array in Ruby when you pass in an array and you Splat it and it expands. So one thing you can do with this. It's cool that I do is this I have a Render function in my templating system, and I just pass in the dictionary and it's a lot like the way Rails does options But it's supported by the language and it's neat Okay, the next thing doc strings. These are cool. This is in Python specifically Emacs list has these I added them to JavaScript one time. It was horrible But these are neat function neat feature So basically the way most commenting works in Python is the first line of a function definition Is a string and it can be a multi-line string or just a simple String and it describes the function and the reason you do that Excuse me. The reason you do that is you can get access to that so in the code you can find out the doc string for any function that was defined and What that lets them do besides write cool tools is Stuff like this So and some Python libraries, they'll be really simple test case in the documentation That is serving to illustrate how you use the function but they can also run doc tests on it doc test is a module that comes with Python and The doc test will execute all the code in the strings as like unit tests So you can have documentation that serves as unit tests in your code, which is pretty cool It's not a replacement for unit tests I tried doing it that way and I ended up with like huge comments with like every single edge case And then you didn't even find the code, but it's nice to make sure that like your examples are actually correct And I end up running doc tests on on new libraries all the time that I find them just because it's fun There's a thing in Ruby There's a doc test library that does this with comments You put comments above a function and it will find the code in the function and it will execute it And it's nice, but it's a little different because it has its own specific syntax So you have to kind of go out of your way to appease the Ruby doc test library Whereas in Python the doc test library just works. So it's cool. It'd be nice if we had that Help. I just found this while I was writing the talk and I'm freaking embarrassed because it's so awesome Basically using the doc test and all that stuff or using the document strings You can run help on any Python module or piece of code and it'll basically give you like a man page and it just infers all this based on Hooks like double underscore slots that you set like for this one name I have this disk module that I ran help on and the first thing is probably just because they put a comment at the top Saying this is the name of the module. It'll tell me where the file is There's probably a link and then it'll go through it'll find all the functions and it'll pull out the doc string It'll show me how to use the function and what the documentation is and this is all that generated dynamically on the fly from a module I ran it on the unescaped method that we were playing with earlier and you know, that's awesome. I don't know. It's nice Okay, so Python's Import statement. It's module system. The way that you include code into other code is really awesome And if you ever use common JS or like node.js, they do something very similar So the way that this works if you've ever done any Python or play with any Python You've probably seen this file floating around and you've been like all those guys look at that sucks They doing it's actually really cool So what this file means is that the directory in which it's located is a module So let's say I have a directory called Django if in that Django directory I have this file when I do import Django. It loads this file. Okay, it's because in Python files are modules Which is awesome That means that there's no globals So if you define a function in the top level of a file and like in Ruby you'd be horrified at doing that, right? It doesn't matter because that is in a module. It's a namespace So you can never really stomp on the global namespace in Python because there isn't one There's only the current running context and then modules So a lot of times you'll see code where they have a file and they just to find functions right there It doesn't matter because they're in the context of a module they don't need to go through that extra step of creating a class and Creating some like false organization So here's how this works in practice This is from Django dispatch import signal and so what is going on here is Python looks for this directory structure It tries to find this file and then it looks at all the variables in that file And if it can find one that's called signal, it'll just link it, right? It'll just do like a pointer in the current namespace, so that's how you pull in classes variables anything they do this a lot with settings and You can do multiple things at once here's one where from twisted that internet import reactor protocol I'm pulling in a bunch of stuff from twisted internet module at the same time reactor protocol using it later You don't know what you're looking for exactly and you're like living dangerously you can import everything at once And it's neat you can also expose and control your API this way so you can say all right I'm gonna have One main pi stash and it that pi and in there I'm going to import all of my classes from other places and then so all you have to worry about ever is importing from pi stash Where I could have this like deep crazy directory structure that exposes all my internals you don't need to worry about For Django, this is really nice too because when you're looking at Django code They're importing features that they're using at the top and right there You know where that code is defined and how they're getting it and you can just go check it out You don't even need to worry about actually learning that much about Python You can just look at the load path. You can see it's Django dispatch find Django slash dispatch and away you go Yield it's a trick. It's not what you think it is. They have a yield keyword, but it's not our yield It's different Python supports generators and so what that means is You can do things like let's see I should have wrote an example. It's weird It's a numeration basically so you can create a numeration structures and control flow using yield what happens is with yield creates a generator and it returns an object you can call next on and When you call next yield will keep going so the code starts hits a yield stops you call next and it keeps going So using this you can create things like like iterators You know go through a range of objects call next and it'll just add one to it or whatnot in a loop Or you can do things like break pointing code You can have a chunk of code with the yield and then part two of the code and then another yield So each time you call next you're actually going to different chunks of code Anyway, I'll show you how that's actually useful in a second because it sounds crazy Python has sequences, which are basically enumerables They have tuples lists and dictionaries which are immutable arrays arrays and hashes So just very people pull up give your people talking about that stuff. That's what it means list comprehensions, I don't think that Ruby needs these but I put them in here because they're cool Basically, they don't have an Python has no enumerable module which is like one of the best things ever in Ruby But what they do have at list comprehensions, which well, they look like this So basically you're going to have expressions that filter an array or a sequence in line. And so here I'm saying Find every x in range zero through five and then if you know, it's it's even Return just that number. So it's kind of backwards because the expression is at the end and the return values at the front but you can do this to do stuff really quickly and simply just like this filtering through a list and One of the cool things that I found out you can do is if you change the brackets On the list comprehension into parentheses it returns a generator So you can have your list comprehension be lazily evaluated So you can do stuff like to infinity and then you can do it run next on it until you need it and then stop from there So that's kind of cool This is really interesting to me because it says a lot about the way Python is implemented You can call this vars function and what this does is it returns a dictionary of all the variables and what they point to In scope it's it's a dictionary right So this means if you want to create a new local variable in the current scope you can do this So there's no name variable. I can call vars I can set the slot in the dictionary of name to whatever I want and then bam right there It's in the scope. I mean we can do this stuff all this We do this stuff all the time in Ruby with the vowel and metaprogramming But it's interesting in Python because a lot of the languages implementation is surfaced as data types in that language so for instance Python supports multiple inheritance, which I'm not going to talk about because it's weird but the way that The the class hierarchy is stored is just as a tuple So you define a class you give it more than one parent and then you can access that tuple anytime you want Python do whatever to it Bite code, I don't know a lot about bite code, but I know it's like popular. So I put it in here That's what it looks like you get these little pie-c files all over your stuff and it's cool It makes it fast Okay, I'm here's some cool libraries This one this I tricked you I do know about bite code So you can give anything in Python to this disk library this disassembler And it'll show you the bite code that that is going to represent that's going to make and So I actually was I was working on a project in someone. There's no loop structure There's no like loop keyword in Python So you just do like a while true or a while one and so I had a while one in some my code and I replace it to be while true because I thought it looked nicer and This dude called me out on it showed me the bite code and explained to me that while one was more efficient using the bite code And I was like, okay, that's awesome You win this one so you can do stuff like that. It's it's cool. You can look at the code if you if there's some part You need to optimize you can look at what it's doing Rubinius can probably do this awesomely, but this is there right now. It's cool to look at Sphinx I mentioned earlier. It's documentation thing. It's I thought it was gonna be like our doc And I wasn't gonna put it in here, but it's not like our doc It's a little bit different because the documentation is based on like I said before essays and structures and that sort of thing So here's an example, and it sets up an index for you It gives you a little table of contents and all that and it's a cool way to write documentation Pi soy it's got this is the this is by far the worst names library of any language I've ever heard Does anyone can you just in your head? Can you even guess what it's gonna be? It's a writing games Okay, so you write open GL and physics stuff in it It has bindings to open GL and physics engines written in C And then you can use Python to write a game and if you've ever played a frets on fire I think it's called. It's like a guitar hero clone for the computer. It's all written in Python using this I think and it's pretty cool NumPy this is an interesting thing about Python is they have a really big like scientific computing community Like scientists and NASA people not that we don't but there it's huge Like there are people at PyCon that had never even heard of Google App Engine because they're like scientists people But anyway, they have really good libraries for doing that sort of thing like NumPy and SciPy SciPy depends on NumPy and they actually have a full scientific computing conference last year was in Pasadena Just devoted to scientific computing in Python, so it's pretty cool. You can kind of eat lunch with those people at the conference SimPy it's symbolic mathematics computer algebra system obviously Next So Mercurial, it's actually we have a love at GitHub We have a love hate relationship with Mercurial because it's written all in Python It's so nice. It's so nice We wrote hg get as a plug into Mercurial and it's just like you can understand the code very simply you can like Import it you can embed it you can play with it. You can overwrite functions. Whereas git is all see in pearl Not the pearls bad, but it's like see and then pearl scripts wrapping to see to do things So Mercurial is just really well architected and it's nice to write for and it's a cool system because What happens a lot in Python? Which for some reason doesn't happen a lot in Ruby yet is you get things like Mercurial in track Where everyone uses them and they don't even know that it's Python. They don't even care It's just not marketed to Python people. It's marketed to everyone. So you should think about doing that a little bit Psycho, I thought this was a VM, but I was wrong So what psycho is is it's a library to see extension for Python that adds like a specialized Just-in-time compiler to do stuff really fast some stuff really fast So math stuff they say can be 40 times faster And so if you're doing a lot of that maybe in conjunction with the scientists you can load this in and get a big speed up Twisted which I mentioned. It's probably the Python library. I know the best. It's similar to a vent machine, but In that uses the reactor pattern, but it's a little bit more ambitious and its approach because they want to implement every protocol ever in twisted and ship it with the the package So that's that's really cool If you're writing like an IRC library you just install twisted the IRC stuff is there You could just go to the website and find the documentation and it's interesting projects They also have a book and a website They have one of those websites where if you Google twist it and then you click on the first link Their website will highlight the Google term so the whole page will be yellow It's just the worst thing ever so don't ever Google twist it is all I'm saying Django everyone's heard of it. It's awesome. It's neat They have some cool ideas too like the admin which I saw now that there's like a really good Rails version of the Django admin That only took us four years There's also the the rack bug toolbar if you've ever seen that you slip it into your Rails app And it gives you a little toolbar that you could turn on it shows you stats right in line They've had that forever that only took us another four years, but there's some cool stuff there that we can take also Tornado is new and I only put it on here because it's popular. I've never actually used it, but I read the whole man page so Let's count for something Virtual M and pip I have used these two a lot. These are the influence for rip and It's because Python's packaging system. It doesn't have anything as nice as Ruby gems is versioning So you just have one version of everything. That's it You can like upgrade or downgrade or delete it and so virtual M creates separate Python environments including the interpreter So when you make a new virtual environment, it includes a full version of the interpreter which rip does not do And it's a cool solution I think to that problem and then people can you know trade virtual M's like their Pokemon cards or whatever and you're gonna have virtual M specific to your application and You can kind of debug it in the context of your application and not worry about what else is going on It's super helpful if you're new to Pip is a installer. It works really well with virtual M And it's nice because it can install stuff from git mercurial bizarre as well as from the Python package index Pill it's like image magic, but better. I actually I admit it I have no idea what it is. I was told to put it in here because it's supposed to be really good So if you're ever doing Python image stuff pills where to go? SQL alchemy is interesting. I play with it a little bit. It's a database library toolkit Similar to data mapper, but it's more about like I guess data mapper is going in this direction now but it's very much about plugins and Being a toolkit for doing SQL operations that you can write other plugins on top of and other tools on top of so I don't think you often use SQL alchemy directly instead You use something it provides or someone wrote for it to manipulate a database in an interesting way Nose is really awesome and it's something that we should think about a lot So what knows is is it's an improved test runner and basically a better version of test unit So they have their own test unit nose is Like test unit to so to speak, but what's cool about it is it's fully backwards compatible with their unit test module So if you have a test suite already you can use nose with it You can use the nose runner to get the improved output and the improved kind of runner features Some of which are very cool. You can also then just start porting over parts of your code one by one to nose Without breaking your existing code So I think that's a really elegant way to do it is to make it fully backwards compatible Even though you don't like that other library because it gets users I Python it's like IRB on track. You can save sessions. You can replay sessions You can have different sessions swap between each other at the you can swap between different sessions at the same time It has colors everywhere. It's really nice ASCII doc is cool It's what a lot of documentation is written in the git documentation is written in it The fusion guy who's right on the passenger documentation in it as far as I can tell and it's basically a lot like markdown But it includes like filters for highlighting source code in a different language and little tooltips with a cute little icon It's all written in Python. It can output to HTML PDF man page format. It's really useful We actually support on github now and I want to start looking into it, but I'm not yet. Anyway So the final two things I left for the last because they're crazy Pyrex in Scython Pyrex is a language a lot like Python, but it includes C data structures. So there's like typing and whatnot It's very simple It's kind of a subset of Python and it compiles down to C So a lot of times if you want to write a C extension in Python Let's say you want to wrap the discount markdown library and you want to provide Python API to the discount markdown library, which is written in C Well, what you can do is you can write Pyrex code which is basically Python to do the mapping for you and it'll take care all the gluing of the C extensions all the sort Of like registering your module and all that you can write Python that you're familiar with and then you can get access to a C Module really easily. So there's Pyrex in Scython Pyrex is like sort of cc Cathedral. There's one guy overseeing it Scython is the bizarre where they just take anything including his stuff. So it's interesting to see how the two interact, but So that is my my my five thousand foot fifty thousand foot view of Python. The idea here isn't that you should learn Python I don't care if you learn Python I think the idea here is that you know instead of like sitting on this weekend and writing the next testing framework for Ruby or another one You should think about, you know Finally trying out closure. I mean there's some closure experts here I think or you know playing with something like Python or even looking at Pearl I mean cPan has like sixteen thousand modules on it and it's a super awesome system So there's a lot of ideas there that we can take we can look at we can evaluate and Think of them in context of problems in Ruby or just ideas you have so yeah next time you want to work on a Project do it in another language. Maybe is that okay? All right, so thank you Does anyone have any questions I probably won't be able to answer any of them Where do you find Pearl you can it's on github Boom Okay, no yes To yield oh, so I forgot about that you're right so Up up up up up up But you do So this is actually implemented with using yield. This is why it's cool. So to go from Right away list comprehension to a lazily evaluated this comprehension. They do this with yield So this is an example something you can do we're here if I did on the object that was to return like dot next I would get zero and then I would get two and I would get four on each call And that's like an example something you can do with yield where it's not like built into the language And it's kind of a cool trick All right. Goodbye