class FileInfo(dict): #we inherit directly from dict "store file metadata" def __init__(self, filename=None): self["name"] = filename #we don't need to initialize the dict because it's not a wrapper class #5. Special Class methods: #5.1. The __getitem__ Special Method: def __getitem__(self, key): return self.data[key] >>> f = fileinfo.FileInfo("/music/_singles/kairo.mp3") >>> f {'name':'/music/_singles/kairo.mp3'} >>> f.__getitem__("name") #we can use this syntax '/music/_singles/kairo.mp3' >>> f["name"] #or this one '/music/_singles/kairo.mp3' #5.2. The __setitem__ Special Method def __setitem__(self, key, item): self.data[key] = item >>> f {'name':'/music/_singles/kairo.mp3'} >>> f.__setitem__("genre", 31) #we can use this one >>> f {'name':'/music/_singles/kairo.mp3', 'genre':31} >>> f["genre"] = 32 #or this one >>> f {'name':'/music/_singles/kairo.mp3', 'genre':32}
def walkGraffleDoc(self, parent, page=0): # want to pass this around like a continuation cont = nodeListGen(parent.childNodes) i = 0 mydict = None for e in cont: if e.nodeType == e.DOCUMENT_TYPE_NODE: pass localname = e.localName if localname == "plist": # Apple's main container self.walkGraffleDoc(e, page) if localname == "dict": mydict = self.ReturnGraffleDict(e) if mydict is not None: # Extract file information self.fileinfo = fileinfo.FileInfo(mydict) # Graffle lists it's image references separately self.imagelist = mydict.get("ImageList", []) # Sometimes have multiple sheets if mydict.get("Sheets") is not None: self.extractPage(mydict["Sheets"][page]) else: self.extractPage(mydict)
def getFileInfo(filename): fileObj = fileinfo.FileInfo(filename) fileObj.updatePathInfo() fileObj.getAttrs() fileObj.calcHashes() return (fileObj)
def leakmem(): f = fileinfo.FileInfo('/music/1. B.o.B - Dont Let Me Fall.mp3') #print f print f['name'] f.__setitem__('genre', 31) print f f['genre'] = 32 print f
class FileInfo(UserDict): #FileInfo inherits from UserDict "store file metadata" def __init__(self, filename=None): #filename default value is none, but we can put whatever we want in it UserDict.__init__(self) #we must also intialize the parent classe self["name"] = filename #when we create an instance and give it a value, it goes to 'name' #2. Instantiating Classes: >>> import fileinfo #importing the module >>> f = fileinfo.FileInfo("/music/_singles/kairo.mp3") #creating an instance >>> f.__class__ <class fileinfo.FileInfo at 010EC204> #the classe is indeed FileInfo >>> f {'name': '/music/_singles/kairo.mp3'} #the value given when instanciating the class is given to 'name' #3. Exploring UserDict: A Wrapper Class #3.1. Defining the UserDict Class class UserDict:#Doesn't inherit from any class (base class) def __init__(self, dict=None): self.data = {} if dict is not None: self.update(dict) #copies items from dict and puts them in self.data #3.2. UserDict Normal Methods def clear(self): self.data.clear() def copy(self): if self.__class__ is UserDict: return UserDict(self.data) import copy return copy.copy(self) def keys(self): return self.data.keys() def items(self): return self.data.items() def values(self): return self.data.values()
import fileinfo f = fileinfo.FileInfo("abcd.txt") f.firstline() f.lastline() f.nthline(4) f.getsize() f.close()
import fileinfo f = fileinfo.FileInfo("input.txt") f.getlinecount() f.getsize() f.getage() f.getfirstline() f.getnthline(3) f.getlastline() f.search("line") f.close()
#! /usr/bin/python import sys sys.path.append('../../') import fileinfo f = fileinfo.FileInfo('example5_7.py') print f.__class__ print f.__doc__ print f
#! /usr/bin/python import sys sys.path.append('../../') import fileinfo def __gettime__(self, key): """""" return self.data[key] f = fileinfo.FileInfo('./example5_12.py') print f print f.__getitem__("name") print f["name"]
import fileinfo f = fileinfo.FileInfo("/home/grenouille/Music/1. B.o.B - Dont Let Me Fall.mp3") print f.__class__ print f.__doc__ print f
def leakmem(): """""" f = fileinfo.FileInfo('example5_7.py')
#!/usr/bin/python import os import sys import fileinfo f = fileinfo.FileInfo("C:\Users\Public\Music\Sample Music\Sleep\ Away.mp3") #print f #print f.__getitem__("name") f.__setitem__("Genre",31) #print f mp3File = fileinfo.MP3FileInfo() #print mp3File mp3File["name"] = "C:\Users\Public\Music\Sample Music\Sleep\ Away.mp3" #print mp3File.tagDataMap #modifying class attribute class counter: count = 0 def __init__(self): self.__class__.count +=1 a = counter() b = counter() #print b.count #print counter.count