0
|
1 """
|
|
2 Author: Timothy Tickle
|
|
3 Description: Utility class for generic functions.
|
|
4 """
|
|
5
|
|
6 #####################################################################################
|
|
7 #Copyright (C) <2012>
|
|
8 #
|
|
9 #Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
10 #this software and associated documentation files (the "Software"), to deal in the
|
|
11 #Software without restriction, including without limitation the rights to use, copy,
|
|
12 #modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
|
13 #and to permit persons to whom the Software is furnished to do so, subject to
|
|
14 #the following conditions:
|
|
15 #
|
|
16 #The above copyright notice and this permission notice shall be included in all copies
|
|
17 #or substantial portions of the Software.
|
|
18 #
|
|
19 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
20 #INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
21 #PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
22 #HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
23 #OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
24 #SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25 #####################################################################################
|
|
26
|
|
27 __author__ = "Timothy Tickle"
|
|
28 __copyright__ = "Copyright 2012"
|
|
29 __credits__ = ["Timothy Tickle"]
|
|
30 __license__ = "MIT"
|
|
31 __maintainer__ = "Timothy Tickle"
|
|
32 __email__ = "ttickle@sph.harvard.edu"
|
|
33 __status__ = "Development"
|
|
34
|
|
35 class Utility():
|
|
36 """
|
|
37 Class to perform misc methods.
|
|
38 """
|
|
39
|
|
40 #Tested 6
|
|
41 @staticmethod
|
|
42 def getIndices(aList, dataElement):
|
|
43 """
|
|
44 Returns the index or indicies of the element in the list.
|
|
45
|
|
46 :param aList: List ot search for element.
|
|
47 :type List.
|
|
48 :param dataElement: Element for which to search.
|
|
49 :type Object of the same type as is found in the list.
|
|
50 :return: List of indicies indicating where the element occurs in the list. Returns [] when the element is not in the list.
|
|
51 """
|
|
52
|
|
53 aretIndices = []
|
|
54 for dataIndex in xrange(0,len(aList)):
|
|
55 if(aList[dataIndex] == dataElement):
|
|
56 aretIndices.append(dataIndex)
|
|
57 return aretIndices
|
|
58
|
|
59 #Tested 6
|
|
60 @staticmethod
|
|
61 def reduceList(aList, dataIndicies):
|
|
62 """
|
|
63 Reduces a list to just the data indicies given.
|
|
64
|
|
65 :param aList: List to reduce.
|
|
66 :type List
|
|
67 :param dataIndicies: list of indicies to keep.
|
|
68 :type List of integers
|
|
69 :return: Reduced list. Returns [] when the and empty index list is given.
|
|
70 """
|
|
71 return [aList[dataIndicies[dataIndex]] for dataIndex in xrange(0,len(dataIndicies))]
|
|
72
|
|
73 #Tested 8
|
|
74 @staticmethod
|
|
75 def RGBToHex(adColor):
|
|
76 """
|
|
77 Change a RGB float to hex.
|
|
78
|
|
79 :param adColor: A list of 3 elements which are floats between 0.0 and 1.0
|
|
80 :type A list of floats
|
|
81 :return: A string (HEX formatted) representation of the RGB color
|
|
82 """
|
|
83
|
|
84 charR = (hex(int(adColor[0]*255)))[2:]
|
|
85 if(str(charR) == "0"):
|
|
86 charR = "00"
|
|
87 charG = (hex(int(adColor[1]*255)))[2:]
|
|
88 if(str(charG) == "0"):
|
|
89 charG = "00"
|
|
90 charB = (hex(int(adColor[2]*255)))[2:]
|
|
91 if(str(charB) == "0"):
|
|
92 charB = "00"
|
|
93 return "".join(["#",charR, charG, charB])
|