Announcement

Wednesday, July 11, 2012

Django connection pooling using sqlalchemy connection pool

As you know, Django uses new database connection for each request. This works well initially. However as the load on the server increases, creating/destroying connections to database starts taking significant amount of time.  You will find many questions about using some kind of connection pooling for Django on sites like StackOverflow . For example, Django persistent database connection. 

At BootStrapToday we use sqlalchemy's connection pooling mechanism with Django for pooling the database connections. We use variation of approach by Igor Katson described in http://dumpz.org/67550/.  Igor's approach requires patching Django which we wanted to avoid. Hence we created a small function that we import in one of __init__.py (or models.py) (i.e. some file which gets imported early in the application startup).


import sqlalchemy.pool as pool
pool_initialized=False

def init_pool():
     if not globals().get('pool_initialized', False):
         global pool_initialized
         pool_initialized = True
         try:
             backendname = settings.DATABASES['default']['ENGINE']
             backend = load_backend(backendname)

             #replace the database object with a proxy.
             backend.Database = pool.manage(backend.Database)

             backend.DatabaseError = backend.Database.DatabaseError
             backend.IntegrityError = backend.Database.IntegrityError
             logging.info("Connection Pool initialized")
         except:
             logging.exception("Connection Pool initialization error")

#Now call init_pool function to initialize the connection pool. No change required in the
# Django code.
init_pool()

So far this seems to be working quite well.

Tuesday, June 19, 2012

CSS and Icon sprites - tips and tricks

[NOTE : This post was originally published on  BootStrapToday blog as "CSS and Icon sprites – tips and tricks"]

We used image sprites for icons on bootstraptoday.com for version 2.0 and also in the latest version that we are working on (version 3). Quickly I realized that making sure that the icons looks same on major browsers is a task in itself.  For example, when you have an empty 'span' element which is supposed to display the icons, different browser will treat it slightly differently. Some browsers will give it a height/width of 1 character, some browsers height same as line height but width will be 0. If your icons are not arranged properly then putting those icons on buttons with consistent look & feel is a nightmare.  So here are few tips to define the icon image sprites and to use them effectively.

  1. First and most important tip is to have single sized icons in one sprite and arrange them in a grid. So if you have two icon sizes (e.g. 16px and 24px), make two sprites. One for 16px icons arranged on 16px X 16px grid. Another for 24px icons arranged on 24px X 24px grid.  Check the twitter-bootstrap icons.

  2. Use LessCSS or (some other equivalent) to define the icons css.

  3. Use span as 'display:inline-block'.  Typically span elements are inline elements. And you cannot define width/height of an inline element. Since we need to place the icons inline but need the flexibility of defining width/height, use 'display:inline-block'.

  4. Usually set the 'vertical-align:text-top'. This will align the icons to text.

  5. Use content:"". It will satisfy browsers which need to see text inside the span.
Here is a sample .less css fragment to be used with an icon sprite and
@icon-sprite-path:url(../images/icongrid.png);     /* url of the image path */
@icon-grid-size:16px;

.icongrid {
background-image:@icon-sprite-path:
background-repeat:no-repeat;

/* the element should be placed as 'inline' but we need flexibility to define width/height, padding etc*/
display:inline-block;

/* to ensure that each span is 'icon-grid-size' by 'icon-grid-size' square */
padding:@icon-grid-size/2;

/* to satisfy browsers which don't like empty elements */
content:"";

/* align the icons with text */
vertical-align:text-top;

}

/* define individual icon positions in sprite */
.testicon { background-position:0 -16px; }

Now you can use the icons with spans. For example to append the 'test icon' to some text, use
<div> ... your text here
<span class='icongrid testicon"></span>
</div>

Acknowledgement : I picked up some of these ideas by going over the twitter-bootstrap css and icon sprites.

Sunday, April 22, 2012

Teaching mindmapping to 10th Standard Students of Millenium National School

I learned mindmapping about 10-12 years back from Tony Buzan's Mindmap Book. For me Mindmaps work and i have used for everything from documenting software design, planning customer visits, preparing for training sessions/presentations etc etc.  Few years back, I taught mindmapping to my daughters. My elder daughter is now a regular mindmap user and she studies all the subjects using mindmaps. 

Many years I have been thinking that mindmapping will be an excellent life skill if we teach it our students at around 8th-9th grade. So I approached my daughters' school. Millenium National School and asked if I can teach mindmapping to 10th standard students.  School's director Arpita Karakare ma'am and Radhika teacher (coordinator) , Smita teacher were very enthusiastic about the idea. (BTW, I think this enthusiasm about trying out new ideas that can potentially benefit students is the distinguishing character of Millenium school.). Arpita ma'am and few Millenium teachers were already using mindmaps. Hence the concept was not new to school. 

We decided on following format 
  1. 4 hour introductory session for each division
  2. At the end of session give 4-5 topics (from science, history/civics, maths, geography) and students have to submit these 4-5 mindmaps in about 8-10 days time.
  3. I will evaluate these mindmaps and give suggestions for improvements.
So far I have done many corporate trainings on various topics. This was my first experience of teaching students. It was GREAT experience. Students enjoyed it. Some teachers also attended sessions. 

Interestingly many 'C' Division students made really Good mindmaps. 'A'  division students had many more questions/doubts and quickly wanted to try out different ideas. Also many 'A' division students tried mindmapping topics other than the topics for experiences. During followup sessions I could see the gradual improvements in mindmaps as students practiced more. And Mindmapping does require practice. Even after 12 years of using mindmaps, I still have to revise my mindmaps multiple times to completely organize my thoughts about a subject.

I don't know how of these students will remain regular mindmap users. But even if few of them use it regularly, it will give me great satisfaction.

In fact, Arpita ma'am told me about one student. His father called her and asked her that his son is suddenly very enthusiastic study and he is drawing all these pictures/maps. And is that ok ??.  She assured him that 'yes it is ok'. Its a 'great feeling' to be a part of such transformations.

Thanks Team Millennium for the opportunity and your support. (especially Radhika teacher, Smita teacher and Arpita ma'am and all the 10th Standard Students).

As usual here are few good links about Mindmapping.
  1. Tony Buzan's Mindmap Book on Google Books
  2. Tony Buzan giving a quick introduction to Mindmaps
  3. How to mindmap a Text book 
  4. 7 mind-mapping uses for Students. 
  5. Mindmap Art - Have some great examples of mindmaps.
 Some good Mindmap Software:
  1. MindManager Easy to use and has great MS Office integration
  2. iMindmap is developed by Tony Buzan. The iMindmap mindmaps look great.
  3. XMind - Open Source available in Linux, Windows, Mac. Good software to start.
  4. FreeMind -