[Tutor] Store Class in Tuple Before Defining it ...

Damon Timm damontimm at gmail.com
Fri Aug 28 23:21:00 CEST 2009


Sorry for the double post!  Went off by mistake before I was done ...

Anyhow, I would like to have a tuple defined at the beginning of my
code that includes classes *before* they are defined ... as such (this
is on-the-fly-hack-code just for demonstrating my question):

VIDEO_TYPES = (
    (SyncYoutube,
re.compile(r'([^(]|^)http://www\.youtube\.com/watch\?\S*v=(?P<youtubeid>[A-Za-z0-9_-]+)\S*'),),
    (SyncVimeo, re.compile(#more regex here#),),
    (SyncBlip, re.compile(#more regex here#),),
)

class Video(object):
   url = "http://youtube.com/xxxx"
   #variables ...

   def sync(self):
       for videotype in VIDEO_TYPES:
           #check the url against the regex,
           # if it matches then initiate the appropriate class and
pass it the current "self" object
           sync = videotype[0](self).sync()

class SyncYoutube(object):
    def __init__(self,video):
       self.video = video

    def sync(self):
         #do some custom Youtube syncing here

class SyncBlip(object):
     #etc


This way, I can get any video object and simply run Video.sync() and
it will figure out which "sync" to run.  However, I am finding (of
course) that I can't reference a class that hasn't been defined!

I know this is a rush-job question, but I am hoping someone seems my
quandary and maybe has a way around it.  I am learning python as we
speak!

Thanks!  And sorry for the double post.

Damon


On Fri, Aug 28, 2009 at 5:10 PM, Damon Timm<damontimm at gmail.com> wrote:
> Hi -
>
> I would like to have a tuple that holds information, as such:
>
> VIDEO_TYPES = (
>    (SyncYoutube,
> re.compile(r'([^(]|^)http://www\.youtube\.com/watch\?\S*v=(?P<youtubeid>[A-Za-z0-9_-]+)\S*'),),
>
> )
>


More information about the Tutor mailing list