def classify(text): print( 'Categorizer: classifying {text} with google cloud'.format(text=text)) language_client = language_v1.LanguageServiceClient() document = language_v1.Document(content=text, type_=language_v1.Document.Type.PLAIN_TEXT) response = language_client.classify_text(request={'document': document}) category = get(nth(get(response, 'categories')), 'name') print('Categorizer: google cloud category -> {category}'.format( category=category)) if category: return nth(split(category, '/'), 1)
def process_davivienda_message(message): print('Davivienda: processing message') amount = re.sub(r'[^\d.]', '', nth(lines(message.string), 6)) category_type = nth(lines(message.string), 7) desc = trim(nth(split(nth(lines(message.string), 8), ':'), 1)) if has_substr(upper_case(desc), 'PSE'): print('Davivienda: Ignored PSE payment') return None, None, None if has_substr(lower_case(category_type), 'deposito') or has_substr( lower_case(category_type), 'abono'): category_type = moneylover.CATEGORY_TYPE['income'] category_item = 'Salary' if has_substr( upper_case(desc), 'ACH GNB SUDAMERIS') else 'Others' else: category_type = moneylover.CATEGORY_TYPE['expense'] category_item = categorize(desc) return amount, {'type': category_type, 'item': category_item}, desc
def process_pse_message(message): print('PSE(davivienda): processing message') data = lines(last(message.table.table.find_all('span'))) desc = re.sub(r'<[^<>]*>', '', nth(data)) amount = re.sub(r'[^\d,]', '', nth(data, 1)).replace(',', '.') is_visa = has_substr(lower_case(desc), 'credito visa') if is_visa: visa_category_type = moneylover.CATEGORY_TYPE['income'] visa_category_item = 'Payment' category_type = moneylover.CATEGORY_TYPE['expense'] category_item = categorize(desc) return amount, { 'type': category_type, 'item': category_item }, desc, { 'type': visa_category_type, 'item': visa_category_item } if is_visa else None
def process_scotiabank_message(message): print('Scotiabank: processing message') desc = nth(message.table.find_all('p'), 3).string amount = re.sub(r'[^\d.]', '', nth(message.table.find_all('p'), 4).string) category_item = categorize(desc) return amount, {'type': 'EXPENSE', 'item': category_item}, desc
# coding:utf-8 import pydash from pydash.arrays import iterflatten a = [0] * 10 b = [1, 3, 5, 9] print(pydash.nth([1, 2, 3], 0)) def change(x): a[x] = 1 return a # print map(change, b) # change(x) for xi in b: a[xi] = 1 def change1(b): for xi in b: a[xi] = 1 return a if __name__ == '__main__': iterflatten(array, depth=-1)
def test_nth(case, pos, expected): assert _.nth(case, pos) == expected