Python Automation GUI2: common functions

1.Write to file:

mainfile_path = os.path.join(workspace_path,project_name+‘/src/main.c’)

mainfile = open(mainfile_path,‘w’)

for element in newcontent:

    mainfile.write(element)

mainfile.close()

2. read a file:

          infomation_input = open(log_file, ‘r’).read()

3.Press keys:

yautogui.hotkey(‘ctrl’,‘3’)  # press more than 1 keyboard

yautogui.hotkey(‘ctrl’,‘a’)  # press more than 1 keyboard

pyautogui.press(‘enter’presses 1pause 1) # press one key many times

4.Type text:

pyautogui.typewrite(‘close part’)

5.Clipboard

return_text = clipboard.paste()

6.synchronize button status

swtwindow = app.SWT_Window0

swtwindow.Wait(‘ready’)

toolbarwindow = swtwindow[u’74’]

toolbar_button = toolbarwindow.Button(3)

count = 0;

while toolbar_button.IsEnabled() != True:

    toolbar_button = toolbarwindow.Button(3)

    time.sleep(1)

    count = count+1

    print ‘waiting …%s second’ %count

    if count > 120:

        break

7. check path is a directory or not.

os.path.isdir(os.path.join(workspace_path,‘project2’))

8 Check if file is existing:

if os.path.isfile(‘myfile.txt’):

9. split string 

folder_path = s32ds_path.split(\\eclipse\\s32ds.exe’)[0]

10. Input loop:

inputname = 0

while True and ((inputname < 1or (inputname > len(user))):

    try:

        inputname = int(input(“Please select a User to run ?: “))

    except:

        print(“Please type a integer from 1 to %s !” len(user))

        continue

    else:

        if inputname>= and inputname <= len(user):

            break

            else:

                        print(“Please type a integer from 1 to %s !” len(user))

11. Get all things in folder:

all_sub_file_folder = os.listdir(folder_SDK)

12. print string with format function

    userlist = []

    for item in user:

        count += 1

        userlist.append(‘%s: %s’ % (count, item))

    for i in range(len(userlist)/3):

        test = (userlist[i],userlist[i+len(userlist)/3],userlist[i+2*len(userlist)/3])

        print ‘{0:<20} {1:<20} {2:<20}’.format(*test)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s