<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Python on Madprof&#39;s workshop</title>
    <link>http://www.madprof.net/tags/python/</link>
    <description>Recent content in Python on Madprof&#39;s workshop</description>
    <generator>Hugo</generator>
    <language>en</language>
    <lastBuildDate>Thu, 31 Oct 2013 10:59:00 +0000</lastBuildDate>
    <atom:link href="http://www.madprof.net/tags/python/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Merging directories with the magic of Python.</title>
      <link>http://www.madprof.net/2013/10/31/merging-directories-with-the-magic-of-python/</link>
      <pubDate>Thu, 31 Oct 2013 10:59:00 +0000</pubDate>
       <guid isPermaLink="false">https://blog.madprof.net/?p=314</guid> 
      <description>&lt;p&gt;We finally got the last projects out of that monstrosity &amp;lsquo;Final Cut Server&amp;rsquo;, but one project at the end was a nightmare to export, and we weren&amp;rsquo;t sure which files from the end actually were in a different version of the project that we already had.&lt;/p&gt;
&lt;p&gt;We essentially needed to merge two different versions of projects directories, making sure not to lose any files, and we didn&amp;rsquo;t want to lose the organization of the files.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s a quick python script I wipped up to make it quicker.&lt;/p&gt;
&lt;p&gt;With the 4000 odd files in the project, it took under a second to run, and it turned out we only had about 20 files which hadn&amp;rsquo;t already been merged.  Much simpler to sort out.&lt;/p&gt;
&lt;p&gt;The script took about 10 minutes to write and test. This is why you should learn to program.  Hacking stuff like this up is easy, and saves *so* much time.&lt;/p&gt;
&lt;p&gt;(Yes, you probably could do this with a couple of lines of perl or BASH, but what the heck.)&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;background-color:#f0f3f3;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#09f;font-style:italic&#34;&gt;#!/usr/bin/env python&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;from&lt;/span&gt; &lt;span style=&#34;color:#0cf;font-weight:bold&#34;&gt;subprocess&lt;/span&gt; &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;import&lt;/span&gt; Popen, PIPE
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;from&lt;/span&gt; &lt;span style=&#34;color:#0cf;font-weight:bold&#34;&gt;os&lt;/span&gt; &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;import&lt;/span&gt; stat
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;from&lt;/span&gt; &lt;span style=&#34;color:#0cf;font-weight:bold&#34;&gt;os.path&lt;/span&gt; &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;import&lt;/span&gt; basename, abspath
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#c0f&#34;&gt;run&lt;/span&gt;(&lt;span style=&#34;color:#555&#34;&gt;*&lt;/span&gt;command):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    found &lt;span style=&#34;color:#555&#34;&gt;=&lt;/span&gt; Popen(command, stdout&lt;span style=&#34;color:#555&#34;&gt;=&lt;/span&gt;PIPE)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;return&lt;/span&gt; found&lt;span style=&#34;color:#555&#34;&gt;.&lt;/span&gt;communicate()[&lt;span style=&#34;color:#f60&#34;&gt;0&lt;/span&gt;]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;def&lt;/span&gt; &lt;span style=&#34;color:#c0f&#34;&gt;files_in&lt;/span&gt;(dirname):
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;return&lt;/span&gt; [x &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;for&lt;/span&gt; x &lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;in&lt;/span&gt; run(&lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;find&amp;#39;&lt;/span&gt;, abspath(dirname), &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;-type&amp;#39;&lt;/span&gt;,&lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;f&amp;#39;&lt;/span&gt;, &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;-print0&amp;#39;&lt;/span&gt;)&lt;span style=&#34;color:#555&#34;&gt;.&lt;/span&gt;split(&lt;span style=&#34;color:#366&#34;&gt;chr&lt;/span&gt;(&lt;span style=&#34;color:#f60&#34;&gt;0&lt;/span&gt;)) &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;if&lt;/span&gt; x]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;if&lt;/span&gt; &lt;span style=&#34;color:#033&#34;&gt;__name__&lt;/span&gt; &lt;span style=&#34;color:#555&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;__main__&amp;#39;&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;from&lt;/span&gt; &lt;span style=&#34;color:#0cf;font-weight:bold&#34;&gt;sys&lt;/span&gt; &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;import&lt;/span&gt; argv
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;try&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        sourcedir &lt;span style=&#34;color:#555&#34;&gt;=&lt;/span&gt; files_in(argv[&lt;span style=&#34;color:#f60&#34;&gt;1&lt;/span&gt;])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        destdir &lt;span style=&#34;color:#555&#34;&gt;=&lt;/span&gt; files_in(argv[&lt;span style=&#34;color:#f60&#34;&gt;2&lt;/span&gt;])
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;except&lt;/span&gt; &lt;span style=&#34;color:#c00;font-weight:bold&#34;&gt;IndexError&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#366&#34;&gt;print&lt;/span&gt; &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;Usage:&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#366&#34;&gt;print&lt;/span&gt; argv[&lt;span style=&#34;color:#f60&#34;&gt;0&lt;/span&gt;], &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;  &amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#366&#34;&gt;print&lt;/span&gt; &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;Where you want to check if files in  are also in &amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#366&#34;&gt;print&lt;/span&gt; &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;(but perhaps with a different relative path)&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        exit(&lt;span style=&#34;color:#f60&#34;&gt;1&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#366&#34;&gt;print&lt;/span&gt; &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;---------------------------------------------------&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#366&#34;&gt;print&lt;/span&gt; &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#a00&#34;&gt;{0}&lt;/span&gt;&lt;span style=&#34;color:#c30&#34;&gt; files in &lt;/span&gt;&lt;span style=&#34;color:#a00&#34;&gt;{1}&lt;/span&gt;&lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#555&#34;&gt;.&lt;/span&gt;format(&lt;span style=&#34;color:#366&#34;&gt;len&lt;/span&gt;(sourcedir), abspath(argv[&lt;span style=&#34;color:#f60&#34;&gt;1&lt;/span&gt;]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#366&#34;&gt;print&lt;/span&gt; &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#a00&#34;&gt;{0}&lt;/span&gt;&lt;span style=&#34;color:#c30&#34;&gt; files in &lt;/span&gt;&lt;span style=&#34;color:#a00&#34;&gt;{1}&lt;/span&gt;&lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#555&#34;&gt;.&lt;/span&gt;format(&lt;span style=&#34;color:#366&#34;&gt;len&lt;/span&gt;(destdir), abspath(argv[&lt;span style=&#34;color:#f60&#34;&gt;2&lt;/span&gt;]))
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#366&#34;&gt;print&lt;/span&gt; &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;---------------------------------------------------&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    destnames &lt;span style=&#34;color:#555&#34;&gt;=&lt;/span&gt; {}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;for&lt;/span&gt; destfile &lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;in&lt;/span&gt; destdir:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        destnames[basename(destfile)] &lt;span style=&#34;color:#555&#34;&gt;=&lt;/span&gt; {&lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;size&amp;#39;&lt;/span&gt;: stat(destfile)&lt;span style=&#34;color:#555&#34;&gt;.&lt;/span&gt;st_size,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                                         &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;path&amp;#39;&lt;/span&gt;: destfile }
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;for&lt;/span&gt; newfile &lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;in&lt;/span&gt; sourcedir:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        base &lt;span style=&#34;color:#555&#34;&gt;=&lt;/span&gt; basename(newfile)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;if&lt;/span&gt; base &lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;not&lt;/span&gt; &lt;span style=&#34;color:#000;font-weight:bold&#34;&gt;in&lt;/span&gt; destnames:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#366&#34;&gt;print&lt;/span&gt; newfile, &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;is NOT in the new dir&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;else&lt;/span&gt;:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            destfile &lt;span style=&#34;color:#555&#34;&gt;=&lt;/span&gt; destnames[base]
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;            &lt;span style=&#34;color:#069;font-weight:bold&#34;&gt;if&lt;/span&gt; stat(newfile)&lt;span style=&#34;color:#555&#34;&gt;.&lt;/span&gt;st_size &lt;span style=&#34;color:#555&#34;&gt;!=&lt;/span&gt; destfile[&lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;size&amp;#39;&lt;/span&gt;]:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                &lt;span style=&#34;color:#366&#34;&gt;print&lt;/span&gt; &lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#a00&#34;&gt;{0}&lt;/span&gt;&lt;span style=&#34;color:#c30&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color:#a00&#34;&gt;{1}&lt;/span&gt;&lt;span style=&#34;color:#c30&#34;&gt;) differs from &lt;/span&gt;&lt;span style=&#34;color:#a00&#34;&gt;{2}&lt;/span&gt;&lt;span style=&#34;color:#c30&#34;&gt;(&lt;/span&gt;&lt;span style=&#34;color:#a00&#34;&gt;{3}&lt;/span&gt;&lt;span style=&#34;color:#c30&#34;&gt;)&amp;#39;&lt;/span&gt;&lt;span style=&#34;color:#555&#34;&gt;.&lt;/span&gt;format(
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                      newfile, stat(newfile)&lt;span style=&#34;color:#555&#34;&gt;.&lt;/span&gt;st_size,
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;                      destfile[&lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;path&amp;#39;&lt;/span&gt;], destfile[&lt;span style=&#34;color:#c30&#34;&gt;&amp;#39;size&amp;#39;&lt;/span&gt;])
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
    </item>
    <item>
      <title>merging directories with the magic of python</title>
      <link>http://www.madprof.net/posts/merging-directories-with-the-magic-of-python_1/</link>
      <pubDate>Thu, 31 Oct 2013 05:34:54 +0100</pubDate>
       <guid isPermaLink="false">http://www.madprof.net/posts/merging-directories-with-the-magic-of-python_1/</guid> 
      <description>&lt;p&gt;We finally got the last projects out of that monstrosity &amp;lsquo;Final Cut Server&amp;rsquo;, but one project at the end was a nightmare to export, and we weren&amp;rsquo;t sure which files from the end actually were in a different version of the project that we already had.&lt;/p&gt;
&lt;p&gt;We essentially needed to merge two different versions of projects directories, making sure not to lose any files, and we didn&amp;rsquo;t want to lose the organization of the files.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s a quick python script I wipped up to make it quicker.&lt;/p&gt;
&lt;p&gt;With the 4000 odd files in the project, it took under a second to run, and it turned out we only had about 20 files which hadn&amp;rsquo;t already been merged.  Much simpler to sort out.&lt;/p&gt;
&lt;p&gt;The script took about 10 minutes to write and test. This is why you should learn to program.  Hacking stuff like this up is easy, and saves &lt;em&gt;so&lt;/em&gt; much time.&lt;/p&gt;
&lt;p&gt;(Yes, you probably could do this with a couple of lines of perl or BASH, but what the heck.)&lt;/p&gt;
&lt;!-- raw HTML omitted --&gt;</description>
    </item>
    <item>
      <title>python string concatination speed</title>
      <link>http://www.madprof.net/posts/python-string-concatination-speed_1/</link>
      <pubDate>Tue, 27 Mar 2012 00:00:00 +0000</pubDate>
       <guid isPermaLink="false">http://www.madprof.net/posts/python-string-concatination-speed_1/</guid> 
      <description>&lt;p&gt;I made a quick python script to convert OpenLP song (lyric) databases into the presentation format used by ProPresenter.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://github.com/danthedeckie/OpenLP-To-ProPresenter5-Converter&#34;&gt;https://github.com/danthedeckie/OpenLP-To-ProPresenter5-Converter&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;is the link.  I put it together in a few hours, it should have been quicker, but I&amp;rsquo;m still re-acquainting myself with python.&lt;/p&gt;
&lt;p&gt;It is a nice language.&lt;/p&gt;
&lt;p&gt;One thing I did today, while cleaning up a bit, was wonder about something I remember from doing python years ago - string concatenation.  Joining two strings (texts) together.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;quot;Hello&amp;quot; + &amp;quot;World&amp;quot; -&amp;gt; &amp;quot;HelloWorld&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I remembered something about it being slow, and python recommending using&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&#39;&#39;.join((&amp;quot;Hello&amp;quot;, &amp;quot;World&amp;quot;))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;which seems to me one of the most blatently ugly obscure gotchas I&amp;rsquo;ve come across in a long while.&lt;/p&gt;
&lt;p&gt;Anyway, I refactored my code into that style - converting databases and song lyrics and writing XML stuff is pretty much all Text formatting and concatination.&lt;/p&gt;
&lt;p&gt;It made no discernable difference.  So I went back to normal easier to read &lt;code&gt;x += y&lt;/code&gt;, and &lt;code&gt;x = y + z&lt;/code&gt; type of code.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
