def validate(prices): raw = DataEngine.getData(prices).strip() raw = csv.reader(io.StringIO(raw)) data = {} for row in raw: if len(row) == 0: continue elif len(row) > 2 or len(row) == 1: raise DataEngineException('Rows must have exactly 2 columns') elif len(row) == 2: try: data[row[0].strip()] = [float(row[1])] + [None] * 7 except: raise DataEngineException( 'The first column must be a string, the second column must be a number' ) elif len(row) == 8: try: data[row[0].strip()] = [] for item in row[1:]: if item == None: data[row[0].strip()].append(item) else: data[row[0].strip()].append(float(item)) except: raise DataEngineException( 'The first column must be a string, the other 8 columns must be numbers' ) else: raise DataEngineException( 'Prices for goods must either be in a single currency or have 8 currencies defined' ) return data
def validate(prices): raw = DataEngine.getData(prices).strip() raw = csv.reader(io.StringIO(raw)) data = {} for row in raw: if len(row) == 0: continue elif len(row) > 2 or len(row) == 1: raise DataEngineException('Rows must have exactly 2 columns') elif len(row) == 2: try: data[row[0].strip()] = [float(row[1])]+[None]*7 except: raise DataEngineException('The first column must be a string, the second column must be a number') elif len(row) == 8: try: data[row[0].strip()] = [] for item in row[1:]: if item == None: data[row[0].strip()].append(item) else: data[row[0].strip()].append(float(item)) except: raise DataEngineException('The first column must be a string, the other 8 columns must be numbers') else: raise DataEngineException('Prices for goods must either be in a single currency or have 8 currencies defined') return data
def validate(prices): raw = DataEngine.getData(prices).strip() try: data = json.loads(raw) except: raise DataEngineException('JSON data failed to be parsed') if 'prices' not in data: raise DataEngineException('Sim data must have prices key') if 'metric' not in data: raise DataEngineException('Sim data must have metric key') if type(data['metric']) != int: raise DataEngineException('Metric must be an integer between 0 and 31') for k in data['prices']: if type(k) != str: raise DataEngineException('Keys must be strings') if type(data['prices'][k]) not in (int, list, float): raise DataEngineException('Values must be arrays or numbers') if type(data['prices'][k]) == list: if len(data['prices'][k]) != 8: raise DataEngineException('Array values must have exactly 8 elements') if any(map(lambda i: type(i) not in [int, float, type(None)], data['prices'][k])): raise DataEngineException('Array values must be all numbers or null') return data
def validate(prices): raw = DataEngine.getData(prices).strip() raw = csv.reader(io.StringIO(raw)) rows = list(raw) if len(rows) < 2: raise DataEngineException('Must contain at least two rows') if len(rows[0]) < 2: raise DataEngineException('Must contain at least two columns') try: metric = rows[0][0] if 'metricString' in metric: metric = int(metric[12:])-1 elif 'metricNumber' in metric: metric = int(metric[12:])+8-1 elif metric.lower().strip() == 'country': metric = metric.lower().strip() else: assert(False) assert((type(metric)==int and metric<32 and metric>-1) or (metric=='country')) except: raise DataEngineException('Metric name not valid/recognized') if type(metric) == int: if metric<8: progress = list(map(lambda p: p.strip().lower(), rows[0][1:])) else: try: progress = [] for p in rows[0][1:]: if p.strip().lower() == 'default': progress.append('default') else: progress.append(float(p)) except: raise DataEngineException('Numberic progress metric must have numeric progress values for the columns') elif type(metric) == str: progress = list(map(lambda p: p.strip().lower(), rows[0][1:])) data = dict(map(lambda p: (p, {}), progress)) if any(map(lambda p: p.lower() == 'default', progress)) == False: raise DataEngineException('Missing default pricing column') for row in rows[1:]: # Remove trailing spaces row = list(map(lambda r: r.strip(), row)) if len(row) != len(row[0]): raise DataEngineException('All rows must contain the same number of elements') for i, value in enumerate(row[1:]): try: data[progress[i]][row[0]] = [float(value)] + [None]*7 except: raise DataEngineException('The first column must be a tring, all other columns must be good prices encoded as numbers') return {'metric':metric, 'data':data}
def validate(prices): raw = DataEngine.getData(prices).strip() try: data = json.loads(raw) except: raise DataEngineException('JSON data failed to be parsed') return data
def validate(prices): raw = DataEngine.getData(prices).strip() try: data = json.loads(raw) except: raise DataEngineException('JSON data failed to be parsed') for k in data: if type(k) != str: raise DataEngineException('Keys must be strings') if type(data[k]) not in (int, list, float): raise DataEngineException('Values must be arrays or numbers') if type(data[k]) == list: if len(data[k]) != 8: raise DataEngineException('Array values must have exactly 8 elements') if any(map(lambda i: type(i) not in [int, float, type(None)], data[k])): raise DataEngineException('Array values must be all numbers or null') return data
def validate(prices): raw = DataEngine.getData(prices).strip() raw = csv.reader(io.StringIO(raw)) rows = list(raw) if len(rows) < 2: raise DataEngineException('Must contain at least two rows') if len(rows[0]) < 2: raise DataEngineException('Must contain at least two columns') try: metric = rows[0][0] if 'metricString' in metric: metric = int(metric[12:]) - 1 elif 'metricNumber' in metric: metric = int(metric[12:]) + 8 - 1 elif metric.lower().strip() == 'country': metric = metric.lower().strip() else: assert (False) assert ((type(metric) == int and metric < 32 and metric > -1) or (metric == 'country')) except: raise DataEngineException('Metric name not valid/recognized') if type(metric) == int: if metric < 8: progress = list(map(lambda p: p.strip().lower(), rows[0][1:])) else: try: progress = [] for p in rows[0][1:]: if p.strip().lower() == 'default': progress.append('default') else: progress.append(float(p)) except: raise DataEngineException( 'Numberic progress metric must have numeric progress values for the columns' ) elif type(metric) == str: progress = list(map(lambda p: p.strip().lower(), rows[0][1:])) data = dict(map(lambda p: (p, {}), progress)) if any(map(lambda p: p.lower() == 'default', progress)) == False: raise DataEngineException('Missing default pricing column') for row in rows[1:]: # Remove trailing spaces row = list(map(lambda r: r.strip(), row)) if len(row) != len(row[0]): raise DataEngineException( 'All rows must contain the same number of elements') for i, value in enumerate(row[1:]): try: data[progress[i]][row[0]] = [float(value)] + [None] * 7 except: raise DataEngineException( 'The first column must be a tring, all other columns must be good prices encoded as numbers' ) return {'metric': metric, 'data': data}