Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Wednesday, 21 December 2011

Different ways for low level caching in Django

Often in a Django projects complex data processing can be involved in order to get desire output. It can be a good idea to use some kind of caching. Django provides a robust cache system. It's possible to store and retrieve wide range of data with different level of complexity.
Most easiest ways to use caching is to use:

But I'm more interested in mixing two other types of caching:
Trivial examples of them can be found at Caching websites with Django and Memcached…
But what if on the one hand we have some complex python code which give us a complex output (like dict, or list of dicts, or something like this) and on the other hand this complex output is used in the template for another complex work (e.g., building table with many dependencies).
How to deal with such cases?

Just use both of them

That is, complex output is cached in the python code and there is cached fragment in the template.
Pros: simple enough
Cons: if the cache key used in the python code is expired, the complex calculation will be made in order to get the complex result. But the key used for template fragment might haven't expired, so the efforts are wasted.

Do the hard work, only if it's needed

Not only the key used in the python code should be checked, but also the one generated for the cached template fragment. If the last one is not expired, the hard work can be skipped.

Pros: complex calculations are performed only when they are really needed
Cons: additional code is needed in order to generate same key as the one used by {% cache %} template tag. There is no guarantee that this key will be generated in the same way in the next Django version.

Cache only the final result

Save your template fragment that should be cached in separate template file. When you get your complex output form your hard calculations use render_to_string to get desire result and put it in the cache. The result is string, which can be used 'as is' in the template.

I will be glad to hear your opinion on the topic.

Monday, 11 July 2011

Python script to toggle Skype main window with keyboard

I spent some time searching for some way to toggle skype's main window with keyboard shortcut.
But there is no way to do it with anything out of the box.
Finally found a really useful python script. You can read more at scorpspot.blogspot.com.

Wednesday, 2 March 2011

Having global variable in your django templates

This post is based on question in stackoverflow.com
The question is about having 'now' in blocktrans.
In blocktrans we can't use templatetags so we will assign result of the templatetags to a variable, like this:
 {% url "add_account" as add_account_url %}

But 'now' doesn't act the same way.

Friday, 18 February 2011

openssl_sign in python

You need to sign something with private RSA key. You have some documentation or make your google search about subject.  And all excamples are with PHP function openssl_sign.
But we don't eat PHP anymore. So let's translate few lines PHP into something better, like Python.

Thursday, 17 February 2011

Images, python and base64

Kari - embeded as base64 :)
Look at the image on the left. It's embedded. The idea is very simple.

Open some image file.

Read it in a string.

Encode it with base64.

Now you can use the result as src in the img tag. This image can be used in html email messages or can be used in the css.



Friday, 11 February 2011

Random python thoughts, sorted by number of occurrences

This is just a memo about ease of working with ranges, random choices and sorting.
Let's see a piece of code:
import random
import string
from operator import itemgetter

sample_dict = {}
for x in range(random.choice(range(2,25))):
    r_num = random.choice(range(0,x+1))
    lst = random.sample(string.letters,r_num)
    for l in lst:
        if l in sample_dict.keys():
            sample_dict[l] += 1
        else:
            sample_dict[l] = 1
print sample_dict
itms = sample_dict.iteritems()
print sorted(itms,key=itemgetter(1),reverse=True)
This is python, so there is no need for explaining. But it's worth to mention some cool things for newcomers.
When you need a list of numbers just choose your range - range(2,25).
And if you need just a random number from this range - random.choice(range(2,25)).
And if you need a few letters, you can get them from string.letters, like this random.sample(string.letters,5). Now you have five random letters.
The script above is counting, how often these random letters are repeated, by using them as dict keys. When you have such dict you must sort it by values in order to show occurences of every key - sorted(sample_dict.iteritems(),key=itemgetter(1),reverse=True)

Sunday, 21 November 2010

Organize your photos by date in nautilus

I already know how to use the exif information in pyhton to group photos in folders by date the picture was taken( see here ). The problem is that you must run the script in a terminal and the script must by in the python path. So I changed it a bit. Now the script can be used in the Nautilus. To group photos in any directory I can select Scripts-> Group By Date.

Tuesday, 2 November 2010

Simple way to organize photos by date

I have a lot of pictures. Like most people today. They were all dumped in one directory. It was very difficult to view them. So I wrote a very short python script that reads from the EXIF date the photo was taken. Create a directory for that date and move the photo in this directory.
Here's the script:
import os
from PIL import Image
import shutil

cdate_tag = 0x9003

files = [f for f in os.listdir('.') if f.endswith('.JPG')]

for f in files:
    if os.path.isfile(f):
        img = Image.open(f)
        date = img._getexif()[0x9003].split(' ')[0].replace(':','_')
        if not os.path.isdir(date):
            os.mkdir(date)
        shutil.move(f,os.path.join(date,f))
        print f,date
        img.fp.close()