Python 集合


列表——list

pyton中的列表,有点类似与java中的数组,也是有序的,可以对其新增,删除元素,但是它的灵活性和功能要比数组强大很多;list使用“[”和“]”表示定义一个列表。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = 'Roy.Yin'

'''
python list
'''

#定义一个空的列表
names = []

names = ['roy','lucy','lily','jack']
print names

#末尾添加元素
names.append('sarah')

#index位子插入元素
names.insert(2,'mark')

#删除元素
names.remove('lucy')

#获取某个位子的元素
print names[0]

#index超过最大值会抛异常,list index out of range
#print names[8]

#list可以使用负数的index来表示倒序索引,如-1表示的倒数第1个元素
print names[-1]

#以堆栈的方式弹出index位子的元素,默认index=0
name = names.pop(1)
print name

#返回元素所在的index值
print names.index('jack')

#返回list的元素个数
print len(names)

#返回元素在列表中出现的次数
print names.count('roy')

names2 = ['tom','sala']
#将name2中的元素添加倒names中
names.extend(names2)
print names

Read More

The Zen of Python —— Python 之禅

Beautiful is better than ugly.
优美胜于丑陋(Python 以编写优美的代码为目标).

Explicit is better than implicit.
明了胜于晦涩(优美的代码应当是明了的,命名规范,风格相似).

Simple is better than complex.
简洁胜于复杂(优美的代码应当是简洁的,不要有复杂的内部实现).

Complex is better than complicated.
复杂胜于凌乱(如果复杂不可避免,那代码间也不能有难懂的关系,要保持接口简洁).

Flat is better than nested.
扁平胜于嵌套(优美的代码应当是扁平的,不能有太多的嵌套).

Sparse is better than dense.
间隔胜于紧凑(优美的代码有适当的间隔,不要奢望一行代码解决问题).

Readability counts.
可读性很重要(优美的代码是可读的).

Read More

LinkedHashMap实现原理

特点

  1. Map接口实现,继承于HashMap
  2. 允许null键和null值
  3. 迭代有序(插入顺序或访问顺序)
  4. 非线程安全
  5. 插入快,查询快,删除快

数据结构

LinkedHashMap继承HashMap,也是哈希表和链表的结合体,但是LinkedHashMap不只于此,LinkedHashMap还维护了所有元素的双重链表,有了这个双重链表,可以在一定程度上使得LinkedHashMap元素有序,具体是插入顺序,还是访问顺序,看具体的设置。LinkedHashMap的其他操作方式与HashMap基本一致。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>{
//双向链表的表头元素
private transient Entry<K,V> header;

//设置迭代顺序,ture表示访问顺序,false表示插入顺序
private final boolean accessOrder;

...

//内部Entry实现,继承HashMap的Entry
private static class Entry<K,V> extends HashMap.Entry<K,V> {
//增加了before,after用来保存前一个元素和后一个元素的引用
Entry<K,V> before, after;
...
}
...
}

Read More

Nginx配置与优化

include使用

当我们配置多个虚拟主机在nginx配置文件中的时候,我们会越来越发现,nginx.conf文件越来越大,维护起来也越来越麻烦,这个时候我们就需要使用include,把每个虚拟主机配置到自己的conf中,然后在nginx.conf中include就可以了。

1
2
3
4
5
#目录
nginx.conf
site/
www_abc_com.conf
www_edf_com.conf

1
2
3
4
5
6
#nginx.conf配置
http{
...
include site/www_abc_com.conf;
include site/www_edf_com.conf;
}

当然一些特殊功能模块的配置也可以独立出一个单一的配置文件,这样nginx.conf的结构会很清晰。

Read More

主要模块

location模块

location是Nginx的重要模块,基本语法:location [=|~|~*|^~] /uri/ { … }

  1. “=” 严格匹配。如果这个查询匹配,那么将停止搜索并立即处理此请求
  2. “^~” 表示URI定位必须以指定模式开始,如果匹配,停止搜索其他模式
  3. “~” 和 “~*” 为区分大小写匹配和不区分大小写匹配
  4. “!~”和“!~*” 为区分大小写不匹配和不区分大小不匹配
  5. “@xxx” 表示定义location区段名,客户端不能访问,内部产生的请求可以,例如try_files或error_page
  6. “/” 表示通用匹配,任何请求都会匹配到
    如果多个location匹配按什么顺序呢?Nginx首先会按“=”去匹配,匹配不了然后按照“^~”匹配,如果还是匹配不了,就会按location的顺序匹配其他的规则,最后还是没匹配就使用“/”通用匹配。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#精确匹配,不能用正则
location = /abc {
}

#URI定位匹配,可用正则
location ^~ /abc {
}

#正则匹配 区分大小写
location ~ /abc {
}

#正则匹配 不区分大小写
location ~* /abc {
}

#匹配所有
location / {

}

Read More

安装与配置

介绍

Nginx是由俄罗斯软件工程师IgorSysoev开发的一个高性能的HTTP和反向代理服务器,具备IMAP/POP3和SMTP服务器功能。Nginx最大的特点是对高并发的支持和高效的负载均衡,在高并发的需求场景下,是Apache服务器不错的替代品。目前,包括新浪、腾讯等知名网站已经开始使用Nginx作为Web应用服务器。

安装

1
2
3
4
5
6
$ wget http://nginx.org/download/nginx-1.6.3.tar.gz #下载nginx安装包
$ tar -xvf nginx-1.6.3.tar.gz #解压安装包
$ cd nginx-1.6.3/
$ ./configure --prefix=/usr/local/nginx
$ make
$ make install #安装

Read More

HashSet实现原理

特点

  1. 基于HashMap实现
  2. 无序
  3. 允许null值
  4. 元素不重复
  5. 非线程安全

数据结构和初始化

HashSet底层是基于HashMap实现的,它的所有数据都存放到HashMap中,更准确的说应该的HashMap的key中,所以无法保证有序,但保证元素的唯一性(HashMap的key是唯一的,本质还是通过key对象的equals和hashCode方法实现的),而HashMap的value值则直接赋值PRESENT常量。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//基于HashMap
private transient HashMap<E,Object> map;
//常量PRESENT,用于设值HashMap的value
private static final Object PRESENT = new Object();
//默认构造,初始化了HashMap,HashMap默认的加载因子为0.75
public HashSet() {
map = new HashMap<E,Object>();
}
//指定初始容量
public HashSet(int initialCapacity) {
map = new HashMap<E,Object>(initialCapacity);
}
//指定初始的容量和加载因子
public HashSet(int initialCapacity, float loadFactor) {
map = new HashMap<E,Object>(initialCapacity, loadFactor);
}
//指定初始集合
public HashSet(Collection<? extends E> c) {
map = new HashMap<E,Object>(Math.max((int) (c.size()/.75f) + 1, 16));
addAll(c);
}
//指定初始容量和加载因子,实例下一个LinkedHashMap
//这个构造方法是包访问权限,是为LinkedHashSet提供的
HashSet(int initialCapacity, float loadFactor, boolean dummy) {
map = new LinkedHashMap<E,Object>(initialCapacity, loadFactor);
}

Read More

ArrayList的实现原理

特点

  1. List接口的可变数组实现
  2. 允许null值
  3. 非线程安全
  4. 有序
  5. 查询快,插入慢,删除慢

数据结构

ArrayList的数据结构比较简单,底层使用数组保存所有的元素,所有的操作也是在这个数组上.然后使用一个size保存了当前ArrayList的元素个数

1
2
3
4
5
//包含元素的个数
private int size;
//底层数组
//transient:表示序列化的时候不包含此属性
private transient Object[] elementData;

ArrayList存取实现

初始化

ArrayList有3个构造函数:无参构造,容量设定构造,列表设定构造,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//无参构造,默认容量为10
public ArrayList() {
this(10);
}
//容量设定构造
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
//列表设定构造,将列表值置入数组
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
size = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
// 如果toArray()返回不是Object[],转为Object[]
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}

Read More

HashMap的实现原理

特点

1.基于哈希表的Map实现
2.非同步,也就是非线程安全
3.允许NULL键和NULL值
4.无序
5.插入快,查询快,删除快

数据结构

由于HashMap是基于哈希表,所以拥有哈希表的所有优点和缺点;HashMap实际上是位桶和链表的结合体.

hashmap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//初始的数组容量大小
static final int DEFAULT_INITIAL_CAPACITY = 16;

//负载因子
static final float DEFAULT_LOAD_FACTOR = 0.75f;

//table数组扩容的临界值,是当前数组容量*负载因子
int threshold;

//table数组
transient Entry[] table;

//链表实现
static class Entry<K,V> implements Map.Entry<K,V> {
final K key;
V value;
Entry<K,V> next;
final int hash;

/**
* Creates new entry.
*/

Entry(int h, K k, V v, Entry<K,V> n) {
value = v;
next = n;
key = k;
hash = h;
}
...
}

Read More

刚刚搭建完博客,想着以后要是访问的人多了,博客顶不住怎么办(这个完全是借口,其实就是自己的三分钟热情),就顺便把Blog进行下压测,压测的命令很多(ab、http_load、webbench),那就这样,用最熟悉的那一个:ab,就这么愉快的决定了!

介绍

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ ab
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
-n requests 压测所执行的总请求个数,默认1
-c concurrency 一次产生的请求个数,默认是一次一个,也就是并发请求的个数
-t timelimit 测试所进行的最大秒数,
-s timeout 每次请求的超时时间,
默认30
-p postfile POST方式. Remember also to set -T
-u putfile PUT方式. Remember also to set -T
-T content-type Content-type header to use for POST/PUT data, eg.
'application/x-www-form-urlencoded'
Default is 'text/plain'
-k 保持长连接 Use HTTP KeepAlive feature
...
...

一般来说,使用-c -n 就足够了!

Read More