Пример #1
0
def all_country_info():
    allinfo = response.json()['Countries']
    table = Table(show_header=True, style="cyan")
    table.add_column("country", style="magenta", width=34, no_wrap=True)

    table.add_column("countrycode", style="cyan", width=20, no_wrap=True)
    table.add_column("NewConfirmed", style="white", width=20, no_wrap=True)
    table.add_column("TotalConfirmed", style="white", width=20, no_wrap=True)
    table.add_column("NewDeaths", style="red", width=20, no_wrap=True)
    table.add_column("TotalDeaths", style="red", width=20, no_wrap=True)
    table.add_column("NewRecovered", style="green", width=20, no_wrap=True)
    table.add_column("TotalRecovered", style="green", width=20, no_wrap=True)
    table.add_column("Date", width=34)

    for i in allinfo:
        table.add_row(
            f"{i['Country']}",
            f"{i['CountryCode']}",
            f"{i['NewConfirmed']}",
            f"{i['TotalConfirmed']}",
            f"{i['NewDeaths']}",
            f"{i['TotalDeaths']}",
            f"{i['NewRecovered']}",
            f"{i['TotalRecovered']}",
            f"{i['Date']}",
        )
    console.print(table)
Пример #2
0
        )
        f.write("  supportedComponentsWithTests = [\n")
        for component, deps in build_inputs.items():
            available, extras, missing = deps
            if len(missing) == 0 and component in components_with_tests:
                f.write(f'    "{component}"' + "\n")
        f.write("  ];\n")
        f.write("}\n")

    supported_components = reduce(lambda n, c: n + (build_inputs[c][2] == []),
                                  components.keys(), 0)
    total_components = len(components)
    print(f"{supported_components} / {total_components} components supported, "
          f"i.e. {supported_components / total_components:.2%}")

    if outdated:
        table = Table(title="Outdated dependencies")
        table.add_column("Package")
        table.add_column("Current")
        table.add_column("Wanted")
        for package, version in sorted(outdated.items()):
            table.add_row(package, version['current'], version['wanted'])

        console = Console()
        console.print(table)


if __name__ == "__main__":
    run_mypy()
    main()
Пример #3
0
    def handle(self, *args, **options):

        if options['id']:
            article = Articles.objects.get(id=int(options['id']))
            content = pypandoc.convert_text(article.text,
                                            'markdown_github',
                                            format='html')
            table = Table(show_header=True, header_style="bold magenta")
            table.add_column("ID")
            table.add_column("Title")
            table.add_column("Body")
            table.add_row(str(article.id), article.title, Markdown(content))
            article.read = True
            article.save()
        else:
            articles = Articles.objects.all()
            table = Table(show_header=True, header_style="bold magenta")
            table.add_column("ID")
            table.add_column("Feeds")
            table.add_column("Title")
            table.add_column("Read")
            table.add_column("Later")
            table.add_column("Created")

            for line in articles:
                read = "[green]Unread[/]" if line.read is False else "[yellow]Read[/]"
                read_later = "[green]Unread[/]" if line.read_later is False else "[yellow]Read[/]"
                table.add_row(str(line.id), line.feeds.title, line.title, read,
                              read_later, str(line.date_created))
        console.print(table)
Пример #4
0
        def parsingYa(login):
            # Запись в txt
            if Ya == '4':
                file_txt = open(dirresults + "/results/Yandex_parser/" +
                                str(hvostfile) + '_' +
                                time.strftime("%d_%m_%Y_%H_%M_%S", time_data) +
                                ".txt",
                                "w",
                                encoding="utf-8")
            # raise Exception("")
            else:
                file_txt = open(dirresults + "/results/Yandex_parser/" +
                                str(login) + ".txt",
                                "w",
                                encoding="utf-8")

            progressYa = Progress(
                "[progress.percentage]{task.percentage:>3.0f}%",
                auto_refresh=False)

            # Парсинг
            for login in progressYa.track(listlogin, description=""):
                urlYa = f'https://yandex.ru/collections/api/users/{login}/'
                try:
                    r = my_session.get(urlYa, headers=head0, timeout=3)
                except:
                    print(Fore.RED + "\nОшибка\n" + Style.RESET_ALL)
                    continue
                try:
                    rdict = json.loads(r.text)
                except:
                    rdict = {}
                    rdict.update(public_id="Увы", display_name="-No-")

                pub = rdict.get("public_id")
                name = rdict.get("display_name")
                email = str(login) + "@yandex.ru"

                if rdict.get("display_name") == "-No-":
                    if Ya != '4':
                        print(Style.BRIGHT + Fore.RED + "\nНе сработало")
                    else:
                        wZ1bad.append(str(login))
                        continue
                    continue
                else:
                    table1 = Table(title="\n" + Style.BRIGHT + Fore.RED +
                                   str(login) + Style.RESET_ALL,
                                   style="green")
                    table1.add_column("Имя", style="magenta")
                    table1.add_column("Логин", style="cyan")
                    table1.add_column("E-mail", style="cyan")
                    if Ya == '3':
                        table1.add_row(name, "Пропуск", "Пропуск")
                    else:
                        table1.add_row(name, login, email)
                    console = Console()
                    console.print(table1)

                    otzyv = f"https://reviews.yandex.ru/user/{pub}"
                    market = f"https://market.yandex.ru/user/{pub}/reviews"
                    collections = f"https://yandex.ru/collections/user/{login}/"
                    if Ya == '3':
                        music = f"\033[33;1mПропуск\033[0m"
                    else:
                        music = f"https://music.yandex.ru/users/{login}/tracks"
                    dzen = f"https://zen.yandex.ru/user/{pub}"
                    qu = f"https://yandex.ru/q/profile/{pub}/"
                    raion = f"https://local.yandex.ru/users/{pub}/"

                    print("\033[32;1mЯ.Отзывы:\033[0m", otzyv)
                    print("\033[32;1mЯ.Маркет:\033[0m", market)
                    print("\033[32;1mЯ.Картинки:\033[0m", collections)
                    print("\033[32;1mЯ.Музыка:\033[0m", music)
                    print("\033[32;1mЯ.Дзен:\033[0m", dzen)
                    print("\033[32;1mЯ.Кью:\033[0m", qu)
                    print("\033[32;1mЯ.Район:\033[0m", raion)

                    yalist = [
                        otzyv, market, collections, music, dzen, qu, raion
                    ]

                    file_txt.write(
                        f"{login} | {email} | {name}\n\n{otzyv}\n{market}\n{collections}\n{music}\n{dzen}\n{qu}\n{raion}",
                    )
                    progressYa.refresh()

                for webopen in yalist:
                    if webopen == music and Ya == '3':
                        continue
                    else:
                        webbrowser.open(webopen)

            if Ya == '4':
                # запись в txt концовка
                file_txt.write(
                    f"\nНеобработанные данные из файла '{hvostfile}':\n")
                for badsites in wZ1bad:
                    file_txt.write(f"{badsites}\n")
                file_txt.write(f"\nОбновлено: " +
                               time.strftime("%d/%m/%Y_%H:%M:%S", time_data) +
                               ".")
                file_txt.close()
Пример #5
0
        def parsingYa(login):
# Запись в txt
            if Ya == '4':
                file_txt = open(dirresults + "/results/Yandex_parser/" + str(hvostfile) + '_' + \
                time.strftime("%d_%m_%Y_%H_%M_%S", time_data) + ".txt", "w", encoding="utf-8")
            #raise Exception("")
            else:
                file_txt = open(dirresults + "/results/Yandex_parser/" + str(login) + ".txt", "w", encoding="utf-8")

# Парсинг
            for login in listlogin:
                urlYa = f'https://yandex.ru/collections/api/users/{login}/'
                try:
                    r = sessionY.get(urlYa, headers = head0, timeout=3)
                    azS.append(r)
                except:
                    print(Fore.RED + "\nОшибка" + Style.RESET_ALL)
                    if Ya != '4':
                        ravno()
                    continue

            with progressYa:
                if Ya == '4':
                    task = progressYa.add_task("", total=len(listlogin))

                for reqY, login in zip(azS, listlogin):
                    if Ya == '4':
                        progressYa.refresh()
                        progressYa.update(task, advance=1)
                    rY=reqY.result()
                    try:
                        rdict = json.loads(rY.text)
                    except:
                        rdict = {}
                        rdict.update(public_id="Увы", display_name="-No-")

                    pub = rdict.get("public_id")
                    name = rdict.get("display_name")
                    email=str(login)+"@yandex.ru"

                    if rdict.get("display_name") == "-No-":
                        if Ya != '4':
                            print(Style.BRIGHT + Fore.RED + "\nНе сработало")
                            console.rule(characters = '=', style="cyan bold\n")
                        else:
                            wZ1bad.append(str(login))
                            continue
                        continue
                    else:
                        table1 = Table(title = "\n" + Style.BRIGHT + Fore.RED + str(login) + Style.RESET_ALL, style="green")
                        table1.add_column("Имя", style="magenta", overflow="fold")
                        table1.add_column("Логин", style="cyan", overflow="fold")
                        table1.add_column("E-mail", style="cyan", overflow="fold")
                        if Ya == '3':
                            table1.add_row(name,"Пропуск","Пропуск")
                        else:
                            table1.add_row(name,login,email)
                        console.print(table1)

                        otzyv=f"https://reviews.yandex.ru/user/{pub}"
                        market=f"https://market.yandex.ru/user/{pub}/reviews"
                        collections=f"https://yandex.ru/collections/user/{login}/"
                        if Ya == '3':
                            music=f"\033[33;1mПропуск\033[0m"
                        else:
                            music=f"https://music.yandex.ru/users/{login}/tracks"
                        dzen=f"https://zen.yandex.ru/user/{pub}"
                        qu=f"https://yandex.ru/q/profile/{pub}/"

                        print("\033[32;1mЯ.Отзывы:\033[0m", otzyv)
                        print("\033[32;1mЯ.Маркет:\033[0m", market)
                        print("\033[32;1mЯ.Картинки:\033[0m", collections)
                        print("\033[32;1mЯ.Музыка:\033[0m", music)
                        print("\033[32;1mЯ.Дзен:\033[0m", dzen)
                        print("\033[32;1mЯ.Кью:\033[0m", qu)

                        yalist=[otzyv, market, collections, music, dzen, qu]

                        file_txt.write(f"{login} | {email} | {name}\n{otzyv}\n{market}\n{collections}\n{music}\n{dzen}\n{qu}\n\n")

                    for webopen in yalist:
                        if webopen == music and Ya == '3':
                            continue
                        else:
                            webbrowser.open(webopen)
            ravno()
            azS.clear()

# сохранение в html
            if Ya == '4':
# запись в txt концовка
                file_txt.write(f"\nНеобработанные данные из файла '{hvostfile}' ({len(wZ1bad)}):\n")
                for badsites in wZ1bad:
                    file_txt.write(f"{badsites}\n")
                file_txt.write(f"\nОбновлено: " + time.strftime("%d/%m/%Y_%H:%M:%S", time_data) + ".")
                file_txt.close()
Пример #6
0
async def main():
    async with meraki.aio.AsyncDashboardAPI(output_log=False,
                                            suppress_logging=True,
                                            maximum_concurrent_requests=5,
                                            wait_on_rate_limit=True,
                                            nginx_429_retry_wait_time=2,
                                            maximum_retries=100) as aiomeraki:
        #
        # Get wireless AP list
        devices = await aiomeraki.networks.getNetworkDevices(NETWORK_ID)
        access_points = [
            device for device in devices if "MR" in device['model']
        ]
        # Fetching association events for each AP
        pp("\n\nFetching association events...")
        start_time = (datetime.now() - timedelta(days=2)).isoformat()
        get_events_tasks = [
            get_events(aiomeraki, start_time, device['serial'])
            for device in access_points
        ]
        association_event_lists = await asyncio.gather(*get_events_tasks)
        # Flatten the list of lists
        association_events = [
            event for event_list in association_event_lists
            for event in event_list
        ]
        pp(f"Gathered {len(association_events)} association events.")
        #
        # Analyzing the association events, to understand which clients are connected to 2.4GHz channels and which to 5GHz channels.
        pp('Analyzing association events...')
        for event in association_events:
            # Client in 2.4GHz
            if int(event['eventData']['channel']) < 13:
                if event['clientId'] in results.keys():
                    results[event['clientId']]['2.4GHz'] = True
                else:
                    results[event['clientId']] = {
                        '2.4GHz': True,
                        '5GHz': False,
                        'SSID': event['ssidName']
                    }
                    results[
                        event['clientId']]['name'] = event['clientDescription']
            # Client in 5GHz
            elif int(event['eventData']['channel']) > 13:
                if event['clientId'] in results.keys():
                    results[event['clientId']]['5GHz'] = True
                else:
                    results[event['clientId']] = {
                        '2.4GHz': False,
                        '5GHz': True,
                        'SSID': event['ssidName']
                    }
                    results[
                        event['clientId']]['name'] = event['clientDescription']
            # Client unknown
            else:
                print(
                    f"Error processing client {event['clientId']}, unknown channel"
                )

        # Adding client capabilities
        client_list = list(results.keys())
        pp(f"Fetching client capabilities for {len(client_list)} clients...\n\n"
           )
        client_details_tasks = [
            get_client_details(aiomeraki, client_id)
            for client_id in client_list
        ]

        for task in asyncio.as_completed(client_details_tasks):
            await task

        # Summarize the numbers
        wirelessClientCount = len(results)
        clients_2_only = [
            client for client in results
            if results[client]['5GHz'] == False and results[client]['2.4GHz']
            == True and results[client]['5GHz_capable'] == False
        ]
        clients_5_only = [
            client for client in results if results[client]['5GHz'] == True
            and results[client]['2.4GHz'] == False
        ]
        clients_error = [
            client for client in results if results[client]['5GHz'] == False
            and results[client]['2.4GHz'] == False
        ]
        clients_5_on_2 = [
            client for client in results
            if results[client]['2.4GHz'] == True and (
                results[client]['5GHz_capable'] == True
                or results[client]['5GHz'] == True)
        ]
        #
        if wirelessClientCount > 0:
            clients_2_percent = round(
                len(clients_2_only) * 100 / wirelessClientCount)
            clients_5_percent = round(
                len(clients_5_only) * 100 / wirelessClientCount)
            clients_5_on_2_percent = round(
                len(clients_5_on_2) * 100 / wirelessClientCount)
            clients_error_percent = round(
                len(clients_error) * 100 / wirelessClientCount)
            #
            table = Table(title="Wireless Clients Summary")
            table.add_column("Total Clients",
                             justify="center",
                             style="cyan",
                             no_wrap=True)
            table.add_column("5GHz Only Clients",
                             justify="center",
                             style="cyan",
                             no_wrap=True)
            table.add_column("2.4GHz Only Clients",
                             justify="center",
                             style="cyan",
                             no_wrap=True)
            table.add_column("2.4GHz + 5GHz Clients",
                             justify="center",
                             style="cyan",
                             no_wrap=True)
            table.add_column("Unknown",
                             justify="center",
                             style="cyan",
                             no_wrap=True)
            #
            table.add_row(str(wirelessClientCount), str(len(clients_5_only)),
                          str(len(clients_2_only)), str(len(clients_5_on_2)),
                          str(len(clients_error)))
            table.add_row("---", "---", "---", "---")
            table.add_row("100 % ",
                          str(clients_5_percent) + " % ",
                          str(clients_2_percent) + " % ",
                          str(clients_5_on_2_percent) + " % ",
                          str(clients_error_percent) + " % ")
            #
            console = Console()
            console.print(table)

            if len(clients_5_on_2) > 0:
                input(
                    '\n\nPress any key to get a list of 5GHz capable clients on 2.4GHz...\n'
                )
                #
                table = Table(title="5GHz capable clients on 2.4GHz")
                table.add_column("Client ID",
                                 justify="left",
                                 style="red",
                                 no_wrap=True)
                table.add_column("SSID",
                                 justify="left",
                                 style="red",
                                 no_wrap=True)
                table.add_column("Description",
                                 justify="left",
                                 style="red",
                                 no_wrap=True)
                #
                for client in clients_5_on_2:
                    table.add_row(client, results[client]['SSID'],
                                  results[client].get('name', 'Unknown'))
                #
                console = Console()
                console.print(table)
from rich.console import Console
from rich.table import Table

table = Table(title="Star Wars Movies")

table.add_column("Released", justify="right", style="cyan", no_wrap=True)
table.add_column("Title", style="magenta")
table.add_column("Box Office", justify="right", style="green")

table.add_row("Dec 20, 2019", "Star Wars: The Rise of Skywalker",
              "$952,110,690")
table.add_row("May 25, 2018", "Solo: A Star Wars Story", "$393,151,347")
table.add_row("Dec 15, 2017", "Star Wars Ep. V111: The Last Jedi",
              "$1,332,539,889")
table.add_row("Dec 16, 2016", "Rogue One: A Star Wars Story", "$1,332,439,889")

console = Console()
console.print(table)
Пример #8
0
def _default_tasks_table() -> Table:
    """Create the default structure for the Tasks Table, hard coded columns and no rows added."""
    table = Table(width=120, box=box.SIMPLE_HEAVY)
    for header, header_col_settings in TASK_COLUMNS_SETTINGS.items():
        table.add_column(header, **header_col_settings)
    return table
Пример #9
0
def _default_cluster_table() -> Table:
    """Create the default structure for the Cluster Table, hard coded columns and no rows added."""
    table = Table(width=120, box=box.HORIZONTALS)
    for header, header_col_settings in CLUSTER_COLUMNS_SETTINGS.items():
        table.add_column(header, **header_col_settings)
    return table
        table.add_row(str(c), str(eps), str(r2_score(y_test, predictions)), str(time.time() - start_time))
        console.log('another iteration complete')
console.print(table)
winsound.Beep(2000, 1000)
"""
# %%
# file logging for SVM hyperparameter optimisation
import winsound
from rich_console import console
from rich.table import Table
from tqdm import tqdm, trange
from rich.live import Live
import time

table = Table(title="SVR for INM")
table.add_column('kernel', style='bright_yellow')
table.add_column('C', style='magenta')
table.add_column('epsilon', style='cyan')
table.add_column('R2', style='green')
table.add_column('time for run (sec)', style='white')

# if below line has file opened as 'w' and file already has useful data, then change open type to 'a'
with open('inm_data_svm_hpp_search.txt', 'a') as f:
    try:
        f.write('All other parameters take their default values.\n')
        f.write('kernel,C,epsilon,r2,time\n')
        with Live(table, refresh_per_second=0.1):
            for kernel in [
                    'rbf',
            ]:
                for c in [1000]:
Пример #11
0
def main():
    #torch.backends.cudnn.enabled = False

    parser = argparse.ArgumentParser(
        description='Midiocrity VAE model training')
    parser.add_argument('--config',
                        '-c',
                        dest="filename",
                        metavar='FILE',
                        help='path to the config file',
                        default='../config/midiocrity_vae.yaml')

    args = parser.parse_args()
    with open(args.filename, 'r') as file:
        try:
            config = yaml.safe_load(file)
        except yaml.YAMLError as exc:
            print(exc)

    rprint(config)

    # For reproducibility
    if 'seed' in config['train_params']:
        torch.manual_seed(config['train_params']['seed'])
        np.random.seed(config['train_params']['seed'])
        torch.backends.cudnn.deterministic = True
        torch.backends.cudnn.benchmark = False

    if config["device"] == 'cuda' and torch.cuda.is_available():
        device = torch.device(config["device"])
        dtype = torch.float
    else:
        device = torch.device('cpu')
        dtype = torch.float

    mvae = MidiocrityVAE(
        encoder_params={
            "n_cropped_notes": config['model_params']['n_cropped_notes'],
            "z_dim": config['model_params']['z_dim'],
            "phrase_size": config['model_params']['phrase_size'],
            "hidden_size":
            config['model_params']['encoder_params']['hidden_size'],
            "num_layers":
            config['model_params']['encoder_params']['num_layers'],
            "batch_size": config['data_params']['batch_size'],
            "n_tracks": config['model_params']['decoder_params']['n_tracks'],
        },
        decoder_params={
            "n_cropped_notes": config['model_params']['n_cropped_notes'],
            "z_dim": config['model_params']['z_dim'],
            "phrase_size": config['model_params']['phrase_size'],
            "hidden_size":
            config['model_params']['decoder_params']['hidden_size'],
            "num_layers":
            config['model_params']['decoder_params']['num_layers'],
            "batch_size": config['data_params']['batch_size'],
            "n_tracks": config['model_params']['decoder_params']['n_tracks'],
        },
        device=device,
    )

    rprint(mvae)

    table = Table(title="MVAE Trainable Parameters")
    table.add_column("Component")
    table.add_column("Module")
    table.add_column("Parameters", justify="right")
    params = {"encoder": 0, "decoder": 0}
    for name, param in mvae.named_parameters():
        if not param.requires_grad:
            continue
        component = name.split('.')[0]
        num_params = param.numel()
        table.add_row(component, name, str(num_params))

        params[component] += num_params

    rprint(table)
    rprint(params)
    rprint(f"Device: {mvae.device}")

    optimizer = optim.Adam(mvae.parameters(),
                           lr=config['train_params']['LR'],
                           weight_decay=config['train_params']['weight_decay'])
    scheduler = lr_scheduler.ExponentialLR(
        optimizer, gamma=config['train_params']['scheduler_gamma'])

    beta = config['train_params']['beta_init']

    loader = MidiDataloader(config['data_params']['tensor_folder'],
                            config['data_params']['batch_size'],
                            config['data_params']['shuffle'],
                            config['data_params']['num_workers'],
                            batch_limit=config['data_params']['batch_limit'])

    train_losses = []
    valid_losses = []
    with torch.autograd.set_detect_anomaly(False):
        with Progress(auto_refresh=False) as progress:
            pprint = progress.console.print
            step_tot = 0
            tstart = time.time()
            tcycle = tstart
            task = progress.add_task("Training...",
                                     total=config['train_params']['epochs'])
            metrics = np.zeros(3)

            train_epoch_losses = np.zeros(3)
            valid_epoch_losses = np.zeros(3)

            for epoch in range(config['train_params']['epochs']):
                step_batch = 0

                # Train
                mvae.train()
                loader.set_phase('train')
                for batch in loader:
                    step_tot += 1
                    step_batch += 1
                    X = batch[0]
                    X = X[:, :, :, 0:1]  # Only train the drum tracks
                    X = X.to(device=device, dtype=dtype)

                    mvae.zero_grad()
                    mu, logvar, z, recon = mvae(X)
                    # pprint(mu, logvar, z, recon)

                    kl_loss, recon_loss, loss = mvae.loss(
                        mu, logvar, X, recon, beta)
                    loss.backward()

                    if config['train_params']['clip_norm'] is not None:
                        nn.utils.clip_grad_norm_(
                            mvae.parameters(),
                            config['train_params']['clip_norm'])

                    optimizer.step()

                    losses_np = np.array([
                        kl_loss.detach().cpu().numpy(),
                        recon_loss.detach().cpu().numpy(),
                        loss.detach().cpu().numpy()
                    ])
                    metrics = metrics + losses_np
                    train_epoch_losses = train_epoch_losses + losses_np

                    if step_tot % config['output_params']['print_step'] == 0:
                        # Average metrics for this print cycle
                        metrics /= config['output_params']['print_step']
                        ttotal = time.time() - tstart
                        tcycle = time.time() - tcycle
                        pprint(
                            f"Epoch {epoch} [{step_tot}] "
                            f"ttotal: {str(timedelta(seconds=ttotal)).split('.')[0]} "
                            f"tcycle: {str(timedelta(seconds=tcycle)).split('.')[0]} "
                            f"beta: {beta:.3f} "
                            f"KLDiv: {metrics[0]:.4f} "
                            f"ReconCEL: {metrics[1]:.4f} "
                            f"Loss: {metrics[2]:.4f}")

                        metrics *= 0
                        tcycle = time.time()

                    if step_tot % config['output_params']['save_step'] == 0:
                        torch.save(mvae.state_dict(),
                                   (f"{config['output_params']['save_dir']}"
                                    f"{config['output_params']['name']}"
                                    f".iter-{step_tot}.pt"))

                    if step_tot % config['train_params']['anneal_step'] == 0:
                        scheduler.step()
                        pprint(
                            f"Learning rate: {scheduler.get_last_lr()[0]:.6f}")

                    # Increase KL weight (beta)
                    if (step_tot >
                            config['train_params']['beta_increase_step_start']
                            and step_tot %
                            config['train_params']['beta_increase_step_rate']
                            == 0):
                        beta = min(
                            config['train_params']['beta_max'],
                            beta + config['train_params']['beta_increase'])

                train_epoch_losses /= step_batch
                train_losses.append(train_epoch_losses.copy())
                ttotal = time.time() - tstart
                pprint(
                    f"\n{'#' * 80}\n"
                    f"Epoch {epoch}: Training complete\n"
                    f"Epoch {epoch} [{step_tot}] "
                    f"ttotal: {str(timedelta(seconds=ttotal)).split('.')[0]} "
                    f"Train KLDiv: {train_epoch_losses[0]:.4f} "
                    f"Train ReconCEL: {train_epoch_losses[1]:.4f} "
                    f"Train Loss: {train_epoch_losses[2]:.4f}")
                train_epoch_losses *= 0

                # Validation
                step_batch = 0
                mvae.eval()
                loader.set_phase('valid')
                with torch.no_grad():
                    for batch in loader:
                        step_batch += 1
                        X = batch[0]
                        X = X[:, :, :, 0:1]  # Only train the drum tracks
                        X = X.to(device=device, dtype=dtype)
                        mu, logvar, z, recon = mvae(X)
                        kl_loss, recon_loss, loss = mvae.loss(
                            mu, logvar, X, recon, beta)
                        losses_np = np.array([
                            kl_loss.detach().cpu().numpy(),
                            recon_loss.detach().cpu().numpy(),
                            loss.detach().cpu().numpy()
                        ])
                        valid_epoch_losses = valid_epoch_losses + losses_np

                valid_epoch_losses /= step_batch
                valid_losses.append(valid_epoch_losses.copy())
                ttotal = time.time() - tstart
                pprint(
                    f"Epoch {epoch}: Validation complete\n"
                    f"Epoch {epoch} [{step_tot}] "
                    f"ttotal: {str(timedelta(seconds=ttotal)).split('.')[0]} "
                    f"Valid KLDiv: {valid_epoch_losses[0]:.4f} "
                    f"Valid ReconCEL: {valid_epoch_losses[1]:.4f} "
                    f"Valid Loss: {valid_epoch_losses[2]:.4f}"
                    f"\n{'#' * 80}\n")

                valid_epoch_losses *= 0
                if (config['output_params']['save_epoch'] and
                        epoch % config['output_params']['save_epoch'] == 0):
                    torch.save(mvae.state_dict(),
                               (f"{config['output_params']['save_dir']}"
                                f"{config['output_params']['name']}"
                                f".epoch-{epoch}.pt"))

                progress.advance(task)

    # Epoch x (kl_loss, recon_loss, loss)
    train_losses = np.stack(train_losses)
    valid_losses = np.stack(valid_losses)
    np.save((f"{config['output_params']['save_dir']}"
             f"{config['output_params']['name']}"
             f"-train_losses.npy"), train_losses)
    np.save((f"{config['output_params']['save_dir']}"
             f"{config['output_params']['name']}"
             f"-valid_losses.npy"), valid_losses)
Пример #12
0
def chat_main(client):
    """ (Clubhouse) -> NoneType

    Main function for chat
    """
    max_limit = 20
    channel_speaker_permission = False
    _wait_func = None
    _ping_func = None

    def _request_speaker_permission(client, channel_name, user_id):
        """ (str) -> bool

        Raise hands for permissions
        """
        if not channel_speaker_permission:
            client.audience_reply(channel_name, True, False)
            _wait_func = _wait_speaker_permission(client, channel_name,
                                                  user_id)
            print(
                "[/] You've raised your hand. Wait for the moderator to give you the permission."
            )

    @set_interval(30)
    def _ping_keep_alive(client, channel_name):
        """ (str) -> bool

        Continue to ping alive every 30 seconds.
        """
        client.active_ping(channel_name)
        return True

    @set_interval(10)
    def _wait_speaker_permission(client, channel_name, user_id):
        """ (str) -> bool

        Function that runs when you've requested for a voice permission.
        """
        # Get some random users from the channel.
        _channel_info = client.get_channel(channel_name)
        if _channel_info['success']:
            for _user in _channel_info['users']:
                if _user['user_id'] != user_id:
                    user_id = _user['user_id']
                    break
            # Check if the moderator allowed your request.
            res_inv = client.accept_speaker_invite(channel_name, user_id)
            if res_inv['success']:
                print("[-] Now you have a speaker permission.")
                print(
                    "    Please re-join this channel to activate a permission."
                )
                return False
        return True

    # Choose which channel to enter.
    # Join the talk on success.
    user_id = client.HEADERS.get("CH-UserID")
    print_channel_list(client, max_limit)
    time.sleep(3)
    channel_name = sys.argv[1]
    channel_info = client.join_channel(channel_name)
    if not channel_info['success']:
        # Check if this channel_name was taken from the link
        channel_info = client.join_channel(channel_name, "link", "e30=")
        if not channel_info['success']:
            print(
                f"[-] Error while joining the channel ({channel_info['error_message']})"
            )
            return

    # List currently available users (TOP 20 only.)
    # Also, check for the current user's speaker permission.
    channel_speaker_permission = False
    console = Console()
    table = Table(show_header=True, header_style="bold magenta")
    table.add_column("user_id", style="cyan", justify="right")
    table.add_column("username")
    table.add_column("name")
    table.add_column("is_speaker")
    table.add_column("is_moderator")
    users = channel_info['users']
    i = 0
    for user in users:
        i += 1
        if i > max_limit:
            break
        table.add_row(
            str(user['user_id']),
            str(user['name']),
            str(user['username']),
            str(user['is_speaker']),
            str(user['is_moderator']),
        )
        # Check if the user is the speaker
        if user['user_id'] == int(user_id):
            channel_speaker_permission = bool(user['is_speaker'])
    console.print(table)

    # Check for the voice level.
    if RTC:
        token = channel_info['token']
        RTC.joinChannel(token, channel_name, "", int(user_id))
        RTC.startAudioRecording("./data/" + channel_info['topic'] + ".wav",
                                32000, 2)
    else:
        print("[!] Agora SDK is not installed.")
        print("    You may not speak or listen to the conversation.")

    # Activate pinging
    client.active_ping(channel_name)
    _ping_func = _ping_keep_alive(client, channel_name)
    _wait_func = None

    # Add raise_hands key bindings for speaker permission
    # Sorry for the bad quality
    if not channel_speaker_permission:

        if sys.platform == "darwin":  # OSX
            _hotkey = "9"
        elif sys.platform == "win32":  # Windows
            _hotkey = "ctrl+shift+h"

        print(
            f"[*] Press [{_hotkey}] to raise your hands for the speaker permission."
        )
        keyboard.add_hotkey(_hotkey,
                            _request_speaker_permission,
                            args=(client, channel_name, user_id))

    while True:
        time.sleep(100)
        channel_info = client.get_channel(channel_name)
        if channel_info['should_leave'] == True or channel_info[
                'success'] == False:
            break

    # Safely leave the channel upon quitting the channel.
    if _ping_func:
        _ping_func.set()
    if _wait_func:
        _wait_func.set()
    if RTC:
        RTC.leaveChannel()
        RTC.stopAudioRecording()
    client.leave_channel(channel_name)
Пример #13
0
class TUI:
    def __init__(self):
        self.core = Core()
        self.session = PromptSession(reserve_space_for_menu=6)
        self.headless = False
        self.console = None
        self.table = None
        self.cfSlugs = None
        self.wowiSlugs = None
        self.completer = None
        self.os = platform.system()
        install()

    def start(self):
        # Check if headless mode was requested
        if len(sys.argv) == 2 and sys.argv[1].lower() == 'headless':
            self.headless = True
        self.setup_console()
        self.print_header()
        # Check if executable is in good location
        if not glob.glob('World*.app') and not glob.glob('Wow*.exe') or \
                not os.path.isdir(Path('Interface/AddOns')) or not os.path.isdir('WTF'):
            self.console.print(
                '[bold red]This executable should be placed in the same directory where Wow.exe, '
                'WowClassic.exe or World of Warcraft.app is located.[/bold red]\n\n'
            )
            pause(self.headless)
            sys.exit(1)
        # Detect Classic client
        if os.path.basename(os.getcwd()) == '_classic_':
            self.core.clientType = 'wow_classic'
            set_terminal_title(f'CurseBreaker v{__version__} - Classic')
        # Check if client have write access
        try:
            with open('PermissionTest', 'w') as _:
                pass
            os.remove('PermissionTest')
        except IOError:
            self.console.print(
                '[bold red]CurseBreaker doesn\'t have write rights for the current directory.\n'
                'Try starting it with administrative privileges.[/bold red]\n\n'
            )
            pause(self.headless)
            sys.exit(1)
        self.auto_update()
        self.core.init_config()
        self.setup_table()
        # Curse URI Support
        if len(sys.argv) == 2 and 'twitch://' in sys.argv[1]:
            try:
                self.c_install(sys.argv[1].strip())
            except Exception as e:
                self.handle_exception(e)
            timeout(self.headless)
            sys.exit(0)
        if len(sys.argv) == 2 and '.ccip' in sys.argv[1]:
            try:
                path = sys.argv[1].strip()
                self.c_install(self.core.parse_cf_xml(path))
                if os.path.exists(path):
                    os.remove(path)
            except Exception as e:
                self.handle_exception(e)
            timeout(self.headless)
            sys.exit(0)
        # CLI command
        if len(sys.argv) >= 2:
            command = ' '.join(sys.argv[1:]).split(' ', 1)
            if command[0].lower() == 'headless':
                pass
            elif getattr(self, f'c_{command[0].lower()}', False):
                try:
                    getattr(self, f'c_{command[0].lower()}')(
                        command[1].strip() if len(command) > 1 else False)
                except Exception as e:
                    self.handle_exception(e)
                sys.exit(0)
            else:
                self.console.print('Command not found.')
                sys.exit(0)
        # Addons auto update
        if len(self.core.config['Addons']) > 0:
            if not self.headless:
                self.console.print(
                    'Automatic update of all addons will start in 5 seconds.\n'
                    'Press any button to enter interactive mode.',
                    highlight=False)
            starttime = time.time()
            keypress = None
            while True:
                if self.headless:
                    break
                elif kbhit():
                    keypress = getch()
                    break
                elif time.time() - starttime > 5:
                    break
            if not keypress:
                if not self.headless:
                    self.print_header()
                try:
                    self.c_update(None, True)
                    if self.core.backup_check():
                        self.setup_table()
                        self.console.print(
                            f'\n[green]Backing up WTF directory{"!" if self.headless else ":"}[/green]'
                        )
                        self.core.backup_wtf(
                            None if self.headless else self.console)
                    if self.core.config['WAUsername'] != 'DISABLED':
                        self.setup_table()
                        self.c_wa_update(None, False)
                except Exception as e:
                    self.handle_exception(e)
                self.console.print('')
                self.print_log()
                pause(self.headless)
                sys.exit(0)
        if self.headless:
            sys.exit(1)
        self.setup_completer()
        self.print_header()
        self.console.print(
            'Use command [green]help[/green] or press [green]TAB[/green] to see a list of available comm'
            'ands.\nCommand [green]exit[/green] or pressing [green]CTRL+D[/green] will close the applica'
            'tion.\n\n')
        if len(self.core.config['Addons']) == 0:
            self.console.print(
                'Command [green]import[/green] might be used to detect already installed addons.\n\n'
            )
        # Prompt session
        while True:
            try:
                command = self.session.prompt(
                    HTML('<ansibrightgreen>CB></ansibrightgreen> '),
                    completer=self.completer)
            except KeyboardInterrupt:
                continue
            except EOFError:
                break
            else:
                command = command.split(' ', 1)
                if getattr(self, f'c_{command[0].lower()}', False):
                    try:
                        self.setup_table()
                        getattr(self, f'c_{command[0].lower()}')(
                            command[1].strip() if len(command) > 1 else False)
                        self.setup_completer()
                    except Exception as e:
                        self.handle_exception(e)
                else:
                    self.console.print('Command not found.')

    def auto_update(self):
        if getattr(sys, 'frozen', False):
            try:
                if os.path.isfile(sys.executable + '.old'):
                    try:
                        os.remove(sys.executable + '.old')
                    except PermissionError:
                        pass
                payload = requests.get(
                    'https://api.github.com/repos/AcidWeb/CurseBreaker/releases/latest',
                    headers=HEADERS).json()
                if 'name' in payload and 'body' in payload and 'assets' in payload:
                    remoteversion = payload['name']
                    changelog = payload['body']
                    url = None
                    for binary in payload['assets']:
                        if (self.os == 'Windows' and '.exe' in binary['name'])\
                                or (self.os == 'Darwin' and '.zip' in binary['name'])\
                                or (self.os == 'Linux' and '.gz' in binary['name']):
                            url = binary['browser_download_url']
                            break
                    if url and StrictVersion(
                            remoteversion[1:]) > StrictVersion(__version__):
                        self.console.print(
                            '[green]Updating CurseBreaker...[/green]')
                        shutil.move(sys.executable, sys.executable + '.old')
                        payload = requests.get(url, headers=HEADERS)
                        if self.os == 'Darwin':
                            zipfile.ZipFile(io.BytesIO(
                                payload.content)).extractall()
                        else:
                            with open(sys.executable, 'wb') as f:
                                if self.os == 'Windows':
                                    f.write(payload.content)
                                elif self.os == 'Linux':
                                    f.write(gzip.decompress(payload.content))
                        os.chmod(sys.executable, 0o775)
                        self.console.print(
                            f'[bold green]Update complete! Please restart the application.[/bold green]'
                            f'\n\n[green]Changelog:[/green]\n{changelog}\n\n')
                        self.print_log()
                        pause(self.headless)
                        sys.exit(0)
            except Exception as e:
                self.console.print(
                    f'[bold red]Update failed!\n\nReason: {str(e)}[/bold red]\n\n'
                )
                self.print_log()
                pause(self.headless)
                sys.exit(1)

    def handle_exception(self, e, table=True):
        if self.table.row_count > 1 and table:
            self.console.print(self.table)
        if getattr(sys, 'frozen', False):
            sys.tracebacklimit = 0
        if isinstance(e, list):
            for es in e:
                self.console.print(
                    Traceback.from_exception(exc_type=es.__class__,
                                             exc_value=es,
                                             traceback=es.__traceback__))
        else:
            self.console.print(
                Traceback.from_exception(exc_type=e.__class__,
                                         exc_value=e,
                                         traceback=e.__traceback__))

    def print_header(self):
        clear()
        if self.headless:
            self.console.print(
                f'[bold green]CurseBreaker[/bold green] [bold red]v{__version__}[/bold red] | '
                f'[yellow]{datetime.now()}[/yellow]',
                highlight=False)
        else:
            self.console.print(
                Rule(
                    f'[bold green]CurseBreaker[/bold green] [bold red]v{__version__}[/bold red]'
                ))
            self.console.print('\n')

    def print_log(self):
        if self.headless:
            html = self.console.export_html(inline_styles=True,
                                            theme=HEADLESS_TERMINAL_THEME)
            with open('CurseBreaker.html', 'a+', encoding='utf-8') as log:
                log.write(html)

    def setup_console(self):
        if self.headless:
            self.console = Console(record=True)
            if self.os == 'Windows':
                window = windll.kernel32.GetConsoleWindow()
                if window:
                    windll.user32.ShowWindow(window, 0)
        elif 'WINDIR' in os.environ and 'WT_SESSION' not in os.environ:
            set_terminal_size(100, 50)
            windll.kernel32.SetConsoleScreenBufferSize(
                windll.kernel32.GetStdHandle(-11), wintypes._COORD(100, 200))
            self.console = Console(width=97)
        elif self.os == 'Darwin':
            set_terminal_size(100, 50)
            self.console = Console()
        else:
            self.console = Console()

    def setup_completer(self):
        if not self.cfSlugs or not self.wowiSlugs:
            # noinspection PyBroadException
            try:
                self.cfSlugs = pickle.load(
                    gzip.open(
                        io.BytesIO(
                            requests.get(
                                'https://storage.googleapis.com/cursebreaker/cfslugs.pickle.gz',
                                headers=HEADERS).content)))
                self.wowiSlugs = pickle.load(
                    gzip.open(
                        io.BytesIO(
                            requests.get(
                                'https://storage.googleapis.com/cursebreaker/wowislugs.pickle.gz',
                                headers=HEADERS).content)))
            except Exception:
                self.cfSlugs = []
                self.wowiSlugs = []
        commands = [
            'install', 'uninstall', 'update', 'force_update', 'wa_update',
            'status', 'orphans', 'search', 'import', 'export', 'toggle_backup',
            'toggle_dev', 'toggle_wa', 'set_wa_api', 'set_wa_wow_account',
            'uri_integration', 'help', 'exit'
        ]
        addons = sorted(self.core.config['Addons'],
                        key=lambda k: k['Name'].lower())
        for addon in addons:
            name = f'"{addon["Name"]}"' if ',' in addon["Name"] else addon[
                "Name"]
            commands.extend([
                f'uninstall {name}', f'update {name}', f'force_update {name}',
                f'toggle_dev {name}', f'status {name}'
            ])
        for item in self.cfSlugs:
            commands.append(f'install cf:{item}')
        for item in self.wowiSlugs:
            commands.append(f'install wowi:{item}')
        commands.extend([
            'install ElvUI', 'install ElvUI:Dev', 'install Tukui',
            'install SLE:Dev'
        ])
        accounts = self.core.detect_accounts()
        for account in accounts:
            commands.append(f'set_wa_wow_account {account}')
        self.completer = WordCompleter(commands,
                                       ignore_case=True,
                                       sentence=True)

    def setup_table(self):
        self.table = Table(box=box.SQUARE)
        self.table.add_column('Status',
                              header_style='bold white',
                              justify='center')
        self.table.add_column('Name', header_style='bold white')
        self.table.add_column('Version', header_style='bold white')

    def c_install(self, args):
        if args:
            if args.startswith('-i '):
                args = args[3:]
                optignore = True
            else:
                optignore = False
            addons = [
                addon.strip()
                for addon in list(reader([args], skipinitialspace=True))[0]
            ]
            with Progress('{task.completed}/{task.total}',
                          '|',
                          BarColumn(bar_width=self.console.width),
                          '|',
                          auto_refresh=False,
                          console=self.console) as progress:
                task = progress.add_task('', total=len(addons))
                while not progress.finished:
                    for addon in addons:
                        installed, name, version = self.core.add_addon(
                            addon, optignore)
                        if installed:
                            self.table.add_row('[green]Installed[/green]',
                                               name, version)
                        else:
                            self.table.add_row(
                                '[bold black]Already installed[/bold black]',
                                name, version)
                        progress.update(task, advance=1, refresh=True)
            self.console.print(self.table)
        else:
            self.console.print(
                '[green]Usage:[/green]\n\tThis command accepts a comma-separated list of links as an arg'
                'ument.\n\tOption [white]-i[/white] will disable the client version check.\n[bold green]'
                'Supported URL:[/bold green]\n\thttps://www.curseforge.com/wow/addons/[[addon_name]] [bo'
                'ld white]|[/bold white] cf:[[addon_name]]\n\thttps://www.wowinterface.com/downloads/[[a'
                'ddon_name]] [bold white]|[/bold white] wowi:[[addon_id]]\n\thttps://www.tukui.org/addon'
                's.php?id=[[addon_id]] [bold white]|[/bold white] tu:[[addon_id]]\n\thttps://www.tukui.o'
                'rg/classic-addons.php?id=[[addon_id]] [bold white]|[/bold white] tuc:[[addon_id]]\n\tEl'
                'vUI [bold white]|[/bold white] ElvUI:Dev\n\tTukui\n\tSLE:Dev',
                highlight=False)

    def c_uninstall(self, args):
        if args:
            addons = [
                addon.strip()
                for addon in list(reader([args], skipinitialspace=True))[0]
            ]
            with Progress('{task.completed}/{task.total}',
                          '|',
                          BarColumn(bar_width=self.console.width),
                          '|',
                          auto_refresh=False,
                          console=self.console) as progress:
                task = progress.add_task('', total=len(addons))
                while not progress.finished:
                    for addon in addons:
                        name, version = self.core.del_addon(addon)
                        if name:
                            self.table.add_row(
                                f'[bold red]Uninstalled[/bold red]', name,
                                version)
                        else:
                            self.table.add_row(
                                f'[bold black]Not installed[/bold black]',
                                addon, '')
                        progress.update(task, advance=1, refresh=True)
            self.console.print(self.table)
        else:
            self.console.print(
                '[green]Usage:[/green]\n\tThis command accepts a comma-separated list of links as an arg'
                'ument.\n[bold green]Supported URL:[/bold green]\n\thttps://www.curseforge.com/wow/addon'
                's/[[addon_name]] [bold white]|[/bold white] cf:[[addon_name]]\n\thttps://www.wowinterfa'
                'ce.com/downloads/[[addon_name]] [bold white]|[/bold white] wowi:[[addon_id]]\n\thttps:/'
                '/www.tukui.org/addons.php?id=[[addon_id]] [bold white]|[/bold white] tu:[[addon_id]]'
                '\n\thttps://www.tukui.org/classic-addons.php?id=[[addon_id]] [bold white]|[/bold white]'
                ' tuc:[[addon_id]]\n\tElvUI [bold white]|[/bold white] ElvUI:Dev\n\tTukui\n\tSLE:Dev',
                highlight=False)

    def c_update(self, args, addline=False, update=True, force=False):
        if len(self.core.cfCache) > 0 or len(self.core.wowiCache) > 0:
            self.core.cfCache = {}
            self.core.wowiCache = {}
            self.core.checksumCache = {}
        if args:
            addons = [
                addon.strip()
                for addon in list(reader([args], skipinitialspace=True))[0]
            ]
        else:
            addons = sorted(self.core.config['Addons'],
                            key=lambda k: k['Name'].lower())
        with Progress(
                '{task.completed:.0f}/{task.total}',
                '|',
                BarColumn(bar_width=self.console.width + 1),
                '|',
                auto_refresh=False,
                console=None if self.headless else self.console) as progress:
            task = progress.add_task('', total=len(addons))
            if not args:
                self.core.bulk_check(addons)
                self.core.bulk_check_checksum(addons, progress)
            while not progress.finished:
                exceptions = []
                for addon in addons:
                    try:
                        name, versionnew, versionold, modified = self.core.\
                            update_addon(addon if isinstance(addon, str) else addon['URL'], update, force)
                        if versionold:
                            if versionold == versionnew:
                                if modified:
                                    self.table.add_row(
                                        '[bold red]Modified[/bold red]', name,
                                        versionold)
                                else:
                                    self.table.add_row(
                                        '[green]Up-to-date[/green]', name,
                                        versionold)
                            else:
                                if modified:
                                    self.table.add_row(
                                        '[bold red]Update suppressed[/bold red]',
                                        name, versionold)
                                else:
                                    self.table.add_row(
                                        f'[yellow]{"Updated " if update else "Update available"}'
                                        f'[/yellow]', name,
                                        f'[yellow]{versionnew}[/yellow]')
                        else:
                            self.table.add_row(
                                f'[bold black]Not installed[/bold black]',
                                addon, '')
                    except Exception as e:
                        exceptions.append(e)
                    progress.update(task,
                                    advance=1 if args else 0.5,
                                    refresh=True)
        if addline:
            self.console.print('\n')
        self.console.print(self.table)
        if len(exceptions) > 0:
            self.handle_exception(exceptions, False)

    def c_force_update(self, args):
        if args:
            self.c_update(args, False, True, True)
        else:
            self.console.print(
                '[green]Usage:[/green]\n\tThis command accepts a comma-separated list of links or addon '
                'names as an argument.')

    def c_status(self, args):
        self.c_update(args, False, False)

    def c_orphans(self, _):
        orphansd, orphansf = self.core.find_orphans()
        self.console.print(
            '[green]Directories that are not part of any installed addon:[/green]'
        )
        for orphan in sorted(orphansd):
            self.console.print(
                orphan.replace('[GIT]', '[yellow][[GIT]][/yellow]'))
        self.console.print(
            '\n[green]Files that are leftovers after no longer installed addons:[/green]'
        )
        for orphan in sorted(orphansf):
            self.console.print(orphan)

    def c_uri_integration(self, _):
        if self.os == 'Windows':
            self.core.create_reg()
            self.console.print(
                'CurseBreaker.reg file was created. Attempting to import...')
            out = os.system('"' + str(
                Path(os.path.dirname(sys.executable), 'CurseBreaker.reg')) +
                            '"')
            if out != 0:
                self.console.print(
                    'Import failed. Please try to import REG file manually.')
            else:
                os.remove('CurseBreaker.reg')
        else:
            self.console.print('This feature is available only on Windows.')

    def c_toggle_dev(self, args):
        if args:
            status = self.core.dev_toggle(args)
            if status is None:
                self.console.print(
                    '[bold red]This addon doesn\'t exist or it is not installed yet.[/bold red]'
                )
            elif status == 0:
                self.console.print(
                    'Addon switched to the [yellow]beta[/yellow] channel.')
            elif status == 1:
                self.console.print(
                    'Addon switched to the [red]alpha[/red] channel.')
            elif status == 2:
                self.console.print(
                    'Addon switched to the [green]stable[/green] channel.')
        else:
            self.console.print(
                '[green]Usage:[/green]\n\tThis command accepts an addon name as an argument.'
            )

    def c_toggle_backup(self, _):
        status = self.core.backup_toggle()
        self.console.print(
            'Backup of WTF directory is now:',
            '[green]ENABLED[/green]' if status else '[red]DISABLED[/red]')

    def c_toggle_wa(self, args):
        if args:
            if args == self.core.config['WAUsername']:
                self.console.print(
                    f'WeakAuras version check is now: [green]ENABLED[/green]\nAuras created by '
                    f'[white]{self.core.config["WAUsername"]}[/white] are now included.'
                )
                self.core.config['WAUsername'] = ''
            else:
                self.core.config['WAUsername'] = args.strip()
                self.console.print(
                    f'WeakAuras version check is now: [green]ENABLED[/green]\nAuras created by '
                    f'[white]{self.core.config["WAUsername"]}[/white] are now ignored.'
                )
        else:
            if self.core.config['WAUsername'] == 'DISABLED':
                self.core.config['WAUsername'] = ''
                self.console.print(
                    'WeakAuras version check is now: [green]ENABLED[/green]')
            else:
                self.core.config['WAUsername'] = '******'
                shutil.rmtree(Path('Interface/AddOns/WeakAurasCompanion'),
                              ignore_errors=True)
                self.console.print(
                    'WeakAuras version check is now: [red]DISABLED[/red]')
        self.core.save_config()

    def c_set_wa_api(self, args):
        if args:
            self.console.print('Wago API key is now set.')
            self.core.config['WAAPIKey'] = args.strip()
            self.core.save_config()
        elif self.core.config['WAAPIKey'] != '':
            self.console.print('Wago API key is now removed.')
            self.core.config['WAAPIKey'] = ''
            self.core.save_config()
        else:
            self.console.print(
                '[green]Usage:[/green]\n\tThis command accepts API key as an argument.'
            )

    def c_set_wa_wow_account(self, args):
        if args:
            args = args.strip()
            if os.path.isfile(
                    Path(f'WTF/Account/{args}/SavedVariables/WeakAuras.lua')):
                self.console.print(
                    f'WoW account name set to: [white]{args}[/white]')
                self.core.config['WAAccountName'] = args
                self.core.save_config()
            else:
                self.console.print('Incorrect WoW account name.')
        else:
            self.console.print(
                '[green]Usage:[/green]\n\tThis command accepts the WoW account name as an argument.'
            )

    def c_wa_update(self, _, verbose=True):
        if os.path.isdir(Path('Interface/AddOns/WeakAuras')):
            accounts = self.core.detect_accounts()
            if len(accounts) == 0:
                return
            elif len(accounts) > 1 and self.core.config['WAUsername'] == '':
                if verbose:
                    self.console.print(
                        'More than one WoW account detected.\nPlease use [white]set_wa_wow_account[/whit'
                        'e] command to set the correct account name.')
                else:
                    self.console.print(
                        '\n[green]More than one WoW account detected.[/green]\nPlease use [white]set_wa_'
                        'wow_account[/white] command to set the correct account name.'
                    )
                return
            elif len(accounts) == 1 and self.core.config['WAUsername'] == '':
                self.core.config['WAAccountName'] = accounts[0]
                self.core.save_config()
            wa = WagoUpdater(self.core.config['WAUsername'],
                             self.core.config['WAAccountName'],
                             self.core.config['WAAPIKey'])
            if self.core.waCompanionVersion != self.core.config[
                    'WACompanionVersion']:
                self.core.config[
                    'WACompanionVersion'] = self.core.waCompanionVersion
                self.core.save_config()
                force = True
            else:
                force = False
            wa.parse_storage()
            status = wa.check_updates()
            wa.install_companion(self.core.clientType, force)
            wa.install_data()
            if verbose:
                self.console.print('[green]Outdated WeakAuras:[/green]')
                for aura in status[0]:
                    self.console.print(aura, highlight=False)
                self.console.print('\n[green]Detected WeakAuras:[/green]')
                for aura in status[1]:
                    self.console.print(aura, highlight=False)
            else:
                self.console.print(
                    f'\n[green]The number of outdated WeakAuras:[/green] {len(status[0])}',
                    highlight=False)
        elif verbose:
            self.console.print('WeakAuras addon is not installed.')

    def c_search(self, args):
        if args:
            results = self.core.search(args)
            self.console.print('[green]Top results of your search:[/green]')
            for url in results:
                if self.core.check_if_installed(url):
                    self.console.print(f'{url} [yellow][[Installed]][/yellow]',
                                       highlight=False)
                else:
                    self.console.print(url, highlight=False)
        else:
            self.console.print(
                '[green]Usage:[/green]\n\tThis command accepts a search query as an argument.'
            )

    def c_import(self, args):
        hit, partial_hit, miss = self.core.detect_addons()
        if args == 'install' and len(hit) > 0:
            self.c_install(','.join(hit))
        else:
            self.console.print(f'[green]Addons found:[/green]')
            for addon in hit:
                self.console.print(addon)
            self.console.print(f'\n[yellow]Possible matches:[/yellow]')
            for addon in partial_hit:
                self.console.print(' [white]or[/white] '.join(addon))
            self.console.print(f'\n[red]Unknown directories:[/red]')
            for addon in miss:
                self.console.print(f'{addon}')
            self.console.print(
                f'\nExecute [white]import install[/white] command to install all detected addons.\nPossi'
                f'ble matches need to be installed manually with the [white]install[/white] command.'
            )

    def c_export(self, _):
        self.console.print(self.core.export_addons(), highlight=False)

    def c_help(self, _):
        self.console.print(
            '[green]install [URL][/green]\n\tCommand accepts a comma-separated list of links.\n'
            '[green]uninstall [URL/Name][/green]\n\tCommand accepts a comma-separated list of links or'
            ' addon names.\n'
            '[green]update [URL/Name][/green]\n\tCommand accepts a comma-separated list of links or ad'
            'don names.\n\tIf no argument is provided all non-modified addons will be updated.\n'
            '[green]force_update [URL/Name][/green]\n\tCommand accepts a comma-separated list of links'
            ' or addon names.\n\tSelected addons will be reinstalled or updated regardless of their cu'
            'rrent state.\n'
            '[green]wa_update[/green]\n\tCommand detects all installed WeakAuras and generate WeakAura'
            's Companion payload.\n'
            '[green]status[/green]\n\tPrints the current state of all installed addons.\n'
            '[green]orphans[/green]\n\tPrints list of orphaned directories and files.\n'
            '[green]search [Keyword][/green]\n\tExecutes addon search on CurseForge.\n'
            '[green]import[/green]\n\tCommand attempts to import already installed addons.\n'
            '[green]export[/green]\n\tCommand prints list of all installed addons in a form suitable f'
            'or sharing.\n'
            '[green]toggle_backup[/green]\n\tEnables/disables automatic daily backup of WTF directory.'
            '\n[green]toggle_dev [Name][/green]\n\tCommand accepts an addon name as argument.\n\tPrior'
            'itizes alpha/beta versions for the provided addon.\n'
            '[green]toggle_wa [Username][/green]\n\tEnables/disables automatic WeakAuras updates.\n\tI'
            'f a username is provided check will start to ignore the specified author.\n'
            '[green]set_wa_api [API key][/green]\n\tSets Wago API key required to access private auras'
            '.\n\tIt can be procured here: [link=https://wago.io/account]https://wago.io/account[/link]'
            '\n[green]set_wa_wow_account [Account name][/green]\n\tSets WoW account used by WeakAuras up'
            'dater.\n\tNeeded only if WeakAuras are used on more than one WoW account.\n'
            '[green]uri_integration[/green]\n\tEnables integration with CurseForge page.\n\t[i]"Install"'
            '[/i] button will now start this application.\n'
            '\n[bold green]Supported URL:[/bold green]\n\thttps://www.curseforge.com/wow/addons/[[addon_'
            'name]] [bold white]|[/bold white] cf:[[addon_name]]\n\thttps://www.wowinterface.com/downloa'
            'ds/[[addon_name]] [bold white]|[/bold white] wowi:[[addon_id]]\n\thttps://www.tukui.org/add'
            'ons.php?id=[[addon_id]] [bold white]|[/bold white] tu:[[addon_id]]\n\thttps://www.tukui.org'
            '/classic-addons.php?id=[[addon_id]] [bold white]|[/bold white] tuc:[[addon_id]]\n\tElvUI [b'
            'old white]|[/bold white] ElvUI:Dev\n\tTukui\n\tSLE:Dev',
            highlight=False)

    def c_exit(self, _):
        sys.exit(0)
Пример #14
0
class CustomProgress(Progress):
    class CompletedColumn(ProgressColumn):
        def render(self, task):
            """Calculate common unit for completed and total."""
            download_status = f"{int(task.completed)}/{int(task.total)} btc"
            return Text(download_status, style="progress.download")

    class TransferSpeedColumn(ProgressColumn):
        """Renders human readable transfer speed."""
        def render(self, task):
            """Show data transfer speed."""
            speed = task.speed
            if speed is None:
                return Text("?", style="progress.data.speed")
            speed = f"{speed:,.{2}f}"
            return Text(f"{speed} btc/s", style="progress.data.speed")

    def __init__(self, *args, use_info_table: bool = True, **kwargs):
        super(CustomProgress, self).__init__(*args, **kwargs)

        self.info_table = Table(show_footer=False)
        self.info_table.add_column("Phase")

        self.test_style = "black on white"
        self.train_style = "white on black"
        self.use_info_table = use_info_table

    def add_info_table_cols(self, new_cols):
        """
        Add cols from ordered dict if not present in info_table
        """

        cols = set([x.header for x in self.info_table.columns])
        missing = set(new_cols) - cols
        if len(missing) == 0:
            return

        # iterate on new_cols since they are in order
        for c in new_cols:
            if c in missing:
                self.info_table.add_column(c)

    def update_info_table(self, aux: Dict[str, float], phase: str):
        """
        Update the info_table with the latest results
        :param aux:
        :param phase: either 'train' or 'test'
        """

        self.add_info_table_cols(aux.keys())
        epoch = aux.pop("epoch")
        aux = OrderedDict((k, f"{v:4.3f}") for k, v in aux.items())
        if phase == "train":
            st = self.train_style
        else:
            st = self.test_style
        self.info_table.add_row(phase,
                                str(epoch),
                                *list(aux.values()),
                                style=st)

    def get_renderables(self) -> Iterable[RenderableType]:
        """Display progress together with info table"""

        # this method is called once before the init, so check if the attribute is present
        if hasattr(self, "use_info_table"):
            use_table = self.use_info_table
            info_table = self.info_table
        else:
            use_table = False
            info_table = Table()

        if use_table:
            task_table = self.make_tasks_table(self.tasks)
            rendable = Columns((info_table, task_table),
                               align="left",
                               expand=True)
        else:
            rendable = self.make_tasks_table(self.tasks)

        yield rendable
Пример #15
0
 def comparison(renderable1: RenderableType, renderable2: RenderableType) -> Table:
     table = Table(show_header=False, pad_edge=False, box=None, expand=True)
     table.add_column("1", ratio=1)
     table.add_column("2", ratio=1)
     table.add_row(renderable1, renderable2)
     return table
Пример #16
0
def upload(
    api: BaiduPCSApi,
    from_to_list: List[FromTo],
    ondup: str = "overwrite",
    encrypt_key: Any = None,
    salt: Any = None,
    encrypt_type: EncryptType = EncryptType.No,
    max_workers: int = CPU_NUM,
    slice_size: int = DEFAULT_SLICE_SIZE,
    ignore_existing: bool = True,
    show_progress: bool = True,
):
    """Upload from_tos

    Args:
        max_workers (int): The number of concurrent workers
        slice_size (int): The size of slice for uploading slices.
        ignore_existing (bool): Ignoring these localpath which of remotepath exist.
        show_progress (bool): Show uploading progress.
    """

    excepts = {}
    semaphore = Semaphore(max_workers)
    with _progress:
        with ThreadPoolExecutor(max_workers=max_workers) as executor:
            futs = {}
            for from_to in from_to_list:
                semaphore.acquire()
                task_id = None
                if show_progress:
                    task_id = _progress.add_task("upload",
                                                 start=False,
                                                 title=from_to.from_)

                fut = executor.submit(
                    sure_release,
                    semaphore,
                    upload_file,
                    api,
                    from_to,
                    ondup,
                    encrypt_key=encrypt_key,
                    salt=salt,
                    encrypt_type=encrypt_type,
                    slice_size=slice_size,
                    ignore_existing=ignore_existing,
                    task_id=task_id,
                )
                futs[fut] = from_to

            for fut in as_completed(futs):
                e = fut.exception()
                if e is not None:
                    from_to = futs[fut]
                    excepts[from_to] = e

    # Summary
    if excepts:
        table = Table(title="Upload Error", box=SIMPLE, show_edge=False)
        table.add_column("From", justify="left", overflow="fold")
        table.add_column("To", justify="left", overflow="fold")
        table.add_column("Error", justify="left")

        for from_to, e in sorted(excepts.items()):
            table.add_row(from_to.from_, Text(str(e), style="red"))

        _progress.console.print(table)
Пример #17
0
def SelectNetwork():
    # Fetch and select the organization
    print('\n\nFetching organizations...\n')
    organizations = dashboard.organizations.getOrganizations()
    organizations.sort(key=return_name)
    ids = []
    table = Table(title="Meraki Organizations")
    table.add_column("Organization #",
                     justify="left",
                     style="cyan",
                     no_wrap=True)
    table.add_column("Org Name", justify="left", style="cyan", no_wrap=True)
    counter = 0
    for organization in organizations:
        ids.append(organization['id'])
        table.add_row(str(counter), organization['name'])
        counter += 1
    console = Console()
    console.print(table)
    isOrgDone = False
    while isOrgDone == False:
        selected = input(
            '\nKindly select the organization ID you would like to query: ')
        try:
            if int(selected) in range(0, counter):
                isOrgDone = True
            else:
                print('\t[bold red]Invalid Organization Number\n')
        except:
            print('\t[bold red]Invalid Organization Number\n')
    # Fetch and select the network within the organization
    print('\n\nFetching networks...\n')
    networks = dashboard.organizations.getOrganizationNetworks(
        organizations[int(selected)]['id'])
    networks.sort(key=return_name)
    ids = []
    table = Table(title="Available Networks")
    table.add_column("Network #", justify="left", style="green", no_wrap=True)
    table.add_column("Network Name",
                     justify="left",
                     style="green",
                     no_wrap=True)
    counter = 0
    for network in networks:
        ids.append(network['id'])
        table.add_row(str(counter), network['name'])
        counter += 1
    console = Console()
    console.print(table)
    isNetDone = False
    while isNetDone == False:
        selected = input(
            '\nKindly select the Network you would like to query: ')
        try:
            if int(selected) in range(0, counter):
                isNetDone = True
            else:
                print('\t[bold red]Invalid Organization Number\n')
        except:
            print('\t[bold red]Invalid Organization Number\n')
    return (networks[int(selected)]['id'])
Пример #18
0
def print_table(rows: List[BuildRow]):
    if not rows:
        console.print("No rows to print", style="bold red")
        return

    table = Table(show_header=True, header_style="bold blue")
    table.add_column("Team", style="bold yellow")
    table.add_column("Mins", style="dim", justify="right")
    table.add_column("Start Date", style="dim")
    table.add_column("End Date", style="dim")
    table.add_column("Elapsed", justify="right")
    table.add_column("Used", justify="right")

    for build_row in rows:
        style = "red" if build_row.percent_used > build_row.percent_elapsed else "green"
        table.add_row(
            build_row.team,
            f"{build_row.used} mins",
            f"{build_row.start_date:%Y-%m-%d}",
            f"{build_row.end_date:%Y-%m-%d}",
            f"[{style}]{build_row.percent_elapsed:.1f}%[/{style}]",
            f"[{style}]{build_row.percent_used:.1f}%[/{style}]",
        )

    console.print(table)
Пример #19
0
from rich.table import Table
from rich.console import Console
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib as mpl

from constants import persistence_file

mpl.use('Agg')
episodes = Table(show_header=True)
all_rewards = []
console = Console()
episodes.add_column("Episode")
episodes.add_column("Average Reward")
episodes.add_column("Episode Reward")
episodes.add_column("Epsilon")


def track_reward(dqn, _episode, episode_reward):
    all_rewards.append(episode_reward)


def append_row(dqn, episode, episode_reward):
    episodes.add_row(str(dqn.episodes_run), str(np.average(all_rewards)),
                     str(episode_reward), str(dqn.epsilon))


def clear_console(_1, _2, _3):
    console.clear()
Пример #20
0
def check_task(browser):
    # table = PrettyTable(["每日登录", "选读文章", "视频数量", "视频时长", "每日答题", "每周答题", "专项答题", "今日累计积分", "成长总积分"])
    table = Table(show_header=True, header_style="bold black")
    table.add_column("每日登录", justify='center')
    table.add_column("选读文章", justify='center')
    table.add_column("视频数量", justify='center')
    table.add_column("视频时长", justify='center')
    table.add_column("每日答题", justify='center')
    table.add_column("每周答题", justify='center')
    table.add_column("专项答题", justify='center')
    table.add_column("今日累计积分", justify='center')
    table.add_column("成长总积分", justify='center')
    tableRow = []
    settingsPath = 'data/settings.json'
    with open(settingsPath, 'r', encoding='utf-8') as f:
        settings = f.read()
    # print(settings)
    settings = json.loads(settings)

    exam_temp_Path = './data/exam_temp.json'
    with open(exam_temp_Path, 'r', encoding='utf-8') as f:
        exam_temp = f.read()
    exam_temp = json.loads(exam_temp)

    res = CheckResType.NULL
    browser.get('https://www.xuexi.cn/index.html')
    time.sleep(round(random.uniform(1, 3), 2))
    browser.get('https://pc.xuexi.cn/points/my-points.html')
    browser.implicitly_wait(3)
    time.sleep(round(random.uniform(1, 3), 2))

    # 每日登录积分
    login = browser.find_element_by_xpath(
        '//*[@id="app"]/div/div[2]/div/div[3]/div[2]/div[1]/div[2]/div[1]/div[2]'
    )
    tableRow.append(login.text.strip())

    # 选读文章积分
    article = browser.find_element_by_xpath(
        '/html/body/div[1]/div/div[2]/div/div[3]/div[2]/div[2]/div[3]/div[1]/div[2]'
    )
    tableRow.append(article.text.strip())
    # 视听学习积分
    video = browser.find_element_by_xpath(
        '/html/body/div[1]/div/div[2]/div/div[3]/div[2]/div[3]/div[2]/div[1]/div[2]'
    )
    tableRow.append(video.text.strip())
    # 视听学习时长积分
    video_time = browser.find_element_by_xpath(
        '/html/body/div[1]/div/div[2]/div/div[3]/div[2]/div[4]/div[2]/div[1]/div[2]'
    )
    tableRow.append(video_time.text.strip())
    # 每日答题积分
    daily = browser.find_element_by_xpath(
        '//*[@id="app"]/div/div[2]/div/div[3]/div[2]/div[5]/div[2]/div[1]/div[2]'
    )
    tableRow.append(daily.text.strip())
    # 每周答题积分
    weekly = browser.find_element_by_xpath(
        '//*[@id="app"]/div/div[2]/div/div[3]/div[2]/div[6]/div[2]/div[1]/div[2]'
    )
    tableRow.append(weekly.text.strip())
    # 专项答题积分
    special = browser.find_element_by_xpath(
        '//*[@id="app"]/div/div[2]/div/div[3]/div[2]/div[7]/div[2]/div[1]/div[2]'
    )
    tableRow.append(special.text.strip())
    # 今日积分
    todayPoints = browser.find_element_by_xpath(
        '//*[@id="app"]/div/div[2]/div/div[2]/div[2]/span[3]')
    tableRow.append(todayPoints.text.strip())
    # 总积分
    allPoints = browser.find_element_by_xpath(
        '//*[@id="app"]/div/div[2]/div/div[2]/div[2]/span[1]')
    tableRow.append(allPoints.text.strip())

    # 打印表格
    table.add_row(tableRow[0], tableRow[1], tableRow[2], tableRow[3],
                  tableRow[4], tableRow[5], tableRow[6], tableRow[7] + '分',
                  tableRow[8] + '分')
    print(table)

    if settings['浏览文章'] == "true" and article.text != '12分/12分':
        res = CheckResType.ARTICLE
    if settings['观看视频'] == "true" and (video.text != '6分/6分'
                                       or video_time.text != '6分/6分'):
        if res == CheckResType.ARTICLE:
            res = CheckResType.ARTICLE_AND_VIDEO
        else:
            res = CheckResType.VIDEO

    # 检查设置文件
    if settings['自动答题'] != 'true':
        return res

    if settings[
            '每日答题'] == 'true' and res == CheckResType.NULL and daily.text != '5分/5分':
        res = CheckResType.DAILY_EXAM
    if exam_temp['WEEKLY_EXAM'] == 'true' and settings[
            '每周答题'] == 'true' and res == CheckResType.NULL and weekly.text != '5分/5分':
        res = CheckResType.WEEKLY_EXAM
    if exam_temp['SPECIAL_EXAM'] == 'true' and settings[
            '专项答题'] == 'true' and res == CheckResType.NULL and special.text != '10分/10分':
        res = CheckResType.SPECIAL_EXAM

    return res
Пример #21
0
    def __init__(self,
                 name="test",
                 n_tracks=1,
                 bpm=60,
                 resolution=960,
                 to_file=True,
                 to_port=False):

        self.music = Music()

        self.console = Console()

        self.logs = []

        self.to_port = to_port

        self.console.rule("[bold green] Initializing player [/]")

        # file declaration
        self.file = mido.MidiFile()

        # composing file name
        self.filename = "mid/" + name + ".mid"

        self.tracks = []

        # record of all played instructions
        self.history = []

        # keep track of playing status
        self.is_playing = False

        self.resolution = resolution  # resolução ppq
        self.log("\t➜ Resolution: {} ticks/beat".format(resolution))
        self.file.ticks_per_beat = self.resolution

        # default: 4/4
        self.time_signature = None
        self.set_time_signature(TimeSignature(4, 4, upbeats=[1]))

        self.tempo = mido.bpm2tempo(bpm)
        self.log("\t➜ Tempo set to {} bpm ({} μs/beat)".format(
            mido.tempo2bpm(self.tempo), self.tempo))

        # appending tracks
        for i in range(1, n_tracks + 1):
            track = mido.MidiTrack()
            self.tracks.append(track)
            self.file.tracks.append(self.tracks[i - 1])
            self.log("\t➜ Track {} created and added to MID file".format(i -
                                                                         1))

        #
        #                        OUTPUT PORTS
        #
        mido.set_backend('mido.backends.rtmidi')
        self.out_port = None

        if to_port:
            outs = mido.get_output_names()

            table = Table(title="\n[yellow]Available output ports")
            table.add_column("#", style="green bold", justify="left")
            table.add_column("Outport name", style="white", justify="center")
            for id, port in enumerate(outs):
                table.add_row(str(id), port)

            self.console.print(table, justify="center")
            i = int(
                self.console.input(
                    "\t\t\t ➜ Select by output id: [yellow bold]# "))
            self.out_port = mido.open_output(outs[i])
            self.log(
                "\n\t\t ➜ Selected out port: \n\t\t[bold green]{}[/]\n".format(
                    self.out_port.name))

        #
        #               DONE
        #

        self.console.rule("")
        self.console.print("[bold green][✓] Player created [/]",
                           justify="center")
        self.console.rule("")
        self.console.print("[bold] Press enter to start", justify="center")
        self.console.input()
        self.console.rule("")
Пример #22
0
 def display_neighborhood(info_city):
     table = Table(show_header=True, header_style="black on green")
     table.add_column("Bairros da cidade", justify="center")
     for item in info_city:
         table.add_row(item["nome"], style='green')
     console.print(table)
Пример #23
0
    def cli_compare_one_query(self, console, q, start_idx, end_idx, docs,
                              run1_name, run2_name):
        docid2rank_run1 = defaultdict(lambda: "Not ranked")
        for doc in q["run_1"]:
            docid2rank_run1[doc["doc_id"]] = doc["rank"]

        docid2rank_run2 = defaultdict(lambda: "Not ranked")
        for doc in q["run_2"]:
            docid2rank_run2[doc["doc_id"]] = doc["rank"]

        self.print_query_to_console(q, console)

        # rprint(query_panel)
        table = Table(
            show_header=True,
            header_style="bold red",
            title=
            "Comparision [bold]run#1:[/bold] [red]{}[/red] vs [bold]run#2:[/bold] [red]{}[/red]"
            .format(run1_name, run2_name),
            show_lines=True,
            expand=True,
        )
        table.add_column("DocID", justify="center", style="cyan", no_wrap=True)
        table.add_column("Run #1", justify="left", style="magenta")
        table.add_column("Rel", justify="center", style="green")
        table.add_column("Snippet", justify="full", style="white")
        table.add_column(" ")
        table.add_column("DocID", justify="left", style="cyan", no_wrap=True)
        table.add_column("Run #2", justify="left", style="magenta")
        table.add_column("Rel", justify="center", style="green")
        table.add_column("Snippet", justify="full", style="white")

        def handle_non(rel):
            if rel == None:
                return "Unjudged"
            else:
                return str(rel)

        for run1_doc, run2_doc in zip(q["run_1"][start_idx:end_idx],
                                      q["run_2"][start_idx:end_idx]):
            snp_1 = self.render_snippet_for_cli(run1_doc["doc_id"],
                                                run1_doc["snippet"], docs)
            snp_2 = self.render_snippet_for_cli(run2_doc["doc_id"],
                                                run2_doc["snippet"], docs)
            table.add_row(
                run1_doc["doc_id"],
                "[bold]Score[/bold]: {}\n[bold]Rank[/bold]:{}\n[bold]Rank in run#2[/bold]: {}"
                .format(run1_doc["score"], str(run1_doc["rank"]),
                        docid2rank_run2[run1_doc["doc_id"]]),
                handle_non(run1_doc["relevance"]),
                snp_1,
                " ",
                run2_doc["doc_id"],
                "[bold]Score[/bold]: {}\n[bold]Rank[/bold]:{}\n[bold]Rank in run#1[/bold]: {}"
                .format(run2_doc["score"], str(run2_doc["rank"]),
                        docid2rank_run1[run2_doc["doc_id"]]),
                handle_non(run2_doc["relevance"]),
                snp_2,
            )
        console.print(table)
Пример #24
0
# rich_live.py
import time
import random
from datetime import datetime

from rich.live import Live
from rich.table import Table

table = Table()
table.add_column("Row ID")
table.add_column("Description")
table.add_column("Level")

levels = ["[red]FATAL", "[magenta]CRITICAL", "[yellow]MAJOR", "[cyan]warning"]

with Live(table, refresh_per_second=4):  # update 4 times a second to feel fluid
    for row in range(12):
        time.sleep(0.4)  # arbitrary delay
        # update the renderable internally
        table.add_row(str(row), "issue at {}".format(datetime.now()), random.choice(levels))
Пример #25
0
def module1():
# Домен > IPv4/v6
    def res46(dipp):
        try:
            res46 = socket.getaddrinfo(f"{dipp}", 80)
        except:
            pass
        try:
            res4 = res46[0][4][0]
        except:
            res4 = "-"
        try:
            if ":" not in res46[-1][4][0]:
                res6 = "-"
            else:
                res6 = res46[-1][4][0]
        except:
            res6 = "-"
        #print(res46)
        return res4, res6

# Запрос future request
    def reqZ():
        try:
            r=req.result()
            return r.text
        except requests.exceptions.ConnectionError:
            print(Fore.RED + "\nОшибка соединения\n" + Style.RESET_ALL)
        except requests.exceptions.Timeout:
            print(Fore.RED + "\nОшибка таймаут\n" + Style.RESET_ALL)
        except requests.exceptions.RequestException:
            print(Fore.RED + "\nОшибка не идентифицирована\n" + Style.RESET_ALL)
        except requests.exceptions.HTTPError:
            print(Fore.RED + "\nОшибка HTTPS\n" + Style.RESET_ALL)
        return "Err"

# Выбор поиска одиночный или '-f'
    ravno()
    print("\n\033[36mВведите домен (пример:\033[0m \033[32;1mexample.com\033[0m\033[36m), или IPv4/IPv6 (пример:\033[0m \033[32;1m8.8.8.8\033[0m\033[36m),\n\
или url (пример: \033[32;1mhttps://example.com/1/2/3/foo\033[0m\033[36m), \n\
или укажите файл_с данными, выбрав ключ (пример:\033[0m \033[32;1m--file\033[0m\033[36m или\033[0m \033[32;1m-f\033[0m\033[36m)\n\
[\033[0m\033[32;1m-f\033[0m\033[36m] --> обработатка файла данных\n\
[\033[0m\033[32;1menter\033[0m\033[36m] --> информация о своем GEO_IP\n\
[\033[0m\033[31;1mq\033[0m\033[36m] --> Выход")
    dip = input("\n")

# выход
    if dip == "q":
        print(Style.BRIGHT + Fore.RED + "Выход")
        sys.exit()

# проверка данных
    elif dip == '--file' or dip == '-f':
        while True:
            print("""\033[36m├──Выберите тип поиска
│
[\033[0m\033[32;1m1\033[0m\033[36m] --> Online (медленно)
[\033[0m\033[32;1m2\033[0m\033[36m] --> Offline (быстро)
[\033[0m\033[32;1m3\033[0m\033[36m] --> Offline_тихий (очень быстро)
[\033[0m\033[32;1mhelp\033[0m\033[36m] --> Справка\n\
[\033[31;1mq\033[0m\033[36m] --> Выход\033[0m""")

            dipbaza = input('\n')

# Выход
            if dipbaza == "q":
                print("\033[31;1mВыход\033[0m")
                sys.exit(0)
# Справка
            elif dipbaza == "help":
                print("\033[32;1m└──Справка\033[0m\n")
                print("""\033[32mРежим '\033[32;1mOnline поиск\033[0m\033[32m'. Модуль GEO_IP/domain от Snoop Project использует публичный api
и создает статистическую и визуализированную информацию по ip/url/domain цели (массиву данных)
    (ограничения: запросы ~15к/час, невысокая скорость обработки данных, отсутствие информации о провайдерах).
Преимущества использования 'Online поиска':
в качестве входных данных можно использовать не только ip-адреса, но и domain/url.
Пример файла с данными (список.txt):

\033[36m1.1.1.1
2606:2800:220:1:248:1893:25c8:1946
google.com
https://example.org/fo/bar/7564
случайная строка\033[0m

\033[32mРежим '\033[32;1mOffline поиск\033[0m\033[32m'. Модуль GEO_IP/domain от Snoop Project использует специальные базы данных
и создает статистическую и визуализированную информацию только по ip цели (массиве данных)
    (базы данных доступны свободно от компании Maxmind).
Преимущества использования 'Offline поиска': скорость (обработка тысяч ip без задержек),
стабильность (отсутствие зависимости от интернет соединения и персональных настроек DNS/IPv6 пользователя),
масштабный охват/покрытие (предоставляется информация об интернет-провайдерах).

Режим '\033[32;1mOffline_тихий поиск\033[0m\033[32m':: Тот же режим, что и режим 'Offline', но не выводит на печать промежуточные таблицы с данными.
Даёт прирост в производительности в ~4 раза.
Пример файла с данными (список.txt):

\033[36m8.8.8.8
93.184.216.34
2606:2800:220:1:248:1893:25c8:1946
случайная строка\033[0m

\033[32mSnoop довольно умён и способен определять и различать во входных данных: IPv4/v6/domain/url, вычищая ошибки и случайные строки.
По окончанию обработки данных пользователю предоставляются:
статистические отчеты в [txt/csv и визуализированные данные на карте OSM].

Примеры для чего можно использовать модуль GEO_IP/domain от Snoop Project.
Например, если у пользователя имеется список ip адресов от DDoS атаки,
он может проанализировать откуда исходила  max/min атака и от кого (провайдеры).
Решая квесты-CTF, где используются GPS/IPv4/v6. 
В конечном итоге юзать плагин в образовательных целях или из естественного любопытства (проверить любые ip-адреса и их принадлежность к провайдеру и местности).\033[0m""")
                helpend()

# Оффлайн поиск
# Открываем GeoCity
            elif dipbaza == "2" or dipbaza == "3":
                while True:
                    print("\033[31;1m└──В Demo version этот метод плагина недоступен\033[0m\n")
                    donate()
                    break

                break

# Онлайн поиск
            elif dipbaza == "1":
                print("\033[31;1m└──В Demo version этот метод плагина недоступен\033[0m\n")
                donate()
                break

# Неверный выбор ключа при оффлайн/онлайн поиске. Выход
            else:
                print(Style.BRIGHT + Fore.RED + "└──Неверный выбор" + Style.RESET_ALL)
                ravno()

# одиночный запрос
    else:
        if dip == "":
            pass
            uu3 = dip
        else:
            u = urlparse(dip).hostname
            uu3 = dip
            if bool(u) == False:
                dip=dip.split("/")[0].strip()
            else:
                dip=u.replace("www.", "").strip()
        session = requests.Session()
        url2 = 'https://freegeoip.app/json/{}'.format(dip)
        try:
            r=session.get(url=url2, headers = head0, timeout=3)
            dip1 = r.text
            dip_dic = json.loads(dip1)
            T1=dip_dic.get("country_code")
            T2=dip_dic.get("time_zone")
            T3=dip_dic.get("latitude")
            T4=dip_dic.get("longitude")
            T5=dip_dic.get("ip")
        except:
            T1="-"
            T2="-"
            T3="stop"
            T4="stop"
            T5="-"
            print("""\033[31;1m\n
|\ | _ ._  _
| \|(_)| |(/_
        \033[0m""")

# IP/Домен > Домен и IPv4v6
        try:
            resD1=socket.getfqdn(dip)
            res4, res6 = res46(resD1)
        except:
            resD1="-"
            print("err")

        table = Table(title = Style.BRIGHT + Fore.RED + str(uu3) + Style.RESET_ALL, style="green")
        table.add_column("Сountry", style="magenta")
        if dip == "":
            table.add_column("Your IP", style="cyan", overflow="fold")
        else:
            table.add_column("IPv4", style="cyan", overflow="fold")
            table.add_column("IPv6", style="cyan", overflow="fold")
        table.add_column("Domain", style="green", overflow="fold")
        table.add_column("Time_Zone", style="green", overflow="fold")
        if dip == "":
            table.add_row(T1,T5,resD1,T2)
        else:
            table.add_row(T1,res4,res6,resD1,T2)
        console.print(table)
        if T3 == "stop" and T4 =="stop":
            print("\n")
            URL_GEO = ""
        else:
            URL_GEO = f"https://www.openstreetmap.org/#map=13/{T3}/{T4}"
            URL_GEO2 = f"https://www.google.com/maps/@{T3},{T4},28m/data=!3m1!1e3"
            print(Style.BRIGHT + Fore.BLACK + f"{URL_GEO}" + Style.RESET_ALL)
            print(Style.BRIGHT + Fore.BLACK + f"{URL_GEO2}\n" + Style.RESET_ALL)

        module1()
Пример #26
0
def _display_all_jobs(logger, rows):
    console = Console()
    table = Table(show_header=True, title="List of Jobs", box=box.DOUBLE_EDGE)
    header_style = "bold blue"
    table.add_column(header="Id", header_style=header_style)
    table.add_column(header="Name", header_style=header_style)
    table.add_column(header="Type", header_style=header_style)
    table.add_column(header="Tags", header_style=header_style)
    table.add_column(
        header="Analyzers\nCalled", justify="center", header_style=header_style
    )
    table.add_column(
        header="Process\nTime(s)", justify="center", header_style=header_style
    )
    table.add_column(header="Status", header_style=header_style)
    try:
        for el in rows:
            table.add_row(
                str(el["id"]),
                el["observable_name"] if el["observable_name"] else el["file_name"],
                el["observable_classification"]
                if el["observable_classification"]
                else el["file_mimetype"],
                ", ".join([t["label"] for t in el["tags"]]),
                el["no_of_analyzers_executed"],
                str(el["process_time"]),
                get_status_text(el["status"]),
            )
        console.print(table, justify="center")
    except Exception as e:
        logger.fatal(e)
Пример #27
0
def print_methods_table(found, class_obj, name):
    """
    Prints a table with the methods found by search_class_method

    :param found: dictionary with found functions
    :param class_obj: class obj. Where the methods where searched in
    :param name: str, None. Query string
    """

    # make rich table
    table = Table(
        show_header=True,
        header_style="bold magenta",
        box=box.SIMPLE,
    )
    table.add_column("#", style="dim", width=3, justify="center")
    table.add_column("name", style="bold " + lightgreen)
    table.add_column("Class", justify="left")
    table.add_column("", style="bold " + lightgreen)

    table.add_column("Module", justify="left")
    table.add_column("Signature")

    # list methods
    count = 0
    for obj, methods in found.items():
        for k, v in methods.items():
            if not isfunction(v):
                continue  # skip docstrings etc

            lineno = inspect.getsourcelines(getattr(class_obj, k))[1]
            cs = get_class_that_defined_method(v)
            sig = textify(str(signature(cs)), maxlen=50)

            if cs == class_obj:
                cs = f"[{lightgreen}]{_name(cs)}[/{lightgreen}]"
                method_name = k
            else:
                cs = f"[{salmon}]{_name(cs)}[/{salmon}]"
                method_name = f"[{salmon}]{k}[/{salmon}]"

            module = f"[white]{_module(v)} [dim](line: {lineno})"

            table.add_row(
                str(count),
                method_name,
                cs,
                "",
                module,
                sig,
            )
            count += 1

    st = f"bold black on {mocassin}"
    console.print(
        f"\n[{mocassin}]Looking for methods of [{st}] {_name(class_obj)} ({_module(class_obj)}) [/{st}] with query name: [{st}] {name} [/{st}]:",
        table,
    )
Пример #28
0
def make_test_card() -> Table:
    """Get a renderable that demonstrates a number of features."""
    table = Table.grid(padding=1, pad_edge=True)
    table.title = "Rich features"
    table.add_column("Feature", no_wrap=True, justify="center", style="bold red")
    table.add_column("Demonstration")

    color_table = Table(
        box=None,
        expand=False,
        show_header=False,
        show_edge=False,
        pad_edge=False,
    )
    color_table.add_row(
        # "[bold yellow]256[/] colors or [bold green]16.7 million[/] colors [blue](if supported by your terminal)[/].",
        (
            "✓ [bold green]4-bit color[/]\n"
            "✓ [bold blue]8-bit color[/]\n"
            "✓ [bold magenta]Truecolor (16.7 million)[/]\n"
            "✓ [bold yellow]Dumb terminals[/]\n"
            "✓ [bold cyan]Automatic color conversion"
        ),
        ColorBox(),
    )

    table.add_row("Colors", color_table)

    table.add_row(
        "Styles",
        "All ansi styles: [bold]bold[/], [dim]dim[/], [italic]italic[/italic], [underline]underline[/], [strike]strikethrough[/], [reverse]reverse[/], and even [blink]blink[/].",
    )

    lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque in metus sed sapien ultricies pretium a at justo. Maecenas luctus velit et auctor maximus."
    lorem_table = Table.grid(padding=1, collapse_padding=True)
    lorem_table.pad_edge = False
    lorem_table.add_row(
        Text(lorem, justify="left", style="green"),
        Text(lorem, justify="center", style="yellow"),
        Text(lorem, justify="right", style="blue"),
        Text(lorem, justify="full", style="red"),
    )
    table.add_row(
        "Text",
        Group(
            Text.from_markup(
                """Word wrap text. Justify [green]left[/], [yellow]center[/], [blue]right[/] or [red]full[/].\n"""
            ),
            lorem_table,
        ),
    )

    def comparison(renderable1: RenderableType, renderable2: RenderableType) -> Table:
        table = Table(show_header=False, pad_edge=False, box=None, expand=True)
        table.add_column("1", ratio=1)
        table.add_column("2", ratio=1)
        table.add_row(renderable1, renderable2)
        return table

    table.add_row(
        "Asian\nlanguage\nsupport",
        ":flag_for_china:  该库支持中文,日文和韩文文本!\n:flag_for_japan:  ライブラリは中国語、日本語、韓国語のテキストをサポートしています\n:flag_for_south_korea:  이 라이브러리는 중국어, 일본어 및 한국어 텍스트를 지원합니다",
    )

    markup_example = (
        "[bold magenta]Rich[/] supports a simple [i]bbcode[/i]-like [b]markup[/b] for [yellow]color[/], [underline]style[/], and emoji! "
        ":+1: :apple: :ant: :bear: :baguette_bread: :bus: "
    )
    table.add_row("Markup", markup_example)

    example_table = Table(
        show_edge=False,
        show_header=True,
        expand=False,
        row_styles=["none", "dim"],
        box=box.SIMPLE,
    )
    example_table.add_column("[green]Date", style="green", no_wrap=True)
    example_table.add_column("[blue]Title", style="blue")
    example_table.add_column(
        "[cyan]Production Budget",
        style="cyan",
        justify="right",
        no_wrap=True,
    )
    example_table.add_column(
        "[magenta]Box Office",
        style="magenta",
        justify="right",
        no_wrap=True,
    )
    example_table.add_row(
        "Dec 20, 2019",
        "Star Wars: The Rise of Skywalker",
        "$275,000,000",
        "$375,126,118",
    )
    example_table.add_row(
        "May 25, 2018",
        "[b]Solo[/]: A Star Wars Story",
        "$275,000,000",
        "$393,151,347",
    )
    example_table.add_row(
        "Dec 15, 2017",
        "Star Wars Ep. VIII: The Last Jedi",
        "$262,000,000",
        "[bold]$1,332,539,889[/bold]",
    )
    example_table.add_row(
        "May 19, 1999",
        "Star Wars Ep. [b]I[/b]: [i]The phantom Menace",
        "$115,000,000",
        "$1,027,044,677",
    )

    table.add_row("Tables", example_table)

    code = '''\
def iter_last(values: Iterable[T]) -> Iterable[Tuple[bool, T]]:
    """Iterate and generate a tuple with a flag for last value."""
    iter_values = iter(values)
    try:
        previous_value = next(iter_values)
    except StopIteration:
        return
    for value in iter_values:
        yield False, previous_value
        previous_value = value
    yield True, previous_value'''

    pretty_data = {
        "foo": [
            3.1427,
            (
                "Paul Atreides",
                "Vladimir Harkonnen",
                "Thufir Hawat",
            ),
        ],
        "atomic": (False, True, None),
    }
    table.add_row(
        "Syntax\nhighlighting\n&\npretty\nprinting",
        comparison(
            Syntax(code, "python3", line_numbers=True, indent_guides=True),
            Pretty(pretty_data, indent_guides=True),
        ),
    )

    markdown_example = """\
# Markdown

Supports much of the *markdown* __syntax__!

- Headers
- Basic formatting: **bold**, *italic*, `code`
- Block quotes
- Lists, and more...
    """
    table.add_row(
        "Markdown", comparison("[cyan]" + markdown_example, Markdown(markdown_example))
    )

    table.add_row(
        "+more!",
        """Progress bars, columns, styled logging handler, tracebacks, etc...""",
    )
    return table
y_raw.drop(list(range(768, 840)), inplace=True)

# standardizing the raw data
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X_raw)
y_scaled = scaler.fit_transform(y_raw)

# test train split
X_train, X_test, y_train, y_test = train_test_split(X_scaled,
                                                    y_scaled,
                                                    shuffle=False,
                                                    test_size=0.3)

# create table
table = Table(title='xgb results')
table.add_column('learning rate')
table.add_column('n_estimators')
table.add_column('max_depth')
table.add_column('R2')

# def xgb(max_depth, n_estimators, learning_rate):
#     xgb = XGBRegressor(max_depth=max_depth, n_estimators=n_estimators, learning_rate=learning_rate)
#     xgb.fit(X_train, y_train)
#     y_pred = xgb.predict(X_test)
#     r2 = r2_score(y_test, y_pred)
#     return r2

# # using xgboost
# max_r2 = 0
# for lr in np.linspace(0.1, 0.6, 30):
#     for n in range(20, 100):
Пример #30
0
                "taille_zip": file_info.compress_size,
                "pays": countries[file_info.filename[:-4]],
            }
            lire_png(fh, drapeau)
            all_files.append(drapeau)

list_files = sorted(all_files, key=itemgetter("png_ratio"))

# Code supplémentaire pour affichage du résultat dans le terminal

from rich.console import Console
from rich.table import Table

console = Console()
table = Table(show_header=True, header_style="bold magenta")
table.add_column("Fichier")
table.add_column("Pays")
table.add_column("PNG ratio", justify="right")
table.add_column("ZIP ratio", justify="right")

for elt in list_files[:5]:
    table.add_row(
        elt["fichier"],
        elt["pays"],
        f"{elt['png_ratio']:.2f}",
        f"{elt['zip_ratio']:.2f}",
    )

table.add_row()

for elt in list_files[-5:]: