Mercurial > hgrepos > Python > libs > ConfigMix
changeset 384:8c3aaa894089
Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s()
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Wed, 10 Nov 2021 01:53:50 +0100 |
| parents | 5c72da46b8ae |
| children | 4beeb291926d |
| files | CHANGES.txt configmix/config.py |
| diffstat | 2 files changed, 40 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/CHANGES.txt Wed Nov 10 01:53:23 2021 +0100 +++ b/CHANGES.txt Wed Nov 10 01:53:50 2021 +0100 @@ -17,7 +17,10 @@ - **[feature]** New access methods :py:meth:`~configmix.config.Configuration.getfirstvarl`, - :py:meth:`~configmix.config.Configuration.getfirstvarl_s` + :py:meth:`~configmix.config.Configuration.getfirstvarl_s`, + :py:meth:`~configmix.config.Configuration.getfirstintvarl_s` + :py:meth:`~configmix.config.Configuration.getfirstboolvarl_s`, + :py:meth:`~configmix.config.Configuration.getfirstfloatvarl_s` - **[feature]** New access method py:meth:`~configmix.config.Configuration.getfirstfloatvar_s`
--- a/configmix/config.py Wed Nov 10 01:53:23 2021 +0100 +++ b/configmix/config.py Wed Nov 10 01:53:50 2021 +0100 @@ -275,6 +275,17 @@ else: return s + def getfirstintvarl_s(self, *paths, **kwds): + """Get a (possibly substituted) variable and coerce text to a + number. + + """ + s = self.getfirstvarl_s(*paths, **kwds) + if isinstance(s, self._TEXTTYPE): + return int(s, 0) + else: + return s + def getintvar_s(self, varname, default=_MARKER): """Get a (possibly substituted) variable and coerce text to a number. @@ -311,6 +322,20 @@ else: return s + def getfirstboolvarl_s(self, *paths, **kwds): + """Get a (possibly substituted) variable and convert text to a + boolean + + """ + s = self.getfirstvarl_s(*paths, **kwds) + if isinstance(s, self._TEXTTYPE): + sl = s.strip().lower() + if sl not in self._BOOL_CVT: + raise ValueError("Not a boolean: %r" % s) + return self._BOOL_CVT[sl] + else: + return s + def getboolvar_s(self, varname, default=_MARKER): """Get a (possibly substituted) variable and convert text to a boolean @@ -356,6 +381,17 @@ else: return s + def getfirstfloatvarl_s(self, *path, **kwds): + """Get a (possibly substituted) variable and convert text to a + float + + """ + s = self.getfirstvarl_s(*path, **kwds) + if isinstance(s, self._TEXTTYPE): + return float(s) + else: + return s + def getfloatvar_s(self, varname, default=_MARKER): """Get a (possibly substituted) variable and convert text to a float
