PyScript support better string formatting
almarklein opened this issue · 1 comments
@almarklein commented on Wed Dec 07 2016
Currently, this fails in PyScipt because the transpiler implements the formatting, but is not smart enough to know what t
is:
t = 'some %s thing'
print(t % 'cool')
I think we should add a format
method to the std lib, so that we can make t.format(...)
work, and possibly transform c-style formatting to a format
call as well. And perhaps we can also support f"some {thing_aspect} thing"
(new Py 3.6 syntax) while we're at it.
@almarklein commented on Wed Dec 07 2016
Transcript supports .format()
, so that'd be a good place to start.
@jrversteegh commented on Tue Feb 07 2017
Also '%f' % 3.00
produces different output in python and JS.
How about https://github.com/alexei/sprintf.js?
@almarklein commented on Wed Feb 08 2017
@jrversteegh I was about to respond that I don't like including 3d party libs to avoid bloating, but that lib is actually pretty small in minified form ...
That said, maybe we should all be using Python 3.6 f'the number is {a}'
:)
@jrversteegh commented on Wed Feb 08 2017
Really, a third way to format strings? When I first met Python, I was soooo happy to leave Tim Toady behind ;)
I am a little confused what flexx's main target is, but the readme first mentions "to create desktop applications". For that I would accept a little bloat if it means free functionality. If you want a lean web framework, my guess is that flexx wouldn't be the way to go anyway, but IANAWD
@almarklein commented on Wed Feb 08 2017
Really, a third way to format strings?
Haha, I agree, does not seem very Pythonic. The str.format()
does not seem to have really taken off (I rarely use it myself, anyway). The new 3.6 style seems to provide an approach that is so much nicer than the %-style formatting that I hope it gets adopted way better.
What I mean with my comment is that the current support for %f
and friends may be good enough, especially if people will start using 3.6-style formatting more.
I do agree that its an illusion to think that Flexx is "lean", and including something like sprintg.js
wont cause significant bloat by itself, allowing inclusion of 3d party code in multiple places will certainly make Flexx heavier. It's something I want to keep an eye on, which is why I am reluctant to include any 3d party code unless there are very good reasons.
@jrversteegh commented on Thu Feb 09 2017
What I mean with my comment is that the current support for %f and friends may be good enough,
Also In JS? Not being able to provide a precision specifier makes it pretty much useless imho, so one would have to do all number formatting in python on the server side.
@Winand commented on Thu Jul 13 2017
Maybe it can be determined at runtime if a variable is a string or not?
I don't think 3.6 formatting can replace %
and str.format
, 'cause you need to set all of the variables beforehand and then declare your f'string'. Usually i create a variable with a string "template" and then format it as needed (as shown in the first post here)
@Winand commented on Sat Jul 01 2017
Can be used in simple cases. Supports both {}
and {#}
(though can produce unexpected results if both styles are used in one string)
def str_format():
s = this
for i in range(arguments.length):
s = s.replace("{}", arguments[i], 1)
s = s.replace("{%d}" % i, arguments[i])
return s
String.prototype.format = str_format
@almarklein commented on Mon Jul 03 2017
Thanks!
I don't think 3.6 formatting can replace % and str.format, 'cause you need to set all of the variables beforehand and than declare your f'string'.
I think you're right, relying on f-string alone is probably too limiting.
@jrversteegh @Winand Implemented in #17
Note, that we cannot do this, because PScript is not smart enough to know whether t
is str
or float
:
t = 'some %s thing'
print(t % 'cool')
But we can do this:
t = 'some {} thing'
print(t.format('cool'))