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.
// all return "1bc"
$.format('{a}bc', {a:'1'}) // named arguments
$.format('{0}bc', [1]) // array arguments
$.format('{0}bc', 1) // normal arguments
$.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
$.format('{a:08.2f}bc', {a:'1'}) // return 00001.00bc
$.extend(jQuery.strConversion,
{'U': function(input, arg){ return input.toUpperCase(); }
});
$.format('{0:U}bc', 'a') // return Abc
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 ?
This entry is in the process of being rewritten, thanks for your patience and sorry for the inconvenience.