Best Python code snippet using autotest_python
callback_list.py
Source:callback_list.py
...86 self._delta_t_batch = 0.087 self._delta_ts = collections.defaultdict(88 lambda: collections.deque([], maxlen=self._queue_length)89 )90 def _process_logs(self, logs):91 """Turns tensors into numpy arrays or Python scalars."""92 if logs:93 return logs94 return {}95 def append(self, callback):96 self.callbacks.append(callback)97 def set_params(self, params):98 self.params = params99 for callback in self.callbacks:100 callback.set_params(params)101 def set_model(self, model):102 self.model = model103 if self._history:104 model.history = self._history105 for callback in self.callbacks:106 callback.set_model(model)107 def _call_batch_hook(self, mode, hook, batch, logs=None):108 """Helper function for all batch_{begin | end} methods."""109 if not self.callbacks:110 return111 hook_name = "on_{mode}_batch_{hook}".format(mode=mode, hook=hook)112 if hook == "begin":113 self._t_enter_batch = time.time()114 if hook == "end":115 # Batch is ending, calculate batch time.116 self._delta_t_batch = time.time() - self._t_enter_batch117 logs = logs or {}118 t_before_callbacks = time.time()119 for callback in self.callbacks:120 batch_hook = getattr(callback, hook_name)121 batch_hook(batch, logs)122 self._delta_ts[hook_name].append(time.time() - t_before_callbacks)123 # delta_t_median = np.median(self._delta_ts[hook_name])124 # if (125 # self._delta_t_batch > 0.0126 # and delta_t_median > 0.95 * self._delta_t_batch127 # and delta_t_median > 0.1128 # ):129 # logging.warning(130 # "Method (%s) is slow compared "131 # "to the batch update (%f). Check your callbacks.",132 # hook_name,133 # delta_t_median,134 # )135 def _call_begin_hook(self, mode):136 """Helper function for on_{train|test|predict}_begin methods."""137 if mode == ModeKeys.TRAIN:138 self.on_train_begin()139 elif mode == ModeKeys.TEST:140 self.on_test_begin()141 else:142 self.on_predict_begin()143 def _call_end_hook(self, mode):144 """Helper function for on_{train|test|predict}_end methods."""145 if mode == ModeKeys.TRAIN:146 self.on_train_end()147 elif mode == ModeKeys.TEST:148 self.on_test_end()149 else:150 self.on_predict_end()151 def on_batch_begin(self, batch, logs=None):152 if self._should_call_train_batch_hooks:153 logs = self._process_logs(logs)154 self._call_batch_hook(ModeKeys.TRAIN, "begin", batch, logs=logs)155 def on_batch_end(self, batch, logs=None):156 if self._should_call_train_batch_hooks:157 logs = self._process_logs(logs)158 self._call_batch_hook(ModeKeys.TRAIN, "end", batch, logs=logs)159 def on_epoch_begin(self, epoch, logs=None):160 """Calls the `on_epoch_begin` methods of its callbacks.161 This function should only be called during TRAIN mode.162 Arguments:163 epoch: integer, index of epoch.164 logs: dict. Currently no data is passed to this argument for this method165 but that may change in the future.166 """167 logs = self._process_logs(logs)168 for callback in self.callbacks:169 callback.on_epoch_begin(epoch, logs)170 self._reset_batch_timing()171 def on_epoch_end(self, epoch, logs=None):172 """Calls the `on_epoch_end` methods of its callbacks.173 This function should only be called during TRAIN mode.174 Arguments:175 epoch: integer, index of epoch.176 logs: dict, metric results for this training epoch, and for the177 validation epoch if validation is performed. Validation result keys178 are prefixed with `val_`.179 """180 logs = self._process_logs(logs)181 for callback in self.callbacks:182 callback.on_epoch_end(epoch, logs)183 def on_train_batch_begin(self, batch, logs=None):184 """Calls the `on_train_batch_begin` methods of its callbacks.185 Arguments:186 batch: integer, index of batch within the current epoch.187 logs: dict. Has keys `batch` and `size` representing the current batch188 number and the size of the batch.189 """190 # TODO(b/150629188): Make ProgBarLogger callback not use batch hooks191 # when verbose != 1192 if self._should_call_train_batch_hooks:193 logs = self._process_logs(logs)194 self._call_batch_hook(ModeKeys.TRAIN, "begin", batch, logs=logs)195 def on_train_batch_end(self, batch, logs=None):196 """Calls the `on_train_batch_end` methods of its callbacks.197 Arguments:198 batch: integer, index of batch within the current epoch.199 logs: dict. Metric results for this batch.200 """201 if self._should_call_train_batch_hooks:202 logs = self._process_logs(logs)203 self._call_batch_hook(ModeKeys.TRAIN, "end", batch, logs=logs)204 def on_test_batch_begin(self, batch, logs=None):205 """Calls the `on_test_batch_begin` methods of its callbacks.206 Arguments:207 batch: integer, index of batch within the current epoch.208 logs: dict. Has keys `batch` and `size` representing the current batch209 number and the size of the batch.210 """211 if self._should_call_test_batch_hooks:212 logs = self._process_logs(logs)213 self._call_batch_hook(ModeKeys.TEST, "begin", batch, logs=logs)214 def on_test_batch_end(self, batch, logs=None):215 """Calls the `on_test_batch_end` methods of its callbacks.216 Arguments:217 batch: integer, index of batch within the current epoch.218 logs: dict. Metric results for this batch.219 """220 if self._should_call_test_batch_hooks:221 logs = self._process_logs(logs)222 self._call_batch_hook(ModeKeys.TEST, "end", batch, logs=logs)223 def on_predict_batch_begin(self, batch, logs=None):224 """Calls the `on_predict_batch_begin` methods of its callbacks.225 Arguments:226 batch: integer, index of batch within the current epoch.227 logs: dict. Has keys `batch` and `size` representing the current batch228 number and the size of the batch.229 """230 if self._should_call_predict_batch_hooks:231 logs = self._process_logs(logs)232 self._call_batch_hook(ModeKeys.PREDICT, "begin", batch, logs=logs)233 def on_predict_batch_end(self, batch, logs=None):234 """Calls the `on_predict_batch_end` methods of its callbacks.235 Arguments:236 batch: integer, index of batch within the current epoch.237 logs: dict. Metric results for this batch.238 """239 if self._should_call_predict_batch_hooks:240 logs = self._process_logs(logs)241 self._call_batch_hook(ModeKeys.PREDICT, "end", batch, logs=logs)242 def on_train_begin(self, logs=None):243 """Calls the `on_train_begin` methods of its callbacks.244 Arguments:245 logs: dict. Currently no data is passed to this argument for this method246 but that may change in the future.247 """248 logs = self._process_logs(logs)249 for callback in self.callbacks:250 callback.on_train_begin(logs)251 def on_train_end(self, logs=None):252 """Calls the `on_train_end` methods of its callbacks.253 Arguments:254 logs: dict. Currently no data is passed to this argument for this method255 but that may change in the future.256 """257 logs = self._process_logs(logs)258 for callback in self.callbacks:259 callback.on_train_end(logs)260 def on_test_begin(self, logs=None):261 """Calls the `on_test_begin` methods of its callbacks.262 Arguments:263 logs: dict. Currently no data is passed to this argument for this method264 but that may change in the future.265 """266 logs = self._process_logs(logs)267 for callback in self.callbacks:268 callback.on_test_begin(logs)269 def on_test_end(self, logs=None):270 """Calls the `on_test_end` methods of its callbacks.271 Arguments:272 logs: dict. Currently no data is passed to this argument for this method273 but that may change in the future.274 """275 logs = self._process_logs(logs)276 for callback in self.callbacks:277 callback.on_test_end(logs)278 def on_predict_begin(self, logs=None):279 """Calls the 'on_predict_begin` methods of its callbacks.280 Arguments:281 logs: dict. Currently no data is passed to this argument for this method282 but that may change in the future.283 """284 logs = self._process_logs(logs)285 for callback in self.callbacks:286 callback.on_predict_begin(logs)287 def on_predict_end(self, logs=None):288 """Calls the `on_predict_end` methods of its callbacks.289 Arguments:290 logs: dict. Currently no data is passed to this argument for this method291 but that may change in the future.292 """293 logs = self._process_logs(logs)294 for callback in self.callbacks:295 callback.on_predict_end(logs)296 def __iter__(self):...
fixpatches.py
Source:fixpatches.py
...40 x = t.numpy()41 return x.item() if np.ndim(x) == 0 else x42 return t # Don't turn ragged or sparse tensors to NumPy.43 return nest.map_structure(_to_single_numpy_or_python_type, tensors)44 def _process_logs(self, logs):45 """Turns tensors into numpy arrays or Python scalars if necessary."""46 if logs is None:47 return {}48 return tf_utils.sync_to_numpy_or_python_type(logs)49 def _call_batch_hook_helper(self, hook_name, batch, logs):50 """Helper function for `on_*_batch_*` methods."""51 if self._check_timing:52 start_time = time.time()53 logs = self._process_logs(logs)54 for callback in self.callbacks:55 hook = getattr(callback, hook_name)56 hook(batch, logs)57 if self._check_timing:58 if hook_name not in self._hook_times:59 self._hook_times[hook_name] = []60 self._hook_times[hook_name].append(time.time() - start_time)61 def on_epoch_end(self: typing.Any, epoch: int, logs: dict = None) -> None:62 """63 Calls the `on_epoch_end` methods of its callbacks.64 This function should only be called during TRAIN mode.65 :param epoch: Integer, index of epoch.66 :param logs: Dict, metric results for this training epoch, and for67 the validation epoch if validation is performed. Validation68 result keys are prefixed with `val_`.69 """70 logs = self._process_logs(logs)71 for callback in self.callbacks:72 callback.on_epoch_end(epoch, logs)73 tf_utils.sync_to_numpy_or_python_type = sync_to_numpy_or_python_type74 callback_list_class = keras.callbacks.CallbackList75 callback_list_class._process_logs = _process_logs76 callback_list_class._call_batch_hook_helper = _call_batch_hook_helper...
logs.py
Source:logs.py
1from typing import List2from e2e.dockertest.file_system import FileSystem3def process_logs(docker_container, file_system: FileSystem) -> List[str]:4 return _process_logs(docker_container, file_system)5def _process_logs(container, file_system: FileSystem):6 actual_logs = container.logs()7 encoded = actual_logs.decode('utf-8')8 split = encoded.split('\n')9 logs = _remove_cat_logs(split, file_system)10 return logs11def _remove_cat_logs(current_logs: List[str], file_system: FileSystem):12 line_number_of_start_cat_logs = []13 line_number_of_end_cat_logs = []14 final_logs = current_logs15 for index, line in enumerate(current_logs, start=0):16 if line.startswith('start cat'):17 line_number_of_start_cat_logs.append(index)18 if line.startswith('end cat'):19 line_number_of_end_cat_logs.append(index)...
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!!