<?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; python</title>
	<atom:link href="http://coder.cl/category/programming/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://coder.cl</link>
	<description>web developer &#38; system programmer</description>
	<lastBuildDate>Sun, 13 May 2012 17:32:40 +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>writing python daemons</title>
		<link>http://coder.cl/2012/05/writing-python-daemons/</link>
		<comments>http://coder.cl/2012/05/writing-python-daemons/#comments</comments>
		<pubDate>Sat, 12 May 2012 19:54:41 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=2456</guid>
		<description><![CDATA[A daemon in Unix&#8482; and similar operating systems, is a background process, which runs without using the terminal or the X11 system as main I/O system. In other operating system, this is called service. A daemon has a very specific tasks to do, for example the NTP daemon keeps your computer date updated connecting to [...]]]></description>
			<content:encoded><![CDATA[<p>A <i>daemon</i> in Unix&trade; and similar operating systems, is a background process, which runs without using the terminal or the X11 system as main I/O system. In other operating system, this is called <i>service</i>. A daemon has a very specific tasks to do, for example the NTP daemon keeps your computer date updated connecting to NTP servers. Many applications that require asynchronous tasks, require this kind of applications to make your life easier. For example a job queue processor for the Gearman job server can be handled with this kind of applications.</p>
<p><span id="more-2456"></span></p>
<p>A daemon, classically on most Unix&trade; first closes the three main I/O streams: <i>stdin</i>, <i>stdout</i> and <i>stderr</i>, then the <tt>fork(2)</tt> system call is used, creating an image of the current process, once the call is made, an <tt>exit(1)</tt> call is made on the parent process, and the child process keeps working in background. Due to the Python philosophy of being a productive language, this not so complex process is already implemented in the <tt>daemon</tt> module, so with this module you can create a daemon program.</p>
<pre name="code" class="brush:python;">

import os
import sys
import daemon
import atexit

def main():
    """
    Main Program
    """

    install_signal_handler()
    atexit.register(at_exit_handler)

    opts = parse_opts()
    config = parse_config(opts)
    if not opts.cwd:
        print("No Working Directory")
        sys.exit(0)

    with daemon.DaemonContext():
        os.chdir(opts.cwd)
        install_signal_handler()
        start_schedule(config, opts)

""" Executes the main() Function """
if __name__ == '__main__':
    main()
</pre>
<p>If you observe the code, it changes the working directory using <tt>os.chdir()</tt>, because once a daemon is created, its working directory is automatically changed to the root directory or <tt>/</tt>. Some daemons or servers like the Apache HTTP server, have a precompiled working directory, but it also allows to use <tt>chroot</tt> directory to make it work outside the precompiled working directory. Also, it is installing signal handlers, to allow signal processing, like SIGHUP and SIGCHLD. The <tt>parse_opts()</tt> and <tt>parse_config()</tt> functions, are functions to parse daemon arguments and daemon configuration using the <tt>optparse</tt> and <tt>ConfigParser</tt> modules respectively.</p>
<p>The <tt>atexit</tt> module is used to ensure that most program resources &mdash; like files, connections and similar stuff &mdash; are released or closed, once the daemon process terminates. So, the following <tt>atexit</tt> example shows a routine releasing resources.</p>
<pre name="code" class="brush:python;">

from django.db import connections
import atexit

def at_exit_handler():
    """
    At Exit Function (close all connections)
    """
    for con in connections.all():
        try:
            con.close()
        except Exception, exc:
            log().error(exc)
    log().close()
</pre>
<p>The signal handler have very similar behaviour, it register a processing callback, which is executed using each time a signal is received. For example on the SIGHUP signal, I am doing a daemon rehash, rather than doing a daemon termination.</p>
<pre name="code" class="brush:python;">

import signal as sig
import traceback as tb

def rehash_daemon():
    """
    Rehash the daemon, reading configurations again.
    """
    global DAEMON_CONFIG
    DAEMON_CONFIG = parse_config()

def install_signal_handler():
    """
    Signal Handler Installer
    """
    try:
        sig.signal(sig.SIGHUP, rehash_daemon)
    except Exception, exc:
        log().error(repr(exc))
        log().error(tb.format_exc(exc))
        return False
</pre>
<p>This will allow you to create a robust daemon, capable to handle signals, resource releasing at the exit event and similar stuff. Remember that as any language with a garbage collector, Python has the disadvantage of leak memory if you do not cut the object references properly, due to the reference counting used by the garbage collector inside the GIL cycles. Your daemon design should be optimal.</p>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2012. |
<a href="http://coder.cl/2012/05/writing-python-daemons/">Permalink</a> |
<a href="http://coder.cl/2012/05/writing-python-daemons/#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2012/05/writing-python-daemons/&title=writing python daemons">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/2012/05/writing-python-daemons/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>django and amazon s3</title>
		<link>http://coder.cl/2012/01/django-and-amazon-s3/</link>
		<comments>http://coder.cl/2012/01/django-and-amazon-s3/#comments</comments>
		<pubDate>Thu, 19 Jan 2012 13:18:45 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=2183</guid>
		<description><![CDATA[Amazon S3 is a well known web based storage system provided as SaaS service provided by Amazon Web Services. On Django you can integrate that service using the storage interface called Django Storages, but you must have some considerations using that SaaS storage interface. Mainly regarding the Date header sent to the service on each [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://aws.amazon.com/s3/' title='Amazon Simple Storage Service (Amazon S3)'>Amazon S3</a> is a well known web based storage system provided as SaaS service provided by <a href='http://aws.amazon.com/' title='Amazon Web Services (AWS)'>Amazon Web Services</a>. On Django you can integrate that service using the storage interface called <a href='http://code.welldev.org/django-storages/' title='Django Storages (django-storages)'>Django Storages</a>, but you must have some considerations using that SaaS storage interface. Mainly regarding the <tt>Date</tt> header sent to the service on each read, write and similar operations, where you must send an updated header with the proper Time Zone and format.</p>
<p><span id="more-2183"></span></p>
<p>Once you have configured and working the django-storages package, you should fill the <tt>Date</tt> header or the <tt>x-amz-date</tt> header each time that you do a request to S3. So, the only way to do that is not to leave the module sending the date automatically, instead you should write the settings variable <tt>AWS_HEADERS</tt>.</p>
<pre name="code" class="brush:python;">

import pytz
from datetime import datetime
from django.conf import settings

def get_aws_date(self):
    """
    Returns the server date formatted and using the server
    time zone localized to be used as Date header with
    Amazon S3.
    """
    stz = pytz.timezone(settings.TIME_ZONE)
    dtm = stz.localize(datetime.now()).strftime("%a, %d %b %Y %H:%M:%S %z")
    settings.AWS_HEADERS['Date'] = dtm
    settings.AWS_HEADERS['x-amz-date'] = dtm
    return dtm
</pre>
<p>Also, you must consider this requirement to work with S3 on all your requests. Since django-storages checks if the <tt>AWS_HEADERS</tt> has the Date or x-amz-date headers set, you must set that header each time that you make a request to S3, so you cannot use a batch <tt>read()</tt> from the storage or a batch <tt>write()</tt> to the storage, because it will use the previously sent Date header and it will fail, because it is considered inconsistent by the S3 authentication mechanism. If we use S3 as default storage, the example below will fail, because it will send file chunks with the same Date header, because the header was set previously.</p>
<pre name="code" class="brush:python;">

from django.core.files.storage import default_storage

if default_storage.exist('test-large-file.mp3'):
    mp3file = open('test-large-file.mp3')
    s3file = default_storage.open('test-large-file.mp3')
    mp3file.write(s3file.read())
    mp3file.close()
    s3file.close()
</pre>
<p>So, you need to use small chunks to read from the storage, as the example below.</p>
<pre name="code" class="brush:python;">

from django.core.files.storage import default_storage

if default_storage.exist('test-large-file.mp3'):
    get_aws_date()
    mp3file = open('test-large-file.mp3')
    s3file = default_storage.open('test-large-file.mp3')
    buff = s3file.read(settings.AWS_CHUNK_SIZE)
    while buff:
        mp3file.write(buff)
        try:
            buff = s3file.read(settings.AWS_CHUNK_SIZE)
        except Exception, exc:
            buff = None
    mp3file.close()
    s3file.close()
</pre>
<p>But also you must consider few issues about this. Since AWS_HEADERS is a global variable, writing to that variable will slow down your code because it lacks time on GIL usage, also it will lack your threaded application if it written twice, even if you are using locks and Django can handle parallel writes to that variable. So, be careful reading and writing large files from S3, and take a look on how the Date header is sent on each request.</p>
<pre name="code" class="brush:python;">

def _add_aws_auth_header(self, headers, method,
                         bucket, key, query_args):
    if not headers.has_key('Date'):
        headers['Date'] = time.strftime("%a, %d %b %Y %X GMT",
                                        time.gmtime())

    c_string = canonical_string(method, bucket, key,
                                query_args, headers)
    headers['Authorization'] =
        "AWS %s:%s" % (self.aws_access_key_id,
                       encode(self.aws_secret_access_key, c_string))
</pre>
<p>Where the <tt>%X</tt> <a href='http://docs.python.org/library/datetime.html#strftime-strptime-behavior' title='Python Manual, strftime() and strptime() Behavior'>specifier for localized machines</a> with an environment variable <tt>LC_ALL</tt> different from <i>C</i> will throw the wrong date format disallowing your application to work with S3. This is a well known bug reported <a href='https://bitbucket.org/david/django-storages/issue/56/aws-authentication-requires-a-valid-date' title='#56 "AWS authentication requires a valid Date or x-amz-date header"' rel="nofollow">on this link</a>. So, the right implementation, should be as follows.</p>
<pre name="code" class="brush:python;">

def _add_aws_auth_header(self, headers, method,
                         bucket, key, query_args):
    if not 'Date' in headers:
        stz = pytz.timezone(settings.TIME_ZONE)
        dtm = stz.localize(datetime.now()).strftime("%a, %d %b %Y %H:%M:%S %z")
        headers['Date'] = dtm

    c_string = canonical_string(method, bucket, key,
                                query_args, headers)
    headers['Authorization'] =
        "AWS %s:%s" % (self.aws_access_key_id,
                       encode(self.aws_secret_access_key, c_string))
</pre>
<p>Good luck using S3.</p>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2012. |
<a href="http://coder.cl/2012/01/django-and-amazon-s3/">Permalink</a> |
<a href="http://coder.cl/2012/01/django-and-amazon-s3/#comments">6 comments</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2012/01/django-and-amazon-s3/&title=django and amazon s3">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/2012/01/django-and-amazon-s3/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>decorated template tags in django</title>
		<link>http://coder.cl/2012/01/decorated-template-tags-in-django/</link>
		<comments>http://coder.cl/2012/01/decorated-template-tags-in-django/#comments</comments>
		<pubDate>Fri, 06 Jan 2012 17:26:01 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=2153</guid>
		<description><![CDATA[Django provides an API to create custom template tags for those applications mounted over this nice Web Application Framework. Sometimes we need to decorate our functions, but you cannot decorate a function registered as tag in Django. The most elegant solution to solve this problem is to use a closure which will wrap the template [...]]]></description>
			<content:encoded><![CDATA[<p>Django provides an API to create custom template tags for those applications mounted over this nice Web Application Framework. Sometimes we need to <a href='http://wiki.python.org/moin/PythonDecorators' title='Python Decorators'>decorate</a> our functions, but you cannot decorate a function registered as tag in Django. The most elegant solution to solve this problem is to use a closure which will wrap the template tag function to be used decorated template tag.</p>
<p><span id="more-2153"></span></p>
<pre name="code" class="brush:python;">

from django import template
from django.conf import settings

from owner.common.decorators import html_escape_output

register = template.Library()

def fmt_owner_name(_owner):
    """
    Formats the Owner Name
    """
    @html_escape_output()
    def _fmt_owner_name(owner):
        nms = u'%s %s' % (first_word_on_string(owner.first_name),
                          first_word_on_string(owner.last_name))
        if len(nms) &gt; settings.MAX_NAME_ON_CARD:
            return u'%s...' % (nms[0:settings.MAX_NAME_ON_CARD])
        else:
            return nms
    return _format_owner_name(_owner)
fmt_owner_name = register.simple_tag(fmt_owner_name)
</pre>
<p>So, in this example, the template tag will escape the owner name, where all template tags in Django are considering HTML output as safe output, so they do not escape the HTML output automatically, even if you use <i>is_safe</i> and <i>needs_autoescape</i> properties, they require special treatment when you need to escape the HTML tags. In the example above the closure <tt>_fmt_owner_name</tt> is decorated with <tt>@html_escape_output</tt> which forces escaped HTML output to the Django template.</p>
<p>Also you can stack any number of decorators that you want. I think that this avoids using <i>cgi.escape</i> and similar stuff on each return statement. This helps you on Django template tag processing with common operations, like the example above. And it reduces the amount of code, making it more legible and simple, where we are using the fourth aphorism from the <a href='http://www.python.org/dev/peps/pep-0020/' title='PEP20: Zen of Python'>Zen of Python</a>: <i>&laquo;complex is better than complicated&raquo;</i>. Then we apply the template tag in the Django template as follows.</p>
<pre name="code" class="brush:xhtml;">

&lt;div class=&quot;owner-name&quot;&gt;
  {% fmt_owner_name owner_obj %}
&lt;/div&gt;
</pre>
<p>I hope that this will help you on the task of reducing the amount of repeated calls.</p>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2012. |
<a href="http://coder.cl/2012/01/decorated-template-tags-in-django/">Permalink</a> |
<a href="http://coder.cl/2012/01/decorated-template-tags-in-django/#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2012/01/decorated-template-tags-in-django/&title=decorated template tags in django">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/2012/01/decorated-template-tags-in-django/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>integrating selenium and django</title>
		<link>http://coder.cl/2011/12/integrating-selenium-and-django/</link>
		<comments>http://coder.cl/2011/12/integrating-selenium-and-django/#comments</comments>
		<pubDate>Mon, 12 Dec 2011 20:53:34 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=2090</guid>
		<description><![CDATA[As you know, using unit tests will only provide a testing over algorithms and low level interfaces. For a higher level approach, you have the automated testing suite that provides the Selenium, which provides you a test case API to be used in functional tests. If you want to automate functional tests under Django, you [...]]]></description>
			<content:encoded><![CDATA[<p>As you know, using unit tests will only provide a testing over algorithms and low level interfaces. For a higher level approach, you have the automated testing suite that provides the Selenium, which provides you a test case API to be used in functional tests. If you want to automate functional tests under Django, you can use the selenium IDE to record your actions on the web site, and export them as test case instructions for the selenium API, which will run those tests as you in the web browser, or even other tools like <a href='http://htmlunit.sourceforge.net/' title='Html Unit'>Html Unit</a>. This includes several browsers supported by the API, including a server that allows you to mount a test grid using various machines.</p>
<p><span id="more-2090"></span></p>
<p>Without falling in numerous details, I will try to explain how to mount selenium tests under Django. The very first step is to download and install the <a href='http://seleniumhq.org/projects/ide/' title='Selenium IDE Plug-In'>selenium plug-in</a>, that allows you to create automated functional tests for the selenium API.</p>
<div class="figure">
<a href='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/12/selenium-ide.jpg?v=2.0' title='Selenium IDE' target="_blank"><img src='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/12/selenium-ide.jpg?v=2.0' border='0' alt='Selenium IDE' /></a></p>
<div>Selenium IDE</div>
</div>
<p>Once you have created the functional test using the record option, you can save it with the native selenium format, which is a simplified HTML file. Once you saved the test case, you can <i>export</i> the test case using the export option on the file menu, where you will see two formats provided for Python integration, called <i>Web Driver</i> and <i>Remote Control</i>. The first one called <i>Web Driver</i> is used with a local browser &mdash; on the local test server &mdash; and the second one called <i>Remote Control</i> should be used with grid servers, an option that supports multiple browsers by calling remote actions on a grid of parallel test servers. Once you export the file in the Web Driver format, you see that the python code is based on the standard unit testing suite, as the example that follows.</p>
<pre name="code" class="brush: python;">

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re

class TestWebdriver(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://myserver.com/"
        self.verificationErrors = []

    def test_login_view(self):
        driver = self.driver
        driver.get("/home/")
        driver.find_element_by_link_text("Login").click()
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("test@myserver.com")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("testpass123123")
        driver.find_element_by_css_selector("input.submit.login-but").click()
        driver.find_element_by_link_text("MainPanel").click()

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()
</pre>
<p>To run that test, the file should be saved on the <tt>app/tests/</tt> directory, and should run using a standard browser. In my case, I am using <a href='https://github.com/kmmbvnr/django-jenkins' title='django-jenkins module'>django-jenkins</a> for continuous integration with <a href='http://jenkins-ci.org/' title='Jenkins'>Jenkins</a> or <a href='http://www.atlassian.com/software/bamboo/overview' title='Atlassian Bamboo'>Atlassian Bamboo</a>. So, you should add the <a href='http://pypi.python.org/pypi/selenium/' title='Python Selenium'>Python Selenium</a> API, where you will find the <tt>webdriver</tt> package to use with the web driver API &mdash; remember that it is used with a local browser &mdash; or the <tt>selenium</tt> package that provides API calls to use grid servers for parallel tests.</p>
<p>So, the functional test integration runs as standard integration for any build. You can separate the test suites customising the build script for Jenkins or Bamboo and have separate reports. That is a preferred option if you want to keep detailed information about your tests. Enjoy integrating automated functional tests.</p>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2011. |
<a href="http://coder.cl/2011/12/integrating-selenium-and-django/">Permalink</a> |
<a href="http://coder.cl/2011/12/integrating-selenium-and-django/#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2011/12/integrating-selenium-and-django/&title=integrating selenium and django">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/12/integrating-selenium-and-django/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>why to go with multi-paradigm?</title>
		<link>http://coder.cl/2011/11/why-to-go-with-multi-paradigm/</link>
		<comments>http://coder.cl/2011/11/why-to-go-with-multi-paradigm/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 15:07:57 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=1957</guid>
		<description><![CDATA[Hybrid languages are cool. Most powerful programming languages are those languages which can handle a multi-paradigm scope. You can reduce the amount of code considerably when you are using a multi-paradigm approach. Due to the imperative origin and procedural approach of object oriented languages, having only object oriented language approach is not enough to solve [...]]]></description>
			<content:encoded><![CDATA[<p>Hybrid languages are cool. Most powerful programming languages are those languages which can handle a multi-paradigm scope. You can reduce the amount of code considerably when you are using a multi-paradigm approach. Due to the imperative origin and procedural approach of object oriented languages, having only object oriented language approach is not enough to solve problems. For example as we reviewed the widely used MapReduce distributed computing model has its origin on the functional paradigm in two higher order functions called <tt>map</tt> and <tt>reduce</tt> &mdash; where the synonym of <i>reduce</i> is <tt>fold</tt>, applying a kind of Monoid where the data is lifted and transformed by the <tt>map</tt> function and then is processed and reduced by the <tt>fold</tt> or <tt>reduce</tt> function.</p>
<p><span id="more-1957"></span></p>
<p><br/></p>
<h3>tl;dr</h3>
<p>I prefer multi-paradigm languages because you can do cool stuff with less code.</p>
<p><br/></p>
<h3>why do I prefer it?</h3>
<p>If you reduce the amount of code that you are programming, you can decrease the amount of possible bugs by reducing its cyclomatic complexity. So, using functional approach in certain cases will reduce the amount of code that you are producing, increasing your productivity considerably. Take a look on the following code made on PHP to find the lowest value in an array of associative arrays.</p>
<pre class="brush: php;">
&lt;?php

/* PHP example on associative arrays */

$min = 9999;
$tst = array(array('a' =&gt; 30, 'b' =&gt; 20, 'c' =&gt; 10),
             array('a' =&gt; 29, 'b' =&gt; 19, 'c' =&gt; 9),
             array('a' =&gt; 28, 'b' =&gt; 18, 'c' =&gt; 8));

foreach ($tst as $t) {
    if ($t['a'] &lt; $min) {
        $min = $t['a'];
    }
}

echo &quot;{$min}n&quot;;

 ?&gt;
</pre>
<p>Now take a look on the same problem but implemented in Python using multi-paradigm approach, with functional features and using higher order functions.</p>
<pre class="brush: python;">

### Python example using higher order functions and
### functional approach

tst = [{'a': 30, 'b': 20, 'c': 10},
       {'a': 29, 'c': 19, 'c': 9},
       {'a': 28, 'c': 18, 'c': 8}]

t = min(*tst, key=lambda x: x['a'])
print(t['a'])
</pre>
<p>You can clearly see that the code was reduced to only one line of code using a functional approach, using <tt>min</tt> as higher order function, so the <i>&eta;-reduction</i> is evident. And you can handle a wide variety of problems using the functional approach, reducing the amount of lines of code considerably, increasing your productivity and doing better code. Do you remember how to calculate the factorial?, do you remember the factorial example in Python?. Take a look on the PHP implementation against the Python implementation.</p>
<pre class="brush: php;">
&lt;?php

/* PHP factorial example */

function fact($n) {
    if ($n == 0) return 1;
    $r = 1;
    for ($i = 1; $i &lt;= $n; $i++) {
        $r = $r * $i;
    }
    return $r;
}

$f = fact(4);
echo &quot;{$f}n&quot;;

 ?&gt;
</pre>
<p>Now take a look on the Python implementation. You will notice a multi-paradigm approach using procedural and functional paradigms, using higher order functions again.</p>
<pre class="brush: python;">

### Python factorial example

def fact(n):
    if n == 0: return 1
    return reduce(int.__mul__, xrange(1, n + 1))

print(fact(4))
</pre>
<p>I really like to know how functional programming can allow you to reduce the amount of lines of code which are required to complete certain tasks, and how it is expressive as procedural code does, but it is pretty much well fluent and it has less cyclomatic complexity than the procedural approach. Functional code is very legible. The other advantage is the fact that some compilers supporting the functional paradigm are enabled with <i>tail calls</i> implementation, so you do not need to worry about creating stack frames and generating stack overflows with recursive functions. Languages that currently supports multi-paradigm approach are JavaScript, Python, Ruby and Scala, among other languages.</p>
<p>So, I recommend you that you should start learning some functional programming principles and techniques. On the past I got tired of those companies with silly restrictions about using reductions and making the code looks like literate programming code, instead of well qualified <i>enterprise code</i>, and since I am working as freelancer, I just use the best practices that each language requires.</p>
<p>Do not get stuck in object oriented and procedural paradigms <i>only</i>, learn other paradigms and approaches, so you can handle a wider number of problems reaching better solutions, more optimal in lines of code, cyclomatic complexity, algorithmic complexity and time complexity. In some manner, what I can say is the fact that <i>more lines of code</i> do not means better, it depends on the language that you are using and how do you implement your solutions, and having a multi-paradigm approach can lead you to deliver better code. Sometimes &mdash; certain languages &mdash; can allow you to implement the same solution with less code, you just need to know the proper technique.</p>
<p>Many languages now are including some functional features, and are going multi-paradigm. For example Java 8 has included Lambda Expressions, C# includes Lambda Expressions, they just need to implement Closures to start doing some cool stuff in certain cases. PHP 5.3 now includes Lambda Expressions or Anonymous Functions. So, you should learn about the functional paradigm, among other stuff that will be required in the future.</p>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2011. |
<a href="http://coder.cl/2011/11/why-to-go-with-multi-paradigm/">Permalink</a> |
<a href="http://coder.cl/2011/11/why-to-go-with-multi-paradigm/#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2011/11/why-to-go-with-multi-paradigm/&title=why to go with multi-paradigm?">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/why-to-go-with-multi-paradigm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>re: monoids in python</title>
		<link>http://coder.cl/2011/10/re-monoids-in-python/</link>
		<comments>http://coder.cl/2011/10/re-monoids-in-python/#comments</comments>
		<pubDate>Wed, 12 Oct 2011 22:52:25 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=1844</guid>
		<description><![CDATA[On a recent post on his blog, Francisco Mota has described Monoids with both, the Mathematical background and the functional background. If you read careful the post, you will notice that he has created the Monoid class. He describes a Monoid as a set with neutral element and an the operation , where the triple [...]]]></description>
			<content:encoded><![CDATA[<p>On a recent post on his blog, Francisco Mota <a href='http://fmota.eu/2011/10/09/monoids-in-python.html' title='Monoids in Python'>has described Monoids</a> with both, the Mathematical background and the functional background. If you read careful the post, you will notice that he has created the <tt>Monoid</tt> class. He describes a Monoid as a set <img src='http://s.wordpress.com/latex.php?latex=A&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='A' title='A' class='latex' /> with neutral element <img src='http://s.wordpress.com/latex.php?latex=%5Cvarepsilon&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\varepsilon' title='\varepsilon' class='latex' /> and an the operation <img src='http://s.wordpress.com/latex.php?latex=%28%20%5Ccdot%20%29%20%3A%20A%20%5Ctimes%20A%20%5Cmapsto%20A&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='( \cdot ) : A \times A \mapsto A' title='( \cdot ) : A \times A \mapsto A' class='latex' />, where the triple <img src='http://s.wordpress.com/latex.php?latex=%5Clangle%20A%2C%20%5Cvarepsilon%2C%20%5Ccdot%20rangle&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\langle A, \varepsilon, \cdot rangle' title='\langle A, \varepsilon, \cdot rangle' class='latex' /> denotes the Monoid. In terms of Category Theory, the set should be a category and the operation should be a morphism.</p>
<p><span id="more-1844"></span></p>
<p>Some examples of Monoids, should be <img src='http://s.wordpress.com/latex.php?latex=%5Clangle%20%5Cmathbb%7BN%7D%2C%201%2C%20%5Ctimes%20%5Crangle&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\langle \mathbb{N}, 1, \times \rangle' title='\langle \mathbb{N}, 1, \times \rangle' class='latex' />, <img src='http://s.wordpress.com/latex.php?latex=%5Clangle%20%5Cmathbb%7BN%7D%2C%200%2C%20%2B%20%5Crangle&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\langle \mathbb{N}, 0, + \rangle' title='\langle \mathbb{N}, 0, + \rangle' class='latex' />, and so on, to describe some operations that should be mapped to natural numbers. Applied to the Python programming language, he describes sets as lists or tuples, and Python data types as Categories to treat certain operations as Monoids. Francisco has proposed a set of challenges regarding Monoids in Python. Here I will describe some solutions to those challenges.</p>
<ol>
<li>Using only listm, how would you concatenate a list of lists. That is, how would you transform [[a, b, c], [d, e], [f], []] into [a, b, c, d, e, f]?</li>
<li>The reverse of a monoid is a monoid where the operator take its arguments in reverse order. Create a method, <tt>reverse</tt>, that creates a reverse monoid. What is <tt>listm.reverse()</tt>?</li>
<li>Are there monoids that are fixed-points of the star method? That is, is there a monoid m such that m and m.star() are indistinguishable? (Side challenge: formalize the notion of &ldquo;indistinguishable&rdquo;.)</li>
<li>Do monoids have a dual concept? Monoids, as defined here, are roughly an embodiment of &ldquo;map/reduce&rdquo;, which is used to process and analyze data, potentially taking a large body of data and reducing it. Is there a similar concept, a &ldquo;comonoid&rdquo;, that generates data, instead of reducing it?</li>
<li>Let <tt>lift = int</tt>, and let <tt>op</tt> return an <tt>int</tt>. Can we enumerate all possible monoids? If not, what can we enumerate?</li>
</ol>
<p></p>
<h3>the final monoid class</h3>
<pre class="brush: python;">

class Monoid(object):
    null = None
    lift = None
    op = None

    def __init__(self, null, lift, op):
        self.null = null
        self.lift = lift
        self.op = op

    def fold(self, xs):
        if hasattr(xs, '__fold__') and callable(xs.__fold__):
            return xs.__fold__(self)
        else:
            mxs = map(self.lift, xs)
            return reduce(self.op, mxs, self.null)

    def rfold(self, xs):
        def reverse_reduce(op, lst, null):
            acm = null
            if len(lst) &lt;= 0:
                return acm
            cnt = 0
            lim = len(lst) - 1
            while cnt &lt; lim:
                acm = op(lst[cnt], acm)
                cnt += 1
            return op(lst[lim], acm)
        mxs = map(self.lift, xs)
        return reverse_reduce(self.op, mxs, self.null)

    def __call__(self, *args):
        return self.fold(args)

    def star(self):
        return Monoid(self.null, self.fold, self.op)

    def reverse(self):
        class ReverseMonoid(Monoid):
            def __call__(self, *args):
                return self.rfold(*args)
            def star(self):
                return ReverseMonoid(self.null, self.rfold, self.op)
        return ReverseMonoid(self.null, self.lift, self.op)
</pre>
<p></p>
<h3>1. listm as list concatenator</h3>
<pre class="brush: python;">
## challenge 1

listm = Monoid([], lambda x: [x], lambda a, b: a + b)
sample_list = [['a', 'b', 'c'], ['d', 'e'], ['f'], []]
a = listm.star().fold(sample_list)
print("a = %s" % (repr(a)))

## end challenge 1
</pre>
<p></p>
<h3>2. reverse litsm monoid</h3>
<p>A reverse Monoid, takes the <i>operation</i> arguments on reverse order, instead of passing <tt>a, b</tt>, it uses <tt>b, a</tt> as operation arguments on the <tt>reduce</tt> function. The <tt>reverse()</tt> method returns a ReverseMonoid instance.</p>
<pre class="brush: python;">
## challenge 2

listmr = listm.reverse()
b = listmr(a)
print("---> Challenge 2.")
print("b = %s" % (repr(b)))

## end challenge 2
</pre>
<p></p>
<h3>3. fixed-point monoids</h3>
<p>A fixed-point monoid to its <tt>star()</tt> method requires a <tt>fold()</tt> or reduction method returning the same type as its arguments. The only type that allows such stuff in native Python is the string, so we will use the string type related Monoid to define a fixed-point Monoid to its <tt>star()</tt> method. The <tt>joinm</tt> monoid meets that requirement.</p>
<pre class="brush: python;">
### challenge 3.1

sample_string1 = "hello 1!"
sample_string2 = "hello 2!"
sample_string3 = "hello 3!"

joinm = Monoid('', lambda x: str(x), lambda a, b: a + b)

sjoinm  = joinm.star()
rjoinm = joinm.reverse()

print("---&gt; Challenge 3.2")

c0 = joinm(sample_string1,
           sample_string2,
           sample_string3)
print("c0 = %s" % (repr(c0)))

c1 = sjoinm(sample_string1,
            sample_string2,
            sample_string3)
print("c1 = %s" % (repr(c1)))

## end challenge 3
</pre>
<p>The example above, should be formalized as <img src='http://s.wordpress.com/latex.php?latex=%5Clangle%20str%2C%20str%28%29%2C%20%2B%20%5Crangle&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\langle str, str(), + \rangle' title='\langle str, str(), + \rangle' class='latex' />. Other example can be implemented using some type checking tricks.</p>
<pre class="brush: python;">
## challenge 3.2

def sum_list(x):
    return ([lambda n: sum(n),
             lambda m: m][cmp(type(x),
                              list)])(x)

def reduce_list(a, b):
    return ([lambda n: sum(n),
             lambda m: m][cmp(type(a),
                              list)])(a)
           + ([lambda n: sum(n),
               lambda m: m][cmp(type(b),
                                list)])(b)

sample_list = [1, 2, 3, 4, 5]

summ = Monoid(0, sum_list, reduce_list)
ssumm  = summ.star()
rsumm = summ.reverse()

print("---&gt; Challenge 3.1")

c0 = summ(sample_list)
print("c0 = %s" % (repr(c0)))

c1 = ssumm(sample_list)
print("c1 = %s" % (repr(c1)))

c2 = rsumm(sample_list)
print("c2 = %s" % (repr(c2)))

## end challenge 3.2
</pre>
<p></p>
<h3>4. comonoid using monoid constructor</h3>
<pre class="brush: python;">
## challenge 4

def sum_list(x):
    return range(0, ([lambda n: sum(n),
                      lambda m: m][cmp(type(x),
                                       list)])(x))

def reduce_list(a, b):
    return range(0, ([lambda n: sum(n),
                      lambda m: m][cmp(type(a),
                                       list)])(a)
                 + ([lambda n: sum(n),
                     lambda m: m][cmp(type(b),
                                      list)])(b))

### challenge 4
sample_list = [1, 2, 3, 4, 5]
summ = Monoid(0, sum_list, reduce_list)
ssumm  = summ.star()
rsumm = summ.reverse()

print("---> Challenge 4")

c0 = summ(sample_list)
print("c0 = %s" % (repr(c0)))

c1 = ssumm(sample_list)
print("c1 = %s" % (repr(c1)))

c2 = rsumm(sample_list)
print("c2 = %s" % (repr(c2)))

## end challenge 4
</pre>
<p></p>
<h3>5. enumerate int monoids</h3>
<p>Since the number of operations that can be made using the int type in Python, we cannot enumerate such set of monoids. Such monoid, that should be declared as <tt>Monoid (0, lambda x: int(x), <i>f</i>)</tt>, where <i>f</i> can be any function returning <tt>int</tt>. The only set that we can enumerate is the limited number of integer numbers that can operate with <i>f</i>. I hope that you will enjoy the examples above.</p>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2011. |
<a href="http://coder.cl/2011/10/re-monoids-in-python/">Permalink</a> |
<a href="http://coder.cl/2011/10/re-monoids-in-python/#comments">One comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2011/10/re-monoids-in-python/&title=re: monoids in python">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/re-monoids-in-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>custom aggregates on django</title>
		<link>http://coder.cl/2011/09/custom-aggregates-on-django/</link>
		<comments>http://coder.cl/2011/09/custom-aggregates-on-django/#comments</comments>
		<pubDate>Wed, 28 Sep 2011 22:00:58 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=1816</guid>
		<description><![CDATA[Python provides a query API through its ORM library. All models can define queries using the most common aggregates like Sum, Avg, Max, Min and Count. Since you cannot use SQL functions with Django aggregates, you must define your own aggregates to be used with functions. For example in one of my recent projects I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p>Python provides a query API through its ORM library. All models can define queries using the most common aggregates like Sum, Avg, Max, Min and Count. Since you cannot use SQL functions with Django aggregates, you must define your own aggregates to be used with functions. For example in one of my recent projects I&#8217;ve used some MySQL functions &mdash; and PostgreSQL functions on the PostgreSQL version &mdash; with the Sum and Avg aggregates. You can build your own Aggregates and place them, for example, on the <tt>app/aggregates.py</tt> module.</p>
<p><span id="more-1816"></span></p>
<p>Django works using two layers to build queries. The first layer is model descriptive and the second one is the SQL compiler, which has its basis on the model description layer to construct each query. So you need to define both classes to build your own Aggregate &mdash; and skip the Django side processing that requires some complex queries. The first one is the SQL layer, which must define the proper SQL elements to be used to construct the query aggregate.</p>
<pre class="brush: python;">
from django.db import models

class SumWithFunctionSQL(models.sql.aggregates.Aggregate):
    sql_function = 'SUM'
    sql_template = '%(function)s( %(proc_func)s( %(field)s ) )'

class SumWithFunction(models.Aggregate):
    name = 'Sum'

    def add_to_query(self, query, alias, col, source, is_summary):
        aggregate = SumWithFunctionSQL(col,
                                       source=source,
                                       is_summary=is_summary,
                                       **self.extra)
        query.aggregates[alias] = aggregate
</pre>
<p>On the example above, we will provide <tt>proc_func</tt> extra argument to the aggregate constructor, so we will be able to use the <tt>Sum</tt> aggregate wrapping a function call on each processed row, which will be provided by the <tt>proc_func</tt> argument. On the next example, we will see an <tt>If</tt> construct to be used with MySQL, which can be replaced by <tt>Case</tt> statements on PostgreSQL.</p>
<pre class="brush: python;">
from django.db import models
from django.conf import settings

sum_if_sql_template = '%(%s)s(IF(%(%s)s, %(%s)s, %(%s)s))' %
                      ('function',
                       'condition',
                       'when_true',
                       'when_false')

class SumIfSQL(models.sql.aggregates.Aggregate):
    sql_function = 'SUM'
    sql_template = sum_if_sql_template

class SumIf(models.Aggregate):
    name = 'Sum'

    def add_to_query(self, query, alias, col, source, is_summary):
        aggregate = SumIfSQL(col,
                             source=source,
                             is_summary=is_summary,
                             **self.extra)
        query.aggregates[alias] = aggregate
</pre>
<p>The example above uses the <tt>condition</tt>, <tt>when_true</tt> and <tt>when_false</tt> extra arguments to be passed to the aggregate constructor, where the <tt>condition</tt> holds the SQL condition which must meet the SQL layer, <tt>when_true</tt> the value to be used in the Sum aggregate when the condition evaluates to true and <tt>when_false</tt> hold the value to be used when the condition evaluates to false.</p>
<pre class="brush: python;">
query = AccountDetail.
        objects.
        values('account', 'currency', 'interest', 'dividend').
        annotate(quantity_sum=Sum('quantity'),
                 amount_sum=Sum('amount'),
                 applied_interest=SumIf('amount',
                                        condition='amount &gt; 0.0',
                                        when_true='amount * interest',
                                        when_false='0.0'),
                 applied_dividend=SumIf('amount',
                                        condition='amount &lt; 0.0',
                                        when_true='amount * dividend',
                                        when_false='0.0')).
        order_by('account', 'currency', 'interest', 'dividend').
        filter(**filter)
details = query.all()
return details
</pre>
<p>On the example above we have made a simple usage of the <i>SumIf</i> aggregate. I hope that you will like how it works and build a similar solution. The examples here do are not using the SQL layer compiler due to its complexity, but if you study enough you will be able to use the SQL compiler.</p>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2011. |
<a href="http://coder.cl/2011/09/custom-aggregates-on-django/">Permalink</a> |
<a href="http://coder.cl/2011/09/custom-aggregates-on-django/#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2011/09/custom-aggregates-on-django/&title=custom aggregates on django">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/09/custom-aggregates-on-django/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>hmac performance measurement in python</title>
		<link>http://coder.cl/2011/09/hmac-performance-measurement-in-python/</link>
		<comments>http://coder.cl/2011/09/hmac-performance-measurement-in-python/#comments</comments>
		<pubDate>Sat, 24 Sep 2011 20:57:21 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=1808</guid>
		<description><![CDATA[HMAC is a well known algorithm used in authentication mechanisms. It uses message digest algorithms, such as MD5, SHA1 and SHA256. Python provides modules which allows you to create HMAC shadowed passwords to be used in your applications. The implementation is based on hashlib and hmac, and both modules are using the OpenSSL library wrapped [...]]]></description>
			<content:encoded><![CDATA[<p>HMAC is a well known algorithm used in authentication mechanisms. It uses message digest algorithms, such as MD5, SHA1 and SHA256. Python provides modules which allows you to create HMAC shadowed passwords to be used in your applications. The implementation is based on <tt>hashlib</tt> and <tt>hmac</tt>, and both modules are using the OpenSSL library wrapped in Python using the Python C/API. I&#8217;ve measured the behaviour of those algorithms in a threaded environment, to let me know which one is the more convenient to be used in sites without creating heavy CPU loads when creating user accounts in batch processes, since we are using a VPS which is handles its costs based on resource usage.</p>
<p><span id="more-1808"></span></p>
<p>The first observation is the fact that all available algorithms have an exponential increment on CPU usage while they increase its complexity. Since <tt>hashlib</tt> do not uses locking, it is transparent to the module to be used in a threaded environment. There is no resource locking or thread blocking routines in the module, so it do not requires synchronisation, and even it doesn&#8217;t use synchronisation internally. Probably OpenSSL uses some kind of it, but it is transparent, without affecting the runtime.</p>
<div class="figure">
<a href='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/09/python-data-100.jpg' title='HMAC Algorithm Performance' target="_blank"><img src='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/09/python-data-100.jpg' border='0' alt='HMAC Algorithm Performance' /></a></p>
<div>HMAC Algorithm Performance</div>
</div>
<p>Is well known that the probability of hash collisions on lower algorithms, like MD5 and SHA1 is higher than SHA512 and similar ones. Probably we just need to use a safer algorithm that offers a better performance than others, but offers a safer digest like SHA256 as minimal requirement, so we store passwords for a long time without the risk of finding some password cracker that can crash our security system. We know that MD5 and SHA1 are not safe any more.</p>
<blockquote><p>
If you&#8217;re willing to spend about 2,000 USD and a week or two picking up CUDA, you can put together your own little supercomputer cluster which will let you try around 700,000,000 passwords a second. And that rate you&#8217;ll be cracking those passwords at the rate of more than one per second. [Source <a href="http://codahale.com/how-to-safely-store-a-password/" title="bcrypt">bcrypt</a>].
</p></blockquote>
<p>On a threaded environment you can see that lower algorithms have a similar performance and behaviour. All of them seems to have a very similar behaviour, where thread creation is time consuming, but once all threads are created it begins increasing the time required to complete the task.</p>
<div class="figure">
<a href='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/09/profile-data-thread-non-strong.jpg' title='Non-Strong HMAC Algorithms Performance in Threaded Environment' target="_blank"><img src='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/09/profile-data-thread-non-strong.jpg' border='0' alt='Non-Strong HMAC Algorithms Performance in Threaded Environment' /></a></p>
<div>Non-Strong HMAC Algorithms Performance in Threaded Environment</div>
</div>
<p>The performance for strong algorithms seem to be similar, but more time consuming that the lower ones. Also on higher algorithms, such as SHA384 and SHA512 have a more stable behaviour, using more homogeneous times to complete the task. Seems that the best option by stability and being stronger enough is SHA384, which has better performance than SHA512 and it reliable enough to store strong passwords. The bcrypt advice should be taken seriously by some institutions that require strong password storage, but mere applications like blogs and similar ones, do not require that kind of strength.</p>
<div class="figure">
<a href='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/09/profile-data-thread-strong.jpg' title='Strong HMAC Algorithms Performance in Threaded Environment' target="_blank"><img src='http://coder.cl.qfl.wpcdn.arcostream.com/uploads/2011/09/profile-data-thread-strong.jpg' border='0' alt='Strong HMAC Algorithms Performance in Threaded Environment' /></a></p>
<div>Strong HMAC Algorithms Performance in Threaded Environment</div>
</div>
<p>Probably we will adopt SHA384 as our best option. We need that process to be finished early and we do not have a TRNG installed on the machine to obtain enough entropy. We are thinking to buy an <a href='http://www.entropykey.co.uk/'>Entropy Key</a> device to guarantee a better performance on cryptographic routines, like randomise initial passwords and bring a better performance on the server. Also we are thinking to buy some devices from <a href='http://soekris.com/'>Soekris Engineering</a> to enhance the performance of certain tasks. I well known that cryptographic tasks, like using TLS/SSL connections over TCP like HTTPS can lead to heavy CPU usage. I hope that all of those tools will serve us well.</p>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2011. |
<a href="http://coder.cl/2011/09/hmac-performance-measurement-in-python/">Permalink</a> |
<a href="http://coder.cl/2011/09/hmac-performance-measurement-in-python/#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2011/09/hmac-performance-measurement-in-python/&title=hmac performance measurement in python">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/09/hmac-performance-measurement-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>inotify and python</title>
		<link>http://coder.cl/2011/08/inotify-and-python/</link>
		<comments>http://coder.cl/2011/08/inotify-and-python/#comments</comments>
		<pubDate>Sun, 14 Aug 2011 16:16:46 +0000</pubDate>
		<dc:creator>dmw</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=1742</guid>
		<description><![CDATA[As you know Linux provides the inotify(7) API to monitor file system events using a convenient system calls that allows you to retrieve information about file system changes. The system calls that provides that API are implemented in a wide variety of high level programming languages, such as Python, Perl, and others. The Python implementation [...]]]></description>
			<content:encoded><![CDATA[<p>As you know Linux provides the <strong>inotify(7)</strong> API to monitor file system events using a convenient system calls that allows you to retrieve information about file system changes. The system calls that provides that API are implemented in a wide variety of high level programming languages, such as Python, Perl, and others. The Python implementation to wrap those calls has at least three variants, so I will review each of them in this article.</p>
<p><span id="more-1742"></span></p>
<p>The two well known variants of the <strong>inotify(7)</strong> API for Python are <tt>inotifyx</tt> and <tt>pyinotify</tt>. The most complete file system notification API is provided by the <tt>pyinotify</tt> Python package.</p>
<p>The <tt>inotifyx</tt> interface provides a simple function based API, with an object wrapper for event objects through the <tt>InotifyEvent</tt> class. It lacks a main loop for event notifications, so you need to create the event loop by your own. Also it is small and simple, do not requires too much knowledge of the notification API.</p>
<p>So, the best option for me is the <tt>pyinotify</tt> interface, which provides full integration with the file system event API, so you can track with those events with a full object oriented implementation where you can handle any kind of events over files and directories. One of my recent projects is a log monitor daemon that integrates file system monitoring tasks to log storage on a RDBMS engine. Each time that a log is written, it examines the remaining lines from the current offset to the end of the file that has been written, and extracts the log entries to be stored on the RDBMS. The event chosen to be handled in this case is the IN_CLOSE_WRITE event, which indicates that the directory has suffered a file writes and file close events.</p>
<pre class="brush: python;">

class WatchLogProceesing(pyinotify.ProcessEvent):
    """
    This class is a monitor for those files that are placed
    on the log directory. Once the system writes those system
    and closes them, this class notifies to its handler to
    process the event.
    """
    def process_IN_CLOSE_WRITE(self, event):
        """
        Process the close/write event.
        """
        daemon_log("%s: processing %s"
                   % (s.argv[0], event.pathname))
        process_input(event.pathname)

def monitor_dir(directory):
    """
    Starts monitoring a directory for changes using the
    pyinotify API
    """
    try:
        wmn = pyinotify.WatchManager()
        notifier = pyinotify.Notifier(wmn,
                                      WatchLogProceesing())
        wmn.add_watch(directory,
                      pyinotify.IN_CLOSE_WRITE)
        notifier.loop()
    except Exception, exc:
        print(str(exc))
        print(format_exc(exc))
</pre>
<p>The <tt>notifier.loop()</tt> call, means that the notifier will enter in an event loop handler, catching all included events in the <tt>WatchManager.add_watch()</tt> call. This API is very similar to the <a href='http://people.freebsd.org/~jlemon/papers/kqueue.pdf' title='Kqueue: A generic and scalable event notification facility'>BSD Kqueue</a> interface, but less complete, since <strong>inotify(7)</strong> only can handle file system events, and <strong>kqueue(2)</strong> can handle <i>file descriptor</i> events, so you can apply kqueue system calls to sockets and related stuff, making of kqueue more universal system calls for file descriptor events. Try playing with both, kqueue and inotify API, you will find it interesting :)</p>
<hr />
<p><small>© dmw for <a href="http://coder.cl">coder . cl</a>, 2011. |
<a href="http://coder.cl/2011/08/inotify-and-python/">Permalink</a> |
<a href="http://coder.cl/2011/08/inotify-and-python/#comments">No comment</a> |
Add to
<a href="http://del.icio.us/post?url=http://coder.cl/2011/08/inotify-and-python/&title=inotify and python">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/08/inotify-and-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for ( 2e96b290f6c5ccd2bc6830557c0aba6e coder.cl/category/programming/python/feed/ ) in 0.53567 seconds, on May 15th, 2012 at 5:31 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on May 22nd, 2012 at 5:31 am UTC -->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- Quick Cache Is Fully Functional :-) ... A Quick Cache file was just served for ( 2e96b290f6c5ccd2bc6830557c0aba6e coder.cl/category/programming/python/feed/ ) in 0.00081 seconds, on May 18th, 2012 at 12:09 pm UTC. -->
