本文整理汇总了Python中Orange.widgets.settings.DomainContextHandler.new_context方法的典型用法代码示例。如果您正苦于以下问题:Python DomainContextHandler.new_context方法的具体用法?Python DomainContextHandler.new_context怎么用?Python DomainContextHandler.new_context使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Orange.widgets.settings.DomainContextHandler
的用法示例。
在下文中一共展示了DomainContextHandler.new_context方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestDomainContextHandler
# 需要导入模块: from Orange.widgets.settings import DomainContextHandler [as 别名]
# 或者: from Orange.widgets.settings.DomainContextHandler import new_context [as 别名]
#.........这里部分代码省略.........
test_filter([0, [1, 2, 3], "abcd", 5.4], [0, [1, 2, 3], "abcd", 5.4])
def test_encode_setting(self):
setting = ContextSetting(None)
var = self.domain[0]
val = self.handler.encode_setting(None, setting, var)
self.assertEqual(val, (var.name, 100 + vartype(var)))
# Should not crash on anonymous variables
var.name = ""
val = self.handler.encode_setting(None, setting, var)
self.assertEqual(val, (var.name, 100 + vartype(var)))
def test_encode_list_settings(self):
setting = ContextSetting(None)
var1, var2 = self.domain[:2]
val = self.handler.encode_setting(None, setting, [None, var1, var2])
self.assertEqual(
val,
([None,
(var1.name, 100 + vartype(var1)),
(var2.name, 100 + vartype(var2))], -3))
a_list = [1, 2, 3]
val = self.handler.encode_setting(None, setting, a_list)
self.assertEqual(val, [1, 2, 3])
self.assertIsNot(val, a_list)
a_list = []
val = self.handler.encode_setting(None, setting, a_list)
self.assertEqual(val, [])
self.assertIsNot(val, a_list)
a_list = [None, None]
val = self.handler.encode_setting(None, setting, a_list)
self.assertEqual(val, [None, None])
self.assertIsNot(val, a_list)
def test_decode_setting(self):
setting = ContextSetting(None)
var = self.domain[0]
val = self.handler.decode_setting(setting, (var.name, 100 + vartype(var)),
self.domain)
self.assertIs(val, var)
all_metas_domain = Domain([], metas=[var])
val = self.handler.decode_setting(setting, (var.name, 100 + vartype(var)),
all_metas_domain)
self.assertIs(val, var)
def test_decode_list_setting(self):
setting = ContextSetting(None)
var1, var2 = self.domain[:2]
val = self.handler.decode_setting(
setting,
([None,
(var1.name, 100 + vartype(var1)),
(var2.name, 100 + vartype(var2))], -3),
self.domain)
self.assertEqual(val, [None, var1, var2])
val = self.handler.decode_setting(setting, [1, 2, 3], self.domain)
self.assertEqual(val, [1, 2, 3])
def test_backward_compatible_params(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
DomainContextHandler(metas_in_res=True)
self.assertIn(OrangeDeprecationWarning,
[x.category for x in w])
def test_deprecated_str_as_var(self):
if LooseVersion(Orange.__version__) >= LooseVersion("3.26"):
# pragma: no cover
self.fail("Remove support for variables stored as string settings "
"and this test.")
context = Mock()
context.attributes = {"foo": 2}
context.metas = {}
setting = ContextSetting("")
setting.name = "setting_name"
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
DomainContextHandler.encode_setting(context, setting, "foo")
self.assertIn("setting_name", w[0].message.args[0])
def create_context(self, domain, values):
if domain is None:
domain = Domain([])
context = self.handler.new_context(domain,
*self.handler.encode_domain(domain))
context.values = values
return context
示例2: TestDomainContextHandler
# 需要导入模块: from Orange.widgets.settings import DomainContextHandler [as 别名]
# 或者: from Orange.widgets.settings.DomainContextHandler import new_context [as 别名]
#.........这里部分代码省略.........
(('c1', Continuous), ('d1', Discrete),
('d2', Discrete), ('d3', Discrete),
('c2', Continuous), ('d4', Discrete)))
self.assertEqual(widget.text, 'u')
self.assertEqual(widget.with_metas, [('d1', Discrete),
('d2', Discrete)])
def test_open_context_with_imperfect_match(self):
self.handler.bind(SimpleWidget)
context = self.create_context(None, dict(
text=('u', -2),
with_metas=[('d1', Discrete), ('d1', Continuous),
('c1', Continuous), ('c1', Discrete)],
if_selected=[('c1', Discrete), ('c1', Continuous),
('d1', Discrete), ('d1', Continuous)],
selected=[2],
))
self.handler.global_contexts = \
[Mock(values={}), context, Mock(values={})]
widget = SimpleWidget()
self.handler.initialize(widget)
self.handler.open_context(widget, self.args[0])
context = widget.current_context
self.assertEqual(context.attributes, self.args[1])
self.assertEqual(context.metas, self.args[2])
self.assertSequenceEqual(context.ordered_domain,
(('c1', Continuous), ('d1', Discrete),
('d2', Discrete), ('d3', Discrete),
('c2', Continuous), ('d4', Discrete)))
self.assertEqual(widget.text, 'u')
self.assertEqual(widget.with_metas, [('d1', Discrete),
('c1', Continuous)])
self.assertEqual(widget.if_selected, [('c1', Continuous),
('d1', Discrete)])
self.assertEqual(widget.selected, [1])
def test_open_context_with_no_match(self):
self.handler.bind(SimpleWidget)
widget = SimpleWidget()
self.handler.initialize(widget)
widget.text = 'u'
self.handler.open_context(widget, self.args[0])
self.assertEqual(widget.text, 'u')
self.assertEqual(widget.with_metas, [])
context = widget.current_context
self.assertEqual(context.attributes, self.args[1])
self.assertEqual(context.metas, self.args[2])
self.assertSequenceEqual(context.ordered_domain,
(('c1', Continuous), ('d1', Discrete),
('d2', Discrete), ('d3', Discrete),
('c2', Continuous), ('d4', Discrete)))
self.assertEqual(context.values['text'], ('u', -2))
def test_filter_value(self):
setting = ContextSetting([])
setting.name = "value"
def test_filter(before_value, after_value):
data = dict(value=before_value)
self.handler.filter_value(setting, data, *self.args)
self.assertEqual(data.get("value", None), after_value)
# filter list values
test_filter([], [])
# When list contains attributes asa tuple of (name, type),
# Attributes not present in domain should be filtered out
test_filter([("d1", Discrete), ("d1", Continuous),
("c1", Continuous), ("c1", Discrete)],
[("d1", Discrete), ("c1", Continuous)])
# All other values in list should remain
test_filter([0, [1, 2, 3], "abcd", 5.4], [0, [1, 2, 3], "abcd", 5.4])
def test_encode_setting(self):
setting = ContextSetting(None)
var = self.domain[0]
val = self.handler.encode_setting(None, setting, var)
self.assertEqual(val, (var.name, 100 + vartype(var)))
def test_decode_setting(self):
setting = ContextSetting(None)
var = self.domain[0]
val = self.handler.decode_setting(setting, (var.name, 100 + vartype(var)), self.domain)
self.assertIs(val, var)
def create_context(self, domain, values):
if not domain:
domain = Domain([])
context = self.handler.new_context(domain,
*self.handler.encode_domain(domain))
context.values = values
return context
示例3: DomainContextHandlerTestCase
# 需要导入模块: from Orange.widgets.settings import DomainContextHandler [as 别名]
# 或者: from Orange.widgets.settings.DomainContextHandler import new_context [as 别名]
#.........这里部分代码省略.........
def test_clone_context(self):
self.handler.bind(SimpleWidget)
context = self.create_context(self.domain, dict(
text=('u', -2),
with_metas=[('d1', Discrete), ('d1', Continuous),
('c1', Continuous), ('c1', Discrete)],
required=('u', Continuous)
))
new_values = self.handler.clone_context(context, *self.args).values
self.assertEqual(new_values['text'], ('u', -2))
self.assertEqual([('d1', Discrete), ('c1', Continuous)],
new_values['with_metas'])
self.assertNotIn('required', new_values)
def test_open_context(self):
self.handler.bind(SimpleWidget)
context = self.create_context(self.domain, dict(
text=('u', -2),
with_metas=[('d1', Discrete), ('d2', Discrete)]
))
self.handler.global_contexts = \
[Mock(values={}), context, Mock(values={})]
widget = SimpleWidget()
self.handler.initialize(widget)
self.handler.open_context(widget, self.args[0])
context = widget.current_context
self.assertEqual(context.attributes, self.args[1])
self.assertEqual(context.metas, self.args[2])
self.assertSequenceEqual(context.ordered_domain,
(('c1', Continuous), ('d1', Discrete),
('d2', Discrete), ('d3', Discrete),
('c2', Continuous), ('d4', Discrete)))
self.assertEqual(widget.text, 'u')
self.assertEqual(widget.with_metas, [('d1', Discrete),
('d2', Discrete)])
def test_open_context_with_imperfect_match(self):
self.handler.bind(SimpleWidget)
context = self.create_context(None, dict(
text=('u', -2),
with_metas=[('d1', Discrete), ('d1', Continuous),
('c1', Continuous), ('c1', Discrete)],
if_selected=[('c1', Discrete), ('c1', Continuous),
('d1', Discrete), ('d1', Continuous)],
selected=[2],
))
self.handler.global_contexts = \
[Mock(values={}), context, Mock(values={})]
widget = SimpleWidget()
self.handler.initialize(widget)
self.handler.open_context(widget, self.args[0])
context = widget.current_context
self.assertEqual(context.attributes, self.args[1])
self.assertEqual(context.metas, self.args[2])
self.assertSequenceEqual(context.ordered_domain,
(('c1', Continuous), ('d1', Discrete),
('d2', Discrete), ('d3', Discrete),
('c2', Continuous), ('d4', Discrete)))
self.assertEqual(widget.text, 'u')
self.assertEqual(widget.with_metas, [('d1', Discrete),
('c1', Continuous)])
self.assertEqual(widget.if_selected, [('c1', Continuous),
('d1', Discrete)])
self.assertEqual(widget.selected, [1])
def test_open_context_with_no_match(self):
self.handler.bind(SimpleWidget)
widget = SimpleWidget()
self.handler.initialize(widget)
widget.text = 'u'
self.handler.open_context(widget, self.args[0])
self.assertEqual(widget.text, 'u')
self.assertEqual(widget.with_metas, [])
context = widget.current_context
self.assertEqual(context.attributes, self.args[1])
self.assertEqual(context.metas, self.args[2])
self.assertSequenceEqual(context.ordered_domain,
(('c1', Continuous), ('d1', Discrete),
('d2', Discrete), ('d3', Discrete),
('c2', Continuous), ('d4', Discrete)))
self.assertEqual(context.values['text'], ('u', -2))
def create_context(self, domain, values):
if not domain:
domain = Domain([])
context = self.handler.new_context(domain,
*self.handler.encode_domain(domain))
context.values = values
return context