Python 2.7 Scripting For System Administrators
DevOps Training Architect II in Content
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.
Continue If You're Interested in Python 2
Check out the Python 3 version of this course.
About the Course Author
Learn a little about me and what you can do to get the most out of this course.
Learn about the Linux Academy features that this course uses and general features that will improve your learning experience.
Get a brief overview of the course's content and flow.
Setting Up a Cloud Server Development Environment
Links for this VideoContent Repository .bashrc file .vimrc fileBefore 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 choiceThis video will show you how to utilize a cloud server as a development environment to follow along with this course.
History and Benefits of Python
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.
Just Enough Python
Introducing the REPL for Rapid Experimentation
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
Learn how to create Python scripts and run them using the Python CLI or as an executable.
Using Comments in Python
Learn how to document your Python code using comments.
Data Types - Strings and Numbers
Start learning about the built-in string and number types in Python.
Data Types - Booleans and None
Learn how to represent truth and nothingness in Python using the built-in Boolean and None types.
Working with Variables
Learn how to create variables in a Python script.
Lists and Tuples
Learn how to use the most common collections in Python: lists and tuples.
Learn to use Python dictionaries to organize key-value information.
Control Flow - Conditionals and Comparisons
Learn how to make comparisons in your Python code and branch your programs using Python's "if", "elif", and "else".
Control Flow - Loops
Learn how to run code repeatedly and iterate over collections by using loops.
Control Flow - Logic Operations
Learn how to perform logic operations in Python.
Exercise: Creating and Displaying Variables
Exercise: Working with If/Else
Exercise: Iterating Over Lists
Python 2.7 Scripting For System Administrators Python Basics
Scripting with Python
Reading User Input
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
Improve the organization of your code and make it easy to reuse logic by creating functions in Python.
Using Standard Library Packages
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
Learn how to interact with your user's environment by reading in environment variables.
Interacting with Files
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:
Parsing Command Line Parameters
Improve the user experience of your scripts by accepting command line parameters and displaying help text.
Handling Errors with try/except/else/finally
Create better scripts by anticipating errors that will occur and making your scripts handle them gracefully.
Execute Shell Commands from Within Python
Leverage code that isn't written in Python for your scripts by learning how to invoke other shell commands.
Make your scripts more useful by returning useful error codes when something goes wrong by setting exit statuses.
Advanced Iteration with List Comprehensions
/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/wordsThe 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 wordsOn a debian system, you'll to run this command to populate the
$ sudo apt-get install --reinstall wamerican-insane
Standard Library: random & json
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
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
Learn some of the more common math functions and how to potentially use regular expressions in your Python.
Exercise: Creating and Using Functions
Exercise: Using the 'os' Package and Environment Variables
Exercise: Creating Files Based on User Input
Exercise: Handling Errors When Files Don't Exist
Exercise: Interacting with External Commands
Exercise: Setting Exit Status on Error
Python 2.7 Scripting For System Administrators Python Scripting and IO
Third Party Packages
Installing 'pip' to Manage Python Packages
Learn how to install the `pip` package manager so that you can start working with third-party packages.
Installing Third-Party Packages Using 'pip'
Learn to use `pip` to install third-party packages, leveraging the open-source community.
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
Learn to utilize the third-party packages in your own scripts by using the popular `requests` library.
Exercise: Installing Third-Party Packages
Exercise: Utilizing Third-Party Packages
Python 2.7 Scripting For System Administrators Python Packages and Dependencies
Creating a Larger Scripting Project
Examining the Problem and Planning
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
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)
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)
We continue working on our command line parser by doing a little refactoring.
Implementing PostgreSQL Interaction
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
Use TDD to implement the way we store files locally.
Implementing S3 Storage
Use TDD to implement the way we store files on AWS S3.
Wiring the Units Together
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
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
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
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
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
Exercise: Test Drive CLI Parser
Exercise: Implementing User Management
Exercise: JSON Parsing and Exporting
Exercise: Creating the Console Script
Exercise: Building a Wheel Distribution
Scripting For System Administrators Python Projects Testing and Distribution
Thank You and Next Steps
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!
Take this course and learn a new skill today.
Transform your learning with our all access plan.Start 7-Day Free Trial