<?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; Programming</title>
	<atom:link href="http://www.codigomanso.com/en/category/programacion/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>Manso Hack: Speedup Google App Engine SDK SQLite Database</title>
		<link>http://www.codigomanso.com/en/2011/08/manso-trick-speedup-sqlite-database-in-google-app-engine-sd/</link>
		<comments>http://www.codigomanso.com/en/2011/08/manso-trick-speedup-sqlite-database-in-google-app-engine-sd/#comments</comments>
		<pubDate>Sun, 21 Aug 2011 10:54:29 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[Programacion]]></category>
		<category><![CDATA[google app engine]]></category>
		<category><![CDATA[truco manso]]></category>
		<category><![CDATA[gae]]></category>
		<category><![CDATA[hack manso]]></category>
		<category><![CDATA[mansohack]]></category>
		<category><![CDATA[mansotrick]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=1065</guid>
		<description><![CDATA[I was trying to initialize a local database using the Google App Engine SDK, and I was going crazy.
&#8211;use_sqlite parameter was not even solving my problem. Inserts on the database were really slow, like 10 per second. A nightmare.
OK, ok, it is an SDK, it emulates the server&#8230; it is intended to be used to [...]]]></description>
			<content:encoded><![CDATA[<p>I was trying to initialize a local database using the Google App Engine SDK, and I was going crazy.</p>
<p>&#8211;use_sqlite parameter was not even solving my problem. Inserts on the database were really slow, like 10 per second. A nightmare.</p>
<p>OK, ok, it is an SDK, it emulates the server&#8230; it is intended to be used to develop your application, speed is not the most important thing&#8230; I agree. But if you have to wait 10 minutes to insert 1000 data entries, then you can desperate. If they were like 1 million in 10 minutes, then I would understand, it would be still slow, but I will understand, but 1000 inserts? 10 minutes? are we all crazy?</p>
<p>Anyway, assuming SQLite seems to be a good database and works usually great, I though I could get that speed improved. Looking at the SQLite FAQ I found following question:</p>
<p><a href="http://www.sqlite.org/faq.html#q19" target="_blank"><strong>(19) INSERT is really slow &#8211; I can only do few dozen INSERTs per second</strong></a></p>
<p>Basically it seems SQLite should be fine doing 50,000 inserts per second in a normal computer, BUT to guarantee the data integrity it locks inserts until it makes sure they have been written to disk, so if your computer hangs your data remains safe.</p>
<p>These are good news because I don&#8217;t care of my local data in the local develoment environment, so in the event of a crash I can just rebuild it in a minute, instead of rebuild it in an hour.</p>
<p>Finally the solution is to use &#8220;<a href="http://www.sqlite.org/pragma.html#pragma_synchronous" target="_blank">PRAGMA synchronous=OFF</a>&#8220;, and the only way of using this pragma is to edit a line in de Google App Engine SDK, after initializing SQLite database</p>
<p>So you&#8217;ll have to edit following file:</p>
<p>google_appengine/google/appengine/datastore/datastore_sqlite_stub.py</p>
<p>Look for the &#8220;__init__&#8221; constructor and after the connection to the database gets initialized &#8220;self.__connection = sqlite3.connect [...]&#8221; you can place following statment:</p>
<p>self.__connection.execute(&#8216;PRAGMA synchronous=OFF&#8217;)</p>
<p>Changing that line I went from about 10 inserts/second to more than 100 inserts/second. More than a 10x gain for changing a single line. Great!</p>
<p>Now I can continue on the thing I was working on without getting crazy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2011/08/manso-trick-speedup-sqlite-database-in-google-app-engine-sd/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Manso Trick: Easy way to generate random hex string in Python</title>
		<link>http://www.codigomanso.com/en/2011/07/truco-manso-obtener-una-cadena-hexadecimal-aleatoria-en-python/</link>
		<comments>http://www.codigomanso.com/en/2011/07/truco-manso-obtener-una-cadena-hexadecimal-aleatoria-en-python/#comments</comments>
		<pubDate>Wed, 06 Jul 2011 07:22:53 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[Programacion]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[truco manso]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=1058</guid>
		<description><![CDATA[Generate a random string in hexadecimal is one of those things that you always have to do, from time to time.
As far as I know, the easiest way to do this in python is to import uuid module and run the following code:
uuid.uuid4().hex
With the call above we have 32 hexadecimal characters string (16 random bytes).
Do [...]]]></description>
			<content:encoded><![CDATA[<p>Generate a random string in hexadecimal is one of those things that you always have to do, from time to time.<br />
As far as I know, the easiest way to do this in python is to import uuid module and run the following code:<br />
<code>uuid.uuid4().hex</code></p>
<p>With the call above we have 32 hexadecimal characters string (16 random bytes).</p>
<p>Do you know any other approach?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2011/07/truco-manso-obtener-una-cadena-hexadecimal-aleatoria-en-python/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>MansoTrick: Convert 24h time string to 12h AM/PM format (Python)</title>
		<link>http://www.codigomanso.com/en/2011/05/trucomanso-transformar-el-tiempo-en-formato-24h-a-formato-12h-python/</link>
		<comments>http://www.codigomanso.com/en/2011/05/trucomanso-transformar-el-tiempo-en-formato-24h-a-formato-12h-python/#comments</comments>
		<pubDate>Sat, 28 May 2011 18:13:24 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[truco manso]]></category>
		<category><![CDATA[appengine]]></category>
		<category><![CDATA[formatting]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=1052</guid>
		<description><![CDATA[Manso trick to convert from 24h time format to 12h (AM/PM) time format in Python

def ampmformat (hhmmss):
  """
    This method converts time in 24h format to 12h format
    Example:   "00:32" is "12:32 AM"
              [...]]]></description>
			<content:encoded><![CDATA[<p>Manso trick to convert from 24h time format to 12h (AM/PM) time format in Python</p>
<pre><code>
def ampmformat (hhmmss):
  """
    This method converts time in 24h format to 12h format
    Example:   "00:32" is "12:32 AM"
               "13:33" is "01:33 PM"
  """
  ampm = hhmmss.split (":")
  if (len(ampm) == 0) or (len(ampm) > 3):
    return hhmmss

  # is AM? from [00:00, 12:00[
  hour = int(ampm[0]) % 24
  isam = (hour >= 0) and (hour < 12)

  # 00:32 should be 12:32 AM not 00:32
  if isam:
    ampm[0] = ('12' if (hour == 0) else "%02d" % (hour))
  else:
    ampm[0] = ('12' if (hour == 12) else "%02d" % (hour-12))

  return ':'.join (ampm) + (' AM' if isam else ' PM')
</code></pre>
<p>Now some examples of the returned values:</p>
<pre><code>ampmformat ("00:00:00") # returns "12:00:00 AM"
ampmformat ("12:00:00") # returns "12:00:00 PM"

ampmformat ("01:23:45") # returns "01:23:45 AM"
ampmformat ("13:23:45") # returns "01:23:45 PM"
ampmformat ("05:43:21") # returns "05:43:21 AM"
ampmformat ("17:43:21") # returns "05:43:21 PM"

ampmformat ("11:59:59") # returns "11:59:59 AM"
ampmformat ("23:59:59") # returns "11:59:59 PM"
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2011/05/trucomanso-transformar-el-tiempo-en-formato-24h-a-formato-12h-python/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Manso Trick: Getting the latitude/longitude from an address</title>
		<link>http://www.codigomanso.com/en/2010/11/truco-manso-obtener-la-latitudlongitud-a-partir-de-una-localizacion-o-direccion/</link>
		<comments>http://www.codigomanso.com/en/2010/11/truco-manso-obtener-la-latitudlongitud-a-partir-de-una-localizacion-o-direccion/#comments</comments>
		<pubDate>Sat, 13 Nov 2010 11:27:32 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[Programacion]]></category>
		<category><![CDATA[truco manso]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[geocoding]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[latitude]]></category>
		<category><![CDATA[longitude]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=1038</guid>
		<description><![CDATA[For a project soon to be born (I hope), I&#8217;ve been doing a quick research for a way to obtain the latitude and longitude from a geographical address. For example, to determine the latitude/longitude from a totally random address like &#8220;Emerson Street, Palo Alto, California&#8221;  BTW, Hi Mariano! Thanks for the stay in USA last [...]]]></description>
			<content:encoded><![CDATA[<p>For a project soon to be born (I hope), I&#8217;ve been doing a quick research for a way to obtain the latitude and longitude from a geographical address. For example, to determine the latitude/longitude from a totally random address like &#8220;Emerson Street, Palo Alto, California&#8221;  BTW, Hi Mariano! Thanks for the stay in USA last year <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>This is called geocoding and after spending half a second looking searching on internet I&#8217;ve found two simple REST APIs. Yahoo! and Google provide their own APIs and I&#8217;m sure a lot of companies out there make use of it, because of their accuracy.</p>
<p>If you want to use Yahoo! API you need to sign up so they give you some sort of appid and you can start making queries.</p>
<p>In the other hand, Google let&#8217;s you do this as easily as:</p>
<p><a href="http://maps.googleapis.com/maps/api/geocode/json?address=Emerson+Street,+Palo+Alto,+california&amp;sensor=false">http://maps.googleapis.com/maps/api/geocode/json?address=Emerson+Street,+Palo+Alto,+california&amp;sensor=false</a></p>
<p>This URL returns a JSON response which can be easily parsed in all languages (you can also ask for the XML, but I sort of hate XML so I prefer JSON).</p>
<p>Apart from returning the latitude and longitude, it also returns the formatted address, which can be great to normalize the address in your database, in case you need such thing. For example, for the query before, it says the formatted address is:</p>
<p>&#8220;Emerson St, Palo Alto, CA, USA&#8221;</p>
<p>On top of that, you should always read the Terms Of Service to make sure your app stays aligned with the fair use of the API, but that&#8217;s up to you.</p>
<p>Although more inacurate, another interesting project is GeoNames (see the URL below).</p>
<p>For more information:</p>
<ul>
<li>Google Geocoding API:<br />
<a href="http://code.google.com/apis/maps/documentation/geocoding/index.html">http://code.google.com/apis/maps/documentation/geocoding/index.html</a></li>
<li>Yahoo! Place Finder:<br />
<a href="http://developer.yahoo.com/geo/placefinder/">http://developer.yahoo.com/geo/placefinder/</a></li>
<li>GeoNames geographical database:<br />
<a href="http://www.geonames.org/">http://www.geonames.org/</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2010/11/truco-manso-obtener-la-latitudlongitud-a-partir-de-una-localizacion-o-direccion/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Unicode support in Python: Frustrations and Solutions</title>
		<link>http://www.codigomanso.com/en/2010/09/unicode-support-in-python-frustrations-and-solutions/</link>
		<comments>http://www.codigomanso.com/en/2010/09/unicode-support-in-python-frustrations-and-solutions/#comments</comments>
		<pubDate>Tue, 21 Sep 2010 23:49:55 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[frustracion]]></category>
		<category><![CDATA[pesadillas]]></category>
		<category><![CDATA[unicode]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=1032</guid>
		<description><![CDATA[To be honest, I think the support of unicode in Python before Python 3, being totally honest, is a f*knig nightmare. I prefer PHP 5.x support for unicode (= zero support).
One can get just crazy, sometimes things work, sometimes things crash somewhere unexpectly, or they don&#8217;t really crash, they just crash when you want to [...]]]></description>
			<content:encoded><![CDATA[<p>To be honest, I think the support of unicode in Python before Python 3, being totally honest, is a f*knig nightmare. I prefer PHP 5.x support for unicode (= zero support).</p>
<p>One can get just crazy, sometimes things work, sometimes things crash somewhere unexpectly, or they don&#8217;t really crash, they just crash when you want to show some string on the console doing a print&#8230; </p>
<p>Anyway, trying to solve my frustrations with unicode strings in Python I found a link that explains lots of things and helped me a lot:</p>
<p><a href="http://packages.python.org/kitchen/unicode-frustrations.html">Overcoming unicode frustrations in Python 2</a></p>
<p>The previous link is a must read for people getting UnicodeEncodeError exceptions, like:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">UnicodeEncodeError: <span style="color: #ff0000;">'ascii'</span> codec can<span style="color: #ff0000;">'t encode character u'</span>\xe1<span style="color: #ff0000;">' in position 654: ordinal not in range(128)</span></pre></div></div>

<p>Finally, my hope is that Python 3 solves all the evil in the world of the unicode&#8230; but I need to see it working to truly believe what I&#8217;ve been reading so far&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2010/09/unicode-support-in-python-frustrations-and-solutions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Serializing native data in Python</title>
		<link>http://www.codigomanso.com/en/2010/09/serializing-native-data-variables-in-python/</link>
		<comments>http://www.codigomanso.com/en/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[For a project I&#8217;m currently working on I needed to serialize some structures in Python. The only premise is that they were only made with basic/native Python types, such as lists, dicts, strings, integers, &#8230;

The thinkgs I was considering were:

compact representation
fast serialization/deserialization
human-readable / human-editable a must

So after thinking for a while, I found 3 possible [...]]]></description>
			<content:encoded><![CDATA[<p>For a project I&#8217;m currently working on I needed to serialize some structures in Python. The only premise is that they were only made with basic/native Python types, such as lists, dicts, strings, integers, &#8230;
</p>
<p>The thinkgs I was considering were:</p>
<ul>
<li>compact representation</li>
<li>fast serialization/deserialization</li>
<li>human-readable / human-editable a must</li>
</ul>
<p>So after thinking for a while, I found 3 possible solutions to serialize arbitrary data:</p>
<ul>
<li>pickle/cPickle module for serializing in the internal pickle format which comes with python by default</li>
<li>simplejson for serializing in JSON format which is human readable</li>
<li>PyYAML for serializing in YAML format which is human readable</li>
</ul>
<p>The next step was to get a huge amount of data, and do the tests for both speed and length of the resulting data. Following you can appreciate the speed results for two data sets:</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>The bars above represent the time in seconds it took to encode/decode the dataset 100 times (although numbers go from 0 to 100, are absolute times, not percentages).</p>
<p>Apart from the speed, I also measured the lenght of the serialized data obtained:</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>Conclussion:</h5>
<p>According to the results, JSON serialization/deserialization is the best choice. It encodes/decodes slightly slower than pickle module on huge datasets (we are talking about 4% slower, not that much), and the resulting string is not only human readable, but the resulting output is smaller than pickle and yaml encoded strings.</p>
<p>All tests were made on Python 2.5</p>
<h5>The code</h5>
<p>This is a fragment of the code used to get the speed results:</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/en/2010/09/serializing-native-data-variables-in-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>bbcodeutils: BBCode parser and BBCode to HTML for Python</title>
		<link>http://www.codigomanso.com/en/2010/09/bbcodeutils-bbcode-parser-and-bbcode-to-html-for-python/</link>
		<comments>http://www.codigomanso.com/en/2010/09/bbcodeutils-bbcode-parser-and-bbcode-to-html-for-python/#comments</comments>
		<pubDate>Wed, 15 Sep 2010 11:54:00 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[Programacion]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[bbcode]]></category>
		<category><![CDATA[bbcode2html]]></category>
		<category><![CDATA[parser]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=947</guid>
		<description><![CDATA[Yesterday I was looking for a Python module able to parse Bulletin Board Code (bbcode for friends) or able to transform bbcode to HTML.
After looking on the Internet I decided to create bbcodeutils a Python module to parse, generate and transform bbcode. I created in a way that I think is really simple to use.
Inside [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Yesterday I was looking for a Python module able to parse Bulletin Board Code (bbcode for friends) or able to transform bbcode to HTML.</p>
<p style="text-align: justify;">After looking on the Internet I decided to create <strong>bbcodeutils</strong> a Python module to parse, generate and transform bbcode. I created in a way that I think is really simple to use.</p>
<p>Inside <b>bbcodeutils</b> you will find following classes:</p>
<ul style="text-align: justify;">
<li><strong>bbcodeparser</strong>: parses bbcode and fixes invalid strings</li>
<li><strong>bbcode2html</strong>: gets a parsed bbcode string and generates HTML from it</li>
<li><strong>bbcodebuilder</strong>: good to generate BBCode programmatically</li>
</ul>
<p style="text-align: justify;">Here you can find the link to download <a href="http://www.codigomanso.com/archives/python/bbcodeutils-v1.0.tgz">bbcodeutils v1.0</a>. You can use this module freely (see readme.txt for details).
</p>
<p style="text-align: justify;">Following there are some examples with the most common operations.</p>
<h5>Examples:</h5>
<p>Before you try any example I&#8217;m assuming you will do something to load the bbcodeutils module, like:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> bbcodeutils <span style="color: #ff7700;font-weight:bold;">import</span> bbcodebuilder, bbcodeparser</pre></div></div>

<h6>Parsing bbcode strings</h6>
<p>Parsing a bbcode string can be achieved using the <b>bbcodeparser</b> constructor, or calling <b>parse</b> method on a bbcodeparser object:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> bbcode = bbcodeparser<span style="color: black;">&#40;</span><span style="color: #483d8b;">'[b]bold string[/b]'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>or:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> bbcode = bbcodeparser<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;</span> bbcode.<span style="color: black;">parse</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">'[b]first string[/b]'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This is just to illustrate how to parse a string, but this won&#8217;t produce any output at all. What you are probably looking for is to fix an invalid bbcode string or to transform a bbcode string into valid HTML. Here we go&#8230;</p>
<h6>Fix an invalid BBCode string</h6>
<p>To fix an invalid bbcode string you just have to parse a string and stringify the parser object or to call the <b>bbcode</b> method.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>bbcode<span style="color: black;">&#41;</span>
<span style="color: #483d8b;">&quot;This is [b]bold and [i]this bold and italics[/i][/b].&quot;</span>
&nbsp;
<span style="color: #66cc66;">&gt;</span> bbcode = bbcodeparser<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;This is [b]bold and [i]this bold and italics[/b].[/color]&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;</span> bbcode.<span style="color: black;">bbcode</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #483d8b;">&quot;This is [b]bold and [i]this bold and italics[/i][/b].&quot;</span></pre></div></div>

<p>Please note that the [/color] tag is removed and the closing [/i] tag is added properly.</p>
<h6>Transform bbcode to HTML</h6>
<p>The class bbcodeparser implements <b>html</b> method which uses <b>bbcode2html</b> internally. Calling this function will return valid HTML code.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> bbcode = bbcodeparser<span style="color: black;">&#40;</span><span style="color: #483d8b;">'[b]bold string[/b]'</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;</span> bbcode.<span style="color: black;">html</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #483d8b;">&quot;&lt;b&gt;bold string&lt;/b&gt;&quot;</span></pre></div></div>

<h6>Create BBCode programmatically</h6>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> bbcode = bbcodebuilder<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;</span> bbcode.<span style="color: black;">b</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'string in bold'</span><span style="color: black;">&#41;</span>
<span style="color: #483d8b;">&quot;[b]string in bold[/b]&quot;</span>
&nbsp;
<span style="color: #66cc66;">&gt;</span> bbcode.<span style="color: black;">url</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://www.codigomanso.com/'</span><span style="color: black;">&#41;</span>
<span style="color: #483d8b;">&quot;[url]http://www.codigomanso.com/[/url]&quot;</span>
&nbsp;
<span style="color: #66cc66;">&gt;</span> bbcode.<span style="color: black;">url</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Welcome to Codigo Manso'</span>, <span style="color: #483d8b;">'http://www.codigomanso.com/'</span><span style="color: black;">&#41;</span>
<span style="color: #483d8b;">&quot;[url=http://www.codigomanso.com/]Welcome to Codigo Manso[/url]&quot;</span>
&nbsp;
<span style="color: #66cc66;">&gt;</span> bbcode.<span style="color: #008000;">list</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'item 1'</span>, <span style="color: #483d8b;">'item 2'</span><span style="color: black;">&#41;</span>
<span style="color: #483d8b;">&quot;[list]
   [*]item 1
   [*]item 2
[/list]&quot;</span></pre></div></div>

<h6>Common operations in one line</h6>
<p>Fix invalid BBCode in one line:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>bbcodeparser<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;[color=red]Fix this [b]string&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #483d8b;">&quot;[color=red]Fix this [b]string[/b][/color]&quot;</span></pre></div></div>

<p>Transform BBCode to HTML in one line:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>bbcodeparser<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;[color=red]Fix this [b]string&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #483d8b;">'&lt;span style=&quot;color: red;&quot;&gt;Fix this &lt;b&gt;string&lt;/b&gt;&lt;/span&gt;'</span></pre></div></div>

<p>Thats it!</p>
<p>If you have any doubt feel free to leave a comment on this post</p>
<p>Enlaces de interés:</p>
<ul>
<li><a href="http://www.codigomanso.com/archives/python/bbcodeutils-v1.0.tgz">bbcodeutils v1.0 for Python</a></li>
<li><a href="http://www.bbcode.org/reference.php">Referencia de BBCode</a></li>
<li><a href="http://en.wikipedia.org/wiki/BBCode">Entrada sobre BBCode en la Wikipedia</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2010/09/bbcodeutils-bbcode-parser-and-bbcode-to-html-for-python/feed/</wfw:commentRss>
		<slash:comments>282</slash:comments>
		</item>
		<item>
		<title>[SOLVED] Add Unique Constraints to Google App Engine databases</title>
		<link>http://www.codigomanso.com/en/2010/09/solved-anadir-claves-unicas-en-google-app-engine-en-3-lineas/</link>
		<comments>http://www.codigomanso.com/en/2010/09/solved-anadir-claves-unicas-en-google-app-engine-en-3-lineas/#comments</comments>
		<pubDate>Fri, 10 Sep 2010 15:44:31 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[google app engine]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[db]]></category>
		<category><![CDATA[gae]]></category>
		<category><![CDATA[gql]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=931</guid>
		<description><![CDATA[The problem:
Google App Engine rules! The truth is that I&#8217;m starting to feel confortable programming in Python, although I still like the curly braces to indentate.
Anyway, the datastore used by Google is superpowerful and supersimple to use, but it has some limitations. With the App Engine SDK you can easily say which attributes you want [...]]]></description>
			<content:encoded><![CDATA[<h5>The problem:</h5>
<p>Google App Engine rules! The truth is that I&#8217;m starting to feel confortable programming in Python, although I still like the curly braces to indentate.</p>
<p>Anyway, the datastore used by Google is superpowerful and supersimple to use, but it has some limitations. With the App Engine SDK you can easily say which attributes you want to index (indexed = True), which ones you want to be required (required = True), which is the default value (default=&#8221;whatever&#8221;) but you cannot add a unique constraint on an attribute or set of attributes (there is no unique=True).</p>
<p>This is the typical nick or e-mail field in a database. You don&#8217;t want two users share the same nick or the same e-mail. You want all users to be identified by their nicks or their e-mails.
</p>
<p>¿What&#8217;s the problem? As I said, you can&#8217;t do that using the Google App Engine SDK&#8230; hopefully the solution is really easy <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<h5>The solution:</h5>
<p>The only thing you have to do is to override <b>put</b> inside our model, and launch an exception when a new element that violates our unique constraints is going to be added to the database.</p>
<p>In order to provide a simple example, let&#8217;s create a model named <b>User</b> using <b>name</b>, <b>email</b>, <b>language</b> and <b>creation_date</b> as the attributes:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> User <span style="color: black;">&#40;</span>db.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>: 
   creation_date = db.<span style="color: black;">DateTimeProperty</span> <span style="color: black;">&#40;</span>auto_now_add=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
   name          = db.<span style="color: black;">StringProperty</span> <span style="color: black;">&#40;</span>required = <span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
   <span style="color: #dc143c;">email</span>         = db.<span style="color: black;">EmailProperty</span> <span style="color: black;">&#40;</span>required = <span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
   language      = db.<span style="color: black;">StringProperty</span> <span style="color: black;">&#40;</span>default=<span style="color: #483d8b;">&quot;en&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Now, to reproduce the problem in GAE, the following code will create two different entries:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">User <span style="color: black;">&#40;</span>name=<span style="color: #483d8b;">&quot;Mark Johnson&quot;</span>, <span style="color: #dc143c;">email</span>=<span style="color: #483d8b;">&quot;test@codigomanso.com&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">put</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
User <span style="color: black;">&#40;</span>name=<span style="color: #483d8b;">&quot;JohnMarkinson&quot;</span>, <span style="color: #dc143c;">email</span>=<span style="color: #483d8b;">&quot;test@codigomanso.com&quot;</span>, language=<span style="color: #483d8b;">&quot;es&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">put</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Now let&#8217;s add a constraint to forbid two users to share the same e-mail. With this constraint the second <b>put</b>should fail or launch an exception. ¿How do we do that? It&#8217;s as simple as it seems. We just need to redefine <b>put</b> so when an new element is going to be added to the database, we check if violates our rules and we launch an exception.</p>
<p>The new class with the constraints would be as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> User <span style="color: black;">&#40;</span>db.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>: 
   creation_date = db.<span style="color: black;">DateTimeProperty</span> <span style="color: black;">&#40;</span>auto_now_add=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
   name          = db.<span style="color: black;">StringProperty</span> <span style="color: black;">&#40;</span>required = <span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
   <span style="color: #dc143c;">email</span>         = db.<span style="color: black;">EmailProperty</span> <span style="color: black;">&#40;</span>required = <span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
   language      = db.<span style="color: black;">StringProperty</span> <span style="color: black;">&#40;</span>default=<span style="color: #483d8b;">&quot;en&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
   <span style="color: #ff7700;font-weight:bold;">def</span> put <span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
     <span style="color: #808080; font-style: italic;"># Make sure e-mails are unique for each user</span>
     <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">is_saved</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: black;">&#40;</span>User.<span style="color: black;">gql</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">'WHERE email = :1'</span>, <span style="color: #008000;">self</span>.<span style="color: #dc143c;">email</span><span style="color: black;">&#41;</span>.<span style="color: black;">count</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
       <span style="color: #ff7700;font-weight:bold;">raise</span> DuplicatedInstanceError <span style="color: black;">&#40;</span><span style="color: #483d8b;">'User.email'</span>, <span style="color: #008000;">self</span>.<span style="color: #dc143c;">email</span><span style="color: black;">&#41;</span>
&nbsp;
     <span style="color: #808080; font-style: italic;"># call the parent method</span>
     db.<span style="color: black;">Model</span>.<span style="color: black;">put</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span></pre></div></div>

<p>We needed only 3 lines of code (4 counting the def line)&#8230; it can&#8217;t be easier</p>
<h5>Some inconveniences</h5>
<p>After thinking on it for a while, this implementation is far from being perfect, although it will work on most cases. I see two problems:</p>
<ul>
<li>You have to make an extra query before inserting an item (updates behave as always)
</li>
<li>Using this solution you can end up with two users having the same e-mail. How can that be? Have you heard about race conditions?  The count-put operation is not atomic, so if two users try to register the same e-mail at almost the same time (you will have two instances of the application running), then it can happen that they do the count at the same time (returning zero) and so none of both will raise an exception.
</li>
</ul>
<p>The first problem is not a problem at all, just something to be aware of.</p>
<p>The second problem it can be a huge problem. There are two solutions that come to my mind right now, one is to run the count-put in a transaction for new instances (transactions are atomic), and the other one would be to create a mutex around the count-put. To me, this is something I&#8217;m not worried about, but you need to know that it can happen.
</p>
<h5>Apendix: DuplicatedInstanceError</h5>
<p>If you are smart, you have probably realiced that I launched an exception of the type  DuplicatedInstanceError. This exception does not exist, I created to be able to detect duplicated instances. My implementation is below, but you can use whatever you want:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> DuplicatedInstanceError <span style="color: black;">&#40;</span><span style="color: #008000;">Exception</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, attrpath, value = <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
    Accept a dict of attribute and value pairs, or just one attribute and a value:
&nbsp;
    Example:
       # user unique constraint has been violated (User.nick to be unique)
       DuplicatedInstanceError ('</span>User.<span style="color: black;">nick</span><span style="color: #483d8b;">', '</span>jsmith<span style="color: #483d8b;">')
&nbsp;
       # student unique constraint has been violated (student nick'</span>s are fin <span style="color: #ff7700;font-weight:bold;">for</span> different schools<span style="color: black;">&#41;</span>
       DuplicatedInstanceError <span style="color: black;">&#40;</span><span style="color: black;">&#123;</span>
         <span style="color: #483d8b;">'Student.nick'</span>   : <span style="color: #483d8b;">'jsmith'</span>,
         <span style="color: #483d8b;">'Student.school'</span> : <span style="color: #483d8b;">'demo-primary-school'</span>
       <span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
    <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
    self.attrpath = '</span><span style="color: #483d8b;">'
&nbsp;
    # accept a dict as the only parameter
    if isinstance (attrpath, dict):
      for (k, v) in attrpath.items():
        self.attrpath += '</span>, <span style="color: #483d8b;">' + str (k) + '</span> = <span style="color: #483d8b;">' + str(v)
      self.attrpath = self.attrpath[2:]
    # accept a couple of strings
    else:
      self.attrpath = str(attrpath) + '</span> = <span style="color: #483d8b;">' + str(value) 
&nbsp;
    self.attrpath = '</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">' + self.attrpath + '</span><span style="color: black;">&#41;</span><span style="color: #483d8b;">'
    return
&nbsp;
  def __str__ (self):
    return str (self.attrpath)</span></pre></div></div>

<p>That&#8217;s all!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2010/09/solved-anadir-claves-unicas-en-google-app-engine-en-3-lineas/feed/</wfw:commentRss>
		<slash:comments>194</slash:comments>
		</item>
		<item>
		<title>Manso Trick: simple strip HTML tags using Python</title>
		<link>http://www.codigomanso.com/en/2010/09/truco-manso-eliminar-tags-html-en-python/</link>
		<comments>http://www.codigomanso.com/en/2010/09/truco-manso-eliminar-tags-html-en-python/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 10:42:33 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[Programacion]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[truco manso]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[mansotrick]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[trick]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=919</guid>
		<description><![CDATA[The idea is to make a simple function that strips all HTML tags on a given string.
I know the following code might not seem the best code in the world, but keep in mind that I&#8217;m just looking for a simple function that does the job.

def stripHTMLTags &#40;html&#41;:
  &#34;&#34;&#34;
    Strip HTML [...]]]></description>
			<content:encoded><![CDATA[<p>The idea is to make a simple function that strips all HTML tags on a given string.</p>
<p>I know the following code might not seem the best code in the world, but keep in mind that I&#8217;m just looking for a <b>simple function</b> that does the job.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> stripHTMLTags <span style="color: black;">&#40;</span>html<span style="color: black;">&#41;</span>:
  <span style="color: #483d8b;">&quot;&quot;&quot;
    Strip HTML tags from any string and transfrom special entities
  &quot;&quot;&quot;</span>
  <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
  text = html
&nbsp;
  <span style="color: #808080; font-style: italic;"># apply rules in given order!</span>
  rules = <span style="color: black;">&#91;</span>
    <span style="color: black;">&#123;</span> r<span style="color: #483d8b;">'&gt;<span style="color: #000099; font-weight: bold;">\s</span>+'</span> : u<span style="color: #483d8b;">'&gt;'</span><span style="color: black;">&#125;</span>,                  <span style="color: #808080; font-style: italic;"># remove spaces after a tag opens or closes</span>
    <span style="color: black;">&#123;</span> r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\s</span>+'</span> : u<span style="color: #483d8b;">' '</span><span style="color: black;">&#125;</span>,                   <span style="color: #808080; font-style: italic;"># replace consecutive spaces</span>
    <span style="color: black;">&#123;</span> r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\s</span>*&lt;br<span style="color: #000099; font-weight: bold;">\s</span>*/?&gt;<span style="color: #000099; font-weight: bold;">\s</span>*'</span> : u<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#125;</span>,      <span style="color: #808080; font-style: italic;"># newline after a &lt;br&gt;</span>
    <span style="color: black;">&#123;</span> r<span style="color: #483d8b;">'&lt;/(div)<span style="color: #000099; font-weight: bold;">\s</span>*&gt;<span style="color: #000099; font-weight: bold;">\s</span>*'</span> : u<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#125;</span>,       <span style="color: #808080; font-style: italic;"># newline after &lt;/p&gt; and &lt;/div&gt; and &lt;h1/&gt;...</span>
    <span style="color: black;">&#123;</span> r<span style="color: #483d8b;">'&lt;/(p|h<span style="color: #000099; font-weight: bold;">\d</span>)<span style="color: #000099; font-weight: bold;">\s</span>*&gt;<span style="color: #000099; font-weight: bold;">\s</span>*'</span> : u<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#125;</span>,   <span style="color: #808080; font-style: italic;"># newline after &lt;/p&gt; and &lt;/div&gt; and &lt;h1/&gt;...</span>
    <span style="color: black;">&#123;</span> r<span style="color: #483d8b;">'&lt;head&gt;.*&lt;<span style="color: #000099; font-weight: bold;">\s</span>*(/head|body)[^&gt;]*&gt;'</span> : u<span style="color: #483d8b;">''</span> <span style="color: black;">&#125;</span>,     <span style="color: #808080; font-style: italic;"># remove &lt;head&gt; to &lt;/head&gt;</span>
    <span style="color: black;">&#123;</span> r<span style="color: #483d8b;">'&lt;a<span style="color: #000099; font-weight: bold;">\s</span>+href=&quot;([^&quot;]+)&quot;[^&gt;]*&gt;.*&lt;/a&gt;'</span> : r<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\1</span>'</span> <span style="color: black;">&#125;</span>,  <span style="color: #808080; font-style: italic;"># show links instead of texts</span>
    <span style="color: black;">&#123;</span> r<span style="color: #483d8b;">'[ <span style="color: #000099; font-weight: bold;">\t</span>]*&lt;[^&lt;]*?/?&gt;'</span> : u<span style="color: #483d8b;">''</span> <span style="color: black;">&#125;</span>,            <span style="color: #808080; font-style: italic;"># remove remaining tags</span>
    <span style="color: black;">&#123;</span> r<span style="color: #483d8b;">'^<span style="color: #000099; font-weight: bold;">\s</span>+'</span> : u<span style="color: #483d8b;">''</span> <span style="color: black;">&#125;</span>                   <span style="color: #808080; font-style: italic;"># remove spaces at the beginning</span>
  <span style="color: black;">&#93;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">for</span> rule <span style="color: #ff7700;font-weight:bold;">in</span> rules:
    <span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: black;">&#40;</span>k,v<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> rule.<span style="color: black;">items</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
      regex = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span> <span style="color: black;">&#40;</span>k<span style="color: black;">&#41;</span>
      text  = regex.<span style="color: black;">sub</span> <span style="color: black;">&#40;</span>v, text<span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;"># replace special strings</span>
  special = <span style="color: black;">&#123;</span>
    <span style="color: #483d8b;">'&amp;nbsp;'</span> : <span style="color: #483d8b;">' '</span>, <span style="color: #483d8b;">'&amp;amp;'</span> : <span style="color: #483d8b;">'&amp;'</span>, <span style="color: #483d8b;">'&amp;quot;'</span> : <span style="color: #483d8b;">'&quot;'</span>,
    <span style="color: #483d8b;">'&amp;lt;'</span>   : <span style="color: #483d8b;">'&lt;'</span>, <span style="color: #483d8b;">'&amp;gt;'</span>  : <span style="color: #483d8b;">'&gt;'</span>
  <span style="color: black;">&#125;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: black;">&#40;</span>k,v<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> special.<span style="color: black;">items</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    text = text.<span style="color: black;">replace</span> <span style="color: black;">&#40;</span>k, v<span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">return</span> text</pre></div></div>

<p>To see this function in action (and get an idea of what does), following you can find an example of a HTML containing some javascript, styles, classes, and several kind of tags:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">print</span> stripHTMLTags <span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt; Whatever &lt;/title&gt;
  &lt;script language=&quot;javascript&quot;&gt;
  var j = 0;
  function asdf() {
    if (j &lt; 0)
      alert (&quot;Whatever&quot;);
  }
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body style=&quot;background: red;&quot;&gt;
    &lt;div class=&quot;container&quot;&gt;
      &lt;h1&gt;This is the title&lt;/h1&gt;
      &lt;p&gt;
        This is the first paragraph with little text.
        &lt;br/&gt;
        This is the second line of the first paragraph (note the BR tag).
      &lt;/p&gt;
      &lt;p&gt;This is the second paragraph with some extra text.&lt;/p&gt;     
    &lt;/div&gt;
    &lt;div&gt;
      &lt;p&gt;
        &lt;span&gt;Follow this link:&lt;/span&gt;
        &amp;nbsp;&amp;nbsp;&lt;a href=&quot;http://www.codigomanso.com/&quot;&gt;Codigo Manso&lt;/a&gt;
      &lt;/p&gt;
&nbsp;
      &lt;p&gt;Do you like it?&lt;/p&gt;
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;'</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span></pre></div></div>

<p>And this is the output of stripHTMLTags:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">This is the title
&nbsp;
This is the first paragraph with little text.
This is the second line of the first paragraph (note the BR tag). 
&nbsp;
This is the second paragraph with some extra text.
&nbsp;
Follow this link:  http://www.codigomanso.com/
&nbsp;
Do you like it?</pre></div></div>

<p>My requirements were to do a simple function that transformed HTML into text. I know it won&#8217;t handle all cases, not even bad formatted HTML code. It&#8217;s OK! I&#8217;m fine with that.</p>
<p>If you need a function which handles wrong-formatted HTML for your project I recommend you to look for a good HTML or XML parser (<a href="http://www.crummy.com/software/BeautifulSoup/">BeautifulSoup is a great HTML parser for Python</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2010/09/truco-manso-eliminar-tags-html-en-python/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Manso Trick: Pad a number with leading zeroes in javascript</title>
		<link>http://www.codigomanso.com/en/2010/07/simple-javascript-formatting-zero-padding/</link>
		<comments>http://www.codigomanso.com/en/2010/07/simple-javascript-formatting-zero-padding/#comments</comments>
		<pubDate>Fri, 30 Jul 2010 17:08:33 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[formatting]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[padding]]></category>
		<category><![CDATA[truco manso]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=907</guid>
		<description><![CDATA[I was missing a simple and elegant method for padding a number with leading zeroes in javascript.
A typical example of leading zeroes is when you want to show the current time and you want the time to be formatted like hh:mm. There is no problem when it&#8217;s 12:40, but when is five minutes past one, [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">I was missing a simple and elegant method for padding a number with leading zeroes in javascript.</p>
<p style="text-align: justify;">A typical example of leading zeroes is when you want to show the current time and you want the time to be formatted like hh:mm. There is no problem when it&#8217;s 12:40, but when is five minutes past one, you get 1:5, which is not what you would expect. You want it formatted like 01:05. The same happens with dates and in several other situations.</p>
<p style="text-align: justify;">Yesterday I found the best solution I&#8217;ve seen for this problem. Before that, let&#8217;s see possible solutions.</p>
<p style="text-align: justify;">Usually, needing only one zero, I would do something like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">h <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;0&quot;</span> <span style="color: #339933;">+</span> h<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> h<span style="color: #339933;">;</span></pre></div></div>

<p style="text-align: justify;">This is not bad at all, but is not elegant. The problem is, what if we want it to be formatted with 3 digits? Then, using the same schema, we can end up with something like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">h <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;0&quot;</span> <span style="color: #339933;">+</span> h<span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;00&quot;</span> <span style="color: #339933;">+</span> h<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> h<span style="color: #339933;">;</span></pre></div></div>

<p style="text-align: justify;">Man, this is ugly and unreadable!! Maybe it will improve with an if/else, but it will be bad anyway. Probably if you need to do something like that, you will create a function that will handle this problem and then, you will call that function saying you want h padded with 3 zeros. But there is another solution that does not require you to use or create such a funciton&#8230;</p>
<p style="text-align: justify;">Have a look for two digit and three digit:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;0&quot;</span> <span style="color: #339933;">+</span> h<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// devolverá &quot;01&quot; si h=1; &quot;12&quot; si h=12</span>
<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;00&quot;</span> <span style="color: #339933;">+</span> h<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;001&quot; si h=1; &quot;012&quot; si h=12;&quot;123&quot; si h = 123</span></pre></div></div>

<p style="text-align: justify;">I don&#8217;t know what are you thinking after seeing this solution, but I got flashed when I saw it. Just amazing!</p>
<p style="text-align: justify;">Let&#8217;s explain what&#8217;s going on. The code above only concatenates the &#8220;00&#8243; with the number we want to format (e.g: &#8220;00&#8243; + 12 = &#8220;0012&#8243;) and then, we get the last three digits using the <b>slice</b> string function (that&#8217;s why you have a -3). Guess what? The last three characters are &#8220;012&#8243; which is the string we want.</p>
<p style="text-align: justify;">Of course this solution is not only limited to leading zeroes, you can use it with leading spaces, or whatever character you want.</p>
<p>Sources:</p>
<ul>
<li><a href="http://gugod.org/2007/09/padding-zero-in-javascript.html">Padding zero in Javascript</a></li>
<li><a href="http://dev.enekoalonso.com/2010/07/20/little-tricks-string-padding-in-javascript/">Little tricks: string padding in Javascript</a> of Eneko Alonso blog (originally seen here)</li>
</ul>
<p><b>Update:</b> <a href="http://milan.adamovsky.com/">Milan Adamovsky</a> has created a web page <a href="http://jsperf.com/zero-padding">http://jsperf.com/zero-padding</a> where you can check the performance issues of several functions to pad with zeroes, and you can try it directly in your browser <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2010/07/simple-javascript-formatting-zero-padding/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
	</channel>
</rss>

