memorandum备忘录

python doc:python standard library

https://docs.python.org/3/library/index.html

 

python2 -m pip install --upgrade pip --force-reinstall

 

安装easy_install

  下载:ez_setup.py, 运行:python ez_setup.py --user

安装pip

  下载:get-pip.py 运行:python 

或者:

  download 相应的包:https://pypi.python.org/simple/pip/

  easy_install 上面的包

  注意:使用Version 1.5会有验证,建议选Version 1.2

安装指导:https://pypi.python.org/pypi/setuptools/1.0#using-setuptools-and-easyinstall

可能遇到的error:

1. windows error:cannot find vcvarsall.bat

  SET VS90COMNTOOLS=%VS100COMNTOOLS%

可以解决。原因,pip 基于visual c++ 2008, 若安装的是2010则运行以上command,若安装的是更高版本,请参看 环境变量 设置。

2. error: Cannot fetch index base URL http://pypi.python.org/simple/

  手动下载包,pip install 包

  有说是网络连接问题,

    代理问题,加 --proxy "IP:port", e.g. pip install APScheduler --proxy "proxy.cd.intel.com:911"

 

 

用命令行查看python自带文档:

      在command window 输入python 然后回车。

      dir(str)       str.__dict__

      help(str.split)

import __builtin__

help(__builtin__.hex)

 

import sys
print sys.builtin_module_names

 

 python比较好的IDE:

      Ulipad 轻量,调试不太好

      Eric 不错

      Eclipse 的pyDev插件  不错

      pycharm

 

 规则(The Zen of Python):

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

 

去掉 list 中的重复元素:

{}.fromkeys(list).keys()
or
set(list)

 构造 dict 并且将每个 value 初始化为0。

注意:不要使用{}.fromkeys(list, {}) 或 {}.fromkeys(list, []), 这样的初始化会使所有key对应同一个{}或[].

{}.fromkeys(list, 0)
or
dict.fromkeys(list, 0)

dict 按value 的降序排序, 结果为 tuple list

from operator import itemgetter
sorted(adict.iteritems(), key=itemgetter(1), reverse=True)

sorted(adict.items(), key=lambda d: d[1], reverse=True)

 dict 按 key 的升序排序

from operator import itemgetter
sorted(adict.iteritems(), key=itemgetter(0), reverse=False)

获取文件夹大小

def get_dir_size(path):
    size = 0L
    for parent, dirs, files in os.walk(path):
        size += sum([os.path.getsize(os.path.join(parent, f)) for f in files])
    return format(size,',')

正则表达式

    p = re.compile(r'\[*L(.+);')
    array = p.findall(s)

#如果 s = '[Ljava/lang/String;Ljava/io/File;'
#则返回 ['java/lang/String;Ljava/io/File']
    p = re.compile(r'\[*L(.+?);') #非贪婪模式
    array = p.findall(s)

#如果 s = '[Ljava/lang/String;Ljava/io/File;'
#则返回 ['java/lang/String', 'java/io/File']

 遍历:

for k in xrange(0,20,5):
    print k

#=>
0
5
10
15
    cuts = [2, 3, 6]
    for i, j in zip([0]+cuts, cuts+[None]):
        print i, j
#=>
0 2
2 3
3 6
6 None

字符串切片:

def test_3(s):
    cuts = [2, 3, 6]
    pieces = [s[i:j] for i, j in zip([0]+cuts, cuts+[None])]
    print pieces

 字符串合并

batches_output = '%s\t%s\t%s\t%s\t%s\n'
print batches_output%( '1', '2', '3', 4, 5)

 遍历list

for index, item in enumerate(alist):
    print index, item

遍历文件夹

import os
for parent, dirs, files in os.walk(dirname):
    for d in dirs:
        print os.path.join(parent, d)
    for f in files:
        print os.path.join(parent, f)

以当前时间生成output 文件夹名

EXECUTED_TIME = time.time()
EXECUTED_TIME_STR = '{0}_{1:03}'.format(time.strftime('%Y_%m_%d_%H_%M_%S',time.localtime(EXECUTED_TIME)), int(EXECUTED_TIME * 1000) % 1000)
OUTPUT_DIR = './output/{0}'.format(EXECUTED_TIME_STR)

# 2014_10_14_10_11_59_679
# 2014_10_10_09_41_06_223

 

 list 加法

l = [2, 3]
s = ["dfjo"]+l  
>>s
['dfjo', 2, 3]

 

 python3元组作为函数参数

def test(name, birth, flag):
    print(name)
    print(birth)
    print(flag)

t = ('tom', '2010-01-02', 1)
test(*t)

 

 

 

 

posted @ 2014-12-09 14:03  道以万计  阅读(248)  评论(0编辑  收藏  举报