Daniel Arbuckle's Mastering Python
eBook - ePub

Daniel Arbuckle's Mastering Python

Daniel Arbuckle

Partager le livre
  1. 274 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
eBook - ePub

Daniel Arbuckle's Mastering Python

Daniel Arbuckle

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

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.

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Daniel Arbuckle's Mastering Python est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Daniel Arbuckle's Mastering Python par Daniel Arbuckle en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Computer Science et Programming in Python. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
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...

Table des matiĂšres