1

Right now i am working with a file .txt with this information:

["corrector", "Enabled"]
["Inteligencia", "Enabled"]

Then in my python program it loads that data at the very beggining, this way:

for line in open("menu.txt", 'r'):
  retrieved = json.loads(line)
  if retrieved[0] == "corrector":
    corrector = retrieved[1]
  if retrieved[0] == "Inteligencia":
    Inteligencia = retrieved[1]

So far it works perfect, however as this is for a chat bot, i want to make possible to change the value of that variables directly from the chat, and i tried this code when i call "!Enable corrector" from the chat.

  if corrector == "Enabled":
    room.message("ERROR: Already Enabled")
  else:
    data = []
    with open('menu.txt', 'r+') as f:
      for line in f:
        data_line = json.loads(line)
        if data_line[0] == "corrector": 
          data_line[1] = "Enabled"
        data.append(data_line)
      f.seek(0)
      f.writelines(["%s\n" % json.dumps(i) for i in data])
      f.truncate()
    room.message("corrector enabled")

That also works, and if i open the .txt file i can see the value it's already changed. The real problem is that python didn't seem to accept that i changed a variable, and it still thinks it's "disabled" while it's already "enabled". It won't read the variable as "enabled" until i restart the program.

I was wondering if there is a refresh option for variables or a workaround to change the value of a variables on the fly and make the effect lasts without a restart.

Saelyth
  • 1,694
  • 2
  • 25
  • 42
  • possible duplicate of [dynamic variable](http://stackoverflow.com/questions/10963804/dynamic-variable) – Marcin Aug 27 '13 at 20:22
  • 3
    Variables aren't tied to where they came from; there seems to be a fundamental misunderstanding of what a "variable" is. You have to read the file again. – Izkata Aug 27 '13 at 20:22
  • possible duplicate of [How to create an unknown amount of variables in python](http://stackoverflow.com/questions/17685199/how-to-create-an-unknown-amount-of-variables-in-python) – abarnert Aug 27 '13 at 20:58
  • There was another dup just a couple hours ago. I gathered up 11 independent ones while writing [this blog post](http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html). – abarnert Aug 27 '13 at 20:58

1 Answers1

2

change the value of a variables on the fly

This code changes the value of a variable on the fly:

a = 1
a = 2

Your question suggests that you want to be able to look up a value by a calculated name. The solution is to use a dict:

mydict = {'corrector':0}
mydict['corrector'] = 1

If you want to change the values in the file, you'll need to write out a new file based on the data you have. It looks like you're loading json, so the json module will help you out with that.

Marcin
  • 48,559
  • 18
  • 128
  • 201
  • yeah, i am using json. So the problem is that a variable can't update their information from a list but it can from a dict? That's what i understood from your answer. – Saelyth Aug 27 '13 at 20:29
  • @Saelyth "a variable can't update their information from a list but it can from a dict" does not mean anything. A variable doesn't update its information from anything. A variable gets a new value through an assignment statement. That's it. – Marcin Aug 27 '13 at 20:31
  • Ah ok, sorry about how noob i can be at this. I'm just learning python as my first programming language and i'm mostly learning from the info that i can get from google or this website. Thanks for the answer, i'll try to use dicts from now on (well, i'll have to learn as i didn't know what a dict was 10 minutes ago). Thanks! – Saelyth Aug 27 '13 at 20:34
  • @Saelyth I strongly recommend that you follow a tutorial. – Marcin Aug 27 '13 at 20:37