From 572eeee024707ea744e3ab0f56987ac7a32dafda Mon Sep 17 00:00:00 2001 From: "Adam Ginsburg (keflavich)" Date: Thu, 23 Oct 2014 14:11:28 +0200 Subject: [PATCH] replace eval with ast.literal_eval and account for cases of inf, nan in the tree --- astrodendro/io/util.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/astrodendro/io/util.py b/astrodendro/io/util.py index 28d2d3d..873ca16 100644 --- a/astrodendro/io/util.py +++ b/astrodendro/io/util.py @@ -1,9 +1,24 @@ import numpy as np +import ast +from numpy import inf,nan from .. import six from astropy.utils.console import ProgressBar from astropy import log +def _fix_string(string): + """ + Given a string that may contain inf or nan, replace inf and nan with None + so that it can be parsed by ast.literal_eval + """ + return string.replace("inf","None").replace("nan","None") + +def _eval_string(string): + """ + Parse a string into a dictionary using ast.literal_eval + """ + return ast.literal_eval(_fix_string(string)) + def parse_newick(string): @@ -51,7 +66,7 @@ def parse_newick(string): branch_id = int(branch_id) # Add branch definition to overall definition - items[branch_id] = eval("{%s}" % string[start + 1:end]) + items[branch_id] = _eval_string("{%s}" % string[start + 1:end]) # Remove branch definition from string string = string[:start] + string[end + 1:]