<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" 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" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;C0MGQ3w8eip7ImA9WhBaFEQ.&quot;"><id>tag:blogger.com,1999:blog-20019988</id><updated>2013-05-25T21:17:02.272+07:00</updated><category term="Tuple" /><category term="Built-In Functions" /><category term="Module" /><category term="Dictionary" /><category term="CGI" /><category term="Database" /><category term="Function" /><category term="Bimbel" /><category term="I/O and File" /><category term="List" /><category term="Class" /><title>&gt;&gt;&gt; python scripting</title><subtitle type="html">"""the common problems in python programming"""</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://mbraharjo.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://mbraharjo.blogspot.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>16</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/BudisBlog" /><feedburner:info uri="budisblog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>BudisBlog</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><entry gd:etag="W/&quot;C0MGQ3wyeyp7ImA9WhBaFEQ.&quot;"><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><app:edited xmlns:app="http://www.w3.org/2007/app">2013-05-25T21:17:02.293+07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Function" /><title>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
Python 3.2.3 (default, Oct 19 2012, 20:13:42) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&amp;gt;&amp;gt;&amp;gt; def func(*args):
...   for i in args:
...     print(i, end=' ')
... 
&amp;gt;&amp;gt;&amp;gt; func(10)         # one argument
10
&amp;gt;&amp;gt;&amp;gt; func(10,20)      # two arguments
10 20 
&amp;gt;&amp;gt;&amp;gt; func(10, 20, 30) # three arguments
10 20 30 
&amp;gt;&amp;gt;&amp;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?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/20019988/posts/default/144537598549717617?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BudisBlog/~3/86Vgm8vD5UA/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><feedburner:origLink>http://mbraharjo.blogspot.com/2013/05/passing-variable-length-arguments.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Ck4FQ3czfip7ImA9WhBaE0s.&quot;"><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><app:edited xmlns:app="http://www.w3.org/2007/app">2013-05-24T09:01:52.986+07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="I/O and File" /><category scheme="http://www.blogger.com/atom/ns#" term="Module" /><title>Creating A "tar" 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/
budi@linux:~/pythoncode$ ls -la
total 20
drwxr-xr-x  2 root root 4096 May 24 08:42 .
drwxr-xr-x 38 budi budi 4096 May 24 08:21 ..
-rwxr-xr-x  1 root root  714 May 24 08:23 test1.py
-rwxr-xr-x  1 root root 2143 May 24 08:22 test2.py
-rwxr-xr-x  1 root root 1429 May 24 08:22 test3.py
budi@linux:~/pythoncode$ sudo python3.2
[sudo] password for budi:
Python 3.2.3 (default, Oct 19 2012, 20:13:42) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&amp;gt;&amp;gt;&amp;gt; import os
&amp;gt;&amp;gt;&amp;gt; os.getcwd()
'/home/budi/pythoncode'
&amp;gt;&amp;gt;&amp;gt; import tarfile, glob
&amp;gt;&amp;gt;&amp;gt; tfile = tarfile.open("test.tar", 'w')
&amp;gt;&amp;gt;&amp;gt; for filename in glob.glob("*.py"):
...    tfile.add(filename)
... 
&amp;gt;&amp;gt;&amp;gt; tfile.close()
&amp;gt;&amp;gt;&amp;gt; exit()
budi@linux:~/pythoncode$ ls -la
total 32
drwxr-xr-x  2 root root  4096 May 24 08:43 .
drwxr-xr-x 38 budi budi  4096 May 24 08:21 ..
-rwxr-xr-x  1 root root   714 May 24 08:23 test1.py
-rwxr-xr-x  1 root root  2143 May 24 08:22 test2.py
-rwxr-xr-x  1 root root  1429 May 24 08:22 test3.py
&lt;strong&gt;-rw-r--r--  1 root root 10240 May 24 08:45 test.tar&lt;/strong&gt;
budi@linux:~/pythoncode$ 
&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?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/20019988/posts/default/5621508360783232662?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BudisBlog/~3/6UvJDpMoYO4/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><feedburner:origLink>http://mbraharjo.blogspot.com/2013/05/creating-tar-file.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEMBQH4-cCp7ImA9WhBaEkQ.&quot;"><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><app:edited xmlns:app="http://www.w3.org/2007/app">2013-05-23T15:07:31.058+07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="I/O and File" /><category scheme="http://www.blogger.com/atom/ns#" term="Database" /><category scheme="http://www.blogger.com/atom/ns#" term="Module" /><title>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
&amp;gt;&amp;gt;&amp;gt; import csv
&amp;gt;&amp;gt;&amp;gt; data = [(1,10), (2,20), (3,30), (4,40), (5,50)]
&amp;gt;&amp;gt;&amp;gt; with open("data.csv", 'w', newline='', encoding='utf-8') as csvfile:
          w = csv.writer(csvfile, delimiter=';')
          w.writerows(data)
          csvfile.close()
 
&amp;gt;&amp;gt;&amp;gt; # STEP 2: Create a table with two integer fields on your SQLite database
&amp;gt;&amp;gt;&amp;gt; import sqlite3
&amp;gt;&amp;gt;&amp;gt; conn = sqlite3.connect("sqlite3db.db")
&amp;gt;&amp;gt;&amp;gt; c = conn.cursor()
&amp;gt;&amp;gt;&amp;gt; c.execute("create table test(a integer, b integer)")
&amp;lt;sqlite3.Cursor object at 0x020CDE20&amp;gt;
&amp;gt;&amp;gt;&amp;gt; c.close()
&amp;gt;&amp;gt;&amp;gt; conn.close()
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; # STEP 3: Import your data.csv into test table
&amp;gt;&amp;gt;&amp;gt; # import csv, sqlite3
&amp;gt;&amp;gt;&amp;gt; with open("data.csv", 'r', newline='', encoding='utf-8') as csvfile:
          conn = sqlite3.connect("sqlite3db.db")
          c = conn.cursor()
          r = csv.reader(csvfile, delimiter=';')
          for row in r:
             c.execute("insert into test values(?,?)", (row[0], row[1]))
          conn.commit()
          csvfile.close()
 
&amp;lt;sqlite3.Cursor object at 0x020CDEA0&amp;gt;
&amp;lt;sqlite3.Cursor object at 0x020CDEA0&amp;gt;
&amp;lt;sqlite3.Cursor object at 0x020CDEA0&amp;gt;
&amp;lt;sqlite3.Cursor object at 0x020CDEA0&amp;gt;
&amp;lt;sqlite3.Cursor object at 0x020CDEA0&amp;gt;
&amp;gt;&amp;gt;&amp;gt; # now, check your data on the table
&amp;gt;&amp;gt;&amp;gt; for (a, b) in c.execute("select a, b from test"):
          print("%s\t%s" % (a, b))
 
1     10
2     20
3     30
4     40
5     50
&amp;gt;&amp;gt;&amp;gt; conn.close()
&amp;gt;&amp;gt;&amp;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?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/20019988/posts/default/865355541413547951?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BudisBlog/~3/7kxR3wfMiWs/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><feedburner:origLink>http://mbraharjo.blogspot.com/2013/05/importing-excel-file-into-sqlite-table.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkYERnc5fyp7ImA9WhBaEUU.&quot;"><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><app:edited xmlns:app="http://www.w3.org/2007/app">2013-05-22T06:48:27.927+07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="I/O and File" /><category scheme="http://www.blogger.com/atom/ns#" term="Built-In Functions" /><category scheme="http://www.blogger.com/atom/ns#" term="Module" /><title>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
&amp;gt;&amp;gt;&amp;gt; import os
&amp;gt;&amp;gt;&amp;gt; os.chdir("E:\\pythoncode")
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; # creating zip file
&amp;gt;&amp;gt;&amp;gt; import zipfile
&amp;gt;&amp;gt;&amp;gt; files = ("abs.py", "floor.py", "exp.py", "log.py", "log10.py")
&amp;gt;&amp;gt;&amp;gt; zf = zipfile.ZipFile("test.zip", 'w', compression=zipfile.ZIP_DEFLATED)
&amp;gt;&amp;gt;&amp;gt; for file in files:
          zf.write(file)
 
&amp;gt;&amp;gt;&amp;gt; zf.close()
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; # reading data from zip file
&amp;gt;&amp;gt;&amp;gt; zf = zipfile.ZipFile("test.zip", 'r')
&amp;gt;&amp;gt;&amp;gt; for filename in zf.namelist():
          nbytes = len(zf.read(filename))
          print(filename, '\t', nbytes, " bytes")
 
abs.py      143  bytes
floor.py    199  bytes
exp.py      179  bytes
log.py      179  bytes
log10.py    194  bytes
&amp;gt;&amp;gt;&amp;gt; zf.close()
&amp;gt;&amp;gt;&amp;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?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/20019988/posts/default/6574784022543626392?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BudisBlog/~3/-fy1r_ibzjU/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><feedburner:origLink>http://mbraharjo.blogspot.com/2013/05/compressing-files-into-zip-file.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkUMSXw9eCp7ImA9WhBaEU4.&quot;"><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><app:edited xmlns:app="http://www.w3.org/2007/app">2013-05-21T16:58:08.260+07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="I/O and File" /><category scheme="http://www.blogger.com/atom/ns#" term="Database" /><category scheme="http://www.blogger.com/atom/ns#" term="Module" /><title>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
&amp;gt;&amp;gt;&amp;gt; conn = sqlite3.connect(":memory:")
&amp;gt;&amp;gt;&amp;gt; c = conn.cursor()
&amp;gt;&amp;gt;&amp;gt; c.execute("create table test(c1 int, c2 int)")
&amp;lt;sqlite3.Cursor object at 0x0223DD20&amp;gt;
&amp;gt;&amp;gt;&amp;gt; data = [(1,10), (2,20), (3,30), (4,40), (5,50)]
&amp;gt;&amp;gt;&amp;gt; c.executemany("insert into test values(?,?)", data)
&amp;lt;sqlite3.Cursor object at 0x0223DD20&amp;gt;
&amp;gt;&amp;gt;&amp;gt; conn.commit()
&amp;gt;&amp;gt;&amp;gt; with open("test.csv", 'w', newline='', encoding='utf-8') as csvfile:
          w = csv.writer(csvfile, delimiter=';')
          for row in c.execute("select * from test"):
             w.writerow(row)
  
6
6
6
6
6
&amp;gt;&amp;gt;&amp;gt; csvfile.close()
&amp;gt;&amp;gt;&amp;gt; c.close()
&amp;gt;&amp;gt;&amp;gt; conn.close()
&amp;gt;&amp;gt;&amp;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?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/20019988/posts/default/5245148202323368088?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BudisBlog/~3/jgruJiCGN7o/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><feedburner:origLink>http://mbraharjo.blogspot.com/2013/05/exporting-sqlite-table-into-excep-file.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CU8AQ3c7eyp7ImA9WhBaEEw.&quot;"><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><app:edited xmlns:app="http://www.w3.org/2007/app">2013-05-20T08:37:22.903+07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Database" /><title>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="margin-left:0px;"&gt;
&lt;li&gt;MySQL Server; it can be found &lt;a href="http://dev.mysql.com/downloads/mysql/"&gt;here&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;MySQL Connector/Python; it can be found &lt;a href="http://dev.mysql.com/downloads/connector/python/"&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
&amp;gt;&amp;gt;&amp;gt; conn = mysql.connector.connect(user="budi", password="r4h4Rj0",
          host="127.0.0.1", database="pythondb")
&amp;gt;&amp;gt;&amp;gt; c = conn.cursor()
&amp;gt;&amp;gt;&amp;gt; # creating table
&amp;gt;&amp;gt;&amp;gt; c.execute("""
create table books(
   isbn char(17) not null,
   title varchar(50),
   author varchar(25),
   publisher varchar(25),
   constraint books_pk primary key(isbn))
""")
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; # inserting data into table
&amp;gt;&amp;gt;&amp;gt; data = [
('978-0-672-32862-6','Python Essential Reference','David Beazley','Addison-Wesley'),
('978-0-321-56615-7','Programming in Objective-C','Stephen G. Kotchan','Addison-Wesley'),
('978-0-321-68056-3','Programming in Python 3','Mark Summerfield','Addison-Wesley')]
&amp;gt;&amp;gt;&amp;gt; c.executemany("insert into books values(%s, %s, %s, %s)", data)
&amp;gt;&amp;gt;&amp;gt; conn.commit()
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; # performing query
&amp;gt;&amp;gt;&amp;gt; c.execute("select * from books")
&amp;gt;&amp;gt;&amp;gt; 
&amp;gt;&amp;gt;&amp;gt; # fetching table rows
&amp;gt;&amp;gt;&amp;gt; resultset = c.fetchall()
&amp;gt;&amp;gt;&amp;gt; for isbn, title, author, publisher in resultset:
          print("%s\t%s\t%s\t%s" % (isbn, title, author, publisher))
 
978-0-321-56615-7  Programming in Objective-C  Stephen G. Kotchan  Addison-Wesley
978-0-321-68056-3  Programming in Python 3     Mark Summerfield    Addison-Wesley
978-0-672-32862-6  Python Essential Reference  David Beazley       Addison-Wesley
&amp;gt;&amp;gt;&amp;gt; c.close()
&amp;gt;&amp;gt;&amp;gt; conn.close()
&amp;gt;&amp;gt;&amp;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?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/20019988/posts/default/7683994565701965798?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BudisBlog/~3/AWA3z_Lwaz8/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><feedburner:origLink>http://mbraharjo.blogspot.com/2013/05/mysql-python.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkUMQ3szfip7ImA9WhBaEE0.&quot;"><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><app:edited xmlns:app="http://www.w3.org/2007/app">2013-05-20T04:51:22.586+07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="I/O and File" /><category scheme="http://www.blogger.com/atom/ns#" term="Built-In Functions" /><title>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("yourfile")&lt;/code&gt;&lt;/pre&gt;

or

&lt;pre&gt;&lt;code&gt;os.stat("yourfile").st_size&lt;/code&gt;&lt;/pre&gt;

Example:
&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; import os
&amp;gt;&amp;gt;&amp;gt; os.path.getsize("E:\\pythoncode\\test.py")
13
&amp;gt;&amp;gt;&amp;gt; os.stat("E:\\pythoncode\\test.py").st_size
13
&amp;gt;&amp;gt;&amp;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?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/20019988/posts/default/7582297397673113374?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BudisBlog/~3/ngSKhQb7Rkg/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><feedburner:origLink>http://mbraharjo.blogspot.com/2013/05/getting-file-size-in-python.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0cCQnwzeCp7ImA9WhBbGU8.&quot;"><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><app:edited xmlns:app="http://www.w3.org/2007/app">2013-05-19T06:51:03.280+07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="I/O and File" /><category scheme="http://www.blogger.com/atom/ns#" term="Function" /><title>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
&amp;gt;&amp;gt;&amp;gt; def ls(path):
          names = os.listdir(path)
          for name in names:
             isdir = os.path.isdir(os.path.join(path, name))
             if isdir:
               s = name + "\t\t\t&amp;lt;DIR&amp;gt;"
             else:
               s = name
             print(s)

&amp;gt;&amp;gt;&amp;gt; samplepath = "C:\\Python32"
&amp;gt;&amp;gt;&amp;gt; ls(samplepath)
django-admin.py
DLLs                    &amp;lt;DIR&amp;gt;
Doc                     &amp;lt;DIR&amp;gt;
include                 &amp;lt;DIR&amp;gt;
Lib                     &amp;lt;DIR&amp;gt;
libs                    &amp;lt;DIR&amp;gt;
LICENSE.txt
NEWS.txt
python.exe
pythonw.exe
qt.conf
README.txt
Scripts                 &amp;lt;DIR&amp;gt;
tcl                     &amp;lt;DIR&amp;gt;
Tools                   &amp;lt;DIR&amp;gt;
w9xpopen.exe
&amp;gt;&amp;gt;&amp;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?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/20019988/posts/default/7174009107246107899?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BudisBlog/~3/qDpg4aFM_oM/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><feedburner:origLink>http://mbraharjo.blogspot.com/2013/05/listing-files-and-subdirectories-in.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0QASHo5cSp7ImA9WhBbGEs.&quot;"><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><app:edited xmlns:app="http://www.w3.org/2007/app">2013-05-18T15:22:29.429+07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Class" /><category scheme="http://www.blogger.com/atom/ns#" term="Module" /><title>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
&amp;gt;&amp;gt;&amp;gt; import math
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; # define an abstract class
&amp;gt;&amp;gt;&amp;gt; class Shape(metaclass=ABCMeta):
          @abstractmethod
          def area(self): pass
          @abstractmethod
          def circumference(self): pass

&amp;gt;&amp;gt;&amp;gt; # first descendant class
&amp;gt;&amp;gt;&amp;gt; class Rectangle(Shape):
           def __init__(self, w, h):
              self.width = w
              self.height = h
           # overriding Shape's area()
           def area(self):
              return self.width * self.height
           # overriding Shape's circumference()
           def circumference(self):
              return 2 * (self.width + self.height)

&amp;gt;&amp;gt;&amp;gt; # second descendant class
&amp;gt;&amp;gt;&amp;gt; class Circle(Shape):
           def __init__(self, r):
              self.radius = r
           # overriding Shape's area()
           def area(self):
              return math.pi * (self.radius ** 2)
           # overriding Shape's circumference()
           def circumference(self):
              return 2 * math.pi * self.radius

&amp;gt;&amp;gt;&amp;gt; # you can't create an instance of Shape class
&amp;gt;&amp;gt;&amp;gt; obj = Shape()
Traceback (most recent call last):
  File "&lt;pyshell#33&gt;", line 1, in &lt;module&gt;
    obj = Shape()
TypeError: Can&amp;rsquo;t instantiate abstract class Shape with abstract methods area, 
circumference
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; # create an instance of Rectangle
&amp;gt;&amp;gt;&amp;gt; obj1 = Rectangle(5, 3)
&amp;gt;&amp;gt;&amp;gt; obj1.area()  # call Rectangle's area()
15
&amp;gt;&amp;gt;&amp;gt; obj1.circumference()
16
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; # create an instance of Circle
&amp;gt;&amp;gt;&amp;gt; obj2 = Circle(3)
&amp;gt;&amp;gt;&amp;gt; obj2.area()  # call Circle's area()
28.274333882308138
&amp;gt;&amp;gt;&amp;gt; obj2.circumference()
18.84955592153876
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

If you're using Python 2, use this code:

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; # define an abstract class
&amp;gt;&amp;gt;&amp;gt; class Shape:
          __metaclass__= ABCMeta
          @abstractmethod
          def area(self): pass
          @abstractmethod
          def circumference(self): pass
&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?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/20019988/posts/default/5293870056413231959?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BudisBlog/~3/hdDChqIs1zw/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><feedburner:origLink>http://mbraharjo.blogspot.com/2013/05/defining-abstract-class-and-abstract.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0QDSX45eCp7ImA9WhBbGEs.&quot;"><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><app:edited xmlns:app="http://www.w3.org/2007/app">2013-05-18T15:22:58.020+07:00</app:edited><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="Tuple" /><category scheme="http://www.blogger.com/atom/ns#" term="Module" /><title>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
&amp;gt;&amp;gt;&amp;gt; ################ file writing #################
&amp;gt;&amp;gt;&amp;gt; data = [(1, "Dennis Ritchie", "C"),
     (2, "Bjarne Stroustrup", "C++"),
     (3, "James Gosling", "Java"),
     (4, "Larry Wall", "Perl"),
     (5, "Guido Van Rossum", "Python"),
     (6, "Yukihiro Matsumoto", "Ruby")]
&amp;gt;&amp;gt;&amp;gt; with open("founder.csv", 'w', newline='', encoding='utf-8') as csvfile:
          w = csv.writer(csvfile, delimiter=';')
          w.writerows(data)
          csvfile.close()
 
&amp;gt;&amp;gt;&amp;gt; ################ file reading #################
&amp;gt;&amp;gt;&amp;gt; with open("founder.csv", 'r', newline='', encoding='utf-8') as csvfile:
          r = csv.reader(csvfile, delimiter=';')
          for row in r:
             for i in range(len(row)):
                print(row[i], '\t', end='')
             print()
          csvfile.close()
 
1  Dennis Ritchie     C  
2  Bjarne Stroustrup  C++  
3  James Gosling      Java  
4  Larry Wall         Perl  
5  Guido Van Rossum   Python  
6  Yukihiro Matsumoto Ruby  
&amp;gt;&amp;gt;&amp;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?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/20019988/posts/default/1918766871014898779?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BudisBlog/~3/_7rGxgPAH_o/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><feedburner:origLink>http://mbraharjo.blogspot.com/2013/05/csv-excel-file-writing-and-reading.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUIMR3g4eCp7ImA9WhBbGE4.&quot;"><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><app:edited xmlns:app="http://www.w3.org/2007/app">2013-05-18T06:33:06.630+07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="I/O and File" /><title>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
&amp;gt;&amp;gt;&amp;gt; os.chdir("E:\\pythoncode")
&amp;gt;&amp;gt;&amp;gt; # create a new file
&amp;gt;&amp;gt;&amp;gt; f = open("myfile.py", 'w')
&amp;gt;&amp;gt;&amp;gt; f.write("""
print("Hello World!")
""")
23
&amp;gt;&amp;gt;&amp;gt; f.close()  # close file
&amp;gt;&amp;gt;&amp;gt; # import py_compile module to compile a single file
&amp;gt;&amp;gt;&amp;gt; import py_compile
&amp;gt;&amp;gt;&amp;gt; py_compile.compile("myfile.py")  # compile myfile.py
'__pycache__\\myfile.cpython-32.pyc'
&amp;gt;&amp;gt;&amp;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

E:\pythoncode&amp;gt;cd __pycache__

E:\pythoncode\__pycache__&amp;gt;dir
 Volume in drive E has no label.
 Volume Serial Number is 18C5-5253

 Directory of E:\pythoncode\__pycache__

17/05/2013  10:24    &amp;lt;DIR&amp;gt;          .
17/05/2013  10:24    &amp;lt;DIR&amp;gt;          ..
17/05/2013  10:24               137 myfile.cpython-32.pyc
               1 File(s)            137 bytes
               2 Dir(s)  54.142.066.688 bytes free

E:\pythoncode\__pycache__&amp;gt;python myfile.cpython-32.pyc
Hello World!

E:\pythoncode\__pycache__&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

If you want to compile all files in a directory, use "compileall" module.

&lt;pre&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; import compileall
&amp;gt;&amp;gt;&amp;gt; mydir = os.getcwd()  # get current directory
&amp;gt;&amp;gt;&amp;gt; compileall.compile_dir(mydir)
Listing 'E:\\pythoncode'...
Compiling 'E:\\pythoncode\\myfile.py'...
Compiling 'E:\\pythoncode\\test.py'...
1
&amp;gt;&amp;gt;&amp;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?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/20019988/posts/default/5853428769038682306?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BudisBlog/~3/e6-ffLGH1S0/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><feedburner:origLink>http://mbraharjo.blogspot.com/2013/05/compiling-python-code-into-byte-code.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkEDSH8_eSp7ImA9WhBbFkQ.&quot;"><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><app:edited xmlns:app="http://www.w3.org/2007/app">2013-05-16T17:04:39.141+07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="I/O and File" /><title>The print() Function Without New Line</title><content type="html">To print a string (or number) without new line, include &lt;b&gt;end=' '&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)
&amp;gt;&amp;gt;&amp;gt; for item in items:
 print(item, end=' ')
 
10 20 30 40 50 
&amp;gt;&amp;gt;&amp;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?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/20019988/posts/default/6708016047936541399?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BudisBlog/~3/DrZrOJlBIxI/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><feedburner:origLink>http://mbraharjo.blogspot.com/2013/05/the-print-function-without-new-line.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUEMQX08cCp7ImA9WhBbGE4.&quot;"><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><app:edited xmlns:app="http://www.w3.org/2007/app">2013-05-18T06:34:40.378+07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Database" /><title>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
Type "copyright", "credits" or "license()" for more information.
&amp;gt;&amp;gt;&amp;gt; import sqlite3
&amp;gt;&amp;gt;&amp;gt; conn = sqlite3.connect("sqlite3db.db")
&amp;gt;&amp;gt;&amp;gt; c = conn.cursor()
&amp;gt;&amp;gt;&amp;gt; c.execute("create table author (authorid integer, authorname varchar(25))")
&amp;lt;sqlite3.Cursor object at 0x0225DB20&amp;gt;
&amp;gt;&amp;gt;&amp;gt; data = [(1, 'David M. Beazley'), (2, 'Mark Summerfield')]
&amp;gt;&amp;gt;&amp;gt; c.executemany("insert into author values(?, ?)", data)
&amp;lt;sqlite3.Cursor object at 0x0225DB20&amp;gt;
&amp;gt;&amp;gt;&amp;gt; conn.commit()
&amp;gt;&amp;gt;&amp;gt; resultset = c.execute("select * from author")
&amp;gt;&amp;gt;&amp;gt; for authorid, authorname in resultset:
 print(authorid, '\t', authorname)
 
1   David M. Beazley
2   Mark Summerfield
&amp;gt;&amp;gt;&amp;gt; c.close()
&amp;gt;&amp;gt;&amp;gt; conn.close()
&amp;gt;&amp;gt;&amp;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?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/20019988/posts/default/6114246696829526157?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BudisBlog/~3/kVe1TxBso_8/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><feedburner:origLink>http://mbraharjo.blogspot.com/2013/05/sqlite3-python.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CE8EQHk-cCp7ImA9WhBbFkU.&quot;"><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><app:edited xmlns:app="http://www.w3.org/2007/app">2013-05-16T12:40:01.758+07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="CGI" /><title>Displaying Image in Python CGI Script</title><content type="html">To display an image file using CGI script, you must place the file outside "cgi-bin" directory. If you try to access your image file directly from cgi-bin directory, you will get the "Internal Server Error" message on your web browser.


You can access that file from your code as follows:

&lt;pre&gt;&lt;code&gt;print("&amp;lt;img src='../images/pythonlogo.jpg'&amp;gt;")&lt;/code&gt;&lt;/pre&gt;

Here's the complete code:

&lt;pre&gt;&lt;code&gt;#!/usr/bin/python

print("Content-type: text/html\r\n\r\n")
print("&amp;lt;html&amp;gt;")
print("&amp;lt;head&amp;gt;")
print("&amp;lt;title&amp;gt;CGI&amp;lt;/title&amp;gt;")
print("&amp;lt;/head&amp;gt;")
print("&amp;lt;body&amp;gt;")
print("&amp;lt;img src='../images/pythonlogo.jpg' /&amp;gt;")
print("&amp;lt;/body&amp;gt;")
print("&amp;lt;/html&amp;gt;")&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?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/20019988/posts/default/4877394296205692113?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BudisBlog/~3/oroRWAgP914/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><feedburner:origLink>http://mbraharjo.blogspot.com/2013/05/displaying-image-in-python-cgi-script.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DU8MRXo9eSp7ImA9WhBbFUU.&quot;"><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><app:edited xmlns:app="http://www.w3.org/2007/app">2013-05-15T10:18:04.461+07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Dictionary" /><category scheme="http://www.blogger.com/atom/ns#" term="Built-In Functions" /><title>The cmp() Function in Python 3</title><content type="html">Unlike Python 2, Python 3 doesn'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
def cmp(a, b):
   return (a.items() &gt; b.items()) - (a.items() &lt; b.items())

dict1 = {'one': 10, 'two': 20, 'three': 30}
dict2 = {'one': 10, 'two': 20}

print("Return value: ", cmp(dict1, dict2))
&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?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/20019988/posts/default/2340008081033981573?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BudisBlog/~3/1Rrb9PC1_oU/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><feedburner:origLink>http://mbraharjo.blogspot.com/2013/05/the-cmp-function-in-python-3_4698.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUYDRH4-fip7ImA9WhRUF0U.&quot;"><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><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-29T04:46:15.056+07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Bimbel" /><title>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="font-weight: bold;"&gt;BEST INFORMATIC | Bimbingan Belajar Pemrograman Sistem Cepat&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.best-informatic.com/"&gt;&lt;span style="font-weight: bold;font-size:180%;" &gt;&lt;span style="color: rgb(255, 204, 51);"&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?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/20019988/posts/default/3838145551207503647?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/BudisBlog/~3/QTsUZ7QUlkA/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><feedburner:origLink>http://mbraharjo.blogspot.com/2012/01/best-informatic-bimbingan-pemrograman.html</feedburner:origLink></entry></feed>
