<?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>pko.ch &#187; python</title>
	<atom:link href="http://pko.ch/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://pko.ch</link>
	<description>Reflections about reflection</description>
	<lastBuildDate>Sat, 28 Mar 2009 16:23:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Taming KbdMgr.exe</title>
		<link>http://pko.ch/2009/03/28/taming-kbdmgrexe/</link>
		<comments>http://pko.ch/2009/03/28/taming-kbdmgrexe/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 15:54:56 +0000</pubDate>
		<dc:creator>pkoch</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[troubleshooting]]></category>

		<guid isPermaLink="false">http://pko.ch/?p=30</guid>
		<description><![CDATA[I have a bootcamp'ed Macbook Pro. As many people, I experienced some lagging sound and graphics from the computer, apparently at random. I googled it a bit, and found DPCs were the cause of it. And the cause DPCs were so slow were some drivers and <code>KbdMgr.exe</code>, a small app that listens to the hardware keys to raise brightness and stuff. I updated the drivers, and the lag dropped a bit, but not that much. Then I turned to <code>KbdMgr.exe</code>.  And python popped into my mind. After some 15 minutes of research, I found <a href="http://pypi.python.org/pypi/pywin32/210">win32all</a>. And from there, It was just remembering all of the win32 I had forgotten. I eventually churned out some code to scratch my itch.]]></description>
			<content:encoded><![CDATA[<p>I have a bootcamp&#8217;ed Macbook Pro. As many people, I experienced some lagging sound and graphics from the computer, apparently at random. I googled it a bit, and found DPCs were the cause of it. And the cause DPCs were so slow were some drivers and <code>KbdMgr.exe</code>, a small app that listens to the hardware keys to raise brightness and stuff.</p>
<p>I updated the drivers, and the lag dropped a bit, but not that much. Then I turned to <code>KbdMgr.exe</code>. I read that a quick fix was to set the affinity to the second core and set it to minimum priority. And so I did, and it worked! However, It was just a pain in the butt to do that dance on each boot. I thought of doing some wrapper <code>exe</code> for <code>KbdMgr.exe</code>, but just the thought of downloading Visual Studio and all the SDKs gave me nauseas. And python popped into my mind.</p>
<p>After some 15 minutes of research, I found <a href="http://pypi.python.org/pypi/pywin32/210">win32all</a>. And from there, It was just remembering all of the win32 I had forgotten. I eventually churned out some code to scratch my itch.</p>
<p><code></p>
<pre>
import win32api
import win32com.client
import win32process
import win32con
import time
import sys, traceback

class ProcessSeekerThrottler(object):

    def __init__(self):
        self.coup_de_grace = self.throttle_and_set_affinity
        self.backoff_interval = 1.0

    def throttle_and_set_affinity(self,pid):
        proc = win32api.OpenProcess(win32con.PROCESS_SET_INFORMATION, 0, pid)

        win32process.SetProcessAffinityMask(proc,0x02)
        win32process.SetPriorityClass(proc,win32process.IDLE_PRIORITY_CLASS)

        win32api.CloseHandle(proc)

    def pids_for(self,process_name):
        self.WMI = win32com.client.GetObject('winmgmts:')
        results = self.WMI.ExecQuery('select * from Win32_Process where Name="%s"'%(process_name,))
        if len(results) < 1:
            raise Exception("Not found: %s"%(process_name,))
        else:
            return [result.Properties_('ProcessId').Value for result in results]

    def hunt_pid_for(self,process_name):
        done = False
        while not done:
            try:
                [self.coup_de_grace(pid) for pid in self.pids_for(process_name)]
                print "Killed!"
                done = True
            except Exception as e:
                print >> sys.stderr, "-"*60
                traceback.print_exc(None,sys.stderr)
                print >> sys.stderr, "Backing off."
                time.sleep(self.backoff_interval)

if __name__ == '__main__':
    try:
        ProcessSeekerThrottler().hunt_pid_for("KbdMgr.exe")
    except Exception as e:
        traceback.print_exc(None,sys.stderr)
        raw_input()
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://pko.ch/2009/03/28/taming-kbdmgrexe/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>There&#8217;s no silver bullet, but there&#8217;s a silver DSCM. And it&#8217;s quick.</title>
		<link>http://pko.ch/2008/08/23/theres-no-silver-bullet-but-theres-a-silver-dscm-and-its-quick/</link>
		<comments>http://pko.ch/2008/08/23/theres-no-silver-bullet-but-theres-a-silver-dscm-and-its-quick/#comments</comments>
		<pubDate>Sat, 23 Aug 2008 01:59:23 +0000</pubDate>
		<dc:creator>pkoch</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Mercurial]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://pko.ch/?p=5</guid>
		<description><![CDATA[Pre scriptum: This was dangling in my drafts forever. I just decided to post it as it is. In this post: Why I use Mercurial (sometimes abbreviated as hg) but not Git, and why I like it. One day, some moons ago, I watched a presentation with Linus on Git, and how it would forever [...]]]></description>
			<content:encoded><![CDATA[<p><em>Pre scriptum</em>: This was dangling in my drafts forever. I just decided to post it as it is.</p>
<p>In this post: Why I use <a href="http://www.selenic.com/mercurial/wiki/">Mercurial</a> (sometimes abbreviated as hg) but not <a href="http://git.or.cz/">Git</a>, and why I like it.</p>
<p>One day, some moons ago, I watched a <a href="http://www.youtube.com/watch?v=4XpnKHJAok8">presentation with Linus on Git</a>, and how it would forever change our life. He explained why it&#8217;s a must-use on a very big distributed team or/and at a personal level. I mostly agreed. I still thought this was just a clever hack to solve merge conflits on svn and keep changes controlled on a mental level. CVS is dead, SVN is not. For most small teams, SVN is just a shortcut for the common workflow one would do with git, except for one little thing: you commit locally, and publish as needed.</p>
<p>Mercurial, or any DSCM, is great for detaching the act of committing work (accepting it as a valid step towards a goal) from the act of publishing work (putting your modifications up for other parties).</p>
<p>I prefer Mercurial over Git for the polish and supporting the anti-&#8221;linus is god&#8221; movement. I really didn&#8217;t like the way I was told he dismissed the idea of using incremental logs. I&#8217;m not being an extremist, or i would use <a href="http://codeville.org/">Codeville</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://pko.ch/2008/08/23/theres-no-silver-bullet-but-theres-a-silver-dscm-and-its-quick/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Memoization in Python: easier than what it should be</title>
		<link>http://pko.ch/2008/08/22/memoization-in-python-easier-than-what-it-should-be/</link>
		<comments>http://pko.ch/2008/08/22/memoization-in-python-easier-than-what-it-should-be/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 10:14:52 +0000</pubDate>
		<dc:creator>pkoch</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://pko.ch/?p=15</guid>
		<description><![CDATA[I was writing some DB-access-intensive Python application and felt the need to cache function results. After fiddling with some dictionaries, I felt there was some underlying pattern I wasn&#8217;t spotting. Then it hit me. I was just memoizing function calls. So I came up with this decorator. import functools import cPickle def memoize(fctn): memory = [...]]]></description>
			<content:encoded><![CDATA[<p>I was writing some DB-access-intensive Python application and felt the need to cache function results. After fiddling with some dictionaries, I felt there was some underlying pattern I wasn&#8217;t spotting. Then it hit me. I was just memoizing function calls.</p>
<p>So I came up with this decorator.<br />
<code></p>
<pre>
import functools
import cPickle
def memoize(fctn):
        memory = {}
        @functools.wraps(fctn)
        def memo(*args,**kwargs):
                haxh = cPickle.dumps((args, sorted(kwargs.iteritems())))

                if haxh not in memory:
                        memory[haxh] = fctn(*args,**kwargs)

                return memory[haxh]
        if memo.__doc__:
            memo.__doc__ = "\n".join([memo.__doc__,"This function is memoized."])
        return memo
</pre>
<p></code></p>
<p>Python is <strong>really easy</strong> to write.</p>
<p><strong>23 August 2008 &#8211; Update:</strong> Corrected the bug pointed out by John.<br />
<strong>25 August 2008 &#8211; Update:</strong> Improved as per <a href="http://www.reddit.com/comments/6xj70/memoization_in_python_easier_than_what_it_should/">reddit comments</a>&#8216;s suggestions.</p>
]]></content:encoded>
			<wfw:commentRss>http://pko.ch/2008/08/22/memoization-in-python-easier-than-what-it-should-be/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Moar python on Bluehost</title>
		<link>http://pko.ch/2008/05/08/moar-python-on-bluehost/</link>
		<comments>http://pko.ch/2008/05/08/moar-python-on-bluehost/#comments</comments>
		<pubDate>Thu, 08 May 2008 23:26:27 +0000</pubDate>
		<dc:creator>pkoch</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Mercurial]]></category>
		<category><![CDATA[bluehost]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[troubleshooting]]></category>

		<guid isPermaLink="false">http://pko.ch/?p=4</guid>
		<description><![CDATA[It all began with me wanting Mergurial (hg for short) on Bluehost. I find mercurial as good as git but with more polish. I just followed some tutorial to get hg on Bluehost, and it was good. However, I longed for more. Pylons more. It&#8217;s a lot of packages to install, so I really didn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>It all began with me wanting <a href="http://www.selenic.com/mercurial/">Mergurial</a> (hg for short) on <a href="http://www.bluehost.com/">Bluehost</a>. I find mercurial as good as <a href="http://git.or.cz/">git</a> but with more polish. I just followed <a href="http://www.blusb.eu/blog/2007/09/17/mercurial-scm-in-shared-hosts-bluehostcom/">some tutorial to get hg on Bluehost</a>, and it was good.</p>
<p>However, I longed for more. <a href="http://pylonshq.com/">Pylons</a> more. It&#8217;s a lot of packages to install, so I really didn&#8217;t want to do it by hand. If I resign to do it this time, it will happen on my next coding whim too. I tried to use <a href="http://peak.telecommunity.com/dist/ez_setup.py">ez_setup</a>, but it would screw up with my prefix. Tried virtual python from the <a href="http://peak.telecommunity.com/DevCenter/EasyInstall">easy_install  guys</a>, no go either. Tried messing with PYTHONPATH, PATH and the like, but it became really ugly really fast.</p>
<p>Bluehost&#8217;s python is very old. Has a big beard and all. Bluehost already forces me to use cgi after all, so i get to choose who runs the script. It might as well be my own version of python. And so it was! Grabbed the source, built it with a prefix and, lo and behold, I had a shiny python working. That simple. The night before it was configuration madness. This time, <code>./configure --PREFIX=$HOME/local &#038;&#038; make &#038;&#038; make install</code> or some cash equivalent operation. All went good from there.</p>
<p>Lesson learned: In shared hosting, when forced to use cgi, <code>./configure --PREFIX=$HOME/local</code> goes a long way. Use it!</p>
]]></content:encoded>
			<wfw:commentRss>http://pko.ch/2008/05/08/moar-python-on-bluehost/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
