<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='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'><id>tag:blogger.com,1999:blog-20019988</id><updated>2014-03-13T02:42:13.670+07:00</updated><category term="I/O and File"/><category term="Module"/><category term="Database"/><category term="Function"/><category term="Built-In Functions"/><category term="Class"/><category term="List"/><category term="Bimbel"/><category term="CGI"/><category term="Dictionary"/><category term="Number"/><category term="String"/><category term="Tuple"/><title type='text'>&gt;&gt;&gt; python scripting</title><subtitle type='html'>&quot;&quot;&quot;the common problems in python programming&quot;&quot;&quot;</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>23</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-20019988.post-3523362943427530721</id><published>2013-06-13T07:00:00.000+07:00</published><updated>2013-06-13T07:00:00.310+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="List"/><title type='text'>Sorting The List Elements in Python</title><content type='html'>The list elements can be sorted using sort() method or sorted() function. This code shows you the difference between list&#39;s sort() method and sorted() function.  &lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; # using sort() method&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; li = [99, 28, 45, 30, 40]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; li.sort()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; li&lt;br /&gt;[28, 30, 40, 45, 99]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; # using sorted() function&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; originalList = [99, 28, 45, 30, 40]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; sortedList = sorted(originalList)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; sortedList&lt;br /&gt;[28, 30, 40, 45, 99]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; originalList&lt;br /&gt;[99, 28, 45, 30, 40]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/3523362943427530721/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/06/sorting-list-elements-in-python.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/3523362943427530721'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/3523362943427530721'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/06/sorting-list-elements-in-python.html' title='Sorting The List Elements in Python'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-4461380353800865642</id><published>2013-06-07T21:01:00.001+07:00</published><updated>2013-06-07T21:01:40.369+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Function"/><title type='text'>Creating Recursive Function in Python</title><content type='html'>This example code shows you how to create a recursive function in Python.  &lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; def factorial(num):&lt;br /&gt;          if num &lt; 0:&lt;br /&gt;             print(&quot;ERROR: Invalid parameter value&quot;)&lt;br /&gt;             return&lt;br /&gt;          elif num == 0 or num == 1:&lt;br /&gt;             return 1&lt;br /&gt;          else:&lt;br /&gt;             return num * factorial(num-1)&lt;br /&gt; &lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; factorial(4)&lt;br /&gt;24&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; factorial(5)&lt;br /&gt;120&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/4461380353800865642/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/06/creating-recursive-function-in-python.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/4461380353800865642'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/4461380353800865642'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/06/creating-recursive-function-in-python.html' title='Creating Recursive Function in Python'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-3740692924572661018</id><published>2013-05-31T23:32:00.001+07:00</published><updated>2013-05-31T23:32:13.242+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Class"/><title type='text'>Defining a Class Variable in Python</title><content type='html'>Class variable is a variable that is shared by all instances of a class. It is defined within a class but outside any of the class&#39;s methods. This code shows you how to define it.  &lt;pre&gt;&lt;code&gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; class Book(object):&lt;br /&gt;          counter = 0    # class variable&lt;br /&gt;          def __init__(self, isbn, title, author, publisher):&lt;br /&gt;             Book.counter += 1&lt;br /&gt;             self.isbn = isbn&lt;br /&gt;             self.title = title&lt;br /&gt;             self.author = author&lt;br /&gt;             self.publisher = publisher&lt;br /&gt;          def displayBook(self):&lt;br /&gt;             print(&quot;Book #%d&quot; % Book.counter)&lt;br /&gt;             print(&quot;%s\t%s\t%s\t%s&quot; %&lt;br /&gt;                   (self.isbn,self.title,self.author,self.publisher))&lt;br /&gt;		&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; a = Book(&#39;978-0-672-32862-6&#39;,&#39;Python&#39;,&#39;David Beazley&#39;,&#39;Addison-Wesley&#39;)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; a.displayBook()&lt;br /&gt;Book #1&lt;br /&gt;978-0-672-32862-6  Python David Beazley  Addison-Wesley&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; b = Book(&#39;978-0-321-56615-7&#39;,&#39;Objective-C&#39;,&#39;Stephen G. Kotchan&#39;,&#39;Addison-Wesley&#39;)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; b.displayBook()&lt;br /&gt;Book #2&lt;br /&gt;978-0-321-56615-7  Objective-C  Stephen G. Kotchan  Addison-Wesley&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; c = Book(&#39;978-0-321-68056-3&#39;,&#39;Python 3&#39;,&#39;Mark Summerfield&#39;,&#39;Addison-Wesley&#39;)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; c.displayBook()&lt;br /&gt;Book #3&lt;br /&gt;978-0-321-68056-3  Python 3  Mark Summerfield  Addison-Wesley&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/3740692924572661018/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/defining-class-variable-in-python.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/3740692924572661018'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/3740692924572661018'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/defining-class-variable-in-python.html' title='Defining a Class Variable in Python'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-4769195939434300081</id><published>2013-05-30T23:50:00.000+07:00</published><updated>2013-05-30T23:58:11.197+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="I/O and File"/><category scheme="http://www.blogger.com/atom/ns#" term="Module"/><title type='text'>Copying a File in Python</title><content type='html'>To copy a file in Pyhton, we can use shutil.copy(). &lt;pre&gt;&lt;code&gt;budi@linux:~$ sudo gedit test.py&lt;br /&gt;[sudo] password for budi: &lt;br /&gt;budi@linux:~$ cat test.py&lt;br /&gt;#!/usr/bin/python3.2&lt;br /&gt;&lt;br /&gt;print(&quot;Hello World!&quot;)&lt;br /&gt;budi@linux:~$ python3.2&lt;br /&gt;Python 3.2.3 (default, Oct 19 2012, 20:13:42) &lt;br /&gt;[GCC 4.6.3] on linux2&lt;br /&gt;Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; import os&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; import shutil&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; dir = os.getcwd()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; sourcefile = &quot;test.py&quot;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; destfile = &quot;dest.py&quot;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; shutil.copy(os.path.join(dir, sourcefile), os.path.join(dir, destfile))&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; exit()&lt;br /&gt;budi@linux:~$ cat dest.py&lt;br /&gt;#!/usr/bin/python3.2&lt;br /&gt;&lt;br /&gt;print(&quot;Hello World!&quot;)&lt;br /&gt;budi@linux:~$&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/4769195939434300081/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/copying-file.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/4769195939434300081'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/4769195939434300081'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/copying-file.html' title='Copying a File in Python'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-3127662175089632170</id><published>2013-05-29T06:37:00.000+07:00</published><updated>2013-05-29T06:38:46.561+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Function"/><category scheme="http://www.blogger.com/atom/ns#" term="Number"/><category scheme="http://www.blogger.com/atom/ns#" term="String"/><title type='text'>Displaying Thousands Separator in a Number</title><content type='html'>The other way to add thousands separator in a number:  &lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; def addthousandseparator(number, separator=&#39;,&#39;):&lt;br /&gt;          lstr = list(str(number))&lt;br /&gt;          n = len(lstr) - 3&lt;br /&gt;          while n &amp;gt; 0:&lt;br /&gt;            lstr.insert(n, separator)&lt;br /&gt;            n -= 3&lt;br /&gt;          return &quot;&quot;.join(lstr)&lt;br /&gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; addthousandseparator(1234567)&lt;br /&gt;&#39;1,234,567&#39;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; addthousandseparator(1234567890, &#39;.&#39;)&lt;br /&gt;&#39;1.234.567.890&#39;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/3127662175089632170/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/displaying-thousands-separator-in-number.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/3127662175089632170'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/3127662175089632170'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/displaying-thousands-separator-in-number.html' title='Displaying Thousands Separator in a Number'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-1048850098641514811</id><published>2013-05-28T08:57:00.001+07:00</published><updated>2013-05-28T09:01:50.470+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Module"/><title type='text'>Opening an URL in Python</title><content type='html'>We can open an URL from Python code using open(), open_new() or open_new_tab() function on webbrowser module.  &lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; import webbrowser&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; webbrowser.open(&quot;http://www.python.org&quot;)&lt;br /&gt;True&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; webbrowser.open_new(&quot;http://www.python.org&quot;)&lt;br /&gt;True&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; webbrowser.open_new_tab(&quot;http://www.python.org&quot;)&lt;br /&gt;True&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/1048850098641514811/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/opening-url-in-python.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/1048850098641514811'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/1048850098641514811'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/opening-url-in-python.html' title='Opening an URL in Python'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-7327489670468891786</id><published>2013-05-26T12:15:00.000+07:00</published><updated>2013-05-26T12:24:34.315+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Class"/><title type='text'>Method Overloading in Python</title><content type='html'>Unlike C++, Python doesn&#39;t support method overloading. To impelement this C++&#39;s feature in Python, we can use the variable-length argument or isinstance() function.  These codes show you how to implement method overloading both in C++ and Python. &lt;br /&gt;&lt;br /&gt;budi@linux:~$ sudo gedit overload.cpp&lt;br /&gt;budi@linux:~$ cat overload.cpp &lt;pre&gt;&lt;code&gt;/* C++ code */&lt;br /&gt;&lt;br /&gt;#include &lt;iostream&gt;&lt;br /&gt;&lt;br /&gt;class Example {&lt;br /&gt;public:&lt;br /&gt;  int div(int a, int b) { return a / b; }&lt;br /&gt;  double div(double a, double b) { return a / b; }&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;  Example *obj = new Example();&lt;br /&gt;  std::cout&amp;lt;&amp;lt;&quot;Integer division: &quot;&lt;br /&gt;           &amp;lt;&amp;lt;obj-&amp;gt;div(10, 3)&amp;lt;&amp;lt;std::endl;&lt;br /&gt;  std::cout&amp;lt;&amp;lt;&quot;Floating-point division: &quot;&lt;br /&gt;           &amp;lt;&amp;lt;obj-&amp;gt;div(10.0, 3.0)&amp;lt;&amp;lt;std::endl;&lt;br /&gt;  delete obj;&lt;br /&gt;  return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt; budi@linux:~$ sudo g++-4.6 overload.cpp -o overload.o&lt;br /&gt;budi@linux:~$ sudo chmod 755 overload.o&lt;br /&gt;budi@linux:~$ ./overload.o&lt;br /&gt;&lt;strong&gt;Integer division: 3&lt;br /&gt;Floating-point division: 3.33333&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;budi@linux:~$ sudo gedit overload.py&lt;br /&gt;budi@linux:~$ cat overload.py &lt;pre&gt;&lt;code&gt;#!/usr/bin/python3.2&lt;br /&gt;&lt;br /&gt;# Python code&lt;br /&gt;&lt;br /&gt;class Example(object):&lt;br /&gt;   def __init__(self):&lt;br /&gt;      pass&lt;br /&gt;   def div(self, a, b):&lt;br /&gt;     if isinstance(a, int) and isinstance(b, int):&lt;br /&gt;        return a // b&lt;br /&gt;     else:&lt;br /&gt;        return a / b&lt;br /&gt;&lt;br /&gt;if __name__ == &#39;__main__&#39;:&lt;br /&gt;   obj = Example()&lt;br /&gt;   print(&quot;Integer division: &quot;, obj.div(10, 3))&lt;br /&gt;   print(&quot;Floating-point division: &quot;, obj.div(10.0, 3.0))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt; budi@linux:~$ sudo chmod 755 overload.py&lt;br /&gt;budi@linux:~$ ./overload.py&lt;br /&gt;&lt;strong&gt;Integer division:  3&lt;br /&gt;Floating-point division:  3.3333333333333335&lt;br /&gt;&lt;/strong&gt;</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/7327489670468891786/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/method-overloading-in-python.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/7327489670468891786'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/7327489670468891786'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/method-overloading-in-python.html' title='Method Overloading in Python'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-144537598549717617</id><published>2013-05-25T21:17:00.000+07:00</published><updated>2013-05-25T21:17:02.293+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Function"/><title type='text'>Passing Variable-length Arguments</title><content type='html'>This following code shows you how to pass variable-length argument in a function.  &lt;pre&gt;&lt;code&gt;budi@linux:~$ python3.2&lt;br /&gt;Python 3.2.3 (default, Oct 19 2012, 20:13:42) &lt;br /&gt;[GCC 4.6.3] on linux2&lt;br /&gt;Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; def func(*args):&lt;br /&gt;...   for i in args:&lt;br /&gt;...     print(i, end=&#39; &#39;)&lt;br /&gt;... &lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; func(10)         # one argument&lt;br /&gt;10&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; func(10,20)      # two arguments&lt;br /&gt;10 20 &lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; func(10, 20, 30) # three arguments&lt;br /&gt;10 20 30 &lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/144537598549717617/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/passing-variable-length-arguments.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/144537598549717617'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/144537598549717617'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/passing-variable-length-arguments.html' title='Passing Variable-length Arguments'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-5621508360783232662</id><published>2013-05-24T08:59:00.000+07:00</published><updated>2013-05-24T09:01:52.986+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="I/O and File"/><category scheme="http://www.blogger.com/atom/ns#" term="Module"/><title type='text'>Creating A &quot;tar&quot; File</title><content type='html'>The following shell commands and Python codes show you how to create a tar archive file with no compression.  &lt;pre&gt;&lt;code&gt;budi@linux:~$ cd pythoncode/&lt;br /&gt;budi@linux:~/pythoncode$ ls -la&lt;br /&gt;total 20&lt;br /&gt;drwxr-xr-x  2 root root 4096 May 24 08:42 .&lt;br /&gt;drwxr-xr-x 38 budi budi 4096 May 24 08:21 ..&lt;br /&gt;-rwxr-xr-x  1 root root  714 May 24 08:23 test1.py&lt;br /&gt;-rwxr-xr-x  1 root root 2143 May 24 08:22 test2.py&lt;br /&gt;-rwxr-xr-x  1 root root 1429 May 24 08:22 test3.py&lt;br /&gt;budi@linux:~/pythoncode$ sudo python3.2&lt;br /&gt;[sudo] password for budi:&lt;br /&gt;Python 3.2.3 (default, Oct 19 2012, 20:13:42) &lt;br /&gt;[GCC 4.6.3] on linux2&lt;br /&gt;Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; import os&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; os.getcwd()&lt;br /&gt;&#39;/home/budi/pythoncode&#39;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; import tarfile, glob&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; tfile = tarfile.open(&quot;test.tar&quot;, &#39;w&#39;)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; for filename in glob.glob(&quot;*.py&quot;):&lt;br /&gt;...    tfile.add(filename)&lt;br /&gt;... &lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; tfile.close()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; exit()&lt;br /&gt;budi@linux:~/pythoncode$ ls -la&lt;br /&gt;total 32&lt;br /&gt;drwxr-xr-x  2 root root  4096 May 24 08:43 .&lt;br /&gt;drwxr-xr-x 38 budi budi  4096 May 24 08:21 ..&lt;br /&gt;-rwxr-xr-x  1 root root   714 May 24 08:23 test1.py&lt;br /&gt;-rwxr-xr-x  1 root root  2143 May 24 08:22 test2.py&lt;br /&gt;-rwxr-xr-x  1 root root  1429 May 24 08:22 test3.py&lt;br /&gt;&lt;strong&gt;-rw-r--r--  1 root root 10240 May 24 08:45 test.tar&lt;/strong&gt;&lt;br /&gt;budi@linux:~/pythoncode$ &lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/5621508360783232662/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/creating-tar-file.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/5621508360783232662'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/5621508360783232662'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/creating-tar-file.html' title='Creating A &quot;tar&quot; File'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-865355541413547951</id><published>2013-05-23T13:39:00.001+07:00</published><updated>2013-05-23T15:07:31.058+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Database"/><category scheme="http://www.blogger.com/atom/ns#" term="I/O and File"/><category scheme="http://www.blogger.com/atom/ns#" term="Module"/><title type='text'>Importing Excel File into SQLite Table</title><content type='html'>The following steps show you how to import data from Excel file into SQLite table.  &lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; # STEP 1: Create your Excel file&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; import csv&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; data = [(1,10), (2,20), (3,30), (4,40), (5,50)]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; with open(&quot;data.csv&quot;, &#39;w&#39;, newline=&#39;&#39;, encoding=&#39;utf-8&#39;) as csvfile:&lt;br /&gt;          w = csv.writer(csvfile, delimiter=&#39;;&#39;)&lt;br /&gt;          w.writerows(data)&lt;br /&gt;          csvfile.close()&lt;br /&gt; &lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; # STEP 2: Create a table with two integer fields on your SQLite database&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; import sqlite3&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; conn = sqlite3.connect(&quot;sqlite3db.db&quot;)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; c = conn.cursor()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; c.execute(&quot;create table test(a integer, b integer)&quot;)&lt;br /&gt;&amp;lt;sqlite3.Cursor object at 0x020CDE20&amp;gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; c.close()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; conn.close()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; # STEP 3: Import your data.csv into test table&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; # import csv, sqlite3&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; with open(&quot;data.csv&quot;, &#39;r&#39;, newline=&#39;&#39;, encoding=&#39;utf-8&#39;) as csvfile:&lt;br /&gt;          conn = sqlite3.connect(&quot;sqlite3db.db&quot;)&lt;br /&gt;          c = conn.cursor()&lt;br /&gt;          r = csv.reader(csvfile, delimiter=&#39;;&#39;)&lt;br /&gt;          for row in r:&lt;br /&gt;             c.execute(&quot;insert into test values(?,?)&quot;, (row[0], row[1]))&lt;br /&gt;          conn.commit()&lt;br /&gt;          csvfile.close()&lt;br /&gt; &lt;br /&gt;&amp;lt;sqlite3.Cursor object at 0x020CDEA0&amp;gt;&lt;br /&gt;&amp;lt;sqlite3.Cursor object at 0x020CDEA0&amp;gt;&lt;br /&gt;&amp;lt;sqlite3.Cursor object at 0x020CDEA0&amp;gt;&lt;br /&gt;&amp;lt;sqlite3.Cursor object at 0x020CDEA0&amp;gt;&lt;br /&gt;&amp;lt;sqlite3.Cursor object at 0x020CDEA0&amp;gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; # now, check your data on the table&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; for (a, b) in c.execute(&quot;select a, b from test&quot;):&lt;br /&gt;          print(&quot;%s\t%s&quot; % (a, b))&lt;br /&gt; &lt;br /&gt;1     10&lt;br /&gt;2     20&lt;br /&gt;3     30&lt;br /&gt;4     40&lt;br /&gt;5     50&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; conn.close()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;QH6X8NHXHVP8</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/865355541413547951/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/importing-excel-file-into-sqlite-table.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/865355541413547951'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/865355541413547951'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/importing-excel-file-into-sqlite-table.html' title='Importing Excel File into SQLite Table'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-6574784022543626392</id><published>2013-05-22T06:48:00.000+07:00</published><updated>2013-05-22T06:48:27.927+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Built-In Functions"/><category scheme="http://www.blogger.com/atom/ns#" term="I/O and File"/><category scheme="http://www.blogger.com/atom/ns#" term="Module"/><title type='text'>Compressing Files into Zip File</title><content type='html'>This code is used to compress your files into zip file.  &lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; # change directory&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; import os&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; os.chdir(&quot;E:\\pythoncode&quot;)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; # creating zip file&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; import zipfile&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; files = (&quot;abs.py&quot;, &quot;floor.py&quot;, &quot;exp.py&quot;, &quot;log.py&quot;, &quot;log10.py&quot;)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; zf = zipfile.ZipFile(&quot;test.zip&quot;, &#39;w&#39;, compression=zipfile.ZIP_DEFLATED)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; for file in files:&lt;br /&gt;          zf.write(file)&lt;br /&gt; &lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; zf.close()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; # reading data from zip file&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; zf = zipfile.ZipFile(&quot;test.zip&quot;, &#39;r&#39;)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; for filename in zf.namelist():&lt;br /&gt;          nbytes = len(zf.read(filename))&lt;br /&gt;          print(filename, &#39;\t&#39;, nbytes, &quot; bytes&quot;)&lt;br /&gt; &lt;br /&gt;abs.py      143  bytes&lt;br /&gt;floor.py    199  bytes&lt;br /&gt;exp.py      179  bytes&lt;br /&gt;log.py      179  bytes&lt;br /&gt;log10.py    194  bytes&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; zf.close()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/6574784022543626392/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/compressing-files-into-zip-file.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/6574784022543626392'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/6574784022543626392'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/compressing-files-into-zip-file.html' title='Compressing Files into Zip File'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-5245148202323368088</id><published>2013-05-21T16:50:00.000+07:00</published><updated>2013-05-21T16:58:08.260+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Database"/><category scheme="http://www.blogger.com/atom/ns#" term="I/O and File"/><category scheme="http://www.blogger.com/atom/ns#" term="Module"/><title type='text'>Exporting SQLite Table into Excel File</title><content type='html'>This code illustrates how to export an SQLite table into Excel (CSV) file.  &lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; import sqlite3, csv&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; conn = sqlite3.connect(&quot;:memory:&quot;)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; c = conn.cursor()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; c.execute(&quot;create table test(c1 int, c2 int)&quot;)&lt;br /&gt;&amp;lt;sqlite3.Cursor object at 0x0223DD20&amp;gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; data = [(1,10), (2,20), (3,30), (4,40), (5,50)]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; c.executemany(&quot;insert into test values(?,?)&quot;, data)&lt;br /&gt;&amp;lt;sqlite3.Cursor object at 0x0223DD20&amp;gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; conn.commit()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; with open(&quot;test.csv&quot;, &#39;w&#39;, newline=&#39;&#39;, encoding=&#39;utf-8&#39;) as csvfile:&lt;br /&gt;          w = csv.writer(csvfile, delimiter=&#39;;&#39;)&lt;br /&gt;          for row in c.execute(&quot;select * from test&quot;):&lt;br /&gt;             w.writerow(row)&lt;br /&gt;  &lt;br /&gt;6&lt;br /&gt;6&lt;br /&gt;6&lt;br /&gt;6&lt;br /&gt;6&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; csvfile.close()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; c.close()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; conn.close()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/5245148202323368088/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/exporting-sqlite-table-into-excep-file.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/5245148202323368088'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/5245148202323368088'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/exporting-sqlite-table-into-excep-file.html' title='Exporting SQLite Table into Excel File'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-7683994565701965798</id><published>2013-05-20T07:29:00.000+07:00</published><updated>2013-05-20T08:37:22.903+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Database"/><title type='text'>MySQL + Python</title><content type='html'>This code shows you how to use Python with MySQL database. For this demo, the following has already been installed: &lt;ul style=&quot;margin-left:0px;&quot;&gt;&lt;li&gt;MySQL Server; it can be found &lt;a href=&quot;http://dev.mysql.com/downloads/mysql/&quot;&gt;here&lt;/a&gt;&lt;/li&gt;&lt;li&gt;MySQL Connector/Python; it can be found &lt;a href=&quot;http://dev.mysql.com/downloads/connector/python/&quot;&gt;here&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; import mysql.connector&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; conn = mysql.connector.connect(user=&quot;budi&quot;, password=&quot;r4h4Rj0&quot;,&lt;br /&gt;          host=&quot;127.0.0.1&quot;, database=&quot;pythondb&quot;)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; c = conn.cursor()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; # creating table&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; c.execute(&quot;&quot;&quot;&lt;br /&gt;create table books(&lt;br /&gt;   isbn char(17) not null,&lt;br /&gt;   title varchar(50),&lt;br /&gt;   author varchar(25),&lt;br /&gt;   publisher varchar(25),&lt;br /&gt;   constraint books_pk primary key(isbn))&lt;br /&gt;&quot;&quot;&quot;)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; # inserting data into table&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; data = [&lt;br /&gt;(&#39;978-0-672-32862-6&#39;,&#39;Python Essential Reference&#39;,&#39;David Beazley&#39;,&#39;Addison-Wesley&#39;),&lt;br /&gt;(&#39;978-0-321-56615-7&#39;,&#39;Programming in Objective-C&#39;,&#39;Stephen G. Kotchan&#39;,&#39;Addison-Wesley&#39;),&lt;br /&gt;(&#39;978-0-321-68056-3&#39;,&#39;Programming in Python 3&#39;,&#39;Mark Summerfield&#39;,&#39;Addison-Wesley&#39;)]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; c.executemany(&quot;insert into books values(%s, %s, %s, %s)&quot;, data)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; conn.commit()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; # performing query&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; c.execute(&quot;select * from books&quot;)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; # fetching table rows&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; resultset = c.fetchall()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; for isbn, title, author, publisher in resultset:&lt;br /&gt;          print(&quot;%s\t%s\t%s\t%s&quot; % (isbn, title, author, publisher))&lt;br /&gt; &lt;br /&gt;978-0-321-56615-7  Programming in Objective-C  Stephen G. Kotchan  Addison-Wesley&lt;br /&gt;978-0-321-68056-3  Programming in Python 3     Mark Summerfield    Addison-Wesley&lt;br /&gt;978-0-672-32862-6  Python Essential Reference  David Beazley       Addison-Wesley&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; c.close()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; conn.close()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt; </content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/7683994565701965798/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/mysql-python.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/7683994565701965798'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/7683994565701965798'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/mysql-python.html' title='MySQL + Python'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-7582297397673113374</id><published>2013-05-20T04:47:00.000+07:00</published><updated>2013-05-20T04:51:22.586+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Built-In Functions"/><category scheme="http://www.blogger.com/atom/ns#" term="I/O and File"/><title type='text'>Getting File Size in Python</title><content type='html'>In Python, you can get the file size using the following ways:  &lt;pre&gt;&lt;code&gt;os.path.getsize(&quot;yourfile&quot;)&lt;/code&gt;&lt;/pre&gt; or  &lt;pre&gt;&lt;code&gt;os.stat(&quot;yourfile&quot;).st_size&lt;/code&gt;&lt;/pre&gt; Example: &lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; import os&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; os.path.getsize(&quot;E:\\pythoncode\\test.py&quot;)&lt;br /&gt;13&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; os.stat(&quot;E:\\pythoncode\\test.py&quot;).st_size&lt;br /&gt;13&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/7582297397673113374/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/getting-file-size-in-python.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/7582297397673113374'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/7582297397673113374'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/getting-file-size-in-python.html' title='Getting File Size in Python'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-7174009107246107899</id><published>2013-05-19T06:49:00.000+07:00</published><updated>2013-05-19T06:51:03.280+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Function"/><category scheme="http://www.blogger.com/atom/ns#" term="I/O and File"/><title type='text'>Listing The Files and Subdirectories in A Directory</title><content type='html'>You can use this function to list all files and subdirectories in a directory.  &lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; import os&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; def ls(path):&lt;br /&gt;          names = os.listdir(path)&lt;br /&gt;          for name in names:&lt;br /&gt;             isdir = os.path.isdir(os.path.join(path, name))&lt;br /&gt;             if isdir:&lt;br /&gt;               s = name + &quot;\t\t\t&amp;lt;DIR&amp;gt;&quot;&lt;br /&gt;             else:&lt;br /&gt;               s = name&lt;br /&gt;             print(s)&lt;br /&gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; samplepath = &quot;C:\\Python32&quot;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; ls(samplepath)&lt;br /&gt;django-admin.py&lt;br /&gt;DLLs                    &amp;lt;DIR&amp;gt;&lt;br /&gt;Doc                     &amp;lt;DIR&amp;gt;&lt;br /&gt;include                 &amp;lt;DIR&amp;gt;&lt;br /&gt;Lib                     &amp;lt;DIR&amp;gt;&lt;br /&gt;libs                    &amp;lt;DIR&amp;gt;&lt;br /&gt;LICENSE.txt&lt;br /&gt;NEWS.txt&lt;br /&gt;python.exe&lt;br /&gt;pythonw.exe&lt;br /&gt;qt.conf&lt;br /&gt;README.txt&lt;br /&gt;Scripts                 &amp;lt;DIR&amp;gt;&lt;br /&gt;tcl                     &amp;lt;DIR&amp;gt;&lt;br /&gt;Tools                   &amp;lt;DIR&amp;gt;&lt;br /&gt;w9xpopen.exe&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/7174009107246107899/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/listing-files-and-subdirectories-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/7174009107246107899'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/7174009107246107899'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/listing-files-and-subdirectories-in.html' title='Listing The Files and Subdirectories in A Directory'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-5293870056413231959</id><published>2013-05-18T13:47:00.000+07:00</published><updated>2013-05-18T15:22:29.429+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Class"/><category scheme="http://www.blogger.com/atom/ns#" term="Module"/><title type='text'>Defining Abstract Class (and Abstract Method) in Python</title><content type='html'>The following code illustrates the usage of the ABC (Abstract Base Class) module.  &lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; from abc import ABCMeta, abstractmethod&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; import math&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; # define an abstract class&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; class Shape(metaclass=ABCMeta):&lt;br /&gt;          @abstractmethod&lt;br /&gt;          def area(self): pass&lt;br /&gt;          @abstractmethod&lt;br /&gt;          def circumference(self): pass&lt;br /&gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; # first descendant class&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; class Rectangle(Shape):&lt;br /&gt;           def __init__(self, w, h):&lt;br /&gt;              self.width = w&lt;br /&gt;              self.height = h&lt;br /&gt;           # overriding Shape&#39;s area()&lt;br /&gt;           def area(self):&lt;br /&gt;              return self.width * self.height&lt;br /&gt;           # overriding Shape&#39;s circumference()&lt;br /&gt;           def circumference(self):&lt;br /&gt;              return 2 * (self.width + self.height)&lt;br /&gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; # second descendant class&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; class Circle(Shape):&lt;br /&gt;           def __init__(self, r):&lt;br /&gt;              self.radius = r&lt;br /&gt;           # overriding Shape&#39;s area()&lt;br /&gt;           def area(self):&lt;br /&gt;              return math.pi * (self.radius ** 2)&lt;br /&gt;           # overriding Shape&#39;s circumference()&lt;br /&gt;           def circumference(self):&lt;br /&gt;              return 2 * math.pi * self.radius&lt;br /&gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; # you can&#39;t create an instance of Shape class&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; obj = Shape()&lt;br /&gt;Traceback (most recent call last):&lt;br /&gt;  File &quot;&lt;pyshell#33&gt;&quot;, line 1, in &lt;module&gt;&lt;br /&gt;    obj = Shape()&lt;br /&gt;TypeError: Can&amp;rsquo;t instantiate abstract class Shape with abstract methods area, &lt;br /&gt;circumference&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; # create an instance of Rectangle&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; obj1 = Rectangle(5, 3)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; obj1.area()  # call Rectangle&#39;s area()&lt;br /&gt;15&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; obj1.circumference()&lt;br /&gt;16&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; # create an instance of Circle&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; obj2 = Circle(3)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; obj2.area()  # call Circle&#39;s area()&lt;br /&gt;28.274333882308138&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; obj2.circumference()&lt;br /&gt;18.84955592153876&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt; If you&#39;re using Python 2, use this code:  &lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; # define an abstract class&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; class Shape:&lt;br /&gt;          __metaclass__= ABCMeta&lt;br /&gt;          @abstractmethod&lt;br /&gt;          def area(self): pass&lt;br /&gt;          @abstractmethod&lt;br /&gt;          def circumference(self): pass&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/5293870056413231959/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/defining-abstract-class-and-abstract.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/5293870056413231959'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/5293870056413231959'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/defining-abstract-class-and-abstract.html' title='Defining Abstract Class (and Abstract Method) in Python'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-1918766871014898779</id><published>2013-05-17T16:29:00.000+07:00</published><updated>2013-05-18T15:22:58.020+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="I/O and File"/><category scheme="http://www.blogger.com/atom/ns#" term="List"/><category scheme="http://www.blogger.com/atom/ns#" term="Module"/><category scheme="http://www.blogger.com/atom/ns#" term="Tuple"/><title type='text'>CSV (Excel) File Writing and Reading</title><content type='html'>The following code shows you how to write and read data to/from a CSV (Comma Separated Values) file.  &lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; import csv&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; ################ file writing #################&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; data = [(1, &quot;Dennis Ritchie&quot;, &quot;C&quot;),&lt;br /&gt;     (2, &quot;Bjarne Stroustrup&quot;, &quot;C++&quot;),&lt;br /&gt;     (3, &quot;James Gosling&quot;, &quot;Java&quot;),&lt;br /&gt;     (4, &quot;Larry Wall&quot;, &quot;Perl&quot;),&lt;br /&gt;     (5, &quot;Guido Van Rossum&quot;, &quot;Python&quot;),&lt;br /&gt;     (6, &quot;Yukihiro Matsumoto&quot;, &quot;Ruby&quot;)]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; with open(&quot;founder.csv&quot;, &#39;w&#39;, newline=&#39;&#39;, encoding=&#39;utf-8&#39;) as csvfile:&lt;br /&gt;          w = csv.writer(csvfile, delimiter=&#39;;&#39;)&lt;br /&gt;          w.writerows(data)&lt;br /&gt;          csvfile.close()&lt;br /&gt; &lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; ################ file reading #################&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; with open(&quot;founder.csv&quot;, &#39;r&#39;, newline=&#39;&#39;, encoding=&#39;utf-8&#39;) as csvfile:&lt;br /&gt;          r = csv.reader(csvfile, delimiter=&#39;;&#39;)&lt;br /&gt;          for row in r:&lt;br /&gt;             for i in range(len(row)):&lt;br /&gt;                print(row[i], &#39;\t&#39;, end=&#39;&#39;)&lt;br /&gt;             print()&lt;br /&gt;          csvfile.close()&lt;br /&gt; &lt;br /&gt;1  Dennis Ritchie     C  &lt;br /&gt;2  Bjarne Stroustrup  C++  &lt;br /&gt;3  James Gosling      Java  &lt;br /&gt;4  Larry Wall         Perl  &lt;br /&gt;5  Guido Van Rossum   Python  &lt;br /&gt;6  Yukihiro Matsumoto Ruby  &lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/1918766871014898779/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/csv-excel-file-writing-and-reading.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/1918766871014898779'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/1918766871014898779'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/csv-excel-file-writing-and-reading.html' title='CSV (Excel) File Writing and Reading'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-5853428769038682306</id><published>2013-05-17T10:40:00.000+07:00</published><updated>2013-05-18T06:33:06.630+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="I/O and File"/><title type='text'>Compiling Python Code into Byte Code</title><content type='html'>This code shows you how to compile Python source code (.py) into Python byte code (.pyc).  &lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; import os&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; os.chdir(&quot;E:\\pythoncode&quot;)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; # create a new file&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; f = open(&quot;myfile.py&quot;, &#39;w&#39;)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; f.write(&quot;&quot;&quot;&lt;br /&gt;print(&quot;Hello World!&quot;)&lt;br /&gt;&quot;&quot;&quot;)&lt;br /&gt;23&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; f.close()  # close file&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; # import py_compile module to compile a single file&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; import py_compile&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; py_compile.compile(&quot;myfile.py&quot;)  # compile myfile.py&lt;br /&gt;&#39;__pycache__\\myfile.cpython-32.pyc&#39;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt; Next, you can execute the .pyc file as follows:  &lt;pre&gt;&lt;code&gt;E:\&amp;gt;cd pythoncode&lt;br /&gt;&lt;br /&gt;E:\pythoncode&amp;gt;cd __pycache__&lt;br /&gt;&lt;br /&gt;E:\pythoncode\__pycache__&amp;gt;dir&lt;br /&gt; Volume in drive E has no label.&lt;br /&gt; Volume Serial Number is 18C5-5253&lt;br /&gt;&lt;br /&gt; Directory of E:\pythoncode\__pycache__&lt;br /&gt;&lt;br /&gt;17/05/2013  10:24    &amp;lt;DIR&amp;gt;          .&lt;br /&gt;17/05/2013  10:24    &amp;lt;DIR&amp;gt;          ..&lt;br /&gt;17/05/2013  10:24               137 myfile.cpython-32.pyc&lt;br /&gt;               1 File(s)            137 bytes&lt;br /&gt;               2 Dir(s)  54.142.066.688 bytes free&lt;br /&gt;&lt;br /&gt;E:\pythoncode\__pycache__&amp;gt;python myfile.cpython-32.pyc&lt;br /&gt;Hello World!&lt;br /&gt;&lt;br /&gt;E:\pythoncode\__pycache__&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt; If you want to compile all files in a directory, use &quot;compileall&quot; module.  &lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; import compileall&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; mydir = os.getcwd()  # get current directory&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; compileall.compile_dir(mydir)&lt;br /&gt;Listing &#39;E:\\pythoncode&#39;...&lt;br /&gt;Compiling &#39;E:\\pythoncode\\myfile.py&#39;...&lt;br /&gt;Compiling &#39;E:\\pythoncode\\test.py&#39;...&lt;br /&gt;1&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/5853428769038682306/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/compiling-python-code-into-byte-code.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/5853428769038682306'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/5853428769038682306'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/compiling-python-code-into-byte-code.html' title='Compiling Python Code into Byte Code'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-6708016047936541399</id><published>2013-05-16T13:55:00.000+07:00</published><updated>2013-05-16T17:04:39.141+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="I/O and File"/><title type='text'>The print() Function Without New Line</title><content type='html'>To print a string (or number) without new line, include &lt;b&gt;end=&#39; &#39;&lt;/b&gt; as argument within your print() function.  Example: &lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; items = (10, 20, 30, 40, 50)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; for item in items:&lt;br /&gt; print(item, end=&#39; &#39;)&lt;br /&gt; &lt;br /&gt;10 20 30 40 50 &lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/6708016047936541399/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/the-print-function-without-new-line.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/6708016047936541399'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/6708016047936541399'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/the-print-function-without-new-line.html' title='The print() Function Without New Line'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-6114246696829526157</id><published>2013-05-16T13:34:00.000+07:00</published><updated>2013-05-18T06:34:40.378+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Database"/><title type='text'>SQLite3 + Python</title><content type='html'>This code shows you how to connect SQLite3 database and Python code.  &lt;pre&gt;&lt;code&gt;Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win32&lt;br /&gt;Type &quot;copyright&quot;, &quot;credits&quot; or &quot;license()&quot; for more information.&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; import sqlite3&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; conn = sqlite3.connect(&quot;sqlite3db.db&quot;)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; c = conn.cursor()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; c.execute(&quot;create table author (authorid integer, authorname varchar(25))&quot;)&lt;br /&gt;&amp;lt;sqlite3.Cursor object at 0x0225DB20&amp;gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; data = [(1, &#39;David M. Beazley&#39;), (2, &#39;Mark Summerfield&#39;)]&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; c.executemany(&quot;insert into author values(?, ?)&quot;, data)&lt;br /&gt;&amp;lt;sqlite3.Cursor object at 0x0225DB20&amp;gt;&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; conn.commit()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; resultset = c.execute(&quot;select * from author&quot;)&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; for authorid, authorname in resultset:&lt;br /&gt; print(authorid, &#39;\t&#39;, authorname)&lt;br /&gt; &lt;br /&gt;1   David M. Beazley&lt;br /&gt;2   Mark Summerfield&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; c.close()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt; conn.close()&lt;br /&gt;&amp;gt;&amp;gt;&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/6114246696829526157/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/sqlite3-python.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/6114246696829526157'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/6114246696829526157'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/sqlite3-python.html' title='SQLite3 + Python'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-4877394296205692113</id><published>2013-05-16T12:35:00.001+07:00</published><updated>2013-05-16T12:40:01.758+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="CGI"/><title type='text'>Displaying Image in Python CGI Script</title><content type='html'>To display an image file using CGI script, you must place the file outside &quot;cgi-bin&quot; directory. If you try to access your image file directly from cgi-bin directory, you will get the &quot;Internal Server Error&quot; message on your web browser.   You can access that file from your code as follows:  &lt;pre&gt;&lt;code&gt;print(&quot;&amp;lt;img src=&#39;../images/pythonlogo.jpg&#39;&amp;gt;&quot;)&lt;/code&gt;&lt;/pre&gt; Here&#39;s the complete code:  &lt;pre&gt;&lt;code&gt;#!/usr/bin/python&lt;br /&gt;&lt;br /&gt;print(&quot;Content-type: text/html\r\n\r\n&quot;)&lt;br /&gt;print(&quot;&amp;lt;html&amp;gt;&quot;)&lt;br /&gt;print(&quot;&amp;lt;head&amp;gt;&quot;)&lt;br /&gt;print(&quot;&amp;lt;title&amp;gt;CGI&amp;lt;/title&amp;gt;&quot;)&lt;br /&gt;print(&quot;&amp;lt;/head&amp;gt;&quot;)&lt;br /&gt;print(&quot;&amp;lt;body&amp;gt;&quot;)&lt;br /&gt;print(&quot;&amp;lt;img src=&#39;../images/pythonlogo.jpg&#39; /&amp;gt;&quot;)&lt;br /&gt;print(&quot;&amp;lt;/body&amp;gt;&quot;)&lt;br /&gt;print(&quot;&amp;lt;/html&amp;gt;&quot;)&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/4877394296205692113/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/displaying-image-in-python-cgi-script.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/4877394296205692113'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/4877394296205692113'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/displaying-image-in-python-cgi-script.html' title='Displaying Image in Python CGI Script'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-2340008081033981573</id><published>2013-05-15T10:16:00.001+07:00</published><updated>2013-05-15T10:18:04.461+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Built-In Functions"/><category scheme="http://www.blogger.com/atom/ns#" term="Dictionary"/><title type='text'>The cmp() Function in Python 3</title><content type='html'>Unlike Python 2, Python 3 doesn&#39;t provides the cmp() function. When I want to compare two dictionaries, I write a code like this: &lt;pre&gt;&lt;code&gt;# define function&lt;br /&gt;def cmp(a, b):&lt;br /&gt;   return (a.items() &gt; b.items()) - (a.items() &lt; b.items())&lt;br /&gt;&lt;br /&gt;dict1 = {&#39;one&#39;: 10, &#39;two&#39;: 20, &#39;three&#39;: 30}&lt;br /&gt;dict2 = {&#39;one&#39;: 10, &#39;two&#39;: 20}&lt;br /&gt;&lt;br /&gt;print(&quot;Return value: &quot;, cmp(dict1, dict2))&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;This function compares dict1 and dict2. It returns a negative number if dict1 &lt; dict2, a positive number if dict1 &gt; dict2, or 0 if dict1 == dict2.</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/2340008081033981573/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/the-cmp-function-in-python-3_4698.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/2340008081033981573'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/2340008081033981573'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2013/05/the-cmp-function-in-python-3_4698.html' title='The cmp() Function in Python 3'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-20019988.post-3838145551207503647</id><published>2012-01-29T04:30:00.005+07:00</published><updated>2012-01-29T04:46:15.056+07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Bimbel"/><title type='text'>BEST INFORMATIC | Bimbingan Belajar Pemrograman Sistem Cepat</title><content type='html'>Bagi Anda yang ingin (serius) memiliki skill di bidang pemrograman dalam waktu yang sangat cepat (hanya 1 BULAN), silahkan kunjungi:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;BEST INFORMATIC | Bimbingan Belajar Pemrograman Sistem Cepat&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href=&quot;http://www.best-informatic.com/&quot;&gt;&lt;span style=&quot;font-weight: bold;font-size:180%;&quot; &gt;&lt;span style=&quot;color: rgb(255, 204, 51);&quot;&gt;http://www.best-informatic.com&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;Metode praktis yang sudah terbukti ampuh mencetak siswa dari NOL sampai MAHIR. Menggunakan teknik BEDAH KASUS. Dijamin bisa!</content><link rel='replies' type='application/atom+xml' href='http://mbraharjo.blogspot.com/feeds/3838145551207503647/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mbraharjo.blogspot.com/2012/01/best-informatic-bimbingan-pemrograman.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/3838145551207503647'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20019988/posts/default/3838145551207503647'/><link rel='alternate' type='text/html' href='http://mbraharjo.blogspot.com/2012/01/best-informatic-bimbingan-pemrograman.html' title='BEST INFORMATIC | Bimbingan Belajar Pemrograman Sistem Cepat'/><author><name>Budi Raharjo</name><uri>http://www.blogger.com/profile/01893627813757442876</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>