使用 while 循环时如何将重复代码转换为函数
How to turn repeating code in to functions when a while loop is used
我有一些非常重复的 python 代码,我想将其转换为函数,但我不确定其中的逻辑,因为涉及 while 循环。
#Get initial tracks for playlist and save to JSON file
tracks_file_count = 0
tracks_file_name = f'{str(tracks_file_count)}_tracks.json'
tracks_file = open(tracks_file_name,'w+')
tracks_response = requests.request('GET',
f'{config.global_url}/users/{user}/playlists/{playlist_id}/tracks',
headers=headers
)
tracks_file.write(tracks_response.text)
tracks_file.close()
tracks_file = open(tracks_file_name,'r')
tracks = json.load(tracks_file)
#Save comma seperated track ID's to variable
id_dict = []
for i in tracks['items']:
track_id = i['track']['id']
id_dict.append(track_id)
id_comma = (','.join(id_dict))
#Get Audio Features
features_file_count = 0
features_file_name = f'{str(features_file_count)}_features.json'
features_file = open(features_file_name,'w+')
features_response = requests.request('GET',
f'{config.global_url}/audio-features/?ids={id_comma}',
headers=headers
)
features_file.write(features_response.text)
features_file.close()
features_file = open(features_file_name,'r')
features = json.load(features_file)
track_writer(tracks, playlist_id)
feature_writer(features, playlist_id)
next_url = tracks['next']
while next_url != None:
tracks_file_count += 1
tracks_file_name = f'{str(tracks_file_count)}_tracks.json'
tracks_file = open(tracks_file_name,'w+')
tracks_response = requests.request('GET', next_url,
headers=headers
)
tracks_file.write(tracks_response.text)
tracks_file.close()
tracks_file = open(tracks_file_name,'r')
tracks = json.load(tracks_file)
#Save comma seperated track ID's to variable
id_dict = []
for i in tracks['items']:
track_id = i['track']['id']
id_dict.append(track_id)
id_comma = (','.join(id_dict))
#Get Audio Features
features_file_count += 1
features_file_name = f'{str(features_file_count)}_features.json'
features_file = open(features_file_name,'w+')
features_response = requests.request('GET',
f'{config.global_url}/audio-features/?ids={id_comma}',
headers=headers
)
features_file.write(features_response.text)
features_file.close()
features_file = open(features_file_name,'r')
features = json.load(features_file)
track_writer(tracks, playlist_id)
feature_writer(features, playlist_id)
next_url = tracks['next']
Link 到原始 gist (编者注:缩进不是 100% 保留,换行).
在我的例子中,我首先用 0_ 命名我的文件,然后当条件不等于 'None' 我再次执行代码但增加文件名以便它以 1_ 开头等等在。 URL 也发生了变化。这个
显示唯一的变化是递增 1 和 URL。
只需取出公共部分并将它们分解为一个函数即可:
def repetitiveStuff(tracks_file_count, next_url, features_file_count)
tracks_file_name = f'{str(tracks_file_count)}_tracks.json'
tracks_file = open(tracks_file_name,'w+')
tracks_response = requests.request('GET', next_url, headers=headers)
tracks_file.write(tracks_response.text)
tracks_file.close()
tracks_file = open(tracks_file_name,'r')
tracks = json.load(tracks_file)
#Save comma seperated track ID's to variable
id_dict = []
for i in tracks['items']:
track_id = i['track']['id']
id_dict.append(track_id)
id_comma = (','.join(id_dict))
#Get Audio Features
features_file_name = f'{str(features_file_count)}_features.json'
features_file = open(features_file_name,'w+')
features_response = requests.request('GET',
f'{config.global_url}/audio-features/?ids={id_comma}',
headers=headers
)
features_file.write(features_response.text)
features_file.close()
features_file = open(features_file_name,'r')
features = json.load(features_file)
track_writer(tracks, playlist_id)
feature_writer(features, playlist_id)
return tracks['next']
#Get initial tracks for playlist and save to JSON file
next_url = repetitiveStuff(
0,
f'{config.global_url}/users/{user}/playlists/{playlist_id}/tracks',
0
)
tracks_file_count = 0
features_file_count = 0
while next_url != None:
tracks_file_count += 1
features_file_count += 1
next_url = repetitiveStuff(tracks_file_count, next_url, features_file_count)
请注意,现在标识符 tracks_file_count
、next_url
和 features_file_count
被使用 两次:作为函数的参数,以及作为全局变量变量/循环变量。 next_url
现在由创建 tracks
字典的函数返回。
我有一些非常重复的 python 代码,我想将其转换为函数,但我不确定其中的逻辑,因为涉及 while 循环。
#Get initial tracks for playlist and save to JSON file
tracks_file_count = 0
tracks_file_name = f'{str(tracks_file_count)}_tracks.json'
tracks_file = open(tracks_file_name,'w+')
tracks_response = requests.request('GET',
f'{config.global_url}/users/{user}/playlists/{playlist_id}/tracks',
headers=headers
)
tracks_file.write(tracks_response.text)
tracks_file.close()
tracks_file = open(tracks_file_name,'r')
tracks = json.load(tracks_file)
#Save comma seperated track ID's to variable
id_dict = []
for i in tracks['items']:
track_id = i['track']['id']
id_dict.append(track_id)
id_comma = (','.join(id_dict))
#Get Audio Features
features_file_count = 0
features_file_name = f'{str(features_file_count)}_features.json'
features_file = open(features_file_name,'w+')
features_response = requests.request('GET',
f'{config.global_url}/audio-features/?ids={id_comma}',
headers=headers
)
features_file.write(features_response.text)
features_file.close()
features_file = open(features_file_name,'r')
features = json.load(features_file)
track_writer(tracks, playlist_id)
feature_writer(features, playlist_id)
next_url = tracks['next']
while next_url != None:
tracks_file_count += 1
tracks_file_name = f'{str(tracks_file_count)}_tracks.json'
tracks_file = open(tracks_file_name,'w+')
tracks_response = requests.request('GET', next_url,
headers=headers
)
tracks_file.write(tracks_response.text)
tracks_file.close()
tracks_file = open(tracks_file_name,'r')
tracks = json.load(tracks_file)
#Save comma seperated track ID's to variable
id_dict = []
for i in tracks['items']:
track_id = i['track']['id']
id_dict.append(track_id)
id_comma = (','.join(id_dict))
#Get Audio Features
features_file_count += 1
features_file_name = f'{str(features_file_count)}_features.json'
features_file = open(features_file_name,'w+')
features_response = requests.request('GET',
f'{config.global_url}/audio-features/?ids={id_comma}',
headers=headers
)
features_file.write(features_response.text)
features_file.close()
features_file = open(features_file_name,'r')
features = json.load(features_file)
track_writer(tracks, playlist_id)
feature_writer(features, playlist_id)
next_url = tracks['next']
Link 到原始 gist (编者注:缩进不是 100% 保留,换行).
在我的例子中,我首先用 0_ 命名我的文件,然后当条件不等于 'None' 我再次执行代码但增加文件名以便它以 1_ 开头等等在。 URL 也发生了变化。这个
显示唯一的变化是递增 1 和 URL。
只需取出公共部分并将它们分解为一个函数即可:
def repetitiveStuff(tracks_file_count, next_url, features_file_count)
tracks_file_name = f'{str(tracks_file_count)}_tracks.json'
tracks_file = open(tracks_file_name,'w+')
tracks_response = requests.request('GET', next_url, headers=headers)
tracks_file.write(tracks_response.text)
tracks_file.close()
tracks_file = open(tracks_file_name,'r')
tracks = json.load(tracks_file)
#Save comma seperated track ID's to variable
id_dict = []
for i in tracks['items']:
track_id = i['track']['id']
id_dict.append(track_id)
id_comma = (','.join(id_dict))
#Get Audio Features
features_file_name = f'{str(features_file_count)}_features.json'
features_file = open(features_file_name,'w+')
features_response = requests.request('GET',
f'{config.global_url}/audio-features/?ids={id_comma}',
headers=headers
)
features_file.write(features_response.text)
features_file.close()
features_file = open(features_file_name,'r')
features = json.load(features_file)
track_writer(tracks, playlist_id)
feature_writer(features, playlist_id)
return tracks['next']
#Get initial tracks for playlist and save to JSON file
next_url = repetitiveStuff(
0,
f'{config.global_url}/users/{user}/playlists/{playlist_id}/tracks',
0
)
tracks_file_count = 0
features_file_count = 0
while next_url != None:
tracks_file_count += 1
features_file_count += 1
next_url = repetitiveStuff(tracks_file_count, next_url, features_file_count)
请注意,现在标识符 tracks_file_count
、next_url
和 features_file_count
被使用 两次:作为函数的参数,以及作为全局变量变量/循环变量。 next_url
现在由创建 tracks
字典的函数返回。