python入门及IDE搭建

IDE:eclipse3.8.2
插件:PyDev 2.8.2
python:python-2.7.6.amd64
具体软件,请朋友们自行下载。
版本一定要对应,pyDev只能选择2.8。选择3.4则eclipse3.8.2认不出来。另外快捷键会冲突,需要修改一下。
启动eclipse
【设置快捷键】
选择window -> preference -> General -> keys
查找Word completion,在binding:里面,按退格键(即:删除快捷键的意思)
查找Content Assist,在binding里面,添加快捷键alt+/
python入门及IDE搭建_第1张图片
【关联python】
选择window -> preference -> PyDev -> INterpreters -> Python Interpreter
然后在上面的框里,new,设置python.exe的路径
python入门及IDE搭建_第2张图片
【例子】
然后启动项目,快乐的体验python吧。其实入门很简单,找几个现成的例子学习一下就可以。
一个简单的demo,这个事我兄弟大伟写的,他的博客是http://ailikes.iteye.com/
其中的语法,都可以通过eclipse的自动提示进行完成。非常爽。基本语法包括异常处理,日期处理,文件操作,for循环,if语句等等吧。请大家自行体会吧
# @author: xudawei
# @date:2014-03-12

import os
import shutil
import time
import socket
import sys
import logging
from test.test_socket import try_address


CURRENT_TIME = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
DEPLOY_HOME = "/application/search/vertical-search"
# logs directory
LOG_HOME = "/data0/search/vertical-search/vertical-search-deploy/logs"
# temp directory
TMP_HOME = "/tmp/src"
BACKUP_HOME = "/data0/search/vertical-search/backup/%s" % CURRENT_TIME
SERVER_IP = ["10.10.160.68", "10.10.160.69", "10.10.160.70"]
VERTICAL_SEARCH_SVN = "http://svn.pangu365.com/svn/techrd/apps/base_application/ba-vertical-search/trunk"

def package():
    try:
        # download source
        # maybe required username and password
        os.system("rm -rf %s" % TMP_HOME)
        os.system("svn export %s %s" % (VERTICAL_SEARCH_SVN, TMP_HOME))
        # generate package
        os.system("cd %s/vertical-search-core && mvn clean -U deploy -Dmaven.test.skip=true -T 1C" % TMP_HOME)
        logging.info("package success")  
    except Exception, e:
        logging.error("package error: %s" % e)
        
# backup and deploy
def deployAll():
     deployService()
     deploySchedule()
     deployImport()
     deployShell()
     
def deployService():
    # backup,stop,delete,scp,start
    try:
        os.system("cd %s/vertical-search-service && mvn clean package -U -Dmaven.test.skip=true -Ponline -T 1C" % TMP_HOME)
        for index, item in enumerate(SERVER_IP):
            os.system("ssh %s \"cp -r %s/vertical-search-service/ %s\"" % (item, DEPLOY_HOME, BACKUP_HOME))
            os.system("ssh %s \"sh %s/vertical-search-service/bin/server.sh stop\"" % (item, DEPLOY_HOME))
            os.system("ssh %s \"rm -rf %s/vertical-search-service/* \"" % (item, DEPLOY_HOME))
            os.system("scp -r %s/vertical-search-service/target/deploy/* %s:%s/vertical-search-service/" % (TMP_HOME, item, DEPLOY_HOME))
            os.system("ssh %s \"sh %s/vertical-search-service/bin/server.sh start \"" % (item, DEPLOY_HOME))
            logging.info("deployService success at : %s" % item)  
    except Exception, e:
        logging.error("deployService error: %s" % e)      
              
# schedule same as service
def deploySchedule():
    try:
        os.system("cd %s/vertical-search-schedule && mvn clean package -U -Dmaven.test.skip=true -Ponline -T 1C" % TMP_HOME)
        for index, item in enumerate(SERVER_IP):
            os.system("ssh %s \"cp -r %s/vertical-search-schedule/ %s\"" % (item, DEPLOY_HOME, BACKUP_HOME))
            os.system("ssh %s \"sh %s/vertical-search-schedule/bin/server.sh stop\"" % (item, DEPLOY_HOME))
            os.system("ssh %s \"rm -rf %s/vertical-search-schedule/* \"" % (item, DEPLOY_HOME))
            os.system("scp -r %s/vertical-search-schedule/target/deploy/* %s:%s/vertical-search-schedule/" % (TMP_HOME, item, DEPLOY_HOME))
            os.system("ssh %s \"sh %s/vertical-search-schedule/bin/server.sh start \"" % (item, DEPLOY_HOME))
            logging.info("deploySchedule success at : %s" % item)  
    except Exception, e:
        logging.error("deploySchedule error: %s" % e)   
                         
def deployImport():
    try:
        for index, item in enumerate(SERVER_IP):
            os.system("ssh %s \"cp -r %s/vertical-search-import/ %s\"" % (item, DEPLOY_HOME, BACKUP_HOME))
            os.system("ssh %s \"rm -rf %s/vertical-search-import/* \"" % (item, DEPLOY_HOME))
            os.system("scp -r %s/vertical-search-import/* %s:%s/vertical-search-import/" % (TMP_HOME, item, DEPLOY_HOME))
            logging.info("deployImport success at : %s" % item)  
    except Exception, e:
        logging.error("deployImport error: %s" % e)
        
def deployShell():
    try:
        for index, item in enumerate(SERVER_IP):
            os.system("ssh %s \"cp -r %s/vertical-search-shell/ %s\"" % (item, DEPLOY_HOME, BACKUP_HOME))
            os.system("ssh %s \"rm -rf %s/vertical-search-shell/* \"" % (item, DEPLOY_HOME))
            os.system("scp -r %s/vertical-search-shell/* %s:%s/vertical-search-shell/" % (TMP_HOME, item, DEPLOY_HOME))
            logging.info("deployShell success at : %s" % item)  
    except Exception, e:
        logging.error("deployShell error: %s" % e)
        
if __name__ == "__main__":
    try:
        os.system("mkdir -p %s" % LOG_HOME)
        logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s", filename=LOG_HOME + "/deploy.log")
        
        logging.info("begin deploy vertical-search")
        # prepared
        package()
        # deploy
        if len(sys.argv) == 1:
            deployAll()
        else:
            for index, item in enumerate(sys.argv):
                if item == sys.argv[0]:
                    logging.info("shell name is %s" % item)
                elif item == "shell":
                    logging.info("the deploy project is shell")
                    deployShell()
                elif item == "import":
                    logging.info("the deploy project is import")
                    deployImport()
                elif item == "service":
                    logging.info("the deploy project is service")
                    deployService()
                elif item == "schedule":
                    logging.info("the deploy project is schedule")
                    deploySchedule()
                else:
                     print("input error shell|import|service|schedule")
                     
        localIP = socket.gethostbyname(socket.gethostname())
        logging.info("this Script is running at: %s" % localIP)
        # delete all source
        shutil.rmtree(TMP_HOME)
        os.system("rm -rf %s" % TMP_HOME)
        logging.info("deploy vertical-search success")
    except Exception, e:
        logging.error("main error: %s" % e)

你可能感兴趣的:(python)