-
Notifications
You must be signed in to change notification settings - Fork 0
/
Python useful commands
111 lines (98 loc) · 6.04 KB
/
Python useful commands
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
Directory commands:
-------------------------
import os -> module to import
os.getcwd() -> Get Current Directory
os.chdir('C:\\Python33') -> Change current directory
os.listdir() -> Lists the files in the directory
os.mkdir('test') -> Creating new folder
os.rename('test','new_one') -> Renaming the folder or file
os.remove('old.txt') -> Removing file
os.rmdir('new_one') -> Removing directory
import shutil
shutil.copy(<src_file>,<dst_file>) -> To copy a file(use case.. copy the template and edit only the variables)
IP address commands:
-------------------------
import ipaddress -> module to import
ipaddress.ip_address('192.168.1.1') -> Host address
ipaddress.ip_network('192.168.0.0/24') -> Network address
ipaddress.ip_interface('192.168.0.1/24') -> Interface address(an address with subnet mask)
>>> a = ip_network('192.168.1.0/24') -> subnet_of
>>> b = ip_network('192.168.1.128/30')
>>> b.subnet_of(a)
>>> for addr in IPv4Network('192.0.2.0/28'): -> Iteration
... addr
...
IPv4Address('192.0.2.0')
IPv4Address('192.0.2.1')
IPv4Address('192.0.2.2')
IPv4Address('192.0.2.3')
>>> IPv4Network('192.0.2.0/28')[0] -> Accessing particular address
IPv4Address('192.0.2.0')
>>> IPv4Network('192.0.2.0/28')[15]
IPv4Address('192.0.2.15')
>>> interface = IPv4Interface('192.0.2.5/24')
>>> interface.ip
IPv4Address('192.0.2.5')
>>> interface.network -> Network of a host
IPv4Network('192.0.2.0/24')
>>> interface.with_prefixlen -> With Prefix len
'192.0.2.5/24'
>>> interface.with_netmask -> Subnet mask
'192.0.2.5/255.255.255.0'
>>> interface.with_hostmask -> Wildcard mask
'192.0.2.5/0.0.0.255'
Excel commands:
---------------
Excel writing:
import xlsxwriter -> Importing module for Excel writing
workbook=xlsxwriter.Workbook('example.xlsx') -> Creating workbook
workbook.close() -> Closing workbook(at the end)
sheet1=workbook.add_worksheet('sheet1') -> Creating sheet
ft = workbook.add_format({'bold': True,'border':1, 'align': 'center'}) -> Creating font for the workbook
ft.set_align('vcenter') -> Setting alignment
ft.set_font_size(10) -> Setting font size
ft.set_font_name('Segoe UI') -> Setting font name
ft.set_font_color('#FFFFFF') -> Setting font color
ft.set_bg_color('#05204F') -> Setting font bg color
sheet1.write(2,0,'sample text',ft) -> Writing to a particular cell of a sheet & assigning the font
sheet3.set_column(<col_strat>, <col_end>, <width>) -> Set coumn width for the range of columns
Excel reading:
import xlrd -> Module to import
from xlrd import open_workbook,cellname -> Libraries to import
book = open_workbook("example.xlsx") -> Opening the book to read
sheet = book.sheet_by_index(0) -> Accessing particular sheet
sneet.name -> Access sheet name
sheet.nrwos -> Number of rows in the sheet
sneet.ncols -> Number of cols in the sheet
for sheet in book.sheets(): -> Iterating through the sheets
val = sheet.cell(row_index,col_index).value -> Assining value of a particular cell to variable 'val'
Regular expressions:
-------------------
import re -> Library to import
heroRegex = re.compile (r'Batman|Tina Fey') -> Create the pattern
heroRegex.sub('<replacement word>','<source string>') -> Retruns a new string, that replaced the pattern with 'replacement word'
re.match(<pattern>,<string to test>) -> Returns true if there is a match, can be used with conditionals..
ipregex = re.compile(r'\d+\.\d+\.\d+\.\d+') -> creates a pattern to match an IP address(though there is no 255 check here
re.findall(ipregex, <source string>) -> returns a list of all the occureances of the ipregex pattern
String Operations:
------------------
text.split()
text.strip()
text.replace()
pyautogui:
----------
import pyautogui
screenWidth, screenHeight = pyautogui.size() # Returns two integers, the width and height of the screen. (The primary monitor, in multi-monitor setups.)
currentMouseX, currentMouseY = pyautogui.position() # Retursn two integes, the x and y of the mouse cursor's current position.
pyautogui.moveTo(100, 150) # Move the mouse to the x, y coordinates 100, 150.
pyautogui.click() # Click the mouse at its current location.
pyautogui.click(200, 220) # Click the mouse at the x, y coordinates 200, 220.
pyautogui.move(None, 10) # Move mouse 10 pixels down, that is, move the mouse relative to its current position.
pyautogui.doubleClick() # Double click the mouse at the
pyautogui.moveTo(500, 500, duration=2, tween=pyautogui.tweens.easeInOutQuad) # Use tweening/easing function to move mouse over 2 seconds.
pyautogui.write('Hello world!', interval=0.25) # Type with quarter-second pause in between each key.
pyautogui.press('esc') # Simulate pressing the Escape key.
pyautogui.keyDown('shift')
pyautogui.write(['left', 'left', 'left', 'left', 'left', 'left'])
pyautogui.keyUp('shift')
pyautogui.hotkey('ctrl', 'c')