Re: [Tutor] getattr help

2012-03-10 Thread Peter Otten
mjole...@gmail.com wrote:

> What is the purpose of getattr? Why not just use help or am I completely
> misunderstanding this?
> 
>>From what I read, getattr allows you to get a reference to a function
>>without knowing its name until runtime.
> 
> However, the example provided is:
> 
> li = ['larry', 'curly]
> getattr(li, 'pop')
> 
> It seems to me that I need to know the name of the function to use
> getattr?

The name need not be known *until* *runtime*. You can pass it as a variable 
which makes the program more flexible. Here is an example:

$ cat getattrdemo.py 
class Joe:
def run(self):
print("running")
def jump(self):
print("jumping")
def say_hello(self):
print("Hello, I am Joe")

class Sue:
def say_hello(elf):
print("Hello, I am Sue")
def swim(self):
print("swimming")

for person in [Joe(), Sue()]:
person.say_hello()
actions = [action for action in dir(person) if not 
action.startswith("_")]
print("I can", ", ".join(actions))
while True:
action = input("What do you want me to do? ")
if action == "":
print("bye")
break
if action in actions:
getattr(person, action)()
else:
print("I'm afraid I can't", action)

The code in the for loop does not "know" what joe or sue can do, it detects 
it at runtime. Therefore you don't have to change it when you add another 
person (as long as it can .say_hello()). 

And here's what you may see when you run the script:

$ python3 getattrdemo.py 
Hello, I am Joe
I can jump, run, say_hello
What do you want me to do? jump
jumping
What do you want me to do? run
running
What do you want me to do? swim
I'm afraid I can't swim
What do you want me to do? 
bye
Hello, I am Sue
I can say_hello, swim
What do you want me to do? say_hello
Hello, I am Sue
What do you want me to do? talk
I'm afraid I can't talk
What do you want me to do? swim
swimming
What do you want me to do? 
bye
$

If you want to see a more advanced example of the technique, have a look at 
cmd.py in Python's standard library.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python using RXVT vs Konsole?

2012-03-10 Thread brandon w
Up arrow does work in other applications. The only application that it does
not work in is konsole.
I actually do not care for konsole that much. I think I will just use some
other terminal.

I found this on the Internet:

The interpreter's line-editing features usually aren't very sophisticated.
On Unix, whoever installed the interpreter may have enabled support for the
GNU readline library, which adds more elaborate interactive editing and
history features. Perhaps the quickest check to see whether command line
editing is supported is typing *Control-P*, or the *up-arrow* to the first
Python prompt you get. If it beeps, you have command line editing; see Appendix
A  for an introduction to the keys.
If nothing appears to happen, or if P is echoed, command line editing isn't
available; you'll only be able to use backspace to remove characters from
the current line.

http://pytut.infogami.com/node4.html

konsole somehow disabled command line editing.

On Fri, Mar 9, 2012 at 3:49 AM, Alan Gauld wrote:

> On 09/03/12 02:07, brandon w wrote:
>
>  I am able to up-arrow to get the last typed command using rxvt but when I
>> use konsole and I press the up-arrow I get the symbols: ^[[A
>> Why is that?
>>
>
> Your terminal settings look like they are messed up.
> Does up arrow work in any other applications?
> emacs or vim for example?
>
> If not you will need to configure your termcap/terminfo or stty settings I
> suspect.
>
> If it does work in other apps then I don't know what's happening!
>


>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Floating point error in tkinter

2012-03-10 Thread Válas Péter
Sorry for sending it in private first, Steven drew my attention to it. This
list has a strange setting, I got used to lists that send replies
automatically to list, and did not notice the error.

2012. március 9. 18:53 Válas Péter írta, :

> 2012/3/9 Steven D'Aprano 
>
>>
>> What makes you think it is a Python bug? Is there something that makes
>> you expect that Tkinter should accept strings with commas instead of
>> floats?
>>
> Better to say: I think it is a Tkinter bug (unless we expect Windows to
> adapt itself to Python), and Tkinter is part of the Python dstribution
> AFAIK. As you may see, Tkinter is the only module to appear in the error
> message. We may say it is a Windows bug, but this is the case of the
> elephant and the mouse. :-)
>
>
>> The error message is pretty clear: tkinter is expecting a float, but
>> receiving a string instead.
>>
> No; it receives a float formatted by Hungarian standards as set in control
> panel of Windows, and does not recognize it as a float.
>
>
>>
>> import locale
>> locale.setlocale(locale.LC_ALL, 'english_us')
>>
> That's what I definitely wouldn't like to do, because I *do need*Hungarian 
> locale very much for everything else. Too big a price.
>
> VP
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] UnicodeEncodeError: 'cp932' codec can't encode character '\xe9' in position

2012-03-10 Thread Robert Sjoblom
Okay, so here's a fun one. Since I'm on a japanese locale my native
encoding is cp932. I was thinking of writing a parser for a bunch of
text files, but I stumbled on even printing the contents due to ...
something. I don't know what encoding the text file uses, which isn't
helping my case either (I have asked, but I've yet to get an answer).

Okay, so:

address = "C:/Path/to/file/file.ext"
with open(address, encoding="cp1252") as alpha:
text = alpha.readlines()
for line in text:
print(line)

It starts to print until it hits the wonderful character é or '\xe9',
where it gives me this happy traceback:
Traceback (most recent call last):
  File "C:\Users\Azaz\Desktop\CK2 Map Painter\Parser\test parser.py",
line 8, in 
print(line)
UnicodeEncodeError: 'cp932' codec can't encode character '\xe9' in
position 13: illegal multibyte sequence

I can open the document and view it in UltraEdit -- and it displays
correct characters there -- but UE can't give me what encoding it
uses. Any chance of solving this without having to switch from my
japanese locale? Also, the cp1252 is just an educated guess, but it
doesn't really matter because it always comes back to the cp932 error.

-- 
best regards,
Robert S.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python using RXVT vs Konsole?

2012-03-10 Thread Alan Gauld

On 10/03/12 15:22, brandon w wrote:

Up arrow does work in other applications. The only application that it
does not work in is konsole.


Konsole is a shell, you run applications inside Konsole.
Python is one such application. What I was asking was do the arrow keys 
work in other Konsole applications - like vim. (or emacs started with 
the -nw option) or pico...



I actually do not care for konsole that much. I think I will just use
some other terminal.


That might be the simplest option! :-).

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Floating point error in tkinter

2012-03-10 Thread Alan Gauld

On 10/03/12 19:40, Válas Péter wrote:


Better to say: I think it is a Tkinter bug (unless we expect Windows
to adapt itself to Python), and Tkinter is part of the Python


But it's really a Tcl bug because Tkinter is built on top of Tcl/Tk
and (mostly) cannot fix bugs that exist in the underlying framework.


The error message is pretty clear: tkinter is expecting a float, but
receiving a string instead.

No; it receives a float formatted by Hungarian standards as set in
control panel of Windows, and does not recognize it as a float.


No, it is receiving a string - note the quotation marks.
But Tcl is a strange language that interprets many variables
as strings and, especially in Tk, it is common to pass numerical
values as strings. (In early Tcl versions all variables were strings 
interpreted by Tcl at runtime, one of the most common complaints

and a reason for its early lethargy. Nowadays Tcl uses native
types like most other languages and is no longer sloth like...
But some artifacts remain)


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] UnicodeEncodeError: 'cp932' codec can't encode character '\xe9' in position

2012-03-10 Thread Dave Angel

On 03/10/2012 06:38 PM, Robert Sjoblom wrote:

Okay, so here's a fun one. Since I'm on a japanese locale my native
encoding is cp932. I was thinking of writing a parser for a bunch of
text files, but I stumbled on even printing the contents due to ...
something. I don't know what encoding the text file uses, which isn't
helping my case either (I have asked, but I've yet to get an answer).

Okay, so:

address = "C:/Path/to/file/file.ext"
with open(address, encoding="cp1252") as alpha:
 text = alpha.readlines()
 for line in text:
 print(line)

It starts to print until it hits the wonderful character é or '\xe9',
where it gives me this happy traceback:
Traceback (most recent call last):
   File "C:\Users\Azaz\Desktop\CK2 Map Painter\Parser\test parser.py",
line 8, in
 print(line)
UnicodeEncodeError: 'cp932' codec can't encode character '\xe9' in
position 13: illegal multibyte sequence

I can open the document and view it in UltraEdit -- and it displays
correct characters there -- but UE can't give me what encoding it
uses. Any chance of solving this without having to switch from my
japanese locale? Also, the cp1252 is just an educated guess, but it
doesn't really matter because it always comes back to the cp932 error.



There are just 256 possible characters in cp1252, and 256 in cp932.  So 
you should expect to see this error if your input file is 
unconstrained.  And since you don't know what encoding it's in, you 
might as well consider it unconstrained.


In other words, there are possible characters in the cp1252 that just 
won't display in cp932.


You can "solve" the problem by pretending the input file is also cp932 
when you open it. That way you'll get the wrong characters, but no 
errors.  Or you can solve it by encoding the output explicitly, telling 
it to ignore errors.  I don't know how to do that in Python 3.x.  
Finally, you can change your console to be utf-8, and find a font that 
includes both sets of characters.



--

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] UnicodeEncodeError: 'cp932' codec can't encode character '\xe9' in position

2012-03-10 Thread Robert Sjoblom
> You can "solve" the problem by pretending the input file is also cp932 when
> you open it. That way you'll get the wrong characters, but no errors.
So I tried that:
Traceback (most recent call last):
  File "C:\Users\Azaz\Desktop\CK2 Map Painter\Parser\test parser.py",
line 6, in 
text = alpha.readlines()
UnicodeDecodeError: 'cp932' codec can't decode bytes in position
1374-1375: illegal multibyte sequence

> Or
> you can solve it by encoding the output explicitly, telling it to ignore
> errors.  I don't know how to do that in Python 3.x.
Me neither. I will research this tomorrow.

> Finally, you can change
> your console to be utf-8, and find a font that includes both sets of
> characters.
While that might be a tempting solution, it would be best if this
worked without having to do any changes to the environment itself; it
would be best if it could run on any platform, but I'll take a Windows
machine with no changes to command line if I have to.

-- 
best regards,
Robert S.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor