Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > python3 - the hardest hello world ever ?

Reply
Thread Tools

python3 - the hardest hello world ever ?

 
 
Helmut Jarausch
Guest
Posts: n/a
 
      10-15-2008
Paul Boddie wrote:
> On 15 Okt, 12:08, Helmut Jarausch <(E-Mail Removed)-aachen.de>
> wrote:
>> What defines me as latin1-user?

>
> What does sys.stdout.encoding say? In Python 2.x, at least, that


It says ansi_x3.4-1968

Where can I change this?
> attribute should reflect the capabilities of your environment
> (specifically, the character encoding) and help determine whether it
> makes sense for Python to try and encode Unicode objects (plain
> strings in Python 3.x) using a particular output encoding when
> printing those objects to the display.
>


Thanks,
Helmut.


--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
Reply With Quote
 
 
 
 
Paul Boddie
Guest
Posts: n/a
 
      10-15-2008
On 15 Okt, 17:59, Helmut Jarausch <(E-Mail Removed)> wrote:
> Paul Boddie wrote:
> > What does sys.stdout.encoding say? In Python 2.x, at least, that

>
> It says *ansi_x3.4-1968


That's ASCII, yes.

> Where can I change this?


What's your locale? I can provoke the same setting if I run a Python
program like this:

LC_ALL=en_US.ascii python xxx.py

Are you running some kind of GNU/Linux distribution or something else?
If the former, have you installed various language/locale packages? If
you're not sure, which language or country did you select when
installing or configuring your system? This may seem like an odd line
of questioning, but UNIX-like systems have a history of treating
everything as bytes, which works acceptably until you have to take a
stand on what those bytes mean.

Another important question: what does Python 2.x do with the following
program...?

import sys
print sys.stdout.encoding
print u"\xe6\xf8\xe5"

You should get three Scandinavian characters if the encoding and
locales match. Otherwise, you'll either get a different output
(indicating a mismatch) or an error (indicating that the environment
cannot handle the characters output by the program). Sometimes you can
persuade a terminal to use a different character set, and this might
help, too.

Paul
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      10-15-2008
Helmut Jarausch wrote:

> Paul Boddie wrote:
>> On 15 Okt, 12:08, Helmut Jarausch <(E-Mail Removed)-aachen.de>
>> wrote:
>>> What defines me as latin1-user?

>>
>> What does sys.stdout.encoding say? In Python 2.x, at least, that

>
> It says ansi_x3.4-1968
>
> Where can I change this?


By changing your console's terminal settings. See what

locale -a

outputs.

See this:


(devtools)dir@client8049:~$ locale -a
C
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZW.utf8
POSIX
(devtools)dir@client8049:~$ python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Welcome to rlcompleter2 0.96
for nice experiences hit <tab> multiple times
>>> import sys
>>> sys.stdout.encoding

'UTF-8'
>>>



Diez
 
Reply With Quote
 
Martin v. L÷wis
Guest
Posts: n/a
 
      10-15-2008
> What defines me as latin1-user?

That your locale is based on Latin-1, e.g. because it is a German
locale. How precisely that works depends on the operating system.

> So my system seems to be an ASCII system?


At least that's what Python determined. If Python couldn't have found
out that you usually use Latin-1, your system is misconfigured. If
Python could have found out, but failed to do so, it's a bug in Python.

Regards,
Martin
 
Reply With Quote
 
Helmut Jarausch
Guest
Posts: n/a
 
      10-16-2008
Martin v. L÷wis wrote:
>> What defines me as latin1-user?

>
> That your locale is based on Latin-1, e.g. because it is a German
> locale. How precisely that works depends on the operating system.
>
>> So my system seems to be an ASCII system?

>
> At least that's what Python determined. If Python couldn't have found
> out that you usually use Latin-1, your system is misconfigured. If
> Python could have found out, but failed to do so, it's a bug in Python.
>


Many thanks, it works when setting the LANG environment variable.

Still, I wished it were possible call sys.setdefaultencoding
at the very beginning of a script.

Why isn't that possible?

Helmut.


--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
Reply With Quote
 
Martin v. L÷wis
Guest
Posts: n/a
 
      10-16-2008
> Still, I wished it were possible call sys.setdefaultencoding
> at the very beginning of a script.
>
> Why isn't that possible?


The default encoding was used when combining byte-oriented
text and unicode-oriented text. Such combination is no longer
supported, hence the notion of a default encoding
has disappeared. You have to perform conversion between bytes
and strings now explicitly.

Regards,
Martin
 
Reply With Quote
 
Helmut Jarausch
Guest
Posts: n/a
 
      10-16-2008
Martin v. L÷wis wrote:
>> Still, I wished it were possible call sys.setdefaultencoding
>> at the very beginning of a script.
>>
>> Why isn't that possible?

>
> The default encoding was used when combining byte-oriented
> text and unicode-oriented text. Such combination is no longer
> supported, hence the notion of a default encoding
> has disappeared. You have to perform conversion between bytes
> and strings now explicitly.
>


I meant setting the default encoding which is used by print (e.g.) when
outputting the internal unicode string to a file.
As far as I understood, currently I am fixed to setting either
the 'locale' or to switch settings for each output file (by settting
the _encoding property.
I wished I could override the locale settings within a Python script.

Thanks,
Helmut.


--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
Reply With Quote
 
Paul Boddie
Guest
Posts: n/a
 
      10-16-2008
On 16 Okt, 11:28, Helmut Jarausch <(E-Mail Removed)-aachen.de>
wrote:
>
> I meant setting the default encoding which is used by print (e.g.) when
> outputting the internal unicode string to a file.
> As far as I understood, currently I am fixed to setting either
> the 'locale' or to switch settings for each output file (by settting
> the _encoding property.
> I wished I could override the locale settings within a Python script.


You could use the locale module.

But seriously, I'd like to know whether the program I posted works
with Python 2.x because there could be differences between 2.x and
3.x, and we'd obviously like to solve your problems regardless of
which Python version you're using.

Paul
 
Reply With Quote
 
Helmut Jarausch
Guest
Posts: n/a
 
      10-16-2008
Paul Boddie wrote:
> On 16 Okt, 11:28, Helmut Jarausch <(E-Mail Removed)-aachen.de>
> wrote:
>> I meant setting the default encoding which is used by print (e.g.) when
>> outputting the internal unicode string to a file.
>> As far as I understood, currently I am fixed to setting either
>> the 'locale' or to switch settings for each output file (by settting
>> the _encoding property.
>> I wished I could override the locale settings within a Python script.

>
> You could use the locale module.
>
> But seriously, I'd like to know whether the program I posted works
> with Python 2.x because there could be differences between 2.x and
> 3.x, and we'd obviously like to solve your problems regardless of
> which Python version you're using.
>


Yes, of course.
I have always worked with latin-1 strings with an US locale under
python-2.x with x < 6 (I haven't tried 2.6, though). I hope to switch to 3.0
as soon as possible.


--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
Reply With Quote
 
Martin v. L÷wis
Guest
Posts: n/a
 
      10-16-2008
> I meant setting the default encoding which is used by print (e.g.) when
> outputting the internal unicode string to a file.


Having such a thing would be conceptually wrong. What encoding should
be used depends on the file - different files may have different
encodings. When opening a file, you need to specify the encoding.

> As far as I understood, currently I am fixed to setting either
> the 'locale' or to switch settings for each output file (by settting
> the _encoding property.


That's not true. You can also specify the encoding when opening the file

> I wished I could override the locale settings within a Python script.


You can monkey-patch locale.getpreferredencoding, which is used when
determining what encoding to use when opening new files. I don't
recommend doing so, though.

Regards,
Martin
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regular expression to parse {"hello", "hello world","1hello-2*hello"} Roy Java 6 01-07-2008 08:06 PM
to print in the reverse order, ("Hello World" -> "World Hello") vijay C Programming 8 04-26-2005 02:11 AM
Re: The worst 'hello world' example ever written... C C++ 424 10-09-2003 08:57 AM
Re: The worst 'hello world' example ever written... Greg Comeau C++ 1 09-29-2003 04:15 PM
Re: The worst 'hello world' example ever written... The Saqe C++ 2 09-24-2003 02:23 PM



Advertisments