Python-缩短代码

时间:2017-11-29 10:52:45

标签: python python-3.x python-2.7

我有一个名为string_constants的文件,看起来像这样 -

class ModelEntityKeys:
    MODEL_ENTITY_DATA = 'model_entity_data'
    NETWORK_NAME = 'network_name'
    MODEL_FACTORS= 'model_factors'
    CLASSES= 'classes'
    COEF = 'coef_'
    INTERCEPT = 'intercept_'
    N_ITER = 'n_iter_'
    VARIABLES = 'variables'
    CATG_VARIABLES = "catg_variables"
    CONT_VARIABLES = "cont_variables"
    LABEL_NAME = "label_name"
    TEST_COST = "test_cost"
    TEST_ACCURACY = "test_accuracy"
    TEST_TIME_ELAPSED = "test_time_elapsed"
    EPOCH_TIME_ELAPSED = "epoch_time_elapsed"
    EPOCH_ACCURACY = "epoch_accuracy"
    EPOCH_COST = "epoch_cost"
    GRAPH_SAVE_PATH = "graph_save_path"
    DATA_SAVE_PATH = "data_save_path"
    ML_SAVE_PATH = "ml_save_path"
    DL_SAVE_PATH = "dl_save_path"
    MODEL_NAME = "model_name"
    TIMESTAMP = "timestamp"
    COST = "cost"

其他类似的课程很少。我正在导入这些字符串以将它们作为字典的键传递,这就是我所拥有的 -

from xai.string_constants import ModelEntityKeys
# omitting some code
# ...
    self.intercept = self.data_all_entity_dict[ModelEntityKeys.MODEL_ENTITY_DATA][ModelEntityKeys.MODEL_FACTORS][ModelEntityKeys.INTERCEPT]

字典self.data_all_entity_dict是一个嵌套字典,看起来像这样 -

{
  "model_entity_data": {
    "network_name": "sample_2_logistic_network",
    "model_name": "sample_2_logistic_model",
    "timestamp": "20171129_142512",
    "cost": "mse",
    "path": {
      "dl_save_path": "/saves/dl/",
      "ml_save_path": "/saves/ml/",
      "data_save_path": "/data/",
      "graph_save_path": "/graphs/tf/"
    },
    "train_meta": {
      "epoch_cost": 0.10952380952380952,
      "epoch_accuracy": 0.8904761904761904,
      "epoch_time_elapsed": "0:00:00.002164"
    },
    "test_meta": {
      "test_cost": 0.13333333333333333,
      "test_accuracy": 0.8666666666666667,
      "test_time_elapsed": "0:00:00.000675"
    },
    "model_factors": {
      "classes_": [
        0.0,
        1.0
      ],
      "coef_": [
        [
          0.007875385355666441,
          8.192464586946051e-06,
          0.006161374233310335,
          -0.051444957788776335,
          0.00043294254544011014,
          0.00017207830816790075,
          -0.00020155122167492249
        ]
      ],
      "intercept_": [
        0.0004034696319330871
      ],
      "n_iter_": [
        10
      ],
      "variables": [
        "age",
        "income",
        "edu_yrs",
        "yrs_since_exercise",
        "security_label_<prefix>_A",
        "security_label_<prefix>_B",
        "security_label_<prefix>_C"
      ],
      "catg_variables": [
        "security_label"
      ],
      "cont_variables": [
        "age",
        "income",
        "edu_yrs",
        "yrs_since_exercise"
      ],
      "label_name": "prob"
    }
  }
}

问题在于使用

self.intercept = self.data_all_entity_dict[ModelEntityKeys.MODEL_ENTITY_DATA][ModelEntityKeys.MODEL_FACTORS][ModelEntityKeys.INTERCEPT]

太长并且会破坏可读性。有没有办法缩短这条线?

1 个答案:

答案 0 :(得分:3)

导入时,您可以先缩短ModelEntityKeys

from xai.string_constants import ModelEntityKeys as mek

然后将self.data_all_entity_dict别名更短的内容:

d = self.data_all_entity_dict 
self.intercept = d[mek.MODEL_ENTITY_DATA][mek.MODEL_FACTORS][mek.INTERCEPT]

但实际上会做的是保留关于&#34; data_all_entity_dict&#34;的所有知识。结构在一个地方并提供getter方法:

class ModelEntity:
   MODEL_ENTITY_DATA = 'model_entity_data'
   NETWORK_NAME = 'network_name'
   MODEL_FACTORS= 'model_factors'
   CLASSES= 'classes'
   # etc

  def __init__(self, data):
      self.data = data

  @property
  def model_entity_data(self):
      return self.data[self.MODEL_ENTITY_DATA]

  @property
  def model_factors(self):
      return self.model_entity_data[self.MODEL_FACTORS]

  @property
  def intercept(self):
      return self.model_factors[self.INTERCEPT]

  # etc

然后

from xai.string_constants import ModelEntity
entity = ModelEntity(self.data_all_entity_dict)
self.intercept = entity.intercept