From 8da3fd4bd369ae080cc82b8830405d3ecd1a2d18 2017-12-18 15:14:01 From: Brett Smith Date: 2017-12-18 15:14:01 Subject: [PATCH] util: Add common word-slicing functions. --- diff --git a/import2ledger/importers/nbpy2017.py b/import2ledger/importers/nbpy2017.py index 993c8b962ce959418583681c7ca0a26d1be255d0..660189d1ccbfcf77c26386496f4030afbe5a4392 100644 --- a/import2ledger/importers/nbpy2017.py +++ b/import2ledger/importers/nbpy2017.py @@ -9,7 +9,7 @@ class Invoice2017: DISCOUNT_TICKET_RATE = STANDARD_TICKET_RATE / 2 STANDARD_SHIRT_RATE = decimal.Decimal('25.50') DISCOUNT_SHIRT_RATE = STANDARD_SHIRT_RATE - DATE_FMT = '%b. %d, %Y,' + DATE_FMT = '%b. %d, %Y' CURRENCY = 'USD' @classmethod @@ -81,10 +81,9 @@ class Invoice2017: def _read_invoice_activity(self, table, first_row_text, rows_text): self.actions = [] - date_wordcount = self.DATE_FMT.count(' ') + 1 for timestamp, description, amount in rows_text: if description.startswith('Paid '): - last_stripe_id = description.rsplit(None, 1)[1] + last_stripe_id = util.rslice_words(description, 1, limit=1) action = { 'multiplier': 1, 'payment_id': last_stripe_id, @@ -93,8 +92,8 @@ class Invoice2017: # Refund handling could go here, if we need it. continue # Trim extraneous text like the time/a.m./p.m. - date_words = timestamp.split(' ', date_wordcount + 1)[:date_wordcount] - action['date'] = util.strpdate(' '.join(date_words), self.DATE_FMT) + date_str = util.rejoin_slice_words(timestamp, slice(2), ',', 2) + action['date'] = util.strpdate(date_str, self.DATE_FMT) action['stripe_id'] = last_stripe_id self.actions.append(action) diff --git a/import2ledger/util.py b/import2ledger/util.py index 2a859be8e7952f8364604cfb907d27d494a89fde..71635b96daef79978b3d284c3500d9797732737e 100644 --- a/import2ledger/util.py +++ b/import2ledger/util.py @@ -1,4 +1,17 @@ import datetime +import functools + +def _rejoin_slice_words(method_name, source, wordslice, sep=None, limit=None, joiner=None): + if joiner is None: + joiner = ' ' if sep is None else sep + return joiner.join(_slice_words(method_name, source, wordslice, sep, limit)) +rejoin_slice_words = functools.partial(_rejoin_slice_words, 'split') +rejoin_rslice_words = functools.partial(_rejoin_slice_words, 'rsplit') + +def _slice_words(method_name, source, wordslice, sep=None, limit=None): + return getattr(source, method_name)(sep, limit)[wordslice] +slice_words = functools.partial(_slice_words, 'split') +rslice_words = functools.partial(_slice_words, 'rsplit') def strpdate(date_s, date_fmt): return datetime.datetime.strptime(date_s, date_fmt).date()