Re: [Tutor] tkinter, create widgets during runtime?

2011-01-27 Thread Elwin Estle
>From the lack of replies...I am guessing that this can't be done.  Tho I just 
>realized I had a typo in part of it.

The line that reads:
"Is there a way to do something like this in Tkinter?  Or am I correct in
 guessing that if it is not possible, it is probably more complicated 
than the above?"

...should have said, "Or am I correct in guessing tat if it IS possible, it is 
probably more..."

--- On Wed, 1/26/11, Elwin Estle  wrote:

From: Elwin Estle 
Subject: [Tutor] tkinter, create widgets during runtime?
To: tutor@python.org
Date: Wednesday, January 26, 2011, 12:21 PM

With Tcl/Tk, you can generate widgets "on the fly" during program execution, 
without having to explicitly create them in your code.  i.e., something like:

for {set i 0} {$i <= 5} {incr i} {
    label .myLabel_$i -text "this is label myLabel_$i"
    pack .myLabel_$i
}

...which will generate five labels from "myLabel_0" to "myLabel_5".

Useful if you need a varying number of widgets, or a series of "incremental" 
widgets.

Is there a way to do something like this in Tkinter?  Or am I correct in 
guessing that if it is not possible, it is probably more complicated than the 
above?






  
-Inline Attachment Follows-

___
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] get xml for parsing?

2011-01-27 Thread Alex Hall
On 1/27/11, Stefan Behnel  wrote:
> Alex Hall, 27.01.2011 05:01:
>> How would I go about getting the xml from a website through the site's
>> api? The url does not end in .xml since the xml is generated based on
>> the parameters in the url. For example:
>> https://api.website.com/user/me/count/10?api_key=MY_KEY
>> would return ten results (the count parameter) as xml. How do I
>> actually get this xml into my program? TIA.
>
> The filename extension doesn't matter. If you know it's XML that you get
> back, you can use ElementTree (in the xml.etree package) or lxml (external
> package) to read it. Use the urllib2 package in the standard library to
> request the XML page, then pass the result into the XML parser (parse()).
Okay, thanks!
>
> Stefan
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tkinter, create widgets during runtime?

2011-01-27 Thread Karim


I never did that but this is python!
You can create anything you want at runtime!
Make a factory method in you gui class which inherit from Frame or 
whatever container code below is testing:


>>> from Tkinter import *
>>> class gui(Frame):
...  def __init__(self, master=None):
...  Frame.__init__(self, master)
...  self.pack()
...  def createLabels(self):
...  labels = []
...  for  i in range(4):
...   labelName = "myLabel_{0}".format(i)
...   labels.append(Label(master=self, text="this is label " 
+ labelName))

...  for label in labels:
...  label.pack()
...
>>> a=gui()
>>> a.createLabels()


self is the instance of your inherited Frame or else container.

Regards
Karim


On 01/27/2011 01:15 PM, Elwin Estle wrote:
From the lack of replies...I am guessing that this can't be done.  Tho 
I just realized I had a typo in part of it.


The line that reads:
"Is there a way to do something like this in Tkinter?  Or am I correct 
in guessing that if it is not possible, it is probably more 
complicated than the above?"


...should have said, "Or am I correct in guessing tat if it IS 
possible, it is probably more..."


--- On *Wed, 1/26/11, Elwin Estle //* wrote:


From: Elwin Estle 
Subject: [Tutor] tkinter, create widgets during runtime?
To: tutor@python.org
Date: Wednesday, January 26, 2011, 12:21 PM

With Tcl/Tk, you can generate widgets "on the fly" during program
execution, without having to explicitly create them in your code. 
i.e., something like:


for {set i 0} {$i <= 5} {incr i} {
label .myLabel_$i -text "this is label myLabel_$i"
pack .myLabel_$i
}

...which will generate five labels from "myLabel_0" to "myLabel_5".

Useful if you need a varying number of widgets, or a series of
"incremental" widgets.

Is there a way to do something like this in Tkinter?  Or am I
correct in guessing that if it is not possible, it is probably
more complicated than the above?





-Inline Attachment Follows-

___
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


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


Re: [Tutor] tkinter, create widgets during runtime?

2011-01-27 Thread python
Elwin,

There is a dedicated Python Tkinter mailing list called
tkinter-discuss. I would post your question to this mailing list
- I've found them very helpful.

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


Re: [Tutor] tkinter, create widgets during runtime?

2011-01-27 Thread Wayne Werner
On Wed, Jan 26, 2011 at 11:21 AM, Elwin Estle wrote:

> With Tcl/Tk, you can generate widgets "on the fly" during program
> execution, without having to explicitly create them in your code.  i.e.,
> something like:
>
> for {set i 0} {$i <= 5} {incr i} {
> label .myLabel_$i -text "this is label myLabel_$i"
> pack .myLabel_$i

}
>

for x in xrange(5):
Label(root, text="This is label %d" %x).pack()

will generate the labels. You can't put the variable names in the local
namespace without some tricksy methods. You could, however, have a dict
labels and just do

labels[x] = Label(root, text="blah")
labels[x].pack()

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


[Tutor] Facebook

2011-01-27 Thread Christopher King
Dear Tutors,
I'm using the Facebook API. How do you get an access token? The
documentations at http://developers.facebook.com/docs/api, but I
can't figure out an easy way to do it.

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


Re: [Tutor] tkinter, create widgets during runtime?

2011-01-27 Thread Karim


Hello Wayne,

I provided a complete example see the mail above, pretty similar to yours.

Regards
Karim

On 01/27/2011 04:54 PM, Wayne Werner wrote:


On Wed, Jan 26, 2011 at 11:21 AM, Elwin Estle 
mailto:chrysalis_reb...@yahoo.com>> wrote:


With Tcl/Tk, you can generate widgets "on the fly" during program
execution, without having to explicitly create them in your code. 
i.e., something like:


for {set i 0} {$i <= 5} {incr i} {
label .myLabel_$i -text "this is label myLabel_$i"
pack .myLabel_$i

}


for x in xrange(5):
Label(root, text="This is label %d" %x).pack()

will generate the labels. You can't put the variable names in the 
local namespace without some tricksy methods. You could, however, have 
a dict labels and just do


labels[x] = Label(root, text="blah")
labels[x].pack()

HTH,
Wayne


___
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] Facebook

2011-01-27 Thread Luke Paireepinart
And what have you tried? What libs are you using? Which part is confusing you?

-
Sent from a mobile device. Apologies for brevity and top-posting.
-

On Jan 27, 2011, at 9:08 AM, Christopher King  wrote:

> Dear Tutors,
> I'm using the Facebook API. How do you get an access token? The 
> documentations at http://developers.facebook.com/docs/api, but I can't figure 
> out an easy way to do it.
> 
> Sincerely,
> Chris
> ___
> 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] Facebook

2011-01-27 Thread Christopher King
Well actually I got it working now. I used the library from
https://github.com/facebook/python-sdk/. It said you had to create an app,
which I didn't want to do. That is, to get the access token. An access token
is what gives me the right to look at peoples profile.

What I found, was an app already made, in which I can edit the html to
have it request any permission I want it to, and then give me the access
token for my own programs. Its let's you save the code in a permalink. He is
my code: http://fbrell.com/saved/6b455533f5f2c124cdc74a635d09cbd4. It will
not work by itself, so you must slightly edit it. Where it says, put
permissions here, type what permissions you want the token to
have, separated by commas, and click run code. Then click the button. A list
of request-able permissions are here:
http://developers.facebook.com/docs/authentication/permissions/. I've tested
it, and its cool. I posted to my wall from facebook!

On Thu, Jan 27, 2011 at 11:43 AM, Luke Paireepinart
wrote:

> And what have you tried? What libs are you using? Which part is confusing
> you?
>
> -
> Sent from a mobile device. Apologies for brevity and top-posting.
> -
>
> On Jan 27, 2011, at 9:08 AM, Christopher King  wrote:
>
> Dear Tutors,
> I'm using the Facebook API. How do you get an access token? The
> documentations at  
> http://developers.facebook.com/docs/api, but I can't figure out an easy
> way to do it.
>
> Sincerely,
> Chris
>
> ___
> 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] Facebook

2011-01-27 Thread Steven D'Aprano

Luke Paireepinart wrote:

And what have you tried? What libs are you using? Which part is confusing you?


Surely it is the part Christopher says... getting an access token?

I wonder whether he has read this?

http://developers.facebook.com/docs/authentication/?_fb_noscript=1

(which is the first link that comes up when googling for "facebook api 
access token").



On Jan 27, 2011, at 9:08 AM, Christopher King  wrote:


Dear Tutors,
I'm using the Facebook API. How do you get an access token? The 
documentations at http://developers.facebook.com/docs/api, but I can't figure 
out an easy way to do it.




--
Steven

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


Re: [Tutor] tkinter, create widgets during runtime?

2011-01-27 Thread Karim


Sorry, I forgot to update continuously the Frame:

>>> app.mainloop()

Karim

On 01/27/2011 02:59 PM, Karim wrote:


I never did that but this is python!
You can create anything you want at runtime!
Make a factory method in you gui class which inherit from Frame or 
whatever container code below is testing:


>>> from Tkinter import *
>>> class gui(Frame):
...  def __init__(self, master=None):
...  Frame.__init__(self, master)
...  self.pack()
...  def createLabels(self):
...  labels = []
...  for  i in range(4):
...   labelName = "myLabel_{0}".format(i)
...   labels.append(Label(master=self, text="this is label 
" + labelName))

...  for label in labels:
...  label.pack()
...
>>> a=gui()
>>> a.createLabels()


self is the instance of your inherited Frame or else container.

Regards
Karim


On 01/27/2011 01:15 PM, Elwin Estle wrote:
From the lack of replies...I am guessing that this can't be done.  
Tho I just realized I had a typo in part of it.


The line that reads:
"Is there a way to do something like this in Tkinter?  Or am I 
correct in guessing that if it is not possible, it is probably more 
complicated than the above?"


...should have said, "Or am I correct in guessing tat if it IS 
possible, it is probably more..."


--- On *Wed, 1/26/11, Elwin Estle //* wrote:


From: Elwin Estle 
Subject: [Tutor] tkinter, create widgets during runtime?
To: tutor@python.org
Date: Wednesday, January 26, 2011, 12:21 PM

With Tcl/Tk, you can generate widgets "on the fly" during program
execution, without having to explicitly create them in your
code.  i.e., something like:

for {set i 0} {$i <= 5} {incr i} {
label .myLabel_$i -text "this is label myLabel_$i"
pack .myLabel_$i
}

...which will generate five labels from "myLabel_0" to "myLabel_5".

Useful if you need a varying number of widgets, or a series of
"incremental" widgets.

Is there a way to do something like this in Tkinter?  Or am I
correct in guessing that if it is not possible, it is probably
more complicated than the above?





-Inline Attachment Follows-

___
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



___
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] tkinter, create widgets during runtime?

2011-01-27 Thread Karim


Sorry, I forgot to update continuously the Frame:

>>> a.mainloop()

Karim

On 01/27/2011 02:59 PM, Karim wrote:


I never did that but this is python!
You can create anything you want at runtime!
Make a factory method in you gui class which inherit from Frame or 
whatever container code below is testing:


>>> from Tkinter import *
>>> class gui(Frame):
...  def __init__(self, master=None):
...  Frame.__init__(self, master)
...  self.pack()
...  def createLabels(self):
...  labels = []
...  for  i in range(4):
...   labelName = "myLabel_{0}".format(i)
...   labels.append(Label(master=self, text="this is label 
" + labelName))

...  for label in labels:
...  label.pack()
...
>>> a=gui()
>>> a.createLabels()


self is the instance of your inherited Frame or else container.

Regards
Karim


On 01/27/2011 01:15 PM, Elwin Estle wrote:
From the lack of replies...I am guessing that this can't be done.  
Tho I just realized I had a typo in part of it.


The line that reads:
"Is there a way to do something like this in Tkinter?  Or am I 
correct in guessing that if it is not possible, it is probably more 
complicated than the above?"


...should have said, "Or am I correct in guessing tat if it IS 
possible, it is probably more..."


--- On *Wed, 1/26/11, Elwin Estle //* wrote:


From: Elwin Estle 
Subject: [Tutor] tkinter, create widgets during runtime?
To: tutor@python.org
Date: Wednesday, January 26, 2011, 12:21 PM

With Tcl/Tk, you can generate widgets "on the fly" during program
execution, without having to explicitly create them in your
code.  i.e., something like:

for {set i 0} {$i <= 5} {incr i} {
label .myLabel_$i -text "this is label myLabel_$i"
pack .myLabel_$i
}

...which will generate five labels from "myLabel_0" to "myLabel_5".

Useful if you need a varying number of widgets, or a series of
"incremental" widgets.

Is there a way to do something like this in Tkinter?  Or am I
correct in guessing that if it is not possible, it is probably
more complicated than the above?





-Inline Attachment Follows-

___
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



___
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


[Tutor] small ElementTree problem

2011-01-27 Thread Alex Hall
Hi all,
I am using, and very much enjoying, the ElementTree library. However,
I have hit a problem. Say I have something along the lines of:


Message from Service
1.0
 
  
   1
   result 1
  
  
   2
   result 2
  
 


In my ResultSet class, I parse this to get the text of elements like
message or version. Then, I use root.findall("list/result") and
iterate over the result, passing to a second function which parses the
passed-in elements into Result objects. For example:

all=root.findall("list/result")
for i in all:
 self.mylist.append(Obj().parse(i))

In Obj.parse(), the element passed in is treated like this:

def parse(data):
 root=data.getroot()
 self.id=root.find("id").text
self.name=root.find("name).text

Printing the results of the above through Obj.id or Obj.name gives me
odd behavior:
print Obj.id :>  None

What is going on? Does the root change when I call find()? Why would
an Element object get used when I clearly say to use the text of the
found element? TIA.

-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] tkinter, create widgets during runtime?

2011-01-27 Thread Elwin Estle
Thanks!  That was just what I was looking for.

--- On Thu, 1/27/11, Wayne Werner  wrote:

From: Wayne Werner 
Subject: Re: [Tutor] tkinter, create widgets during runtime?
To: "Elwin Estle" 
Cc: tutor@python.org
Date: Thursday, January 27, 2011, 10:54 AM


On Wed, Jan 26, 2011 at 11:21 AM, Elwin Estle  
wrote:


With Tcl/Tk, you can generate widgets "on the fly" during program execution, 
without having to explicitly create them in your code.  i.e., something like:



for {set i 0} {$i <= 5} {incr i} {
    label .myLabel_$i -text "this is label myLabel_$i"
    pack .myLabel_$i 

}



for x in xrange(5):    Label(root, text="This is label %d" %x).pack()
will generate the labels. You can't put the variable names in the local 
namespace without some tricksy methods. You could, however, have a dict labels 
and just do


labels[x] = Label(root, text="blah")labels[x].pack()
HTH,Wayne



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


[Tutor] ascii codec cannot encode character

2011-01-27 Thread Alex Hall
Hello again:
I have never seen this message before. I am pulling xml from a site's
api and printing it, testing the wrapper I am writing for the api. I
have never seen this error until just now, in the twelfth result of my
search:
UnicodeEncodeError: 'ASCII' codec can't encode character u'\u2019' in
position 42: ordinal not in range(128)

I tried making the strings Unicode by saying something like
self.title=unicode(data.find("title").text)
but the same error appeared. I found the manual chapter on this, but I
am not sure I want to ignore since I do not know what this character
(or others) might mean in the string. I am not clear on what 'replace'
will do. Any suggestions?

-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] small ElementTree problem

2011-01-27 Thread Karim


id is a tag so it is a OBJECT Element with attributes accessible by 
dictionnary x.attrib[key] and x.text for tag content text.


canonical string representation of your Element object: Obj.id :>composite 
pattern.
For result use iterator as below (not tested but should be ok):

*_/#Parsing:/_

  doc = ElementTree()
  doc.parse(xmlFile)*

*_/#iteration over tag element:/_

ids  = []
names =[]
for result in doc.iter('result'):
  idElem   = result.find('id')
***   nameElem = result.find('name')**
*   ids.append(idElem.text)
  names.append(nameElement.text)

final = zip(ids, names)
*

You are not obliged to provide the full XPATH. Etree search for you.

Regards
Karim

On 01/27/2011 11:23 PM, Alex Hall wrote:

Hi all,
I am using, and very much enjoying, the ElementTree library. However,
I have hit a problem. Say I have something along the lines of:


Message from Service
1.0
  
   
1
result 1
   
   
2
result 2
   
  


In my ResultSet class, I parse this to get the text of elements like
message or version. Then, I use root.findall("list/result") and
iterate over the result, passing to a second function which parses the
passed-in elements into Result objects. For example:

all=root.findall("list/result")
for i in all:
  self.mylist.append(Obj().parse(i))

In Obj.parse(), the element passed in is treated like this:

def parse(data):
  root=data.getroot()
  self.id=root.find("id").text
self.name=root.find("name).text

Printing the results of the above through Obj.id or Obj.name gives me
odd behavior:
print Obj.id :>None

What is going on? Does the root change when I call find()? Why would
an Element object get used when I clearly say to use the text of the
found element? TIA.



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


Re: [Tutor] small ElementTree problem

2011-01-27 Thread Alex Hall
Thanks, I think I have it working.

On 1/27/11, Karim  wrote:
>
> id is a tag so it is a OBJECT Element with attributes accessible by
> dictionnary x.attrib[key] and x.text for tag content text.
>
> canonical string representation of your Element object: Obj.id :>   'result' at [mem addr]
> And root is also an Element but the parent of id and name childs =>
> composite pattern.
> For result use iterator as below (not tested but should be ok):
>
> *_/#Parsing:/_
>
>doc = ElementTree()
>doc.parse(xmlFile)*
>
> *_/#iteration over tag element:/_
>
> ids  = []
> names =[]
> for result in doc.iter('result'):
>idElem   = result.find('id')
> ***   nameElem = result.find('name')**
> *   ids.append(idElem.text)
>names.append(nameElement.text)
>
> final = zip(ids, names)
> *
>
> You are not obliged to provide the full XPATH. Etree search for you.
>
> Regards
> Karim
>
> On 01/27/2011 11:23 PM, Alex Hall wrote:
>> Hi all,
>> I am using, and very much enjoying, the ElementTree library. However,
>> I have hit a problem. Say I have something along the lines of:
>>
>> 
>> Message from Service
>> 1.0
>>   
>>
>> 1
>> result 1
>>
>>
>> 2
>> result 2
>>
>>   
>> 
>>
>> In my ResultSet class, I parse this to get the text of elements like
>> message or version. Then, I use root.findall("list/result") and
>> iterate over the result, passing to a second function which parses the
>> passed-in elements into Result objects. For example:
>>
>> all=root.findall("list/result")
>> for i in all:
>>   self.mylist.append(Obj().parse(i))
>>
>> In Obj.parse(), the element passed in is treated like this:
>>
>> def parse(data):
>>   root=data.getroot()
>>   self.id=root.find("id").text
>> self.name=root.find("name).text
>>
>> Printing the results of the above through Obj.id or Obj.name gives me
>> odd behavior:
>> print Obj.id :>  > print self.id :>  None
>>
>> What is going on? Does the root change when I call find()? Why would
>> an Element object get used when I clearly say to use the text of the
>> found element? TIA.
>>
>
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ascii codec cannot encode character

2011-01-27 Thread Steven D'Aprano

Alex Hall wrote:

Hello again:
I have never seen this message before. I am pulling xml from a site's
api and printing it, testing the wrapper I am writing for the api. I
have never seen this error until just now, in the twelfth result of my
search:
UnicodeEncodeError: 'ASCII' codec can't encode character u'\u2019' in
position 42: ordinal not in range(128)

I tried making the strings Unicode by saying something like
self.title=unicode(data.find("title").text)
but the same error appeared. I found the manual chapter on this, but I
am not sure I want to ignore since I do not know what this character
(or others) might mean in the string. I am not clear on what 'replace'
will do. Any suggestions?


Short version
=

You need to decode the bytes you get from the XML into unicode 
characters. You would do this using something like:


unicode(data.find("title").text, encoding='utf-8')

If that doesn't work, change utf-8 to another encoding. If the XML file 
tells you what the encoding should be, use that.


Alternatively, you could say:

unicode(data.find("title").text, errors='replace')

to substitute a "missing character" glyph for any undecodable bytes in 
the XML stream, or


unicode(data.find("title").text, errors='ignore')

to just ignore them.


Long version


You can't just say "turn these bytes into unicode" and expect it to 
magically work. Remember, in Python 2, so-called "strings" are actually 
strings of *bytes*, not characters. If you're a native English speaker, 
you've probably never needed to care about the distinction, but it is real.


When you have a string "spam", what that *really* is is a sequence of 
bytes 73 70 61 6D (in hexadecimal). By convention, Python uses the ASCII 
encoding map bytes to characters (e.g. hex 73 <=> "s"). That's not the 
only choice, but it has been the conventional choice for so long that 
people have forgotten that there are any other choices.


The problem with ASCII is that it only knows how to deal with 128 
different bytes, and about 30 of those are invisible control characters. 
The other 128 bytes don't mean anything in ASCII, and you can run into 
problems trying to deal with them as text.


There are hundreds of thousands of useful characters in the world, and 
only 128 ASCII ones. Prior to Unicode, people would choose their own 
preferred set of 256 useful characters, and semi-arbitrarily assign them 
to each of the 256 different bytes. Consequently there was a plethora of 
ad hoc encodings where a byte like (say) xC4  might represent (say)


'Ä' on Windows computers used in northern and western Europe
'─' on computers in Greece
'ƒ' on Macintosh computers in Western Europe
'ń' on Macintoshes in Eastern Europe

and so forth. As you can imagine, exchanging files from one machine to 
another was a nightmare. This is where Unicode comes in -- in theory, 
there is a Unicode character for every useful character in any language 
anywhere, including mathematical symbols, dingbats, ancient dead 
languages, pictograms, and more.


BUT files on disk, and in memory, are in bytes, not characters. You need 
some way to convert a character string into bytes, and back again. There 
are many different ways of doing so, depending on whether you care about 
making it as fast as possible, or as efficient as possible, or 
compatible with some pre-Unicode character set. And this is where the 
idea of encodings come in. You can see a list of supported encodings here:


http://docs.python.org/library/codecs.html#standard-encodings

So the idea is, when you have a stream of bytes (say, from reading from 
a disk), you have to *decode* those bytes into Unicode text, and to 
write that text back again, you have to *encode* it to bytes.


Now, Python tries to be very conservative: if you don't specify an 
encoding, it assumes you want ASCII, the lowest common denominator 
encoding that keeps English speakers happy. Lucky us. Until we have to 
deal with one or more bytes which can't be decoded into ASCII:


>>> "\xC4".decode('ascii')
Traceback (most recent call last):
  File "", line 1, in 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 0: 
ordinal not in range(128)


Python isn't going to guess what character you want byte C4 to 
represent. We've already seen there are at least four different choices. 
You have to tell it which one you mean:


>>> print unicode("\xC4", encoding='macroman')
ƒ


Must-read article:
http://www.joelonsoftware.com/articles/Unicode.html





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


Re: [Tutor] ascii codec cannot encode character

2011-01-27 Thread Alex Hall
On 1/27/11, Steven D'Aprano  wrote:
> Alex Hall wrote:
>> Hello again:
>> I have never seen this message before. I am pulling xml from a site's
>> api and printing it, testing the wrapper I am writing for the api. I
>> have never seen this error until just now, in the twelfth result of my
>> search:
>> UnicodeEncodeError: 'ASCII' codec can't encode character u'\u2019' in
>> position 42: ordinal not in range(128)
>>
>> I tried making the strings Unicode by saying something like
>> self.title=unicode(data.find("title").text)
>> but the same error appeared. I found the manual chapter on this, but I
>> am not sure I want to ignore since I do not know what this character
>> (or others) might mean in the string. I am not clear on what 'replace'
>> will do. Any suggestions?
>
> Short version
> =
>
> You need to decode the bytes you get from the XML into unicode
> characters. You would do this using something like:
>
> unicode(data.find("title").text, encoding='utf-8')
>
> If that doesn't work, change utf-8 to another encoding. If the XML file
> tells you what the encoding should be, use that.
>
> Alternatively, you could say:
>
> unicode(data.find("title").text, errors='replace')
>
> to substitute a "missing character" glyph for any undecodable bytes in
> the XML stream, or
>
> unicode(data.find("title").text, errors='ignore')
>
> to just ignore them.
I tried both of those and got a different error. I have since fixed it
so I no longer have the exact text, but it was something about not
supporting convertion from unicode. I finally ended up doing this:
self.title=data.find("title").text.encode("utf-8")
and it seems happy enough, though I get odd characters above 128. I
suppose it is better than a traceback, and I suspect I just have the
wrong character set. Still, I found it very odd that unicode(string,
errors='replace') threw an exception.
>
>
> Long version
> 
>
> You can't just say "turn these bytes into unicode" and expect it to
> magically work. Remember, in Python 2, so-called "strings" are actually
> strings of *bytes*, not characters. If you're a native English speaker,
> you've probably never needed to care about the distinction, but it is real.
>
> When you have a string "spam", what that *really* is is a sequence of
> bytes 73 70 61 6D (in hexadecimal). By convention, Python uses the ASCII
> encoding map bytes to characters (e.g. hex 73 <=> "s"). That's not the
> only choice, but it has been the conventional choice for so long that
> people have forgotten that there are any other choices.
>
> The problem with ASCII is that it only knows how to deal with 128
> different bytes, and about 30 of those are invisible control characters.
> The other 128 bytes don't mean anything in ASCII, and you can run into
> problems trying to deal with them as text.
>
> There are hundreds of thousands of useful characters in the world, and
> only 128 ASCII ones. Prior to Unicode, people would choose their own
> preferred set of 256 useful characters, and semi-arbitrarily assign them
> to each of the 256 different bytes. Consequently there was a plethora of
> ad hoc encodings where a byte like (say) xC4  might represent (say)
>
> 'Ä' on Windows computers used in northern and western Europe
> '─' on computers in Greece
> 'ƒ' on Macintosh computers in Western Europe
> 'ń' on Macintoshes in Eastern Europe
>
> and so forth. As you can imagine, exchanging files from one machine to
> another was a nightmare. This is where Unicode comes in -- in theory,
> there is a Unicode character for every useful character in any language
> anywhere, including mathematical symbols, dingbats, ancient dead
> languages, pictograms, and more.
>
> BUT files on disk, and in memory, are in bytes, not characters. You need
> some way to convert a character string into bytes, and back again. There
> are many different ways of doing so, depending on whether you care about
> making it as fast as possible, or as efficient as possible, or
> compatible with some pre-Unicode character set. And this is where the
> idea of encodings come in. You can see a list of supported encodings here:
>
> http://docs.python.org/library/codecs.html#standard-encodings
>
> So the idea is, when you have a stream of bytes (say, from reading from
> a disk), you have to *decode* those bytes into Unicode text, and to
> write that text back again, you have to *encode* it to bytes.
>
> Now, Python tries to be very conservative: if you don't specify an
> encoding, it assumes you want ASCII, the lowest common denominator
> encoding that keeps English speakers happy. Lucky us. Until we have to
> deal with one or more bytes which can't be decoded into ASCII:
>
>  >>> "\xC4".decode('ascii')
> Traceback (most recent call last):
>File "", line 1, in 
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 0:
> ordinal not in range(128)
>
> Python isn't going to guess what character you want byte C4 to
> represent. We've already seen there are at 

Re: [Tutor] small ElementTree problem

2011-01-27 Thread Stefan Behnel

Hi,

since you said that you have it working already, here are just a few 
comments on your code.


Alex Hall, 27.01.2011 23:23:

all=root.findall("list/result")
for i in all:
  self.mylist.append(Obj().parse(i))


It's uncommon to use "i" for anything but integer loop variables. And 'all' 
is not a very telling name either. I'd use something like "all_results" and 
"result_element" instead.




In Obj.parse(), the element passed in is treated like this:

def parse(data):
  root=data.getroot()


I don't understand this. You already have an Element here according to your 
code above. Why do you try to call getroot() on it? Again, "data" is not 
very telling. Giving it a better name will help you here.




  self.id=root.find("id").text
self.name=root.find("name).text


There's a findtext() method on Elements for this purpose.



Printing the results of the above through Obj.id or Obj.name gives me
odd behavior:
print Obj.id :>None


"Obj.id" is a class attribute. "self.id" is an instance attribute. 
Different things.




Does the root change when I call find()?


No.



Why would
an Element object get used when I clearly say to use the text of the
found element?


I don't think the code snippets you showed us above are enough to answer this.

Stefan

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