Showing posts with label Development. Show all posts
Showing posts with label Development. Show all posts

Thursday, August 28, 2008

Compare two XML strings in Python

I had to compare two XML strings for some unit tests, and if you want to do it without considering the indentation, or the newlines, it is a little bit tricky.

I thought that parsing the original xml and returning it again (using minidom), I'd got a raw string without any meaningless space, or any newline, but actually it returned the original string. Using toprettyxml() method also returns a trivial result, based on the original string (even when you specify the indent and the newline characters).

So the best way I've found by now is to write a custom function that returns what I want, an XML string without any trivial character between tag and tag. Here you have the code:

def raw_xml(xml_str):
    from xml.dom import minidom
    xml = minidom.parseString(xml_str)
    return u''.join([unicode(line).strip() for line in xml.toprettyxml().splitlines()])

Thursday, June 7, 2007

File encodings

Many times, specially when writing web pages, you have to deal with texts with different encodes (if web site isn't in us-ascii, of course). Here are some useful tips for not spending too much time with this:

bash# file -i <filename> (for knowing the encoding for a specific file)

vim# :set fileencoding=utf-8 (for changing encoding of current file, converting characters from one charset to the new one in a human understandable way)

vim# :set encoding=utf-8 (for setting encode of new files; default is system locale)

Remember that those thinks just apply to plain files, that are saved without any header or any information about encoding (editors try to figure out encodes).

A little bit offtopic, but an interesting vim command is also:

vim# :set ff=unix (for changing eol from dos/mac to unix)

UPDATE: If you have many files to convert, or don't know how to use vim... :) you can use GNU iconv program.

Wednesday, April 25, 2007

Firefox Extensions

When developing web pages is essential having a good set of firefox extensions. It gives you powerful tools for checking accessibility, and for getting more information on your website, and are also good fot getting information about all websites...

Those are my favourites:
Html Validator: Quick view if you are following standards.
Live PageRank: Quick view on site's PageRank.
Web Developer: Many tools, specially for disabling css or javascript, and for resizing screen.

Saturday, April 7, 2007

bug in IE

It's known that Microsoft Internet Explorer usually shows pages in a different way than good web programmers expect. Luckily those differences diminish when using XHTML Strict and CSS, but sometimes we are surprised for some huge bugs.

This is the case when trying to create an empty layer, something like that:
our_style {
height: 1px;
width: 100px;
background-color: black;
}
[...]
<div class="our_style"></div>

Obviously we expect something like a horizontal line, but IE increases our layer height to 10px 18px or whatever...

This time there is a easy solution for solving the bug, that is just adding a zero font size for the layer:

our_style {
font-size: 0px
height: 1px;
width: 100px;
background-color: black;
}

Wednesday, March 7, 2007

Web 2.0

Those days everybody is using Web 2.0 and AJAX, but everyone understand a different meaning of those technologies. Here I'll explain my interpretation.

In short I'll use this formula:
Web 2.0 = XHTML Strict + CSS2 + AJAX

It means the end of using html tables for positioning objects in the page (HTML 4.0 or XHTML Transactional). Web 2.0 uses CSS2 properties to do it. In this formula AJAX is the XMLHttpRequest JavaScript object, that allows to get HTTP data using JavaScript code (and prevents reloading the whole page).

A good reference for more of those technologies is W3Schools. In addition there is a very good presentation for best practices (and accessibility) called Unobstructive AJAX.