Package: xmlrepr
Overviewxmlreprmodule is used to have a nice representations for objects in xml format.
so, instead of having representation like this<__main__.Foo object at 0x00000001>,
you can simply try this<Foo />. This package also could be used for complex representations:<Program name="main">
    <VarDecl name="x" type="INTEGER" />
    <VarDecl name="y" type="REAL" />
    <ProcDecl name="calc">
        <Param name="num" type="INTEGER" />
        <Block>
            <Assign left="y">
                <BinOp type="DIV">
                    <Var name="num" />
                    2
                </BinOp>
            </Assign>
        </Block>
    </ProcDecl>
    <Block>
    
    </Block>
</Program>installingthere are two other basic releases:0.0.60.0.7simple introductionfunctionparametersdocumentationstatusreprarg1, level=None, props=None, children=Noneseereprat0.0.7Deprecatedxmlreprname, props:dict=None, children:list=Nonerecursively builds xml representationsRecommendedxmlrepr(name, props=None, children=None)nameThe name of the xml tag. string value. e.g.propsDictionary represents properties of the tag.e.g.ifbool(props)gives False(it's None by default), no property will get displayed.e.g.Keys and values of props could be string or any object.if value in props is boolean value(True or False), will just display the key if True and omit them if False.e.g.if the key and value strings has'\n'in them, will got replaced byr'\n'.(this is a current fix to a bug)childrenA list of objects that are ready to be represented in xml format.UsageIn__str__or__repr__, just make call to this function and return the result.(no need for__xml__and its 'level' parameter)Why is it Recommended ?(And why is repr deprecated ?)xmlrepr module is intended to asmuch simpleas it could be. Having__xml__method and 'level' parameter wasn't a good thing.
Using__repr__and__str__instead of__xml__. Depending on recursion istead of 'level' parameter. making it easier for developers.xmlrepr function came toreplacerepr withless codeand dependencies. for example stop usingglobals()['__builtins__']['repr']thing.xmlrepr function isdeveloper friendly.xmlrepr hasfixed bugsthat are still in repr function.ExamplesLet's see some examples to understand the module easily.fromxmlreprimportxmlreprinput=xmlrepr('input',dict(name='text',type='text',value='welcome',required=True))span=xmlrepr('span')p=xmlrepr('p',0,[span])a=xmlrepr('a',dict(href='\n'),[p])print(xmlrepr('form',None,[input,a,"some text\nand lines"]))Output:<form>
    <input name="text" type="text" value="welcome" required />
    <a href="\n">
        <p>
            <span />
        </p>
    </a>
    some text 
    and lines
</form>Play around the code above to make sure you understand it.Source CodeHere is howxmlreprfunction is implemented.defxmlrepr(name,props=None,children=None):props=' '+' '.join('%s="%s"'%(key.replace('\n',r'\n'),value.replace('\n',r'\n'))ifvalue!=Trueelsekeyforkey,valueinprops.items()ifvalue!=False)ifpropselse''ifchildren:# regular tag -> recursionreturn"<{name}{props}>\n{indent}{inners}\n</{name}>".format(name=name,props=props,indent=' '*4,inners='\n'.join(str(child)forchildinchildren).replace('\n','\n'))else:# self-closing tag -> stop recursionreturn"<%s%s/>"%(name,props)