[Python] problema con format string, convertire da nuova a vecchia

2012-07-06 Per discussione Pietro
Ciao a tutti!

devo convertire questa format string:

>>> "{0} - {1} {2:.> 40.6f}s".format('RasterNumpy', 'add', 0.008465832344)
'RasterNumpy - add ... 0.008466s'

in modo da utilizzare al posto della nuova notazione la vecchia, se
provo ad utilizzare:

>>> "%s - %s %.> 40.6f s" % ('RasterNumpy', 'add', 0.008465832344)
---
ValueErrorTraceback (most recent call last)
 in ()
> 1 "%s - %s %.> 40.6f s" % ('RasterNumpy', 'add', 0.008465832344)

ValueError: unsupported format character '>' (0x3e) at index 10

Qualcuno sa come fare? Ho cercato ma nella documentazione sono
riportati solo esempi utilizzando il nuovo standard...

Grazie per l'aiuto.

Pietro
___
Python mailing list
Python@lists.python.it
http://lists.python.it/mailman/listinfo/python


Re: [Python] problema con format string, convertire da nuova a vecchia

2012-07-06 Per discussione Marco De Paoli
Il giorno 06 luglio 2012 17:16, Pietro  ha scritto:

> Qualcuno sa come fare? Ho cercato ma nella documentazione sono
> riportati solo esempi utilizzando il nuovo standard...
>

http://docs.python.org/release/2.5/lib/typesseq-strings.html


ciao,
Marco
___
Python mailing list
Python@lists.python.it
http://lists.python.it/mailman/listinfo/python


Re: [Python] problema con format string, convertire da nuova a vecchia

2012-07-06 Per discussione Pietro
Grazie Marco,

modificando in questo modo funziona:

>>> "%s - %s % 40.6f s" % ('RasterNumpy', 'add', 0.008465832344)
'RasterNumpy - add 0.008466 s'

Non sono riuscito a mettere i punti al posto delle spazio... ma non è
un grosso problema!

Grazie ancora.

Pietro
___
Python mailing list
Python@lists.python.it
http://lists.python.it/mailman/listinfo/python


Re: [Python] problema con format string, convertire da nuova a vecchia

2012-07-06 Per discussione Alberto Granzotto
2012/7/6 Pietro :
> Grazie Marco,
>
> modificando in questo modo funziona:
>
 "%s - %s % 40.6f s" % ('RasterNumpy', 'add', 0.008465832344)
> 'RasterNumpy - add 0.008466 s'
>
> Non sono riuscito a mettere i punti al posto delle spazio... ma non è
> un grosso problema!

è poco leggibile ma magari ti aiuta:

>>> "%s - %s %s s" % ('RasterNumpy', 'add', ('%.6f' % 0.008465832344).rjust(40, 
>>> '.'))
'RasterNumpy - add 0.008466 s'

magari formattando il float con i puntini in una riga a parte ne
guadagni in leggibilità :)

ciao,
alberto

> Grazie ancora.
>
> Pietro
> ___
> Python mailing list
> Python@lists.python.it
> http://lists.python.it/mailman/listinfo/python
___
Python mailing list
Python@lists.python.it
http://lists.python.it/mailman/listinfo/python


Re: [Python] problema con format string, convertire da nuova a vecchia

2012-07-06 Per discussione Pietro
Grazie Alberto,

2012/7/6 Alberto Granzotto :
> è poco leggibile ma magari ti aiuta:
>
 "%s - %s %s s" % ('RasterNumpy', 'add', ('%.6f' % 
 0.008465832344).rjust(40, '.'))
> 'RasterNumpy - add 0.008466 s'

purtroppo la stringa è da inserire in un template di jinja

from jinja2 import Template

TXT = u"""
{% for region in regions %}
{{ '#'*60 }}
### Benchmark cols = {{ region.cols }} rows = {{ region.rows}} cells =
{{ region.cells }}
{{ '#'*60 }}

# equation: c = a + b
{% for execmode, operation in region.results.iteritems() %}
{{ "%-30s - %5s % 12.6fs"|format(execmode, 'add', operation.add.time) }}
{%- endfor %}

# equation: c = if a > 50 then 1 else 0
{% for execmode, operation in region.results.iteritems() %}
{{ "%-30s - %5s % 12.6fs"|format(execmode, 'if', operation.if.time) }}
{%- endfor %}
{%- endfor %}
"""

def get_txt(results):
txt = Template(TXT)
return txt.render(regions = results)


Non credo di poter quindi utilizzare il tuo metodo, potrei al limite
crarmi la stringa prima, ed inserirla direttamente nel dizionario che
gli passo...

Ma alla fine i puntini non sono così importanti... :-)

Grazie a tutti per l'aiuto.
Buon fine settimana!

Pietro
___
Python mailing list
Python@lists.python.it
http://lists.python.it/mailman/listinfo/python