Data Structure In Python

Data Structure In Python

Today I met a problem that I would like to count the duplicate items in a list. Generally, I could iterate the list and count it by myself.

But I don’t think that’s a good idea, and python perhaps have the easiest way to do this.

So I searched for the solution and find it. there is a build-in function to do this.

1
2
3
4
5
6
7
8
9
>>> list_test = ['test','test','xiche','xiche','xiche',1,1,1,1,1,['a']]
>>> print(list_test.count('test'))
2
>>> print(list_test.count('xiche'))
3
>>> print(list_test.count(1))
5
>>> print(list_test.count(['a']))
1

So it’s important to be familar with the usage of data structure in python and it will help you to code more efficient.

And let me summarize some basic operations about them and I will continue to update this article when I find some new interesting tips.

Learn to look into offical document is very import.

Read more

Spider for Douban (01)

Top n Movies of Spider for Douban

网上有许多爬虫的入门脚本,都是去爬豆瓣前 250 的电影信息,我也参考并写了一个,做为入门。

因为豆瓣的网页结构相对稳定,变化不多,不然要花太多时候去维护。

在代码中可以修改 page 的值来取得更多的页数,我发现其实是可以超过 250 个的,排名 250 后的资源,也可以通过手动修改得到。

Read more

Send email by python

Send email by python

python有两个原生库(smtplib,email)可以用来发送邮件,只要你有smtp server就行。之前在公司已经写过简单的类来实现,回来又重新写了一个使用163邮箱来发送的工具类。

目前已经能够完成基本发送功能,包括html格式,添加附件等,只是弄了好久,还是没有办法在正文中添加图片,一直554错误,被网易认为垃圾信息(错误代码一览)而发送不了,之后有时间再看。

Read more

Process Info

Process Info

之前的测试中,使用psutil可以得到系统的CPU/Memory的使用百分比,但是没试过读取个别进程的信息。现在想要实现这样的功能,方便更好的分析java.exe的内存使用变化。

有一种思路是使用系统本身提供的查看进程的shell cmdsubprocess.check_output组合得到信息。

Read more

Catch CPU/Memory Monitor

根据log中的实际取得monitor中的数据。

现在有一个完整的导数据的log,类似于如下ImportLog.txt:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
...略...
2018-06-14 00:07:58,156 [INFO ] -----START IMPORT-----
2018-06-14 00:07:58,515 [INFO ] xxx
2018-06-14 00:10:39,065 [INFO ] xxx
2018-06-14 00:10:39,081 [INFO ] Import Time(s):160.55
2018-06-14 00:10:39,128 [INFO ] xxx
2018-06-14 00:10:39,128 [INFO ] Finished:xxxAAxxx
2018-06-14 00:10:39,128 [INFO ] Import Time(s):160.96
2018-06-14 00:10:39,128 [INFO ] -----END IMPORT-----
2018-06-14 00:10:44,201 [INFO ] -----START IMPORT-----
2018-06-14 00:10:46,590 [INFO ] xxx\\IMPORT.exe BBB
2018-06-14 00:57:30,360 [INFO ] Finished:\xxx_BBB_xxx
2018-06-14 00:57:30,360 [INFO ] Import Time(s):2803.77
2018-06-14 00:57:31,125 [INFO ] -----END IMPORT-----
...略...
<!-- more -->

而整个过程有监控数据类似如下Monitor.csv:

1
2
3
4
5
6
7
8
9
Time,CPU(%),Memory(%)
2018/06/13 23:51:17,2.7,12.17
2018/06/13 23:51:19,0.0,12.16
2018/06/13 23:51:21,0.0,12.16
2018/06/13 23:51:23,0.0,12.14
2018/06/13 23:51:25,0.4,12.15
2018/06/13 23:51:28,0.0,12.15
2018/06/13 23:51:30,0.0,12.15
...略...

那么现在的问题是只需要在跑BBB的时候的那段时间的监控数据,于是临时写了个简单的HardCode的过程达到目的,主要用到了datetime对时间的处理。

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
"""
author: xiche
create at: 06/14/2018
description:
Catch monitor data from whole proces
"""
from datetime import datetime

# datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
dateformat1 = "%Y-%m-%d %H:%M:%S" #"2018-06-13 23:51:17"
dateformat2 = "%Y/%m/%d %H:%M:%S" #"2018/06/13 23:51:52"

def __main__():
dt_start = None
dt_end = None
lines_new = []
with open("ImportLog.txt", "r") as f:
lines = f.readlines()
for line in lines:
if("BBB" in line and "Finished" in line):
dt_end = datetime.strptime(line[0:19], dateformat1)
break
if("BBB" in line):
dt_start = datetime.strptime(line[0:19], dateformat1)

with open("Monitor.csv", "r") as f:
lines = f.readlines()
dt_current = None
for line in lines:
try:
dt_current = datetime.strptime(line[0:19], dateformat2)
if(dt_current < dt_start):
continue
elif(dt_current > dt_end):
break #之前竟然是continue。。。。
else:
lines_new.append(line)
except:
print("No expect formated:{}".format(line))

with open("Monitor_BBB.csv", "w") as f:
f.writelines(lines_new)
# dt_start = datetime.strptime(dt_start_str, dateformat1)
# dt_end = datetime.strptime(dt_start_str, dateformat2)

# a = datetime.strptime("2018-06-13 23:51:17", "%Y-%m-%d %H:%M:%S")
# b = datetime.strptime("2018/06/13 23:51:52", "%Y/%m/%d %H:%M:%S")
# print(a, b)
__main__()

ACM with Python

使用python编写acm题目

ACM一般来说都是用C或者C++来编写,因为国内的大学入门教程就是以C和C++为主,而且编译后的执行效率也比较高。但是很多的Online Judge都支持别的语言,例如Java,Python,Perl,Pascal,PHP,FPC,C#等等。

省内我们之前常用的几个学校的OJ系统(浙大杭电工大),只有浙大的才支持Python,而且只有2.7.3,不支持Python3+。不过大同小异,下面就用简单的例子来演示下用python来写acm。

Read more

Regex in python

Regex in python 简介

正则表达式 是在文本处理的时候非常高效有用的一种方式,一般的编程语言都会内置相应的模块。

不同的编程语言使用方式不尽相同,核心的表达式模式都是一致的。

最近使用python生成Message,有用到正则表达式,今天简单的总结下python使用的方式。对于表达式本身的使用,之后有时间再详细介绍。

Read more

colorlog

colorlog

这个module的核心就是提供了colored formaterrlogging模块,所以使用上还是遵循logging的方式。

今天就简单的介绍下用法,我感觉够用了,有更多的需求的话可以再去深入了解。

Read more

openpyxl

openpyxl

之前测试PamReport的报表,需要对比txt和excel报表,用java(org.apache.poi)写了对比的脚本。但感觉还是有些麻烦,
java在文本io方面还是不够灵活方便。今天试了python的openpyxl这个库,感觉真的的特别好用,就试着用了一下基本操作。

Read more

System Monitor

System Monitor by python

之前有用powershell获取CPU,内存的信息GetCPUMemory.ps1,利用的是win系统提供的计数器。

今天用python重新写了一个,用的是psutil库并且是用OO的思想组织代码,方便之后的重构与维护

Read more