[BioPython] first steps into python
Brad Chapman
chapmanb@arches.uga.edu
Sat, 24 Feb 2001 11:12:11 -0500
Hi Ewan!
> Ok. To help Ensembl-->python (probably via CORBA) integration. I have
> downloaded biopython and biopython-corba.
Great! Thanks for helping with this.
> I am, therefore, belated learning python. Don't expect any road-to-damscus
> type conversions (yet) however...
Well, we'll see :-)
> Some questions
>
> (a) what is the difference between def __name__ and def name functions?
Functions with double underscores on both sides are special method
attributes of a class. These are normally used to allow you to make
your classes look like built in classes, or to specify how
python will deal with an object of your class. For instance __len__
defines the built in len() operator for your class, so whenever anyone
calls len(your_object) to get the length of your object, the built in
__len__ that you created will be called:
>>> class MyClass:
... def __len__(self):
... return 500
...
>>> spam = MyClass()
>>> len(spam)
500
So using this, you could make your class act like a list. Andrew uses
this a lot in the Seq class to make Seq objects in biopython act
"like" a python string.
__init__ is the most commonly used special method attribute, and is
the "initializer" of a class, which, if it exists, is called whenever
an instance of a class is intialized. This allows you pass arguments
to the class, etc.
> (b) how is inhertiance done in python
Very simply:
>>> class MyBaseClass:
... def __init__(self, spam):
... print "Initialized the base class with", spam
...
>>> class MyDerivedClass(MyBaseClass):
... def __init__(self):
... MyBaseClass.__init__(self, "I love python")
... print "I'm a derived class!"
...
>>> base_class = MyBaseClass("hello")
Initialized the base class with hello
>>> derived_class = MyDerivedClass()
Initialized the base class with I love python
I'm a derived class!
You can also do multiple inheritance and other fun stuff. See:
http://python.org/doc/current/tut/node11.html
for more detailed info on classes.
> (c) is there any concept/files for interfaces either expliciting (java) or
> "just documentation" like bioperl's "I" files
In my view (others probably disagree), interfaces aren't really
something that fits well with python. My python philosophy is that you
get an object that you expect to implement certain functions, and you
just assume that the person who passed it in was smart enough to give
you an object that implements what you need.
The reason I see things like that is that python is very flexible
about interfaces. For instance, you can use strings and lists
identically for many things:
>>> spam = "I'm a string"
>>> len(spam)
12
>>> spam[6]
's'
>>> eggs = ["l", "i", "s", "t"]
>>> len(eggs)
4
>>> eggs[3]
't'
Similary, you could define a class which implemented all the special
method attributes and functions to make it act exactly like a list, so
there is no reason to make them explicitly derive from a base list class.
Personally, when I write comments, I normally say that I expect a
certain class to be a list or implement a list interface or whatever,
and then just trust that the person using my stuff will follow
directions :-)
> (d) could some one sketch out an easy biopython script like:
>
> read embl file.
>
> test whether there is a cacaca repeat in there
>
> if yes, dump a genbank file.
Check out the Tutorial documentation linked to from:
http://biopython.org/wiki/html/BioPython/BiopythonCode.html
There are tons of examples there. Most of the code used there is
also in the Doc/examples directory, so you can check out the straight
python code without any long winded explanations :-).
> yours from the rough-and-ready world of Perl...
welcome-to-the-quick-and-easy-world-of-Python-ly y'rs,
Brad