Reputation: 1871
So I'm writing a python script that modifies TV show titles. this is more for practice than anything. I'm trying to use the @property decorator, but I was under the misconception that when you do "var.property" I thought that the var would get passed to the property... so I'm trying to get splitName() to be able to use the hasDots property from the base class, but I'm not sure how you would pass the name Var into the property. I know I could do this with a method, but I'm trying to learn how to use properties. the splitName() method is going to be the main method once I can get the base class to work properly.
any help on this would be greatly appreciated. also I'm pretty new to python, so if I'm doing anything that is "unpythonic" let me know.
exceptibleExts = ['.avi','.mkv','mp4']
class Base(object):
def __init__(self, source):
self.source = source
self.isTvShow()
# this method will return the file name
def fileName(self):
for name in self.source:
name, ext = os.path.splitext(name)
yield name, ext
@property
def isTvShow(self):
names = self.fileName()
for name in names:
if str(name[1]) in exceptibleExts and str(name[1]).startswith('.') == False:
return True
else:
return False
@property
def hasDots(self):
names = self.fileName()
for name in names:
if '.' in str(name[0]):
return True
else:
return False
@property
def hasDashes(self):
names = self.fileName()
for name in names:
if '-' in str(name[0]):
return True
else:
return False
@property
def startswithNumber(self):
names = self.fileName()
for name in names:
if str(name[0].lstrip()[0].isdigit()):
return True
else:
return False
@property
def hasUnderscore(self):
names = self.fileName()
for name in names:
if '_' in str(name[0]):
return True
else:
return False
class names(Base):
def __init__(self, source):
self.source = source
#pass
self.splitName()
#this method returns true if the show title is in the file name... if not it will return false
def hasShowTitle(self):
pass
def splitName(self):
#names = self.fileNames
showInfo = {}
for name in self.fileName():
print name.hasDots
Upvotes: 0
Views: 117
Reputation: 3890
It is good idea to read documentation carefully when you are learning something.
Look at the third code example here http://docs.python.org/3/library/functions.html#property
class C:
def __init__(self):
self._x = None
@property
def x(self):
"""I'm the 'x' property."""
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
this is how you define setters and deleters for the properties defined using built-in property
decorator.
P.S.: Corresponding Python 2 documentation is here: http://docs.python.org/2/library/functions.html#property
Upvotes: 2