西维蜀黍

【Python】Collection - dict

dict

一个非常有用的 Python 內置数据类型是 字典 (参见 映射类型 — dict)。字典在其他语言里可能会被叫做 联合内存联合数组。与以连续整数为索引的序列不同,字典是以 关键字 为索引的,关键字可以是任意不可变类型,通常是字符串或数字。如果一个元组只包含字符串、数字或元组,那么这个元组也可以用作关键字。但如果元组直接或间接地包含了可变对象,那么它就不能用作关键字。列表不能用作关键字,因为列表可以通过索引、切片或 append()extend() 之类的方法来改变。

理解字典的最好方式,就是将它看做是一个 键: 值 对的集合,键必须是唯一的(在一个字典中)。一对花括号可以创建一个空字典:{} 。另一种初始化字典的方式是在一对花括号里放置一些以逗号分隔的键值对,而这也是字典输出的方式。

字典主要的操作是使用关键字存储和解析值。也可以用 del 来删除一个键值对。如果你使用了一个已经存在的关键字来存储值,那么之前与这个关键字关联的值就会被遗忘。用一个不存在的键来取值则会报错。

对一个字典执行 list(d) 将返回包含该字典中所有键的列表,按插入次序排列 (如需其他排序,则要使用 sorted(d))。要检查字典中是否存在一个特定键,可使用 in 关键字。

以下是使用字典的一些简单示例

>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'jack': 4098, 'sape': 4139, 'guido': 4127}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'jack': 4098, 'guido': 4127, 'irv': 4127}
>>> list(tel)
['jack', 'guido', 'irv']
>>> sorted(tel)
['guido', 'irv', 'jack']
>>> 'guido' in tel
True
>>> 'jack' not in tel
False
  ...


【Python】Collection - list

List

In Python, square brackets ([]) indicate a list, and individual elements in the list are separated by commas.

The “empty list” is just an empty pair of brackets [ ]. The ‘+’ works to append two lists, so [1, 2] + [3, 4] yields [1, 2, 3, 4] (this is just like + with strings).

Here’s a simple example of a list that contains a few kinds of bicycles:

bicycles = ['trek', 'cannondale', 'redline', 'specialized'] 
print(bicycles)

list 的常用方法

  • list.remove(x) - 移除列表中第一个值为 x 的元素。如果没有这样的元素,则抛出 ValueError 异常。

  • list.pop([i]) - 删除列表中给定位置的元素并返回它。如果没有给定位置,a.pop() 将会删除并返回列表中的最后一个元素。( 方法签名中 i 两边的方括号表示这个参数是可选的,而不是要你输入方括号。你会在 Python 参考库中经常看到这种表示方法)。

  • list.clear() - 删除列表中所有的元素。相当于 del a[:]

  • list.index(x[, start[, end]]) - 返回列表中第一个值为 x 的元素的从零开始的索引。如果没有这样的元素将会抛出 ValueError 异常。可选参数 startend 是切片符号,用于将搜索限制为列表的特定子序列。返回的索引是相对于整个序列的开始计算的,而不是 start 参数。

  • list.count(x) - 返回元素 x 在列表中出现的次数。

  • list.sort(key=None, reverse=False) - 对列表中的元素进行排序(参数可用于自定义排序,解释请参见 sorted())。

  • list.reverse() - 反转列表中的元素。

  • list.copy() - 返回列表的一个浅拷贝。相当于 a[:]

  ...


【Python】Python Style Guide

Python Style Guide

  ...


【JavaEE】Java Servlet和 JSP(JavaServer Pages)

狭义的Servlet是指Java语言中的一个接口,广义的Servlet是指任何实现了这个Servlet接口的类,一般情况下,人们将Servlet理解为后者。

  ...


【Database】索引 - 聚集索引(Clustered Index)与非聚集索引(Non-clustered Index)

聚集索引(Clustered Index)非聚集索引(Non-clustered Index)

聚集索引(Clustered Index) - storing all row data within the index)

Clustering alters the data block into a certain distinct order to match the index, resulting in the row data being stored in order. Therefore, only one clustered index can be created on a given database table. Clustered indices can greatly increase overall speed of retrieval, but usually only where the data is accessed sequentially in the same or reverse order of the clustered index, or when a range of items is selected.

Since the physical records are in this sort order on disk, the next row item in the sequence is immediately before or after the last one, and so fewer data block reads are required. The primary feature of a clustered index is therefore the ordering of the physical data rows in accordance with the index blocks that point to them. Some databases separate the data and index blocks into separate files, others put two completely different data blocks within the same physical file(s).

非聚集索引(Non-clustered Index)- storing only references to the data within the index

The data is present in arbitrary order, but the logical ordering is specified by the index. The data rows may be spread throughout the table regardless of the value of the indexed column or expression. The non-clustered index tree contains the index keys in sorted order, with the leaf level of the index containing the pointer to the record (page and the row number in the data page in page-organized engines; row offset in file-organized engines).

In a non-clustered index,

  • The physical order of the rows is not the same as the index order.
  • The indexed columns are typically non-primary key columns used in JOIN, WHERE, and ORDER BY clauses.

There can be more than one non-clustered index on a database table.

索引数据的存储方式 - 聚集索引(聚簇索引,Clustered Index)

聚集索引(clustered index),也称为聚簇索引

聚簇索引并不是一种单独的索引类型,而是一种索引数据的存储方式

当表有聚簇索引时,它的行数据实际上存储在索引的叶子页(leaf page)中。因为无法同时把数据行存放在两个不同的地方,所以一张表只能有一个聚簇索引

  ...


【Django】通过 ORM 访问 MySQL,插入 Emoji 报错

Problem

>>> paras = {u'name': u'\U0001f600'}
>>> from core import models
>>> models.Channel.objects.create(**paras)
Traceback (most recent call last):
    ...
  File "/Library/Python/2.7/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
InternalError: (1366, u"Incorrect string value: '\\xF0\\x9F\\x98\\x80' for column 'name' at row 1")

通过 ORM插入一条数据,在其中的一个类型为varchar的字段中插入一个 Emoji 表情,我插的是😀(对应的字符串为u'\U0001f600'),插入后:提示以下错误:

{InternalError}(1366, u"Incorrect string value: '\\xF0\\x9F\\x98\\x80' for column 'name' at row 1")
  ...


【MySQL】MySQL 的存储引擎(Storage Engines)- MyISAM 与 InnoDB

MySQL 的存储引擎(Storage Engines)

MySQL 有两种存储引擎(Storage Engines):

  • InnoDB’s
  • MyISAM

InnoDB is a general-purpose storage engine that balances high reliability and high performance. In MySQL 8.0, InnoDB is the default MySQL storage engine. Unless you have configured a different default storage engine, issuing a CREATE TABLE statement without an ENGINE clause creates an InnoDB table.

MyISAM

MyISAM不支持聚簇索引,因此,MyISAM中的主键索引(Primary Index) 和辅助索引(Secondary Index),都是非聚集索引(Non-clustered Index)

在MyISAM的B+Tree的叶子节点上的data域中,并不是保存数据本身,而是保存数据存放在磁盘中的地址。

  ...


【Django】Django 项目部署到 uWSGI + Nginx 作为生产环境

Workflow

当客户端发出一个 HTTP 请求后,是如何转到我们的应用程序处理并返回的呢?

我根据其架构组成的不同将这个过程的实现分为两种情况:

两级结构

在两级结构中:

  • uWSGI可以作为WSGI Server(WSGI 服务器),它基于了 HTTP 协议、 WSGI(Web Server Gateway Interface) 协议和 usgi 协议;
  • Flask应用可以作为 WSGI Application,基于 WSGI 协议实现。

当有客户端发来请求,uWSGI接受请求,调用 WSGI Application (或者说Flask应用)。当WSGI Application处理完成后,将结果返回给uWSGI,uWSGI构造相应的 HTTP Response,最终将结果返回给客户端。

通常来说,Flask等Web框架会自己附带一个 WSGI 服务器(这就是 Flask 应用可以直接启动的原因),但是这只是在开发阶段被用到而言。在生产环境中,仍然需要将 Flask 部署到一个高性能的 WSGI Server 中,比如 uWSGI,alternative 还有 gunicorn (Green Unicorn), wsgiref。

三级结构

在三级结构中:

  • uWSGI Server 作为中间件,它基于WSGI 协议与Nginx通信,同时与实现了 WSGI 协议的 WSGI Application(比如一个 Flask Application)进行通信

当有客户端发来请求,Nginx(Web Server)先做处理静态资源(静态资源是Nginx的强项),无法处理的动态请求以 WSGI 协议的格式转发给 WSGI Application(比如一个 Flask Application)。

这里,我们多了一层反向代理有什么好处?

  • 提高Web server性能(uWSGI处理静态资源不如nginx)nginx会在收到一个完整的http请求后再转发给wWSGI)
  • Nginx可以做负载均衡(客户端是和Nginx交互而不是uWSGI),以提升高可用
  ...


【HTTP】Web Server(Web 服务器)、Web Application Server(Web 应用服务器)和 CGI(Common Gateway Interface)的故事

Web Server(Web 服务器)

在网络刚刚盛行的年代(Web 1.0),绝大部分的 Web 资源都是静态的。这意味着,你访问 www.example.com/a.html,你就可以访问 a 页面,而如果访问 www.example.com/b.html,你就可以访问 b页面。显然,信息量是单向的,即网站上有什么,你就只能看什么,而不能修改网站的内容。我们通常把它称为静态网站

狭义的Web Server(Web 服务器)概念中,Web Server只能处理静态页面。

典型的Web Server的例子,就是 Apache HTTP Server,Nginx 还有 LightHttpd。

随着互联网的发展,我们更希望访问一个动态网站(Web 1.5),即可以根据用户传入的信息,动态地生成网页的内容。

这时候,我们就需要一个程序来帮忙处理动态请求(如返回特定用户的信息、获取当前时间等),而静态资源的请求,依然由Web Server来负责。

Web服务器程序会将动态请求转发给帮助程序(helper application),帮助程序处理后,返回处理后的静态结果给Web服务器程序。这样,就避免了 Web 服务器程序需要处理动态页面的问题,如下图:

  ...


【Python】WSGI(Web Server Gateway Interface)、uWSGI Server、uwsgi、WSGI Application 的故事

关于Web Server(Web 服务器)、Web Application Server(Web 应用服务器)和 CGI(Common Gateway Interface)的区别,可以访问【HTTP】Web Server(Web 服务器)、Web Application Server(Web 应用服务器)和 CGI(Common Gateway Interface)的故事

WSGI(Web Server Gateway Interface)

WSGI是 Web Server Gateway Interface 的缩写。

它不是服务器、Python模块、框架、API或者任何软件,而是一种描述 Web服务器(如Nginx)如何与Web application Server(Web应用程序,如用Django、Flask框架编写的程序)通信的规范。

因此,WSGI 是一种规范,或者说一种接口,或者说一种协议,equivalent 是 Java 世界中的 servlet。

WSGI是在 PEP 333 提出的,并在 PEP 3333 进行补充(主要是为了支持 Python3.x)。

这个协议旨在解决众多 Python Web 框架和Web server软件的兼容问题。有了WSGI,你再也不用因为使用特定的Web 应用框架而不得不选择特定的 Web server软件。

只要遵照了 WSGI 协议的的 Web 应用(Web application,我们称之为WSGI应用),都可以在任何遵照了WSGI协议的 Web server 软件(我们称之为 WSGI server,比如 uWSGI)上运行。

而对于WSGI 应用(WSGI Application),我们理论上可以完全自己从头编写一个 WSGI 应用,但是,我们通常会使用WSGI 应用框架(WSGI Application Framework),或者称为WSGI MiddleWare,比如Django,Flask等,因为框架已经帮我们封装了大量的底层逻辑,而让我们可以更好的专注于业务逻辑上。

为了更好的理解 WSGI 应用(WSGI Application),我们接下来会写一个非常原始的 WSGI 应用。

  ...