Daniel Arbuckle's Mastering Python
eBook - ePub

Daniel Arbuckle's Mastering Python

Daniel Arbuckle

Buch teilen
  1. 274 Seiten
  2. English
  3. ePUB (handyfreundlich)
  4. Über iOS und Android verfügbar
eBook - ePub

Daniel Arbuckle's Mastering Python

Daniel Arbuckle

Angaben zum Buch
Buchvorschau
Inhaltsverzeichnis
Quellenangaben

Über dieses Buch

Gain a thorough understanding of operating in a Python development environment, and some of the most important advanced topics with Daniel Arbuckle. This dynamic, concise book is full of real-world solutions for Python 3.6 problems, and advanced-level concepts such as reactive programming, microservices, ctypes and Cython.About This Book• Covers the latest and advanced concepts of Python such as parallel processing with Python 3.6• Explore the Python language from its basic installation and setup to concepts such as reactive programming and microservices• Get introduced to the mechanism for rewriting code in a compiled language along with ctypes and Cython toolsWho This Book Is ForIf you are a programmer and are familiar with the basics of Python, and you want to broaden your knowledge base to develop projects better and faster, this book is for you. Even if you are not familiar with Python, Daniel Arbuckle's Mastering Python starts with the basics and takes you on a journey to become an expert in the technology.What You Will Learn• Get to grips with the basics of operating in a Python development environment• Build Python packages to efficiently create reusable code• Become proficient at creating tools and utility programs in Python• Use the Git version control system to protect your development environment from unwanted changes• Harness the power of Python to automate other software• Distribute computational tasks across multiple processors• Handle high I/O loads with asynchronous I/O to get a smoother performance• Take advantage of Python's metaprogramming and programmable syntax features• Get acquainted with the concepts behind reactive programming and RxPyIn DetailDaniel Arbuckle's Mastering Python covers the basics of operating in a Python development environment, before moving on to more advanced topics. Daniel presents you with real-world solutions to Python 3.6 and advanced-level concepts, such as reactive programming, microservices, ctypes, and Cython tools.You don't need to be familiar with the Python language to use this book, as Daniel starts with a Python primer. Throughout, Daniel highlights the major aspects of managing your Python development environment, shows you how to handle parallel computation, and helps you to master asynchronous I/O with Python 3.6 to improve performance. Finally, Daniel will teach you the secrets of metaprogramming and unit testing in Python, helping you acquire the perfect skillset to be a Python expert.Daniel will get you up to speed on everything from basic programming practices to high-end tools and techniques, things that will help set you apart as a successful Python programmer.Style and ApproachDaniel Arbuckle's Mastering Python covers basic to advanced-level concepts in computer science. If you are a beginner, then Daniel will help you get started. If you are experienced, he will expand your knowledge base.

Häufig gestellte Fragen

Wie kann ich mein Abo kündigen?
Gehe einfach zum Kontobereich in den Einstellungen und klicke auf „Abo kündigen“ – ganz einfach. Nachdem du gekündigt hast, bleibt deine Mitgliedschaft für den verbleibenden Abozeitraum, den du bereits bezahlt hast, aktiv. Mehr Informationen hier.
(Wie) Kann ich Bücher herunterladen?
Derzeit stehen all unsere auf Mobilgeräte reagierenden ePub-Bücher zum Download über die App zur Verfügung. Die meisten unserer PDFs stehen ebenfalls zum Download bereit; wir arbeiten daran, auch die übrigen PDFs zum Download anzubieten, bei denen dies aktuell noch nicht möglich ist. Weitere Informationen hier.
Welcher Unterschied besteht bei den Preisen zwischen den Aboplänen?
Mit beiden Aboplänen erhältst du vollen Zugang zur Bibliothek und allen Funktionen von Perlego. Die einzigen Unterschiede bestehen im Preis und dem Abozeitraum: Mit dem Jahresabo sparst du auf 12 Monate gerechnet im Vergleich zum Monatsabo rund 30 %.
Was ist Perlego?
Wir sind ein Online-Abodienst für Lehrbücher, bei dem du für weniger als den Preis eines einzelnen Buches pro Monat Zugang zu einer ganzen Online-Bibliothek erhältst. Mit über 1 Million Büchern zu über 1.000 verschiedenen Themen haben wir bestimmt alles, was du brauchst! Weitere Informationen hier.
Unterstützt Perlego Text-zu-Sprache?
Achte auf das Symbol zum Vorlesen in deinem nächsten Buch, um zu sehen, ob du es dir auch anhören kannst. Bei diesem Tool wird dir Text laut vorgelesen, wobei der Text beim Vorlesen auch grafisch hervorgehoben wird. Du kannst das Vorlesen jederzeit anhalten, beschleunigen und verlangsamen. Weitere Informationen hier.
Ist Daniel Arbuckle's Mastering Python als Online-PDF/ePub verfügbar?
Ja, du hast Zugang zu Daniel Arbuckle's Mastering Python von Daniel Arbuckle im PDF- und/oder ePub-Format sowie zu anderen beliebten Büchern aus Computer Science & Programming in Python. Aus unserem Katalog stehen dir über 1 Million Bücher zur Verfügung.

Information

Jahr
2017
ISBN
9781787284401

Basic Best Practices

In the previous chapter, we saw how to put together a Python package of code and data. In this chapter, we're going to look at some rather simple things we can do that will make our lives as Python programmers simpler overall. We'll switch gears and look at version control, which will help us to collaborate with other programmers and serve as an undo buffer for the whole lifetime of a project. We're going to look at Python's built-in virtual environment tool, venv, which allows us to keep our programs and dependencies separate from each other and the software installed on our overall system.
You'll learn how to structure our docstrings for maximum utility, how to add Rich Text formatting to them, and how to export them into hyperlinked HTML documentation for viewing in a web browser. You'll also see one more cool advantage we can get from docstrings by actually executing the examples we include in our documentation and making sure they agree with what the code actually does.
In this chapter, we'll cover the following topics:
  • PEP 8 and writing readable code
  • Using version control
  • Using venv to create a stable and isolated work area
  • Getting the most out of docstrings

PEP 8 and writing readable code

In this section, we'll take a quick look at how to format our code so that it'll be easy to read when we come back to it at some later date or when somebody else has to work with it. We will specifically take a look at indentation rules, the Python code style guide, and finally, the standard naming convention.
Python Enhancement Proposals or PEPs are the documents that establish standards in the Python community. Most PEPs describe new features for Python or Python's standard library, but a few of them are more nebulous. PEP 8 is one of those; it tells us what the Python community considers to be well-written, readable code.

PEP 8 — guidelines for Python code

The very first rule PEP 8 introduces is that rules/guidelines in PEP 8 should only apply when they make our code easier to read. This means we should apply PEP 8 to enhance the readability of the code and to make it less complex. For example, if we're working on a project that was already written with a different coding style (that is, it is already easy to read), we should use that project style for new code. If the PEP 8 rules somehow make the code harder to read or make it complex while writing the code, we should ignore those rules. As Guido Van Rossum, the creator of Python, has noted:
Code is read more often than it is written.
Code should always be written in a way that promotes readability.
For more information on PEP 8 rules and guidelines, you can refer to the following link:
https://www.python.org/dev/peps/pep-0008/.
To know when to ignore a particular guideline, you can follow the A Foolish Consistency is the Hobgoblin of Little Minds article at the following link:
https://www.python.org/dev/peps/pep-0008/#a-foolish-consistency-is-the-hobgoblin-of-little-minds.

Code indentation

As programmers, when we read code we look at how it's indented to tell us how the code blocks are nested. However, most other programming languages use actual symbols to tell the language parser where a block begins and ends. In coding, the same information in two different places is a violation of the basic best practices of any programming language. So, Python omits the beginning and ending block markers and uses indentation (as shown in the following code screenshot) to inform the parser as well as the programmer:
There is one problem that arises from that, though!
There are different ways of encoding indentation in a text file. These are as follows:
  • Use Space characters
  • Tab characters
  • A combination of both
The codes we're looking at in the preceding code image mixes spaces and tabs, which, in Python 2 was valid, but a terrible idea, and which, in Python 3, is a syntax error. I've configured the editor to highlight tab characters in color, so we can easily see which indentation comes from spaces and which comes from tabs, to see why mixing spaces and tabs is not good, even when it's allowed.
All we have to do is change the tab width and it will look something like the following code image:
Even though the indentation looked good in the previous code image, now it's clearly wrong. There's no ambiguity if all indentation comes from tab characters. So, using only tabs is valid, even in Python 3. However, it is the recommendation of PEP 8 and the Python community that we always use exactly four spaces to indicate one level of indentation. Any halfway decent editor can insert those spaces for us when we press the Tab key. There are several more recommendations, which we're going to go through quickly in the next sub-section.

Formatting recommendations

The code in the following screenshot demonstrates almost all the PEP 8 formatting recommendations:
I'll now walk us through the recommendations one by one:
  • PEP 8 recommends that a single line of code should not exceed a width of 79 characters
While this is consistent with displaying the code on a standard text mode interface, the primary reason for this rule in the modern world of widescreens and resizable windows is that it helps with reading. Even in contexts that have nothing to do with programming, layout designers prefer to limit line width.
  • Import statements should be placed at the top of the file, with standard library imports first, third-party imports next, and then imports from other modules within the same project
  • There should be a blank line between each group of imports
  • Classes and functions at the top level should have two blank lines separating them.
  • Methods within a class should have one blank line separating them
  • Within a function or method, blank lines should be used to indicate se...

Inhaltsverzeichnis