Monday, September 20, 2010

Python : Virtual Python Environment builder

Virtual Python Environment builder
virtualenv is a tool to create isolated Python environments.
The basic problem being addressed is one of dependencies and versions, and indirectly permissions. Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications? If you install everything into /usr/lib/python2.4/site-packages (or whatever your platform's standard location is), it's easy to end up in a situation where you unintentionally upgrade an application that shouldn't be upgraded.
Or more generally, what if you want to install an application and leave it be? If an application works, any change in its libraries or the versions of those libraries can break the application.
Also, what if you can't install packages into the global site-packages directory? For instance, on a shared host.
In all these cases, virtualenv can help you. It creates an environment that has its own installation directories, that doesn't share libraries with other virtualenv environments (and optionally doesn't use the globally installed libraries either).

The basic usage is:
$ python ENV

Friday, September 10, 2010

Django : FileField() or ImageField()

In Django its very simple to handle images and file field.

First you define the ImageField and FileField in Class.
for example :
Class File(models.Model):
image =models.ImageField(upload_to = 'path')
File =models.FileField(upload_to = 'path')

Using a FileField or an ImageField in a model takes a few steps:
  1. In your settings file, you'll need to define MEDIA_ROOT as the full path to a directory where you'd like Django to store uploaded files. (For performance, these files are not stored in the database.) Define MEDIA_URL as the base public URL of that directory. For Example :MEDIA_ROOT = "/media/"  , MEDIA_URL = 'http://localhost/media/'
  2. Add the FileField or ImageField to your model, making sure to define the upload_to option to tell Django to which subdirectory of MEDIA_ROOT it should upload files.
  3. All that will be stored in your database is a path to the file (relative to MEDIA_ROOT). You'll most likely want to use the convenience url attribute provided by Django. For example, if your ImageField is called file, you can get the absolute URL to your image in a template with {{ object.file.url }}.

Saturday, September 4, 2010

Django application : Contact form

Make the Contact form in django and also mail is send to admin .
from django import forms
class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
email = forms.EmailField(required=False, label='Your e-mail address')
message = forms.CharField(widget=forms.Textarea)
from django.core.mail import send_mail
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from import *
from django.template import RequestContext
from django.core.urlresolvers import reverse

Django application : Add two number using forms

In Django its very simple to use the Django forms and create a very simple application using django forms.
Example: Add to number using Djnago forms
from django import forms
class Output(forms.Form):
input1 = forms.FloatField()
input2 = forms.FloatField()

Script :Find and Replace word from files.

Script for Replace the One word with another word from all files place in present directory.

And also take backup of old files.

Step to use :
> Copy the below script in file
> Change the permission of file $chmod u+x filename
> Run this ./filename
> FindWord is word to find and replace With ReplaceWord
for fl in *.php; do
mv $fl $fl.old
sed 's/FindWord::/ReplaceWord::/g' $fl.old > $fl