[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Helper function for descriptor tutorial



commit 081228149f80448dc9097d7e260b2738792250ca
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Mon Oct 29 18:59:23 2012 -0700

    Helper function for descriptor tutorial
    
    The 'Mirror Mirror on the Wall' tutorial had some repetition due to having both
    an example for getting descriptors from the disk and through the control port.
    Moving that code to a helper function so I can drop the common parts from the
    second example.
---
 docs/api.rst          |    2 +-
 docs/tutorial.rst     |   46 +++++++++++++++++++++-------------------------
 test/unit/tutorial.py |   18 ++++++++++++------
 3 files changed, 34 insertions(+), 32 deletions(-)

diff --git a/docs/api.rst b/docs/api.rst
index 938a10b..c876778 100644
--- a/docs/api.rst
+++ b/docs/api.rst
@@ -7,7 +7,7 @@ Controller
 * **Core**
 
  * `stem.control <api/control.html>`_ - **Controller used to talk with Tor**.
- * `stem.connection <api/connection.html>`_ - Connection and authentication to the Tor control port or socket.
+ * `stem.connection <api/connection.html>`_ - Connection and authentication to the Tor control socket.
  * `stem.socket <api/socket.html>`_ - Low level control socket used to talk with Tor.
  * `stem.process <api/process.html>`_ - Launcher for the Tor process.
  * `stem.response <api/response.html>`_ - Messages that Tor may provide the controller.
diff --git a/docs/tutorial.rst b/docs/tutorial.rst
index dbd83c3..c442326 100644
--- a/docs/tutorial.rst
+++ b/docs/tutorial.rst
@@ -89,16 +89,22 @@ To read this file we'll use the :class:`~stem.descriptor.reader.DescriptorReader
   from stem.descriptor.reader import DescriptorReader
   from stem.util import str_tools
   
-  bw_to_relay = {} # mapping of observed bandwidth to the relay nicknames
-  
-  with DescriptorReader(["/home/atagar/.tor/cached-descriptors"]) as reader:
-    for desc in reader:
-      if desc.exit_policy.is_exiting_allowed():
-        bw_to_relay.setdefault(desc.observed_bandwidth, []).append(desc.nickname)
+  # provides a mapping of observed bandwidth to the relay nicknames
+  def get_bw_to_relay():
+    bw_to_relay = {}
+    
+    with DescriptorReader(["/home/atagar/.tor/cached-descriptors"]) as reader:
+      for desc in reader:
+        if desc.exit_policy.is_exiting_allowed():
+          bw_to_relay.setdefault(desc.observed_bandwidth, []).append(desc.nickname)
+    
+    return bw_to_relay
   
   # prints the top fifteen relays
   
+  bw_to_relay = get_bw_to_relay()
   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.get_size_label(bw_value, 2))
@@ -130,25 +136,15 @@ This can be easily done through the controller too...
 
 ::
 
-  import sys 
-  from stem.control import Controller
-  from stem.util import str_tools
-  
-  bw_to_relay = {} # mapping of observed bandwidth to the relay nicknames
-  
-  with Controller.from_port(control_port = 9051) as controller:
-    controller.authenticate()
+  def get_bw_to_relay():
+    bw_to_relay = {}
     
-    for desc in controller.get_server_descriptors():
-      if desc.exit_policy.is_exiting_allowed():
-        bw_to_relay.setdefault(desc.observed_bandwidth, []).append(desc.nickname)
-  
-  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.get_size_label(bw_value, 2))
-      count += 1
+    with Controller.from_port(control_port = 9051) as controller:
+      controller.authenticate()
       
-      if count > 15: 
-        sys.exit()
+      for desc in controller.get_server_descriptors():
+        if desc.exit_policy.is_exiting_allowed():
+          bw_to_relay.setdefault(desc.observed_bandwidth, []).append(desc.nickname)
+    
+    return bw_to_relay
 
diff --git a/test/unit/tutorial.py b/test/unit/tutorial.py
index 6f538cc..f8a8d09 100644
--- a/test/unit/tutorial.py
+++ b/test/unit/tutorial.py
@@ -54,16 +54,22 @@ class TestTutorial(unittest.TestCase):
       )))
     })
     
-    bw_to_relay = {} # mapping of observed bandwidth to the relay nicknames
-    
-    with reader_wrapper as reader:
-      for desc in reader:
-        if desc.exit_policy.is_exiting_allowed():
-          bw_to_relay.setdefault(desc.observed_bandwidth, []).append(desc.nickname)
+    # provides a mapping of observed bandwidth to the relay nicknames
+    def get_bw_to_relay():
+      bw_to_relay = {}
+      
+      with reader_wrapper as reader:
+        for desc in reader:
+          if desc.exit_policy.is_exiting_allowed():
+            bw_to_relay.setdefault(desc.observed_bandwidth, []).append(desc.nickname)
+      
+      return bw_to_relay
     
     # prints the top fifteen relays
     
+    bw_to_relay = get_bw_to_relay()
     count = 1
+    
     for bw_value in sorted(bw_to_relay.keys(), reverse = True):
       for nickname in bw_to_relay[bw_value]:
         expected_line = "%i. speedyexit (102.13 KB/s)" % count



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits