<?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>alan.lamielle.net &#187; code</title>
	<atom:link href="http://alan.lamielle.net/tags/code/feed" rel="self" type="application/rss+xml" />
	<link>http://alan.lamielle.net</link>
	<description>Alan LaMielle</description>
	<lastBuildDate>Fri, 16 Sep 2011 16:50:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Reversing Python list/string objects</title>
		<link>http://alan.lamielle.net/2009/05/04/reversing-python-liststring-objects</link>
		<comments>http://alan.lamielle.net/2009/05/04/reversing-python-liststring-objects#comments</comments>
		<pubDate>Mon, 04 May 2009 23:26:00 +0000</pubDate>
		<dc:creator>Alan LaMielle</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Tech]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[reverse]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[timing]]></category>

		<guid isPermaLink="false">http://alan.lamielle.net/?p=69</guid>
		<description><![CDATA[I was interested in the relative performance of reversing a python list using the idom list[::-1] rather than the reverse method of the list class. Additionally I wanted to see if this idom was efficient for reversing strings compared to converting to a list, reversing that list, and joining the elements again using ''.join(reversed_list). Here [...]]]></description>
			<content:encoded><![CDATA[<p>I was interested in the relative performance of reversing a python list using the idom <code>list[::-1]</code> rather than the reverse method of the list class.  Additionally I wanted to see if this idom was efficient for reversing strings compared to converting to a list, reversing that list, and joining the elements again using <code>''.join(reversed_list)</code>.  Here are the results:</p>
<p>Reverse list, slicing: (0.58000000000000007, 0.64144396781921387)<br />
Reverse list, method: (0.20000000000000018, 0.19551301002502441)<br />
Reverse string, slicing: (0.45000000000000001, 0.49545693397521973)<br />
Reverse string, list: (3.71, 3.7981550693511963)</p>
<p><span id="more-69"></span>I’m using a quick Timer class that I implemented to report the running time for a chunk of code using both of <code>time.clock()</code> (the first item) and <code>time.time()</code> (the second item).  Here is the code used to produce these results:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">collections</span> <span style="color: #ff7700;font-weight:bold;">import</span> namedtuple
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">copy</span> <span style="color: #ff7700;font-weight:bold;">import</span> deepcopy
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Timer<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
   Times=namedtuple<span style="color: black;">&#40;</span><span style="color: #483d8b;">'times'</span>,<span style="color: #483d8b;">'clock time'</span><span style="color: black;">&#41;</span>
&nbsp;
   <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
      <span style="color: #008000;">self</span>.<span style="color: black;">running</span>=<span style="color: #008000;">False</span>
&nbsp;
   <span style="color: #ff7700;font-weight:bold;">def</span> _set_times<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,field_name<span style="color: black;">&#41;</span>:
      times=Timer.<span style="color: black;">Times</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">time</span>.<span style="color: black;">clock</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,<span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
      <span style="color: #008000;">setattr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,field_name,times<span style="color: black;">&#41;</span>
&nbsp;
   <span style="color: #ff7700;font-weight:bold;">def</span> start<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">running</span>: <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">ValueError</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Timer is already running!'</span><span style="color: black;">&#41;</span>
      <span style="color: #008000;">self</span>.<span style="color: black;">running</span>=<span style="color: #008000;">True</span>
      <span style="color: #008000;">self</span>._set_times<span style="color: black;">&#40;</span><span style="color: #483d8b;">'tstart'</span><span style="color: black;">&#41;</span>
   <span style="color: #ff7700;font-weight:bold;">def</span> stop<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">running</span>: <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">ValueError</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Timer is not running!'</span><span style="color: black;">&#41;</span>
      <span style="color: #008000;">self</span>.<span style="color: black;">running</span>=<span style="color: #008000;">False</span>
      <span style="color: #008000;">self</span>._set_times<span style="color: black;">&#40;</span><span style="color: #483d8b;">'tstop'</span><span style="color: black;">&#41;</span>
&nbsp;
   <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">tstop</span>.<span style="color: black;">clock</span>-<span style="color: #008000;">self</span>.<span style="color: black;">tstart</span>.<span style="color: black;">clock</span>,<span style="color: #008000;">self</span>.<span style="color: black;">tstop</span>.<span style="color: black;">time</span>-<span style="color: #008000;">self</span>.<span style="color: black;">tstart</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#41;</span>
&nbsp;
timer=Timer<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
test_string=<span style="color: #483d8b;">'Time flies like an arrow.  Fruit flies like a banana.'</span>
test_list=<span style="color: #008000;">list</span><span style="color: black;">&#40;</span>test_string<span style="color: black;">&#41;</span>
iterations=<span style="color: #ff4500;">1000000</span>
&nbsp;
timer.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>iterations<span style="color: black;">&#41;</span>:
   new_string=test_string<span style="color: black;">&#91;</span>::-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
timer.<span style="color: black;">stop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Reverse string, slicing:'</span>,timer.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
timer.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>iterations<span style="color: black;">&#41;</span>:
   l=<span style="color: #008000;">list</span><span style="color: black;">&#40;</span>test_string<span style="color: black;">&#41;</span>
   l.<span style="color: black;">reverse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
   new_string=<span style="color: #483d8b;">''</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>l<span style="color: black;">&#41;</span>
timer.<span style="color: black;">stop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Reverse string, list:'</span>,timer.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
timer.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>iterations<span style="color: black;">&#41;</span>:
   test_list=test_list<span style="color: black;">&#91;</span>::-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
timer.<span style="color: black;">stop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Reverse list, slicing:'</span>,timer.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
timer.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>iterations<span style="color: black;">&#41;</span>:
   test_list.<span style="color: black;">reverse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
timer.<span style="color: black;">stop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Reverse list, method:'</span>,timer.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Feel free to use any of this code yourself, consider it under a <span class="caps">BSD</span> license.  Particularly the Timer class has been useful for me in multiple cases.</p>
<p>Based on the timing results, I’d say it looks best to use the slicing idiom on strings and the reverse method on lists.</p>
]]></content:encoded>
			<wfw:commentRss>http://alan.lamielle.net/2009/05/04/reversing-python-liststring-objects/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Environment Variables and GNU Screen</title>
		<link>http://alan.lamielle.net/2009/03/09/environment-variables-and-gnu-screen</link>
		<comments>http://alan.lamielle.net/2009/03/09/environment-variables-and-gnu-screen#comments</comments>
		<pubDate>Mon, 09 Mar 2009 19:32:00 +0000</pubDate>
		<dc:creator>Alan LaMielle</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[screen]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[X]]></category>
		<category><![CDATA[Xorg]]></category>
		<category><![CDATA[zsh]]></category>

		<guid isPermaLink="false">http://alan.lamielle.net/?p=80</guid>
		<description><![CDATA[I am an avid user of GNU screen.  However, one problem I’ve run into lately in Ubuntu involves the interaction of persistent screen sessions and less-than-persistent X sessions.  I’ve found that after starting a new X session, new environment variables are set (such as DISPLAY, SSH_AGENT_PID, and SSH_AUTH_SOCK).  This causes an incosistency between my X [...]]]></description>
			<content:encoded><![CDATA[<p>I am an avid user of <span class="caps">GNU</span> screen.  However, one problem I’ve run into lately in Ubuntu involves the interaction of persistent screen sessions and less-than-persistent X sessions.  I’ve found that after starting a new X session, new environment variables are set (such as <span class="caps">DISPLAY</span>, SSH_AGENT_PID, and <span class="caps">SSH</span>_AUTH_SOCK).  This causes an incosistency between my X session’s variables and those that exist in my persistent screen session.  This post discusses my solution to this issue.</p>
<p>I’m currently running the currently unreleased Ubuntu Jaunty on virtually all of my machines.  At times, such as when resuming my notebook, I find that my X session has died.  This is not that much of an inconvenience for me—I do most of my work within a screen session that exists as long as the machine stays running, regardless of what X decides to do.  However, services such as ssh-agent must be restarted when creating a new X session and along with this restart comes a new set values for <span class="caps">SSH</span>_AGENT_PID and <span class="caps">SSH</span>_AUTH_SOCK.  In addition, it is possible that <span class="caps">DISPLAY</span> may change as well.</p>
<p>While these variables have changed, the screen sessions values have not been updated.  So when I re-attach to the running screen session and attempt to use ssh, I find that rather than using the running ssh-agent process to cache my passphrase, I am prompted in my terminal to enter the passphrase.  If I were able to use the updated environment variables I could use the ssh-agent process and all would be fine.</p>
<p>As I normally do, I googled for this issue and ran across <a href="http://samrowe.com/wordpress/ssh-agent-and-gnu-screen/">this solution</a> to the problem.  Rather than reiterate the details presented in this post, I simply show the two zsh functions I’ve written, as they differ sightly from the afore mentioned post:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">grabvars<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #007800;">VARS</span>=<span style="color: #ff0000;">&quot;SSH_AGENT_PID SSH_AUTH_SOCK DISPLAY&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#For each variable in $VARS</span>
<span style="color: #000000; font-weight: bold;">for</span> x <span style="color: #000000; font-weight: bold;">in</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$VARS</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> ; <span style="color: #000000; font-weight: bold;">do</span>
<span style="color: #7a0874; font-weight: bold;">eval</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$x</span>=\$<span style="color: #007800;">$x</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span>  <span style="color: #ff0000;">'s/=/=&quot;/
s/$/&quot;/
s/^/export /'</span>
<span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #007800;">$VARSLOC</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
sourcevars<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$VARSLOC</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
<span style="color: #7a0874; font-weight: bold;">source</span> <span style="color: #007800;">$VARSLOC</span>
<span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>Note that <span class="caps">VARSLOC</span> names the file that the export statements should be written to.</p>
<p>How do I use these two functions?  sourcevars is called every time a new shell is created (I’ve simply added a call to this function in ~/.zshrc).  grabvars is only called when I create or attach to a screen session, so I’ve aliased ‘screen’ to be ‘grabvars; screen’.  So now when I first login and create a new terminal, this terminal has the updated environment vars.  I do a ‘screen -DR session_name’ to attach, which then calls grabvars, the correct values are written to <span class="caps">VARSLOC</span>, and my screen session is attached.  If I create a new window in my screen session, the correct variables are now set.  If I want the updated variables in an exsiting window, I simply call ‘sourcevars’.</p>
]]></content:encoded>
			<wfw:commentRss>http://alan.lamielle.net/2009/03/09/environment-variables-and-gnu-screen/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

