我有一些非常重复的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']
链接到原始gist (编者注:缩进不是100%保留,包裹行)。
在我的情况下,我首先用0_命名我的文件然后当条件不等于'None'时我再次执行代码但是增加文件名以便它以1_开始,依此类推。 URL也会更改。这个
显示唯一的更改是递增1和URL。
答案 0 :(得分:0)
只需取出公共部分并将它们分解为函数:
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
字典的函数返回。