mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
Env now supports float values
This commit is contained in:
parent
bc23957247
commit
379c549fc1
@ -65,10 +65,10 @@ class Env(object):
|
||||
>>> env.item # Also retrieve as an attribute
|
||||
'I was set as a dictionary item.'
|
||||
|
||||
The variable values can be ``str`` or ``int`` instances, or the ``True``,
|
||||
``False``, or ``None`` constants. When the value provided is an ``str``
|
||||
instance, some limited automatic type conversion is performed, which allows
|
||||
values of specific types to be set easily from configuration files or
|
||||
The variable values can be ``str``, ``int``, or ``float`` instances, or the
|
||||
``True``, ``False``, or ``None`` constants. When the value provided is an
|
||||
``str`` instance, some limited automatic type conversion is performed, which
|
||||
allows values of specific types to be set easily from configuration files or
|
||||
command-line options.
|
||||
|
||||
So in addition to their actual values, the ``True``, ``False``, and ``None``
|
||||
@ -89,11 +89,15 @@ class Env(object):
|
||||
'false'
|
||||
|
||||
If an ``str`` value looks like an integer, it's automatically converted to
|
||||
the ``int`` type. For example:
|
||||
the ``int`` type. Likewise, if an ``str`` value looks like a floating-point
|
||||
number, it's automatically converted to the ``float`` type. For example:
|
||||
|
||||
>>> env.lucky = '7'
|
||||
>>> env.lucky
|
||||
7
|
||||
>>> env.three_halves = '1.5'
|
||||
>>> env.three_halves
|
||||
1.5
|
||||
|
||||
Leading and trailing white-space is automatically stripped from ``str``
|
||||
values. For example:
|
||||
@ -232,7 +236,12 @@ class Env(object):
|
||||
value = m[value]
|
||||
elif value.isdigit():
|
||||
value = int(value)
|
||||
assert type(value) in (str, int, bool, NoneType)
|
||||
else:
|
||||
try:
|
||||
value = float(value)
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
assert type(value) in (str, int, float, bool, NoneType)
|
||||
object.__setattr__(self, key, value)
|
||||
self.__d[key] = value
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
# Authors:
|
||||
# Martin Nagy <mnagy@redhat.com>
|
||||
# Jason Gerard DeRose <jderose@redhat.com>
|
||||
#
|
||||
# Copyright (C) 2008 Red Hat
|
||||
# see file 'COPYING' for use and warranty information
|
||||
@ -41,6 +42,8 @@ good_vars = (
|
||||
('trailing_whitespace', ' value ', 'value'),
|
||||
('an_int', 42, 42),
|
||||
('int_repr', ' 42 ', 42),
|
||||
('a_float', 3.14, 3.14),
|
||||
('float_repr', ' 3.14 ', 3.14),
|
||||
('true', True, True),
|
||||
('true_repr', ' True ', True),
|
||||
('false', False, False),
|
||||
@ -120,9 +123,12 @@ key3 = var3
|
||||
config_good = """
|
||||
[global]
|
||||
|
||||
string = Hello world!
|
||||
null = None
|
||||
yes = True
|
||||
no = False
|
||||
number = 42
|
||||
floating = 3.14
|
||||
"""
|
||||
|
||||
|
||||
@ -372,11 +378,15 @@ class test_Env(ClassChecker):
|
||||
good = tmp.join('good.conf')
|
||||
open(good, 'w').write(config_good)
|
||||
assert path.isfile(good)
|
||||
assert o._merge_from_file(good) == (3, 3)
|
||||
assert list(o) == sorted(keys + ('yes', 'no', 'number'))
|
||||
assert o._merge_from_file(good) == (6, 6)
|
||||
added = ('string', 'null', 'yes', 'no', 'number', 'floating')
|
||||
assert list(o) == sorted(keys + added)
|
||||
assert o.string == 'Hello world!'
|
||||
assert o.null is None
|
||||
assert o.yes is True
|
||||
assert o.no is False
|
||||
assert o.number == 42
|
||||
assert o.floating == 3.14
|
||||
|
||||
def new(self):
|
||||
"""
|
||||
|
Loading…
Reference in New Issue
Block a user