[Tutor] Store Class in Tuple Before Defining it ...
Damon Timm
damontimm at gmail.com
Sat Aug 29 15:14:18 CEST 2009
Hi Everyone - thanks for your responses. Answered my direct questions:
[1] can't be done at the top and
[2] would have to move the tuple somewhere else
as well as gave me some new ideas about completely rethinking the
design ... I love keeping the RE definitions with the child classes
... makes it easy just to add a child anywhere without having to fool
with the original code.
One of my issues has been fixed using the @staticmethod decorator
(which I did not know about). Here is what I have now, and my final
question (perhaps) follows.
####
#this is a django app, by the way
class Video(models.Model):
url = models.URLField('Video URL')
# more fields and functions here
def sync(self):
'Update videos external data - for children only'
pass
@staticmethod
def add_video(url):
'add a video only if it matches correct regex -- need ExceptionHandler'
for sc in Video.__subclasses__():
if sc.RE.match(url):
return sc(url=url)
class YoutubeVideo(Video):
RE = re.compile(r'([^(]|^)http://www\.youtube\.com/watch\?\S*v=(?P<youtubeid>[A-Za-z0-9_-]+)\S*')
def sync(self):
# do custom syncing here
print "am syncing a YOUTUBE video"
class ViemoVideo(Video):
RE = re.compile(r'([^(]|^)http://(www.|)vimeo\.com/(?P<vimeoid>\d+)\S*')
def sync(self):
# do custom syncing here
print "am syncing a VIMEO video"
##############
So, this is *great* because now I can "add_video" without knowing the
video url type at all.
>>> v = Video.add_video(url="http://www.youtube.com/watch?v=UtEg3EQwN9A")
>>> v
<YoutubeVideo: >
>>>
Perfect! The last part is figuring out the syncing -- what I would
like to do would be either:
>>> Video.sync_all_videos() #create another static method
Or, if I had to, create another function that did a:
>>> for video in Video.objects.all(): # object.all() is a django function that returns everything
... video.sync()
However, I am not sure how to determine the "actual" class of a video
when I am dealing only with the parent. That is, how do I call a
child's sync() function when I am dealing the parent object? As
suggested (in email below) I could re-run the regex for each parent
video for each sync, but that seems like it could be an expensive
operation (supposing one day I had thousands of videos to deal with).
Is there another easy way to find the "real" class of an object when
dealing with the parent? I know so little, I have to think there
might be something (like the @staticmethod decorator!).
Thanks again!
Damon
On Sat, Aug 29, 2009 at 8:05 AM, Lie Ryan<lie.1296 at gmail.com> wrote:
> what I suggest you could do:
>
> class Video(object):
> # Video is a mixin class
> def __init__(self, url):
> self.url = url
> def sync(self):
> # source agnostic sync-ing or just undefined
> pass
> @staticmethod
> def find_video(url):
> for sc in Video.__subclasses__():
> if sc.RE.match(url)
> return sc(url)
> class Youtube(Video):
> RE = re.compile('... some regex here ...')
> def sync(self):
> # costum sync-ing
> pass
> class Blip(Video):
> RE = re.compile('... other regex here ...')
> def sync(self):
> # costum sync-ing
> pass
> a = Video.find_video('http://www.youtube.com/xxxx')
>
> that way, url detection will only happen on class initialization instead of
> every time sync is called.
More information about the Tutor
mailing list