惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
InfoQ
S
SegmentFault 最新的问题
T
Tailwind CSS Blog
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
爱范儿
爱范儿
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
量子位
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
The Cloudflare Blog
小众软件
小众软件
云风的 BLOG
云风的 BLOG
WordPress大学
WordPress大学
P
Proofpoint News Feed
Stack Overflow Blog
Stack Overflow Blog
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
B
Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog

Stonecharioteer on Tech

I Traced My Traffic Through a Home Tailscale Exit Node What Was I Reading Last? In Three Not-So-Easy Pieces Dogfooding Is Hard Code blocks in your books, finally GoForGo v0.9.0 Merrilin - We built an app to read books I use a Macbook now Data Structures & Algorithms - Preparing for Interviews Using a local DNS namespace for local service discovery Direction KOllector - Publishing KOReader Highlights gbt: branches touched in the last 24 hours A Soiree into Symbols in Ruby Some Smalltalk about Ruby Loops Ruby Blocks Returning from Ruby Blocks, Procs and Lambdas My Linux Laptop Finally Works: How Claude Helped Me Fix Years of Annoyances TIL: Watchexec - Modern File Watching for Development Workflows A Less Busy Mind GoForGo - Learn Go through live examples Migrating My Old Blog to Hugo with Claude The Qtile Window Manager: A Python-Powered Tiling Experience Read the RFCs that Built the Internet Py-x-Protobuf - Or How I Learned to Stop Worrying and Love Protocol Buffers Python Reverse a List New Beginnings Leaving ChainSafe Systems Screen Lock for Cinnamon Desktop using Zenity and Terminal Commands Crews Not Teams A System for Getting Better at LeetCode
TIL: Python Code Quality and Development Tools Deep Dive
Python Code Quality Authority (PyCQA)# · 2020-07-18 · via Stonecharioteer on Tech

Diving deep into my learning archive, I discovered a treasure trove of Python development tools and resources that every serious Python developer should know about. These discoveries span from code quality enforcement to CPython internals understanding.

The Python Code Quality Authority is an organization that maintains several essential Python code quality tools:

PyCQA Tools Ecosystem

  • pylint - Comprehensive static analysis
  • flake8 - Style guide enforcement
  • mccabe - Cyclomatic complexity analysis
  • prospector - Meta-tool combining multiple analyzers
  • bandit - Security-focused static analysis

McCabe Complexity Analysis

Cyclomatic Complexity, also known as McCabe Complexity, measures the number of linearly independent paths through a program’s source code. The mccabe module helps identify overly complex functions:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Example of measuring complexity
from mccabe import get_code_complexity

def complex_function(x):
    if x > 10:
        if x > 20:
            if x > 30:
                return "very high"
            return "high"
        return "medium"
    return "low"

# This function would have high cyclomatic complexity

Complexity Guidelines

  • 1-10: Simple, low risk
  • 11-20: Moderate complexity
  • 21-50: High complexity, consider refactoring
  • >50: Very high risk, definitely refactor

Advanced Code Quality with Prospector

Prospector is a meta-tool that runs multiple Python code analysis tools and presents the results in a unified format:

1
2
3
# Install and run prospector
pip install prospector
prospector myproject/

It combines:

  • pylint for comprehensive analysis
  • pep8/pycodestyle for style checking
  • pep257/pydocstyle for docstring conventions
  • pyflakes for logical errors
  • mccabe for complexity analysis

CPython Internals Resources

Understanding Python’s internals makes you a better Python developer. Here are the essential resources I discovered:

Core Learning Materials

  1. CPython Internals Book by Anthony Shaw - Comprehensive guide to Python’s implementation
  2. CPython Source Code Guide - RealPython’s detailed walkthrough
  3. Advanced Internals of CPython by Prashanth Raghu - Deep technical PDF resource

Video Resources

Must-Watch CPython Content

Modern Python Development Practices

Import Sorting with isort

isort automatically sorts Python imports according to PEP 8 guidelines:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Before isort
import sys
from myproject import settings
import os
from django.conf import settings as django_settings

# After isort
import os
import sys

from django.conf import settings as django_settings

from myproject import settings

Configuration in pyproject.toml:

1
2
3
4
[tool.isort]
profile = "black"
multi_line_output = 3
line_length = 88

Profiling with Line Profiler

line_profiler provides line-by-line timing information:

1
2
3
4
5
6
7
8
@profile
def slow_function():
    # Your code here
    time.sleep(0.1)
    result = sum(range(1000000))
    return result

# Run with: kernprof -l -v script.py

Memory Profiling with Guppy/Heapy

Guppy3/Heapy helps identify memory leaks and understand memory usage:

1
2
3
4
5
from guppy import hpy
h = hpy()
print(h.heap())

# Shows detailed memory usage by object type

File Watching and Automation

The entr Command

entr runs commands when files change - perfect for development workflows:

1
2
3
4
5
6
7
8
# Run tests when Python files change
find . -name "*.py" | entr python -m pytest

# Restart server on code changes
find . -name "*.py" | entr -r python app.py

# Run linting on save
find . -name "*.py" | entr pylint

Development Workflow

entr is incredibly useful for continuous testing, linting, or building during development. It’s more reliable than many IDE file watchers and works across all platforms.

Advanced Python Features and PEPs

PEP 618: Optional Length-Checking to zip

PEP 618 introduced strict parameter to zip():

1
2
3
4
5
6
7
# Before PEP 618 - silent truncation
list1 = [1, 2, 3]
list2 = [4, 5]
result = list(zip(list1, list2))  # [(1, 4), (2, 5)]

# After PEP 618 - explicit error
result = list(zip(list1, list2, strict=True))  # ValueError!

PEP 622: Structural Pattern Matching

PEP 622 brought pattern matching to Python 3.10+:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def handle_data(data):
    match data:
        case {"type": "user", "name": str(name)}:
            return f"User: {name}"
        case {"type": "product", "id": int(product_id)}:
            return f"Product ID: {product_id}"
        case list() if len(data) > 10:
            return "Large list"
        case _:
            return "Unknown data"

Try it in the Pattern Matching Playground.

Flask Extensions for Code Quality

1
2
# Flask-specific linting
pip install pylint-flask pylint-flask-sqlalchemy

These plugins understand Flask patterns and reduce false positives:

1
2
3
4
5
6
7
8
# Without pylint-flask: "Instance of 'Flask' has no 'route' member"
# With pylint-flask: Correctly understands Flask patterns
from flask import Flask
app = Flask(__name__)

@app.route('/')  # No longer flagged as error
def home():
    return "Hello World"

Profiling Flask Applications

Use Werkzeug’s profiler middleware for detailed performance analysis:

1
2
3
4
5
6
from werkzeug.contrib.profiler import ProfilerMiddleware
from flask import Flask

app = Flask(__name__)
app.wsgi_app = ProfilerMiddleware(app.wsgi_app,
                                 restrictions=[30])  # Top 30 calls

Python Packaging Evolution

Modern Python Packaging

The landscape is evolving rapidly:

  • Poetry - Modern dependency management
  • Flit - Simple publishing workflow
  • pyproject.toml - New standard for project metadata

PEP 508: Dependency Specification

PEP 508 defines the format for dependency specifications:

1
2
3
4
5
6
7
8
9
# Basic dependency
requests >= 2.25.0

# Environment markers
dataclasses; python_version < "3.7"
pywin32; sys_platform == "win32"

# Complex conditions
scipy >= 1.0.0; (python_version >= "3.7" and platform_machine != "aarch64")

Quality of Life Improvements

Useful Development Tools

Key Takeaways

  1. Code Quality is Multi-Dimensional: Use multiple tools (pylint, flake8, prospector) for comprehensive analysis
  2. Understanding Internals Matters: CPython knowledge helps write better, more efficient code
  3. Automation is Essential: Tools like entr and proper CI/CD make development smoother
  4. Modern Python is Evolving: Stay updated with new PEPs and packaging standards
  5. Profiling Before Optimizing: Use proper tools to identify actual bottlenecks

Development Philosophy

The best developers don’t just write code that works - they write code that’s maintainable, efficient, and follows established patterns. These tools help achieve that goal systematically.

This exploration of Python development tools reinforces that professional Python development requires a comprehensive toolkit beyond just knowing the language syntax.


These discoveries came from my learning archive spanning 2020, showing how Python tooling and best practices continue to evolve while maintaining backward compatibility and developer productivity.