[Tutor] removing xml elements with ElementTree

2019-03-20 Thread street . sweeper
An opportunity to work in Python, and the necessity of working with some XML 
too large to visualize, got me thinking about an answer Alan Gauld had written 
to me a few years ago 
(https://mail.python.org/pipermail/tutor/2015-June/105810.html).  I have 
applied that information in this script, but I have another question :)

Let's say I have an xml file like this:

-- order.xml 


Bob
321 Main St


D20
4


CS211
1


BL5
7


AC400
1




-- end order.xml 

Items CS211 and AC400 are not valid items, and I want to remove their 
 nodes.  I came up with the following (python 3.6.7 on linux):

 xml_delete_test.py 

import os
import xml.etree.ElementTree as ET

hd = os.path.expanduser('~')
inputxml = os.path.join(hd,'order.xml')
outputxml = os.path.join(hd,'fixed_order.xml')

valid_items = ['D20','BL5']

tree = ET.parse(inputxml)
root = tree.getroot()
saleslines = root.find('saleslines').findall('salesline')
for e in saleslines[:]:
if e.find('item').text not in valid_items:
saleslines.remove(e)

tree.write(outputxml)

-- end xml_delete_test.py --

The above code runs without error, but simply writes the original file to disk. 
 The desired output would be:

-- fixed_order.xml 


Bob
321 Main St


D20
4


BL5
7




-- end fixed_order.xml 

What I find particularly confusing about the problem is that after running 
xml_delete_test.py in the Idle editor, if I go over to the shell and type 
saleslines, I can see that it's now a list of two elements.  I run the 
following:

for i in saleslines:
print(i.find('item').text)

and I see that it's D20 and BL5, my two valid items.  Yet when I write tree out 
to the disk, it has the original four.  Do I need to refresh tree somehow?

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


Re: [Tutor] removing xml elements with ElementTree

2019-03-20 Thread Peter Otten
street.swee...@mailworks.org wrote:

> An opportunity to work in Python, and the necessity of working with some
> XML too large to visualize, got me thinking about an answer Alan Gauld had
> written to me a few years ago
> (https://mail.python.org/pipermail/tutor/2015-June/105810.html).  I have
> applied that information in this script, but I have another question :)
> 
> Let's say I have an xml file like this:
> 
> -- order.xml 
> 
> 
> Bob
> 321 Main St
> 
> 
> D20
> 4
> 
> 
> CS211
> 1
> 
> 
> BL5
> 7
> 
> 
> AC400
> 1
> 
> 
> 
> 
> -- end order.xml 
> 
> Items CS211 and AC400 are not valid items, and I want to remove their
>  nodes.  I came up with the following (python 3.6.7 on linux):
> 
>  xml_delete_test.py 
> 
> import os
> import xml.etree.ElementTree as ET
> 
> hd = os.path.expanduser('~')
> inputxml = os.path.join(hd,'order.xml')
> outputxml = os.path.join(hd,'fixed_order.xml')
> 
> valid_items = ['D20','BL5']
> 
> tree = ET.parse(inputxml)
> root = tree.getroot()
> saleslines = root.find('saleslines').findall('salesline')
> for e in saleslines[:]:
> if e.find('item').text not in valid_items:
> saleslines.remove(e)
> 
> tree.write(outputxml)
> 
> -- end xml_delete_test.py --
> 
> The above code runs without error, but simply writes the original file to
> disk.  The desired output would be:
> 
> -- fixed_order.xml 
> 
> 
> Bob
> 321 Main St
> 
> 
> D20
> 4
> 
> 
> BL5
> 7
> 
> 
> 
> 
> -- end fixed_order.xml 
> 
> What I find particularly confusing about the problem is that after running
> xml_delete_test.py in the Idle editor, if I go over to the shell and type
> saleslines, I can see that it's now a list of two elements.  I run the
> following:
> 
> for i in saleslines:
> print(i.find('item').text)
> 
> and I see that it's D20 and BL5, my two valid items.  Yet when I write
> tree out to the disk, it has the original four.  Do I need to refresh tree
> somehow?
> 
> Thanks!

First of all, thank you for this clear and complete problem description!

> saleslines = root.find('saleslines').findall('salesline')

Here findall()

returns a new list of matches which is completely independent of the element 
tree. Therefore

> saleslines.remove(e)

will remove the element e from this indepent list, and only from that.
To remove an element from the tree you have to know its parent, and then

parent_element.remove(child_element)

will actually modify the tree. 

In your case the parent is always , so you can restrict yourself 
to its children:

saleslines = root.find('saleslines')
for e in saleslines.findall('salesline'):
if e.find('item').text not in valid_items:
saleslines.remove(e)


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


[Tutor] Help

2019-03-20 Thread Eric Oh Yeah Yeah
How do I make Python 3 pick a random variable out of a set of variables I
give it?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help

2019-03-20 Thread Alan Gauld via Tutor

On 20/03/19 14:30, Eric Oh Yeah Yeah wrote:

How do I make Python 3 pick a random variable out of a set of variables I
give it?


There are several options but if you look in the random module you 
should find one that suits your particular needs.


choice() or randrange() may be good options.

If that's not enough of a hint come back with more specific details 
about what kind of "set of variables" you are using.


Alan G.


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


[Tutor] is there a graphics library for common tkinter Button functions?

2019-03-20 Thread Chris Roy-Smith

Hi,
Is there a "graphics library" of common button uses? that is things like 
forward record, back record, 1st record, last record, printer, save and 
the likes.
I don't have very artistic abilities, so would prefer to save making my 
own library.


Thank you
Chris Roy-Smith

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


Re: [Tutor] is there a graphics library for common tkinter Button functions?

2019-03-20 Thread Alan Gauld via Tutor

On 20/03/19 22:43, Chris Roy-Smith wrote:
Is there a "graphics library" of common button uses? that is things like 
forward record, back record, 1st record, last record, printer, save and 
the likes.


The short answer is no. But you can assign any bitmap image
to a button. (You can use other formats too but bitmaps are
easiest in my experience!)

But putting an image on the button does not give it any
functionality. You need to program that yourself.

HTH,

Alan G.

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