views:

191

answers:

3

What are the best practices for extending a python module -- in this case I want to extend python-twitter by adding new methods to the base API class.

I've looked at tweepy, and I like that as well, I just find python-twitter easier to understand and extend with the functionality I want.

I have the methods written already, I'm just trying to figure out the best way to add them into the module, without changing the core.

+4  A: 

Don't add them to the module. Subclass the classes you want to extend and use your subclasses in your own module, not changing the original stuff at all.

Mike Graham
Ok, I'm not familiar with sub-classing in python yet, (I'm an engineer, still getting used to OO style), do you have an favorite resources? So, basically there is nothing special about a module... I can just write a new module that uses python-twitter API class as a base class and then add in the methods I want..?
@user319045, To subclass you'd just write your own module that creates a new class using the syntax `class SomeName(othermodule.TheClassYouAreExtending):...` and `SomeName` will depend back on the original class in a well-defined way. This is called *inheritance* and is how you would normally add methods to a class. This and other topics are covered in http://tinyurl.com/thinkcspy2e, which is a book that will introduce you to Python and OOP. It is available free online and in print and is better than some of the other options I've seen.
Mike Graham
The official tutorial http://docs.python.org/tut/ is also pretty good, but may prove harder for people who are not experienced programmers with good understanding of the programming paradigms they are using.
Mike Graham
@user319045, Inheritance is probably overused. There's a real chance that what you really want is just to define functions that take an instance of one of python-twitter's classes as an argument or to define a new class that has an instance of one of python-twitter's classes as an attribute. (The latter technique is called *composition* when you need to distinguish it from inheritance.) I cannot tell you for sure what you want without understanding what problem you are solving exactly.
Mike Graham
+2  A: 

A few ways.

The easy way:

Don't extend the module, extend the classes.

exttwitter.py

import twitter

class Api(twitter.Api):
    pass 
    # override/add any functions here.

Downside : Every class in twitter must be in exttwitter.py, even if it's just a stub (as above)

A harder (possibly un-pythonic) way:

Import * from python-twitter into a module that you then extend.

For instance :

basemodule.py

 class Ball():
    def __init__(self,a):
        self.a=a
    def __repr__(self):
        return "Ball(%s)" % self.a

def makeBall(a):
    return Ball(a)

def override():
    print "OVERRIDE ONE"

def dontoverride():
    print "THIS WILL BE PRESERVED"

extmodule.py

from basemodule import *
import basemodule

def makeBalls(a,b):
    foo = makeBall(a)
    bar = makeBall(b)
    print foo,bar

def override():
    print "OVERRIDE TWO"

def dontoverride():
    basemodule.dontoverride()
    print "THIS WAS PRESERVED"

runscript.py

import extmodule

#code is in extended module
print extmodule.makeBalls(1,2)
#returns Ball(1) Ball(2)

#code is in base module
print extmodule.makeBall(1)
#returns Ball(1)

#function from extended module overwrites base module
extmodule.override()
#returns OVERRIDE TWO

#function from extended module calls base module first
extmodule.dontoverride()
#returns THIS WILL BE PRESERVED\nTHIS WAS PRESERVED

I'm not sure if the double import in extmodule.py is pythonic - you could remove it, but then you don't handle the usecase of wanting to extend a function that was in the namespace of basemodule.

As far as extended classes, just create a new API(basemodule.API) class to extend the Twitter API module.

Rizwan Kassim
thanks!, very nice example,
A: 

May I suggest not to reinvent the Wheel here? I'm building a >6k line Twitter Client for 2 month now, at first I checked python-twitter too, but it's lagging a lot behind the recent API changes,, Development doesn't seem to be that active either, also there was(at least when I last checked) no support for OAuth/xAuth).

So after searching around a bit more I discovered tweepy:
http://github.com/joshthecoder/tweepy

Pros: Active development, OAauth/xAuth and up to date with the API.
Chances are high that what you need is already in there.

So I suggest going with that, it's working for me, the only thing I had to add was xAuth(that got merge back to tweepy :)

Oh an a shameless plug, if you need to parse Tweets and/or format them to HTML use my python version of the twitter-text-* libraries:
http://github.com/BonsaiDen/twitter-text-python

This thing is unittestetd an guaranteed to parse Tweets just like Twitter.com does it.

Ivo Wetzel
Thanks, I've looked at tweepy, I just seem to like the style of python-twitter better. I'm not building a twitter client, just a tool suite to handle a few things very well with regard to research.