[GRASS-dev] [Issue] String formatting issue

Hi everyone,

I came across this type error while porting libs.

msg = _("Module run %s %s ended with error") % (module, code)
TypeError: %b requires a bytes-like object, or an object that
implements __bytes__, not 'NoneType'

This uses % format specifier which sometimes fails on Python 3 because
of strings and bytes differences.

I can resolve this by using fomat() like this:
msg = _(("Module run {} {} ended with error").format(module,code))

Do you think this is a good way to resolve this?
Is "format()" going to work well with translations using the
''gettext()'' used as _() as shown in the above line?

Thanks,
Sanjeet

Hi Sanjeet,
try like this:

msg = _("Bla bla {0} with {1}").format(zero, one)

Māris.

2018-06-04 3:08 GMT+03:00 Sanjeet <edu.sanjeet@gmail.com>:

Hi everyone,

I came across this type error while porting libs.

msg = _("Module run %s %s ended with error") % (module, code)
TypeError: %b requires a bytes-like object, or an object that
implements __bytes__, not 'NoneType'

This uses % format specifier which sometimes fails on Python 3 because
of strings and bytes differences.

I can resolve this by using fomat() like this:
msg = _(("Module run {} {} ended with error").format(module,code))

Do you think this is a good way to resolve this?
Is "format()" going to work well with translations using the
''gettext()'' used as _() as shown in the above line?

Thanks,
Sanjeet
_______________________________________________
grass-dev mailing list
grass-dev@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-dev

* Sanjeet <edu.sanjeet@gmail.com> [2018-06-03 18:08:29 -0600]:

Hi everyone,

I came across this type error while porting libs.

msg = _("Module run %s %s ended with error") % (module, code)
TypeError: %b requires a bytes-like object, or an object that
implements __bytes__, not 'NoneType'

This uses % format specifier which sometimes fails on Python 3 because
of strings and bytes differences.

I can resolve this by using fomat() like this:
msg = _(("Module run {} {} ended with error").format(module,code))

Do you think this is a good way to resolve this?
Is "format()" going to work well with translations using the
''gettext()'' used as _() as shown in the above line?

Thanks,
Sanjeet

Normally everyone would want to follow the "new" style of formatting
strings, which offers many more options. Why stick to the old style,
i.e. using the % notation?

Cheers, Nikos

On Mon, Jun 4, 2018 at 1:58 AM, Maris Nartiss <maris.gis@gmail.com> wrote:

Hi Sanjeet,
try like this:

msg = _("Bla bla {0} with {1}").format(zero, one)

Hi Maris,

I tried and it worked too.

Thanks,
Sanjeet

On Mon, Jun 4, 2018 at 3:54 AM, Nikos Alexandris
<nik@nikosalexandris.net> wrote:

* Sanjeet <edu.sanjeet@gmail.com> [2018-06-03 18:08:29 -0600]:

I can resolve this by using fomat() like this:
msg = _(("Module run {} {} ended with error").format(module,code))

Do you think this is a good way to resolve this?
Is "format()" going to work well with translations using the
''gettext()'' used as _() as shown in the above line?

Normally everyone would want to follow the "new" style of formatting
strings, which offers many more options. Why stick to the old style,
i.e. using the % notation?

Hi Nikos,

Yes, the .format() style should be followed in most cases. I could be
wrong but I guess the old style has been used to support older
versions of Python. But it seems we can use the 'format' style since
we are only keeping support for Python 2.7 (or 2.6 if possible).

Sanjeet

Hi,

2018-06-04 19:52 GMT+02:00 Sanjeet <edu.sanjeet@gmail.com>:

Yes, the .format() style should be followed in most cases. I could be
wrong but I guess the old style has been used to support older
versions of Python. But it seems we can use the 'format' style since
we are only keeping support for Python 2.7 (or 2.6 if possible).

yes, please use format(). And the old style (%) should be replaced by format().

Martin

--
Martin Landa
http://geo.fsv.cvut.cz/gwiki/Landa
http://gismentors.cz/mentors/landa