0

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'

TH14
  • 622
  • 10
  • 24
  • 1
    what is the point of using `exec` there? – Netwave Mar 07 '19 at 12:11
  • each time 'Credit' is found in comment, i want the variable person_{} assigned the value of entries[index].who.encode("UTF8") which is a name and the same for the date found in that comment. – TH14 Mar 07 '19 at 12:20
  • Possible duplicate of [generating variable names on fly in python](https://stackoverflow.com/questions/4010840/generating-variable-names-on-fly-in-python) – snakecharmerb Mar 08 '19 at 08:38

0 Answers0