
python
lcqin111
这个作者很懒,什么都没留下…
展开
-
关于python的路径问题
import osimport sysprint('#' *10)print(os.path.abspath(os.path.dirname('__file__')))print('#' *10)print(os.path.abspath('__file__'))print('#' *10)print(os.path.dirname(os.path.abspath(sys.argv[0])))print('#' *10)print(sys.argv[0])print('#' *10)原创 2022-03-16 21:53:01 · 901 阅读 · 0 评论 -
pytorch中的嵌套函数
from torch._six import container_abcsfrom itertools import repeatfrom typing import Listdef _ntuple(n): def parse(x): if isinstance(x, container_abcs.Iterable): return x return tuple(repeat(x, n)) return parse_s.原创 2021-06-04 15:52:50 · 1446 阅读 · 2 评论 -
scrapy:同时运行多个爬虫
有的时候需要在scrapy工程里面运行多个爬虫,试用了网上两种方法。运行环境:scrapy:2.3.0 + python3.8第一种:# coding:utf-8from scrapy import cmdlinecmdline.execute("scrapy crawl spider1".split())cmdline.execute("scrapy crawl spider2".split())无效,只能运行第一个。第二种:https://www.cnblogs.com原创 2020-11-08 15:15:44 · 1389 阅读 · 0 评论 -
在scrapy与selemium对接结束后,关闭浏览器的方法
参考https://blog.youkuaiyun.com/Hepburn_li/article/details/91039747博客。一般在DownloaderMiddleware中建立browser对象。例如:class NewscrawlerDownloaderMiddleware: # Not all methods need to be defined. If a method is not defined, # scrapy acts as if the downloader mi原创 2020-08-04 13:09:39 · 768 阅读 · 2 评论 -
Annaconda更换国内源
https://www.cnblogs.com/yikemogutou/p/11396045.html较全方法集锦转载 2020-07-25 16:08:44 · 5307 阅读 · 0 评论 -
python: classmethod修饰符的使用以及在scrapy中的使用例子
import pymongoclass MongoPipeline(object): def __init__(self, mongo_uri, mongo_db): self.mongo_uri = mongo_uri self.mongo_db = mongo_db @classmethod def from_crawler(cls, crawler): return cls(mongo_uri=crawle.原创 2020-07-23 14:53:15 · 332 阅读 · 0 评论 -
matplotlib颜色表
原文地址:https://finthon.com/matplotlib-color-list/转载 2020-04-07 08:21:23 · 6968 阅读 · 0 评论 -
PYQT: 主窗口调用另一个主程序的方法
本例给出了三种窗口打开方式。其中主界面打开主界面的方式,比较实用,却少有示例。特发布于此,希望对大家有帮助。一.主界面打开 新的主界面二.主界面打开 对话框三.主界面打开 提示框【如下代码,完全复制,直接运行,即可使用】import sysfrom PyQt5.QtWidgets import *from PyQt5.QtCore import *from PyQt5.Q...转载 2019-12-21 20:40:41 · 4880 阅读 · 9 评论 -
pyqt: 关于近期遇到的多线程问题+多线程方案
问题描述:写了一个小应用,有两个控件一个播放摄像头视频,一个固定时间间隔显示的算法检测过的图片(图片实时从摄像头读取)。最开始做的方案是:一个线程负责采集摄像头的图像数据流, 一个线程负责把数据流的图片显示到控件上, 在主线程进...原创 2019-12-21 20:31:18 · 813 阅读 · 0 评论 -
python: 通过serial与stm32通信的编码问题
参考链接:decode错误处理方案可选用的编码使用环境:ubuntu18.04python3.65问题点:使用pyserial与stm32通信,使用如下形式的编码进行write (input_s + '\r\n').encode('utf-8')但是在接受stm的信息的时候使用'utf-8'解码会出现乱码问题在查了可用编码后,发现可能是中文无法解码的问题,...原创 2019-12-18 10:02:37 · 1324 阅读 · 1 评论 -
使用labelImg标注得到的VOC格式数据集转换成COCO格式数据集
本文代码参考https://blog.youkuaiyun.com/u010397980/article/details/90341223只做个笔记使用参考代码如下#coding:utf-8 # pip install lxml import osimport globimport jsonimport shutilimport numpy as npimport xml.et...转载 2019-11-19 17:18:56 · 6166 阅读 · 9 评论 -
Pytorch:RuntimeError: DataLoader worker (pid 9119)
问题:在测试阶段出现如题问题RuntimeError: DataLoader worker (pid 9119) is killed by signal: Killed.问题来源:经过测试和网上搜索,基本确定了问题是内存不足造成的,我设置的num_workers=8在训练阶段没有出现问题,可以确定使用八个线程对于我的内存是足够的,那么就查找到了test阶段,在这里我发现,te...原创 2019-10-15 20:10:34 · 2396 阅读 · 0 评论 -
Python:中文注释
python代码有中文注释方法在python编写代码的时候,避免不了会出现或是用到中文,这时候你需要在文件开头加上中文注释。比如创建一个python list,在代码上面注释上它的用途,如果开头不声明保存编码的格式是什么,那么它会默认使用ASKII码保存文件,这时如果你的代码中有中文就会出错了,即使你的中文是包含在注释里面的。所以加上中文注释很重要。#-*-coding=utf-8-*-或...转载 2018-07-10 14:05:41 · 968 阅读 · 0 评论 -
类方法
class Model: name = "DNN" def __init__(self, name): self.name = name def print_name(self): print(self.name) @classmethod def print_cls_name(cls): print(cls...转载 2019-03-12 15:26:47 · 110 阅读 · 0 评论 -
Python: 装饰器
直接上程序:def decorate(func): func.__doc__ += '\nDecorate by decorater.' return funcdef add(x, y): 'Return the sum of x and y.' return x + yadd = decorate(add)如上,我们需要在add函数的帮助文档...原创 2019-03-02 16:45:10 · 123 阅读 · 0 评论 -
Python: 闭包
def make_averager():count = 0total = 0def averager(new_value):nonlocal count, totalcount += 1total += new_valuereturn total / countreturn averager>>> from test import make_averager&g...转载 2019-03-01 21:48:52 · 94 阅读 · 0 评论 -
python : time模块weekday()
weekday(year,month,day)返回给定日期的日期码。0(星期一)到6(星期日)。月份为 1(一月) 到 12(12月)。所以,如果要使返回的数字与星期对应,则在返回值后面加1。...原创 2018-12-26 10:02:52 · 2590 阅读 · 0 评论 -
Python: 使用Numpy对list操作
>>> a = []>>> b = [1,2,3]>>> c = [4,5,6]>>> a.append(b)>>> a.append(c)>>> a[[1, 2, 3], [4, 5, 6]]>>> t原创 2018-12-24 21:22:01 · 2665 阅读 · 0 评论 -
Python: operator.itemgetter(item)
operator.itemgetter(item)返回一个可调用对象,该对象使用操作数的方法从其操作数中获取项>>> itemgetter(1)('ABCDEFG')'B'>>> itemgetter(1,3,5)('ABCDEFG')('B', 'D', 'F')>>> itemgetter(slice(2,None))('...翻译 2018-12-10 12:15:01 · 527 阅读 · 0 评论 -
Python: 字典的操作
删除键值对: del alien_0['points']从字典 alien_0 中删除键 'points' 及其值检查字典中是否存在键和值: ‘zophie’ in spam.values() 值中是否含有zophie ‘color’ i...转载 2018-07-17 00:34:39 · 146 阅读 · 0 评论 -
Python: print(__doc__)
转自:https://blog.youkuaiyun.com/u010837794/article/details/74940885?locationNum=6&fps=1作用输出文件开头注释的内容推广1)momodule.py"""This is the module docstring."""def f(x): """This is the function docs...转载 2018-12-04 21:45:50 · 1260 阅读 · 0 评论 -
Python : 类定义中,不加self.和加self.的区别【关于局部变量和全局变量的使用详解】
先上代码,毕竟无代码言吊>>> class k(): c = 6 def __init__(self): self.c=1>>> k.c6>>> mk = k()>>> mk.c1差别就在这个地方,第一个C实例是无法调用的,是类中的局部变量,只有父类可以调用,而且,单独输入C...原创 2018-11-21 20:45:38 · 11687 阅读 · 1 评论 -
Python:使用方法修改字符串的大小写
在 print() 语句中,方法 title() 出现在这个变量的后面。方法 是 Python 可对数据执行的操作。在 name.title() 中, name 后面的句点( . )让 Python 对变量 name 执行方法 title() 指定的操作。每个方法后面都跟着一对括号,这是因为方法通常需要额外的信息来完成其工作。这种信息是在括号内提供的。函数 title() 不需要额外的信息,因此...转载 2018-07-10 11:22:16 · 360 阅读 · 0 评论 -
Python:删除空白
Python 能够找出字符串开头和末尾多余的空白。要确保字符串末尾没有空白,可使用方法 rstrip()>>> favorite_language = 'python '>>> favorite_language'python ' >>> favorite_language.rstrip()'python' >>>...转载 2018-07-10 11:50:14 · 2314 阅读 · 3 评论 -
python : 属性封装
class Model: __name = "DNN" def __init__(self, name): self.__name = name def print_name(self): print(self.__name) @classmethod def print_cls_name(cls): pr...转载 2019-03-12 15:32:05 · 614 阅读 · 0 评论 -
python : special method
class X: passclass Y: """Class Y""" def __str__(self): return "{} object".format(self.__class__.__name__) def __len__(self): return 10 def __bool__(self): ...转载 2019-03-12 16:00:32 · 384 阅读 · 0 评论 -
Python: 在不确定需要输入行数的情况下,逐行读取输入数据
HACKER RANK的一个编程题import sys # Read input and assemble Phone Bookn = int(input())phoneBook = {}for i in range(n): contact = input().split(' ') phoneBook[contact[0]] = contact[1]# Proce...原创 2018-09-06 14:03:10 · 8562 阅读 · 0 评论 -
Python: 记录一个关于图片直接转化为pytorch.tensor和numpy.array的不同之处的问题
img = Image.open(img_path).convert("RGB")img2 = torchvision.transforms.functional.to_tensor(img)print(img2)img1 = np.array(img)print(img1)输出是这样的:不仅shape不一样,而且值也是不一样的。解释如下:tensor = t...原创 2019-08-12 00:28:14 · 2681 阅读 · 0 评论 -
python四个魔法方法__len__,__getitem__,__setitem__,__delitem__
转自:https://blog.youkuaiyun.com/yuan_j_y/article/details/9317817python中除了可以使用内建的类型,如list,tuple,dict,还可以创建自己的对象来实现像这些内建类型的访问,不过需要在定义类的时候对一些魔法方法逐一实现。如下:class DictDemo: def __init__(self,key,value)...转载 2019-05-17 22:00:57 · 446 阅读 · 0 评论 -
Pytorch: RuntimeError: expected Double tensor (got Float tensor)
normalize = tvtsf.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])img = normalize(t.from_numpy(img))在这段代码运行后,会出现RuntimeError: expected Double tensor (go...原创 2019-05-25 09:00:59 · 7612 阅读 · 0 评论 -
Matplotlib:生成图片但不显示图片
Generate images without having a window appear使用matplotlib.use()函数设置制图后端。import matplotlibmatplotlib.use('Agg')import matplotlib.pyplot as pltplt.plot([1,2,3])plt.savefig('myfig')上面代码不显示图...原创 2019-05-10 10:28:44 · 5159 阅读 · 0 评论 -
Python: 关于类初始化使用的一点细节
先上代码>>> class nn: def __init__(self,ss,kk): self.ss=ss self.kk=kk>>> class kn(nn): def __init__(self,qq=1,ww=2): ss = qq * 15 kk = ww *10 super(kn, self).__init__(ss...原创 2019-05-05 20:54:08 · 507 阅读 · 0 评论 -
Python : OS 模块常用
os.path.join(路径,*路径小号)智能地加入一个或多个路径组件。返回值是路径和*paths的任何成员的串联,在除了last之外的每个非空部分之后只有一个目录separator(),这意味着如果最后一部分为空,结果将仅在分隔符中结束。如果组件是绝对路径,则抛弃所有先前组件并从绝对路径组件继续连接。os.pathsepThe character conventionally...原创 2019-04-03 16:21:00 · 152 阅读 · 0 评论 -
Python : -> 符号
def f(ham: str, eggs: str = 'eggs') -> str: print("Annotations:", f.__annotations__) print("Arguments:", ham, eggs) return ham + ' and ' + eggs这些是类型提示。各种类型的检查器可以使用它们来确定您是否使用了正确的类型...原创 2019-04-04 10:53:50 · 301 阅读 · 0 评论 -
python : 可变变量作为函数参数
>>> def add_one( L = [] ):... L.append(1)... return L...>>> a = add_one()>>> b = add_one(list(range(5)))>>> c = add_one() + list(range(5))>>> len(b...原创 2019-03-19 09:47:39 · 655 阅读 · 0 评论 -
python :惰性计算
python中含有惰性计算,即调用的时候才开始计算https://www.cnblogs.com/shiqi17/p/9608195.html>>> f_list = [lambda x: x**i for i in range(5)]>>> sum((f_list[j](10) for j in range(5)))输出为50000...转载 2019-03-19 09:17:49 · 938 阅读 · 0 评论 -
python : property
Property的设计初衷:【引以为戒:考虑计算成本,考虑使用方代码不更改,初次见代码心有感慨,道路长漫漫,我辈须努力】代码复用延迟计算更加规范的对象属性访问管理class X: def __init__(self, w, h): self.w = w self.h = h self.BMI = w / h ** 2 ...转载 2019-03-12 16:23:12 · 181 阅读 · 0 评论 -
python : attribute access
Attribute相关的操作一一般有:CreateReadUpdateDeleteclass X: pass if __name__ == "__main__": # print(X.a) X.a = "a" print(X.a) X.a = "aa" print(X.a) del X.a # print(X....转载 2019-03-12 16:03:26 · 251 阅读 · 0 评论 -
Python: for 循环,建立列表
for 循环可以搭配range函数和列表来实现循环for i in range(a,b,c)a 为循环起始点,b为终点,不等于b,c为步长 new_users = ['Kain', 'lily', 'Jie', 'peng', 'tj']for i in new_users:使用for 遍历整个列表 list1 = list(range(1, 10))...转载 2018-07-11 15:55:06 · 5543 阅读 · 0 评论 -
Python: 定义函数规则
向函数传递实参的方式很多,可使用位置实参 ,这要求实参的顺序与形参的顺序相同;也可使用关键 字实参 ,其中每个实参都由变量名和值组成;还可使用列表和字典。位置实参:Python 将按顺序将函数调用中的实参关联到函数定义中相应的形参def describe_pet(animal_type, pet_name):""" 显示宠物的信息 """print("\nI have a...转载 2018-07-13 10:16:38 · 10927 阅读 · 0 评论