Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: No value for argument 'filenames' in unbound method call

Tags:

python

I have a class to wrap around the config stuff of my program. But when I try to test i in ipython like this:

c = Config(test.cfg)

I get the error:

"TypeError: read() missing 1 required positional argument: 'filenames'
"

But I pylint throws this error in my emacs:

"No value for argument 'filenames' in unbound method call"

I think the error adbove is the reason of the error above it. But I don't know why. I think the method is call bound since I used self as reference but I'm not shure and I don't get why it doesn't accept cfg as paramter.

This is my class:

from configparser import ConfigParser

class Config:
    """Wrapper around config files
    Args:
        cfg: main config file
    """
    def __init__(self, cfg=None):
        self.cfg = []
        self.cfgraw = cfg
        self.scfgs = []
        self.scfgs_raw = []
        if not cfg is None:
            self.add(typ="main", cfg=cfg)


    def add(self, typ, cfg):
        """add config file
        Args:
             typ: type of file main or sub
        Returns:
             none
        """
        if typ is not "main" and cfg is None:
            raise ValueError('no file provied and type not main')

        if typ is "sub":
            for index in range(len(self.scfgs)):
                self.scfgs[index] = ConfigParser
                self.scfgs[index].read(cfg)

        if typ is "main":
            self.cfg = ConfigParser
            self.cfg.read(cfg)
    def write(self, typ=None, index=None):
        """write changes made
        Args:
            typ: type of target file
            index: if type is sub add index to write selected file
        Returns:
            none
        """
        if typ is "main":
            self.cfg.write(self.cfgraw)
        if typ is "sub":
            if index is None:
                for item in range(len(self.scfgs)):
                    self.scfgs[item].write(self.scfgs_raw[item])
            else:
                self.scfgs[item].write(self.scfgs_raw[item])   
like image 713
Thaodan Avatar asked Jun 10 '16 13:06

Thaodan


2 Answers

You are not initializing your ConfigParser correctly on the line before the read -- you are missing the () to actually create an instance.

Try:

if typ is "main":
    self.cfg = ConfigParser()
    self.cfg.read(cfg)
like image 57
Tom Sitter Avatar answered Nov 19 '22 00:11

Tom Sitter


You don't instantiate ConfigParser when you assign it to the self.scfgs or self.cfg attributes; so when you then call read on it, you're calling an unbound method via the class, rather than a method on the instance.

It should be:

self.cfg = ConfigParser()

etc

like image 38
Daniel Roseman Avatar answered Nov 19 '22 00:11

Daniel Roseman