<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>loutontheweb.co.cc: Latest entries</title><link>http://loutontheweb.co.cc/weblog/</link><description>Latest entries</description><language>en-us</language><lastBuildDate>Sun, 31 Jul 2011 02:58:37 -0500</lastBuildDate><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/loutontheweb" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="loutontheweb" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><title>Python tip</title><link>http://loutontheweb.co.cc/categories/python/python-tip/</link><description>&lt;p&gt;Instead of:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if one == None or two == None or three == None:
    pass
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;use
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if None in (one, two, three):
    pass
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;or
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if any([x == None for x in one, two, three]):
    pass
&lt;/code&gt;&lt;/pre&gt;</description><pubDate>Sun, 31 Jul 2011 02:58:37 -0500</pubDate><guid>tag:loutontheweb.co.cc,2011-07-31:/categories/python/python-tip/</guid><category>Python</category></item><item><title>Django nonrelational batch support</title><link>http://loutontheweb.co.cc/categories/django/django-nonrelational-batch-support/</link><description>&lt;p&gt;I have implemented custom backend for Django, that let you use batch operations with Django and Google App Engine. You should be aware that API is not stable yet and I did not get official approval from django-nonrel team yet (although there is some progress).
&lt;/p&gt;

&lt;h3&gt;Get started&lt;/h3&gt;
&lt;p&gt;Get all needed libraries:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;hg clone https://bitbucket.org/wkornewald/django-testapp batch-save-test
hg clone https://bitbucket.org/wkornewald/django-nonrel django-nonrel-bs
hg clone https://bitbucket.org/wkornewald/djangoappengine djangoappengine-bs
hg clone https://bitbucket.org/wkornewald/djangotoolbox djangotoolbox-bs
hg clone https://bitbucket.org/wkornewald/django-dbindexer django-dbindexer-bs
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Apply patches:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;hg -R django-nonrel-bs pull -u https://bitbucket.org/vladimir_webdev/django-nonrel
hg -R djangoappengine-bs pull -u https://bitbucket.org/vladimir_webdev/ djangoappengine
hg -R django-dbindexer-bs pull -u https://bitbucket.org/vladimir_webdev/django-dbindexer
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Create symbolic links to fetched libraries:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;cd batch-save-test
ln -s ../django-nonrel-bs/django
ln -s ../djangoappengine-bs djangoappengine
ln -s ../djangotoolbox-bs/djangotoolbox
ln -s ../django-dbindexer-bs/dbindexer
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Try to run server:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;./manage.py runserver
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now you should be able to use batch operations with Django.
&lt;/p&gt;

&lt;h3&gt;Usage&lt;/h3&gt;
&lt;p&gt;The simplest example of using batch operations looks like this:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;from __future__ import with_statement
from django.db.models import BatchOperation
with BatchOperation() as op:
    for i in range(100):
        op.save(Post(title='Title %d' % i, text='Text %d' % i))
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;That's it. Internally code above will create two pools: one for save operations and one for delete operations. When pool is filled entries will be flushed to backend and backend is responsible to batch save them. Available configuration options are:
&lt;/p&gt;
&lt;ul&gt;
 &lt;li&gt;
     pool_size (save_pool_size, delete_pool_size) - number of models instances stored in pool (500 by default). If you experience problems with memory you can try to lower this value.
 &lt;/li&gt;

 &lt;li&gt;
     batch_size (save_batch_size, delete_batch_size) - number of models instances that will be flushed in one batch operation (100 by default). If you experience problems with datastore timeout exceptions you can try to lower this value.
 &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We need to make differences between pool_size and batch_size, because theoretically Django can be configured to use several databases. So pool can contain instances for different databases.
&lt;/p&gt;
&lt;p&gt;You can configure BatchOperation() like this:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;config = dict(default=dict(pool_size=50,
                           save_batch_size=50,
                           delete_pool_size=50))
with BatchOperation(config) as op:
    for p in Post.objects.all()[:100]:
        op.delete(p)
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Stats&lt;/h3&gt;
&lt;p&gt;I have tested batch saves with such views:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;def plain_save(request):
    for i in range(100):
        Post.objects.create(title='Title %d' % i, text='Text %d' % i)
    return http.HttpResponse('Ok')

def batch_save(request):
    with BatchOperation() as op:
        for i in range(100):
            op.save(Post(title='Title %d' % i, text='Text %d' % i))
    return http.HttpResponse('Ok')
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;and got following results from appstats:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;"GET /plain_save/" 200 real=2734ms cpu=1720ms api=6583ms overhead=21ms (100 RPCs)
"GET /batch_save/" 200 real=609ms cpu=193ms api=6516ms overhead=0ms (1 RPC)
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Feedback&lt;/h3&gt;
&lt;p&gt;Feel free to add feedback/report bugs at &lt;a href="http://groups.google.com/group/django-non-relational"&gt;django-nonrel user group&lt;/a&gt;.
&lt;/p&gt;</description><pubDate>Tue, 05 Apr 2011 10:19:54 -0500</pubDate><guid>tag:loutontheweb.co.cc,2011-04-05:/categories/django/django-nonrelational-batch-support/</guid><category>Django</category></item><item><title>Links for Jan. 9, 2011</title><link>http://loutontheweb.co.cc/categories/links/links-for-jan-9-2011/</link><description>&lt;ul&gt;
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://djangoweek.ly/"&gt;Weekly links about django&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;DjangoWeekly is a free newsletter, keeping you you up-to-date with what’s happening in Django.
&lt;/p&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://neopythonic.blogspot.com/2011/01/new-app-engine-datastore-api.html"&gt;New App Engine Datastore API&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;Guido Van Rossum presents new asynchronous App Engine Datastore API, which is based on Python generators and "trampoline" framework.
&lt;/p&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://lukeplant.me.uk/blog/posts/class-based-views-and-dry-ravioli/"&gt;Class Based Views and Dry Ravioli&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;Django's new class based views are very cool. I am starting to clean up an existing project using them, and lots of existing views are turning into declarative code. But it makes me worried about the &lt;a href="http://c2.com/cgi/wiki?RavioliCode"&gt;ravioli effect&lt;/a&gt;.
&lt;/p&gt;
    &lt;/li&gt;
&lt;/ul&gt;</description><pubDate>Sun, 09 Jan 2011 11:25:11 -0600</pubDate><guid>tag:loutontheweb.co.cc,2011-01-09:/categories/links/links-for-jan-9-2011/</guid><category>Links</category></item><item><title>Links for Nov. 11, 2010</title><link>http://loutontheweb.co.cc/categories/links/links-for-nov-11-2010/</link><description>&lt;ul&gt;
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://google-styleguide.googlecode.com/svn/trunk/pyguide.html"&gt;Google Python Style Guide&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;This style guide contains many details that are initially hidden from view. They are marked by the triangle icon, which you see here on your left. Click it now. You should see "Hooray" appear below.
&lt;/p&gt;
    &lt;/li&gt;
  
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://pythonconquerstheuniverse.wordpress.com/2010/10/20/a-globals-class-pattern-for-python/"&gt;A Globals Class pattern for Python&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;A series of articles: &lt;a href="http://pythonconquerstheuniverse.wordpress.com/2010/10/20/a-globals-class-pattern-for-python/"&gt;A Globals Class pattern for Python&lt;/a&gt;, &lt;a href="http://pythonconquerstheuniverse.wordpress.com/2010/10/26/a-globals-module-pattern/"&gt;A Globals Module pattern&lt;/a&gt;, &lt;a href="http://pythonconquerstheuniverse.wordpress.com/2010/10/23/an-arguments-container-pattern/"&gt;An Arguments Container pattern&lt;/a&gt;.
&lt;/p&gt;
    &lt;/li&gt;
  
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://eli.thegreenplace.net/2010/11/06/bob-a-scheme-interpreter-compiler-and-vm-in-python/"&gt;Bob: a Scheme interpreter, compiler, and VM in Python&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;A Scheme interpreter, compiler, and VM written in pure Python.
&lt;/p&gt;
    &lt;/li&gt;
  
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://sites.google.com/site/pythonexcel/"&gt;Working with Excel Files in Python&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;Python-excel.org seems to be down, but http://sites.google.com/site/pythonexcel/ is still alive. Hooray!
&lt;/p&gt;
    &lt;/li&gt;
&lt;/ul&gt;</description><pubDate>Thu, 11 Nov 2010 07:50:56 -0600</pubDate><guid>tag:loutontheweb.co.cc,2010-11-11:/categories/links/links-for-nov-11-2010/</guid><category>Links</category></item><item><title>Links for Nov. 2, 2010</title><link>http://loutontheweb.co.cc/categories/links/links-for-nov-2-2010/</link><description>&lt;ul&gt;
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://www.doughellmann.com/PyMOTW/contents.html"&gt;Python Module of the Week&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;PyMOTW is a series of blog posts written by Doug Hellmann. It was started as a way to build the habit of writing something on a regular basis. The focus of the series is building a set of example code for the modules in the Python standard library.
&lt;/p&gt;
    &lt;/li&gt;
  
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://www.doughellmann.com/articles/how-tos/python-exception-handling/index.html"&gt;Python Exception Handling Techniques&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;Error reporting and processing through exceptions is one of Python’s key features. Care must be taken when handling exceptions to ensure proper application cleanup while maintaining useful error reporting.
&lt;/p&gt;
    &lt;/li&gt;
  
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://code.google.com/appengine/articles/scaling/overview.html"&gt;Google App Engine: Best practices&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;Best practices for writing scalable applications for Google App Engine.
&lt;/p&gt;
    &lt;/li&gt;
  
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://plumberjack.blogspot.com/2010/09/configuring-logging-for-web.html"&gt;Configuring logging for Web applications&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;You can easily configure your web applications with Python logging, so that logged messages can go into web-application-specific log files (or other destinations) even for multiple web applications in the same Python process, and have those logs contain request-specific information such as remote user IP address, request method and others.
&lt;/p&gt;
    &lt;/li&gt;
  
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://glyf.livejournal.com/70684.html"&gt;Python: Singleton pattern&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;The right way to implement Singleton pattern.&lt;/p&gt;
    &lt;/li&gt;
&lt;/ul&gt;</description><pubDate>Tue, 02 Nov 2010 07:05:52 -0500</pubDate><guid>tag:loutontheweb.co.cc,2010-11-02:/categories/links/links-for-nov-2-2010/</guid><category>Links</category></item><item><title>Links for Oct. 27, 2010</title><link>http://loutontheweb.co.cc/categories/links/links-for-oct-27-2010/</link><description>&lt;ul&gt;
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://groups.google.com/group/django-developers/browse_thread/thread/eee872b03286075e?pli=1"&gt;Django on Google App Engine via SQL (not nonrel) &lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;There are good chances that Django 1.3 will be fully supported on Google App Engine.
&lt;/p&gt;
    &lt;/li&gt;
  
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://coffeeonthekeyboard.com/bleach-html-sanitizer-and-auto-linker-for-django-344/"&gt;Bleach, HTML sanitizer and auto-linker&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;Bleach is a whitelist-based HTML sanitizer and auto-linker in Python, built on html5lib, for AMO and SUMO and released under the BSD license.
&lt;/p&gt;
    &lt;/li&gt;
  
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://goo.gl/1rwf"&gt;Old and New Python Versions&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;Old and new versions of Python, forward- and back-ported from other releases of Ubuntu and Debian.
&lt;/p&gt;
    &lt;/li&gt;
  
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://www.mysqlperformanceblog.com/2010/10/25/mysql-limitations-part-3-subqueries/"&gt;MySQL subqueries limitations&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;MySQL subqueries don't work as expected, use joins instead.
&lt;/p&gt;
    &lt;/li&gt;
  
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://www.agmweb.ca/blog/andy/2286/"&gt;When App Engine went wrong&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;App Engine fail story.&lt;/p&gt;
    &lt;/li&gt;
&lt;/ul&gt;</description><pubDate>Wed, 27 Oct 2010 14:31:56 -0500</pubDate><guid>tag:loutontheweb.co.cc,2010-10-27:/categories/links/links-for-oct-27-2010/</guid><category>Links</category></item><item><title>Links for Oct. 21, 2010</title><link>http://loutontheweb.co.cc/categories/links/links-for-oct-21-2010/</link><description>&lt;ul&gt;
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://boodebr.org/main/python/build-windows-extensions"&gt;Building Python extension with mingw and cygwin for Windows&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;Building Python extension with mingw and cygwin for Windows.
&lt;/p&gt;
    &lt;/li&gt;
  
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://stackoverflow.com/questions/1247863/monty-hall-problem/1247888"&gt;Monty Hall Problem&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;Monty Hall Problem coded in Python. So easy to be coded and so hard to be understood on intuitive level...
&lt;/p&gt;
    &lt;/li&gt;
  
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://css-tricks.com/css-font-size/"&gt;CSS: px vs em vs % vs pt&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;When using a value, you need to declare a unit of measure which itself has four options. Which is best?
&lt;/p&gt;
    &lt;/li&gt;
  
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://37signals.com/svn/posts/2608-ryans-talk-at-future-of-web-apps-2010-london"&gt;Steps of creating a web app&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;Steps of creating a web app including modeling, sketching, HTML, Photoshop explorations and moving from static mockups to live running code.
&lt;/p&gt;
    &lt;/li&gt;
  
    &lt;li&gt;
      &lt;h5&gt;
        &lt;a href="http://www.stereoplex.com/blog/python-unicode-and-unicodedecodeerror"&gt;Python, str, unicode, bytes and UnicodeDecodeError&lt;/a&gt;
      &lt;/h5&gt;
      &lt;p&gt;Great article explaining differences between Python &lt;code&gt;str&lt;/code&gt; and &lt;code&gt;unicode&lt;/code&gt; types.
&lt;/p&gt;
    &lt;/li&gt;
&lt;/ul&gt;</description><pubDate>Thu, 21 Oct 2010 16:21:31 -0500</pubDate><guid>tag:loutontheweb.co.cc,2010-10-21:/categories/links/links-for-oct-21-2010/</guid><category>Links</category></item><item><title>Split django settings</title><link>http://loutontheweb.co.cc/categories/django/split-django-settings/</link><description>&lt;p&gt;Related links:
&lt;/p&gt;
&lt;ul&gt;
 &lt;li&gt;
     &lt;a href="http://www.robgolding.com/blog/2010/05/03/extending-settings-variables-with-local_settings-py-in-django/"&gt;Extending Settings Variables with local_settings.py in Django&lt;/a&gt;
 &lt;/li&gt;

 &lt;li&gt;
     &lt;a href="http://code.djangoproject.com/wiki/SplitSettings"&gt;Splitting up the settings file&lt;/a&gt;
 &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Some points&lt;/h3&gt;
&lt;ul&gt;
 &lt;li&gt;
     I don't see any sense "to move sensitive or machine/user specific settings like database passwords and such out of the main &lt;code&gt;settings.py&lt;/code&gt; file". What's the point to share incomplete settings?
 &lt;/li&gt;

 &lt;li&gt;
     I want to be able to upload my application to production without modifying any lines of the code.
 &lt;/li&gt;

 &lt;li&gt;
     Host based solutions don't work with SSH / Command Prompt.
 &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Let's start&lt;/h3&gt;
&lt;ul&gt;
 &lt;li&gt;
     &lt;code&gt;settings.py&lt;/code&gt; - production version;
 &lt;/li&gt;

 &lt;li&gt;
     &lt;code&gt;dev_settings.py&lt;/code&gt; - development version.
 &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;At the top of &lt;code&gt;dev_settings.py&lt;/code&gt;:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;from settings import *

# here we can redefine our settings for development machine
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We also have to slightly modify manage.py file:
&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if os.environ.get('DEVELOPMENT', None):
    import dev_settings as settings
else:
    import settings
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Finally we have to create environment variable &lt;code&gt;DEVELOPMENT&lt;/code&gt; on development machine.
&lt;/p&gt;
&lt;p&gt;I think it is the easiest way to have different settings for development, staging, testing and production.
&lt;/p&gt;</description><pubDate>Thu, 23 Sep 2010 10:16:11 -0500</pubDate><guid>tag:loutontheweb.co.cc,2010-09-23:/categories/django/split-django-settings/</guid><category>Django</category></item></channel></rss>

