2

I have a module that needs to update new variable values from the web, about once a week. I could place those variable values in a file & load those values on startup. Or, a simpler solution would be to simply auto-update the code.

Is this possible in Python?

Something like this...

def self_updating_module_template():
    dynamic_var1 = {'dynamic var1'} # some kind of place holder tag
    dynamic_var2 = {'dynamic var2'} # some kind of place holder tag

    return

def self_updating_module():
    dynamic_var1 = 'old data' 
    dynamic_var2 = 'old data'

    return

def updater():
    new_data_from_web = ''
    new_dynamic_var1 = new_data_from_web  # Makes API call.  gets values.
    new_dynamic_var2 = new_data_from_web

    # loads self_updating_module_template

    dynamic_var1 = new_dynamic_var1
    dynamic_var2 = new_dynamic_var2

    # replace module place holders with new values.
    # overwrite self_updating_module.py.

    return
3
  • Do they always change at the same time? What are they used for? Where do they need to be available within your application, and when? This is far too broad to address, but the first thing I'd look at is whether, when a request is made, you could check if the data is more than a week old and, if so, fetch the new data before handling the request. I do something similar for config here. Commented Dec 18, 2016 at 13:51
  • It is very uncommon to let the code be dynamically changed by data. You want to seperate the data from the running code. If there is code that can be imported dynamically and then run, you could look into Subprocesses, let the program invoke a seperate program. Having a module be dynamically rewritten, you would need to run the code again, so invoke the program again after the write. Only then will the rewritten hardcoded values be assigned to the actual variables in the python run time. This is quite an exotic way to do things though. Commented Dec 18, 2016 at 14:12
  • The code should not be updated. The value of some variables might. Best way to do so would be to use a counter that runs evreytime the document is loaded and if its been 10 days then you could query a server for new values. But seriously i would just call the api everytime the document is loaded and get up to date values all the time. Its hard to tell what your trying to achieve but that is what i would do. Unless the data is very heavy and you want to save money. Commented Dec 18, 2016 at 22:09

1 Answer 1

1

I would recommend that you use configparser and a set of default values located in an ini-style file.

The ConfigParser class implements a basic configuration file parser language which provides a structure similar to what you would find on Microsoft Windows INI files. You can use this to write Python programs which can be customized by end users easily.

Whenever the configuration values are updated from the web api endpoint, configparser also lets us write those back out to the configuration file. That said, be careful! The reason that most people recommend that configuration files be included at build/deploy time and not at run time is for security/stability. You have to lock down the endpoint that allows updates to your running configuration in production and have some way to verify any configuration value updates before they are retrieved by your application:

import configparser

filename = 'config.ini'

def load_config():
    config = configparser.ConfigParser()
    config.read(filename)

    if 'WEB_DATA' not in config:
        config['WEB_DATA'] = {'dynamic_var1': 'dynamic var1', # some kind of place holder tag
                              'dynamic_var2': 'dynamic var2'} # some kind of place holder tag
    return config



def update_config(config):
    new_data_from_web = ''
    new_dynamic_var1 = new_data_from_web  # Makes API call.  gets values.
    new_dynamic_var2 = new_data_from_web

    config['WEB_DATA']['dynamic_var1'] = new_dynamic_var1
    config['WEB_DATA']['dynamic_var2'] = new_dynamic_var2

def save_config(config):
    with open(filename, 'w') as configfile:
        config.write(configfile)

Example usage::

# Load the configuration
config = load_config()
# Get new data from the web
update_config(config)
# Save the newly updated configuration back to the file
save_config(config)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.