<?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>Codigo Manso &#187; serialize</title>
	<atom:link href="http://www.codigomanso.com/es/tag/serialize/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codigomanso.com</link>
	<description>Programación, informática y tecnología</description>
	<lastBuildDate>Sun, 21 Aug 2011 10:54:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Serializando datos nativos en Python</title>
		<link>http://www.codigomanso.com/es/2010/09/serializing-native-data-variables-in-python/</link>
		<comments>http://www.codigomanso.com/es/2010/09/serializing-native-data-variables-in-python/#comments</comments>
		<pubDate>Tue, 21 Sep 2010 10:47:07 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[serialization]]></category>
		<category><![CDATA[serialize]]></category>
		<category><![CDATA[yaml]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=1026</guid>
		<description><![CDATA[Resulta que para un proyecto en Python en el que estoy trabajando necesitaba serializar algunas estructuras. El tema es que únicamente hacía falta serializar tipos nativos como listas, diccionarios, cadenas, enteros, etc&#8230;
Lo que me hacía falta era:

representación compacta
serialización/deserialización rápida
legible / editable por un humano

Así que después de pensar un rato, he encontrado 3 posibles soluciones [...]]]></description>
			<content:encoded><![CDATA[<p>Resulta que para un proyecto en Python en el que estoy trabajando necesitaba serializar algunas estructuras. El tema es que únicamente hacía falta serializar tipos nativos como listas, diccionarios, cadenas, enteros, etc&#8230;</p>
<p>Lo que me hacía falta era:</p>
<ul>
<li>representación compacta</li>
<li>serialización/deserialización rápida</li>
<li>legible / editable por un humano</li>
</ul>
<p>Así que después de pensar un rato, he encontrado 3 posibles soluciones para serializar datos arbitrarios:</p>
<ul>
<li>pickle/cPickle que serializa cualquier cosa, viene con python, pero no es legible/editable</li>
<li>simplejson que serializa en formato JSON (el resultado es legible y editable)</li>
<li>PyYAML que serializa en YAML (el resultado es legible y editable)</li>
</ul>
<p>El siguiente paso era coger un buen montón de datos, y hacer tests para ver que tal funcionan los encoders/decoders en cada uno de esos módulos. </p>
<p>A continuación puedes ver los gráficos de los tiempos de encoding/decoding para dos conjuntos de datos empleando cada uno de los formatos anteriores:</p>
<p><img src="http://chart.apis.google.com/chart?chxl=1:|YAML|JSON|pickle+raw|pickle+highest&#038;chxp=1,0.5,1.5,2.5,3.5&#038;chxr=1,0,4&#038;chxt=y,x&#038;chbh=a,4,0&#038;chs=500x320&#038;cht=bvs&#038;chco=3072F3,FF9900,FF9900&#038;chd=s:oBBB,,&#038;chdlp=l&#038;chtt=Python+Serialization+Speed+Test+(Tiny+Dataset)&#038;chts=000000,10.5" width="500" height="320" alt="Python Serialization Speed Test (Tiny Dataset)" /></p>
<p><img src="http://chart.apis.google.com/chart?chxl=1:|YAML|JSON|pickle+raw|pickle+highest&#038;chxp=1,0.5,1.5,2.5,3.5&#038;chxr=1,0,4&#038;chxs=0,676767,15.5,-0.5,l,676767&#038;chxt=y,x&#038;chbh=a,4,0&#038;chs=500x320&#038;cht=bvs&#038;chco=3072F3&#038;chd=s:5CBB&#038;chdlp=l&#038;chtt=Python+Serialization+Speed+Test+(Huge+Dataset)&#038;chts=000000,10.5" width="500" height="320" alt="Python Serialization Speed Test (Huge Dataset)" /></p>
<p>A parte de la velocidad, también me interesaba medir la longitud de las cadenas generadas (cuanto menos, mejor):</p>
<ul>
<li>yaml   tiny = 32604  // huge = 912912</li>
<li>json   tiny = 31305 // huge = 876540</li>
<li>pickle-raw    tiny = 34504 // huge = 986531</li>
<li>pickle-highest   tiny = 37541 // huge = 1101480</li>
</ul>
<h5>Conclusión:</h5>
<p>Como se puede ver en las gráficas, y en la tabla anterior, considero que JSON es el que me hace mejor papel.  Codifica / decodifica ligeramente más despacio que pickle (un 4% más despacio) pero la cadena resultante es legible y editable y además ocupa menos espacio que picke y que yaml.</p>
<p>Todos los tests han sido realizados con Python 2.5</p>
<h5>El código:</h5>
<p>Finalmente, aquí dejo un fragmento de código usado para obtener los tiempos que aparecen en los gráficos anteriores:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">    start = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>:
      yaml.<span style="color: black;">safe_load</span> <span style="color: black;">&#40;</span>yaml.<span style="color: black;">safe_dump</span> <span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">self</span>.<span style="color: black;">write</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;YAML TIME = %.2f<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - start<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    start = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>:
      JSONDecoder<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">decode</span> <span style="color: black;">&#40;</span>JSONEncoder<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">encode</span> <span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">self</span>.<span style="color: black;">write</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;JSON TIME = %.2f<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - start<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    start = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>:
      <span style="color: #dc143c;">pickle</span>.<span style="color: black;">loads</span> <span style="color: black;">&#40;</span><span style="color: #dc143c;">pickle</span>.<span style="color: black;">dumps</span> <span style="color: black;">&#40;</span>data, <span style="color: #dc143c;">pickle</span>.<span style="color: black;">HIGHEST_PROTOCOL</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">self</span>.<span style="color: black;">write</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PICKLE TIME = %.2f<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - start<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    start = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>:
      <span style="color: #dc143c;">pickle</span>.<span style="color: black;">loads</span> <span style="color: black;">&#40;</span><span style="color: #dc143c;">pickle</span>.<span style="color: black;">dumps</span> <span style="color: black;">&#40;</span>data, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">self</span>.<span style="color: black;">write</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PICKLE-RAW TIME = %.2f<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - start<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/es/2010/09/serializing-native-data-variables-in-python/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Extensión jQuery para serializar formularios</title>
		<link>http://www.codigomanso.com/es/2008/12/extension-jquery-para-serializar-formularios/</link>
		<comments>http://www.codigomanso.com/es/2008/12/extension-jquery-para-serializar-formularios/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 09:42:44 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programacion]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[checkbox]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[send]]></category>
		<category><![CDATA[serialize]]></category>
		<category><![CDATA[unchecked]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/?p=257</guid>
		<description><![CDATA[jQuery ya dispone de un método llamado serialize, que básicamente transforma un formulario en una cadena típica de un POST o un GET, bastante útil para enviar un formulario con una petición al más puro estilo AJAX tal cual lo hace el navegador.
El problema es que a mi no me gusta lo que hace el [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">jQuery ya dispone de un método llamado <strong>serialize</strong>, que básicamente transforma un formulario en una cadena típica de un POST o un GET, bastante útil para enviar un formulario con una petición al más puro estilo AJAX tal cual lo hace el navegador.</p>
<p style="text-align: justify;">El problema es que a mi no me gusta lo que hace el navegador con los &#8220;checkboxes&#8221;.</p>
<p style="text-align: justify;">Como cualquier otro input, los checkboxes tienen un valor asociado (por defecto &#8220;on&#8221;, aunque no estoy seguro de si esto varía según el navegador). Lo que uno esperaría de value, es que sea &#8220;on&#8221; si está checked y &#8220;off&#8221; si está unchecked. Pero no señores, no se confundan, siempre es &#8220;on&#8221;, igual que si le dices que value=&#8221;pepito&#8221;, value siempre será &#8220;pepito&#8221; independientemente de si el checkbox está checkeado o no.</p>
<p style="text-align: justify;">Entonces (se preguntará el lector avispado que aún no haya tenido el placer de programar en HTML), ¿como sabe el servidor si el checkbox está checked o unchecked? Elemental mi querido Watson. El navegador sólo envia esa información si está checked y no la envia si está unchecked. ¿Que putada, no? Porque hay veces que parece que esto rompe algunos flujos típicos con otros datos.</p>
<p style="text-align: justify;">No es que este comportamiento de los navegadores sea muy problematico, pero a veces a uno le interesa que funcione de otra manera.</p>
<p style="text-align: justify;">Sea como fuere, aquí pongo una extensión para jQuery, que básicamente lo que hace es lo mismo que &#8220;serialize&#8221; pero pone siempre los checkboxes, de tal forma que pone &#8220;<strong>nombre=1</strong>&#8221; cuando el checkbox está activado, y &#8220;<strong>nombre=0</strong>&#8221; cuando no está activado.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">jserialize</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> serialized <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">':checkbox'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> tofind    <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span> <span style="color: #339933;">+</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> toreplace <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">checked</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">'1'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'0'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">checked</span><span style="color: #009900;">&#41;</span>   <span style="color: #009900;">&#123;</span> serialized <span style="color: #339933;">=</span> serialized.<span style="color: #660066;">replace</span> <span style="color: #009900;">&#40;</span>tofind<span style="color: #339933;">,</span> toreplace<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">else</span>                <span style="color: #009900;">&#123;</span> serialized <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;&amp;amp;&quot;</span> <span style="color: #339933;">+</span> toreplace<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">return</span> serialized<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Enlaces de interés:</strong></p>
<ul>
<li><a href="http://www.codigomanso.com/2008/12/resetear-un-formulario-con-jquery/" target="_blank">Resetear un formulario con jQuery</a></li>
<li><a href="http://groups.google.com/group/jquery-en/browse_thread/thread/a73638a11636299d/ed6a56666cc8347a?#ed6a56666cc8347a" target="_blank">Pregunta de Badtant sobre porque serialize no envia los unchecked checkboxes</a></li>
<li><a href="http://www.jquery.com" target="_blank">Página oficial de jQuery</a></li>
<li><a href="http://docs.jquery.com/Ajax/serialize" target="_blank">Método jQuery.serialize</a></li>
<li><a href="http://docs.jquery.com/Ajax/serializeArray" target="_blank">Método jQuery.serializeArray</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/es/2008/12/extension-jquery-para-serializar-formularios/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

