def get_data_API(url=None, fname=None): if PRODUCTION or not SIMULATE: log.debug("Get url %s" % (url)) try: response = get(url, headers=HEADERS, timeout=TIMEOUT, verify=False) except exceptions.RequestException, e: log.error("Exception in requests get %s. Reason %s" % (url, str(e))) raise Paso2015(__name__) if response.status_code == 200: return response.json() else: log.error("API responded with code %s" % (response.status_code)) raise Paso2015(__name__)
def t_candidates_percentage(d=None): '''Transform candidates percentage for piece automation''' try: data = d[0]["general"][0]["partidos"] except (KeyError, IndexError), e: log.error("Error getting data from memory. Reason %s" % (str(e))) raise Paso2015(__name__)
def format_percentage(num=None): if type(num) is str or unicode: try: num = float(num) except ValueError, e: log.error("Could not convert to number reason %s" % (str(e))) raise Paso2015(__name__)
def get_percentage(dict=None, key1=None, key2=None): '''Get percentage formatted for the frontend app''' try: total = float(dict[key2]) value = int(dict[key1]) except KeyError, e: log.error("Did not find key: %s" % (key2)) raise Paso2015(__name__)
def t_ranking(d_d=None): '''Transformation to obtain the ranking data for the front page''' try: data_parties = d_d["partido_00"]["c_00"] data_summary = d_d["resumen"] except KeyError, e: log.error("Error getting data from memory. Reason %s" % (str(e))) raise Paso2015(__name__)
def t_rename_data(d=None, translation=None, p_keys=None): '''translate desired data''' target_dict = {} try: for k, v in translation.iteritems(): if (k in p_keys): d[k] = format_percentage(d[k]) target_dict[v] = d[k] except KeyError, e: log.error("Could not find required key %s in %s" % (k, d)) raise Paso2015(__name__)
def write_JSON_file(path=None, fname=None, data=None): '''Write JSON files to disk''' try: with io.open('%s/%s.json' % (JSON_DATA_PATH, fname), 'w', encoding='utf8') as f: log.debug("writing output JSON: %s.json" % (fname)) f.write(json.dumps(data, ensure_ascii=False)) except IOError, e: log.error("Failed to save JSON file %s. Reason %s" % (fname, str(e))) raise Paso2015(__name__)
def update_time_increased(od=None, nd=None): '''Compare times to determine if it has increased from last execution. Take into account midnight change''' updated = False try: ot = od["ut"] nt = nd["ut"] omp = od["mp"] nmp = nd["mp"] except KeyError, e: log.error("Could not retrieve keys. Reason %s" % (str(e))) raise Paso2015(__name__)
def t_results_section_API(d=None, comuna=None, dest_dict=None): '''Transform the received data to the desired format''' a99 = [] a00 = [] try: if not comuna: # 0 stores the global results for the election data = d["general"][0]["partidos"] else: data = d["general"][0]["comunas"]["partidos"] except (KeyError, IndexError), e: log.error("Did not find data in memory. Reason" % (str(e))) raise Paso2015(__name__)
def t_resumen_API(origin_dict=None): '''get the desired data''' target_dict = {} try: for k, v in RESUMEN_RENAME.iteritems(): target_dict[v] = origin_dict['resumen'][k] except KeyError: log.error("Could not find required key %s in %s" % (k, origin_dict)) raise Paso2015(__name__) # Calculate table percentage mp = get_percentage(target_dict, 'mi', 'mt') target_dict["mp"] = mp # Calculate voting percentage vp = get_percentage(target_dict, 'v', 'e') target_dict["vp"] = vp return target_dict
def sort_results_by_percentage(d=None, special=False): '''sort by percentage''' for i in range(0, 16): try: l = d["c_%02d" % i] l.sort(key=lambda x: int(x['v']), reverse=True) if special: # Move special parties to the bottom of list tmp = [] indexes = [] for idx, row in enumerate(l): if (row["id"] in SPECIAL_PARTIES.keys()): # Remove from the list and store in tmp list tmp.append(row) indexes.append(idx) # Remove indexes in inverse order not to generate # IndexOutOfBounds for i in reversed(indexes): l.pop(i) tmp.sort(key=lambda x: SPECIAL_PARTIES[x["id"]], reverse=False) l.extend(tmp) except KeyError: log.error("Did not find key c_%02d in dict %s" % (i, d)) raise Paso2015(__name__)
PERC_KEYS), "c_%02d" % (comuna): t_a } # Create the key for the policitical party # inside the target dict dest_dict["partido_%s" % (row["id_partido"])] = t_d else: # For every other section # We only need to create a section key # with the candidates array dest_dict["partido_%s" % (row["id_partido"])]["c_%02d" % (comuna)] = t_a except KeyError, e: log.error("Error processing key. Reason %s" % (str(e))) raise Paso2015(__name__) except IndexError, e: log.error("Error processing index. Reason %s" % (str(e))) raise Paso2015(__name__) dest_dict["partido_99"]["c_%02d" % (comuna)] = a99 dest_dict["partido_00"]["c_%02d" % (comuna)] = a00 def t_sort_results_API(d_d=None): ''' sort the results by descending percentage taking into account special parties at the bottom''' for k, v in d_d.iteritems(): if k == "resumen": continue if k == "partido_00": sort_results_by_percentage(v, special=True)