【问题标题】:Global variable or switch statement to enable and disable comments用于启用和禁用注释的全局变量或 switch 语句
【发布时间】:2021-04-30 02:40:04
【问题描述】:

我正在寻找代码开头的全局变量或 switch 语句来启用或禁用特定的 cmets。 例如,我想在脚本的开头决定是否保存我的绘图:

fig, ax = plt.subplots()
ax.plot(np.arange(1, 91), scores_df["MaeCvMean"])
ax.fill_between(np.arange(1, 91), scores_df["MaeCvMean"] + scipy.stats.norm.ppf(0.975)*scores_df["MaeCvStd"],
                scores_df["MaeCvMean"] - scipy.stats.norm.ppf(0.975)*scores_df["MaeCvStd"], color="C0", alpha=0.15)
ax.set(xlabel="Minute", ylabel="Mae")
plt.xticks(np.arange(0, 100, step=10))
plt.legend(["Mae", "95% CI"], loc="upper left", fancybox=True, shadow=True)
plt.savefig("../Figures/cv_mae_ann.pdf", format="pdf", bbox_inches="tight")
plt.show()

在这个例子中,我想在序言中控制是否关闭 plt.savefig 命令。 我可以想象这样的 switch 语句:

COMMENTING = 0  # 0 to disable comments and 1 to enable comments

有没有办法做到这一点?

【问题讨论】:

    标签: python python-3.x switch-statement global-variables comments


    【解决方案1】:

    你的意思是这样的:

    DEBUG = True #flag to control comments
    
    fig, ax = plt.subplots()
    ax.plot(np.arange(1, 91), scores_df["MaeCvMean"])
    ax.fill_between(np.arange(1, 91), scores_df["MaeCvMean"] + scipy.stats.norm.ppf(0.975)*scores_df["MaeCvStd"],
                    scores_df["MaeCvMean"] - scipy.stats.norm.ppf(0.975)*scores_df["MaeCvStd"], color="C0", alpha=0.15)
    ax.set(xlabel="Minute", ylabel="Mae")
    plt.xticks(np.arange(0, 100, step=10))
    plt.legend(["Mae", "95% CI"], loc="upper left", fancybox=True, shadow=True)
    if DEBUG: #<-- add here
        plt.savefig("../Figures/cv_mae_ann.pdf", format="pdf", bbox_inches="tight")
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多