wideband
发表于 2023-3-3 14:00:22
高阶函数,函数当成参数进行传递
def sum(a,b,f):
return f(a)+f(b)
result1 = sum(-1,2,abs)
result2 = sum(1.5,2.7,round)
print(result1) #3
print(result2) #5
wideband
发表于 2023-3-4 10:34:06
精度问题
print(0.1 + 0.2 == 0.3)
###False
import math
print(math.isclose(0.1 + 0.2, 0.3))
###True
print(0.1+0.2)
#0.30000000000000004
wideband
发表于 2023-3-4 15:58:09
for循环,更节省时间:
numbers = (1, 3, 6)
newNumbers = list(numbers)
print(newNumbers)
import time
start = time.perf_counter()
n = 0
for i in range(0,100000000):
pass
n = n+1
end = time.perf_counter()
print(n)
print(end - start)
wideband
发表于 2023-3-5 09:42:18
f.seek(2,0)
wideband
发表于 2023-3-13 14:36:59
百度的语音技术开发平台下创建一个 语音识别的 应用识别 //www.360doc.com/content/22/1210/15/68330161_1059726912.shtml
一在百度智能云创建语音识别应用。cloud.baidu.com, 产品服务---人工智能---语音技术, 创建应用,默认立即创建。
二 、获取Access token , 通过导入requests包,使用项目API key 和Secret Key 获取。运行,封装成一个函数。
三、通过 Post 将上传音频文件,获得语音识别结果。
wideband
发表于 2023-3-13 17:55:52
pip install certifi
pip install chardet # No module named 'chardet'
pip install idna
pip install urllib3
wideband
发表于 2023-3-26 18:37:16
语音关键词识别,并转换文字输出OK:
# -*- coding: utf-8 -*-
# 使用讯飞语音转写API进行音频转文字#
import base64
import hashlib
import hmac
import json
import os
import time
import requests
import urllib
import re
import paddlehub as hub
mode = hub.Module(name='auto_punc', version='1.0.0')
lfasr_host = 'https://raasr.xfyun.cn/v2/api'
# 请求的接口名
api_upload = '/upload'
api_get_result = '/getResult'
class RequestApi(object):
def __init__(self, appid, secret_key, upload_file_path):
self.appid = appid
self.secret_key = secret_key
self.upload_file_path = upload_file_path
self.ts = str(int(time.time()))
self.signa = self.get_signa()
def get_signa(self):
appid = self.appid
secret_key = self.secret_key
m2 = hashlib.md5()
m2.update((appid + self.ts).encode('utf-8'))
md5 = m2.hexdigest()
md5 = bytes(md5, encoding='utf-8')
# 以secret_key为key, 上面的md5为msg, 使用hashlib.sha1加密结果为signa
signa = hmac.new(secret_key.encode('utf-8'), md5, hashlib.sha1).digest()
signa = base64.b64encode(signa)
signa = str(signa, 'utf-8')
return signa
def upload(self):
upload_file_path = self.upload_file_path
file_len = os.path.getsize(upload_file_path)
file_name = os.path.basename(upload_file_path)
param_dict = {}
param_dict['appId'] = self.appid
param_dict['signa'] = self.signa
param_dict['ts'] = self.ts
param_dict["fileSize"] = file_len
param_dict["fileName"] = file_name
param_dict["duration"] = "200"
data = open(upload_file_path, 'rb').read(file_len)
response = requests.post(url =lfasr_host + api_upload+"?"+urllib.parse.urlencode(param_dict),
headers = {"Content-type":"application/json"},data=data)
result = json.loads(response.text)
return result
def get_result(self):
uploadresp = self.upload()
orderId = uploadresp['content']['orderId']
param_dict = {}
param_dict['appId'] = self.appid
param_dict['signa'] = self.signa
param_dict['ts'] = self.ts
param_dict['orderId'] = orderId
param_dict['resultType'] = "transfer,predict"
status = 3
# 建议使用回调的方式查询结果,查询接口有请求频率限制
while status == 3:
response = requests.post(url=lfasr_host + api_get_result + "?" + urllib.parse.urlencode(param_dict),
headers={"Content-type": "application/json"})
result = json.loads(response.text)
status = result['content']['orderInfo']['status']
if status == 4:
break
time.sleep(5)
#print("get_result resp:",result)
return result
#这里是音乐文件的文件夹
dir_path = r'D:\\pythonProject13\\mp3\\'
fileList=[]
# 输入讯飞开放平台的appid,secret_key和待转写的文件路径
if __name__ == '__main__':
keyString=['微信','老爸']
fileList=os.listdir(dir_path)
for fileName in fileList:
time.sleep(2)
print("正在识别:"+fileName)
dir=dir_path+fileName
api = RequestApi(appid="1485995d",
secret_key="a5fe900e4baff4234241690022143024",
upload_file_path=dir)
res=api.get_result()
obj=res['content']['orderResult']
pattern = re.compile("[\u4e00-\u9fff]+")
# 用正则匹配所有的汉字
matches = pattern.findall(str(obj))
sencent = ""
for i in range(len(matches)):
sencent += matches
print(sencent)
punc_texts = ""
long = (len(sencent)/10)
for i in range(10):
punc_texts += mode.add_puncs(sencent)
#with open("202332626.txt","w") as f:
with open("D:/pythonProject13/mp3/" + fileName + "_0326.txt", "w") as f:
f.write(punc_texts)
for key in keyString:
if(key in obj):
print(fileName+"--检测到关键字【"+key+"】")
wideband
发表于 2023-3-29 08:44:28
print("我明年%s岁,学号%06d,体重%.2f"%(age+1,id,weight))
wideband
发表于 2023-3-30 09:34:36
codewars.com
等级提升练习题
wideband
发表于 2023-4-18 09:53:06
可迭代对象:可以用 for 循环遍历的对象,iter
from collections import Iterable
isinstance (a, Iterable)
如果是 True 则是;
wideband
发表于 2023-4-18 09:54:28
可迭代 对象,转换为迭代器: aa = iter(a)
wideband
发表于 2023-5-17 08:58:06
创建实例属性的三种方法:
1)实例对象化;
2)实例对象 调用实例方法;
3)构造函数:又称魔术方法,不需手动调用;
1: 通过类对象(不同于 实例对象)调用实例方法;
2: self:实例对象地址;
3:谁调用,传递谁;
wideband
发表于 2023-6-5 10:48:44
关于函数说明:
1:python里面,函数 :这3个功能一样; 1)不写 return 2) return 3)return None
2:函数可以返回多个值,以元组形式 输出; 但是 可以用多个变量接收 函数得多个返回值,直接拆包;
3:函数运行中,遇到return 会终止函数
wideband
发表于 2023-6-14 11:11:34
类 和 实例对象,分别在不同的内存空间,所以:
实例对象不可以直接访问类中的方法和属性, 需要借助类中属性:__class_ _ 属性,用来保存类对象 在内存中的地址。
__class_ _ 属性,不是方法。实例对象内部 只会保存一 些属性 , 方法 保存 在类对象中。
wideband
发表于 2023-6-14 14:13:48
鸭子类型,多态,兄弟类,混合类: 一个类名 叫 minin
前端:python APP
后端 :golang 微服务器分发
wideband
发表于 2023-6-14 15:41:11
cls: 类对象
self : 实例对象
object :python3的 基类
__init__
__new__
__str__
wideband
发表于 2023-6-15 07:09:22
类属性是所有实例共享的属性,可以通过类名或实例对象来进行访问;
而实例属性是特定实例独有的属性,只能通过实例对象来进行访问。
另外,当通过实例对象访问一个属性时,Python会先在实例中查找该属性,如果没有找到,则继续到该实例所属的类中查找,直到最终到达该属性的定义处。
wideband
发表于 2023-6-17 06:25:45
知识点补课:
1)元组拆包 //www.bilibili.com/video/BV1o4411M71o?p=195&vd_source=bbd2966da727b3376515872084138922
num1,num2=(1,2) ,也可以接收 函数返回2个值的,比如 函数体内:return 1,2
2) 字典拆包
dict1 ={"name":"Tom","age":8}
a,b = dict1
print(a,b), print(dict1,dict1)#分别name, age和 Tom ,8
wideband
发表于 2023-6-17 07:03:04
引用://www.bilibili.com/video/BV1o4411M71o?p=199&vd_source=bbd2966da727b3376515872084138922
快递,靠什么 进行发送的? 地址; 值靠什么传递?地址(引用);
a = 1#一个内存,里面存放了 1,贴上 标签 a,就是 地址,引用
b =a #把 a的地址 也赋给 b ,所以,此内存 等于有了2 个标签 a,b ,但是 实际是同一个地址;
在 Python 中,可变类型的数据包括:
列表(list):可以通过增删改查操作改变其内容
字典(dict):可以通过增删改查操作改变其内容
集合(set):可以通过增删操作改变其内容
Bytearray:可以通过修改操作改变其内容
需要注意的是,不可变类型的数据有:数字(int、float)、字符串(str)、元组(tuple)。
wideband
发表于 2023-6-17 07:41:54
super() 是一个内置函数,可以用于调用父类的方法。
它能够访问父类中的属性和方法,在子类中可以对其进行修改或扩展。
class Parent:
def __init__(self, name):
self.name = name
def say_hello(self):
print("Hello, " + self.name)
class Child(Parent):
def say_hello(self):
super().say_hello()# 调用父类方法
print("Welcome!")
child = Child("Tom")
child.say_hello()
子类 Child 重写了父类 Parent 的 say_hello() 方法,并使用 super() 方法在子类中调用了父类的同名方法。这样,在子类中先输出 "Hello, Tom",再输出 "Welcome!"。