0

I've watched videos and read text, but I'm still having a hard time grasping classes and their syntax and how they interact. I have the following code that takes in a CSV and puts it into a dict within a list. I want this list to be analyzed in other functions but I don't know how to call it and use it.

How can I use the list from 'data_to_dict' in 'Top_Artists'?

class allData:

    def data_to_dict(self):
        try:
            with open(self.filename, 'r') as data:
                header = data.readline().strip().split(',')

                entries = []
                for row in data:
                    entry = row.strip().split(',')
                    listens = {}
                    for col_pos, col_header in enumerate(header):
                        listens[col_header] = entry[col_pos]
                    **return entries**
                    entries.append(listens)
        except IOError:
            return "Error."


    def Top_Artists():
        **entries = self.data_to_dict()**

        artist_count = Counter()

        for d in entries:

            # counts unique artists
            arts = d['artist']
            artist_count[arts] += 1

Update: I figured out my problem - besides the 'self''s and a return in the data_to_dict, I added:

def __init__(self, filename):
    self.filename = filename
mo_shun
  • 305
  • 2
  • 3
  • 8
  • 3
    You can't, as it's neither `return`ed nor stored as an instance attribute. Also, none of your methods have the `self` parameter. Strongly consider following a formal tutorial. – jonrsharpe Dec 08 '14 at 20:55
  • @jonrsharpe is exactly right. You need to `return entries` when the `for` loop is done adding entries. – gcarvelli Dec 08 '14 at 20:57
  • You need explicit `self` references as the first argument to all class methods: http://stackoverflow.com/q/2709821/3651800 – Matt Coubrough Dec 08 '14 at 20:57
  • That was my fault, I had made the def's but put them quickly in a class for my question- so I forgot the self. Guess I need a lot more practice. – mo_shun Dec 08 '14 at 21:16

3 Answers3

0

I can see a couple of issues here. The most obvious solution is that the

entries = self.data_to_dict() 

doesn't have the necessary parameter (namely filename) set, Nor as rightly pointed out in the comments does it have the neccessary self parameter. Therefore you need to have the filename sent in similar to this:

entries = self.data_to_dict("path/to/file")

and the other code should be as follows:

def data_to_dict(self, filename):

as the simplest hard-coded solution

Daniel Casserly
  • 3,552
  • 2
  • 29
  • 60
  • 2
    data_to_dict() is missing the self argument so this alone won't actually solve the problem – Matt Coubrough Dec 08 '14 at 20:59
  • edited to reflect that. I haven't done serious python in a while and had completely forgotten. Thanks Matt – Daniel Casserly Dec 08 '14 at 21:03
  • Thank you! I was able to finally figure out my problem. I added a def __init__ to handle the input file, and added a 'return' to the data_to_dict. Then I only needed the 'entries = self.data_to_dict()' to make the other def work. – mo_shun Dec 09 '14 at 01:45
0

It's not clear to me what you are trying to do with this example. Class functions take an argument (usually called "self") that you can modify. For example:

class Foo:
    def make_list(self):
        self.my_list = [1,2,3]

x = Foo()
x.make_list()
print(x.my_list)

Check the python tutorial on classes for more: https://docs.python.org/2/tutorial/classes.html

Thomas Nelson
  • 693
  • 5
  • 17
0

When you methods within a class in Python must pass as argument the keyword self, this will return the class entirely on the same variable.

class Something:

    def method_1(self):
        # type something

    def method_2(self):
        other = self.method_1()

the method_2 use the method_1.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Joel Jaime
  • 459
  • 2
  • 9