# python语法

# 1. 函数注释

def fetch_bigtable_rows(big_table, keys, other_silly_variable=None):
    """Fetches rows from a Bigtable.

    Retrieves rows pertaining to the given keys from the Table instance
    represented by big_table.  Silly things may happen if
    other_silly_variable is not None.

    Args:
        big_table: An open Bigtable Table instance.
        keys: A sequence of strings representing the key of each table row
            to fetch.
        other_silly_variable: Another optional variable, that has a much
            longer name than the other args, and which does nothing.

    Returns:
        A dict mapping keys to the corresponding table row data
        fetched. Each row is represented as a tuple of strings. For
        example:

        {'Serak': ('Rigel VII', 'Preparer'),
         'Zim': ('Irk', 'Invader'),
         'Lrrr': ('Omicron Persei 8', 'Emperor')}

        If a key from the keys argument is missing from the dictionary,
        then that row was not found in the table.

    Raises:
        IOError: An error occurred accessing the bigtable.Table object.
    """
    pass

参考链接:https://www.jianshu.com/p/4facd9ff2fcd

# 2. getattr() 函数用于返回一个对象属性值。

getattr(object, name[, default])
>>>class A(object):
...     bar = 1
... 
>>> a = A()
>>> getattr(a, 'bar')        # 获取属性 bar 值
1
>>> getattr(a, 'bar2')       # 属性 bar2 不存在,触发异常
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute 'bar2'
>>> getattr(a, 'bar2', 3)    # 属性 bar2 不存在,但设置了默认值
3
>>>

# 3. 获取当前路径

# 要点

利用os获取当前路径:

curdir = os.path.abspath(os.path.dirname(__file__)) + '/'

# 明细

在使用python的时候总会遇到路径切换的使用情况,如想从文件夹test下的test.py调用data文件夹下的data.txt文件(参考python获取当前目录路径和上一级路径 (opens new window)):

└── folder
    ├── data
    │   └── data.txt
    └── test
        └── test.py

一种方法可以在data文件下加入__init__.py 然后在test.py 中import data 就可以调用data.txt文件;

另一种方法可以借助python os模块的方法对目录结构进行操作,下面就说一下这种方式的使用:

import os

print '***获取当前目录***'
print os.getcwd()
print os.path.abspath(os.path.dirname(__file__))

print '***获取上级目录***'
print os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
print os.path.abspath(os.path.dirname(os.getcwd()))
print os.path.abspath(os.path.join(os.getcwd(), ".."))

print '***获取上上级目录***'
print os.path.abspath(os.path.join(os.getcwd(), "../.."))

输出结果为:

***获取当前目录***
/workspace/demo/folder/test
/workspace/demo/folder/test

***获取上级目录***
/workspace/demo/folder
/workspace/demo/folder
/workspace/demo/folder

***获取上上级目录***
/workspace/demo

# 4. pip工具安装

1、安装setup-tools

下载地址:https://pypi.python.org/pypi/setuptools

下载安装包,可以使用wget命令下载。下载及安装命令如下,

wget https://pypi.python.org/packages/45/29/8814bf414e7cd1031e1a3c8a4169218376e284ea2553cc0822a6ea1c2d78/setuptools-36.6.0.zip#md5=74663b15117d9a2cc5295d76011e6fd1
unzip setuptools-36.6.0.zip 
cd setuptools-36.6.0
python setup.py install

2、Python2安装pip

下载地址:https://pypi.python.org/pypi/pip

可以使用wget命令下载。下载及安装命令如下,

wget https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz#md5=35f01da33009719497f01a4ba69d63c9
tar -zxvf pip-9.0.1.tar.gz
cd pip-9.0.1
python setup.py install

3、创建pip软链接

如果上面执行完成,可以正常使用pip命令,也可以不执行下面命令创建软链接。

进入到/usr/local/python27/bin目录,如果上面安装没报错的话,就可以看到easy_installpip命令,创建命令如下,

rm -rf /usr/bin/easy_install* /usr/bin/pip
ln -s /usr/local/python27/bin/pip2.7 /usr/bin/pip
ln -s /usr/local/python27/bin/pip2.7 /usr/bin/pip27
ln -s /usr/local/python27/bin/pip2.7 /usr/bin/pip2.7
ln -s /usr/local/python27/bin/easy_install /usr/bin/easy_install
ln -s /usr/local/python27/bin/easy_install /usr/bin/easy_install27
ln -s /usr/local/python27/bin/easy_install /usr/bin/easy_install2.7
# 验证操作是否成功
pip --version
pip 9.0.1 from /usr/local/python27/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg (python 2.7)
easy_install --version
setuptools 36.5.0 from /usr/local/python27/lib/python2.7/site-packages/setuptools-36.5.0-py2.7.egg (Python 2.7)

参考:https://www.cjavapy.com/article/826/

# 5. Python多线程程序优化

参考链接:https://blog.csdn.net/cedricporter/article/details/6819324

  1. GIL学习与了解;
  2. Python扩展;
  3. ctypes。

虽然CPython的线程库封装了操作系统的原生线程,但却因为 GIL 的存在导致多线程不能利用多个 CPU 内核的计算能力。好在,现在 Python 有了多进程(multiprocessing), 扩展程序(C 语言扩展机制)和ctypes,这样便可以充分利用多核资源。