Browse Source

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.
pull/252/head
AntiCompositeNumber 4 years ago
parent
commit
a42c728744
No known key found for this signature in database GPG Key ID: A888A323AB506229
1 changed files with 9 additions and 0 deletions
  1. +9
    -0
      mwparserfromhell/nodes/template.py

+ 9
- 0
mwparserfromhell/nodes/template.py View File

@@ -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)

Loading…
Cancel
Save