From a7dda77474c7730e23e8c376e6d91cea4eab9235 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Mon, 19 Aug 2013 13:11:56 -0400 Subject: [PATCH] Template.has_param() -> Template.has() --- CHANGELOG | 2 ++ README.rst | 2 +- docs/changelog.rst | 13 ++++++++----- docs/usage.rst | 2 +- mwparserfromhell/nodes/template.py | 7 +++++-- tests/test_docs.py | 2 +- tests/test_template.py | 22 +++++++++++----------- 7 files changed, 29 insertions(+), 21 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 3876562..8922738 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -11,6 +11,8 @@ v0.3 (unreleased): remove() now accepts other Wikicode objects and strings representing parts of wikitext, instead of just nodes. These methods also make all possible substitutions instead of just one. +- Renamed Template.has_param() to has() for consistency with Template's other + methods; has_param() is now an alias. - The C tokenizer extension now works on Python 3 in addition to Python 2.7. - Various fixes and cleanup. diff --git a/README.rst b/README.rst index f1092ee..b5fd912 100644 --- a/README.rst +++ b/README.rst @@ -90,7 +90,7 @@ whitespace:: >>> text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}" >>> code = mwparserfromhell.parse(text) >>> for template in code.filter_templates(): - ... if template.name.matches("Cleanup") and not template.has_param("date"): + ... if template.name.matches("Cleanup") and not template.has("date"): ... template.add("date", "July 2012") ... >>> print code diff --git a/docs/changelog.rst b/docs/changelog.rst index c5e2516..86dfd78 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -16,11 +16,14 @@ Unreleased if you rely on any filter() methods being non-recursive by default.** - Added a :py:meth:`.matches` method to :py:class:`~.Wikicode` for page/template name comparisons. -- The *obj* param of :py:meth:`Wikicode.insert_before <.insert_before>`, - :py:meth:`~.insert_after`, :py:meth:`~.replace`, and :py:meth:`~.remove` now - accepts :py:class:`~.Wikicode` objects and strings representing parts of - wikitext, instead of just nodes. These methods also make all possible - substitutions instead of just one. +- The *obj* param of :py:meth:`Wikicode.insert_before() <.insert_before>`, + :py:meth:`~.insert_after`, :py:meth:`~.Wikicode.replace`, and + :py:meth:`~.Wikicode.remove` now accepts :py:class:`~.Wikicode` objects and + strings representing parts of wikitext, instead of just nodes. These methods + also make all possible substitutions instead of just one. +- Renamed :py:meth:`Template.has_param() <.has_param>` to + :py:meth:`~.Template.has` for consistency with :py:class:`~.Template`\ 's + other methods; :py:meth:`~.has_param` is now an alias. - The C tokenizer extension now works on Python 3 in addition to Python 2.7. - Various fixes and cleanup. diff --git a/docs/usage.rst b/docs/usage.rst index 15b384b..974c670 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -58,7 +58,7 @@ names, which takes care of capitalization and whitespace:: >>> text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}" >>> code = mwparserfromhell.parse(text) >>> for template in code.filter_templates(): - ... if template.name.matches("Cleanup") and not template.has_param("date"): + ... if template.name.matches("Cleanup") and not template.has("date"): ... template.add("date", "July 2012") ... >>> print code diff --git a/mwparserfromhell/nodes/template.py b/mwparserfromhell/nodes/template.py index 6dfc4f0..c326b65 100644 --- a/mwparserfromhell/nodes/template.py +++ b/mwparserfromhell/nodes/template.py @@ -164,7 +164,7 @@ class Template(Node): def name(self, value): self._name = parse_anything(value) - def has_param(self, name, ignore_empty=True): + def has(self, name, ignore_empty=True): """Return ``True`` if any parameter in the template is named *name*. With *ignore_empty*, ``False`` will be returned even if the template @@ -180,6 +180,9 @@ class Template(Node): return True return False + has_param = lambda self, *args, **kwargs: self.has(*args, **kwargs) + has_param.__doc__ = "Alias for :py:meth:`has`." + def get(self, name): """Get the parameter whose name is *name*. @@ -226,7 +229,7 @@ class Template(Node): name, value = parse_anything(name), parse_anything(value) self._surface_escape(value, "|") - if self.has_param(name): + if self.has(name): self.remove(name, keep_field=True) existing = self.get(name) if showkey is not None: diff --git a/tests/test_docs.py b/tests/test_docs.py index 53b3b76..6d066bd 100644 --- a/tests/test_docs.py +++ b/tests/test_docs.py @@ -90,7 +90,7 @@ class TestDocs(unittest.TestCase): text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}" code = mwparserfromhell.parse(text) for template in code.filter_templates(): - if template.name.matches("Cleanup") and not template.has_param("date"): + if template.name.matches("Cleanup") and not template.has("date"): template.add("date", "July 2012") res = "{{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{uncategorized}}" self.assertPrint(code, res) diff --git a/tests/test_template.py b/tests/test_template.py index 28592df..9ed099d 100644 --- a/tests/test_template.py +++ b/tests/test_template.py @@ -115,23 +115,23 @@ class TestTemplate(TreeEqualityTestCase): self.assertEqual([], node1.params) self.assertIs(plist, node2.params) - def test_has_param(self): - """test Template.has_param()""" + def test_has(self): + """test Template.has()""" node1 = Template(wraptext("foobar")) node2 = Template(wraptext("foo"), [pgenh("1", "bar"), pgens("\nabc ", "def")]) node3 = Template(wraptext("foo"), [pgenh("1", "a"), pgens("b", "c"), pgens("1", "d")]) node4 = Template(wraptext("foo"), [pgenh("1", "a"), pgens("b", " ")]) - self.assertFalse(node1.has_param("foobar")) - self.assertTrue(node2.has_param(1)) - self.assertTrue(node2.has_param("abc")) - self.assertFalse(node2.has_param("def")) - self.assertTrue(node3.has_param("1")) - self.assertTrue(node3.has_param(" b ")) - self.assertFalse(node4.has_param("b")) - self.assertTrue(node3.has_param("b", False)) - self.assertTrue(node4.has_param("b", False)) + self.assertFalse(node1.has("foobar")) + self.assertTrue(node2.has(1)) + self.assertTrue(node2.has("abc")) + self.assertFalse(node2.has("def")) + self.assertTrue(node3.has("1")) + self.assertTrue(node3.has(" b ")) + self.assertFalse(node4.has("b")) + self.assertTrue(node3.has("b", False)) + self.assertTrue(node4.has("b", False)) def test_get(self): """test Template.get()"""