translate
Notice This is a beta feature offered by Google. Also this is automatic translation, which means the results are often inacurate and/or hilarious. Enjoy.

ARCHIVES / MUSIC / RSS
Blog

jQuery: Advanced String Formatting à la Python3K

h3  ~  17 May 2008, 17:00  –  2 comments

This week in my spare time I implemented the Python3000 Advanced String Formatting described here.

It was more easy than I apprehended, less than 200 lines (without comments) and I managed to use a minimal amount of regular expressions so it's quite fast. Still, I did this as a hobby and it's the first release so don't expect it to be perfect, I don't recommend to use it in serious projects yet. That said, comments, feedback and patches are more than welcome.

You can submit bugs to the issues tracker on the project page, please specify the plugin (jquery-string) in labels.

The API documentation is here.

Usage examples

Simple replacement

// all return "1bc"
$.format('{a}bc', {a:'1'}) // named arguments
$.format('{0}bc', [1])     // array arguments
$.format('{0}bc', 1)       // normal arguments

Type conversion

$.format('{a:d}bc', {a:'a1'})  // return "1bc"

$.format('{a:d}bc', {a:1.5})   // return "1bc"

$.format('{a:.2f}bc', {a:'1'}) // returns 1.00bc

Padding

$.format('{a:08.2f}bc', {a:'1'}) // return 00001.00bc

User defined formatting

$.extend(jQuery.strConversion, 
    {'U': function(input, arg){ return input.toUpperCase(); }
});

$.format('{0:U}bc', 'a') // return Abc

Known differences with Python

  • JavaScript precision is more limited than Python
  • Python zero pad exponent (10 -> 1.0e+01), not JavaScript? (10 -> 1.0e+1)
  • My repr and str implementation is not like Python

To come

  • -|+|s flags handling
  • proper escaping
  • repr should truncate using precision
  • jQuery.fn extension, ex: $('input').format('0.2f')
  • sprintf method using the same conversion object, because sprintf is also useful

Batch unit test URLs in Django

h3  ~  15 Apr 2008, 21:59  –  Post a comment

While working on a personal project I got tired of writing tests for each of my URLs like this

from django.test import TestCase
from django.test.client import Client

class TestViews(TestCase):
    fixtures = ['initial_data']
    def setUp(self):
        self.c = Client()

    def test_home(self):
        rs = self.c.get('/home/')
        self.failUnless(rs)
        self.assertEquals(rs.status_code, 200)

    def test_submit(self):
        
        rs = self.c.post('/login/',
            {'username': 'sponge', 'password': 'bob'})
        self.assertEquals(rs.status_code, 302)
        self.failUnless(rs)

	rs = self.c.get('/submit/')
        self.failUnless(rs)
        self.assertEquals(rs.status_code, 200)

    # repeat for each URLs of the site/application

It's fine, but it could be more convenient.. so I came up with this with a little refactoring:

from django.test import TestCase
from django.test.client import Client

class TestViews(TestCase):
    fixtures = ['initial_data']
    def setUp(self):
        self.c = Client()

    def urltest(self, url, status_code):
        rs = self.c.get(url)
        self.failUnless(rs)
        self.assertEquals(rs.status_code, status_code, 
            '%s returned %s, %s was expected' % 
            (url, rs.status_code, status_code))
            

    def test_anonymous_urls(self):
        urls = [('/',        301), # permanently moved
                ('/about/',  200),
                ('/home/',   200),
                ('/submit/', 302)] #redirected
                # so on..

        for url in urls:
            self.urltest(url[0], url[1])

    def test_user_urls(self):
        rs = self.c.post('/login/', 
            {'username': 'sponge', 'password': 'bob'})
        self.failUnless(rs)
	# still have to test this one individually..
        self.assertEquals(rs.status_code, 302)


        urls = [('/',        301), # still moved..
                ('/about/',  200),
                ('/home/',   200),
                ('/submit/', 200)] # OK
                # so on..

        for url in urls:
            self.urltest(url[0], url[1])

I can probably extend this idea, but I haven't seen the need yet. Any ideas ?

Why I hate Flash.

h3  ~  15 Apr 2008, 12:50  –  31 comments

This entry is in the process of being rewritten, thanks for your patience and sorry for the inconvenience.

Copyrighted stuff .. u know.