代码编写writeup

第一题:md5(‘root’)

md5root
代码如下:

1
2
3
4
5
6
7
import hashlib
time=0
m1='root'
while time <500:
time+=1
m1=hashlib.md5(m1.encode('utf-8')).hexdigest()
print(m1)

阅读全文
threading模块使用总结

threading简介

线程是操作系统能够进行运算调度的最小单位。线程被包含在进程中,是进程中实际处理单位。一条线程就是一堆指令集合。一条线程是指进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务.进程(process)是一块包含了某些资源的内存区域

Thread类

Thread是线程类,有两种使用方法,直接传入要运行的方法或从Thread继承并覆盖run():

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
# coding:utf-8
import threading
import time
#方法一:将要执行的方法作为参数传给Thread的构造方法
def action(arg):
time.sleep(1)
print 'the arg is:%s\r' %arg

for i in xrange(4):
t =threading.Thread(target=action,args=(i,))
t.start()

print 'main thread end!'

#方法二:从Thread继承,并重写run()
class MyThread(threading.Thread):
def __init__(self,arg):
super(MyThread, self).__init__()#注意:一定要显式的调用父类的初始化函数。
self.arg=arg
def run(self):#定义每个线程要运行的函数
time.sleep(1)
print 'the arg is:%s\r' % self.arg

for i in xrange(4):
t =MyThread(i) # 直接可运行run函数
t.start()

print 'main thread end!'

阅读全文
pymysql模块使用总结

基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 导入pymysql模块
import pymysql
# 连接database
conn = pymysql.connect(host=“你的数据库地址”,user=“用户名”
,password=“密码”,database=“数据库名”,charset=“utf8”)
# 得到一个可以执行SQL语句并且将结果作为字典返回的游标
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 定义要执行的SQL语句
sql = """
CREATE TABLE USER1 (
id INT auto_increment PRIMARY KEY ,
name CHAR(10) NOT NULL UNIQUE,
age TINYINT NOT NULL
)ENGINE=innodb DEFAULT CHARSET=utf8;
"""
# 执行SQL语句
cursor.execute(sql)
# 关闭光标对象
cursor.close()
# 关闭数据库连接
conn.close()
阅读全文
random模块使用总结

随机整数,下限必须小于上限

1
2
3
>>> import random
>>> random.randint(0,99)
21

随机选取0到100的偶数

1
2
>>> import random
>>> random.randrange(0, 101, 2)
阅读全文
os模块运用

os模块之路径运用

方法 说明
os.path.abspath(path) 返回绝对路径
os.path.basename(path) 返回文件名
os.path.commonprefix(list) 返回list(多个路径)中,所有path共有的最长的路径
os.path.dirname(path) 返回文件路径
os.path.exists(path) 路径存在则返回True,路径损坏返回False
os.path.lexists 路径存在则返回True,路径损坏也返回True
os.path.expanduser(path) 把path中包含的”~”和”~user”转换成用户目录
os.path.expandvars(path) 根据环境变量的值替换path中包含的”$name”和”${name}”
os.path.getatime(path) 返回最近访问时间(浮点型秒数)
os.path.getmtime(path) 返回最近文件修改时间
os.path.getctime(path) 返回文件path创建时间
os.path.getsize(path) 返回文件大小,如果文件不存在就返回错误
os.path.isabs(path) 判断是否为绝对路径
os.path.isfile(path) 判断路径是否为文件
os.path.isdir(path) 判断路径是否为目录
os.path.islink(path) 判断路径是否为链接
os.path.sismount(path) 判断路径是否为挂载点
os.path.join(path1[,path2[,…]]) 把目录和文件名合成一个路径
os.path.normcase(path) 转换path的大小写和斜杠
os.path.normpath(path) 规范path字符串形式
os.path.realpath(path) 返回path的真实路径
os.path.relpath(path[,start]) 从start开始计算相对路径
os.path.samefile(path1,path2) 判断目录或文件是否相同
os.path.sameopenfile(fp1,fp2) 判断fp1和fp2是否指向同一文件
os.path.samestat(stat1,stat2) 判断stat tuple stat1和stat2是否指向同一个文件
os.path.split(path) 把路径分割成dirname和basename,返回一个元组
os.path.splitdrive(path) 一般用在windows下,返回驱动器名和路径组成的元组
os.path.splitext(path) 分割路径,返回路径名和文件扩展名的元组
os.path.splitunc(path) 把路径分割为加载点与文件
os.path.walk(path,visit,arg) 遍历path,进入每个目录都调用visit函数,visit函数必须有3个参数(arg,dirname,names),dirname表示当前目录的目录名,names代表当前目录下的所有文件名,args则为walk的第三个参数
os.path.supports_unicode_filenames 设置是否支持unicode路径名
阅读全文
Algolia