def test_building_cli_from_function(monkeypatch): monkeypatch.setattr(sys, 'argv', [ 'entry_point', '--command', 'run', '--path', 'file.txt', '--flag', '1', ]) params = {} def run(command: str, path: str = 'default.txt', flag: bool = False): nonlocal params params['command'] = command params['path'] = path params['flag'] = flag make_cli(run) assert len(params) == 3 assert params['command'] == 'run' assert params['path'] == 'file.txt' assert params['flag']
def test_function_signature_without_args(): def no_args(): pass spec = make_cli(no_args, spec_only=True) assert not spec
def test_function_signature_with_required_and_default_params(): def some_optional(first: int, second: bool = False): pass spec = make_cli(some_optional, spec_only=True) assert len(spec) == 2 assert spec[0] == {'name': 'first', 'type': int, 'required': True} assert spec[1] == {'name': 'second', 'type': bool, 'default': False}
def test_function_signature_without_annotations(): def no_annot(a, b=2.0, c=3): pass spec = make_cli(no_annot, spec_only=True) assert len(spec) == 3 assert spec[0] == {'name': 'a', 'type': str, 'required': True} assert spec[1] == {'name': 'b', 'type': float, 'default': 2.0} assert spec[2] == {'name': 'c', 'type': int, 'default': 3}
def test_function_signature_with_all_required_params(): def all_required(integer: int, floating: float, string: str): pass spec = make_cli(all_required, spec_only=True) assert len(spec) == 3 assert spec[0] == {'name': 'integer', 'type': int, 'required': True} assert spec[1] == {'name': 'floating', 'type': float, 'required': True} assert spec[2] == {'name': 'string', 'type': str, 'required': True}
def test_function_signature_with_default_params(): def all_optional(x: int = 1, y: int = 2, z: int = 3): pass spec = make_cli(all_optional, spec_only=True) assert len(spec) == 3 assert spec[0] == {'name': 'x', 'type': int, 'default': 1} assert spec[1] == {'name': 'y', 'type': int, 'default': 2} assert spec[2] == {'name': 'z', 'type': int, 'default': 3}
def test_printing_parameters_passed_info_function(monkeypatch): monkeypatch.setattr( sys, 'argv', ['entry_point', '--first', 'a', '--second', 'b', '--third', 'c']) expected_output = textwrap.dedent(''' invoked with parameters ----------------------- first=a second=b third=c ''') def run(first: str, second: str, third: str): pass buf = io.StringIO() with contextlib.redirect_stdout(buf): make_cli(run, show_params=True) captured = buf.getvalue() assert captured == expected_output[1:] # exclude first newline char
anime += [(0, 1)] playlist = open(m3u, 'w') playlist.write('#EXTM3U\n\n\n') playlist.mode = 'a' for url, title in anime[start - 1:end]: if url == 0: break html = openurl(url).text title = title.replace('–', '-') video = findall(r"src='(.+?)'", html)[1] print(url, title) print() print(video) print('\n') playlist.write(f'#EXTINF:-1, ANIM3U - {title}\n{video}\n\n') playlist.close() make_cli(anim3u)
import importlib import sys from ancli import make_cli try: entry_point = sys.argv[1] except IndexError: print('Error: no entry point name provided!') sys.exit(1) try: module_path, function_name = entry_point.split(':') except ValueError: print('Error: entry point name should have format a.b.c:function') sys.exit(1) mod = importlib.import_module(module_path) try: func = getattr(mod, function_name) except AttributeError: print(f'Error: function \'{function_name}\' is not found') sys.exit(1) sys.argv = [sys.argv[0]] + sys.argv[2:] make_cli(func)
bar.update(1) vis.line(X=[iteration], Y=[avg_loss], win='loss', name='avg_loss', update='append') val_dl = dataset['valid'] n = len(val_dl) model.eval() with torch.no_grad(): matches = [] with tqdm(total=n) as bar: for batch in val_dl: x = batch['features'].to(device) y = batch['targets'].to(device) out = model(x) y_pred = out.softmax(dim=1).argmax(dim=1) matched = (y == y_pred).detach().cpu().numpy().tolist() matches.extend(matched) bar.update(1) acc = np.mean(matches) vis.line(X=[epoch], Y=[acc], win='acc', name='val_acc', update='append') print(f'validation accuracy: {acc:2.2%}') acc_str = str(int(round(acc * 10_000, 0))) path = os.path.join(logdir, f'train.{epoch}.{acc_str}.pth') torch.save(model.state_dict(), path) if __name__ == '__main__': if not is_notebook(): ancli.make_cli(train)