Python 2.7 Scripting For System Administrators

Course

Intro Video

Photo of Keith Thompson

Keith Thompson

DevOps Training Architect II in Content

Length

06:07:44

Difficulty

Intermediate

Course Details

In this course, you will develop the skills that you need to write effective and powerful scripts and tools using Python. We will go through the necessary features of the Python language to be able to leverage its additional benefits in writing scripts and creating command line tools (data types, loops, conditionals, functions, error handling, and more). Beyond the language itself, you will go through the full development process including project set up, planning, and automated testing to build two different command line tools.

Syllabus

Introduction

Continue If You're Interested in Python 2

00:00:28

Lesson Description:

Check out the Python 3 version of this course.

About the Course Author

00:01:59

Lesson Description:

Learn a little about me and what you can do to get the most out of this course.

Course Tools

00:07:19

Lesson Description:

Learn about the Linux Academy features that this course uses and general features that will improve your learning experience.

Syllabus

00:06:43

Lesson Description:

Get a brief overview of the course's content and flow.

Setting Up a Cloud Server Development Environment

00:08:23

Lesson Description:

Links for this Video Content Repository .bashrc file .vimrc file Before diving into the course it's important that you have the following installed: Python 2.7 (pre-installed on modern Linux distributions) git wget which words (need file at /usr/share/dict/words) lsof text editor of your choice This video will show you how to utilize a cloud server as a development environment to follow along with this course.

Introducing Python

History and Benefits of Python

00:06:20

Lesson Description:

Before learning how to write scripts using Python, we cover the background of Python, the language's core feature set, and its non-programming benefits.

What's the Deal with Python 3?

00:05:39

Lesson Description:

We briefly talk about the issues that Python 3 set out to solve and the current state of Python. Links from this video: Python 2 or Python 3 What's new with Python 3

Just Enough Python

Introducing the REPL for Rapid Experimentation

00:01:53

Lesson Description:

Learn to utilize the Read Evaluate Print Loop (REPL) that Python provides to experiment with Python code and explore the language.

Creating and Running Python Scripts

00:02:13

Lesson Description:

Learn how to create Python scripts and run them using the Python CLI or as an executable.

Using Comments in Python

00:01:50

Lesson Description:

Learn how to document your Python code using comments.

Data Types - Strings and Numbers

00:09:13

Lesson Description:

Start learning about the built-in string and number types in Python.

Data Types - Booleans and None

00:02:33

Lesson Description:

Learn how to represent truth and nothingness in Python using the built-in Boolean and None types.

Working with Variables

00:02:57

Lesson Description:

Learn how to create variables in a Python script.

Lists and Tuples

00:13:08

Lesson Description:

Learn how to use the most common collections in Python: lists and tuples.

Dictionaries (dicts)

00:06:59

Lesson Description:

Learn to use Python dictionaries to organize key-value information.

Control Flow - Conditionals and Comparisons

00:07:21

Lesson Description:

Learn how to make comparisons in your Python code and branch your programs using Python's "if", "elif", and "else".

Control Flow - Loops

00:08:31

Lesson Description:

Learn how to run code repeatedly and iterate over collections by using loops.

Control Flow - Logic Operations

00:06:38

Lesson Description:

Learn how to perform logic operations in Python.

Exercise: Creating and Displaying Variables

00:30:00

Exercise: Working with If/Else

00:30:00

Exercise: Iterating Over Lists

00:30:00

Python Basics

Scripting with Python

Reading User Input

00:04:53

Lesson Description:

Learn how to prompt the user of your scripts to enter information and how to use that information in your code.

Encapsulating Behavior Using Functions

00:12:47

Lesson Description:

Improve the organization of your code and make it easy to reuse logic by creating functions in Python.

Using Standard Library Packages

00:08:05

Lesson Description:

Learn to leverage a massive amount of built-in functionality by pulling in extra software packages from Python's standard library.

Working with Environment Variables

00:04:51

Lesson Description:

Learn how to interact with your user's environment by reading in environment variables.

Interacting with Files

00:07:13

Lesson Description:

Learn to read, write, and append to files using Python. Note: if you're using Python 3, the following line will not work: xmen_file.seek(-1, os.SEEK_END) The equivalent for Python 3 would be: xmen_file.seek(0, os.SEEK_END)

Parsing Command Line Parameters

00:15:58

Lesson Description:

Improve the user experience of your scripts by accepting command line parameters and displaying help text.

Handling Errors with try/except/else/finally

00:10:11

Lesson Description:

Create better scripts by anticipating errors that will occur and making your scripts handle them gracefully.

Execute Shell Commands from Within Python

00:08:20

Lesson Description:

Leverage code that isn't written in Python for your scripts by learning how to invoke other shell commands.

Exit Statuses

00:02:25

Lesson Description:

Make your scripts more useful by returning useful error codes when something goes wrong by setting exit statuses.

Advanced Iteration with List Comprehensions

00:07:47

Lesson Description:

Note: your /usr/share/dict/words file might not be exactly like mine so don't be surprised if you get slightly different results. Learn to use "list comprehensions" in Python as a way to more succinctly iterate over your lists to return new values. Installing /usr/share/dict/words The words file might not be installed on your system (such as a CentOS 7 cloud server), but you can install it using: $ sudo yum install -y words On a debian system, you'll to run this command to populate the /usr/share/dict/words file: $ sudo apt-get install --reinstall wamerican-insane

Standard Library: random & json

00:10:17

Lesson Description:

Learn to utilize more of the standard library by creating JSON files with random contents as part of a short example project.

Standard Library: shutil & glob

00:12:26

Lesson Description:

Learn to search files by name using (glob) patterns and find replacements in the standard library for common UNIX tools like mv, mkdir, cp, and rm.

Standard Library: re & math

00:12:23

Lesson Description:

Learn some of the more common math functions and how to potentially use regular expressions in your Python.

Exercise: Creating and Using Functions

00:30:00

Exercise: Using the 'os' Package and Environment Variables

00:30:00

Exercise: Creating Files Based on User Input

00:30:00

Exercise: Handling Errors When Files Don't Exist

00:30:00

Exercise: Interacting with External Commands

00:30:00

Exercise: Setting Exit Status on Error

00:30:00

Python Scripting and IO

Third Party Packages

Installing 'pip' to Manage Python Packages

00:03:44

Lesson Description:

Learn how to install the `pip` package manager so that you can start working with third-party packages.

Installing Third-Party Packages Using 'pip'

00:07:31

Lesson Description:

Learn to use `pip` to install third-party packages, leveraging the open-source community.

Virtualenv

00:04:38

Lesson Description:

Working with the system Python works fine, unless you need to have multiple versions of a Python package. Virtualenvs are Pythons way of sandboxing dependencies.

Using Third-Party Code in Your Scripts

00:07:35

Lesson Description:

Learn to utilize the third-party packages in your own scripts by using the popular `requests` library.

Exercise: Installing Third-Party Packages

00:30:00

Exercise: Utilizing Third-Party Packages

00:30:00

Python Packages and Dependencies

Creating a Larger Scripting Project

Examining the Problem and Planning

00:08:52

Lesson Description:

For this final section, we'll be building a full project from planning to distribution. The first thing that we need to do is plan out our approach and set up some of our non-Python dependencies.

Structuring the Project

00:13:27

Lesson Description:

Time to set up our Python project. We'll be creating a virtualenv, documenting our project, creating our repository, and making our project installable via pip.

Test Driving the Command Line Parser (Part 1)

00:20:31

Lesson Description:

The first unit of our application that we'll set up is our CLI's parser. We will be implementing this (and the rest of the units) using Test Driven Development (TDD) techniques. In this video, we'll set up our testing environment and implement the initial version of our parser.

Test Driving the Command Line Parser (Part 2)

00:09:38

Lesson Description:

We continue working on our command line parser by doing a little refactoring.

Implementing PostgreSQL Interaction

00:15:39

Lesson Description:

Working on our second unit, we'll be implementing our tool's database interaction by interfacing with the `pg_dump` tool.

Implementing Local File Storage

00:04:57

Lesson Description:

Use TDD to implement the way we store files locally.

Implementing S3 Storage

00:09:14

Lesson Description:

Use TDD to implement the way we store files on AWS S3.

Wiring the Units Together

00:15:13

Lesson Description:

With all of the individual units built and tested, it's time to combine them to build our final CLI implementation.

Building and Sharing a Wheel Distribution

00:06:52

Lesson Description:

Learn how to build a distribution of the pgbackup project so that it can easily be shared and installed using pip.

Installing Python 3 on CentOS 7

00:03:41

Lesson Description:

Learn to build Python 3 from source on a CentOS system. This prepares you for adding Python 3 support to the pgbackup package.

(Optional) Installing Python 3 on Debian

00:04:13

Lesson Description:

Learn to build Python 3 from source on a Debian system. This prepares you for adding Python 3 support to the pgbackup package. Mark this video as complete if you've already installed Python 3.

Testing Against Python 2 and Python 3

00:12:29

Lesson Description:

We've been working with mostly Python 2, but that doesn't mean that the same code can't be run with Python 3. Let's take a look at how we can test our code under both Python versions using the `tox` package.

Exercise: Creating a Python Project

00:30:00

Exercise: Test Drive CLI Parser

00:30:00

Exercise: Implementing User Management

02:00:00

Exercise: JSON Parsing and Exporting

01:30:00

Exercise: Creating the Console Script

00:30:00

Exercise: Building a Wheel Distribution

00:30:00

Python Projects, Testing, and Distribution

Conclusion

Thank You and Next Steps

00:01:23

Lesson Description:

Congratulations on finishing this course. Don't forget to review the content and tell the community what you learned and thought of this course. Have fun writing Python!