Question

I am having trouble with dynamically creating a class instance member. I am trying to create a class object which behaves like a directory and has instance members which are named after the files contained in the class.

However, I can't reach the __setitem__() method for the class. I might be approaching this wrong. I am not sure.

Here is my code:

import IPython
import sys, os
ipath =  IPython.utils.path.get_ipython_dir()
tutorial_config = "%s/profile_peter/startup"%ipath
sys.path.append(tutorial_config)
from tutorial_helpers import directoryClass, fileClass
## Determine Operating System
if sys.platform == "win32":
        OperatingSystem = "Windows"
elif sys.platform =="linux2":
        OperatingSystem = "Linux"
else:
    raise Exception("could not find OS")
print OperatingSystem
root_path = os.getcwd()


class OS_appropriete_file():
    separator = None
    def __init__(self, rootPath, relative_path):
        self.create_separator(relative_path)
        self = fileClass(rootPath+self.separator+relative_path)
    def create_separator(self,relative_path):
        if OperatingSystem == "Windows":
            escape_sequences = ['a','b','f','t','v','n','r','x']
            self.separator = "\\"
        elif OperatingSystem == "Linux":
            self.separator = "/"



class dirClass(object):
    def __init__(   self,root,newFileName):
        separator = self.create_separator(newFileName)
        self.root = root+separator+newFileName
    def create_file(self,fileName):
        FileName_withoutExtension = fileName.split(".")[0]
        print "extless:"
        print FileName_withoutExtension
        super(dirClass,self).__setitem__(FileName_withoutExtension,             OS_appropriete_file(self.root,fileName))
        #self.__class__.__dict__[FileName_withoutExtension] = OS_appropriete_file(self.root,fileName)
def create_separator(self,relative_path):
    if OperatingSystem == "Windows":
        escape_sequences = ['a','b','f','t','v','n','r','x']
        separator = "\\"
    elif OperatingSystem == "Linux":
        separator = "/"

    return separator



class files():
    mypackage=dirClass(root_path,"mypackage")
    mypackage.create_file("__init__.py")
    mypackage.create_file("main.py")
    ## subpackage
    subpackage1=dirClass(root_path,"subpackage1")
    subpackage1.create_file("sub1.py")
    subpackage1.create_file("__init__.py")
    subpackage2=dirClass(root_path,"subpackage2")
    subpackage2.create_file("sub2.py")
    subpackage2.create_file("__init__.py")


if __name__ == "__main__":
    fileEx = files()

When I import this file using from referenced_paths_and_files import * I get the error:

AttributeError: 'super' object has no attribute '__setitem__'

What I am trying to do is create a dirClass instance that has a member variable that is named after the argument "newFileName" which is an instance of OS_appropriete_file.

Thanks so much for any help!

Was it helpful?

Solution

object does not have a __setitem__ method; it is not a type that supports indexing. Only mutable sequence and mapping objects implement __setitem__. The method is normally used to hook into index assignment (obj[key] = value).

I suspect you wanted to set an attribute instead; if so, just use setattr() on self:

setattr(self, FileName_withoutExtension, OS_appropriete_file(self.root,fileName))

OTHER TIPS

It seems that you must implement the method __setitem__ in dirClass before you can use it in create_file. The __setitem__ method is an abstract method/interface that each class that uses it must implement it.

This reference may be relevant.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top