def make_raw_bills(self): titles = [t.strip() for t in next(iter(self.reader))] title_idx = dict((title, i) for i, title in enumerate(titles)) raw_bills = [] for vals in self.reader: if len(vals) == 0 or vals[0].startswith('#') or \ ''.join(vals) == '': continue def val(title): idx = title_idx[title] try: return vals[idx] except KeyError: raise BadRequest( "For title " + title + " and index " + str(idx)) def date_val(title): try: return Datetime.strptime( val(title), "%d/%m/%Y").replace(tzinfo=pytz.utc) except ValueError as e: raise BadRequest( "At line number " + str(self._line_number) + ", while trying to find the value of " + title + " the date couldn't be parsed. " + str(e) + " The full line is " + self.last_line) issue_date = date_val('Invoice Date') bill_from = validate_hh_start(date_val('Bill Period Start')) bill_to = validate_hh_start(date_val('Bill Period End')) + \ relativedelta(days=1) - HH kwh = Decimal(val('Site Data')) breakdown = dict( [ (v, float(val(k))) for k, v in col_map.items() if k in titles]) breakdown['raw_lines'] = [self._title_line, self.last_line] net = Decimal(val('NET_AMT')) vat = Decimal(val('VATTTL')) gross = Decimal(val('TTLCHG')) raw_bills.append( { 'bill_type_code': 'N', 'account': val('Bill Ref No.'), 'mpans': [], 'reference': val('Invoice No.'), 'issue_date': issue_date, 'start_date': bill_from, 'finish_date': bill_to, 'kwh': kwh, 'net': net, 'vat': vat, 'gross': gross, 'breakdown': breakdown, 'reads': []}) return raw_bills
def __next__(self): try: self.line_number, self.values = next(self.shredder) mpan_core_str = self.get_field(0, "MPAN Core") datum = {'mpan_core': parse_mpan_core(mpan_core_str)} channel_type_str = self.get_field(1, "Channel Type") datum['channel_type'] = parse_channel_type(channel_type_str) start_date_str = self.get_field(2, "Start Date") datum['start_date'] = validate_hh_start( to_utc(Datetime.strptime(start_date_str, "%Y-%m-%d %H:%M"))) value_str = self.get_field(3, "Value") datum['value'] = Decimal(value_str) status = self.get_field(4, "Status") if len(status) != 1: raise BadRequest( "The status character must be one character in length.") datum['status'] = status return datum except BadRequest as e: e.description = ''.join("Problem at line number: ", str(self.line_number), ": ", str(self.values), ": ", e.description) raise e
def __next__(self): try: self.line_number, self.values = next(self.shredder) mpan_core_str = self.get_field(0, "MPAN Core") datum = {'mpan_core': parse_mpan_core(mpan_core_str)} channel_type_str = self.get_field(1, "Channel Type") datum['channel_type'] = parse_channel_type(channel_type_str) start_date_str = self.get_field(2, "Start Date") datum['start_date'] = validate_hh_start( datetime.datetime.strptime( start_date_str, "%Y-%m-%d %H:%M").replace(tzinfo=pytz.utc)) value_str = self.get_field(3, "Value") datum['value'] = decimal.Decimal(value_str) status = self.get_field(4, "Status") if len(status) != 1: raise BadRequest( "The status character must be one character in length.") datum['status'] = status return datum except BadRequest as e: e.description = ''.join( "Problem at line number: ", str(self.line_number), ": ", str(self.values), ": ", e.description) raise e
def parse_date(row, idx, is_finish=False): date_str = row[idx].strip() try: if len(date_str) == 10: dt = utc_datetime_parse(date_str, "%Y-%m-%d") if is_finish: dt = dt + relativedelta(days=1) - HH else: dt = utc_datetime_parse(date_str, "%Y-%m-%d %H:%M") return validate_hh_start(dt) except ValueError as e: raise BadRequest( "Difficulty parsing a date in the row {row} at position " "{idx}: {e}".format(row=row, idx=idx, e=e))
def parse_date(date_string): return validate_hh_start(datetime.datetime.strptime(date_string, "%Y%m%d"))
def make_raw_bills(self): titles = [t.strip() for t in next(iter(self.reader))] title_idx = dict((title, i) for i, title in enumerate(titles)) raw_bills = [] for vals in self.reader: if len(vals) == 0 or vals[0].startswith('#') or \ ''.join(vals) == '': continue def val(title): idx = title_idx[title] try: return vals[idx] except KeyError: raise BadRequest("For title " + title + " and index " + str(idx)) def date_val(title): try: return to_utc(Datetime.strptime(val(title), "%d/%m/%Y")) except ValueError as e: raise BadRequest("At line number " + str(self._line_number) + ", while trying to find the value of " + title + " the date couldn't be parsed. " + str(e) + " The full line is " + self.last_line) def dec_val(title): return Decimal(val(title)) issue_date = date_val('Invoice Date') bill_from = validate_hh_start(date_val('Bill Period Start')) bill_to = validate_hh_start(date_val('Bill Period End')) + \ relativedelta(days=1) - HH kwh = dec_val('Site Data') breakdown = dict([(v, dec_val(k)) for k, v in col_map.items() if k in titles]) breakdown['raw_lines'] = [ self._title_line.strip(), self.last_line.strip() ] net = Decimal('0.00') + dec_val('NET_AMT') vat = Decimal('0.00') + dec_val('VATTTL') gross = Decimal('0.00') + dec_val('TTLCHG') raw_bills.append({ 'bill_type_code': 'N', 'account': val('Bill Ref No.'), 'mpans': [], 'reference': val('Invoice No.'), 'issue_date': issue_date, 'start_date': bill_from, 'finish_date': bill_to, 'kwh': kwh, 'net': net, 'vat': vat, 'gross': gross, 'breakdown': breakdown, 'reads': [] }) return raw_bills
def make_raw_bills(self): titles = [t.strip() for t in next(iter(self.reader))] title_idx = dict((title, i) for i, title in enumerate(titles)) raw_bills = [] for vals in self.reader: if len(vals) == 0 or vals[0].startswith("#") or "".join( vals) == "": continue def val(title): idx = title_idx[title] try: return vals[idx] except KeyError: raise BadRequest("For title " + title + " and index " + str(idx)) def date_val(title): try: return to_utc(Datetime.strptime(val(title), "%d/%m/%Y")) except ValueError as e: raise BadRequest("At line number " + str(self._line_number) + ", while trying to find the value of " + title + " the date couldn't be parsed. " + str(e) + " The full line is " + self.last_line) def dec_val(title): return Decimal(val(title)) issue_date = date_val("Invoice Date") bill_from = validate_hh_start(date_val("Bill Period Start")) bill_to = (validate_hh_start(date_val("Bill Period End")) + relativedelta(days=1) - HH) kwh = dec_val("Site Data") breakdown = dict([(v, dec_val(k)) for k, v in col_map.items() if k in titles]) breakdown["raw_lines"] = [ self._title_line.strip(), self.last_line.strip() ] net = Decimal("0.00") + dec_val("NET_AMT") vat = Decimal("0.00") + dec_val("VATTTL") gross = Decimal("0.00") + dec_val("TTLCHG") mpan_core = parse_mpan_core(val("Mpan")) raw_bill = { "bill_type_code": "N", "account": val("Bill Ref No."), "mpan_core": mpan_core, "reference": val("Invoice No."), "issue_date": issue_date, "start_date": bill_from, "finish_date": bill_to, "kwh": kwh, "net": net, "vat": vat, "gross": gross, "breakdown": breakdown, "reads": [], } raw_bills.append(raw_bill) return raw_bills