hello python
which python
export PATH=/home/q/python27/bin:$PATH
python -V
Python 2.7.6 (default, Dec 28 2013, 20:51:17)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'hello, world'
hello, world
$ python -c "print 'hello, world'"
hello, world
pip install ipython
pip install bpython
Python 做日常不需要 IDEREPL + 编辑器 =『试代码』= 正确的打开方式
basic syntax -- getting help
>>> help(5)
Help on int object:
(etc etc)
>>> dir(5)
['__abs__', '__add__', ...]
>>> abs.__doc__
'abs(number) -> number
Return the absolute value of the argument.'
basic syntax -- data types
a_int = 1
a_float = 3.14159265358979323846
a_str = 'a'
a_str, b_str = 'a', "b" # 单双引号有区别吗?
a_bool = False
a_list = [2, 1, 'x', True]
a_tuple = (3, 5)
b_tuple = (3, )
is_c_a_tuple = (3)
# 我是注释:看看 c 是什么类型
t = type(is_c_a_tuple)
'''
我是多行注释
也可以当做文档字符串
同时也是多行字符串的表示,写 sql 用这个可以排版哦
比 java[script] 一坨加号爽多了
'''
print t
a_set = {3, 1, 3, 2}
a_dict = { 'a': 22, 4: 'bb', 'u': u'哦,差点忘了 unicode string' }
basic syntax -- flow control
rangelist = range(10)
>>> print rangelist
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for number in rangelist:
# Check if number is one of
# the numbers in the tuple.
if number in (3, 4, 7, 9):
break
else:
continue
else:
# The "else" clause is optional and is
# executed only if the loop didn't "break".
pass # Do nothing
if rangelist[1] == 2:
print "The second item (lists are 0-based) is 2"
elif rangelist[1] == 3:
print "The second item (lists are 0-based) is 3"
else:
print "Dunno"
while rangelist[1] == 1:
pass
basic syntax -- function
# Same as def funcvar(x): return x + 1
funcvar = lambda x: x + 1
>>> print funcvar(1)
2
# an_int and a_string are optional, they have default values
# if one is not passed (2 and "A default string", respectively).
def passing_example(a_list, an_int=2, a_string="A default string"):
a_list.append("A new item")
an_int = 4
return a_list, an_int, a_string
>>> my_list = [1, 2, 3]
>>> my_int = 10
>>> print passing_example(my_list, my_int)
([1, 2, 3, 'A new item'], 4, "A default string")
>>> my_list
[1, 2, 3, 'A new item']
>>> my_int
10
basic syntax -- exception
def some_function():
try:
# Division by zero raises an exception
10 / 0
except ZeroDivisionError:
print "Oops, invalid."
else:
# Exception didn't occur, we're good.
pass
finally:
# This is executed after the code block is run
# and all exceptions have been handled, even
# if a new exception is raised while handling.
print "We're done with that."
>>> some_function()
Oops, invalid.
We're done with that.
basic syntax -- class
# We subclass from object to get a class.
class Human(object):
# A class attribute. It is shared by all instances of this class
species = "H. sapiens"
# Basic initializer
def __init__(self, name):
# Assign the argument to the instance's name attribute
self.name = name
# An instance method. All methods take "self" as the first argument
def say(self, msg):
return "%s: %s" % (self.name, msg)
# A class method is shared among all instances
# They are called with the calling class as the first argument
@classmethod
def get_species(cls):
return cls.species
# A static method is called without a class or instance reference
@staticmethod
def grunt():
return "*grunt*"
daily routines -- begin
#!/usr/bin/env python
# coding=utf-8
import os
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
pwd = os.path.dirname(os.path.abspath(__file__)) + '/'
def main():
print 'hello python'
if __name__ == '__main__':
main()
daily routines -- postgres
import psycopg2
import psycopg2.extras
def get_hs():
con = psycopg2.connect(
database = 'hotel',
user = 'searcher',
password = '''abcdefg1234567''',
host = 'l-abc.def.com',
port = 5435
)
return con
def get_cursor(con):
return con.cursor(cursor_factory=psycopg2.extras.DictCursor)
con = get_hs()
cur = get_cursor(con)
try:
cur.execute('begin')
cur.execute('truncate table city_suggest_app_info')
insert_sql = 'insert into city_suggest_app_info(city_id, citytag, app, app_info) values (%s, %s, %s, %s)'
cur.executemany(insert_sql, rows)
cur.execute('commit')
except:
logger.error('something wrong')
cur.execute('rollback')
finally:
cur.close()
con.close()
daily routines -- mysql
import pymysql
def get_hotel_hive():
con = pymysql.connect(
host='l-abc.def.com',
port=3306,
user='hotel_hive',
passwd='xxx',
db='hotel_hive',
charset='utf8'
)
return con
con = get_hotel_hive()
cur = con.cursor(pymysql.cursors.DictCursor)
seq_score = {}
cur.execute("select seq, score from top_query_all where cityurl = 'chengdu' and server_tag = 'A' and query = '7天连锁' ")
for r in cur.fetchall():
seq = r['seq']
seq_score[seq] = r['score']
con.commit()
cur.close()
con.close()
daily routines -- http request
import requests
resp = requests.get('http://tuan.aaa.bbb.com/render/cityHotelCount.jsp')
jd = resp.json()
for city, count in jd['cities'].items():
info = t.get(city)
if not info:
continue
info['tuan']['weight'] = info['hs']['weight']
info['tuan']['online_count'] = count
daily routines -- json
In [71]: import json
In [72]: d = {'a': '你好', '世界': 'b'}
In [73]: print json.dumps(d)
{"a": "\u4f60\u597d", "\u4e16\u754c": "b"}
In [74]: print json.dumps(d, ensure_ascii=False, indent=2, sort_keys=True)
{
"a": "你好",
"世界": "b"
}
In [77]: s = '{"ping":"pong", "a":3, "中文":"2014-05-19"}'
In [78]: jd = json.loads(s)
In [79]: for k, v in jd.items():
....: print k, v, type(v)
....:
a 3 <type 'int'>
中文 2014-05-19 <type 'unicode'>
ping pong <type 'unicode'>
daily routines -- http server
python -m SimpleHTTPServer 8000
# 比较简单的文件传文件策略(网上可找到支持带上传的版本)
# pip install Flask
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
# $ python hello.py
# * Running on http://localhost:5000/
daily routines -- dev ops
from fabric.api import *
import datetime, time, re
env.user = 'gavin'
def h(h_str):
p = re.compile('\[(\d+)-(\d+)\]')
m = p.search(h_str)
a = int(m.group(1))
b = int(m.group(2))
env.hosts = [re.sub(p, str(i), h_str) for i in range(a, b + 1)]
def r(cmd):
run(cmd)
def s(cmd):
sudo(cmd)
daily routines -- dev ops
@parallel
scripts = ['export_ctrip_view.sh', 'export_detail_click.sh']
with cd('/home/q/tools/bin'):
for s in scripts:
sudo('rm %s' % s)
put('./%s' % s, s, use_sudo=True)
sudo('chown root:root %s' % s)
sudo('chmod a+x %s' % s)
@parallel
def add_group_and_user():
sudo('groupadd hadoop_hotel')
sudo('sudo useradd hadoop_hotel -g hadoop_hotel -M')
sudo('sudo usermod -p "your_password" hadoop_hotel')
fab -f fab.py h:h_str=l-pgstats[1-2].h.xxx r:cmd=uptime
daily routines -- dev ops
[inet_http_server] ; inet (TCP) server disabled by default
port=*:8091 ; (ip_address:port specifier, *:port for all iface)
username=gavin ; (default is no username (open server))
password=gavin ; (default is no password (open server))
[program:head_image_sync]
command = /home/work/python27/bin/python /home/work/app/head_image_sync/web.py
directory = /home/work/app/head_image_sync/
autostart = true
autorestart = true
stderr_logfile = /home/work/app/head_image_sync/logs/stderr.log
stdout_logfile = /home/work/app/head_image_sync/logs/stdout.log
supervisord -f xxx.conf
supervisorctl start/stop/restart your_app
supervisorctl update/reload
daily routines -- misc
def get_logger():
level = logging.DEBUG
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(module)s[%(lineno)d] - %(funcName)s - %(message)s')
logger = logging.getLogger(__name__)
logger.setLevel(level)
ch = logging.StreamHandler()
ch.setLevel(level)
ch.setFormatter(formatter)
logger.addHandler(ch)
return logger
logger = get_logger()