Best Python code snippet using molecule_python
config.py
Source:config.py
1from configparser import ConfigParser2import os3class myconf(ConfigParser):4 def __init__(self, defaults=None):5 ConfigParser.__init__(self, defaults=defaults)6 def optionxform(self, optionstr):7 return optionstr8class Configurable(myconf):9 def __init__(self, config_file):10 config = myconf()11 config.read(config_file)12 self._config = config13 print('Loaded config file sucessfully.')14 for section in config.sections():15 for k, v in config.items(section):16 print(k, ":", v)17 # if not os.path.isdir(self.save_dir):18 # os.mkdir(self.save_dir)19 config.write(open(config_file, 'w'))20 # Data21 @property22 def word_Embedding(self):23 return self._config.getboolean('Data', 'word_Embedding')24 @property25 def freq_1_unk(self):26 return self._config.getboolean('Data', 'freq_1_unk')27 @property28 def word_Embedding_Path(self):29 return self._config.get('Data', 'word_Embedding_Path')30 @property31 def datafile_path(self):32 return self._config.get('Data', 'datafile_path')33 @property34 def name_trainfile(self):35 return self._config.get('Data', 'name_trainfile')36 @property37 def name_devfile(self):38 return self._config.get('Data', 'name_devfile')39 @property40 def name_testfile(self):41 return self._config.get('Data', 'name_testfile')42 @property43 def min_freq(self):44 return self._config.getint('Data', 'min_freq')45 @property46 def word_data(self):47 return self._config.getboolean('Data', 'word_data')48 @property49 def char_data(self):50 return self._config.getboolean('Data', 'char_data')51 @property52 def shuffle(self):53 return self._config.getboolean('Data', 'shuffle')54 @property55 def epochs_shuffle(self):56 return self._config.getboolean('Data', 'epochs_shuffle')57 @property58 def FIVE_CLASS_TASK(self):59 return self._config.getboolean('Data', 'FIVE_CLASS_TASK') \60 @property61 def TWO_CLASS_TASK(self):62 return self._config.getboolean('Data', 'TWO_CLASS_TASK')63 # Save64 @property65 def snapshot(self):66 value = self._config.get('Save', 'snapshot')67 if value == "None" or value == "none":68 return None69 else:70 return value71 @property72 def predict(self):73 value = self._config.get('Save', 'predict')74 if value == "None" or value == "none":75 return None76 else:77 return value78 @property79 def test(self):80 return self._config.getboolean('Save', 'test')81 @property82 def save_dir(self):83 return self._config.get('Save', 'save_dir')84 @save_dir.setter85 def save_dir(self, value):86 self._config.set('Save', 'save_dir', str(value))87 @property88 def rm_model(self):89 return self._config.getboolean('Save', 'rm_model')90 # Model91 @property92 def static(self):93 return self._config.getboolean("Model", "static")94 @property95 def wide_conv(self):96 return self._config.getboolean("Model", "wide_conv")97 @property98 def CNN(self):99 return self._config.getboolean("Model", "CNN")100 @property101 def HighWay_CNN(self):102 return self._config.getboolean("Model", "HighWay_CNN")103 @property104 def CNN_MUI(self):105 return self._config.getboolean("Model", "CNN_MUI")106 @property107 def DEEP_CNN(self):108 return self._config.getboolean("Model", "DEEP_CNN")109 @property110 def DEEP_CNN_MUI(self):111 return self._config.getboolean("Model", "DEEP_CNN_MUI")112 @property113 def LSTM(self):114 return self._config.getboolean("Model", "LSTM")115 @property116 def GRU(self):117 return self._config.getboolean("Model", "GRU")118 @property119 def BiLSTM(self):120 return self._config.getboolean("Model", "BiLSTM")121 @property122 def BiLSTM_1(self):123 return self._config.getboolean("Model", "BiLSTM_1")124 @property125 def HighWay_BiLSTM_1(self):126 return self._config.getboolean("Model", "HighWay_BiLSTM_1")127 @property128 def CNN_LSTM(self):129 return self._config.getboolean("Model", "CNN_LSTM")130 @property131 def CNN_BiLSTM(self):132 return self._config.getboolean("Model", "CNN_BiLSTM")133 @property134 def CLSTM(self):135 return self._config.getboolean("Model", "CLSTM")136 @property137 def CBiLSTM(self):138 return self._config.getboolean("Model", "CBiLSTM")139 @property140 def CGRU(self):141 return self._config.getboolean("Model", "CGRU")142 @property143 def BiGRU(self):144 return self._config.getboolean("Model", "BiGRU")145 @property146 def CNN_BiGRU(self):147 return self._config.getboolean("Model", "CNN_BiGRU")148 @property149 def embed_dim(self):150 return self._config.getint("Model", "embed_dim")151 @property152 def lstm_hidden_dim(self):153 return self._config.getint("Model", "lstm_hidden_dim")154 @property155 def lstm_num_layers(self):156 return self._config.getint("Model", "lstm_num_layers")157 @property158 def batch_normalizations(self):159 return self._config.getboolean("Model", "batch_normalizations")160 @property161 def bath_norm_momentum(self):162 return self._config.getfloat("Model", "bath_norm_momentum")163 @property164 def batch_norm_affine(self):165 return self._config.getboolean("Model", "batch_norm_affine")166 @property167 def dropout(self):168 return self._config.getfloat("Model", "dropout")169 @property170 def dropout_embed(self):171 return self._config.getfloat("Model", "dropout_embed")172 @property173 def max_norm(self):174 value = self._config.get("Model", "max_norm")175 if value == "None" or value == "none":176 return None177 else:178 return value179 @property180 def clip_max_norm(self):181 return self._config.getint("Model", "clip_max_norm")182 @property183 def kernel_num(self):184 return self._config.getint("Model", "kernel_num")185 @property186 def kernel_sizes(self):187 value = self._config.get("Model", "kernel_sizes")188 # print(list(value))189 value = [int(k) for k in list(value) if k != ","]190 return value191 @kernel_sizes.setter192 def kernel_sizes(self, value):193 self._config.set("Model", "kernel_sizes", str(value))194 @property195 def init_weight(self):196 return self._config.getboolean("Model", "init_weight")197 @property198 def init_weight_value(self):199 return self._config.getfloat("Model", "init_weight_value")200 # Optimizer201 @property202 def learning_rate(self):203 return self._config.getfloat("Optimizer", "learning_rate")204 @property205 def Adam(self):206 return self._config.getboolean("Optimizer", "Adam")207 @property208 def SGD(self):209 return self._config.getboolean("Optimizer", "SGD")210 @property211 def Adadelta(self):212 return self._config.getboolean("Optimizer", "Adadelta")213 @property214 def momentum_value(self):215 return self._config.getfloat("Optimizer", "optim_momentum_value")216 @property217 def weight_decay(self):218 return self._config.getfloat("Optimizer", "weight_decay")219 # Train220 @property221 def num_threads(self):222 return self._config.getint("Train", "num_threads")223 @property224 def device(self):225 return self._config.getint("Train", "device")226 @property227 def cuda(self):228 return self._config.getboolean("Train", "cuda")229 @property230 def epochs(self):231 return self._config.getint("Train", "epochs")232 @property233 def batch_size(self):234 return self._config.getint("Train", "batch_size")235 @property236 def log_interval(self):237 return self._config.getint("Train", "log_interval")238 @property239 def test_interval(self):240 return self._config.getint("Train", "test_interval")241 @property242 def save_interval(self):...
configdict_test.py
Source:configdict_test.py
1# Copyright 2018 The TensorFlow Authors.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14"""Tests for config_util.configdict."""15from __future__ import absolute_import16from __future__ import division17from __future__ import print_function18from absl.testing import absltest19from tf_util import configdict20class ConfigDictTest(absltest.TestCase):21 def setUp(self):22 super(ConfigDictTest, self).setUp()23 self._config = configdict.ConfigDict({24 "int": 1,25 "float": 2.0,26 "bool": True,27 "str": "hello",28 "nested": {29 "int": 3,30 },31 "double_nested": {32 "a": {33 "int": 3,34 },35 "b": {36 "float": 4.0,37 }38 }39 })40 def testAccess(self):41 # Simple types.42 self.assertEqual(1, self._config.int)43 self.assertEqual(1, self._config["int"])44 self.assertEqual(2.0, self._config.float)45 self.assertEqual(2.0, self._config["float"])46 self.assertTrue(self._config.bool)47 self.assertTrue(self._config["bool"])48 self.assertEqual("hello", self._config.str)49 self.assertEqual("hello", self._config["str"])50 # Single nested config.51 self.assertEqual(3, self._config.nested.int)52 self.assertEqual(3, self._config["nested"].int)53 self.assertEqual(3, self._config.nested["int"])54 self.assertEqual(3, self._config["nested"]["int"])55 # Double nested config.56 self.assertEqual(3, self._config["double_nested"].a.int)57 self.assertEqual(3, self._config["double_nested"]["a"].int)58 self.assertEqual(3, self._config["double_nested"].a["int"])59 self.assertEqual(3, self._config["double_nested"]["a"]["int"])60 self.assertEqual(4.0, self._config.double_nested.b.float)61 self.assertEqual(4.0, self._config.double_nested["b"].float)62 self.assertEqual(4.0, self._config.double_nested.b["float"])63 self.assertEqual(4.0, self._config.double_nested["b"]["float"])64 # Nonexistent parameters.65 with self.assertRaises(AttributeError):66 _ = self._config.nonexistent67 with self.assertRaises(KeyError):68 _ = self._config["nonexistent"]69 def testSetAttribut(self):70 # Overwrite existing simple type.71 self._config.int = 4072 self.assertEqual(40, self._config.int)73 # Overwrite existing nested simple type.74 self._config.nested.int = 4075 self.assertEqual(40, self._config.nested.int)76 # Overwrite existing nested config.77 self._config.double_nested.a = {"float": 50.0}78 self.assertIsInstance(self._config.double_nested.a, configdict.ConfigDict)79 self.assertEqual(50.0, self._config.double_nested.a.float)80 self.assertNotIn("int", self._config.double_nested.a)81 # Set new simple type.82 self._config.int_2 = 1083 self.assertEqual(10, self._config.int_2)84 # Set new nested simple type.85 self._config.nested.int_2 = 2086 self.assertEqual(20, self._config.nested.int_2)87 # Set new nested config.88 self._config.double_nested.c = {"int": 30}89 self.assertIsInstance(self._config.double_nested.c, configdict.ConfigDict)90 self.assertEqual(30, self._config.double_nested.c.int)91 def testSetItem(self):92 # Overwrite existing simple type.93 self._config["int"] = 4094 self.assertEqual(40, self._config.int)95 # Overwrite existing nested simple type.96 self._config["nested"].int = 4097 self.assertEqual(40, self._config.nested.int)98 self._config.nested["int"] = 5099 self.assertEqual(50, self._config.nested.int)100 # Overwrite existing nested config.101 self._config.double_nested["a"] = {"float": 50.0}102 self.assertIsInstance(self._config.double_nested.a, configdict.ConfigDict)103 self.assertEqual(50.0, self._config.double_nested.a.float)104 self.assertNotIn("int", self._config.double_nested.a)105 # Set new simple type.106 self._config["int_2"] = 10107 self.assertEqual(10, self._config.int_2)108 # Set new nested simple type.109 self._config.nested["int_2"] = 20110 self.assertEqual(20, self._config.nested.int_2)111 self._config.nested["int_3"] = 30112 self.assertEqual(30, self._config.nested.int_3)113 # Set new nested config.114 self._config.double_nested["c"] = {"int": 30}115 self.assertIsInstance(self._config.double_nested.c, configdict.ConfigDict)116 self.assertEqual(30, self._config.double_nested.c.int)117 def testDelete(self):118 # Simple types.119 self.assertEqual(1, self._config.int)120 del self._config.int121 with self.assertRaises(AttributeError):122 _ = self._config.int123 with self.assertRaises(KeyError):124 _ = self._config["int"]125 self.assertEqual(2.0, self._config["float"])126 del self._config["float"]127 with self.assertRaises(AttributeError):128 _ = self._config.float129 with self.assertRaises(KeyError):130 _ = self._config["float"]131 # Nested config.132 self.assertEqual(3, self._config.nested.int)133 del self._config.nested134 with self.assertRaises(AttributeError):135 _ = self._config.nested136 with self.assertRaises(KeyError):137 _ = self._config["nested"]138if __name__ == "__main__":...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!