-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow mathematical expression to be passed for random_seed
- Loading branch information
Showing
6 changed files
with
115 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# https://stackoverflow.com/a/30516254 | ||
|
||
import ast | ||
import math | ||
|
||
|
||
class MathExpr(object): | ||
allowed_nodes = ( | ||
ast.Module, | ||
ast.Expr, | ||
ast.Load, | ||
ast.Expression, | ||
ast.Add, | ||
ast.Sub, | ||
ast.UnaryOp, | ||
ast.Num, | ||
ast.BinOp, | ||
ast.Mult, | ||
ast.Div, | ||
ast.Pow, | ||
ast.BitOr, | ||
ast.BitAnd, | ||
ast.BitXor, | ||
ast.USub, | ||
ast.UAdd, | ||
ast.FloorDiv, | ||
ast.Mod, | ||
ast.LShift, | ||
ast.RShift, | ||
ast.Invert, | ||
ast.Call, | ||
ast.Name, | ||
) | ||
|
||
functions = { | ||
"abs": abs, | ||
"complex": complex, | ||
"min": min, | ||
"max": max, | ||
"pow": pow, | ||
"round": round, | ||
} | {key: value for (key, value) in vars(math).items() if not key.startswith("_")} | ||
|
||
def __init__(self, expr): | ||
if any(elem in expr for elem in "\n#"): | ||
raise ValueError(expr) | ||
|
||
node = ast.parse(expr.strip(), mode="eval") | ||
for curr in ast.walk(node): | ||
if not isinstance(curr, self.allowed_nodes): | ||
raise ValueError(curr) | ||
|
||
self.code = compile(node, "<string>", "eval") | ||
|
||
def __call__(self, **kwargs): | ||
return eval(self.code, {"__builtins__": None}, self.functions | kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright (c) 2018-2022 Simons Foundation | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You may obtain a copy of the License at | ||
# https:#www.gnu.org/licenses/gpl-3.0.txt | ||
# | ||
# Authors: Alexander Hampel | ||
|
||
from solid_dmft.dmft_tools.matheval import MathExpr | ||
import unittest | ||
|
||
|
||
class test_mathexpr(unittest.TestCase): | ||
def test_simple(self): | ||
expr = MathExpr("1 + 1") | ||
result = expr() | ||
self.assertEqual(result, 2) | ||
|
||
def test_variables(self): | ||
expr = MathExpr("34788 * it + 928374 * rank") | ||
result = expr(it=5, rank=9) | ||
self.assertEqual(result, 34788 * 5 + 928374 * 9) | ||
|
||
def test_breakout(self): | ||
with self.assertRaises(ValueError): | ||
expr = MathExpr("(1).__class__") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |