Anaconda

  • 使用anaconda 安装python包的路径为

    ~/anaconda2/lib/python2.7/site-packages/

    使用 sudo pip命令安装包的目录为:

    /usr/local/lib/python2.7/dist-packages/

  • Official website: http://anaconda.org/

  • install packages steps

    anaconda search -t conda imagemagic
    anaconda show kalefranz/imagemagick # 与在网页端搜索 imagemagick 一样
    
  • Using conda: http://conda.pydata.org/docs/using/using.html

  • 安装anaconda后需要source ~/.bashrc

  • 基类

    class A(object):
      def __init__(self):
        pass
    
      def get_output_shape_for(self):
        """
        write something
        """
        raise NotImplementedError
    
  • slice 切片

    >>> slice
    <type 'slice'>
    >>> slice(None)
    slice(None, None, None) # start, stop, step
    
    >>> ls = range(10)
    >>> print ls[slice(0, 10, 2)]
    [0, 2, 4, 6, 8]
    >>>print ls[slice(None)]
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    
  • zip(*)

    >>> ls = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
    >>> for s in zip(ls):
          print s
    ((1, 2, 3), )
    ((4, 5, 6), )
    ((7, 8, 9), )
    >>> for s in zip(*ls):
          print s
    (1, 4, 7)
    (2, 5, 8)
    (3, 6, 9)
    
  • generator

    >>> print ((s for s in range(3)))
    <type 'generator'>
    
  • 装饰器

    class A(object): ##必须有object
      def __init__(self):
          self.name = 'john'
      @property
      def name(self):
          print 'property'
          return self._name
    
      @name.setter
      def name(self, name):
          print 'setter'
          self._name = name
    
    >>> a = A()
    setter
    property
    
  • python 处理文件

    os.path 模块博客

    python built-in functions

    os.path offical

    shutil.copy("oldfile","newfile")  #python 移动文件,oldfile只能是文件夹,newfile可以是文件,也可以是目标目录
    os.path.join(name1, name2)
    os.path.isdir(dir_name)
    os.listdir(directory)
    file_name.endswith(suffix, start, end)
    os.path.dirname(path) #返回文件路径
    
  • python 简单的数据操作

    s = '123+'
    s[:s.find('+')] == '123'
    dic = {}
    dic.setdefault('a', []) # dic.get('a', []) and also set dic['a']=[] if 'a' not in dic
    print __file__
    
    from numpy.matlib import repmat
    repmat(x, m,n) #类似于matlab中的repmat函数
    
  • iterator vs generator

    blog-using iterators and generators

    next(generator_name)

    The transformation of images is not under thread lock so it can be done in parallel* 受线程保护的程序不能够并行,

  • python 互相导入

    test1.py, test2.py 两个文件可以互相导入

  • line_profiler

    line_profiler is a module for doing line-by-line profiling of functions. kernprof is a convenient script for running either line_profiler or the Python standard library’s cProfile or profile modules, depending on what is available.

  • 记忆体、递归

    @functools.lru_cache(maxsize=None) wiki

  • python class

    class A():
      '''
      class A description
      '''
      def __init__(self):
              pass
      x = 10
    
    >>> A.__dict__
    {'x': 10, '__module__': '__main__', '__doc__': '\n\tclass A description\n\t', '__init__': <function __init__ at 0x7f5335e46488>}
    >>> A.__doc__
    '\n\tclass A description\n\t'
    
  • blog-super-considered-super by Deep Thoughts by Raymond Hettinger

    super() is in the business of delegating method calls to some class in the instance’s ancestor tree

    Things to Know About Python Super

    What does super do in Python?-stackoverflow

  • 下面两段代码在 blocks_dict 很大时的速率相差很大,后者明显优于前者

    blocks_list_1 = []
    for ref in refcode_list:
      if ref in blocks_dict.keys(): # blocks_dict a dictionary
          tmp = blokc_dict[ref]
          blocks_list_1.append(tmp)
      else:
          continue
    
    blocks_list_1 = []
    for ref in refcode_list:
      try:
          tmp = blokc_dict[ref]
          blocks_list_1.append(tmp)
      except Exception:
          continue
    
  • 切换python运行版本

    ls /usr/bin/python* python-versions

    vim ~/.bashrc

    # add this line in the file bottom
    alias python='/usr/bin/python2.7'
    . ~/.bashrc
    
  • python 优雅的操作字典

    https://www.linuxzen.com/python-you-ya-de-cao-zuo-zi-dian.html

  • Numpy 读.txt文件,生成矩阵

    http://hyry.dip.jp/tech/book/page/scipy/numpy_file.html

  • 美团点评技术团队

    http://tech.meituan.com/ http://www.w3ctech.com/