Implementing class concept is not working in python
<p>Program 1 : function based :-</p>

<pre><code>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)
</code></pre>

<p>This is xml :-</p>

<pre><code>&lt;users&gt;
&lt;user username="admin"  password="admin" remote_access="yes"&gt;&lt;/user&gt;
&lt;user username="private_user1" password="user1" &gt;&lt;group&gt;group1&lt;/group&gt;&lt;group&gt;group2&lt;/group&gt;&lt;/user&gt;
&lt;user username="private_user2" fullname="user2" password="user2"&gt;&lt;group&gt;group1&lt;/group&gt;&lt;group&gt;group2&lt;/group&gt;&lt;/user&gt;
&lt;/users&gt;
</code></pre>

<p>my class implementaion :- fully wrong :(</p>

<pre><code>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() 
</code></pre>

<p>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. :(</p>

<p>i need to make the above code into a Python Class : thats the requirment :(</p>
