学习文档接触到的知识 openclose

辛辛苦苦整理,脑子却不是很清楚,都怪晚上猫不睡觉,让我也没睡好

openset 和 closeset

1、import time   #时间模块 

查阅的知识:time模块是包含各方面对时间操作的函数. 尽管这些常常有效但不是所有方法在任意平台中有效. time用struct_time表示时间
time.time(): 返回一个时间戳
time.asctime([t]): 转换gmtime()和localtime()返回的元组或struct_time为string.
time.clock(): 在第一次调用的时候, 返回程序运行的时间. 第二次之后返回与之前的间隔.
time.ctime([secs]): 将时间戳转换为时间字符串, 如没有提供则返回当前的时间字符串,并与asctime(localtime())一样.
time.gmtime([secs]): 将时间戳转化为, UTC 时区的struct_time.
time.localtime([secs]): 类似gmtime()但会把他转换成本地时区.
time.mktime(t): struct_time 转化为时间戳.
time.sleep(secs): 线程推迟指定时间, 以秒为单位.
time.strftime(format[,t]): 根据参数转换一个sturc_time或元组为字符串.
time.strptime(string[, format]): 与strftime相反,返回一个struct_time.

import time
>>> print (time.asctime(time.gmtime()))
Fri Aug 18 01:51:17 2017
>>> print(time.clock())
1.9555558038801022e-06
>>> print(time.clock())
26.231219819837438
>>> print(time.clock())
40.59816275532226
>>> print(time.ctime())
Fri Aug 18 09:53:14 2017
>>> print(time.mktime(time.localtime()))
1503021235.0
文档中的应用:
    start_time = time.clock()
    ~~~~~~for 循环~~~~~~~~
    end_time=time.clock()-start_time

2、from utils import *  
查阅的知识:
比如mmmm.py是一个python文件,在其中有自定义内容:
web = "https://qiwsir.github.io"
def my_name(name):
    print name
class pythoner:
    def __init__(self,lang):
        self.lang = lang
    def programmer(self):
        print "python programmer language is: ",self.lang

想要在其它python文件中,比如test_1.py中引用这个文件的内容,可以这么做:
>>> import mmmm
>>> mmmm.web
'https://qiwsir.github.io'
>>> mmmm.my_name("qiwsir")
qiwsir
>>> py = mmmm.pythoner("c++")   
>>> py.programmer()
python programmer language is:  c++

也可以:
>>> from mmmm import *
>>> my_name('qiwsir')
qiwsir
>>> web
'https://qiwsir.github.io'
>>> py = pythoner("c++")
>>> py.programmer()
python programmer language is:  c++、

||||||||我真是一个勤劳好学的小姑娘|||||||||||||||||

3、gallery_list=open(gallery_name).readlines()   #打开文件并按行整读
查阅的知识:
f = open('poem.txt','r')  
result = list()  
for line in open('poem.txt'):  
    line = f.readline()  
    print line  
    result.append(line)  
print result  
f.close()                  
open('result-readline.txt', 'w').write('%s' % '\n'.join(result)
············
f = open('cdays-4-test.txt', 'r')                   #以读方式打开文件  
result = list()  
for line in f.readlines():                          #依次读取每行  
    line = line.strip()                             #去掉每行头尾空白  
    if not len(line) or line.startswith('#'):       #判断是否是空行或注释行  
        continue                                    #是的话,跳过不处理  
    result.append(line)                             #保存  
result.sort()                                       #排序结果  
print result  
open('cdays-4-result.txt', 'w').write('%s' % '\n'.join(result)) #保存入结果文件  

4、if isinstance(img_list_file,str):    #isinstance()函数判断对象类型
查阅的知识:
>>> a = 10
>>> print(isinstance(a,(int,str)))
True
>>> print(isinstance(a,int))
True
>>> b='bb'
>>> print(isinstance(b,int))
False
>>> def d():
pass

>>> class c:
pass

>>> obj = c()
>>> print(isinstance(obj,(c,int)))
True
>>> print(isinstance(d,(str,int)))
False

对于subclass之类的 type就不行了,所以,强烈建议不要使用type判断对象类型。比如:
lass A:
    pass
class B(A):
    pass
isinstance(A(), A)  # returns True
type(A()) == A      # returns True
isinstance(B(), A)    # returns True
type(B()) == A        # return

>>> class sample:
def __enter__(self):
print ("In__enter__()")
return "Foo"
def __exit__(self,type,value,trace):
print("In__exit__()")

5、with open(img_list_file) as f:  #Python的with语句提供了一种处理方式。例:文件处理,你需要获取一个文件句柄,从文件中读取数据,然后关闭文件句柄。
查阅的知识:
>>> def get_sample():
return sample()
>>> with get_sample() as sample:
print("sample:",sample)

In__enter__()
sample: Foo
In__exit__()


在python2.5及以后,file对象已经写好了enter和exit函数,我们如果要打开文件并保证最后关闭他,只需要这么做:
with open("x.txt") as f:
    data = f.read()
    do something with data

6、 #for caffe
        img=img[np.newaxis,:,:]/256.0
查阅的知识:
用np.newaxis对象进行索引,可以为一个数组添加轴
>>> import numpy as np
>>> z = np.array([1,2,3])
>>> z
array([1, 2, 3])
>>> z[:,np.newaxis]
array([[1],
       [2],
       [3]])
>>> z[np.newaxis, :]
array([[1, 2, 3]])
>>> 

7、np.array_split(images,num_of_images//batch+1):
查阅的知识:
numpy.plit(ary, indices_or_sections, axis=0)
>>> x = np.arange(9.0)
>>> np.split(x, 3)
[array([ 0.,  1.,  2.]), array([ 3.,  4.,  5.]), array([ 6.,  7.,  8.])]

>>> x = np.arange(8.0)
>>> np.array_split(x, 3)   #可以不均等分
[array([ 0.,  1.,  2.]), array([ 3.,  4.,  5.]), array([ 6.,  7.])]

8、   net.blobs['data'].reshape(*imgs.shape)  #blobs['data']是通过layer_name获取layer对应的blob。
拿到blob后,取出这个blob中的data变量,通过Python接口调用后,它会自动被转成ndarray类型,这个类型本身就自带shape函数。

9、net.forward()   #farward是根据输入数据正向预测输入属于哪一类

10、for i,p in enumerate(probe_g_feats):
查阅的知识:
enumerate()说明
enumerate()是python的内置函数
enumerate在字典上是枚举、列举的意思
对于一个可迭代的(iterable)/可遍历的对象(如列表、字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值
enumerate多用于在for循环中得到计数

对一个列表,既要遍历索引,又要遍历元素
list1 = ["这", "是", "一个", "测试"]
for index, item in enumerate(list1):
    print index, item
>>>
0 这
1 是
2 一个
3 测试

11、    pickle.dump((probe_g_feats,probe_i_feats,gallery_feats),open(feat_name,"wb"))  
pickle.dump(obj, file, [,protocol])
  注解:将对象obj保存到文件file中去。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值