How can I print an informational message, using g.message, that contains unicode stuff via python (2.x)?
For example, how do I correctly translate this command
g.message message="ρ(p) = π x L(λ) x d^2 / ESUN(λ) x cos(θ(S))"
(it contains the greek r (ρ, pronounced rho) and pi (π, pronounced pe)
to work in a python script?
Thank you, Nikos
ps- I am trying to understand unicode stuff by reading this <http://nedbatchelder.com/text/unipain.html> but I am still not able to get past this "simple" problem.
Nikos Alexandris wrote:
How can I print an informational message, using g.message, that
contains unicode stuff via python (2.x)?
For example, how do I correctly translate this command
g.message message="ρ(p) = π x L(λ) x d^2 / ESUN(λ) x cos(θ(S))"
(it contains the greek r (ρ, pronounced rho) and pi (π, pronounced pe)
to work in a python script?
In general, you can't assume that the terminal is capable of
displaying those characters.
If they are representable in the current locale, then
encoding = locale.getdefaultlocale()[1]
grass.message(msg.encode(encoding))
should pass the correct bytes.
If they aren't representable, then the .encode() method will raise a
UnicodeEncodeError.
If a UTF-8 locale is being used, any Unicode string can be converted,
but that doesn't mean that the characters will display correctly; if
the font lacks those characters, they'll just show as "?" or "~" or a
box with the hex code or similar.
--
Glynn Clements <glynn@gclements.plus.com>
On 21.11.2014 20:17, Glynn Clements wrote:
Nikos Alexandris wrote:
How can I print an informational message, using g.message, that
contains unicode stuff via python (2.x)?
For example, how do I correctly translate this command
g.message message="ρ(p) = π x L(λ) x d^2 / ESUN(λ) x cos(θ(S))"
(it contains the greek r (ρ, pronounced rho) and pi (π, pronounced pe)
to work in a python script?
In general, you can't assume that the terminal is capable of
displaying those characters.
True. Working on Funtoo here, which has utf-8 pre-set.
If they are representable in the current locale, then
encoding = locale.getdefaultlocale()[1]
grass.message(msg.encode(encoding))
should pass the correct bytes.
I will try this to understand how it works.
If they aren't representable, then the .encode() method will raise a
UnicodeEncodeError.
If a UTF-8 locale is being used, any Unicode string can be converted,
but that doesn't mean that the characters will display correctly; if
the font lacks those characters, they'll just show as "?" or "~" or a
box with the hex code or similar.
So, it's bad idea even trying to do this (and thinking that everyone is going to read it)? Best practice to avoid such situations alltogether and respect the cross-platform principle?
Thank you, Nikos
On Fri, Nov 21, 2014 at 1:46 PM, Nikos Alexandris <nik@nikosalexandris.net>
wrote:
So, it's bad idea even trying to do this (and thinking that everyone is
going to read it)? Best practice to avoid such situations alltogether and
respect the cross-platform principle?
It seems safer to me to print unicode characters using Python only and not
use g.message because for g.message you have to pass the characters to
another command. However, GRASS module message should go through g.message.
You should at least use stderr rather then stdout because stdout is in
GRASS reserved for the actual output of the modules.
Vaclav
Vaclav Petras wrote:
> So, it's bad idea even trying to do this (and thinking that everyone is
> going to read it)? Best practice to avoid such situations alltogether and
> respect the cross-platform principle?
Ideally, yes.
Or you can use localised strings with _("..."), using only ASCII in
the string literal but using locale-specific characters in the message
catalogue.
It seems safer to me to print unicode characters using Python only and not
use g.message because for g.message you have to pass the characters to
another command.
It doesn't matter. g.message will just pass the bytes to G_message()
which will (more or less) just send them to stderr.
So long as the string doesn't contain null bytes and the space, tab,
and newline characters have the same meansing as in ASCII, there isn't
any difference between sending the bytes directly to stderr or sending
them via g.message.
--
Glynn Clements <glynn@gclements.plus.com>