<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-8745390009765826493</atom:id><lastBuildDate>Sat, 21 Sep 2024 20:28:35 +0000</lastBuildDate><category>python</category><category>command line</category><category>console</category><category>programming</category><category>windows</category><category>time management</category><category>DOS</category><category>KPI</category><category>batch files</category><category>code</category><category>legends</category><category>linux</category><category>menu</category><category>plotting</category><category>pylab</category><category>string input</category><category>user interface</category><title>Cross-Platform Command Line Tools</title><description></description><link>http://portablecommandline.blogspot.com/</link><managingEditor>noreply@blogger.com (Unknown)</managingEditor><generator>Blogger</generator><openSearch:totalResults>12</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8745390009765826493.post-7844192951995706939</guid><pubDate>Tue, 23 Jun 2009 20:28:00 +0000</pubDate><atom:updated>2009-06-24T00:45:21.832-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">legends</category><category domain="http://www.blogger.com/atom/ns#">plotting</category><category domain="http://www.blogger.com/atom/ns#">pylab</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>Transparent legend in your plots</title><description>Have you ever made a plot with several data series together and found that there is no way to put an explanatory legend onto the plot without hiding crucial data points? One answer to this problem can be to overlay a transparent legend. There are two routes to this &quot;graphical goodness&quot; that I would immediately think about:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Create an image file from your plot and then &quot;photoshop&quot; it (or gimp it...)&lt;/li&gt;&lt;li&gt;Add the alpha channel directly in the plotting tool...&lt;/li&gt;&lt;/ul&gt;Matlab can possibly do this, I really don&#39;t know. Here&#39;s how to do it in python. Say you have some data you want to plot, for example the sine and cosine function. Then you&#39;d overlay a transparent key to your data. Let&#39;s dive in..&lt;br /&gt;&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-JYZ16qOzzu0KDGPJG_dl4k1dbXbtQ_f_ksHTCmiucoIqQ5XsQW-D4BmJfUiUrOFvNd2KhYKoE2gZg5ZnxlMWRST07th1cLW6t0gwBtoCwDdASn_iINRh4EIw2SlE38sNVnO1fwilayN4/s1600-h/origplotpng.png&quot;&gt;&lt;img style=&quot;margin: 0pt 0pt 10px 10px; float: right; cursor: pointer; width: 320px; height: 241px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-JYZ16qOzzu0KDGPJG_dl4k1dbXbtQ_f_ksHTCmiucoIqQ5XsQW-D4BmJfUiUrOFvNd2KhYKoE2gZg5ZnxlMWRST07th1cLW6t0gwBtoCwDdASn_iINRh4EIw2SlE38sNVnO1fwilayN4/s320/origplotpng.png&quot; alt=&quot;&quot; id=&quot;BLOGGER_PHOTO_ID_5350626182995841234&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style=&quot;font-family:courier new;&quot;&gt;from pylab import *&lt;/span&gt; &lt;span style=&quot;font-family:courier new;&quot;&gt;t = linspace(-2*pi,2*pi)&lt;/span&gt; &lt;span style=&quot;font-family:courier new;&quot;&gt;y1=cos(t&lt;/span&gt;&lt;span style=&quot;font-family:courier new;&quot;&gt;); y2=sin(t);&lt;/span&gt; &lt;span style=&quot;font-family:courier new;&quot;&gt;plot(t,y1,label=&#39;Cosine&#39;)&lt;/span&gt; &lt;span style=&quot;font-family:courier new;&quot;&gt;plot(t,y2,label=&#39;Sine&#39;)&lt;/span&gt; &lt;span style=&quot;font-family:courier new;&quot;&gt;xlabel(&#39;T&lt;/span&gt;&lt;span style=&quot;font-family:courier new;&quot;&gt;ime&#39;)&lt;/span&gt; &lt;span style=&quot;font-family:courier new;&quot;&gt;ylabel(&#39;y&#39;)&lt;br /&gt;axis([-2*pi,2*pi,-1,1])&lt;br /&gt;leg=legend()&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family:courier new;&quot;&gt;savefig(&#39;namehere.png&#39;)&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-family:courier new;&quot;&gt;close&lt;/span&gt;&lt;span style=&quot;font-family:courier new;&quot;&gt;()&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now we see from the resulting plot that the legend covers some of the graph. Now, before the &quot;savefig&quot; command, add the following:&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3BDmG9CtdVt2AzWwJiwFTclh4CrHbRSBMS-mtenF4ioD_2BehYYpHTZw6FNuEDKEs5e0dpJdf244OXEVm9AuH9qm0VRVzRPwDgxsN-zLT8iHfJltkune29C849AdayaKigN13Bq1pXbPQ/s1600-h/alphaplotpng.png&quot;&gt;&lt;img style=&quot;margin: 0pt 0pt 10px 10px; float: right; cursor: pointer; width: 320px; height: 241px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3BDmG9CtdVt2AzWwJiwFTclh4CrHbRSBMS-mtenF4ioD_2BehYYpHTZw6FNuEDKEs5e0dpJdf244OXEVm9AuH9qm0VRVzRPwDgxsN-zLT8iHfJltkune29C849AdayaKigN13Bq1pXbPQ/s320/alphaplotpng.png&quot; alt=&quot;&quot; id=&quot;BLOGGER_PHOTO_ID_5350627130502142018&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style=&quot;font-family:courier new;&quot;&gt;frame=leg.get_frame()&lt;/span&gt; &lt;span style=&quot;font-family:courier new;&quot;&gt;&lt;br /&gt;frame.set_alpha(0.4)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The resulting plot is shown below the original. Better, don&#39;t you think? :-)&lt;br /&gt;&lt;br /&gt;PS! Click on the plots to show larger versions!</description><link>http://portablecommandline.blogspot.com/2009/06/transparent-legend-in-your-plots.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-JYZ16qOzzu0KDGPJG_dl4k1dbXbtQ_f_ksHTCmiucoIqQ5XsQW-D4BmJfUiUrOFvNd2KhYKoE2gZg5ZnxlMWRST07th1cLW6t0gwBtoCwDdASn_iINRh4EIw2SlE38sNVnO1fwilayN4/s72-c/origplotpng.png" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8745390009765826493.post-3265594390564466946</guid><pubDate>Mon, 15 Jun 2009 07:47:00 +0000</pubDate><atom:updated>2009-06-15T00:48:42.201-07:00</atom:updated><title>Coding is better with java ... the coffee, that is.</title><description>&lt;span style=&quot;font-family: &#39;Times New Roman&#39;;&quot;&gt;Ah... a non-tech, short post. This morning I started implementing some numerical experiments. Sitting in my favorite coffee shop drinking the best brew I know - that I call perfect conditions for coding. And, as always, Python is your friend (Java is a coffee type, not a programming language... ).&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;So what did I do in the coffee shop? I wrote a small reporting tool to compute total hours spent on different projects as registered in the &lt;a href=&quot;http://portablecommandline.blogspot.com/2009/05/time-registering-command-line-tool-in.html&quot;&gt;timereg&lt;/a&gt; tool I described previously. The tool is very simple; I just read the timereg log into a Python list using the &lt;i&gt;readlines&lt;/i&gt; command and exploit the structure. I make two lists; one for project names and one for hours spent. Each time a project name is encountered that I haven&#39;t seen before when iterating over the readlines list, I add it and also add the number of hours spent in the first instance of this project. Then, just keep adding for each project:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;Courier New&#39;;&quot;&gt;for k in readlineslist:&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;Courier New&#39;;&quot;&gt;   if k[0] in names:&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;Courier New&#39;;&quot;&gt;      for z in range(0,names):&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;Courier New&#39;;&quot;&gt;         if readlineslist[z]==k[0]:&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;Courier New&#39;;&quot;&gt;            timelist[z]+=k[-1]&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;Courier New&#39;;&quot;&gt;....&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Get the picture? Of course I write off everything to a text file for billing purposes.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Well, time to hit the road to the real office. Enjoy your day and happy pythoneering!&lt;/div&gt;&lt;/span&gt;</description><link>http://portablecommandline.blogspot.com/2009/06/coding-is-better-with-java-coffee-that.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8745390009765826493.post-4524845840968762616</guid><pubDate>Sat, 13 Jun 2009 22:41:00 +0000</pubDate><atom:updated>2009-06-13T16:46:09.998-07:00</atom:updated><title>Automate text processing with string methods</title><description>Have you ever had the need to reorganize a text file? Say you have some file that is organized in the following way:&lt;br /&gt;&lt;br /&gt;Variable1&lt;br /&gt;20&lt;br /&gt;Variable2&lt;br /&gt;30&lt;br /&gt;....&lt;br /&gt;&lt;br /&gt;Say you would like to reorganize that into something more spreadsheet-like, such as&lt;br /&gt;&lt;br /&gt;Variable1    20&lt;br /&gt;Variable2    30&lt;br /&gt;---&lt;br /&gt;&lt;br /&gt;Easy enough by hand in Excel or OOo calc if there are a couple of values. What if your list contains 20.000 entries? Then a script is your hero. Let&#39;s make a command line tool to reorganize such files using Python. We want the script to work like this on the command line:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family:Courier New;&quot;&gt;&gt;&gt; reorganize inputfile.txt outputfile.txt&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;Therefore, we need to import the sys.argv list (containing command line arguments). Next, we need to use file methods to read the contents of file A into a list that we can operate on. The start of our script is thus:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family:Courier New;&quot;&gt;#!/usr/bin/env python&lt;br /&gt;&lt;br /&gt;from sys import argv as filenames&lt;br /&gt;&lt;br /&gt;inputfile=open(str(filenames[1]),&#39;r&#39;)&lt;br /&gt;inputlist=inputfile.readlines()&lt;br /&gt;inputfile.close()&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;Now we have a Python list like this:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family:Courier New;&quot;&gt;inputlist=[&#39;Variable1\n&#39;,&#39;20\n&#39;,&#39;Variable2\n&#39;,&#39;30\n&#39;]&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;where the &#39;\n&#39; thing means line break. We cannot just write this directly to a new file, we need to strip the &#39;\n&#39; from list elements with even index numbers (indexing starting at zero) and we need to add &#39;\t&#39; to the same list elements to get a tab-separated file easily. An easy way to do this is the following; make two lists, one holding the &quot;names&quot; and one holding the &quot;values&quot;. While making these lists, remove all line breaks using the replace method of string objects and replace them with tab characters (&#39;\t&#39;). The code:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family:Courier New;&quot;&gt;ColA=[]; ColB=[];&lt;br /&gt;for k in range(0,len(inputlist)/2):&lt;br /&gt;   ColA.append(inputlist[2*k].replace(&#39;\n&#39;,&#39;\t&#39;)&lt;br /&gt;   ColB.append(inputlist[2*k+1]&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;Then, simply write to the output file:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family:Courier New;&quot;&gt;outputfile=open(filenames[2],&#39;w&#39;)&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family:Courier New;&quot;&gt;for k in range(0,len(ColA)):&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family:Courier New;&quot;&gt;        outputfile.write(ColA[k]+ColB[k])&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family:Courier New;&quot;&gt;outputfile.close()&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;Putting these commands together yields a script that gives you the desired result... (This is a tool I use every day - text processing is so much simpler for a scripter than for the average Excel user). Note that it is possible to do this directly in a spreadsheet using macros. I might consider writing the solution to that one day to. Or not.. :-)</description><link>http://portablecommandline.blogspot.com/2009/06/automate-text-processing-with-string.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8745390009765826493.post-2301064392093937489</guid><pubDate>Fri, 05 Jun 2009 21:04:00 +0000</pubDate><atom:updated>2009-06-05T14:17:49.849-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">console</category><category domain="http://www.blogger.com/atom/ns#">KPI</category><category domain="http://www.blogger.com/atom/ns#">programming</category><category domain="http://www.blogger.com/atom/ns#">string input</category><title>regKPI: register a KPI value in a database or text file</title><description>Some days ago I wrote a post on a simple console program. One of the sub-routines in that program was a function called regKPI. We will now dwelve into this function a bit (see original post at &lt;a href=&quot;http://portablecommandline.blogspot.com/2009/05/simple-console-program.html&quot;&gt;A simple console program&lt;/a&gt;). If we have a system where we have several KPI&#39;s to record, the simplest way to deal with this is the ask the user for a KPI name. Then we know that the answer should be treated as a string. For this we use Python&#39;s &lt;i&gt;raw input&lt;/i&gt; construct: &lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;def regKPI():&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;KPIName = raw_input(&quot;What is the name of your KPI ?&quot;)&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;---&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This way the user does not have to enclose her answer in string delimiters to make the program understand we are talking about character strings. The next question can be &quot;&lt;i&gt;what is the value of your KPI?&quot;, &lt;/i&gt;and we can take this as a string too. If we are only expecting numbers, we can use just input, and the numeric form is taken directly, but then it has to converted to a string afterwards if it is going to be written into an ASCII file (my favorite &quot;mini&quot; database). &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;To see how to write the data to a text file, wee the blog post &quot;&lt;a href=&quot;http://portablecommandline.blogspot.com/2009/05/writing-time-data-to-file.html&quot;&gt;Writing time data to file&lt;/a&gt;&quot;. That pretty much sums it up for now. Next time we will look at how we can get KPI data from the &quot;database&quot; and do some statistical analysis on them using Python tools! (Hm... this is getting away from the core concept here of making command line tools - we&#39;ll finish this console program thing and return to our true path afterwards! Promise!).&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;&lt;/div&gt;</description><link>http://portablecommandline.blogspot.com/2009/06/regkpi-register-kpi-value-in-database.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8745390009765826493.post-8175150825156718282</guid><pubDate>Sun, 31 May 2009 20:56:00 +0000</pubDate><atom:updated>2009-05-31T13:56:06.289-07:00</atom:updated><title>Johan Louwers Personal Blog.: Python dictionary variables</title><description>&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Reading tips: another Python blog:&lt;/div&gt;&lt;a href=&quot;http://johanlouwers.blogspot.com/2009/05/python-dictionary-variables.html&quot;&gt;Johan Louwers Personal Blog.: Python dictionary variables&lt;/a&gt;</description><link>http://portablecommandline.blogspot.com/2009/05/johan-louwers-personal-blog-python.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8745390009765826493.post-2196889481369158225</guid><pubDate>Thu, 28 May 2009 19:17:00 +0000</pubDate><atom:updated>2009-05-28T12:27:47.886-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">console</category><category domain="http://www.blogger.com/atom/ns#">menu</category><category domain="http://www.blogger.com/atom/ns#">programming</category><title>A simple console program</title><description>Say you want to make your own console program. This program should be able of recording data you enter to a text file, and extracting data from the text file to analyze them. One such program can be a tool to register values of different variables you are measuring, such as the number of visitors you have had in your office today. Say you count office visits, and you want to store this in the file under the name &quot;office&quot;. Then, you also count how many times during the day your phone rings. This variable is called &quot;phone&quot;. You create a program that pops up this menu: &lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;1.  Register KPI&lt;/div&gt;&lt;div&gt;2. Create KPI report&lt;/div&gt;&lt;div&gt;3. Quit&lt;/div&gt;&lt;div&gt;&gt;&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;When you choose 1, you are prompted for name an value of variable. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The menu is displayed when a function called &lt;i&gt;mainmenu&lt;/i&gt; is called. Such a menu can be written as follows in Python:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;def mainmenu():&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;print &#39;1: Register KPI&#39;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;print &#39;2: Create KPI report&#39;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;print &#39;3: Quit&#39;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;menuChoice = raw_input(&#39;&gt;&gt; &#39;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Depending on the input the user gives, we cann call different functions (still within mainmenu):&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;if menuChoice == &#39;1&#39;:&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;regKPI()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;elif menuChoice  == &#39;2&#39;:&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;createReport()&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;elif menuChoice == &#39;3&#39;:&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;print &#39;Goodbye&#39;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;else:&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;print &#39;Not a valid menu item. Pleast try again&#39;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;font-family:&#39;courier new&#39;;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot;  style=&quot;color:#33CC00;&quot;&gt;main()&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;There you go, a working menu. Next time: we show what the functions regKPI and createReport look like :-)&lt;/div&gt;</description><link>http://portablecommandline.blogspot.com/2009/05/simple-console-program.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8745390009765826493.post-833702569641238566</guid><pubDate>Tue, 26 May 2009 18:04:00 +0000</pubDate><atom:updated>2009-05-26T11:11:11.851-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">console</category><category domain="http://www.blogger.com/atom/ns#">linux</category><category domain="http://www.blogger.com/atom/ns#">user interface</category><category domain="http://www.blogger.com/atom/ns#">windows</category><title>Console programs</title><description>There are many programs that run in console environments. They are more often seen on Linux than other systems, but that doesn&#39;t mean they are not useful on Windows. My *nix favorite console programs are &lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;alpine: e-mail client for the console (easy to use and with a ton of features)&lt;/li&gt;&lt;li&gt;midnight commander: file organizer&lt;/li&gt;&lt;li&gt;vim: text editor&lt;/li&gt;&lt;li&gt;gnuplot: open source plotting program&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;These are quite complicated also when it comes to user interfaces, and are very usable programs. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;If you are a little less ambitious, making simple console programs with &quot;menus&quot; is quite simple. A database handling program may have the following menu:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;MENU:&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;1.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;REGISTER NEW FIELD&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;2.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;EDIT FIELD VALUES&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;3.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;HELP&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;SELECT MENU ITEM &gt;&gt;  ......&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Easy enought to use, eh? You want to read the help file, hit 3 on your keyboard followed by enter. Next post will show how to build a quite useful program using a console interface of this type.... (not uncommon in apps from the 1970&#39;s and 1980&#39;s)!&lt;/div&gt;&lt;/div&gt;</description><link>http://portablecommandline.blogspot.com/2009/05/console-programs.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8745390009765826493.post-7061087476997880336</guid><pubDate>Sun, 24 May 2009 18:56:00 +0000</pubDate><atom:updated>2009-05-24T11:59:36.627-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">batch files</category><category domain="http://www.blogger.com/atom/ns#">DOS</category><category domain="http://www.blogger.com/atom/ns#">programming</category><category domain="http://www.blogger.com/atom/ns#">windows</category><title>Avoid the *.py extension when calling scripts under Windows</title><description>If you&#39;d like to make your scripts run by typing &lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;myscript args&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;instead of &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;myscript.py args&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;you can wrap the python script inside a *.bat file. Say you want to run myscript.py and it takes two arguments, make the following batch DOS file: &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;myscript.py %1 %2&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The percentage+number things play the role of sys.argv in Python such that command line arguemnts are piped through to the Python script.&lt;/div&gt;</description><link>http://portablecommandline.blogspot.com/2009/05/avoid-py-extension-when-calling-scripts.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8745390009765826493.post-8656412118632879029</guid><pubDate>Sun, 24 May 2009 17:21:00 +0000</pubDate><atom:updated>2009-05-24T10:32:30.916-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">command line</category><category domain="http://www.blogger.com/atom/ns#">python</category><title>Command line tools take command line arguments</title><description>You all know them and use them; command line tools with arguments. Especially on linux, this is the way things go, but also in Windows. Think about the dos command dir, and its attributes; &lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;dir /p: list directory one screen full at the time&lt;/li&gt;&lt;li&gt;dir /w: list directory in columns, &lt;/li&gt;&lt;li&gt;....&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;On linux you have &lt;i&gt;ls&lt;/i&gt; with a bunch of options, for example &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;ls -al&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;to show files with owners and permissions. How do we make tools that can take command line arguments in Python? &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Fortunately, as the &lt;a href=&quot;http://www.python.org&quot;&gt;www.python.org&lt;/a&gt; site says, Python comes with batteries included. Command line arguments are stored in a list that can be accesed from the sys module. The stdin (command line input) arguments are found using sys.argv. If you use this line: &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;from sys import argv as inputargs&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;inputargs will be a list containing text strings with your input arguments. Then, if you have some script like this&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;from sys import argv as inputargs&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;myString = &#39;Hello &#39;+inputargs[1]+&#39;, this is &#39;+inputargs[2]+&#39; speaking!&#39;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;print myString&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The result of running this command: &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;./myscript.py guest &quot;the owner&quot;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;will be: &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;Hello guest, this is the owner speaking!&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Nice to know! We&#39;ll put it to use in the next post! Happy hacking :-)&lt;/div&gt;&lt;/div&gt;</description><link>http://portablecommandline.blogspot.com/2009/05/command-line-tools-take-command-line.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8745390009765826493.post-8477298390403797893</guid><pubDate>Sat, 23 May 2009 20:43:00 +0000</pubDate><atom:updated>2009-05-23T13:49:42.278-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">code</category><category domain="http://www.blogger.com/atom/ns#">command line</category><category domain="http://www.blogger.com/atom/ns#">python</category><category domain="http://www.blogger.com/atom/ns#">time management</category><title>Writing time data to file</title><description>In the last post I wrote about a small command line utility for registering hours spent on different tasks. I never got to showing how to write to text files in Python, so let&#39;s get down to it now (what&#39;s better to do a Saturday evening than hacking away in Python?)!&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;First, to open a file for editing, you use the following syntax in Python: &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;myFile=open(&#39;filename.txt&#39;,&#39;attribute&#39;)&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;where &lt;i&gt;attribute&lt;/i&gt; is one of the following:&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;a - append: appends new text to the end of the file&lt;/li&gt;&lt;li&gt;w - write: write to file, will overwrite contents already there&lt;/li&gt;&lt;li&gt;r - read: does only give read access to the file&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;For our purpose we will use the append attrribute (&lt;i&gt;a&lt;/i&gt;). What you create in this way is a file object, that has certain methods available to it depending on the attribute given to the constructor &lt;i&gt;open&lt;/i&gt;. The one we will be using is, surprisingly enough, &lt;i&gt;write&lt;/i&gt;:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;myFile.write(myString)&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;When you create a file object, the entire file is loaded into your computers working memory (RAM). When you are done working on the file, you should close it to free memory and avoid unforeseen computer trolls to pop out and eat you. You do this by issueing the &lt;i&gt;close&lt;/i&gt; method to your file object; &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;myFile.close()&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;If you add the three code lines written here to the script outlined in the last post, you will have a working time registering tool!&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</description><link>http://portablecommandline.blogspot.com/2009/05/writing-time-data-to-file.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8745390009765826493.post-4261445989365789556</guid><pubDate>Fri, 22 May 2009 08:47:00 +0000</pubDate><atom:updated>2009-05-22T02:11:42.046-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">command line</category><category domain="http://www.blogger.com/atom/ns#">python</category><category domain="http://www.blogger.com/atom/ns#">time management</category><category domain="http://www.blogger.com/atom/ns#">windows</category><title>Time registering command line tool in Python</title><description>&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;Often, I want to write a one-liner to a file to register information. I do my time tracking this way, for example. First, you need Python to get data from the command line. Say you want to track number of hours spent on a project at work and get this into a tab-delimited file that you can open in a spreadsheet later (Excel, Calc, etc). You want a utility where you simply write the following at the command prompt: &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;regtime projectname hours&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;and it should automatically make the following line in your &quot;spreadsheet&quot;:&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;date&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;  &lt;/span&gt;projectname&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;  &lt;/span&gt;hours&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;In order to do this, we need to get the current date in Python. Luckily, there is a nice module called time that allows us to do that in a simple fashion. First, import it as follows: &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;from  time import strftime as getdate&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;currentDate=getdate(&#39;%Y-%m-%d&#39;)&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;What this does is that it gets the current local time used on the computer and writes a text string with the date:&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;currentDate=&#39;2009-05-22&#39;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;We also need to get the command line arguments &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: italic;&quot;&gt;projectname&lt;/span&gt; and &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: italic;&quot;&gt;hours&lt;/span&gt; into Python.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;For this we need the list sys.argv.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;from sys import argv as arguments&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-family: arial;&quot;&gt; projectname=argv[1]&lt;/span&gt;;&lt;/span&gt; hours=argv[2];&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Ok, lets summarize with a script that takes the two command line arguments and writes the &quot;spreadsheet&quot; line to the screen:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;from sys import argv as cla &lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;from time import strftime as getDate  &lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;myString = getDate(&#39;%Y-%m-%d&#39;)+&#39;\t&#39;+cla[1]+&#39;\t&#39;+cla[2]+&#39;\n&#39; &lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;print myString&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The result of running this script with arguments &quot;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: italic;&quot;&gt;Command line tool&quot; &lt;/span&gt;and 1 is shown below:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space:pre&quot;&gt; &lt;/span&gt;2009-05-22      Command line tool       1&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Next post will show how to write this at the end of a text file. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: italic;&quot;&gt;Some comments on the above code:&lt;/span&gt;&lt;/div&gt;&lt;div&gt;a) the character \t means &quot;insert tab in text string&quot;&lt;/div&gt;&lt;div&gt;b) the character \n means &quot;insert line break&quot;&lt;/div&gt;&lt;div&gt;c) the string &#39;%Y-%m-%d&#39; formats the date to ISO standard&lt;/div&gt;&lt;div&gt;d) adding string elements works in the obvious way: &#39;foo&#39;+&#39;bar&#39;=&#39;foobar&#39;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;</description><link>http://portablecommandline.blogspot.com/2009/05/time-registering-command-line-tool-in.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8745390009765826493.post-4755102618845318088</guid><pubDate>Fri, 22 May 2009 08:43:00 +0000</pubDate><atom:updated>2009-05-22T01:46:43.252-07:00</atom:updated><title>Write your own command line tools!</title><description>As a multi-platform computer user, I always miss some things when going from one platform to antoher. Especially when I go to Windows, I miss some command line tools. One remedy would be Cygwin, but for some reason I don&#39;t get the idea of Cygwin. I&#39;d rather use Linux directly then, and I don&#39;t need complicated tools. I just like to have some simple possibilities. This blog will contain posts about writing small command line tools in Python that work on Windows (XP), Macs and Linux computers, all using the same scripts, or with very minor modifications. This is kinda my hobby, but it has also made my work day more fun. </description><link>http://portablecommandline.blogspot.com/2009/05/write-your-own-command-line-tools.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item></channel></rss>