好吧,我甚至不能完全确定我的标题是否完全准确,因为我当时完全不了解类继承和实例,但我知道这是我需要或应该掌握的东西。你知道吗
背景:尝试为我的银行创建一个自定义导入程序,以便与流行的Beancount/fava复式记账会计系统一起使用。我最初是作为一个bug向fava报告的,但后来意识到它不是bug,更重要的是我对Python类缺乏一般性的理解,所以我认为最好在这里发布。你知道吗
所以…我创建了下面的导入脚本文件,据我所知,它是beancount的一个子类csv.导入程序(https://github.com/beancount/beancount/blob/master/beancount/ingest/importers/csv.py)是beancount导入程序(https://github.com/beancount/beancount/blob/master/beancount/ingest/importer.py)的子类
在我的进口商我骑过两种方法csv.导入程序,name()和file\u account()。我的目标是根据文件名和字典查找导出与输入文件关联的源帐户。我不想在我的子类中使用extract()方法,但是在csv.导入程序有引用的extract()方法自助帐户表示用于提取事务的源帐户的。目前我的脚本是这样的,如果我给它一个名为'SIMPLII_9999_2018-01-01.csv'的文件,帐户将正确派生为'资产:Simplii:Chequing-9999'。但是,如果我没有在fava中实际导入事务,而是尝试从同一个文件中再次提取事务,则派生帐户将变为'资产:Simplii:Chequing-9999:Chequing-9999'。你知道吗
我要做的是从输入文件派生源帐户,并将此信息作为自助帐户父类中的变量(csv.导入程序)对于我的类实例(我想)。我在我的类中做错了什么导致派生源帐户被转移到下一个实例?你知道吗
#!/usr/bin/env python3
from beancount.ingest import extract
from beancount.ingest.importers import csv
from beancount.ingest import cache
from beancount.ingest import regression
import re
from os import path
from smart_importer.predict_postings import PredictPostings
class SimpliiImporter(csv.Importer):
'''
Importer for the Simplii bank.
Note: This undecorated class can be regression-tested with
beancount.ingest.regression.compare_sample_files
'''
config = {csv.Col.DATE: 'Date',
csv.Col.PAYEE: 'Transaction Details',
csv.Col.AMOUNT_DEBIT: 'Funds Out',
csv.Col.AMOUNT_CREDIT: 'Funds In'}
account_map = {'9999':'Chequing-9999'}
def __init__(self, *, account, account_map=account_map):
self.account_map = account_map
self.account = 'Assets:Simplii'
super().__init__(
self.config,
self.account,
'CAD',
['Filename: .*SIMPLII_\d{4}_.*\.csv',
'Contents:\n.*Date, Transaction Details, Funds Out, Funds In'],
institution='Simplii'
)
def name(self):
cls = self.__class__
return '{}.{}'.format(cls.__module__, cls.__name__)
def file_account(self, file):
__account = None
if file:
m = re.match(r'.+SIMPLII_(\d{4})_.*', file.name)[1]
if m:
sub_account = self.account_map.get(m)
if sub_account:
__account = self.account + ':' + sub_account
return __account
def extract(self, file):
self.account = self.file_account(file)
return super().extract(file)
@PredictPostings(training_data='/beancount/personal.beancount')
class SmartSimpliiImporter(SimpliiImporter):
'''
A smart version of the Simplii importer.
'''
pass
所以我已经设法让这个工作,但我不认为这是正确的方式来做它。。。你知道吗
我把提取函数改成这样
基本上我设定了自助帐户对于需要的值,调用父类extract函数saving results to variable,重置自助帐户变量和返回结果。似乎更多的工作,而不是正确的方式,但至少它在这里的情况下,它可以帮助别人了。。。你知道吗
相关问题 更多 >
编程相关推荐