0

I have an issue printing things efficiently. I return each rows from a function. What is the most efficient way to print all the row column values for debugging?

(Also it does not print properly the last item (when there is a loop) where it is a list of object name. Can somebody teach the way to do it correctly? )

def get_address(address, filter=None, limit=None, offset=None, api_code=None):
    resource = 'address/{0}?format=json'.format(address)
    if filter is not None:
        if isinstance(filter, FilterType):
            resource += '&filter=' + str(filter.value)
        else:
            raise ValueError('Filter must be of FilterType enum')
    if limit is not None:
        resource += '&limit=' + str(limit)
    if offset is not None:
        resource += '&offset=' + str(offset)
    if api_code is not None:
        resource += '&api_code=' + api_code
    response = util.call_api(resource)
    json_response = json.loads(response)
    return Address(json_response)

class Address:
    def __init__(self, a):
        self.hash160 = a['hash160']
        self.address = a['address']
        self.n_tx = a['n_tx']
        self.total_received = a['total_received']
        self.total_sent = a['total_sent']
        self.final_balance = a['final_balance']
        self.transactions = [Transaction(tx) for tx in a['txs']]
    
def test_getAddressByBase58(address):
    address = get_address(address)
    a = address.hash160
    b = address.final_balance
    c = address.total_received
    d = address.total_sent
    e = address.n_tx
    f = address.transactions
    return a,b,c,d,e,f

print(test_getAddressByBase58('********************************'))

output:

('4428937923472494379ad36a', 0, 532398745780, 5192730, 13, [<explorer.Transaction object at 0x10d08bfd0>, <explorer.Transaction object at 0x10e56c048>, <explorer.Transaction object at 0x10e56c160>, <explorer.Transaction object at 0x10e577860>, <explorer.Transaction object at 0x10e577f98>, <explorer.Transaction object at 0x10e57a0f0>, <explorer.Transaction object at 0x10e5877f0>, <explorer.Transaction object at 0x10e587ac8>, <explorer.Transaction object at 0x10e587be0>, <explorer.Transaction object at 0x10e596320>, <explorer.Transaction object at 0x10e596cf8>, <explorer.Transaction object at 0x10e596e10>, <explorer.Transaction object at 0x10e599390>, <explorer.Transaction object at 0x10e5a6a90>, <explorer.Transaction object at 0x10e5a6ba8>, <explorer.Transaction object at 0x10e5b42e8>, <explorer.Transaction object at 0x10e5b4908>, <explorer.Transaction object at 0x10e5b4a20>, <explorer.Transaction object at 0x10e5c3160>, <explorer.Transaction object at 0x10e5c3240>, <explorer.Transaction object at 0x10e5c3320>, <explorer.Transaction object at 0x10e5c3400>, <explorer.Transaction object at 0x10e5c34e0>, <explorer.Transaction object at 0x10e5cfbe0>, <explorer.Transaction object at 0x10e5cfcf8>, <explorer.Transaction object at 0x10e5de438>, <explorer.Transaction object at 0x10e5de630>, <explorer.Transaction object at 0x10e5de710>, <explorer.Transaction object at 0x10e5eae10>, <explorer.Transaction object at 0x10e5eaef0>, <explorer.Transaction object at 0x10e5ee3c8>, <explorer.Transaction object at 0x10e5ee4e0>, <explorer.Transaction object at 0x10e5f9be0>, <explorer.Transaction object at 0x10e5f9cf8>, <explorer.Transaction object at 0x10e5f9dd8>, <explorer.Transaction object at 0x10e5fdcf8>, <explorer.Transaction object at 0x10e5fde10>, <explorer.Transaction object at 0x10e60c550>, <explorer.Transaction object at 0x10e617c50>, <explorer.Transaction object at 0x10e617d30>, <explorer.Transaction object at 0x10e617e80>, <explorer.Transaction object at 0x10e617f60>, <explorer.Transaction object at 0x10e61b0f0>, <explorer.Transaction object at 0x10e61b1d0>, <explorer.Transaction object at 0x10e61b2e8>, <explorer.Transaction object at 0x10e6279e8>, <explorer.Transaction object at 0x10e62d0b8>, <explorer.Transaction object at 0x10e62d198>, <explorer.Transaction object at 0x10e62d2b0>, <explorer.Transaction object at 0x10e62d3c8>])

EDIT:

I tried the following code in the class.

class Address:
    def __init__(self, a):
        self.hash160 = a['hash160']
        self.address = a['address']
        self.n_tx = a['n_tx']
        self.total_received = a['total_received']
        self.total_sent = a['total_sent']
        self.final_balance = a['final_balance']
        self.transactions = [Transaction(tx) for tx in a['txs']]
    def __repr__(self):
        return (
        self.hash160,
        self.address,
        self.n_tx,
        self.total_received,
        self.total_sent,
        self.final_balance,
        self.transactions)

BUt it returned the error:

Traceback (most recent call last):
  File "test_explo.py", line 11, in <module>
    print(get_address('*************************'))
TypeError: __str__ returned non-string (type tuple)

Why is this mentioning str? I am not using it.

Kind Contributor
  • 17,547
  • 6
  • 53
  • 70
delalma
  • 838
  • 3
  • 12
  • 24
  • 1
    implement [__repr__()](https://docs.python.org/3/reference/datamodel.html#object.__repr__) method in the object class you are trying to print – deadshot Jun 30 '20 at 09:43
  • Does [this](https://stackoverflow.com/questions/1535327/how-to-print-instances-of-a-class-using-print) help? – AnsFourtyTwo Jun 30 '20 at 09:46
  • 3
    Does this answer your question? [How to print instances of a class using print()?](https://stackoverflow.com/questions/1535327/how-to-print-instances-of-a-class-using-print) – AnsFourtyTwo Jun 30 '20 at 09:46
  • thanks. Not sure to fully understand. What about the __init__? should I replace it by __str__? – delalma Jun 30 '20 at 09:59
  • @komatiraju032 I have edited the answer. Can you look please? – delalma Jul 01 '20 at 00:59
  • 1
    You have to return a string in __repr__() method. Convert the values to string and return it will work – deadshot Jul 01 '20 at 02:01
  • @komatiraju032 I have changed all value in the __init__ to str ( ex: `str(a['hash160'])`) but it throw the same error again – delalma Jul 01 '20 at 04:37
  • 1
    you have to return a string from `__repr__()` method may be you are returning tuple of strings. – deadshot Jul 01 '20 at 05:12
  • The duplicate question reference is incorrect. The poster would like to know how to debug the row data, to see the values of each item, so that they may be able to find the column that has the error "__str__ returned non-string (type tuple)". I had a quick look, and I couldn't find any other similar answer for the python language. – Kind Contributor Jul 06 '20 at 12:01

0 Answers0