time — Clock Time — PyMOTW 3

The time module provides access to several different types of clocks, each useful for different purposes. The standard system calls like time() report the system “wall clock” time. The monotonic() clock can be used to measure elapsed time in a long-running process because it is guaranteed never to move backwards, even if the system time is changed. For performance testing, perf_counter() provides access to the clock with the highest available resolution to make short time measurements more accurate. The CPU time is available through time() , and process_time() returns the combined processor time and system time.

Read more…

This post is part of the Python Module of the Week series for Python 3. See PyMOTW.com for more articles from the series.

Thank You for 10 years of Python Module of the Week!

This year marks the 10th anniversary of the first post for the Python Module of the Week series. What started as an exercise to push myself to write on a regular basis has, thanks to all of you, evolved into a much more long-lived and popular endeavor than I ever expected. A project of this scope is truly a group effort, and I have been fortunate to find good people to work with. I would like to take this opportunity to thank all of them, too.

A short time after starting PyMOTW, I also started working on Python Magazine, and that made me interested in more formal publishing options. I met Debra Williams Cauley from Pearson Education at PyCon. Debra showed me how to frame the blog and website content in a form that would work as a book, and guided me through the process. I revised the content I had for the web site to make it ready for The Python Standard Library by Example over the course of 2011, with technical review help from Matt Culbreth, Katie Cunningham, Jeff McNeil, and Keyton Wessinger. The folks on the Pearson production staff were amazing, and the book was definitely stronger and more successful due to their help. I’m extremely happy with the results of what we were able to create together.

Even though Python 3 was available at that time I started, it was not widely adopted yet, so the original edition of the blog and book were focused on Python 2. As expected, we are starting to see an increasing number of tools and libraries drop their compatibility layers so they support only Python 3. Many new Python developers are starting with 3, skipping 2 entirely. Due to the continued popularity of both the site and the book, I knew it was time to pick up the project again.

I started looking seriously at updating the examples on pymotw.com to use Python 3.5 in 2015, and began publishing updates to the site the following year, pruning the modules that were deprecated and adding examples for some of the new modules. I’m grateful for the feedback I received from early readers of those posts, enabling me to make improvements and corrections as I worked. I received special assistance from Jim Baker, who helped with the readline module examples (especially by pointing out the gnureadline package for platforms where GNU libraries are old or not installed by default, like on my Mac) and Patrick Kettner, who ran the platform module examples on Windows and provided the output for me to include. Thank you both! The technical reviewers for the new book were Diana Clarke, Ian Cordasco, Mark McClain, Paul McLanahan, and Ryan Petrello. All of them were very helpful in spotting errors and any confusing sections. As before, the Pearson production team has been exceptional partners.

The updated examples for Python 3.5 are all available now at https://pymotw.com/3/, and The Python 3 Standard Library by Example is available for sale now. I hope you all find the new version as useful as the original.

The project would not be what it is today without the encouragement and interest from the community. It’s always gratifying to hear from all of you who found either the site or the book useful in your work. Thank you for your continued support!

Updated: I overlooked the contribution from Jesse Noller, who wrote the foreword for the first book. That’s no small thing, and I appreciate the time he invested on that and the nice things he said. I also appreciate Raymond Hettinger’s blurb on the back of the new book. Thank you both.

re — Regular Expressions — PyMOTW 3

Regular expressions are text matching patterns described with a formal syntax. The patterns are interpreted as a set of instructions, which are then executed with a string as input to produce a matching subset or modified version of the original. The term “regular expressions” is frequently shortened to as “regex” or “regexp” in conversation. Expressions can include literal text matching, repetition, pattern-composition, branching, and other sophisticated rules. A large number of parsing problems are easier to solve with a regular expression than by creating a special-purpose lexer and parser.

Read more…

This post is part of the Python Module of the Week series for Python 3. See PyMOTW.com for more articles from the series.

string — Text Constants and Templates — PyMOTW 3

The string module dates from the earliest versions of Python. Many of the functions previously implemented in the module have been moved to methods of str objects. The string module retains several useful constants and classes for working with str objects, and this discussion will concentrate on them.

Read more…

This post is part of the Python Module of the Week series for Python 3. See PyMOTW.com for more articles from the series.

ensurepip — Install the Python Package Installer — PyMOTW 3

While Python is the “Batteries Included” programming language and comes with a wide variety of modules in the standard library, there are even more libraries, frameworks, and tools available to be installed from the Python Package Index . To install those packages, a developer needs the installer tool pip . Installing a tool meant to install tools presents an interesting bootstrapping issue, which ensurepip solves.

Read more…

This post is part of the Python Module of the Week series for Python 3. See PyMOTW.com for more articles from the series.

venv — Create Virtual Environments — PyMOTW 3

Python virtual environments are set up for installing packages and running programs in a way that isolates them from other packages installed on the rest of the system. Because each environment has its own interpreter executable and directory for installing packages, it is easy to create environments configured with various combinations of Python and package versions all on the same computer.

Read more…

This post is part of the Python Module of the Week series for Python 3. See PyMOTW.com for more articles from the series.