from student import Student ## From the File import the thing inside it import csv class Class: """ This file is used to create a 'class' of 'students.' It will take a csv of names and 'used' values and will create a list of students. This list of students will be able to be quiered by student name for info, and the students will be editable. At the end of execution, it is possible to write the class to a CSV file for storage. """ def __init__(self, classFile="class.txt"): """ Dictionaries are Hashmaps and lists are just Linked Lists. classDict ={} provides a O(1) lookup for students calssList =[] provides a list example for working with data """ self._classDict = {} self._classList = [] studentInfo = self._importClass(classFile) for entry in studentInfo: if len(entry) < 2: print "Entry with less than 2 fields";continue ## eliminates empty entries elif len(entry) > 2: print "Error with text file: ",entry else: self.addStudent(entry[0],entry[1]) def _importClass(self, fileName): """ This handles reading from a csv. Returns the raw data. Will be in the format: list of list of data, meaning: [ [data,data,data], [data,data,data], ... ] Also known as 2 D aray. The lists contained will have the student information. """ importedData = [] with open(str(fileName),'rb') as F: r = csv.reader(F,delimiter = ',') for row in r: importedData.append(row) return importedData def _exportClass(self, fileName): with open (str(fileName),'wb') as F: W = csv.writer(F,delimiter=',') W.writerow(["Name", "Has Been Picked?"]) for student in self._classList: W.writerow([student.whoIs(),bool(eval(student.wasPicked()))]) def backupClass(self, fileName="class.txt"): self._exportClass(fileName) def addStudent(self, Name, wasPicked=False): newStudent = Student(Name, wasPicked) self._classDict[Name] = newStudent self._classList.append(newStudent) def lookupStudent(self, Name): if Name in self._classDict: return self._classDict[Name] else: return None
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question