#!/usr/pkg/bin/python """arcane tricks and other useful routines things that may or may not fit well in other libs but need to be put somewhere so I don't spend a half hour to figure them out again every time I need the functionality""" Copyright = """ arcane - tricky or not-so-tricky little routines for various porpoises Copyright (C) 2004 John Comeau This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. """ errormessage = "Not all needed libraries found, upgrade or check path" try: True # not defined in older Python releases except: True, False = 1, 0 try: import sys, os, types, re, pwd sys.path.append(os.sep.join([pwd.getpwuid(os.geteuid())[5], 'lib', 'python'])) from com.jcomeau import gpl, jclicense except: try: sys.stderr.write("%s\n" % errormessage) except: print errormessage raise # get name this program was called as self = sys.argv[0].split(os.sep)[-1] mypath = os.sep.join(sys.argv[0].split(os.sep)[0:-1]) # now get name we gave it when we wrote it originalself = Copyright.split()[0] # globals and routines that should be in every program # (yes, you could import them, but there are problems in that approach too) def DebugPrint(*whatever): pass # defined instead by pytest module, use that for debugging def wtf(): "just in case someone called this as a program" sys.stderr.write("wtf do you think you're doing?\n") def ReverseDict(Dictionary): """reverses a dictionary whose values are all unique in other words, returns a new dictionary whose keys are the values of the original, and whose values are the keys of the original""" return dict(map(None, Dictionary.values(), Dictionary.keys())) def pause(*Prompt): if (len(Prompt)) == 0: Prompt = "Hit to continue..." else: Prompt = Prompt[0] sys.stderr.write(Prompt) sys.stdin.readline() def NearestValue(Item, Items): "returns the item in the list Items which is closest to Item" modified = map(lambda i: i - Item, Items) modified.sort(lambda i, j: cmp(abs(i), abs(j))) return Item + modified[0] def NearestValueIndex(Item, Items): "returns index in Items of nearest value to Item" modified = map(lambda i: i - Item, Items) reordered = list(modified) # makes a new list, not just a reference reordered.sort(lambda i, j: cmp(abs(i), abs(j))) return modified.index(reordered[0]) class countdict(dict): "useful subclass for counting, initializes unknown keys to value of 0" def __getitem__(self, key): try: return super(countdict, self).__getitem__(key) except KeyError: return 0 if __name__ == '__main__': # if this program was imported by another, the above test will fail, # and this following code won't be used... wtf() else: # if you want something to be done on import, do it here; otherwise pass pass