[elbe-devel] [PATCH 1/1] Fix ebase.text expected behavior
Torben Hohn
torben.hohn at linutronix.de
Fri Jun 28 13:02:27 CEST 2019
On Fri, Jun 28, 2019 at 11:54:31AM +0200, dion at linutronix.de wrote:
> From: Olivier Dion <dion at linutronix.de>
>
> When passing *default* as a keyword argument to 'ebase.text', we
> expect *default* to be return if the path to the node _doesn't_
> exists.
>
> There's a couple of places where the expected behavior is different.
> By passing a _dict_ object like as default and passing a second
> keyword argument called *key*.
>
> Thus, here's the convention:
>
> 1) Is *default* a _dict_ like object, i.e. does it implement the
> '__getitem__' method?
> 2) If 1) is True, is there a second keyword called *key*?
> 3) If 2) is True then get the item *default[key]*.
> 4) If 1) or 2) are False , then return *default*.
>
> This is a weird implementation .. it would make much more sense to
> pass *default=my_dict[key]* directly instead of *default=my_dict,
> key=key*.
>
> Signed-off-by: Olivier Dion <dion at linutronix.de>
its indeed weird. And i cant remember, why we/i did it that
way. it was 2013 after all ;)
But lets just fix it for now.
Reviewed-by: Torben Hohn <torben.hohn at linutronix.de>
> ---
> elbepack/treeutils.py | 14 +++++++-------
> 1 file changed, 7 insertions(+), 7 deletions(-)
>
> diff --git a/elbepack/treeutils.py b/elbepack/treeutils.py
> index 0caea6d5..b1ac3711 100644
> --- a/elbepack/treeutils.py
> +++ b/elbepack/treeutils.py
> @@ -38,16 +38,16 @@ class ebase(object):
> def __init__(self, et):
> self.et = et
>
> - def text(self, path, **args):
> + def text(self, path, **kwargs):
> el = self.et.find("./" + path)
> - if (el is None) and "default" not in args:
> - raise Exception("Cant find path %s" % path)
> - elif (el is None) and "default" in args:
> - default = args["default"]
> - if isinstance(default, str):
> + if el is None:
> + if "default" in kwargs:
> + default = kwargs["default"]
> + if hasattr(default, "__getitem__") and "key" in kwargs:
> + return default[kwargs["key"]]
> return default
>
> - return default[args["key"]]
> + raise Exception("Cant find path %s" % path)
>
> return el.text
>
> --
> 2.11.0
>
>
> _______________________________________________
> elbe-devel mailing list
> elbe-devel at linutronix.de
> https://lists.linutronix.de/mailman/listinfo/elbe-devel
--
Torben Hohn
Linutronix GmbH | Bahnhofstrasse 3 | D-88690 Uhldingen-Mühlhofen
Phone: +49 7556 25 999 18; Fax.: +49 7556 25 999 99
Hinweise zum Datenschutz finden Sie hier (Informations on data privacy
can be found here): https://linutronix.de/kontakt/Datenschutz.php
Linutronix GmbH | Firmensitz (Registered Office): Uhldingen-Mühlhofen |
Registergericht (Registration Court): Amtsgericht Freiburg i.Br., HRB700
806 | Geschäftsführer (Managing Directors): Heinz Egger, Thomas Gleixner
More information about the elbe-devel
mailing list