From c5749895ab4f6893bdf1d398691d1dd33e81574c Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Sun, 16 Nov 2008 20:15:34 +0000 Subject: have some more thandy. This update includes a working downloader with tor support, a package system framework, and more. Most of what's left is glue code. git-svn-id: file:///home/or/svnrepo/updater/trunk@17288 55e972cd-5a19-0410-ae62-a4d7a52db4cd --- lib/thandy/util.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'lib/thandy/util.py') diff --git a/lib/thandy/util.py b/lib/thandy/util.py index b7281ae..2db7df4 100644 --- a/lib/thandy/util.py +++ b/lib/thandy/util.py @@ -3,6 +3,7 @@ import os import sys import tempfile +import random try: import json @@ -20,6 +21,7 @@ def moveFile(fromLocation, toLocation): os.unlink(toLocation) except OSError: pass + os.rename(fromLocation, toLocation) @@ -75,3 +77,21 @@ def getKeylist(keys_fname, checkKeys=True): keydb.addFromKeylist(obj['signed'], allowMasterKeys=False) return keydb + +def randChooseWeighted(lst): + """Given a list of (weight,item) tuples, pick an item with + probability proportional to its weight. + """ + + totalweight = sum(w for w,i in lst) + position = random.uniform(0, totalweight) + soFar = 0 + + # We could use bisect here, but this is not going to be in the + # critical path. If it is, oops. + for w,i in lst: + soFar += w + if position < soFar: + return i + + return lst[-1][1] -- cgit v1.2.3