AV-Portal 3.23.3 (4dfb8a34932102951b25870966c61d06d6b97156)

Python Standard Library, The Hidden Gems

Video in TIB AV-Portal: Python Standard Library, The Hidden Gems

Formal Metadata

Python Standard Library, The Hidden Gems
Digging into unexpected features that the Python Standard Library exposes for us.
Title of Series
CC Attribution - NonCommercial - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this license.
Release Date

Content Metadata

Subject Area
The Python Standard Library is a very rich toolset, which is the reason why Python is stated to come with ""batteries included"". In such an amount of features and tools it's easy to get lost and miss some of the less unknown modules or gems hidden within the whole load of functions and classes. This talk aims at showcasing some recipes, snippets and usages of standard library modules and functions that are less known or that are not covered in the most common books and tutorials available for Python. The talk will try to showcase a bunch of short examples in the hope to foster the ""oh, wow! I didn't think about that"" reaction at least once in the audience. We will see how frequently for tasks where you used third party libraries or frameworks a solution bultin into the standard library is already available, and such solution is guaranteed to be maintained and well working for the years to come thanks to the standard library reliability and stability. The showcased examples are took from the “Modern Python Standard Library Cookbook” book that I authored.
Keywords CPython Debugging Development General python
Module (mathematics) Point (geometry) Focus (optics) Functional (mathematics) GUI widget View (database) Multiplication sign Source code Projective plane Software maintenance Formal language Web 2.0 Personal digital assistant Googol Software Core dump Video game Website Software framework Endliche Modelltheorie Point cloud
Module (mathematics) Intelligent Network Complex (psychology) Email Server (computing) Code Multiplication sign Cartesian coordinate system Login Computer icon Software bug Product (business) Crash (computing) Message passing Explosion Process (computing) Integrated development environment Personal digital assistant Crash (computing) Video game Software testing Endliche Modelltheorie Error message Physical system
Email Functional (mathematics) Multiplication sign Line (geometry) Cartesian coordinate system Web application Crash (computing) Message passing Software Personal digital assistant Videoconferencing Spacetime Exception handling
Point (geometry) Server (computing) Context awareness Dynamical system Code Multiplication sign Real number Parameter (computer programming) Login Data dictionary Proper map Declarative programming Formal language Positional notation Different (Kate Ryan album) Object (grammar) Hierarchy Software testing Data structure Namespace Social class Email Namespace Content (media) Mathematical analysis Line (geometry) Cartesian coordinate system Category of being Word Web-Designer Object (grammar)
Functional (mathematics) Context awareness Computer file Code Formal language Number Web 2.0 Roundness (object) Semiconductor memory Object (grammar) Operator (mathematics) File system Elasticity (physics) Energy level Software framework Namespace Physical system Form (programming) Namespace Maxima and minima Limit (category theory) Band matrix Category of being Web application Process (computing) Personal digital assistant MiniDisc output Video game Object (grammar)
Matching (graph theory) Computer file Multiplication sign Login Login Cartesian coordinate system Web application Root Process (computing) Read-only memory Profil (magazine) Semiconductor memory Source code Process (computing) Physical system Flux
Touchscreen Information File format Bit Function (mathematics) Cartesian coordinate system Proper map Proof theory Radical (chemistry) Web application Crash (computing) Software Semiconductor memory Personal digital assistant Operator (mathematics) MiniDisc Electronic visual display Video game console Arithmetic progression Resultant Window Exception handling
Point (geometry) Touchscreen Software Real number Video game console Right angle Utility software Line (geometry) Arithmetic progression
Module (mathematics) Ocean current Complex (psychology) Functional (mathematics) Implementation Touchscreen Information Equaliser (mathematics) Computer file Real-time operating system Function (mathematics) Mereology Radical (chemistry) Programmschleife Loop (music) Solitary confinement Configuration space Video game console Arithmetic progression Resultant Window Physical system Family
Web application Medical imaging Crash (computing) Process (computing) Computer file Computer file Content (media) Right angle
Module (mathematics) Functional (mathematics) Implementation Computer file State of matter Content (media) Data storage device Insertion loss Database Temporal logic Cartesian coordinate system Mereology Code Usability Software bug Crash (computing) Term (mathematics) Personal digital assistant Interrupt <Informatik> MiniDisc Utility software Endliche Modelltheorie Reading (process) Physical system
Context awareness Functional (mathematics) Computer file Ferry Corsten State of matter Code Content (media) Line (geometry) Data management Word Personal digital assistant Operator (mathematics) File system MiniDisc Exception handling
Point (geometry) Context awareness Functional (mathematics) Computer file Differential (mechanical device) Multiplication sign Virtual machine Open set Mereology Coprocessor Crash (computing) Read-only memory Different (Kate Ryan album) Semiconductor memory Address space Physical system Module (mathematics) Multiplication Projective plane Content (media) Shared memory Data storage device Electronic mailing list Database transaction Database Data management Process (computing) Software Personal digital assistant MiniDisc Writing Reading (process)
Web page Complex (psychology) Dynamical system Code Multiplication sign Virtual machine Set (mathematics) Web browser Function (mathematics) Mereology Read-only memory Semiconductor memory Different (Kate Ryan album) Endliche Modelltheorie Information security Physical system Social class Module (mathematics) Default (computer science) Email Trail Inheritance (object-oriented programming) Namespace Software developer Projective plane Data storage device Fitness function Content (media) Variable (mathematics) Connected space Web application Data management Process (computing) Integrated development environment Software Vector space Personal digital assistant String (computer science) Video game Right angle Object (grammar) Escape character Writing
File format Markup language String (computer science) Multiplication sign String (computer science) Software framework Function (mathematics) Escape character Resultant Wave packet Social class
Implementation Context awareness Inheritance (object-oriented programming) Markup language File format Code Multiplication sign Drop (liquid) Mass Line (geometry) XML Spherical cap Personal digital assistant String (computer science) String (computer science) Order (biology) Representation (politics) Video game Object (grammar) Table (information) Communications protocol Social class
Web 2.0 Web application Functional (mathematics) Web-Designer Software framework Software framework Mereology Magnetic stripe card
Point (geometry) Windows Registry Server (computing) Functional (mathematics) Link (knot theory) Code Multiplication sign Parameter (computer programming) Web browser Regular graph Mereology Web 2.0 Root Semiconductor memory Representation (politics) Software framework God Social class Dependent and independent variables Information Sampling (statistics) Electronic mailing list Regulärer Ausdruck <Textverarbeitung> Cartesian coordinate system System call Web application Uniform resource locator Personal digital assistant Software framework Video game Object (grammar) Routing Resultant
Ocean current Functional (mathematics) Context awareness Code Multiplication sign Parameter (computer programming) Machine vision Web 2.0 Causality Semiconductor memory Term (mathematics) Single-precision floating-point format Energy level Software framework Installation art Information Line (geometry) Tracing (software) Software maintenance Cartesian coordinate system Web application Word Data management Integrated development environment Software framework Spacetime
Functional (mathematics) Message passing Code Letterpress printing Code Summierbarkeit Line (geometry) Tracing (software) Near-ring
Module (mathematics) Dataflow Functional (mathematics) Information Code Multiplication sign Letterpress printing Line (geometry) Tracing (software) Tracing (software) System call Order (biology) Endliche Modelltheorie Row (database)
[Applause] he already said everything but I let me have a quick introduction of what I am doing in my life right now I'm the maintainer of the tool reveals two web framework which is one of the top 10 web
frameworks in Python currently and maintain of the bigger project for caching and session which is widely used by many web frameworks contributed to me Tosca widgets without kojiki as you can see all web related projects so most of my time is spent in the web world with Python and from the work point of view I'm one of the founders of a company in Italy technical paisan of three startups in Italy and one of the engineers behind the crunch dollar your project which is a great project to don't know it let's have a look and reason why I decided to speak about this topic is because I recently published the book which is named the modern I don't start the library cookbook as I you can guess it talks about the Python standard library why I decided to write such a book when item is full of great documentation great books and things like that the reason is that most of the cookbooks that are available for Python are actually about the language the core features of the language some of them showcase some features that are available to the standard library but most of the focus is about the language itself and we have some great sources for documentation about the standard library like the documentation itself the Python module of the wit project and things like that but they try to focus on how you can use those models how they work like they are more like a reference when you already know that they exist they are not a very convenient way to discover new models or corner cases or ways you can use those models and the Python standard library it's really huge I still discover new features and models and functions every day and I've been using it for like 15 years or something like that and this is actually very interesting topic because race there was a proposal to reduce the amount of modules included in Astana library because is so big that the burden over the maintainer is not manageable and more some models are maintained for years because no one has knowledge about them there are not enough people willing to maintain the standard library to maintain it at its current site so sadly in the next years it's going to shrink but still it has some very cool Eden gems that I want to share with you the first one that I want
to show review is how to report crashes from your production applications or like any kind of application actually this is something that icons are very important for any kind of application want to know that there is an issue because you get notified by your monitoring system not because your user come and complain to you so you want to be aware of any kind of issue before it can impact an user or before it impacts them enough that they start complaining and being reported of crashes it's one of the best way that you can know there is a bug in your code because as much as you can test your application as much you could have a quality assurance process the users will only discover bugs because the complexity of the world out there is much bigger than anything we can replicate in a constrained environment like our testing systems and things like that so this is an example of what I want to achieve which is I every time the application crashes it sends me an email with the trash traceback and the error and this can be achieved actually in a very simple manner to the logging model in Python I guess that most of you who have used the logging module at least once in your life but I think that not many people know that the logging module is actually very flexible it can be configured in
many ways the logging model is structured in a way that you have what's called and gloves which are in charge of doing something when you submit a message to the logging system and one of the things that they can do is actually send forwarded message to you through an SMTP server so you can get your log by email whenever the login system outputs a new log and that's of course not very convenient for logging yourself you don't want to reach thousands of email every time you write something on the logging but if used properly it can be a very good way to provide ins about some specific messages and in this case what
I'm going to do is decorate a function usually the main of your software or the main of your whiskey application in case of web applications with a decorator that every time there is an exceptions it calls the exception logging out there on the logger that we just configured if you'll notice in the video space the crash trash logger is configured to send every entry by email and here we tell the logger to record a new exception the the conveniences that the exception helper actually does not only record the message which is going to be used as the subject of the email but it also records the trace back and line where we crashed and things like that so if this is used
properly you are going to receive my
mail every single hour that your application faces so that you know before you usual start to complain and these are requires like two lines of code you just decorate the entry point of your application or the most important functions and you'll get notified it works especially well in the context of web development because you have a single up to entry point in the wall application that it's called by the Whiskey server every time it has to serve a request so every time the request stretches you get notified for that specific request
another cool feature that's available in the Python library very few people know about is that you have a way to implement literal objects if you have a word with other dynamic languages like probably JavaScript because it's the most widespread one you probably got used to having a literal object and be able to just throw around objects are located at any point in your code with any kind of properties and not having to declare classes or proper structures and things like that that's not the way best way to organize your code but it can be very convenient when experimenting or when writing tests that have to simulate the behavior of other objects and you don't want to end up using real logs or things like that when our acting around in general it can be convenient to be able to declare objects that don't require a proper class hierarchy to be created and usually in Python you frequently end up using dictionaries for this purpose because you can put anything you want in the dictionary but if you have to code called third parties code it's going probably to accept some kind of dictionary some kind of object and not a plain dictionary as the argument so the problem with dictionaries is that they do not support the dot notation for the assess to their content and that's the reason why in this and the library we have something like the namespaces where you can actually set any property you like they act like a dictionary nothing different but the major difference from this analysis that you can access all the properties that are inside a
namespace we've the dotted notation so in practice you can create a namespace everywhere and pass them around and as we are talking about a fully dynamic language there will be accepted by all the function that you need to call as far as they have the properties that they expect so you reach nearly the same
level of little objects that you can achieve in other just like JavaScript and so on and another very cool feature I want to talk you about is that the fact that Python provides the full temporary files this is a kind of temporary file that not frequently used in code but it can be very convenient for some reasons I think that all of you had the least once in their life they need to store some data in memory in the form of a file or read it or something like that and frequently we end up using the bytes on your as the way to store this data to keep this data round the problem with bytes a yo is that it grows as far as your memory allows it so in Kansan context where you have constrained resources so where we have multiple processes running for the same resources this can be a problem because like let's say that you are storing in a byte say or a fight that was uploaded by a user to your web application if the users applauds a 20 gigabyte file and it's able to actually send it to you you are going to consume 20 gigabytes of memory and that's going to be a problem if your system doesn't have 20 gigabytes of memory or if your system has other processes that compete for the same memory and the solution to this problem is usually to ally on on disk file like temporary files most web frameworks end up storing when you submit a file they actually store it as a temporary file on disk somewhere and they return you in assets to death temporary file and this is cool because it solves the memory consumption problem the disk is really far bigger than the memory you are available so it shouldn't be a major problem but has the problem that it has a major performance impact the disk is far slower than your memory and in some cases you can be also trottle by the maximum number of our operation every second you can do and things like that like especially if you are on she'll find system like elastic phone system or things like that there you have a maximum number of i/o operations that you can do across the world cluster so if you have many nodes that are writing and reading on the same file system you're probably going to eat there are your limit before you eat the bandwidth limit the solution to this problem is actually available in the
standard library and prevents you from facing these kind of problems that this
is actually real logs from an application that has been continuously killed a web application you see Apache is usually the process that got picked by the room killer just because the user was applauding very big files in the intent to bring down the system on purpose and it was such a scenic at
doing that but if you use the spool time profile you are going to get the best of the both worlds what's going to happen is that as far as the file is smaller than the specified match side Python is going to work on it in memory so it's going to practice to use something like
bytes are you everything happens in memory is very fast you can work it on nearly real-time and things like that but if you file scrolls for the result inside it gets swapped to disk so you don't have to care about your users exhausting or your memory or things like that and this is probably the best solution to that kind of problem because for realistic sizes for those that will be 90% of your requests you are going to process them quickly memory and for big requests that might be malicious or might be an exception you are going just be a bit slower because you offload them to the DS but you're not going to crash and more another interesting problem that it's frequently in some kind of application is proper output alignment and display if you ever brought the terminal application you probably face the case where we see a few hours to provide the out with you you want to provide a proof of concept of your web application and you spend the rest of the day is trying to format it in a way that works in a reasonable way on all sides and kinds of terminals however you users might have
decided to recite their window and things like that that's a very hard problem that not frequently many software out there that has been around for years are not very good at solving and for to show you this case I picked a very common example which is a progress bar progress bar very widespread they are very recognized and known way to show to you users the progress of some kind of operation but they are not a very good example for this case because if you don't format properly a progress bar your user is not going to understand anything it would be just a huge mess of things on each screen and will lose any ability to provide the information that
you want for example this is the progress bar from real software existing out there and you can see that at a certain point it just gets a huge mess of truncated data and one point it goes on a new line it's really hard to understand what's going on is it progressing at all at which point my right now and things like that so this is why you want to make sure that your software is properly able to adapt to the screen size of your users and the
standard library actually has a way to do that and I use that utility to
implement a progress but that adapts properly to the sides of the screen and here you can see the example it's very simple to use you just decorate the function that has to report the progress and yield any progress advancement from that function so this function can compute whatever it has to compute write the result whenever it has to write it and meanwhile it can report progress by yielding values back to the decorator and we output the result is as you can see they nicely formatted progress bar the implementation is pretty straightforward it's just a function that loops in continuously consumes data from your decorated function your decorated function there is the it's called to retrieve the generator you see Jam equal func and then it just goes on calling forever next over your generator so the implementation is not the interesting part here the interesting part here is that in the Shu TL method and su Shi module sorry you have a way to grab the where is it I can't find it anymore here sorry at the top you have a way to grab the current terminal side so you are able to adapt the output of your function to the current sides of the terminal window and this will take care for you of most of the complexity because it's not something very simple it depends on the terminal configuration kind of system you are on on Windows it's different than on UNIX systems and things like that so it's not a very straightforward information to achieve but the standard library takes care of making sure you get back something meaningful for you and even better if I move these information within the while loop it will also adapt if the user resizes on real time the windows so I don't have to care about it let's see the next feature
available in the standard library how to survive an atomic sellout not really actually it's how to survive atomic right so in many web applications
there is a need to replace the content of say file of some data that you are storing the misc suppose like an upload an image or something like that only if you successfully brought it for real if you completed writing if your process gets killed in the middle of the riot if you process crashes in the middle of the right of
things like that we shouldn't I have not corrupted the file we were replacing this is actually a very common problem for nearly all applications that have to store data like think of your database system would you be happy if you were inserting data and if it crashes in the middle of insertion you corrupted the whole content of the database that's probably not something that's going to make you very happy but most of the application also around suffer from ease bug from
this issue the if you interrupt a bride that will have corrupted the data because the way their system frights the data is by writing in chunks of bytes so if interrupts in the middle of two chunks you only wrote the first part of the data and probably you end up with a file that has half of the previous content that half of the new content that makes no sense at all or it might have only half of the new content so it seals truncated invalid data and we can implement using the standard library a utility that makes you safe from this kind of issue and it will also allow us to retain the same exact behavior who are you we are used to when working with the plain open function we are just going to use safe open instead of open and then we can write a read from that file without caring about the fact that it will guarantee for us the atomic price to the file so if we use the safe open in case of our codes crash in the middle of writing it will all work back to the previous state of the file and implementation is very straight forward we are going again to rely on a very convenient model of the Pancoast on a library which is the temporary file module the temporary file module is a kind of temporal file which is the name the term five it means that you don't only get back handle that gives you access to that temporary file but give us back actual file name on disk so something on which we can call more functions based on the file name itself and why is that
important because by virtue of the fact that we are within the context manager when you open a file we actually don't open this file but we open a temporary file so we are going to write to a new file and not to the one you were targeting to so in this example you see that I opened TMP my file and what I'm going to do is that instead of opening TMP my file I'm actually going to open a temporary file somewhere on the disk then my code works on that temporary file and only if it succeeds only if it succeeds the temporary file is closed and then the file is renamed so it's moved to the place that was previously used by the file we wanted to open and this works because as far as you are on the same file system so you are not moving across two different discs or a remote file see something like that those five years you are on the same file system the rename operation will be atomic there is no way to interrupt alph way every name it's already named it or not so at the end I brought the wall content on my file or not because I was able to up and and the wall functional rename my temporary file to the target file and and so I replaced the target file with the data that I brought on the temporary file or I failed doing that and so I didn't really think my temporary file is left there and my my target file is still there as it was before doesn't even know that something was written somewhere instead if I get an exception I just throw away that Empire file and nothing was ever written my target file is still in the same access state it was before and we can see all these words with this simple example you say that in the first case I obviously just open the file and write the content and if I read it back and we see hello world because I wrote everything and closed my context manager while in the second example I opened my file I write some content and I throw an exception which will cause my code to exit from the context manager now if I point back the content of my file what do you expect to see
it's important to notice that in this example I use the standard open so I didn't use my safe open and for this reason what I'm going to see is only the first line of their content my previously written el award is thrown away and it cuts my place we might
replace the yellow world but I never add the second part of the text written on disk which is what you expect it's very straightforward to understand its content the problem is that i thrown away the previous content of the file it's lost forever but i didn't finish writing the new content that i wanted to drive while instead if we use the safe open for the same exact example if i read back the content of the file after the crash of my codebase I won't read replace the award but I will actually read l-word nothing was ever written to my file by virtue of out there safe open context manager works so I didn't corrupt my previous data in any way I didn't write anything and everything is as it was before it's like having the transaction system for files if you fail you can roll back the content of the file to the previous state and be happy one more feature that's available in
astana library and very few people know about is the multi processing manager some people probably used it in the past too and ended up relying on it to share data across multiple processes it's very common it's the reason why it was born it's a it's a way to share data across processes you can store any value inside the manager and that value will be available to all the processors the chef the same manager so you can write multi processing software without having to manually share the data across the processes any data that you store in the manager will be available readable from all the processes that are children of the process that for the manager and they can replace the daytime things like that it's like having a database where you can store the data you want to share across all your system by the interesting feature is that very few people know how the manager actually works in the way it works is that it actually Ford's a sub process of your process and that's a process is a fully functioning memory database system the listings for requests through TCP so the point is that from any process on any machine you would be able to connect that process to the manager and store data there you don't even need to be a child of the process that for the manager as far as you know which port and address is listening you can write a read data to that manager so what we actually have is a fully functional database system without having to start or manage any database system for a very simple project this can become a convenient way to store the data and permit houses across different processors from different machines and it's easy to see that if you pay out the manager with the shelf module you actually implemented the file differential or database system because you shelve on disk all the data that it's in the manager and you can restore it from disk the next time the manager start so let's see how it works in this case I'm going for convenience to just rely on the fact that I formed the manager so that I don't have to show you what the machinery involved in to connecting to a remote manager because the example is already pretty long like these and
didn't fit in a singles life so let's suppose we have a first process that right that's at 42 as a value in the manager and the second process that set a dict as the second body in the manager and the last process the sets day time as the value in the manager those three variables first second and last will be available to a fourth process that is able to read their content and print them as far as that process knows about the manager so as far as they were forked all from the same parent they can read and write to the same manager and they try it's available to all the processes that are sharing that same manager the only thing you need to make sure is that you don't fork the manager in the middle of your code because the way it was told you makes a fork of your project so you don't want your code dynamically forking processes copying the world memory of your process including there all the data that you did store in that process because you probably know that forking relies on the copy-on-write poverty but you probably don't remember that Python has built in a reference counting so anytime you assess an object you are actually going to break the copy of right because you have to increment the reference counting in the object so forking and copying writing Python they're not very efficient as you would expect and here there is the example that I told you where you can actually assess the namespace from any machine on the system as far as they are connected to a network and you can see that I can create a manager and tell the manager that it's going to listen to the part 50,000 I'm specified just because that way I know which part is listening to otherwise it's something dynamic that I will never know and then I tell to the manager to self connections forever so I just start a process that stays there blocked and listen for requests and goes on and on serving any requests that he receives and from a second process I make another manager that instead of listening for requests connect to the other manager so I can talk to the second manager to connect to the other manager and the interesting part is that if I set a value in the first manager like in this case I set five in value the other process it's perfectly able to print that value even do that value was totally different machine doesn't even have to be on the same machine because we are relying on the network connections you just need to pay attention to the fact that the models if you store complex objects like classes or things like that those modules need to be available on both systems because otherwise who will face peeking issues of course but that's the only real real thing you need to care about as far as you have the same environment on both machines you are able to send values back and forth across the one network without having to care too much one more really convenient feature that I found think that many of you had faced at
least once in your life is that if you have to write our HTML output maybe a web page maybe send an email you know that you need to escape anything that you place in that email and that the user provided to you because otherwise if the user provided malicious content like JavaScript or similar you are going to send to the browser of your users that JavaScript Aziz and it would be executed and it can be a very powerful factor of attacks this is still a very common issue in web applications like 90% of the security reports that you can find for website hotel I usually through this vector which means that this is something we developers are not very good at handling and the most common way to endure this problem is by doing manual escapes on every single page where you inject a value in the output and that's not something that works because it's got boring and tedious and humans are not very good at doing boring things they start to skipping them saying here you will never happen anything and things like that or they forget and so on so the best approach to this problem is actually having escaping by default if the software escape everything unless you explicitly don't want to escape it so you don't have to remember to escape things and in worst case you wish or something that it's broken to the user but it's not malicious in any way and the way we can achieve this is through the standard library for motto if you
know the way you have the format strings in Python has been based on the swing formatter a training that we implemented on top of this thing format but they provide the same exact feature but the thing for matter is something that we can customize so it provides the string formatting feature to a class that we can customize in this example I made an HTML format that allows me to format HTML and why is that important because every time I replace a value in my target string it actually gets escaped for me so if you see in the output the strong name was escaped so I didn't put HTML in the result I properly escaped that but the title where I explicitly stated that it's markup so I want to Tezz markup was not escape was formatted disease okay there are very cool eyebrows out there that take of these the most famous one I
think it's mark mark sub safe from the flask guys but the is something that you can easily achieve without the need of any external dependency or anywhere framework at all it's available understand a library itself just by sub classing the string formatter and the way the thing formatter works is that every time it
has to replace a value every time it has to format a value it's going to call the get filled mini table it's going to tell the girls with me today I need the value of this variable like in this case we are the name and title so every time therefore matters to resolve name and every time interest oneself title it's going to call the cap feel method and what we can do is that after we actually retrieved that value from the class from the parent class we can check if that value is a markup value and the protocol of which the markup class is base is that it has a underscore underscore HTML method so if so our class if our value is a markup based class respects the markup protocol we are going to put it as is or more than Aziz formatted as HTML so call the proton while instead if our value is not markup we are going to escape it if it's a string let's escape it that way every time we format a value it gets escaped automatically and we don't have to remember to escape it and here you can see a very simple implementation of the markup audible it just relied on having a H and under HTML meet order returns the SK the HTML representation of that object which in case of a string it's the string itself so it's very simple it's just way to tag a string as that has to be formatted Aziz instead of being escaped so you can see that in five lines of code you actually end up having the same Excel feature that you will lack by installing an external
dependency and it's a feature that can save your life in many contexts I think that most of you already know this comic
stripe but this is very fun one last
thing I want to show you actually two last things I want to show you but this is one of my favorite as I told you I'm involved in web development and I maintain my framework so it's very fun for me to talk about how to provide a web framework and it's something that we can do in five minutes through the standard library because if you look very well into the sauna library you will see that you have all the pieces that you need to write a fully functional web application that take care of the most complex part of handling the web requests for you so this is the web framework that I wrote
only relying on the standard library and it might look like something that you already used in your life it's very similar we just declare a whiskey application and then attach all the functions that we want to route that gets for us and the routes can be even complex like having regular special arguments everything like that so anytime I assess the route I will see a low ward anytime assassin Inc is returned here you click link try this other link and then get me to an end point except some argument and I can bring back the arguments and things like that okay so it practically fully found how well framework for most basic needs and at the end I just have my web application so I will list a non HTTP for me the to a root concept that I use as a convenience you don't really need to do that but it can be convenient to represent the incoming data and the outgoing data to to response and request classes that's what your probably is doing other work femurs and the response class or needs to take care of remembering what's the status that you want to take to send back and what the others that you want to send back and then provide the simple metal to send back that responds why the request is going to keep in memory or the information that God sent to us by the browser and allow us to read what was the requested path what was the requested arguments and things like that they were framework itself it's just these code nothing more nothing less so it's pretty simple the root decorator which is the one we used here to register the values end point just saves the end points you wanted to register into a list of available end points so it keeps somewhere recorded that for this URL we want to call this method nothing else of course as the end point can be a regular expression we compiled a trailer special the Sun method it's even simpler because we reply relies on some either it's ready-made in astana library the whiskey reference sample so we create a web server a whiskey server that we listen on port 8000 and every single request that gets received on that part is forwarded to self so to yourself to your web application and it's forwarded by calling the provided callable object so in this case by calling our dunder committed so every time we receive a request we get the dunder call method call and what we do is that we create the request representation we check if any of there is a register the route matches with the request path that we got and then we call it so we just called the registry endpoint we get bad the response that they had provided us and we send back the response to the browser that's all so it's very safe forward look if there
is any registered callback for the end called a callback get the result for the callback and send back the result to their browser that's all you need to implement a fully functional web framework ad will say wait sue that I
lied you will notice the proper if you start doing these in two weeks you're still writing a web frameworks and three months it will be two million lines of code things like that and in three years you will be the maintainer of one more word framework in Python so don't do that because general flask or whatever you want but it can be convenient if you only want to give you expose to three endpoints in a web application that maybe has two running constrained environment where you don't have enough memory space or a properly functional package manager to install dependencies or things like that or you just don't want to bother maintaining dependencies and things like that five lines of code
and you are apu with your own web support the last way CP i want to show you is based on code tracing this is not something that you are going to use in your code itself but it's something that can be convenient to understand other people code so whenever you use in your library or whenever you have to approach a new code base it can be very hard to understand what's going on you know nothing about the library or that code base so it takes time before you start understanding how it works at the general level of a vision about their library works and in my experience the best way to start understanding death start having that vision it's by following what goes on when you run the library so look at which lines of code get executed let's look at which arguments get passed around follow the execution of the library or of the application you are trying to understand the problem is that when you try to do these with the buggers they focus on the specific line you are executing and after a while you lose the global vision what's going on after the term dime you end up deep into their stock of function cause you don't even remember anymore where you came from you you have all the information you care about about the single current line of code you have the context of the code surrounding the line of code you wherever you are but you probably forgot where you came from so it's not very good to have a global vision of what's going on into the application as a world and a very good way to have that global vision is to
rely on tracing of the world code so this is a very example simple example that I did and I asked to the tracer to trace the execution of function and what's function function is a very simple function and just like does the sum of two numbers and as you can see I get back the printed code and I get a little class near toward the lines that were executed so you can see that in this execution I didn't enter this if and so I didn't print this this message I just got a 1b to return a plus B and so I understood what's going on why didn't print that message because they never passed the deef and things like that of course in this very simple example it's obvious understand but it's
already a good way to understand what your code bed contains and it can surprise you like these food that contains human flesh I'm not sure I'm going to try how is that all implemented
it's based on the trace model so inside Python itself inside the Sunna library itself you have the trace model which allows you to trace the execution of
code base and what we do it just every time the tracer provides us informations we record that information somewhere and then to the print trace execution we print the code base with the information of which lines got executed so it's very straightforward to understand you just record which line where trace and then print lines back so you can see all modules that got executed in which order which lines were executed and try to understand which is the full flow of a function call that you did that was the
last recipe that I wanted to share with you if you want to see more recipes of
course go read the book and thank you for your attention