Clean code in Python

Video thumbnail (Frame 0) Video thumbnail (Frame 1101) Video thumbnail (Frame 2760) Video thumbnail (Frame 6321) Video thumbnail (Frame 6857) Video thumbnail (Frame 8545) Video thumbnail (Frame 10848) Video thumbnail (Frame 11470) Video thumbnail (Frame 12584) Video thumbnail (Frame 14533) Video thumbnail (Frame 15697) Video thumbnail (Frame 17113) Video thumbnail (Frame 17927) Video thumbnail (Frame 18755) Video thumbnail (Frame 19438) Video thumbnail (Frame 20166) Video thumbnail (Frame 20795) Video thumbnail (Frame 21984) Video thumbnail (Frame 22822) Video thumbnail (Frame 23524) Video thumbnail (Frame 26944) Video thumbnail (Frame 27661) Video thumbnail (Frame 29470) Video thumbnail (Frame 31363)
Video in TIB AV-Portal: Clean code in Python

Formal Metadata

Clean code in Python
Title of Series
Part Number
Number of Parts
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
Mariano Anaya - Clean code in Python Introduction to the clean code principles applied to Python code. Let's honor the readable nature of the Python syntax so anyone can maintain our code: "readability counts". This talk introduces general concepts of code quality and how they apply for Python. We analyse technical debt, refactoring, and unit testing in the context of a project striving for a better code base. ----- Introduction to the clean code principles tailored for Python projects. The goal is to achieve better code quality and a more maintainable code base. Python has a nature of being clear, and easy to follow, so let's take advantage of it in our own code, in order to enforce the principle "readability counts" by writing pythonic code. This talk introduces general concepts of code quality for Python developers, analyzing technical debt, with examples on how to achieve a more legible, maintainable and clean code base, by refactoring, writing unit tests and having good coding guidelines for the project. If you are giving your first steps with Python, you will gain insight on best practices for writing good software from the start. If you are a experienced developer, the ideas should work as food for thought, helping with recommendations for code reviews, best practices, etc.
Architecture Email Systementwurf Open source Software developer Lecture/Conference View (database) Software developer Software Moment <Mathematik> Coma Berenices Formal language
Functional (mathematics) Implementation Open source Code Multiplication sign Direction (geometry) Set (mathematics) Water vapor Mass Machine code Theory Formal language Mathematics Cross-correlation Software Data structure Endliche Modelltheorie Extension (kinesiology) Error message Dialect Focus (optics) Military base Software developer Projective plane Physical law Code Coroutine Software maintenance Formal language Arithmetic mean Computer animation Software Logic Statement (computer science) Video game Collision Reading (process)
Functional (mathematics) Execution unit Parallel port Line (geometry) Parameter (computer programming) System call Number Flow separation Computer animation Order (biology) Logic Authorization Statement (computer science) Self-organization Endliche Modelltheorie Sinc function
Functional (mathematics) Code Multiplication sign Set (mathematics) Mereology Element (mathematics) Number Mathematics Structured programming Ontology Logic Software framework Data structure Abstraction Forcing (mathematics) Projective plane Interior (topology) Code Line (geometry) Price index Performance appraisal Mathematics Uniform resource locator Computer animation Function (mathematics) Quicksort Library (computing)
Database Cursor (computers) Mereology Database transaction Graph coloring Order (biology) Software framework Data structure Error message Task (computing) Exception handling Block (periodic table) Constructor (object-oriented programming) Line (geometry) Software maintenance Sequence System call Subject indexing Uniform resource locator Word Computer animation Logic Linearization Moving average Table (information) Exception handling Arithmetic progression Fundamental theorem of algebra Row (database)
Functional (mathematics) Interior (topology) Set (mathematics) Sound effect Cursor (computers) Database Database transaction Sequence Flow separation Order (biology) Mathematics Computer animation Logic Complex number Universe (mathematics) Exception handling Error message Task (computing)
Point (geometry) Context awareness Implementation Functional (mathematics) Matching (graph theory) Planning Mereology Computer animation Logic Different (Kate Ryan album) Operator (mathematics) Core dump Logic Social class Key (cryptography) Quicksort Implementation Abstraction Task (computing)
Point (geometry) Area Implementation Point (geometry) Keyboard shortcut Number Digital photography Computer animation Order (biology) Logic Social class Key (cryptography) Implementation
Point (geometry) Implementation Point (geometry) Data storage device Attribute grammar Element (mathematics) Connected space Neuroinformatik Product (business) Writing Category of being Array data structure Computer animation Strategy game Personal digital assistant Object (grammar) Social class Software testing Key (cryptography) Object (grammar) Category of being Social class
Purchasing Bit Line (geometry) Counting System call Product (business) Product (business) Computer animation Term (mathematics) Statement (computer science) Abelian category Category of being Family
Computer animation Interface (computing) Social class Parameter (computer programming) Category of being Abelian category Family Element (mathematics) Product (business) Social class
Server (computing) Functional (mathematics) Context awareness Service (economics) Ferry Corsten State of matter Complete metric space Causality Core dump Series (mathematics) Endliche Modelltheorie Task (computing) Exception handling Context awareness Inheritance (object-oriented programming) Block (periodic table) Weight Interface (computing) State of matter Preconditioner Data management Process (computing) Computer animation Personal digital assistant Function (mathematics) Statement (computer science) Social class Object (grammar) Family Task (computing)
Axiom of choice Complex (psychology) Functional (mathematics) Multiplication sign Execution unit Streaming media Mereology Disk read-and-write head Event horizon Formal language Word Different (Kate Ryan album) Logic Software testing Data structure Implementation Context awareness Execution unit Standard deviation Touchscreen Kolmogorov complexity Projective plane Code Planning Mereology Unit testing Variable (mathematics) P (complexity) System call Computer animation Integrated development environment Function (mathematics) String (computer science) Pauli exclusion principle Software testing Right angle Object (grammar) Quicksort Metric system Writing
Medical imaging Computer animation Information Open source Multiplication sign Pauli exclusion principle Information Complete metric space Machine code
OK this is a modern idea he is going to take a the torque of all clean Python the let here be Corbeil's uh um the alone on the you've regarding this going going like and so provide the infrared language 1st on the armies in of sees as a software developer I'm interested in open source technology and so forth picture on the left side values and by the cost of different to contact me or each viewing of this means it are interesting talking about all of these concepts after the talk so before we started in with
definitions I like to of America a few comments 1st article in this life is written by 3 but there is there to be any problems no perinatally theories in an origin of life on so there were a lot of and 2nd more detail is is not a by no means something strange or as you must do must implement there are some ideas surveillance and some of them are appeal so you might think otherwise and thus virtually OK so an inverse analogous say that there's no a sole definition of going cold there and his day will find as many regions of developers and offers a little of the tallest writers what to set up the goal is the 1 in which every function and that's pretty much what you would expect and a you can believe beautiful glowing also makes it look like the language was made for the problem and the reason why I think these qualities because precisely because of that last statement all that's what we call by funding of gold that is a matter phone and we see examples of that code actually them so tool how common
ground of understanding we can say that can go focus which means that does 1 thing well I am that thing that is doing is should be pretty much what you'd expect from a collision will not be misleading or error-prone or are confusing so should be clear and is important for many reasons because I really the quality of the coal determining the quality of the software you know there's a strong correlation between a poor goal based on a software house out of error science have to maintain Russell policy is also true if you gonna have maintain your gold a clean readable and understandable it would be much less likely that errors and problems in the gold and there are there will be easier to to spot so really the guns and we know from a set of Python and it makes total sense because if you think about it as developers will spend much more time reading called that actually writing code whenever we want to make a change or new feature we 1st have to realize surroundings of the cold war going to modify and on the extent to which you vary the collection so is doing is what ultimately will and attorney only find helpful she changes in the gold or new features so it's related to what a development we all know that we use mass is lost a dominant prices from should new functionality faster and also mentioned that because that blueprint that in our model that you have a way to to represent the business logic and the water is always try to so it has to be re Delisle is useful on the other hand we have some of scenarios where exactly the opposite to what we want for example complex obfuscated called the the what there has been directions duplicated goal like the worst thing we can kind Russian and go that is not in danger revealing and they still aren't reading their business logic already sees our requirements exports implementation details we should should be encapsulated or structure and this also is solved by adopting a law that there are many ways of thinking that but having up Oracle bases are only 1 of the worst and my things Source article that also invisible and is not only in the idea for the project raised something that is sometimes hard to spot or identified so let's try to see some examples of these are ways of life and and they were
1st example is something really simple which is big and all the goal is considered these functions that year should read 1 line per are they of the year by using that it works and you have the authority of the exact LOO but noticeable that our model 6 months a year and absence since of 1st you for I I of
our trying to figure out ways trying to achieve we're trying to when you see by trying to all of the some calculations and see the year divide it utilize some numbers and you can actually spotlights entity but you find that the that the reason is meant to say an extra they they say OK maybe that's trying to figure out the 3 years people not Lester Brown and if only had this approach should be getting their culture actually tell you what it's strategy so if I had to no if want to know if the years not arrive how function that's it is the unit here so I can read a call and I can actually replace that earnings statement was something meaningful in the goal of this are a simple yet powerful thing you can do in order to increase the availability of gold our because he's not allowed by reusing call is also breaking concerns the officiating different problems into the fray layers I'm having parallelization in the brush parameter functions of the 1st nite of organization in approaches functions 1 1 thing only and do it well and study from the very simple example is a
unsaturated College cation because if you think about it most of the times gold a duplicated because the have progressed structure phrase so you might say that's a validation in and some part of the goal and we say OK I need a lot of semen evaluations so someone might say OK that's copy this line from here to here based here this change this number 2 1 lead to a whole set but actually not right because it maybe you introduce absent obligation In the goal inadvertently there is 1 they have because the company the whole program structure and we all know what we want to avoid code duplication at all costs because then you get a cold forces you to peddle changes you got to change things in many many places in the cell for at the same time and you've read 1 of those I of our there's a problem so instead we will want to location and you can remember the dry principle and for these which is stands for don't repeat yourself and things could be defined must be defined once and only once in the project narrative and efficient in the work so this will be the sort after our principle and suffered element over the past few years and never got have been made enhancement priority for example libraries frameworks tools designed others and there's a great and especially and the published 9 and double values is also collects subtle tool when it comes to buy from which are decorated I explaining
all the details about the greatest because this is to extend the topic and it is worthwhile to open itself for alienation what's relevant for the purpose of these ontologies addressing college indication that had the general idea is that we can have some functionality obstructed in 1 place but repeated or reused many many times this you with there that cities with an example so it's a high
maintenance the delays a task that is is part of framework colonies on behind it is not they got is made up of a sequence of commands and is the 1 to be executed so the 1st task is to of the index of the tire examples risk so I have a sequence which is uh cost of 1 underlessee follows like this I you very command sequence with a which is provided is is an error or something went wrong I know he said here at the minus 1 otherwise I know that they that worked fine readers so far so good this assumes that some the dividing goes up the framework requires by then no requirement that arises as is
that I need to move from the ductile uh from some records from in Table 2 and Table 2 on the use of the most recent are also table so I say OK I can bloody tools commands to see the same it's 1 for inserting the rolls into the article on domestic along for deleting that the roles fundamental but then as part of trying the framework and the last thing I had to preserve the exact same logic so I can do something like OK for every command in sequence places you did with courses just provided if an exception occurs I love the exception retirement as 1 or wise finally the worked fine readers and actually also works when was to broaden agrees that I had was so the logic but but this exactly the same colors before there is something from the try except block is the exact same license before and the reason why they might happen is because those lines that words are in charge of linear handling along in the progress structure location remains so is similar is kind of similar to the very 1st example with leaders there was some quality colony construction so it was actually other Europe wrong to adjudicate call this even as result with great so it
is I think with the new function article the status 100 it is going to be integrated so it assumes he receives the function which is the 1 that is going to be the greater I inside defines a new function under controlled the logic for or doing you have basic Hilderic along a sequence again effects at University of these sets of character minus 1 otherwise they looked at that task completed well urgency this assumes that the function that's been decorated relies a sequence of commands so there's an interesting thing here offers felony for that is no longer in a musical note no the status of the and is in charge of 1 thing which is the handling and executing the so now to functions can be changed
to use why the so I can remove all that duplicated logic and steadily returned a sequence of commands which is the real part but there's still preserving the same logic because being degraded by the status from the top on the function should actually these 3 things with this change 1st we assign a name the the reason I must go now it's called the status and the now there is a separation of concerns of logic remember that there greater now only handles data commands and the longing and knows nothing about the commands itself whereas these a function for the opposite behavior is a function only return sequence of relevant commands and that you know nothing about the error last a 3rd were removed to make it a goal and have defined only once so it was a cool and way use the greatest 1 to toward reciprocation so he
somehow related to an not of which is really a she implementation details they did not have to run like a task of part of your core functionality we also have to do some more things are related from to know which you cannot avoid you and escape from India is similar to the 1 before we do not want to make some sort of it you have different things in same problem with the 1 and the same logic are separated into smaller so let's see what I have to to offer for each scenario because
0 sorry for a simple example as a whole and what obligations and I'm engage on with players like playing online and I have a requirement that says that when a player features again I need to update the score with the new points that just and for the match is just the so liaison arguing operators that assumption I call that accumulate points with new points that I have to search for plane I I think this warrants but if you take a closer look you think that in indeed is this is
not very good because mixing up implementation details with this is thought what I want to do is to all new points for even player by instead hardly will erase collection all always he 0 which since defeats so we had a full value need to share their photos were actually want to do which is to add a new score for the player but then again our and our implementation not technical details of how it is said some Eastern I will wear led to is another
previews accumulate on the boat might suggest point like if it were in your area Python viable because it's easier to follow and understand so to the the point of a player and you dine area just like points the same for the set so that use accumulate points there something particular about it just trust 2009 20 number a signal within Europe really Python bindings for us so in order to do that we can use the
property the greater which is a beauty the great Python and this and you find points to be met of the class and there would come on I encapsulated strategy implementation details solvent worries scholarly doesn't know what's behind it and that's a good thing to have no knows all the points on which of the 2 methods were 2 properties there are smaller in size and easier to and understand so you can
use the property greater to use some computations based on our object at you use I should refer to this approach is still writing like custom get said that because the goal would be easier to read follow and also the test because now you can guess you call it anything that fast and like a point as you you know how to have an like arrays correction for your taste or what the connection Apache etc. so it's easier to understand and follow this much more by now let's suppose I have like a will obligations in this case there is an online store that has stop representing other products that are used in the so there's divine categories I'm afraid of you in this 1 that
says like request profit customer that is going to handle this scenario where some customers trying to make a purchase of a product on the they go up with quality refined by this is a bit hard to read pretty and these lines are not very expressive but you what the try to read those lines and find a clue nearly a statement when he says that is probably it and stuff they say OK maybe debris assigned were actually trying to figure out that again I'm getting around call so say if I want to figure out if the brother of the civilians dull I'm a writer just seem to ride a bike you
brought England's stuff but actually makes perfect sense is to speaking in terms of the main problems and self-documenting there's nothing in the common doesn't need an explanation and always wore these recalled where you write something like that if in current stock but
entirely translated as by calling that contains many of the so-called matching method because of the double underscore busses of brothers of parameters so there's a slight OK now we know that we can implement it contains many of into
his that's a have an interface they wanna had before we to optimize unsolicited income encapsulated away in the class but it actually makes sense to and our case for
for a managing state or handling the scenario is 1 without ago that began of fun and core functionality approach it by also required to do certain tasks for example you might have a goal that has preconditions postconditions revolves for example operating a server who want to make sure of the process and the data you but it's actually making sure that base closing the or reason the resources it allocates so there is a growing here that we might uh again fear the risk of actually trying to make some of those things and instead they should be 2 in the house separated into the fray policy if you can let our conscious mind shared which they are based on reasonable there's a whole lot of of run an offline made but so my lack of requires that at this because services stop before running the around that they're all flying back up and of course I want to make sure mean innovative services operating again so is illustrated with his best all the series and started service inside flying without which doesn't belong to any making the called much more couple or more properly and so dumb separated out into a handler which is going to be Goldman contigs manager like so when I write like we and an object that implemented contigs my brother work by constantly translates that cause the N-terminal ultimately which in this case is doubly tallies series that follows the weights statement blocks and then I go around the core functionality were actually want to which in this case he's running the depth and then after that statement completes on ultimately goals the exit unify exception occurred so if making things easier because I am happily handling itself or managed education or scenario 0 make sure you you've this fails or is something wrong with the data service that will be the left operand and model we can slightly improve
these by using the context the greater which he's by an interface providing the context of I was inheritance understand that interface you implement the and the and I think it's in methods and once you have that you can use that to the greater for the function so when you're in this case if I call it like these aren't going to be established under this is going to be called automatically inside the context manager accordingly introduction so from all of these were
gonna draw the conclusion that there is always a much more by finding ways to write things and events were directly funny goal is to actually take advantage of the features of the language I would that you're cheese so if you you're a viable so your objects a plane well with Python Rasera variables and like 2 pieces of sheets of the matched to a so in a way that makes sense that example of stock and sometimes not achieved I lived there most common answer is much but no there are many many automatic methods or features of the language that such chance of screen doors etc. so if you're starting and you're being Python are encourage you to try to find these choice in Python unit to transform you go into much expressly 1 major experience syllabary my useful uses examples of ideas not while the like in a according you wanna pull request for the civic always on so the Republicans say other ways like the right by funny goal is to actually take advantage of the language of the future and sometimes and means they're using the greater at removing duplication 2nd that's my this and a
psychological some standards for the right the call and can be understood by a team of it's in the debate on going I spend all you can be 100 % with unless you know how find because they're actually looking for different things although it's the way to even mind are tractable docstrings streams of functional data you want to keep your ears about what to expect at any function in the cold on everything here for the so called practical also apply for disease also maintain your unit tests are clean and we use structure so that useful and also easily used at test time T because will naturally follow these logical actually trying to find more pieces because you will want to widen your coat testable and have finally some tools that we can use to provide metrics for all the goal without like the sort of finding the role the run this your project I will give you metrics such as cyclomatic complexity might intermediate dates etc. have even uses head start always cold needs more improvements are urgently I finally got a which works with the British tool but also lets you define your own standards to run or reject ultimately as part of a duration environment an
example interested some of the topics you can have more information in some of these sources a user support for the top are that will be all that is
defining questions where we are the answer thing URI image it time the the of questions any of the new 1 no yeah so movements and the beginning of it