<?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/"
	>

<channel>
	<title>Webmasterwords &#187; Python</title>
	<atom:link href="http://www.webmasterwords.com/category/python/feed" rel="self" type="application/rss+xml" />
	<link>http://www.webmasterwords.com</link>
	<description>Useful snippets for Webmasters</description>
	<lastBuildDate>Wed, 21 Sep 2011 15:55:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Python UCM Client</title>
		<link>http://www.webmasterwords.com/python-ucm-client?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=python-ucm-client</link>
		<comments>http://www.webmasterwords.com/python-ucm-client#comments</comments>
		<pubDate>Fri, 16 Sep 2011 17:03:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.webmasterwords.com/?p=60</guid>
		<description><![CDATA[This package provides a simple client for calling the services on Oracle Content Server 11g via the standard web interface. It can be used in conjunction with any of the services provided by Content Server defined <a href="http://download.oracle.com/docs/cd/E14571_01/doc.1111/e11011/toc.htm">here</a>

<h2>Installation</h2>

easy_install ucmclient

<h2>Examples</h2>

<h3>Uploading a file to Content Server</h3>


from ucmclient import UcmClient, ServiceFailed

#connect to ucm with specified hostname and content server port
ucmClient = UcmClient('ucmserver.example.com','16200')
ucmClient.login('weblogic','password')

#build a dictionary of service parameters.
ucmServiceData ={
 'dDocTitle':'New Article 1',
 'dDocName':'News_Article_01',
 'dDocType':'News']]></description>
			<content:encoded><![CDATA[<p>This package provides a simple client for calling the services on Oracle Content Server 11g via the standard web interface. It can be used in conjunction with any of the services provided by Content Server  defined <a href="http://download.oracle.com/docs/cd/E14571_01/doc.1111/e11011/toc.htm">here</a></p>
<h2>Installation</h2>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">easy_install ucmclient</pre></div></div>

<h2>Examples</h2>
<h3>Uploading a file to Content Server</h3>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> ucmclient <span style="color: #ff7700;font-weight:bold;">import</span> UcmClient, ServiceFailed
&nbsp;
<span style="color: #808080; font-style: italic;">#connect to ucm with specified hostname and content server port</span>
ucmClient = UcmClient<span style="color: black;">&#40;</span><span style="color: #483d8b;">'ucmserver.example.com'</span>,<span style="color: #483d8b;">'16200'</span><span style="color: black;">&#41;</span>
ucmClient.<span style="color: black;">login</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'weblogic'</span>,<span style="color: #483d8b;">'password'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#build a dictionary of service parameters.</span>
ucmServiceData =<span style="color: black;">&#123;</span>
    <span style="color: #483d8b;">'dDocTitle'</span>:<span style="color: #483d8b;">'New Article 1'</span>,
    <span style="color: #483d8b;">'dDocName'</span>:<span style="color: #483d8b;">'News_Article_01'</span>,
    <span style="color: #483d8b;">'dDocType'</span>:<span style="color: #483d8b;">'News'</span>,
    <span style="color: #483d8b;">'dDocAuthor'</span>:<span style="color: #483d8b;">'weblogic'</span>,
    <span style="color: #483d8b;">'dDocAccount'</span>:<span style="color: #483d8b;">'weblogic'</span>,
    <span style="color: #483d8b;">'dSecurityGroup'</span>:<span style="color: #483d8b;">'Public'</span>,
    <span style="color: #808080; font-style: italic;">#files can be submitted using an open file object</span>
    <span style="color: #483d8b;">'primaryFile'</span>:<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'news.txt'</span>,<span style="color: #483d8b;">'r'</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#125;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">try</span>:
    ucmClient.<span style="color: black;">call_service</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'CHECKIN_UNIVERSAL'</span>,ucmServiceData<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">except</span> ServiceFailed <span style="color: #ff7700;font-weight:bold;">as</span> e:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'checkin failed - %s'</span> <span style="color: #66cc66;">%</span> e</pre></div></div>

<h3>Searching for files on Content Server</h3>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> ucmclient <span style="color: #ff7700;font-weight:bold;">import</span> UcmClient, ServiceFailed
&nbsp;
ucmClient = UcmClient<span style="color: black;">&#40;</span><span style="color: #483d8b;">'ucmserver.example.com'</span>,<span style="color: #483d8b;">'16200'</span><span style="color: black;">&#41;</span>
ucmClient.<span style="color: black;">login</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'weblogic'</span>,<span style="color: #483d8b;">'password'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#Search for files of type News</span>
ucmServiceData = <span style="color: black;">&#123;</span>
        <span style="color: #483d8b;">'QueryText'</span>:<span style="color: #483d8b;">'dDocType&lt;matches&gt;`News`'</span>,
        <span style="color: #483d8b;">'ResultCount'</span>:<span style="color: #ff4500;">10</span>
<span style="color: black;">&#125;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">try</span>:
    search = ucmClient.<span style="color: black;">call_service</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'GET_SEARCH_RESULTS'</span>,ucmServiceData<span style="color: black;">&#41;</span>
    <span style="color: #808080; font-style: italic;">#Access resultset by name and iterate through results</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> doc <span style="color: #ff7700;font-weight:bold;">in</span> search.<span style="color: black;">fetch</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'SearchResults'</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> doc<span style="color: black;">&#91;</span><span style="color: #483d8b;">'dDocName'</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">except</span> ServiceFailed <span style="color: #ff7700;font-weight:bold;">as</span> e:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'search failed - %s'</span> <span style="color: #66cc66;">%</span> e</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.webmasterwords.com/python-ucm-client/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The python &quot;with&quot; statement explained with examples.</title>
		<link>http://www.webmasterwords.com/python-with-statement?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=python-with-statement</link>
		<comments>http://www.webmasterwords.com/python-with-statement#comments</comments>
		<pubDate>Wed, 14 Sep 2011 18:21:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.webmasterwords.com/?p=54</guid>
		<description><![CDATA[Python's with statement was introduced into the language in version 2.5 and is defined in <a href="http://www.python.org/dev/peps/pep-0343/">PEP 343</a>. The python docs provide the following description "The with statement is used to wrap the execution of a block with methods defined by a context manager. This allows common try...except...finally usage patterns to be encapsulated for convenient reuse." but what does this actually mean?

Lets start with an example:

<h2>With File</h2>


with open("test.txt") as testfile]]></description>
			<content:encoded><![CDATA[<p>Python&#8217;s with statement was introduced into the language in version 2.5 and is defined in <a href="http://www.python.org/dev/peps/pep-0343/">PEP 343</a>. The python docs provide the following description &#8220;The with statement is used to wrap the execution of a block with methods defined by a context manager. This allows common try&#8230;except&#8230;finally usage patterns to be encapsulated for convenient reuse.&#8221; but what does this actually mean?</p>
<p>Lets start with an example:</p>
<h2>With File</h2>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">with</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;test.txt&quot;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> testfile:
    testcontents = testfile.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> testcontents</pre></div></div>

<p>This example demonstrates opening and printing the contents of a file &#8220;test.txt&#8217; to the stdout. The first thing most people should (hopefully) notice is that when using the &#8220;with&#8221; statement it is not necessary to close the file after we&#8217;ve used it. Thats because the open function provides a context manager which closes the file after use.</p>
<p>In python a context manager in is an object that defines how a resource should be initially setup for use and cleared up after use. It implements two functions __enter__ which is called when the resource is setup and __exit__ when it is no longer needed. Its is the __exit__ method of the open file context manager that ensures the file is closed after use.</p>
<h2>Another Context Manager</h2>
<p>A very common application functionality is to access a database, add some records and finally commit those updates as a transaction. Using SQLAlchemy it would look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> sqlalchemy <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
db = create_engine<span style="color: black;">&#40;</span><span style="color: #483d8b;">'sqlite:///test.db'</span><span style="color: black;">&#41;</span>
dbcon =  db.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
transaction = dbcon.<span style="color: black;">begin</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
dbcon.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'insert into NameDB (FirstName, LastName,Age) values (&quot;john&quot;,&quot;smith&quot;,34)'</span><span style="color: black;">&#41;</span>
dbcon.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'insert into NameDB (FirstName, LastName,Age) values (&quot;dave&quot;,&quot;smith&quot;,37)'</span><span style="color: black;">&#41;</span>
transaction.<span style="color: black;">commit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>You can see that here the resource in question is the database transaction, we set it up before the inserts and close it after the commit. However because the SQLAlchemy transaction object implement a context manager the above code could be written more succinctly as:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> sqlalchemy <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
db = create_engine<span style="color: black;">&#40;</span><span style="color: #483d8b;">'sqlite:///test.db'</span><span style="color: black;">&#41;</span>
dbcon =  db.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">with</span> dbcon.<span style="color: black;">begin</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    dbcon.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'insert into NameDB (FirstName, LastName,Age) values (&quot;john&quot;,&quot;smith&quot;,34)'</span><span style="color: black;">&#41;</span>
    dbcon.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'insert into NameDB (FirstName, LastName,Age) values (&quot;dave&quot;,&quot;smith&quot;,37)'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>In the next post I will discuss creating your own context managers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webmasterwords.com/python-with-statement/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python Split and Join Examples</title>
		<link>http://www.webmasterwords.com/python-split-and-join-examples?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=python-split-and-join-examples</link>
		<comments>http://www.webmasterwords.com/python-split-and-join-examples#comments</comments>
		<pubDate>Mon, 26 Jan 2009 12:39:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.webmasterwords.com/python-split-and-join-examples</guid>
		<description><![CDATA[Split and join in python are similar to the equivalent functions in most dynamic languages with a few minor differences:

<strong>Python Split</strong>

Split a sentence into separate words:

sentence = "the cat sat on the mat"
print sentence.split()
['the', 'cat', 'sat', 'on', 'the', 'mat']
Split words separated by a single character:

sentence = "the,cat,sat,on,the,mat"
print sentence.split(',')
['the', 'cat', 'sat', 'on', 'the', 'mat']
Split words separated by a string:

sentence = "theSPACEcatSPACEsatSPACEonSPACEtheSPACEmat"
print sentence.split('SPACE')
['the', 'cat', 'sat', 'on', 'the', 'mat']
Split only the first two]]></description>
			<content:encoded><![CDATA[<p>Split and join in python are similar to the equivalent functions in most dynamic languages with a few minor differences:</p>
<p><strong>Python Split</strong></p>
<p>Split a sentence into separate words:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">sentence = <span style="color: #483d8b;">&quot;the cat sat on the mat&quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> sentence.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'the'</span>, <span style="color: #483d8b;">'cat'</span>, <span style="color: #483d8b;">'sat'</span>, <span style="color: #483d8b;">'on'</span>, <span style="color: #483d8b;">'the'</span>, <span style="color: #483d8b;">'mat'</span><span style="color: black;">&#93;</span></pre></div></div>

<p>Split words separated by a single character:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">sentence = <span style="color: #483d8b;">&quot;the,cat,sat,on,the,mat&quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> sentence.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">','</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'the'</span>, <span style="color: #483d8b;">'cat'</span>, <span style="color: #483d8b;">'sat'</span>, <span style="color: #483d8b;">'on'</span>, <span style="color: #483d8b;">'the'</span>, <span style="color: #483d8b;">'mat'</span><span style="color: black;">&#93;</span></pre></div></div>

<p>Split words separated by a string:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">sentence = <span style="color: #483d8b;">&quot;theSPACEcatSPACEsatSPACEonSPACEtheSPACEmat&quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> sentence.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'SPACE'</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'the'</span>, <span style="color: #483d8b;">'cat'</span>, <span style="color: #483d8b;">'sat'</span>, <span style="color: #483d8b;">'on'</span>, <span style="color: #483d8b;">'the'</span>, <span style="color: #483d8b;">'mat'</span><span style="color: black;">&#93;</span></pre></div></div>

<p>Split only the first two words in a comma separated string</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">sentence = <span style="color: #483d8b;">&quot;the,cat,sat,on,the,mat&quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> sentence.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">','</span>,<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'the'</span>, <span style="color: #483d8b;">'cat'</span>, <span style="color: #483d8b;">'sat,on,the,mat'</span><span style="color: black;">&#93;</span></pre></div></div>

<p><strong>Python Join</strong></p>
<p>Join uses a sligthly difference implmentation to what you might be used to. The character that joins the elements is the one upon which the function is called</p>
<p>Joining an array with spaces</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #483d8b;">' '</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'the'</span>, <span style="color: #483d8b;">'cat'</span>, <span style="color: #483d8b;">'sat'</span>, <span style="color: #483d8b;">'on'</span>, <span style="color: #483d8b;">'the'</span>, <span style="color: #483d8b;">'mat'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #483d8b;">'the cat sat on the mat'</span></pre></div></div>

<p>Joining an array with commas</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #483d8b;">','</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'the'</span>, <span style="color: #483d8b;">'cat'</span>, <span style="color: #483d8b;">'sat'</span>, <span style="color: #483d8b;">'on'</span>, <span style="color: #483d8b;">'the'</span>, <span style="color: #483d8b;">'mat'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #483d8b;">'the,cat,sat,on,the,mat'</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.webmasterwords.com/python-split-and-join-examples/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

