I am trying to assign a variable a string and date value using exec(). I initially was getting a
SyntaxError: unqualified exec is not allowed in function 'getinfo' it contains a nested function with free variables when I tried to execute it like exec('person_{} = entries[index].who.encode("UTF8")' ).format(number_of_credits)
I have tried everything, it works without any errors when I use globals(), locals() but doesnt produce an ouput.
def getinfo(client, ticket):
if ticket.number ==3:
stuck = list(response.ticket.approval.approvers)
for i in stuck:
if "status: CURRENT_PENDING" in str(i):
stuck_on= str(i).splitlines()
stuck_on= stuck_on[1]
stuck_on= stuck_on.replace('assignees: ', "")
stuck_on= stuck_on.replace('"', "")
entries= response.ticket.core.worklog.entries
number_of_approvals = 0
index =-1
for e in entries:
index+=1
if 'Credit' in e.comment:
number_of_credits +=1
credits= 'person_{}'.format(number_of_credits)
credit_date = 'person_date_{}'.format(number_of_credits)
_locals = locals()
exec('credits = entries[index].who.encode("UTF8")', globals(), _locals)
exec('credit_date = time.strftime("%Y-%m-%d", time.localtime(int(str(entries[index].when)[:-3])))', globals(), _locals)
for as many persons and dates there are I would like to assign values to them. I.e person_1= entries[index].who.encode("UTF8") which evaluates to 'Bob' and person_date_1 = '2009-01-08'