This has got to be the weirdest and most frustrating bug I have ever come across.
My code is very simple. It uses eval() to turn the contents of a text file into a dictionary, and assigns that dictionary to a variable. It looks like this:
try:
with open("/ROBOT2/settings.txt", 'r') as file:
settings = eval(file.read())
print(settings)
except FileNotFoundError:
settings = {
'Exit': None,
'Turn Reduction': 10,
'Start Phase': 'STRT',
'Smart Focus': False,
'Smart Shutoff': False,
'Fast Test': False,
}
My text file has this data:
{‘Exit’: None, ‘Start Phase’: ‘STRT’, ‘Turn Reduction’: 0, ‘Smart Focus’: False, ‘Smart Shutoff’: True, ‘Fast Test’: False}
It then prints and uses this:
{‘Start Phase’: ‘STRT’, ‘Smart Focus’: False, ‘Smart Shutoff’: True, ‘Turn Reduction’: 0, ‘Fast Test’: False, ‘Exit’: None}
Suddenly everything is in a random order, and my code relies on this dictionary being ordered.
When I take the SD card back to my computer, the contents in the file are scrambled just as it prints.
This also happens with every other evaluate command that I have that reads a file.
I tried switching my SD card to a smaller one, and I also tried changing the code. Even when the main part reads as
try:
with open("/ROBOT2/settings.txt", 'r') as file:
#settings = eval(file.read())
settings = {'Exit': None}
exec(file.read())
print(settings)
and the file says
settings = {‘Exit’: None, ‘Start Phase’: ‘STRT’, ‘Turn Reduction’: 0, ‘Smart Focus’: False, ‘Smart Shutoff’: True, ‘Fast Test’: False,}
The effect is still the same
So, how do I fix this? Do I try another SD card? Am I reading files wrong? Does eval and exec not work well? What’s going on?