Feeds:
Posts
Comments

Posts Tagged ‘Python’

While i was working on the project onspot_v2, i came across with a specific need. onspot_v2 makes use of the django admin interface for data entry. When employees are entering data, ‘branch’ field in the form will get populated automatically as per the branch of the employee. When a manager is entering data using the form, an extra field should appear to select the ‘branch’ instead of automatically populating. Googling didn’t help much. Finally i thought of overriding the ModelAdmin.add_view() method which is invoked when you try to add an entry using the admin. I changed the editable property of the field ‘branch’ inside after checking the request.user and it worked out. Part of my code in admin.py

from django.contrib import admin
from books.models import Agent, PolicyIssue

class PolicyIssueAdmin(admin.ModelAdmin):
 
    def add_view(self, request, form_url='', extra_context=None):
        if request.user.get_profile().is_employee:
            self.model.branch.field.editable = False
        else:
            self.model.branch.field.editable = True
        return super(PolicyIssueAdmin, self).add_view(request, form_url)

admin.site.register(PolicyIssue, PolicyIssueAdmin)

Read Full Post »

Actually this is a sample question appeared in codejam contest conducted by mobme wireless. Imdbpy python module is used to retrieve movie informations from imdb.

For those who don’t know about this event : http://codejam.mobme.in/

Sample Question :

Write a program that ranks Hollywood actors based on the number of their appearances in a list of top 100 movies. There are a number of top movie lists on the Internet and it’s up to you to choose one. We’d prefer you choose one that has an open API.

Solution in Python :

#!/usr/bin/env python

__author__ = "Rag Sagar.V"
__email__ = '@'.join(['ragsagar','.'.join([_ for _ in ['gmail','com']])])


from twisted.internet import reactor, threads
import re,imdb,itertools


actors_rating = {} #actors_rating['actor name'] = rank
rank = 0
count = 1
current_rank = 0
concurrent = 5
finished = itertools.count(1)
reactor.suggestThreadPoolSize(concurrent)


try:
	imdb_access = imdb.IMDb()
except imdb.IMDbError, err:
	print err
		
top_100 = imdb_access.get_top250_movies()[:100]


def populate_actors(mid):
	movie = imdb_access.get_movie(int(mid))
	#print movie
	for i in (0,1):
		actor_name =  movie['cast'][i]['name']
		if actors_rating.has_key(actor_name):
			actors_rating[actor_name] = actors_rating[actor_name] + 1
		else:
			actors_rating[actor_name] = 1
	if finished.next()==added:
		reactor.stop()
	
added = 0
for movie in top_100:
	added += 1	
	req = threads.deferToThread(populate_actors, movie.getID())

try:
	reactor.run()
except KeyboardInterrupt:
	reactor.stop()	

   
for actor in sorted(actors_rating, key=actors_rating.get, reverse=True):
	previous_rank = current_rank
	current_rank = actors_rating[actor]
	if previous_rank !=  current_rank :
		rank += count
		count = 1
	else:
		count += 1	
	print rank,actor   
    
    

Dependency :
imdbpy

Read Full Post »

Here is a python script to wish you Good Morning or Good Evening according to the time of the day.

#!/usr/bin/python
import time

current_hour = time.strptime(time.ctime(time.time())).tm_hour

if current_hour < 12 :
	print "Good Morning!"
elif current_hour == 12 :
	print "Good Noon!"
elif current_hour > 12 and current_hour < 18 :
	print "Good AfterNoon!"
elif current_hour >= 18 :
	print "Good Evening!"

Now if you want to display it when you open the console add the following line to your ~/.bashrc file.

python /path/to/the/script.py

Happy Hacking! 🙂

Read Full Post »

Older Posts »

%d bloggers like this: