The problems faced by a common model interface in frameworks

Tags:

You sometimes see people asking why the Zend Framework (or some others) don’t provide a library for the Model part in MVC.

While ZF provides data access classes in the form of Zend_Db and related components, it doesn’t provide any concrete examples of how you would implement a model class. This often confuses especially beginners.

Providing a common base model, or an interface for implementing models, does have certain benefits, but it also has some problems…

Read More

Scraping HTML with Python

Tags:

Have you ever had to write a script that scrapes data from an HTML page? Was the page horribly bad HTML too? If so, you probably know how annoying and time consuming it can be to write a script that reliably fetches data from such a mess.

I was recently asked to write a script that scrapes an ASP.NET page at work. It was a paginated list of people, and each person was linked to a page with more details about them. Naturally, the HTML code was also a horrible mess.

The usual first step in such is to try if the XML functionality in your language of choice can make something workable out from the code. Since that will only work with well-formed XHTML, it was not an option here. The next thing is regular expressions, but they are such a huge pain to write and maintain for something like parsing specific data out from HTML.

Luckily, there’s a better way to do it in Python, using a library called BeautifulSoup. It’s definitely the best tool for this job I’ve seen.

Read More

Thoughts on unit testing and application design

Tags:

Here are some thoughts and observations regarding application design and unit testing in the quiz-project I recently wrote about.

When I was writing the Amazing Programming Language Guessing Script, I didn’t first write any unit tests. Partially because I just wanted to test something quickly, partially because I didn’t think there was any point in doing it.

When I later wanted to refactor the code to a bit better shape, I experienced the need for unit tests first hand: I refactored the code and totally broke it.

Read More

Programming language guessing game

Tags:

Some of you asked me about the “quiz” thing that showed up in one of the screenshots in my NetBeans review… Well, here it is:

During that weekend when I was trying out NetBeans, I wrote a small Zend Framework + Doctrine application which attempts to guess a programming language the user is thinking of, based on a set of questions the user needs to answer.

I have now set up the game, so go ahead and try the Awesome Programming Language Guessing Script. Yeah I wanted to give it a fun name =)

Let me know what you think, and if it’s actually getting any answers right or not – it’s using some relatively simple logic based on which questions have been answered what for which language etc., so I don’t know how it’ll perform when more than a few people answer the stuff. So if you’re a statistical math guru, feel free to suggest an algorithm for the game ;)

I might release the source code for the game sometime, as a demonstration of using ZF, Doctrine and unit testing and such.

Edit: the source code is now available here. Checkout/export the latest tag for the latest functioning code. Note that the code is still in development =)

Food for thought: utilizing models in MVC

Tags:

“What is a model” and “Is Zend_Db_Table a model” seem to be asked once in a while on #zftalk. Frameworks say they have a full model available, thus they are MVC frameworks

ORM libraries say they generate models. It seems the ActiveRecord pattern has become somewhat synonymous with model.

Pádraic Brady wrote an excellent post on how models are misunderstood, and how this poor understanding causes “fat stupid ugly controllers”, as he puts it.

Here are some thoughts on actually putting Pádraic’s advice into practice, and some examples in context of the Zend Framework.

Read More

NetBeans 6.5 review

Tags:

During the weekend, I tried out NetBeans 6.5 and its new PHP related functionality. I had earlier seen some quick shots of how the support was, and it seemed like a good contender for big names like Zend Studio.

What features does NetBeans 6.5 have for PHP developers? How does it compare against Zend Studio for Eclipse?

Read More

Generating Sudoku puzzles using JavaScript

Tags:

While I’m not the biggest fan of Sudoku puzzles, I wanted to make a sudoku widget for the Opera x-widgets challenge. This required me to study the algorithms used for generating sudokus, which was actually a quite interesting challenge.

While there are some examples of generating sudokus online, such as this interesting approach, it seems there’s only one “true” way: Using a backtracking algorithm.

There were also some examples of backtracking, but I found them confusing and not easily usable as-is, so I decided to write my own generator script, that anyone could just take and use in their own projects.

Read More