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 |
|---|---|---|---|
| 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 byr'\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 ?)
-
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. -
xmlrepr function came to replace repr with less code and dependencies. for example stop using
globals()['__builtins__']['repr']thing. -
xmlrepr function is developer friendly.
-
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c4af104b87e60e5c5db3ba2873e270ac15099dedac9bfdccec2ce4b1255fac3a
|
|
| MD5 |
b9136141ebda20b1ece0ab5d442cd209
|
|
| BLAKE2b-256 |
e1595db84c562482fea41d586047faca3ff441fda52d715bacc1e3aaef9f1dbb
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e44284969313621f44df5f8ed640b00b6ae51cdc669ef7e95cadba69aa1b87e
|
|
| MD5 |
e0e8f6812057c6d98ab50a2cd6f541c0
|
|
| BLAKE2b-256 |
b7f38e93b5ba2630a5b7eba4e5e21f23b69b6dcf62e4baf32c05460e8befe802
|