#!/usr/bin/env python # This is NOT a program - just a collection of functions. # Running it will do Nothing. # fmtstr.py # A collection of functions for uniquing lists, # filtering strings and # formatting strings (e.g. as phone numbers or postal codes etc.) # Copyright Paul Evans 2002, Released under the GPL (except as credited) # # Tue Sep 17 16:36:05 PDT 2002 Paul Evans # This 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 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 a copy of the GNU General Public License. # Probably you have *many* of them... # If not, write to the Free Software Foundation, # Inc., 675 Mass Ave, Cambridge, MA 02139, USA or visit # http://www.gnu.org/philosophy/free-sw.html ###################################################################### # Unique a python list object # # Ref: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560 # # Author: Raymond Hettinger, 2002/03/17 # ###################################################################### def uniqOrdered(alist) # Fastest order preserving set = {} return [set.setdefault(e,e) for e in alist if e not in set] def uniqFast(alist) # Fastest without order preserving set = {} map(set.__setitem__, alist, []) return set.keys() ###################################################################### # Adding an 's' to a noun if plural # # Ref: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81058 # # Author: Robin Parmar, 2001/10/16 # ###################################################################### def plural(num=0, text=''): return "%d %s%s" % (num, text, "s"[num==1:]) ################################################# # Filter a string for only allowable characters # ################################################# def filter(inStr, allowed): outStr = '' for c in inStr: if c in allowed: outStr += c return outStr ############################ # Get a current date stamp # ############################ import time def ds(): # returns an ISO date stamp stamp = time.strftime('%Y-%m-%d', time.localtime(time.time() ) ) return(stamp) ############################################################ # Format a string to a pattern (e.g. phone number) # # Ref: comp.lang.python # # Author: Guido van Rossum (yes, *that* one..) 1998/10/13 # ############################################################ import string def fmtstr(fmt, str): res = [] i = 0 for c in fmt: if c == '#': res.append(str[i:i+1]) i = i+1 else: res.append(c) res.append(str[i:]) return string.join(res) #################################################### # Format a string to a pattern (e.g. phone number) # # Done in reverse to handle var lengths # #################################################### import re def formatStr(inStr, fmtStr, p = '^'): inList = [x for x in inStr] #list from strings.. fmtList = [x for x in fmtStr] inList.reverse(); fmtList.reverse() outList = [] i = 0 for c in fmtList: if c == p: try: outList.append(inList[i]) i += 1 # break if fmtStr longer than inStr except IndexError: break else: outList.append(c) # handle inStr longer than fmtStr while i < len(inList): outList.append(inList[i]) i += 1 outList.reverse() outStr = ''.join(outList) # remove stray parens/- etc while re.match('[)|-| ]', outStr[0]): outStr = outStr[1:] # match any legit parens while outStr.count(')') > outStr.count('('): outStr = '(' + outStr return outStr ##################################### # A format useful with money values # ##################################### def moneyfmt(self, instr): return '%5.2f' % float(instr) ################################### # Safe integer conversion example # # Ref: email # # Author: Mike Orr, 2002/09/17 # ################################### def safeInt(s): try: return int(s) except (TypeError, ValueError): return 0 # If 0 is an acceptable default value.