views:

2796

answers:

6

Referring on this question, i have a similar -but not the same- problem..

On my way, i'll have some text file, structured like:

var_a: 'home'
var_b: 'car'
var_c: 15.5

And i need that python read the file and then create a variable named var_a with value 'home', and so on.

Example:

#python stuff over here
getVarFromFile(filename) #this is the function that im lookin for
print var_b
#output: car, as string
print var_c
#output 15.5, as number.

Is this possible, i mean, even keep the var type?

Notice that i have the full freedom to the text file structure, i can use the format i like if the one i proposed isn't the best.

EDIT: the ConfigParser can be a solution, but i dont like it so much, becose in my script i'll have then to refer to the variables in the file with

config.get("set", "var_name")

But what i'll love is to refer to the variable direclty, as i declared it in the python script..

There is a way to impoer the file as a python dictionary?

Oh, last thing, keep in mind that i dont know exactly how many variables would i have in the text file

Edit 2: i'm very interessing to the stephan json solution, becose in that way the text file could be readed simply with others languages (php, then via ajax javascript, for example), but i fail in something while acting that solution:

#for the example, i dont load the file but create a var with the supposed file content
file_content = "'var_a': 4, 'var_b': 'a string'"
mydict = dict(file_content)
#Error: ValueError: dictionary update sequence element #0 has length 1; 2 is required
file_content_2 = "{'var_a': 4, 'var_b': 'a string'}"
mydict_2 = dict(json.dump(file_content_2, True))
#Error:
#Traceback (most recent call last):
#File "<pyshell#5>", line 1, in <module>
#mydict_2 = dict(json.dump(file_content_2, True))
#File "C:\Python26\lib\json\__init__.py", line 181, in dump
#fp.write(chunk)
#AttributeError: 'bool' object has no attribute 'write'

In what kind of issues can i fall with the Json format? And, how can i read a json array in a text file, and transform it in a python dict?

p.s: i dont like the solution using .py files, i'll prefer .txt, .inc, .whatever is not restrictive to one language

A: 

You can treat your text file as a python module and load it dynamically:

import imp
imp.load_source( name, pathname[, file])

Example:

// mydata.txt
var1 = 'hi'
var2 = 'how are you?'
var3 = { 1:'elem1', 2:'elem2' }
//...

// In your script file
def getVarFromFile(filename):
    import imp
    f = open(filename)
    global data
    data = imp.load_source('data', '', f)
    f.close()

getVarFromFile('c:/mydata.txt'):
print data.var1
print data.var2
print data.var3
...
Nick D
+5  A: 

Use ConfigParser.

Your config:

[myvars]
var_a: 'home'
var_b: 'car'
var_c: 15.5

Your python code:

import ConfigParser

config = ConfigParser.ConfigParser()
config.read("config.ini")
var_a = config.get("myvars", "var_a")
var_b = config.get("myvars", "var_b")
var_c = config.get("myvars", "var_c")
Igor Krivokon
Just edited - this way is fine, but if possible im lookin for another one who would let me using the variables in a usefull way
DaNieL
I've updated the answer to show you how to use these as variables.
Igor Krivokon
Or you could take the Bunch class from http://code.activestate.com/recipes/52308/ and hack it to be recursive, so you could refer to config.myvars.var_a directly.
Jouni K. Seppänen
+2  A: 

Load your file with JSON or PyYAML into a dictionary the_dict (see doc for JSON or PyYAML for this step, both can store data type) and add the dictionary to your globals dictionary, e.g. using globals().update(the_dict).

If you want it in a local dictionary instead (e.g. inside a function), you can do it like this:

for x in the_dict.items():
    exec('%s=%s' % x)

as long as it is safe to use exec. If not, you can use the dictionary directly.

stephan
Can you please show a basilar example?
DaNieL
A shorter idiom is globals().update(the_dict) or locals().update(the_dict).
Jouni K. Seppänen
Thanks for pointing this out for globals(). Amended my answer. locals() is however read-only (see http://docs.python.org/library/functions.html#locals).
stephan
It's not really read-only (I have used it myself) but apparently there are circumstances in which writing to it doesn't work. Thanks for pointing this out.
Jouni K. Seppänen
+5  A: 

But what i'll love is to refer to the variable direclty, as i declared it in the python script..

Assuming you're happy to change your syntax slightly, just use python and import the "config" module.

# myconfig.py:

var_a = 'home'
var_b = 'car'
var_c = 15.5

Then do

from myconfig import *

And you can reference them by name in your current context.

yangyang
+1, This is absolutely THE best way to do config files. No need to grow your own config syntax, config parsers, config loaders etc. when you can just re-use python's core parts!
Simon
+1 yes, as long as you can trust your config file and don't need portability
stephan
Convenience and flexibility ++ vs. security -- : in some situations it's great, in others... less so.
mavnn
I have to discard this solution becose i dont want to restric the file to python only, i think is better to use a generic file format (.txt, .inc, .whatever) that can be accessed by others languages too (with all the safe measure that this solution will mean).Anyway i'll keep in mind this way for the future
DaNieL
+1  A: 

What you want appear to want is the following, but this is NOT RECOMMENDED:

>>> for line in open('dangerous.txt'):
...     exec('%s = %s' % tuple(line.split(':', 1)))
... 
>>> var_a
'home'

This creates somewhat similar behavior to PHP's register_globals and hence has the same security issues. Additionally, the use of exec that I showed allows arbitrary code execution. Only use this if you are absolutely sure that the contents of the text file can be trusted under all circumstances.

You should really consider binding the variables not to the local scope, but to an object, and use a library that parses the file contents such that no code is executed. So: go with any of the other solutions provided here.

(Please note: I added this answer not as a solution, but as an explicit non-solution.)

Stephan202
Yes, thanks for the raccomandation, i've banned the exec function both from my php and python scripts time ago ;)
DaNieL
+2  A: 

How reliable is your format? If the seperator is always exactly ': ', the following works. If not, a comparatively simple regex should do the job.

As long as you're working with fairly simple variable types, Python's eval function makes persisting variables to files surprisingly easy.

(The below gives you a dictionary, btw, which you mentioned was one of your prefered solutions).

def read_config(filename):
    f = open(filename)
    config_dict = {}
    for lines in f:
        items = lines.split(': ', 1)
        config_dict[items[0]] = eval(items[1])
    return config_dict
mavnn
I can wrote the text file exaclty as i want, the format var: value is just for example, if others formats are better, fell free to suggest
DaNieL
As long as it's consistant, and the value is of the form generated by repr(variable) it doesn't really matter for this method. Just pick a seperator that will never be part of the variable name and use that in the split method.
mavnn