forked from chriswernst/data-science-lessons-DI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Python_Notes.py
1264 lines (790 loc) · 30.3 KB
/
Python_Notes.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
PYTHON 3.5 NOTES
Last Updated 10/7/2017
Might want to use Spyder 3.5 or iPython in order to have access to all libraries
- and DON'T use pip3 to update libraries when using Anaconda
http://stackoverflow.com/questions/33851379/pyaudio-installation-on-mac-python-3
'''
#################### BASIC FUNCTIONALITIES ####################
"""
OR
'''
is meant for documentation
"""
# is a regular comment
import os
import numpy
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
# make calls to different .py documents -- 'libraries' or 'modules'
from extra_functions import function1, function2, var1, var3
import numpy
from numpy import *
from scipy import *
# the 'from libName import *' allows the module to be used without the prefix. For example:
from numpy import *
pi
Out[2]: 3.141592653589793 # Output
# shorten/nickname library names
import numpy as np
np.pi
import webbrowser as wb
wb.open('http://google.com')
# to import using shorthand
import os
os.getcwd()
# imports relevant lib and gets working directory
os.chdir('C:\\Users\\NYCCE\\Documents\\Python 3.5\\Programs') # Windows Directory
os.chdir('/Robotics Nanodegree - Udacity/Term 1/Project 1 - Search and Sample') # OSX Directory
# sets the directory
dir(modulename)
#lists the variables in named module
locals()
#gives all the variables avail.. typically locals() > dir()
globals()
#gives all of the vars avail... often locals() == globals()
sys.path
# gives the available paths
who
# gives defined vars and 'shortcuts'
whos
# lists the modules that are activated
win.close()
#closes window
del
# deletes vars
clear
# clears console
# Gives length of list
len(listName)
# continues to the next line without executing '\
above_thresh = (yellowPixels[:,:,0] > rgb_thresh[0]) \
& (yellowPixels[:,:,1] > rgb_thresh[1]) \
& (yellowPixels[:,:,2] < rgb_thresh[2])
# To run Python in Command Line(Terminal)
chmod +x NameofScript.py
./NameofScript.py
# makes the make beep sound
print('\a')
# prints a blank line after writing hello
print('Hello\n')
# takes an int input from user
interval = eval(input("Please enter the interval length in seconds: "))
# allows for substitution from SymPy
YZ_intrinsic_num = YZ_intrinsic_sym.evalf(subs={q1: 45*dtr, q2: 60*dtr})
# takes a str input from user
name = input("Please enter name: ")
# formatting strings
'%.8f' % (1/3.0)
# outputs '0.33333333' - 8 decimal places
# Set the separate rows to different variables:
b
# output is:
Out[225]:
array([[3, 4, 5],
[6, 7, 8]])
b1, b2 = b
b1
# output is:
Out[227]: array([3, 4, 5])
b2
# output is:
Out[228]: array([6, 7, 8])
#################### MODULES / LIBRARIES ########################
import numpy
import scipy
import math
import matplotlib
import pylab
import os
import io
import webbrowser
import requests
import datetime
import time
import bs4
import openpyxl # Python to excel module pg.277 of Automate Boring things
import pyautogui #CH.18 'automate the Boring...' manipulate mouse & keyboard
import pyglet # a music / sounds module
import wave
import pyaudio
import sqlite3
import tkinter
import sympy
####################### Package Management ########################
# from the OS terminal to include python packages in Anaconda
conda install packageName
# If you're using regular python package manager
pip3 install serial
# OR
sudo pip3 install modulename
###################### BASIC PYTHON LISTS, TUPLES #################
# Lists - can be changed
# Tuples - cannot be changed (immutable)
# Lists
A=[1,2,3,4]
A[0]
Out[138]: 1 # First object in the list is a 1.
# Remember Python is a indexed at 0
A[0]=2
Out[145]: [2, 2, 3, 4] # replaces the '1' with a 2
# Tuples
a=(1,2,3,4)
a[0]
Out[163]: 1
a=((1,2),(3,4))
# Generates a 2x2 tuple matrix
a[0]=2
# the output generates an error
# TypeError: 'tuple' object does not support item assignment
red_channel[:,:,[1, 2]] = 0 # Zero out the green and blue channels
# can be read as, all rows, all columns, items 1 and two of the 3-tuple
########## FOR LOOPS
#INPUT CODE
count=0
while(count<5):
print (count)
count +=1
else:
print ("count value reached %d" %(count))
#OUTPUTS THE FOLLOWING
0
1
2
3
4
count value reached 5
#
for i in range(10):
pag.moveTo(100, 100, 0.25)
pag.moveTo(200, 100, 0.25)
pag.moveTo(200, 200, 0.25)
pag.moveTo(100, 200, 0.25)
# Meaning of %d and %s
They are used for formatting strings. %s acts a placeholder for a string while
%d acts as a placeholder for a number. Their associated values are passed
in via a tuple using the % operator.
##################### MATRICES #################
# Transpose function
import numpy
from numpy import *
a = [1,2,3,4,5]
aT = matrix(a).transpose()
# transpose output
Out[46]:
matrix([[1],
[2],
[3],
[4],
[5]])
# Reshaping function
c=[1,2,3,4,5,6]
cT = numpy.reshape(c,(2,3))
# reshaping output
Out[60]:
array([[1, 2, 3],
[4, 5, 6]])
# Get the total number of units in a matrix
# or a vector. Output = rows X columns
size(matrix name, axis=0 or 1) #axis is optional. 0=rows, 1=columns
# gives the matrix dimensions
numpy.shape(matrix name)
# Increment
X+=1
# Decrement
X
-=1
# manipulating / altering matrices
matrix_name[row, column]
green_channel[:,:,[0, 2]] = 0 # Zero out the red and blue channels
# in a large matrix, with multiple items per positions, this tells the program to include, all rows ':', all columns ':', and items 0 and 2 in the list
# items R G B - zero referencing the RED, 2 referencing the blue
# create a Matrix
numpy.zeros((rows, columns))
numpy.matrix([[1, 2],[3, 4]])
# outputs
matrix([[1, 2],
[3, 4]])
# use of a for loop
for row in range(0,np.size(blue_channel,0)): #this goes across the rows
for col in range(0,np.size(blue_channel,1)): #this goes across the columns
if(blue_column[row,col] > 45):
blue_channel_test[row,col] = 1
else:
blue_channel_test[row,col] = 0
# now display the grayscale image
plt.imshow(blue_channel_test, cmap='gray')
# plt is matplotlib.pyplot
# From SYMPY - See the Sympy Module section
a = Matrix([[1,1,2],[3,4,5],[6,7,8]])
######################################################## CLASSES ########################################################
class Databucket():
def __init__(self):
# The __init__ function is called a constructor, or initializer, and is automatically called when you create a new instance of a class.
self.images = csv_img_list
# the quoted names below are column headers in the csv file
self.xpos = df["X_Position"].values
self.ypos = df["Y_Position"].values
self.yaw = df["Yaw"].values
self.count = -2 # This will be a running index, setting to -1 is a hack
# because moviepy (below) seems to run one extra iteration
self.worldmap = np.zeros((200, 200, 3)).astype(np.float)
self.ground_truth = ground_truth_3d # Ground truth worldmap
# Instantiate a Databucket().. this will be a global variable/object
# that you can refer to in the process_image() function below
data = Databucket()
# We can now make calls to this class since we instantiated it with 'data':
data.count += 1 # Keeps track of the index in the Databucket() by incrementing
data.yaw[data.count] # calls the current yaw position from the Databucket()
####################################### EXCEPTION HANDLING #############################
### Example 1:
# Runs through the list and executes the command even if there is an error
for i in range(len(emailList)):
try:
outputWriter.writerow([emailList[i]])
except UnicodeEncodeError:
pass
# can also use an except clause without specifying the error name:
except:
print("Unexpected error:", sys.exc_info()[0])
raise
### Example 2:
# A nice way to set up a program to handle errors:
def main():
try:
# Your primary code here...
except UnboundLocalError:
plt.savefig(imageDirectory + imageName[0:-4] +'faceDetect.jpg')
# Save the file
print("Make sure there are faces and eyes in your image!")
# Exception for an image without faces and/or eyes
except FileNotFoundError:
print("Image not found. Verify the file name and path!")
# Exception for bad image path
main()
###################################################### MODULES ##################################################################
###################################################### MODULES ##################################################################
###################################################### MODULES ##################################################################
###################################################### MODULES ##################################################################
###################################################### MODULES ##################################################################
###################################################### MODULES ##################################################################
###################################################### MODULES ##################################################################
###################################################### MODULES ##################################################################
###################################################### MODULES ##################################################################
###################################################### MODULES ##################################################################
###################################################### MODULES ##################################################################
###################################################### MODULES ##################################################################
###################################################### MODULES ##################################################################
###################################################### MODULES ##################################################################
###################################################### MODULES ##################################################################
###################################################### MODULES ##################################################################
###################################################### MODULES ##################################################################
###################################################### MODULES ##################################################################
################################################ MATPLOTLIB MODULE ############################
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# Define the filename, read and plot the image
filename = 'camera_test_image.jpg'
image = mpimg.imread(filename)
# the mpimg.imread transfers the image file into a 3tuple numerical array
plt.imshow(image)
#displays the given jpg image in the command line
plt.show()
%matplotlib inline
# brings windows back into the console
# this can also be set from the preferences menu and changed from "inline" to automatic
plt.close()
#closes the opened window
# some plyplot functions to generate multiple images on one window, draw an arrow, etc
fig = plt.figure(figsize=(12,9))
plt.subplot(221)
# The 22X naming convention means the figure will be broken into that many rows and columns
plt.imshow(image)
plt.subplot(222)
plt.imshow(warped)
plt.subplot(223)
plt.imshow(colorsel, cmap='gray')
plt.subplot(224)
plt.plot(xpix, ypix, '.')
plt.ylim(-160, 160)
plt.xlim(0, 160)
arrow_length = 100
x_arrow = arrow_length * np.cos(avg_angle)
y_arrow = arrow_length * np.sin(avg_angle)
plt.arrow(0, 0, x_arrow, y_arrow, color='red', zorder=2, head_width=10, width=2)
plt.show()
################################################## NUMPY MODULE ############################
import numpy as np
np.mean(yellowPixels[:,:,0])
# gives the average of all rows, all columns, and the first of the tuples. This is for an RGB average. Specifically, "Red"
blueMin = np.min(yellowPixels[:,:,2])
redMax = np.max(yellowPixels[:,:,0])
# min / max functions
np.size(var, which_axis)
np.size(image, 0)
# returns the number of rows
# has the same effect as:
var.shape[0]
image.shape[1]
# returns the number of columns
np.shape(image)
# gives use the size of the matrix
numpy.nonzero()
image.nonzero()
# determines values that are non-zero
np.reshape(list, (rows,cols))
np.reshape(a, (2,2))
np.clip(a, a_min, a_max, out=none)
# truncates values
# example:
b=[1,2,3,4,5,6,7,8,9,0]
np.clip(b, 1, 4)
Out[33]: array([1, 2, 3, 4, 4, 4, 4, 4, 4, 1])
# replaces values above 4 with 4; and replaces values less than 1 with 1.
# How is this useful? See below for use with the rover:
steering = np.clip(avg_angle_degrees, -15, 15)
# our avg_angle_degrees turns out to be 39, but we can only turn a maximum of 15 degrees at a time
degrees = angle_in_radians * 180/np.pi
# converts radians to degrees
yaw_rad = yaw * np.pi / 180
# converts degress to radians
dist = np.sqrt(x_pixel**2 + y_pixel**2)
# above is pythagorean's theorem
# dist is C, or hypoteneuse
# Calculate angle away from vertical for each pixel
angles = np.arctan2(y_pixel, x_pixel)
area = np.concatenate((redcol, greencol, bluecol), axis=2)
# joins the three columns(160,320,1) together as a (160, 320, 3) array
np.isfinite(Rover.vel)
# returns a boolean value for if the inputted value is finite
######################################### SYMPY MODULE - MATRIX MANIPULATION ############################
# "SymPy is a full-featured computer algebra system (CAS) that will enable you to construct and
# manipulate matrices symbolically and then numerically evaluate them when needed."
# Learn more at: http://docs.sympy.org/latest/tutorial/matrices.html
from sympy import symbols, cos, sin, pi, simplify, eye, zeros, ones, diag
from sympy.matrices import Matrix
# examples:
# standard square matrix
a = Matrix([[1,1,2],[3,4,5],[6,7,8]])
# column vector:
colVec = Matrix([1,2,3,4,5])
# Matrix
P = Matrix([[ ],
[ ],
[]])
# returns the shape of the matric
a.shape
colVec.shape
# returns the selected row or column
a.row(0)
a.col(0)
# inverse of a matrix
a**-1
# Transpose of a matrix
a.T
# Create the identity matrix
eye(3)
# Create an empty matrix
zeros(3,2)
# Create a matrix with ones
ones(4,2)
# Creates a square matrix with only the diagonal elements filled in
diag(2,2,2,2,2)
# To compute the determinant, use det
M = Matrix([[1, 0, 1], [2, -1, 3], [4, 3, 2]])
M.det()
# Conversions between radians to degrees
rtd = 180./np.pi # radians to degrees
dtr = np.pi/180. # degrees to radians
# Now we create the rotation matrices for elementary rotations about the X, Y, and Z axes, respectively.
# The about Z rotation matrix is derived on page 45
R_x = Matrix([[ 1, 0, 0],
[ 0, cos(q1), -sin(q1)],
[ 0, sin(q1), cos(q1)]])
# Rotation about X axis
R_y = Matrix([[ cos(q2), 0, sin(q2)],
[ 0, 1, 0],
[-sin(q2), 0, cos(q2)]])
# Rotation about Y axis
R_z = Matrix([[ cos(q3), -sin(q3), 0],
[ sin(q3), cos(q3), 0],
[ 0, 0, 1]])
# Rotation about Z axis
offset = Matrix([[offsetX],
[offsetY],
[offsetZ]])
T_x = Matrix([[ 1, 0, 0, offset[0]],
[ 0, cos(q1), -sin(q1), offset[1]],
[ 0, sin(q1), cos(q1), offset[2]],
[ 0, 0, 0, 1]])
# Homogenous Transform about the X axis
T_y = Matrix([[cos(q2), 0, sin(q2), offset[0]],
[ 0, 1, 0, offset[1]],
[-sin(q2), 0, cos(q2), offset[2]],
[ 0, 0, 0, 1]])
# Homogenous Transform about the Y axis
T_z = Matrix([[cos(q3), -sin(q3), 0, offset[0]],
[ sin(q3), cos(q3), 0, offset[1]],
[ 0, 0, 1, offset[2]],
[ 0, 0, 0, 1]])
# Homogenous Transform about the Z axis
alpha = rtd * atan2(R_XYZ[1,0], R_XYZ[0,0])
beta = rtd * atan2(-R_XYZ[2,0], sqrt(R_XYZ[0,0]**2 + R_XYZ[1,0]**2) )
gamma = rtd * atan2(R_XYZ[2,1], R_XYZ[2,2])
# Numerically Evaluate the matrices
print("Rotation about the X-axis by 45-degrees")
print(R_x.evalf(subs={q1: 45*dtr}))
print("Rotation about the y-axis by 45-degrees")
print(R_y.evalf(subs={q2: 45*dtr}))
print("Rotation about the Z-axis by 30-degrees")
print(R_z.evalf(subs={q3: 30*dtr}))
# Evaluates as a floating point number
# Row Join:
Ra = Matrix([[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])
Ta = Ra.row_join(Matrix([[0],
[0],
[0]]))
# OUTPUT:
Matrix([
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0]])
bottomRow = Matrix([[0,0,0,1]])
# Make a row vector
# Column join:
Tb_a = Rb_a.row_join(tb_a).col_join(bottomRow)
######################################################### PILMODULE ############################
from PIL import Image
pil_im = Image.open('chrysler.jpg')
grey_im = Image.open('chrysler.jpg').convert('LA')
# Converts to greyscale
imshow(grey_im)
grey_im.save('greyChrysler.png')
plt.figure()
plt.gray()
gryChrysler = plt.contour(gray_pil_im, origin='image')
axis('equal')
axis('off')
plt.savefig('grayContourChrysler.png')
# Greyscale is roughly: Y = 0.299 R + 0.587 G + 0.114 B
R = pil_im[:,:,0]
G = pil_im[:,:,1]
B = pil_im[:,:,2]
grey_pil_im = 0.299*R + 0.587*G + 0.114*B
imshow(grey_pil_im)
# color List = b,g,r,c,m,y,k,w
######################################################### OPEN CV MODULE ############################
import cv2
cv2.getPerspectiveTransform()
cv2.warpPerspective()
# Used in our first project in Udacity Nanodegree
face_cascade = cv2.CascadeClassifier('/Users/UserName/anaconda/envs/py35/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('/Users/UserName/anaconda/envs/py35/share/OpenCV/haarcascades/haarcascade_eye.xml')
# Used in face and eye recognition 'facialRecognition.py'
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
face = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
eye = cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
######################################################### PYLAB CV MODULE ############################
import pylab
pylab.savefig('filename.png')
pylab.savefig('filename.png', bbox_inches='tight')
class matplotlib.figure.Figure(figsize=None, dpi=None, facecolor=None, edgecolor=None, linewidth=0.0, frameon=None, subplotpars=None, tight_layout=None)
# saves file as .png
########################################################## PANDAS MODULE ############################
import pandas as pd
# Change the path below to your data directory
# If you are in a locale (e.g., Europe) that uses ',' as the decimal separator
# change the '.' to ','
df = pd.read_csv('../test_dataset/robot_log.csv', delimiter=';', decimal='.')
########################################################## TIME MODULE ############################
import time
time.sleep(2)
# waits 2 seconds
time.ctime()
# Outputs 'Tue Jan 17 17:54:05 2017'
########################################################## DATE TIME MODULE ######################
import datetime
datetime.datetime.now().time()
# outputs just time
datetime.datetime.now()
# outputs the date and time
str(datetime.datetime.now())
# makes it human readable
datetime.datetime.utcnow()
# gives universal time zone
######################################################## SQLite3 MODULE ####################
from sqlite3 import connect
conn = connect(r'/Users/ChrisErnst/temp.db')
# generates a .db file
curs = conn.cursor()
curs.execute('create table emp (who, job, pay)')
prefix = 'insert into emp values '
curs.execute(prefix + "('Bob', 'dev', 100)")
curs.execute(prefix + "('Sue', 'dev', 120)")
curs.execute("select * from emp where pay > 100")
for (who, job, pay) in curs.fetchall():
print(who, job, pay)
payscale = curs.execute("select * from emp where pay > 90")
payscale.fetchall()
# outputs all employees making > 90
############################################################### CSV MODULE ##########################
# Send to CSV
import csv
# Open an existing csv/ or create one if not existing
outputFile = open('testcontacts.csv', 'w', newline='')
# Write to that csv
outputWriter = csv.writer(outputFile)
outputWriter.writerow(list1)
outputFile.close()
# Writes to three separate rows
outputWriter.writerow([list1[0]])
outputWriter.writerow([list1[1]])
outputWriter.writerow([list1[2]])
################################################################### GUI AUTOMATION MODULE #############
import pyautogui as pag
pag.PAUSE = 1
# sets the pause between commands to one second
pag.FAILSAFE = True
# allows a cease of the program if the mouse if navigated
# to the upper left hand corner of the screen
pag.size()
# outputs the size of the current working screen
screenWidth, screenHeight = pag.size()
# sets vars from outputs
pag.moveTo(100, 200, 2)
# moves the mouse pointer to the indicated location (X, Y, time in seconds to complete)
pag.moveRel(0, -200, 2)
# moves the cursor relative to current position (positive-up, neg-down)
pag.hotkey('command', ' ')
# opens the spotlight on OSX
pag.typewrite('write this string')
#types the string
pag.confirm(text='', title='', buttons=['OK', 'Cancel'])
#creates a dialog box
# a loop to move mouse in a square
for i in range(10):
pag.moveTo(100, 100, 0.25)
pag.moveTo(200, 100, 0.25)
pag.moveTo(200, 200, 0.25)
pag.moveTo(100, 200, 0.25)
# Navigates to file save, and saves file
pag.moveTo(126, 7, 2)
pag.click()
pag.moveTo(137, 120, 1.5)
pag.click()
# opens chrome and searches the words below
import time
pag.hotkey('command', ' ')
pag.press('c')
pag.press('h')
pag.press('r')
time.sleep(2)
pag.press('enter')
pag.hotkey('command', 't')
time.sleep(1)
pag.typewrite('puppers')
pag.press('enter')
# finds an image on the screen
button7location = pag.locateOnScreen('calc7key.png')
button7location
(1416, 562, 50, 41)
button7x, button7y = pag.center(button7location)
button7x, button7y
(1441, 582)
pyautogui.click(button7x, button7y)
# clicks the center of where the 7 button was found
#
import time
pag.hotkey('command', ' ')
pag.press('c')
pag.press('h')
pag.press('r')
time.sleep(2)
pag.press('enter')
pag.hotkey('command', 't')
time.sleep(1)
pag.typewrite('images.google.com')
pag.press('enter')
pag.typewrite('apple')
pag.press('enter')
time.sleep(1)2
list(pag.locateAllOnScreen('lookslikethis.jpg'))
# prints mouse position constantly
try:
while True:
x, y = pyautogui.position()
positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
print(positionStr, end='')
print('\b' * len(positionStr), end='', flush=True)
except KeyboardInterrupt:
print('\n')
# Verify dependencies are installed with:
sudo pip3 install pyobjc-framework-Quartz, sudo pip3 install pyobjc-core
# then
sudo pip3 install pyobjc
# If the GUI automation gets out of hand, use:
# command-shift-option-q
# To pause the program: