Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Skip the prediction text
rest_of_line = rest_of_line[len(suggestion):]
# Add text to the predictor
self.__predictor.add(suggestion)
# If the suggestion doesn't match
else:
# Debug
end = 0
for i in range(min(len(rest_of_line), len(suggestion))):
if rest_of_line[i] != suggestion[i]:
end = i
break
if end > 0:
new_logs = logs + [(suggestion[:end], colors.Background.green),
(suggestion[end:], colors.Background.red),
("#", None)]
else:
new_logs = logs + [(suggestion[end:], colors.Background.red),
("#", None)]
logger.log_color(new_logs)
# Add the next character
self.__predictor.add(rest_of_line[0])
logs.append((rest_of_line[0], None))
rest_of_line = rest_of_line[1:]
# Add a new line
self.__predictor.add("\n")
def ansi_code(text: str, color: List[ANSICode] or ANSICode or None):
"""
### Add ansi color codes
"""
if color is None:
return text
elif type(color) is list:
return "".join(color) + f"{text}{colors.Reset}"
else:
return f"{color}{text}{colors.Reset}"
def _log_key_value(self, items: List[Tuple[any, any]]):
max_key_len = 0
for k, v in items:
max_key_len = max(max_key_len, len(str(k)))
count = 0
for k, v in items:
count += 1
spaces = " " * (max_key_len - len(str(k)))
self.log_color([(f"{spaces}{k}: ", None),
(str(v), colors.Style.bold)])
self.log_color([
("Total ", None),
(str(count), colors.Style.bold),
(" item(s)", None)])
def ansi_code(self, text: str, color: List[ANSICode] or ANSICode or None):
"""
### Add ansi color codes
"""
if color is None:
return text
elif type(color) is list:
return "".join(color) + f"{text}{colors.Reset}"
else:
return f"{color}{text}{colors.Reset}"
def _log_key_value(self, items: List[Tuple[any, any]], is_show_count=True):
max_key_len = 0
for k, v in items:
max_key_len = max(max_key_len, len(str(k)))
count = 0
for k, v in items:
count += 1
spaces = " " * (max_key_len - len(str(k)))
self.log_color([(f"{spaces}{k}: ", None),
(str(v), colors.Style.bold)])
if is_show_count:
self.log_color([
("Total ", None),
(str(count), colors.Style.bold),
(" item(s)", None)])
def list_trials(trials: List[Trial], logger: Logger):
for trial in trials:
commit_message = trial.commit_message.replace("\n", "\\n")
logger.log_color([
(trial.trial_date, colors.BrightColor.cyan),
(" ", None),
(trial.trial_time, colors.BrightColor.cyan),
(" : ", None),
(f"\"{trial.comment}\"", colors.BrightColor.orange),
(" commit=", None),
(f"\"{commit_message}\"", colors.BrightColor.purple),
])
def __log_looping_line(self):
parts = [(f"{self.global_step :8,}: ", colors.BrightColor.orange)]
parts += self.__loop.log_sections()
parts += self.__indicators_print
parts += self.__loop.log_progress()
self.log_color(parts, new_line=False)
if k in queues:
if len(queues[k]) == 0:
continue
v = np.mean(queues[k])
elif k in histograms:
if len(histograms[k]) == 0:
continue
v = np.mean(histograms[k])
else:
if len(scalars[k]) == 0:
continue
v = np.mean(scalars[k])
parts.append((f" {k}: ", None))
if self.is_color:
parts.append((f"{v :8,.2f}", colors.Style.bold))
else:
parts.append((f"{v :8,.2f}", None))
return parts
def print_info_and_check_repo(self):
"""
## 🖨 Print the experiment info and check git repo status
"""
self.logger.log_color([
(self.info.name, colors.Style.bold)
])
self.logger.log_color([
("\t", None),
(self.trial.comment, colors.BrightColor.cyan)
])
self.logger.log_color([
("\t", None),
("[dirty]" if self.trial.is_dirty else "[clean]", None),
(": ", None),
(f"\"{self.trial.commit_message.strip()}\"", colors.BrightColor.orange)
])
# Exit if git repository is dirty
if self.check_repo_dirty and self.trial.is_dirty:
self.logger.log("Cannot trial an experiment with uncommitted changes. ",
new_line=False)