Saturday, 4 August 2018

python: how to get up until the last error made by my code

So when I run this... the error is on this line bomb=pd.DataFrame(here,0) but the trace shows me a bunch of code from the pandas library to get to the error.

import traceback,sys
import pandas as pd        

def error_handle(err_var,instance_name=None): #err_var list of variables, instance_name
    print(traceback.format_exc())
    a= sys._getframe(1).f_locals

    for i in err_var: # selected var for instance
        t= a[instance_name]
        print i,"--->",getattr(t,i.split(".")[1])



here=['foo']

err_var = ['self.needthisone','self.constant2']
class test:

    def __init__(self):
        self.constant1 = 'hi1'
        #self.constant2 = 'hi2'
        #self.needthisone = ':)'
        for i in err_var:
            setattr(self, i.split('.')[1], None)

    def other_function(self):
        self.other_var=5

    def testing(self):
        self.other_function()
        vars=[self.constant1,self.constant2]

        try:
            for i in vars: 
                bomb=pd.DataFrame(here,0)

        except:
            error_handle(err_var,'self')

t=test()
t.testing()    

How do I suppress all that and have the error just look like this:

Traceback (most recent call last):
  File "C:\Users\Jason\Google Drive\python\error_handling.py", line 34, in testing
    bomb=pd.DataFrame(here,0)
TypeError: Index(...) must be called with a collection of some kind, 0 was passed

I just want what's relevant to me and the last line of code that I wrote which was bad.

This is the original:

Traceback (most recent call last):
  File "C:\Users\Jason\Google Drive\python\error_handling.py", line 35, in testing
    bomb=pd.DataFrame(here,0)
  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 330, in __init__
    copy=copy)
  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 474, in _init_ndarray
    index, columns = _get_axes(*values.shape)
  File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 436, in _get_axes
    index = _ensure_index(index)
  File "C:\Python27\lib\site-packages\pandas\core\indexes\base.py", line 3978, in _ensure_index
    return Index(index_like)
  File "C:\Python27\lib\site-packages\pandas\core\indexes\base.py", line 326, in __new__
    cls._scalar_data_error(data)
  File "C:\Python27\lib\site-packages\pandas\core\indexes\base.py", line 678, in _scalar_data_error
    repr(data)))
TypeError: Index(...) must be called with a collection of some kind, 0 was passed

self.needthisone ---> None
self.constant2 ---> None



from python: how to get up until the last error made by my code

No comments:

Post a Comment