Re: [Tutor] while loop

2014-03-29 Thread Dave Angel
 Scott W Dunning  Wrote in message:
> 
> On Mar 28, 2014, at 9:54 PM, Scott W Dunning  wrote:
> 
>> Hello, I’m working on some practice exercises from my homework and I’m 
>> having some issues figuring out what is wanted.  
>> 
>> We’re working with the while loop and this is what the question states;
>> 
>> Write a function print_n that prints a string n times using iteration.
>> 
>>  """Print the string `s`, `n` times. 
>> 
>> 
>> This is also in the exercises and I’m not sure what it means and why 
>> it’s there.
>> 
>> assert isinstance(s, str)
>> assert isinstance(n, int)

What are you uncertain about,  assert or isinstance?  Such
 statements are frequently used to make sure the function
 arguments are of the right type. 

>
> 
> This is what I have so far but I’m not really sure it’s what the 
> excersise is asking for?
> 
> n = 5
> def print_n(s, n):
>while n > 0:
>print s * n
> 
> print_n("hello", 10)
> 

So did your code print the string 10 times?  When asking for help,
 it's useful to show what you tried,  and what was expected,  and
 what actually resulted. 

You use * to replicate the string,  but that wasn't what the
 assignment asked for. So take out the *n part. You're supposed to
 use iteration,  specifically the while loop. 

Your while loop doesn't quit after 10 times, it keeps going.  Can
 you figure out why?



-- 
DaveA

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


[Tutor] ElementTree, iterable container, depth of elements

2014-03-29 Thread street . sweeper
I'm trying to sort the order of elements in an xml file, mostly
to make visual inspection/comparison easier.  The example xml and
code on http://effbot.org/zone/element-sort.htm get me almost
what I need, but the xml I'm working with has the element I'm
trying to sort on one level deeper.


That page's example xml:


  

  Ned
  555-8904


  John
  555-5782


  Julius
  555-3642

  



And that page's last example of code:

  import xml.etree.ElementTree as ET
  tree = ET.parse("data.xml")
  def getkey(elem):
return elem.findtext("number")
  container = tree.find("entries")
  container[:] = sorted(container,key=getkey)
  tree.write("new-data.xml")

I used the interactive shell to experiment a bit with that,
and I can see that 'container' in

  container = tree.find("entries")

is iterable, using

  for a in container:
print(a)

However, the xml I'm working with looks something like this:


  

  
20140325
dentist
  
  
20140324
barber
  

  



What I'd like to do is rearrange the  elements within
 based on the  element.  If I remove the 
level, this will work, but I'm interested in getting the code to
work without editing the file.

I look for "Date" and "diary" rather than "number" and "entries"
but when I try to process the file as-is, I get an error like


Traceback (most recent call last):
  File "./xmlSort.py", line 16, in 
container[:] = sorted(container, key=getkey)
TypeError: 'NoneType' object is not iterable


"container[:] = sorted(container, key=getkey)" confuses me,
particularly because I don't see how the elem parameter is passed
to the getkey function.

I know if I do

  root = tree.getroot()

(from the python.org ElementTree docs) it is possible to step
down through the levels of root with root[0], root[0][0], etc,
and it seems to be possible to iterate with

  for i in root[0][0]:
print(i)

but trying to work root[0][0] into the code has not worked,
and tree[0] is not possible.

How can I get this code to do its work one level down in the xml?

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


Re: [Tutor] ElementTree, iterable container, depth of elements

2014-03-29 Thread Dave Angel
 street.swee...@mailworks.org Wrote in message:
> I'm trying to sort the order of elements in an xml file, mostly
> to make visual inspection/comparison easier.  The example xml and
> code on http://effbot.org/zone/element-sort.htm get me almost
> what I need, but the xml I'm working with has the element I'm
> trying to sort on one level deeper.
> 
> 
> That page's example xml:
> 
> 
>   
> 
>   Ned
>   555-8904
> 
> 
>   John
>   555-5782
> 
> 
>   Julius
>   555-3642
> 
>   
> 
> 
> 
> And that page's last example of code:
> 
>   import xml.etree.ElementTree as ET
>   tree = ET.parse("data.xml")
>   def getkey(elem):
> return elem.findtext("number")
>   container = tree.find("entries")
>   container[:] = sorted(container,key=getkey)

That would be more clearly written
  sort(container,key=getkey)

>   tree.write("new-data.xml")
> 
> 
..
> Traceback (most recent call last):
>   File "./xmlSort.py", line 16, in 
> container[:] = sorted(container, key=getkey)
> TypeError: 'NoneType' object is not iterable
> 

That simply tells you that tree.find () returns None. 

> 
> "container[:] = sorted(container, key=getkey)" confuses me,
> particularly because I don't see how the elem parameter is passed
> to the getkey function.
> 

You should play with a list,  sorting it with a key function.  For
 example make a list of strings,  using a key=

def mykey (elem):
  return elem.lower ()

To see what's going on,  add a print to that function,  showing
 yourself that the sort function will call mykey () on each
 element.


I can't help with the xml stuff,  but it seems clear you have to
 produce a find call that gives you the right list.


-- 
DaveA

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


Re: [Tutor] ElementTree, iterable container, depth of elements

2014-03-29 Thread Peter Otten
street.swee...@mailworks.org wrote:

> I'm trying to sort the order of elements in an xml file, mostly
> to make visual inspection/comparison easier.  The example xml and
> code on http://effbot.org/zone/element-sort.htm get me almost
> what I need, but the xml I'm working with has the element I'm
> trying to sort on one level deeper.
> 
> 
> That page's example xml:
> 
> 
>   
> 
>   Ned
>   555-8904
> 
> 
>   John
>   555-5782
> 
> 
>   Julius
>   555-3642
> 
>   
> 
> 
> 
> And that page's last example of code:
> 
>   import xml.etree.ElementTree as ET
>   tree = ET.parse("data.xml")
>   def getkey(elem):
> return elem.findtext("number")
>   container = tree.find("entries")
>   container[:] = sorted(container,key=getkey)
>   tree.write("new-data.xml")
> 
> I used the interactive shell to experiment a bit with that,
> and I can see that 'container' in
> 
>   container = tree.find("entries")
> 
> is iterable, using
> 
>   for a in container:
> print(a)
> 
> However, the xml I'm working with looks something like this:
> 
> 
>   
> 
>   
> 20140325
> dentist
>   
>   
> 20140324
> barber
>   
> 
>   
> 
> 
> 
> What I'd like to do is rearrange the  elements within
>  based on the  element.  If I remove the 
> level, this will work, but I'm interested in getting the code to
> work without editing the file.
> 
> I look for "Date" and "diary" rather than "number" and "entries"
> but when I try to process the file as-is, I get an error like
> 
> 
> Traceback (most recent call last):
>   File "./xmlSort.py", line 16, in 
> container[:] = sorted(container, key=getkey)
> TypeError: 'NoneType' object is not iterable
> 
> 
> "container[:] = sorted(container, key=getkey)" confuses me,
> particularly because I don't see how the elem parameter is passed
> to the getkey function.

In the original example container is the "entries" element, and sorted() 
iterates over the items of its first argument. Iteration over an element 
yield its children, i. e. the first "entry" element, then the second 
"entry", and so on.
 
> I know if I do
> 
>   root = tree.getroot()
> 
> (from the python.org ElementTree docs) it is possible to step
> down through the levels of root with root[0], root[0][0], etc,
> and it seems to be possible to iterate with
> 
>   for i in root[0][0]:
> print(i)
> 
> but trying to work root[0][0] into the code has not worked,
> and tree[0] is not possible.
> 
> How can I get this code to do its work one level down in the xml?

try

tree.find("main").find("diary") 

or

container = tree.find("main/diary")

or even 

tree.find(".//diary") # we don't care about the parent


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