0

Program 1 : function based :-

import xml.etree.ElementTree as ET
import sys   
doc       = ET.parse("users.xml")
root      = doc.getroot()
root_new  = ET.Element("users") 
for child in root:
    username             = child.attrib['username']
    password             = child.attrib['password']   
    # create "user" here
    user    = ET.SubElement(root_new, "user") 
    user.set("username",username)               
    user.set("password",password) 
    #checking attribute for skipping KeyError
    if 'remote_access' in child.attrib:
        remote_access   = child.attrib['remote_access']
        user.set("remote_access",remote_access) 
    for g in child.findall("group"):
        # create "group" here
        group     = ET.SubElement(user,"group")  
        if g.text != "lion":
            group.text = g.text 
tree = ET.ElementTree(root_new)
tree.write(sys.stdout)

This is xml :-

<users>
<user username="admin"  password="admin" remote_access="yes"></user>
<user username="private_user1" password="user1" ><group>group1</group><group>group2</group></user>
<user username="private_user2" fullname="user2" password="user2"><group>group1</group><group>group2</group></user>
</users>

my class implementaion :- fully wrong :(

import xml.etree.ElementTree as ET
import sys

class users_detail (object):

    def __init__( self, xml_path ):
     """bla bla
     """
    try:
        doc = ET.parse("users.xml")
    except:
        print 'xml not found' 
        root      = doc.getroot() 
        root_new  = ET.Element("users") 
        for child in root:
            username             = child.attrib['username']
            password             = child.attrib['password']
            user    = ET.SubElement(root_new, "user") 
            user.set("username",username)               
            user.set("password",password) 
            if 'remote_access' in child.attrib:
                remote_access   = child.attrib['remote_access']
            for g in child.findall("group"):
                group     = ET.SubElement(user,"group")  
                if g.text != "lion":
                    group.text = g.text 
        tree = ET.ElementTree(root_new)
        tree.write(sys.stdout)
if __name__=='main':
    users_detail() 

How to implement this in better way via object oriented concept class. And my class implementaion is not working at all. please help me out. :(

i need to make the above code into a Python Class : thats the requirment :(

4
  • 2
    you don't seem to understand much about classes, i sugget you beagin with reading a book on the subject itmaybeahack.com/homepage/books/python.html , even if someone would implement it for you, it wouldn't be much use for you before you undestand the very basics. At the moment this class is utterly useless (even if it would work) Commented Nov 6, 2012 at 10:29
  • Who is that person ? who will implement :( any how thanks for the book Commented Nov 6, 2012 at 10:33
  • BTW, your "function implementation" is not anywhere near "function implementation" as well Commented Nov 6, 2012 at 13:34
  • You will have to learn a very basic thing of Python to et anything working from there - I suggest following some exercises from learnpythonthehardway.org before proceeding Commented Nov 6, 2012 at 13:35

1 Answer 1

2

You have an indentation problem (everything after the print statement is indented one level too far).

And tell your teacher from me that rewriting random code into a "class" is a nonsense requirement. There's no need for a class in Python for code like this.

Sign up to request clarification or add additional context in comments.

1 Comment

Teacher asking me, she need it in class :( what to do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.