From 0f16d0c63ee024ea38391805bab7ad54b46bfd92 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Sat, 27 Dec 2014 21:19:17 -0800 Subject: [PATCH] Target documentation for Python 3 usage 2 is dead, long live 3. Mainly turning print info a function and urllib import fixes --- README.rst | 33 +++++++++++++++++---------------- docs/integration.rst | 4 ++-- docs/usage.rst | 36 ++++++++++++++++++------------------ mwparserfromhell/wikicode.py | 2 +- 4 files changed, 38 insertions(+), 37 deletions(-) diff --git a/README.rst b/README.rst index 27b112a..93dee92 100644 --- a/README.rst +++ b/README.rst @@ -47,19 +47,19 @@ For example:: >>> text = "I has a template! {{foo|bar|baz|eggs=spam}} See it?" >>> wikicode = mwparserfromhell.parse(text) - >>> print wikicode + >>> print(wikicode) I has a template! {{foo|bar|baz|eggs=spam}} See it? >>> templates = wikicode.filter_templates() - >>> print templates + >>> print(templates) ['{{foo|bar|baz|eggs=spam}}'] >>> template = templates[0] - >>> print template.name + >>> print(template.name) foo - >>> print template.params + >>> print(template.params) ['bar', 'baz', 'eggs=spam'] - >>> print template.get(1).value + >>> print(template.get(1).value) bar - >>> print template.get("eggs").value + >>> print(template.get("eggs").value) spam Since nodes can contain other nodes, getting nested templates is trivial:: @@ -73,14 +73,14 @@ templates manually. This is possible because nodes can contain additional ``Wikicode`` objects:: >>> code = mwparserfromhell.parse("{{foo|this {{includes a|template}}}}") - >>> print code.filter_templates(recursive=False) + >>> print(code.filter_templates(recursive=False)) ['{{foo|this {{includes a|template}}}}'] >>> foo = code.filter_templates(recursive=False)[0] - >>> print foo.get(1).value + >>> print(foo.get(1).value) this {{includes a|template}} - >>> print foo.get(1).value.filter_templates()[0] + >>> print(foo.get(1).value.filter_templates()[0]) {{includes a|template}} - >>> print foo.get(1).value.filter_templates()[0].get(1).value + >>> print(foo.get(1).value.filter_templates()[0].get(1).value) template Templates can be easily modified to add, remove, or alter params. ``Wikicode`` @@ -95,19 +95,19 @@ whitespace:: ... if template.name.matches("Cleanup") and not template.has("date"): ... template.add("date", "July 2012") ... - >>> print code + >>> print(code) {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{uncategorized}} >>> code.replace("{{uncategorized}}", "{{bar-stub}}") - >>> print code + >>> print(code) {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}} - >>> print code.filter_templates() + >>> print(code.filter_templates()) ['{{cleanup|date=July 2012}}', '{{bar-stub}}'] You can then convert ``code`` back into a regular ``unicode`` object (for saving the page!) by calling ``unicode()`` on it:: >>> text = unicode(code) - >>> print text + >>> print(text) {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}} >>> text == code True @@ -136,14 +136,15 @@ If you're not using a library, you can parse any page using the following code (via the API_):: import json - import urllib + from urllib.parse import urlencode + from urllib.request import urlopen import mwparserfromhell API_URL = "http://en.wikipedia.org/w/api.php" def parse(title): data = {"action": "query", "prop": "revisions", "rvlimit": 1, "rvprop": "content", "format": "json", "titles": title} - raw = urllib.urlopen(API_URL, urllib.urlencode(data)).read() + raw = urlopen(API_URL, urlencode(data).encode()).read() res = json.loads(raw) text = res["query"]["pages"].values()[0]["revisions"][0]["*"] return mwparserfromhell.parse(text) diff --git a/docs/integration.rst b/docs/integration.rst index 102b3b9..f6f3610 100644 --- a/docs/integration.rst +++ b/docs/integration.rst @@ -22,12 +22,12 @@ If you're not using a library, you can parse any page using the following code (via the API_):: import json - import urllib + import urllib.request import mwparserfromhell API_URL = "http://en.wikipedia.org/w/api.php" def parse(title): - raw = urllib.urlopen(API_URL, data).read() + raw = urllib.request.urlopen(API_URL, data).read() res = json.loads(raw) text = res["query"]["pages"].values()[0]["revisions"][0]["*"] return mwparserfromhell.parse(text) diff --git a/docs/usage.rst b/docs/usage.rst index c471397..a1adfce 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -12,19 +12,19 @@ extra methods. For example:: >>> text = "I has a template! {{foo|bar|baz|eggs=spam}} See it?" >>> wikicode = mwparserfromhell.parse(text) - >>> print wikicode + >>> print(wikicode) I has a template! {{foo|bar|baz|eggs=spam}} See it? >>> templates = wikicode.filter_templates() - >>> print templates + >>> print(templates) ['{{foo|bar|baz|eggs=spam}}'] >>> template = templates[0] - >>> print template.name + >>> print(template.name) foo - >>> print template.params + >>> print(template.params) ['bar', 'baz', 'eggs=spam'] - >>> print template.get(1).value + >>> print(template.get(1).value) bar - >>> print template.get("eggs").value + >>> print(template.get("eggs").value) spam Since nodes can contain other nodes, getting nested templates is trivial:: @@ -38,14 +38,14 @@ templates manually. This is possible because nodes can contain additional :class:`.Wikicode` objects:: >>> code = mwparserfromhell.parse("{{foo|this {{includes a|template}}}}") - >>> print code.filter_templates(recursive=False) + >>> print(code.filter_templates(recursive=False)) ['{{foo|this {{includes a|template}}}}'] >>> foo = code.filter_templates(recursive=False)[0] - >>> print foo.get(1).value + >>> print(foo.get(1).value) this {{includes a|template}} - >>> print foo.get(1).value.filter_templates()[0] + >>> print(foo.get(1).value.filter_templates()[0]) {{includes a|template}} - >>> print foo.get(1).value.filter_templates()[0].get(1).value + >>> print(foo.get(1).value.filter_templates()[0].get(1).value) template Templates can be easily modified to add, remove, or alter params. @@ -61,24 +61,24 @@ takes care of capitalization and whitespace:: ... if template.name.matches("Cleanup") and not template.has("date"): ... template.add("date", "July 2012") ... - >>> print code + >>> print(code) {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{uncategorized}} >>> code.replace("{{uncategorized}}", "{{bar-stub}}") - >>> print code + >>> print(code) {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}} - >>> print code.filter_templates() + >>> print(code.filter_templates()) ['{{cleanup|date=July 2012}}', '{{bar-stub}}'] -You can then convert ``code`` back into a regular :class:`unicode` object (for -saving the page!) by calling :func:`unicode` on it:: +You can then convert ``code`` back into a regular :class:`str` object (for +saving the page!) by calling :func:`str` on it:: - >>> text = unicode(code) - >>> print text + >>> text = str(code) + >>> print(text) {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}} >>> text == code True -(Likewise, use :func:`str(code) ` in Python 3.) +(Likewise, use :func:`unicode(code) ` in Python 2.) For more tips, check out :class:`Wikicode's full method list <.Wikicode>` and the :mod:`list of Nodes <.nodes>`. diff --git a/mwparserfromhell/wikicode.py b/mwparserfromhell/wikicode.py index c24bc5f..ebfa9c7 100644 --- a/mwparserfromhell/wikicode.py +++ b/mwparserfromhell/wikicode.py @@ -567,7 +567,7 @@ class Wikicode(StringMixIn): following:: >>> text = "Lorem ipsum {{foo|bar|{{baz}}|spam=eggs}}" - >>> print mwparserfromhell.parse(text).get_tree() + >>> print(mwparserfromhell.parse(text).get_tree()) Lorem ipsum {{ foo