简体   繁体   English

实现类概念在python中不起作用

[英]Implementing class concept is not working in python

Program 1 : function based :- 计划1:基于功能: -

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 :- 这是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 :( 我需要将上面的代码变成Python类:这就是要求:(

You have an indentation problem (everything after the print statement is indented one level too far). 你有一个缩进问题( print语句之后的所有内容都缩进了一个级别)。

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. 对于像这样的代码,不需要Python中的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM