你会如何简化这一过程?

时间:2018-02-08 10:58:24

标签: python

好的,我有这种怪物,你会如何简化它?使用循环?

if season in range(1, 5):
        if episode_num == 1:
            if season == 1:
                subprocess.Popen([player_path, s1_path + episodes_s1[0]])
            elif season == 2:
                subprocess.Popen([player_path, s2_path + episodes_s2[0]])
            elif season == 3:
                subprocess.Popen([player_path, s3_path + episodes_s3[0]])
            else:
                subprocess.Popen([player_path, s4_path + episodes_s4[0]])
        elif episode_num == 2:
            if season == 1:
                subprocess.Popen([player_path, s1_path + episodes_s1[episode_num-1]])
            elif season == 2:
                subprocess.Popen([player_path, s2_path + episodes_s2[episode_num-1]])
            elif season == 3:
                subprocess.Popen([player_path, s3_path + episodes_s3[episode_num-1]])
            else:
                subprocess.Popen([player_path, s4_path + episodes_s4[episode_num-1]])
        elif episode_num == 3:
            if season == 1:
                subprocess.Popen([player_path, s1_path + episodes_s1[episode_num-1]])
            elif season == 2:
                subprocess.Popen([player_path, s2_path + episodes_s2[episode_num-1]])
            elif season == 3:
                subprocess.Popen([player_path, s3_path + episodes_s3[episode_num-1]])
            else:
                subprocess.Popen([player_path, s4_path + episodes_s4[episode_num-1]])
        elif episode_num == 4: ...
输入()获取

赛季 episode_num 整数

player_path sX_path 字符串;

episodes_sX 列表

6 个答案:

答案 0 :(得分:2)

更改数据结构可能更容易。

例如

program = {"s_paths":["1","2","3"],"s_episodes":[["1","2"],["1","2"]]}

现在您可以在没有所有IF声明的情况下访问它们

program['s_paths'][season - 1]
program['s_episodes'][season - 1][episode_num - 1]

所以,你只需返回

即可
subprocess.Popen([player_path, program['s_paths'][season - 1] + program['s_episodes'][season - 1][episode_num - 1]])

答案 1 :(得分:1)

如下:

season_path = "s{}_path".format(season)
episodes = [
    episodes_s1,
    episodes_s2,
    episodes_s3,
    episodes_s4,
]

subprocess.Popen([player_path, s1_path + episodes[season - 1][episode_num - 1]])

答案 2 :(得分:0)

好的,所以这是一个初步的想法,没有我身边的深刻理解。 您的代码可以转换为此(或多或少):

player_path = 'C:/myplayer'
s1_path = 'C:/files/s1'
s2_path = 'C:/files/s2'
s3_path = 'C:/files/s3'
s4_path = 'C:/files/s4'
episodes_s1 = ['ep01', 'ep02', 'ep03']
episodes_s2 = ['ep01', 'ep02', 'ep03']
episodes_s3 = ['ep01', 'ep02', 'ep03']
episodes_s4 = ['ep01', 'ep02', 'ep03']

season_paths = {k:v for k, v in enumerate([s1_path, s2_path, s3_path, s4_path], 1)}
# season_paths = {1: 'C:/files/s1', 2: 'C:/files/s2', 3: 'C:/files/s3', 4: 'C:/files/s4'}

season_episodes = {k:v for k, v in enumerate([episodes_s1, episodes_s2, episodes_s3, episodes_s4, ...], 1)}
# season_episodes = {1: ['ep01', 'ep02', 'ep03'],
#                    2: ['ep01', 'ep02', 'ep03'],
#                    3: ['ep01', 'ep02', 'ep03'],
#                    4: ['ep01', 'ep02', 'ep03']}

season = 1
episode_num = 2
print([player_path, season_paths[season], season_episodes[season][episode_num - 1])
# ['C:/myplayer', 'C:/files/s1', 'ep02']

if season in range(1,5):
    subprocess.Popen([player_path, season_paths[season], season_episodes[season][episode_num - 1]])

编辑:现在我在这里了解你的任务。用户提供季节和剧集,您已经列出了路径和剧集,并且您希望相应地启动播放器。

我会删除if season in range,直接调用子流程并检查这样的异常:

try:
    subprocess.Popen([player_path, season_paths[season], season_episodes[season][episode_num - 1]])
except IOError, ...:
    print("Could not start player {} with file {}".format(player_path,season_paths[season]))

答案 3 :(得分:0)

我这样建议你:

import os

seasons = [
        (s1_path, episodes_s1),
        (s2_path, episodes_s2),
        (s3_path, episodes_s3)
    ]

if season in range(0, len(seasons)):
    if episode_num in range(0, len(seasons[season][1])): # seasons[season][1]) is the seasons episodes

        # Create path via os.path.join, better way
        episode_path = os.path.join(seasons[season][0], seasons[season][1][episode_num - 1])
        subprocess.Popen([player_path, episode_path])

答案 4 :(得分:0)

这是我的建议。我试图在数据结构的定义中保持可读性和简单性。主要想法是将sX_pathepisodes_sX相关联(在我的案例列表中):

d = ['unused',
     [s1_path,episodes_s1],
     [s2_path,episodes_s2],
     [s3_path,episodes_s3],
     [s4_path,episodes_s4]]

if (season in range(1,5)) and (episodeNum in range(1,len(d[season][1])+1)):
    subprocess.Popen([player_path, d[season][0] + d[season][1][episodeNum-1]])

答案 5 :(得分:0)

您也可以使用包含数据的对象/结构数组。 并像Myserie [1] [3] .path

那样访问它