argparse模块

import argparse
parser = argparse.ArgumentParser()
parser.add_argument(‘-src’, ‘–source’, dest=’video_source’, type=int,
default=0, help=’Device index of the camera.’)
parser.add_argument(‘-wd’, ‘–width’, dest=’width’, type=int,
default=1280, help=’Width of the frames in the video stream.’)
parser.add_argument(‘-ht’, ‘–height’, dest=’height’, type=int,
default=720, help=’Height of the frames in the video stream.’)

print(args.width)
(参数依次为:dest - 解析后的参数名称,默认情况下,对于可选参数选取最长的名称,中划线转换为下划线
type - 命令行参数应该被转换成的类型
default - 不指定参数时的默认值
help - 参数的帮助信息,当指定为 argparse.SUPPRESS 时表示不显示该参数的帮助信息.)
使用:e.g. 命令行输入python objection_detection_app.py –width 20
输出结果为20

数据库使用模块

2017 MySQLdb只支持Python2. ,还不支持3.

  1. MySQLdb
    ` import MySQLdb

db = MySQLdb.connect(“localhost”,”root”,’…’,’test’)

cursor = db.cursor()
name=”ali”
age = 10

sql = “select * from student;”

sql = “insert into student(name,age) values(‘%s’,’%s’)”%(name, age)

sql = “delete from student where name=’%s’”%(name)
cursor.execute(sql)

result = cursor.fetchall()

print type(result)

db.commit()
db.close()


>> 2. pymysql
>>> 

import pymysql

ms = pymysql.connect(host=’localhost’, user=’root’, passwd=’…’, database=’person_seat_detection’, charset=’utf8’)
cur = ms.cursor()

‘’’

存数据

sql = “insert into schinfo values(1, ‘北华航天工业学院’, ‘河北省廊坊市’)”
cur.execute(sql)
ms.commit()
‘’’

读数据

sql = “select * from schinfo”
count = cur.execute(sql)
for i in range(count):
print(cur.fetchone()) # 结果为(1, ‘北华航天工业学院’, ‘河北省廊坊市’)

cur.close()
ms.close()

count = cur.execute(“””SELECT cid, sq_name, content, labels, score

from content_copy1_copy

where labels != ‘’”””)

flag = 0

for i in range(count):

fc = cur.fetchone()

cur_cid = ms.cursor()

count_cid = cur_cid.execute(“select cid from shequscore_copy1_copy where cid = %s”, (fc[0]))

if count_cid == 0:

cur_inser = ms.cursor()

cur_inser.execute(“insert into shequscore_copy1_copy values(%s, %s, %s, %s, %s)”,

(fc[0], fc[1], fc[2], fc[3], fc[4]))

flag += 1

ms.commit()

`

Python3多线程

线程可以分为:

  • 内核线程:由操作系统内核创建和撤销。
  • 用户线程:不需要内核支持而在用户程序中实现的线程
    Python3线程中常用的两个模块为:
    _thread
    threading(推荐使用)

threading模块除了包含_thread模块中的所有方法外,还提供的其他方法:

  • threading.currentThread():返回当前的线程变量
  • threading.enumerate():返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程
  • threading.activeCount():返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。
  • run():用以表示线程活动的方法。
  • start():启动线程活动。
  • join([time]):等待至线程中止。这阻塞调用线程直至线程的join()方法被调用中止-正常退出或抛出未处理的异常-或者是可选的超时发生。
  • isAlive():返回线程是否活动的。
  • getName():返回线程名。
  • setName():设置线程名。

参考链接:http://www.runoob.com/python3/python3-multithreading.html
http://python.jobbole.com/81546/

  • 创建线程的方法之一:

import threading, time, random count = 0 lock = threading.Lock() def doAdd(): '''@summary: 将全局变量count 逐一的增加10000。 ''' global count, lock lock.acquire() for i in xrange(10000): count = count + 1 lock.release() for i in range(5): threading.Thread(target = doAdd, args = (), name = 'thread-' + str(i)).start() time.sleep(2) # 确保线程都执行完毕 print count
for i in range(1): t = Thread(target=worker, args=(input_q, output_q)) # 线程开始执行worker方法,方法参数值为input_q, output_q t.daemon = True # 被 flag 为 daemon 的线程需要长期在后台执行(比如发送心跳包、检查未读消息等),并且不需和用户直接交互,和守护进程类似。 t.start()
解释:def init(self, group=None, target=None, name=None, args=(), kwargs={})

  • 参数group是预留的,用于将来扩展;
  • 参数target是一个可调用对象(也称为活动[activity]),在线程启动后执行;
  • 参数name是线程的名字。默认值为“Thread-N“,N是一个数字。
  • 参数args和kwargs分别表示调用target时的参数列表和关键字参数。

tensorflow

Python

os.path.join() 用于路径拼接文件路径



本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!