[BioPython] named tuples for biopython?

Giovanni Marco Dall'Olio dalloliogm at gmail.com
Fri Oct 17 10:03:32 UTC 2008


Hi,
python 2.6 is going to implement a new kind of data (like lists, strings,
etc..) called 'named_tuple'.
It is intended to be a better data format to be used when parsing record
files and databases.

You can download the recipe from here (it should be included experimentally
in python 2.6):
- http://code.activestate.com/recipes/500261/

Basically, you instantiate a named_tuple object with this syntax:

>> Person = NamedTuple("Person name surname")

"Person" is a label for the named_tuple; the following fields, 'name' and
'surname'
Then you will have named_tuple object which is basically a mix between a
dictionary, a custom class and a tuple:

>> Person = NamedTuple("Person name surname")
>> Einstein = Person('Albert', 'Einstein')
>> Einstein.name
'Albert'

>> Einstein.surname
'Einstein'

>> people = []
>> for line in f.readlines():
>>     people.append(Person(line.split())
>>
>> for person in people:
>>     print person.name, person.surname

named_tuples are also read-only object, so they should be less
memory-expensive
It is like tuples against lists, but more customizable.

I am really not good ad explaining, and I can't find a good tutorial that
illustrate this.
I read a good article about named_tuples, but it is in italian language (
http://stacktrace.it/2008/05/gestione-dei-record-python-1/). Maybe you can
understand the code examples.

Has any of you heard about this new data type? Do you think it could be
useful for biopython? There is a lot of file parsing / database interfacing
in bioinformatics :)


p.s. since I didn't trust HTML-based mails to keep code formatting, I also
posted this same message on nodalpoint:
http://www.nodalpoint.org/2008/10/17/python_2_6_will_implement_a_new_data_format_named_tuple_can_it_be_of_use_for_biopython


-- 
-----------------------------------------------------------

My Blog on Bioinformatics (italian): http://bioinfoblog.it



More information about the Biopython mailing list