[tor-commits] [metrics-tasks/master] task-6329: Add a 'count' parameter to 'as-sets' and 'countries' output

karsten at torproject.org karsten at torproject.org
Sun Jul 15 08:17:02 UTC 2012


commit 39fe1e46b9c4a13bd181edbde285a3e82a5cadd9
Author: delber <delber at riseup.net>
Date:   Sat Jul 14 12:53:25 2012 +0000

    task-6329: Add a 'count' parameter to 'as-sets' and 'countries' output
---
 task-6329/README              |    8 ++++----
 task-6329/tor-relays-stats.py |   18 ++++++++++--------
 2 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/task-6329/README b/task-6329/README
index 51f9dd7..443a3d6 100644
--- a/task-6329/README
+++ b/task-6329/README
@@ -10,9 +10,9 @@ Usage
 Usage: tor-relays-stats.py <output> [args ...]
 
 Where <output> is one of:
- - countries [FLAGS]
+ - countries [COUNT] [FLAGS]
    relative percentage of the consensus in each countries
- - as-sets [FLAGS] [COUNTRIES]
+ - as-sets [COUNT] [FLAGS] [COUNTRIES]
    relative percentage of the consensus in each AS sets
  - top [COUNT] [FLAGS] [COUNTRIES]
    top relays according their place in the whole consensus
@@ -21,8 +21,8 @@ Examples:
 
  - To get the top five exit nodes in France:
    tor-relays-stats.py top 5 Exit fr
- - To get weights of each AS of all relays in Germany:
-   tor-relays-stats.py as-sets Running de
+ - To get weights of the top ten AS of all relays in Germany:
+   tor-relays-stats.py as-sets 10 Running de
 
 Retrieving the data
 -------------------
diff --git a/task-6329/tor-relays-stats.py b/task-6329/tor-relays-stats.py
index ed81bb5..d6b3e86 100755
--- a/task-6329/tor-relays-stats.py
+++ b/task-6329/tor-relays-stats.py
@@ -40,7 +40,8 @@ class RelayStats(object):
             relays.append(relay)
         return relays
 
-    def output_countries(self, flags=''):
+    def output_countries(self, count='10', flags=''):
+        count = int(count)
         flags = flags.split()
         relays = self.get_relays(flags)
         countries = {}
@@ -53,10 +54,11 @@ class RelayStats(object):
         ranking = sorted(countries.iteritems(), key=operator.itemgetter(1))
         ranking.reverse()
         total_consensus_weight = self.get_total_consensus_weight(relays)
-        for country, weight in ranking:
+        for country, weight in ranking[:count]:
             print "%3.2f%% %s" % (weight * 100.0 / total_consensus_weight, country)
 
-    def output_as_sets(self, flags='', countries=''):
+    def output_as_sets(self, count='10', flags='', countries=''):
+        count = int(count)
         flags = flags.split()
         relays = self.get_relays(flags, countries)
         as_sets = {}
@@ -69,7 +71,7 @@ class RelayStats(object):
         total_consensus_weight = self.get_total_consensus_weight(relays)
         ranking = sorted(as_sets.iteritems(), key=operator.itemgetter(1))
         ranking.reverse()
-        for as_set, weight in ranking:
+        for as_set, weight in ranking[:count]:
             print "%3.4f%% %s" % (weight * 100.0 / total_consensus_weight, as_set)
 
     def output_top(self, count='10', flags='', countries=''):
@@ -93,9 +95,9 @@ def usage():
     print >>sys.stderr, """Usage: %(progname)s <output> [args ...]
 
 Where <output> is one of:
- - countries [FLAGS]
+ - countries [COUNT] [FLAGS]
    relative percentage of the consensus in each countries
- - as-sets [FLAGS] [COUNTRIES]
+ - as-sets [COUNT] [FLAGS] [COUNTRIES]
    relative percentage of the consensus in each AS sets
  - top [COUNT] [FLAGS] [COUNTRIES]
    top relays according their place in the whole consensus
@@ -104,8 +106,8 @@ Examples:
 
  - To get the top five exit nodes in France:
    %(progname)s top 5 Exit fr
- - To get weights of each AS of all relays in Germany:
-   %(progname)s as-sets Running de
+ - To get weights of the top ten AS of all relays in Germany:
+   %(progname)s as-sets 10 Running de
 """ % { 'progname': sys.argv[0] }
     sys.exit(1)
 





More information about the tor-commits mailing list