Skip to main content

module for XML representations

Project description

Overview

xmlrepr module 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>

installing

there are two other basic releases:

simple introduction

function parameters documentation status
repr arg1, level=None, props=None, children=None see repr at 0.0.7 Deprecated
xmlrepr name, props:dict=None, children:list=None recursively builds xml representations Recommended

xmlrepr(name, props=None, children=None)

name
  • The name of the xml tag. string value. e.g.
props
  • Dictionary represents properties of the tag. e.g.
  • if bool(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 by r'\n'. (this is a current fix to a bug)
children
  • A list of objects that are ready to be represented in xml format.

Usage

  • In __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 ?)

  1. xmlrepr module is intended to as much simple as 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.

  2. xmlrepr function came to replace repr with less code and dependencies. for example stop using globals()['__builtins__']['repr'] thing.

  3. xmlrepr function is developer friendly.

  4. xmlrepr has fixed bugs that are still in repr function.

Examples

Let's see some examples to understand the module easily.

from xmlrepr import xmlrepr

input = 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 Code

Here is how xmlrepr function is implemented.

def xmlrepr(name, props=None, children=None):
    props = ' '+' '.join(
        '%s="%s"' %(
            key.replace('\n', r'\n'),
            value.replace('\n', r'\n')
        ) if value != True else key
        for key, value in props.items()
        if value != False
    ) if props else ''
    if children:
        # regular tag -> recursion
        return "<{name}{props}>\n{indent}{inners}\n</{name}>".format(
            name=name,
            props=props,
            indent= ' '*4,
            inners= '\n'.join(
                str(child)
                for child in children
            ).replace('\n', '\n    ')
        )
    else:
        # self-closing tag -> stop recursion
        return "<%s%s />" %(name, props)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xmlrepr-0.1.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xmlrepr-0.1.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file xmlrepr-0.1.0.tar.gz.

File metadata

  • Download URL: xmlrepr-0.1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.3

File hashes

Hashes for xmlrepr-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c4af104b87e60e5c5db3ba2873e270ac15099dedac9bfdccec2ce4b1255fac3a
MD5 b9136141ebda20b1ece0ab5d442cd209
BLAKE2b-256 e1595db84c562482fea41d586047faca3ff441fda52d715bacc1e3aaef9f1dbb

See more details on using hashes here.

File details

Details for the file xmlrepr-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: xmlrepr-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.3

File hashes

Hashes for xmlrepr-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8e44284969313621f44df5f8ed640b00b6ae51cdc669ef7e95cadba69aa1b87e
MD5 e0e8f6812057c6d98ab50a2cd6f541c0
BLAKE2b-256 b7f38e93b5ba2630a5b7eba4e5e21f23b69b6dcf62e4baf32c05460e8befe802

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page