Book Image

Mastering Python Scripting for System Administrators

By : Ganesh Sanjiv Naik
Book Image

Mastering Python Scripting for System Administrators

By: Ganesh Sanjiv Naik

Overview of this book

Python has evolved over time and extended its features in relation to every possible IT operation. Python is simple to learn, yet has powerful libraries that can be used to build powerful Python scripts for solving real-world problems and automating administrators' routine activities. The objective of this book is to walk through a series of projects that will teach readers Python scripting with each project. This book will initially cover Python installation and quickly revise basic to advanced programming fundamentals. The book will then focus on the development process as a whole, from setup to planning to building different tools. It will include IT administrators' routine activities (text processing, regular expressions, file archiving, and encryption), network administration (socket programming, email handling, the remote controlling of devices using telnet/ssh, and protocols such as SNMP/DHCP), building graphical user interface, working with websites (Apache log file processing, SOAP and REST APIs communication, and web scraping), and database administration (MySQL and similar database data administration, data analytics, and reporting). By the end of this book, you will be able to use the latest features of Python and be able to build powerful tools that will solve challenging, real-world tasks
Table of Contents (21 chapters)

Functions

A function is a set of statements that perform a specific task. Using functions helps in breaking our program into smaller parts. Programs will be more organized if we use functions as it avoids repetition and makes code reusable. Look at the following syntax:

def function_name(parameters):
statement(s)

Refer to the following example:

def welcome(name):
print("Hello " + name + ", Welcome to Python Programming !")

welcome("John")


Output:
Hello John, Welcome to Python Programming !

The return statement

The return statement is used to exit a function. Refer to the following syntax:

return [expression_list]

This statement may contain an expression where a value has to be returned. If there is no expression, then the function will return a None object, as shown in the following example:

def return_value(a):
if a >= 0:
return a
else:
return -a
print(return_value(2))
print(return_value(-4))

Output:
2
4

Lambda functions

In Python, an anonymous function is a function that is defined without a name and is called a lambda function, as it is defined using a keyword lambda. We use these functions whenever we require a function for a short period of time.

Lambda functions are used along with built-in functions, such as filter(), and map().

The filter() function returns a list of elements and has only one iterable as input. The following shows an example using filter():

numbers = [10, 25, 54, 86, 89, 11, 33, 22]
new_numbers = list(filter(lambda x: (x%2 == 0) , numbers))
print(new_numbers)

Output:
[10, 54, 86, 22]

In this example, the filter() function is taking a lambda function and a list as an argument.

The map() function returns a list of results after applying the specified function. Now, let's look at an example using map():

my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(map(lambda x: x * 2 , my_list))
print(new_list)

Output:
[2, 10, 8, 12, 16, 22, 6, 24]

Here, the map() function is taking a lambda function and a list.