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
|
DISCOUNT_TICKET_RATE = STANDARD_TICKET_RATE / 2
|
||||||
STANDARD_SHIRT_RATE = decimal.Decimal('25.50')
|
STANDARD_SHIRT_RATE = decimal.Decimal('25.50')
|
||||||
DISCOUNT_SHIRT_RATE = STANDARD_SHIRT_RATE
|
DISCOUNT_SHIRT_RATE = STANDARD_SHIRT_RATE
|
||||||
DATE_FMT = '%b. %d, %Y,'
|
DATE_FMT = '%b. %d, %Y'
|
||||||
CURRENCY = 'USD'
|
CURRENCY = 'USD'
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -81,10 +81,9 @@ class Invoice2017:
|
||||||
|
|
||||||
def _read_invoice_activity(self, table, first_row_text, rows_text):
|
def _read_invoice_activity(self, table, first_row_text, rows_text):
|
||||||
self.actions = []
|
self.actions = []
|
||||||
date_wordcount = self.DATE_FMT.count(' ') + 1
|
|
||||||
for timestamp, description, amount in rows_text:
|
for timestamp, description, amount in rows_text:
|
||||||
if description.startswith('Paid '):
|
if description.startswith('Paid '):
|
||||||
last_stripe_id = description.rsplit(None, 1)[1]
|
last_stripe_id = util.rslice_words(description, 1, limit=1)
|
||||||
action = {
|
action = {
|
||||||
'multiplier': 1,
|
'multiplier': 1,
|
||||||
'payment_id': last_stripe_id,
|
'payment_id': last_stripe_id,
|
||||||
|
@ -93,8 +92,8 @@ class Invoice2017:
|
||||||
# Refund handling could go here, if we need it.
|
# Refund handling could go here, if we need it.
|
||||||
continue
|
continue
|
||||||
# Trim extraneous text like the time/a.m./p.m.
|
# Trim extraneous text like the time/a.m./p.m.
|
||||||
date_words = timestamp.split(' ', date_wordcount + 1)[:date_wordcount]
|
date_str = util.rejoin_slice_words(timestamp, slice(2), ',', 2)
|
||||||
action['date'] = util.strpdate(' '.join(date_words), self.DATE_FMT)
|
action['date'] = util.strpdate(date_str, self.DATE_FMT)
|
||||||
action['stripe_id'] = last_stripe_id
|
action['stripe_id'] = last_stripe_id
|
||||||
self.actions.append(action)
|
self.actions.append(action)
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,17 @@
|
||||||
import datetime
|
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):
|
def strpdate(date_s, date_fmt):
|
||||||
return datetime.datetime.strptime(date_s, date_fmt).date()
|
return datetime.datetime.strptime(date_s, date_fmt).date()
|
||||||
|
|
Loading…
Reference in a new issue