labs. bfloch.com/script/Python Factory
private.  
/script /output
/links
/about



 »    
10/Aug/20
python

 
» Python Factory

Found this at http://ubuntuforums.org/showthread.php?t=749534

Code:

import sys
 
class FooBase(object):
    def __new__(cls, url, *arguments, **keyword):
        for subclass in FooBase.__subclasses__():
            if url.startswith(subclass.prefix):
                return super(cls, subclass).__new__(subclass, *arguments, **keyword)
        raise Exception, 'Prefix not supported'
 
class HttpFoo(FooBase):
    prefix = 'http'
 
class FtpFoo(FooBase):
    prefix = 'ftp'
 
if __name__ == '__main__':
    foo = FooBase('http://abc.com')
    print foo.prefix
 
    foo = FooBase('ftp://abc.com')
    print foo.prefix
 
    try:
        foo = FooBase('ssh://abc.com')
    except Exception, message:
        print message
..................................................

Comments:

No Comments for this post yet...

This post has 1 feedback awaiting moderation...

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.

Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email and url)
(Allow users to contact you through a message form (your email will NOT be displayed.))