Creating Python Scripts for Repetitive Task

As a network engineer, I do a lot of repetitive work. And this can be mind numbing as well as a key source of human error in tasks that I perform. So as a step to simplify my repetitive tasks and reduce the amount of human errors in my work, I generally create a python script for any task that I will need to do more than once. So I figured I would lay out an easy how-to for creating these python scripts in hopes it will help others with these tasks.

First things first, you will need to have Python3 installed in order to use it. There are many online tutorials for installing Python, so I won’t ad nauseum to that. Here is a link to an easy-to-follow installation instruction. The link covers Windows, Mac and Linux. Once installed and I am going to assume that it is added to your ‘PATH’ (adding path to Python in Windows, MAC or Linux). The ‘PATH’ of an operating system creates symbolic links to programs so that you can invoke them from anywhere.

The next item up that is required, is a syntax highlighting text editor. Something that will make your python script be colored as you type it out so that syntax error and such are easy to see. My personal preference for a text editor is Notepad++. It is an amazing tool for anything that requires text editing. If you are unfamiliar with Notepad++, I would definitely suggest checking it out. Of course, you can use any text editor. Even the default notepad in windows or vim in Linux, if that is your preference. As long as you can type in it, and save it with a different file extension, it will work for this application.

#This is a python script example

#This is the comment above the first example
def example(var1='',var2='',var3=''):
    """{
    'var1':'This is the question that is answered to fulfill var1',
    'var2':'This is the question that is answered to fulfill var2',
    'var3':'This is the question that is answered to fulfill var3'
    }"""
    print(f'This is a sentence that the {var1} is injected into.')
    print(f'This is a {var2} that the variable is injected into.')
    print(f'This {var3} a sentence that the variable is injected into.')
    print(f'This {var3} a sentence that the variable is injected into.')

#You can use a space after the commas, or you can choose not to. It is a personal preference. A line can have multiple variables. There is no limitation on that.
def example2(var1='', var2='', var3=''):
    print(f'This {var3} a {var2} that the {var1} is injected into.')

#You can also just use variable replacement instead of an equals sign
def example3(var1,var2,var3):
    print(f'This {var3} a {var2} that the {var1} is injected into.')

#Now for calling the functions
#If you remove the pound symbol from these statements, they will run as soon as you import them into 
#the IDE. Or you can run it externally and they will print to your terminal session.
#print(example(var1='variable',var2='sentence',var3='is'))

#print(example2(var1='variable', var2='sentence', var3='is'))

#print(example3('variable','sentence','is'))

The script can be saved as ‘.py’, and just make note of where you save it. Navigate to the folder where the file is saved. Or create a workspace folder for it elsewhere and store it there. Navigate to where the ‘SimpleScript.py’ is located with windows explorer or Linux and open the python instance in that folder. By right clicking on the windows explorer and choosing ‘open terminal here’. And that is what it will do. If in Linux, it would be in your home drive by default. Otherwise, I expect you will know where it is at. Instantiate ‘python3’ while you are in the folder. Next you need to import the script into a python ide session. First, before doing this make sure you turn your terminal history to 128000, or higher if possible history size. I’ve never regretted having to much terminal rollback available.

Once you enter the Python session, you should import the python script, so that you can call the functions within it. This is fairly straight forward, and with no logic. It is the base of templatizing the standard configs that you generate and reduce copy and human error. They greatly simplify my day-to-day, because generating configurations from a single service to a service router is just a matter of can I answer the questions for the script. My file is saved as “python_script_example.py”. I import the script using a variable. And this keeps the main headspace of your IDE clean. It is a best practice. The you use that variable for the python script. And then suffix a function on it. And voila! You have call the function from another document. If you don’t want to navigate, you can use the entire real path with the import statement and import things from anywhere. Personal preference kind of thing.

Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import python_script_example as ps
>>>
>>>
>>> ps.example(var1="variable",var2='sentence',var3='is')
This is a sentence that the variable is injected into.
This is a sentence that the variable is injected into.
This is a sentence that the variable is injected into.
This is a sentence that the variable is injected into.
>>>
>>>
>>>
>>>
>>> ps.example2(var1="variable", var2='sentence', var3='is')
This is a sentence that the variable is injected into.
>>>
>>>
>>>
>>>
>>>
>>> ps.example3('variable','sentence','is')
This is a sentence that the variable is injected into.
>>>

And there you have it. You have made a basic script. You have imported that script into the IDE python instance and executed it’s code with the input that you provided. Single quote ‘ ‘ or double quotes ” “, it does not matter in Python. Just as a space with the commas, is optional. As well as spacing with equals signs. Python is a very flexible language!

Leave a Reply