一个计算机技术爱好者与学习者

0%

输出日志到控制台

1
2
3
4
5
6
7
8
import sys
import logging

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
log.addHandler(logging.StreamHandler(sys.stdout)) #默认sys.error

log.info('print info level log to console')
阅读全文 »

前言

python操作mysql数据库进行增删查改,是经常遇到的需求,下面整理一下具体操作方法。

数据库准备

1、安装配置好mysql
参考《使用Docker安装配置Mysql》

2、创建一个测试用的数据库

1
2
3
4
5
6
7
8
9
10
create database vkphp default character set utf8 collate utf8_general_ci;

use vkphp;

CREATE TABLE IF NOT EXISTS `user` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
`password` varchar(32) NOT NULL DEFAULT '',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

更多命令参考 《MySQL常用命令》

阅读全文 »

pip简介

Pip is a package-management system written in Python and is used to install and manage software packages. The Python Software Foundation recommends using pip for installing Python applications and its dependencies during deployment. Pip connects to an online repository of public packages, called the Python Package Index. Pip can be configured to connect to other package repositories (local or remote), provided that they comply to Python Enhancement Proposal 503.

Most distributions of Python come with pip preinstalled. Python 2.7.9 and later (on the python2 series), and Python 3.4 and later include pip by default.

相关文档:

阅读全文 »

为什么需要重试?

典型场景:程序的实现需要调用第三方的API,但是我们并不能保证第三方API一直好用,也不能保证网络一直畅通,所以在调用第三方API时需要加上错误重试。

通用场景:程序的运行不符合预期,我们知道再次调用大概率可以使之符合预期,这时就需要重试。

而重试逻辑我们可以通过循环多次调用实现,也可以使用封装好的重试模块。
本文我们就学习一下Python中比较流行的 retrying 模块,实现一些重试Demo。

阅读全文 »

怎样获取一个脚本的路径?

怎样获取执行脚本的绝对路径?怎样获取执行脚本的父绝对路径?怎样获取入口脚本的绝对路径?。。。
带着这些问题,我们进行一个简单的实验,获知这些问题的答案。

阅读全文 »