Today i was reading someone written code and trying to understand it, but I have encountered so many times that programmers override builtin methods or set new properties , the code below is from a tutorial i came accross , I hope some one can help me out how code implementation like this is helpful, both the classes shown below are part of the same py module...
class Asset(dict):
"""
Asset (dict)
Container object representing an Asset somewhere on
the filesystem. Used before for defining a new Asset
for import, and for an Asset currently in the system.
"""
def __init__(self):
super(Asset, self).__init__()
for val in ('name', 'show', 'scene', 'image'):
self[val] = ''
def __repr__(self):
"""
__repr__()
Specifies the formatted representation of the class,
when printing.
"""
# Since Asset subclasses a dictionary, we can use self as a string formatter
return "\nName: %(name)s \n\t Show: %(show)s \n\tScene: %(scene)s \n\tImage: %(image)s" % self
# a getter property
@property
def name(self):
return self.get('name', '')
# a setter property - this make Asset.name read AND write capable.
@name.setter
def name(self, val):
self['name'] = val
@property
def show(self):
return self.get('show', '')
@show.setter
def show(self, val):
self['show'] = val
@property
def scene(self):
return self.get('scene', '')
@scene.setter
def scene(self, val):
self['scene'] = val
@property
def image(self):
return self.get('image', '')
@image.setter
def image(self, val):
self['image'] = val
the class above is instantiated from the method below like asset=Asset() and then dictionary keys such as scene,name, show, image are assigned values respectively from the method below.
My question is why do we need to build or why the programmer built it in such a way when he could have put the dictionary in the same method below? why does he need to set properties in the class above ?
how is it useful?
class AssetImporter(object):
def list(self, name='', allShows=False):
"""
list(string name='', bool allShows=False) -> list(Asset,...)
Returns a list of Asset objects currently found
under the current show.
If allShows == True, return matching assets in all shows.
Specifying a name returns only Assets whose asset.name contains
the given name. i.e.
name = 'Asset' can return:
MyAsset1, Asset, Asset50, OldAsset
"""
dirs = []
if allShows:
for item in os.listdir(self._rootDir):
full = os.path.join(self._rootDir, item)
if os.path.isdir(full):
dirs.append(full)
else:
dirs.append( self._getShowDir() )
assets = []
for d in dirs:
show = os.path.basename(d)
for item in os.listdir(d):
if item.startswith("."):
continue
full = os.path.join(d, item)
if os.path.isfile(full):
aName = os.path.splitext(item)[0]
# if a specific asset name was given, and
# this one isnt it.. move on
if name and not name in item:
continue
imgExt = os.path.splitext(self.DEFAULT_IMAGE)[1]
imgFile = '%s%s' % (aName, imgExt)
asset = Asset()
asset['scene'] = full
asset['name'] = aName
asset['show'] = show
asset['image'] = os.path.join(d, 'images', imgFile)
assets.append(asset)
return assets