Installing Python2.6.7 On CentOS5

Source Link: http://www.hostucan.com/webmaster-tutorials/centos-python-2.6-install
Install Python 2.6 on CentOS 5

CentOS 5 is the most common Python hosting environment in the market, but the default version of python it is 2.4 now. As some projects request the python version to be greater than V2.5, in this article, you will see how to install python 2.6 by compiling source code step by step.

1. Download python source from python.org and Uncompress it.

$ wget http://www.python.org/ftp/python/2.6.7/Python-2.6.7.tgz
$ tar xzvf Python-2.6.7.tgz
2. Build & Install. In the source code folder:
$ ./configure –prefix=/opt/python2.6 –with-threads –enable-shared –with-zlib=/usr/include
$ make
$ make install
3. Add Python 2.6 lib to system lib. execute:
$ sudo ln -s /opt/python2.6/lib/libpython2.6.so /usr/lib
$ sudo ln -s /opt/python2.6/lib/libpython2.6.so.1.0 /usr/lib

# Create necessary links and cache
$ sudo /sbin/ldconfig -v

# make a symbolic link to python
$ sudo ln -s /opt/python2.6/bin/python /usr/local/python
4. Check python version use “python -V”. If you still see old version of Python, you need to edit /etc/profile (as root), add the following line:
pathmunge /opt/python2.6/bin/
Above setting should be added before the line like:
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC
Then re-login system, try “python -V” again. 5. After install yum, try “yum search php” This is to make sure we did not accidentally mess up “yum”, which is the biggest threat when upgrading Python version. If you see no errors, you should be good to start using Python 2.6.

Update python-MySQLdb

http://blog.csdn.net/inte_sleeper/article/details/6556103

Python-OptParser Global args operations.


def setup_cmdline_parser():
    parser = _o.OptionParser()
    parser.add_option("-l", "--live", dest="live", action="store_true",
                      default=True, help = "show the ncurses live monitoring frontend (default)")
    parser.add_option("-n", "--notlive", dest="live", action="store_false",
                      default="True", help = "write to file instead of live monitoring")
    parser.add_option("-p", "--prefix", dest="prefix",
                      default = "log", help="prefix to use for output files")
    parser.add_option("-t", "--time", dest="duration", action="store", type="int", default=10,
            help="stop logging to file after this much time has elapsed (in seconds). set to 0 to keep logging indefinitely")
    return parser


def main():
    global options
    global args
    global domains

    parser = setup_cmdline_parser()
    (options, args) = parser.parse_args()

    if len(args):
        parser.error("No parameter required")
    if options.mspersample < 0:
        parser.error("option --ms_per_sample: invalid negative value: '%d'" %
                     options.mspersample)
    # If --ms_per_sample= is too large, no data may be logged.
    if not options.live and options.duration != 0 and \
       options.mspersample > options.duration * 1000:
        parser.error("option --ms_per_sample: too large (> %d ms)" %
                     (options.duration * 1000))

    start_xenbaked()
    if options.live:
        show_livestats(options.cpu)
    else:
        try:
            writelog()
        except:
            print 'Quitting.'
    stop_xenbaked()

Using options, args out of functions with “global”


Python-Django clean_data 和 cleaned_data

clean_data 和 cleaned_data

在我们使用Django的表单对象时我们会对表单对象进行验证,比如:
>>>python manage.py shell
>>>from pageage1 import MyForm
>>>form=MyForm({‘username’:’Eric’,’email’:’test@test.com’})
>>>form.is_valid()
True

如果验证通过,你可以通过form.clean_data访问这些验证通过的数据,不过这是在Django0.96中的方式,在Django1.0中这个属性变成了form.cleaned_data:

>>>form.clean_data # Django 0.96
>>>form.cleaned_data # Django 1.0

HVS_system CGI scripts.

get_status.cgi

#!/usr/bin/env python
# -*- coding: UTF-8 -*-# enable debugging

import sys, os
import cgi
import MySQLdb
from urllib import *

host = '10.130.120.53'
user = 'xxxxxx'
passwd = 'xxxxxxx'
db = 'asset'

print "Content-Type: text/plain;charset=utf-8"
print

form = cgi.FieldStorage()
request_host = form['request_host'].value 
request_status = form['status'].value

#check request_host
client_host = os.environ['REMOTE_ADDR']

if request_host != client_host:
    print "Error: illegal host request."
    sys.exit(55)
if request_status != '1':
    print "ERROR: Status %s cannot acceptable!" % request_status
    sys.exit(55)


conn=MySQLdb.connect(host,user,passwd,db)
cursor = conn.cursor()
sql_getstatus = 'select domu_name,status from hvs_domu_info where eth0 = "%s"' % request_host
n = cursor.execute(sql_getstatus)

#if not n:
#    print "Error: Bad request from host."
#    sys.exit(55)
rows = cursor.fetchall()
domu_name = rows[0][0]
status = rows[0][1]


print "Request_host : %s\nCheck status : %s" %(request_host,status)

def boot_status(status):
    if status == 0:
        boot = "off"
    elif status == 1:
        boot = "idle"
    elif status == 2:
        boot = "on"
    else:
        boot = "Uknown"
    return boot

if boot_status(status) == 'off':
    sql = 'update hvs_domu_info set status = 1 where eth0 ="%s"' % request_host
    e = cursor.execute(sql)
    conn.commit()
    n = cursor.execute(sql_getstatus)
    rows = cursor.fetchall()
    for row in rows:
        status = row[1]
        domu_id = row[0]
    print "Running status: %s" % boot_status(status)
    print "domu_id: %s" % domu_id
    print "Client host:%s" % client_host
elif boot_status(status) == 'on':
    print "Running status: %s" % boot_status(status)
    print "domu_id: %s" % domu_id
elif boot_status(status) == 'idle':
    print "domu_id: %s" % domu_name
    print "Warnning: Instance: %s already initialized." % domu_name
conn.close()

Python plugin setting with Vim

1. Go to: http://vim.sourceforge.net/scripts/script.php?script_id=850
Download the plugin which you need.

package script version date Vim version user release notes
pydiction-1.2.zip 1.2 2009-07-25 7.0 ryan kulla Added “from module” completion, Tab backwards support, and more third party libs.

Install details UNIX/LINUX: Place python_pydiction.vim in:  ~/.vim/after/ftplugin/

If this directory doesn’t already exist, create it. Vim looks there automatically.

WINDOWS: Assuming you installed Vim to C:\vim\, put python_pydiction.vim in C:\vim\vimfiles\ftplugin

You may install the other files (complete-dict and pydiction.py) anywhere you want. For this example, we’ll assume you put them in C:\vim\vimfiles\ftplugin\pydiction\

Don’t put any files but python_pydiction.vim in the ftplugin directory, only .vim files should go there.

In your vimrc file, first add the following line to enable ftplugins:

filetype plugin on

then make sure you set “g:pydiction_location” to the full path of where you installed complete-dict, i.e.:

let g:pydiction_location = ‘C:/vim/vimfiles/ftplugin/pydiction/complete-dict’