Skip to main content

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)
  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)

Metadata

Release files for xmlrepr 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xmlrepr 0.1.0
File Size Uploaded
xmlrepr-0.1.0.tar.gz 4.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for xmlrepr 0.1.0
File Interpreter ABI Platform
xmlrepr-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.0 kB

Release files / xmlrepr-0.1.0.tar.gz

Download URL xmlrepr-0.1.0.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c4af104b87e60e5c5db3ba2873e270ac15099dedac9bfdccec2ce4b1255fac3a
BLAKE2b-256 checksum
How to use checksums
e1595db84c562482fea41d586047faca3ff441fda52d715bacc1e3aaef9f1dbb
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / xmlrepr-0.1.0-py3-none-any.whl

Download URL xmlrepr-0.1.0-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8e44284969313621f44df5f8ed640b00b6ae51cdc669ef7e95cadba69aa1b87e
BLAKE2b-256 checksum
How to use checksums
b7f38e93b5ba2630a5b7eba4e5e21f23b69b6dcf62e4baf32c05460e8befe802
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

0.0.4

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page