#111 Cygwin 下的时区问题

2023-02-24

Cygwin 下执行一个 Python 脚本,其中 datetime.now() 获取到的时间居然是 UTF 时间。
执行 date 命令也是如此。

执行 tzselect,三次分别选 Asia,China,Beijing Time,然后就好了。

命令中有提示:如果要永久有效,需要在 ~/.profile 中加入 TZ='Asia/Shanghai'

关键是,TZ 其实有配置:

export | grep TZ
declare -x TZ="Asia/Shanghai"

#110 Python 2.7 Json 字符串类型的问题

2022-12-19

今日发现一个 Python 2.7 的一个大坑:部分版本下 JSON 解析字符串为 str 类型,部分版本解析成 unicode 类型。

Python 2.7 (r27:82500, Apr 12 2016, 21:09:36)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import json; print(repr(json.loads('"a"')));
'a'

Python 2.7.15 (default, Jul 22 2019, 17:38:55)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import json; json.loads('"s"'); exit();
's'

Python 2.7.15 (default, Jun 26 2018, 11:17:18)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import json; json.loads('"s"'); exit();
u's'

Python 2.7.18 (default, Mar 23 2022, 15:07:54)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import json; json.loads('"s"'); exit();
u's'

找到一个相关的 Issue:json.loads() on str should return unicode, not str
对应 GitHub 上的 Issue#54247

根据相关内容,这个问题影响了 Python 2.7 的一些版本。


还有:json.loads() returns str instead of unicode for empty strings
对应 GitHub 上的 Issue#56191

#109 Pytest setup 和 teardown 方法

2022-07-12
  • 全局
  • setup_module(module) / teardown_module(module) 引入包的时候执行
  • setup_function(function) / teardown_function(function)
  • 模块级别
  • setup() / teardown() 测试模块载入的时候执行
  • 类级别
  • setup_class(cls) / teardown_class(cls)
  • setup_method(self, method) / teardown_method(self, method)
  • setup(self) / teardown(self) nose 语法,会被上面两个方法覆盖

Supported nose Idioms

  • setup() and teardown() at module/class/method level: any function or method called setup will be called during the setup phase for each test, same for teardown.
  • SkipTest exceptions and markers
  • setup/teardown decorators
  • __test__ attribute on modules/classes/functions
  • general usage of nose utilities

#107 给新人的目标

2022-04-19

一些阶段性目标:

  1. 参照网上查到的博客和文档,先列出大纲以及制定学习计划
    大概以下五个主题的内容:

  2. Python 标准库

  3. Tornado
  4. Redis
  5. RabbitMQ (pika)
  6. MySQL (SQLAlchemy/pymysql)

  7. 如果资源方面允许,可以实现一个内部系统开发,进行编码和设计方面的指导

  8. 阅读项目文档,理解邮件和短信的处理流程,项目结构,以及每个项目的核心逻辑
  9. 给定一个周边项目进行开发维护,小任务开发(Code Review)
  10. 从写文档和单元测试开始,逐渐参与核心项目的开发和维护

其他:Linux 基础,Git,我们的开发流程和规范

标准库

  1. 常用基础库: os, sys, shutil, re, time, datetime, ramdom, json, pickle ...
  2. 日志: logging
  3. HTTP 相关: urllib, http
  4. 邮件相关: email, smtplib, smtpd
  5. 编码和加密相关: md5, sha, base64, hmac, binascii
  6. 单元测试: unitest 非常重要

Tornado

  1. Web 框架先走起来
  2. 基本用法,比如 add_timeout, call_later, add_callback ...
  3. 了解 IOLoop/IOStream
  4. 项目开发过程中的一些实践

Redis

  1. 安装
  2. redis-cli
  3. 数据类型
  4. 命令清单
  5. redis

RabbitMQ

  1. 基本概念 (Exchange / Queue / Binding / Channel / Connection)
  2. 安装和使用
  3. pika
  4. 发布/订阅 (生产, 消费)

MySQL

  1. 数据类型
  2. 连接
  3. 索引
  4. pymysql / mysqlclient
  5. SQLAlchemy

#105 Poetry

2022-01-12

我研究了半天之后感觉无法掌握 poetry,决定放弃,还是用我的 Pipenv。

img

Poetry 是一个 Python 虚拟环境管理器。

#103 Python GIL 的最新动态

2021-10-22

开源中国上看到有人通过一些实验验证来视图说服 Python 核心团队移除 GIL,根据他的数据,移除 GIL 可以大幅提升多线程性能(19.8 倍)。