<?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>Deep into the core</title>
	<atom:link href="http://blog.deepcore.gr/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://blog.deepcore.gr</link>
	<description>Deepcore in the blogosphere</description>
	<lastBuildDate>Fri, 21 Aug 2009 15:48:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Crawl, automate or test with HtmlUnit and JRuby</title>
		<link>http://blog.deepcore.gr/?p=70</link>
		<comments>http://blog.deepcore.gr/?p=70#comments</comments>
		<pubDate>Fri, 21 Aug 2009 15:34:07 +0000</pubDate>
		<dc:creator>snupdc</dc:creator>
				<category><![CDATA[HtmlUnit]]></category>
		<category><![CDATA[Jruby]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[crawling]]></category>

		<guid isPermaLink="false">http://deepcore.gr/wordpress/?p=70</guid>
		<description><![CDATA[I bet you get dizzy with all the web testers that exist out there. I've been searching for the most  suitable one for my needs,  in order to write my own autonomous automators and crawlers. The main problem i was facing was that most of the libraries or gui-less browsers i used didn't support javascript, [...]]]></description>
			<content:encoded><![CDATA[<p>I bet you get dizzy with all the web testers that exist out there. I've been searching for the most  suitable one for my needs,  in order to write my own autonomous automators and crawlers. The main problem i was facing was that most of the libraries or gui-less browsers i used didn't support javascript, and that was a pain in the ass because i was getting stuck to a lot of pages. I have the impression that javascript is being used extensively by web developers nowadays and so if you want to write your own code doing some interesting stuff inside the web u will need support for javascript!</p>
<p>I came up with htmlunit library which is gui-less browser for java easy to use and has good support for javascript. Well it's even easier to use this library when you write your programs in jruby.</p>
<p>Below i will explain about the setting up  those together and writing some very usefull bots for your everyday needs.</p>
<ul>
<li>Install jruby</li>
<li>Download htmlunit</li>
<li>Enable JRuby and include jar files</li>
<li>Write some code</li>
</ul>
<h2>Step 1</h2>
<p>First of all we have to install jruby. If you compile jruby yourself remember to include it in your classpath.</p>
<h3>Mac OS X</h3>
<p>You will have to download and install MacPorts (<a href="http://www.macports.org/install.php">http://www.macports.org/install.php</a>) and then issue the following command:</p>
<pre>$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> port <span style="color: #c20cb9; font-weight: bold;">install</span> jruby</pre>
<p></p>
<h3>Linux</h3>
<p>Use the package manager you have installed in your system. You simply write the following for distributions using aptitude:</p>
<pre>$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> apt-get <span style="color: #c20cb9; font-weight: bold;">install</span> jruby</pre>
<h3>Windows</h3>
<p><a href="http://www.devdaily.com/blog/post/ruby/installing-jruby-on-windows-xp-system/">http://www.devdaily.com/blog/post/ruby/installing-jruby-on-windows-xp-system/</a><br />
</p>
<h2>Step 2</h2>
<p>Download htmlunit from <a href="http://sourceforge.net/projects/htmlunit/files/">http://sourceforge.net/projects/htmlunit/files/ </a><br />
Place the downloaded jars into a folder named lib.</p>
<pre class="bash">&nbsp;
<span style="color: #c20cb9; font-weight: bold;">tar</span> -zxvf htmlunit-x_x.<span style="color: #c20cb9; font-weight: bold;">tar</span>.gz
<span style="color: #7a0874; font-weight: bold;">cd</span> htmlunit-x-x/
<span style="color: #c20cb9; font-weight: bold;">mv</span> lib/ path_of_your_choice/
&nbsp;</pre>
<h2>Step 3</h2>
<p>Top in the ruby file you  are working write the following:</p>
<pre class="ruby"><span style="color:#008000; font-style:italic;"># Require Java so we can use the Java libraries</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'java'</span>;
&nbsp;
<span style="color:#008000; font-style:italic;"># Get HTML Unit and all of its required libraries</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'htmlunit-2.1.jar'</span>;</pre>
<h2>Example: Vodafone bill</h2>
<p>A simple example retreiving the bill for my mobile phone from vodafone:</p>
<p><strong>voda.rb</strong></p>
<pre class="ruby"><span style="color:#008000; font-style:italic;"># Require Java so we can use the Java libraries</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'java'</span>;
&nbsp;
<span style="color:#008000; font-style:italic;"># Get HTML Unit and all of its required libraries</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'htmlunit-2.1.jar'</span>;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'commons-httpclient-3.1.jar'</span>;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'commons-io-1.4.jar'</span>;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'commons-logging-1.1.1.jar'</span>;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'commons-lang-2.4.jar'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'commons-codec-1.3.jar'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'xercesImpl-2.8.1.jar'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'xml-apis-1.0.b2.jar'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'jaxen-1.1.1.jar'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'commons-collections-3.2.jar'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'js-1.7R1.jar'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'nekohtml-1.9.7.jar'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'sac-1.3.jar'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'cssparser-0.9.5.jar'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'xalan-2.7.0.jar'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'xercesImpl-2.8.1.jar'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Include the Web Client class</span>
include_class <span style="color:#996600;">'com.gargoylesoftware.htmlunit.WebClient'</span>;
include_class <span style="color:#996600;">'com.gargoylesoftware.htmlunit.BrowserVersion'</span>;
&nbsp;
<span style="color:#008000; font-style:italic;"># Function to connect to vodafone website</span>
<span style="color:#9966CC; font-weight:bold;">def</span> connect_to_vodafone
version = BrowserVersion.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#996600;">&quot;Netscape&quot;</span>, <span style="color:#996600;">&quot;5.0 (Macintosh; en-US)&quot;</span>, <span style="color:#996600;">&quot;Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14&quot;</span>, <span style="color:#996600;">&quot;1.2&quot;</span> , <span style="color:#006666;">5.0</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;version:ok&quot;</span>
wc = WebClient.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>version<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;wc:ok&quot;</span>
page = wc.<span style="color:#9900CC;">getPage</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;http://www.vodafone.gr/portal/client/cms/viewCmsPage.action?pageId=1032&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;load_page:ok&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>Logging in to vodafone.gr ...<span style="color:#000099;">\n</span>&quot;</span>
<span style="color:#008000; font-style:italic;">#get login box</span>
forms = page.<span style="color:#9900CC;">getForms</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
login_form = <span style="color:#0000FF; font-weight:bold;">nil</span>
forms.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |form|
<span style="color:#9966CC; font-weight:bold;">if</span> form.<span style="color:#9900CC;">getActionAttribute</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>? <span style="color:#996600;">&quot;/portal/client/idm/login!login.action&quot;</span>
login_form = form
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
username = login_form.<span style="color:#9900CC;">getInputByName</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;username&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
password = login_form.<span style="color:#9900CC;">getInputByName</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;password&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
button = login_form.<span style="color:#9900CC;">getInputByName</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Submit&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#set values to login box</span>
username.<span style="color:#9900CC;">setValueAttribute</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;your_pass&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
password.<span style="color:#9900CC;">setValueAttribute</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;your_username&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
mypage = button.<span style="color:#9900CC;">click</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
mypage = wc.<span style="color:#9900CC;">getPage</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;https://www.vodafone.gr/portal/client/idm/loadUserProfile.action&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;
account_form = mypage.<span style="color:#9900CC;">getFormByName</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;myAccountSelectBill&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
select_drop_down = mypage.<span style="color:#9900CC;">getByXPath</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'//select[@id=&quot;billingAccountField&quot;]'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#008000; font-style:italic;">#results for 1st account</span>
get_results<span style="color:#006600; font-weight:bold;">&#40;</span>select_drop_down.<span style="color:#9900CC;">asText</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>,mypage<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> get_results<span style="color:#006600; font-weight:bold;">&#40;</span>am,page<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;">#Collect the data you are interested in</span>
total_amount = page.<span style="color:#9900CC;">getByXPath</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'//input[@id=&quot;payBill_totalOwnedAmount&quot;]'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">getValueAttribute</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
recent_amount = <span style="color:#996600;">&quot;&quot;</span>
duration = <span style="color:#996600;">&quot;&quot;</span>
page.<span style="color:#9900CC;">getByXPath</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'//td[@class=&quot;main_text pad5&quot;]'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> |td|
<span style="color:#9966CC; font-weight:bold;">if</span> td.<span style="color:#9900CC;">asXml</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#996600;">&quot;€&quot;</span>
recent_amount = td.<span style="color:#9900CC;">asText</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">if</span> td.<span style="color:#9900CC;">asXml</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#996600;">&quot;-&quot;</span>
duration = td.<span style="color:#9900CC;">asText</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#Print collection</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>Vodafone bill&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;-------------------------------------------&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;A/M: &quot;</span>+am+<span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Total amount: &quot;</span> + total_amount + <span style="color:#996600;">&quot; €<span style="color:#000099;">\n</span>&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Recent bill amount: &quot;</span> + recent_amount.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">' '</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">','</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'.'</span><span style="color:#006600; font-weight:bold;">&#41;</span> + <span style="color:#996600;">&quot; €<span style="color:#000099;">\n</span>&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Duration: &quot;</span> + duration + <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span><span style="color:#000099;">\n</span>&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
connect_to_vodafone</pre>
<p><strong>Execution</strong><br />
<code>
<pre class="bash">jruby -Ipath_to_lib_folder voda.rb <span style="color: #000000;">2</span>&gt;/dev/null</pre>
<p> </code></p>
<p>More examples to come <img src='http://blog.deepcore.gr/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.deepcore.gr/?feed=rss2&amp;p=70</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Creating a CMS using CouchDB, Django and 30 lines of python</title>
		<link>http://blog.deepcore.gr/?p=39</link>
		<comments>http://blog.deepcore.gr/?p=39#comments</comments>
		<pubDate>Fri, 10 Jul 2009 19:55:37 +0000</pubDate>
		<dc:creator>kostas</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[cms]]></category>
		<category><![CDATA[couchdb]]></category>

		<guid isPermaLink="false">http://deepcore.gr/wordpress/?p=39</guid>
		<description><![CDATA[I bet you have heard the news on the street about this old ericsson programming language coming back to life bringing functional programming on the web. Yes, I talk about erlang! The language that is used by companies like Facebook, Amazon and of course Ericsson for their network products. It offers features like hot code [...]]]></description>
			<content:encoded><![CDATA[<p>I bet you have heard the news on the street about this old ericsson programming language coming back to life bringing functional programming on the web. Yes, I talk about erlang! The language that is used by companies like Facebook, Amazon and of course Ericsson for their network products. It offers features like hot code swapping, lightweight inter-process communication and more. Generally it is a really great language that fits really well into the "Cloud computing" industry.</p>
<p>As I was exploring the language a while ago, I came into a project called Apache CouchDB, an object-oriented database, or to be more precise a "document store". At first I thought, oh just another object store, but then, after a bit of research, I got in love with it. It is wonderful because It gives you the ability to store, retrieve and query structured data without having to define a schema, one can also attach files onto each document! And the best of all you can do all of this through a neat web interface! Of course it is programmed in Erlang and this is the reason why it offers great speed, stability and distributed features like replication. As I mentioned earlier you can even query data, using uhm... yes... JavaScript! Smart.</p>
<p>After I used it for a couple of hours, I thought that it would really be a great Django template store! One could serve all templates in CouchDB and also define template instances using documents that include all the variables a template renders. Isn't this some kind of a CMS? I started a simple implementation and after no more than 30 lines of python there it was! Really simple but also really functional!</p>
<p><strong>Here are the steps!</strong></p>
<ol>
<li>Install CouchDB</li>
<li>Create a new Django project</li>
<li>Create a new app inside tha django project, I called mine totemplate.</li>
<li>Create the appropriate views and setup the urls.py</li>
<li>Design the database on CouchDB</li>
<li>enjoy!</li>
</ol>
<h2>Step 1</h2>
<p>Installing CouchDB should be really easy for all platforms.</p>
<h3>Mac OS X</h3>
<p>You will have to download and install MacPorts (<a href="http://www.macports.org/install.php">http://www.macports.org/install.php</a>) and then issue the following command:</p>
<pre class="bash">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> port <span style="color: #c20cb9; font-weight: bold;">install</span> couchdb</pre>
<h3>Linux</h3>
<p>Here is a link to a tutorial for linux: <a href="http://barkingiguana.com/2008/06/28/installing-couchdb-080-on-ubuntu-804">http://barkingiguana.com/2008/06/28/installing-couchdb-080-on-ubuntu-804</a>.</p>
<h3>Windows</h3>
<p>If you are on windows you can take a look here: <a href="http://wiki.apache.org/couchdb/Installing_on_Windows">http://wiki.apache.org/couchdb/Installing_on_Windows</a> I haven't tested it myself but it should work fine.</p>
<p>After you have installed couchdb, you will also have to install the python library for it. This is called couchdb-python and is available here:<br />
<a href="http://code.google.com/p/couchdb-python/">http://code.google.com/p/couchdb-python/</a>.<br />
For you that have easy_install installed on your machines, issuing:</p>
<pre class="bash">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> easy_install couchdb-pytho</pre>
<p>should do the job quickly and easilly.</p>
<p><strong>Here is the views.py:</strong></p>
<pre class="python"><span style="color: #808080; font-style: italic;"># Create your views here.</span>
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">http</span> <span style="color: #ff7700;font-weight:bold;">import</span> HttpResponse
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">template</span> <span style="color: #ff7700;font-weight:bold;">import</span> Template, Context
<span style="color: #ff7700;font-weight:bold;">from</span> couchdb <span style="color: #ff7700;font-weight:bold;">import</span> *
<span style="color: #ff7700;font-weight:bold;">from</span> totemplate.<span style="color: black;">settings</span> <span style="color: #ff7700;font-weight:bold;">import</span> COUCH_SERVER
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> show<span style="color: black;">&#40;</span>request, resource_id, page_id<span style="color: black;">&#41;</span>:
    couch_store = Server<span style="color: black;">&#40;</span>COUCH_SERVER<span style="color: black;">&#41;</span>
    category_name = couch_store<span style="color: black;">&#91;</span><span style="color: #483d8b;">'indexers'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>resource_id<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'category'</span><span style="color: black;">&#93;</span>
    category = couch_store<span style="color: black;">&#91;</span>category_name<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>page_id<span style="color: black;">&#93;</span>
    template = couch_store<span style="color: black;">&#91;</span><span style="color: #483d8b;">'templates'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>category<span style="color: black;">&#91;</span><span style="color: #483d8b;">'template_id'</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>
    html = Template<span style="color: black;">&#40;</span>template<span style="color: black;">&#91;</span><span style="color: #483d8b;">'body'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>.<span style="color: black;">render</span><span style="color: black;">&#40;</span>Context<span style="color: black;">&#40;</span>category<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> HttpResponse<span style="color: black;">&#40;</span>html<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> index<span style="color: black;">&#40;</span>request, resource_id<span style="color: black;">&#41;</span>:
    couch_store = Server<span style="color: black;">&#40;</span>COUCH_SERVER<span style="color: black;">&#41;</span>
    indexer = couch_store<span style="color: black;">&#91;</span><span style="color: #483d8b;">'indexers'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>resource_id<span style="color: black;">&#93;</span>
    t = Template<span style="color: black;">&#40;</span>indexer<span style="color: black;">&#91;</span><span style="color: #483d8b;">'template'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    html = t.<span style="color: black;">render</span><span style="color: black;">&#40;</span>Context<span style="color: black;">&#40;</span>couch_store<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> HttpResponse<span style="color: black;">&#40;</span>html<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> resources<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:
    couch_store = Server<span style="color: black;">&#40;</span>COUCH_SERVER<span style="color: black;">&#41;</span>
    resources = <span style="color: black;">&#123;</span><span style="color: #483d8b;">&quot;indexers&quot;</span>:<span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#125;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> indexer <span style="color: #ff7700;font-weight:bold;">in</span> couch_store<span style="color: black;">&#91;</span><span style="color: #483d8b;">'indexers'</span><span style="color: black;">&#93;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> couch_store<span style="color: black;">&#91;</span><span style="color: #483d8b;">'indexers'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>indexer<span style="color: black;">&#93;</span>.<span style="color: black;">has_key</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'template'</span><span style="color: black;">&#41;</span>:
            resources<span style="color: black;">&#91;</span><span style="color: #483d8b;">'indexers'</span><span style="color: black;">&#93;</span> += <span style="color: black;">&#91;</span>indexer<span style="color: black;">&#93;</span>
    t = Template<span style="color: black;">&#40;</span> couch_store<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;settings&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;index&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;body&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    html = t.<span style="color: black;">render</span><span style="color: black;">&#40;</span>Context<span style="color: black;">&#40;</span>resources<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> HttpResponse<span style="color: black;">&#40;</span>html<span style="color: black;">&#41;</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.deepcore.gr/?feed=rss2&amp;p=39</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Caching the result of a python function using memcached and decorators</title>
		<link>http://blog.deepcore.gr/?p=26</link>
		<comments>http://blog.deepcore.gr/?p=26#comments</comments>
		<pubDate>Fri, 12 Jun 2009 12:19:28 +0000</pubDate>
		<dc:creator>kostas</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[memcached]]></category>

		<guid isPermaLink="false">http://deepcore.gr/wordpress/?p=26</guid>
		<description><![CDATA[Caching the result of a function can significantly increase the performance of a centralized system. In web applications this is a common practice since the same function is called over and over using the same parameters for each request. Here we present an approach for the python programming language using decorators. This way we can decorate a python function and automatically cache the result for a specified amount of time.]]></description>
			<content:encoded><![CDATA[<p><strong>sarcachem.py</strong></p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> memcache, <span style="color: #dc143c;">time</span>
&nbsp;
HOST = <span style="color: #483d8b;">&quot;127.0.0.1&quot;</span>
PORT = <span style="color: #483d8b;">&quot;11211&quot;</span>
MC_CLIENT = memcache.<span style="color: black;">Client</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'%s:%s'</span>%<span style="color: black;">&#40;</span>HOST,PORT<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>, debug=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> sarcachem:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">class</span> helper:
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, outer, fun<span style="color: black;">&#41;</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">outer</span> = outer
            <span style="color: #008000;">self</span>.<span style="color: black;">fun</span> = fun
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__call__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, *args, **kwargs<span style="color: black;">&#41;</span>:
            <span style="color: #808080; font-style: italic;"># If cached value does not exist</span>
            <span style="color: #808080; font-style: italic;"># 1. Check to see if it is locked</span>
            <span style="color: #808080; font-style: italic;">#    If it is, wait until it unlocks</span>
            <span style="color: #808080; font-style: italic;">#    If it is not, lock and calculate value,</span>
            <span style="color: #808080; font-style: italic;">#    unlock when finished</span>
            <span style="color: #808080; font-style: italic;"># Return cached value</span>
            key = <span style="color: #483d8b;">&quot;%s.%s-&amp;gt;(%s,%s)&quot;</span>%<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">outer</span>.<span style="color: black;">salt</span>,
                                   <span style="color: #008000;">self</span>.<span style="color: black;">fun</span>.<span style="color: black;">func_name</span>,
                                   <span style="color: #008000;">repr</span><span style="color: black;">&#40;</span>args<span style="color: black;">&#41;</span>,
                                   <span style="color: #008000;">repr</span><span style="color: black;">&#40;</span>kwargs<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            key_lock = <span style="color: #483d8b;">&quot;00_locked_%s&quot;</span>%<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">if</span> MC_CLIENT.<span style="color: black;">get</span><span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> MC_CLIENT.<span style="color: black;">get</span><span style="color: black;">&#40;</span>key_lock<span style="color: black;">&#41;</span>:
                    MC_CLIENT.<span style="color: #008000;">set</span><span style="color: black;">&#40;</span>key_lock,<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
                    result = <span style="color: #008000;">self</span>.<span style="color: black;">fun</span><span style="color: black;">&#40;</span>*args, **kwargs<span style="color: black;">&#41;</span>
                    MC_CLIENT.<span style="color: #008000;">set</span><span style="color: black;">&#40;</span>key,result,<span style="color: #dc143c;">time</span>=<span style="color: #008000;">self</span>.<span style="color: black;">outer</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#41;</span>
                    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Storing: &quot;</span>, key, <span style="color: #483d8b;">&quot;: &quot;</span>, MC_CLIENT.<span style="color: black;">get</span><span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>
                    MC_CLIENT.<span style="color: black;">delete</span><span style="color: black;">&#40;</span>key_lock<span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
                        <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
                        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> MC_CLIENT.<span style="color: black;">get</span><span style="color: black;">&#40;</span>key_lock<span style="color: black;">&#41;</span>:
                            <span style="color: #ff7700;font-weight:bold;">break</span>
                        <span style="color: #ff7700;font-weight:bold;">else</span>:
                            <span style="color: #ff7700;font-weight:bold;">continue</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">return</span> MC_CLIENT.<span style="color: black;">get</span><span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,<span style="color: #dc143c;">time</span>=<span style="color: #ff4500;">3</span>,salt=<span style="color: #483d8b;">&quot;base&quot;</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot; In this function we set all our decorator's parameters &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: #dc143c;">time</span> = <span style="color: #dc143c;">time</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">salt</span> = salt
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__call__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, fun<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> sarcachem.<span style="color: black;">helper</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, fun<span style="color: black;">&#41;</span></pre>
<p>And here is the way you can use it:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> sarcachem <span style="color: #ff7700;font-weight:bold;">import</span> sarcachem
&nbsp;
@sarcachem<span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>,__file__<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> fib<span style="color: black;">&#40;</span>number=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #808080; font-style: italic;"># Suck my life into the CPUHOLE</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">100000</span><span style="color: black;">&#41;</span>:
        i<span style="color: #ff4500;">+100</span>;
    <span style="color: #808080; font-style: italic;"># END OF LIFE SUCKER</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> number==<span style="color: #ff4500;">0</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>
    <span style="color: #ff7700;font-weight:bold;">elif</span> number==<span style="color: #ff4500;">1</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>fib<span style="color: black;">&#40;</span>number<span style="color: #ff4500;">-1</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>fib<span style="color: black;">&#40;</span>number<span style="color: #ff4500;">-2</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__==<span style="color: #483d8b;">&quot;__main__&quot;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> fib<span style="color: black;">&#40;</span><span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>, fib<span style="color: black;">&#40;</span><span style="color: #ff4500;">29</span><span style="color: black;">&#41;</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.deepcore.gr/?feed=rss2&amp;p=26</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Up and running</title>
		<link>http://blog.deepcore.gr/?p=8</link>
		<comments>http://blog.deepcore.gr/?p=8#comments</comments>
		<pubDate>Fri, 12 Jun 2009 11:24:59 +0000</pubDate>
		<dc:creator>snupdc</dc:creator>
				<category><![CDATA[Announcements]]></category>
		<category><![CDATA[announcement]]></category>

		<guid isPermaLink="false">http://deepcore.gr/wordpress/?p=8</guid>
		<description><![CDATA[Welcome to our brand new blog. We decided to start deepcore.gr with our blog for the moment. New stuff coming in the near future.
Enjoy your stay,
The team
]]></description>
			<content:encoded><![CDATA[<p>Welcome to our brand new blog. We decided to start deepcore.gr with our blog for the moment. New stuff coming in the near future.</p>
<p>Enjoy your stay,</p>
<p>The team</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.deepcore.gr/?feed=rss2&amp;p=8</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
