當前位置: 首頁>>代碼示例>>Python>>正文


Python RootTask.format_and_eval_string方法代碼示例

本文整理匯總了Python中ecpy.tasks.base_tasks.RootTask.format_and_eval_string方法的典型用法代碼示例。如果您正苦於以下問題:Python RootTask.format_and_eval_string方法的具體用法?Python RootTask.format_and_eval_string怎麽用?Python RootTask.format_and_eval_string使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在ecpy.tasks.base_tasks.RootTask的用法示例。


在下文中一共展示了RootTask.format_and_eval_string方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: TestEvaluation

# 需要導入模塊: from ecpy.tasks.base_tasks import RootTask [as 別名]
# 或者: from ecpy.tasks.base_tasks.RootTask import format_and_eval_string [as 別名]
class TestEvaluation(object):
    """Test evaluating strings and caching in running mode.

    """

    def setup(self):
        self.root = RootTask()
        database = self.root.database
        database.set_value('root', 'val1', 1)
        database.create_node('root', 'node1')
        database.set_value('root/node1', 'val2', 10.0)
        database.add_access_exception('root', 'root/node1', 'val2')

    def test_eval_pure_string_editing_mode(self):
        """Test evaluating a word.

        """
        test = 'test'
        formatted = self.root.format_and_eval_string(test)
        assert formatted == 'test'

    def test_eval_editing_mode1(self):
        """Test eval expression with only standard operators.

        """
        test = '{val1}/{val2}'
        formatted = self.root.format_and_eval_string(test)
        assert formatted == 0.1
        assert not self.root._eval_cache

    def test_eval_editing_mode2(self):
        """Test eval expression containing a math function.

        """
        test = 'cos({val1}/{val2})'
        formatted = self.root.format_and_eval_string(test)
        assert formatted == cos(0.1)
        assert not self.root._eval_cache

    def test_eval_editing_mode3(self):
        """Test eval expression containing a cmath function.

        """
        self.root.database.set_value('root', 'val1', 10.0)
        test = 'cm.sqrt({val1}/{val2})'
        formatted = self.root.format_and_eval_string(test)
        assert formatted == 1+0j
        assert not self.root._eval_cache

    def test_eval_editing_mode4(self):
        """Test eval expression containing a numpy function.

        """
        self.root.database.set_value('root', 'val1', [1.0, -1.0])
        test = 'np.abs({val1})'
        formatted = self.root.format_and_eval_string(test)
        assert_array_equal(formatted, numpy.array((1.0, 1.0)))
        assert not self.root._eval_cache

    def test_eval_pure_string_running_mode(self):
        """Test evaluating a word.

        """
        self.root.database.prepare_to_run()
        test = 'test'
        formatted = self.root.format_and_eval_string(test)
        assert formatted == 'test'

    def test_eval_running_mode1(self):
        """Test eval expression with only standard operators.

        """
        self.root.database.prepare_to_run()
        test = '{val1}/{val2}'
        formatted = self.root.format_and_eval_string(test)
        assert formatted == 0.1
        assert self.root._eval_cache
        assert test in self.root._eval_cache
        self.root.database.set_value('root', 'val1', 2)
        formatted = self.root.format_and_eval_string(test)
        assert formatted == 0.2

    def test_eval_running_mode2(self):
        """Test eval expression containing a math function.

        """
        self.root.database.prepare_to_run()
        test = 'cos({val1}/{val2})'
        formatted = self.root.format_and_eval_string(test)
        assert formatted == cos(0.1)
        assert self.root._eval_cache
        assert test in self.root._eval_cache
        self.root.database.set_value('root', 'val1', 2)
        formatted = self.root.format_and_eval_string(test)
        assert formatted == cos(0.2)

    def test_eval_running_mode3(self):
        """Test eval expression containing a cmath function.

        """
#.........這裏部分代碼省略.........
開發者ID:MatthieuDartiailh,項目名稱:ecpy,代碼行數:103,代碼來源:test_string_ops.py


注:本文中的ecpy.tasks.base_tasks.RootTask.format_and_eval_string方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。