<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>coder . cl &#187; pyxser</title>
	<atom:link href="http://coder.cl/category/projects/pyxser/feed/" rel="self" type="application/rss+xml" />
	<link>http://coder.cl</link>
	<description>web developer &#38; system programmer</description>
	<lastBuildDate>Tue, 15 May 2012 10:46:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>instant xml api using pyxser</title>
		<link>http://coder.cl/2011/11/instant-xml-api-using-pyxser/</link>
		<comments>http://coder.cl/2011/11/instant-xml-api-using-pyxser/#comments</comments>
		<pubDate>Sat, 05 Nov 2011 13:42:24 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[pyxser]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=1915</guid>
		<description><![CDATA[Probably you do not understand pyxser at all. It is a serializer and deserializer which converts Python objects into XML as plain text. Among JSON and other formats, XML can help in some tasks like transmitting object through the network, for example building API calls using remote queries. Here I will guide you on how [...]]]></description>
			<content:encoded><![CDATA[<p>Probably you do not understand <a href='http://coder.cl/products/pyxser/' title='Python XML Serialization'>pyxser</a> at all. It is a serializer and deserializer which converts Python objects into XML as plain text. Among JSON and other formats, XML can help in some tasks like transmitting object through the network, for example building API calls using remote queries. Here I will guide you on how to build an XML query API for your Django driven application in few minutes. You just need to understand how <a href='http://coder.cl/products/pyxser/' title='Python XML Serialization'>pyxser</a> works and how to use the pyxser module. Remember that you can see the documentation once it is installed, even if you do not have Internet, just by running the pydoc daemon with <tt>pydoc -p 8080</tt> and connecting to the port <i>8080</i> in your machine &mdash; you can choose another port if it is not working.</p>
<p><span id="more-1915"></span></p>
<p><br/></p>
<h3>tl;dr</h3>
<p>You can setup a query API that throws XML through HTTP under Django using pyxser.</p>
<p><br/></p>
<h3>advice</h3>
<p>All examples here are working, you must be really careful with the authentication and object permissions before using the routines in this post. So, try to wrap those routines correctly using the Django authentication components to filter query requests. Probably OAuth related modules may help. Also the examples are not using the Python and Django best practices, so you need to adjust them to fit the best practices requirements. Finally, do not take all examples very literal, they are just examples and this is just a proof of concept article.</p>
<p><br/></p>
<h3>serializing model objects</h3>
<p>The <a href='http://coder.cl/products/pyxser/' title='Python XML Serialization'>pyxser</a> extension &mdash; which is written in C and uses libxml2 as its basis for XML processing &mdash; has two main arguments for the serialization routines: <tt>obj</tt> and <tt>enc</tt>, where <tt>obj</tt> is the object to be serialized and <tt>enc</tt> is the XML encoding to be used, so you can serialize a <i>valid</i> object using <tt>pyxser.serialize(obj = my_object, enc = 'utf-8')</tt>. You can see the full <a href='http://coder.cl/products/pyxser/' title='Python XML Serialization'>pyxser</a> documentation using the pydoc command and looking forward for the pyxser module.</p>
<p>To serialize Django models, you need to restrict some fields, so you need to filter them, you do not need to worry about processing each model field, you just need to worry to filter the model fields properly using the <tt>selector</tt> argument and the <tt>depth</tt> argument. Take a look on the following decorator.</p>
<pre class="brush: python;">

import pyxser as px

def render_to_xml(**pyxser_args):
    def outer(f):
        @wraps(f)
        def inner_xml(request, *args, **kwargs):

            result = f(request, *args, **kwargs)
            r = HttpResponse(mimetype='text/xml')
            try:
                render = px.serialize(obj=result,
                                      enc='utf-8',
                                      **pyxser_args)
            except Exception, exc:
                render = &quot;&lt;pyxs:obj/&gt;&quot;
            if result:
                r.write(render)
            else:
                r.write(&quot;&lt;pyxs:obj/&gt;&quot;)
            return r
        return inner_xml
    return outer
</pre>
<p>If you apply the decorator above in a Django view, it will return the serialized object as <tt>text/xml</tt> to the HTTP client. So, your view must return a valid object to be serialized by pyxser. It applies the <tt>pyxser.serialize</tt> function to the given output from your view. Now take a look to a view which uses this decorator to throw XML.</p>
<pre class="brush: python;">
def get_class(kls):
    try:
        parts = kls.split('.')
        module = ".".join(parts[:-1])
        m = __import__(module)
        for comp in parts[1:]:
            m = getattr(m, comp)
        return m
    except:
        return False

## use an URL as follows:
## (r'x/g/(?P&lt;model&gt;[w.]+)/(?P&lt;oid&gt;d+)/',
##  u'views_xml.get_model_object'),
@require_http_methods(["GET", "OPTIONS", "HEAD"])
@render_to_xml(selector=do_value_attrs, depth=2)
def get_model_object(request, model=None, oid=None):
    obj = object()
    try:
        db_model = get_class(model)
        obj = db_model.objects.get(pk=oid)
        return obj
    except Exception, exc:
        log.error(exc)
    return obj
</pre>
<p>The view above returns an object from the given model name <tt>model</tt> and the given primary key <tt>oid</tt>, and passes the <tt>do_value_attrs</tt> selector function as attribute selector to pyxser, and restrict the serialization <tt>depth</tt> to two levels. Remember that pyxser allows to serialize circular references and cross references between objects, so we need to restrict the serialization depth, in case of Django models we can work with 2 levels in almost all models and the field selector <tt>do_value_attrs</tt> can be defined as follows.</p>
<pre class="brush: python;">

DENIED_FIELDS = ['user', 'customer', 'users', 'customers']
DENIED_CLASSES = ['Related', 'Foreign', 'Many']

def is_allowed_class(fld):
    for nm in DENIED_CLASSES:
        if nm in fld.__class__.__name__:
            return False
    for nm in DENIED_FIELDS:
        if nm in fld.name:
            return False
    return True

def do_value_attrs(o):
    values = dict()
    if hasattr(o, '_meta') and hasattr(o._meta, 'fields'):
        for fldn in o._meta.fields:
            if is_allowed_class(fldn):
                values[fldn.name] = getattr(o, fldn.name)
    else:
        for kw in o.__dict__:
            values[kw] = getattr(o, kw)
    return values
</pre>
<p>Where we are filtering all fields in model objects that we do not want to serialize and all field classes that pyxser should not serialize for plain object transmision. Other objects which are not model related objects are serialized as plain Python objects using their dictionaries to get the object attributes, and also <tt>DENIED_FIELDS</tt> are skipped and <tt>DENIED_CLASSES</tt> are skipped too. The resulting XML for URLs like <i>/p/x/g/offer.models.Marca/1/</i> is as follows.</p>
<pre class="brush: xml;">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;pyxs:obj xmlns:pyxs="http://projects.coder.cl/pyxser/model/"
          version="1.0"
          type="Marca"
          module="prod.models"
          objid="id3128007116"&gt;
  &lt;pyxs:prop type="unicode" name="nombre" size="4"&gt;Sony&lt;/pyxs:prop&gt;
  &lt;pyxs:prop type="long" name="id"&gt;1&lt;/pyxs:prop&gt;
  &lt;pyxs:prop type="unicode" name="slug" size="4"&gt;sony&lt;/pyxs:prop&gt;
&lt;/pyxs:obj&gt;
</pre>
<p>The pyxser serialization model holds <i>type information</i>, so any serialized object carries <i>type information</i> to be user in deserialization tasks, then you can handle the object back in any machine supporting pyxser and get the object deserialized to its original class using the <tt>unserialize</tt> function.</p>
<p><br/></p>
<h3>defining object containers</h3>
<p>The pyxser extension cannot handle Django model containers directly, I mean those returned by the <tt>all</tt> method in query sets. So, you need to create a plain container to hold those objects that are retrieved from the database. Take a look on following view.</p>
<pre class="brush: python;">
class Container(object):
    count = 0
    items = []
    def __init__(self):
        pass

def collect_filters(qd):
    data = qd.copy()
    filters = dict()
    for kw in data:
        if kw.startswith('filter__'):
            name = kw.replace('filter__', '')
            filters[name] = data[kw]
    return filters

### use an URL as follows:
### (r'x/l/(?P&lt;model&gt;[w.]+)/(?P&lt;limit&gt;d+)/',
###  u'views_xml.get_model_list'),
@require_http_methods(["GET", "OPTIONS", "HEAD"])
@render_to_xml(selector=select_value_attrs, depth=4)
def get_model_list(request, model=None, limit=1):
    container = Container()
    container.count = 0
    container.items = []
    try:
        db_model = get_class(model)
        filters = collect_filters(request.GET)
        objs = db_model.objects.filter(**filters).all()[0:limit]
        container.count = len(objs)
        container.items = map(lambda x: x, objs)
        return container
    except Exception, exc:
        log.error(exc)
    return container
</pre>
<p>If you take a look carefully to this example, you will notice that we are using a very simple <tt>Container</tt> class to hold your objects. The resulting XML for the URL <i>/p/x/l/prod.models.Marca/5/?filter__nombre__contains=son</i> is as follows.</p>
<pre class="brush: xml;">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;pyxs:obj xmlns:pyxs="http://projects.coder.cl/pyxser/model/"
          version="1.0"
          type="Container"
          module="prod.views_xml"
          objid="id3107379372"&gt;
  &lt;pyxs:prop type="int" name="count"&gt;3&lt;/pyxs:prop&gt;
  &lt;pyxs:col type="list" name="items"&gt;
    &lt;pyxs:obj type="Marca" module="prod.models" objid="id3107380204"&gt;
      &lt;pyxs:prop type="unicode" name="nombre" size="5"&gt;Epson&lt;/pyxs:prop&gt;
      &lt;pyxs:prop type="long" name="id"&gt;10&lt;/pyxs:prop&gt;
      &lt;pyxs:prop type="unicode" name="slug" size="5"&gt;epson&lt;/pyxs:prop&gt;
    &lt;/pyxs:obj&gt;
    &lt;pyxs:obj type="Marca" module="prod.models" objid="id3107380300"&gt;
      &lt;pyxs:prop type="unicode" name="nombre" size="8"&gt;Ericsson&lt;/pyxs:prop&gt;
      &lt;pyxs:prop type="long" name="id"&gt;15&lt;/pyxs:prop&gt;
      &lt;pyxs:prop type="unicode" name="slug" size="8"&gt;ericsson&lt;/pyxs:prop&gt;
    &lt;/pyxs:obj&gt;
  &lt;/pyxs:col&gt;
&lt;/pyxs:obj&gt;
</pre>
<p>The resulting XML serialized object holds three Marca objects and all of them have their <tt>type</tt> to be deserialized once they are retrieved. If you want your objects to be deserialized back, you just need to use the <tt>pyxser.unserialize</tt> function properly, which is documented in the pyxser extension itself. I hope that you will like how pyxser works.</p>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2011. |
<a href="http://coder.cl/2011/11/instant-xml-api-using-pyxser/">Permalink</a> |
<a href="http://coder.cl/2011/11/instant-xml-api-using-pyxser/#comments">One comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2011/11/instant-xml-api-using-pyxser/&title=instant xml api using pyxser">del.icio.us</a>
<br/>
Post tags: <br/>
<br/>
<a href='http://creativecommons.org/licenses/by-nc-sa/3.0/' title='Creative Commons'><img src="http://coder.cl.qfl.wpcdn.arcostream.com/images/creative-commons.png" alt="Creative Commons"> Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://coder.cl/2011/11/instant-xml-api-using-pyxser/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>pyxser stats</title>
		<link>http://coder.cl/2011/10/pyxser-stats/</link>
		<comments>http://coder.cl/2011/10/pyxser-stats/#comments</comments>
		<pubDate>Sun, 30 Oct 2011 00:23:50 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[pyxser]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=1898</guid>
		<description><![CDATA[As you know I am the main developer &#8212; and unique developer, I hope more people will be interested in the future &#8212; of the pyxser project. It has a very specific purpose, but it is the only stand alone Python Object to XML serializer that exists. There is also the serialization routine built-in in [...]]]></description>
			<content:encoded><![CDATA[<p>As you know I am the main developer &mdash; and unique developer, I hope more people will be interested in the future &mdash; of the <i>pyxser</i> project. It has a very specific purpose, but it is the only stand alone Python Object to XML serializer that exists. There is also the serialization routine built-in in the <i>lxml</i> extension. The project is hosted on Source Forge, not popular as Git Hub does, but it is a well known project hosting site. The fact is that it is linked from sites and recommended along the Internet by some people. It provides a nice serialization model that allows you to standardise the XML serialization.</p>
<p><span id="more-1898"></span></p>
<p>It has been downloaded by many people from different sources where it is distributed, reaching about 8.000 downloads from different locations. That makes me very happy, because it is being tested and used widely in real world applications.</p>
<div class="figure">
<a href="http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/10/pyxser-stats.png" title="Pyxser Downloads Stats" target="_blank"><img src="http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/10/pyxser-stats.png" border="0" alt="Pyxser Downloads Stats"/></a></p>
<div>Pyxser Downloads Stats</div>
</div>
<p>Is a real pleasure to know that it is helping in the development of many applications. Also some other projects like <tt>jquery-validators</tt> it&#8217;s being downloaded over 3.000 times. I think that the best features of <i>pyxser</i>, is the fact it can do circular and cross referenced serialization without crashing. Remember that .NET and Java does not support circular and cross referenced XML serialization, so I have reached the right serialization model with <i>pyxser</i>. I have plans to migrate the serialization model and algorithm to other platform, like Java.</p>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2011. |
<a href="http://coder.cl/2011/10/pyxser-stats/">Permalink</a> |
<a href="http://coder.cl/2011/10/pyxser-stats/#comments">One comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2011/10/pyxser-stats/&title=pyxser stats">del.icio.us</a>
<br/>
Post tags: <br/>
<br/>
<a href='http://creativecommons.org/licenses/by-nc-sa/3.0/' title='Creative Commons'><img src="http://coder.cl.qfl.wpcdn.arcostream.com/images/creative-commons.png" alt="Creative Commons"> Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://coder.cl/2011/10/pyxser-stats/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>comparing pyxser and jsonx</title>
		<link>http://coder.cl/2011/04/comparing-pyxser-and-jsonx/</link>
		<comments>http://coder.cl/2011/04/comparing-pyxser-and-jsonx/#comments</comments>
		<pubDate>Sat, 30 Apr 2011 15:05:41 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[pyxser]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=1455</guid>
		<description><![CDATA[If you are a pyxser user, you may know that it uses a standard and structured serialization model. It was designed on January of 2009, and his target from that date until today is to generate a universal serialization model, so it can be ported to other languages easily, and maintain its more interesting features, [...]]]></description>
			<content:encoded><![CDATA[<p>If you are a <a href='http://coder.cl/products/pyxser/' title='Python XML Serialization, pyxser'>pyxser</a> user, you may know that it uses a standard and structured serialization model. It was designed on <a href='http://coder.cl/2009/01/pyxser-serialization-redesign-and-becoming-strict/' title='Initial pyxser Serialization Model'>January of 2009</a>, and his target from that date until today is to generate a universal serialization model, so it can be ported to other languages easily, and maintain its more interesting features, like <i>cross reference</i> and <i>circular reference</i> serialization capability.</p>
<p><span id="more-1455"></span></p>
<p>At other side, JSONx is an IBM standard that has as main purpose to build XML representation of JSON objects. It is using a model very similar to the <strong>pyxser</strong> model, but restricted to JSON supported data types, so it is not extensible like pyxser. It only supports objects, arrays, booleans, strings, numbers, and nulls. The pyxser side is quite simple, it has objects, collections and plain properties. Objects can hold other objects, collections and properties; collections can hold other objects and properties and properties can hold <i>only</i> plain text data types, but flexible enough to create a custom serialization and hold a Base 64 encoded binary string and similar stuff.</p>
<div class="figure">
<a href='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/04/JSONx.png' title='JSONx Structure' target='_blank'><img src='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/04/JSONx.png' border='0' alt='JSONx Structure' /></a></p>
<div>JSONx Structure</div>
</div>
<p>JSONx is more complex than pyxser. It is doing the same <u>mistakes</u> than other serializers, since it holds the <i>data type</i> on the XML element itself. Instead, pyxser is doing the task of placing the data type and namespace on separate attributes. The data type is stored on the <tt>type</tt> attribute and the namespace is stored on the <tt>module</tt> attribute. We can talk about JSONx as <i>domain specific</i> XML serialization, at the pyxser side, we can talk as <i>domain abstraction</i> serialization model.</p>
<div class="figure">
<a href='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/04/pyxser.png' title='PyXSer Structure' target='_blank'><img src='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/04/pyxser.png' border='0' alt='PyXSer Structure' /></a></p>
<div>JSONx Structure</div>
</div>
<p>The JSONx model is not available through public XML Schema, its XML Schema is held inside IBM products. You can search for the pyxser XML Schema here, on its public URL: <a href='http://projects.coder.cl/pyxser/dtd/pyxser-1.0.xsd'>pyxser XML Schema</a>, and the serialization model implementation is available for the Python language as Python Extension (a module written in C) and you can find it here: <a href='http://coder.cl/products/pyxser/' title='pyxser'>pyxser</a>. The JSONx reference can be found here: <a href='http://publib.boulder.ibm.com/infocenter/wsdatap/v3r8m1/index.jsp?topic=/xs40/convertingbetweenjsonandjsonx05.htm' title='JSONx'>JSONx</a>. Also you can find pyxser XML document definitions in the form of XML schemas and DTD, and it is distributed with the pyxser Python extension:</p>
<ul>
<li><a href='http://projects.coder.cl/pyxser/dtd/pyxser-1.0.xsd'>pyxser XML Schema.</a></li>
<li><a href='http://projects.coder.cl/pyxser/dtd/pyxser-1.0-c14n.xsd'>pyxser XML Schema, C14N Compatible.</a></li>
<li><a href='http://projects.coder.cl/pyxser/dtd/pyxser-1.0.dtd'>pyxser XML DTD.</a></li>
<li><a href='http://projects.coder.cl/pyxser/dtd/pyxser-1.0-c14n.dtd'>pyxser XML DTD, C14N Compatible.</a></li>
</ul>
<p>Understanding the pyxser XML Schema will drive you to understand its abstraction model. So, have fun implementing domain abstraction, instead of doing domain specific XML serializations.</p>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2011. |
<a href="http://coder.cl/2011/04/comparing-pyxser-and-jsonx/">Permalink</a> |
<a href="http://coder.cl/2011/04/comparing-pyxser-and-jsonx/#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2011/04/comparing-pyxser-and-jsonx/&title=comparing pyxser and jsonx">del.icio.us</a>
<br/>
Post tags: <br/>
<br/>
<a href='http://creativecommons.org/licenses/by-nc-sa/3.0/' title='Creative Commons'><img src="http://coder.cl.qfl.wpcdn.arcostream.com/images/creative-commons.png" alt="Creative Commons"> Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://coder.cl/2011/04/comparing-pyxser-and-jsonx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[ann] pyxser-1.5.2-r2.win32 was released</title>
		<link>http://coder.cl/2011/03/ann-pyxser-1-5-2-r2-win32-was-released/</link>
		<comments>http://coder.cl/2011/03/ann-pyxser-1-5-2-r2-win32-was-released/#comments</comments>
		<pubDate>Sat, 26 Mar 2011 17:55:47 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[c]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[pyxser]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=1397</guid>
		<description><![CDATA[Dear pyxser users, I&#8217;m pleased to announce that I have released pyxser-1.5.2r-r2.win32. This is the same release as 1.5.2-r2, but for Win32 platform. It does not add new features or similar stuff, it&#8217;s just a compiled binary distribution for Windows machines. You can download this release build for Python 2.7 and Win32 machines on SourceForge. [...]]]></description>
			<content:encoded><![CDATA[<p>Dear <a href="http://coder.cl/products/pyxser/">pyxser</a> users, I&#8217;m pleased to announce that I have released <b>pyxser-1.5.2r-r2.win32</b>. This is the same release as 1.5.2-r2, but for Win32 platform. It does not add new features or similar stuff, it&#8217;s just a compiled binary distribution for Windows machines. You can download this release build for Python 2.7 and Win32 machines on SourceForge. Please follow the following link to download the <a href='https://sourceforge.net/projects/pyxser/files/pyxser-1.5.2-r2/'>pyxser-1.5.2-r2.win32</a> package. It uses the Windows Installer and it&#8217;s an executable. You just need to download an official Python 2.7 distribution to use this release.</p>
<p><span id="more-1397"></span></p>
<p>Some notes on performance. Windows has a very poor performance compared to Linux and FreeBSD platforms. Please check the following plot that compares the performance on Windows, Linux and FreeBSD:</p>
<div class="figure">
<a href='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/03/profile-1.5.2.png?v=4.0' title='Compare pyxser-1.5.2 on Windows, Linux and FreeBSD'><img src='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/03/profile-1.5.2.png?v=4.0' alt='Compare pyxser-1.5.2 on Windows, Linux and FreeBSD' border='0' /></a></p>
<div>Compare pyxser-1.5.2 on Windows, Linux and FreeBSD</div>
</div>
<p>The performance on Windows is very poor. The effort required to migrate the source from POSIX platforms to Windows platforms is minimum. The difference the major effort was made on porting the setup script. My code is almost standard and it requires a C99 compiler, and usually I use the flags <tt>-Wall -Wextra -Wshadow -pedantic -std=c99</tt> to compile my sources, so I&#8217;m using very strict dialect. Seems that the Windows C compiler is C90 by default. No way to deal with the Windows compiler for some C keywords. I&#8217;m expecting to bring constant support for Win32 platform on future releases. I will be waiting your feedback :)</p>
<p>Some notes on the FreeBSD install, is the fact that it is a standard distribution, without kernel optimization and it is using distribution binaries &mdash; that means that everything is made for 80&#215;386 machines &mdash; and also it does not have any compiler optimization. Also, is well known that FreeBSD default binaries are slow, mainly because it lacks kernel optimization and standard C library optimizations. The libxml2 port on FreeBSD seems to be the problem, since it is not using the jemalloc library, and it is using the internal libxml2 memory allocator.</p>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2011. |
<a href="http://coder.cl/2011/03/ann-pyxser-1-5-2-r2-win32-was-released/">Permalink</a> |
<a href="http://coder.cl/2011/03/ann-pyxser-1-5-2-r2-win32-was-released/#comments">One comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2011/03/ann-pyxser-1-5-2-r2-win32-was-released/&title=[ann] pyxser-1.5.2-r2.win32 was released">del.icio.us</a>
<br/>
Post tags: <br/>
<br/>
<a href='http://creativecommons.org/licenses/by-nc-sa/3.0/' title='Creative Commons'><img src="http://coder.cl.qfl.wpcdn.arcostream.com/images/creative-commons.png" alt="Creative Commons"> Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://coder.cl/2011/03/ann-pyxser-1-5-2-r2-win32-was-released/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>[ann] pyxser-1.5.2r was released</title>
		<link>http://coder.cl/2011/01/ann-pyxser-1-5-2r-was-released/</link>
		<comments>http://coder.cl/2011/01/ann-pyxser-1-5-2r-was-released/#comments</comments>
		<pubDate>Sat, 08 Jan 2011 15:14:41 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[pyxser]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=1203</guid>
		<description><![CDATA[Dear pyxser users, I&#8217;m pleased to announce that I&#8217;ve released pyxser-1.5.2r. This release adds backport support for Python 2.4 and few algorithm enhancements. As you know this serializer supports cross references and circular references, and have a wide variety of serialization options for your object trasmition and storage tasks. Please feel free to send me [...]]]></description>
			<content:encoded><![CDATA[<p>Dear <a href="http://coder.cl/products/pyxser/">pyxser</a> users, I&#8217;m pleased to announce that I&#8217;ve released <b>pyxser-1.5.2r</b>. This release adds backport support for Python 2.4 and few algorithm enhancements. As you know this serializer supports cross references and circular references, and have a wide variety of serialization options for your object trasmition and storage tasks. Please feel free to send me your feedback and participate on the pyxser development. You can participate through the SourceForge mailing lists and forums. Also you can report bugs and send me your feedback through the SourceForge web site.</p>
<p><span id="more-1203"></span></p>
<p>The <i>ChangeLog</i> for this release is as follows:</p>
<pre>
1.5.2r (2011.01.08):

        Daniel Molina Wegener <dmw@coder.cl>

        * Added support for Python 2.4
        * Replaced the use of the commands package by the
        subprocess package on the setup script.
        * On the next release will be added support
        for Python 3.X ;)

        Thanks to pyxser users for their feedback.</pre>
<p><br/></p>
<h3>type backporting</h3>
<p>I&#8217;ve received an email from Juha Tuomala reporting incomapibility on pyxser to install it under Python 2.4. Also he has sent me a patch to make it work with Python 2.4. I&#8217;ve reviewed the patch and it was including some system macros, so it would be a little bit hard to include in the pyxser distribution. I&#8217;ve decided to study the problem about type backporting to make pyxser compatible with Python 2.4. The main problem was the <tt>Py_ssize_t</tt>, that is not available on Python 2.4, and used from Python 2.5 on the <tt>_PyTuple_Resize</tt> Python internal. The patch that Juha tried to send me was creating some macros to replace that type definition. But researching a little a about the <tt>Py_ssize_t</tt> type, I&#8217;ve found the <a href="http://www.python.org/dev/peps/pep-0353/">PEP353</a> that guides on how to backport the usage of the <tt>Py_ssize_t</tt> type.</p>
<pre name="code" class="c">
#if PY_VERSION_HEX &lt; 0x02050000 &amp;&amp; !defined(PY_SSIZE_T_MIN)
typedef int Py_ssize_t;
#define PY_SSIZE_T_MAX INT_MAX
#define PY_SSIZE_T_MIN INT_MIN
#endif
</pre>
<p>The preprocessor conditional block above can be used to backport the <tt>Py_ssize_t</tt> type without overwriting system types, and since it is a PEP, it should be the official way to backport that specific type. The other problem was about the <tt>PyAnySet_CheckExact</tt> macro. In this case, I&#8217;ve used the that portion of the patch directly, allowing me to backport some pyxser internal functions to Python 2.4</p>
<pre name="code" class="c">
#if PY_VERSION_HEX &lt; 0x02050000 &amp;&amp; !defined(PY_SSIZE_T_MIN)
#define Py_TYPE(ob)             (((PyObject*)(ob))->ob_type)
#define PyAnySet_CheckExact(ob)
        (Py_TYPE(ob) == &amp;PySet_Type || Py_TYPE(ob) == &amp;PyFrozenSet_Type)
#endif
</pre>
<p>I&#8217;ve used the same technique to detect the Python version and use that specific macro just for the required Python version. This is not affecting the future versions of Pyxser. For the next pyxser release, I&#8217;m planning to port it to Python 3.X. I hope that you will enjoy trasmitting and sending Python objects in XML format using the pyxser serializer ;)</p>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2011. |
<a href="http://coder.cl/2011/01/ann-pyxser-1-5-2r-was-released/">Permalink</a> |
<a href="http://coder.cl/2011/01/ann-pyxser-1-5-2r-was-released/#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2011/01/ann-pyxser-1-5-2r-was-released/&title=[ann] pyxser-1.5.2r was released">del.icio.us</a>
<br/>
Post tags: <br/>
<br/>
<a href='http://creativecommons.org/licenses/by-nc-sa/3.0/' title='Creative Commons'><img src="http://coder.cl.qfl.wpcdn.arcostream.com/images/creative-commons.png" alt="Creative Commons"> Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://coder.cl/2011/01/ann-pyxser-1-5-2r-was-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[ann] pyxser-1.5.1r was released</title>
		<link>http://coder.cl/2010/10/ann-pyxser-1-5-1r-was-released/</link>
		<comments>http://coder.cl/2010/10/ann-pyxser-1-5-1r-was-released/#comments</comments>
		<pubDate>Mon, 11 Oct 2010 16:25:47 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[c]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[pyxser]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=980</guid>
		<description><![CDATA[Dear pyxser users, I&#8217;m pleased to announce that I&#8217;ve released pyxser-1.5.1r. This release includes a new argument for deserialization functions, so you can skip the initialization construct by using cinit = False as argument, this improves performance, but leaves uninitiated objects, without calling its default constructor. The ChangeLog for this release is as follows: 1.5.1r [...]]]></description>
			<content:encoded><![CDATA[<p>Dear <a href="http://coder.cl/products/pyxser/"><strong>pyxser</strong></a> users, I&#8217;m pleased to announce that I&#8217;ve released <b>pyxser-1.5.1r</b>. This release includes a new argument for deserialization functions, so you can skip the initialization construct by using <tt>cinit = False</tt> as argument, this improves performance, but leaves uninitiated objects, without calling its default constructor.</p>
<p><span id="more-980"></span></p>
<p>The <i>ChangeLog</i> for this release is as follows:</p>
<pre>
1.5.1r (2010.10.11):

        Daniel Molina Wegener &lt;dmw@coder.cl&gt;

        * On all files: algorithms were optimized, the code
        was flattened applying "The Zen of Python" and the
        performance was enhanced in 10%.

        * Was added the cinit argument to deserialization
        functions, which control whether or not, the default
        constructor is called, instead of creating a raw
        instance of deserialized objects.

        Thanks to pyxser users for their feedback.
</pre>
<p>The <tt>cinit = False</tt> argument makes <strong>pyxser</strong> to skip the default constructor, calling <tt>PyInstance_NewRaw()</tt> instead of the default constructor. This skips the initialization code on the <tt>__init__()</tt> method. The impact on the performance &mdash; if your objects do not require of the default constructor &mdash; is as follows:</p>
<div class="figure">
<a href="http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2010/10/profile-1.5.1-cinit.svg" target="_blank"><img src="http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2010/10/profile-1.5.1-cinit.png" border="0" /></a>
</div>
<p>This enhancement is minimal, but saves time when you work with very simple objects. To take a look on the documentation, the <strong>pyxser</strong> extension is self documented, so you just need to call <tt>pydoc -p8181</tt>, connect your browser to <i>http://localhost:8181/</i> and search for the pyxser extension documentation.</p>
<p>I hope that you will enjoy this release :D</p>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2010. |
<a href="http://coder.cl/2010/10/ann-pyxser-1-5-1r-was-released/">Permalink</a> |
<a href="http://coder.cl/2010/10/ann-pyxser-1-5-1r-was-released/#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2010/10/ann-pyxser-1-5-1r-was-released/&title=[ann] pyxser-1.5.1r was released">del.icio.us</a>
<br/>
Post tags: <br/>
<br/>
<a href='http://creativecommons.org/licenses/by-nc-sa/3.0/' title='Creative Commons'><img src="http://coder.cl.qfl.wpcdn.arcostream.com/images/creative-commons.png" alt="Creative Commons"> Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://coder.cl/2010/10/ann-pyxser-1-5-1r-was-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[ann] pyxser-1.5r is available</title>
		<link>http://coder.cl/2010/08/ann-pyxser-1-5r-available/</link>
		<comments>http://coder.cl/2010/08/ann-pyxser-1-5r-available/#comments</comments>
		<pubDate>Wed, 25 Aug 2010 00:26:59 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[c]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[pyxser]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=892</guid>
		<description><![CDATA[Dear pyxser users, I&#8217;m pleased to announce that I&#8217;ve released pyxser-1.5r. This release has several bug fixes plus enhancements. pyxser now is 15% faster and do not have memory leaks. For people who do know pyxser, it is a Python Object to XML serializer and deserializer. You install pyxser, and you will have functions to [...]]]></description>
			<content:encoded><![CDATA[<p>Dear <a href="http://coder.cl/products/pyxser/">pyxser</a> users, I&#8217;m pleased to announce that I&#8217;ve released <b>pyxser-1.5r</b>. This release has several bug fixes plus enhancements. <strong>pyxser</strong> now is <i>15%</i> faster and do not have memory leaks. For people who do know <strong>pyxser</strong>, it is a <i>Python Object</i> to <i>XML</i> serializer and deserializer. You install pyxser, and you will have functions to convert Python Objects into XML and viceversa, convert that XML back into a <i>Python Object</i>.</p>
<p><span id="more-892"></span></p>
<p>This release is quite special, I&#8217;ve added lazy initialization of some resources, so it runs <i>15%</i> faster, and I&#8217;ve removed all memory leaks, so it just keeps using about <i>65MB</i> of RAM when is doing the <i>1,500,000.00</i> serialization and deserialization test. I&#8217;ve spent more time coding <strong>pyxser</strong> for this release. Just comparing the performance between the older release <i>1.4.6r</i> &mdash; a buggy release &mdash; with the current release <i>1.5r</i> we can see the performance enhancement as follows:</p>
<div class="figure">
<a href='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2010/08/profile-trunk-1.5r.svg?ver=2.0' target='_blank'><img src='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2010/08/profile-trunk-1.5r.png?ver=2.0' border='0' /></a>
</div>
<p>The memory usage was reduced, on what <i>object creation</i> refers, but not on certain tasks &mdash; remember that now I&#8217;m using lazy initialization for some resources &mdash; and we can see a very similar to previous releases plot on how the memory is handled:</p>
<div class="figure">
<a href='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2010/08/profile-memory-1.5r.svg?ver=2.0' target='_blank'><img src='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2010/08/profile-memory-1.5r.png?ver=2.0' border='0' /></a>
</div>
<p>The <i>memoization</i> has suffered some important changes. Now it uses <a href='http://docs.python.org/c-api/object.html#PyObject_Hash'>PyObject_Hash()</a> as object identification method. It&#8217;s qute slower than past revisions, but still it works faster using lazy initializations. Another important change made to the serialization algorithm, is the fact that now it is skipping <i>callable</i> objects, so function will not be serialized on any manner.</p>
<p>On the distribution now you will find on the <tt>test</tt> <del>folder</del> directory, some interesting tests, such as <i>utf-16</i> encoding test and <i>ascii</i> encoding test. All serializations now are using the Python embeded <i>Unicode</i> codecs for deserialization, that is not the case for serialization, it is using the <i>LibXML2</i> codec, so it can handle more encodings.</p>
<p>I must strongly thank to <strong>pyxser</strong> users for their feedback.</p>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2010. |
<a href="http://coder.cl/2010/08/ann-pyxser-1-5r-available/">Permalink</a> |
<a href="http://coder.cl/2010/08/ann-pyxser-1-5r-available/#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2010/08/ann-pyxser-1-5r-available/&title=[ann] pyxser-1.5r is available">del.icio.us</a>
<br/>
Post tags: <br/>
<br/>
<a href='http://creativecommons.org/licenses/by-nc-sa/3.0/' title='Creative Commons'><img src="http://coder.cl.qfl.wpcdn.arcostream.com/images/creative-commons.png" alt="Creative Commons"> Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://coder.cl/2010/08/ann-pyxser-1-5r-available/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>comparing pyxser &amp; .net serialization</title>
		<link>http://coder.cl/2010/08/comparing-pyxser-net-serialization/</link>
		<comments>http://coder.cl/2010/08/comparing-pyxser-net-serialization/#comments</comments>
		<pubDate>Tue, 17 Aug 2010 17:15:43 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[pyxser]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=862</guid>
		<description><![CDATA[Did you know about the InvalidOperationException on the System namespace on .NET?. Specifically, what happens on .NET serializations with cross referenced or circular referenced objects in .NET?. Object Oriented programming is complex, and objects can hold complex structures, including those kind of object references, but in .NET you have prohibited to use that kind of [...]]]></description>
			<content:encoded><![CDATA[<p>Did you know about the <tt>InvalidOperationException</tt> on the <tt>System</tt> namespace on <strong>.NET</strong>?. Specifically, what happens on <strong>.NET</strong> serializations with <i>cross referenced</i> or <i>circular referenced</i> objects in <strong>.NET</strong>?. Object Oriented programming is complex, and objects can hold complex structures, including those kind of object references, but in <strong>.NET</strong> you have prohibited to use that kind of object references, cross and circular ones, if you want to serialize an object. Instead of prohibition, <a href='http://coder.cl/products/pyxser/'><strong>pyxser</strong></a> &mdash; my Python-Object to XML serializer &mdash; allows you to create that kind of references, preserving its original reference across the serialization and deserialization process.</p>
<p><span id="more-862"></span></p>
<p>Let&#8217;s take a look on the <strong>.NET</strong> serialization model. It allows you to create custom XML elements and custom attributes, but does it works with complex objects?. The problem with the <strong>.NET</strong> serialization model is the fact that it can&#8217;t handle cross refereces or circular references, since the serialization model do not consider that kind of object references. If you try to serialize an object with that kind of reference, you will get a pretty exception like this:</p>
<pre>
Unhandled Exception: System.InvalidOperationException:
There was an error generating the XML document. --->
System.InvalidOperationException: A circular reference was
detected while serializing an object of type TestData.
   at System.Xml.Serialization.XmlSerializationWriter.WriteStartElement(String name,
      String ns, Object o, Boolean writePrefixed, XmlSerializerNamespaces xmlns)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterTestDa
</pre>
<p>Here is an example of cross referenced object in <strong>.NET</strong>, so you can verify yourself that you <strong>must not</strong> use that kind of references on <strong>.NET</strong>:</p>
<pre class='brush: csharp;'>
using System;
using System.Xml.Serialization;
using System.IO;

[XmlRoot(&quot;TestDataXml&quot;)]
public class TestData
{
    private int _Identity = 0;
    [XmlElement(&quot;Identity&quot;)]
    public int Identity
    {
        get { return this._Identity; }
        set { this._Identity = value; }
    }

    private string _Name = &quot;&quot;;
    [XmlElement(&quot;DataName&quot;)]
    public string Name
    {
        get { return this._Name; }
        set { this._Name = value; }
    }

    private string _IgnoreMe = &quot;&quot;;
    [XmlIgnore]
    public string IgnoreMe
    {
        get { return this._IgnoreMe; }
        set { this._IgnoreMe = value; }
    }

    private CrossData _CrossReference;
    [XmlElement(&quot;CircularReference&quot;)]
    public CrossData CrossReference
    {
        get { return this._CrossReference; }
        set { this._CrossReference = value; }
    }

    public TestData()
    {
    }

}

[XmlRoot(&quot;TestDataXml&quot;)]
public class CrossData
{
    private int _Identity = 0;
    [XmlElement(&quot;Identity&quot;)]
    public int Identity
    {
        get { return this._Identity; }
        set { this._Identity = value; }
    }

    private string _Name = &quot;&quot;;
    [XmlElement(&quot;DataName&quot;)]
    public string Name
    {
        get { return this._Name; }
        set { this._Name = value; }
    }

    private string _IgnoreMe = &quot;&quot;;
    [XmlIgnore]
    public string IgnoreMe
    {
        get { return this._IgnoreMe; }
        set { this._IgnoreMe = value; }
    }

    private TestData _CrossReference;
    [XmlElement(&quot;CircularReference&quot;)]
    public TestData CrossReference
    {
        get { return this._CrossReference; }
        set { this._CrossReference = value; }
    }

    public CrossData()
    {
    }

}

public class MainTest
{
    public static void Main(string[] args)
    {
        TestData test = new TestData();
        test.Identity = 1;
        test.Name = &quot;Cross Reference 1&quot;;
        test.IgnoreMe = &quot;Ignored&quot;;
        CrossData cross = new CrossData();
        cross.Identity = 2;
        cross.Name = &quot;Cross Reference 2&quot;;
        test.CrossReference = cross;
        cross.CrossReference = test;
        XmlSerializer serializer = new XmlSerializer(test.GetType());
        MemoryStream stream = new MemoryStream(512);
        serializer.Serialize(stream, test);
        Console.WriteLine(stream.ToString());
        stream.Close();
    }
}
</pre>
<p>And here is an example of circular referenced object in <strong>.NET</strong>, so you can verify yourself that you <strong>must not</strong> use that kind of references on <strong>.NET</strong>:</p>
<pre class='brush: csharp;'>
using System;
using System.Xml.Serialization;
using System.IO;

[XmlRoot(&quot;TestDataXml&quot;)]
public class TestData
{
    private int _Identity = 0;
    [XmlElement(&quot;Identity&quot;)]
    public int Identity
    {
        get { return this._Identity; }
        set { this._Identity = value; }
    }

    private string _Name = &quot;&quot;;
    [XmlElement(&quot;DataName&quot;)]
    public string Name
    {
        get { return this._Name; }
        set { this._Name = value; }
    }

    private string _IgnoreMe = &quot;&quot;;
    [XmlIgnore]
    public string IgnoreMe
    {
        get { return this._IgnoreMe; }
        set { this._IgnoreMe = value; }
    }

    private TestData _CircularRefence;
    [XmlElement(&quot;CircularReference&quot;)]
    public TestData CircularReference
    {
        get { return this._CircularRefence; }
        set { this._CircularRefence = value; }
    }

    public TestData()
    {
    }

}

public class MainTest
{
    public static void Main(string[] args)
    {
        TestData test = new TestData();
        test.Identity = 1;
        test.Name = &quot;Circular Reference&quot;;
        test.IgnoreMe = &quot;Ignored&quot;;
        test.CircularReference = test;
        XmlSerializer serializer = new XmlSerializer(test.GetType());
        MemoryStream stream = new MemoryStream(512);
        serializer.Serialize(stream, test);
        Console.WriteLine(stream.ToString());
        stream.Close();
    }
}
</pre>
<p>I really can&#8217;t stand with all this software quality on what <i>software design</i> refers. What happens in other levels of <strong>.NET</strong>, is that really safe as they claims?. Do they know about compiler construction techniques and which types of data can be encapsulated on <strong>XML</strong>. Possibly not&#8230; Here you have a simple example with <a href='http://coder.cl/products/pyxser/'><strong>pyxser</strong></a>, which does the both tasks, since the <strong>pyxser</strong> serialization model allows you to serialize and deserialize that kind of object refereces:</p>
<pre class='brush: python;'>
#!/usr/bin/env python
#
#
#

import pyxser
import testmod.testmod as t

class TestData():
    m1 = &quot;hola&quot;
    m2 = &quot;chao&quot;
    m3 = None
    def __init__(self):
        m1 = &quot;chao&quot;
        m2 = &quot;hola&quot;
        m3 = self

class TestCross():
    m1 = &quot;chao&quot;
    m2 = &quot;hola&quot;
    m3 = None
    def __init__(self):
        m1 = &quot;chao&quot;
        m2 = &quot;hola&quot;
        m3 = self

def main():
    x = t.TestData()
    x.m1 = &quot;hola&quot;
    x.m2 = &quot;chao&quot;
    x.m3 = x
    print x
    circular = pyxser.serialize(obj = x, enc = &quot;utf-8&quot;, depth = 50)
    print &quot;---8&lt;------ CIRCULAR REFERENCE ----8&lt;---&quot;
    print circular
    y = t.TestCross()
    y.m1 = &quot;chao&quot;
    y.m2 = &quot;hola&quot;
    y.m3 = x
    x.m3 = y
    cross = pyxser.serialize(obj = y, enc = 'utf-8', depth = 50)
    print &quot;---8&lt;------ CROSS REFERENCE ----8&lt;---&quot;
    print cross

if __name__ == &quot;__main__&quot;:
    main()
</pre>
<p>As you will see in the next piece of <strong>XML</strong>, the first serialization gives me the next result:</p>
<pre class='brush: xml;'>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;pyxs:obj xmlns:pyxs=&quot;http://projects.coder.cl/pyxser/model/&quot; version=&quot;1.0&quot; type=&quot;TestData&quot; module=&quot;testmod.testmod&quot; objid=&quot;id3077395276&quot;&gt;
  &lt;pyxs:prop type=&quot;str&quot; name=&quot;m1&quot;&gt;hola&lt;/pyxs:prop&gt;
  &lt;pyxs:obj module=&quot;testmod.testmod&quot; type=&quot;TestData&quot; name=&quot;m3&quot; objref=&quot;id3077395276&quot;/&gt;
  &lt;pyxs:prop type=&quot;str&quot; name=&quot;m2&quot;&gt;chao&lt;/pyxs:prop&gt;
&lt;/pyxs:obj&gt;
</pre>
<p>The second one brings me the next result:</p>
<pre class='brush: xml;'>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;pyxs:obj xmlns:pyxs=&quot;http://projects.coder.cl/pyxser/model/&quot; version=&quot;1.0&quot; type=&quot;TestCross&quot; module=&quot;testmod.testmod&quot; objid=&quot;id3077395180&quot;&gt;
  &lt;pyxs:prop type=&quot;str&quot; name=&quot;m1&quot;&gt;chao&lt;/pyxs:prop&gt;
  &lt;pyxs:obj module=&quot;testmod.testmod&quot; type=&quot;TestData&quot; name=&quot;m3&quot; objid=&quot;id3077395276&quot;&gt;
    &lt;pyxs:prop type=&quot;str&quot; name=&quot;m1&quot;&gt;hola&lt;/pyxs:prop&gt;
    &lt;pyxs:obj module=&quot;testmod.testmod&quot; type=&quot;TestCross&quot; name=&quot;m3&quot; objref=&quot;id3077395180&quot;/&gt;
    &lt;pyxs:prop type=&quot;str&quot; name=&quot;m2&quot;&gt;chao&lt;/pyxs:prop&gt;
  &lt;/pyxs:obj&gt;
  &lt;pyxs:prop type=&quot;str&quot; name=&quot;m2&quot;&gt;hola&lt;/pyxs:prop&gt;
&lt;/pyxs:obj&gt;
</pre>
<p>The solution is done using the proper <a href='http://www.w3.org/TR/REC-xml/#sec-attribute-types'>ID</a> and <a href='http://www.w3.org/TR/REC-xml/#sec-attribute-types'>IDREF</a> attributes on the <strong>pyxs:obj</strong> element. So you can serialize complex object trees without problems. There is no trick by using those kind of elements, and are present on markup languages from <a href='http://en.wikipedia.org/wiki/Standard_Generalized_Markup_Language'>SGML</a>, and anyone with a minimum knowledge about <a href='http://www.w3.org/XML/'>XML</a> knows about them. And this is <strong>only one point</strong> on a large set of <strong>points</strong> against <strong>.NET</strong> that makes me leave the development under that platform. Do not come here with your <del>pretty</del> <i>cool</i> story about <i>how nice</i> is the development under <strong>.NET</strong>.</p>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2010. |
<a href="http://coder.cl/2010/08/comparing-pyxser-net-serialization/">Permalink</a> |
<a href="http://coder.cl/2010/08/comparing-pyxser-net-serialization/#comments">3 comments</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2010/08/comparing-pyxser-net-serialization/&title=comparing pyxser &#038; .net serialization">del.icio.us</a>
<br/>
Post tags: <br/>
<br/>
<a href='http://creativecommons.org/licenses/by-nc-sa/3.0/' title='Creative Commons'><img src="http://coder.cl.qfl.wpcdn.arcostream.com/images/creative-commons.png" alt="Creative Commons"> Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://coder.cl/2010/08/comparing-pyxser-net-serialization/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>pyxser profiling</title>
		<link>http://coder.cl/2010/08/pyxser-profiling/</link>
		<comments>http://coder.cl/2010/08/pyxser-profiling/#comments</comments>
		<pubDate>Sat, 07 Aug 2010 20:34:01 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[c]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[pyxser]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=789</guid>
		<description><![CDATA[Today I was enhancing pyxser, and I&#8217;ve reduced some memory usage and enhanced its performance. I&#8217;ve reduced some functions to use less instructions and gained a little bit of better performance. But seems that still is using a large amount of dictionaries. or leaving them in memory until the test is finished. Also, I&#8217;ve modified [...]]]></description>
			<content:encoded><![CDATA[<p>Today I was enhancing <a href='http://coder.cl/products/pyxser/'><strong>pyxser</strong></a>, and I&#8217;ve reduced some memory usage and enhanced its performance. I&#8217;ve reduced some functions to use less instructions and gained a little bit of better performance. But seems that still is using a large amount of dictionaries. or leaving them in memory until the test is finished. Also, I&#8217;ve modified the profiling script, to run 1000 times each function. The enhancements looks promising&#8230;</p>
<p><span id="more-789"></span></p>
<p><br/></p>
<h3>memory profiling</h3>
<p>On the profiling script, the only remaining reference to constructed objects. Some interesting notes about the memory profiling task, is that the most allocated object type is <i>str</i> with 278 allocations, and is using the <i>47%</i> with <i>10384</i> bytes, but the heaviest object is <i>dictionary</i> with 79 allocations, the <i>13%</i> and <i>457720</i> bytes. I&#8217;ve tested <strong>pyxser</strong> using valgrind without any kind of true report of memory leaks.</p>
<div class="figure">
<a href='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2010/08/profile-memory.svg?ver=2.0'><img src='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2010/08/profile-memory.png?ver=2.0' border='0' alt='pyxser memory profiling' /></a>
</div>
<p>Also, in the pyxser distribution, you can find the <tt>test-utf8-leak.py</tt> script, which executes serialization and deserialization functions up to <i>1000000</i> times, leaving python executable just using a small portion of the operating system memory, and certainly you will notice that <strong>pyxser</strong> maintains its memory usage along that test.</p>
<p><br/></p>
<h3>speed tests</h3>
<p>Speed tests are going fine, I&#8217;ve reduced a small amount of the used by the serialized and created a new test that executes each pyxser function 1000 times. The result can be seen as follows:</p>
<div class="figure">
<a href='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2010/08/profile-trunk.svg?ver=2.2'><img src='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2010/08/profile-trunk.png?ver=2.2' border='0' alt='pyxser memory profiling' /></a>
</div>
<p>Deserialization functions are the slowest ones, requiring almost the double time that serialization takes, but thinking a little, you will notice that the XML parsing process is slower than Python object tree traversal. Some functions, like <tt>getdtd()</tt> and <tt>getdtdc14n()</tt> practically do not require time to being executed, since both DTDs are pre-allocated on module loading, the same applies to pyxser XML schemas. I think that I will reduce the execution and load time along the time, since pyxser has reached a good maturity level, without memory leaks and well structured tests, for different kinds of objects &mdash; as any lazy resource initialization.</p>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2010. |
<a href="http://coder.cl/2010/08/pyxser-profiling/">Permalink</a> |
<a href="http://coder.cl/2010/08/pyxser-profiling/#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2010/08/pyxser-profiling/&title=pyxser profiling">del.icio.us</a>
<br/>
Post tags: <br/>
<br/>
<a href='http://creativecommons.org/licenses/by-nc-sa/3.0/' title='Creative Commons'><img src="http://coder.cl.qfl.wpcdn.arcostream.com/images/creative-commons.png" alt="Creative Commons"> Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://coder.cl/2010/08/pyxser-profiling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[ann] pyxser-1.4.6r released</title>
		<link>http://coder.cl/2010/08/ann-pyxser-1-4-6r-released/</link>
		<comments>http://coder.cl/2010/08/ann-pyxser-1-4-6r-released/#comments</comments>
		<pubDate>Tue, 03 Aug 2010 16:38:08 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[c]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[pyxser]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=760</guid>
		<description><![CDATA[pyxser is a Python extension which holds functions to serialize and deserialize Python objects into XML. It&#8217;s one of my FOSS projects. Some characteristics of of this serializer are the fact that it can serialize objects with circular references and cross referenced objects &#8212; try to serialize an object with circular references in .NET. Other [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://coder.cl/products/pyxser/'><strong>pyxser</strong></a> is a Python extension which holds functions to serialize and deserialize Python objects into XML. It&#8217;s one of my FOSS projects. Some characteristics of of this serializer are the fact that it can serialize objects with circular references and cross referenced objects &mdash; try to serialize an object with circular references in .NET. Other facts is that it uses an <i>O(n)</i> algorithm, with <i>n</i> equal to the number of objects and not their references in the object tree. Today I&#8217;ve released <strong>pyxser-1.4.6r</strong>, let me show what I&#8217;ve done&#8230;</p>
<p><span id="more-760"></span></p>
<p><br/></p>
<h3>metrics</h3>
<p><strong>pyxser</strong> is complex. It requires some compiler construction techniques. The main one is <i>memoization</i>, which uses an internal and thread safe cache to allow the serializer to create unique XML instances of the <i>pyxs:obj</i> element. Also, it uses mutually recursive functions while is harvesting objects from the object tree using an preorder traversal tree algorithm. The result of implementing it, is the following table:</p>
<table width="80%" cellpadding="0" cellspacing="0" border="0" align="center">
<tr>
<th width="60%">metric</th>
<th width="20%" align="right">overall</th>
<th width="20%" align="right">per module</th>
</tr>
<tr>
<td>number of modules</td>
<td align="right">1</td>
<td align="right">&nbsp;&mdash;&nbsp;</td>
</tr>
<tr>
<td>lines of code</td>
<td align=right>5343</td>
<td align=right>5343</td>
</tr>
<tr>
<td>mccabe&#8217;s cyclomatic number</td>
<td align=right>1025</td>
<td align=right>1025</td>
</tr>
<tr>
<td>loc/com</td>
<td align=right>10.024</td>
<td align="right">&nbsp;&mdash;&nbsp;</td>
</tr>
<tr>
<td>mvg/com</td>
<td align=right>1.923</td>
<td align="right">&nbsp;&mdash;&nbsp;</td>
</tr>
</table>
<p>But there is a fact, <strong>pyxser is fast as hell!</strong>. It can do <i>10010</i> serializations in <i>1.992</i> seconds and <i>11011</i> deserializations in <i>6.910</i> seconds. So, I was introducing some enhancements and securing the code doing code harndening test and shorting code flow paths. Now the code looks flat and more elegant.</p>
<p><br/></p>
<h3>profiling</h3>
<p>After securing the code I got a slower pyxser implementation. So, I was impressed since I got to double the time on certain internal functions. Here is the metric on how code was executed:</p>
<div class="figure">
<a href='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2010/08/profile-4r-6rc1.svg?ver=2.2'><img src='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2010/08/profile-4r-6rc1.png?ver=2.2' border='0' /></a>
</div>
<p>But after some code cleanup, refactoring and code path shortening, I got much better results, and I&#8217;ve maintained the original perfomance with hardened, ordered and flat code. The reduction of nested statements was quite hard, but I&#8217;ve done it. It has just three levels as an average of nested statements:</p>
<div class="figure">
<a href='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2010/08/profile-4r-6r.svg?ver=2.2'><img src='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2010/08/profile-4r-6r.png?ver=2.2' border='0' /></a>
</div>
<p><br/></p>
<h3>enhancements</h3>
<p><strong>pyxser</strong> now can serialize, for example <i>SqlAlchemy</i> objects, sent them through the network and restore the instance so far away. For those kind of objects you must follow a simple rule, use the <i>selector</i> argument and the <i>depth</i> argument. The <strong>pyxser</strong> distribution comes with some test files, <tt>test-utf8-sqlalchemy.py</tt> is one of those files, where we define a class <i>User</i>:</p>
<pre class='brush: python;'>
class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    fullname = Column(String)
    password = Column(String)

    def __init__(self):
        self.name = None
        self.fullname = None
        self.password = None

    def get_set(self):
        return (self.name, self.fullname, self.password)

    def __repr__(self):
        return &quot;User('%s','%s', '%s')&quot; % self.get_set()
</pre>
<p>The first rule is quite simple: the constructor must be a simple constructor, without arguments, but it can hold initialization code, just remain it without arguments. And the second rule is to use the <i>selector</i> argument:</p>
<pre class='brush: python;'>
def sqlalch_filter(v):
    r = ((not callable(v[1]))
         and (not (v[0].startswith(&quot;_&quot;)))
         and ((v[0] != &quot;metadata&quot;)))
    return r

def sqlalch_selector(o):
    r = dict(filter(sqlalch_filter, inspect.getmembers(o)))
    return r

serialized = pyxser.serialize(obj = ed_user, enc = 'utf-8', selector = sqlalch_selector, depth = 2)
</pre>
<p>This will create a very simple object:</p>
<pre class='brush: xml;'>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;pyxs:obj xmlns:pyxs=&quot;http://projects.coder.cl/pyxser/model/&quot; version=&quot;1.0&quot; type=&quot;User&quot; module=&quot;testpkg.sample&quot; objid=&quot;id170543564&quot;&gt;
  &lt;pyxs:prop type=&quot;str&quot; name=&quot;fullname&quot;&gt;Ed Jones&lt;/pyxs:prop&gt;
  &lt;pyxs:prop type=&quot;str&quot; name=&quot;password&quot;&gt;password&lt;/pyxs:prop&gt;
  &lt;pyxs:prop type=&quot;str&quot; name=&quot;name&quot;&gt;ed&lt;/pyxs:prop&gt;
&lt;/pyxs:obj&gt;
</pre>
<p>But when it is restored on the end, it will remain intact.</p>
<p>Some people was saying that pyxser do not comes with documentation. Those people have not seen the documentation. <strong>pyxser</strong> as any Python extension, is self documented, this means that you must launch <i>pydoc</i> to read it. If you never have used pydoc, just try it: <tt>pydoc -p8080</tt>, this will create a small web server running on localhost to let you read the documentation. It has some other interesting parameters, others than <i>depth</i> and <i>selector</i>, such as <i>typemap</i>, which allows you to create custom serializations of certain objects &mdash; such as files.</p>
<p><br/></p>
<h3>where is located</h3>
<p>The project is hosted at SourceForge: <a href='https://sourceforge.net/projects/pyxser/'>pyxser@sourceforge</a> and the home page of the project is located <a href='http://coder.cl/products/pyxser/'>pyxser@coder.cl</a>.</p>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2010. |
<a href="http://coder.cl/2010/08/ann-pyxser-1-4-6r-released/">Permalink</a> |
<a href="http://coder.cl/2010/08/ann-pyxser-1-4-6r-released/#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2010/08/ann-pyxser-1-4-6r-released/&title=[ann] pyxser-1.4.6r released">del.icio.us</a>
<br/>
Post tags: <br/>
<br/>
<a href='http://creativecommons.org/licenses/by-nc-sa/3.0/' title='Creative Commons'><img src="http://coder.cl.qfl.wpcdn.arcostream.com/images/creative-commons.png" alt="Creative Commons"> Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)</a>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://coder.cl/2010/08/ann-pyxser-1-4-6r-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for ( 2e96b290f6c5ccd2bc6830557c0aba6e coder.cl/category/projects/pyxser/feed/ ) in 0.48918 seconds, on May 18th, 2012 at 12:16 pm UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on May 25th, 2012 at 12:16 pm UTC -->
