Jump to content

Usefull Python code


Recommended Posts

Adding this to your script will display the time it took to run.

import time
startTime = time.time()

#your code here

executionTime = (time.time() - startTime)
vs.AlrtDialog('Execution time in seconds: ' + str(executionTime))

 

 

Use this to use a list to elimimate options from another list

OmitList =['name1','name2','name3', 'name4','name5'] #replace name# with names of items to skip

for List2 in reversed(range(1,List2+1)): #List2 is the list of items you wish to process. Remove Reversed() to step through list in order.
	if any(item == vs.GetName(List2) for item in OmitList): #if name of List2 item matches name in OmitList it will result in True 
		continue #skip rest of script for this item and continue to next item in List2
	#Insert script to execute if name is not in OmitList.  

 

Edited by The Hamma
  • Like 2
Link to comment
  • 10 months later...

For the second one, you're not making use of the crazy features built into Python to write really compact code!

 

Namely, you can use a list comprehension to get just your second list without the omitted items, or you can just simply use the "in" test while looping through the items in your second list!

 

 

omitList =['name2', 'name3', 'name4'] 

listToFilter = ['name1', 'name2', 'name3', 'name4', 'name5']

#either use a list comprehension to get the filtered list
filteredList = [name for name in listToFilter if name not in omitList]

# or step through the list and use "in" to test each name
for name in listToFilter:
    if name in omitList:
        continue
    #rest of your function
  • Like 1
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...