I had the same 'problem' as Daniel Kluev reported on 2010-06-28 on the
mailing list, namely that I switched to using the .format string method
some time ago instead of using the % operator. ( I considered having the
'%' key on my keyboard emit an electric shock, as I kept falling back to
using the % operator for 2-3 weeks).
I did not see any code that this had been implemented in the since then,
and also 'format' is still in BUILTINS.TODO list.
However pypy is now at 2.7.1 (was at 2.5 at the time of the email
exchange, and did not have .format). So I pulled newformat.py and
test_newformat.py from there, simplified the first into a file
stringformat.py so that it no longer has external dependencies and the
second so it uses unittest and tested the whole thing under CPython 2.7.
Then I dug into the pyjs code and found pyjslib.py and added a format
operator on string:
String.prototype.format = function() {
var args =
$p['tuple']($pyjs_array_slice.call(arguments,0,arguments.length-1));
var kw = arguments.length >= 1 ? arguments[arguments.length-1] :
arguments[arguments.length];
if (typeof kw != 'object' || kw.__name__ != 'dict' || typeof
kw.$pyjs_is_kwarg == 'undefined') {
if (typeof kw != 'undefined') args.__array.push(kw);
kw = arguments[arguments.length+1];
} else {
delete kw['$pyjs_is_kwarg'];
}
if (typeof kw == 'undefined') {
kw = $p['__empty_dict']();
}
return $p['string_format'](this, args, kw);
}
String.prototype.format.__args__ = ['args', ['kw']];
I copied this code from pyjs generated code for a python function that
has the same signature as .format.
this calls string_format wich is imported from stringformat.py
string_format takes 3 parameters: a formatstring, a list of args, and a
dictionary, so it has not to the *args and **kw handling of .format again.
I finally copied the test_newformat.py code into a test routine and
lacking unittesting I called the various functions in the classes from
an added __init__.
Most test now work (more than 95%). One of the things I have to look
into has probably to do with the newformat.py code relying on
multiplication of strings to make the fill of a field specified with a
specific width.
Even without those last few tests I think this is useful. So my
questions are:
- if anyone is interested please let me know how to best provide you
with the stuff ( check in to git?, diff? )
- is the above insertion of .format in the pyjslib.py the correct way to
do this?
- where should the stringformat.py file best go (it is currently next to
pyjslib.py)
- any pointers on how I should do the testing of stringformat.py in a
pyjamas like way?
- should the CPython 2.7 unittest(2( code be checked in somewhere, or do
I keep that for myself?
ToDo:
- fix the code for the failing test cases
- further simplification of the code
Anthon