0

I'm trying to modify a class which has methods based on the csv module.

The data is a simple three-column csv file which I wanted to turn into a nested list of dictionaries.

I've managed to that with the help of this SO question.

However, I'd like to be able to implement a more general 'load_data' method, one which I could use decorators to introduce methods to output to take two columns of a loaded csv and output as a list of dictionaries.

My attempt to produce the wrapper function and proposed csv_to_dict_dicts method are commented out.

import csv

class AutoVivification(dict):
    """Implementation of perl's autovivification feature."""
    def __getitem__(self, item):
        try:
            return dict.__getitem__(self, item)
        except KeyError:
            value = self[item] = type(self)()
            return value
"""
def csv_decorator(func):
    def func_wrapper(self):
        for row in reader:
        return dict(self.data[row[0]][row[1]] = row[2] )
    return func_wrapper
"""


class CSV:
    def __init__(self):
        self.data = AutoVivification()

    def load_data(self, path):
        with open(path, encoding="utf-8") as f:
            self.path = path
            reader = csv.reader(f)
            for row in reader:
                self.data[row[0]][row[1]] = row[2]

    #@csv_decorator
    #def csv_to_dict_dicts(self):
    #    return self.load_data(path)


c = CSV()
c.load_data(path)

UPDATE:

I've tried using SO question thus:

def csv_decorator(func):
    def func_wrapper(self):
        reader = func(self)
        for row in reader:
            self.data[row[0]][row[1]] = row[2]
    return func_wrapper


class CSV:
    def __init__(self):
        self.data = AutoVivification()

@csv_decorator
def load_data(self, path):
    with open(path, encoding="utf-8") as f:
        self.path = path
        reader = csv.reader(f)
        return reader

However, I receive the error:

TypeError: func_wrapper() takes 1 positional argument but 2 were given
elksie5000
  • 7,084
  • 12
  • 57
  • 87
  • 1
    Possible duplicate of https://stackoverflow.com/questions/1263451/python-decorators-in-classes – anuragal Nov 23 '17 at 12:19
  • @Anuragal, thank you for your response. I'd not been able to find that question, and I appreciate you flagging it. I'm trying to find a solution within it that I can generalise. I was't trying to suggest duplicate questions. – elksie5000 Nov 23 '17 at 12:36
  • 1
    You need to allow `func_wrapper()` to accept the path argument as well and also pass it to `func()` when it is called, so `def func_wrapper(self, path)` then `func(self, path)` – eugenhu Nov 23 '17 at 12:42
  • 1
    @eugenhu, Ah! I think I've cracked it. Thank you for your help. – elksie5000 Nov 23 '17 at 12:48

1 Answers1

0

With the guidance of the contributors above, I've got some code that works.

def csv_decorator(func):
    def func_wrapper(self, path):
        reader = func(self, path)
        for row in reader:
            self.data[row[0]][row[1]] = row[2]
    return func_wrapper


class CSV:
    def __init__(self):
        self.data = AutoVivification()

    @csv_decorator
    def load_data(self, path):
        f = open(path, encoding="utf-8")
        self.path = path
        reader = csv.reader(f)
        return reader
elksie5000
  • 7,084
  • 12
  • 57
  • 87