diff options
author | Nick Mathewson <nickm@torproject.org> | 2008-11-16 20:15:34 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2008-11-16 20:15:34 +0000 |
commit | c5749895ab4f6893bdf1d398691d1dd33e81574c (patch) | |
tree | 4ee47c3c6c56e313c3074f04c77a3637cf0fe31d /lib/thandy/util.py | |
parent | 02a2e5807f23ad0cad9a49b5febe08ec25fcc74c (diff) |
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
Diffstat (limited to 'lib/thandy/util.py')
-rw-r--r-- | lib/thandy/util.py | 20 |
1 files changed, 20 insertions, 0 deletions
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] |