Fredovsky
Fredovsky

Reputation: 387

Python variable value from separate file?

I have a Python script that runs 24hrs a day.

A module from this script is using variables values that I wish to change from time to time, without having to stop the script, edit the module file, then launch the script again (I need to avoid interruptions as much as I can).

I thought about storing the variables in a separate file, and the module would, when needed, fetch the new values from the file and use them.

Pickle seemed a solution but is not human readable and therefore not easily changeable. Maybe a JSON file, or another .py file I import over again ?

Another advantage of doing so, for me, is that in case of interruption (eg. server restart), I can resume the script with the latest variable values if I load them from a separate file.

Is there a recommended way of doing such things ?

Something along the lines :

# variables file:
variable1 = 10
variable2 = 25


# main file:
while True:
    import variables
    print('Sum:', str(variable1+variable2))
    time.sleep(60)

Upvotes: 2

Views: 178

Answers (1)

C14L
C14L

Reputation: 12548

An easy way to maintain a text file with variables would be the YAML format. This answer explains how to use it, basically:

import yaml
stream = open("vars.yaml", "r")
docs = yaml.load_all(stream)

If you have more than a few variables, it may be good to check the file descriptor to see if the file was recently updated, and only re-load variables when there was a change in the file.

import os
last_updated = os.path.getmtime('vars.yaml')

Finally, since you want avoid interruption of the script, it may be good to have the script catch any errors in the YAML file and warn the user, instead of just throwing an exception and die. But also remember that "errors should never pass silently". What is the best approach here would depend on your use-case.

Upvotes: 2

Related Questions