|
Programmer's Notebook |
All computer source code presented on this page, unless it includes attribution to another author, is provided by Ed Halley under the Artistic License. Use such code freely and without any expectation of support. I would like to know if you make anything cool with the code, or need questions answered.
python/ bindings.py boards.py buzz.py cache.py cards.py constraints.py english.py getopts.py gizmos.py goals.py improv.py interpolations.py namespaces.py nihongo.py nodes.py octalplus.py patterns.py persist.py physics.py pieces.py quizzes.py recipes.py relays.py romaji.py ropen.py sheets.py strokes.py subscriptions.py svgbuild.py testing.py things.py timing.py ucsv.py useful.py uuid.py vectors.py weighted.py java/ GlobFilenameFilter.java RegexFilenameFilter.java StringBufferOutputStream.java ThreadSet.java TracingThread.java Utf8ConsoleTest.java perl/ CVQM.pm Kana.pm Typo.pm cxx/ CCache.h equalish.cpp |
# useful - generic useful standalone routines ''' Several utility routines that are hardly worth mentioning. AUTHOR Ed Halley (ed@halley.cc) 17 September 2005 ''' import random #---------------------------------------------------------------------------- def all(seq): '''Returns True if all of the given sequence are non-False.''' for x in seq: if not x: return False return True def none(seq): '''Returns True if none of the given sequence is non-False.''' for x in seq: if x: return False return True def any(seq): '''Returns True if any of the given sequence is non-False.''' for x in seq: if x: return True return False def wrap(text, width=72): '''Wrap a piece of text at whitespace, so as to fit within margins. The width of the space is measured in characters, suitable for monospaced fonts like console applications. ''' return reduce(lambda line, word, width=width: "%s%s%s" % (line, " \n"[(len(line)-line.rfind("\n")-1 + len(word.split("\n",1)[0]) >= width)], word), text.split(" ")) def weighted(choices, total=0): '''Given a dict of key:weight pairs, chooses a key at random. The dict values are non-negative numerical weights. Keys with higher values are chosen more often than keys with lower values. If the caller knows the total of all weights, it can be given to avoid recalculating it internally on each call. If the given total is not accurate, a key may be chosen with a poorly-shaped distribution. ''' if not total: total = sum(choices.values()) mark = random.random()*total keys = choices.keys() for i in xrange(len(keys)): span = choices[keys[i]] if span > mark: return keys[i] mark -= span # should not reach here if total is accurate return random.choice(keys) #---------------------------------------------------------------------------- if __name__ == '__main__': raise Exception, \ 'This module is not a stand-alone script. Import it in a program.' |
|
Contact Ed Halley by email at
ed@halley.cc. Text, code, layout and artwork are Copyright © 1996-2008 Ed Halley. Copying in whole or in part, with author attribution, is expressly allowed. Any references to trademarks are illustrative and are controlled by their respective owners. |
|