python常用編寫格式和規範

0.目錄格式

列舉了幾種常用寫法,深度學習各種模型常用3中方法,

#1:
VIS/
|-- bin/
|   |-- vis
|
|-- vis/
|   |-- tests/
|   |   |-- __init__.py
|   |   |-- test_main.py
|   |
|   |-- __init__.py
|   |-- main.py
|
|-- docs/
|   |-- conf.py
|   |-- config.json
|
|-- setup.py
|-- requirements.txt
|-- README

#2.
VIS/
|-- src/
|   |-- util/
|   |   |-- __init__.py
|   |   |-- process.py
|   |   |-- frame2video.py 
|   |
|   |-- __init__.py
|   |-- main.py
|
|-- cfg/
|   |-- config.json
|
|-- setup.py
|-- requirements.txt
|-- README

#3.深度學習常用
├─caffe-fast-rcnn
├─data
│  ├─demo
│  └─scripts
├─experiments
│  ├─cfgs
│  ├─logs
│  └─scripts
├─lib
│  ├─datasets
│  │  ├─tools
│  │  └─VOCdevkit-matlab-wrapper
│  ├─fast_rcnn
│  ├─nms
│  ├─pycocotools
│  ├─roi_data_layer
│  ├─rpn
│  ├─transform
│  └─utils
├─models
│  ├─coco
│  │  ├─VGG16
│  │  │  ├─faster_rcnn_end2end
│  │  │  └─fast_rcnn
│  │  └─VGG_CNN_M_1024
│  │      ├─faster_rcnn_end2end
│  │      └─fast_rcnn
│  └─pascal_voc
│      ├─VGG16
│      │  ├─faster_rcnn_alt_opt
│      │  ├─faster_rcnn_end2end
│      │  └─fast_rcnn
│      ├─VGG_CNN_M_1024
│      │  ├─faster_rcnn_alt_opt
│      │  ├─faster_rcnn_end2end
│      │  └─fast_rcnn
│      └─ZF
│          ├─faster_rcnn_alt_opt
│          ├─faster_rcnn_end2end
│          └─fast_rcnn
└─tools

1.默認python路徑聲明

第一行大部分python文件的頭部都會寫上 #!/usr/bin/python 或者 #!/usr/bin/env

#!/usr/bin/env python

普通運行模式例如(linux) : python .py 第一行語句無效。
可執行模式:./
.py (文件要有可執行權限chmod a+x *.py),第一行語句爲腳本語言指定解釋器。
注:
#!/usr/bin/env python 是找系統 PATH 中指定的第一個 python 來執行你的腳本,比如虛擬環境設爲第一個。
#!/usr/bin/python 是找系統默認 python 環境,但是大家普遍不愛用默認的。

2.編碼

第二行一般設置編碼

# _*_ coding:utf-8 _*_
  • 如果沒有此文件編碼類型的聲明,則 python 默認以ASCII編碼去處理;如果你沒聲明編碼,但是文件中又包含非ASCII編碼的字符的話,python解析器去解析的 python 文件就會報錯。
  • 必須放在python文件的第一行或第二行;
  • 支持的格式有三種:(一般用2)
# coding=<encoding name>
最常見的,帶冒號的(大多數編輯器都可以正確識別的):
#!/usr/bin/python
# -*- coding: <encoding name> -*-
vim的:
#!/usr/bin/python
# vim: set fileencoding=<encoding name> :

3.版權聲明

以下是幾個實例,來自github等

#1
 *Copyright: Copyright (c) 2017
 *Created on 2019-6-6
 *Author: kobe
 *Version 1.0 
 *Title: a simple implement of faster-rcnn

#2.
/* Generated by kobe 0.20.1 on Wed Oct  5 13:15:30 2016 */

#3.
# --------------------------------------------------------
# Fast R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------


#4.
# --------------------------------------------------------
# Pytorch multi-GPU Faster R-CNN
# Licensed under The MIT License [see LICENSE for details]
# Written by Jiasen Lu, Jianwei Yang, based on code from Ross Girshick
# --------------------------------------------------------

4.編碼風格

命名規範: https://www.cnblogs.com/Maker-Liu/p/5528213.html
Google python風格: https://zh-google-styleguide.readthedocs.io/en/latest/google-cpp-styleguide/comments/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章