[stem/master] Use python3 print statements for tutorial examples

commit f8d646ec969f0f7a7b4bf8afc2564a0c787832a9 Author: Damian Johnson <atagar@torproject.org> Date: Tue May 26 09:51:44 2015 -0700 Use python3 print statements for tutorial examples Swiching our print statements to something compatible with both python 2.x and 3.x. With this our tests now pass with both versions. --- docs/_static/example/client_usage_using_pycurl.py | 8 ++++---- docs/_static/example/compare_flags.py | 10 +++++----- docs/_static/example/current_descriptors.py | 4 ++-- docs/_static/example/custom_path_selection.py | 4 ++-- .../example/descriptor_from_tor_control_socket.py | 2 +- .../example/descriptor_from_tor_data_directory.py | 2 +- docs/_static/example/ephemeral_hidden_services.py | 8 ++++---- docs/_static/example/exit_used.py | 16 ++++++++-------- docs/_static/example/get_hidden_service_descriptor.py | 2 +- docs/_static/example/hello_world.py | 2 +- docs/_static/example/introduction_points.py | 4 ++-- docs/_static/example/list_circuits.py | 6 +++--- docs/_static/example/outdated_relays.py | 10 +++++----- docs/_static/example/past_descriptors.py | 2 +- .../example/persisting_a_consensus_with_parse_file.py | 2 +- docs/_static/example/read_with_parse_file.py | 2 +- docs/_static/example/reading_twitter.py | 8 ++++---- docs/_static/example/running_hidden_service.py | 10 +++++----- docs/_static/example/tor_descriptors.py | 4 ++-- docs/_static/example/utilities.py | 14 +++++++------- docs/_static/example/validate_descriptor_content.py | 2 +- docs/_static/example/votes_by_bandwidth_authorities.py | 6 +++--- 22 files changed, 64 insertions(+), 64 deletions(-) diff --git a/docs/_static/example/client_usage_using_pycurl.py b/docs/_static/example/client_usage_using_pycurl.py index 847c106..a15db5e 100644 --- a/docs/_static/example/client_usage_using_pycurl.py +++ b/docs/_static/example/client_usage_using_pycurl.py @@ -35,10 +35,10 @@ def query(url): def print_bootstrap_lines(line): if "Bootstrapped " in line: - print term.format(line, term.Color.BLUE) + print(term.format(line, term.Color.BLUE)) -print term.format("Starting Tor:\n", term.Attr.BOLD) +print(term.format("Starting Tor:\n", term.Attr.BOLD)) tor_process = stem.process.launch_tor_with_config( config = { @@ -48,7 +48,7 @@ tor_process = stem.process.launch_tor_with_config( init_msg_handler = print_bootstrap_lines, ) -print term.format("\nChecking our endpoint:\n", term.Attr.BOLD) -print term.format(query("https://www.atagar.com/echo.php"), term.Color.BLUE) +print(term.format("\nChecking our endpoint:\n", term.Attr.BOLD)) +print(term.format(query("https://www.atagar.com/echo.php"), term.Color.BLUE)) tor_process.kill() # stops tor diff --git a/docs/_static/example/compare_flags.py b/docs/_static/example/compare_flags.py index a668e11..84f8a4f 100644 --- a/docs/_static/example/compare_flags.py +++ b/docs/_static/example/compare_flags.py @@ -30,12 +30,12 @@ for fingerprint in all_fingerprints: maatuska_vote = votes['maatuska'].routers.get(fingerprint) if not moria1_vote and not maatuska_vote: - print "both moria1 and maatuska haven't voted about %s" % fingerprint + print("both moria1 and maatuska haven't voted about %s" % fingerprint) elif not moria1_vote: - print "moria1 hasn't voted about %s" % fingerprint + print("moria1 hasn't voted about %s" % fingerprint) elif not maatuska_vote: - print "maatuska hasn't voted about %s" % fingerprint + print("maatuska hasn't voted about %s" % fingerprint) elif 'Running' in moria1_vote.flags and 'Running' not in maatuska_vote.flags: - print "moria1 has the Running flag but maatuska doesn't: %s" % fingerprint + print("moria1 has the Running flag but maatuska doesn't: %s" % fingerprint) elif 'Running' in maatuska_vote.flags and 'Running' not in moria1_vote.flags: - print "maatuska has the Running flag but moria1 doesn't: %s" % fingerprint + print("maatuska has the Running flag but moria1 doesn't: %s" % fingerprint) diff --git a/docs/_static/example/current_descriptors.py b/docs/_static/example/current_descriptors.py index 9ddca01..f86b5b9 100644 --- a/docs/_static/example/current_descriptors.py +++ b/docs/_static/example/current_descriptors.py @@ -4,6 +4,6 @@ downloader = DescriptorDownloader() try: for desc in downloader.get_consensus().run(): - print "found relay %s (%s)" % (desc.nickname, desc.fingerprint) + print("found relay %s (%s)" % (desc.nickname, desc.fingerprint)) except Exception as exc: - print "Unable to retrieve the consensus: %s" % exc + print("Unable to retrieve the consensus: %s" % exc) diff --git a/docs/_static/example/custom_path_selection.py b/docs/_static/example/custom_path_selection.py index 48f6ee7..610ec28 100644 --- a/docs/_static/example/custom_path_selection.py +++ b/docs/_static/example/custom_path_selection.py @@ -74,6 +74,6 @@ with stem.control.Controller.from_port() as controller: for fingerprint in relay_fingerprints: try: time_taken = scan(controller, [fingerprint, EXIT_FINGERPRINT]) - print '%s => %0.2f seconds' % (fingerprint, time_taken) + print('%s => %0.2f seconds' % (fingerprint, time_taken)) except Exception as exc: - print '%s => %s' % (fingerprint, exc) + print('%s => %s' % (fingerprint, exc)) diff --git a/docs/_static/example/descriptor_from_tor_control_socket.py b/docs/_static/example/descriptor_from_tor_control_socket.py index 202f66e..349f6f6 100644 --- a/docs/_static/example/descriptor_from_tor_control_socket.py +++ b/docs/_static/example/descriptor_from_tor_control_socket.py @@ -4,4 +4,4 @@ with Controller.from_port(port = 9051) as controller: controller.authenticate() for desc in controller.get_network_statuses(): - print "found relay %s (%s)" % (desc.nickname, desc.fingerprint) + print("found relay %s (%s)" % (desc.nickname, desc.fingerprint)) diff --git a/docs/_static/example/descriptor_from_tor_data_directory.py b/docs/_static/example/descriptor_from_tor_data_directory.py index cdfd78b..5125cf4 100644 --- a/docs/_static/example/descriptor_from_tor_data_directory.py +++ b/docs/_static/example/descriptor_from_tor_data_directory.py @@ -1,4 +1,4 @@ from stem.descriptor import parse_file for desc in parse_file('/home/atagar/.tor/cached-consensus'): - print 'found relay %s (%s)' % (desc.nickname, desc.fingerprint) + print('found relay %s (%s)' % (desc.nickname, desc.fingerprint)) diff --git a/docs/_static/example/ephemeral_hidden_services.py b/docs/_static/example/ephemeral_hidden_services.py index b3b621b..85d36fa 100644 --- a/docs/_static/example/ephemeral_hidden_services.py +++ b/docs/_static/example/ephemeral_hidden_services.py @@ -9,7 +9,7 @@ def index(): return "<h1>Hi Grandma!</h1>" -print ' * Connecting to tor' +print(' * Connecting to tor') with Controller.from_port() as controller: controller.authenticate() @@ -17,11 +17,11 @@ with Controller.from_port() as controller: # Create a hidden service where visitors of port 80 get redirected to local # port 5000 (this is where Flask runs by default). - print " * Creating our hidden service in %s" % hidden_service_dir + print(" * Creating our hidden service in %s" % hidden_service_dir) response = controller.create_ephemeral_hidden_service({80: 5000}, await_publication = True) - print " * Our service is available at %s.onion, press ctrl+c to quit" % response.service_id + print(" * Our service is available at %s.onion, press ctrl+c to quit" % response.service_id) try: app.run() finally: - print " * Shutting down our hidden service" + print(" * Shutting down our hidden service") diff --git a/docs/_static/example/exit_used.py b/docs/_static/example/exit_used.py index 7ecd19a..9a16c13 100644 --- a/docs/_static/example/exit_used.py +++ b/docs/_static/example/exit_used.py @@ -4,8 +4,8 @@ from stem import StreamStatus from stem.control import EventType, Controller def main(): - print "Tracking requests for tor exits. Press 'enter' to end." - print + print("Tracking requests for tor exits. Press 'enter' to end.") + print("") with Controller.from_port() as controller: controller.authenticate() @@ -23,12 +23,12 @@ def stream_event(controller, event): exit_fingerprint = circ.path[-1][0] exit_relay = controller.get_network_status(exit_fingerprint) - print "Exit relay for our connection to %s" % (event.target) - print " address: %s:%i" % (exit_relay.address, exit_relay.or_port) - print " fingerprint: %s" % exit_relay.fingerprint - print " nickname: %s" % exit_relay.nickname - print " locale: %s" % controller.get_info("ip-to-country/%s" % exit_relay.address, 'unknown') - print + print("Exit relay for our connection to %s" % (event.target)) + print(" address: %s:%i" % (exit_relay.address, exit_relay.or_port)) + print(" fingerprint: %s" % exit_relay.fingerprint) + print(" nickname: %s" % exit_relay.nickname) + print(" locale: %s" % controller.get_info("ip-to-country/%s" % exit_relay.address, 'unknown')) + print("") if __name__ == '__main__': diff --git a/docs/_static/example/get_hidden_service_descriptor.py b/docs/_static/example/get_hidden_service_descriptor.py index baeec1d..98c1bf9 100644 --- a/docs/_static/example/get_hidden_service_descriptor.py +++ b/docs/_static/example/get_hidden_service_descriptor.py @@ -5,4 +5,4 @@ with Controller.from_port(port = 9051) as controller: # descriptor of duck-duck-go's hidden service (http://3g2upl4pq6kufc4m.onion) - print controller.get_hidden_service_descriptor('3g2upl4pq6kufc4m') + print(controller.get_hidden_service_descriptor('3g2upl4pq6kufc4m')) diff --git a/docs/_static/example/hello_world.py b/docs/_static/example/hello_world.py index 88dd881..4e5df98 100644 --- a/docs/_static/example/hello_world.py +++ b/docs/_static/example/hello_world.py @@ -6,4 +6,4 @@ with Controller.from_port(port = 9051) as controller: bytes_read = controller.get_info("traffic/read") bytes_written = controller.get_info("traffic/written") - print "My Tor relay has read %s bytes and written %s." % (bytes_read, bytes_written) + print("My Tor relay has read %s bytes and written %s." % (bytes_read, bytes_written)) diff --git a/docs/_static/example/introduction_points.py b/docs/_static/example/introduction_points.py index 3c97e47..973a71c 100644 --- a/docs/_static/example/introduction_points.py +++ b/docs/_static/example/introduction_points.py @@ -4,7 +4,7 @@ with Controller.from_port(port = 9051) as controller: controller.authenticate() desc = controller.get_hidden_service_descriptor('3g2upl4pq6kufc4m') - print "DuckDuckGo's introduction points are...\n" + print("DuckDuckGo's introduction points are...\n") for introduction_point in desc.introduction_points(): - print ' %s:%s => %s' % (introduction_point.address, introduction_point.port, introduction_point.identifier) + print(' %s:%s => %s' % (introduction_point.address, introduction_point.port, introduction_point.identifier)) diff --git a/docs/_static/example/list_circuits.py b/docs/_static/example/list_circuits.py index 61fdd5e..b89e99f 100644 --- a/docs/_static/example/list_circuits.py +++ b/docs/_static/example/list_circuits.py @@ -8,8 +8,8 @@ with Controller.from_port(port = 9051) as controller: if circ.status != CircStatus.BUILT: continue - print - print "Circuit %s (%s)" % (circ.id, circ.purpose) + print("") + print("Circuit %s (%s)" % (circ.id, circ.purpose)) for i, entry in enumerate(circ.path): div = '+' if (i == len(circ.path) - 1) else '|' @@ -18,4 +18,4 @@ with Controller.from_port(port = 9051) as controller: desc = controller.get_network_status(fingerprint, None) address = desc.address if desc else 'unknown' - print " %s- %s (%s, %s)" % (div, fingerprint, nickname, address) + print(" %s- %s (%s, %s)" % (div, fingerprint, nickname, address)) diff --git a/docs/_static/example/outdated_relays.py b/docs/_static/example/outdated_relays.py index a78c6ee..67d0ea9 100644 --- a/docs/_static/example/outdated_relays.py +++ b/docs/_static/example/outdated_relays.py @@ -4,16 +4,16 @@ from stem.version import Version downloader = DescriptorDownloader() count, with_contact = 0, 0 -print "Checking for outdated relays..." -print +print("Checking for outdated relays...") +print("") for desc in downloader.get_server_descriptors(): if desc.tor_version < Version('0.2.3.0'): count += 1 if desc.contact: - print ' %-15s %s' % (desc.tor_version, desc.contact.decode("utf-8", "replace")) + print(' %-15s %s' % (desc.tor_version, desc.contact.decode("utf-8", "replace"))) with_contact += 1 -print -print "%i outdated relays found, %i had contact information" % (count, with_contact) +print("") +print("%i outdated relays found, %i had contact information" % (count, with_contact)) diff --git a/docs/_static/example/past_descriptors.py b/docs/_static/example/past_descriptors.py index 4b3e5cb..4100484 100644 --- a/docs/_static/example/past_descriptors.py +++ b/docs/_static/example/past_descriptors.py @@ -2,4 +2,4 @@ from stem.descriptor.reader import DescriptorReader with DescriptorReader(["/home/atagar/server-descriptors-2013-03.tar"]) as reader: for desc in reader: - print "found relay %s (%s)" % (desc.nickname, desc.fingerprint) + print("found relay %s (%s)" % (desc.nickname, desc.fingerprint)) diff --git a/docs/_static/example/persisting_a_consensus_with_parse_file.py b/docs/_static/example/persisting_a_consensus_with_parse_file.py index bbec73a..4a78c46 100644 --- a/docs/_static/example/persisting_a_consensus_with_parse_file.py +++ b/docs/_static/example/persisting_a_consensus_with_parse_file.py @@ -7,4 +7,4 @@ consensus = next(parse_file( )) for fingerprint, relay in consensus.routers.items(): - print "%s: %s" % (fingerprint, relay.nickname) + print("%s: %s" % (fingerprint, relay.nickname)) diff --git a/docs/_static/example/read_with_parse_file.py b/docs/_static/example/read_with_parse_file.py index 96ec99e..da78b21 100644 --- a/docs/_static/example/read_with_parse_file.py +++ b/docs/_static/example/read_with_parse_file.py @@ -3,4 +3,4 @@ from stem.descriptor import parse_file server_descriptors = parse_file('/tmp/descriptor_dump', descriptor_type = 'server-descriptor 1.0') for relay in server_descriptors: - print relay.fingerprint + print(relay.fingerprint) diff --git a/docs/_static/example/reading_twitter.py b/docs/_static/example/reading_twitter.py index b5dc684..28f840d 100644 --- a/docs/_static/example/reading_twitter.py +++ b/docs/_static/example/reading_twitter.py @@ -78,10 +78,10 @@ tor_process = stem.process.launch_tor_with_config( try: for index, tweet in enumerate(poll_twitter_feed('ioerror', 3)): - print "%i. %s" % (index + 1, tweet["created_at"]) - print tweet["text"] - print + print("%i. %s" % (index + 1, tweet["created_at"])) + print(tweet["text"]) + print("") except IOError, exc: - print exc + print(exc) finally: tor_process.kill() # stops tor diff --git a/docs/_static/example/running_hidden_service.py b/docs/_static/example/running_hidden_service.py index 4a4d923..5392ef4 100644 --- a/docs/_static/example/running_hidden_service.py +++ b/docs/_static/example/running_hidden_service.py @@ -12,7 +12,7 @@ def index(): return "<h1>Hi Grandma!</h1>" -print ' * Connecting to tor' +print(' * Connecting to tor') with Controller.from_port() as controller: controller.authenticate() @@ -25,16 +25,16 @@ with Controller.from_port() as controller: # Create a hidden service where visitors of port 80 get redirected to local # port 5000 (this is where Flask runs by default). - print " * Creating our hidden service in %s" % hidden_service_dir + print(" * Creating our hidden service in %s" % hidden_service_dir) result = controller.create_hidden_service(hidden_service_dir, 80, target_port = 5000) # The hostname is only available when we can read the hidden service # directory. This requires us to be running with the same user as tor. if result.hostname: - print " * Our service is available at %s, press ctrl+c to quit" % result.hostname + print(" * Our service is available at %s, press ctrl+c to quit" % result.hostname) else: - print " * Unable to determine our service's hostname, probably due to being unable to read the hidden service directory" + print(" * Unable to determine our service's hostname, probably due to being unable to read the hidden service directory") try: app.run() @@ -43,6 +43,6 @@ with Controller.from_port() as controller: # want to delete the hidden service directory if you'd like to have this # same *.onion address in the future. - print " * Shutting down our hidden service" + print(" * Shutting down our hidden service") controller.remove_hidden_service(hidden_service_dir) shutil.rmtree(hidden_service_dir) diff --git a/docs/_static/example/tor_descriptors.py b/docs/_static/example/tor_descriptors.py index 2b59ad4..0ec0666 100644 --- a/docs/_static/example/tor_descriptors.py +++ b/docs/_static/example/tor_descriptors.py @@ -14,7 +14,7 @@ def get_bw_to_relay(): if desc.exit_policy.is_exiting_allowed(): bw_to_relay.setdefault(desc.observed_bandwidth, []).append(desc.nickname) except Exception as exc: - print "Unable to retrieve the server descriptors: %s" % exc + print("Unable to retrieve the server descriptors: %s" % exc) return bw_to_relay @@ -25,7 +25,7 @@ count = 1 for bw_value in sorted(bw_to_relay.keys(), reverse = True): for nickname in bw_to_relay[bw_value]: - print "%i. %s (%s/s)" % (count, nickname, str_tools.size_label(bw_value, 2)) + print("%i. %s (%s/s)" % (count, nickname, str_tools.size_label(bw_value, 2))) count += 1 if count > 15: diff --git a/docs/_static/example/utilities.py b/docs/_static/example/utilities.py index 460f6ff..0e8f01f 100644 --- a/docs/_static/example/utilities.py +++ b/docs/_static/example/utilities.py @@ -6,23 +6,23 @@ from stem.util.system import pid_by_name resolvers = system_resolvers() if not resolvers: - print "Stem doesn't support any connection resolvers on our platform." + print("Stem doesn't support any connection resolvers on our platform.") sys.exit(1) picked_resolver = resolvers[0] # lets just opt for the first -print "Our platform supports connection resolution via: %s (picked %s)" % (', '.join(resolvers), picked_resolver) +print("Our platform supports connection resolution via: %s (picked %s)" % (', '.join(resolvers), picked_resolver)) tor_pids = pid_by_name('tor', multiple = True) if not tor_pids: - print "Unable to get tor's pid. Is it running?" + print("Unable to get tor's pid. Is it running?") sys.exit(1) elif len(tor_pids) > 1: - print "You're running %i instances of tor, picking the one with pid %i" % (len(tor_pids), tor_pids[0]) + print("You're running %i instances of tor, picking the one with pid %i" % (len(tor_pids), tor_pids[0])) else: - print "Tor is running with pid %i" % tor_pids[0] + print("Tor is running with pid %i" % tor_pids[0]) -print "\nConnections:\n" +print("\nConnections:\n") for conn in get_connections(picked_resolver, process_pid = tor_pids[0], process_name = 'tor'): - print " %s:%s => %s:%s" % (conn.local_address, conn.local_port, conn.remote_address, conn.remote_port) + print(" %s:%s => %s:%s" % (conn.local_address, conn.local_port, conn.remote_address, conn.remote_port)) diff --git a/docs/_static/example/validate_descriptor_content.py b/docs/_static/example/validate_descriptor_content.py index f6b27a5..f47ed5d 100644 --- a/docs/_static/example/validate_descriptor_content.py +++ b/docs/_static/example/validate_descriptor_content.py @@ -1,4 +1,4 @@ from stem.descriptor import parse_file for desc in parse_file('/home/atagar/.tor/cached-consensus', validate = True): - print 'found relay %s (%s)' % (desc.nickname, desc.fingerprint) + print('found relay %s (%s)' % (desc.nickname, desc.fingerprint)) diff --git a/docs/_static/example/votes_by_bandwidth_authorities.py b/docs/_static/example/votes_by_bandwidth_authorities.py index e957be5..f6816b7 100644 --- a/docs/_static/example/votes_by_bandwidth_authorities.py +++ b/docs/_static/example/votes_by_bandwidth_authorities.py @@ -14,7 +14,7 @@ for authority in remote.get_authorities().values(): for authority_name, query in queries.items(): try: - print "Getting %s's vote from %s:" % (authority_name, query.download_url) + print("Getting %s's vote from %s:" % (authority_name, query.download_url)) measured, unmeasured = 0, 0 @@ -24,6 +24,6 @@ for authority_name, query in queries.items(): else: unmeasured += 1 - print ' %i measured entries and %i unmeasured' % (measured, unmeasured) + print(' %i measured entries and %i unmeasured' % (measured, unmeasured)) except Exception as exc: - print " failed to get the vote (%s)" % exc + print(" failed to get the vote (%s)" % exc)
participants (1)
-
atagar@torproject.org