Skip to content

Instantly share code, notes, and snippets.

@billinkc
Created December 21, 2022 19:18
Show Gist options
  • Save billinkc/bdad2c3209573dfe3e4a8d1d0b51e365 to your computer and use it in GitHub Desktop.
Save billinkc/bdad2c3209573dfe3e4a8d1d0b51e365 to your computer and use it in GitHub Desktop.
Debugging eval calls which I've never done
class foo:
name="string"
num=-1
math="string2"
def __init__(self,name,num,math):
self.name=name
self.num=num
self.math=math
def __str__(self):
return f"{self.name}: {self.num}|{self.math}"
def __repr__(self):
return f"{self.name}: {self.num}|{self.math}"
content = ['steve:1:blee']
foolist=[]
fooval=-1
foostr=""
lestring = "string"
for line in content:
fooname=line.split(":")[0]
if line.split(":")[1].isnumeric():
fooval=int(line.split(":")[1])
else:
foostr=line.split(":")[1]
if fooval != -1:
if (not False):
wat = foo(fooname,fooval,"string")
print(str(wat))
print(type(wat))
foolist.append(wat)
if (False):
zum = ("foolist.append(foo(%s,%d,%s))" % (fooname,fooval,"string"))
print(zum)
print(type(zum))
print(fooname)
print(fooval)
exec("foolist.append(foo(%s,%d,%s))" % ("fooname",fooval,'"lestring"'))
else:
eval("foolist.append(foo(%s,%d,%s))" % (fooname,-1,fooval))
print(type(foolist[0]))
print(foolist[0])
comment = """
OUTPUT:
Traceback (most recent call last):
File "advent21.py", line 42, in
eval("foolist.append(foo(%s,%d,%s))" % (fooname,-2,fooval))
File "", line 1, in
NameError: name 'steve' is not defined
IDEAL END RESULT:
foolist[0] is a foo-class object named "steve"
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment