我正在编写一个非常简单的代码来使用 piecash 将交易写入 GNU Cash book。我的日期格式有问题。这是代码:
from collections import defaultdict
import piecash
import datetime
gnubook = 'E:\\piecash_test\\teste.gnucash'
book = piecash.open_book(gnubook, readonly=False, do_backup=False)
p_date = datetime.datetime(2020, 12, 26)
#p_date = datetime.date(2020, 12, 26)
broker = book.accounts(name="Broker account")
dest = book.accounts(name="AAPL")
inj_tr = piecash.Transaction( currency=book.default_currency,
description='Transaction Description',
post_date=p_date,
splits=[
piecash.Split( account=dest,
value=1000,
quantity=100,
memo='Transaction destination memo'),
piecash.Split( account=broker,
value=-1000,
memo='Transaction broker memo'),
])
book.save()
问题在于 p_date 变量。如果使用:
p_date = datetime.datetime(2020, 12, 26)
我从 piecash 收到以下错误:
Traceback (most recent call last):
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1204, in _execute_context
context = constructor(dialect, self, conn, *args)
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\engine\default.py", line 855, in _init_compiled
param.append(processors[key](compiled_params[key]))
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\sql\type_api.py", line 1227, in process
return impl_processor(process_param(value, dialect))
File "E:\piecash_test\piecash\venv\lib\site-packages\piecash\sa_extra.py", line 135, in process_bind_param
assert isinstance(value, datetime.date) and not isinstance(value, datetime.datetime), \
AssertionError: value 2020-12-26 00:00:00 is not of type datetime.date but type <class 'datetime.datetime'>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\orm\session.py", line 2540, in flush
self._flush(objects)
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\orm\session.py", line 2682, in _flush
transaction.rollback(_capture_exception=True)
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\util\langhelpers.py", line 68, in __exit__
compat.raise_(
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\util\compat.py", line 182, in raise_
raise exception
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\orm\session.py", line 2642, in _flush
flush_context.execute()
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 422, in execute
rec.execute(self)
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 586, in execute
persistence.save_obj(
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\orm\persistence.py", line 239, in save_obj
_emit_insert_statements(
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\orm\persistence.py", line 1135, in _emit_insert_statements
result = cached_connections[connection].execute(
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1011, in execute
return meth(self, multiparams, params)
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\sql\elements.py", line 298, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1124, in _execute_clauseelement
ret = self._execute_context(
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1206, in _execute_context
self._handle_dbapi_exception(
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1510, in _handle_dbapi_exception
util.raise_(
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\util\compat.py", line 182, in raise_
raise exception
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1204, in _execute_context
context = constructor(dialect, self, conn, *args)
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\engine\default.py", line 855, in _init_compiled
param.append(processors[key](compiled_params[key]))
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\sql\type_api.py", line 1227, in process
return impl_processor(process_param(value, dialect))
File "E:\piecash_test\piecash\venv\lib\site-packages\piecash\sa_extra.py", line 135, in process_bind_param
assert isinstance(value, datetime.date) and not isinstance(value, datetime.datetime), \
sqlalchemy.exc.StatementError: (builtins.AssertionError) value 2020-12-26 00:00:00 is not of type datetime.date but type <class 'datetime.datetime'>
[SQL: INSERT INTO transactions (guid, currency_guid, num, post_date, enter_date, description) VALUES (?, ?, ?, ?, ?, ?)]
[parameters: [{'post_date': datetime.datetime(2020, 12, 26, 0, 0), 'enter_date': datetime.datetime(2020, 12, 26, 12, 37, 10), 'description': 'Transaction Description', 'num': '', 'currency_guid': None}]]
但是,使用 format p_date = datetime.date(2020, 12, 26)
,错误变为:
Traceback (most recent call last):
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\util\compat.py", line 182, in raise_
raise exception
File "E:\piecash_test\piecash\venv\lib\site-packages\sqlalchemy\orm\state.py", line 430, in _initialize_instance
return manager.original_init(*mixed[1:], **kwargs)
File "E:\piecash_test\piecash\venv\lib\site-packages\piecash\core\transaction.py", line 244, in __init__
raise GncValidationError("post_date should be a date object")
piecash._common.GncValidationError: post_date should be a date object
我知道问题与变量是否属于type
或datetime
是否属于其中instance
有关,但我无法解决它。有人可以帮帮我吗?谢谢