Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Update a Module (http://www.programmingforums.org/showthread.php?t=9586)

Dietrich Apr 29th, 2006 8:58 AM

Update a Module
 
Is there a way to update a variable in a module from the program that imported this module? So other programs importing this module later can also use the update.

Sane Apr 29th, 2006 9:13 AM

Make the variable stored in a data file? Then the module will open the data file everytime it is loaded/called to use that variable?

Arevos Apr 29th, 2006 1:19 PM

You can use the shelve or pickle modules to maintain persistant variables. The __file__ variable contains the filepath of the currently executing module. Combine the two, and you could have something like:
:

import shelve
import os

# Open a shelve file in the same directory as the module
filename = os.path.join(os.path.dirname(__file__), "data.shelf")
shelf = shelve.open(filename)

# Store a variable in the file:
shelf["something"] = 10

# Retrieve a variable from the file:
print shelf["foobar"]

# Delete a variable
del shelf["old"]



All times are GMT -5. The time now is 11:17 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC