本文整理汇总了Python中pants.option.option_value_container.OptionValueContainer类的典型用法代码示例。如果您正苦于以下问题:Python OptionValueContainer类的具体用法?Python OptionValueContainer怎么用?Python OptionValueContainer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了OptionValueContainer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_iterator
def test_iterator(self):
o = OptionValueContainer()
o.a = 3
o.b = 2
o.c = 1
names = list(iter(o))
self.assertListEqual(['a', 'b', 'c'], names)
示例2: for_scope
def for_scope(self, scope):
"""Return the option values for the given scope.
Values are attributes of the returned object, e.g., options.foo.
Computed lazily per scope.
"""
# Short-circuit, if already computed.
if scope in self._values_by_scope:
return self._values_by_scope[scope]
# First get enclosing scope's option values, if any.
if scope == GLOBAL_SCOPE:
values = OptionValueContainer()
if self._legacy_values:
values.update(vars(self._legacy_values)) # Proxy any legacy option values.
else:
values = copy.copy(self.for_scope(scope.rpartition('.')[0]))
# Now add our values.
try:
flags_in_scope = self._scope_to_flags.get(scope, [])
self._parser_hierarchy.get_parser_by_scope(scope).parse_args(flags_in_scope, values)
self._values_by_scope[scope] = values
return values
except ParseError as e:
self.print_help(str(e))
sys.exit(1)
示例3: test_unknown_values
def test_unknown_values(self):
o = OptionValueContainer()
o.foo = RankedValue(RankedValue.HARDCODED, 1)
self.assertEqual(1, o.foo)
with self.assertRaises(AttributeError):
o.bar
示例4: test_iterator
def test_iterator(self):
o = OptionValueContainer()
o.a = RankedValue(RankedValue.FLAG, 3)
o.b = RankedValue(RankedValue.FLAG, 2)
o.c = RankedValue(RankedValue.FLAG, 1)
names = list(iter(o))
self.assertListEqual(['a', 'b', 'c'], names)
示例5: test_standard_values
def test_standard_values(self):
o = OptionValueContainer()
o.foo = 1
self.assertEqual(1, o.foo)
with self.assertRaises(AttributeError):
o.bar
示例6: for_scope
def for_scope(self, scope):
"""Return the option values for the given scope.
Values are attributes of the returned object, e.g., options.foo.
Computed lazily per scope.
"""
# Short-circuit, if already computed.
if scope in self._values_by_scope:
return self._values_by_scope[scope]
# First get enclosing scope's option values, if any.
if scope == GLOBAL_SCOPE:
values = OptionValueContainer()
else:
values = copy.deepcopy(self.for_scope(enclosing_scope(scope)))
# Now add our values.
flags_in_scope = self._scope_to_flags.get(scope, [])
self._parser_hierarchy.get_parser_by_scope(scope).parse_args(flags_in_scope, values)
self._values_by_scope[scope] = values
for option in values:
self._option_tracker.record_option(
scope=scope, option=option, value=values[option], rank=values.get_rank(option)
)
return values
示例7: test_deepcopy
def test_deepcopy(self):
# deepcopy semantics can get hairy when overriding __setattr__/__getattr__, so we test them.
o = OptionValueContainer()
o.foo = 1
o.bar = {'a': 111}
p = copy.deepcopy(o)
o.bar['b'] = 222 # Add to original dict.
self.assertEqual(1, p.foo)
self.assertEqual({'a': 111}, p.bar) # Ensure dict was copied.
示例8: test_indexing
def test_indexing(self):
o = OptionValueContainer()
o.add_forwardings({'foo': 'bar'})
o.bar = 1
self.assertEqual(1, o['foo'])
self.assertEqual(1, o['bar'])
with self.assertRaises(AttributeError):
o['baz']
示例9: for_scope
def for_scope(self, scope, inherit_from_enclosing_scope=True):
"""Return the option values for the given scope.
Values are attributes of the returned object, e.g., options.foo.
Computed lazily per scope.
:API: public
"""
# Short-circuit, if already computed.
if scope in self._values_by_scope:
return self._values_by_scope[scope]
# First get enclosing scope's option values, if any.
if scope == GLOBAL_SCOPE or not inherit_from_enclosing_scope:
values = OptionValueContainer()
else:
values = copy.copy(self.for_scope(enclosing_scope(scope)))
# Now add our values.
flags_in_scope = self._scope_to_flags.get(scope, [])
self._parser_hierarchy.get_parser_by_scope(scope).parse_args(flags_in_scope, values)
# If we're the new name of a deprecated scope, also get values from that scope.
deprecated_scope = self.known_scope_to_info[scope].deprecated_scope
# Note that deprecated_scope and scope share the same Optionable class, so deprecated_scope's
# Optionable has a deprecated_options_scope equal to deprecated_scope. Therefore we must
# check that scope != deprecated_scope to prevent infinite recursion.
if deprecated_scope is not None and scope != deprecated_scope:
# Do the deprecation check only on keys that were explicitly set on the deprecated scope
# (and not on its enclosing scopes).
explicit_keys = self.for_scope(deprecated_scope,
inherit_from_enclosing_scope=False).get_explicit_keys()
if explicit_keys:
warn_or_error(self.known_scope_to_info[scope].deprecated_scope_removal_version,
'scope {}'.format(deprecated_scope),
'Use scope {} instead (options: {})'.format(scope, ', '.join(explicit_keys)))
# Update our values with those of the deprecated scope (now including values inherited
# from its enclosing scope).
# Note that a deprecated val will take precedence over a val of equal rank.
# This makes the code a bit neater.
values.update(self.for_scope(deprecated_scope))
# Record the value derivation.
for option in values:
self._option_tracker.record_option(scope=scope, option=option, value=values[option],
rank=values.get_rank(option))
# Cache the values.
self._values_by_scope[scope] = values
return values
示例10: test_deepcopy
def test_deepcopy(self):
# copy semantics can get hairy when overriding __setattr__/__getattr__, so we test them.
o = OptionValueContainer()
o.foo = RankedValue(RankedValue.FLAG, 1)
o.bar = RankedValue(RankedValue.FLAG, {'a': 111})
p = copy.deepcopy(o)
# Verify that the result is in fact a copy.
self.assertEqual(1, p.foo) # Has original attribute.
o.baz = RankedValue(RankedValue.FLAG, 42)
self.assertFalse(hasattr(p, 'baz')) # Does not have attribute added after the copy.
# Verify that it's a deep copy by modifying a referent in o and reading it in p.
o.bar['b'] = 222
self.assertEqual({'a': 111}, p.bar)
示例11: test_is_flagged
def test_is_flagged(self):
o = OptionValueContainer()
o.foo = RankedValue(RankedValue.NONE, 11)
self.assertFalse(o.is_flagged('foo'))
o.foo = RankedValue(RankedValue.CONFIG, 11)
self.assertFalse(o.is_flagged('foo'))
o.foo = RankedValue(RankedValue.ENVIRONMENT, 11)
self.assertFalse(o.is_flagged('foo'))
o.foo = RankedValue(RankedValue.FLAG, 11)
self.assertTrue(o.is_flagged('foo'))
示例12: test_value_ranking
def test_value_ranking(self):
o = OptionValueContainer()
o.foo = RankedValue(RankedValue.CONFIG, 11)
self.assertEqual(11, o.foo)
self.assertEqual(RankedValue.CONFIG, o.get_rank('foo'))
o.foo = RankedValue(RankedValue.HARDCODED, 22)
self.assertEqual(11, o.foo)
self.assertEqual(RankedValue.CONFIG, o.get_rank('foo'))
o.foo = RankedValue(RankedValue.ENVIRONMENT, 33)
self.assertEqual(33, o.foo)
self.assertEqual(RankedValue.ENVIRONMENT, o.get_rank('foo'))
o.foo = RankedValue(RankedValue.FLAG, 44)
self.assertEqual(44, o.foo)
self.assertEqual(RankedValue.FLAG, o.get_rank('foo'))
示例13: test_iterator
def test_iterator(self):
o = OptionValueContainer()
o.add_forwardings({'a': '_a'})
o.add_forwardings({'b': '_b'})
o.add_forwardings({'b_prime': '_b'}) # Should be elided in favor of b.
o.add_forwardings({'c': '_c'})
names = list(iter(o))
self.assertListEqual(['a', 'b', 'c'], names)
示例14: test_forwarding
def test_forwarding(self):
o = OptionValueContainer()
o.add_forwardings({'foo': 'bar'})
o.bar = 1
self.assertEqual(1, o.foo)
o.bar = 2
self.assertEqual(2, o.foo)
o.add_forwardings({'baz': 'qux'})
o.qux = 3
self.assertEqual(2, o.foo)
self.assertEqual(3, o.baz)
# Direct setting overrides forwarding.
o.foo = 4
self.assertEqual(4, o.foo)
示例15: test_indexing
def test_indexing(self):
o = OptionValueContainer()
o.add_forwardings({'foo': 'bar'})
o.bar = 1
self.assertEqual(1, o['foo'])
self.assertEqual(1, o['bar'])
self.assertEqual(1, o.get('foo'))
self.assertEqual(1, o.get('foo', 2))
self.assertIsNone(o.get('unknown'))
self.assertEqual(2, o.get('unknown', 2))
with self.assertRaises(AttributeError):
o['baz']