Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extend a python module? Adding new functionality to the `python-twitter` package

What are the best practices for extending an existing Python module – in this case, I want to extend the python-twitter package 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 trying to figure out the most Pythonic and least disruptive way to add them into the python-twitter package module, without changing this modules’ core.

like image 353
user319045 Avatar asked Apr 24 '10 20:04

user319045


People also ask

What is the extension of Python modules?

In Python, Modules are simply files with the “. py” extension containing Python code that can be imported inside another Python Program. In simple terms, we can consider a module to be the same as a code library or a file that contains a set of functions that you want to include in your application.

Should I add __ init __ py?

Although Python works without an __init__.py file you should still include one. It specifies that the directory should be treated as a package, so therefore include it (even if it is empty).

Can packages contain modules in Python?

A Python package is nothing but a collection of modules along with a __init__.py file. The modules can also be arranged in hierarchy of folders inside a package. Just by adding an empty __init__.py file to the in the folder, Python knows it is a Package.


2 Answers

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.

like image 146
Rizwan Kassim Avatar answered Sep 28 '22 16:09

Rizwan Kassim


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.

like image 22
Mike Graham Avatar answered Sep 28 '22 18:09

Mike Graham