# HG changeset patch # User Franz Glasner # Date 1636505630 -3600 # Node ID 8c3aaa894089287badbcb61de86fd1c6fde9ae9a # Parent 5c72da46b8ae5ffe708038743d763b5c6a3a3be5 Implemented Configuration.getfirstintvarl_s(), .getfirstboolvar_s() and .getfirstfloatvarl_s() diff -r 5c72da46b8ae -r 8c3aaa894089 CHANGES.txt --- 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` diff -r 5c72da46b8ae -r 8c3aaa894089 configmix/config.py --- 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