Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def mirror_line(s):
"""Mirror a line and its characters using translation"""
global _mirror_in, _mirror_out
s2 = s[::-1]
trans = string.maketrans(_mirror_in, _mirror_out)
return s2.translate(trans)
def docstring2lines(s):
return filter(None, s.split("\n"))
def argmax(iterable):
return max(enumerate(iterable), key=lambda x: x[1])
class tqdm_custommulti(tqdm):
"""
tqdm with nice customizable bar symbols!
"""
@staticmethod
def status_printer(file):
"""
Manage the printing and in-place updating of a multiline bar
"""
fp = file
fp_flush = getattr(fp, 'flush', lambda: None) # pragma: no cover
def fp_write(s):
fp.write(_unicode(s))
fp_flush()
def run_epoch(loader, model, criterion, optimizer, epoch, tag):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
end = time.time()
if optimizer:
current_lr = get_learning_rate(optimizer)[0]
else:
current_lr = None
tqdm_disable = bool(os.environ.get('TASK_NAME', '')) # for KakaoBrain
loader = tqdm(loader, disable=tqdm_disable)
loader.set_description('[%s %04d/%04d]' % (tag, epoch, args.epochs))
for i, (input, target) in enumerate(loader):
# measure data loading time
data_time.update(time.time() - end)
input, target = input.cuda(), target.cuda()
output = model(input)
loss = criterion(output, target)
# measure accuracy and record loss
losses.update(loss.item(), input.size(0))
if len(target.size()) == 1:
err1, err5 = accuracy(output.data, target, topk=(1, 5))
__author__ = {"github.com/": ["lrq3000"]}
__all__ = ['tqdm_custom', 'tcrange']
def mirror_chars(s):
"""Mirror characters using translation"""
import string
ins = '()<>[]\/{}bd'
outs = ')(><][/\}{db'
trans = string.maketrans(ins,outs)
return s.translate(trans)
class tqdm_custom(tqdm):
"""
tqdm with nice customizable bar symbols!
"""
def format_meter(self, n, total, elapsed, ncols=None, prefix='',
ascii=False, unit='it', unit_scale=False, rate=None,
bar_format=None):
"""
Return a string-based progress bar given some parameters
Parameters
----------
n : int
Number of finished iterations.
total : int
The expected total number of iterations. If meaningless (), only
basic progress statistics are displayed (no ETA).
'| {n_fmt}/{total_fmt} [{elapsed_str}<{remaining_str}, {rate_fmt}]'
Possible vars: bar, n, n_fmt, total, total_fmt, percentage,
rate, rate_fmt, elapsed, remaining, l_bar, r_bar, desc.
Returns
-------
out : Formatted meter and stats, ready to display.
"""
custom_symbols = None
# sanity check: total
if total and n > total:
total = None
format_interval = tqdm.format_interval
elapsed_str = format_interval(elapsed)
# if unspecified, attempt to use rate = average speed
# (we allow manual override since predicting time is an arcane art)
if rate is None and elapsed:
rate = n / elapsed
inv_rate = 1 / rate if (rate and (rate < 1)) else None
format_sizeof = tqdm.format_sizeof
rate_fmt = ((format_sizeof(inv_rate if inv_rate else rate)
if unit_scale else
'{0:5.2f}'.format(inv_rate if inv_rate else rate))
if rate else '?') \
+ ('s' if inv_rate else unit) + '/' + (unit if inv_rate else 's')
if unit_scale:
n_fmt = format_sizeof(n)