Upgrade to Python 3
David Blume

David Blume commited on 2020-12-27 10:09:33
Showing 4 changed files, with 21 additions and 27 deletions.

... ...
@@ -3,6 +3,8 @@
3 3
 This is just a test project. You can do anything here, test python
4 4
 and IDE projects.
5 5
 
6
+This branch is of Python 3 code. There's a python2 branch too.
7
+
6 8
 ### Getting the project
7 9
 
8 10
 You can get a copy of this project by clicking on the
... ...
@@ -1,13 +1,11 @@
1
-#!/usr/bin/env python
2
-# From https://dl.dropboxusercontent.com/u/3967849/pyru/_build/html/threading.html
3
-from __future__ import print_function
4
-import threading, Queue
1
+#!/usr/bin/env python3
2
+# From https://pybay.com/site_media/slides/raymond2017-keynote/threading.html
3
+import threading, queue
5 4
 
6
-#############################################################################
7 5
 
8 6
 _counter = 0
9 7
 
10
-counter_queue = Queue.Queue()
8
+counter_queue = queue.Queue()
11 9
 
12 10
 def counter_manager():
13 11
     'I have EXCLUSIVE rights to update the _counter variable'
... ...
@@ -21,9 +19,8 @@ def counter_manager():
21 19
             '---------------'])
22 20
         counter_queue.task_done()
23 21
 
24
-#############################################################################
25 22
 
26
-print_queue = Queue.Queue()
23
+print_queue = queue.Queue()
27 24
 
28 25
 def print_manager():
29 26
     'I have EXCLUSIVE rights to call the "print" keyword'
... ...
@@ -33,7 +30,6 @@ def print_manager():
33 30
             print(line)
34 31
         print_queue.task_done()
35 32
 
36
-#############################################################################
37 33
 
38 34
 def worker():
39 35
     'My job is to increment the counter and print the current count'
... ...
@@ -1,7 +1,7 @@
1
-#!/usr/bin/env python
2
-# https://dl.dropboxusercontent.com/u/3967849/pyru/_build/html/process.html
3
-from __future__ import print_function
4
-import urllib
1
+#!/usr/bin/env python3
2
+# From https://pybay.com/site_media/slides/raymond2017-keynote/process.html
3
+import urllib.request
4
+import urllib.error
5 5
 
6 6
 sites = [
7 7
 #    'https://www.yahoo.com/',
... ...
@@ -9,7 +9,7 @@ sites = [
9 9
 #    'http://www.python.org',
10 10
 #    'http://www.jython.org',
11 11
     'http://www.pypy.org',
12
-#    'http://www.perl.org',
12
+    'http://www.perl.org',
13 13
 #    'http://www.cisco.com',
14 14
 #    'http://www.facebook.com',
15 15
 #    'http://www.twitter.com',
... ...
@@ -21,13 +21,14 @@ sites = [
21 21
 ]
22 22
 
23 23
 
24
-def sitesize(url):
24
+def sitesize(url: str):
25 25
     ''' Determine the size of a website '''
26
-    # TODO: Use requests instead, or migrate to python3
27
-    f = urllib.urlopen(url)  # Could use "with" in python3
26
+    try:
27
+        with urllib.request.urlopen(url) as f:
28 28
             page = f.read()
29
-    f.close()
30 29
         return url, len(page)
30
+    except urllib.error.HTTPError as e:
31
+        return url, str(e)
31 32
 
32 33
 
33 34
 def run():
... ...
@@ -1,8 +1,7 @@
1
-#!/usr/bin/env python
1
+#!/usr/bin/env python3
2 2
 # -*- coding: utf-8 -*-
3 3
 #
4 4
 # "Hello Friends" in Chinese: 朋友你好
5
-from __future__ import print_function
6 5
 import os
7 6
 import sys
8 7
 import time
... ...
@@ -11,7 +10,7 @@ import counter.counter
11 10
 import sitesize.sitesize
12 11
 
13 12
 
14
-def set_v_print(verbose):
13
+def set_v_print(verbose: bool):
15 14
     """
16 15
     Defines the function v_print.
17 16
     It prints if verbose is true, otherwise, it does nothing.
... ...
@@ -19,14 +18,10 @@ def set_v_print(verbose):
19 18
     :param verbose: A bool to determine if v_print will print its args.
20 19
     """
21 20
     global v_print
22
-    if verbose:
23
-        def v_print(*s):
24
-            print(' '.join([i.encode('utf8') for i in s]))
25
-    else:
26
-        v_print = lambda *s: None
21
+    v_print = print if verbose else lambda *a, **k: None
27 22
 
28 23
 
29
-def main(debug):
24
+def main(debug: bool):
30 25
     start_time = time.time()
31 26
     localdir = os.path.abspath(os.path.dirname(sys.argv[0]))
32 27
     v_print("Running counter...")
33 28