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

推荐订阅源

罗磊的独立博客
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
GbyAI
GbyAI
B
Blog RSS Feed
S
SegmentFault 最新的问题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
小众软件
小众软件
The Last Watchdog
The Last Watchdog
W
WeLiveSecurity
Webroot Blog
Webroot Blog
S
Security @ Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
Microsoft Security Blog
Microsoft Security Blog
月光博客
月光博客
博客园 - 聂微东
F
Fortinet All Blogs
H
Hacker News: Front Page
A
About on SuperTechFans
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
Check Point Blog
V
V2EX
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
N
News | PayPal Newsroom
Cisco Talos Blog
Cisco Talos Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Cloudflare Blog
P
Privacy & Cybersecurity Law Blog
N
Netflix TechBlog - Medium
C
CXSECURITY Database RSS Feed - CXSecurity.com
Recorded Future
Recorded Future
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
大猫的无限游戏
大猫的无限游戏
美团技术团队
Security Latest
Security Latest
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
I
InfoQ
Recent Announcements
Recent Announcements
The GitHub Blog
The GitHub Blog
Google Online Security Blog
Google Online Security Blog
T
The Exploit Database - CXSecurity.com

陈少文的网站

巨变与机遇的未来十年 Kubernetes 平台管理软件压力测试方案 使用镜像部署 Hexo 静态页面 终于等到你 - GitHub 镜像仓库服务(ghcr.io) 一起来学 Go --(6)Interface 一起来学 Go --(5)Goroutine 和 Channel 什么是函数式编程 如何在 Kubernetes 集群集成 Kata 柯里化与偏函数 使用 PyGithub 自动创建 Label 软件产品是团队能力的输出 Helm 2 、Helm 3 比较 IoT 变现 Kubernetes 中的 DNS 服务 国内的 Helm 镜像源 Harbor 使用自签证书支持 Https 访问 DevOps 工具链之 Prow 如何使用 kfctl 安装 Kubeflow VS Code 无法下载 Go 插件的工具包 工程师更应具有服务精神 你不知道的 Docker 使用技巧 使用 Docker 运行 Tensorflow 论中国 什么是左移 如何清空 Git 仓库全部历史记录 一禅小和尚 有风吹过厨房 时间的玫瑰 如何在 CentOS 安装 GPU 驱动 开发 Tips(19) 使用 Velero 备份 Kubernetes 集群 Kubernetes Cheat Sheet 开发 Tips(18) 如何构建一个 Java 工程 开发 Tips(17) KubeSpray 安装 Kubernetes 报错 ip in ansible_all_ipv4_addresses 基于 Kubernetes 和 Jenkins 搭建自动化测试系统 在 Kubernetes 上动态创建 Jenkins Slave 使用 Jenkins 进行服务拨测 开发 Tips(16) Kubernetes 签发 Ingress 证书及日常故障运维 Kubernetes 中 Deployment 的基本操作 Kubernetes 中的证书 如何使用 KubeBuilder 开发一个 Operator Kubernetes 1.6.0 安装问题汇总 镜像管理工具 -- Harbor 开发 Tips(15) Docker 如何拉取镜像 开发 Tips(14) 使用 Helm 安装 harbor 开发 Tips(13) 使用 S2I 构建云原生应用 在 Kubernetes 中使用 emptyDir、hostPath、localVolume 开发 Tips(12) 开发 Tips(11) 代码质量分析工具 SonarQube 使用 Kubeadm 安装 Kubernetes 集群 一起来学 Go --(4)常用函数 Kubernetes 中的 Ceph Kubernetes 之 Volumes Kubernetes 之 Labels、Selectors 开发 Tips(10) 开源正在重构商业模式 Kubernetes 之网络 Kubernetes 之 API 使用 Helm 和 Operator 快速部署 Prometheus Kubernetes 复杂有状态应用管理框架 -- Operator Kubernetes 的包管理器 -- Helm 一起来学 Go --(3)Go Modules 如何一步一步地优化博客方案 kubectl 实用指南 Kubernetes 中的基本概念 搭建远程 Kubernetes 开发环境 大公司和小公司的 ToB 思路 开发 Tips(9) Go 入门指南 一起来学 Go --(2)数据与逻辑结构 如何预防 Web 富文本中的 XSS 攻击 django-xss-cleaner 云工作时代 一起来学 Go --(1)背景与特点 SaaS 开发团队的不同阶段 你不知道的 Git 使用技巧 输出既服务 微服务设计 继续奔跑 开发 Tips(8) 从账户安全到二次验证 Django 性能之数据库查询优化 Django 性能之分库分表 敏捷开发之研发流程 打造一致性的团队 开发 Tips(7) Pytest 进阶学习之 Mock PaaS 部署之 buildpack Go 开发配置 领域输出才是 PaaS 的核心竞争力 Pytest 入门学习 开发 Tips(6) 如何使用 Jenkins、Docker、GitLab 搭建 Django 自动化部署流程
开发 Tips(3)
微信公众号 · 2018-11-02 · via 陈少文的网站

Please enable Javascript to view the contents

主要记录最近遇到的一些开发问题,解决方法。

1. Python 的日志模块

Python 的 logging 模块主要由四个部分组成:

  1. Loggers: 可供程序直接调用的接口
  2. Handlers: 将日志记录输出至合适的位置
  3. Filters: 提供更细粒度的日志是否输出判断
  4. Formatters: 定制最终记录打印的布局格式

看下面这个例子,log1.py 文件

1
2
3
4
5
6
7
8
import logging

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

logger.info('info log')
logger.debug('debug log')
logger.warning('Warning log')

直接执行,输出:

1
2
2018-10-26 21:08:02,905 - __main__ - INFO - info log
2018-10-26 21:08:02,907 - __main__ - WARNING - Warning log

log2 文件

1
from log1 import logger

直接执行,输出:

1
2
2018-10-26 21:11:36,849 - log1 - INFO - info log
2018-10-26 21:11:36,849 - log1 - WARNING - Warning log

format 部分参数:

  • %(levelno)s:打印日志级别的数值
  • %(levelname)s:打印日志级别的名称
  • %(pathname)s:打印当前执行程序的路径,其实就是 sys.argv[0]
  • %(filename)s:打印当前执行程序名
  • %(funcName)s:打印日志的当前函数
  • %(lineno)d:打印日志的当前行号
  • %(asctime)s:打印日志的时间
  • %(thread)d:打印线程 ID
  • %(threadName)s:打印线程名称
  • %(process)d:打印进程 ID
  • %(processName)s:打印线程名称
  • %(module)s:打印模块名称
  • %(message)s:打印日志信息

2. Python 的调试工具

  • pdb

The Python Debugger 是官方调试器,内置在 Python 标准模块中。

使用方式:$python -m pdb scriptfile 或者在代码中 pdb.set_trace()

  • ipdb

基于 ipython 的 pdb,是一个增强版的 pdb。

使用方式:$ipdb scriptfile$python –pdb scriptfile

  • PuDB

全屏的基于控制台的可视化调试器。

使用方式:$python -m pudb.run scriptfile$pudb scriptfile

3. Babel 转码器

ES6 提供了许多新特性,但并不是所有的浏览器都能够完美支持,ES5 支持得好很多。Babel 是一个 ES6 转码器,可以将 ES6 代码转为 ES5 代码。这意味着,你可以用 ES6 的语法编写程序,又不用担心现有环境是否支持。

1
2
3
4
5
6
7
// 转码前
input.map((item) => item + 1);

// 转码后
input.map(function (item) {
  return item + 1;
});

Babel 的配置文件是 .babelrc ,存放在项目的根目录下。使用该文件设置转码规则和插件,基本格式如下:

1
2
3
4
{
  "presets": [],
  "plugins": []
}

4. Badge 生成器

开源项目的文档中,通常会添加各种 Badge 。这些 Badge 有的是写死的,有的是第三方工具动态获取的。推荐 http://shields.io/ 可以非常方便的生成各种 Badge 。

5. 代码统计工具 - Cloc

Cloc 是一款使用 Perl 语言开发的开源代码统计工具,支持多平台、多语言,能够计算指定目标文件或文件夹中的文件数(files)、空白行数(blank)、注释行数(comment)和代码行数(code)。

Windows 下,可以先下载安装 msys2,使用 $pacman -S cloc 安装 Cloc,然后统计:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
cloc .
--------------------------------------------------------------------------------
Language                      files          blank        comment           code
--------------------------------------------------------------------------------
Python                          523          10217          13308          61964
JSON                             24             22              0          33329
HTML                            267           2994            752          29736
XML                               3              0              0          21113
...
--------------------------------------------------------------------------------
SUM:                           2157         142804         147553         912384
--------------------------------------------------------------------------------

6. Django Model 的更新信号处理

通过 created 字段,可以区分 Django Model 的新建和更新操作。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from django.db.models.signals import post_save


@receiver(post_save, sender=User)
def handle_when_user_updated(sender, instance, created, **kwargs):
    if not created:
        # User object updated
        pass
    else:
        # User object created
        pass

微信公众号