def val_from(row, index): raw = ws.row_values(row)[index] if isinstance(raw, (float, int)): d = text_type(int(raw)) else: d = text_type(raw) return d
def format_number(data, is_ratio=False, is_yesno=False, should_yesno=False, float_precision=2, add_percent=False): if is_yesno and should_yesno: return "OUI" if bool(data) else "NON" int_fmt = "%d" float_fmt = "%." + text_type(float_precision) + "f" as_int = lambda v: (int(v), int_fmt) v = data if is_ratio: v = v * 100 if float(v).is_integer(): v, f = as_int(v) else: try: v, f = float(v), float_fmt except: f = "{}" raise else: if v.is_integer(): v, f = as_int(v) try: v = locale.format(f, v, grouping=True) except Exception: pass if add_percent: return "{}%".format(v) return text_type(v)
def report_status_verbose(value): if is_aggregated(report): return value else: for v, name in report.YESNO.items(): if text_type(v) == value: return text_type(name) return value
def update(cls, key, value): qs = cls.objects.filter(key=key) if not qs.count(): return cls.objects.create(key=key, value=text_type(value)) else: inst = qs.get() inst.value = text_type(value) inst.save() return inst
def price_for_orange(month_data): total_in = month_data['stats']['nb_sms_in'][text_type(ORANGE)] total_out = month_data['stats']['nb_sms_out'][text_type(ORANGE)] mo_price = 25 mt_price = 17 ratio = 1.1 nb_paying_out = total_out - total_in * ratio if nb_paying_out < 0: nb_paying_out = 0 return mo_price * total_in + nb_paying_out * mt_price
def operator_from_malinumber(number, default=settings.FOREIGN): ''' ORANGE or MALITEL based on the number prefix ''' indicator, clean_number = phonenumber_cleaned(normalized_phonenumber(number)) if indicator is not None and indicator != text_type(COUNTRY_PREFIX): return default for operator, opt in settings.OPERATORS.items(): for prefix in opt: if clean_number.startswith(text_type(prefix)): return operator return default
def join(node): ''' Join branch node's leaves into single string. ~ Applies only on branch nodes. ~ Note: node becomes a leaf! ~ join() uses toLeaves() ''' ''' usage: op : '+' / '-' / '*' / '/' hexNum : [0..9 a..eA..E]+ : hexToDec operation : hexNum op hexNum : join (hexToDec tranformation converts) source "c-9 a0*ff 33/a" method scan result ['12-9' '160*255' '51/10'] ''' if node.kind is Node.LEAF: return if isinstance(node.value, str): return toLeaves(node) childTexts = [] for child in node.value: if isinstance(child.value, py3compat.text_type): childTexts.append(py3compat.text_type(child.value)) else: childTexts.append(str(child.value)) node.value = ''.join(childTexts)
def main_labels(self): l = [] for p in self.periods: l.append(text_type(p)) if self.add_total: l.append("TOTAL") return l
def generic_table(datamatrix, title=""): """ Generic Table based on a strict input format: datamatrix = [ ["Item Header", "Col1 header", "Col2 Header"], ["Line 1 name", "Col1 Data", "Col2 Data"], ["Line 2 name", "Col1 Data", "Col2 Data"], ] """ if not len(datamatrix): return error_widget("Aucune ligne pour le tableau") nb_col = len(datamatrix[-1]) # smart column sizes if nb_col <= 3: col_large = 6000 col_regular = 2000 elif nb_col <= 5: col_large = 4000 col_regular = 1500 else: col_large = 3000 col_regular = 1000 thin_edge = BorderPS(width=20, style=BorderPS.SINGLE) def p(text, align_center=False): return Paragraph(ParagraphPS( alignment=ParagraphPS.CENTER if align_center else ParagraphPS.LEFT), text) thin_frame = FramePS(thin_edge, thin_edge, thin_edge, thin_edge) cols = [col_large] for _ in range(1, nb_col): cols.append(col_regular) table = Table(*cols) # first header row : title and period names args = [] for header in datamatrix[0]: args.append(Cell(p(text_type(header), True), thin_frame)) table.AddRow(*args) # data rows for line in datamatrix[1:]: args = [] for idx, cell_data in enumerate(line): align_center = not idx == 0 args.append(Cell(p(number_format(cell_data), align_center), thin_frame)) table.AddRow(*args) return table
def date_to_ident(adate): year, month, day = adate.timetuple()[0:3] hyear = text_type(year)[-1] if day > 16: hmonth = ALPHA[month * 2] hday = hex(day // 2)[2:] else: hmonth = ALPHA[month] hday = hex(day)[2:] return "{y}{m}{d}".format(m=hmonth, d=hday, y=hyear)
def build_sections_list(): d = {} for fname in os.listdir(os.path.join(*['dmd', 'analysis'])): if not fname.endswith('.py') \ or not re.match(r'^section([0-9]+)\.py$', fname): continue mod = import_path('dmd.analysis.{}'.format(fname[:-3]), failsafe=True) if mod is None: continue d.update({text_type(mod.SECTION_ID): mod.SECTION_NAME}) return d
def parsed_equal_string(self, source, result, method_name, templates={}, postprocessor='raw'): preprocessed = self._preprocessor(templates).parseTest(source).value self.assertEquals( py3compat.text_type( self._grammar(method_name, postprocessor).parseTest(preprocessed).leaves()), result)
def humanize_value(data, is_expected=True, is_missing=False, is_ratio=False, is_yesno=False, should_yesno=False, float_precision=2): float_fmt = "{0:." + text_type(float_precision) + "f}" zero_end = '.' + ''.zfill(float_precision) if not is_expected: return "n/a" if is_missing: return "-" t = type(data) v = data if t is int or t is long: v = data if t is float: v = float_fmt.format(data) if v.endswith(zero_end): v = int(float(v)) if is_yesno and should_yesno: v = "OUI" if bool(v) else "NON" if is_ratio and not (is_yesno and should_yesno): v = "{}%".format(v) return text_type(v)
def upload(request, template_name='upload.html'): context = {'page': 'upload'} if request.method == 'POST': # ensure can_upload if not request.user.partner.can_upload: raise PermissionDenied(_("You are not allowed to submit data.")) form = ExcelUploadForm(request.POST, request.FILES) if form.is_valid(): filepath = handle_uploaded_file(request.FILES['data_file']) try: xls_data = read_xls(filepath, request.user.partner) except Exception as e: logger.exception(e) if isinstance(e, (IncorrectExcelFile, ExcelValueMissing, ExcelValueError, UploadPermissionDenied)): messages.error(request, text_type(e)) else: messages.error( request, _("Unexpected Error while reading XLS file")) return redirect('upload') # sucessfuly read the XLS file xls_data = create_records_from(request, xls_data, filepath) if xls_data['feedback']['redirect']: # send message with appropriate level and redirect getattr(messages, xls_data['feedback']['level'])( request, xls_data['feedback']['text']) return redirect('home') context.update({'xls_data': xls_data}) else: # django form validation errors pass else: form = ExcelUploadForm() context.update({'form': form}) return render(request, template_name, context)
def view(request, entity_uuid=None, perioda_str=None, periodb_str=None, indicator_slug=None, **kwargs): context = {'page': 'analysis_section1'} # handling entity context.update(process_entity_filter(request, entity_uuid)) # handling periods context.update(process_period_filter(request, perioda_str, 'perioda')) context.update(process_period_filter(request, periodb_str, 'periodb')) if context['perioda'] > context['periodb']: context['perioda'], context['periodb'] = \ context['periodb'], context['perioda'] periods = MonthPeriod.all_from(context['perioda'], context['periodb']) context.update({'selected_periods': periods}) all_indicators = Indicator.objects.all() if indicator_slug: qs = all_indicators.filter(slug=indicator_slug) indicator = qs.get() else: qs = indicator = None context.update({ 'section': text_type(SECTION_ID), 'section_name': SECTION_NAME, 'elements': build_context(periods=periods, entity=context['entity'], qs=qs), 'indicators': all_indicators, 'indicator': indicator, }) # absolute URI for links context.update({'baseurl': request.build_absolute_uri()}) return render(request, kwargs.get('template_name', 'analysis_section1.html'), context)
def phonenumber_repr(number, skip_indicator=text_type(COUNTRY_PREFIX)): ''' properly formated for visualization: (xxx) xx xx xx xx ''' def format(number): if len(number) % 2 == 0: span = 2 else: span = 3 # use NBSP return " ".join(["".join(number[i:i + span]) for i in range(0, len(number), span)]) indicator, clean_number = phonenumber_cleaned(number) if indicator and indicator != skip_indicator: return "(%(ind)s) %(num)s" \ % {'ind': indicator, 'num': format(clean_number)} return format(clean_number)
def iterate(username): """ adds and increment a counter at end of username """ # make sure username matches length requirements username = new_slug(username) if not is_available(username): # find the counter if any sp = re.split(r'([0-9]+)$', username) if len(sp) == 3: # increment existing counter username = sp[0] salt = text_type(int(sp[1]) + 1) else: # start counter at 1 salt = '1' # bundle counter and username then loop return iterate(new_slug(username, salt)) else: # username is available return username
def test_choice_regex(self): """Make sure the Choice-optimize-as-Regexp works""" numbers_transform_grammar = """\ test_choice_regex <toolset> def to_int(node): node.value = int(node.value) <definition> SEP : ' ' : drop digit : [0..4] / [5..9] : liftNode integer : digit+ : join number : integer : to_int addedNumber : SEP number : liftNode numbers : number (addedNumber)* : extract """ make_parser = makeParser(numbers_transform_grammar) parser = make_parser() source = "12 3 5" result = "[number:'12' number:'3' number:'5']" self.assertEquals(py3compat.text_type(parser.parseTest(source).value), result)
def summary_for(partner): pqs = records.filter(created_by=partner) return { 'partner': partner, 'nb_validations': pqs.count(), 'status': { s['validation_status']: { 'name': text_type( DataRecord.VALIDATION_STATUSES.get( s['validation_status'])), 'count': pqs.filter( validation_status=s['validation_status']).count(), 'all': pqs.filter(validation_status=s['validation_status']) } for s in pqs.values('validation_status') } }
def test_custom_toolset(self): """Make sure we can use a custom toolset inside the grammar.""" numbers_transform_grammar = """\ test_custom_toolset_numbers_transform <toolset> def to_real(node): node.value = float(node.value) <definition> SEP : ' ' : drop DOT : '.' digit : [0..9] integer : digit+ : join real : integer DOT integer? : join number : real / integer : to_real addedNum : SEP number : liftNode numbers : number (addedNum)* : extract """ make_parser = makeParser(numbers_transform_grammar) parser = make_parser() source = "1 3.141592 5" result = "[integer:'1.0' real:'3.141592' integer:'5.0']" self.assertEquals(py3compat.text_type(parser.parseTest(source).value), result)
def uuids(self): return text_type(self.uuid)
def malaria_monthly_routine_as_xls(report): """ Export les données d'un rapport en xls """ from snisi_core.models.Reporting import PERIODICAL_SOURCE def report_status_verbose(value): if is_aggregated(report): return value else: for v, name in report.YESNO.items(): if text_type(v) == value: return text_type(name) return value def is_aggregated(report): return report.REPORTING_TYPE != PERIODICAL_SOURCE # On crée le doc xls book = xlwt.Workbook(encoding='utf-8') # On crée une feuille nommé Report sheet = book.add_sheet("Report") # J'agrandi la colonne à trois fois la normale. sheet.col(0).width = 0x0d00 * 3 # Principe # write((nbre ligne - 1), nbre colonne, "conten", style(optionnel). # write_merge((nbre ligne - 1), (nbre ligne - 1) + nbre de ligne # à merger, (nbre de colonne - 1), (nbre de colonne - 1) + nbre # de colonne à merger, "conten", style(optionnel)). if is_aggregated(report): sheet.write_merge(0, 0, 0, 12, "Formulaire de Collecte - Données" "sur l'Information de Routime du PNLP - " "Niveau Aggrégé", styletitleform) else: sheet.write_merge(0, 0, 0, 12, "Formulaire de Collecte - Données" "sur l'Information de Routime du PNLP - " "Niveau Primaire", styletitleform) sheet.write(2, 0, "Localité", styledescription) sheet.write(3, 0, "Code SNISI", styledescription) sheet.write_merge(4, 5, 0, 1, "Classification", styletitle) sheet.write_merge( 6, 6, 0, 1, "Total consultation, toutes causes confondues", stylelabel) sheet.write_merge( 7, 7, 0, 1, "Nbre de Cas de paludisme (Tous suspectés)", stylelabel) sheet.write_merge( 8, 8, 0, 1, "Cas de paludisme testés (GE et/ou TDR)", stylelabel) sheet.write_merge( 9, 9, 0, 1, "Cas de paludisme confirmés (GE et/ou TDR)", stylelabel) sheet.write_merge( 10, 10, 0, 1, "Nbre de Cas de paludisme Simple", stylelabel) sheet.write_merge( 11, 11, 0, 1, "Nbre de Cas de paludisme Grave", stylelabel) sheet.write_merge(12, 12, 0, 1, "Nbre de Cas traités avec CTA", stylelabel) sheet.write_merge(13, 13, 0, 12, "") sheet.write_merge(14, 15, 0, 1, "Classification", styletitle) sheet.write_merge(16, 16, 0, 1, "Total Hospitalisations toutes" "causes confondues", stylelabel) sheet.write_merge(17, 17, 0, 1, "Total Hospitalisés Paludisme", stylelabel) sheet.write_merge(18, 18, 0, 12, "") sheet.write_merge(19, 20, 0, 1, "Classification", styletitle) sheet.write_merge(21, 21, 0, 1, "Total cas de décès toutes causes" "confondues", stylelabel) sheet.write_merge(22, 22, 0, 1, "Cas de décès pour paludisme", stylelabel) sheet.write_merge(23, 23, 0, 9, "") sheet.write_merge(24, 24, 0, 5, "Moustiquaires imprégnées" "d'insecticide distribuées", styletitle) sheet.write_merge(25, 25, 0, 1, "Classification", styletitle) sheet.write_merge(26, 26, 0, 1, "Nombre de moustiquaires" "distribuées", stylelabel) sheet.write_merge(27, 27, 0, 8, "") sheet.write_merge(28, 28, 0, 12, "", styleborformbutton) sheet.write_merge(2, 2, 1, 1, report.entity.display_short_health_hierarchy(), styleentity) sheet.write(3, 1, report.entity.slug, styletitle) sheet.write_merge(1, 1, 0, 12, "", styledescription) sheet.write(2, 2, "Mois", styledescription) sheet.write(2, 3, report.period.middle().month, styledate) sheet.write(2, 4, "", styledescription) sheet.write(2, 5, "Année", styledescription) sheet.write(2, 6, report.period.middle().year, styledate) sheet.write_merge(2, 2, 7, 12, "", styledescription) # SECTION Consultation sheet.write_merge(4, 4, 2, 7, "Consultation", styletitle) # les données de < 5 ans sheet.write_merge(5, 5, 2, 3, "< 5 ans", styletitle) sheet.write_merge( 6, 6, 2, 3, report.u5_total_consultation_all_causes, stylevariable) sheet.write_merge( 7, 7, 2, 3, report.u5_total_suspected_malaria_cases, stylevariable) sheet.write_merge( 8, 8, 2, 3, report.u5_total_tested_malaria_cases, stylevariable) sheet.write_merge( 9, 9, 2, 3, report.u5_total_confirmed_malaria_cases, stylevariable) sheet.write_merge( 10, 10, 2, 3, report.u5_total_simple_malaria_cases, stylevariable) sheet.write_merge( 11, 11, 2, 3, report.u5_total_severe_malaria_cases, stylevariable) sheet.write_merge( 12, 12, 2, 3, report.u5_total_treated_malaria_cases, stylevariable) # les données de 5 ans et plus sheet.write_merge(5, 5, 4, 5, "5 ans et plus", styletitle) sheet.write_merge( 6, 6, 4, 5, report.o5_total_consultation_all_causes, stylevariable) sheet.write_merge( 7, 7, 4, 5, report.o5_total_suspected_malaria_cases, stylevariable) sheet.write_merge( 8, 8, 4, 5, report.o5_total_tested_malaria_cases, stylevariable) sheet.write_merge( 9, 9, 4, 5, report.o5_total_confirmed_malaria_cases, stylevariable) sheet.write_merge( 10, 10, 4, 5, report.o5_total_simple_malaria_cases, stylevariable) sheet.write_merge( 11, 11, 4, 5, report.o5_total_severe_malaria_cases, stylevariable) sheet.write_merge( 12, 12, 4, 5, report.o5_total_treated_malaria_cases, stylevariable) # les données des Femmes enceintes sheet.write_merge(5, 5, 6, 7, "Femmes enceintes", styletitle) sheet.write_merge( 6, 6, 6, 7, report.pw_total_consultation_all_causes, stylevariable) sheet.write_merge( 7, 7, 6, 7, report.pw_total_suspected_malaria_cases, stylevariable) sheet.write_merge( 8, 8, 6, 7, report.pw_total_tested_malaria_cases, stylevariable) sheet.write_merge( 9, 9, 6, 7, report.pw_total_confirmed_malaria_cases, stylevariable) sheet.write_merge( 10, 10, 6, 7, report.pw_total_simple_malaria_cases or 0, stylevariable) sheet.write_merge( 11, 11, 6, 7, report.pw_total_severe_malaria_cases, stylevariable) sheet.write_merge( 12, 12, 6, 7, report.pw_total_treated_malaria_cases, stylevariable) # SECTION Hospitalisations sheet.write_merge(14, 14, 2, 7, "Hospitalisations", styletitle) # les données de < 5 ans sheet.write_merge(15, 15, 2, 3, "< 5 ans", styletitle) sheet.write_merge( 16, 16, 2, 3, report.u5_total_inpatient_all_causes, stylevariable) sheet.write_merge( 17, 17, 2, 3, report.u5_total_malaria_inpatient, stylevariable) # + 5 ans sheet.write_merge( 15, 15, 4, 5, " + 5 ans", styletitle) sheet.write_merge( 16, 16, 4, 5, report.o5_total_inpatient_all_causes, stylevariable) sheet.write_merge( 17, 17, 4, 5, report.o5_total_malaria_inpatient, stylevariable) # les données des Femmes enceintes sheet.write_merge(15, 15, 6, 7, "Femmes enceintes", styletitle) sheet.write_merge( 16, 16, 6, 7, report.pw_total_inpatient_all_causes, stylevariable) sheet.write_merge( 17, 17, 6, 7, report.pw_total_malaria_inpatient, stylevariable) # SECTION Decès sheet.write_merge(19, 19, 2, 7, "Decès", styletitle) # * les données de < 5 ans sheet.write_merge(20, 20, 2, 3, "< 5 ans", styletitle) sheet.write_merge( 21, 21, 2, 3, report.u5_total_death_all_causes, stylevariable) sheet.write_merge( 22, 22, 2, 3, report.u5_total_malaria_death, stylevariable) # les données de 5 ans et plus sheet.write_merge(20, 20, 4, 5, "5 ans et plus", styletitle) sheet.write_merge( 21, 21, 4, 5, report.o5_total_death_all_causes, stylevariable) sheet.write_merge( 22, 22, 4, 5, report.o5_total_malaria_death, stylevariable) # les données de Femmes enceintes sheet.write_merge(20, 20, 6, 7, "Femmes enceintes", styletitle) sheet.write_merge( 21, 21, 6, 7, report.pw_total_death_all_causes, stylevariable) sheet.write_merge( 22, 22, 6, 7, report.pw_total_malaria_death, stylevariable) # SECTION Moustiquaires imprégnéés d'insecticide distrivuées # < 5 ans sheet.write_merge(25, 25, 2, 3, "< 5 ans", styletitle) sheet.write_merge( 26, 26, 2, 3, report.u5_total_distributed_bednets, stylevariable) sheet.write_merge( 25, 25, 4, 5, "Femmes enceintes", styletitle) sheet.write_merge( 26, 26, 4, 5, report.pw_total_distributed_bednets, stylevariable) # SECTION Rupture de stock CTA pendant le mois (Oui, Non) sheet.write_merge(3, 3, 9, 12, "Rupture de stock CTA pendant" "le mois", styletitle) sheet.write_merge(4, 4, 9, 11, "CTA Nourisson - Enfant", stylelabel) sheet.write( 4, 12, report_status_verbose( report.stockout_act_children), stylevariable) sheet.write_merge(5, 5, 9, 11, "CTA Adolescent", stylelabel) sheet.write( 5, 12, report_status_verbose(report.stockout_act_youth), stylevariable) sheet.write_merge(6, 6, 9, 11, "CTA Adulte", stylelabel) sheet.write( 6, 12, report_status_verbose(report.stockout_act_adult), stylevariable) sheet.write_merge(7, 7, 8, 12, "") # SECTION PEC de cas de Paludisme grave Rupture de soctk OUI/NON sheet.write_merge(8, 8, 9, 12, "PEC de cas de Paludisme grave", styletitle) sheet.write_merge(9, 9, 9, 12, "Rupture de soctk OUI/NON", styletitle) sheet.write_merge(10, 10, 9, 11, "Arthemether injectable", stylelabel) sheet.write( 10, 12, report_status_verbose( report.stockout_artemether), stylevariable) sheet.write_merge(11, 11, 9, 11, "Quinine Injectable", stylelabel) sheet.write( 11, 12, report_status_verbose(report.stockout_quinine), stylevariable) sheet.write_merge(12, 12, 9, 11, "Serum", stylelabel) sheet.write( 12, 12, report_status_verbose(report.stockout_serum), stylevariable) # SECTION Rupture de stock pendant le mois O/N (Oui, Non) sheet.write_merge( 14, 14, 10, 12, "Rupture de stock pendant le mois O/N", styletitle) sheet.write_merge(15, 15, 10, 11, "MILD", stylelabel) sheet.write( 15, 12, report_status_verbose(report.stockout_bednet), stylevariable) sheet.write_merge(16, 16, 10, 11, "TDR", stylelabel) sheet.write( 16, 12, report_status_verbose(report.stockout_rdt), stylevariable) sheet.write_merge(17, 17, 10, 11, "SP", stylelabel) sheet.write( 17, 12, report_status_verbose(report.stockout_sp), stylevariable) # SECTION CPN/SP des femme s enceintes (nbre) sheet.write_merge( 19, 20, 10, 12, "CPN/SP des femmes enceintes (nbre)", styletitleform) sheet.write_merge(21, 21, 10, 11, "CPN 1", stylelabel) sheet.write(21, 12, report.pw_total_anc1, stylevariable) sheet.write_merge(22, 22, 10, 11, "SP 1", stylelabel) sheet.write(22, 12, report.pw_total_sp1, stylevariable) sheet.write_merge(23, 23, 10, 11, "SP 2 et +", stylelabel) sheet.write(23, 12, report.pw_total_sp2, stylevariable) sheet.write(25, 9, "Nom et Prénom : {}".format(report.created_by.name())) sheet.write(26, 9, "Le Responsable CSCom/CSRéf") sheet.write(27, 9, "Date :") sheet.write(27, 10, report.created_on.day, styledate) sheet.write(27, 11, report.created_on.month, styledate) sheet.write(27, 12, report.created_on.year, styledate) sheet.write_merge(0, 28, 13, 13, "", styleborformright) if report.validated_on: validation_str = "Validé le {on} par {by}".format( on=text_type(report.validated_on.strftime("%c").decode('utf8')), by=text_type(report.validated_by)) if report.auto_validated: validation_str = "{} (auto)".format(validation_str) sheet.write(31, 0, validation_str) else: sheet.write(31, 0, report.verbose_validation_status) sheet.write(32, 0, report.verbose_arrival_status) # if report.REPORTING_LEVEL == AGGREGATED_LEVEL: if is_aggregated(report): sheet.write(35, 0, "Sources Primaires", styletitle) i = 36 for source in report.indiv_sources.all(): source_str = "%(entity)s - %(receipt)s" \ % {'entity': source.entity.display_name(), 'receipt': report.receipt} sheet.write(i, 0, source_str, stylelabel) i += 1 sheet.write(i, 0, "Sources Agrégées", styletitle) i += 1 for source in report.agg_sources.all(): source_str = "%(entity)s - %(receipt)s" \ % {'entity': source.entity.display_name(), 'receipt': report.receipt} sheet.write(i, 0, source_str, stylelabel) i += 1 stream = StringIO.StringIO() book.save(stream) return stream
def parsed_equal_string(self, source, result, method_name, templates={}, postprocessor='raw'): preprocessed = self._preprocessor(templates).parseTest(source).value self.assertEquals(py3compat.text_type(self._grammar(method_name, postprocessor).parseTest(preprocessed).leaves()), result)
def parsed_equal_string(self, source, result, templates={}): self.assertEquals(py3compat.text_type(self._grammar(templates).parseTest(source).value), result)
def main(arguments): debug = arguments.get('--verbose') or False change_logging_level(debug) json_folder = arguments.get('--json') or None html_path = arguments.get('--output') or "dashboard.html" logger.info("Generating Dashboard.") # load global statistics with open(os.path.join(json_folder, 'statistics.json')) as f: statistics = OrderedDict(sorted(json.load(f).items(), key=tssort)) # update stats with price data for key in statistics.keys(): if key == 'relayers': continue statistics[key]['stats']['estimated_price'] = \ estimated_price_for(statistics[key], statistics['relayers']) statistics[key]['stats']['estimated_vat'] = \ multiply_items(statistics[key]['stats']['estimated_price'], 0.18) statistics[key]['stats']['estimated_price_total'] = \ multiply_items(statistics[key]['stats']['estimated_price'], 1.18) # load daily total for each month def loadjs(key): with open(os.path.join(json_folder, '{}.json'.format(key)), 'r') as f: return OrderedDict(sorted(json.load(f).items(), key=tssort)) daily_data = { key: loadjs(key) for key in sorted(statistics.keys()) if key not in ('total', 'relayers') } # cumulative values for each days with open(os.path.join(json_folder, 'cumulative.json')) as f: cumulative = OrderedDict(sorted(json.load(f).items(), key=tssort)) # list of fields to loop on fields = OrderedDict([ ('nb_sms_total', "Nombre SMS TOTAL"), ('nb_sms_in', "Nombre SMS Entrant"), ('nb_sms_out', "Nombre SMS Sortant"), ('nb_messages_total', "Nombre Messages TOTAL"), ('nb_messages_in', "Nombre Messages Entrant"), ('nb_messages_out', "Nombre Messages Sortant"), ('estimated_price', "Coût estimatif HT"), ('estimated_vat', "Coût estimatif TVA"), ('estimated_price_total', "Coût estimatif TTC"), ]) # prepare context context = { 'update_time': datetime_from_iso(statistics['total']['update_time']).strftime( '%d %B %Y, %Hh%I').decode('utf-8'), 'relayers': OrderedDict( sorted(statistics['relayers'].items(), key=lambda x: x[1]['relayer'])), 'months_data': OrderedDict([(k, v) for k, v in sorted(statistics.items(), reverse=True) if k != 'relayers']), 'daily_data': daily_data, 'cumulative': cumulative, 'fields': fields, 'orange_id': ORANGE, 'malitel_id': MALITEL, 'orange_key': text_type(ORANGE), 'malitel_key': text_type(MALITEL), 'amount_fields': [k for k in fields.keys() if k.startswith('estimated_')] } # render template in file template = jinja_env.get_template('dashboard_tmpl.html') with open(html_path, 'w') as f: html = template.render(**context) if PY2: html = html.encode('utf-8') f.write(html)
def write_month(sheet, period, entity, expected, start_row, is_sam): is_mam = not is_sam # shortcut row = start_row # counter # write a line containing only period name period_name = get_periods_str(period) \ if isinstance(period, list) else text_type(period) sheet.write_merge(row, row, 0, 27, period_name.upper(), style_period) # update row size for period name xl_set_row_height(sheet, row, 0.69) # update row count row += 1 # if not expected, just move to next period if expected is None or not expected.arrived_report: return row report = expected.arrived_report() # URENAM if is_mam: children = get_children(entity, is_sam=False) nb_lines = nb_lines_for(URENAMNutritionR, len(children)) # write URENAM (vertical merge) sheet.write_merge(row, row + nb_lines - 1, 0, 0, "URENAM", style_label_uren) age_groups = URENAMNutritionR.age_groups() nb_age_groups = len(age_groups) for child in children: # write entity name (spans over all ages + total) sheet.write_merge(row, row + nb_age_groups, 1, 1, text_type(child), style_label) for age_group in URENAMNutritionR.age_groups(): # write line for this age group write_line(sheet, child, children, age_group, age_groups, report, expected, row, False, False) row += 1 # write line for the total of all age group at this entity write_line(sheet, child, children, None, age_groups, report, expected, row, False, False) row += 1 # write summary of all children for that period (for each age group) sheet.write_merge( row, row + len(URENAMNutritionR.age_groups()), 1, 1, "TOTAL", style_label) for age_group in URENAMNutritionR.age_groups(): # write line for this age group write_line(sheet, "TOTAL", children, age_group, age_groups, report, expected, row, True, True) row += 1 # write line for the total of all age group at this entity write_line(sheet, "TOTAL", children, None, age_groups, report, expected, row, True, True) row += 1 else: # SAM sheet contains URENI alone first then URENAS alone ### # URENI ### children = get_children(entity, is_sam, is_ureni=True) nb_lines = nb_lines_for(URENINutritionR, len(children)) nb_ureni_children = len(children) # write URENI (vertical merge) sheet.write_merge(row, row + nb_lines - 1, 0, 0, "URENI", style_label_uren) age_groups = URENINutritionR.age_groups() nb_age_groups = len(age_groups) for child in children: # entity name spans over all ages + total sheet.write_merge(row, row + nb_age_groups, 1, 1, text_type(child), style_label) for age_group in URENINutritionR.age_groups(): # write line for this age group write_line(sheet, child, children, age_group, age_groups, report, expected, row, True, True) row += 1 # write line for the total of all age group at this entity write_line(sheet, child, children, None, age_groups, report, expected, row, True, True) row += 1 # write summary of all children for that period (for each age group) sheet.write_merge(row, row + nb_age_groups, 1, 1, "TOTAL", style_label) for age_group in URENINutritionR.age_groups(): # write line for this age group write_line(sheet, "TOTAL", children, age_group, age_groups, report, expected, row, True, True) row += 1 # write line for the total of all age group at this entity write_line(sheet, "TOTAL", children, None, age_groups, report, expected, row, True, True) row += 1 ### # URENAS ### children = get_children(entity, is_sam, is_ureni=False) nb_lines = nb_lines_for(URENASNutritionR, len(children)) nb_urenas_children = len(children) # write URENAS (vertical merge) sheet.write_merge(row, row + nb_lines - 1, 0, 0, "URENAS", style_label_uren) age_groups = URENASNutritionR.age_groups() nb_age_groups = len(age_groups) for child in children: # entity name spans over all ages + total sheet.write_merge(row, row + nb_age_groups, 1, 1, text_type(child), style_label) for age_group in URENASNutritionR.age_groups(): # write line for this age group write_line(sheet, child, children, age_group, age_groups, report, expected, row, True, False) row += 1 # write line for the total of all age group at this entity write_line(sheet, child, children, None, age_groups, report, expected, row, True, False) row += 1 # write summary of all children for that period (for each age group) sheet.write_merge(row, row + nb_age_groups, 1, 1, "TOTAL", style_label) for age_group in age_groups: # write line for this age group write_line(sheet, "TOTAL", children, age_group, age_groups, report, expected, row, True, False) row += 1 # write line for the total of all age group at this entity write_line(sheet, "TOTAL", children, None, age_groups, report, expected, row, True, False) row += 1 ### # URENI + URENAS ### age_groups = list(OrderedDict.fromkeys( URENINutritionR.age_groups() + URENASNutritionR.age_groups())) nb_age_groups = len(age_groups) + 1 # write URENI + URENAS (vertical merge) sheet.write_merge(row, row + nb_age_groups - 1, 0, 1, "TOTAL URENI + URENAS", style_label_uren2) for age_group in age_groups: # write line for this age group write_line(sheet, "TOTAL URENI + URENAS", children, "sam_{}".format(age_group), age_groups, report, expected, row, True, None, nb_ureni_children=nb_ureni_children, nb_ureni_age_groups=len(URENINutritionR.age_groups()), nb_urenas_children=nb_urenas_children, nb_urenas_age_groups=len(URENASNutritionR.age_groups())) row += 1 # write line for the total of all age group at this entity write_line(sheet, "TOTAL", children, None, age_groups, report, expected, row, True, None, nb_ureni_children=nb_ureni_children, nb_ureni_age_groups=len(URENINutritionR.age_groups()), nb_urenas_children=nb_urenas_children, nb_urenas_age_groups=len(URENASNutritionR.age_groups())) row += 1 return row
def price_for_malitel(month_data): # Malitel are unique price 20F/sms in&out nb_total = month_data['stats']['nb_sms_total'][text_type(MALITEL)] price = 20 if nb_total <= 30000 else 15 return nb_total * price
def parsed_equal_string(self, source, result, templates={}): self.assertEquals( py3compat.text_type( self._grammar(templates).parseTest(source).value), result)
def verbose_integrity_status(self): return text_type(self.INTEGRITY_STATUSES.get(self.integrity_status))
def verbose_completion_status(self): return text_type(self.COMPLETION_STATUSES.get(self.completion_status))
def verbose_validation_status(self): return text_type(self.VALIDATION_STATUSES.get(self.validation_status))
def verbose_arrival_status(self): return text_type(self.ARRIVAL_STATUSES.get(self.arrival_status))
def unit_str(cls, input_slug): return text_type(cls.INPUTS_UNITS.get(input_slug))
def process(source): source = source.strip() if not source.endswith("\n"): source += "\n" preprocessed = preprocessor_parser.parseTest(source).value return py3compat.text_type(parser.parseTest(preprocessed).leaves())
def view(request, entity_uuid=None, perioda_str=None, periodb_str=None, indicator_slug=None, **kwargs): context = {'page': 'analysis_section2'} # handling entity context.update(process_entity_filter(request, entity_uuid)) # handling periods # context.update(process_period_filter(request, period_str, 'period')) context.update(process_period_filter(request, perioda_str, 'perioda')) context.update(process_period_filter(request, periodb_str, 'periodb')) if context['perioda'] > context['periodb']: context['perioda'], context['periodb'] = \ context['periodb'], context['perioda'] periods = MonthPeriod.all_from(context['perioda'], context['periodb']) context.update({'selected_periods': periods}) def cached_data_list(entity, periods, indicator): return [get_cached_data('section2-arrivals', entity=entity, period=period, indicator=indicator) for period in periods] context.update({ 'section': text_type(SECTION_ID), 'section_name': SECTION_NAME, 'arrivals': OrderedDict( [(indicator, cached_data_list(context['entity'], periods, indicator)) for indicator in Indicator.get_all_routine()]) }) # evolution graph cp = { 'periods': [period.to_tuple() for period in periods], 'points': [get_cached_data('section2-points', entity=context['entity'], period=period) for period in periods] } perioda = periods[0] periodb = periods[-1] context.update({ 'cp_title': "Évolution de la complétude à {name} entre {pa} et {pb}" .format(name=context['entity'].short_name, pa=perioda.strid, pb=periodb.strid), 'cp_fname': "completeness-_{pa}_{pb}" .format(pa=perioda.strid, pb=periodb.strid), 'cp_categories': [p[1].name for p in cp['periods']], 'cp_series': [{'name': "Complétude", 'data': cp['points']}] }) # absolute URI for links context.update({ 'baseurl': request.build_absolute_uri(), 'lineage': [Entity.PROVINCE]}) return render(request, kwargs.get('template_name', 'analysis_section2.html'), context)
def view(request, entity_uuid=None, perioda_str=None, periodb_str=None, indicator_slug=None, **kwargs): context = {'page': 'analysis_section2'} # handling entity context.update(process_entity_filter(request, entity_uuid)) # handling periods # context.update(process_period_filter(request, period_str, 'period')) context.update(process_period_filter(request, perioda_str, 'perioda')) context.update(process_period_filter(request, periodb_str, 'periodb')) if context['perioda'] > context['periodb']: context['perioda'], context['periodb'] = \ context['periodb'], context['perioda'] periods = MonthPeriod.all_from(context['perioda'], context['periodb']) context.update({'selected_periods': periods}) def cached_data_list(entity, periods, indicator): return [ get_cached_data('section2-arrivals', entity=entity, period=period, indicator=indicator) for period in periods ] context.update({ 'section': text_type(SECTION_ID), 'section_name': SECTION_NAME, 'arrivals': OrderedDict([(indicator, cached_data_list(context['entity'], periods, indicator)) for indicator in Indicator.get_all_routine()]) }) # evolution graph cp = { 'periods': [period.to_tuple() for period in periods], 'points': [ get_cached_data('section2-points', entity=context['entity'], period=period) for period in periods ] } perioda = periods[0] periodb = periods[-1] context.update({ 'cp_title': "Évolution de la complétude à {name} entre {pa} et {pb}".format( name=context['entity'].short_name, pa=perioda.strid, pb=periodb.strid), 'cp_fname': "completeness-_{pa}_{pb}".format(pa=perioda.strid, pb=periodb.strid), 'cp_categories': [p[1].name for p in cp['periods']], 'cp_series': [{ 'name': "Complétude", 'data': cp['points'] }] }) # absolute URI for links context.update({ 'baseurl': request.build_absolute_uri(), 'lineage': [Entity.PROVINCE] }) return render(request, kwargs.get('template_name', 'analysis_section2.html'), context)
def input_str(cls, input_slug): return text_type(cls.INPUTS_LABELS.get(input_slug))
def table_for_indicator(document, indicator_table): if not indicator_table.nb_lines(): return error_widget("Aucune période") minify = indicator_table.periods > 3 col_large = 3000 col_regular = 1000 col_intro_mini = 2600 col_mini = 670 col_mini_pc = 570 thin_edge = BorderPS(width=20, style=BorderPS.SINGLE) # thick_edge = BorderPS( width=80, style=BorderPS.SINGLE ) def p(text, align_center=False): text_elem = TEXT(text, size=14) if minify else text return Paragraph(ParagraphPS( alignment=ParagraphPS.CENTER if align_center else ParagraphPS.LEFT), text_elem) thin_frame = FramePS(thin_edge, thin_edge, thin_edge, thin_edge) # thick_frame = FramePS( thick_edge, thick_edge, thick_edge, thick_edge ) # mixed_frame = FramePS( thin_edge, thick_edge, thin_edge, thick_edge ) # build base table (7 columns) cols = [col_intro_mini] if minify else [col_large] for _ in indicator_table.periods: cols.append(col_mini if minify else col_regular) if indicator_table.add_percentage: cols.append(col_mini_pc if minify else col_regular) if indicator_table.add_total: cols.append(col_mini if minify else col_regular) table = Table(*cols, alignment=Table.CENTER) # first header row : title and period names args = [] for period in indicator_table.periods: span = 2 if indicator_table.add_percentage else 1 args.append(Cell(p(text_type(period), True), thin_frame, span=span)) if indicator_table.add_total: args.append(Cell(p("TOTAL", True), thin_frame)) table.AddRow(Cell(indicator_table.title, thin_frame, start_vertical_merge=True), *args) # second header row : Nbre/% sub header args = [] for period in indicator_table.periods: args.append(Cell(p("Nbre", True), thin_frame)) if indicator_table.add_percentage: args.append(Cell(p("%", True), thin_frame)) if indicator_table.add_total: args.append(Cell(p("Nbre", True), thin_frame)) table.AddRow(Cell(thin_frame, vertical_merge=True), *args) # data rows for line in indicator_table.render_with_labels_human(as_human=True): args = [] for idx, cell_data in enumerate(line): align_center = not idx == 0 args.append(Cell(p(number_format(cell_data), align_center), thin_frame)) table.AddRow(*args) return table
def __unicode__(self): itemText = u" ".join(py3compat.text_type(item) for item in self) return "[%s]" % itemText