Official ObjectGraph Blog

Archive for the ‘python’ Category

OpenFlashCards.com Launched

Thursday, March 27th, 2008

We would like to announce that OpenFlashCards.com was published today. This is a website which you can browse,create, and share multiple choice questions flash cards. The interface is simple and easy to start to create your own cards. Please feel free to create your cards. We will add more features on this website :
1. Embedding Cards in your website.
2. Community Functions such as rating system.
3. More configuration about flash cards settings.


http://www.openflashcards.com/

Check these screen shots:

Please give us your suggestion and recommended categories in this comment box!

PageChat Update: Smaller Player

Saturday, March 22nd, 2008

We enhanced the pagechat player to hide the top bar and users menu. Now all you have to do is copy and paste the following code

<script>
var pagechat_width = ‘600px’;
var pagechat_height = ‘400px’;
</script>
<script
src=”http://s1.pagechat.com/pagechat/js/embed.js”
id=”pagechat_embed”>
</script>

Check it out @ http://www.oreillymaker.com

Announcing PageChat.com

Tuesday, February 5th, 2008

PageChat.com is an innovative tool to communicate with anyone by dynamically creating chat rooms.

You could also use page chat to communicate with others about various topics of interest. Integrate our chat rooms with your websites/blogs to create a unique experience for your audience to chat with each other while on the same page.

We are currently releasing this version as a public beta and these are upcoming features:

  • Sharing Files
  • Guest View
  • Youtube integration

Please Check it out @

http://www.pagechat.com/

Enjoy!

PIL on captcha - Transparency and Masking for CAPTCHA

Friday, January 19th, 2007

It seems that there are no good introductions for how to create transparent image text. However we want it for common image module like CAPTCHA which is an image authentication method. It’s easy to just draw text on image, but in order to rotate the text, you have to rotate the image with the text. I think one of the solution is using mask after rotation. You need 3 images to do this.

1. Background

2. Text

3. Mask

2 and 3 must be in a same size, and when you “paste” the text, you can specify the mask as the extra paramter.

This is simple exapmle, to rotated “Hello” on the background hello.jpg


import Image,ImageDraw,ImageFont

# Create a background image
image = Image.new('RGB', (300, 50), (220,210,190))
draw = ImageDraw.Draw(image)

# Create a text image
textImg = Image.new('RGB',(150,40),(0,0,0))
tmpDraw = ImageDraw.Draw(textImg)
textFont = ImageFont.truetype('ARIAL.TTF',36)
tmpDraw.text((0, 0), 'Hello', font = textFont, fill = (10,200,200))
textImg = textImg.rotate(-10)

# Create a mask (same size as the text image)
mask = Image.new('L',(150, 40),0)
mask.paste(textImg,(0,0))

# Paste text image with the mask
image.paste(textImg,(100,0),mask)

image.save('hello.jpg')


In order to generate CAPTCHA-like image, you might want to apply random xy, color, font size, lines, etc…

So it’ll be like this:

captcha.jpg

This is entire source for these examples. Please install PIL (do easy_install PIL) first.


Download Source Code Here (7.95KB)



Line numbers and other cool stuff Added to Pygments.com

Sunday, January 14th, 2007

I added line numbers and some formatting capabilities to Pygments.com

Now the script tag renders line numbers by default. Also RTF download of colored code is available on the Test Page for those nice looking word documents

Check it out @ http://www.pygments.com

Pygments.com Launched

Saturday, January 13th, 2007

I purchased Pygments.com domain yesterday. Iam surprised the domain is still available. i guess the Pygments guys dont really care about the .com domain name.



Any way access it via http://www.pygments.com


You can use the code colorizer by going to the
Colorizer Test Program.


But the cool thing i added is, a javascript service to colorize any code.
Lets say you have a webpage with code, that needs colorizing, Just add this Javascript to the end of the page (Before you close the body tag)



<script src=”http://www.pygments.com/js/init.js” type=”text/javascript”></script>



All you will need to do is define your code in <pre> tags with unique ids that include some information about the type of the language.



For example:



<pre id=”python_1″>



print “Hello World”

</pre>


It will automatically colorize the page by making JSON type requests to pygments.com with the code in the page.

So the above would come out like this


print "Hello World"


BTW: pygments javascript is enabled on this site

Recompilining Apache with mod python

Friday, January 5th, 2007

After a long struggle with getting Django working with Apache, I came to the conclusion the Apache that came with Cent OS does not work well with the newly compiled Python 2.4.4, It seems to fall back on the older version of python (2.3.4) which is already in the system.



So i decided to compile Apache from source and it works beautifully.



And apache is very clean, when you compile from scratch. I was able to compile the latest version (2.2) and recompile mod python.



Removed all httpd related packages from CentOS

php-5.1.6-1.2.1.centos
php-mysql-5.1.6-1.2.1.centos
php-pear-1.4.9-1.2.centos
php-pdo-5.1.6-1.2.1.centos
httpd-suexec-2.0.52-28.ent.centos4
httpd-2.0.52-28.ent.centos4



Then got the latest version of apache.


#wget http://apache.mirrormax.net/httpd/httpd-2.2.3.tar.gz
#tar xvfz httpd-2.2.3.tar.gz
#cd httpd-2.2.3
#./configure
#make
#make install



this time mod python was again complaining. You will have to compile with the apxs installed by the new apache 2.2.3


#./configure --with-apxs=/usr/local/apache2/bin/apxs



So what did i learn from this exercise.



Always recompile everything unless you want to stick to versions of python that came with the distro.

Installing Python 2.4.4 on Cent OS 4.4 (Final)

Thursday, January 4th, 2007

We at ObjectGraph are moving to a dedicated hosting solution. The server is hosted in NY. It has Cent OS 4.4 (Based on RHEL 4).

It comes installed with Python 2.3.4. A lot of packages (like yum) depend on this version of python in the system.

So when i found out that some of the syntax like ‘@staticfunction’ we were using to develop in django is not compatible with python 2.3.4, I was distraught. I decided i need to upgrade python.

Here is a simple test program that fails in the older version of Python.


class Test:
        def __init__(self):
                print "Hello World!"
        def sum(self,a,b):
                return a+b

        @staticmethod
        def sum1(a,b):
                return a+b
t=Test()
print t.sum(10,10)
print t.sum1(10,10)

After a lot of struggle to find a binary package of Python 2.4.4 for Cent OS, I gave up and compiled python from source.

It was pretty straightforward and i am happy with the results. This is what i did.

#wget http://www.python.org/ftp/python/2.4.4/Python-2.4.4.tgz
#tar xvfz *.tgz
#cd Python-2.4.4
#./configure
#make
#make install

Just run python to see if it is the version 2.4.4

Now python is cleanly installed. The site-packages folder under /usr/local/lib/python2.4 are empty. So get the latest version of django and install it


#svn co http://code.djangoproject.com/svn/django/trunk/ django
#cd django
#python setup.py install

Similary you can install MySQLdb and other libraries you want on python.
Now the next steps for us is to get django running on Apache using mod_python