]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/python/oils/utils/utils.py
added a list unique-ifier
[Evergreen.git] / Open-ILS / src / python / oils / utils / utils.py
1 # -----------------------------------------------------------------------
2 # Copyright (C) 2007  Georgia Public Library Service
3 # Bill Erickson <billserickson@gmail.com>
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 # -----------------------------------------------------------------------
15
16 import re, md5
17
18
19 # -----------------------------------------------------------------------
20 # Grab-bag of general utility functions
21 # -----------------------------------------------------------------------
22
23
24 # -----------------------------------------------------------------------
25 # more succinct search/replace call
26 # -----------------------------------------------------------------------
27 def replace(str, pattern, replace):
28    return re.compile(pattern).sub(replace, str)
29
30
31 def isEvent(evt):
32     return (evt and isinstance(evt, dict) and evt.get('ilsevent') != None)
33
34 def eventCode(evt):
35     if isEvent(evt):
36         return evt['ilsevent']
37     return None
38
39 def eventText(evt):
40     if isEvent(evt):
41         return evt['textcode']
42     return None
43
44       
45 def md5sum(str):
46     m = md5.new()
47     m.update(str)
48     return m.hexdigest()
49
50 def unique(arr):
51     ''' Unique-ify a list.  only works if list items are hashable '''
52     o = {}
53     for x in arr:
54         o[x] = 1
55     return o.keys()
56