From a42c7287444a17138b5164b7248c0e1db8099888 Mon Sep 17 00:00:00 2001 From: AntiCompositeNumber Date: Wed, 19 Aug 2020 00:43:42 -0400 Subject: [PATCH] nodes: Add __getitem__, __setitem__, and __delitem__ to Template These are just aliases for existing methods, without the ability to specifiy additional parameters. However, including them makes Template more dict-like, so it's a good idea to have them. --- mwparserfromhell/nodes/template.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mwparserfromhell/nodes/template.py b/mwparserfromhell/nodes/template.py index d71a6bb..b0e9011 100644 --- a/mwparserfromhell/nodes/template.py +++ b/mwparserfromhell/nodes/template.py @@ -232,6 +232,9 @@ class Template(Node): else: return default + def __getitem__(self, name): + return self.get(name) + def add(self, name, value, showkey=None, before=None, preserve_spacing=True): """Add a parameter to the template with a given *name* and *value*. @@ -312,6 +315,9 @@ class Template(Node): self.params.append(param) return param + def __setitem__(self, name, value): + return self.add(name, value) + def remove(self, param, keep_field=False): """Remove a parameter from the template, identified by *param*. @@ -357,3 +363,6 @@ class Template(Node): raise ValueError(name) for i in reversed(to_remove): self.params.pop(i) + + def __delitem__(self, param): + return self.remove(param)