Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我怎样才能擦干这个密码_Python_Python 3.x_Telegram Bot_Dry_Python Telegram Bot - Fatal编程技术网

Python 我怎样才能擦干这个密码

Python 我怎样才能擦干这个密码,python,python-3.x,telegram-bot,dry,python-telegram-bot,Python,Python 3.x,Telegram Bot,Dry,Python Telegram Bot,我刚刚开始学习python 我创建了这个函数,但正如您在“if”语句中看到的,我复制了代码 我只将file\u id=filename.photo[-1]。file\u id更改为file\u id=filename.video.file\u id 使函数正常工作,但如何缩短此代码 谢谢你抽出时间 def create_post(文件名): 如果filename.content_type==“photo”: 文件\u id=filename.photo[-1]。文件\u id file=bot.

我刚刚开始学习python 我创建了这个函数,但正如您在“if”语句中看到的,我复制了代码 我只将
file\u id=filename.photo[-1]。file\u id
更改为
file\u id=filename.video.file\u id
使函数正常工作,但如何缩短此代码

谢谢你抽出时间

def create_post(文件名):
如果filename.content_type==“photo”:
文件\u id=filename.photo[-1]。文件\u id
file=bot.get\u文件(文件id)
下载的\u文件=bot.download\u文件(file.file\u路径)
打开(“image.jpg”,“wb”)作为新的_文件:
新建\u文件。写入(下载的\u文件)
image='image.jpg'
令牌=存储\文件\临时(图像)
标题={
“内容类型”:“多部分/表单数据”,
“接受”:“应用程序/json”,
“内容类型”:“应用程序/json”
}
文件D={
“title”:随机字符串(),
“安全”:“安全”,
“contentToken”:令牌
}
r=requests.post(url=url+“/posts/”,json=filesd,auth=(用户名,密码),
页眉=页眉)
打印(r.json())
返回r.json()
elif filename.content_type==“视频”:
file\u id=filename.video.file\u id
file=bot.get\u文件(文件id)
下载的\u文件=bot.download\u文件(file.file\u路径)
打开(“image.jpg”,“wb”)作为新的_文件:
新建\u文件。写入(下载的\u文件)
image='image.jpg'
令牌=存储\文件\临时(图像)
标题={
“内容类型”:“多部分/表单数据”,
“接受”:“应用程序/json”,
“内容类型”:“应用程序/json”
}
文件D={
“title”:随机字符串(),
“安全”:“安全”,
“contentToken”:令牌
}
r=requests.post(url=url+“/posts/”,json=filesd,auth=(用户名,密码),
页眉=页眉)
打印(r.json())
返回r.json()

一旦获得
文件\u id=filename.photo[-1].file\u id
之后,基本上就是完全相同的处理过程。所以只要把这一行放到
if-else
中,剩下的就放一个方法

if filename.content_type == 'photo':
        file_id = filename.photo[-1].file_id
elif filename.content_type == 'video':
        file_id = filename.video.file_id
//call some method with file_id where you do the rest of the work.
somemeaningful_function_name(file_id)


def somemeaningful_function_name(file_id):
    //work with file_id

你自己也说过干原则。当处理类似的事情时,只需找到重复的部分。根据经验,如果要多次重用同一代码块,只需将其放入函数中并使用即可

一旦获得
文件\u id=filename.photo[-1].file\u id
之后,基本上就是相同的处理过程。所以只要把这一行放到
if-else
中,剩下的就放一个方法

if filename.content_type == 'photo':
        file_id = filename.photo[-1].file_id
elif filename.content_type == 'video':
        file_id = filename.video.file_id
//call some method with file_id where you do the rest of the work.
somemeaningful_function_name(file_id)


def somemeaningful_function_name(file_id):
    //work with file_id

你自己也说过干原则。当处理类似的事情时,只需找到重复的部分。根据经验,如果要多次重用同一代码块,只需将其放入函数中并使用即可

这个问题更适合。@a_客人太晚了我的朋友这个问题更适合。@a_客人太晚了我的朋友