git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

perplexing error


Sharan Basappa wrote:

> I am running a small python code. The main module calls another data
> loader module. I keep this getting this error.
> 
> AttributeError: 'str' object has no attribute 'raw_data'
> 
> I tried few things in the code and finally realized that no matter the
> line number is 68 of data loader module. Of course, this was after a few
> iterations. I then commented this line itself but I still get this error.
> 
> Below is a little longer code snippet.
> 
> AttributeErrorTraceback (most recent call last)
> D:\Projects\Initiatives\machine learning\Log analyzer\log_analyzer.py in
> <module>()
>      32         model = para['models']
>      33         assert model in ['DT', 'LR', 'SVM']
> ---> 34         raw_data, event_mapping_data =
> data_loader.bgl_data_loader(para)
>      35
>      36 logger.debug("raw data %s \n", raw_data)
> D:\Projects\Initiatives\machine
> learning\loglizer-master\loglizer-master\utils\data_loader.py in
> bgl_data_loader(para)
>      66         # get the label for each log
>      67         data_df['label'] = (data_df['label'] != '-').astype(int)
> ---> 68         #logger.debug("data frame %s \n", data_df)
>      69         logger.debug("\n")
>      70         raw_data = data_df[['label','seconds_since']].as_matrix()
> AttributeError: 'str' object has no attribute 'raw_data'
> 
> If you notice, line 68 is commented.
> I am not sure what I am doing wrong.
> 
> Need some inputs ...

The source code in the traceback is read from the file when the exception is 
raised. If you edit a module after importing it the traceback may pick lines 
that caused the code raising the exception, but now contain something 
completely different. A little demo:

>>> with open("tmp.py", "w") as f: f.write("def f():\n return 1/0\n")
... 
21
>>> import tmp
>>> with open("tmp.py", "w") as f: f.write("def f():\n return 1/2\n")
... 
21
>>> tmp.f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/peter/tmp.py", line 2, in f
    return 1/2
ZeroDivisionError: division by zero

Therefore you must ensure that the interpreter is restarted after changing 
the source code. This may include restarting a server or an editor.