Custom Search

SUBSCRIBE

SUBSCRIBE : 300 + Free Video Tutorials Programming (Python, golang, Julia), DevOps

Monday, April 26, 2010

django sorl-thumbnail X-Accel-Redirect

django sorl-thumbnail X-Accel-Redirect
django sorl-thumbnail X-Accel-Redirect Customization
nginx x-accel-redirect protection of static files sorl-thumbnail django

---------------- in settings.py

MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')

MEDIA_URL = '/media/'
UPLOAD_PATH = 'upload/projects'
THUMBNAIL_X_ACCEL_REDIRECT_UPLOAD_DIR = 'upload/'
THUMBNAIL_X_ACCEL_REDIRECT_APP_DIR = '/siren/'

---------------- in template

{% load thumbnail %}

<a href="">
<img src="{% thumbnail mockup.image 100x100 %}" alt="{{ mockup.caption }}" title="{{ mockup.caption }}"/>
</a>

---------------- in /sorl/thumbnail/main.py

import os

from django.conf import settings
from django.utils.encoding import iri_to_uri, force_unicode

from sorl.thumbnail.base import Thumbnail
from sorl.thumbnail.processors import dynamic_import
from sorl.thumbnail import defaults


def get_thumbnail_setting(setting, override=None):
"""
Get a thumbnail setting from Django settings module, falling back to the
default.

If override is not None, it will be used instead of the setting.
"""
if override is not None:
return override
if hasattr(settings, 'THUMBNAIL_%s' % setting):
return getattr(settings, 'THUMBNAIL_%s' % setting)
else:
return getattr(defaults, setting)


def build_thumbnail_name(source_name, size, options=None,
quality=None, basedir=None, subdir=None, prefix=None,
extension=None):
quality = get_thumbnail_setting('QUALITY', quality)
basedir = get_thumbnail_setting('BASEDIR', basedir)
subdir = get_thumbnail_setting('SUBDIR', subdir)
prefix = get_thumbnail_setting('PREFIX', prefix)
extension = get_thumbnail_setting('EXTENSION', extension)
path, filename = os.path.split(source_name)
basename, ext = os.path.splitext(filename)
name = '%s%s' % (basename, ext.replace(os.extsep, '_'))
size = '%sx%s' % tuple(size)

# Handle old list format for opts.
options = options or {}
if isinstance(options, (list, tuple)):
options = dict([(opt, None) for opt in options])

opts = options.items()
opts.sort() # options are sorted so the filename is consistent
opts = ['%s_' % (v is not None and '%s-%s' % (k, v) or k)
for k, v in opts]
opts = ''.join(opts)
extension = extension and '.%s' % extension
thumbnail_filename = '%s%s_%s_%sq%s%s' % (prefix, name, size, opts,
quality, extension)
return os.path.join(basedir, path, subdir, thumbnail_filename)


class DjangoThumbnail(Thumbnail):
imagemagick_file_types = get_thumbnail_setting('IMAGEMAGICK_FILE_TYPES')

def __init__(self, relative_source, requested_size, opts=None,
quality=None, basedir=None, subdir=None, prefix=None,
relative_dest=None, processors=None, extension=None):
relative_source = force_unicode(relative_source)
# Set the absolute filename for the source file
source = self._absolute_path(relative_source)

quality = get_thumbnail_setting('QUALITY', quality)
convert_path = get_thumbnail_setting('CONVERT')
wvps_path = get_thumbnail_setting('WVPS')
if processors is None:
processors = dynamic_import(get_thumbnail_setting('PROCESSORS'))

# Call super().__init__ now to set the opts attribute. generate() won't
# get called because we are not setting the dest attribute yet.
super(DjangoThumbnail, self).__init__(source, requested_size,
opts=opts, quality=quality, convert_path=convert_path,
wvps_path=wvps_path, processors=processors)

# Get the relative filename for the thumbnail image, then set the
# destination filename
if relative_dest is None:
relative_dest = \
self._get_relative_thumbnail(relative_source, basedir=basedir,
subdir=subdir, prefix=prefix,
extension=extension)
relative_dest_url_x_accel_redirect = relative_dest.replace(settings.THUMBNAIL_X_ACCEL_REDIRECT_UPLOAD_DIR, '') #<---- Inside IF statement
filelike = not isinstance(relative_dest, basestring)
if filelike:
self.dest = relative_dest
else:
self.dest = self._absolute_path(relative_dest)

# Call generate now that the dest attribute has been set
self.generate()

# Set the relative & absolute url to the thumbnail
if not filelike:
self.relative_url = \
iri_to_uri('/'.join(relative_dest_url_x_accel_redirect.split(os.sep))) #<----
self.absolute_url = '%s%s' % (settings.THUMBNAIL_X_ACCEL_REDIRECT_APP_DIR,
self.relative_url) #<---- Inside IF statement
#self.absolute_url = '%s%s' % (settings.MEDIA_URL,
self.relative_url)

def _get_relative_thumbnail(self, relative_source,
basedir=None, subdir=None, prefix=None,
extension=None):
"""
Returns the thumbnail filename including relative path.
"""
return build_thumbnail_name(relative_source, self.requested_size,
self.opts, self.quality, basedir, subdir,
prefix, extension)

def _absolute_path(self, filename):
absolute_filename = os.path.join(settings.MEDIA_ROOT, filename)
return absolute_filename.encode(settings.FILE_CHARSET)

def __unicode__(self):
return self.absolute_url


----------------

***************************

Friday, April 23, 2010

file upload and edit in django

file upload and edit in django

===================== Example 1 (default)

--------------- in models.py

class Mockup(models.Model):
def get_path(self, image_name):
upload_path = ('%s/%s/mockups/%s/%s') %(settings.UPLOAD_PATH, self.mockupset.project.id, self.mockupset.mockupset_number, image_name)
return upload_path

mockupset = models.ForeignKey(MockupSet)
caption = models.CharField(max_length=200, blank=True)
image = models.ImageField(upload_to=get_path, null=False, blank=False)
created_on = models.DateTimeField()
created_by = models.ForeignKey(ProjectMember, related_name='mockup_created_by')


------------- in forms.py

class MockupForm(ModelForm):
class Meta:
model = Mockup
exclude = ('mockupset', 'created_by', 'created_on')

def __init__(self, current_user, mockupset, *args, **kwargs):
super(MockupForm, self).__init__(*args, **kwargs)
self._current_user = current_user
self._mockupset_id = mockupset

def save(self, commit=True):
m = super(MockupForm, self).save(commit=False)
m.mockupset = self._mockupset_id
m.created_by = self._current_user
m.created_on = datetime.datetime.now()
if commit:
m.save()
return m


------------- in template

{% block content %}
<div class="title-block">
<h1>Add New Image</h1>
<div class="header_links">
<a href="{% url mockupset_detail project.id mockupset.mockupset_number %}">Back</a>
</div>
</div>

<div class="form-wrapper">
{% if form.errors %}
<p class="error">
Please correct the error below.
</p>
{% endif %}
<form enctype="multipart/form-data" action="{% if mockup %} {% url mockup_edit project.id mockupset.mockupset_number mockup.id %} {% else %} {% url mockup_add project.id mockupset.mockupset_number %} {% endif %}" method="post">
<div class="row">
{{form.caption.label_tag }}
{{ form.caption }}
{{ form.caption.errors}}
</div>

<div class="row">
{{ form.image.label_tag }}
{{ form.image }}
{{ form.image.errors}}
</div>

<div class="row">
<label> </label>
{% if mockup %}
<button type="submit">Save Chanages</button>
{% else %}
<button type="submit">Create Mockup</button>
{% endif %}
</div>
</form>
</div>
{% endblock %}

-------------in views.py (Add, Edit)

def mockup_add(request, project_id, mockupset_number):
project = get_object_or_404(Project, pk=project_id)
mockupset = get_object_or_404(MockupSet, project=project, mockupset_number=mockupset_number)
projectmember = get_object_or_404(ProjectMember, user=request.user, project=project)
if request.method == 'POST':
form = MockupForm(projectmember, mockupset, request.POST, request.FILES)
if form.is_valid():
m = form.save()
return HttpResponseRedirect(reverse('mockupset_list', kwargs={'project_id':project.id}))
elif request.method == 'GET':
form = MockupForm(projectmember, mockupset)
return render_to_response('siren/mockup/mockup_add.html', {
'form':form,
'project':project,
'mockupset':mockupset
}, context_instance=RequestContext(request))



def mockup_edit(request, project_id, mockupset_number, mockup_id):
project = get_object_or_404(Project, pk=project_id)
mockup = get_object_or_404(Mockup, pk=mockup_id)
mockupset = get_object_or_404(MockupSet, project=project, mockupset_number=mockupset_number)
projectmember = get_object_or_404(ProjectMember, user=request.user, project=project)
if request.method == 'POST':
form = MockupForm(projectmember, mockupset, request.POST, request.FILES, instance=mockup)
if form.is_valid():
m = form.save()
return HttpResponseRedirect(reverse('mockupset_list', kwargs={'project_id':project.id}))
elif request.method == 'GET':
form = MockupForm(projectmember, mockupset, instance=mockup)
return render_to_response('siren/mockup/mockup_add.html', {
'form':form,
'mockup': mockup,
'mockupset':mockupset,
'project':project
}, context_instance=RequestContext(request))



========================== Example 2 (upload to image field in the another model)

--------------- in models.py


class MockupSet(fts.SearchableModel):
project = models.ForeignKey(Project)
mockupset_number = models.IntegerField(default=0)
title = models.CharField(max_length=200)
description = models.TextField(blank=True)
created_on = models.DateTimeField()
created_by = models.ForeignKey(ProjectMember, related_name='mockupset_created_by')


class Mockup(models.Model):
def get_path(self, image_name):
upload_path = ('%s/%s/mockups/%s/%s') %(settings.UPLOAD_PATH, self.mockupset.project.id, self.mockupset.mockupset_number, image_name)
return upload_path

mockupset = models.ForeignKey(MockupSet)
caption = models.CharField(max_length=200, blank=True)
image = models.ImageField(upload_to=get_path, null=False, blank=False)
created_on = models.DateTimeField()
created_by = models.ForeignKey(ProjectMember, related_name='mockup_created_by')


-------------- in forms.py

class MockupSetForm(ModelForm):
image_one = forms.ImageField(required=False)
image_two = forms.ImageField(required=False)
image_three = forms.ImageField(required=False)
class Meta:
model = MockupSet
exclude = ('project', 'mockupset_number', 'created_by', 'created_on')

def __init__(self, current_user, project, *args, **kwargs):
super(MockupSetForm, self).__init__(*args, **kwargs)
self._current_user = current_user
self._project_id = project

def save(self, commit=True):
ms = super(MockupSetForm, self).save(commit=False)
ms.project = self._project_id
ms.created_by = self._current_user
ms.created_on = datetime.datetime.now()
if commit:
ms.save()
print "oo"
if self.cleaned_data['image_one']:
img = self.cleaned_data['image_one']
ms.mockup_set.create(caption=str(img), image=img, created_on=datetime.datetime.today(), created_by=self._current_user)
if self.cleaned_data['image_two']:
img = self.cleaned_data['image_two']
ms.mockup_set.create(caption=str(img), image=img, created_on=datetime.datetime.today(), created_by=self._current_user)
if self.cleaned_data['image_three']:
img = self.cleaned_data['image_three']
ms.mockup_set.create(caption=str(img), image=img, created_on=datetime.datetime.today(), created_by=self._current_user)
return ms


--------------- in template

{% block content %}
<div class="title-block">
<h1>Add New Mockupset</h1>
<div class="header_links">
<a href="{% url mockupset_list project.id %}">List Mockupset</a>
</div>
</div>

<div class="form-wrapper">
{% if form.errors %}
<p class="error">
Please correct the error below.
</p>
{% endif %}
<form enctype="multipart/form-data" action="{% if mockupset %} {% url mockupset_edit project.id mockupset.mockupset_number %} {% else %} {% url mockupset_add project.id %} {% endif %}" method="post">
<div class="row">
{{form.title.label_tag }}
{{ form.title }}
{{ form.title.errors}}
</div>

<div class="row">
{{ form.description.label_tag }}
{{ form.description }}
{{ form.description.errors}}
</div>

<div class="row">
{{ form.image_one.label_tag }}
{{ form.image_one }}
{{ form.image_one.errors}}
</div>

<div class="row">
{{ form.image_two.label_tag }}
{{ form.image_two }}
{{ form.image_two.errors}}
</div>

<div class="row">
{{ form.image_three.label_tag }}
{{ form.image_three }}
{{ form.image_three.errors}}
</div>

<div class="row">
<label> </label>
{% if mockupset %}
<button type="submit">Save Chanages</button>
{% else %}
<button type="submit">Create Mockupset</button>
{% endif %}
</div>
</form>
</div>
{% endblock %}


--------------- in views.py (Add)

# For Add
def mockupset_add(request, project_id):
project = get_object_or_404(Project, pk=project_id)
projectmember = get_object_or_404(ProjectMember, user=request.user, project=project)
if request.method == 'POST':
form = MockupSetForm(projectmember, project, request.POST, request.FILES)
if form.is_valid():
ms = form.save()
return HttpResponseRedirect(reverse('mockupset_list', kwargs={'project_id':project.id}))
elif request.method == 'GET':
form = MockupSetForm(projectmember, project)
return render_to_response('siren/mockupset/mockupset_add.html', {
'form':form,
'project':project
}, context_instance=RequestContext(request))


========================== Example 3

Dynamic file upload paths in Django

Dynamic file upload paths in Django

============================== Example 1 (Not using ModelForm)

Not using CustomImageField, it is old technique

Pass a callable in upload_to
-------------------------------------
It is now possible for the upload_to parameter of the FileField or ImageField to be a callable, instead of a string. The callable is passed the current model instance and uploaded file name and must return a path. That sounds ideal.

Here’s an example:


------------------------ in models.py

import os
from django.db import models

def get_image_path(instance, filename):
return os.path.join('photos', instance.id, filename)

class Photo(models.Model):
image = models.ImageField(upload_to=get_image_path)

------------------------OR in models.py

import os
from django.db import models

class Photo(models.Model):
def get_image_path(self, filename):
return os.path.join('photos', self.id, filename)

image = models.ImageField(upload_to=get_image_path)

--------------------------

get_image_path is the callable (in this case a function). It simply gets the id from the instance of Photo and uses that in the upload path. Images will be uploaded to paths like:

photos/1/kitty.jpg

You can use whatever fields are in the instance (slugs, etc), or fields in related models. For example, if Photo models are associated with an Album model, the upload path for a Photo could include the Album slug.

Note that if you are using the id, you need to make sure the model instance was saved before you upload the file. Otherwise, the id hasn’t been set at that point and can’t be used.

For reference, here’s what the main part of the view might look like:

------------------------ in views.py

if request.method == 'POST':
form = PhotoForm(request.POST, request.FILES)
if form.is_valid():
photo = Photo.objects.create()
image_file = request.FILES['image']
photo.image.save(image_file.name, image_file)



This is much simpler than the hacks used in CustomImageField and provides a nice flexible way to specify file or image upload paths per-model instance.

Note: If you are using ModelForm, when you call form.save() it will save the file – no need to do it yourself as in the example above.
-----

============================== Example 2 (Using ModelForm)

------------------------ in models.py

import os
from django.db import models

def get_image_path(instance, filename):
return os.path.join('photos', instance.id, filename)

class Photo(models.Model):
image = models.ImageField(upload_to=get_image_path)

------------------------OR in models.py

import os
from django.db import models

class Photo(models.Model):
def get_image_path(self, filename):
return os.path.join('photos', self.id, filename)

image = models.ImageField(upload_to=get_image_path)

----------------------- in forms.py

class MockupForm(ModelForm):
class Meta:
model = Photo

----------------------- in views.py

if request.method == 'POST':
form = PhotoForm(request.POST, request.FILES)
if form.is_valid():
form.save()

Note: You are using ModelForm,So when you call form.save() it will save the file automatically.

==============================

How protect all file uploads by using xaccel-redirect in django and nginx

How protect all file uploads by using xaccel-redirect in django and nginx


http://kovyrin.net/2006/11/01/nginx-x-accel-redirect-php-rails/

http://spongetech.wordpress.com/2007/11/13/the-complete-nginx-solution-to-sending-flowers-and-files-with-rails/

http://kovyrin.net/2006/05/18/nginx-as-reverse-proxy/

http://wiki.nginx.org/NginxXSendfile


django
----------
http://www.djangosnippets.org/snippets/491/

Tuesday, April 20, 2010

django template tag for highlight nav item

django template tag for highlight nav item django template tag for highlight selected nav item

-------------------------- in template

<div id="menu">
<ul id="nav-menu">
<li class="{% active_page request.path dashboard %}"> <a href="{% url dashboard %}">Dashboard</a> </li>
<li class="{% active_page request.path dashboard_todo_list %}"> <a href="{% url dashboard_todo_list %}">To-Dos</a> </li>
<li class="{% active_page request.path dashboard_milestone_list %}"> <a href="{% url dashboard_milestone_list %}">Milestones</a> </li>
<li class="{% active_page request.path ticket_list %} last"> <a href="{% url ticket_list %}">Tickets</a> </li>
</ul>
</div>

-------------------------- in templatetags/tags.py

class ActivePageNode(template.Node):
def __init__(self ,tag_name, path, url):
self.path = path
self.url = url
self.tag_name = tag_name

def render(self, context):
self.path = template.resolve_variable(self.path, context)
print "path -->", self.path
print "url -->", reverse(self.url)
if self.path == reverse(self.url):
print "equal"
return 'current'


@register.tag
def active_page(parser, token):
try:
tag_name, path, url = token.split_contents()
print "tag name:", tag_name
print "path:", path
print "url:", url
except ValueError:
raise template.TemplateSyntaxError
return ActivePageNode(tag_name, path, url)


--------------------------

output
---------
tag name: active_page
path: request.path
url: dashboard

tag name: active_page
path: request.path
url: dashboard_todo_list

tag name: active_page
path: request.path
url: dashboard_milestone_list

tag name: active_page
path: request.path
url: ticket_list

path --> /siren/dashboard/milestones/
url --> /siren/dashboard/

path --> /siren/dashboard/milestones/
url --> /siren/dashboard/todolists/

path --> /siren/dashboard/milestones/
url --> /siren/dashboard/milestones/
equal

path --> /siren/dashboard/milestones/
url --> /siren/dashboard/tickets

--------------------------------

Wednesday, April 14, 2010

python list if in and or

python list if in and or

list
-----
CONTRIBUTORS = ['author', 'editor', 'translator', '']

PUBLISHERS = ['publisher', 'illustrator']

if in or
----------
if field in CONTRIBUTORS or field in PUBLISHERS:
print field


if in and
-------------
if field in CONTRIBUTORS and field in PUBLISHERS:
print field

Tuesday, April 13, 2010

django template filter to group objects alphabetically based on title or name

django template filter to group objects alphabetically based on title or name

--------------filter

@register.filter
def group_by_file_name(object_list):
res ={}
for x in object_list:
r = re.search('^[a-zA-Z]',x.title)
if r:
key = r.group().lower()
if not res.has_key(key):
print "reg", key
res[key] = [x]
else:
res[key].append(x)

n = re.search('^[0-9_]',x.title)
if n:
print "reg", n.group()
if not res.has_key('#'):
res['#'] = [x]
else:
res['#'].append(x)

print "result dictionary", res
# converting dictionary to list of tuples, since template support only List.
l = []
for k,v in res.items():
l.append((k,v))
l.sort(cmp = lambda x, y: cmp(x, y))
print "output list", l
return l

--------------- template

{% for g in files.object_list|group_by_file_name %}
<h4 class="file-atoz"><span>{{g.0}}</span></h4>
{% for p in g.1%}
<div class="outer-block">
{% for v in p.files %}
{%ifequal forloop.counter 1%}
<div class="inner-block">
<h1><a href="{{v.file.url}}"> {{v.file.name|format_file_name}}</a></h1>
<p>{{p.description}}</p>
<span class="details"> by {{v.created_by|gen_short_name}} on {{v.created_on.date}}, ({{v.file.size|filesizeformat}})</span>
<a href="{% url project_file_add_new_version project.id p.id%}" class="">Upload a new version</a>
<a href="{% url project_file_edit project.id p.id v.id%}" class="edit">Edit</a>
<a href="{%url project_file_delete project.id p.id v.id%}" class="delete">Delete</a>
</div>
<ul>
{% else %}
<li>
<h1><a href="{{v.file.url}}"> {{v.file.name|format_file_name}}</a></h1>
<span class="details"> by {{v.created_by|gen_short_name}} on {{v.created_on.date}}, ({{v.file.size|filesizeformat}})</span>
<a href="{% url project_file_edit project.id p.id v.id%}" class="edit">Edit</a>
</li>
{%endifequal%}
{% endfor %}
</ul>
</div>
{%endfor%}
{% endfor %}

--------------
Output:
-------

#
--
2sam
6repo

A
--
att_file
art
actr

B
--
balact
beta
bstar

C
--
catrot
camels
copies

**************************************************************

Friday, April 9, 2010

how generate complex AND query using Q() in django

how generate complex AND query using Q() in django

queries = request.POST.get('query', '')
print "quires -->", queries
if queries:
queries = json.loads(queries)
print "json queries -->", queries
q = Q()
for field, condition, query in queries:
print "field -->", field
print "condition -->", condition
print "query -->", query
q = q & Q(**{str(field)+"__"+str(condition):query})
print "query output -->", q
book = Book.objects.filter(q & Q(library=library_id)).order_by(*s)

output:
-------
quires --> [["author","icontains","saju"],["editor","startswith","a"]]
json queries --> [[u'author', u'icontains', u'saju'], [u'editor', u'startswith', u'a']]

field -->author
condition-->icontains
query -->saju

field -->editor
condition-->startswith
query -->a

query output --> (AND: ('author__icontains', u'saju'), ('editor__startswith', u'a'))

-----------------

** --> Means keyword argument
* --> Means positional argument

Q() --> can accept keyword argument
order_by() --> can accept positional argument

Q(**{str(field)+"__"+str(condition):query})
order_by(*s)

-----------------

for field, condition, query in queries:
Q(**{str(field)+"__"+str(condition):query})

is equivalent to

Q(**{'author__icontains':'saju', 'editor__startswith':'a'})

that is Q() with keyword arguments

-----------------

Related Field has invalid lookup: icontains

Related Field has invalid lookup: icontains
--------------------------------------------
Exception Type: TypeError
Exception Value:
Related Field has invalid lookup: icontains


=========================== Example 1
solution:ForeignKey
-------------------
> I have code for searching:
>
> news = News.objects.filter(
> Q(title__icontains=phrase)
> | Q(tags__icontains=phrase)
> | Q(short__icontains=phrase)
> | Q(content__icontains=phrase)
> | Q(author__icontains=phrase)
> | Q(source__icontains=phrase)
> )

> News model looks like that:
>
> class News(models.Model):
> title = models.CharField(maxlength="100")
> tags = models.CharField(maxlength="75")
> short = models.TextField()
> content = models.TextField()
> author = models.ForeignKey(User, editable=False)
> source = models.CharField(maxlength="100")
> date = models.DateTimeField(auto_now_add=True)
> status = models.IntegerField(editable=False, default="0")
> def __str__(self):
> return '%s' % (self.title)
> class Admin():
> pass
>
You need to specify which fields in the User/Author table to match on
(the default is the primary key , which doesn't make sense here).
Something like

|Q(author__username__icontains=phrase)

------------------

Okay. It works!

The missing part in my code was:
Q(author__username__icontains=phrase). I couldn't find it. Thank you
for your help.


=========================== Example 2
solution:ManyToMany
-------------------

-------------------- model

class Book(models.Model):
library = models.ForeignKey(Library)
accession_no = models.CharField(max_length=200, blank=True, null=True)
type = models.CharField(max_length=500, blank=True, null=True)
author = models.ManyToManyField('Contributor', related_name='book_authors')
editor = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_editors')
general_editor = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_general_editors')
reviser = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_revisers')
compiler = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_compilers')
translator = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_translators')
illustrator = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_illustrators')
forward = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_forwards')
interpreter = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_interpreters')
cover_designer = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_cover_designers')
commentry = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_commentries')
preface = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_prefaces')
introduction = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_introductions')
prologue = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_prologues')
reteller = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_retellers')
cover_photographer = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_cover_photographers')
title = models.CharField(max_length=500)
publisher = models.ManyToManyField('Publisher', related_name='book_publishers')
class_no = models.CharField(max_length=200, blank=True, null=True)
----
---- etc--
-------------------- view

search_data = request.GET.copy()
keyword = search_data['search-query']
l = ["class_no", "accession_no", "title", "compiler__name", "reviser__name", "editor__name", "author",
"translator__name", "illustrator__name", "forward__name", "interpreter__name", "cover_designer__name", "type",
"title_of_orig", "reprint", "edition", "first_publ_year", "place", "publisher__name",
"year_of_publ", "no_of_vols_in_set", "pages", "illus", "series", "series_editor",
"lang_of_orig", "notes", "isbn", "publ_code", "price", "distributor", "appendix",
"introduction__name", "errata", "preface__name", "prologue__name", "publ_sl_no", "month_of_publ",
"commentry__name"]
q = Q()
for x in l:
q = q | Q(**{x+"__icontains":keyword})
print "qry", q
queryset=Book.objects.filter(q & Q(library=library_id)

--------------------



**************************************************************

Thursday, April 8, 2010

How insert value to new table created by ManyToMany field in django

How insert value to new table created by ManyToMany field in django

http://www.djangoproject.com/documentation/models/many_to_many/

how to print ManyToMany Field value in Template in django

how to print ManyToMany Field value in Template in django
==================================

{{book.author}} print some thing like

how get author name?
its a many-to-many field
so you have to do
book.author.all()
also, you have to change the related name to authors
so that it becomes book.authors.all()

====================================

Example

------------------- models
class Book(models.Model):
library = models.ForeignKey(Library)
accession_no = models.CharField(max_length=200, blank=True, null=True)
type = models.CharField(max_length=500, blank=True, null=True)
author = models.ManyToManyField('Contributor', related_name='book_authors')
editor = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_editors')
reviser = models.ManyToManyField('Contributor', blank=True, null=True, related_name='book_revisers')


class Contributor(models.Model):
library = models.ForeignKey(Library)
name = models.CharField(max_length=1000)

def __unicode__(self):
return self.name

class Meta:
ordering = ('name',)

------------------- template

{{book.author.all.0}}
{{book.editor.all.0}}
{{book.reviser.all.0}}


-------------------

how to join list of objects with join method in python django

how to join list of objects with join method in python django


Caught an exception while rendering: sequence item 0: expected string, Contributor found
join menthod join list of stings, so we have to convert list of objets to list of strings.

solution:

Here 'contributors' is a list of objects
contributors = book.author.all()
format_contributor(contributors)

def format_contributor(contributors):
y = []
for x in contributors:
y.append(str(x))
return ";".join(y)

Wednesday, April 7, 2010

python postgresql migration script example three# -*- coding: utf-8 -*- import sys import psycopg2 pgsql_source_connection = None pgsql_source_curs

# -*- coding: utf-8 -*-

import sys
import psycopg2


pgsql_source_connection = None
pgsql_source_cursor = None
pgsql_source_queries = 0


# Gets the PostgreSQL Source credentials
def get_pg_source_credentials():
c = {}
c['host'] = 'localhost'
c['database'] = 'library'
c['user'] = 'saju'
c['password'] = 'xxx'
c['port'] = '5432'
return c


# Opens a connection to the Source PostgreSQL database
def get_pgsql_source_connection():
try:
credentials = get_pg_source_credentials()
conn = psycopg2.connect("dbname='" + credentials['database'] + "' " + \
"user='" + credentials['user'] + "' " + \
"host='" + credentials['host'] + "' " + \
"port='" + credentials['port'] + "' " + \
"password='" + credentials['password'] + "'")
print "connected to Source db"
except Exception, e:
print "I am unable to connect to the Source PostgreSQL database"
raise e
return conn


def copy_data():

pgsql_source_connection = get_pgsql_source_connection()
pgsql_source_cursor = pgsql_source_connection.cursor()

c = pgsql_source_connection.cursor()
c.execute('begin;')

c.execute('CREATE TABLE "lib_contributor" (\
"id" serial NOT NULL PRIMARY KEY,\
"library_id" integer NOT NULL REFERENCES "lib_library" ("id") DEFERRABLE INITIALLY DEFERRED,\
"name" varchar(1000) NOT NULL\
)')


c.execute('CREATE TABLE "lib_book_author" (\
"id" serial NOT NULL PRIMARY KEY,\
"book_id" integer NOT NULL REFERENCES "lib_book" ("id") DEFERRABLE INITIALLY DEFERRED,\
"contributor_id" integer NOT NULL REFERENCES "lib_contributor" ("id") DEFERRABLE INITIALLY DEFERRED,\
UNIQUE ("book_id", "contributor_id")\
)')

c.execute('CREATE TABLE "lib_book_editor" (\
"id" serial NOT NULL PRIMARY KEY,\
"book_id" integer NOT NULL REFERENCES "lib_book" ("id") DEFERRABLE INITIALLY DEFERRED,\
"contributor_id" integer NOT NULL REFERENCES "lib_contributor" ("id") DEFERRABLE INITIALLY DEFERRED,\
UNIQUE ("book_id", "contributor_id")\
)')

c.execute('CREATE TABLE "lib_book_reviser" (\
"id" serial NOT NULL PRIMARY KEY,\
"book_id" integer NOT NULL REFERENCES "lib_book" ("id") DEFERRABLE INITIALLY DEFERRED,\
"contributor_id" integer NOT NULL REFERENCES "lib_contributor" ("id") DEFERRABLE INITIALLY DEFERRED,\
UNIQUE ("book_id", "contributor_id")\
)')

c.execute('CREATE TABLE "lib_book_compiler" (\
"id" serial NOT NULL PRIMARY KEY,\
"book_id" integer NOT NULL REFERENCES "lib_book" ("id") DEFERRABLE INITIALLY DEFERRED,\
"contributor_id" integer NOT NULL REFERENCES "lib_contributor" ("id") DEFERRABLE INITIALLY DEFERRED,\
UNIQUE ("book_id", "contributor_id")\
)')

c.execute('CREATE TABLE "lib_book_translator" (\
"id" serial NOT NULL PRIMARY KEY,\
"book_id" integer NOT NULL REFERENCES "lib_book" ("id") DEFERRABLE INITIALLY DEFERRED,\
"contributor_id" integer NOT NULL REFERENCES "lib_contributor" ("id") DEFERRABLE INITIALLY DEFERRED,\
UNIQUE ("book_id", "contributor_id")\
)')

c.execute('CREATE TABLE "lib_book_illustrator" (\
"id" serial NOT NULL PRIMARY KEY,\
"book_id" integer NOT NULL REFERENCES "lib_book" ("id") DEFERRABLE INITIALLY DEFERRED,\
"contributor_id" integer NOT NULL REFERENCES "lib_contributor" ("id") DEFERRABLE INITIALLY DEFERRED,\
UNIQUE ("book_id", "contributor_id")\
)')

c.execute('CREATE TABLE "lib_book_forward" (\
"id" serial NOT NULL PRIMARY KEY,\
"book_id" integer NOT NULL REFERENCES "lib_book" ("id") DEFERRABLE INITIALLY DEFERRED,\
"contributor_id" integer NOT NULL REFERENCES "lib_contributor" ("id") DEFERRABLE INITIALLY DEFERRED,\
UNIQUE ("book_id", "contributor_id")\
)')

c.execute('CREATE TABLE "lib_book_interpreter" (\
"id" serial NOT NULL PRIMARY KEY,\
"book_id" integer NOT NULL REFERENCES "lib_book" ("id") DEFERRABLE INITIALLY DEFERRED,\
"contributor_id" integer NOT NULL REFERENCES "lib_contributor" ("id") DEFERRABLE INITIALLY DEFERRED,\
UNIQUE ("book_id", "contributor_id")\
)')

c.execute('CREATE TABLE "lib_book_cover_designer" (\
"id" serial NOT NULL PRIMARY KEY,\
"book_id" integer NOT NULL REFERENCES "lib_book" ("id") DEFERRABLE INITIALLY DEFERRED,\
"contributor_id" integer NOT NULL REFERENCES "lib_contributor" ("id") DEFERRABLE INITIALLY DEFERRED,\
UNIQUE ("book_id", "contributor_id")\
)')

c.execute('CREATE TABLE "lib_book_commentry" (\
"id" serial NOT NULL PRIMARY KEY,\
"book_id" integer NOT NULL REFERENCES "lib_book" ("id") DEFERRABLE INITIALLY DEFERRED,\
"contributor_id" integer NOT NULL REFERENCES "lib_contributor" ("id") DEFERRABLE INITIALLY DEFERRED,\
UNIQUE ("book_id", "contributor_id")\
)')

c.execute('CREATE TABLE "lib_book_preface" (\
"id" serial NOT NULL PRIMARY KEY,\
"book_id" integer NOT NULL REFERENCES "lib_book" ("id") DEFERRABLE INITIALLY DEFERRED,\
"contributor_id" integer NOT NULL REFERENCES "lib_contributor" ("id") DEFERRABLE INITIALLY DEFERRED,\
UNIQUE ("book_id", "contributor_id")\
)')

c.execute('CREATE TABLE "lib_book_introduction" (\
"id" serial NOT NULL PRIMARY KEY,\
"book_id" integer NOT NULL REFERENCES "lib_book" ("id") DEFERRABLE INITIALLY DEFERRED,\
"contributor_id" integer NOT NULL REFERENCES "lib_contributor" ("id") DEFERRABLE INITIALLY DEFERRED,\
UNIQUE ("book_id", "contributor_id")\
)')

c.execute('CREATE TABLE "lib_book_prologue" (\
"id" serial NOT NULL PRIMARY KEY,\
"book_id" integer NOT NULL REFERENCES "lib_book" ("id") DEFERRABLE INITIALLY DEFERRED,\
"contributor_id" integer NOT NULL REFERENCES "lib_contributor" ("id") DEFERRABLE INITIALLY DEFERRED,\
UNIQUE ("book_id", "contributor_id")\
)')

c.execute('CREATE TABLE "lib_book_reteller" (\
"id" serial NOT NULL PRIMARY KEY,\
"book_id" integer NOT NULL REFERENCES "lib_book" ("id") DEFERRABLE INITIALLY DEFERRED,\
"contributor_id" integer NOT NULL REFERENCES "lib_contributor" ("id") DEFERRABLE INITIALLY DEFERRED,\
UNIQUE ("book_id", "contributor_id")\
)')

c.execute('CREATE TABLE "lib_book_cover_photographer" (\
"id" serial NOT NULL PRIMARY KEY,\
"book_id" integer NOT NULL REFERENCES "lib_book" ("id") DEFERRABLE INITIALLY DEFERRED,\
"contributor_id" integer NOT NULL REFERENCES "lib_contributor" ("id") DEFERRABLE INITIALLY DEFERRED,\
UNIQUE ("book_id", "contributor_id")\
)')


c.execute('CREATE TABLE "lib_publisher" (\
"id" serial NOT NULL PRIMARY KEY,\
"library_id" integer NOT NULL REFERENCES "lib_library" ("id") DEFERRABLE INITIALLY DEFERRED,\
"name" varchar(300) NOT NULL\
)')

c.execute('CREATE TABLE "lib_book_publisher" (\
"id" serial NOT NULL PRIMARY KEY,\
"book_id" integer NOT NULL REFERENCES "lib_book" ("id") DEFERRABLE INITIALLY DEFERRED,\
"publisher_id" integer NOT NULL REFERENCES "lib_publisher" ("id") DEFERRABLE INITIALLY DEFERRED,\
UNIQUE ("book_id", "publisher_id")\
)')


c.execute('select * from lib_book;')
for book in list(c.fetchall()):
#book[4] author
#book[5] editor
#book[6] reviser
#book[7] compiler
#book[8] translator
#book[9] illustrator
#book[10] forward
#book[11] interpreter
#book[12] cover_designer
#book[44] commentry
#book[45] preface
#book[46] introduction
#book[47] prologue
#book[49] reteller
#book[50] cover_photographer
#book[19] publisher

if book[4]:
print "author", book[4]
c.execute("insert into lib_contributor (library_id, name) values (%s,%s) returning id", (book[1], book[4],))
id = c.fetchone()[0]
c.execute("INSERT INTO lib_book_author (book_id, contributor_id) values(%s,%s)", (book[0], id))

if book[5]:
print "editor", book[5]
c.execute("insert into lib_contributor (library_id, name) values (%s,%s) returning id", (book[1], book[5],))
id = c.fetchone()[0]
c.execute("INSERT INTO lib_book_editor (book_id, contributor_id) values(%s,%s)", (book[0], id))

if book[6]:
print "reviser", book[6]
c.execute("insert into lib_contributor (library_id, name) values (%s,%s) returning id", (book[1], book[6],))
id = c.fetchone()[0]
c.execute("INSERT INTO lib_book_reviser (book_id, contributor_id) values(%s,%s)", (book[0], id))

if book[7]:
print "compiler", book[7]
c.execute("insert into lib_contributor (library_id, name) values (%s,%s) returning id", (book[1], book[7],))
id = c.fetchone()[0]
c.execute("INSERT INTO lib_book_compiler (book_id, contributor_id) values(%s,%s)", (book[0], id))

if book[8]:
print "translator", book[8]
c.execute("insert into lib_contributor (library_id, name) values (%s,%s) returning id", (book[1], book[8],))
id = c.fetchone()[0]
c.execute("INSERT INTO lib_book_translator (book_id, contributor_id) values(%s,%s)", (book[0], id))

if book[9]:
print "illustrator", book[9]
c.execute("insert into lib_contributor (library_id, name) values (%s,%s) returning id", (book[1], book[9],))
id = c.fetchone()[0]
c.execute("INSERT INTO lib_book_illustrator (book_id, contributor_id) values(%s,%s)", (book[0], id))

if book[10]:
print "forward", book[10]
c.execute("insert into lib_contributor (library_id, name) values (%s,%s) returning id", (book[1], book[10],))
id = c.fetchone()[0]
c.execute("INSERT INTO lib_book_forward (book_id, contributor_id) values(%s,%s)", (book[0], id))

if book[11]:
print "interpreter", book[11]
c.execute("insert into lib_contributor (library_id, name) values (%s,%s) returning id", (book[1], book[11],))
id = c.fetchone()[0]
c.execute("INSERT INTO lib_book_interpreter (book_id, contributor_id) values(%s,%s)", (book[0], id))

if book[12]:
print "cover_designer", book[12]
c.execute("insert into lib_contributor (library_id, name) values (%s,%s) returning id", (book[1], book[12],))
id = c.fetchone()[0]
c.execute("INSERT INTO lib_book_cover_designer (book_id, contributor_id) values(%s,%s)", (book[0], id))

if book[44]:
print "commentry", book[44]
c.execute("insert into lib_contributor (library_id, name) values (%s,%s) returning id", (book[1], book[44],))
id = c.fetchone()[0]
c.execute("INSERT INTO lib_book_commentry (book_id, contributor_id) values(%s,%s)", (book[0], id))

if book[45]:
print "preface", book[45]
c.execute("insert into lib_contributor (library_id, name) values (%s,%s) returning id", (book[1], book[45],))
id = c.fetchone()[0]
c.execute("INSERT INTO lib_book_preface (book_id, contributor_id) values(%s,%s)", (book[0], id))

if book[46]:
print "introduction", book[46]
c.execute("insert into lib_contributor (library_id, name) values (%s,%s) returning id", (book[1], book[46],))
id = c.fetchone()[0]
c.execute("INSERT INTO lib_book_introduction (book_id, contributor_id) values(%s,%s)", (book[0], id))

if book[47]:
print "prologue", book[47]
c.execute("insert into lib_contributor (library_id, name) values (%s,%s) returning id", (book[1], book[47],))
id = c.fetchone()[0]
c.execute("INSERT INTO lib_book_prologue (book_id, contributor_id) values(%s,%s)", (book[0], id))

if book[49]:
print "reteller", book[49]
c.execute("insert into lib_contributor (library_id, name) values (%s,%s) returning id", (book[1], book[49],))
id = c.fetchone()[0]
c.execute("INSERT INTO lib_book_reteller (book_id, contributor_id) values(%s,%s)", (book[0], id))

if book[50]:
print "cover_photographer", book[50]
c.execute("insert into lib_contributor (library_id, name) values (%s,%s) returning id", (book[1], book[50],))
id = c.fetchone()[0]
c.execute("INSERT INTO lib_book_cover_photographer (book_id, contributor_id) values(%s,%s)", (book[0], id))

if book[19]:
c.execute("insert into lib_publisher (library_id, name) values (%s,%s) returning id", (book[1], book[19],))
id = c.fetchone()[0]
c.execute("INSERT INTO lib_book_publisher (book_id, publisher_id) values(%s,%s)", (book[0], id))

c.execute('commit;')



c.execute('begin;')
c.execute('ALTER TABLE lib_book DROP COLUMN author')
c.execute('ALTER TABLE lib_book DROP COLUMN editor')
c.execute('ALTER TABLE lib_book DROP COLUMN reviser')
c.execute('ALTER TABLE lib_book DROP COLUMN compiler')
c.execute('ALTER TABLE lib_book DROP COLUMN translator')
c.execute('ALTER TABLE lib_book DROP COLUMN illustrator')
c.execute('ALTER TABLE lib_book DROP COLUMN forward')
c.execute('ALTER TABLE lib_book DROP COLUMN interpreter')
c.execute('ALTER TABLE lib_book DROP COLUMN cover_designer')
c.execute('ALTER TABLE lib_book DROP COLUMN commentry')
c.execute('ALTER TABLE lib_book DROP COLUMN preface')
c.execute('ALTER TABLE lib_book DROP COLUMN introduction')
c.execute('ALTER TABLE lib_book DROP COLUMN prologue')
c.execute('ALTER TABLE lib_book DROP COLUMN reteller')
c.execute('ALTER TABLE lib_book DROP COLUMN cover_photographer')
c.execute('commit;')


c.execute('begin;')
c.execute('ALTER TABLE lib_physicalbook RENAME COLUMN accessionNo TO accession_no')
c.execute('ALTER TABLE lib_member RENAME COLUMN memberID TO member_id')
c.execute('ALTER TABLE lib_member RENAME COLUMN joinDate TO join_date')
c.execute('ALTER TABLE lib_member RENAME COLUMN expiryDate TO expiry_date')
c.execute('ALTER TABLE lib_member RENAME COLUMN numTickets TO num_tickets')
c.execute('ALTER TABLE lib_member RENAME COLUMN numAvailTickets TO num_avail_tickets')

c.execute('ALTER TABLE lib_reservation RENAME COLUMN reservedDate TO reserved_date')
c.execute('ALTER TABLE lib_reservation RENAME COLUMN expiryDate TO expiry_date')
c.execute('ALTER TABLE lib_reservation RENAME COLUMN lentOut TO lent_out')

c.execute('ALTER TABLE lib_lending RENAME COLUMN physicalBook_id TO physical_book_id')
c.execute('ALTER TABLE lib_lending RENAME COLUMN previousState_id TO physical_book_id')
c.execute('ALTER TABLE lib_lending RENAME COLUMN nextState_id TO next_state_id')
c.execute('ALTER TABLE lib_lending RENAME COLUMN reservedDate TO reserved_date')
c.execute('ALTER TABLE lib_lending RENAME COLUMN issueDate TO issue_date')
c.execute('ALTER TABLE lib_lending RENAME COLUMN returnDate TO return_date')
c.execute('ALTER TABLE lib_lending RENAME COLUMN returnedDate TO returned_date')

c.execute('ALTER TABLE lib_memberprofile_bookProfiles RENAME TO lib_memberprofile_book_profile')
c.execute('commit;')


c.close()



def main():
copy_data()


if __name__ == "__main__":
main()

Tuesday, April 6, 2010

Saturday, April 3, 2010

Python Decorators Tutorial

Python Decorators Tutorial


I think it's safe to say that the goal of macros in a language is to provide a way to modify elements of the language. That's what decorators do in Python -- they modify functions, and in the case of class decorators, entire classes. This is why they usually provide a simpler alternative to metaclasses.

What Can You Do With Decorators?

Decorators allow you to inject or modify code in functions or classes. Sounds a bit like Aspect-Oriented Programming (AOP) in Java, doesn't it? Except that it's both much simpler and (as a result) much more powerful. For example, suppose you'd like to do something at the entry and exit points of a function (such as perform some kind of security, tracing, locking, etc. -- all the standard arguments for AOP). With decorators, it looks like this:

@entryExit
def func1():
    print "inside func1()"
 
@entryExit
def func2():
    print "inside func2()"

The @ indicates the application of the decorator.

Function Decorators

A function decorator is applied to a function definition by placing it on the line before that function definition begins. For example:

@myDecorator
def aFunction():
    print "inside aFunction"

When the compiler passes over this code, aFunction() is compiled and the resulting function object is passed to the myDecorator code, which does something to produce a function-like object that is then substituted for the original aFunction().

What does the myDecorator code look like? Well, most introductory examples show this as a function, but I've found that it's easier to start understanding decorators by using classes as decoration mechanisms instead of functions. In addition, it's more powerful.

The only constraint upon the object returned by the decorator is that it can be used as a function -- which basically means it must be callable. Thus, any classes we use as decorators must implement __call__.

What should the decorator do? Well, it can do anything but usually you expect the original function code to be used at some point. This is not required, however:

class myDecorator(object):
 
    def __init__(self, f):
        print "inside myDecorator.__init__()"
        f() # Prove that function definition has completed
 
    def __call__(self):
        print "inside myDecorator.__call__()"
 
@myDecorator
def aFunction():
    print "inside aFunction()"
 
print "Finished decorating aFunction()"
 
aFunction()

When you run this code, you see:

inside myDecorator.__init__()
inside aFunction()
Finished decorating aFunction()
inside myDecorator.__call__()

Notice that the constructor for myDecorator is executed at the point of decoration of the function. Since we can call f() inside __init__(), it shows that the creation of f() is complete before the decorator is called. Note also that the decorator constructor receives the function object being decorated. Typically, you'll capture the function object in the constructor and later use it in the __call__() method (the fact that decoration and calling are two clear phases when using classes is why I argue that it's easier and more powerful this way).

When aFunction() is called after it has been decorated, we get completely different behavior; the myDecorator.__call__() method is called instead of the original code. That's because the act of decoration replaces the original function object with the result of the decoration -- in our case, the myDecorator object replaces aFunction. Indeed, before decorators were added you had to do something much less elegant to achieve the same thing:

def foo(): pass
foo = staticmethod(foo)

With the addition of the @ decoration operator, you now get the same result by saying:

@staticmethod
def foo(): pass

This is the reason why people argued against decorators, because the @ is just a little syntax sugar meaning "pass a function object through another function and assign the result to the original function."

The reason I think decorators will have such a big impact is because this little bit of syntax sugar changes the way you think about programming. Indeed, it brings the idea of "applying code to other code" (i.e.: macros) into mainstream thinking by formalizing it as a language construct.

More Useful

Now let's go back and implement the first example. Here, we'll do the more typical thing and actually use the code in the decorated functions:

class entryExit(object):
 
    def __init__(self, f):
        self.f = f
 
    def __call__(self):
        print "Entering", self.f.__name__
        self.f()
        print "Exited", self.f.__name__
 
@entryExit
def func1():
    print "inside func1()"
 
@entryExit
def func2():
    print "inside func2()"
 
func1()
func2()

The output is:

Entering func1
inside func1()
Exited func1
Entering func2
inside func2()
Exited func2

You can see that the decorated functions now have the "Entering" and "Exited" trace statements around the call.

The constructor stores the argument, which is the function object. In the call, we use the __name__ attribute of the function to display that function's name, then call the function itself.

Using Functions as Decorators

The only constraint on the result of a decorator is that it be callable, so it can properly replace the decorated function. In the above examples, I've replaced the original function with an object of a class that has a __call__() method. But a function object is also callable, so we can rewrite the previous example using a function instead of a class, like this:

def entryExit(f):
    def new_f():
        print "Entering", f.__name__
        f()
        print "Exited", f.__name__
    return new_f
 
@entryExit
def func1():
    print "inside func1()"
 
@entryExit
def func2():
    print "inside func2()"
 
func1()
func2()
print func1.__name__

new_f() is defined within the body of entryExit(), so it is created and returned when entryExit() is called. Note that new_f() is a closure, because it captures the actual value of f.

Once new_f() has been defined, it is returned from entryExit() so that the decorator mechanism can assign the result as the decorated function.

The output of the line print func1.__name__ is new_f, because the new_f function has been substituted for the original function during decoration. If this is a problem you can change the name of the decorator function before you return it:

def entryExit(f):
    def new_f():
        print "Entering", f.__name__
        f()
        print "Exited", f.__name__
    new_f.__name__ = f.__name__
    return new_f

The information you can dynamically get about functions, and the modifications you can make to those functions, are quite powerful in Python.

More Examples

http://wiki.python.org/moin/PythonDecoratorLibrary


********************************************

So let’s start with the basic no-argument decorator. It doesn’t take any declarative arguments when decorating a function; you just type @some_deco without parenthesis when using it.

def some_deco(f):
    def _inner(*args, **kwargs):
        print "Decorated!"
        return f(*args, **kwargs)
 
    return _inner
 
@some_deco
def some_func(a, b):
    return a + b
 
print some_func(1, 2)
 
 

This will print output:

Decorated!
3

According to the PEP, if you wrap some_func with @some_deco, it is the equivalent of some_func = some_deco(some_func). What it fails to call out is that this happens at load time. What we're returning from the decorator function is a new function or callable that will be bound to the name some_func when this module is loaded. Subsequent run-time calls to some_func are actually calling the _inner function we defined on the fly when the decorator was called.

-------------------------------------------

def log_wrap(message):
    """Print `message` each time the decorated function is called."""
    def _second(f):
        def _inner(*args, **kwargs):
            print message
            return f(*args, **kwargs) 
 
        # This is called second, so return a callable that takes arguments
        # like the first example.
        return _inner
 
    # This is called first, so we want to pass back our inner function that accepts
    # a function as argument
    return _second
 
@log_wrap("Called it!")
def func(a, b):
    return a + b
 
print func(1, 2)

This will print output:

Decorated!
3

Ok, ok, that's not terribly useful, but it illustrates the point (and expands on the first example by being a parameterized version). It requires that we construct two callable items inside our decorator, and that each will be called in turn during load time. Let's quick dissect what happens at load time. When python gets to the declaration of func with the decorator sitting on top of it, it calls the decorator function with its argument (in this case, the string "Called!"). What is returned is a function reference to _second. Next, that just-returned reference to _second is invoked with a single argument: a function pointer to func, which is what we're actually trying to decorate. The return value of _second is a reference to _inner, which will be bound to the name func, ready for run-time invocation.

-------------------------------------------

********************************************

Basic Decoration

The basics: A python function takes arguments, performs a task, and returns a value. A decorator takes as its argument a function, and returns another function with different semantics. It has “decorated” the function with pre-call and post-call behaviors and conditions.

The biggest (and hardest) part for many beginning programmers to grasp is this: a function name is just a variable. A function is just an object; when you call a function, you’re (1) dereferencing the variable functionname, (2) checking that the referenced object is callable, (3) passing that object arguments and (4) invoking a new execution frame, which in turn builds a context and attempts to run the referenced object with the arguments. (That context, by the way, persists for all functions defined within it at each execution pass; this is how closure works, and decorators are highly closure-dependent.)

In some ways, what I’m about to do is go over the same ground as django.views.generic.list_detail.object_detail, but I think my way is somewhat more interesting, and is illustrative to the task at hand. What I’d like is to encapsulate my business logic in one function, and the details of rendering it could then be added in later via decoration. Here’s a very simple example:

@render('cards/home.html')
def home(request):
    return {'cards ':Card.objects.all()}

The business logic is simple: the home page for this application returns all the cards. And the decoration is equally simple: We’re literally going to “decorate” this data with the HTML for the home page.

So, we need a function that wraps Django’s render_to_response, decorating it with the template name and the RequestContext object (that provides all the miscellaneous information most templates in Django need). The outermost decorator ultimately needs to return a function that does this while preserving the template name in a closure. And we want to wrap our innermost function in the name of the function we pass in. Here’s the entirety of it:

from django.template import RequestContext
from functools import wraps
from django.shortcuts import render_to_response
 
def render(template_name):
    def inner_render(fn):
        def wrapped(request, *args, **kwargs):
            return render_to_response(template_name,
                                       fn(request, *args, **kwargs),
                                       context_instance = RequestContext(request))
        return wraps(fn)(wrapped)
    return inner_render

-------------------------------------------



*******************************




Friday, April 2, 2010

Python Lambda Functions an Introduction

Python Lambda Functions an Introduction

Python supports the creation of anonymous functions (i.e. functions that are not
bound to a name) at runtime, using a construct called "lambda" and is often used
in conjunction with typical functional concepts like filter(), map() and reduce().

This piece of code shows the difference between a normal function definition ("f")
and a lambda function ("g"):

>>> def f (x): return x**2
...
>>> print f(8)
64
>>>
>>> g = lambda x: x**2
>>>
>>> print g(8)
64

As you can see, f() and g() do exactly the same and can be used in the same ways.
Note that the lambda definition does not include a "return" statement -- it always
contains an expression which is returned. Also note that you can put a lambda
definition anywhere a function is expected, and you don't have to assign it to a
variable at all.

-----------------------------

>>> def make_incrementor (n): return lambda x: x + n
>>>
>>> f = make_incrementor(2)
>>> g = make_incrementor(6)
>>>
>>> print f(42), g(42)
44 48
>>>
>>> print make_incrementor(22)(33)
55

The above code defines a function "make_inrementor" that creates an anonymous
function on the fly and returns it. The returned function increments its argument by
the value that was specified when it was created.

You can now create multiple different incrementor functions and assign them to
variables, then use them independent from each other. As the last statement
demonstrates, you don't even have to assign the function anywhere -- you can
just use it instantly and forget it when it's not needed anymore.

The following takes this a step further.
>>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
>>>
>>> print filter(lambda x: x % 3 == 0, foo)
[18, 9, 24, 12, 27]
>>>
>>> print map(lambda x: x * 2 + 10, foo)
[14, 46, 28, 54, 44, 58, 26, 34, 64]
>>>
>>> print reduce(lambda x, y: x + y, foo)
139

First we define a simple list of integer values, then we use the standard functions
filter(), map() and reduce() to do various things with that list. All of the three
functions expect two arguments: A function and a list.

-----------------------------

The following example is one way to compute prime numbers in Python (not the most
efficient one, though):
>>> nums = range(2, 50)
>>> for i in range(2, 8):
... nums = filter(lambda x: x == i or x % i, nums)
...
>>> print nums
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

-----------------------------

In the following example, a sentence is split up into a list of words, then a list is
created that contains the length of each word.
>>> sentence = 'It is raining cats and dogs'
>>> words = sentence.split()
>>> print words
['It', 'is', 'raining', 'cats', 'and', 'dogs']
>>>
>>> lengths = map(lambda word: len(word), words)
>>> print lengths
[2, 2, 7, 4, 3, 4]

-----------------------------

Sorting in Python

Sorting in Python

To sort a list in Python, use the “sort” method. For example:

li=[1,9,2,3];
li.sort();
print li;

Note that sort is a method, and the list is changed in place.

Suppose you have a matrix, and you want to sort by second column. Example Solution:

li=[[2,6],[1,3],[5,4]]
li.sort(lambda x, y: cmp(x[1],y[1]))
print li; # prints [[1, 3], [5, 4], [2, 6]]

The argument to sort is a function of two arguments, that returns -1, 0, 1. This function is a decision function that tells sort() how to decide the order of any two elements in your list. If the first argument is “less” then second argument, the function should return -1. If equal, then 0. Else, 1.

-------------------------

Here's a more complex example. Suppose you have a list of strings.

'my283.jpg'
'my23i.jpg'
'web7-s.jpg'
'fris88large.jpg'
...

You want to sort them by the number embedded in them. What you have to do, is to provide sort() method a order-deciding function, that takes two strings, and compares the integer inside the string. Here's the solution:

li=[
'my283.jpg',
'my23i.jpg',
'web7-s.jpg',
'fris88large.jpg',
]

def myComp (x,y):
import re
def getNum(str): return float(re.findall(r'\d+',str)[0])
return cmp(getNum(x),getNum(y))

li.sort(myComp)
print li # returns ['web7-s.jpg', 'my23i.jpg', 'fris88large.jpg', 'my283.jpg']

Here, we defined a function myComp to tell sort about the ordering. Normally, one would use the “lambda” construct, but Python's lambda construct cannot be used to define any function that takes more than one Python-line to express.

-------------------------

Python's “sort” method's optional parameters: “key” and “reverse

Most of the time, sorting is done for a list of atomic element such as [3,2,4]. This is simply done by myList.sort() without any argument. Other than simple list, sort is frequently used on matrixes (e.g. [[2,6],[1,3],[5,4]]). For matrixes, almost always a particular column is used for the basis of ordering. For example, if we want to sort by second column, we do: “li.sort(lambda x, y: cmp(x[1],y[1]))”. Since this is frequently used, Python provides a somewhat shorter syntax for it, by specifying the column used as the ordering “key”. For example:

li=[[2,6],[1,3],[5,4]]
li.sort(key=lambda x:x[1] ) # is equivalent to the following
#li.sort(lambda x, y: cmp(x[1],y[1]))
print li; # prints [[1, 3], [5, 4], [2, 6]]

Because the Python compiler is not very refined, this specialized syntax is algorithmically a order of magnitude faster than the general form “lambda x, y: cmp(x[1],y[1])”.

That is to say, the programer must now learn 2 syntaxes for the ordering function, one general and one specialized, and he must choose the right one otherwise he will be penalized by a magnitude of slow down. This also means that, in order to make the right choice of the syntax, the programer must known in advance of the nature of the list to be sorted.

Another idiosyncratic provision is the optional “reverse” argument. This parameter is necessary when using the “key” parameter. In the normal order comparison function lambda(x,y), the ascending/descending nature can be changed by simply switching the parameters x and y. But now a single “key=lambda(x)” can't provide that, thus another optional parameter “reverse” is provided. Example:

The following are equivalent:

li.sort(key=lambda x:x[1], reverse=True )
li.sort(lambda x, y: cmp(x[1],y[1]), reverse=True)
li.sort(lambda x, y: cmp(y[1],x[1]))

Of course, one can reverse a list by the “reverse()” method for lists. But li.sort(...).reverse() is algorithmically another order of magnitude slower since Python compiler is not smart.

-----------------------

Sorting a list of objects by multiple attributes in python

Sorting a list of objects by multiple attributes in python

In Python 2.5 you can do this with operator.attrgetter():

L.sort(key=operator.attrgetter('project', 'message', 'created_on'))

What is Lambda in Python

What is Lambda in Python

Python's lambda allows you to declare a one-line nameless minifunction on the fly.
The format is:
lambda parameter(s): expression using the parameter(s)
It returns the result of the expression. The expression has to be a one-liner
(no newlines)! Here is a little example ...

import string
wordList = ['Python', 'is', 'really', 'great', 'stuff']
wordList.sort()
print "After standard sort (ASCII upper case is before lower case):"
print wordList

wordList.sort(lambda x, y: cmp(string.lower(x), string.lower(y)))
print "After case insensitve sort with lambda and lower:"
print wordList