<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Beginning Python for Bioinformatics</title>
	<atom:link href="http://python.genedrift.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://python.genedrift.org</link>
	<description>a step-by-step guide to create Python applications in bioinformatics</description>
	<lastBuildDate>Thu, 27 Jan 2011 13:59:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1-beta2-17056</generator>
		<item>
		<title>Rebuilding</title>
		<link>http://python.genedrift.org/2010/12/28/rebuilding/</link>
		<comments>http://python.genedrift.org/2010/12/28/rebuilding/#comments</comments>
		<pubDate>Tue, 28 Dec 2010 16:50:54 +0000</pubDate>
		<dc:creator>Paulo Nuin</dc:creator>
				<category><![CDATA[Phase 2]]></category>

		<guid isPermaLink="false">http://python.genedrift.org/?p=375</guid>
		<description><![CDATA[The blog engine (WP) has been hacked last week and I&#8217;m still rebuilding some of its features. Sorry for any inconvenience.]]></description>
			<content:encoded><![CDATA[<p>The blog engine (WP) has been hacked last week and I&#8217;m still rebuilding some of its features. Sorry for any inconvenience.</p>
]]></content:encoded>
			<wfw:commentRss>http://python.genedrift.org/2010/12/28/rebuilding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why I left Biostar, but I still like Stackoverflow</title>
		<link>http://python.genedrift.org/2010/12/01/why-i-left-biostar-but-i-still-like-stackoverflow/</link>
		<comments>http://python.genedrift.org/2010/12/01/why-i-left-biostar-but-i-still-like-stackoverflow/#comments</comments>
		<pubDate>Wed, 01 Dec 2010 15:44:08 +0000</pubDate>
		<dc:creator>Paulo Nuin</dc:creator>
				<category><![CDATA[off topic]]></category>
		<category><![CDATA[biostar]]></category>

		<guid isPermaLink="false">http://python.genedrift.org/?p=373</guid>
		<description><![CDATA[About eight months ago I started using Biostar as I saw it as a great opportunity to exchange some ideas, concepts, tips in biology and bioinformatics. I even mentioned the website in this space, trying to bring more people to the mix; at the time the community wasn&#8217;t big enough, and some days went by [...]]]></description>
			<content:encoded><![CDATA[<p>About eight months ago I started using Biostar as I saw it as a great opportunity to exchange some ideas, concepts, tips in biology and bioinformatics. I even mentioned the website in this space, trying to bring more people to the mix; at the time the community wasn&#8217;t big enough, and some days went by without any question being posted.</p>
<p>But a couple of months ago my interest started to go down the drain. I don&#8217;t know if it was the constant next-generation sequencing barrage of questions every day, if it was the infantile blog/twitter posts from members competing for points or maybe the lack of votes for some answers that I posted (that&#8217;s selfish on my part, I admit). But at some point it seemed that the website turned into a competition of CVs or knowledge, very different from what I could see in different Stackoverflow spin-offs or in the main site. I guess the turning-point, or the moment I realized that the scientific community (at least in bioinformatics and related fields) will never the be the same as the programming and statistical ones, was the time I gave an answer that had less votes that the one saying &#8220;it&#8217;s not possible&#8221;. </p>
<p>Maybe the problem is myself, I don&#8217;t like cliques, don&#8217;t mind helping people for nothing, don&#8217;t care about reputation. I didn&#8217;tt care about how many points I had, and used the down-vote to actually vote down answers that I didn&#8217;t see as pertinent (if you never used those sites, every down-vote removes one point from your score). I still think that Biostar is a great idea, and I wish it becomes a great resource for all the bio fields. Maybe if the community gets big enough, maybe if don&#8217;t see the same group of people that you see every where else it might become a better place to hang-out online. But right now, I&#8217;m over it. </p>
]]></content:encoded>
			<wfw:commentRss>http://python.genedrift.org/2010/12/01/why-i-left-biostar-but-i-still-like-stackoverflow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Preview of Django 1.1 Testing and Debugging</title>
		<link>http://python.genedrift.org/2010/05/20/preview-of-django-1-1-testing-and-debugging/</link>
		<comments>http://python.genedrift.org/2010/05/20/preview-of-django-1-1-testing-and-debugging/#comments</comments>
		<pubDate>Thu, 20 May 2010 21:34:41 +0000</pubDate>
		<dc:creator>Paulo Nuin</dc:creator>
				<category><![CDATA[Phase 2]]></category>
		<category><![CDATA[book]]></category>
		<category><![CDATA[preview]]></category>

		<guid isPermaLink="false">http://python.genedrift.org/?p=371</guid>
		<description><![CDATA[Packt Publishing invited me to review Django 1.1 Testing and Debuggingby Karen M. Tracey. They also kindly provided a free chapter that you can download from the link below. A full review will be posted as soon as I finish the book. preview chapter &#8211; Chapter No.3 &#8220;Testing 1, 2, 3: Basic Unit Testing&#8221;]]></description>
			<content:encoded><![CDATA[<p>Packt Publishing invited me to review <a href="http://www.packtpub.com/django-1-1-testing-and-debugging/book/mid/280410xne4fe?utm_source=python.genedrift.org&#038;utm_medium=affiliate&#038;utm_content=blog&#038;utm_campaign=mdb_003140">Django 1.1 Testing and Debugging</a>by Karen M. Tracey. They also kindly provided a free chapter that you can download from the link below. A full review will be posted as soon as I finish the book.</p>
<p>preview chapter &#8211; <a href="http://python.genedrift.org/7566_Django 1.1 Testing and Debugging_SampleChapter_1.pdf">Chapter No.3 &#8220;Testing 1, 2, 3: Basic Unit Testing&#8221;</a></p>
]]></content:encoded>
			<wfw:commentRss>http://python.genedrift.org/2010/05/20/preview-of-django-1-1-testing-and-debugging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Initial impressions about Bioinformatics Programming using Python</title>
		<link>http://python.genedrift.org/2010/05/01/initial-impressions-about-bioinformatics-programming-using-python/</link>
		<comments>http://python.genedrift.org/2010/05/01/initial-impressions-about-bioinformatics-programming-using-python/#comments</comments>
		<pubDate>Sun, 02 May 2010 01:59:17 +0000</pubDate>
		<dc:creator>Paulo Nuin</dc:creator>
				<category><![CDATA[Phase 2]]></category>

		<guid isPermaLink="false">http://python.genedrift.org/?p=369</guid>
		<description><![CDATA[Last week I made a 5 book order at Amazon and one of them was Bioinformatics Programming Using Python: Practical Programming for Biological Data (Animal Guide) by Mitchell L Model. I started reading the book late Friday night, and I&#8217;m on the third chapter, where there&#8217;s an introduction to sequences. So far, I found the [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I made a 5 book order at Amazon and one of them was <a href="http://www.amazon.com/gp/product/059615450X?ie=UTF8&#038;tag=genedrift-20&#038;linkCode=as2&#038;camp=1789&#038;creative=9325&#038;creativeASIN=059615450X">Bioinformatics Programming Using Python: Practical Programming for Biological Data (Animal Guide)</a><img src="http://www.assoc-amazon.com/e/ir?t=genedrift-20&#038;l=as2&#038;o=1&#038;a=059615450X" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> by Mitchell L Model. </p>
<p>I started reading the book late Friday night, and I&#8217;m on the third chapter, where there&#8217;s an introduction to sequences. So far, I found the book very confusing, especially as it claims to be a book for people with no programming background. The examples are OK, but there&#8217;s a very messy mixture of Python interpreter and standalone script usage, as the author jumps back and forth them. Another thing is that some examples are explained in detail including the line number, while others you depend on the code&#8217;s docstring to understand it.</p>
<p>So far, I&#8217;m not impressed. The initial Python sequence example is a set and in this chapter there already are some functional programming concepts, what can be quite challenging to someone that has never programmed in their life. And in the second chapter the reader sees a ternary operator. Another criticism, is that in the preface the author suggests using Python 3, instead of 2, what might add to the frustration of the beginner when a module cannot be installed. </p>
<p>I will continue reading it and post whenever I have a more complete overview of the book.</p>
]]></content:encoded>
			<wfw:commentRss>http://python.genedrift.org/2010/05/01/initial-impressions-about-bioinformatics-programming-using-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Python for Bioinformatics by Sebastian Bassi: a (short) review</title>
		<link>http://python.genedrift.org/2010/04/19/python-for-bioinformatics-by-sebastian-bassi-a-review/</link>
		<comments>http://python.genedrift.org/2010/04/19/python-for-bioinformatics-by-sebastian-bassi-a-review/#comments</comments>
		<pubDate>Mon, 19 Apr 2010 14:01:08 +0000</pubDate>
		<dc:creator>Paulo Nuin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[book]]></category>
		<category><![CDATA[review]]></category>

		<guid isPermaLink="false">http://python.genedrift.org/?p=366</guid>
		<description><![CDATA[I promised some time ago to post a complete review of Python for Bioinformatics (Chapman &#038; Hall/CRC Mathematical &#038; Computational Biology) by Sebastian Bassi. It&#8217;s long overdue, but the delay allowed me to get more acquainted to the book and its contents. I can only say that I highly recommend this book, especially for the [...]]]></description>
			<content:encoded><![CDATA[<p>I promised some time ago to post a complete review of <a href="http://www.amazon.com/gp/product/1584889292?ie=UTF8&#038;tag=genedrift-20&#038;linkCode=as2&#038;camp=1789&#038;creative=9325&#038;creativeASIN=1584889292">Python for Bioinformatics (Chapman &#038; Hall/CRC Mathematical &#038; Computational Biology)</a><img src="http://www.assoc-amazon.com/e/ir?t=genedrift-20&#038;l=as2&#038;o=1&#038;a=1584889292" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> by Sebastian Bassi. It&#8217;s long overdue, but the delay allowed me to get more acquainted to the book and its contents.</p>
<p>I can only say that I highly recommend this book, especially for the biologist that is beginning in bioinformatics or python (or both). I cannot compare it to any other Python and Bioinformatics books (I&#8217;m planning to buy the another one), but I can say that I could learn a thing or two from Sebastian&#8217;s book. Evidently is not a perfect book, as some of the explanations are a little bit rushed and might be difficult for a beginner. At the same time this is a very carefully thought and planned book and has more than enough for one to learn Python and apply the language to solve biological problems. I really liked the BioPython section, and this section made me use BioPython for the first time. Some of BioPython&#8217;s examples in the book are light years ahead of the examples in the tool&#8217;s website. </p>
<p>Lastly, I would like to congratulate Sebastian for his work and effort in putting together a nice tome for Python and Bioinformatics. It&#8217;s a valuable resource for everyone in the field and certainly will help spread Python in the community.</p>
]]></content:encoded>
			<wfw:commentRss>http://python.genedrift.org/2010/04/19/python-for-bioinformatics-by-sebastian-bassi-a-review/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Biostar: bioinformatics community</title>
		<link>http://python.genedrift.org/2010/04/17/biostar-bioinformatics-community/</link>
		<comments>http://python.genedrift.org/2010/04/17/biostar-bioinformatics-community/#comments</comments>
		<pubDate>Sat, 17 Apr 2010 18:26:08 +0000</pubDate>
		<dc:creator>Paulo Nuin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://python.genedrift.org/?p=364</guid>
		<description><![CDATA[Biostar is a bioinformatics community on the StackExchange network. It&#8217;s still small and not a lot of questions are asked and answered every day, so we need more people participating. If you are new to bioinformatics, or are just curious about the newest trends in the field, help us grow.]]></description>
			<content:encoded><![CDATA[<p><a href="http://biostar.stackexchange.com/">Biostar</a> is a bioinformatics community on the StackExchange network. It&#8217;s still small and not a lot of questions are asked and answered every day, so we need more people participating. If you are new to bioinformatics, or are just curious about the newest trends in the field, help us grow.</p>
]]></content:encoded>
			<wfw:commentRss>http://python.genedrift.org/2010/04/17/biostar-bioinformatics-community/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The real value of blogging</title>
		<link>http://python.genedrift.org/2010/04/10/the-real-value-of-blogging/</link>
		<comments>http://python.genedrift.org/2010/04/10/the-real-value-of-blogging/#comments</comments>
		<pubDate>Sat, 10 Apr 2010 18:45:03 +0000</pubDate>
		<dc:creator>Paulo Nuin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://python.genedrift.org/?p=362</guid>
		<description><![CDATA[A couple of days ago I posted on here an entry called &#8216;The &#8220;sickest&#8221; Python code I&#8217;ve ever created&#8216;. It&#8217;s a code that does some file management for proteomics data, with a different set of inputs each time you run it. The &#8220;sickest&#8221; part of the title is that it was a small challenge to [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of days ago I posted on here an entry called &#8216;<a href="http://python.genedrift.org/2010/04/08/the-sickest-python-code-ive-ever-created-1/">The &#8220;sickest&#8221; Python code I&#8217;ve ever created</a>&#8216;. It&#8217;s a code that does some file management for proteomics data, with a different set of inputs each time you run it.</p>
<p>The &#8220;sickest&#8221; part of the title is that it was a small challenge to me. I&#8217;ve been away of actual hard-core coding for quite sometime, and you lose some of the gist of the thing with time. Mostly, nowadays, I make simple scripts that don&#8217;t require any kind of advanced skills (in any language) and I don&#8217;t worry that much of releasing code or about ultra fast performance. I knew from the time I posted that a lot of people would jump and help and teach me, as I was aware it wasn&#8217;t the most elegant code out there, not even the most Pythonic one too. What also helped me is that my Python/Bioinformatics blog is indexed on Planet Python, and the audience is far more hard-core Python that I ever dreamed of getting by myself alone. </p>
<p>But the real deal is that I believe it would be much more difficult for me to get some positive feedback or even an answer if I had posted bits of my code on a online forum or community or list. Every time I used one of these methods, I either got no answer, or got schooled for not posting in the right format or somebody replied that no one knew how to do it. There&#8217;s the real deal of blogging, and the value is even higher if your audience knows more than you do. I appreciate every comment I got on that post and on others too, I learned things that I wasn&#8217;t able to learn from computer books and online tutorials (yes, I searched sometimes before reading the comments, and sometimes after).</p>
]]></content:encoded>
			<wfw:commentRss>http://python.genedrift.org/2010/04/10/the-real-value-of-blogging/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>The &#8220;sickest&#8221; Python code I&#8217;ve ever created 1</title>
		<link>http://python.genedrift.org/2010/04/08/the-sickest-python-code-ive-ever-created-1/</link>
		<comments>http://python.genedrift.org/2010/04/08/the-sickest-python-code-ive-ever-created-1/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 01:59:54 +0000</pubDate>
		<dc:creator>Paulo Nuin</dc:creator>
				<category><![CDATA[Phase 2]]></category>

		<guid isPermaLink="false">http://python.genedrift.org/?p=357</guid>
		<description><![CDATA[But, I guess, it can be easily refactored/enhanced/despised by the audience that read or have access to this blog via Planet Python. Anyway, for someone like me, whose main task now is not to generate tons of code and lines, I think the code (or part of it) that I will present below is quite [...]]]></description>
			<content:encoded><![CDATA[<p>But, I guess, it can be easily refactored/enhanced/despised by the audience that read or have access to this blog via Planet Python. Anyway, for someone like me, whose main task now is not to generate tons of code and lines, I think the code (or part of it) that I will present below is quite good. Feel free to comment, criticize and say bad and good things about it. </p>
<p>We needed a script that would take files coming out from protein search engines that would be able to compare the peptides and protein sequences, their abundance and some other characteristics. We had a combination of protein and peptide files, with a list of proteins (one protein per line in a tab delimited file) that was related to a list of peptides in another file (one peptide per line, with multiple peptides/lines related to one protein in the original list). Also each line in both files had more than 50 columns, and 8 or 16 of them were the values we wanted to extract. I say 8 or 16 of them because we didn&#8217;t know how many will output each time, as it would depend on the number of samples per run (4 to 8 samples) .</p>
<p>So, we had a couple of issues: we didn&#8217;t know how many proteins would be output (actually found) in each file, we wouldn&#8217;t know how many peptides for each protein would be found and we didn&#8217;t know before hand how many samples would be run at once. One good thing is that the 8-16 columns of values were fixed, always in the same position and with empty cells if no value was registered there. And we had a fourth problem: usually the samples attributions would be random, meaning a control could come in the first value column or could come in the last. And a fifth as we didn&#8217;t know before hand (the tech knew) how many treatments would be run each time. A treatment could be a different experimental condition, a sample grouping or some other extraneous factor. An extra issue is that we would need to compare multiple files, and get protein and peptide abundances in all of them at the same time and finally compare each treatment.</p>
<p>Basically, in order to create an universal script we needed something flexible enough that whatever the experiments threw at use we would be able to handle. First step we decided to use a YAML file that could be filled by the experimental researchers with sample assignments, treatments, etc. The YAML would look like this</p>
<p>B0:<br />
    &#8211; 114: A<br />
    &#8211; 115: D<br />
    &#8211; 116: B<br />
    &#8211; 117: C</p>
<p>B1:<br />
    &#8211; 114: C<br />
    &#8211; 115: A<br />
    &#8211; 116: D<br />
    &#8211; 117: B</p>
<p>In this file B0 and B1 would be the result file names, 114 is the column/channel where the sample was run and and A, B, C and D the treatment. With this set, out objective was to get all proteins and their peptides for treatment A in files B0 and B1, do some calculation and them compare to all proteins and peptides from treatment B, C and D extracted also from files B0 and B1.</p>
<p>First step was to get the names of the treatments from the YAML file</p>
<pre class="brush: python; title: ;">]def get_treatments(mapping):
    treats = set([])
    for entry in mapping:
        [treats.add(list(t.values())[0]) for t in mapping[entry]]

    return treats</pre>
<p>where
<pre class="brush: plain; title: ;">mapping</pre>
<p> is the YAML file name. We used a set to store and sets have unique items, and treatment names can vary from file to file. In the code above we basically read the YAML and the value for each entry.</p>
<p>We then needed a class to store protein information, and there was the story got hairy. With all my (lack of ) experience, I decided to use
<pre class="brush: plain; title: ;">exec</pre>
<p> statements to fix all the uncertainty of the experimental data details. I didn&#8217;t have the treatment names before hand (or in a fixed immutable list), and didn&#8217;t have the columns (channels) that were being used at the time and I have to correctly assign each protein abundance (area) to its place. In the end our class look like this</p>
<pre class="brush: python; title: ;">class Protein():
    &quot;&quot;&quot;Class Protein, stores all the information about channels and areas, name and accession&quot;&quot;&quot;
    def __init__(self, accession, name, treatments):
        self.accession = accession
        self.name = name

        #ratio channels are called based on their name
        for i in treatments:
            exec('self.%s = []' %i)
            exec('self.area%s = []' %i)

    def add_to_channel(self, channel, peptide):
        exec('self.%s.append(peptide)' % channel)

    def add_to_area(self, channel, area):
        exec('self.area%s.append(area)' % channel)</pre>
<p>In order to be faithful to this blog&#8217;s name, I will explain how the code above is supposed to work. First,
<pre class="brush: plain; title: ;">exec</pre>
<p> is a Python statement that support dynamic execution of code. In our case above it was used to name the objects, so we would be able to access them by name in subsequent functions. Let&#8217;s take this for example</p>
<pre class="brush: python; title: ;">for i in treatments:
    exec('self.%s = []' %i)
    exec('self.area%s = []' %i)</pre>
<p>In this snippet we were trying to create lists called (for the YAML file above) A, B, C and D, and another set of lists called areaA, areaB, areaC and areaD. Let&#8217;s say for another experiment we would have treatments &#8220;Control&#8221;, &#8220;Low&#8221; and &#8220;High&#8221; and so on.</p>
<p>The next two functions use the same approach, with exec, this time appending to the freshly created lists. This way it&#8217;s easy to control what the user is throwing at us.</p>
<p>I don&#8217;f know if this the best approach possible, or if it is or not harmful. Maybe experts reading this might have better ideas, and I appreciate them. We check the rest of the script next time.</p>
]]></content:encoded>
			<wfw:commentRss>http://python.genedrift.org/2010/04/08/the-sickest-python-code-ive-ever-created-1/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Python Testing Beginner’s Guide, review</title>
		<link>http://python.genedrift.org/2010/03/03/python-testing-beginner%e2%80%99s-guide-review/</link>
		<comments>http://python.genedrift.org/2010/03/03/python-testing-beginner%e2%80%99s-guide-review/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 03:46:35 +0000</pubDate>
		<dc:creator>Paulo Nuin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[book]]></category>
		<category><![CDATA[review]]></category>

		<guid isPermaLink="false">http://python.genedrift.org/?p=351</guid>
		<description><![CDATA[I posted about a week ago that Packt Publishing had invited me to review Python Testing Beginner&#8217;s Guide by Daniel Arbuckle. Having finished reading the book (I must admit that I haven&#8217;t tried all the code in it), I can say that I have an excellent initial impression of the book. PTBG is not a [...]]]></description>
			<content:encoded><![CDATA[<p>I posted about a week ago that Packt Publishing had invited me to review <strong><a href="http://www.packtpub.com/python-testing-beginners-guide/book/mid/240210aaphjg?utm_source=python.genedrift.org&#038;utm_medium=bookrev&#038;utm_content=blog&#038;utm_campaign=mdb_002570">Python Testing Beginner&#8217;s Guide</a></strong> by Daniel Arbuckle. Having finished reading the book (I must admit that I haven&#8217;t tried all the code in it), I can say that I have an excellent initial impression of the book.</p>
<p>PTBG is not a long book and the topic is divided in 10 chapters and one appendix. One of the first things that I liked about the book is that there&#8217;s no <em>introduction</em> (or something similar) to Python. It just goes straight to the point assuming that you have some good understanding of the language and everything that surrounds it. In the past I was frustrated with some &#8220;Introduction to X with Python&#8221; that wasted precious space talking over and over about a topic, learning Python, better covered in many other books. PTBG does not waste time and space introducing its main topic which is testing, and in my opinion that&#8217;s the best approach, even though it might look a little bit abrupt by some.</p>
<p>The language and text in the book is clear and very pleasant. PTBG is a very well written book and I really enjoyed its style. The first chapters of the book cover Python testing using doctests. For someone like me that didn&#8217;t write so many tests in the normal software development workflow (I know I should write more tests), this section seems like a really nice introduction to the topic, with well thought real-life like examples and a good flow on the explanation of the different features. One small complain that I have is that for a beginner sometimes the code listed in the examples might seem a little bit confusing, and maybe the addition of line numbers might have helped a bit here. But at the same I understand that this is normal style of some Packt books.</p>
<p>After the doctests section, PTBG gets into more advanced techniques, covering a little bit mock objects with <a href="http://labix.org/mocker">Mocker</a>, then moving into unittest and <a href="http://code.google.com/p/python-nose/">nose</a>. The latter is a Python tool that allows for managing, running and automating tests. Also covered is <a href=" http://pypi.python.org/pypi/ twill/">Twill</a>, another third-party library that allows for testing of web applications.</p>
<p>One full chapter is devoted to test-driven development, with a complete walkthrough of this approach. This gives a wrap-up of most of the techniques and modules covered in the book, but there&#8217;s still space for another chapter that shows how beautifully doctests, unittest and nose can be fully integrated and help the development of applications using the test-driven approach.</p>
<p>Overall, I really enjoyed PTBG. As I mentioned, test driven development was never a high priority in the application I usually developed with Python. But certainly this book can be a good starting point for some Python test beginners to incorporate these techniques in their usual development workflow. Scientific software is also a perfect  niche for this type of approach and we should do what is possible in order to avoid the <a href="http://boscoh.com/protein/a-sign-a-flipped-structure-and-a-scientific-flameout-of-epic-proportions">nightmares of the past</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://python.genedrift.org/2010/03/03/python-testing-beginner%e2%80%99s-guide-review/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Preview of Python Testing Beginner&#8217;s Guide</title>
		<link>http://python.genedrift.org/2010/02/22/preview-of-python-testing-beginners-guide/</link>
		<comments>http://python.genedrift.org/2010/02/22/preview-of-python-testing-beginners-guide/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 16:00:53 +0000</pubDate>
		<dc:creator>Paulo Nuin</dc:creator>
				<category><![CDATA[Phase 2]]></category>
		<category><![CDATA[book]]></category>
		<category><![CDATA[preview]]></category>
		<category><![CDATA[python testing]]></category>

		<guid isPermaLink="false">http://python.genedrift.org/?p=347</guid>
		<description><![CDATA[I was invited by Packt Publishing to review Python Testing Beginner&#8217;s Guide by Daniel Arbuckle. This is a book on one of the most important aspects of scientific programming (even though the majority of scientific software don&#8217;t have any testing routines): code testing, checking if your code actually does what is intended to do. I [...]]]></description>
			<content:encoded><![CDATA[<p>I was invited by Packt Publishing to review <strong><a href="http://www.packtpub.com/python-testing-beginners-guide/book/mid/240210aaphjg">Python Testing Beginner&#8217;s Guide</a></strong> by Daniel Arbuckle. This is a book on one of the most important aspects of scientific programming (even though the majority of scientific software don&#8217;t have any testing routines): code testing, checking if your code actually does what is intended to do. I can say I&#8217;m not really an expert on testing so I guess I&#8217;m the right audience for it:</p>
<blockquote><p>You&#8217;ll learn about several of Python&#8217;s automated testing tools, and you&#8217;ll learn about the philosophies and methodologies that they were designed to support, like unit testing and test-driven development. When you&#8217;re done, you&#8217;ll be able to produce thoroughly tested code faster and more easily than ever before, and you&#8217;ll be able to do it in a way that doesn&#8217;t distract you from your &#8220;real&#8221; programming.</p></blockquote>
<p>Packt also supplied a preview/sample chapter that you can download <a href='http://python.genedrift.org/wordpress/wp-content/uploads/2010/02/8846-python-testing-beginners-guide-sample-chapter-5-when-doctest-isnt-enough-unittest-to-the-rescue.pdf'>here</a>.</p>
<p>I hope to get a review ready by the end of the week. before the Ontario Institute of Cancer Research retreat, otherwise I will try to post a full review next week.</p>
]]></content:encoded>
			<wfw:commentRss>http://python.genedrift.org/2010/02/22/preview-of-python-testing-beginners-guide/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Preliminary review of Python for Bioinformatics by Sebastian Bassi</title>
		<link>http://python.genedrift.org/2010/01/03/preliminary-review-of-python-for-bioinformatics-by-sebastian-bassi/</link>
		<comments>http://python.genedrift.org/2010/01/03/preliminary-review-of-python-for-bioinformatics-by-sebastian-bassi/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 19:25:00 +0000</pubDate>
		<dc:creator>Paulo Nuin</dc:creator>
				<category><![CDATA[Phase 2]]></category>
		<category><![CDATA[biopython]]></category>
		<category><![CDATA[book]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[review]]></category>

		<guid isPermaLink="false">http://python.genedrift.org/?p=339</guid>
		<description><![CDATA[Let me start by saying that Python for Bioinformatics (Chapman &#38; Hall/Crc Mathematical &#38; Computational Biology) is a massive book, massive in a way that it contains a lot of material. I still didn&#8217;t have enough time to check everything, but I&#8217;m well into the first section of the book that gives an initial view [...]]]></description>
			<content:encoded><![CDATA[<p>Let me start by saying that <a href="http://www.amazon.com/gp/product/1584889292?ie=UTF8&amp;tag=genedrift-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1584889292">Python for Bioinformatics (Chapman &amp; Hall/Crc Mathematical &amp; Computational Biology)</a><img src="http://www.assoc-amazon.com/e/ir?t=genedrift-20&amp;l=as2&amp;o=1&amp;a=1584889292" alt="" style="border: medium none  ! important; margin: 0px ! important;" border="0" height="1" width="1"> is a massive book, massive in a way that it contains a lot of material. I still didn&#8217;t have enough time to check everything, but I&#8217;m well into the first section of the book that gives an initial view of Python and how to program it. </p>
<p>The initial section of the book is well written (I&#8217;m not going criticize the book in terms of good/poor English, as I&#8217;m not well qualified to do that), and gives a clear perspective on how to program Python for scientists, who are the main target demographic of the book. Of course, it always help to have some basic knowledge of command line shells, but the book also includes some explanations of IDLE and other Python-capable IDEs. I cannot say that I read this section with the enough care and attention, but what I can say is that you won&#8217;t miss a beat with PfB, as it has more material than I expected. I still have to start with the more advanced topics, like <a class="zem_slink freebase/en/biopython" href="http://biopython.org/" title="Biopython" rel="homepage">BioPython</a> and so forth, what I plan to do in the coming month, and as I don&#8217;t have a lot of experience with BioPython, I&#8217;m looking forward to it. </p>
<p>On the other hand I have a small-ish complaint, that maybe is more about style than substance. I don&#8217;t like the design of the book, the way the code interleaves with the text and the way the code explanations are presented. Most of the code blocks are followed by a careful explanation, but this explanation works as a figure label for the code block. That is quite annoying because there are too many stops in the text fluidity as one tends to lose attention to it (my case, not exactly everyone&#8217;s).  </p>
<p>Another minor detail is the use of &#8220;he&#8221; every time scientists are referred (one example is on page 3 on the second phrase of the introduction). The (politically) correct would be to use &#8220;he or she&#8221; or &#8220;she or he&#8221; (but that&#8217;s OK with me).</p>
<p>I will try to post more complete reviews of the sections that I don&#8217;t master. I would also like to thank Sebastian for sending me a copy of the book.</p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/99930d7a-5cef-4e74-b46c-71e031c4d627/" title="Reblog this post [with Zemanta]"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=99930d7a-5cef-4e74-b46c-71e031c4d627" alt="Reblog this post [with Zemanta]"></a><span class="zem-script more-related more-info pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://python.genedrift.org/2010/01/03/preliminary-review-of-python-for-bioinformatics-by-sebastian-bassi/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>This is (more or less) the end</title>
		<link>http://python.genedrift.org/2009/05/22/this-is-more-or-less-the-end/</link>
		<comments>http://python.genedrift.org/2009/05/22/this-is-more-or-less-the-end/#comments</comments>
		<pubDate>Sat, 23 May 2009 02:19:01 +0000</pubDate>
		<dc:creator>Paulo Nuin</dc:creator>
				<category><![CDATA[The End]]></category>
		<category><![CDATA[end]]></category>
		<category><![CDATA[fim]]></category>
		<category><![CDATA[fin]]></category>
		<category><![CDATA[final]]></category>

		<guid isPermaLink="false">http://python.genedrift.org/?p=334</guid>
		<description><![CDATA[So, I&#8217;m closing the blog, maybe for good, maybe not. I haven&#8217;t been updating it and some other responsibilities are consuming my spare time. I would like to thank everyone that contributed, commented and read it. You have my deepest appreciation. My work is fulfilled if I helped at least one person along the way. [...]]]></description>
			<content:encoded><![CDATA[<p>So, I&#8217;m closing the blog, maybe for good, maybe not. I haven&#8217;t been updating it and some other responsibilities are consuming my spare time. </p>
<p>I would like to thank everyone that contributed, commented and read it. You have my deepest appreciation. My work is fulfilled if I helped at least one person  along the way.</p>
<p>Cheers<br />
Paulo</p>
<p>PS: there&#8217;s the wiki, so register and help me improve it.<br />
PS II: sorry that I couldn&#8217;t finish the last project. Maybe some other time.</p>
<p><!--adsense--></p>
]]></content:encoded>
			<wfw:commentRss>http://python.genedrift.org/2009/05/22/this-is-more-or-less-the-end/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wiki</title>
		<link>http://python.genedrift.org/2009/05/11/wiki/</link>
		<comments>http://python.genedrift.org/2009/05/11/wiki/#comments</comments>
		<pubDate>Mon, 11 May 2009 22:43:31 +0000</pubDate>
		<dc:creator>Paulo Nuin</dc:creator>
				<category><![CDATA[Phase 2]]></category>
		<category><![CDATA[bioinformatics]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[wiki]]></category>

		<guid isPermaLink="false">http://python.genedrift.org/?p=294</guid>
		<description><![CDATA[Image via Wikipedia I&#8217;m slowly moving the posts from the blog to a wiki. It makes easier to display post series and allows people to modify/enhance/discuss. The wiki address is http://wiki.genedrift.org.]]></description>
			<content:encoded><![CDATA[<div class="zemanta-img" style="margin: 1em; display: block;">
<div>
<dl style="width: 310px;" class="wp-caption alignright">
<dt class="wp-caption-dt"><a href="http://commons.wikipedia.org/wiki/Image:History_comparison_example.png"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/3/3d/History_comparison_example.png/300px-History_comparison_example.png" alt="History comparison reports highlight the chang..." title="History comparison reports highlight the chang..." width="300" height="263"></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image via <a href="http://commons.wikipedia.org/wiki/Image:History_comparison_example.png">Wikipedia</a></dd>
</dl>
</div>
</div>
<p>I&#8217;m slowly moving the posts from the blog to a wiki. It makes easier to display post series and allows people to modify/enhance/discuss.</p>
<p>The wiki address is <a href="http://wiki.genedrift.org">http://wiki.genedrift.org</a>.</p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/ddf23630-4b1e-4cb1-8363-47b964768c18/" title="Reblog this post [with Zemanta]"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_a.png?x-id=ddf23630-4b1e-4cb1-8363-47b964768c18" alt="Reblog this post [with Zemanta]"></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://python.genedrift.org/2009/05/11/wiki/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Managing a simple database with Python, SQLite and wxPython, 8</title>
		<link>http://python.genedrift.org/2009/04/22/managing-a-simple-database-with-python-sqlite-and-wxpython-8/</link>
		<comments>http://python.genedrift.org/2009/04/22/managing-a-simple-database-with-python-sqlite-and-wxpython-8/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 15:04:17 +0000</pubDate>
		<dc:creator>Paulo Nuin</dc:creator>
				<category><![CDATA[Phase 2]]></category>
		<category><![CDATA[wxPython]]></category>
		<category><![CDATA[bioinformatics]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[SQLite]]></category>

		<guid isPermaLink="false">http://python.genedrift.org/?p=282</guid>
		<description><![CDATA[Image via Wikipedia Thanks to the comments and suggestions to the last post, it&#8217;s possible to make now a more pythonic and clearly generic database update class. Let&#8217;s check how the &#8220;generic&#8221; update/edit entry function is currently: def update_data(self, values_list): '''edits and updates fields''' if sys.platform == 'darwin': (cursor, database) = link_db(self.db_path) else: (cursor, database) [...]]]></description>
			<content:encoded><![CDATA[<div class="zemanta-img" style="margin: 1em; display: block;">
<div>
<dl style="width: 210px;" class="wp-caption alignright">
<dt class="wp-caption-dt"><a href="http://commons.wikipedia.org/wiki/Image:Gene.png"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/0/07/Gene.png/200px-Gene.png" alt="Diagram of the location of introns and exons w..." title="Diagram of the location of introns and exons w..." height="160" width="200"></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image via <a href="http://commons.wikipedia.org/wiki/Image:Gene.png">Wikipedia</a></dd>
</dl>
</div>
</div>
<p>Thanks to the comments and suggestions to the last post, it&#8217;s possible to make now a more pythonic and clearly generic database update class. Let&#8217;s check how the &#8220;generic&#8221; update/edit entry function is currently:</p>
<pre class="brush: python; title: ;">def update_data(self, values_list):
    '''edits and updates fields'''

    if sys.platform == 'darwin':
        (cursor, database) = link_db(self.db_path)
    else:
        (cursor, database) = link_db()

    cursor.execute(&quot;UPDATE bac SET  projects = ?, comments = ?, temperature = ?, cell = ?, box = ?, tubes = ?, chromosome = ?, sdate = ?, clone = ?, source
	= ?, location1 = ?, startpos = ?, endpos = ?,
	gene = ?, genelink = ?, dnaex = ?, validation = ?, pcr = ?, refs = ?, antibiotic = ? WHERE idbac = ?&quot;,
    values_list['projects'], values_list['comments'], values_list['temperature'], values_list['cell'], values_list['box'], values_list['tubes'],
    values_list['chromo'], values_list['date'], values_list['clone'], values_list['source'], values_list['location'], values_list['start']
    values_list['end'], values_list['gene'], values_list['genelink'], values_list['dna'], values_list['validation'], values_list['pcr'],
    values_list['refs'], values_list['antibiotic'], values_list['idbac']))

    database.commit()
    database.close()</pre>
<p>which is really ugly and, although it works, is not really useful outside this small project. Based on the comments the best option was to use placeholders and a dictionary, similar to the approach used on the insert data function. Pre-formatting a string to have both the field name to be updated and a placeholder (for instance <code>:idbac</code>) that will receive the values</p>
<pre class="brush: python; title: ;">update = ','.join(['%s=:%s' % (y, y) for y in values_list])</pre>
<p>where update is the string we want and values_list is the dictionary with all the key-value pairs. I tried this approach, using this structure in the generic function, but then I decided that the best alternative was to put this <code>join</code> in the derived class function and pre-populate the string with the values and then send this string directly to the update function. In the end I opted to use this </p>
<pre class="brush: python; title: ;">update = ','.join(['%s=\&quot;%s\&quot;' % (y, values_list[y]) for y in values_list])</pre>
<p>The latter is slightly different to what was suggested. The original one would create a tuple with the keys from the dictionary, making for instance <code>sdate:sdate</code>. With all these place holders just pass the dictionary and you have all the values inserted. This would be handy if the insert string was being created on the &#8220;generic&#8221; function. If we move this to the derived class, we can use the the alternative, keeping in mind that the values parsed should be surrounded by quotes, otherwise the SQL UPDATE statement will have problems with spaces and other foreign characters that should not be there. So instead of placeholders we will have <code>gene:"<a class="zem_slink" href="http://en.wikipedia.org/wiki/PTEN_%28gene%29" title="PTEN (gene)" rel="wikipedia">PTEN</a>"</code> and we can attache this joined string to the actual commands. We then can move all the machinery from the &#8220;generic&#8221; function that can be written as</p>
<pre class="brush: python; title: ;">def update_data(self, update_string):
    '''edits and updates fields'''

    if sys.platform == 'darwin':
        (cursor, database) = link_db(self.db_path)
    else:
        (cursor, database) = link_db()
    cursor.execute(update_string)

    database.commit()
    database.close()</pre>
<p>That&#8217;s it, very elegant (we will see the derived class in the next post). And finishing our generic class, we would need a delete function, so the user can eliminate entries that he/she doesn&#8217;t want anymore. It&#8217;s also a very simple function</p>
<pre class="brush: python; title: ;">def delete_data(self, delete_string):
    '''deletes one field'''

    if sys.platform == 'darwin':
        (cursor, database) = link_db(self.db_path)
    else:
        (cursor, database) = link_db()
    cursor.execute(delete_string)

    database.commit()
    database.close()</pre>
<p>We will check the delete string next time. Again, I would like to thank for all the comments, it has been really helpful for me.</p>
<p>Previously in the series:<br />
<a href="http://python.genedrift.org/2009/02/09/managing-a-simple-database-with-python-sqlite-and-wxpython-1/">Part 1</a><br />
<a href="http://python.genedrift.org/2009/02/17/managing-a-simple-database-with-python-sqlite-and-wxpython-2/">Part 2</a><br />
<a href="http://python.genedrift.org/2009/02/18/managing-a-simple-database-with-python-sqlite-and-wxpython-3/">Part 3</a><br />
<a href="http://python.genedrift.org/2009/03/02/managing-a-simple-database-with-python-sqlite-and-wxpython-4/">Part 4</a><br />
<a href="http://python.genedrift.org/2009/03/02/managing-a-simple-database-with-python-sqlite-and-wxpython-5/">Part 5</a><br />
<a href="http://python.genedrift.org/2009/03/31/managing-a-simple-database-with-python-sqlite-and-wxpython-6/">Part 6</a><br />
<a href="http://python.genedrift.org/2009/04/20/managing-a-simple-database-with-python-sqlite-and-wxpython-7-includes-a-question/">Part 7</a></p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/e8dc77f5-e3de-4d4f-8ec1-8c0006225743/" title="Reblog this post [with Zemanta]"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_a.png?x-id=e8dc77f5-e3de-4d4f-8ec1-8c0006225743" alt="Reblog this post [with Zemanta]"></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://python.genedrift.org/2009/04/22/managing-a-simple-database-with-python-sqlite-and-wxpython-8/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Managing a simple database with Python, SQLite and wxPython, 7 (includes a question)</title>
		<link>http://python.genedrift.org/2009/04/20/managing-a-simple-database-with-python-sqlite-and-wxpython-7-includes-a-question/</link>
		<comments>http://python.genedrift.org/2009/04/20/managing-a-simple-database-with-python-sqlite-and-wxpython-7-includes-a-question/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 17:21:59 +0000</pubDate>
		<dc:creator>Paulo Nuin</dc:creator>
				<category><![CDATA[Phase 2]]></category>
		<category><![CDATA[wxPython]]></category>
		<category><![CDATA[bioinformatics]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[SQLite]]></category>

		<guid isPermaLink="false">http://python.genedrift.org/2009/04/20/managing-a-simple-database-with-python-sqlite-and-wxpython-7-includes-a-question/</guid>
		<description><![CDATA[And we&#8217;re back. After a couple of weeks of inactivity we will get back to our small soap-opera pf Python, wxPython and SQLite. Continuing in our database management code let&#8217;s check two other functions that changed since our first inception of the code. The first one is the insert_data function that looks like this now [...]]]></description>
			<content:encoded><![CDATA[<p>And we&#8217;re back. After a couple of weeks of inactivity we will get back to our small soap-opera pf Python, wxPython and SQLite. Continuing in our database management code let&#8217;s check two other functions that changed since our first inception of the code. The first one is the <code>insert_data</code> function that looks like this now</p>
<pre class="brush: python; title: ;">def insert_data(self, values_list, insert_string):
    '''inserts data in the database'''

    if sys.platform == 'darwin':
        (cursor, database) = link_db(self.db_path)
    else:
        (cursor, database) = link_db()

    cursor.execute(insert_string % self.table_name, values_list)

    database.commit()
    database.close()</pre>
<p>Basically no changes, apart from the obvious check for the current running operating system, which was explained in the last post. The other function to check is the <code>update_data</code>. This function is new and it wasn&#8217;t in the first version, but as it can be seen it has a problem being a &#8220;generic&#8221; function, because it contains information pertained to the table and database being used in the interface. This function basically received information that needs to be updated in the table&#8217;s fields and by using the SQL <code>UPDATE ... SET</code> edits and updates data in the changed fields. I have tried several different syntaxes to make the execute generic, mainly trying to pre-format the string without success. IF anyone reading this can help, I&#8217;d appreciate.</p>
<pre class="brush: python; title: ;">def update_data(self, values_list):
    '''edits and updates fields'''

    if sys.platform == 'darwin':
        (cursor, database) = link_db(self.db_path)
    else:
        (cursor, database) = link_db()

    cursor.execute(&quot;UPDATE bac SET  projects = ?, comments = ?, temperature = ?, cell = ?, box = ?, tubes = ?, chromosome = ?, sdate = ?, clone = ?, source = ?, location1 = ?, startpos = ?, endpos = ?,
	gene = ?, genelink = ?, dnaex = ?, validation = ?, pcr = ?, refs = ?, antibiotic = ? WHERE idbac = ?&quot;,
    values_list['projects'], values_list['comments'], values_list['temperature'], values_list['cell'], values_list['box'], values_list['tubes'],
    values_list['chromo'], values_list['date'], values_list['clone'], values_list['source'], values_list['location'], values_list['start'],  values_list['end'],
    values_list['gene'], values_list['genelink'], values_list['dna'], values_list['validation'], values_list['pcr'],
    values_list['refs'], values_list['antibiotic'], values_list['idbac']))

    database.commit()
    database.close()</pre>
<p>Anyway, I will explain the logic of the command (OK for a stop gap, but not as a definite solution). <code>values_list</code> is a dictionary that is passed to the function and contains the field names as keys and the new/changed information as values. The execute method simply parses the values from each key in the update string which is then sent to the database and table to be changed. Everything is committed and the database is closed.</p>
<p>As this is a &#8220;generic&#8221; function from a &#8220;generic&#8221; class the ideal scenario would be to the function to receive a pre-formatted string with all the information, as in the insert data function, and update the information in the database. </p>
<p>I would like to thank in advance anyone that can comment on this. Next time we will continue checking the generic class and finalize this part in order to start with the interface build process.</p>
<p>Previously in the series:<br />
<a href="http://python.genedrift.org/2009/02/09/managing-a-simple-database-with-python-sqlite-and-wxpython-1/">Part 1</a><br />
<a href="http://python.genedrift.org/2009/02/17/managing-a-simple-database-with-python-sqlite-and-wxpython-2/">Part 2</a><br />
<a href="http://python.genedrift.org/2009/02/18/managing-a-simple-database-with-python-sqlite-and-wxpython-3/">Part 3</a><br />
<a href="http://python.genedrift.org/2009/03/02/managing-a-simple-database-with-python-sqlite-and-wxpython-4/">Part 4</a><br />
<a href="http://python.genedrift.org/2009/03/02/managing-a-simple-database-with-python-sqlite-and-wxpython-5/">Part 5</a><br />
<a href="http://python.genedrift.org/2009/03/31/managing-a-simple-database-with-python-sqlite-and-wxpython-6/">Part 6</a></p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/d0bb5d11-6f9d-8521-9a2f-6cd30868e375/" title="Reblog this post [with Zemanta]"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_a.png?x-id=d0bb5d11-6f9d-8521-9a2f-6cd30868e375" alt="Reblog this post [with Zemanta]"></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://python.genedrift.org/2009/04/20/managing-a-simple-database-with-python-sqlite-and-wxpython-7-includes-a-question/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Managing a simple database with Python, SQLite and wxPython, 6</title>
		<link>http://python.genedrift.org/2009/03/31/managing-a-simple-database-with-python-sqlite-and-wxpython-6/</link>
		<comments>http://python.genedrift.org/2009/03/31/managing-a-simple-database-with-python-sqlite-and-wxpython-6/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 17:06:08 +0000</pubDate>
		<dc:creator>Paulo Nuin</dc:creator>
				<category><![CDATA[Phase 2]]></category>
		<category><![CDATA[wxPython]]></category>
		<category><![CDATA[bioinformatics]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[SQLite]]></category>

		<guid isPermaLink="false">http://python.genedrift.org/?p=276</guid>
		<description><![CDATA[Image via Wikipedia Let&#8217;s get back to our SQLite and wxPython project. We haven&#8217;t seen anything on wxPython yet, and we will check the interface only on the next post. For now, let&#8217;s see some extra code added to the SQLite access class. Remember that we have a generic class and one class derived from [...]]]></description>
			<content:encoded><![CDATA[<div class="zemanta-img" style="margin: 1em; display: block;">
<div>
<dl style="width: 212px;" class="wp-caption alignright">
<dt class="wp-caption-dt"><a href="http://commons.wikipedia.org/wiki/Image:SQLite_Logo_4.png"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/1/19/SQLite_Logo_4.png/202px-SQLite_Logo_4.png" alt="The :en:SQLite logo as of 2007-12-15" title="The :en:SQLite logo as of 2007-12-15" height="60" width="202"></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image via <a href="http://commons.wikipedia.org/wiki/Image:SQLite_Logo_4.png">Wikipedia</a></dd>
</dl>
</div>
</div>
<p>Let&#8217;s get back to our SQLite and wxPython project. We haven&#8217;t seen anything on wxPython yet, and we will check the interface only on the next post. For now, let&#8217;s see some extra code added to the SQLite access class. Remember that we have a generic class and one class derived from it that would work on accessing specific tables in our database file.</p>
<p>When we last covered the db access routines, there was no search for an entry (the function returned everything in the table no matter what), there was no update function in case someone would want to modify an entry and there was no delete method if you wanted to delete something. In the meantime, I added all of this functionality (and some other) to the generic class and extended it to the class derived from it. Let&#8217;s check how the generic class is now (you will notice that there is an issue in one of the methods, if someone can help me I&#8217;d appreciate. More details later.)</p>
<pre class="brush: python; title: ;">class DB_Generic():
    '''generic class to add DB functionality'''
    def __init__(self, table_name, db_path = ''):
        #par= name of the table to be used
        self.table_name = table_name
        if len(db_path) &amp;gt; 0:
            self.db_path = db_path
            print db_path

    def get_data_generic(self, range = 1, bac_to_get = 0):
        '''gets the data from the database'''       

        if sys.platform == 'darwin':
            (cursor, database) = link_db(self.db_path)
        else:
            (cursor, database) = link_db()

        if range == 1:
            cursor.execute(&quot;&quot;&quot;SELECT * FROM %s&quot;&quot;&quot; % self.table_name)
        elif range == 2:
            cursor.execute(&quot;&quot;&quot;SELECT * FROM %s where idbac = %d&quot;&quot;&quot; % (self.table_name, bac_to_get))

        table_data = cursor.fetchall()
        raw_data = []
        for i in table_data:
            raw_data.append(list(i))

        self.table_data = raw_data
        database.close()

    def insert_data(self, values_list, insert_string):
        '''inserts data in the database'''

        if sys.platform == 'darwin':
            (cursor, database) = link_db(self.db_path)
        else:
            (cursor, database) = link_db()

        cursor.execute(insert_string % self.table_name, values_list)

        database.commit()
        database.close()

    def update_data(self, values_list):
        '''edits and updates fields'''

        if sys.platform == 'darwin':
            (cursor, database) = link_db(self.db_path)
        else:
            (cursor, database) = link_db()

        #change this to generic!!!!!!!!!!!!
        cursor.execute(&quot;UPDATE bac SET  projects = ?, comments = ?, temperature = ?, cell = ?, box = ?, tubes = ?, chromosome = ?, sdate = ?, clone = ?, source = ?, location1 = ?, startpos = ?, endpos = ?,
		gene = ?, genelink = ?, dnaex = ?, validation = ?, pcr = ?, refs = ?, antibiotic = ? WHERE idbac = ?&quot;,
        (values_list['projects'], values_list['comments'], values_list['temperature'], values_list['cell'], values_list['box'], values_list['tubes'],
         values_list['chromo'], values_list['date'], values_list['clone'], values_list['source'], values_list['location'], values_list['start'], values_list['end'],
         values_list['gene'], values_list['genelink'], values_list['dna'], values_list['validation'], values_list['pcr'],
         values_list['refs'], values_list['antibiotic'], values_list['idbac']))

        database.commit()
        database.close()

    def delete_data(self, delete_string):
        '''deletes one field'''

        if sys.platform == 'darwin':
            (cursor, database) = link_db(self.db_path)
        else:
            (cursor, database) = link_db()
        cursor.execute(delete_string)

        database.commit()
        database.close()</pre>
<p>In the next couple of posts we&#8217;ll dissect each function and see what&#8217;s going on. The class definition wasn&#8217;t changed, so we start with <code>get_data_generic</code></p>
<pre class="brush: python; title: ;">def get_data_generic(self, range = 1, bac_to_get = 0):
	'''gets the data from the database'''       

	if sys.platform == 'darwin':
		(cursor, database) = link_db(self.db_path)
	else:
		(cursor, database) = link_db()

	if range == 1:
		cursor.execute(&quot;&quot;&quot;SELECT * FROM %s&quot;&quot;&quot; % self.table_name)
	elif range == 2:
		cursor.execute(&quot;&quot;&quot;SELECT * FROM %s where idbac = %d&quot;&quot;&quot; % (self.table_name, bac_to_get))

	table_data = cursor.fetchall()
	raw_data = []
	for i in table_data:
		raw_data.append(list(i))

	self.table_data = raw_data
	database.close()</pre>
<p>The first difference we notice here is the <code>sys.platform</code> usage. This is required if we intend to package our application as an OS X app, using py2app. When a Python/wxPython application is packaged in OS X, the actual application executable is inside the a directory named after the application (or whatever you set up). In our case here we don&#8217;t provide a way for the Python script to receive the path and name for the database on a command line, as we expect it to be in the executable&#8217;s current directory. Because of that we need to provide a &#8220;config&#8221; file (in our case here a one-line text file with the database path) inside the application wrapper, something we will see in the end of the series.</p>
<p>Another modification here is the <code>range</code> parameter and the addition of the <code>bac_to_get</code> parameter. Notice that both parameters have a value assigned to it. This means that they are optional, the function&#8217;s call can pass them or not. If it doesn&#8217;t pass, their value will be the one assigned on the function declaration. So, here if we are interested in getting all bacs, <code>range</code> will have the value of 1 and we don&#8217;t need to worry about it. If we want an specific bac we will pass <code>range</code> as 2 and then pass the <code>bac_to_get</code> ID to be returned. </p>
<p>A final change/addition is that we added a new select statement for the cases when <code>range</code> equals 2. This time we are adding the bac ID to be returned.</p>
<p>Previously in the series:<br />
<a href="http://python.genedrift.org/2009/02/09/managing-a-simple-database-with-python-sqlite-and-wxpython-1/">Part 1</a><br />
<a href="http://python.genedrift.org/2009/02/17/managing-a-simple-database-with-python-sqlite-and-wxpython-2/">Part 2</a><br />
<a href="http://python.genedrift.org/2009/02/18/managing-a-simple-database-with-python-sqlite-and-wxpython-3/">Part 3</a><br />
<a href="http://python.genedrift.org/2009/03/02/managing-a-simple-database-with-python-sqlite-and-wxpython-4/">Part 4</a><br />
<a href="http://python.genedrift.org/2009/03/02/managing-a-simple-database-with-python-sqlite-and-wxpython-5/">Part 5</a></p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/ea53b728-33c6-47db-aabf-0c695dcfabd8/" title="Zemified by Zemanta"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_a.png?x-id=ea53b728-33c6-47db-aabf-0c695dcfabd8" alt="Reblog this post [with Zemanta]"></a><span class="zem-script more-related"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://python.genedrift.org/2009/03/31/managing-a-simple-database-with-python-sqlite-and-wxpython-6/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>RoR commits</title>
		<link>http://python.genedrift.org/2009/03/15/ror-commits/</link>
		<comments>http://python.genedrift.org/2009/03/15/ror-commits/#comments</comments>
		<pubDate>Sun, 15 Mar 2009 16:59:18 +0000</pubDate>
		<dc:creator>Paulo Nuin</dc:creator>
				<category><![CDATA[Phase 2]]></category>

		<guid isPermaLink="false">http://python.genedrift.org/?p=272</guid>
		<description><![CDATA[Just illustrating my point (or lack of), an animation about the commits of RoR to its repository. Notice the jump after it was migrated to Github Ruby on Rails from Ilya Grigorik on Vimeo. Sorry for the non-Python post.]]></description>
			<content:encoded><![CDATA[<p>Just illustrating my point (or lack of), an animation about the commits of RoR to its repository. Notice the jump after it was migrated to Github</p>
<p><object width="400" height="225"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=2979844&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=2979844&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed></object><br /><a href="http://vimeo.com/2979844">Ruby on Rails</a> from <a href="http://vimeo.com/user1211508">Ilya Grigorik</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p>Sorry for the non-Python post.</p>
]]></content:encoded>
			<wfw:commentRss>http://python.genedrift.org/2009/03/15/ror-commits/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>BioPython and CVS</title>
		<link>http://python.genedrift.org/2009/03/13/biopython-and-cvs/</link>
		<comments>http://python.genedrift.org/2009/03/13/biopython-and-cvs/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 19:25:38 +0000</pubDate>
		<dc:creator>Paulo Nuin</dc:creator>
				<category><![CDATA[off topic]]></category>
		<category><![CDATA[biopython]]></category>
		<category><![CDATA[CVS]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[SVN]]></category>

		<guid isPermaLink="false">http://python.genedrift.org/?p=266</guid>
		<description><![CDATA[Image via Wikipedia I start this post with an apology. I usually don&#8217;t rant or vent here, which are feelings that I usually reserve to my personal blog. I don&#8217;t use BioPython, never used it. I have it installed in my systems, but I never wrote a piece of code importing BioPython routines. But I [...]]]></description>
			<content:encoded><![CDATA[<div class="zemanta-img" style="margin: 1em; display: block;">
<div>
<dl style="width: 135px;" class="wp-caption alignright">
<dt class="wp-caption-dt"><a href="http://en.wikipedia.org/wiki/Image:Bazaar_logo_%28software_product%29.svg"><img src="http://upload.wikimedia.org/wikipedia/en/thumb/c/c1/Bazaar_logo_%28software_product%29.svg/125px-Bazaar_logo_%28software_product%29.svg.png" alt="Bazaar" title="Bazaar" height="128" width="125"></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image via <a href="http://en.wikipedia.org/wiki/Image:Bazaar_logo_%28software_product%29.svg">Wikipedia</a></dd>
</dl>
</div>
</div>
<p>I start this post with an apology. I usually don&#8217;t rant or vent here, which are feelings that I usually reserve to my personal blog.</p>
<p>I don&#8217;t use BioPython, never used it. I have it installed in my systems, but I never wrote a piece of code importing BioPython routines. But I subscribe to their mailing lists, both user and developer. I maybe have written once to the list, and I just follow the discussions there. </p>
<p>Since last year one of the main topics has been the possibility of moving BioPython from CVS to another version control system. Yes, you read it right. It&#8217;s 2009 and BioPython uses CVS and their version control system. Soon, CVS will be like typewriters and LPs to young developers. Last stable release of CVS was sometime in 2005, what in interwebs time is equivalent to something like 1972. Since 2005, Subversion has taken the world of version control by storm, and Git is getting also very strong, not to mention Bazaar, Darcs, Mercurial and some others that I might not be aware of.</p>
<p>This is a discussion that have been dragging for sometime in the list. And it&#8217;s a shame, a clear lack of leadership from whoever is (not) leading the project. BioRuby is Git, BioPerl SVN and BioPython is CVS, because they &#8220;need to care for the legacy developers&#8221;. It&#8217;s like MSFT keeping two copies of the Notepad executable because they needed to cater to legacy applications, but with a different scale of course. With the current Python steam in the non-bioinformatics and bioinformatics community is very sad to see BioPython not evolving (before you ask me, no, I&#8217;m not interested in helping, not the way things are now). Perl which is language forever-in-waiting for its holy grail (Perl 6) has a strong community behind it, and more important an excellent leadership, that&#8217;s not scare of making decisions.</p>
<p>So, if you&#8217;re still using CVS, it&#8217;s 2009!</p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/06117f0a-6d25-497d-b686-1a96e8a7d81c/" title="Zemified by Zemanta"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=06117f0a-6d25-497d-b686-1a96e8a7d81c" alt="Reblog this post [with Zemanta]"></a><span class="zem-script more-related"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://python.genedrift.org/2009/03/13/biopython-and-cvs/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Managing a simple database with Python, SQLite and wxPython, 5</title>
		<link>http://python.genedrift.org/2009/03/02/managing-a-simple-database-with-python-sqlite-and-wxpython-5/</link>
		<comments>http://python.genedrift.org/2009/03/02/managing-a-simple-database-with-python-sqlite-and-wxpython-5/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 00:23:42 +0000</pubDate>
		<dc:creator>Paulo Nuin</dc:creator>
				<category><![CDATA[Phase 2]]></category>
		<category><![CDATA[wxPython]]></category>
		<category><![CDATA[bioinformatics]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[SQLite]]></category>

		<guid isPermaLink="false">http://python.genedrift.org/?p=253</guid>
		<description><![CDATA[We have seen how to connect, get and insert data (at least theoretically) in the database. Now, a little not about the SQL engine of choice here: SQLite. SQLite databases have the main characteristic that they are self-contained files. Also it does not require an installation, works without a server and works pretty well in [...]]]></description>
			<content:encoded><![CDATA[<p>We have seen how to connect, get and insert data (at least theoretically) in the database. Now, a little not about the SQL engine of choice here: SQLite. SQLite databases have the main characteristic that they are self-contained files. Also it does not require an installation, works without a server and works pretty well in most operating systems. </p>
<p>Basically for the type of application we&#8217;re developing here, SQLite seems ideal. It eliminates a lot of infrastructure that would be needed if we were working with MySQL or postgresql. We don&#8217;t need a server or know how to configure users or manage the databases and tables. All we need is contained in a single file that can be transported from system to system and can be accesed from the computers used in the lab, mainly XP and OS X. Also some web frameworks (Rails and <a class="zem_slink" href="http://www.djangoproject.com" title="Django (web framework)" rel="homepage">Django</a>, for instance) can use SQLite, so in the end we can have a desktop application and a web application accessing the same file without extra configuration.</p>
<p>Now the database created for this application has 8 tables and almost no relationships among them. SQLite allows the creation of relationships but in our case only a couple of cases were required. For the table we are using at the moment (bac) there is no need for relationships, although there are some fileds that can benefit from a more relational structure. Also SQLite don&#8217;t have the same data types that are found on the bigger SQL engines. All values can be stored as text, integer, real (floating point numbers), null and blob (verbose type, what you store is what you get). As actual types, you can set columns as Boolean and Data for instance and SQLite will understand them. If you have no experience in creating databases, let&#8217;s check again the table we are using in this small project. First, I would recommend the use of some SQLite database editor. You can find pretty good ones for any computer system and there is even a Firefox extension that allows you to edit some files. Editors make it easier to generate the SQL table creation scripts and make easier to visualize what we are doing.</p>
<p>So, the table bac looks like </p>
<pre class="brush: sql; title: ;">CREATE TABLE bac
(idbac INTEGER PRIMARY KEY,
clone Text,
sdate Date,
source Text,
gene Text,
chromosome Text,
startpos Integer,
endpos Integer,
antibiotic Text,
location1 Text,
temperature Integer,
tubes Integer,
box Integer,
cell Integer,
dnaex Boolean,
validation Boolean,
pcr Boolean,
projects Text,
comments Text,
genelink Text,
refs Text);</pre>
<p>If you go back to our last post, you will see that in the insert statement there is no mention of the <code>idbac</code> field. We don&#8217;t actually insert ay value there, the values that populate this field are created automatically. And <code>idbac</code> is our primary key, meaning it&#8217;s the unique identifier of each bac we insert in this table. And in SQLite a integer primary key is automatically incremented whenever values are inserted in the table. So our first insertion will create <code>idbac</code> 1, the second will create <code>idbac</code> 2 and so on. </p>
<p>I&#8217;m not going to enter in details about database development and administration, but it&#8217;s usual and safe to create tables with an auto-incremental integer primary keys. These fields, apart from make it easier t identify records, make access to such records faster and are great when relationships among tables are set. Let&#8217;s say that we had a column user in our bac table. And let&#8217;s say we had an user table with two columns: user_id and name, user_id being a auto-increment primary key. The user column in back could be linked with the user_id column in the user table, in what we call a one-to-many relationship (one user can insert as many bacs as he wants). One day we want to know who is actually working in the lab and we want to check how many bacs were catalogued by each user. We can easily search the user table and extract information from bacs at the same time thanks to the relationship between the tables. And the result should be returned quite quickly, as we are only searching integers.</p>
<p>All the other fields/columns in our table are straightforward to understand. They are basically related to the type of data they need to store. <code>validation</code> is a boolean because the bac might have been validated or not, just as <code>danex</code> (DNA extraction). At the same time, the number of tubes stored in the freezer will always be an integer. So, why does temperature is an integer? Because we can only store bacs in two type of freezers: -80 (ultra freezers) or -20 (regular freezer that we can have at home), and we don&#8217;t need to worry about fractional numbers. </p>
<p>Well, this is a very short and limited explanation of tables and SQLite. The web is full of resources about it, so next time we will get back to Python.</p>
<p>Previously in the series:<br />
<a href="http://python.genedrift.org/2009/02/09/managing-a-simple-database-with-python-sqlite-and-wxpython-1/">Part 1</a><br />
<a href="http://python.genedrift.org/2009/02/17/managing-a-simple-database-with-python-sqlite-and-wxpython-2/">Part 2</a><br />
<a href="http://python.genedrift.org/2009/02/18/managing-a-simple-database-with-python-sqlite-and-wxpython-3/">Part 3</a><br />
<a href="http://python.genedrift.org/2009/03/02/managing-a-simple-database-with-python-sqlite-and-wxpython-4/">Part 4</a></p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/4be1389f-5603-4b76-961b-b79d985066cc/" title="Zemified by Zemanta"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=4be1389f-5603-4b76-961b-b79d985066cc" alt="Reblog this post [with Zemanta]"></a><span class="zem-script more-related"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://python.genedrift.org/2009/03/02/managing-a-simple-database-with-python-sqlite-and-wxpython-5/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Managing a simple database with Python, SQLite and wxPython, 4</title>
		<link>http://python.genedrift.org/2009/03/02/managing-a-simple-database-with-python-sqlite-and-wxpython-4/</link>
		<comments>http://python.genedrift.org/2009/03/02/managing-a-simple-database-with-python-sqlite-and-wxpython-4/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 16:58:09 +0000</pubDate>
		<dc:creator>Paulo Nuin</dc:creator>
				<category><![CDATA[Phase 2]]></category>
		<category><![CDATA[bioinformatics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[wxPython]]></category>

		<guid isPermaLink="false">http://python.genedrift.org/?p=250</guid>
		<description><![CDATA[Image via Wikipedia Let&#8217;s continue building our small db app. As mentioned in the previous post we need now to instantiate a specific class from our generic SQLite access class. In order to do this we just have to declare a new class and its type will be DB_Generic. class Bac(DB_Generic) This new class is [...]]]></description>
			<content:encoded><![CDATA[<div class="zemanta-img" style="margin: 1em; display: block;">
<div>
<dl style="width: 212px;" class="wp-caption alignright">
<dt class="wp-caption-dt"><a href="http://commons.wikipedia.org/wiki/Image:SQLite_Logo_4.png"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/1/19/SQLite_Logo_4.png/202px-SQLite_Logo_4.png" alt="The :en:SQLite logo as of 2007-12-15" title="The :en:SQLite logo as of 2007-12-15" height="60" width="202"></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image via <a href="http://commons.wikipedia.org/wiki/Image:SQLite_Logo_4.png">Wikipedia</a></dd>
</dl>
</div>
</div>
<p>Let&#8217;s continue building our small db app. As mentioned in the previous post we need now to instantiate a specific class from our generic <a class="zem_slink" href="http://sqlite.org/" title="SQLite" rel="homepage">SQLite</a> access class. In order to do this we just have to declare a new class and its type will be <code>DB_Generic</code>.  </p>
<pre class="brush: python; title: ;">class Bac(DB_Generic)</pre>
<p>This new class is called Bac because it&#8217;s linked to the bac table in our database file. A side note, bacs are Bacterial Artificial Chromosomes and are used in different molecular biology techniques. Mainly in our case bacs have incorporated human DNA segments and are used as probes for deletion, duplication, etc studies.</p>
<p>Now, back to our Python code, as soon as we instantiate our generic class, the object (class) we create has access to all methods and functions from the parent class (by using <code>self</code>), but we still need to create functionality and expose other methods that can be accessed from a class object derived from <code>Bac</code>.</p>
<p>Our instantiated class will be </p>
<pre class="brush: python; title: ;">class Bac(DB_Generic):
    def __init_(self):
        self.bac_data = []
        DB_Generic.__init__(self, 'bac')

    def get_data(self):
        return self.get_data_generic()

    def load_data(self):
        pass

    def add_data(self, values_list):
        insert_string = &quot;&quot;&quot;INSERT INTO %s (projects, comments, temperature, cell, box, tubes, chromosome, sdate, clone, source,
        location1, startpos, endpos, gene, genelink, dnaex, validation, pcr, refs, antibiotic)
        VALUES (:projects, :comments, :temperature, :cell, :box, :tubes, :chromo, :date, :clone, :source, :location, :start,
        :end, :gene, :genelink, :dna, :validation, :pcr, :refs, :antibiotic)&quot;&quot;&quot;
        self.insert_data(values_list, insert_string)</pre>
<p>Pretty simple so far, as we don&#8217;t have a lot of declared methods. Let&#8217;s check one by one</p>
<pre class="brush: python; title: ;">def __init_(self):
    DB_Generic.__init__(self, 'bac')</pre>
<p>The only line is the initialization required by the parent class, and we&#8217;re passing the value that is the table to be accessed. 	</p>
<pre class="brush: python; title: ;">def get_data(self):
	self.get_data_generic()
	return self.table_data</pre>
<p>The <code>get_data</code> function returns the all elements in our table (So far, we still don&#8217;t have an elegant range option) and has one too many lines in it. We will get rid of some useless code here in the future, but it&#8217;s OK the way it is. Basically this code access the <code>get_data_generic</code> from the parent class and gets all the values stored in the table. </p>
<p>There is a function not yet complete that will load data, and will be used in the future. And the last one is the function that actually adds the data to the table with a SQL insert statement</p>
<pre class="brush: python; title: ;">def add_data(self, values_list):
	insert_string = &quot;&quot;&quot;INSERT INTO %s (projects, comments, temperature, cell, box, tubes, chromosome, sdate, clone, source,
	location1, startpos, endpos, gene, genelink, dnaex, validation, pcr, refs, antibiotic)
	VALUES (:projects, :comments, :temperature, :cell, :box, :tubes, :chromo, :date, :clone, :source, :location, :start,
	:end, :gene, :genelink, :dna, :validation, :pcr, :refs, :antibiotic)&quot;&quot;&quot;
	self.insert_data(values_list, insert_string)</pre>
<p>In this function, we have a large string with all the SQL insert options. A SQL insert statement is divided into two parts, one where you point where to insert the values and another where you input the values. Usually simple insert statements will have this structure</p>
<pre class="brush: sql; title: ;">INSERT INTO my_table_name (table_column1, table_column2) VALUES (value1, value2);</pre>
<p>So, we have the table we want to insert values into, its columns and the values we set for each column. After executed this will put value1 into table_column1 and value2 into table_column2. The actual syntax can vary a bit for different SQL engines but the structure is identical in most cases. Pretty simple.</p>
<p>For our insert string above, there are some aspects to call for attention. Again note the triple quote around the statement. This make sure that it&#8217;s not changed and parsed correctly. We also have a <code>%s</code> for the table name, which will be parsed by the parent class function that insert values, then a list of all the tables in the database and then a list of values to insert. And why the values to be inserted have this <code>:value</code> syntax? Because we are previously storing the values in a dictionary, and the &#8220;:&#8221; indicates that we need to get the dictionary value for the correspondent key.</p>
<p>The insert string, and the list of values (actually a dictionary, not the best variable/object name I must admit) is then sent to the parent class to be inserted. Storing the values to be inserted in a dictionary is OK for a one time insert case, where the values are obtained from a form. If you are parsing a large CSV or TSV file, ideally it&#8217;s better to put it in a list, and dump them at the same time.</p>
<p>We&#8217;re progressing. Next we will take a look on some simple SQL table structure and then move to create the form to insert the values and check the table.</p>
<p>Previously in the series:<br />
<a href="http://python.genedrift.org/2009/02/09/managing-a-simple-database-with-python-sqlite-and-wxpython-1/">Part 1</a><br />
<a href="http://python.genedrift.org/2009/02/17/managing-a-simple-database-with-python-sqlite-and-wxpython-2/">Part 2</a><br />
<a href="http://python.genedrift.org/2009/02/18/managing-a-simple-database-with-python-sqlite-and-wxpython-3/">Part 3</a></p>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/4c2737a4-f1de-46bf-ae53-c5de040daf97/" title="Zemified by Zemanta"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=4c2737a4-f1de-46bf-ae53-c5de040daf97" alt="Reblog this post [with Zemanta]"></a><span class="zem-script more-related"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>
<p>evi</p>
]]></content:encoded>
			<wfw:commentRss>http://python.genedrift.org/2009/03/02/managing-a-simple-database-with-python-sqlite-and-wxpython-4/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
