1

I have a set of code that looks like this:

class DataFilter:

    def __init__(self, csvData):
        # converts csv string data to float lists, if possible
        data = []
        for line in csvData:
            try:
                line = line.split(',')
            except:
                print(line)
                return

            for i in range( len(line) ):
                try:
                    line[i] = float(line[i])
                except ValueError:
                    pass
            data.append(line)
        self.data = data

    def find_depth_index(self, depth):
        for index, line in enumerate( self.data ):
            if line[1] > depth:
                return index

    def remove_above_depth(self, depth):
        index = self.find_depth_index( depth )
        return self.data[ index: ]

    def remove_beyond_depth(self, depth):
        index = self.find_depth_index(depth)
        return self.data[ :index ]

data = DataFilter(data).remove_above_depth(SURF_CASING_DEPTH)
print('-----------------------')
data = DataFilter(data).remove_beyond_depth(VERTICAL_SEC_DEPTH)

Then it give me an error:

  File "C:/Users/Eric Soobin Kim/PycharmProjects/untitled/RAPID_use_filtered_data.py", line 35, in remove_beyond_depth
    def remove_beyond_depth(self, depth):
  File "C:/Users/Eric Soobin Kim/PycharmProjects/untitled/RAPID_use_filtered_data.py", line 26, in find_depth_index
    def find_depth_index(self, depth):
AttributeError: 'DataFilter' object has no attribute 'data'

The thing that I don't understand is that, it ran without a problem for the line:

data = DataFilter(data).remove_above_depth(SURF_CASING_DEPTH)

but its not working for,

data = DataFilter(data).remove_beyond_depth(VERTICAL_SEC_DEPTH)

I think my first filtering somehow alters elements in __ini__(), but i don't know what's going on. Why is this happening, and how can i fix it?

3
  • 3
    Looks like you are hitting the return in the except block and self.data will never be set. Commented Mar 5, 2018 at 5:28
  • 1
    @KlausD. I edited my code. I see what you are saying. But in my code, there was no exception raised, so return was not the source of error here. Sorry for the confusion Commented Mar 5, 2018 at 5:47
  • @Eric Kim Can you post the whole code? What values does data have initially? Commented Mar 5, 2018 at 5:52

2 Answers 2

1

You've reassigned data to be equal to something other than what you want.

data = DataFilter(data).remove_above_depth(SURF_CASING_DEPTH)

This means that now you've lost the pointer to data that you once had. Might I suggest making a copy like

new_data = DataFilter(data).remove_above_depth(SURF_CASING_DEPTH)
new_data2 = DataFilter(data).remove_beyond_depth(VERTICAL_SEC_DEPTH)

This way you still have the reference to the old data variable

Sign up to request clarification or add additional context in comments.

7 Comments

This is the correct answer, but it's severely lacking in explanation. How does changing the value of a variable prevent an instance attribute from being created?
@Aran-Fey How's that?
Im not sure if this is what I want, because I am supposed to apply two filters to my data. So, first I filter the data points that doesn't fit my first filter, and then using the filtered data, I apply my second filter.
I don't see a difference. There's no explanation why the data attribute isn't being created. In fact I'm starting to wonder if you misunderstood the question.
@frank No, there was no exception raised in my code. I deleted return in my init function, and it ran fine without raising exception.
|
0

Object has no attribute data because you didnt give it the attribute. atributes are defined by making writing: self.object = [] instead of: object = []

1 Comment

"you didnt give it the attribute" Yes they did: self.data = data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.