0

I have the following Python class:

import sys
import re

class Parser:

    def __init__(self, filename=""):
        self.filename = filename

    def parse(self):

        try:
            table = {}
            fp = open(self.filename, 'r')
            records = fp.readlines()
            for record in records:
                (index, column, value) = record.strip().split()
                value = value[:3]
                table[column] = value

            return table

        except IOError:
            print "Could not open file ", self.filename
            sys.exit(1) 

    def organize_map(self, table={}):

        new_table = {
            'profile_1': [],
            'profile_2': [],
            'profile_3': [],
            'profile_4': []
        }

        for k, v in table.iteritems():

            if re.match("profile1", k):
                new_table['profile_1'].append(int(v))
            elif re.match("profile2", k):
                new_table['profile_2'].append(int(v))
            elif re.match("profile3", k):
                new_table['profile_3'].append(int(v))
            elif re.match("profile4", k):
                new_table['profile_4'].append(int(v)) 


        for k, v in new_table.iteritems():
            v.sort()
            v = v[2:len(v)-2]
            new_table[k] = v
            new_table[k].append(avg(v))
            new_table[k].append(std(v))

        return new_table

parser = Parser()
table = parser.parse()
print parser.organize_map(table)

when i execute the parser.py file, I get:

  File "parser.py", line 94, in <module>
    print parser.organize_map(table)
AttributeError: Parser instance has no attribute 'organize_map'

I don't know why ... I defined organized_map() with the self keyword ... any idea? A sample file:

1: profile1_test_1 155700802.32
2: profile1_test_2 156129130.88
3: profile1_test_3 155961744.64
4: profile1_test_4 155917583.6
5: profile1_test_5 156193748.16
6: profile1_test_6 155749778.88
7: profile1_test_7 156040104.72
8: profile1_test_8 156934277.68
9: profile1_test_9 156976866.56
6
  • Which version of python are you using? Commented Apr 8, 2013 at 21:23
  • It should rather say Could not open file and quit, which it does for me. Commented Apr 8, 2013 at 21:26
  • @LevLevitsky I give the filename though ... I changed it because the file is hardcoded into the class for now ... Commented Apr 8, 2013 at 21:26
  • @LevLevitsky I've added a sample file Commented Apr 8, 2013 at 21:28
  • 1
    It now says NameError: global name 'avg' is not defined which is inside organized_map, so it is called. Are you sure it's not a tab/space problem as suggested below? Commented Apr 8, 2013 at 21:33

1 Answer 1

5

You mix indentation with tabs and spaces in your source code, probably the python interpreter does interpret the tabs differently than you expect. The definition of organize_map is indented with tabs, most likely it ends up to be seen as a local function inside of parse.

Don't mix indentation with tabs and spaces, it only leads to confusion. You can also use Python's -t parameter when running the script to get warnings about inconsistent indentations:

python -t myscript.py
Sign up to request clarification or add additional context in comments.

3 Comments

same error ... using the -t param didn't change anything. The problem is different text editors
I copy/pasted the code from the question into a new window in IDLE and it worked.
I think you've got prob indentation issues

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.