<?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>Sinax</title>
	<atom:link href="http://www.sinax.be/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sinax.be</link>
	<description>IT Architects</description>
	<lastBuildDate>Wed, 19 Jun 2013 05:04:55 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Python Snippet: List comprehensions</title>
		<link>http://www.sinax.be/2013/06/19/python-snippet-list-comprehensions/</link>
		<comments>http://www.sinax.be/2013/06/19/python-snippet-list-comprehensions/#comments</comments>
		<pubDate>Wed, 19 Jun 2013 04:50:38 +0000</pubDate>
		<dc:creator>Michael Anckaert</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[python snippet]]></category>

		<guid isPermaLink="false">http://www.sinax.be/?p=1136</guid>
		<description><![CDATA[Today&#8217;s Python Snippet is the list comprehension. A list comprehension is a Python construct that performs an action on every element of the list. When you use a list comprehension you don&#8217;t have to use a for loop to loop over all elements of the list. You can keep your code cleaner, sim...]]></description>
				<content:encoded><![CDATA[<p>Today&#8217;s Python Snippet is the list comprehension. A list comprehension is a Python construct that performs an action on every element of the list. When you use a list comprehension you don&#8217;t have to use a for loop to loop over all elements of the list.<br />
You can keep your code cleaner, simpler and as a result less bug prone.</p>
<p>Here&#8217;s a code sample with a for loop:</p>
<p>l1 = [0, 1, 2, 3, 4, 5]<br />
l2 = []<br />
for i in l1:<br />
    l2.append(i*2)</p>
<p>A simple for loop that takes a list and adds the double of each element to another list.<br />
This is a fairly common construction in beginner Python code, loop over a list and perform actions on each element.<br />
Now we&#8217;ll write the same functionality using a list comprehension:<br />
l = [0, 1, 2, 3, 4, 5]<br />
l = [i*2 for i in l]</p>
<p>This code is fairly simple even though it seems strange. Our list comprehension on line #2 contains roughly the same format as a for loop. The part for i in l obviously loops over all elements of our list. The part in front of the for-loop like part i*2 simply tells the list comprehension what to do with the current element.<br />
You can read this list comprehension as:<br />
return i*2 for i in l<br />
The return part of our list comprehension is fairly limited. Should you want to do more advanced things you should define a function first and use that in the return part of your list comprehension:</p>
<p>def complex_function(val):<br />
    # complex calculation<br />
    # be sure to return a value<br />
    return val<br />
l = [0, 1, 2, 3, 4, 5]<br />
l = [complex_function(i) for i in l]</p>
<p>In our list comprehensions, we&#8217;ve seen two &#8216;parts&#8217; of our list comprehension. The return part that gets the current element, performs an action and returns it in a new list. The second part is the loop part of the list, this part tells our list comprehension what list to iterate.</p>
<p>Now we&#8217;ll add a third part, which I call the filter part:</p>
<p>l = [0, 1, 2, 3, 4, 5, 6, 7, 8]<br />
l = [i*2 for i in l if i % 2 == 0]<br />
After the loop part we&#8217;ve added the header of an if statement. If this part is True for the current element, the return part of our list comprehension will be executed.</p>
<p>There&#8217;s lots more you can do with list comprehensions like multiple lists or nest list comprehensions.</p>
<p>The official Python documentation about list comprehensions is here: <a href="http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions">http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sinax.be/2013/06/19/python-snippet-list-comprehensions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Django validators</title>
		<link>http://www.sinax.be/2013/06/19/simple-django-validators/</link>
		<comments>http://www.sinax.be/2013/06/19/simple-django-validators/#comments</comments>
		<pubDate>Wed, 19 Jun 2013 04:48:39 +0000</pubDate>
		<dc:creator>Michael Anckaert</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[validators]]></category>

		<guid isPermaLink="false">http://www.sinax.be/?p=1135</guid>
		<description><![CDATA[Creating your own validators for your Django models or forms is very easy. In this post I&#8217;ll show you how to create a simple validator and apply it to your models or forms. For future reference, this post was writting using Django 1.4 A simple validator can be a Python function that takes a va...]]></description>
				<content:encoded><![CDATA[<p>Creating your own validators for your Django models or forms is very easy.<br />
In this post I&#8217;ll show you how to create a simple validator and apply it to your models or forms.</p>
<p>For future reference, this post was writting using Django 1.4</p>
<p>A simple validator can be a Python function that takes a value, checks this value against your logic and raises a ValidationError if the value fails to a agree to your rules.</p>
<p>In our example below, we&#8217;ll write a validator that checks if a given value is greater than or equal to 30.</p>
<p>from django.core.exceptions import ValidationError<br />
def validate_product_price(val):<br />
    if val >= 30:<br />
        raise ValidationError(u&#8221;Not a valid product price&#8221;)</p>
<p>After importing the required exception from the Django module we define our function which takes one parameter, val. Our function simple does some checks on this value and raises the ValidationError exception with some extra information.<br />
Now that we have our validator function defined we can assign it to our model:</p>
<p>class Product(models.Model):<br />
    product_code = models.CharField(max_length=5, unique=True)<br />
    name = models.CharField(max_length=30)<br />
    price = models.FloatField(validators=[validate_product_price])<br />
    # Rest of our model definition&#8230;</p>
<p>When using this model in a form (Form or ModelForm) the calling of the fields clean() method will result in your custom validator being called.<br />
In a later post I&#8217;ll detail the validation flow of Django forms, models and fields. We will also see how you can validate a field depending other field values.</p>
<p>Django has a number of built-in validators that can be used in your code. They take care of various general validation requirements such as email, IP address, length or value validation. You can check out the official Django documentation about validators here: <a href="https://docs.djangoproject.com/en/1.4/ref/validators/">https://docs.djangoproject.com/en/1.4/ref/validators/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sinax.be/2013/06/19/simple-django-validators/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Nginx load balancing</title>
		<link>http://www.sinax.be/2013/06/17/simple-nginx-load-balancing/</link>
		<comments>http://www.sinax.be/2013/06/17/simple-nginx-load-balancing/#comments</comments>
		<pubDate>Mon, 17 Jun 2013 10:02:39 +0000</pubDate>
		<dc:creator>Michael Anckaert</dc:creator>
				<category><![CDATA[Web Technology]]></category>
		<category><![CDATA[nginx]]></category>

		<guid isPermaLink="false">http://www.sinax.be/?p=1128</guid>
		<description><![CDATA[Using Nginx it&#8217;s quite simple to create a basic load balancing setup. Nginx is very popular these days as a Reverse Proxy and getting it to work is actually very simple. The configuration below tells Nginx to listen for HTTP traffic on port 80 that was sent to www.project.com, all requests rec...]]></description>
				<content:encoded><![CDATA[<p>Using Nginx it&#8217;s quite simple to create a basic load balancing setup. Nginx is very popular these days as a Reverse Proxy and getting it to work is actually very simple. <span id="more-1128"></span></p>
<p>The configuration below tells Nginx to listen for HTTP traffic on port 80 that was sent to www.project.com, all requests received are then distributed over the upstream servers that are defined:</p>
<pre>http {
  upstream myservers {
    server 192.168.0.2:8000;
    server 192.168.0.3:8000;
    server 192.168.0.4:8000;    
    server 192.168.0.5:8000;
  }

  server {
    listen 80;
    server_name www.project.com;
    location / {
      proxy_pass http://myservers;
    }
  }
}</pre>
<p>&nbsp;</p>
<p>The setup above will treat all backend servers equally. Using the <strong>weight</strong> option it is possible to direct more traffic to a server, for example when it&#8217;s on a more powerful machine:</p>
<pre>server 192.168.0.2:8000 weight=4;</pre>
<p>&nbsp;</p>
<p>You can check out everything about <a title="Nginx configuration" href="http://wiki.nginx.org/Configuration" target="_blank">Nginx configuration on the Wiki</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sinax.be/2013/06/17/simple-nginx-load-balancing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python Snippet: Enumerate your lists</title>
		<link>http://www.sinax.be/2013/04/19/python-snippet-enumerate-your-lists/</link>
		<comments>http://www.sinax.be/2013/04/19/python-snippet-enumerate-your-lists/#comments</comments>
		<pubDate>Fri, 19 Apr 2013 18:52:22 +0000</pubDate>
		<dc:creator>Michael Anckaert</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[python snippet]]></category>

		<guid isPermaLink="false">http://www.sinax.be/?p=1137</guid>
		<description><![CDATA[In this Python snippet we will see how to enumerate your lists. This short piece of code will help you cut down development time, remove those little counters from your code and generally make for better readable code. Like always, let&#8217;s start with an &#8216;bad&#8217; example: l = &#91;'car',...]]></description>
				<content:encoded><![CDATA[<p>In this Python snippet we will see how to enumerate your lists. This short piece of code will help you cut down development time, remove those little counters from your code and generally make for better readable code.</p>
<p>Like always, let&#8217;s start with an &#8216;bad&#8217; example:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="python" style="font-family:monospace;">l <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: #483d8b;">'car'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'bike'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'boat'</span><span style="color: #66cc66;">,</span> <span style="color: #483d8b;">'plane'</span><span style="color: black;">&#93;</span>
j <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">1</span>
<span style="color: #ff7700;font-weight:bold;">for</span> transport <span style="color: #ff7700;font-weight:bold;">in</span> l:
<span style="color: #ff7700;font-weight:bold;">print</span> j<span style="color: #66cc66;">,</span> transport
j +<span style="color: #66cc66;">=</span> <span style="color: #ff4500;">1</span></pre></td></tr></table></div>

<p>This trivial example iterates over a list and we keep track of the element using a counter. We then print out every element along with the counter.<br />
Now our example using the enumerate() function:</p>
<p>l = ['car', 'bike', 'boat', 'plane']<br />
for j, transport in enumerate(l):<br />
print j+1, transport</p>
<p>As we can see the enumerate function offers us the element and the index of the element (that&#8217;s why we use j+1, 0 based indexes!).<br />
When we look at the enumerate function we see that it returns an enumerate object. This enumerate object contains an iterator that gives us a list of tuples:</p>
<p>&gt;&gt;&gt; l = ['car', 'bike', 'boat', 'plane']<br />
&gt;&gt;&gt; list(enumerate(l))<br />
[(0, 'car'), (1, 'bike'), (2, 'boat'), (3, 'plane')]</p>
<p>So in our for loop we go over all tuples and unpack them into j, transport.<br />
Extra tip: pass along an extra parameter to the enumerate function to define the start the indexing:</p>
<p>&gt;&gt;&gt; l = ['car', 'bike', 'boat', 'plane']<br />
&gt;&gt;&gt; list(enumerate(l, 1))<br />
[(1, 'car'), (2, 'bike'), (3, 'boat'), (4, 'plane')]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sinax.be/2013/04/19/python-snippet-enumerate-your-lists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Klantenbeheer</title>
		<link>http://www.sinax.be/portfolio/klantenbeheer/</link>
		<comments>http://www.sinax.be/portfolio/klantenbeheer/#comments</comments>
		<pubDate>Wed, 02 Jan 2013 10:09:42 +0000</pubDate>
		<dc:creator>Michael Anckaert</dc:creator>
		
		<guid isPermaLink="false">http://www.sinax.be/?post_type=portfolio&#038;p=807</guid>
		<description><![CDATA[De module klantenbeheer biedt u de tools om uw klantenbestand te beheren. Deze module is geïntegreerd met onze modules: &#160;...]]></description>
				<content:encoded><![CDATA[<p>De module klantenbeheer biedt u de tools om uw klantenbestand te beheren.</p>
<p>Deze module is geïntegreerd met onze modules:</p>
<div class="shortcode-list bullets-blue">
<ul>
<li>Facturatie</li>
</ul>
</div>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sinax.be/portfolio/klantenbeheer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Facturatie</title>
		<link>http://www.sinax.be/portfolio/facturatie/</link>
		<comments>http://www.sinax.be/portfolio/facturatie/#comments</comments>
		<pubDate>Wed, 02 Jan 2013 10:02:33 +0000</pubDate>
		<dc:creator>Michael Anckaert</dc:creator>
		
		<guid isPermaLink="false">http://www.sinax.be/?post_type=portfolio&#038;p=801</guid>
		<description><![CDATA[Met de Sinax facturatie genereert u op eenvoudige manier facturen en offerten. Deze module is geïntegreerd met onze modules: &#160;...]]></description>
				<content:encoded><![CDATA[<p>Met de Sinax facturatie genereert u op eenvoudige manier facturen en offerten.</p>
<p>Deze module is geïntegreerd met onze modules:</p>
<div class="shortcode-list bullets-blue">
<ul>
<li>Klantenbeheer</li>
</ul>
</div>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sinax.be/portfolio/facturatie/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
