DaveM@3: def parsePartnerDOB(dob): DaveM@3: dob_ = dob.split('/') DaveM@3: if(len(dob_) != 3): DaveM@3: # deal with no /'s DaveM@3: try: DaveM@3: d = int(dob_[0]) DaveM@3: m = int(dob_[1]) DaveM@3: y = int(dob_[2]) DaveM@3: if y < 100: DaveM@3: y = y + 1900 DaveM@3: if(d > 31 or m > 12 or y > 2017 or y < 1900): DaveM@3: print 'error with DOB '+d+'/'+m+'/'+y DaveM@3: except TypeError: DaveM@3: return None DaveM@3: return (d,m,y) DaveM@3: DaveM@3: def monthStringToNum(s): DaveM@3: m = {'jan':1,'feb':2, DaveM@3: 'mar':3,'apr':4,'may':5, DaveM@3: 'jun':6,'jul':7,'aug':8, DaveM@3: 'sep':9,'oct':10,'nov':11, DaveM@3: 'dec':12} DaveM@3: s_ = string.strip()[:3].lower() DaveM@3: try: DaveM@3: out = m[s] DaveM@3: return out DaveM@3: except: DaveM@3: raise ValueError('Not a month') DaveM@3: DaveM@3: def parseDOB(d,m,y): DaveM@3: d = int(d.strip()) DaveM@3: y = int(y.strip()) DaveM@3: try: DaveM@3: m = monthStringToNum(m.strip()) DaveM@3: except ValueError: DaveM@3: m = int(m.strip()) DaveM@3: if(y < 100): DaveM@3: y = y + 1900 DaveM@3: return (d,m,y) DaveM@3: DaveM@3: def parseTOB(T): DaveM@3: timeFlat = None DaveM@3: try: DaveM@3: T = T.lower().strip() DaveM@3: if 'am' in T: DaveM@3: timeFlag = 0 DaveM@3: T.replace('am','') DaveM@3: if 'pm' in T: DaveM@3: timeFlag = 1 DaveM@3: T.replace('pm','') DaveM@3: t.strip() DaveM@3: if ':' in T: DaveM@3: T.split(':') DaveM@3: H = int(T[0]) DaveM@3: M = int(T[1]) DaveM@3: elif '.' in T: DaveM@3: T.split('.') DaveM@3: H = int(T[0]) DaveM@3: M = int(T[1]) DaveM@3: else: DaveM@3: int(T) DaveM@3: if T < 24 : DaveM@3: H = T DaveM@3: M = 0 DaveM@3: elif T > 100: DaveM@3: H = T/100 DaveM@3: M = T%100 DaveM@3: if timeFlag is not None: DaveM@3: if timeFlag == 0: DaveM@3: H = H%12 DaveM@3: else: DaveM@3: H = H%12 + 12 DaveM@3: except ValueError: DaveM@3: H = 12 DaveM@3: M = 00 DaveM@3: return (H,M) DaveM@3: DaveM@3: def regulateData(dataDict): DaveM@3: p_DOBQ = "What is your partner's date of birth? Please use the format DD/MM/YYYY (for example, 29/03/1981)." DaveM@3: p_TOBQ = "At what exact time were your partner born? Please use the format HHMM (for example, 2204)." DaveM@3: DOB_DQ = "Which day (numeric) have you been born?" DaveM@3: DOB_MQ = "Which month have you been born?" DaveM@3: DOB_YQ = "Year Of Birth" DaveM@3: TOB_Q = "At what exact time were you born? Please use the format HHMM (for example, 2204)." DaveM@3: dataDict['DOB'] = parseDOB(dataDict[DOB_DQ],dataDict[DOB_MQ],dataDict[DOB_YQ]) DaveM@3: dataDict['TOB'] = parseTOB(dataDict[TOB_Q]) DaveM@3: dataDict['pDOB'] = parsePartnerDOB(dataDict[p_DOBQ]) DaveM@3: dataDict['pTOB'] = parseTOB(dataDict[p_TOBQ]) DaveM@3: DaveM@3: return dataDict