zoukankan      html  css  js  c++  java
  • Python 开发日志总结一

    开发中常用Debug Trips

    打印技巧

    在开发中调试中经常打印 json 字符串为了在屏幕上看着更美观舒服

    json 字符串对象

    import json
    
    string = '{"name": "张三", "age": "10"}'
    print(json.dumps(string, indent=4))
    
    # out
    {
      "name": "张三",
      "age": "10"
    }
    

    编码规范

    为了更符合pep8 等规范

    • 在venv 环境中, 安装 autopep8, isort 扩展

      在项目创建 .isort.cfg 配置文件

      [settings]
      # https://github.com/danielfrg/word2vec/blob/master/pyproject.toml
      #
      # 最长度 119
      line_length=119
      multi_line_output=3
      # length_sort=1
      # 不存在该配置项
      # length_sort_stdlib=1
      
      # known_future_library=future,pies
      # known_standard_library=std,std2
      # 导包顺序
      # known_first_party=libs,source.controller,source.models,source.views.schema
      
      # sections=FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER
      
      # default_section=THIRDPARTY
      # no_lines_before=LOCALFOLDER
      #
      
      lines_after_imports = 2
      force_grid_wrap = 0
      include_trailing_comma =1
      use_parentheses = 1
      
    (venv) autopep8 --in-place --aggressive --aggressive file.py file2.py
    
    (venv) isort file.py file2.py
    

    这些繁琐操作, 可以增加git hook

    解析 url 内容

    • python2

      from urlparse import urlparse
      
      parsed_url = urlparse("https://stackoverflow.com/questions/10113090/best-way-to-parse-a-url-query-string/39841923")
      
      # out
      ParseResult(scheme='https', netloc='stackoverflow.com', path='/questions/10113090/best-way-to-parse-a-url-query-string/39841923', params='', query='', fragment='')
      
    • python3

      # urllib.parse 模块提供了一系列用于操纵 URLs 地址及其各组成部分的函数,这些函数或者用于拆分或者用于组装。
      
      from urllib.parse import urlparse
      
      
      parsed_url = urlparse("https://stackoverflow.com/questions/10113090/best-way-to-parse-a-url-query-string/39841923")
      
      
      ParseResult(scheme='https', netloc='stackoverflow.com', path='/questions/10113090/best-way-to-parse-a-url-query-string/39841923', params='', query='', fragment='')
      

      将 URL 拆分为各组成部分

      参考:

      RFC 1738 统一资源定位 (URL) 的语法

      RFC 1808 相对 URLs

      RFC 2396 统一资源标识符 (URI) 的一般语法

      RFC 3986 统一资源标识符 (URI) 的语法

  • 相关阅读:
    UVA657 The die is cast(DFS、BFS)
    三分
    【洛谷P6105】iepsmCmq
    【CF613D】Kingdom and its Cities
    【洛谷P4294】游览计划
    【洛谷P3500】TESIntelligence Test
    【洛谷P6189】[NOI Online 入门组] 跑步
    【洛谷P2973】Driving Out the Piggies
    【洛谷P3164】和谐矩阵
    【洛谷P4161】游戏
  • 原文地址:https://www.cnblogs.com/SkyForce/p/14580451.html
Copyright © 2011-2022 走看看