Beem: account.history_reverse() may fail with TypeError

in #utopian-io6 years ago (edited)

Project Information

Expected behavior

account.history_reverse(start=start, stop=stop, only_ops=['comment']) with start and stop being datetime objects should return the all comment operations of an account in the given time range.

Actual behavior

account.history_reverse(start=start, stop=stop, only_ops=['comment']) may fail with a TypeError exception:

Traceback (most recent call last):
  File "account_history_reverse.py", line 10, in <module>
    for op in a.history_reverse(start=start, stop=stop, only_ops=['comment']):
  File "/usr/local/lib/python3.6/site-packages/beem/account.py", line 1335, in history_reverse
    while(op_est + est_diff + batch_size < first and block_date < start):
TypeError: '<' not supported between instances of 'str' and 'datetime.datetime'

block_date is a string in this case and is compared to start as a datetime object.

How to reproduce

#!/usr/bin/python
from beem.account import Account
from datetime import datetime, timedelta
from beem.utils import addTzInfo

a = Account("steemcleaners")
start = addTzInfo(datetime.utcnow()) - timedelta(days=1)
stop = addTzInfo(datetime.utcnow()) - timedelta(days=7)
for op in a.history_reverse(start=start, stop=stop, only_ops=['comment']):
    print(op)

screenshot.png

Possible fix

Suspicion: block_date is converted to datetime objects in other places with formatTimeString(). This is not the case for the failing location, h["timestamp"] is used directly as a string.

Possible Fix:

diff --git a/beem/account.py b/beem/account.py
index b41cb58..b4ca42f 100644
--- a/beem/account.py
+++ b/beem/account.py
@@ -1331,13 +1331,13 @@ class Account(BlockchainObject):
             est_diff = 0
             if isinstance(start, (datetime, date, time)):
                 for h in self.get_account_history(op_est, 0):
-                    block_date = h["timestamp"]
+                    block_date = formatTimeString(h["timestamp"])
                 while(op_est + est_diff + batch_size < first and block_date < start):
                     est_diff += batch_size
                     if op_est + est_diff > first:
                         est_diff = first - op_est
                     for h in self.get_account_history(op_est + est_diff, 0):
-                        block_date = h["timestamp"]
+                        block_date = formatTimeString(h["timestamp"])
             else:
                 for h in self.get_account_history(op_est, 0):
                     block_num = h["block"]

Environment

# beempy --version
beempy, version 0.19.32
# python --version
Python 3.6.5

GitHub Account

https://github.com/crokkon
A Github issue has been created: https://github.com/holgern/beem/issues/13

Sort:  

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thanks for the PR and the issue report.
I already merged the PR here

That was fast, thanks!

Hey @stmdev
Thanks for contributing on Utopian.
Congratulations! Your contribution was Staff Picked to receive a maximum vote for the bug-hunting category on Utopian for being of significant value to the project and the open source community.

We’re already looking forward to your next contribution!

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 62964.22
ETH 2595.61
USDT 1.00
SBD 2.74