GNS3 Talks: Python for Network Engineers with GNS3 (Part 7) - How to write better code and styles
David Bombal
Udemy: Get the course for $10 here: https://goo.gl/QYC988 GNS3 Academy: Get the course for $10 here: https://goo.gl/vnZJhg More free Python videos here: https://www.youtube.com/playlist?list=PLhfrWIlLOoKPn7T9FtvbOWX8GxgsFFNwn
Learn Python programming with GNS3. In this series of videos I will show you how you can quickly and easily program Cisco networks using Python.
In this video we discuss how to write better code with the Python Style guide: https://www.python.org/dev/peps/pep-0008/
Transcription:
This is one of multiple videos teaching you network programmability using Python and GNS3. This is a very useful document to breed PEP 8 - Style Guide for Python Code. A lot of people get hung up about how the way code is written and you'll often find debates and people flaming one another when code isn't written as neatly or as well as some people think it should be. Now again using the idea of get started and learn, you can always improve the readability of your code and improve as you go along. But this is a document worth reading to try and write better code.
So one of the key insights taught here is that code is: read much more often than it is written so the idea of these guidelines is to improve the readability of code and make it consistent. So they say here style guide is about consistency, consistency with a style guide is important but consistency within a project is more important. Consistency within one module or function is the most important.
If you don't know what a module is or a function, don't worry we'll talk about them later. But notice there are always exceptions. Know when to be inconsistent. Sometimes Style Guide recommendations are just not applicable. When in doubt, use your best judgment. Have a look at other examples and decide what looks best. Don't hesitate to ask. They give you some good reasons to ignore a particular guideline. So as an example ignore a guideline if your code is less readable.
You may have to ignore the style guide to be consistent with old code. But you have some recommendations. Indentation is very important in Python so use 4 spaces per indentation level. They give you some recommendations for continuation lines. So as an example, this is good, but this is not good. There’s a section about tabs or spaces. Spaces are preferred, tabs should only be used to remain consistent with code that is already indented with tabs.
Python 3 disallows the mixing of tabs and spaces for indentation. Python 2 code indented with a mixture of tabs and spaces should be converted to spaces exclusively. You can use the -t option with Python 2 to issue warnings about code that illegally mixes tabs and spaces.
You can use the -tt option to make those warnings errors. These options are highly recommended. ... https://www.youtube.com/watch?v=60QB6z8ZZGo
34066094 Bytes