<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Oracle And Python</title>
	<atom:link href="http://oracleandpython.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://oracleandpython.wordpress.com</link>
	<description>Combined and Separate</description>
	<lastBuildDate>Mon, 02 Feb 2009 22:20:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='oracleandpython.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Oracle And Python</title>
		<link>http://oracleandpython.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://oracleandpython.wordpress.com/osd.xml" title="Oracle And Python" />
	<atom:link rel='hub' href='http://oracleandpython.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Pie Charts and ratio_to_report</title>
		<link>http://oracleandpython.wordpress.com/2009/01/30/pie-charts-and-ratio_to_report/</link>
		<comments>http://oracleandpython.wordpress.com/2009/01/30/pie-charts-and-ratio_to_report/#comments</comments>
		<pubDate>Fri, 30 Jan 2009 17:41:55 +0000</pubDate>
		<dc:creator>ezuall</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://oracleandpython.wordpress.com/?p=178</guid>
		<description><![CDATA[It has been a while since the last tutorial, so to ease back into this series I&#8217;m presenting a simple guide to creating pie charts.  As before we will be using python,  matplotlib and cx_Oracle to generate a practical and useful example. The source code listing is below, anything not explained in the subsequent lines [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oracleandpython.wordpress.com&amp;blog=5058451&amp;post=178&amp;subd=oracleandpython&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It has been a while since the last tutorial, so to ease back into this series I&#8217;m presenting a simple guide to creating pie charts.  As before we will be using python,  matplotlib and cx_Oracle to generate a practical and useful example.</p>
<p>The source code listing is below, anything not explained in the subsequent lines is already covered by the <a title="index" href="http://oracleandpython.wordpress.com/index/" target="_self">previous tutorials</a>.</p>
<div style="border:2px double #cc0000;background:#dddddd none repeat scroll 0 0;overflow:auto;width:400px;font-size:8pt;padding:20px;">
<pre><span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">001 </span>#!/usr/bin/env python
</span></span><span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">002 </span></span></span>
<span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">003 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;">import</span> <span style="color:#000000;">matplotlib</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">004 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;">from</span> <span style="color:#000000;">matplotlib</span> <span style="color:#c00000;">import</span> <span style="color:#000000;">pyplot</span> <span style="color:#c00000;">as</span> <span style="color:#000000;">plot</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">005 </span></span></span>
<span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">006 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;">import</span> <span style="color:#000000;">cx_Oracle</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">007 </span></span></span>
<span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">008 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">query</span> <span style="color:#0000c0;">=</span> <span style="color:#004080;">"""select name, (ratio_to_report(value) over() ) * 100
</span></span><span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">009 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#004080;">              from v$sga"""</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">010 </span></span></span>
<span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">011 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">connection_string</span> <span style="color:#0000c0;">=</span> <span style="color:#004080;">"pyuser/pypassword@orcl"</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">012 </span></span></span>
<span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">013 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">connection</span> <span style="color:#0000c0;">=</span> <span style="color:#000000;">cx_Oracle</span><span style="color:#0000c0;">.</span><span style="color:#000000;">Connection</span><span style="color:#0000c0;">(</span><span style="color:#000000;">connection_string</span><span style="color:#0000c0;">)</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">014 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">cursor</span> <span style="color:#0000c0;">=</span> <span style="color:#000000;">cx_Oracle</span><span style="color:#0000c0;">.</span><span style="color:#000000;">Cursor</span><span style="color:#0000c0;">(</span><span style="color:#000000;">connection</span><span style="color:#0000c0;">)</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">015 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">cursor</span><span style="color:#0000c0;">.</span><span style="color:#000000;">execute</span><span style="color:#0000c0;">(</span><span style="color:#000000;">query</span><span style="color:#0000c0;">)</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">016 </span></span></span>
<span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">017 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">pie_labels</span> <span style="color:#0000c0;">=</span> <span style="color:#0000c0;">[</span><span style="color:#0000c0;">]</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">018 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">pie_data</span>   <span style="color:#0000c0;">=</span> <span style="color:#0000c0;">[</span><span style="color:#0000c0;">]</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">019 </span></span></span>
<span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">020 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;">for</span> <span style="color:#000000;">row</span> <span style="color:#c00000;">in</span> <span style="color:#000000;">cursor</span><span style="color:#0000c0;">.</span><span style="color:#000000;">fetchall</span><span style="color:#0000c0;">(</span><span style="color:#0000c0;">)</span><span style="color:#0000c0;">:</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">021 </span></span></span><span style="font-family:Lucida,Courier New;">  <span style="color:#000000;">pie_labels</span><span style="color:#0000c0;">.</span><span style="color:#000000;">append</span><span style="color:#0000c0;">(</span><span style="color:#000000;">row</span><span style="color:#0000c0;">[</span><span style="color:#0080c0;">0</span><span style="color:#0000c0;">]</span><span style="color:#0000c0;">)</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">022 </span></span></span><span style="font-family:Lucida,Courier New;">  <span style="color:#000000;">pie_data</span><span style="color:#0000c0;">.</span><span style="color:#000000;">append</span><span style="color:#0000c0;">(</span><span style="color:#000000;">row</span><span style="color:#0000c0;">[</span><span style="color:#0080c0;">1</span><span style="color:#0000c0;">]</span><span style="color:#0000c0;">)</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">023 </span></span></span>
<span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">024 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">figure</span> <span style="color:#0000c0;">=</span> <span style="color:#000000;">plot</span><span style="color:#0000c0;">.</span><span style="color:#000000;">figure</span><span style="color:#0000c0;">(</span><span style="color:#0000c0;">)</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">025 </span></span></span>
<span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">026 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">axis</span> <span style="color:#0000c0;">=</span> <span style="color:#000000;">figure</span><span style="color:#0000c0;">.</span><span style="color:#000000;">add_subplot</span><span style="color:#0000c0;">(</span><span style="color:#0080c0;">111</span><span style="color:#0000c0;">)</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">027 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">axis</span><span style="color:#0000c0;">.</span><span style="color:#000000;">pie</span><span style="color:#0000c0;">(</span><span style="color:#000000;">pie_data</span><span style="color:#0000c0;">,</span> <span style="color:#000000;">labels</span><span style="color:#0000c0;">=</span><span style="color:#000000;">pie_labels</span><span style="color:#0000c0;">)</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">028 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">axis</span><span style="color:#0000c0;">.</span><span style="color:#000000;">legend</span><span style="color:#0000c0;">(</span><span style="color:#000000;">pie_data</span><span style="color:#0000c0;">,</span> <span style="color:#000000;">loc</span><span style="color:#0000c0;">=</span><span style="color:#004080;">'best'</span><span style="color:#0000c0;">)</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">029 </span></span></span>
<span style="font-family:Lucida,Courier New;"><span style="color:#008000;"><span style="color:#888888;">030 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">plot</span><span style="color:#0000c0;">.</span><span style="color:#000000;">show</span><span style="color:#0000c0;">(</span><span style="color:#0000c0;">)</span></span></pre>
</div>
<p><strong>008-009 ratio_to_report</strong></p>
<p>This query uses the ratio_to_report oracle function to return every row as a ratio of the sum of the rows.   Multiplying by 100 results in a percentage which is perfect for use with pie charts.   Note that you won&#8217;t always want to use percentages, sometimes the actual value supplies more information.</p>
<p><strong>017-018 Labels and Data</strong></p>
<p>These lines declare two arrays that will be used to store the labels and values retrieved from the database.</p>
<p><strong>027 axis.pie(pie_data, labels=pie_labels)</strong></p>
<p>In line 27 we see a new command.  This is the command used to generate a pie-chart, and in the simple form as it is here, it takes two arguments.  The first is an array of data to be used for the plot and the second is an array containing the corresponding labels for the data.</p>
<p><strong>028 axis.legend(pie_data, loc=&#8217;best&#8217;) </strong></p>
<p>The final line of note is 28,  where we create and place the chart legend.  The two arguments to supply in this case are the data array and the positioning of the legend.  The &#8216;best&#8217; flag means that matplotlib will try to automatically place the legend in an appropriate location.  Other positioning flags you could use are:</p>
<ul>
<li><em>upper right</em></li>
<li><em> upper left</em></li>
<li><em> lower left</em></li>
<li><em> lower right</em></li>
<li><em> right</em></li>
<li><em> center left</em></li>
<li><em> center right</em></li>
<li><em> lower center</em></li>
<li><em> upper center</em></li>
<li><em> center</em></li>
</ul>
<p>Below we can see the final result.</p>
<div id="attachment_186" class="wp-caption aligncenter" style="width: 310px"><a href="http://oracleandpython.files.wordpress.com/2009/01/pie_small.jpg"><img class="size-medium wp-image-186" title="pie_small" src="http://oracleandpython.files.wordpress.com/2009/01/pie_small.jpg?w=300&#038;h=225" alt="Pie Chart" width="300" height="225" /></a><p class="wp-caption-text">Pie Chart</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/oracleandpython.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/oracleandpython.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/oracleandpython.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/oracleandpython.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/oracleandpython.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/oracleandpython.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/oracleandpython.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/oracleandpython.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/oracleandpython.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/oracleandpython.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/oracleandpython.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/oracleandpython.wordpress.com/178/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/oracleandpython.wordpress.com/178/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/oracleandpython.wordpress.com/178/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oracleandpython.wordpress.com&amp;blog=5058451&amp;post=178&amp;subd=oracleandpython&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://oracleandpython.wordpress.com/2009/01/30/pie-charts-and-ratio_to_report/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/839b0f3a5a89f6420a2b33f5160d519f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ezuall</media:title>
		</media:content>

		<media:content url="http://oracleandpython.files.wordpress.com/2009/01/pie_small.jpg?w=300" medium="image">
			<media:title type="html">pie_small</media:title>
		</media:content>
	</item>
		<item>
		<title>cx_Oracle and matplotlib</title>
		<link>http://oracleandpython.wordpress.com/2008/11/28/cx_oracle-and-matplotlib/</link>
		<comments>http://oracleandpython.wordpress.com/2008/11/28/cx_oracle-and-matplotlib/#comments</comments>
		<pubDate>Thu, 27 Nov 2008 23:36:03 +0000</pubDate>
		<dc:creator>ezuall</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[matplotlib]]></category>
		<category><![CDATA[cx_Oracle]]></category>

		<guid isPermaLink="false">http://oracleandpython.wordpress.com/?p=134</guid>
		<description><![CDATA[Using what you&#8217;ve learnt from the first two cx_Oracle tutorials you can now apply this knowledge in a useful way.  To me this often implies some graphic representation.  This tutorial will take you through the simple process of plotting data retrieved from an Oracle database using cx_Oracle to a graph using the matplotlib library.  Matplotlib [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oracleandpython.wordpress.com&amp;blog=5058451&amp;post=134&amp;subd=oracleandpython&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Using what you&#8217;ve learnt from the first two cx_Oracle tutorials you can now apply this knowledge in a useful way.  To me this often implies some graphic representation.  This tutorial will take you through the simple process of plotting data retrieved from an Oracle database using cx_Oracle to a graph using the matplotlib library.  Matplotlib is a library that makes plotting graphs very straightforward and I strongly encourage you to look at the great <a title="matplotlib" href="http://matplotlib.sourceforge.net/" target="_self">examples</a> on the matplotlib site.</p>
<p>As is customary by now, we begin with the source code:</p>
<div style="border:2px double #cc0000;background:#dddddd none repeat scroll 0 0;overflow:auto;width:400px;font-size:8pt;padding:20px;">
<pre><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">001 </span>import</span> <span style="color:#000000;">matplotlib</span>
</span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">002 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;">from</span> <span style="color:#000000;">matplotlib</span> <span style="color:#c00000;">import</span> <span style="color:#000000;">pyplot</span> <span style="color:#c00000;">as</span> <span style="color:#000000;">plot</span>
</span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">003 </span></span>
<span style="color:#808080;"><span style="font-family:Lucida,Courier New;">004 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;">import</span> <span style="color:#000000;">cx_Oracle</span>
</span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">005 </span></span>
<span style="color:#808080;"><span style="font-family:Lucida,Courier New;">006 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">query</span> <span style="color:#0000c0;">=</span> <span style="color:#004080;">"""select size_factor as "Size Factor",
</span></span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">007 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#004080;">           estd_physical_reads as "Physical Reads"
</span></span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">008 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#004080;">           from v$db_cache_advice
</span></span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">009 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#004080;">           where block_size = 8192
</span></span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">010 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#004080;">           order by size_factor"""</span>
</span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">011 </span></span>
<span style="color:#808080;"><span style="font-family:Lucida,Courier New;">012 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">connection_string</span> <span style="color:#0000c0;">=</span> <span style="color:#004080;">"system/systempwd@PYTEST"</span>
</span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">013 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">connection</span> <span style="color:#0000c0;">=</span> <span style="color:#000000;">cx_Oracle</span><span style="color:#0000c0;">.</span><span style="color:#000000;">Connection</span><span style="color:#0000c0;">(</span><span style="color:#000000;">connection_string</span><span style="color:#0000c0;">)</span>
</span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">014 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">cursor</span> <span style="color:#0000c0;">=</span> <span style="color:#000000;">cx_Oracle</span><span style="color:#0000c0;">.</span><span style="color:#000000;">Cursor</span><span style="color:#0000c0;">(</span><span style="color:#000000;">connection</span><span style="color:#0000c0;">)</span>
</span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">015 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">cursor</span><span style="color:#0000c0;">.</span><span style="color:#000000;">execute</span><span style="color:#0000c0;">(</span><span style="color:#000000;">query</span><span style="color:#0000c0;">)</span>
</span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">016 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">column_names</span> <span style="color:#0000c0;">=</span> <span style="color:#000000;">cursor</span><span style="color:#0000c0;">.</span><span style="color:#000000;">description</span>
</span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">017 </span></span>
<span style="color:#808080;"><span style="font-family:Lucida,Courier New;">018 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">x_data</span> <span style="color:#0000c0;">=</span> <span style="color:#0000c0;">[</span><span style="color:#0000c0;">]</span>
</span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">019 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">y_data</span> <span style="color:#0000c0;">=</span> <span style="color:#0000c0;">[</span><span style="color:#0000c0;">]</span>
</span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">020 </span></span>
<span style="color:#808080;"><span style="font-family:Lucida,Courier New;">021 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;">for</span> <span style="color:#000000;">row</span> <span style="color:#c00000;">in</span> <span style="color:#000000;">cursor</span><span style="color:#0000c0;">.</span><span style="color:#000000;">fetchall</span><span style="color:#0000c0;">(</span><span style="color:#0000c0;">)</span><span style="color:#0000c0;">:</span>
</span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">022 </span></span><span style="font-family:Lucida,Courier New;">  <span style="color:#000000;">x_data</span><span style="color:#0000c0;">.</span><span style="color:#000000;">append</span><span style="color:#0000c0;">(</span><span style="color:#000000;">row</span><span style="color:#0000c0;">[</span><span style="color:#0080c0;">0</span><span style="color:#0000c0;">]</span><span style="color:#0000c0;">)</span>
</span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">023 </span></span><span style="font-family:Lucida,Courier New;">  <span style="color:#000000;">y_data</span><span style="color:#0000c0;">.</span><span style="color:#000000;">append</span><span style="color:#0000c0;">(</span><span style="color:#000000;">row</span><span style="color:#0000c0;">[</span><span style="color:#0080c0;">1</span><span style="color:#0000c0;">]</span><span style="color:#0000c0;">)</span>
</span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">024 </span></span>
<span style="color:#808080;"><span style="font-family:Lucida,Courier New;">025 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">figure</span> <span style="color:#0000c0;">=</span> <span style="color:#000000;">plot</span><span style="color:#0000c0;">.</span><span style="color:#000000;">figure</span><span style="color:#0000c0;">(</span><span style="color:#0000c0;">)</span>
</span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">026 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">axis</span> <span style="color:#0000c0;">=</span> <span style="color:#000000;">figure</span><span style="color:#0000c0;">.</span><span style="color:#000000;">add_subplot</span><span style="color:#0000c0;">(</span><span style="color:#0080c0;">111</span><span style="color:#0000c0;">)</span>
</span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">027 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">axis</span><span style="color:#0000c0;">.</span><span style="color:#000000;">plot</span><span style="color:#0000c0;">(</span><span style="color:#000000;">x_data</span><span style="color:#0000c0;">,</span> <span style="color:#000000;">y_data</span><span style="color:#0000c0;">,</span> <span style="color:#004080;">'-'</span><span style="color:#0000c0;">)</span>
</span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">028 </span></span>
<span style="color:#808080;"><span style="font-family:Lucida,Courier New;">029 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">plot</span><span style="color:#0000c0;">.</span><span style="color:#000000;">xlabel</span><span style="color:#0000c0;">(</span><span style="color:#000000;">column_names</span><span style="color:#0000c0;">[</span><span style="color:#0080c0;">0</span><span style="color:#0000c0;">]</span><span style="color:#0000c0;">[</span><span style="color:#0080c0;">0</span><span style="color:#0000c0;">]</span><span style="color:#0000c0;">)</span>
</span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">030 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">plot</span><span style="color:#0000c0;">.</span><span style="color:#000000;">ylabel</span><span style="color:#0000c0;">(</span><span style="color:#000000;">column_names</span><span style="color:#0000c0;">[</span><span style="color:#0080c0;">1</span><span style="color:#0000c0;">]</span><span style="color:#0000c0;">[</span><span style="color:#0080c0;">0</span><span style="color:#0000c0;">]</span><span style="color:#0000c0;">)</span>
</span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">031 </span></span>
<span style="color:#808080;"><span style="font-family:Lucida,Courier New;">032 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">plot</span><span style="color:#0000c0;">.</span><span style="color:#000000;">show</span><span style="color:#0000c0;">(</span><span style="color:#0000c0;">)</span></span></pre>
</div>
<p>At first glance this may look like a lot of code, but not much has changed from what was covered in the first <a title="index" href="http://oracleandpython.wordpress.com/index/" target="_self">tutorials</a>.</p>
<p><strong>001-002 New Imports</strong></p>
<p>These two lines import what we need from matplotlib.  Notice that the <em>as </em>keyword is used to explicitly set an identifier for an imported module.  You can see this identifier being used in line 025.</p>
<p><strong>006-010 Multiline text</strong></p>
<p>In this part of the program a sql query is written spanning multiple lines.  To create a multiline text string in Python you use three double quotes to start the string and three double qoutes to indicate the end.  This is very useful, especially when working with long strings like pieces of SQL.  This query selects advice for the 8K database cache from the v$db_cache_advice table.</p>
<p><strong>018-019 Arrays<br />
</strong></p>
<p>Recall from earlier tutorials that Python does not require you to explicitly define the type of variable you are declaring.  In these two lines we are creating two arrays.  Each array is indicated by square brackets.  One array will store the values for the x-axis and the other will store the values for the y-axis.</p>
<p><strong>021-023 for-loop<br />
</strong></p>
<p>These lines contain a for-loop that fetches all the values in the first column into the x-axis array and all the values from the second column into the y-axis array.</p>
<p><strong>025-027 matplotlib magic<br />
</strong></p>
<p>This part of the code is responsible for plotting the data that we have collected.  Line 25 defines a figure.  This figure is a container for one or more axes. Line 26 creates an axes.  The axes is the part of the plot that we are all familiar with.   It consists of the x- and y-axis as well as the area where the chart is plotted.  The 111 syntax here indicates 1 row, 1 column and 1 subplot meaning, in short, that there is only 1 axes in this figure.  Line 27 plots the data from our arrays and &#8216;-&#8217; indicates that we are using a solid line plot.</p>
<p><strong>029-030 Reusable Axis Labels<br />
</strong></p>
<p>To make this code reusable the axis labels are retrieved from the query using the well know cursor.description data.  This way we can change the program by only altering the query, and the labels will be changed automatically.  You can test this by replacing 006-010 with the following:</p>
<div style="border:2px double #cc0000;background:#dddddd none repeat scroll 0 0;overflow:auto;width:400px;font-size:8pt;padding:20px;">
<pre><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">006 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">query</span> <span style="color:#0000c0;">=</span> <span style="color:#004080;">"""select pga_target_factor as "Size Factor",
</span></span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">007 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#004080;">           estd_pga_cache_hit_percentage "Cache Hit %"
</span></span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">008 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#004080;">           from v$pga_target_advice
</span></span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">009 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#004080;">           order by pga_target_factor
</span></span><span style="color:#808080;"><span style="font-family:Lucida,Courier New;">010 </span></span><span style="font-family:Lucida,Courier New;"><span style="color:#004080;">           """</span></span></pre>
</div>
<p><strong>032 plot.show()</strong></p>
<p>Finally we display the plot we have generated (click on the image for a larger view):</p>
<p><a href="http://oracleandpython.files.wordpress.com/2008/11/graph3.jpg"></a><a href="http://oracleandpython.files.wordpress.com/2008/11/graph4.jpg"><img class="aligncenter size-medium wp-image-166" title="cx_mpl_example1" src="http://oracleandpython.files.wordpress.com/2008/11/graph4.jpg?w=300&#038;h=216" alt="cx_mpl_example1" width="300" height="216" /></a></p>
<p>This plot only shows the base functionality of matplotlib, but it is a good example of the simplicity of using the library and its power.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/oracleandpython.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/oracleandpython.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/oracleandpython.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/oracleandpython.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/oracleandpython.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/oracleandpython.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/oracleandpython.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/oracleandpython.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/oracleandpython.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/oracleandpython.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/oracleandpython.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/oracleandpython.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/oracleandpython.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/oracleandpython.wordpress.com/134/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oracleandpython.wordpress.com&amp;blog=5058451&amp;post=134&amp;subd=oracleandpython&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://oracleandpython.wordpress.com/2008/11/28/cx_oracle-and-matplotlib/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/839b0f3a5a89f6420a2b33f5160d519f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ezuall</media:title>
		</media:content>

		<media:content url="http://oracleandpython.files.wordpress.com/2008/11/graph4.jpg?w=300" medium="image">
			<media:title type="html">cx_mpl_example1</media:title>
		</media:content>
	</item>
		<item>
		<title>Building on the basics</title>
		<link>http://oracleandpython.wordpress.com/2008/10/29/building-on-the-basics/</link>
		<comments>http://oracleandpython.wordpress.com/2008/10/29/building-on-the-basics/#comments</comments>
		<pubDate>Wed, 29 Oct 2008 09:41:19 +0000</pubDate>
		<dc:creator>ezuall</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://oracleandpython.wordpress.com/?p=102</guid>
		<description><![CDATA[The first tutorial, which showed how to connect to an Oracle database from python using cx_Oracle, did not do anything very useful.  This time round we will be looking at a more practical example.  Let&#8217;s start with the program listing below, and I will go through the new parts.  Any lines not covered here are [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oracleandpython.wordpress.com&amp;blog=5058451&amp;post=102&amp;subd=oracleandpython&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The first tutorial, which showed how to connect to an Oracle database from python using cx_Oracle, did not do anything very useful.  This time round we will be looking at a more practical example.  Let&#8217;s start with the program listing below, and I will go through the new parts.  Any lines not covered here are most likely explained in the <a title="First Things First" href="http://oracleandpython.wordpress.com/2008/10/09/first-things-first/" target="_blank">First things first</a> post.</p>
<div style="border:2px double #cc0000;background:#dddddd none repeat scroll 0 0;overflow:auto;width:400px;font-size:8pt;padding:20px;">
<pre><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">001 </span>import</span> <span style="color:#000000;">cx_Oracle</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">002 </span></span></span>
<span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">003 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">query</span> <span style="color:#0000c0;">=</span> <span style="color:#004080;">"select * from v$database"</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">004 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">connection_string</span> <span style="color:#0000c0;">=</span> <span style="color:#004080;">"system/systempassword@PYTEST"</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">005 </span></span></span>
<span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">006 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">connection</span> <span style="color:#0000c0;">=</span> <span style="color:#000000;">cx_Oracle</span><span style="color:#0000c0;">.</span><span style="color:#000000;">Connection</span><span style="color:#0000c0;">(</span><span style="color:#000000;">connection_string</span><span style="color:#0000c0;">)</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">007 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">cursor</span> <span style="color:#0000c0;">=</span> <span style="color:#000000;">cx_Oracle</span><span style="color:#0000c0;">.</span><span style="color:#000000;">Cursor</span><span style="color:#0000c0;">(</span><span style="color:#000000;">connection</span><span style="color:#0000c0;">)</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">008 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">cursor</span><span style="color:#0000c0;">.</span><span style="color:#000000;">execute</span><span style="color:#0000c0;">(</span><span style="color:#000000;">query</span><span style="color:#0000c0;">)</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">009 </span></span></span>
<span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">010 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">column_names</span> <span style="color:#0000c0;">=</span> <span style="color:#000000;">cursor</span><span style="color:#0000c0;">.</span><span style="color:#000000;">description</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">011 </span></span></span>
<span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">012 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;">for</span> <span style="color:#000000;">row</span> <span style="color:#c00000;">in</span> <span style="color:#000000;">cursor</span><span style="color:#0000c0;">.</span><span style="color:#000000;">fetchall</span><span style="color:#0000c0;">(</span><span style="color:#0000c0;">)</span><span style="color:#0000c0;">:</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">013 </span></span></span><span style="font-family:Lucida,Courier New;">  <span style="color:#000000;">c</span> <span style="color:#0000c0;">=</span> <span style="color:#0080c0;">0</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">014 </span></span></span><span style="font-family:Lucida,Courier New;">  <span style="color:#c00000;">for</span> <span style="color:#000000;">column</span> <span style="color:#c00000;">in</span> <span style="color:#000000;">row</span><span style="color:#0000c0;">:</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">015 </span></span></span><span style="font-family:Lucida,Courier New;">    <span style="color:#c00000;">print</span> <span style="color:#000000;">str</span><span style="color:#0000c0;">(</span><span style="color:#000000;">column_names</span><span style="color:#0000c0;">[</span><span style="color:#000000;">c</span><span style="color:#0000c0;">]</span><span style="color:#0000c0;">[</span><span style="color:#0080c0;">0</span><span style="color:#0000c0;">]</span><span style="color:#0000c0;">)</span> <span style="color:#0000c0;">+</span> <span style="color:#004080;">": "</span> <span style="color:#0000c0;">+</span> <span style="color:#000000;">str</span><span style="color:#0000c0;">(</span><span style="color:#000000;">column</span><span style="color:#0000c0;">)</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">016 </span></span></span><span style="font-family:Lucida,Courier New;">    <span style="color:#000000;">c</span> <span style="color:#0000c0;">+=</span> <span style="color:#0080c0;">1</span>
</span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">017 </span></span></span>
<span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">018 </span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">cursor</span><span style="color:#0000c0;">.</span><span style="color:#000000;">close</span><span style="color:#0000c0;">(</span><span style="color:#0000c0;">)</span></span></pre>
</div>
<p>The next step is to have a look at what is new in this code.</p>
<p><strong>003 query = &#8220;select * from v$database&#8221;</strong></p>
<p>The first line of interest is the new query we are running.  Seeing as this tutorial series wants to introduce DBAs to python, we now have a query that will be at least vaguely useful to a DBA.</p>
<p><strong>004 connection_string = &#8220;system/systempassword@PYTEST&#8221;</strong></p>
<p>This time we need to connect as a user with the privilege to select from the v$database view.</p>
<p><strong>010 column_names = cursor.description</strong></p>
<p>The cx_Oracle <a title="Cursor Object" href="http://python.net/crew/atuining/cx_Oracle/html/cursorobj.html" target="_blank">cursor object</a> has an attribute called <em>description </em>which holds MetaData about the columns we are using.  In this example we will be extracting the column name using this attribute.  The column name is the first item in this sequence.</p>
<p><strong>013   c = 0</strong></p>
<p>To keep track of the column number that is currently being processed, we declare a variable called c which is initiated to 0, the first item in a series.</p>
<p><strong>015     print str(column_names[c][0]) + &#8220;: &#8221; + str(column)</strong></p>
<p>In line 15 we print two things.  The <em>column_names[c][0] </em>statement displays the name of every column and then the <em>str(column) </em>part displays the value contained in that column.  Note that we are casting the column as a string to make sure that it can be concatenated with the column name.  String concatenation is acheived by using the + symbol.</p>
<p><strong>016     c += 1</strong></p>
<p>In lines 16 we increment the value of c to reference the number of the next column.</p>
<p>This simple program displays all the values in the v$database table formatted in a very readable way.  Also notice that a table with one row is visualised as a set of attribute-value pairs.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/oracleandpython.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/oracleandpython.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/oracleandpython.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/oracleandpython.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/oracleandpython.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/oracleandpython.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/oracleandpython.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/oracleandpython.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/oracleandpython.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/oracleandpython.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/oracleandpython.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/oracleandpython.wordpress.com/102/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/oracleandpython.wordpress.com/102/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/oracleandpython.wordpress.com/102/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oracleandpython.wordpress.com&amp;blog=5058451&amp;post=102&amp;subd=oracleandpython&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://oracleandpython.wordpress.com/2008/10/29/building-on-the-basics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/839b0f3a5a89f6420a2b33f5160d519f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ezuall</media:title>
		</media:content>
	</item>
		<item>
		<title>Python and me, where we met</title>
		<link>http://oracleandpython.wordpress.com/2008/10/15/python-and-me-where-we-met/</link>
		<comments>http://oracleandpython.wordpress.com/2008/10/15/python-and-me-where-we-met/#comments</comments>
		<pubDate>Wed, 15 Oct 2008 17:33:14 +0000</pubDate>
		<dc:creator>ezuall</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://oracleandpython.wordpress.com/?p=81</guid>
		<description><![CDATA[While working on material for this blog I came across one of my old projects. It was called Beast and was my first major adventure with the Python programming language. The Blender 3D modelling and animation suite features built-in Python scripting and I was fortunate enough to work with the enthusiastic and talented Landis R. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oracleandpython.wordpress.com&amp;blog=5058451&amp;post=81&amp;subd=oracleandpython&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>While working on material for this blog I came across one of my old projects.  It was called Beast and was my first major adventure with the Python programming language.<br />
The <a title="Blender" href="http://www.blender.org/" target="_blank">Blender 3D modelling and animation suite</a> features built-in Python scripting and I was fortunate enough to work with the enthusiastic and talented Landis R. Fields.  Together we were able to create something that I was very proud of.<br />
Beast implements a system for adding hair/fur to 3D characters and you can see an example of what it does below.</p>
<p style="text-align:left;"><a href="http://oracleandpython.files.wordpress.com/2008/10/hairandfur_notes_006.jpg"><img class="size-medium wp-image-82 alignright" title="hairandfur_notes_006" src="http://oracleandpython.files.wordpress.com/2008/10/hairandfur_notes_006.jpg?w=300&#038;h=286" alt="" width="300" height="286" /></a></p>
<p>Before it could go into Beta I finished University and the project was sadly left neglected.  Since then new developments in hardware and software tools have revolutionised hair/fur/fibre modelling and animation.</p>
<p>It is a great memory in so many ways, and that is where I got hooked on the beauty and simplicity of Python.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/oracleandpython.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/oracleandpython.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/oracleandpython.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/oracleandpython.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/oracleandpython.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/oracleandpython.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/oracleandpython.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/oracleandpython.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/oracleandpython.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/oracleandpython.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/oracleandpython.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/oracleandpython.wordpress.com/81/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/oracleandpython.wordpress.com/81/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/oracleandpython.wordpress.com/81/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oracleandpython.wordpress.com&amp;blog=5058451&amp;post=81&amp;subd=oracleandpython&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://oracleandpython.wordpress.com/2008/10/15/python-and-me-where-we-met/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/839b0f3a5a89f6420a2b33f5160d519f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ezuall</media:title>
		</media:content>

		<media:content url="http://oracleandpython.files.wordpress.com/2008/10/hairandfur_notes_006.jpg?w=300" medium="image">
			<media:title type="html">hairandfur_notes_006</media:title>
		</media:content>
	</item>
		<item>
		<title>First things first</title>
		<link>http://oracleandpython.wordpress.com/2008/10/09/first-things-first/</link>
		<comments>http://oracleandpython.wordpress.com/2008/10/09/first-things-first/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 20:57:18 +0000</pubDate>
		<dc:creator>ezuall</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://oracleandpython.wordpress.com/?p=22</guid>
		<description><![CDATA[Before we can get to the more exciting parts we need to cover the basics.  I am including this for completeness, but for a really detailed look into getting started with Python there are various sources.  The goal here is to get going with Python for Oracle DBAs as quickly as possible.  Everyone has a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oracleandpython.wordpress.com&amp;blog=5058451&amp;post=22&amp;subd=oracleandpython&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Before we can get to the more exciting parts we need to cover the basics.  I am including this for completeness, but for a really detailed look into <a title="Python Tutorial" href="http://docs.python.org/tutorial/" target="_blank">getting started with Python</a> there are various sources.  The goal here is to get going with Python for Oracle DBAs as quickly as possible.  Everyone has a different approach to learning a new technology.  What works best for me is learning by example, so we will use this approach here.</p>
<p>In addition to having an Oracle Database, the requirements for this tutorial are listed below:</p>
<ul>
<li><a title="Python" href="http://www.python.org/" target="_blank">Python </a>- At the time of writing this I used 2.5</li>
<li><a title="cx_Oracle" href="http://python.net/crew/atuining/cx_Oracle/" target="_blank">cx_Oracle</a> &#8211; The module for connecting to Oracle Databases</li>
</ul>
<p>The code below shows a connection being made to an Oracle Database and some data being returned and displayed.  This is a trimmed-down version without error checking.</p>
<p>Note:  Like many bloggers before me, I ran into the problem of posting code on a blog here.  This is my first attempt, but I&#8217;m not entirely happy with the format and, therefore, it will be updated at a later stage.</p>
<div style="border:2px double #cc0000;background:#dddddd none repeat scroll 0 0;overflow:auto;width:400px;font-size:8pt;padding:20px;">
<pre><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">001  </span>import</span> <span style="color:#000000;">cx_Oracle</span>
</span><span style="color:#999999;"><span><span style="font-family:Lucida,Courier New;"><span><span style="color:#808080;">002 </span> </span></span></span></span>
<span style="color:#808080;"><span><span style="font-family:Lucida,Courier New;"><span>003  </span></span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">query</span> <span style="color:#0000c0;">=</span> <span style="color:#004080;">"select 'Hello World!!' from dual"</span>
</span><span style="color:#808080;"><span><span style="font-family:Lucida,Courier New;"><span>004  </span></span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">connection_string</span> <span style="color:#0000c0;">=</span> <span style="color:#004080;">"SCOTT/TIGER@PYTEST"</span>
</span><span style="color:#808080;"><span><span style="font-family:Lucida,Courier New;"><span>005  </span></span></span></span>
<span><span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">006 </span> </span></span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">connection</span> <span style="color:#0000c0;">=</span> <span style="color:#000000;">cx_Oracle</span><span style="color:#0000c0;">.</span><span style="color:#000000;">Connection</span><span style="color:#0000c0;">(</span><span style="color:#000000;">connection_string</span><span style="color:#0000c0;">)</span>
</span><span><span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;"><span style="color:#808080;">007 </span> </span></span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">cursor</span> <span style="color:#0000c0;">=</span> <span style="color:#000000;">cx_Oracle</span><span style="color:#0000c0;">.</span><span style="color:#000000;">Cursor</span><span style="color:#0000c0;">(</span><span style="color:#000000;">connection</span><span style="color:#0000c0;">)</span>
</span><span style="color:#808080;"><span><span style="font-family:Lucida,Courier New;"><span>008  </span></span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">cursor</span><span style="color:#0000c0;">.</span><span style="color:#000000;">execute</span><span style="color:#0000c0;">(</span><span style="color:#000000;">query</span><span style="color:#0000c0;">)</span>
<span style="color:#808080;">009
</span></span><span style="color:#808080;"><span><span style="font-family:Lucida,Courier New;"><span>010  </span></span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#c00000;">for</span> <span style="color:#000000;">row</span> <span style="color:#c00000;">in</span> <span style="color:#000000;">cursor</span><span style="color:#0000c0;">.</span><span style="color:#000000;">fetchall</span><span style="color:#0000c0;">(</span><span style="color:#0000c0;">)</span><span style="color:#0000c0;">:</span>
</span><span style="color:#808080;"><span><span style="font-family:Lucida,Courier New;"><span>011  </span></span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#808080;">  </span><span style="color:#c00000;">for</span> <span style="color:#000000;">column</span> <span style="color:#c00000;">in</span> <span style="color:#000000;">row</span><span style="color:#0000c0;">:</span>
</span><span style="color:#808080;"><span><span style="font-family:Lucida,Courier New;"><span>012  </span></span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#808080;">    </span><span style="color:#c00000;">print</span> <span style="color:#000000;">column</span>
<span style="color:#808080;">013
</span></span><span style="color:#808080;"><span><span style="font-family:Lucida,Courier New;"><span>014  </span></span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">cursor</span><span style="color:#0000c0;">.</span><span style="color:#000000;">close</span><span style="color:#0000c0;">(</span><span style="color:#0000c0;">)
</span></span><span style="color:#808080;"><span><span style="font-family:Lucida,Courier New;"><span>015  </span></span></span></span><span style="font-family:Lucida,Courier New;"><span style="color:#000000;">connection</span><span style="color:#0000c0;">.</span><span style="color:#000000;">close</span><span style="color:#0000c0;">(</span><span style="color:#0000c0;">)</span></span></pre>
</div>
<p>Now we can start analysing what is happening line by line.</p>
<p><strong>001 &#8211; import cx_Oracle</strong></p>
<p>Python modules are used to extend the functionality of your application.  For example, if you want to use mathematical functions like sin, pi and sqrt you need to import the math module (import math).</p>
<p>In this case we are importing the cx_Oracle module, which was designed for connecting to Oracle databases and conforms to the <a title="Python Database API Spec" href="http://www.python.org/dev/peps/pep-0249/" target="_blank">Python Database API Specification.</a> This module makes interacting with the Oracle Database really straight forward, as we will see in the following sections.</p>
<p><strong>003  query = &#8220;select &#8216;Hello World!!&#8217; from dual&#8221;</strong></p>
<p>This sets a variable containing the text which is the query to be executed.  This query does not do much, but the idea is to keep the example simple.  Two things are important to note:</p>
<ul>
<li>Python automatically &#8220;knows&#8221; what kind of variable you are declaring.  You do not have to explicitly specify the type.</li>
<li>The string is wrapped in double quotes and the singe quotes are handled correctly.</li>
</ul>
<p><strong>004  connection_string = &#8220;SCOTT/TIGER@PYTEST&#8221; </strong></p>
<p>In this line another string variable is declared, this time containing the connection string of the database.</p>
<p><strong>006  connection = cx_Oracle.Connection(connection_string) </strong></p>
<p>The connection object is created by using the connection string declared in line 004.  Note that this line uses the cx_Oracle module.  Assuming that the supplied connection string is correct, this is all that is needed to create the connection.</p>
<p><strong>007  cursor = cx_Oracle.Cursor(connection) </strong></p>
<p>A connection alone does not get much done.  The object doing the work in this case is the cursor.  Working with Oracle we should all be familiar with cursors.  This python cursor is similar to an explicit PL/SQL cursor and is used to retrieve rows/columns from a table.</p>
<p><strong>008  cursor.execute(query)</strong></p>
<p>Executing the cursor gets us to the point where we can start retrieving data.  The query variable declared earlier is used to specify the source data for the cursor.  There will be a future post detailing what is happening in the background at each of these stages.</p>
<p><strong>010-012 for loop</strong></p>
<p>Retrieving the data is accomplished by using two for loops.  The first loop returns every row, the second loop is nested in the first loop and works within the row, returning the value of every column.  In line 012 the current column is printed for every row.</p>
<p>This generic approach means that the results of most queries can be handled by this script, and changing the data accessed by the script is as easy as changing the SQL statement stored in the query variable.</p>
<p><strong>014 &#8211; 015  *.close()</strong></p>
<p>Although cursors and connections are closed automatically when the program terminates, it is good practise to close these explicitly when you are done with them.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/oracleandpython.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/oracleandpython.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/oracleandpython.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/oracleandpython.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/oracleandpython.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/oracleandpython.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/oracleandpython.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/oracleandpython.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/oracleandpython.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/oracleandpython.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/oracleandpython.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/oracleandpython.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/oracleandpython.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/oracleandpython.wordpress.com/22/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oracleandpython.wordpress.com&amp;blog=5058451&amp;post=22&amp;subd=oracleandpython&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://oracleandpython.wordpress.com/2008/10/09/first-things-first/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/839b0f3a5a89f6420a2b33f5160d519f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ezuall</media:title>
		</media:content>
	</item>
		<item>
		<title>select &#8216;hello world&#8217; from dual;</title>
		<link>http://oracleandpython.wordpress.com/2008/10/03/hello_world_1/</link>
		<comments>http://oracleandpython.wordpress.com/2008/10/03/hello_world_1/#comments</comments>
		<pubDate>Fri, 03 Oct 2008 07:30:30 +0000</pubDate>
		<dc:creator>ezuall</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[hello world]]></category>

		<guid isPermaLink="false">http://oracleandpython.wordpress.com/?p=6</guid>
		<description><![CDATA[This marks the beginning of a blog about two of my main technical interests. I&#8217;ll be starting off with some Python for Oracle DBAs, a subject that I have been interested in since I first read Catherine Devlin&#8217;s article. I will try to be accurate and scientific in my approach, because it is very easy [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oracleandpython.wordpress.com&amp;blog=5058451&amp;post=6&amp;subd=oracleandpython&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This marks the beginning of a blog about two of my main technical interests.  I&#8217;ll be starting off with some Python for Oracle DBAs, a subject that I have been interested in since I first read <a title="Catherine Devlin's Article" href="http://www.oracle.com/technology/pub/articles/devlin-python-oracle.html" target="_blank">Catherine Devlin&#8217;s </a><span><a title="Catherine Devlin's Article" href="http://www.oracle.com/technology/pub/articles/devlin-python-oracle.html" target="_blank">article</a>. </span></p>
<p>I will try to be accurate and scientific in my approach, because it is very easy to confuse facts with what you want to be true.</p>
<p>In conclusion I would like to ask anyone for input on what they think python and oracle can be used for and also what you would like to see examples and explanations of.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/oracleandpython.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/oracleandpython.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/oracleandpython.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/oracleandpython.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/oracleandpython.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/oracleandpython.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/oracleandpython.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/oracleandpython.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/oracleandpython.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/oracleandpython.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/oracleandpython.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/oracleandpython.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/oracleandpython.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/oracleandpython.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oracleandpython.wordpress.com&amp;blog=5058451&amp;post=6&amp;subd=oracleandpython&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://oracleandpython.wordpress.com/2008/10/03/hello_world_1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/839b0f3a5a89f6420a2b33f5160d519f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ezuall</media:title>
		</media:content>
	</item>
	</channel>
</rss>
