Commits

Nick Efford  committed 5dcdb7e

Added new web module.

  • Participants
  • Parent commits 7aea648
  • Tags v1.1

Comments (0)

Files changed (4)

File py3tools/io.py

 ########################################################################
-## py3tools.io - Various tools to simplify I/O in Python 3.           ##
+## py3tools.io - various tools to simplify I/O in Python 3.           ##
 ##                                                                    ##
 ## Author  : Nick Efford <nick.efford (at) gmail.com>                 ##
 ## Created : 2011-03-19                                               ##
 """Various tools to simplify I/O."""
 
 
-__author__ = 'Nick Efford'
-__version__ = '1.0'
+__author__ = "Nick Efford"
+__version__ = "1.1"
 
 
 def printf(template, *args, **kwargs):

File py3tools/string.py

 """Various tools to simplify manipulation of text strings."""
 
 
-__author__ = 'Nick Efford'
-__version__ = '1.0'
+__author__ = "Nick Efford"
+__version__ = "1.1"
 
 
 def join(sequence, separator=''):

File py3tools/timing.py

 from datetime import datetime
 
 
-__author__ = 'Nick Efford'
-__version__ = '1.0'
+__author__ = "Nick Efford"
+__version__ = "1.1"
 
 
 class IntervalTimer:

File py3tools/web.py

+########################################################################
+## py3tools.web - tools to simplify retrieval of web resources.       ##
+##                                                                    ##
+## Author  : Nick Efford <nick.efford (at) gmail.com>                 ##
+## Created : 2013-06-25                                               ##
+########################################################################
+
+"""Tools to simplify retrieval of web resources."""
+
+
+import io
+import json
+import urllib.request
+
+
+__author__ = "Nick Efford"
+__version__ = "1.1"
+
+
+def get_bytes(url):
+    """Retrieves bytes from the given URL."""
+
+    source = urllib.request.urlopen(url)
+    content = source.read()
+    return content
+
+
+def get_text(url, encoding="utf-8"):
+    """Retrieves text data from the given URL."""
+
+    source = urllib.request.urlopen(url)
+    content = source.read().decode(encoding)
+    return content
+
+
+def get_json(url, encoding="utf-8"):
+    """Retrieves JSON data from the given URL.
+
+       The retrieved data are returned as a dictionary.
+    """
+
+    source = urllib.request.urlopen(url)
+    content = source.read().decode(encoding)
+    return json.loads(content)
+
+
+def open(url, mode="rt", encoding="utf-8"):
+    """Opens the given URL, returning a file-like object.
+
+       Valid modes are "rt" (the default) and "rb".
+    """
+
+    if mode == "rt":
+        source = urllib.request.urlopen(url)
+        content = source.read().decode(encoding)
+        return io.StringIO(content)
+    elif mode == "rb":
+        source = urllib.request.urlopen(url)
+        content = source.read()
+        return io.BytesIO(content)
+    else:
+        raise ValueError("invalid mode")