Using Python new style string formatting in Sage

Visto 559 veces
Saltar al primer mensaje no leído

Zoresvit

no leída,
7 mar 2012, 6:50:367/3/12
a sage-s...@googlegroups.com
Is there a way to use new Python string formatting in Sage without disabling the preparser?

For now I get the following:

sage: "int: {0:d};  hex: {0:#x};  oct: {0:#o};  bin: {0:#b}".format(42)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/home/zoresvit/devel/ciphering/algebraic/<ipython console> in <module>()
ValueError: Unknown format code 'd' for object of type 'str'

If the preparser is off, everything works:

sage: preparser(False)                                                 
sage: "int: {0:d};  hex: {0:#x};  oct: {0:#o};  bin: {0:#b}".format(42)
'int: 42;  hex: 0x2a;  oct: 0o52;  bin: 0b101010'

I'm using Sage 4.8:

$ sage --version
Sage Version 4.8, Release Date: 2012-01-20
$ sage -python
Python 2.6.4 (r264, Jan 20 2012, 00:16:31) 
[GCC 4.4.3] on linux2

Thanks.

Jason Grout

no leída,
7 mar 2012, 7:02:177/3/12
a sage-s...@googlegroups.com
On 3/7/12 5:50 AM, Zoresvit wrote:
> Is there a way to use new Python string formatting
> <http://docs.python.org/library/string.html> in Sage without disabling

> the preparser?
>
> For now I get the following:
>
> sage: "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42)
> ---------------------------------------------------------------------------
> ValueError Traceback (most recent call last)
> /home/zoresvit/devel/ciphering/algebraic/<ipython console> in <module>()
> ValueError: Unknown format code 'd' for object of type 'str'
>
>
> If the preparser is off, everything works:
>
> sage: preparser(False)
> sage: "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(42)
> 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'
>

The problem is that apparently format doesn't accept Integers:

sage: "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(int(42))


'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'

sage: "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin:

{0:#b}".format(Integer(42))


---------------------------------------------------------------------------
ValueError Traceback (most recent call last)

/Users/grout/jason/drake/linear-algebra/Exams/<ipython console> in
<module>()

ValueError: Unknown format code 'd' for object of type 'str'

Thanks,

Jason


Zoresvit

no leída,
24 mar 2012, 20:53:3124/3/12
a sage-s...@googlegroups.com
Thanks a lot! Always forgetting Sage has its own type for integers. Some automatic coersion would help a lot in such situation.

Dima Pasechnik

no leída,
25 mar 2012, 2:01:1525/3/12
a sage-s...@googlegroups.com
On 2012-03-25, Zoresvit <zore...@gmail.com> wrote:
> ------=_Part_2975_1350727.1332636811930
> Content-Type: text/plain; charset=UTF-8
> Content-Transfer-Encoding: quoted-printable
>
> Thanks a lot! Always forgetting Sage has its own type for integers. Some=20

> automatic coersion would help a lot in such situation.

Given that not every Integer fits into an int, this would be asking for
trouble. Rather, Sage should specify its own format for Integer, etc.


Nils Bruin

no leída,
28 mar 2012, 15:40:4428/3/12
a sage-s...@googlegroups.com
 
On Saturday, March 24, 2012 11:01:15 PM UTC-7, Dima Pasechnik wrote:

Given that not every Integer fits into an int, this would be asking for
trouble. Rather, Sage should specify its own format for Integer, etc.

true, but Python handles the overflow gracefully by returning a long instead, and in Python 3 there is no int/long dichotomy on the language level any more. It's probably convenient to put a __format__ method on Integer, but its implementation could be very simple:

def __format__(self,*args,**kwargs):
    return int(self).__format__(*args,**kwargs)

If this leads to performance issues, one could consider writing a custom version that accepts the python format mini-language.

Nils Bruin

no leída,
30 mar 2012, 14:12:4530/3/12
a sage-s...@googlegroups.com
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos