Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-shorten代码_Python_Python 3.x_Python 2.7 - Fatal编程技术网

Python-shorten代码

Python-shorten代码,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,我有一个名为string\u constants的文件,看起来像这样- class ModelEntityKeys: MODEL_ENTITY_DATA = 'model_entity_data' NETWORK_NAME = 'network_name' MODEL_FACTORS= 'model_factors' CLASSES= 'classes' COEF = 'coef_' INTERCEPT = 'intercept_' N_I

我有一个名为
string\u 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]
dictionary
self.data\u all\u entity\u 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"
    }
  }
}

太长,会破坏可读性。是否有缩短此行的方法?

导入时,可以先缩短
ModelEntityKeys

from xai.string_constants import ModelEntityKeys as mek
然后将alias
self.data\u all\u entity\u dict
缩写为:

d = self.data_all_entity_dict 
self.intercept = d[mek.MODEL_ENTITY_DATA][mek.MODEL_FACTORS][mek.INTERCEPT]
但实际上,我要做的是将所有关于“data\u all\u entity\u dict”结构的知识保存在一个地方,并提供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
然后


导入时,可以先缩短
ModelEntityKeys

from xai.string_constants import ModelEntityKeys as mek
然后将alias
self.data\u all\u entity\u dict
缩写为:

d = self.data_all_entity_dict 
self.intercept = d[mek.MODEL_ENTITY_DATA][mek.MODEL_FACTORS][mek.INTERCEPT]
但实际上,我要做的是将所有关于“data\u all\u entity\u dict”结构的知识保存在一个地方,并提供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
然后


在括号内可以有新行,而不需要任何反斜杠或其他难看的额外装饰
dict[
(换行)
ModelEntityKeys.MODEL\u ENTITY\u DATA
(换行)etcIs有一种方法可以缩短它。我理解你的建议。只是我想使用更少的字符。在
string\u常量
文件或导入中有什么我可以更改的吗?
ModelEntityKeys.MODEL\u ENTITY\u数据。MODEL\u因子
与你的
ModelEntityKeys
定义不匹配-应该是
ModelEntityKeys.MODEL\u FACTOR
tempvar=self.data\u all\u entity\u dict[ModelEntityKeys.MODEL\u entity\u data];self.intercept=tempvar[ModelEntityKeys.MODEL\u FACTORS][ModelEntityKeys.MODEL\u entity\u data.intercept]
data\u key=ModelEntityKeys.MODEL\u ENTITY\u data;factor\u key=ModelEntityKeys.MODEL\u FACTORS;self.intercept=self.data\u all\u ENTITY\u dict[data\u key][factor\u key][ModelEntityKeys.MODEL\u ENTITY\u data\u intercept]
括号内可以有换行,而不需要任何反斜杠或其他难看的额外修饰。
dict[
(新行)
ModelEntityKeys.MODEL\u ENTITY\u数据
(换行)etcIs有一种方法可以缩短它。我理解你的建议。只是我想使用更少的字符。在
string\u常量
文件或导入中有什么我可以更改的吗?
ModelEntityKeys.MODEL\u ENTITY\u数据。MODEL\u因子
与你的
ModelEntityKeys
定义不匹配-应该是
ModelEntityKeys.MODEL\u FACTOR
tempvar=self.data\u all\u entity\u dict[ModelEntityKeys.MODEL\u entity\u data];self.intercept=tempvar[ModelEntityKeys.MODEL\u FACTORS][ModelEntityKeys.MODEL\u entity\u data.intercept]
data\u key=ModelEntityKeys.MODEL\u ENTITY\u data;factor\u key=ModelEntityKeys.MODEL\u FACTORS;self.intercept=self.data\u all\u ENTITY\u dict[data\u key][factor\u key][ModelEntityKeys.MODEL\u ENTITY\u data\u intercept]