 gentlemen it's the time for the last talk of this this Saturday here at Kiwi Picon here in Christchurch and if you were looking for Lee Begg's talk that was 45 minutes ago in the other room. We have a different Lee for you in this room this afternoon. Lee Symes works at Catalyst IT working on all manner of languages and today is going to be telling us about Python and why it is fantastic and perhaps why some other languages might be good as well. Please make a welcome. Yeah so I'm going to be talking about basically why I absolutely love Python and I and some of the bugbears and some of the other languages that I code in from time to time that I also really like. So a little bit of background about me. I studied a Bachelor of, I studied Information Technology and Mathematics at the University of Queensland in Brisbane and whilst I was there I did a lot of Java. I also dabbled in a couple of other languages Python, C, a little bit of JavaScript. Then at the beginning of this year I got offered a job at Catalyst and jumped the ditch and I'm now working in Auckland for Catalyst doing Python JavaScript. PHP occasionally gathering in Perl which is interesting. So yeah my talk today basically it's kind of five somewhat interconnected sections. So I'm going to start off and talk about why I really don't like jumping between languages. Just some of the really irritating things about going from PHP to Python and back again and then I go on to Async and just talk about basically what I love about Python's Async and the various things you can do with Async. Then I go on to the pain point of packaging and then on to unit testing and finally I'm going to talk about why I love Python just because that's what this talks about apparently. So yeah basically I move between PHP and Python and JavaScript and it's always a pain remembering oh I need to put semi-colon there or do I need to put curly brackets or not is it a colon and then indent and just so many things are different and oh the quotes get me every time but I yeah I I just find it so frustrating moving between the so many different languages that I end up having to do a job and then there's spacing which I mean we've got Pepe in Python and there's a couple of different standards but like do I use two spaces or is that four spaces for this project and you know how do I space my functions and it's complicated and try to remember which one I'm going to use is always fun so and then we get naming conventions and Python doesn't well Python's Python it uses all three the just one long word and then camel case and then snake case and you know that's fine you have to look at documentation and is this the library that uses the camel case one or is it the underscores it's so difficult to remember but luckily the documentation for Python is really great and Java's better it's very rigid and structured you will do it one way but it's good and it's bad at the same time and then just general functions like do I do I push to a list or is that a pen what about do I add that which which function do I use I'll check the documentation and you know is it a while true or is it a do while and you know do I do I follow for each or for in so many different ways of looking over a list but at least he only has one important statement right and it does two namespaces which no does JavaScript doesn't and PHP might apparently so yeah that's me basically doesn't really segue but my next topic is asynchronous so I'm gonna just talk about what I think is I think basically you usually write a script and it goes through and does one thing after the other that's slow and you know kind of boring and so you put it into multiple threads except in Python which has the global interpreter lock which makes funny exciting but you you want to do multiple things at once but you always running to problems and of that list and locking and everything so so threading it's basically you you have multiple threads of execution and you do them at the same time but the problem with threading is obviously if you're if you're trying to say add an element to a list you've got to make sure that you don't have to try to do that at the same time otherwise you either end up with two elements a for list one element and each of the list or some kind of explosion that may or may not cause damage which yeah it has its benefit but you've got to know what you're doing with threading you know so the next option really is multi-processing Python does this really well with the multi-processing library but you know sharing data is kind of hard you got to do it one of it yourself you you can't you can't make things sort of interact easily but then again if you don't need that then multi-processing is great but and then the final one is asyncio I absolutely love asyncio I came from twisting and found asyncio in Python 3 4 3 3 and it is the most amazing thing from from my point of view basically what it allows you to do is it allows you to write purely synchronous code you're running the same thread so you don't have to worry about locking or unlocking and making sure you you know sharing data and everything else like that it's all you know it doesn't it doesn't even need to be worried about but the really cool thing is if you say hit a website if you ask for information for a website what will happen is it will basically instead of blocking the entire program it will kind of do some magic that means that you can run other bits of code somewhere else and then when that website gets back to you it will basically start running as though your function as though it was purely synchronous but in the background a whole lot of stuff happened and it means that you can write relatively easy to understand code that runs faster than just pure do this do that than that but you know it it runs faster and you don't have to worry about oh am I locking this or have I have I got to think about communicating or what if I share this data so that's that's why I really like it because it just makes that so much easier to work with so this kind of where I segue into dealing with packaging I know packaging in Python is complicated I'm kept as just polluting system and you got virtually by virtual Ed and the end whichever what you meant to use and one of them supposed to come with Python except from a bunch who and it's it's horrible because I I don't like it but I'm working in Python so I'm stuck with it admission I've never actually built a self dot pi I've never built a package for Python I really only worked with packages as a user of them but at the same time it's kind of painful that way as well so at least it's in Java that there is no package major for Java you you want to you want to try and like you have a library but it has dependencies well good luck let you build system take care of it it's totally the way to go which is kind of not a good way to go but my my go-to awesome packaging is is no sorry and no package major I really like it it does everything local first so if you go pit sorry if you go noting npm install it doesn't go into your system it sits in a directory and it just it doesn't pollute your system by default and it means that and because of that and because it's really well integrated into the whole no JS in but environment it kind of just works the other thing I really like about the whole node JS ecosystem is that you will you start with a package the the first thing you do when you're writing a node when you start a node project is not write some lines of code it's run npm in it you build your package file and all of a sudden you know five minutes later after answering a couple of simple questions you've got a package and then you can go and code and it's not it's not a big problem it's not it's not frustrating it's just easy and that is something that I think Python could learn a lot from just make it easier to package so what do I think Python could do about this well maybe make it easy to create a cell dot hi you know a lot of Python packages are going to use the same sort of thing you know it's just Python files we just want to install them so maybe right pick it and then I need to install a package because my program depends on it so why don't we just add something that saves it into the cell dot pi when you when you run pip so that you don't have to go and edit requirements of txt or something else that just makes it a bit more complicated and then allow your serve pi to install those packages in your local system so that you so that a new developer can come in and go oh okay I just don't know this project can get I need all the dependencies well I'll just go cell dot pi space in store cool now I've got all the dependencies let's go and do some awesome stuff and the other thing is just introduce it earlier I think that the way node handles it is really good because it means that even if you write even if your package is 15 lines of code it's it's really easy to write a package put it on the the node repository and have other people use it I mean my first package for node was 30 odd lines of code that you know did something really simple and people are using it because it's just easy to throw another package in you know I don't need to make my own stuff because someone else has baked it and I'm just gonna grab it off the shelf and put it in with all this other stuff and it doesn't it doesn't it doesn't matter that you're adding another dependency because easy to handle it doesn't impact it so as part of packaging you obviously want to write unit tests right so what sort of unit testing stuff do we have I'm very fond of the way mocker which is a unit testing library in node writes and writes the unit tests so I don't know how well that we really didn't turn out very well did it that's a shame so basically in Java you write it the same way you write in Python so they're incredibly similar and then using in JavaScript you basically you say I want to describe this thing and you can put some things like I'm describing something I'm describing foo which is part of something and I'm saying that foo should return bar and then when you output it it outputs it as something foo should return bar and it's really it's easy to read it's easy to write and it's easy to see that where the failures are in those tests again Python versus Java is very similar I'm it's very very easy to see well they're probably written from the same chunk of code and ported but then you have the JavaScript asserts which are very business driven or was it business driven development business driven testing and also technical term for that but basically you say something should equal 50 so 100 divided by 2 should equal 50 or you know some string should exist and that I really like that because it makes it really clear what you're trying to say which is nice and then we get the final thing which is kind of where I want to do dynamic stuff like if I'm testing my command line utility the command line utility will take a short opt and a long opt and I want to test that both of the work but I can't I don't want to just copy and paste the entire chunk of code that does the test and and just replace that single character you know the single stritten with you know the short top versus the long one so I will put it in a for loop or in the case of JavaScript for each but in Java that's really quite complicated and it requires external libraries and so and then mocking is just interesting I I tried mocking in Java it's not the nicest experience I've tried mocking in Python it's much nicer just because well just because it's nicer it's Python yeah it's it's really it's not I know mocking's just complicated and yeah so so yeah and then the final thing I guess I really want to talk about is just why I absolutely love Python magic magic that keeps me coming back apart from work so this yeah so generators they're really really cool if you haven't played with them I totally suggest it but some you do things like it's really really simple to do but you the results you can get are really quite complicated with the new yield from which I believe was added it was in one of the Python 3 versions remember the exact one but basically what you can do is you can say I'm going to be a generator but I want to basically loop over everything in this generator as well so you can write the iter tools.chain just by using yield from instead of having to well you probably want to use the iter tools.chain but you can write it with yield from if you wanted to and JavaScript also has iterators but they're a little bit more finicky because you've got to use this of for of rather than just treating it like a list but that's only in ES6 which is hopefully supported by some browsers and I know IE8 maybe and just so you're aware don't try and run the JavaScript one I tried it and I had to kill Firefox because infinite loops are fun so decorators they're really great if you just want to pull stuff like a lot of boilerplate codes like oh I need to check whether someone's authenticated. Oh we'll just create a decorator and go at required authentication and we'll just throw an error if they're not authenticated and they're really really cool they're really easy to use but boy is writing them such a pain they just oh so much boilerplate. So you'll see me use decorators a bit because I'm going to talk about context managers which are my little lady I'm really really really fond of writing context managers now. Just so you aren't aware the top one's Python in case and the bottom one's Java and they do the same thing which is open a file write hello world to it well or something and close it again safely which yeah that's it's quite a bit of code for doing that but like the the whole context manager thing just makes it so much easier to do and then there's a really cool library called ContextLip which allows you to do cool stuff like writing your own context managers without having to know all the undone the enter and the undone the exit and all that stuff you can just use an iterator and a decorator to basically create a context manager so what will happen in here is we'll say we'll run the try and then we'll when we hit the yield it will run whatever's in the with block and once that's so in this case it will throw an error which will again get caught in the exception block and then we'll exit with an exit code of one which is really cool because that's somewhat easy to read and it actually works which is nice so that I really really like that and I can't I can't say how much I like it enough so cool things wonky patching did you know you can put the if statement in your class definition why well yeah you and your code right um yeah I didn't know you could do that until I saw it in a library and thought why would you want to okay it's Python that's why you want to do it um yeah you can do cool stuff like that and just go oh I don't need that function we'll just assign it to an empty lambda core it's not worth we don't need to worry about that function we can call anymore so and then ABC's which are abstract based classes basically what they allow you to do is create say a dictionary or a list or a set or a whole heap of course collection type things at the moment but basically what you can do is you implement like three methods and you've got a dictionary with all the clear and you know it items values keys everything and you haven't had to implement that obviously it's not going to be the best implementation because well you it's going to be a dumb implementation but at the same time it's actually going to be a implementation that works which makes developing this sort of thing really fast and clean because you can just go oh yeah I just implemented those three methods and all of a sudden it's a dictionary I don't need to worry about writing a whole heap of boilerplate that basically does whatever this ABC does to make a dictionary and I promise it's the last thing okay I'm not gonna get in the way of your alcohol or not alcohol so and the final thing I guess Dundas I being able to write something plus something else without having to do a whole heap of crazy stuff is really really really really cool coming from Java where in order to do a big integer plus another big integer you end up writing this big integer dot add this other big integer and then you're gonna say it's something because of the way Java works or the way big integer classes work and then if you want to do comparisons it's complicated but with Python it's oh yeah you just implement those methods and all of a sudden you don't even have to think about the fact that they're different classes they just work which is really really nice so that's my talk thank you all for listening and not throwing stuff thank you very much that was fantastic we have five minutes for questions or opinions about languages that that lead may have mentioned in this talk cool we have one up in the middle here and that means I have to run around just one small remark to the monkey patching system version thing I used that to write code which is Python 2 and 3 compatible until I found out that this API was introduced in Python 2.7 and the machine I was running the code on had 2.6 and version as a string there and no tuple so I got two aspects of this one and I haven't heard of mocking other than mocking the language or something but we do that a lot to Java yeah can you tell so basically what mocking does it's usually used in testing so if we say testing right so there'll be you have like a layer that sits between a credit card provider say and your actual API so what you do is you'll be testing this layer and you want to make sure that it gets it calls the actual credit card provider correctly but you don't want to be calling the credit card provider because that's gonna cost you money so what you do is you basically put what they call a mock which is sort of like a fake object and what happened is it will call that you can say oh when you get this argument return true and then in your test code you can say was this function called yes it was okay great so we're calling the credit card provider correctly so as long as the credit card providers doing everything right we know our code is going to work which is basically what mocking does okay we have a question over here hi I have a question that was a really good talk I have no question yeah great talk I also agree with this two toes and set up tools and all those other packaging things I think they're way more painful than they need to be but I found this really cool package on on pi pi PYPI not the not the other pi pi I'm called cookie cutter I don't know if anyone else here's heard about it it's a great tool if you're just starting a new project you can use this to give you like a skeleton for your your documentation and spanks and your tests and and your packaging with your set up up you why it's really really powerful so yeah we're looking at so we've got a few more questions I noticed that in one of your slides you mentioned the problem with packaging I know nothing about Java not much about Java and Node.js but I would like to say something about Python if you want to install packages through PIP for different projects with different versions without polluting your system without using virtual ENV what you can do is you can create one folder for each of your project and compile Python and make it install installation for each of these projects then you use the Python interpreter for that project to you know to run PIP so they can just install the packages you want for your for that project without polluting each other. That's a normal solution I quite like that. Okay so I've got a couple more questions and then we'll wrap it up and so it's mostly a question. I can phrase it as a question but that won't help so PIP in it PIP install PIP in it and that does a lot of what you want and there's some other stuff I'd like to talk with you offline about and we'll make this the last question. One question and one quick point. Java 8.7 has the contact manager kind of things. Yes. Try with resources. Yes. And my question was what is it about writing decorators you find so obnoxious? I never found them that obnoxious myself. Out of all the things that could be obnoxious in Python decorators is not one thing that comes to mind. I don't write them enough to be able to just hit the keyboard and produce a decorator so I have to look it up and it just doesn't seem nice. Like a lot of Python stuff is really nice like the context managers are really nice but writing decorators you've got to define a wrapped function they go check whether you're being called with one argument that the function itself or you've got to return or you've got to do some of the stuff that makes it work. I mean there's the things with if you're being called if you decorate and then you put arguments in. Yeah that's kind of the pain point for me is you know I need do I need to check whether I'm getting arguments or not is it if it's got arguments does it optionally have arguments and there's just a whole thing of really not quite nice stuff I mean yeah there's not much you can do about it but there's not much I still not very fond of it. I just like to thank Grant at the back there just for pushing me over the cliff all doing this talk. Okay and that's all the time we have thanks for your room.