Datetime.strptime causing infinite loop

I’m not sure what causes this, but for some reason datetime.strptime is causing this to create an infinite loop. If I do not comment out the strptime line, it will run with no issue

image

With line commented out, first line prints, then nothing else. No error thrown

Infninite loops running on the gateway

It’s very weird, I have never had issue working with strptime before.
Here is a copy of the script:

#Get results of query
oldData = system.db.runPrepQuery(query, [])
oldData = system.dataset.toPyDataSet(oldData)

#Will use historical data for comparison later	
histData = []

#Loop through results of query to build dataset
for row in range(oldData.rowCount):
	
	status = oldData.getValueAt(row, 'Status')
	timestamp = oldData.getValueAt(row, 'Timestamp')
	timestamp = str(timestamp)

	#Manual entries do not contain milliseconds
	if '.' in timestamp: 
		timestamp = timestamp[0:timestamp.find('.')]

	#Print before loop to see if anything is weird
	system.perspective.print(timestamp)
	
	#Causes infinite loop >:(
	timestamp = datetime.datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S')
	
	histData.append({"Timestamp":timestamp, "Status":status})

Where is this script being called from? A property change event? Most infinite loops I’ve run into (or caused) have been circular property change events. Property Change Event A → changes some property that triggers Property Change Event B → changes some property that triggers Property Change Event A and so on

Though looking at your script, I don’t see how that would be the case. What does your script end up doing with histData? Is there more to the script? If so post it.

Just use the system.date.* functions instead?

Also couple of questions:

  1. Why use system.db.runPrepQuery() with no parameters? Just use system.db.runQuery() and the results will be returned as a PyDataSet removing the need for the conversion.
  2. Why convert oldData to a PyDataSet and then not use it that way?
#Get results of query
oldData = sysetm.db.runQuery(query)

#Will use historical data for comparison later, List of Dictionaries
histData = []

for row in oldData:
    system.perspective.print(system.date.format(row['Timestamp'])
    timestamp = system.date.addMillis(row['Timestamp'],-1 * system.date.getMillis(row['Timestamp']))
    histData.append({'Timestamp':timestamp,'Status':row['Status']})

That being said, it’s obvious that you haven’t shown the entire script, as there is no Import statement and you would need at least that. I suspect that it looks something like import datetime if you changed that to from datetime import datetime you could get rid of the redundant datetime.datetime.* syntax.

I’m also assuming that there is script following this loop, as it doesn’t do anything aside from printing the timestamp. Is it possible that the query is only returning 1 row? What is supposed to happen after this loop?

It looks like this is in an onStartup script on the view Embed/StatusDetail, I’m not sure that is the best place for it, but then I also don’t know what it’s trying to accomplish.

  1. I have an excel vba that generates script transforms for me to save me some typing. This defaults to prep query, its not a big deal really (doesn’t affect my issue)

  2. Yes I use import datetime and yes there are other ways to do this. This does not affect my issue either

  3. I removed all the code after this because its irrelevant. It gets stuck on that line that I mentioned. I did mess up my description. I meant that it runs fine with a comment out through the strptime command, yet with no comment it gets stuck there. Thats why it prints 4 lines (there are 4 rows in the data), yet only prints one with no comment on that line.

  4. I need this on a startup script because it is generated on startup and I need a one time run on startup. Explaining everything would be too detailed and irrelevant, every code after the one mentioned does not run

I extracted the code to a button to test and got the same results from a button push

I deleted all the code after this, it gets stuck on that strptme command and nothing will run after

Get a thread dump from whatever scope you’re executing from while in this “infinite loop”.

Okay, I’ll agree, won’t effect the outcome of the script.

There is no reason from this script that I can see that datetime.strptime() should not work, from what I can tell you have used the correct format string, and timestamp is a string which should have a matching format (Based on the print in the provided screen shot).

Perhaps wrap it in a try...except to see if it might through an error there?