V.S. Babu: Quick script to maintain a diary
I like to keep my daily notes in a folder in the filesystem with filenames yyyymmdd.otl, using VIM Outliner. 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 diary.cmd and keep in your path.
Francisco Souza: High level acceptance testing in your PHP applications using Python, Lettuce and Selenium
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 (Lettuce) and Ruby (Cucumber), 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.
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.
Installing Lettuce
To install Lettuce from source code, you can clone the repository from github and install it using setuptools:
$ git clone git://github.com/gabrielfalcao/lettuce.git
$ cd lettuce
$ [sudo] python setup.py install
If you use pip or easy_install, you can also install Lettuce with the following commands:
$ [sudo] easy_install lettuce$ [sudo] pip install lettuce
Installing Selenium
The code used in this blog post was written for Selenium 2.0, wich is still alfa. So, to install Selenium, you need to checkout the SVN repository and run the setup.py install command:
$ svn checkout http://selenium.googlecode.com/svn/trunk/ selenium
$ cd selenium
$ [sudo] python setup.py install
With both Selenium and Lettuce installed, let’s code.
Let’s code!
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:
Feature: Login In order to have unlimited powers As a system anonymous user I want to login in the system Scenario Outline: Successful and unsuccessful login Given that there is a user registered in the system with the username "admin" and the password "123" When I navigate to the login page And fill the username field with <login> And fill the password field with <password> And click the "Log in" button Then I see the message <message> Examples: | login | password | message | | admin | 123 | Welcome to admin | | admin | 1234 | Error. Verify your username and password |
Put this feature file (login.feature) inside a directory called feature in the root of your application (or any other place).
Prepare the terrain and define the steps
According the Lettuce documentation, the second round is to define the steps described in the feature file. Before we define our steps, let’s write a terrain.py 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 terrain.py code:
from lettuce import before, after, worldfrom selenium import get_driver, FIREFOX @before.alldef setup_browser(): world.browser = get_driver(FIREFOX) world.root_url = 'http://localhost/php_app_test/' @after.alldef teardown_browser(total): world.browser.quit()
The terrain.py module should be in the features directory. Now we can define the steps from the scenario in a Python module called login_steps, and put this module inside a directory called step_definitions, in the features directory. Here is the directory tree:

And in this Python module, we define the steps:
# -*- coding: utf-8 -*-from lettuce import *
@step(u'Given that there is a user registered in the system with the username "admin" and the password "123"')def do_nothing(step): '''There is nothing that Lettuce can do to guarantee this sentence''' pass
@step(u'When I navigate to the login page')def when_i_navigate_to_the_login_page(step): login_url = world.root_url + 'login.html' world.browser.get(login_url)
@step(u'And I fill the username field with (.*)')def and_i_fill_the_username_field_with_login(step, login): username_field = world.browser.find_element_by_xpath('//input[@name="username"]') username_field.send_keys(login)
@step(u'And I fill the password field with (.*)')def and_i_fill_the_password_field_with_password(step, password): password_field = world.browser.find_element_by_xpath('//input[@name="password"]') password_field.send_keys(password)
@step(u'And I click the "(.*)" button')def and_i_click_the_group1_button(step, button_value): button = world.browser.find_element_by_xpath('//input[@value="%s"]' %(button_value)) button.click()
@step(u'Then I see the message (.*)')def then_i_see_the_message_message(step, message): assert message in world.browser.get_page_source()
If you don’t know Python, take a look at some free books (like Dive into Python and How to think like a computer scientist: Learning with Python) and learn it
Understanding the step definitions
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).
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 Selenium WebDriver docs.
The third step (”And fill the username field with <login>”) 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 xpath syntax to find the field by it’s name, and on the fourth step (”And fill the password field with <password>”), 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.
The last step (”Then I see the message <message>”) asserts that the properly message appears on the screen when the user try to login with the right and wrong data.
The PHP code
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:
<html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"/> <title>Login page</title>
</head> <body id=""> <form action="login.php" method="post" accept-charset="utf-8"> <label for="id_username">Username: </label><input type="text" name="username" value="" id="id_username"/><br /> <label for="id_password">Password: </label><input type="password" name="password" value="" id="id_password"/> <input type="submit" name="submit" value="Log in" id=""/> </form> </body></html>
And the stupid simple PHP code is here:
<?php
if ($_POST['username'] == 'admin' && $_POST['password'] == '123') { echo 'Welcome to admin'; } else { echo 'Error. Verify your username and password'; }
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: http://github.com/franciscosouza/php_app_test.
Francisco Souza: Seleniumless Django applications: using the test client
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.
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.
Django has a powerful tool for web tests: the test client. Using this module, you can make requests and test the response (its content, status code, URL and everything inside a response).
Imagine that you have a Django application that manage all books of the world, and you want to test if the title of the /books page is “All the books of the world”, you can use Selenium, but IMHO, you should not use Selenium.
Suppose that there is the following Lettuce feature file:
Feature: List books In order to know which books are in the system I want to see the list of books
Scenario: Listing all books in the world When I go to the /books URL Then the page title should be "All the books of the world"
If we use Selenium to define the scenario “Listing all books in the world”, we should have a terrain.py file with the following content:
from lettuce import before, after, worldfrom selenium import get_driver, FIREFOX @before.alldef setup_browser(): world.browser = get_driver(FIREFOX) @after.alldef teardown_browser(total): world.browser.quit()
And define the steps definition with the following code:
# -*- coding: utf-8 -*-from lettuce import step, worldfrom lettuce.django import django_urlfrom nose.tools import assert_equals
@step(u'When I go to the /books URL')def when_i_go_to_the_books_url(step): world.browser.get(django_url('books'))
@step(u'Then the page title should be "(.*)"')def then_the_page_title_should_be_group1(step, page_title): assert_equals(world.browser.get_title(), page_title)
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:
$ time ./manage.py harvestDjango's builtin server is running at 0.0.0.0:8000Setting up a test database...OK
...
1 feature (1 passed)1 scenario (1 passed)2 steps (2 passed)
real 0m7.172suser 0m0.740ssys 0m0.270s
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 terrain.py:
from lettuce import before, worldfrom django.test import client @before.alldef setup_browser(): world.browser = client.Client()
And the step definitions:
# -*- coding: utf-8 -*-from lettuce import step, worldfrom nose.tools import assert_equalsfrom lxml import html
@step(u'When I go to the /books URL')def when_i_go_to_the_books_url(step): world.response = world.browser.get('/books/')
@step(u'Then the page title should be "(.*)"')def then_the_page_title_should_be_group1(step, page_title): dom = html.fromstring(world.response.content) title = dom.xpath('//head/title') title = title.pop() assert_equals(title.text, page_title)
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?
$ time ./manage.py harvestDjango's builtin server is running at 0.0.0.0:8000Setting up a test database...OK
...
1 feature (1 passed)1 scenario (1 passed)2 steps (2 passed)
real 0m3.288suser 0m0.700ssys 0m0.130s
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 lxml 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
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 official docs and have fun.
P.S.: Another great tool for Django testing is Django sane testing.
Happy testing
Jared Forsyth: CodeTalker by example: test!
This post looks better on my blog
This is the last in a four-part series, in which I demonstrate how to build a
CSS parser using CodeTalker:
To get the code for this:
git clone git://github.com/jabapyth/css.git
Test!
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.
the tests for our CSS grammar are contained in tests/grammar.py. To run them
(ensure you have py.test installed, then) py.test tests/grammar.py. Or
just ./setup.py test to run all of them.
The codetalker/testing.py
currently only has one function, but it will setup valitation tests for rule
parsing.
Here it is in action:
import css.grammar as grammar
from codetalker import testing
parse_rule = testing.parse_rule(__name__, grammar.grammar)
parse_rule(grammar.class_, (
'.one',
'.green',
'.GReEn',
'.div',
), (
'one',
))
parse_rule(grammar.simple_selector, ( # should pass
'div',
'div#some',
'div#one.green',
'div.frown',
'ul.cheese:first-child',
'li.one.two.three',
'a#b.c.d:last-child',
), ( # should fail
'one',
'div# and',
'div. one',
))
When you do that, parse_rule sets up a verification test for each string.
To get your very own factory function, call parse_rule = testing.parse_rule(__name__,
the_grammar) (it needs your module’s __name__ in order to setup the
functions in your global namespace, where py.test can recognize and run
them.
Then for each rule you want to test, call parse_rule(the_rule,
passing_strings, failing_strings.
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.
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.
Shannon -jj Behrens: Software Engineering: Premature Featurization
“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.
It’s related to creeping featurism. Like premature optimization, it leads to difficult-to-maintain code bloat.
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 second system effect.
To some degree, the opposite of premature featurization is creeping elegance, 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.
It should be pointed out that agile software development 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.
Whereas time management 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 task management is to constrain the list of features requested to the minimal set possible.
Auto Increment with MongoDB – Chris Shiflett
We are currently working on an app that uses a number of technologies, including PHP, Python, and MongoDB. Recently, a need arose to use sequential identifiers for users, similar to an auto_increment column in MySQL.
If you’ve used MongoDB, you might be familiar with the default behavior of using a UUID 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.)
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.
First, create a sequence collection that you can use to determine the next identifier in the sequence. The following creates a collection called seq that has a single sequence in it (for users), but you can add as many as you need:
db.seq.insert({"_id":"users", "seq":new NumberLong(1)});
If you assign seq to 1 instead of new NumberLong(1), it will be interpreted as a float due to a JavaScript quirk.
Before adding a new user, you need to increment the sequence by one and fetch the next identifier. Fortunately, the findandmodify() command provides an atomic way to do this. Using the MongoDB shell, the command would look something like this:
db.seq.findAndModify({query: {"_id":"users"},update: {$inc: {"seq":1}},new: true});
Because I’m using Lithium, I added a method for fetching the next identifier to my User model:
<?phpÂnamespace app\models;Âclass User extends \lithium\data\Model {Âstatic public function seq() {$seq = static::_connection()->connection->command(array('findandmodify' => 'seq','query' => array('_id' => 'users'),'update' => array('$inc' => array('seq' => 1)),'new' => TRUE));Âreturn $seq
Truncated by Planet PHP, read more at the original (another 2980 bytes)
Jared Forsyth: CodeTalker by example: translate
This post looks better on my blog
This is the third in a four-part series, in which I demonstrate how to build a
CSS parser using CodeTalker:
To get the code for this:
git clone git://github.com/jabapyth/css.git
Translation
[for the impatient, here's the final code ].
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
StyleSheet class and a RuleSet class, but how do you go from AST
to that?
In general, you’ll create one translation rule for each grammar rule. Here’s
the code for our stylesheet rule to refresh your memory.
def style_sheet(rule):
rule | ([charset], star(import_), star(section))
rule.astAttrs = {
'charset': charset,
'imports': [import_],
'sections': [section],
}
And here’s what it takes to translate it:
@t.translates(ast.StyleSheet)
def stylesheet(node):
ss = StyleSheet()
if node.charset is not None:
ss.charset = t.translate(node.charset)
ss.imports = list((imp.source, imp.media) for imp in node.imports)
ss.rules = []
for section in node.sections:
if isinstance(section, ast.Ruleset): # right now I'm not dealing
# with other kinds of sections (media, page, font_face)
rule = t.translate(section)
if rule is not None:
ss.rules.append(rule)
return ss
Create the object, populate it with the AST node, and then return it. And
register the function w/ the translator object.
Randell Benavidez: How to find Django version
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:
>>> import django >>> django.VERSION
In my machine, it showed the following:
(1, 2, 1, 'final', 0)
Related posts:
- Install SQLite Database Browser on Fedora
- Using GAE Testbed with GAEUnit: Testing that email was sent
- Install IDLE on Fedora 11
- Convert CHM to PDF in (Fedora) Linux
- Install setuptools in Fedora 12
Bryce Verdier: Project Euler: Problem 5
It’s that time again.
Question five reads:
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?
And here are my solutions (which I hope are correct this time).
Haskell:
module Main where import Data.List then Nothing else Just(x,x - 1)) 20) -- solved this by the help on this URL: -- (http)basildoncoder.com/blog/2008/06/10/project-euler-problem-5/ -- by increaing the loop from 2 to 2520, problem solves in seconds
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.
Python:
#!/usr/bin/python def div_11_to_20(divided): return all([not bool(divided % x) for x in xrange(11,20+1)]) if __name__ == "__main__": count = 2520 while div_11_to_20(count) == False: count += 2520 print "%s" % count
And finally my Perl solution:
#!/usr/bin/perl sub divide_11_to_20 { my ( $divided ) = @_; foreach (11..20) { } } my $main_count = 2520; while ( !divide_11_to_20($main_count) ) { $main_count += 2520; }
Run Times:
Haskell: 0m1.302s
Python: 0m0.769s
Perl: 0m0.223s
Observations:
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.
Python Sprints: Sprints at PyOhio
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 (http://www.pyohio.org/Contribute) 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.
PyOhio is a free annual conference on all aspects of Python development. We hope to see you there!
keep looking »Warning: include(/home/remarkwit/enterpriselamp.org/wp-content/themes/Enterprise_LAMP/r_sidebar.php) [function.include]: failed to open stream: No such file or directory in /home/remarkwit/enterpriselamp.org/wp-content/themes/Enterprise_LAMP/archive.php on line 23
Warning: include() [function.include]: Failed opening '/home/remarkwit/enterpriselamp.org/wp-content/themes/Enterprise_LAMP/r_sidebar.php' for inclusion (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear') in /home/remarkwit/enterpriselamp.org/wp-content/themes/Enterprise_LAMP/archive.php on line 23
