Merken

Practical usage of advanced Python constructs

Zitierlink des Filmsegments
Embed Code

Automatisierte Medienanalyse

Beta
Erkannte Entitäten
Sprachtranskript
so I would assume that we are here all of life or maybe your company offered to pay for the treatment you couldn't say no we
love pleasant and because of its expression flower and nice learning curve and coding style and the fact that Romanian languages language features to look like native languages the general straightforwardness of Python and general lack of a lack of human traits makes it appealing both at the 1st language for people who just started programming and is a new language for experienced developers practice shows that you can quickly start doing useful things and and if you're already experienced programmers so I you still program for Microsoft that mountains to shop for 5 years before I got into my 1st Python project I I I didn't really know that much about price and I didn't have any formal training in Python or reading a book in Python so my my precious purely practical was only interested in getting my task done and not really to deep into theories as was 3 years ago that I learned a lot about life but what I noticed that many tutorials books book was their feature and advanced constantly the usually used practical examples like for example how to generate Fibonacci numbers who really generates Fibonacci numbers and production so that's why I decided to make this talk and provide some practical examples on the use of advanced language concerts in Python so I'm not really here today to teach but more to provide interesting ideas examples to inspire you to practically use those constructed so quick check please raise your hands of those who used yields keyword in production called last in within last year what and superresolution people good who wrote air himself a decorated unless you're in production OK great who wrote context manager lower than last year again uh so it's good so you you're more familiar with the theory so I can not really focus so much on it and jumped examples so for my examples in this talk I used a quote from my perspective that worked on and i collected the quote from different open source libraries so and what is
it Hermoine Python and all the definitions I took from my official clustering so while at user pretty gets a definition so uh so what practically is a terrible terrible is an object that has a teacher and methods and where can used in but it can be used in a for statement in the loop and can be used in the list comprehensions or in generator expressions or you can pass it to functions that expected terrible like all any some filter and whatever and I would like to draw your attention to turtles for those who don't know it provides a lot of worries of functionality to work with a terrible thing iterators so I said that terrible returns iterator so was Iterator iterator basically the whole of the functionality of each rater these to keep the current state of iterations like for example if you iterating released used to remember the current element and that's being returned and basically thing it has to do it has to provide a next month at which is basically a approach produces a you well you on every iteration or raises a stop iteration exceptions if there 3 to results of code can be created you can create a class that implements method and instantiated uh I would like to point out those competing practical work uh of the way and I won't be related to the mother and then generate expression generated expression looks very much like list comprehensions except that the use of around records it's very important differences basically a list comprehensions you immediately get forced the creation of the colonies and production of the covalent so it it takes memory and it's not really you useful and well so of my strong opinion is that and for most of the time you should prefer generator expressions on his comprehension because it saves memory it's something didn't need the Holy uh all of all the then a generator function so generators generators of is a function that has a yield the water well in many of the response so I want to go to the so this is the most practical way to create a complex iterators in Python age was introduced in the same version of Python as because yeah how 1st by immediate and so that creating classes for iteration is not very convenient you can read the discussion about it in that way to fight back and it grew into something way larger into crotons but this is outside the scope so but we quick glance works so if function has the yield work and it's marked as a generator when you call it doesn't actually execute the function it just creates an iterator object when you iterate over it the control flow will actually step and function and go to the 1st yields then return to the a loop outside and then stops until next iteration so let's compute the answer so this code will be executed will go through bring your it'll return but on that's iteration people start from the next line and go on till next yield or and the function so this will print and the 1 2 0 2 0 so what's the difference between the idea of a regular function is that it remembers it stays between the calls so what can be practically used for because many people when they study and the subjects they don't really know well OK this school but what can do 1 of their wary of common usage of generators is based to create a view you over a collection of dual some using a mapping so this is the quote from a project I worked on that some lives the feels some of them that were considered dynamics started with some graphics and basically we needed to get the use of this field maintenance of names in different places so we wrote this simple and generator what's good about it 1st uh you don't need to put the basis for and Eve everywhere in the least 2nd it it looks clean it's easier to read well as by multi-line use comprehension for generating expressions are about other so this is the uh well this 1 some filtering and then once a month so another color where a common way to write generators used to flatten this so this called comes from general framework so flattening list if you have a user fees and you want to iterate over all elements as if it was not a single stream of and when so the is basically usually done with the nested the forces and so they created this iterators so they don't need to copy paste this of course everywhere it's actually good and bad its humble because the turtles module provides a much easier way to flatten least so with a terrible so uh that's and that's the cuckold can be done in 1 line so it's very popular questions the cough also assume that well yeah people have difficulty with it but there can be more complicated flattening logic this quote comes from gender to from Ginger system planting engine used often use with last framework so they have a list of items and item can be and node or at least so if it's not we return its yielded if it's at least we don't to several so this is what here can't user really the turtles that so this is a good example of more complex flattening so what it can be also good for generators is to save memory so this is called comes from requests library is the so called HTTP library for humans and what the word means but it's very convenient library tradition request so somewhere in the depths of food that cover socket object or of some sort of rapid so that role that gender that well it's a network stream needs a sequence of bytes but request responses from service and very large wouldn't really so want to load them in memory completely so they rotate iterated that breaks it into charts and you can iterate over those chunks without actually loading the whole content of the response into memory that's where convenient because it saves memory and it's convenient basically because users of this function may not really need a whole whole responses in memory they can be used to directly write those chances to file so you can say that locally with and it's from memory and performance so it's some more even more complicated so this is their internal usage of this a chance and the traitor so what's that doing here so it's iterated that takes uh and tracer of chunks and produce a traitor all strings so chunks have fixed length about a string but it can be arbitrary length and
their divided by a delimiter so what they do here they break chunk into strings but it can happen that the remainder of chunk is incomplete and we need to get not to get another chance to complete it so here we have the introduce a state of a local variable paintings which can which carries over collapsed over all chunks that is on computers strength they energy but before between fields and the editor chance so that they can they remembered between calls so that they can connect it with the rest of the string and return so if if their chance there are no more chances than they assume that spending is complete string and return it it's a convenient example how also you can save memory and created an iterator over reiterated with the generator so uh which raises generators are also convenient to traverse complex data structures so this is example 1st called All standard worse module of the function walk by simplified significantly well it's like 200 classical but this is the core of it so it uses a recursive iterate you trust generators so it's a very convenient in this case because the file system is a tree data structure so walk returns a couple of current off the list of dearest in this path and use files and then it goes over the list of directories and current path and calls itself basically and use the results so in the end if we iterate World walk generated we will get a flat list of your article data that's where convenient our also iterators can be infinite so some people may be wondering what why would you need a infinity treaty can't even iterate over it you can do for over it because it's like Italy memory and you uh this is example of this agenda templating language for those who are unfamiliar just basically copies of the 4 for tag copies these content it's here generates a roles in the table so we would like to often used in you know and rapid uh that you have tables there the roles come in different colors 1 after another so just because this cycle time which actually will produce on every call row 1 row 2 row 1 row 2 internally is implemented with the turtles cycle reiterated which is infinite iterator in the lower part of the enter tree in the lower part you can see well it basically takes a terrible and then repeats itself 1 by items 1 by 1 forever so every time this tag is called it goes Brenda method and just calls and next item on the trade so it's not a problem that you iterator here is infinite from not never tried to iterate over completely filled call it exactly as many times as I for iterates so this is something called you don't need to maintain in the state like which really was a drunk previously just call maximum cycle so again I of you were at the tools and modules so those are the most common practical examples for using but I recommended to reserve deeper because iterators generators is very complex topic in Python it's because way more advanced user just so I recommend you to a look more into turtles the turtles of module and message because it can improve your productivity than actually yield is not this statement is an expression so it can be used as a mechanism to finance their wages from Paula to a generator which can they later and led to Python to even more advanced stuff so not then I recommended to and read about the yield from its so-called generation delegations and they are those 2 the teachers so used to implement coroutines in Python which is also very interesting and based on proteins the reason you about relatively new uh module as well the whole concept in Python called a Cinco expertise to that it's for a synchronous input output and there heavily based on carotid generators and it it should the need to foot of the slides that the will put them online and so yet again don't worry if you something you can check out so another advanced feature that so orphaned not used in practice by well maybe started developers it said to craters so the again miniaturize so I would assume that many of you know how the great works of basically the crater wraps your function with a sum in a function what is basically all it does it assigns to your function that they decorated the rap version of the function what is it good for practical with a decorated you can modify the input arguments you can modify return value uh well you can do things before call my functions called after functions called you can actually not call the function but also you decorator we decide not to call functions for some reason you can modify some global states and alter wearable somewhat thread locals and stuff like this for this function and then send them back to their values that were before that and you can use to a decrease in the useful to a ascending metadata functions of all sorts so and this is example from my last but framework the heavily used the greatest so we can they demonstrate 2 things here it it is hello is that you it's basically a function that is called when you when you call uh your your web servers uh left alone or special name so what they do here they the user decorators parts Europe into a function arguments and another usage of integrators here use that lead to uh basically makes it possible to discover the function so that so that framework knows which functions are used so in this sense it provides the metadata for your application and this is the small snippet from from my current projects so we have this we want to make an extensible list of so while some filters for their that means and we would like to associate that we would like to show them on you so users can select from a list so we need to something like sort of label for those so I created a crater that provides a human
readable description that is used on your right also item query all the functions in the modules and check if they have this and if the curve is the greater so it's also good for discovery and for providing metadata that can be used for example in your life which so call or not so with a decorator you can make integrated that decides which might be actually we shouldn't call the underlying function at all this is again simplified example from Django they have the critical permission of quiet so you can apply this the crater on that that you function and check if the consulate user has certain permission to call this the you so what you can see here they check if the current user has this permission if you has this permission the call the called the actual function if user does have permission the don't even call the function so that's that that's how we can and check whether you should Lexical-Functional not rays error or whatever something else so this is the degree the rate-limiting we created in 1 of my projects basically it's also for use it counts in a cash cow off on their speech is called by submit the address and for example if it's called more than 10 times in minute we will not generate the response will show an error with some where a simple mechanism for improvement of abusing of service and yes so if the user course it too often we can prevent call completion at caching use a very big feature of that can be done with the greatest so this is in our project to create a really simple and I have to confess even this stupid caching decorator that we applied on properties exclusively that's important point so have this number persons and it comes in is the gender of uh were around so basically just gone related objects so this function is called this properties could be called a lot in the reports to have reports that call this function I don't like 20 times right and we can just drop this the great appears results here and can very conveniently can prevent too many of the calls so how we implemented it's also where straightforward so you can see an here we get the function name that will be numbers and this case so the 1st that there for the object has this attribute we we do get attribute if it's not if it's not in the object we actually call the properties calculated and then set and corresponding attributes and the subsequent course of this property will return the the best value so what's bad about it is basically that's the yeah it's a objects or data changes well you and there is no way to enforce recalculations but this is good for simple cases and like for example if the if you sure that your object 1 change so the proper way to carry with it decorators there is this summary like this but library called don't while is actually creates proper and very sophisticated way to pairs of careful functions and methods everything so they can then she decorated there is Captain arguments you can provide different cancer memcached local memory or anything and they will on actually care where the function for corresponding arguments as well so it's the it's they just will include their function arguments it's were smart you can refresh you can refresh wages and everything I recommend suggests that we want to look into this because this is probably the most secure caching and library with the degrees of substance so far and my my personal award for most creative uses of decorators goes to an and and these were a powerful tool for IT automation and you can use it for deploying your called and servers manager infrastructure was recommended to look into its were powerful but inside the hero who were interesting had a critical timeout now so you can just to function for the does give the function takes longer than loved by default just seconds state security this so a function execution and the reason they're called implemented so the implemented using Unix signals so before the function is called itself so that seemed all around the world call by so and then they said that all 2 seconds so what happens for this work and it's a and Journal feature of units of any other what is compatible with uh it will it will set this alarm internally and when this all alarm goes of it will call it will call the kind the muscle so and the the method will rise time of error and this time out there will appear within the function and it will X is the function immediately so that it takes longer it it'll just stops the function and X so this is a pretty nice way to do it function by time out without do using threats obviously this and will not work on Windows sorry but this is mostly for Unix operating system and you deeper so there decorators or can be way more complex than examples that are shown as there was a previous talk on today and which they didn't use it but I think cover the cover to hope maybe some of you were there in the program there on the big will cover advanced cases of the greatest so correct integrators again to create classes and integrate herself in the class that maintains a state so you can create a way more complex things but I would say it in for more people most practical cases that data simple decorators are enough but I recommend you to research more into the context managers
context manager and this is how well how could we call with this with this statement would it is just a very simple thing is basically it calls and turn on and on and context mental objects and returns and where where you and censorable then it calls the actual called that is within the with that statement and finally it because and so what it what it is they can be good for it's good for if you want deterministic release of and manage resources for example files everyone must use with with files and and because it will cause the file immediately after he stopped using it it's also good to modify some global states for example you can select a piece of gold that will be executed within transactions or a concept some sitting just for this piece of code and then restore the setting and can be used to a lot of interesting metrics for logging from modern Slovenian debugging so a couple of examples in general so the have session transaction so exit and X and basically the check you their current scope within a transaction if yes then we need to commit the transaction Walras well on a roll back so that's that will work to date and transactions and the really important here is that finally is called always so whatever there is in transactions or not it will call this the exit methods always so for example in the request library they have a session in context manager when you exited it causes the session is also good because you usually causes as fast as you start using it and not when garbage collector peaks examples this is a small and context manager that I wrote for debugging basically when it enters its stores internally there time when you entered into a function of not function of the piece of code and when you exit it just brings the amount of MS it took uh took to execute your this of course this is where simple very easy to into micro providing a small small chunks of codes without you doing too much and calling for father analysis of so the innkeeper
again I of course didn't cover all their use of context managers so gender does a lot of stuff of context managers especially when custom transactions they also have is both a decorator and context manager because sum is support so also you can implement with a decorator some context mantras you can implement the database so you can select for example let's say that this function for this piece of code depending if it's decorator context mention all the queries will be executed against slave database is for simple useful for reporting the cover function generator report you can create a context mention saying he use Flickr database for all queries and also more sophisticated love and about not just a stupid milliseconds which is actually where it has very low precision and yeah can do some more sophisticated profiling solicitor think were much coming out this is my e-mail you can tell us some the suggestions questions whatever I register that stupid domain therefore for my personal blog and I will I'm going to put you the slides and there and I will I started a series of polls which basically I cover this sense of what more details with the legislation so if you miss something uh you can come
there and we get all the all the content from the slides will be there in the book so thank you very much for a few I do have time for questions and 1 question to bureaucracy and our rule hashing module in the standard library in kind of doing the same thing as 1 of your examples is just precious alone the result code the result well you for specific parametres sets use so maybe maybe you can also use that 1 question did you already discovered with the pattern to all of us to handle open files in are 3 is discovered what to to handle open files is from the context is not as localization sometimes with with so you basically need to give a wrong to open file handle and you still you need to make sure that it is close that sometimes there could have for that well of yeah well with the context will also in the 1st yes there is there is a lot of this caching their relative caching done with a decrease in very different libraries but I recommend it to check out the well because it as a solution the most advanced 1 it has the the richer functionality coming to the next 1 a well if you want to views from different places well then it's a little bit again gives me a little bit more complicated than yeah you will you you you you can't use a low context mentors are good for small chunks of code so you you have answer but you know you can you could write your own class that will store a file handler and once this and you can call call calls on your own class when you finished doing with this problems as far as I know there is no light straightforward thing to do it with the context members if hi hi is still of the of the declarative from if I have for instance to the College of so it is going to rather than following the yeah this is this is you're right this is the tricky part and yes decreases the order of the greatest effect effects so some decorators for some it's important the water is important mainly in which you do it but for some it's not so yeah order matters general case in more specific so you can write the craters that for example I will continue where there is more than my wonderful care so here it's important a
property should come before the crash results otherwise if you do it the other way you want for just just 1 for proper property but
because the property is a special of build integrated young but terms you can proceed with a greater you can write the the logic that in the way that it wouldn't matter so really depends yes particularly great how so I
think it actually is in no 1st it will call captures all the
greater and the 1 is then it will call property so that's why it's important that property will be like outermost yeah this is a trick here this is tricky part had a
question still have a few minutes
hello thanks for the help they just 1 observation about to be aware that the greater than the standard library of so I saw that the in 1 example that's present then the god is when you're writing a wrapper function functional you're not using that so I think it's worth mentioning that the eighties better to use because it the copies the docstring and functions in those things yeah you're absolutely right there at the various practically you should use their reps from funky to the model that's so the right because it will keep the functioning and no clear serine and all this stuff here but it was just like much of a little maturity things were not the can form Christians constant we know nonsense and and that
Videospiel
Subtraktion
Programmiergerät
Wellenpaket
Open Source
Formale Sprache
Formale Grammatik
Biprodukt
Kontextbezogenes System
Physikalische Theorie
Service provider
Task
Generator <Informatik>
Arithmetischer Ausdruck
Datenmanagement
Perspektive
Fibonacci-Folge
Programmbibliothek
Vorlesung/Konferenz
Projektive Ebene
Kurvenanpassung
Optimierung
Softwareentwickler
Gewichtete Summe
Pauli-Prinzip
Iteration
Aggregatzustand
Computeranimation
Netzwerktopologie
Streaming <Kommunikationstechnik>
Metadaten
Polygonzug
Kontrollstruktur
Dateiverwaltung
Gerade
Kraftfahrzeugmechatroniker
Dicke
Befehl <Informatik>
Sichtenkonzept
Datennetz
Güte der Anpassung
Strömungsrichtung
Ausnahmebehandlung
Digitalfilter
Biprodukt
Erzeugende
Divergente Reihe
Generator <Informatik>
Dienst <Informatik>
Festspeicher
Server
Socket
Ext-Funktor
Zeichenkette
Tabelle <Informatik>
Subtraktion
Folge <Mathematik>
Kontrollstruktur
Wasserdampftafel
Klasse <Mathematik>
Regulärer Ausdruck
Mathematische Logik
Überlagerung <Mathematik>
Loop
Iteration
Endogene Variable
Programmbibliothek
Thread
Generator <Informatik>
Inhalt <Mathematik>
Datenstruktur
Modul
Anwendungsspezifischer Prozessor
Elektronische Publikation
Modul
Unendlichkeit
Turtle <Informatik>
Wort <Informatik>
Kantenfärbung
Streaming <Kommunikationstechnik>
Resultante
Formale Sprache
Versionsverwaltung
Befehl <Informatik>
Kartesische Koordinaten
Element <Mathematik>
Computerunterstütztes Verfahren
Komplex <Algebra>
Synchronisierung
Arithmetischer Ausdruck
Funktion <Mathematik>
Lineares Funktional
Parametersystem
Filter <Stochastik>
Stellenring
Systemaufruf
Ein-Ausgabe
Systemaufruf
Softwarewartung
Rechenschieber
Texteditor
Datenfeld
Funktion <Mathematik>
Geschlecht <Mathematik>
Projektive Ebene
Datenfluss
Verzeichnisdienst
Message-Passing
Aggregatzustand
Implementierung
Nichtlinearer Operator
ROM <Informatik>
Code
Framework <Informatik>
RFID
Datensatz
Benutzerbeteiligung
Koroutine
Softwareentwickler
Implementierung
Elektronische Publikation
Diskretes System
Mailing-Liste
Physikalisches System
Objektklasse
Gerade
Quick-Sort
Integral
Mapping <Computergraphik>
Objekt <Kategorie>
Energiedichte
Basisvektor
Mereologie
Dreiecksfreier Graph
Speicherabzug
Resultante
Punkt
Adressraum
Computeranimation
Deskriptive Statistik
Metadaten
Metropolitan area network
Einheit <Mathematik>
Datenmanagement
Mehrrechnersystem
Zustand
Kurvenanpassung
Default
Lineares Funktional
Parametersystem
Kraftfahrzeugmechatroniker
Multifunktion
Vervollständigung <Mathematik>
Kategorie <Mathematik>
Computersicherheit
Stellenring
Systemaufruf
Abfrage
Kontextbezogenes System
Sinusfunktion
Dienst <Informatik>
Geschlecht <Mathematik>
Rechter Winkel
Festspeicher
Server
Projektive Ebene
Aggregatzustand
Fehlermeldung
Klasse <Mathematik>
Mathematisierung
Zahlenbereich
Sprachsynthese
Überlagerung <Mathematik>
Endogene Variable
Programmbibliothek
Optimierung
Normalvektor
Attributierte Grammatik
Videospiel
Zwei
Varianz
Physikalisches System
Integral
Objekt <Kategorie>
Uniforme Struktur
Minimalgrad
Caching
Verkehrsinformation
Gewichtete Summe
Web log
Aggregatzustand
Code
Computeranimation
Überlagerung <Mathematik>
Metropolitan area network
Domain-Name
Datenmanagement
Determiniertheit <Informatik>
Programmbibliothek
Gleitendes Mittel
Speicher <Informatik>
E-Mail
Analysis
Lineares Funktional
Befehl <Informatik>
Datentyp
Linienelement
Logarithmus
Datenhaltung
Singularität <Mathematik>
Reihe
Varianz
Profil <Aerodynamik>
Abfrage
Gasströmung
Objektklasse
Flickr
Elektronische Publikation
Kontextbezogenes System
Objekt <Kategorie>
Rechenschieber
System F
Transaktionsverwaltung
Menge
Codierung
Speicherbereinigung
Verkehrsinformation
Aggregatzustand
Resultante
Bit
Subtraktion
Wasserdampftafel
Klasse <Mathematik>
Systemzusammenbruch
Extrempunkt
Code
Computeranimation
Metropolitan area network
Mustersprache
Programmbibliothek
Vorlesung/Konferenz
Inhalt <Mathematik>
Soundverarbeitung
Lineares Funktional
Sichtenkonzept
Kategorie <Mathematik>
Stellenring
Systemaufruf
Schlussregel
Zeiger <Informatik>
Kontextbezogenes System
Elektronische Publikation
Modul
Rechenschieber
Caching
Mereologie
Ordnung <Mathematik>
Parametrische Erregung
Standardabweichung
Instantiierung
Motion Capturing
Metropolitan area network
Kategorie <Mathematik>
Mereologie
Systemaufruf
Vorlesung/Konferenz
Term
Große Vereinheitlichung
Computeranimation
Metropolitan area network
Lineares Funktional
Informationsmodellierung
Rechter Winkel
Wrapper <Programmierung>
Grundsätze ordnungsmäßiger Datenverarbeitung
CMM <Software Engineering>
Programmbibliothek
Luenberger-Beobachter
Vorlesung/Konferenz
Hilfesystem
Computeranimation

Metadaten

Formale Metadaten

Titel Practical usage of advanced Python constructs
Serientitel EuroPython 2015
Teil 03
Anzahl der Teile 173
Autor Syschikov, Andrey
Lizenz CC-Namensnennung - keine kommerzielle Nutzung - Weitergabe unter gleichen Bedingungen 3.0 Unported:
Sie dürfen das Werk bzw. den Inhalt zu jedem legalen und nicht-kommerziellen Zweck nutzen, verändern und in unveränderter oder veränderter Form vervielfältigen, verbreiten und öffentlich zugänglich machen, sofern Sie den Namen des Autors/Rechteinhabers in der von ihm festgelegten Weise nennen und das Werk bzw. diesen Inhalt auch in veränderter Form nur unter den Bedingungen dieser Lizenz weitergeben
DOI 10.5446/20078
Herausgeber EuroPython
Erscheinungsjahr 2015
Sprache Englisch
Produktionsort Bilbao, Euskadi, Spain

Inhaltliche Metadaten

Fachgebiet Informatik
Abstract Andrey Syschikov - Practical usage of advanced Python constructs Python is a language of choice for developers with wide range of experience, for some it is a first programming language, others switch to Python after years of experience. Python provides friendly syntax and smooth learning curve. This sometimes leads to developers lacking comprehension of some more advanced constructs. It happens that experienced developers jump into using Python and sometimes miss less known Python language constructs. On the other hands people who purposefully learned Python sometimes lack practical ideas for how to apply those constructs. This talk will be specifically focused on the practical usages of advanced Python constructs like iterators, generators, decorators and context managers. Goal of the talk is to share ideas about how those constructs can be used for practical purposes in real projects. Prior knowledge is not required, there will be a brief introduction to every construct being presented.
Schlagwörter EuroPython Conference
EP 2015
EuroPython 2015

Ähnliche Filme

Loading...