Beem: account.history_reverse() may fail with TypeError

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

H2
H3
H4
3 columns
2 columns
1 column
4 Comments