for key in keys:
self.lookup(key, create=True).fix()
+ def remove(self, parsed):
+ """Given a list [(varname, `Operation`)] as returned by `parse` or `parse_file`,
+ removes the operations from the respective variables config tree.
+ Variables/operations not present int the tree raise ValueError.
+ """
+ for vname, o in parsed:
+ v = self.lookup(vname, create = True)
+ v.remove_operation(o)
+
def parse(self, s, source=None, level=0):
- """Parse `s` (stream/string) into the tree, see `moe.confparser.ConfigParser` for details."""
+ """Parse `s` (stream/string) into the tree, see `moe.confparser.ConfigParser` for details.
+ Returns list of parset operations: [(varname, `Operation`)]"""
import moe.config_parser
p = moe.config_parser.ConfigParser(s, self, source=source, level=level)
- p.parse()
+ return p.parse()
def parse_file(self, filename, desc=None, level=0):
"""Parse an utf-8 file into the tree, see `moe.confparser.ConfigParser` for details.
with open(filename, 'rt') as f:
if desc:
filename += " <" + desc + ">"
- self.parse(f, source=filename, level=level)
+ return self.parse(f, source=filename, level=level)
class ConfigElem(object):
def variables(self):
"Return a set of variables used in the expressions"
- return set(sum([ list(op.expression.variables()) for op in self.operations ], []))
+ if not self.operations:
+ return set([])
+ return set.union(*[ op.expression.variables() for op in self.operations ])
def fix(self):
"""
self.invalidate()
# Remove the operation
self.operations.remove(operation)
- # Remove dependencies on variables unused in other operations
+ # Remove dependencies on variables unused in other defining operations
vs = self.variables()
for v in operation.expression.variables():
if v not in vs:
self.exprlist[i] = unicode(e, 'ascii')
def variables(self):
- "Return an iterator of variables user in the expression"
- return itertools.ifilter(lambda e: isinstance(e, ConfigVar), self.exprlist)
+ "Return a set of variables used in the expression"
+ return set([e for e in self.exprlist if isinstance(e, ConfigVar)])
def __str__(self):
return self.original