# This is my second attempt writting a class
class Getlist(object):
    def __init__(self,list1=[],list2=[]):
        self.list1 = list1
        self.list2 = list2
    def printIt(self):
        print self.list1
        print self.list2

# This will combine the two lists together
class combineList(Getlist):
    def __init__(self):
        for i in range(len(getlist.list1)):
            print getlist.list1[i],getlist.list2[i]

getlist = Getlist([1,2,3],['a','b','c'])
getlist.printIt()
combineList = combineList()
# result should be
# 1a
# 2 b
# 3 c

# this is a sample I got from the Internet, and i modified it
import csv
portfolio = csv.reader(open("Untitled form.csv", "rb"))
portfolio_list = []
portfolio_list.extend(portfolio)
names = []
idigit = []
for data in portfolio_list:
    names.append(data[2])
    idigit.append(data[3])
del names[0]
del idigit[0]
k = len(names)
for i in range(len(names)):
    print names[i], idigit[i]

I am trying to write a custom class that uses this module CSV
I read this tutorial (the link below), and I tried to work through it
http://www.diveintopython.org/object_oriented_framework/defining_classes.html

class FileInfo(UserDict):
    "store file metadata"
    def __init__(self, filename=None):
        UserDict.__init__(self)  
        self["name"] = filename

What should I do if I want to incorpate an exisitng module?

Edited 6 Years Ago by jwxie: n/a

Typically when you create a module that you want to use in another script you save it as a file (we'll use my_module.py as an example). Then in your script in which you want to use said module you would do import my_module ; however this assumes that my_module is visible to the script either in the PYTHONPATH or system PATH, or the current working directory (ie, directory where the script is running from).

EDIT: Whether this works or not also depends on how you implement your module. In your first code example above you have lines of code in the global space of the script (ie, not indented into a function or class. That code would all run when performing an import. A common practice is to use a main function and protect it from running on import like this:

# Example python module

import sys
# Any other imports... imports should always be first

# Some classes, functions, whatever...
# This is your meat and potatos

# Now we'll define a main function
def main():
    # This is the code that runs when you are running this module alone
    print sys.platform

# This checks whether this file is being run as the main script
#  or if its being run from another script
if __name__ == '__main__':
    main()
# Another script running this script (ie, in an import) would use it's own
#  filename as the value of __name__

Hope that clears it up... or maybe I didn't understand your question?

Edited 6 Years Ago by jlm699: n/a

Hi, thank you for all the patience.

I tried with your codes and modified a bit and it worked. It printed win32 in the end. So I decided to hack it with the code I had written.

my.module.py

import csv

class Reader(object):
    def __init__(self,getFile,fileList=[]):
        self.getFile = csv.reader(open("Untitled form.csv", "rb"))
        self.getFileList = fileList
        self.getFileList.extend(getFile)
        
class ReadColumn(Reader):
    def __init__(self,names=[],idigit=[]):
        self.names = names
        self.idigit = idigit
        
    def getList(self):
        for data in Reader.getFileList:
            self.names.append(data[1])
            self.idigit.append(data[2])
            
        del self.names[0]
        del self.idigit[0]
        self.names.sort()
        
    def printList(self):
        for i in range(len(self.names)):
            print self.names[i], self.idigit[i]

def main():
    print ReadColumn.printList()
    
if __name__ == '__main__':
    main()

project.py

import my_module

x = my_module
x.main()

However, I tried to modify for an hour and did countless research on this. I could not resolve this error. IMO, I think I had a an uncertainty in the first place :I don't know if cvs reader is working or not (in this my_module)

Traceback (most recent call last):
File "D:/Python26/my_module.py", line 31, in <module>
main()
File "D:/Python26/my_module.py", line 28, in main
print ReadColumn.printList()
TypeError: unbound method printList() must be called with ReadColumn instance as first argument (got nothing instead)

### Run project.py
Traceback (most recent call last):
File "D:/Python26/finalproject.py", line 17, in <module>
x.main()
File "D:/Python26\my_module.py", line 28, in main
print ReadColumn.printList()
TypeError: unbound method printList() must be called with ReadColumn instance as first argument (got nothing instead)

Thanks for these replies, they had helped me a lot!

Edited 6 Years Ago by jwxie: n/a

Just a quick follow up, jim.

I could modify the my_module.py to this and certainly will work.
But also a follow up back to my very first thread (help with "object", remember?). Is the code below consider treating each student an object?

import csv

portfolio = csv.reader(open("Untitled form.csv", "rb"))
portfolio_list = []
portfolio_list.extend(portfolio)
names = []
idigit = []
for data in portfolio_list:
    names.append(data[2])
    idigit.append(data[3])
del names[0]
del idigit[0]
k = len(names)

def main():
    for i in range(len(names)):
            print names[i], idigit[i]

if __name__ == '__main__':
    main()
This article has been dead for over six months. Start a new discussion instead.