<?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>Enterprise LAMP</title>
	<atom:link href="http://enterpriselamp.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://enterpriselamp.org</link>
	<description></description>
	<lastBuildDate>Fri, 30 Jul 2010 05:37:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>V.S. Babu: Quick script to maintain a diary</title>
		<link>http://vsbabu.org/mt/archives/2010/07/30/quick_script_to_maintain_a_diary.html</link>
		<comments>http://vsbabu.org/mt/archives/2010/07/30/quick_script_to_maintain_a_diary.html#comments</comments>
		<pubDate>Fri, 30 Jul 2010 05:37:08 +0000</pubDate>
		<dc:creator>Planet Python</dc:creator>
				<category><![CDATA[Planet]]></category>

		<guid isPermaLink="false">http://vsbabu.org/mt/archives/2010/07/30/quick_script_to_maintain_a_diary.html</guid>
		<description><![CDATA[<p>
I like to keep my daily notes in a folder in the filesystem with filenames <tt>yyyymmdd.otl</tt>, using <a href="http://bike-nomad.com/vim/vimoutliner.html">VIM Outliner</a>. Here is a small DOS script to make a file for a day if it doesn't exist and then open it. Name it as <tt>diary.cmd</tt> and keep in your path.
</p>]]></description>
			<content:encoded><![CDATA[<p>
I like to keep my daily notes in a folder in the filesystem with filenames <tt>yyyymmdd.otl</tt>, using <a href="http://bike-nomad.com/vim/vimoutliner.html">VIM Outliner</a>. Here is a small DOS script to make a file for a day if it doesn't exist and then open it. Name it as <tt>diary.cmd</tt> and keep in your path.
</p>]]></content:encoded>
			<wfw:commentRss>http://vsbabu.org/mt/archives/2010/07/30/quick_script_to_maintain_a_diary.html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Francisco Souza: High level acceptance testing in your PHP applications using Python, Lettuce and Selenium</title>
		<link>http://www.franciscosouza.net/2010/07/high-level-acceptance-testing-in-your.html</link>
		<comments>http://www.franciscosouza.net/2010/07/high-level-acceptance-testing-in-your.html#comments</comments>
		<pubDate>Fri, 30 Jul 2010 01:08:06 +0000</pubDate>
		<dc:creator>Planet Python</dc:creator>
				<category><![CDATA[Planet]]></category>

		<guid isPermaLink="false">http://www.franciscosouza.net/2010/07/high-level-acceptance-testing-in-your.html</guid>
		<description><![CDATA[Usually, in PHP applications, I don't run acceptance tests. Lately, I have been thinking about how can I change this. I am a little experienced with others acceptance tests frameworks, for Python (<a href="http://www.lettuce.it/" rel="nofollow" target="_blank" title="Lettuce">Lettuce</a>) and Ruby (<a href="http://cukes.info/" rel="nofollow" target="_blank" title="Cucumber">Cucumber</a>), but there is nothing like Lettuce and/or Cucumber in PHP world. So, what can I do? I can just use Lettuce or Cucumber to test any application, including PHP or Java application. In this blog post, I will show how to use behaviour-driven development (BDD) behind Lettuce to write acceptance tests for PHP applications.<br /><a name="more"></a><br />Before start to coding, let's build up our environment, with Python, Lettuce and Selenium. If you use Linux, Mac OS, FreeBSD or any other Unix-like operating system, Python is already installed in your system. So, let's install Lettuce and Selenium.<br /><br /><b><span>Installing Lettuce</span></b><br /><br />To install Lettuce from source code, you can clone the repository from github and install it using setuptools:<br /><br /><code>$ git clone git://github.com/gabrielfalcao/lettuce.git<br />$ cd lettuce<br />$ [sudo] python setup.py install</code><br /><br />If you use pip or easy_install, you can also install Lettuce with the following commands:<br /><br /><code>$ [sudo] easy_install lettuce</code><br /><code>$ [sudo] pip install lettuce</code><br /><br /><b><span>Installing Selenium</span></b><br /><br />The code used in this blog post was written for Selenium 2.0, wich is still alfa. So, to install <a href="http://seleniumhq.org/" rel="nofollow" target="_blank" title="Selenium">Selenium</a>, you need to checkout the SVN repository and run the <i>setup.py install</i> command:<br /><br /><code>$ svn checkout http://selenium.googlecode.com/svn/trunk/ selenium<br />$ cd selenium<br />$ [sudo] python setup.py install</code><br /><br />With both Selenium and Lettuce installed, let's code.<br /><br /><span><b>Let's code! :)</b></span> <br /><br />The example will be very simple: a feature called "Login" with two scenarios: "Successful login" and "Unsuccessful login". See the feature "code" above, and understand it:<br /><br /><pre>Feature: Login<br />  In order to have unlimited powers<br />  As a system anonymous user<br />  I want to login in the system<br /><br />  Scenario Outline: Successful and unsuccessful login<br />    Given that there is a user registered in the system with the username "admin" and the password "123"<br />    When I navigate to the login page<br />    And fill the username field with &#60;login&#62;<br />    And fill the password field with &#60;password&#62;<br />    And click the "Log in" button<br />    Then I see the message &#60;message&#62;<br /><br />  Examples:<br />    &#124; login &#124; password &#124; message                                  &#124;<br />    &#124; admin &#124; 123      &#124; Welcome to admin                         &#124;<br />    &#124; admin &#124; 1234     &#124; Error. Verify your username and password &#124;</pre><br />Put this feature file (<i>login.feature</i>) inside a directory called <i>feature</i> in the root of your application (or any other place).<br /><br /><b><span>Prepare the terrain and define the steps</span></b> <br /><br />According the <a href="http://lettuce.it/tutorial/simple.html#tutorial-simple" rel="nofollow" target="_blank" title="Lettuce documentation">Lettuce documentation</a>, the second round is to define the steps described in the feature file. Before we define our steps, let's write a <i>terrain.py</i> Python module, which prepares the terrain to run our acceptance tests. The only things we need to do is setup a Selenium webdriver instance to test the content and the behaviour of our login page. Here is the <i>terrain.py </i>code:<br /><br /><pre>from lettuce import before, after, world<br />from selenium import get_driver, FIREFOX<br /><br />@before.all<br />def setup_browser():<br />    world.browser = get_driver(FIREFOX)<br />    world.root_url = 'http://localhost/php_app_test/'<br /><br />@after.all<br />def teardown_browser(total):<br />    world.browser.quit()<br /></pre><br />The terrain.py module should be in the <i>features</i> directory. Now we can define the steps from the scenario in a Python module called <i>login_steps</i>, and put this module inside a directory called <i>step_definitions</i>, in the <i>features</i> directory. Here is the directory tree:<br /><br /><div class="separator"><img border="0" src="http://1.bp.blogspot.com/_AewNgvR35uk/TDZd_yb5__I/AAAAAAAAApo/6BgsTuNN_XQ/s1600/screenshot3.png" /></div><br /><div></div>And in this Python module, we define the steps:<br /><br /><pre># -*- coding: utf-8 -*-<br />from lettuce import *<br /><br />@step(u'Given that there is a user registered in the system with the username "admin" and the password "123"')<br />def do_nothing(step):<br />    '''There is nothing that Lettuce can do to guarantee this sentence'''<br />    pass<br /><br />@step(u'When I navigate to the login page')<br />def when_i_navigate_to_the_login_page(step):<br />    login_url = world.root_url + 'login.html'<br />    world.browser.get(login_url)<br /><br />@step(u'And I fill the username field with (.*)')<br />def and_i_fill_the_username_field_with_login(step, login):<br />    username_field = world.browser.find_element_by_xpath('//input[@name="username"]')<br />    username_field.send_keys(login)<br /><br />@step(u'And I fill the password field with (.*)')<br />def and_i_fill_the_password_field_with_password(step, password):<br />    password_field = world.browser.find_element_by_xpath('//input[@name="password"]')<br />    password_field.send_keys(password)<br /><br />@step(u'And I click the "(.*)" button')<br />def and_i_click_the_group1_button(step, button_value):<br />    button = world.browser.find_element_by_xpath('//input[@value="%s"]' %(button_value))<br />    button.click()<br /><br />@step(u'Then I see the message (.*)')<br />def then_i_see_the_message_message(step, message):<br />    assert message in world.browser.get_page_source()</pre><br />If you don't know Python, take a look at some free books (like <a href="http://diveintopython.org/" rel="nofollow" target="_blank" title="Dive into Python">Dive into Python</a> and <a href="http://www.greenteapress.com/thinkpython/thinkpython.html" rel="nofollow" target="_blank" title="Think Python">How to think like a computer scientist: Learning with Python</a>) and learn it :)<br /><br /><span><b>Understanding the step definitions</b></span><br /><br />Let's see more about the step definitions above. In the first step ("Given that there is a user registered in the system with the username "admin" and the password "123""), there is nothing to do: we cannot check and guarantee that there is a user registered in the system (unless we have access to the database, for example).<br /><br />On the second step ("When I navigate to the login page"), we use the browser provided by Selenium WebDriver to go to the login page, in the URL http://localhost/php_app_test/login.html. You can see how to use the Selenium WebDriver in <a href="http://seleniumhq.org/docs/09_webdriver.html" rel="nofollow" target="_blank" title="Selenium 2.0 documentation">Selenium WebDriver docs</a>.<br /><br />The third step ("And fill the username field with &#60;login&#62;") is where we get the username text field by it's name and fill it with the login obtained from this step's sentence. We use the <a href="http://www.w3schools.com/XPath/xpath_syntax.asp" rel="nofollow" target="_blank" title="XPath Syntax at W3Schools">xpath syntax</a> to find the field by it's name, and on the fourth step ("And fill the password field with &#60;password&#62;"), we get the password field by it's name and fill it with the password obtained from the fourth step sentence. The last browser interation is in the sixth step ("And click the "Log in" button"), when we get the "Log in" button and click it.<br /><br />The last step ("Then I see the message &#60;message&#62;") asserts that the properly message appears on the screen when the user try to login with the right and wrong data.<br /><br /><span><b>The PHP code</b></span> <br /><br />Now, let's write the PHP code needed to make this specification work. I will not connect to a database and execute a complete login, we will just build a HTML form in the login.html page and make a simple PHP code to show the properly message in each case. The HTML code is here:<br /><br /><pre>&#60;html&#62;<br />    &#60;head&#62;<br />      &#60;meta http-equiv="Content-type" content="text/html; charset=utf-8"/&#62;<br />      &#60;title&#62;Login page&#60;/title&#62;<br />      <br />    &#60;/head&#62;<br />    &#60;body id=""&#62;<br />        &#60;form action="login.php" method="post" accept-charset="utf-8"&#62;<br />            &#60;label for="id_username"&#62;Username: &#60;/label&#62;&#60;input type="text" name="username" value="" id="id_username"/&#62;&#60;br /&#62;<br />            &#60;label for="id_password"&#62;Password: &#60;/label&#62;&#60;input type="password" name="password" value="" id="id_password"/&#62;<br />            &#60;input type="submit" name="submit" value="Log in" id=""/&#62;<br />        &#60;/form&#62;<br />    &#60;/body&#62;<br />&#60;/html&#62;</pre><br />And the <strike>stupid </strike>simple PHP code is here: <br /><br /><pre>&#60;?php<br /><br />    if ($_POST['username'] == 'admin' &#38;&#38; $_POST['password'] == '123') {<br />        echo 'Welcome to admin';<br />    } else {<br />        echo 'Error. Verify your username and password';<br />    }</pre><br />That was a really simple example that shows how to write high level tests to PHP applications, with Selenium WebDriver and Lettuce :) You can checkout the application code at Github: <a href="http://github.com/franciscosouza/php_app_test" rel="nofollow" target="_blank" title="This project at Github">http://github.com/franciscosouza/php_app_test</a>.<div class="blogger-post-footer"><img width="1" height="1" src="https://blogger.googleusercontent.com/tracker/2635578411501939394-9136094375033144413?l=www.franciscosouza.net" alt="" /></div>]]></description>
			<content:encoded><![CDATA[Usually, in PHP applications, I don't run acceptance tests. Lately, I have been thinking about how can I change this. I am a little experienced with others acceptance tests frameworks, for Python (<a href="http://www.lettuce.it/" rel="nofollow"  title="Lettuce">Lettuce</a>) and Ruby (<a href="http://cukes.info/" rel="nofollow"  title="Cucumber">Cucumber</a>), but there is nothing like Lettuce and/or Cucumber in PHP world. So, what can I do? I can just use Lettuce or Cucumber to test any application, including PHP or Java application. In this blog post, I will show how to use behaviour-driven development (BDD) behind Lettuce to write acceptance tests for PHP applications.<br /><a name="more"></a><br />Before start to coding, let's build up our environment, with Python, Lettuce and Selenium. If you use Linux, Mac OS, FreeBSD or any other Unix-like operating system, Python is already installed in your system. So, let's install Lettuce and Selenium.<br /><br /><b><span>Installing Lettuce</span></b><br /><br />To install Lettuce from source code, you can clone the repository from github and install it using setuptools:<br /><br /><code class="terminal">$ git clone git://github.com/gabrielfalcao/lettuce.git<br />$ cd lettuce<br />$ [sudo] python setup.py install</code><br /><br />If you use pip or easy_install, you can also install Lettuce with the following commands:<br /><br /><code class="terminal">$ [sudo] easy_install lettuce</code><br /><code class="terminal">$ [sudo] pip install lettuce</code><br /><br /><b><span>Installing Selenium</span></b><br /><br />The code used in this blog post was written for Selenium 2.0, wich is still alfa. So, to install <a href="http://seleniumhq.org/" rel="nofollow"  title="Selenium">Selenium</a>, you need to checkout the SVN repository and run the <i>setup.py install</i> command:<br /><br /><code class="terminal">$ svn checkout http://selenium.googlecode.com/svn/trunk/ selenium<br />$ cd selenium<br />$ [sudo] python setup.py install</code><br /><br />With both Selenium and Lettuce installed, let's code.<br /><br /><span><b>Let's code! :)</b></span> <br /><br />The example will be very simple: a feature called "Login" with two scenarios: "Successful login" and "Unsuccessful login". See the feature "code" above, and understand it:<br /><br /><pre class="brush:gherkin">Feature: Login<br />  In order to have unlimited powers<br />  As a system anonymous user<br />  I want to login in the system<br /><br />  Scenario Outline: Successful and unsuccessful login<br />    Given that there is a user registered in the system with the username "admin" and the password "123"<br />    When I navigate to the login page<br />    And fill the username field with &lt;login&gt;<br />    And fill the password field with &lt;password&gt;<br />    And click the "Log in" button<br />    Then I see the message &lt;message&gt;<br /><br />  Examples:<br />    | login | password | message                                  |<br />    | admin | 123      | Welcome to admin                         |<br />    | admin | 1234     | Error. Verify your username and password |</pre><br />Put this feature file (<i>login.feature</i>) inside a directory called <i>feature</i> in the root of your application (or any other place).<br /><br /><b><span>Prepare the terrain and define the steps</span></b> <br /><br />According the <a href="http://lettuce.it/tutorial/simple.html#tutorial-simple" rel="nofollow"  title="Lettuce documentation">Lettuce documentation</a>, the second round is to define the steps described in the feature file. Before we define our steps, let's write a <i>terrain.py</i> Python module, which prepares the terrain to run our acceptance tests. The only things we need to do is setup a Selenium webdriver instance to test the content and the behaviour of our login page. Here is the <i>terrain.py </i>code:<br /><br /><pre class="brush:python">from lettuce import before, after, world<br />from selenium import get_driver, FIREFOX<br /><br />@before.all<br />def setup_browser():<br />    world.browser = get_driver(FIREFOX)<br />    world.root_url = 'http://localhost/php_app_test/'<br /><br />@after.all<br />def teardown_browser(total):<br />    world.browser.quit()<br /></pre><br />The terrain.py module should be in the <i>features</i> directory. Now we can define the steps from the scenario in a Python module called <i>login_steps</i>, and put this module inside a directory called <i>step_definitions</i>, in the <i>features</i> directory. Here is the directory tree:<br /><br /><div class="separator"><img border="0" src="http://1.bp.blogspot.com/_AewNgvR35uk/TDZd_yb5__I/AAAAAAAAApo/6BgsTuNN_XQ/s1600/screenshot3.png" /></div><br /><div></div>And in this Python module, we define the steps:<br /><br /><pre class="brush:python"># -*- coding: utf-8 -*-<br />from lettuce import *<br /><br />@step(u'Given that there is a user registered in the system with the username "admin" and the password "123"')<br />def do_nothing(step):<br />    '''There is nothing that Lettuce can do to guarantee this sentence'''<br />    pass<br /><br />@step(u'When I navigate to the login page')<br />def when_i_navigate_to_the_login_page(step):<br />    login_url = world.root_url + 'login.html'<br />    world.browser.get(login_url)<br /><br />@step(u'And I fill the username field with (.*)')<br />def and_i_fill_the_username_field_with_login(step, login):<br />    username_field = world.browser.find_element_by_xpath('//input[@name="username"]')<br />    username_field.send_keys(login)<br /><br />@step(u'And I fill the password field with (.*)')<br />def and_i_fill_the_password_field_with_password(step, password):<br />    password_field = world.browser.find_element_by_xpath('//input[@name="password"]')<br />    password_field.send_keys(password)<br /><br />@step(u'And I click the "(.*)" button')<br />def and_i_click_the_group1_button(step, button_value):<br />    button = world.browser.find_element_by_xpath('//input[@value="%s"]' %(button_value))<br />    button.click()<br /><br />@step(u'Then I see the message (.*)')<br />def then_i_see_the_message_message(step, message):<br />    assert message in world.browser.get_page_source()</pre><br />If you don't know Python, take a look at some free books (like <a href="http://diveintopython.org/" rel="nofollow"  title="Dive into Python">Dive into Python</a> and <a href="http://www.greenteapress.com/thinkpython/thinkpython.html" rel="nofollow"  title="Think Python">How to think like a computer scientist: Learning with Python</a>) and learn it :)<br /><br /><span><b>Understanding the step definitions</b></span><br /><br />Let's see more about the step definitions above. In the first step ("Given that there is a user registered in the system with the username "admin" and the password "123""), there is nothing to do: we cannot check and guarantee that there is a user registered in the system (unless we have access to the database, for example).<br /><br />On the second step ("When I navigate to the login page"), we use the browser provided by Selenium WebDriver to go to the login page, in the URL http://localhost/php_app_test/login.html. You can see how to use the Selenium WebDriver in <a href="http://seleniumhq.org/docs/09_webdriver.html" rel="nofollow"  title="Selenium 2.0 documentation">Selenium WebDriver docs</a>.<br /><br />The third step ("And fill the username field with &lt;login&gt;") is where we get the username text field by it's name and fill it with the login obtained from this step's sentence. We use the <a href="http://www.w3schools.com/XPath/xpath_syntax.asp" rel="nofollow"  title="XPath Syntax at W3Schools">xpath syntax</a> to find the field by it's name, and on the fourth step ("And fill the password field with &lt;password&gt;"), we get the password field by it's name and fill it with the password obtained from the fourth step sentence. The last browser interation is in the sixth step ("And click the "Log in" button"), when we get the "Log in" button and click it.<br /><br />The last step ("Then I see the message &lt;message&gt;") asserts that the properly message appears on the screen when the user try to login with the right and wrong data.<br /><br /><span><b>The PHP code</b></span> <br /><br />Now, let's write the PHP code needed to make this specification work. I will not connect to a database and execute a complete login, we will just build a HTML form in the login.html page and make a simple PHP code to show the properly message in each case. The HTML code is here:<br /><br /><pre class="brush:xml">&lt;html&gt;<br />    &lt;head&gt;<br />      &lt;meta http-equiv="Content-type" content="text/html; charset=utf-8"/&gt;<br />      &lt;title&gt;Login page&lt;/title&gt;<br />      <br />    &lt;/head&gt;<br />    &lt;body id=""&gt;<br />        &lt;form action="login.php" method="post" accept-charset="utf-8"&gt;<br />            &lt;label for="id_username"&gt;Username: &lt;/label&gt;&lt;input type="text" name="username" value="" id="id_username"/&gt;&lt;br /&gt;<br />            &lt;label for="id_password"&gt;Password: &lt;/label&gt;&lt;input type="password" name="password" value="" id="id_password"/&gt;<br />            &lt;input type="submit" name="submit" value="Log in" id=""/&gt;<br />        &lt;/form&gt;<br />    &lt;/body&gt;<br />&lt;/html&gt;</pre><br />And the <strike>stupid </strike>simple PHP code is here: <br /><br /><pre class="brush:php">&lt;?php<br /><br />    if ($_POST['username'] == 'admin' &amp;&amp; $_POST['password'] == '123') {<br />        echo 'Welcome to admin';<br />    } else {<br />        echo 'Error. Verify your username and password';<br />    }</pre><br />That was a really simple example that shows how to write high level tests to PHP applications, with Selenium WebDriver and Lettuce :) You can checkout the application code at Github: <a href="http://github.com/franciscosouza/php_app_test" rel="nofollow"  title="This project at Github">http://github.com/franciscosouza/php_app_test</a>.<div class="blogger-post-footer"><img width="1" height="1" src="https://blogger.googleusercontent.com/tracker/2635578411501939394-9136094375033144413?l=www.franciscosouza.net" alt="" /></div>]]></content:encoded>
			<wfw:commentRss>http://www.franciscosouza.net/2010/07/high-level-acceptance-testing-in-your.html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Francisco Souza: Seleniumless Django applications: using the test client</title>
		<link>http://www.franciscosouza.net/2010/07/seleniumless-django-applications-using.html</link>
		<comments>http://www.franciscosouza.net/2010/07/seleniumless-django-applications-using.html#comments</comments>
		<pubDate>Fri, 30 Jul 2010 01:07:37 +0000</pubDate>
		<dc:creator>Planet Python</dc:creator>
				<category><![CDATA[Planet]]></category>

		<guid isPermaLink="false">http://www.franciscosouza.net/2010/07/seleniumless-django-applications-using.html</guid>
		<description><![CDATA[Some people waste a lot of time testing Django applications only with Selenium. No, I don't think that you should not use Selenium to test Django applications, I just wanna show that nobody needs to use Selenium for all webtests in Django.<br /><br />Selenium drives a browser, so you can  have true test results on navigation, but Selenium is not needed to test a page title or a response status code, for example. Selenium tests are powerful, but slow, so you can preserve them to use when you really need, and use more simple and fast tools to more simple and fast tests.<br /><a name="more"></a><br />Django has a powerful tool for web tests: <a href="http://docs.djangoproject.com/en/dev/topics/testing/#module-django.test.client" rel="nofollow" target="_blank" title="Django test client documentation">the test client</a>. Using this module, you can make requests and test the response (its content, status code, URL and everything inside a response).<br /><br />Imagine that you have a Django application that manage all books of the world, and you want to test if the title of the <i>/books</i> page is <i>"All the books of the world"</i>, you can use Selenium, but IMHO, you should not use Selenium.<br /><br />Suppose that there is the following <a href="http://www.lettuce.it/" rel="nofollow" target="_blank" title="Lettuce - BDD in Python">Lettuce</a> feature file:<br /><br /><pre>Feature: List books<br />    In order to know which books are in the system<br />    I want to see the list of books<br />    <br />    Scenario: Listing all books in the world<br />        When I go to the /books URL<br />        Then the page title should be "All the books of the world"</pre><br />If we use Selenium to define the scenario "Listing all books in the world", we should have a <i>terrain.py</i> file with the following content:<br /><br /><pre>from lettuce import before, after, world<br />from selenium import get_driver, FIREFOX<br /><br />@before.all<br />def setup_browser():<br />    world.browser = get_driver(FIREFOX)<br /><br />@after.all<br />def teardown_browser(total):<br />    world.browser.quit()</pre><br />And define the steps definition with the following code:<br /><br /><pre># -*- coding: utf-8 -*-<br />from lettuce import step, world<br />from lettuce.django import django_url<br />from nose.tools import assert_equals<br /><br />@step(u'When I go to the /books URL')<br />def when_i_go_to_the_books_url(step):<br />    world.browser.get(django_url('books'))<br /><br />@step(u'Then the page title should be "(.*)"')<br />def then_the_page_title_should_be_group1(step, page_title):<br />    assert_equals(world.browser.get_title(), page_title)</pre><br /><br />There is nothing difficult here, the steps definition code is very simple and the test itself too, but how many time Lettuce spends running this test? Let's check:<br /><br /><pre><code>$ time ./manage.py harvest<br />Django's builtin server is running at 0.0.0.0:8000<br />Setting up a test database...OK<br /><br />...<br /><br />1 feature (1 passed)<br />1 scenario (1 passed)<br />2 steps (2 passed)<br /><br />real 0m7.172s<br />user 0m0.740s<br />sys 0m0.270s</code></pre><br />About 7 seconds to run a really really really simple and small test. We can earn time testing Django applications using the Django test client. Let's rewrite the test above using Django test client, we just need to change the <i>terrain.py</i>:<br /><br /><pre>from lettuce import before, world<br />from django.test import client<br /><br />@before.all<br />def setup_browser():<br />    world.browser = client.Client()</pre><br />And the step definitions:<br /><br /><pre># -*- coding: utf-8 -*-<br />from lettuce import step, world<br />from nose.tools import assert_equals<br />from lxml import html<br /><br />@step(u'When I go to the /books URL')<br />def when_i_go_to_the_books_url(step):<br />    world.response = world.browser.get('/books/')<br /><br />@step(u'Then the page title should be "(.*)"')<br />def then_the_page_title_should_be_group1(step, page_title):<br />    dom = html.fromstring(world.response.content)<br />    title = dom.xpath('//head/title')<br />    title = title.pop()<br />    assert_equals(title.text, page_title)</pre><br />The only notice here is that is we have a little much code to test the title of the page. How many time Lettuce spends testing it?<br /><br /><pre><code>$ time ./manage.py harvest<br />Django's builtin server is running at 0.0.0.0:8000<br />Setting up a test database...OK<br /><br />...<br /><br />1 feature (1 passed)<br />1 scenario (1 passed)<br />2 steps (2 passed)<br /><br />real 0m3.288s<br />user 0m0.700s<br />sys 0m0.130s</code></pre><br />About 3 seconds :) We didn't need to open Firefox for visit the page and get it's title, we just use the Django test client that knows how to deal with Django requests, and <i>lxml</i> to deal with the response content. Think about how many seconds you can earn just stopping to open a browser for the simplest tests of the world =P<br /><br />There is a lot of other stuffs that you can do using the Django Test Client, and I plan to write more about it, but now, just check the <a href="http://docs.djangoproject.com/en/dev/topics/testing/#module-django.test.client" rel="nofollow" target="_blank" title="Django test client documentation">official docs</a> and have fun.<br /><br />P.S.: Another great tool for Django testing is <a href="http://devel.almad.net/trac/django-sane-testing" rel="nofollow" target="_blank" title="Django sane testing">Django sane testing</a>. <br /><br />Happy testing :)<div class="blogger-post-footer"><img width="1" height="1" src="https://blogger.googleusercontent.com/tracker/2635578411501939394-6980222172750097559?l=www.franciscosouza.net" alt="" /></div>]]></description>
			<content:encoded><![CDATA[Some people waste a lot of time testing Django applications only with Selenium. No, I don't think that you should not use Selenium to test Django applications, I just wanna show that nobody needs to use Selenium for all webtests in Django.<br /><br />Selenium drives a browser, so you can  have true test results on navigation, but Selenium is not needed to test a page title or a response status code, for example. Selenium tests are powerful, but slow, so you can preserve them to use when you really need, and use more simple and fast tools to more simple and fast tests.<br /><a name="more"></a><br />Django has a powerful tool for web tests: <a href="http://docs.djangoproject.com/en/dev/topics/testing/#module-django.test.client" rel="nofollow"  title="Django test client documentation">the test client</a>. Using this module, you can make requests and test the response (its content, status code, URL and everything inside a response).<br /><br />Imagine that you have a Django application that manage all books of the world, and you want to test if the title of the <i>/books</i> page is <i>"All the books of the world"</i>, you can use Selenium, but IMHO, you should not use Selenium.<br /><br />Suppose that there is the following <a href="http://www.lettuce.it/" rel="nofollow"  title="Lettuce - BDD in Python">Lettuce</a> feature file:<br /><br /><pre class="brush:gherkin">Feature: List books<br />    In order to know which books are in the system<br />    I want to see the list of books<br />    <br />    Scenario: Listing all books in the world<br />        When I go to the /books URL<br />        Then the page title should be "All the books of the world"</pre><br />If we use Selenium to define the scenario "Listing all books in the world", we should have a <i>terrain.py</i> file with the following content:<br /><br /><pre class="brush:python">from lettuce import before, after, world<br />from selenium import get_driver, FIREFOX<br /><br />@before.all<br />def setup_browser():<br />    world.browser = get_driver(FIREFOX)<br /><br />@after.all<br />def teardown_browser(total):<br />    world.browser.quit()</pre><br />And define the steps definition with the following code:<br /><br /><pre class="brush:python"># -*- coding: utf-8 -*-<br />from lettuce import step, world<br />from lettuce.django import django_url<br />from nose.tools import assert_equals<br /><br />@step(u'When I go to the /books URL')<br />def when_i_go_to_the_books_url(step):<br />    world.browser.get(django_url('books'))<br /><br />@step(u'Then the page title should be "(.*)"')<br />def then_the_page_title_should_be_group1(step, page_title):<br />    assert_equals(world.browser.get_title(), page_title)</pre><br /><br />There is nothing difficult here, the steps definition code is very simple and the test itself too, but how many time Lettuce spends running this test? Let's check:<br /><br /><pre><code>$ time ./manage.py harvest<br />Django's builtin server is running at 0.0.0.0:8000<br />Setting up a test database...OK<br /><br />...<br /><br />1 feature (1 passed)<br />1 scenario (1 passed)<br />2 steps (2 passed)<br /><br />real 0m7.172s<br />user 0m0.740s<br />sys 0m0.270s</code></pre><br />About 7 seconds to run a really really really simple and small test. We can earn time testing Django applications using the Django test client. Let's rewrite the test above using Django test client, we just need to change the <i>terrain.py</i>:<br /><br /><pre class="brush:python">from lettuce import before, world<br />from django.test import client<br /><br />@before.all<br />def setup_browser():<br />    world.browser = client.Client()</pre><br />And the step definitions:<br /><br /><pre class="brush:python"># -*- coding: utf-8 -*-<br />from lettuce import step, world<br />from nose.tools import assert_equals<br />from lxml import html<br /><br />@step(u'When I go to the /books URL')<br />def when_i_go_to_the_books_url(step):<br />    world.response = world.browser.get('/books/')<br /><br />@step(u'Then the page title should be "(.*)"')<br />def then_the_page_title_should_be_group1(step, page_title):<br />    dom = html.fromstring(world.response.content)<br />    title = dom.xpath('//head/title')<br />    title = title.pop()<br />    assert_equals(title.text, page_title)</pre><br />The only notice here is that is we have a little much code to test the title of the page. How many time Lettuce spends testing it?<br /><br /><pre><code>$ time ./manage.py harvest<br />Django's builtin server is running at 0.0.0.0:8000<br />Setting up a test database...OK<br /><br />...<br /><br />1 feature (1 passed)<br />1 scenario (1 passed)<br />2 steps (2 passed)<br /><br />real 0m3.288s<br />user 0m0.700s<br />sys 0m0.130s</code></pre><br />About 3 seconds :) We didn't need to open Firefox for visit the page and get it's title, we just use the Django test client that knows how to deal with Django requests, and <i>lxml</i> to deal with the response content. Think about how many seconds you can earn just stopping to open a browser for the simplest tests of the world =P<br /><br />There is a lot of other stuffs that you can do using the Django Test Client, and I plan to write more about it, but now, just check the <a href="http://docs.djangoproject.com/en/dev/topics/testing/#module-django.test.client" rel="nofollow"  title="Django test client documentation">official docs</a> and have fun.<br /><br />P.S.: Another great tool for Django testing is <a href="http://devel.almad.net/trac/django-sane-testing" rel="nofollow"  title="Django sane testing">Django sane testing</a>. <br /><br />Happy testing :)<div class="blogger-post-footer"><img width="1" height="1" src="https://blogger.googleusercontent.com/tracker/2635578411501939394-6980222172750097559?l=www.franciscosouza.net" alt="" /></div>]]></content:encoded>
			<wfw:commentRss>http://www.franciscosouza.net/2010/07/seleniumless-django-applications-using.html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jared Forsyth: CodeTalker by example: test!</title>
		<link>http://jaredforsyth.com/blog/2010/jul/29/codetalker-example-test/</link>
		<comments>http://jaredforsyth.com/blog/2010/jul/29/codetalker-example-test/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 23:40:07 +0000</pubDate>
		<dc:creator>Planet Python</dc:creator>
				<category><![CDATA[Planet]]></category>

		<guid isPermaLink="false">http://jaredforsyth.com/blog/2010/jul/29/codetalker-example-test/</guid>
		<description><![CDATA[This post <a href="http://jaredforsyth.com/blog/2010/jul/29/codetalker-example-test/">looks better on my blog</a> <br />

<p>This is the last in a four-part series, in which I demonstrate how to build a
CSS parser using <a class="reference external" href="http://jaredforsyth.com/projects/codetalker/">CodeTalker</a>:</p>
<ul class="simple">
<li><a class="reference external" href="http://jaredforsyth.com/blog/2010/jul/27/codetalker-example-tokenize/">tokenize</a></li>
<li><a class="reference external" href="http://jaredforsyth.com/blog/2010/jul/28/codetalker-example-parse/">parse</a></li>
<li><a class="reference external" href="http://jaredforsyth.com/blog/2010/jul/29/codetalker-example-translate/">translate</a></li>
<li><strong>test!</strong></li>
</ul>
<p>To get the code for this:</p>
<p><tt>git clone</tt> <a class="reference external" href="http://github.com/jabapyth/css/">git://github.com/jabapyth/css.git</a></p>
<div class="section">
<h1>Test!</h1>
<p>No code is complete (and some would say it's broken) without a good amount of
testing, and CodeTalker provides a simple way to test your rules individually,
to ensure the best coverage.</p>
<p>the tests for our CSS grammar are contained in <a class="reference external" href="http://github.com/jabapyth/css/blob/master/tests/grammar.py">tests/grammar.py</a>. To run them
(ensure you have py.test installed, then) <tt>py.test tests/grammar.py</tt>. Or
just <tt>./setup.py test</tt> to run all of them.</p>
<p>The <a class="reference external" href="http://github.com/jabapyth/codetalker/blob/master/codetalker/testing.py#L1">codetalker/testing.py</a>
currently only has one function, but it will setup valitation tests for rule
parsing.</p>
<p>Here it is in action:</p>
<div class="codeblock"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">css.grammar</span> <span class="kn">as</span> <span class="nn">grammar</span>
<span class="kn">from</span> <span class="nn">codetalker</span> <span class="kn">import</span> <span class="n">testing</span>

<span class="n">parse_rule</span> <span class="o">=</span> <span class="n">testing</span><span class="o">.</span><span class="n">parse_rule</span><span class="p">(</span><span class="n">__name__</span><span class="p">,</span> <span class="n">grammar</span><span class="o">.</span><span class="n">grammar</span><span class="p">)</span>

<span class="n">parse_rule</span><span class="p">(</span><span class="n">grammar</span><span class="o">.</span><span class="n">class_</span><span class="p">,</span> <span class="p">(</span>
    <span class="s">'.one'</span><span class="p">,</span>
    <span class="s">'.green'</span><span class="p">,</span>
    <span class="s">'.GReEn'</span><span class="p">,</span>
    <span class="s">'.div'</span><span class="p">,</span>
    <span class="p">),</span> <span class="p">(</span>
    <span class="s">'one'</span><span class="p">,</span>
    <span class="p">))</span>

<span class="n">parse_rule</span><span class="p">(</span><span class="n">grammar</span><span class="o">.</span><span class="n">simple_selector</span><span class="p">,</span> <span class="p">(</span> <span class="c"># should pass</span>
    <span class="s">'div'</span><span class="p">,</span>
    <span class="s">'div#some'</span><span class="p">,</span>
    <span class="s">'div#one.green'</span><span class="p">,</span>
    <span class="s">'div.frown'</span><span class="p">,</span>
    <span class="s">'ul.cheese:first-child'</span><span class="p">,</span>
    <span class="s">'li.one.two.three'</span><span class="p">,</span>
    <span class="s">'a#b.c.d:last-child'</span><span class="p">,</span>
    <span class="p">),</span> <span class="p">(</span>                              <span class="c"># should fail</span>
    <span class="s">'one'</span><span class="p">,</span>
    <span class="s">'div# and'</span><span class="p">,</span>
    <span class="s">'div. one'</span><span class="p">,</span>
    <span class="p">))</span>
</pre></div>
</div><p>When you do that, <tt>parse_rule</tt> sets up a verification test for each string.</p>
<p>To get your very own factory function, call <tt>parse_rule = testing.parse_rule(__name__,
the_grammar)</tt> (it needs your module's <tt>__name__</tt> in order to setup the
functions in your <tt>global</tt> namespace, where py.test can recognize and run
them.</p>
<p>Then for each rule you want to test, call <tt>parse_rule(the_rule,
passing_strings, failing_strings</tt>.</p>
<p>This method of incremental testing is really a boon while you are initially
creating your grammar — this way, you don't have to complete the grammar
before you test parts of it out.</p>
<p>Now, in addition to just grammar testing, you should have some testing of your
translation as well, but the nature of those tests depends completely on your
specific project.</p>
</div>]]></description>
			<content:encoded><![CDATA[This post <a href="http://jaredforsyth.com/blog/2010/jul/29/codetalker-example-test/">looks better on my blog</a> <br />

<p>This is the last in a four-part series, in which I demonstrate how to build a
CSS parser using <a class="reference external" href="http://jaredforsyth.com/projects/codetalker/">CodeTalker</a>:</p>
<ul class="simple">
<li><a class="reference external" href="http://jaredforsyth.com/blog/2010/jul/27/codetalker-example-tokenize/">tokenize</a></li>
<li><a class="reference external" href="http://jaredforsyth.com/blog/2010/jul/28/codetalker-example-parse/">parse</a></li>
<li><a class="reference external" href="http://jaredforsyth.com/blog/2010/jul/29/codetalker-example-translate/">translate</a></li>
<li><strong>test!</strong></li>
</ul>
<p>To get the code for this:</p>
<p><tt class="docutils literal">git clone</tt> <a class="reference external" href="http://github.com/jabapyth/css/">git://github.com/jabapyth/css.git</a></p>
<div class="section" id="test">
<h1>Test!</h1>
<p>No code is complete (and some would say it's broken) without a good amount of
testing, and CodeTalker provides a simple way to test your rules individually,
to ensure the best coverage.</p>
<p>the tests for our CSS grammar are contained in <a class="reference external" href="http://github.com/jabapyth/css/blob/master/tests/grammar.py">tests/grammar.py</a>. To run them
(ensure you have py.test installed, then) <tt class="docutils literal">py.test tests/grammar.py</tt>. Or
just <tt class="docutils literal">./setup.py test</tt> to run all of them.</p>
<p>The <a class="reference external" href="http://github.com/jabapyth/codetalker/blob/master/codetalker/testing.py#L1">codetalker/testing.py</a>
currently only has one function, but it will setup valitation tests for rule
parsing.</p>
<p>Here it is in action:</p>
<div class="codeblock"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">css.grammar</span> <span class="kn">as</span> <span class="nn">grammar</span>
<span class="kn">from</span> <span class="nn">codetalker</span> <span class="kn">import</span> <span class="n">testing</span>

<span class="n">parse_rule</span> <span class="o">=</span> <span class="n">testing</span><span class="o">.</span><span class="n">parse_rule</span><span class="p">(</span><span class="n">__name__</span><span class="p">,</span> <span class="n">grammar</span><span class="o">.</span><span class="n">grammar</span><span class="p">)</span>

<span class="n">parse_rule</span><span class="p">(</span><span class="n">grammar</span><span class="o">.</span><span class="n">class_</span><span class="p">,</span> <span class="p">(</span>
    <span class="s">'.one'</span><span class="p">,</span>
    <span class="s">'.green'</span><span class="p">,</span>
    <span class="s">'.GReEn'</span><span class="p">,</span>
    <span class="s">'.div'</span><span class="p">,</span>
    <span class="p">),</span> <span class="p">(</span>
    <span class="s">'one'</span><span class="p">,</span>
    <span class="p">))</span>

<span class="n">parse_rule</span><span class="p">(</span><span class="n">grammar</span><span class="o">.</span><span class="n">simple_selector</span><span class="p">,</span> <span class="p">(</span> <span class="c"># should pass</span>
    <span class="s">'div'</span><span class="p">,</span>
    <span class="s">'div#some'</span><span class="p">,</span>
    <span class="s">'div#one.green'</span><span class="p">,</span>
    <span class="s">'div.frown'</span><span class="p">,</span>
    <span class="s">'ul.cheese:first-child'</span><span class="p">,</span>
    <span class="s">'li.one.two.three'</span><span class="p">,</span>
    <span class="s">'a#b.c.d:last-child'</span><span class="p">,</span>
    <span class="p">),</span> <span class="p">(</span>                              <span class="c"># should fail</span>
    <span class="s">'one'</span><span class="p">,</span>
    <span class="s">'div# and'</span><span class="p">,</span>
    <span class="s">'div. one'</span><span class="p">,</span>
    <span class="p">))</span>
</pre></div>
</div><p>When you do that, <tt class="docutils literal">parse_rule</tt> sets up a verification test for each string.</p>
<p>To get your very own factory function, call <tt class="docutils literal">parse_rule = testing.parse_rule(__name__,
the_grammar)</tt> (it needs your module's <tt class="docutils literal">__name__</tt> in order to setup the
functions in your <tt class="docutils literal">global</tt> namespace, where py.test can recognize and run
them.</p>
<p>Then for each rule you want to test, call <tt class="docutils literal">parse_rule(the_rule,
passing_strings, failing_strings</tt>.</p>
<p>This method of incremental testing is really a boon while you are initially
creating your grammar — this way, you don't have to complete the grammar
before you test parts of it out.</p>
<p>Now, in addition to just grammar testing, you should have some testing of your
translation as well, but the nature of those tests depends completely on your
specific project.</p>
</div>]]></content:encoded>
			<wfw:commentRss>http://jaredforsyth.com/blog/2010/jul/29/codetalker-example-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Shannon -jj Behrens: Software Engineering: Premature Featurization</title>
		<link>http://jjinux.blogspot.com/2010/07/software-engineering-premature.html</link>
		<comments>http://jjinux.blogspot.com/2010/07/software-engineering-premature.html#comments</comments>
		<pubDate>Thu, 29 Jul 2010 23:15:34 +0000</pubDate>
		<dc:creator>Planet Python</dc:creator>
				<category><![CDATA[Planet]]></category>

		<guid isPermaLink="false">http://jjinux.blogspot.com/2010/07/software-engineering-premature.html</guid>
		<description><![CDATA["Premature Featurization" is my term for the tendency to implement a plethora of features before they've even been requested by real users just in case a user might want them.<br /><br />It's related to <a href="http://catb.org/jargon/html/C/creeping-featurism.html">creeping featurism</a>.  Like <a href="http://en.wikipedia.org/wiki/Program_optimization">premature optimization</a>, it leads to difficult-to-maintain code bloat.<br /><br />Product managers and managers in general are especially prone to premature featurization.  When experienced developers themselves fall prey to premature featurization, they may be suffering from <a href="http://en.wikipedia.org/wiki/Second-system_effect">second system effect</a>.<br /><br />To some degree, the opposite of premature featurization is <a href="http://catb.org/jargon/html/C/creeping-elegance.html">creeping elegance</a>, which is where an existing piece of code is polished excessively at the expense of other factors such as the schedule or real world features.<br /><br />It should be pointed out that <a href="http://en.wikipedia.org/wiki/Agile_software_development">agile software development</a> aims to prevent premature featurization in that it forces coders to code only those features that the stake holders have requested as they request them.  However, it tends to be an enabler for premature featurization in that it allows stake holders to successfully request an unlimited number of features since the programmer doesn't have to design a complete, minimalistic, cohesive design ahead of time.<br /><br />Whereas <a href="http://en.wikipedia.org/wiki/Time_management">time management</a> is occasionally used by project managers to organize a developer's time as he attempts to implement a premature list of features, the goal of <a href="http://feedroom.businessweek.com/?fr_story=318982ebc3835e32644ff64aae68e0aa7c38d41c">task management</a> is to constrain the list of features requested to the minimal set possible.<div class="blogger-post-footer"><img width="1" height="1" src="https://blogger.googleusercontent.com/tracker/11788780-1199674432712311700?l=jjinux.blogspot.com" alt="" /></div>]]></description>
			<content:encoded><![CDATA["Premature Featurization" is my term for the tendency to implement a plethora of features before they've even been requested by real users just in case a user might want them.<br /><br />It's related to <a href="http://catb.org/jargon/html/C/creeping-featurism.html">creeping featurism</a>.  Like <a href="http://en.wikipedia.org/wiki/Program_optimization">premature optimization</a>, it leads to difficult-to-maintain code bloat.<br /><br />Product managers and managers in general are especially prone to premature featurization.  When experienced developers themselves fall prey to premature featurization, they may be suffering from <a href="http://en.wikipedia.org/wiki/Second-system_effect">second system effect</a>.<br /><br />To some degree, the opposite of premature featurization is <a href="http://catb.org/jargon/html/C/creeping-elegance.html">creeping elegance</a>, which is where an existing piece of code is polished excessively at the expense of other factors such as the schedule or real world features.<br /><br />It should be pointed out that <a href="http://en.wikipedia.org/wiki/Agile_software_development">agile software development</a> aims to prevent premature featurization in that it forces coders to code only those features that the stake holders have requested as they request them.  However, it tends to be an enabler for premature featurization in that it allows stake holders to successfully request an unlimited number of features since the programmer doesn't have to design a complete, minimalistic, cohesive design ahead of time.<br /><br />Whereas <a href="http://en.wikipedia.org/wiki/Time_management">time management</a> is occasionally used by project managers to organize a developer's time as he attempts to implement a premature list of features, the goal of <a href="http://feedroom.businessweek.com/?fr_story=318982ebc3835e32644ff64aae68e0aa7c38d41c">task management</a> is to constrain the list of features requested to the minimal set possible.<div class="blogger-post-footer"><img width="1" height="1" src="https://blogger.googleusercontent.com/tracker/11788780-1199674432712311700?l=jjinux.blogspot.com" alt="" /></div>]]></content:encoded>
			<wfw:commentRss>http://jjinux.blogspot.com/2010/07/software-engineering-premature.html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Auto Increment with MongoDB &#8211; Chris Shiflett</title>
		<link>http://shiflett.org/blog/2010/jul/auto-increment-with-mongodb</link>
		<comments>http://shiflett.org/blog/2010/jul/auto-increment-with-mongodb#comments</comments>
		<pubDate>Thu, 29 Jul 2010 19:52:18 +0000</pubDate>
		<dc:creator>Planet PHP</dc:creator>
				<category><![CDATA[Planet]]></category>

		<guid isPermaLink="false">tag:www.planet-php.net://cffef8e680a08a3f1be6088a64186bf5</guid>
		<description><![CDATA[<p><a href="http://analog.coop/">We</a> are currently working on an app that uses a number of technologies, including PHP, Python, and <a href="http://mongodb.org/">MongoDB</a>. Recently, a need arose to use sequential identifiers for users, similar to an <code>auto_increment</code> column in MySQL.</p>

<div class="note"><p>If you've used MongoDB, you might be familiar with the default behavior of using a <a href="http://en.wikipedia.org/wiki/Universally_unique_identifier"><abbr title="universally unique identifier">UUID</abbr></a> as the primary key. This is convenient, especially if you partition your database across servers, because you don't have to coordinate the primary key in any way. If you use sequential identifiers (as I demonstrate in this post), you can use multiple servers and interleave identifiers by advancing each server's sequence by the total number of servers. (For example, with two servers, advance each sequence by two, so one server generates even identifiers, and the other generates odd.)</p>
<p>I'd rather not discuss the advantages and disadvantages of either approach, because it's exactly this debate that makes it very difficult to find any useful information on using sequential identifiers with MongoDB. Instead, I'm just going to explain how I did it, and hope this is helpful to someone. :-)</p></div>

<p>First, create a sequence collection that you can use to determine the next identifier in the sequence. The following creates a collection called <code>seq</code> that has a single sequence in it (for <code>users</code>), but you can add as many as you need:</p>

<pre>
<code>db.seq.insert({"_id":"users", "seq":new NumberLong(1)});</code>
</pre>

<div class="note"><p>If you assign <code>seq</code> to <code>1</code> instead of <code>new NumberLong(1)</code>, it will be interpreted as a float due to a JavaScript quirk.</p></div>

<p>Before adding a new user, you need to increment the sequence by one and fetch the next identifier. Fortunately, the <a href="http://mongodb.org/display/DOCS/findandmodify+Command">findandmodify() command</a> provides an atomic way to do this. Using the MongoDB shell, the command would look something like this:</p>

<pre>
<code>db.seq.findAndModify({</code>
<code>    query: {"_id":"users"},</code>
<code>    update: {$inc: {"seq":1}},</code>
<code>    new: true</code>
<code>});</code>
</pre>

<p>Because I'm using <a href="http://lithify.me/">Lithium</a>, I added a method for fetching the next identifier to my <code>User</code> model:</p>

<pre>
<code><span class="default">&#60;?php</span></code>
<code><span class="default"> </span></code>
<code><span class="keyword">namespace </span><span class="default">app</span><span class="keyword">\</span><span class="default">models</span><span class="keyword">;</span></code>
<code><span class="keyword"> </span></code>
<code><span class="keyword">class </span><span class="default">User </span><span class="keyword">extends \</span><span class="default">lithium</span><span class="keyword">\</span><span class="default">data</span><span class="keyword">\</span><span class="default">Model </span><span class="keyword">{</span></code>
<code><span class="keyword"> </span></code>
<code><span class="keyword">    static public function </span><span class="default">seq</span><span class="keyword">() {</span></code>
<code><span class="keyword">        </span><span class="default">$seq </span><span class="keyword">= static::</span><span class="default">_connection</span><span class="keyword">()-&#62;</span><span class="default">connection</span><span class="keyword">-&#62;</span><span class="default">command</span><span class="keyword">(</span></code>
<code><span class="keyword">            array(</span><span class="string">'findandmodify' </span><span class="keyword">=&#62; </span><span class="string">'seq'</span><span class="keyword">,</span></code>
<code><span class="keyword">                  </span><span class="string">'query' </span><span class="keyword">=&#62; array(</span><span class="string">'_id' </span><span class="keyword">=&#62; </span><span class="string">'users'</span><span class="keyword">),</span></code>
<code><span class="keyword">                  </span><span class="string">'update' </span><span class="keyword">=&#62; array(</span><span class="string">'$inc' </span><span class="keyword">=&#62; array(</span><span class="string">'seq' </span><span class="keyword">=&#62; </span><span class="default">1</span><span class="keyword">)),</span></code>
<code><span class="keyword">                  </span><span class="string">'new' </span><span class="keyword">=&#62; </span><span class="default">TRUE</span></code>
<code><span class="default">            </span><span class="keyword">)</span></code>
<code><span class="keyword">        );</span></code>
<code><span class="keyword"> </span></code>
<code><span class="keyword">        return </span><span class="default">$seq</span></code></pre><p><i>Truncated by Planet PHP, read more at <a href="http://shiflett.org/blog/2010/jul/auto-increment-with-mongodb">the original</a> (another 2980 bytes)</i></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://analog.coop/">We</a> are currently working on an app that uses a number of technologies, including PHP, Python, and <a href="http://mongodb.org/">MongoDB</a>. Recently, a need arose to use sequential identifiers for users, similar to an <code>auto_increment</code> column in MySQL.</p>

<div class="note"><p>If you've used MongoDB, you might be familiar with the default behavior of using a <a href="http://en.wikipedia.org/wiki/Universally_unique_identifier"><abbr title="universally unique identifier">UUID</abbr></a> as the primary key. This is convenient, especially if you partition your database across servers, because you don't have to coordinate the primary key in any way. If you use sequential identifiers (as I demonstrate in this post), you can use multiple servers and interleave identifiers by advancing each server's sequence by the total number of servers. (For example, with two servers, advance each sequence by two, so one server generates even identifiers, and the other generates odd.)</p>
<p>I'd rather not discuss the advantages and disadvantages of either approach, because it's exactly this debate that makes it very difficult to find any useful information on using sequential identifiers with MongoDB. Instead, I'm just going to explain how I did it, and hope this is helpful to someone. :-)</p></div>

<p>First, create a sequence collection that you can use to determine the next identifier in the sequence. The following creates a collection called <code>seq</code> that has a single sequence in it (for <code>users</code>), but you can add as many as you need:</p>

<pre class="code">
<code>db.seq.insert({"_id":"users", "seq":new NumberLong(1)});</code>
</pre>

<div class="note"><p>If you assign <code>seq</code> to <code>1</code> instead of <code>new NumberLong(1)</code>, it will be interpreted as a float due to a JavaScript quirk.</p></div>

<p>Before adding a new user, you need to increment the sequence by one and fetch the next identifier. Fortunately, the <a href="http://mongodb.org/display/DOCS/findandmodify+Command">findandmodify() command</a> provides an atomic way to do this. Using the MongoDB shell, the command would look something like this:</p>

<pre class="code">
<code>db.seq.findAndModify({</code>
<code>    query: {"_id":"users"},</code>
<code>    update: {$inc: {"seq":1}},</code>
<code>    new: true</code>
<code>});</code>
</pre>

<p>Because I'm using <a href="http://lithify.me/">Lithium</a>, I added a method for fetching the next identifier to my <code>User</code> model:</p>

<pre class="code">
<code><span class="default">&lt;?php</span></code>
<code><span class="default"> </span></code>
<code><span class="keyword">namespace </span><span class="default">app</span><span class="keyword">\</span><span class="default">models</span><span class="keyword">;</span></code>
<code><span class="keyword"> </span></code>
<code><span class="keyword">class </span><span class="default">User </span><span class="keyword">extends \</span><span class="default">lithium</span><span class="keyword">\</span><span class="default">data</span><span class="keyword">\</span><span class="default">Model </span><span class="keyword">{</span></code>
<code><span class="keyword"> </span></code>
<code><span class="keyword">    static public function </span><span class="default">seq</span><span class="keyword">() {</span></code>
<code><span class="keyword">        </span><span class="default">$seq </span><span class="keyword">= static::</span><span class="default">_connection</span><span class="keyword">()-></span><span class="default">connection</span><span class="keyword">-></span><span class="default">command</span><span class="keyword">(</span></code>
<code><span class="keyword">            array(</span><span class="string">'findandmodify' </span><span class="keyword">=> </span><span class="string">'seq'</span><span class="keyword">,</span></code>
<code><span class="keyword">                  </span><span class="string">'query' </span><span class="keyword">=> array(</span><span class="string">'_id' </span><span class="keyword">=> </span><span class="string">'users'</span><span class="keyword">),</span></code>
<code><span class="keyword">                  </span><span class="string">'update' </span><span class="keyword">=> array(</span><span class="string">'$inc' </span><span class="keyword">=> array(</span><span class="string">'seq' </span><span class="keyword">=> </span><span class="default">1</span><span class="keyword">)),</span></code>
<code><span class="keyword">                  </span><span class="string">'new' </span><span class="keyword">=> </span><span class="default">TRUE</span></code>
<code><span class="default">            </span><span class="keyword">)</span></code>
<code><span class="keyword">        );</span></code>
<code><span class="keyword"> </span></code>
<code><span class="keyword">        return </span><span class="default">$seq</span></code></pre><p><i>Truncated by Planet PHP, read more at <a href="http://shiflett.org/blog/2010/jul/auto-increment-with-mongodb">the original</a> (another 2980 bytes)</i></p>]]></content:encoded>
			<wfw:commentRss>http://shiflett.org/blog/2010/jul/auto-increment-with-mongodb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jared Forsyth: CodeTalker by example: translate</title>
		<link>http://jaredforsyth.com/blog/2010/jul/29/codetalker-example-translate/</link>
		<comments>http://jaredforsyth.com/blog/2010/jul/29/codetalker-example-translate/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 19:40:08 +0000</pubDate>
		<dc:creator>Planet Python</dc:creator>
				<category><![CDATA[Planet]]></category>

		<guid isPermaLink="false">http://jaredforsyth.com/blog/2010/jul/29/codetalker-example-translate/</guid>
		<description><![CDATA[This post <a href="http://jaredforsyth.com/blog/2010/jul/29/codetalker-example-translate/">looks better on my blog</a> <br />

<p>This is the third in a four-part series, in which I demonstrate how to build a
CSS parser using <a class="reference external" href="http://jaredforsyth.com/projects/codetalker/">CodeTalker</a>:</p>
<ul class="simple">
<li><a class="reference external" href="http://jaredforsyth.com/blog/2010/jul/27/codetalker-example-tokenize/">tokenize</a></li>
<li><a class="reference external" href="http://jaredforsyth.com/blog/2010/jul/28/codetalker-example-parse/">parse</a></li>
<li><strong>translate</strong></li>
<li><a class="reference external" href="http://jaredforsyth.com/blog/2010/jul/29/codetalker-example-test/">test!</a></li>
</ul>
<p>To get the code for this:</p>
<p><tt>git clone</tt> <a class="reference external" href="http://github.com/jabapyth/css/">git://github.com/jabapyth/css.git</a></p>
<div class="section">
<h1>Translation</h1>
<p><em>[for the impatient, here's the</em> <a class="reference external" href="http://github.com/jabapyth/css/blob/master/css/translator.py#L1">final code</a> <em>].</em></p>
<p>Like everything else in CodeTalker, translation is mean to be straightforward;
you have an abstract syntax tree, which is cool, but what you really want
is...in this case, an object which represents a stylesheet. So you have a
<tt>StyleSheet</tt> class and a <tt>RuleSet</tt> class, but how do you go from AST
to that?</p>
<p>In general, you'll create one translation rule for each grammar rule.  Here's
the code for our <tt>stylesheet</tt> rule to refresh your memory.</p>
<div class="codeblock"><div class="highlight"><pre><span class="k">def</span> <span class="nf">style_sheet</span><span class="p">(</span><span class="n">rule</span><span class="p">):</span>
    <span class="n">rule</span> <span class="o">&#124;</span> <span class="p">([</span><span class="n">charset</span><span class="p">],</span> <span class="n">star</span><span class="p">(</span><span class="n">import_</span><span class="p">),</span> <span class="n">star</span><span class="p">(</span><span class="n">section</span><span class="p">))</span>
    <span class="n">rule</span><span class="o">.</span><span class="n">astAttrs</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s">'charset'</span><span class="p">:</span> <span class="n">charset</span><span class="p">,</span>
            <span class="s">'imports'</span><span class="p">:</span> <span class="p">[</span><span class="n">import_</span><span class="p">],</span>
            <span class="s">'sections'</span><span class="p">:</span> <span class="p">[</span><span class="n">section</span><span class="p">],</span>
        <span class="p">}</span>
</pre></div>
</div><p>And here's what it takes to translate it:</p>
<div class="codeblock"><div class="highlight"><pre><span class="nd">@t.translates</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">StyleSheet</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">stylesheet</span><span class="p">(</span><span class="n">node</span><span class="p">):</span>
    <span class="n">ss</span> <span class="o">=</span> <span class="n">StyleSheet</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">charset</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">ss</span><span class="o">.</span><span class="n">charset</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">translate</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">charset</span><span class="p">)</span>
    <span class="n">ss</span><span class="o">.</span><span class="n">imports</span> <span class="o">=</span> <span class="nb">list</span><span class="p">((</span><span class="n">imp</span><span class="o">.</span><span class="n">source</span><span class="p">,</span> <span class="n">imp</span><span class="o">.</span><span class="n">media</span><span class="p">)</span> <span class="k">for</span> <span class="n">imp</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">imports</span><span class="p">)</span>
    <span class="n">ss</span><span class="o">.</span><span class="n">rules</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">section</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">sections</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">section</span><span class="p">,</span> <span class="n">ast</span><span class="o">.</span><span class="n">Ruleset</span><span class="p">):</span> <span class="c"># right now I'm not dealing</span>
                                             <span class="c"># with other kinds of sections (media, page, font_face)</span>
            <span class="n">rule</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">translate</span><span class="p">(</span><span class="n">section</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">rule</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                <span class="n">ss</span><span class="o">.</span><span class="n">rules</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">rule</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">ss</span>
</pre></div>
</div><p>Create the object, populate it with the AST node, and then return it. And
register the function w/ the translator object.</p>
</div>]]></description>
			<content:encoded><![CDATA[This post <a href="http://jaredforsyth.com/blog/2010/jul/29/codetalker-example-translate/">looks better on my blog</a> <br />

<p>This is the third in a four-part series, in which I demonstrate how to build a
CSS parser using <a class="reference external" href="http://jaredforsyth.com/projects/codetalker/">CodeTalker</a>:</p>
<ul class="simple">
<li><a class="reference external" href="http://jaredforsyth.com/blog/2010/jul/27/codetalker-example-tokenize/">tokenize</a></li>
<li><a class="reference external" href="http://jaredforsyth.com/blog/2010/jul/28/codetalker-example-parse/">parse</a></li>
<li><strong>translate</strong></li>
<li><a class="reference external" href="http://jaredforsyth.com/blog/2010/jul/29/codetalker-example-test/">test!</a></li>
</ul>
<p>To get the code for this:</p>
<p><tt class="docutils literal">git clone</tt> <a class="reference external" href="http://github.com/jabapyth/css/">git://github.com/jabapyth/css.git</a></p>
<div class="section" id="translation">
<h1>Translation</h1>
<p><em>[for the impatient, here's the</em> <a class="reference external" href="http://github.com/jabapyth/css/blob/master/css/translator.py#L1">final code</a> <em>].</em></p>
<p>Like everything else in CodeTalker, translation is mean to be straightforward;
you have an abstract syntax tree, which is cool, but what you really want
is...in this case, an object which represents a stylesheet. So you have a
<tt class="docutils literal">StyleSheet</tt> class and a <tt class="docutils literal">RuleSet</tt> class, but how do you go from AST
to that?</p>
<p>In general, you'll create one translation rule for each grammar rule.  Here's
the code for our <tt class="docutils literal">stylesheet</tt> rule to refresh your memory.</p>
<div class="codeblock"><div class="highlight"><pre><span class="k">def</span> <span class="nf">style_sheet</span><span class="p">(</span><span class="n">rule</span><span class="p">):</span>
    <span class="n">rule</span> <span class="o">|</span> <span class="p">([</span><span class="n">charset</span><span class="p">],</span> <span class="n">star</span><span class="p">(</span><span class="n">import_</span><span class="p">),</span> <span class="n">star</span><span class="p">(</span><span class="n">section</span><span class="p">))</span>
    <span class="n">rule</span><span class="o">.</span><span class="n">astAttrs</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s">'charset'</span><span class="p">:</span> <span class="n">charset</span><span class="p">,</span>
            <span class="s">'imports'</span><span class="p">:</span> <span class="p">[</span><span class="n">import_</span><span class="p">],</span>
            <span class="s">'sections'</span><span class="p">:</span> <span class="p">[</span><span class="n">section</span><span class="p">],</span>
        <span class="p">}</span>
</pre></div>
</div><p>And here's what it takes to translate it:</p>
<div class="codeblock"><div class="highlight"><pre><span class="nd">@t.translates</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">StyleSheet</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">stylesheet</span><span class="p">(</span><span class="n">node</span><span class="p">):</span>
    <span class="n">ss</span> <span class="o">=</span> <span class="n">StyleSheet</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">charset</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">ss</span><span class="o">.</span><span class="n">charset</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">translate</span><span class="p">(</span><span class="n">node</span><span class="o">.</span><span class="n">charset</span><span class="p">)</span>
    <span class="n">ss</span><span class="o">.</span><span class="n">imports</span> <span class="o">=</span> <span class="nb">list</span><span class="p">((</span><span class="n">imp</span><span class="o">.</span><span class="n">source</span><span class="p">,</span> <span class="n">imp</span><span class="o">.</span><span class="n">media</span><span class="p">)</span> <span class="k">for</span> <span class="n">imp</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">imports</span><span class="p">)</span>
    <span class="n">ss</span><span class="o">.</span><span class="n">rules</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">section</span> <span class="ow">in</span> <span class="n">node</span><span class="o">.</span><span class="n">sections</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">section</span><span class="p">,</span> <span class="n">ast</span><span class="o">.</span><span class="n">Ruleset</span><span class="p">):</span> <span class="c"># right now I'm not dealing</span>
                                             <span class="c"># with other kinds of sections (media, page, font_face)</span>
            <span class="n">rule</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">translate</span><span class="p">(</span><span class="n">section</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">rule</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                <span class="n">ss</span><span class="o">.</span><span class="n">rules</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">rule</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">ss</span>
</pre></div>
</div><p>Create the object, populate it with the AST node, and then return it. And
register the function w/ the translator object.</p>
</div>]]></content:encoded>
			<wfw:commentRss>http://jaredforsyth.com/blog/2010/jul/29/codetalker-example-translate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Randell Benavidez: How to find Django version</title>
		<link>http://blog.randell.ph/2010/07/30/how-to-find-django-version/</link>
		<comments>http://blog.randell.ph/2010/07/30/how-to-find-django-version/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 17:12:52 +0000</pubDate>
		<dc:creator>Planet Python</dc:creator>
				<category><![CDATA[Planet]]></category>

		<guid isPermaLink="false">http://blog.randell.ph/2010/07/30/how-to-find-django-version/</guid>
		<description><![CDATA[<div class="tweetmeme_button">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.randell.ph%2F2010%2F07%2F30%2Fhow-to-find-django-version%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.randell.ph%2F2010%2F07%2F30%2Fhow-to-find-django-version%2F&#38;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>There are cases when you would want to know the version of Django that is being used in your Python installation. To do that, go to your Python console and execute the following commands:</p>

<div class="wp_syntax"><div class="code"><pre><span>&#62;&#62;&#62;</span> <span>import</span> django
<span>&#62;&#62;&#62;</span> django.<span>VERSION</span></pre></div></div>

<p>In my machine, it showed the following:</p>

<div class="wp_syntax"><div class="code"><pre><span>&#40;</span><span>1</span>, <span>2</span>, <span>1</span>, <span>'final'</span>, <span>0</span><span>&#41;</span></pre></div></div>

<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://blog.randell.ph/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark" /></a> </p>

<p>Related posts:<ol><li><a href="http://blog.randell.ph/2009/07/26/install-sqlite-database-browser-on-fedora/" rel="bookmark" title="Permanent Link: Install SQLite Database Browser on Fedora">Install SQLite Database Browser on Fedora</a></li>
<li><a href="http://blog.randell.ph/2010/04/05/using-gae-testbed-with-gaeunit-testing-that-email-was-sent/" rel="bookmark" title="Permanent Link: Using GAE Testbed with GAEUnit: Testing that email was sent">Using GAE Testbed with GAEUnit: Testing that email was sent</a></li>
<li><a href="http://blog.randell.ph/2009/09/17/install-idle-on-fedora-11/" rel="bookmark" title="Permanent Link: Install IDLE on Fedora 11">Install IDLE on Fedora 11</a></li>
<li><a href="http://blog.randell.ph/2010/04/03/convert-chm-to-pdf-in-fedora-linux/" rel="bookmark" title="Permanent Link: Convert CHM to PDF in (Fedora) Linux">Convert CHM to PDF in (Fedora) Linux</a></li>
<li><a href="http://blog.randell.ph/2010/04/04/setuptools/" rel="bookmark" title="Permanent Link: Install setuptools in Fedora 12">Install setuptools in Fedora 12</a></li>
</ol></p>
	Tags: <a href="http://blog.randell.ph/tag/django/" title="Django" rel="tag">Django</a>, <a href="http://blog.randell.ph/tag/python/" title="Python" rel="tag">Python</a><br />]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.randell.ph%2F2010%2F07%2F30%2Fhow-to-find-django-version%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.randell.ph%2F2010%2F07%2F30%2Fhow-to-find-django-version%2F&style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>There are cases when you would want to know the version of Django that is being used in your Python installation. To do that, go to your Python console and execute the following commands:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span>&gt;&gt;&gt;</span> <span>import</span> django
<span>&gt;&gt;&gt;</span> django.<span>VERSION</span></pre></div></div>

<p>In my machine, it showed the following:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span>&#40;</span><span>1</span>, <span>2</span>, <span>1</span>, <span>'final'</span>, <span>0</span><span>&#41;</span></pre></div></div>

<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://blog.randell.ph/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark" /></a> </p>

<p>Related posts:<ol><li><a href="http://blog.randell.ph/2009/07/26/install-sqlite-database-browser-on-fedora/" rel="bookmark" title="Permanent Link: Install SQLite Database Browser on Fedora">Install SQLite Database Browser on Fedora</a></li>
<li><a href="http://blog.randell.ph/2010/04/05/using-gae-testbed-with-gaeunit-testing-that-email-was-sent/" rel="bookmark" title="Permanent Link: Using GAE Testbed with GAEUnit: Testing that email was sent">Using GAE Testbed with GAEUnit: Testing that email was sent</a></li>
<li><a href="http://blog.randell.ph/2009/09/17/install-idle-on-fedora-11/" rel="bookmark" title="Permanent Link: Install IDLE on Fedora 11">Install IDLE on Fedora 11</a></li>
<li><a href="http://blog.randell.ph/2010/04/03/convert-chm-to-pdf-in-fedora-linux/" rel="bookmark" title="Permanent Link: Convert CHM to PDF in (Fedora) Linux">Convert CHM to PDF in (Fedora) Linux</a></li>
<li><a href="http://blog.randell.ph/2010/04/04/setuptools/" rel="bookmark" title="Permanent Link: Install setuptools in Fedora 12">Install setuptools in Fedora 12</a></li>
</ol></p>
	Tags: <a href="http://blog.randell.ph/tag/django/" title="Django" rel="tag">Django</a>, <a href="http://blog.randell.ph/tag/python/" title="Python" rel="tag">Python</a><br />]]></content:encoded>
			<wfw:commentRss>http://blog.randell.ph/2010/07/30/how-to-find-django-version/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bryce Verdier: Project Euler: Problem 5</title>
		<link>http://scrollingtext.org/project-euler-problem-5</link>
		<comments>http://scrollingtext.org/project-euler-problem-5#comments</comments>
		<pubDate>Thu, 29 Jul 2010 17:06:50 +0000</pubDate>
		<dc:creator>Planet Python</dc:creator>
				<category><![CDATA[Planet]]></category>

		<guid isPermaLink="false">http://scrollingtext.org/project-euler-problem-5</guid>
		<description><![CDATA[<p>It's that time again. ;)</p>
<p>Question five reads:<br />
<code>2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?</code></p>
<p>And here are my solutions (which I hope are correct this time).</p>
<p>Haskell:<br />
<div class="geshifilter"><pre><ol><li><div><span>module</span> Main <span>where</span></div></li><li><div>&#160;</div></li><li><div><span>import</span> Data<span>.</span>List</div></li><li><div>&#160;</div></li><li><div>divisible<span>_</span>11<span>_</span>to<span>_</span>20 number <span>=</span> 10 <span>==</span> <span>&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:length"><span>length</span></a> <span>$</span> unfoldr<span>&#40;</span>\x <span>-&#62;</span> <span>if</span> <span>&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:snd"><span>snd</span></a> <span>$</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:quotRem"><span>quotRem</span></a> number x<span>&#41;</span> <span>/=</span> 0 <span>&#124;&#124;</span> x <span>&#60;</span> 11</div></li><li><div>  <span>then</span> Nothing</div></li><li><div>  <span>else</span> Just<span>&#40;</span>x<span>,</span>x <span>-</span> <span>1</span><span>&#41;</span><span>&#41;</span> <span>20</span><span>&#41;</span></div></li><li><div>&#160;</div></li><li><div><span>-- solved this by the help on this URL:</span></div></li><li><div><span>-- (http)basildoncoder.com/blog/2008/06/10/project-euler-problem-5/</span></div></li><li><div><span>-- by increaing the loop from 2 to 2520, problem solves in seconds</span></div></li><li><div>main <span>::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span>IO</span></a> <span>&#40;</span><span>&#41;</span></div></li><li><div>main <span>=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:print"><span>print</span></a> <span>$</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:until"><span>until</span></a> <span>&#40;</span>divisible<span>_</span>11<span>_</span>to<span>_</span>20<span>&#41;</span> <span>&#40;</span><span>+</span><span>2520</span><span>&#41;</span> <span>2520</span></div></li></ol></pre></div></p>
<p>As a quick note, yes I know that I could do this much quicker and cleaner with a list comprehension. I decided to use unfoldr because I wanted the experience of working with it. If it wasn't for this little desire my answer would have looked a lot more like my Python answer.</p>
<p>Python:<br />
<div class="geshifilter"><pre><ol><li><div><span>#!/usr/bin/python</span></div></li><li><div>&#160;</div></li><li><div><span>def</span> div_11_to_20<span>&#40;</span>divided<span>&#41;</span>:</div></li><li><div>  <span>return</span> <span>all</span><span>&#40;</span><span>&#91;</span><span>not</span> <span>bool</span><span>&#40;</span>divided <span>%</span> x<span>&#41;</span> <span>for</span> x <span>in</span> <span>xrange</span><span>&#40;</span>11,20+1<span>&#41;</span><span>&#93;</span><span>&#41;</span></div></li><li><div>&#160;</div></li><li><div><span>if</span> __name__ == <span>&#34;__main__&#34;</span>:</div></li><li><div>  count = 2520</div></li><li><div>  <span>while</span> div_11_to_20<span>&#40;</span>count<span>&#41;</span> == <span>False</span>:</div></li><li><div>    count += 2520</div></li><li><div>&#160;</div></li><li><div>  <span>print</span> <span>&#34;%s&#34;</span> <span>%</span> count</div></li></ol></pre></div></p>
<p>And finally my Perl solution:<br />
<div class="geshifilter"><pre><ol><li><div><span>#!/usr/bin/perl</span></div></li><li><div>&#160;</div></li><li><div><span>sub</span> divide_11_to_20</div></li><li><div><span>&#123;</span></div></li><li><div>  <span>my</span> <span>&#40;</span> <span>$divided</span> <span>&#41;</span> <span>=</span> <span>@_</span><span>;</span></div></li><li><div>&#160;</div></li><li><div>  <span>foreach</span> <span>&#40;</span>11<span>..</span>20<span>&#41;</span></div></li><li><div>  <span>&#123;</span></div></li><li><div>     <a href="http://perldoc.perl.org/functions/return.html"><span>return</span></a> 0 <span>if</span> <span>&#40;</span><span>$divided</span> <span>%</span> <span>$_</span><span>&#41;</span><span>;</span></div></li><li><div>  <span>&#125;</span></div></li><li><div>&#160;</div></li><li><div>  <a href="http://perldoc.perl.org/functions/return.html"><span>return</span></a> <span>1</span><span>;</span></div></li><li><div><span>&#125;</span></div></li><li><div>&#160;</div></li><li><div><span>my</span> <span>$main_count</span> <span>=</span> <span>2520</span><span>;</span></div></li><li><div><span>while</span> <span>&#40;</span> <span>!</span>divide_11_to_20<span>&#40;</span><span>$main_count</span><span>&#41;</span> <span>&#41;</span></div></li><li><div><span>&#123;</span></div></li><li><div>	<span>$main_count</span> <span>+=</span> <span>2520</span><span>;</span></div></li><li><div><span>&#125;</span></div></li><li><div>&#160;</div></li><li><div><a href="http://perldoc.perl.org/functions/print.html"><span>print</span></a> <span>$main_count</span><span>;</span></div></li></ol></pre></div></p>
<p><b>Run Times:</b><br />
Haskell: 0m1.302s<br />
Python: 0m0.769s<br />
Perl: 0m0.223s</p>
<p><b>Observations:</b><br />
It doesn't surprise me that the Perl solution ends up being the fastest on these run times. I say this because per iteration the Perl solutions has less calculations. Both the Haskell and Python solutions perform 10 divisions per iteration, where as the Perl solution only performs 10 divisions when the correct number is is being divided. Its one of those things where the difference is very small, but will become larger as the number of iterations increases.</p>]]></description>
			<content:encoded><![CDATA[<p>It's that time again. ;)</p>
<p>Question five reads:<br />
<code>2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?</code></p>
<p>And here are my solutions (which I hope are correct this time).</p>
<p>Haskell:<br />
<div class="geshifilter"><pre class="haskell geshifilter-haskell"><ol><li><div><span>module</span> Main <span>where</span></div></li><li><div>&nbsp;</div></li><li><div><span>import</span> Data<span>.</span>List</div></li><li><div>&nbsp;</div></li><li><div>divisible<span>_</span>11<span>_</span>to<span>_</span>20 number <span>=</span> 10 <span>==</span> <span>&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:length"><span>length</span></a> <span>$</span> unfoldr<span>&#40;</span>\x <span>-&gt;</span> <span>if</span> <span>&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:snd"><span>snd</span></a> <span>$</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:quotRem"><span>quotRem</span></a> number x<span>&#41;</span> <span>/=</span> 0 <span>||</span> x <span>&lt;</span> 11</div></li><li><div>  <span>then</span> Nothing</div></li><li><div>  <span>else</span> Just<span>&#40;</span>x<span>,</span>x <span>-</span> <span>1</span><span>&#41;</span><span>&#41;</span> <span>20</span><span>&#41;</span></div></li><li><div>&nbsp;</div></li><li><div><span>-- solved this by the help on this URL:</span></div></li><li><div><span>-- (http)basildoncoder.com/blog/2008/06/10/project-euler-problem-5/</span></div></li><li><div><span>-- by increaing the loop from 2 to 2520, problem solves in seconds</span></div></li><li><div>main <span>::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:IO"><span>IO</span></a> <span>&#40;</span><span>&#41;</span></div></li><li><div>main <span>=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:print"><span>print</span></a> <span>$</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:until"><span>until</span></a> <span>&#40;</span>divisible<span>_</span>11<span>_</span>to<span>_</span>20<span>&#41;</span> <span>&#40;</span><span>+</span><span>2520</span><span>&#41;</span> <span>2520</span></div></li></ol></pre></div></p>
<p>As a quick note, yes I know that I could do this much quicker and cleaner with a list comprehension. I decided to use unfoldr because I wanted the experience of working with it. If it wasn't for this little desire my answer would have looked a lot more like my Python answer.</p>
<p>Python:<br />
<div class="geshifilter"><pre class="python geshifilter-python"><ol><li><div><span>#!/usr/bin/python</span></div></li><li><div>&nbsp;</div></li><li><div><span>def</span> div_11_to_20<span>&#40;</span>divided<span>&#41;</span>:</div></li><li><div>  <span>return</span> <span>all</span><span>&#40;</span><span>&#91;</span><span>not</span> <span>bool</span><span>&#40;</span>divided <span>%</span> x<span>&#41;</span> <span>for</span> x <span>in</span> <span>xrange</span><span>&#40;</span>11,20+1<span>&#41;</span><span>&#93;</span><span>&#41;</span></div></li><li><div>&nbsp;</div></li><li><div><span>if</span> __name__ == <span>&quot;__main__&quot;</span>:</div></li><li><div>  count = 2520</div></li><li><div>  <span>while</span> div_11_to_20<span>&#40;</span>count<span>&#41;</span> == <span>False</span>:</div></li><li><div>    count += 2520</div></li><li><div>&nbsp;</div></li><li><div>  <span>print</span> <span>&quot;%s&quot;</span> <span>%</span> count</div></li></ol></pre></div></p>
<p>And finally my Perl solution:<br />
<div class="geshifilter"><pre class="perl geshifilter-perl"><ol><li><div><span>#!/usr/bin/perl</span></div></li><li><div>&nbsp;</div></li><li><div><span>sub</span> divide_11_to_20</div></li><li><div><span>&#123;</span></div></li><li><div>  <span>my</span> <span>&#40;</span> <span>$divided</span> <span>&#41;</span> <span>=</span> <span>@_</span><span>;</span></div></li><li><div>&nbsp;</div></li><li><div>  <span>foreach</span> <span>&#40;</span>11<span>..</span>20<span>&#41;</span></div></li><li><div>  <span>&#123;</span></div></li><li><div>     <a href="http://perldoc.perl.org/functions/return.html"><span>return</span></a> 0 <span>if</span> <span>&#40;</span><span>$divided</span> <span>%</span> <span>$_</span><span>&#41;</span><span>;</span></div></li><li><div>  <span>&#125;</span></div></li><li><div>&nbsp;</div></li><li><div>  <a href="http://perldoc.perl.org/functions/return.html"><span>return</span></a> <span>1</span><span>;</span></div></li><li><div><span>&#125;</span></div></li><li><div>&nbsp;</div></li><li><div><span>my</span> <span>$main_count</span> <span>=</span> <span>2520</span><span>;</span></div></li><li><div><span>while</span> <span>&#40;</span> <span>!</span>divide_11_to_20<span>&#40;</span><span>$main_count</span><span>&#41;</span> <span>&#41;</span></div></li><li><div><span>&#123;</span></div></li><li><div>	<span>$main_count</span> <span>+=</span> <span>2520</span><span>;</span></div></li><li><div><span>&#125;</span></div></li><li><div>&nbsp;</div></li><li><div><a href="http://perldoc.perl.org/functions/print.html"><span>print</span></a> <span>$main_count</span><span>;</span></div></li></ol></pre></div></p>
<p><b>Run Times:</b><br />
Haskell: 0m1.302s<br />
Python: 0m0.769s<br />
Perl: 0m0.223s</p>
<p><b>Observations:</b><br />
It doesn't surprise me that the Perl solution ends up being the fastest on these run times. I say this because per iteration the Perl solutions has less calculations. Both the Haskell and Python solutions perform 10 divisions per iteration, where as the Perl solution only performs 10 divisions when the correct number is is being divided. Its one of those things where the difference is very small, but will become larger as the number of iterations increases.</p>]]></content:encoded>
			<wfw:commentRss>http://scrollingtext.org/project-euler-problem-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python Sprints: Sprints at PyOhio</title>
		<link>http://pythonsprints.com/2010/07/29/sprints-pyohio/</link>
		<comments>http://pythonsprints.com/2010/07/29/sprints-pyohio/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 16:45:00 +0000</pubDate>
		<dc:creator>Planet Python</dc:creator>
				<category><![CDATA[Planet]]></category>

		<guid isPermaLink="false">http://pythonsprints.com/2010/07/29/sprints-pyohio/</guid>
		<description><![CDATA[<p>This year, PyOhio (July 31 - August 3, Columbus, OH) includes two evenings and two full days of sprints, including a Python Core sprint.  The core sprint is part of an integrated program (<a href="http://www.pyohio.org/Contribute">http://www.pyohio.org/Contribute</a>) to recruit new core contributors at the conference, train them, and get them started working in a mentored environment... all in one four-day event.</p>
<p>PyOhio is a free annual conference on all aspects of Python development. We hope to see you there!</p>]]></description>
			<content:encoded><![CDATA[<p>This year, PyOhio (July 31 - August 3, Columbus, OH) includes two evenings and two full days of sprints, including a Python Core sprint.  The core sprint is part of an integrated program (<a href="http://www.pyohio.org/Contribute">http://www.pyohio.org/Contribute</a>) to recruit new core contributors at the conference, train them, and get them started working in a mentored environment... all in one four-day event.</p>
<p>PyOhio is a free annual conference on all aspects of Python development. We hope to see you there!</p>]]></content:encoded>
			<wfw:commentRss>http://pythonsprints.com/2010/07/29/sprints-pyohio/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
