1

I´m working on a very simple code to write a transaction to GNU Cash book using piecash. I´m having problems with the date format. Here is the code:

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()

The problem is with the p_date variable. If a use:

p_date = datetime.datetime(2020, 12, 26)

I receive the following error from 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}]]

But, using the format p_date = datetime.date(2020, 12, 26), the error changes to:

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

I know the problem is related to if the variable is a type of datetime or if it is an instance of it, but I´m unable to solve it. Can someone give me a hand? Thanks

Rods
  • 51
  • 3
  • 1
    hello, could you give your version of piecash & gnucash you are using ? I have no issue when using a date as p_date. – sdementen Dec 27 '20 at 07:31
  • Hi! gnucash 4.2+(2020-09-26), piecash 1.1.2, python 3.8. I´m running the code inside pycharm. Thanks! – Rods Dec 27 '20 at 14:16
  • 1
    Post_date should be a date (not datetime). However, on both stacktraces, I see a datetime for post_date which is surprising. On my laptop with the lastest piecash and a date I have no errors. – sdementen Dec 28 '20 at 15:24

1 Answers1

0

I don´t know what was wrong, but after update python to 3.9 it worked.Thanks

Rods
  • 51
  • 3