Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ast.literal_eval for variables in python?

Suppose I have a file example.py: import example

VVV = 2

DictionaryNameB = {
    'a'                   : VVV,
    'bb'                  : 'SomethingB',
    'c'                   : False,
    'ccc'                 : None,
    'dddd'                : 'true',
    'eeeee'               : 0.123456,
    'f'                   : 2,
    'h'                   : [1,2,3]
}

I wrote a function that uses ast.literal_eval():

def getDicFromFile(self, dic_name):
    with open( 'example.py' ) as f:
        file_data = f.read()
    match = re.findall('%s[^{]+\{[^\}]+\}' % dic_name, file_data, re.MULTILINE)[0]
    # print(match)
    dicObject = ast.literal_eval(match[len(dic_name)+3:])
    return dicObject

I got the error raise ValueError('malformed string') ; ValueError: malformed string

I understand that ast.literal_eval() can't decode the variable VVV, is there another way to do it?

like image 643
Omer Talmi Avatar asked Jul 17 '26 15:07

Omer Talmi


1 Answers

You could use asteval, a library that builds on the ast parse tree to execute limited statements. It'll handle your example out of the box:

from asteval import Interpreter

aeval = Interpreter()
aeval(file_data)
dicObject = aeval.symtable['DictionaryNameB']

Or, you could just import the file:

from importlib import import_module
module = import_module('example')
dicObject = module.DictionaryNameB

asteval allows for quite a wide number of Python constructs. You could compare the Python Abstract Grammar with what handlers the aeval.node_handlers mapping registers, and simply delete any you don't need. You could remove function definitions and calling, looping, binary operations (binop) and exception handling, for example.

like image 149
Martijn Pieters Avatar answered Jul 19 '26 06:07

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!