Daniel Arbuckle's Mastering Python
eBook - ePub

Daniel Arbuckle's Mastering Python

Daniel Arbuckle

Share book
  1. 274 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Daniel Arbuckle's Mastering Python

Daniel Arbuckle

Book details
Book preview
Table of contents
Citations

About This Book

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.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Daniel Arbuckle's Mastering Python an online PDF/ePUB?
Yes, you can access Daniel Arbuckle's Mastering Python by Daniel Arbuckle in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Python. We have over one million books available in our catalogue for you to explore.

Information

Year
2017
ISBN
9781787284401
Edition
1

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 of contents