<?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>Echo.2 &#187; hacking</title>
	<atom:link href="http://blogs.infoecho.net/echo/category/hacking/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.infoecho.net/echo</link>
	<description>ping and pong in the ocean of network</description>
	<lastBuildDate>Tue, 27 Mar 2012 05:49:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Can Not Resist Hacking IPython + d3.js, Another Force Layout Demo for Word Ladder Game</title>
		<link>http://blogs.infoecho.net/echo/2012/03/26/can-not-resist-hacking-ipython-d3-js-another-force-layout-demo-for-word-ladder-game/</link>
		<comments>http://blogs.infoecho.net/echo/2012/03/26/can-not-resist-hacking-ipython-d3-js-another-force-layout-demo-for-word-ladder-game/#comments</comments>
		<pubDate>Tue, 27 Mar 2012 05:45:30 +0000</pubDate>
		<dc:creator>Jason Chin</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blogs.infoecho.net/echo/?p=443</guid>
		<description><![CDATA[Post a video for visualizing the neighbors of English word (http://en.wikipedia.org/wiki/Word_ladder). It shows what can be done now with minimum change to IPython 0.13-dev source + some simple monkey patches. I will write down what I think where we can go from here later. The IPython notebook can be download from here. The monkey patches [...]]]></description>
			<content:encoded><![CDATA[<p>Post a <a href="http://dl.dropbox.com/u/69208751/ipython_d3_word_ladder.mov" title="ipython_d3_word_ladder">video</a> for visualizing the neighbors of English word (http://en.wikipedia.org/wiki/Word_ladder). It shows what can be done now with minimum change to IPython 0.13-dev source + some simple monkey patches.</p>
<p>I will write down what I think where we can go from here later. The IPython notebook can be download from <a href="https://github.com/cschin/IPython-Notebook---d3.js-mashup/blob/master/Word_Ladder_network_vis.ipynb" title="Word_Ladder_network_vis.ipynb">here</a>. The monkey patches that make this working can be downloaded from my fork of the IPython source code from the GitHub site too.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.infoecho.net/echo/2012/03/26/can-not-resist-hacking-ipython-d3-js-another-force-layout-demo-for-word-ladder-game/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://dl.dropbox.com/u/69208751/ipython_d3_word_ladder.mov" length="6310525" type="video/quicktime" />
		</item>
		<item>
		<title>Yet another ipython + d3.js example: motion chart</title>
		<link>http://blogs.infoecho.net/echo/2012/02/26/yet-another-ipython-d3-js-example-motion-chart/</link>
		<comments>http://blogs.infoecho.net/echo/2012/02/26/yet-another-ipython-d3-js-example-motion-chart/#comments</comments>
		<pubDate>Mon, 27 Feb 2012 01:11:41 +0000</pubDate>
		<dc:creator>Jason Chin</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blogs.infoecho.net/echo/?p=413</guid>
		<description><![CDATA[I gave a lightening talk in a recent Bay Area Python meet-up. I went over some of my recent hacks on combining ipython notebook and d3.js. What I wanted to show was how to mix python code and javascript code to create a dynamic programming/data analysis notebook. I created yet another example to demonstrate the [...]]]></description>
			<content:encoded><![CDATA[<p>I gave a lightening talk in <a href="http://www.meetup.com/silicon-valley-python/events/53418592/">a recent Bay Area Python meet-up</a>.  I went over some of my recent hacks on combining ipython notebook and d3.js. What I wanted to show was how to mix python code and javascript code to create a dynamic programming/data analysis notebook.  I created yet another example to demonstrate the great potential on combining the powerful tools.  </p>
<p>If you are interested, you can try the this ipython <a href="https://github.com/cschin/IPython-Notebook---d3.js-mashup/blob/master/GDP_CO2_Example.ipynb" title="notebook">notebook</a>.  You will need to download the development branch of the ipython v 0.13 to see the notebook.  The notebook itself includes some of the explanation on how to run it and how it is done. I did not spend too much polishing the code and the motion chart, but it got the basic ingredients. If you want to peek it, here is a short screen recoding to show it looks like. </p>
<div class="hvlog"> <a href="https://github.com/cschin/IPython-Notebook---d3.js-mashup/raw/master/images/ipy_nb_d3_movable_chart.m4v" rel="enclosure"><br />
<img src="https://github.com/cschin/IPython-Notebook---d3.js-mashup/raw/master/images/ipy_nb_d3_movable_chart.jpg" width=500> <br />(click to download the movie)<br />
</a> </div>
]]></content:encoded>
			<wfw:commentRss>http://blogs.infoecho.net/echo/2012/02/26/yet-another-ipython-d3-js-example-motion-chart/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Experimenting with ipython notebook bi-directional communication</title>
		<link>http://blogs.infoecho.net/echo/2012/02/21/experimenting-with-ipython-notebook-bi-directional-communication/</link>
		<comments>http://blogs.infoecho.net/echo/2012/02/21/experimenting-with-ipython-notebook-bi-directional-communication/#comments</comments>
		<pubDate>Wed, 22 Feb 2012 05:37:38 +0000</pubDate>
		<dc:creator>Jason Chin</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blogs.infoecho.net/echo/?p=407</guid>
		<description><![CDATA[Thanks for Brian Granger pointing out how to make bi-directional communication from javascript in a ipython-notebook front-end to back-end ipython kernel using the existing websocket/zmq channel architecture in ipython (see the thread ). I have been hacking around to see how to do it. I need to modified a few lines of the ipython-notebook javascript [...]]]></description>
			<content:encoded><![CDATA[<p>Thanks for Brian Granger pointing out how to make bi-directional communication from javascript in a ipython-notebook front-end to back-end ipython kernel using the existing websocket/zmq channel architecture in ipython  (see the <a href="http://mail.scipy.org/pipermail/ipython-dev/2012-February/008778.html">thread</a> ). I have been hacking around to see how to do it. I need to modified a few lines of the ipython-notebook javascript to make it work ( see my <a href="https://github.com/cschin/ipython/commit/3a34d3b0c4d42bb1ef7b42660b12d429936cb287">github commit</a> ). I wrote some example to show how it works ( the <a href="https://github.com/cschin/IPython-Notebook---d3.js-mashup/blob/master/BidirectionalComm.ipynb">ipython notebook</a> and a <a href="https://github.com/cschin/IPython-Notebook---d3.js-mashup/blob/master/images/screenshot_bi_widget.jpg">screen shot</a> ).  Pretty cool that it works.  It seems that one can develop a widget library to avoid hand-crafting both the javascript and python code for such communication. All right, one more small step toward to building some cool interactive visualization / analysis tools with ipython.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.infoecho.net/echo/2012/02/21/experimenting-with-ipython-notebook-bi-directional-communication/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iPython Notebook / d3.js mashup</title>
		<link>http://blogs.infoecho.net/echo/2012/02/05/ipython-notebook-d3-js-mashup/</link>
		<comments>http://blogs.infoecho.net/echo/2012/02/05/ipython-notebook-d3-js-mashup/#comments</comments>
		<pubDate>Mon, 06 Feb 2012 04:19:54 +0000</pubDate>
		<dc:creator>Jason Chin</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blogs.infoecho.net/echo/?p=402</guid>
		<description><![CDATA[While I have been using ipython for a long time, I never really it more than just checking whether some code snippets working as expected. (Well, I tried to play with the parallel computing framework with ipython, but I never put it into production.) Just recently, I start to look into the ipython web-based notebook [...]]]></description>
			<content:encoded><![CDATA[<p>While I have been using <code>ipython</code> for a long time, I never really it more than just checking whether some code snippets working as expected. (Well, I tried to play with the parallel computing framework with <code>ipython</code>, but I never put it into production.) Just recently, I start to look into the <code>ipython</code> web-based notebook feature more carefully. It is great and make me think the <code>ipython</code> will make a python programmer or someone uses python for data analysis much more productive. (I used to envy the &#8220;RStudio&#8221; in the R-lang land, now, we python programmer finally have something more competitive.) </p>
<p>The cool thing using a web page as front-end is there are a lot potential using web interface for some cool visualization.  I played with protovis.js a while ago. Recently, I went to a visualization meets-up, d3.js was mentioned a numbers of time.  Then the idea comes to my mind &#8220;is it possible to combine the best of two world, python and d3.js?&#8221; After consulting some more experience users in the ipython-dev mailing list to see what is possible, I decided to spend some of my weekend time to hack it around.  In the meantime, I get the chance to play with tornado, zero-mq and websocket, all the fun stuff these days.  At the end, I am able to pass some javascript code written within the ipython notebook to get the browser to execute it and show some animation with <code>d3.js</code>. This will enable to create more fancier visualization in an interactive way all in a browser.  </p>
<p>My weekend hacking results are hosted at <a href="https://github.com/cschin/IPython-Notebook---d3.js-mashup" title="github repository "> github </a>. I think there is a great potential to make thing like this working better. (For example, can we have a pythonic backend of d3.js? <img src='http://blogs.infoecho.net/echo/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )  It definitely worth to mess it around to see more use like this.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.infoecho.net/echo/2012/02/05/ipython-notebook-d3-js-mashup/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to implement the Needleman–Wunsch alignment algorithm without using a single loop in Python</title>
		<link>http://blogs.infoecho.net/echo/2011/04/10/how-to-implement-the-needleman%e2%80%93wunsch-alignment-algorithm-without-using-a-single-loop-in-python/</link>
		<comments>http://blogs.infoecho.net/echo/2011/04/10/how-to-implement-the-needleman%e2%80%93wunsch-alignment-algorithm-without-using-a-single-loop-in-python/#comments</comments>
		<pubDate>Mon, 11 Apr 2011 04:11:46 +0000</pubDate>
		<dc:creator>Jason Chin</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blogs.infoecho.net/echo/?p=338</guid>
		<description><![CDATA[I am still fascinated about the programming style using co-routine. Actually, it is possible to implement the Needleman–Wunsch alignment algorithm by purely message passing fashion. The following code shows how to implement the algorithm using co-routines again. I modify the code from my previous post such that the alignment array itself is also generated dynamically. [...]]]></description>
			<content:encoded><![CDATA[<p>I am still fascinated about the programming style using co-routine.  Actually, it is possible to implement the <a href="http://en.wikipedia.org/wiki/Needleman%E2%80%93Wunsch_algorithm">Needleman–Wunsch alignment algorithm</a> by purely message passing fashion. The following code shows how to implement the algorithm using co-routines again.  I modify the code from <a href="http://blogs.infoecho.net/echo/2011/03/24/yet-another-python-coroutine-fun-stuff/"> my previous post </a> such that the alignment array itself is also generated dynamically.  We can completely remove those setting up loops.  This code is also annotated to show how it is done. If any reader is interested and have any comment, I do like to hear.     </p>
<pre class="brush: python; title: ; notranslate">
# @author Jason Chin
#
# Copyright (C) 2011 by Jason Chin
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the &quot;Software&quot;), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

&quot;&quot;&quot;

This is an example to implement Needleman-Wunsch sequence algorithm using python's
co-routine. One of the most interest aspect of such implementation is that there
is no explicitly loop. You can not find either the &quot;for&quot; nor &quot;while&quot; keywords
in this code.  Each alignment cell is a co-routine and the calculation of alignement
score and backtracking that generates the alignment string are done with a message
passing fashion.  The alignment cells are also generated in a dynamic way.  A
banded alignment can be done by limiting not generate the whole alignment array but
only the banded part of the array.

Is it useful? I am not sure, but it is definitely fun to show it is possible.

--Jason Chin, Apr. 10, 2011

&quot;&quot;&quot;

### Set up the alignment score scheme
matchScore, mismatchScore, gapScore = 4, -3, -4

### Two testing string for alingment
seq1 = &quot;TTAAGTGTAGCCTTGTGTGACATGTATTTTTAT&quot;
seq2 = &quot;TTTCTAGGTAGTTGTGGTGAGTTTAGTTGATAT&quot;

### cellMap is a dictionary that maps integer pairs to the co-routines
cellMap = {}

### For tracking the global best alignment cell
globalBestCellScore = [None, -100000]

def getAnAlignCell(x, y, seq1, seq2):
    &quot;&quot;&quot;
    This function returns a co-routine the represents an alignment cell at position
    x and y.  The alignment strings are passed explicilty for simplicity.
    &quot;&quot;&quot;

    def alnCell():

        &quot;&quot;&quot;
        This is the co-routine for an alignment cell. A alignment cell co-routine is
        excuted in roughly two stage. The first stage it collects the alignment score
        from the cells at (x-1,y-1), (x-1,y), and (x, y-1) and calculate the best
        alignment score. Depending the alignment path through the alignment cell, a new
        alignment score is generated and passed to the cells at (x+1, y+1),
        (x+1,y), and (x, y+1). If any of those cell has not be generated, it will
        generate the co-routine and regisiter them with the cellMap dictionary. After
        this it waits for the backtracking caculation.  If a cell is in the best alignment
        path, it will pass the best alignment pair to next cell in the best alignment
        path.
        &quot;&quot;&quot;

        global globalBestCellScore
        global cellMap

        b1, b2 = seq1[x], seq2[y]
        mx, my = len(seq1), len(seq2)

        cellData = []

        # if the cell is on the top or the left side of the alignment, they only have
        # to wait for one other cell to pass in the alignment score. Otherwise, they
        # need to collect three messages from those (x-1,y), (x,y-1), and (x-1, y-1)
        # before they can do any calculation.
        if x == 0 or y == 0:
            cellId, s = yield
            cellData.append( (cellId, s) )
        else:
            cellId, s = yield
            cellData.append( (cellId, s) )
            cellId, s = yield
            cellData.append( (cellId, s) )
            cellId, s = yield
            cellData.append( (cellId, s) )

        # find the best cell that gives the best alignment score
        cellData.sort( key=lambda x: -x[1] )
        bestCell, bestScore = cellData[0]

        if bestScore &gt; globalBestCellScore[1]:
            globalBestCellScore = [ (x,y), bestScore ]

        # pass the new alignment score to (x+1, y+1)
        if x+1 &lt; mx and y+1 &lt; my:
            # generate the cell at (x+1, y+1) if necessary
            if (x+1, y+1) not in cellMap:
                cellMap[ (x+1, y+1) ] = getAnAlignCell( x+1, y+1, seq1, seq2 )()
                cellMap[ (x+1, y+1) ].next()
            if b1 == b2: # a match, seq1[x] == seq[2], new_score = bestScore + matchScore
                cellMap[ (x+1, y+1) ].send( ((x,y), bestScore + matchScore) ) # pass the new score to cell (x+1, y+1)
            else: # a mismatch, seq1[x] != seq[2], new_score = bestScore + mismatchScore
                cellMap[ (x+1, y+1) ].send( ((x,y), bestScore + mismatchScore) ) # pass the new score to cell (x+1, y+1)
        # pass the new alignment score to (x+1, y), namely, the base seq1[x] is aligned to a gap
        if x+1 &lt; mx:
            # generate the cell at (x+1, y) if necessary
            if (x+1, y) not in cellMap:
                cellMap[ (x+1, y) ] = getAnAlignCell( x+1, y, seq1, seq2 )()
                cellMap[ (x+1, y) ].next()
            cellMap[ (x+1, y) ].send( ((x,y), bestScore + gapScore) )
        # pass the new alignment score to (x, y+1), namely, the base seq2[y] is aligned to a gap
        if y+1 &lt; my:
            # generate the cell at (x, y+1) if necessary
            if (x, y+1) not in cellMap:
                cellMap[ (x, y+1) ] = getAnAlignCell( x, y+1, seq1, seq2 )()
                cellMap[ (x, y+1) ].next()
            cellMap[ (x, y+1) ].send( ((x,y), bestScore + gapScore) )

        path = yield # wait, if the cell is on the best path, the co-routine will resume 

        # generate the alignment pair according the best alinged cells
        if bestCell[0] &gt;= 0 and bestCell[1] &gt;=0 :
            if path == None:
                path = []

            if bestCell[0] - x == 0:
                c1 = &quot;-&quot;
            else:
                c1 = seq1[x-1]
            if bestCell[1] - y == 0:
                c2 = &quot;-&quot;
            else:
                c2 = seq2[y-1]
            path.extend( [ (c1, c2) ] )

            # send calculated partial path to the best alingment cell to this cell
            cellMap[ bestCell ].send(  path   )

        # return the best path if bestCell[0] = -1 or bestCell[1] = -1
        yield path

    return alnCell

# initialize the cell at (0,0)
cellMap[ (0,0) ] = getAnAlignCell( 0, 0, seq1, seq2 )()
# prime it
cellMap[(0,0)].next()
# start the whole execution by sending in the initial score to cell at (0,0)
cellMap[(0,0)].send( ( (-1, -1), 0 ) )

# get the best global cell
bestCell = globalBestCellScore[0]

# continue to excute the best cell co-routine to get the alignment path
bestPath = cellMap[bestCell].next()
bestPath.reverse()

# some simple mechinary to print out the alignment path
alnRes = zip(*bestPath)
print &quot;&quot;.join(alnRes[0])
print &quot;&quot;.join(alnRes[1])
</pre>
<p>The result:</p>
<pre class="brush: plain; title: ; notranslate">
$ python coAlign_v2.py
-TT-AAGTGTAGCCTTGT-GTGACATGTA-TTTTTA
TTTCTAG-GTAG--TTGTGGTGA-GTTTAGTTGATA
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blogs.infoecho.net/echo/2011/04/10/how-to-implement-the-needleman%e2%80%93wunsch-alignment-algorithm-without-using-a-single-loop-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Yet Another Python Coroutine Fun Stuff</title>
		<link>http://blogs.infoecho.net/echo/2011/03/24/yet-another-python-coroutine-fun-stuff/</link>
		<comments>http://blogs.infoecho.net/echo/2011/03/24/yet-another-python-coroutine-fun-stuff/#comments</comments>
		<pubDate>Fri, 25 Mar 2011 07:14:37 +0000</pubDate>
		<dc:creator>Jason Chin</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blogs.infoecho.net/echo/?p=330</guid>
		<description><![CDATA[It might be a totally useless python hack. Yes, it is possible to implement dynamic programming using message passing style python co-routine with the enhanced python generator. Here is the code. I will write some details about how this piece code works. However, the main idea is simple (although you might need some background knowledge [...]]]></description>
			<content:encoded><![CDATA[<p>It might be a totally useless python hack. Yes, it is possible to implement dynamic programming using message passing style python co-routine with the enhanced python generator. Here is the code.  I will write some details about how this piece code works. However, the main idea is simple (although you might need some background knowledge about sequence alignment algorithm.)  We create a co-routine for each alignment cell. The alignment score is generated by passing the best score around the neighboring cells. The backtracking is also implemented as message passing backward.</p>
<pre class="brush: python; title: ; notranslate">
matchScore, mismatchScore, gapScore = 4, -5, -3
seq1 = &quot;AGTGTAGTTGTGTGAATGTATTTTTAT&quot;
seq2 = &quot;AGGTAGTTGTGGTGATTTAGTTGATAT&quot;

cellMap = {}
globalBestCellScore = [None, -100]

def getAnAlignCell(x, y, p):
    def f():
        global globalBestCellScore
        global cellMap
        b1, b2 = p
        cell1Id, s1 = yield
        cell2Id, s2 = yield
        cell3Id, s3 = yield
        cellData = [ (cell1Id, s1), (cell2Id, s2), (cell3Id, s3) ]
        cellData.sort( key=lambda x: -x[1] )
        bestCell, bestScore = cellData[0]
        if bestScore &gt; globalBestCellScore[1]:
            globalBestCellScore = [ (x,y), bestScore ]
        if x+1 &lt; len(seq1) and y+1 &lt; len(seq2):
            if b1 == b2:
                cellMap[ (x+1, y+1) ].send( ((x,y), bestScore + matchScore) )
            else:
                cellMap[ (x+1, y+1) ].send( ((x,y), bestScore + mismatchScore) )
        if x+1 &lt; len(seq1):
            cellMap[ (x+1, y) ].send( ((x,y), bestScore + gapScore) )
        if y+1 &lt; len(seq2):
            cellMap[ (x, y+1) ].send( ((x,y), bestScore + gapScore) )

        path = yield
        if bestCell[0] &gt;= 0 and bestCell[1] &gt;=0 :
            if path == None:
                path = []
            path.extend( [ (x,y) ] )

            cellMap[ bestCell ].send(  path   )
        yield path
    return f

for x in range(len(seq1)):
    for y in range(len(seq2)):
        cellMap[ (x,y) ] = getAnAlignCell( x, y, (seq1[x], seq2[y]) )()
        cellMap[ (x,y) ].next()

for x in range(len(seq1)):
    cellMap[ (x,0) ].send( ( (x, -1), 0 ) )
    cellMap[ (x,0) ].send( ( (x-1, -1), 0 ) )

for y in range(len(seq2)):
    if y != 0:
        cellMap[ (0,y) ].send( ( (-1, y), 0 ) )
        cellMap[ (0,y) ].send( ( (-1, y-1), 0 ) )

cellMap[(0,0)].send( ( (-1, -1), 0 ) )

bestCell = globalBestCellScore[0]
bestPath = cellMap[bestCell].next()
bestPath.reverse()

s1 = []
s2 = []
px, py = bestPath[0]
for x,y in bestPath[1:]:
    if x - px != 0:
        s1.append(seq1[px])
    else:
        s1.append(&quot;-&quot;)
    if y - py != 0:
        s2.append(seq21)
    else:
        s2.append(&quot;-&quot;)
    px, py = x, y
print &quot;&quot;.join(s1)
print &quot;&quot;.join(s2)
</pre>
<p>The result seems to be correct</p>
<pre class="brush: plain; title: ; notranslate">
$ python coAlign.py
GTGTAGTTGTGTGAATGTATTT--TT-A
G-GTAGTTGTG-G--TG-ATTTAGTTGA
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blogs.infoecho.net/echo/2011/03/24/yet-another-python-coroutine-fun-stuff/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python Generator Fun</title>
		<link>http://blogs.infoecho.net/echo/2011/03/23/python-generator-fun/</link>
		<comments>http://blogs.infoecho.net/echo/2011/03/23/python-generator-fun/#comments</comments>
		<pubDate>Thu, 24 Mar 2011 04:45:52 +0000</pubDate>
		<dc:creator>Jason Chin</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blogs.infoecho.net/echo/?p=319</guid>
		<description><![CDATA[The following python code generates 100 by 100 = 10,000 generators and use them to simulate 100 step random walk 500 times. Not particular useful thing but it was fun to find out you can simulate random walk differently. I will probably try to write some dynamical programming code using the extensive generator in python [...]]]></description>
			<content:encoded><![CDATA[<p>The following python code generates 100 by 100 = 10,000 generators and use them to simulate 100 step random walk 500 times. Not particular useful thing but it was fun to find out you can simulate random walk differently.  I will probably try to write some dynamical programming code using the extensive generator in python (co-routine like construct) if I find some time to work on it. </p>
<pre class="brush: python; title: ; notranslate">

import random

maxStep = 100
fmap = {}
def getFun(i,j):
    def f():
        path = [(i,j)]
        while 1:
            if i &lt; maxStep - 1:
                path.extend( fmap[ (i+1, j+1) ].next() if random.uniform(0,1) &gt; 0.5 else fmap[ (i+1, j) ].next() )
            yield path
            path = [(i,j)]
    return f

for i in range(maxStep):
    for j in range(maxStep):
        f = getFun(i,j)()
        fmap[ (i,j) ] = f

for i in range(500):
    print i, [ x[1] for x in fmap[ (0,0) ].next() ]
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blogs.infoecho.net/echo/2011/03/23/python-generator-fun/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to save PDF/Images from Mobile Safari to WebDAV compatible iPhone program?</title>
		<link>http://blogs.infoecho.net/echo/2009/06/27/how-to-save-pdfimages-in-directly-lfrom-mobile-safari-to-webdav-compatible-iphone-program/</link>
		<comments>http://blogs.infoecho.net/echo/2009/06/27/how-to-save-pdfimages-in-directly-lfrom-mobile-safari-to-webdav-compatible-iphone-program/#comments</comments>
		<pubDate>Sat, 27 Jun 2009 17:37:43 +0000</pubDate>
		<dc:creator>Jason Chin</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blogs.infoecho.net/echo/2009/06/how-to-save-pdfimages-in-directly-lfrom-mobile-safari-to-webdav-compatible-iphone-program/</guid>
		<description><![CDATA[Unlike most desktop browser, there is no &#8220;download&#8221; option to save an image, a PDF file, etc., from the Mobile Safari so one can read thoses files offline. Well, this is purely a restriction imposed by Apple for security or whatever other stupid reasons. Fortunately, with the most recent update of the operation that support [...]]]></description>
			<content:encoded><![CDATA[<p>Unlike most desktop browser, there is no &#8220;download&#8221; option to save an image, a PDF file, etc., from the Mobile Safari so one can read thoses files offline. Well, this is purely a restriction imposed by Apple for security or whatever other stupid reasons.  Fortunately, with the most recent update of the operation that support cut and paste and some third party WebDAV compatible file viewer, there could have a workaround.</p>
<p>I have recently set up a system such that when I found an interesting URL, I can cut and paste the URL to a widget served from my own host such that it will fetch the resource from the URL, and save it to a location that corresponds to the location of a WebDAV service in my web host account. Then, I can setup the WebDAV compatible file viewer (currently, I am using Air Share Pro) to view the file in my WebDAV direcotry.  Specially, you can also download the files in the WevDAV server locally for future offline reading. This is extreme useful. Once the file is local, you don&#8217;t have to worry if you get wifi, EDGE, or 3G signal, and you might also save some battery power since you don&#8217;t have to reload the files from the network when you read a document on and off.</p>
<p>The main caveat is that you probably need your own web hosting service or iDisk that supports WebDAV. Beside that, you will need to some simple CGI/AJAX programming and spend some money to buy a WebDAV compatible file viewer like Air Share Pro. I won&#8217;t be surprised that one day there will be an iPhone app that implements something like this using a similar idea in a more polished way. Nevertheless, such application is always at Apple&#8217;s mercy. Once Apple opens some policy in the OS allowing sharing data and files between application like an real operation system, all workarounds will be totally useless. Wait, actually, the way I set up getting file to iPhone is not limited for submitting URL from Mobile Safari. Any browser can push the resource from an URL to the WebDAV directory, regradless if an desktop OS supports mounting a WebDAV volumn.  Not bad as an quick method to get some useful document in the Internet from a desktop system to iPhone.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.infoecho.net/echo/2009/06/27/how-to-save-pdfimages-in-directly-lfrom-mobile-safari-to-webdav-compatible-iphone-program/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Safari on iPhone to read CHM file</title>
		<link>http://blogs.infoecho.net/echo/2007/09/23/using-safari-on-iphone-to-read-chm-file/</link>
		<comments>http://blogs.infoecho.net/echo/2007/09/23/using-safari-on-iphone-to-read-chm-file/#comments</comments>
		<pubDate>Mon, 24 Sep 2007 01:55:49 +0000</pubDate>
		<dc:creator>Jason Chin</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blogs.infoecho.net/echo/?p=103</guid>
		<description><![CDATA[iPhone is a fancy toy with a lot of power but Apple deliberately locks a lot of the potential power. One thing I like to do on an iPhone is to be able to read CHM files. As a weekend project, I setup the tool chain for iPhone following the instructions. Then, I grabbed the [...]]]></description>
			<content:encoded><![CDATA[<p>iPhone is a fancy toy with a lot of power but Apple deliberately locks a lot of the potential power. One thing I like to do on an iPhone is to be able to read CHM files. As a weekend project, I setup the tool chain for iPhone following <a href="http://code.google.com/p/iphone-dev/wiki/Building">the instructions</a>. Then, I grabbed the source code of <a href="http://www.jedrea.com/chmlib/"><tt>chmlib</tt></a>. With some minor modification, I was able to compile the <tt>chmlib</tt> as an iPhone binary library. That was very encouraging.</p>
<p>This provides a convenient way to make iPhone as a CHM reader. In the <tt>chmlib</tt> source code distribution, there is an example program that runs as a http-server that serves the content of a CHM as standard web page. The &#8220;mobileSafari&#8221; has no problem to render the results, but the fonts are usually too small to read and the text is typically rendered too wide such that a lot horizontal scrolling becomes annoyingly necessary.</p>
<p>I decided to combine some python code with the <tt>chm_http</tt> server from the <tt>chmlib</tt> source code. I modified the source code of <tt>chm_http</tt> so it can call python code to modify the HTML code in the CHM file, replacing the original CSS with new setting for reading on small screen. Furthermore, I found it was tedious to start the <tt>chm_http</tt> from a terminal every time when you want to read a different book. I wrote another small python script that can scan a directory and find all CHM files in the directory to output an index html page. At the end, I was able to use the mobileSafari pointing to the index page and select the book I want to read. The &#8220;chm_http&#8221; server would start automatically to get the book I like to read.</p>
<p>If you are interested in reading CHM on your iPhone. Get this <a href="http://infoecho.net/files/iphoneCHM.tgz">iphoneCHM.tgz</a> <strike>(the file would be upload soon)</strike>. Copy the &#8220;<tt>chm_http2</tt>&#8220;, &#8220;<tt>rewriteHTML.py</tt>&#8220;, and &#8220;<tt>CHMServer</tt>&#8221; to &#8220;<tt>/usr/local/bin/</tt>&#8221; in your iPhone. Change the permission of these files such that you can run all of them. Put some chm files in <tt>/var/root/Media/CHM_Ebooks/</tt>. Open a terminal in the iPhone or ssh into the iPhone to run &#8220;CHMServer&#8221;. After that, ask the Safari to open this URL <tt>http://127.0.0.1:8000</tt>. You should see the links to the CHM files. You can now click on any of them and enjoy a nice reading time.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.infoecho.net/echo/2007/09/23/using-safari-on-iphone-to-read-chm-file/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>More on google map image tiles</title>
		<link>http://blogs.infoecho.net/echo/2007/02/04/more-on-google-map-image-tiles/</link>
		<comments>http://blogs.infoecho.net/echo/2007/02/04/more-on-google-map-image-tiles/#comments</comments>
		<pubDate>Sun, 04 Feb 2007 21:45:07 +0000</pubDate>
		<dc:creator>Jason Chin</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[map]]></category>

		<guid isPermaLink="false">http://blogs.infoecho.net/echo/?p=90</guid>
		<description><![CDATA[Lat (-90 ~ 90): Lon (-180 ~ 180): Zoom (0-17): GetMap Back no data yet This is another google map hack. I will write some details about this in the furture. In the mean time, you can also check this out. If you are interested in my source code for this hack, you probably know [...]]]></description>
			<content:encoded><![CDATA[<p><script src="http://blogs.infoecho.net/echo/js/gmh2.js"></script></p>
<table border="0">
<tbody>
<tr>
<td>Lat (-90 ~ 90):</td>
<td>
<input id="gmh2_lat" size="16" value="0" /></td>
</tr>
<tr>
<td>Lon (-180 ~ 180):</td>
<td>
<input id="gmh2_lon" size="16" value="0" /></td>
</tr>
<tr>
<td>Zoom (0-17):</td>
<td>
<input id="gmh2_zl" size="16" value="17" /></td>
</tr>
</tbody>
</table>
<p><button onclick="gmh2_getMap()">GetMap</button><br />
<button onclick="gmh2_backALevel()">Back</button></p>
<table border="0">
<tbody>
<tr>
<td><img id="gmh2_sat" onclick="gmh2_zoomIn(event)" onmousemove="gmh2_updateCoor(event);" src="http://kh0.google.com/kh?&amp;v=14&amp;t=t" alt="bless all mighty google" width="256" height="256" /></td>
</tr>
<tr>
<td><img id="gmh2_map" onclick="gmh2_zoomIn(event);" onmousemove="gmh2_updateCoor(event);" src="http://mt0.google.com/mt?v=w2.37&amp;x=0&amp;y=0&amp;zoom=17" alt="bless all mighty google" width="256" height="256" /></td>
</tr>
<tr>
<td colspan="2"><textarea id="gmh2_output" cols="40" rows="5" readonly="readonly">no data yet</textarea></td>
</tr>
</tbody>
</table>
<p>This is another google map hack. I will write some details about this in the furture. In the mean time, you can also check <a href="http://www.codeproject.com/useritems/googlemap.asp">this</a> out. If you are interested in my source code for this hack, you probably know how to get it anyway.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.infoecho.net/echo/2007/02/04/more-on-google-map-image-tiles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

