• 格式化代码

    yapf ,isort 格式化,排序你的代码

    python中有库,yapf (格式化),isort (排序)两个结合,格式化你的代码。

    格式化代码

    requirements.txt
    1. yapf==0.28.0
    2. isort==4.3.21
    MakeFile
    1. .PHONY: install
    2. install:
    3. pip install -r requirements.txt
    4. .PHONY: fmt
    5. fmt:
    6. yapf --exclude services/sls-backend-server/init_sls_cluster/conf.tmpl -p -i -r .
    7. isort --skip services/sls-backend-server/init_sls_cluster/conf.tmpl -rc .
    1. yapf -p -i -r .
    2. isort -rc .
    3. (如何忽略掉 某些 文件 目录.)
    4. yapf --exclude services/conf.tmpl -p -i -r .
    5. isort --skip services/conf.tmpl -rc .
    (如果需要忽略多个文件)
    yamf
    Excluding files from formatting (.yapfignore)
    In addition to exclude patterns provided on commandline, YAPF looks for additional patterns specified in a file named .yapfignore located in the working directory from which YAPF is invoked.
    举例,添加一个 .yapfignore文件,内容如下,他就能忽略这两个文件了。
    1. db_pre_handler/conf.tmpl
    2. db_post_handler/conf.tmpl
    .yapfignore也支持正则, 下面这个和上面等效。
    1. */conf.tmpl

isort
-s SKIP, —skip SKIP Files that sort imports should skip over. If you want
to skip multiple files you should specify twice:
—skip file1 —skip file2.
sort 同样也可以用配置文件
Configuring isort
If you find the default isort settings do not work well for your project, isort provides several ways to adjust the behavior.
To configure isort for a single user create a ~/.isort.cfg or $XDG_CONFIG_HOME/isort.cfg file:

  1. [settings]
  2. line_length=120
  3. force_to_top=file1.py,file2.py
  4. skip=file3.py,file4.py
  5. known_future_library=future,pies
  6. known_standard_library=std,std2
  7. known_third_party=randomthirdparty
  8. known_first_party=mylib1,mylib2
  9. indent=' '
  10. multi_line_output=3
  11. length_sort=1
  12. forced_separate=django.contrib,django.utils
  13. default_section=FIRSTPARTY
  14. no_lines_before=LOCALFOLDER

Additionally, you can specify project level configuration simply by placing a .isort.cfg file at the root of your project. isort will look up to 25 directories up, from the file it is ran against, to find a project specific configuration.
在你的项目中, 需要执行 sort 的当前目录, 粗暴的来一个 .isort.cfg 文件。

  1. [settings]
  2. skip=src/common/tianji/tianji_clt.py,\
  3. src/scale_down_sls_cluster_ag/sls_deploy_common.py,\
  4. src/scale_down_sls_cluster_ag/sls_op_common.py,\
  5. src/scale_down_sls_cluster_ag/sls_worker_scaling_016.py,\
  6. src/scale_down_sls_cluster_ag/sls_worker_shutdown_machine.py,\
  7. src/scale_down_sls_cluster

switchyomega + chrome 插件代理. —— ip 代理



python
argparse 模块, 用来对你的函数 执行的参数 做封装的。

  1. try:
  2. import json
  3. except:
  1. import simplejson as json
  2. unittest自动化测试的 模块。
  3. import unittest
  4. class TestStringMethods(unittest.TestCase):
  5. def test_upper(self):
  6. self.assertEqual('foo'.upper(), 'FOO')
  7. def test_isupper(self):
  8. self.assertTrue('FOO'.isupper())
  9. self.assertFalse('Foo'.isupper())
  10. def test_split(self):
  11. s = 'hello world'
  12. self.assertEqual(s.split(), ['hello', 'world'])
  13. # check that s.split fails when the separator is not a string
  14. with self.assertRaises(TypeError):
  15. s.split(2)
  16. if __name__ == '__main__':
  17. unittest.main()
  18. sys.path.append() 添加路径, 方便导入其它路径的模块。
  19. sys.path.append(FILE_PATH + "/data_sdk_v4")
  1. hashlib.md5()
  2. import hashlib #导入hashlib模块
  3. md = hashlib.md5() #获取一个md5加密算法对象
  4. md.update('how to use md5 in hashlib?'.encode('utf-8')) #制定需要加密的字符串
  5. print(md.hexdigest()) #获取加密后的16进制字符串