util: Add common word-slicing functions.
This commit is contained in:
parent
e249049fc1
commit
8da3fd4bd3
2 changed files with 17 additions and 5 deletions
|
@ -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)
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue