FIXED: Percentage calculation mistake
[colly.git] / colly.py
blob90b1c37bc611bf07431e46c2b6ff949f392080dc
1 import sys
2 import csv
4 class Colly(object):
5 _setA = None
6 _setB = None
8 ''' Csv parsing and formatting
9 '''
10 def __init__(self, a, b, aM=[], bM=[]):
11 self.setA = self._prep(a, *aM)
12 self.setB = self._prep(b, *bM)
13 if self.setA == None or self.setB == None:
14 print "Operation failed"
15 raise
17 def _prep(self, csv_file, col=0, start=0, end=sys.maxint):
18 ''' Prepare values in selected column from CSV file (f)
19 '''
20 with open(csv_file) as f:
21 fn = []
22 if col > 0:
23 fn += [0]*(col-len(fn)) #: Pad/ ignore columns (< col)
24 fn.append('name') #: Mark selected column
25 raw = list(csv.DictReader(f, fieldnames=fn))
26 return set(map(lambda e: e['name'], raw[start:end]))
28 ''' Make properties use validation & write-once only
29 '''
30 @property
31 def setA(self): return self._setA
33 @property
34 def setB(self): return self._setB
36 @setA.setter
37 def setA(self, v):
38 if self._setA == None:
39 self._setA = v
41 @setB.setter
42 def setB(self, v):
43 if self._setB == None:
44 self._setB = v
46 @setA.getter
47 def setA(self): return set(self._setA)
49 @setB.getter
50 def setB(self): return set(self._setB)
52 ''' Comparse datasets (/ wrappers for set built-in)
53 '''
54 # Comparison sets
55 @property
56 def all(self): return self.setB.union(self.setB)
58 @property
59 def clean(self): return self.setB.intersection(self.setA)
61 @property
62 def added(self): return self.setB.difference(self.setA)
64 @property
65 def erased(self): return self.setA.difference(self.setB)
67 # Calculations
68 def p_erased(self):
69 c, e = len(self.clean), len(self.erased)
70 if c > e:
71 return 100 * float(e) / float(c)
72 else:
73 return 100