Using Python new style string formatting in Sage

558 views
Skip to first unread message

Zoresvit

unread,
Mar 7, 2012, 6:50:36 AM3/7/12
to 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

unread,
Mar 7, 2012, 7:02:17 AM3/7/12
to 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

unread,
Mar 24, 2012, 8:53:31 PM3/24/12
to 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

unread,
Mar 25, 2012, 2:01:15 AM3/25/12
to 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

unread,
Mar 28, 2012, 3:40:44 PM3/28/12
to 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

unread,
Mar 30, 2012, 2:12:45 PM3/30/12
to sage-s...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages