-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path6. Writing and Troubleshooting Python Scripts
62 lines (47 loc) · 4 KB
/
6. Writing and Troubleshooting Python Scripts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Writing scripts:
include the shebang on the first line of your script, which used by Linux shell to determine which language to use to execute code in script. also including shebang allows you not to have to do python <scriptname> from Linux terminal to execute a py script
shebang: #!/usr/bin/env python
it is a good practice to define code entry point. if you did not define one, your code would get executed top/down when the Python file is run as a standalone and when it’s being used as Python module
#!/usr/bin/env python
def vlan_exists(vlan_id):
vlans_list = [1, 5, 10, 20]
return vlan_id in vlans_list
if __name__ == "__main__": #this is your entry point
vlans = [5, 8, 10, 15, 20]
for vlan in vlans:
print vlan, vlan_exists(vlan)
python scripts ends in .py
in your script use # to add a comment. text following # on the same line is ignored by python and understood to be a comment for readers
to run a py script from a Linux terminal, issue 'python <scriptname>'
it is recommended to use the shebang #! /usr/bin/env python as it will use the version of Python that is configured in your Linux environment. It also allows you to easily change the version of Python being used without having to modify each individual script
# Executing python scripts:
to run a py script from a Linux terminal, issue 'python <scriptname>'
Note: One thing to note is that when scripts are executed, you may often see a .pyc file in the same directory as your original script. Even though Python is an interpreted language, it still compiles byte level code into a .pyc upon execution for speed
# Analyzing code:
Pay attention to the following when getting started:
Accessing data in lists (index) and dictionaries (key)
Know your data types. string, number, list, etc
Variable scoping. variables are usable so be careful with end value of an object
Code blocks and indentation within conditionals and loops
Note: at any point in your code you code run 'print (type(variablename))' to verify variable type. this could be handy to resolve syntax errors
# Variable types
Global variables, ones that are available anywhere in the Python script, are often defined at the top of the script
There is no scope for variables that are defined under the entry point conditional. Any variable that is defined under the entry point if block is a global variable
As variables are passed into functions, they are accessed within the function using a variable that you defined in the function definition. These variables have local scope and are only available in the function
When analyzing code, especially code that is fairly nested from an indentation perspective, you need to pay extra attention to indents. Python is a programming language that heavily uses indentation for blocks of code within functions, loops, conditionals, and so on. Do not forget it, as moving the indent of a single if block could change the complete logic of a section of code and in longer scripts it is hard to catch sometimes
# Error handling:
Even if a Python statement is syntactically correct, it can cause an error when an attempt is made to execute it
Errors detected during execution are called exceptions
Errors (exceptions) are not handled by programs, but by the developer implementing the program
Python uses try/except statements to handle exceptions
Errors that are detected during code execution are called exceptions and what you want to do is handle these exceptions, gracefully if possible. It allows you, as a network programmability engineer, to perform a given task or execute some other piece of code when an exception is seen
It is up to you what to do when the exception is found—do you want to exit the program immediately, set default values for an object, send an alert, reboot a device, and so on
e.g
devices = ['r1', 'r2', 'r3']
try:
print devices[4]
except (IndexError) as err_msg:
print ("Error from python:"), (err_msg)
except Exception as err_msg:
print ("Unknown error happened")
There is no limit to the number of exceptions you can use