diff --git a/bindings/python/cconfigspace/expression.py b/bindings/python/cconfigspace/expression.py index 24bee6dc01ae429f53a9fa1145d7192a6740c8f4..b88c698ff9d5b2d72e4beebea8d706e25af175f9 100644 --- a/bindings/python/cconfigspace/expression.py +++ b/bindings/python/cconfigspace/expression.py @@ -216,6 +216,12 @@ class Literal(Expression): v = self.value if isinstance(v, str): return repr(v) + elif v is None: + return "none" + elif v is True: + return "true" + elif v is False: + return "false" else: return "{}".format(v) diff --git a/bindings/python/cconfigspace/expression_parser.py b/bindings/python/cconfigspace/expression_parser.py index f61624359a75f846fd5f941b91d077937aad545b..1a2fc28bc4d6261a3d25214ba686bc1fe8722e07 100644 --- a/bindings/python/cconfigspace/expression_parser.py +++ b/bindings/python/cconfigspace/expression_parser.py @@ -21,15 +21,17 @@ list: '[' list_item ']' list_item: list_item ',' value | value; value: none - | boolean + | btrue + | bfalse | string | identifier | integer | float; terminals -none: /None/ {prefer}; -boolean: /True|False/ {prefer}; +none: /none/ {prefer}; +btrue: /true/ {prefer}; +bfalse: /false/ {prefer}; identifier: /[a-zA-Z_][a-zA-Z_0-9]*/; string: /"([^\0\t\n\r\f"\\]|\\[0tnrf"\\])+"|'([^\0\t\n\r\f'\\]|\\[0tnrf'\\])+'/; integer: /-?[0-9]+/; @@ -53,8 +55,9 @@ _actions["list_item"] = [ lambda _, n: n[0] + [n[2]], lambda _, n: [n[0]] ] -_actions["none"] = lambda _, value: Literal(value = eval(value)) -_actions["boolean"] = lambda _, value: Literal(value = eval(value)) +_actions["none"] = lambda _, value: Literal(value = None) +_actions["btrue"] = lambda _, value: Literal(value = True) +_actions["bfalse"] = lambda _, value: Literal(value = False) _actions["identifier"] = lambda p, value: Variable(hyperparameter = p.extra.hyperparameter_by_name(value)) _actions["string"] = lambda _, value: Literal(value = eval(value)) _actions["float"] = lambda _, value: Literal(value = float(value)) diff --git a/bindings/python/test/test_expression.py b/bindings/python/test/test_expression.py index 86670dbbe3224419c3899c8fd9ca8e6853ae34c1..923f59f5f0e5f2dd0b4da89b6afec2ccf14f4103 100644 --- a/bindings/python/test/test_expression.py +++ b/bindings/python/test/test_expression.py @@ -32,7 +32,7 @@ class TestExpression(unittest.TestCase): e = ccs.Literal(value = 15) self.assertEqual( "15" , str(e) ) e = ccs.Literal(value = None) - self.assertEqual( "None" , str(e) ) + self.assertEqual( "none" , str(e) ) def test_variable(self): h = ccs.NumericalHyperparameter() @@ -51,12 +51,12 @@ class TestExpression(unittest.TestCase): def test_unary(self): e = ccs.Expression.unary(t = ccs.NOT, node = True) - self.assertEqual( "!True", str(e) ) + self.assertEqual( "!true", str(e) ) self.assertFalse( e.eval() ) def test_binary(self): e = ccs.Expression.binary(t = ccs.OR, left = True, right = False) - self.assertEqual( "True || False", str(e) ) + self.assertEqual( "true || false", str(e) ) self.assertTrue( e.eval() ) if __name__ == '__main__': diff --git a/bindings/python/test/test_expression_parser.py b/bindings/python/test/test_expression_parser.py index 475a1c44c6f644c27e60d1947e04f3b3337d9aa1..1952c9dbd12e9d9846a7b47d98d9e35f3379fde7 100644 --- a/bindings/python/test/test_expression_parser.py +++ b/bindings/python/test/test_expression_parser.py @@ -39,23 +39,23 @@ class TestExpressionParser(unittest.TestCase): self.assertFalse( res.eval() ) def test_boolean(self): - exp = "True" + exp = "true" res = ccs.ccs_parser.parse(exp) self.assertIsInstance( res, ccs.Literal ) self.assertEqual( True, res.eval() ) - self.assertEqual( "True", res.__str__() ) - exp = "False" + self.assertEqual( "true", res.__str__() ) + exp = "false" res = ccs.ccs_parser.parse(exp) self.assertIsInstance( res, ccs.Literal ) self.assertEqual( False, res.eval() ) - self.assertEqual( "False", res.__str__() ) + self.assertEqual( "false", res.__str__() ) def test_none(self): - exp = "None" + exp = "none" res = ccs.ccs_parser.parse(exp) self.assertIsInstance( res, ccs.Literal ) self.assertIsNone( res.eval() ) - self.assertEqual( "None", res.__str__() ) + self.assertEqual( "none", res.__str__() ) if __name__ == '__main__': diff --git a/bindings/ruby/lib/cconfigspace/expression.rb b/bindings/ruby/lib/cconfigspace/expression.rb index fa6cc1bebe53dcb0061f8cb9a1ba03e4c4c33452..3e797dceca440413d6da81f1beca48e6ef71102c 100644 --- a/bindings/ruby/lib/cconfigspace/expression.rb +++ b/bindings/ruby/lib/cconfigspace/expression.rb @@ -206,7 +206,9 @@ module CCS def to_s case value - when nil, String + when nil + "none" + when String value.inspect else value.to_s diff --git a/bindings/ruby/lib/cconfigspace/expression_parser.rb b/bindings/ruby/lib/cconfigspace/expression_parser.rb index 53b4fb6b2709ddbc4637005174e86e0271404517..6452f7352e7af4446cd7d2fb9880b8b725353e2e 100644 --- a/bindings/ruby/lib/cconfigspace/expression_parser.rb +++ b/bindings/ruby/lib/cconfigspace/expression_parser.rb @@ -43,10 +43,12 @@ module CCS rule("]") rule(",") - rule(:none => /nil/).as { |b| + rule(:none => /none/).as { |b| Literal::new(value: nil) } - rule(:boolean => /true|false/).as { |b| - Literal::new(value: eval(b)) } + rule(:btrue => /true/).as { |b| + Literal::new(value: true) } + rule(:bfalse => /false/).as { |b| + Literal::new(value: false) } rule(:float => /-?[0-9]+([eE][+-]?[0-9]+|\.[0-9]+([eE][+-]?[0-9]+)?)/).as {|num| Literal::new(value: Float(num)) } rule(:integer => /-?[0-9]+/).as { |num| @@ -58,7 +60,8 @@ module CCS rule(:value) do |r| r[:none] - r[:boolean] + r[:btrue] + r[:bfalse] r[:string] r[:identifier] r[:float] diff --git a/bindings/ruby/test/test_expression_parser.rb b/bindings/ruby/test/test_expression_parser.rb index 5185837b957a7585f4a7e4c2909bcc5caf37146c..7f134e11dae5ab98906948bd41247b74538ab8c2 100644 --- a/bindings/ruby/test/test_expression_parser.rb +++ b/bindings/ruby/test/test_expression_parser.rb @@ -63,11 +63,11 @@ class CConfigSpaceTestExpressionParser < Minitest::Test def test_none m = CCS::ExpressionParser.new.method(:parse) - exp = "nil" + exp = "none" res = m[exp] assert( res.kind_of? CCS::Literal ) assert_nil( res.eval ) - assert_equal( "nil", res.to_s ) + assert_equal( "none", res.to_s ) end end