Python笔记


基本配置

pycharm

external tools

  • 基本的填表范式

在这里插入代码片

pyuic
$JDKPath$
-m PyQt5.uic.pyuic $FileName$ -o $FileNameWithoutExtension$.py 
$FileDir$/
2to3
$JDKPath$
/home/tqc/anaconda3/bin/2to3 -w "$FilePath$"
$FileDir$

pycharm自动导入包(类似idea的智能操作)

https://blog.youkuaiyun.com/gaopeng0071/article/details/79402399

pycharm自动跳出括号

https://blog.youkuaiyun.com/PAN_Andy/article/details/83276115

分组替换print

https://blog.youkuaiyun.com/blmoistawinde/article/details/80861811

换源

给pip添加阿里源

打开C:\Users\86130\AppData\Roaming\pip\pip.ini
或者新建:C:\Users\TQC\pip\pip.ini
添加

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
[global]
index-url = http://pypi.douban.com/simple
[install]
trusted-host = pypi.douban.com

操作系统

日志

def add_log(info):
    '''日志'''
    nowTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')  # 现在
    with open('log.txt','a+') as f:
        f.writelines(info + str(nowTime) + '\n')

文件读写

yaml读写配置

如果配置文件是一个字典, 用此函数, 获取键对应的值

def get_config(name):
    with open('config.yaml', 'r', encoding='utf-8') as f:
        cont = f.read()
        x = yaml.load(cont)
        return  x[name]

如果配置文件需要读写, 比如读出后需要自增写入:

def get_pos():
    #read
    with open(param_path, 'r+', encoding='utf-8') as f:
        cont = f.read()
        x = yaml.load(cont)
        pos=0
        try:
            pos=int(x['pos'])
        except BaseException:pass
        npos=pos+1
    #write
    with open(param_path, 'w+', encoding='utf-8') as f:
        x['pos']=npos
        yaml.dump(x, f)
    return pos

文件遍历

遍历文件夹

for (root, dirs, files) in os.walk(path)

创建文件夹

def buildDir(name):
    '''对于文件名name,如果不存在则创建'''
    if not os.path.exists(name):
        os.makedirs(name)

序列化

https://www.cnblogs.com/zhangxinqi/p/8034380.html

用默认程序打开文件/网页

os.system(f'xdg-open {tmpName}')

数学运算

随机数

生成随机数

  • o-1随机数
random.random()
  • a-b随机整数
random.randint(a,b)
  • a-b随机范围
random.randrange

随机从列表抽取一项

random.choice(li)

图像处理

依比例缩放

h,w=img.shape  #高为矩阵的行数
nh=80
nw=int(nh*w/h)
img=cv2.resize(img,(nw,nh))   #cv2的缩放按照宽高来,和shape得到的数据相反
plt.imshow(img)

正则表达式

获取url的主机名

def getHost(url):
    pattern_str=r"http:\/\/(.*?)\/"
    pattern=re.compile(pattern_str)
    matchObj=pattern.match(url)
    if matchObj:
        host=matchObj.group(1)
		return host

语法

装饰器

def debug(func):
    def wrapper(*args, **kwargs):  # 指定宇宙无敌参数
        print('before')
        return func(*args, **kwargs)
    return wrapper  # 返回

@debug
def say(a,b,c,*args):
    print(a,b,c)
    for x in args:
        print(x)

内置函数

    print(all([0,1,2])) #要求所有为真
    print(any([0,0,0])) #要求只要有一个为真
    print(chr(57))  #ascii转字符
    print(ord('0'))  #字符转ascii

函数式编程

    #函数式编程
    li=[1,2,3,4,5]
    #列表解析
    li=[x+1 for x in li ]
    print(li)
    #映射,对迭代对象的每一个执行相同的操作
    for x in map(lambda x:x+1,li):
        print(x,end=' ')
    print()
    #筛选,只返回符合条件的迭代对象
    for x in filter(lambda x:not x%2,li):
        print(x,end=' ')
    print()
    #累加
    from functools import reduce
    print(reduce(lambda x,y:x+y,li))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值