"""This module contain tests for detail views."""
import datetime
from django.test import TestCase
from django.utils import timezone
from django.urls import reverse
from polls.models import Question

def create_question(question_text, days, end=None):
    """Create a question with the given text and published day offset to now"""
    time = timezone.now() + datetime.timedelta(days=days)
    if end is not None:
        end_time = timezone.now() + datetime.timedelta(days=end)
        return Question.objects.create(question_text=question_text,
                                       pub_date=time, end_date=end_time)
    return Question.objects.create(question_text=question_text, pub_date=time)

class DetailViewTests(TestCase):
    """The test class for tests detail view."""

    def test_future_question(self):
        """The detail view of a question with a pub_date in the future should
        return to index page.
        """
        future_question = create_question(question_text='Future question.',
                                          days=5)
        url = reverse('polls:detail', args=(future_question.id,))
        response = self.client.get(url)
        self.assertEqual(response.status_code, 302)
        self.assertRedirects(response, reverse("polls:index"))

    def test_past_question(self):
        """The detail view of a question with a pub_date in the past displays
        the question's text.
        """
        past_question = create_question(question_text='Past Question.',
                                        days=-5)
        url = reverse('polls:detail', args=(past_question.id,))
        response = self.client.get(url)
        self.assertContains(response, past_question.question_text)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.test import TestCase
from .forms import ContactAddForm
from django import forms
from django.conf import settings

# Create your tests here.'full_name', 'email', 'enquiry')
class ContactTest(TestCase):

    def test_contact_form(self):
        form = ContactAddForm({
            'full_name': 'Tester Man',
            'email': 'TesterMan@gmail.com',
            'enquiry': 'Please quote for boiler service',
        })

        self.assertTrue(form.is_valid())

    def test_contact_form_missing_email(self):
        form = ContactAddForm({
            'full_name': 'Tester Boy',
            'enquiry': 'Please quote for boiler repair',
        })

        self.assertFalse(form.is_valid())
        self.assertRaisesMessage(forms.ValidationError,
                                 "Please enter your email address",
                                 form.full_clean())


import pandas as pd
from badmintoncleaner import prepare_dataset
import ast
import torch
from tqdm import tqdm
import numpy as np
import os
import re

def calculate_average_column_vectors(csv_files):
    # Create a list to store the column vectors
    column_vectors = []

    # Iterate over each CSV file
    for csv_file in csv_files:
        df = pd.read_csv(csv_file)
        last_row = df.iloc[-1, 1:].values
        column_vectors.append(last_row.tolist())

    # Calculate the average for each column vector
    averages = np.mean(column_vectors, axis=0)

    return averages

def find_optimal_area_fold(csv_files):

    fold = {}
    record_score_list = []
    for csv_file in os.listdir(csv_files):
        if 'record_score' in csv_file:
            df = pd.read_csv(csv_files + '/' + csv_file)
            area_value = df.iloc[-1, 2]

            match = re.search(r'\d+', csv_file)
            if match:
                number = match.group()
            else:
                number = 'NAN'
                print("No number found in the string.")
            fold.update({number: area_value})
    fold_with_min_value_area = min(fold, key=lambda x: fold[x])
    return fold_with_min_value_area

def find_optimal_shot_fold(csv_files):

    fold = {}
    record_score_list = []
    for csv_file in os.listdir(csv_files):
        if 'record_score' in csv_file:
            df = pd.read_csv(csv_files + '/' + csv_file)
            shot_value = df.iloc[-1, 2]
            match = re.search(r'\d+', csv_file)
            if match:
                number = match.group()
            else:
                number = 'NAN'
                print("No number found in the string.")
            fold.update({number: shot_value})
    fold_with_min_value_shot = min(fold, key=lambda x: fold[x])
    return fold_with_min_value_shot

def set_seed(seed_value):
    torch.manual_seed(seed_value)
    torch.cuda.manual_seed(seed_value)
    torch.cuda.manual_seed_all(seed_value)  # gpu vars

def generate_final_csv(area_model_path, shot_model_path, area_fold=None, shot_fold=None):
    SAMPLES = 6  # set to 6 to meet the requirement of this challenge

    # Model for area prediction
    if area_fold is not None:
        config_area = ast.literal_eval(open(f"{area_model_path}/config{area_fold}").readline())
    else:
        config_area = ast.literal_eval(open(f"{area_model_path}/config").readline())

    # Model for shot type prediction
    if shot_fold is not None:
        config_shot = ast.literal_eval(open(f"{shot_model_path}/config{shot_fold}").readline())
    else:
        config_shot = ast.literal_eval(open(f"{shot_model_path}/config").readline())

    set_seed(config_area['seed_value'])

    # Prepare Dataset
    config_area, train_dataloader, val_dataloader, test_dataloader, train_matches, val_matches, test_matches = prepare_dataset(
        config_area)

    config_shot, train_dataloaders, val_dataloaders, test_dataloaders, train_matchess, val_matchess, test_matchess = prepare_dataset(
        config_shot)

    device = torch.device(f"cuda:{0}" if torch.cuda.is_available() else "cpu")
    print(device)
    # load model
    from MuLMINet.MuLMINet import ShotGenEncoder_MuLMINet_Variant1, ShotGenPredictor_MuLMINet_Variant1, \
        ShotGenEncoder_MuLMINet_Variant2, ShotGenPredictor_MuLMINet_Variant2
    from MuLMINet.MuLMINet_runner import shotgen_generator_MuLMINet

    encoder_area, decoder_area, encoder_shot, decoder_shot = None, None, None, None

    if 'Variant1' in area_model_path:
        encoder_area = ShotGenEncoder_MuLMINet_Variant1(config_area)
        decoder_area = ShotGenPredictor_MuLMINet_Variant1(config_area)
    elif 'Variant2' in area_model_path:
        encoder_area = ShotGenEncoder_MuLMINet_Variant2(config_area)
        decoder_area = ShotGenPredictor_MuLMINet_Variant2(config_area)

    if 'Variant1' in shot_model_path:
        encoder_shot = ShotGenEncoder_MuLMINet_Variant1(config_shot)
        decoder_shot = ShotGenPredictor_MuLMINet_Variant1(config_shot)
    elif 'Variant2' in shot_model_path:
        encoder_shot = ShotGenEncoder_MuLMINet_Variant2(config_shot)
        decoder_shot = ShotGenPredictor_MuLMINet_Variant2(config_shot)

    encoder_area.to(device), decoder_area.to(device)
    encoder_shot.to(device), decoder_shot.to(device)

    current_model_path_area = area_model_path + '/'
    current_model_path_shot = shot_model_path + '/'

    if area_fold is not None:
        encoder_path_area = current_model_path_area + 'encoder' + str(area_fold)
        decoder_path_area = current_model_path_area + 'decoder' + str(area_fold)
    else:
        encoder_path_area = current_model_path_area + 'encoder'
        decoder_path_area = current_model_path_area + 'decoder'

    if shot_fold is not None:
        encoder_path_shot = current_model_path_shot + 'encoder' + str(shot_fold)
        decoder_path_shot = current_model_path_shot + 'decoder' + str(shot_fold)
    else:
        encoder_path_shot = current_model_path_shot + 'encoder'
        decoder_path_shot = current_model_path_shot + 'decoder'

    saved_encoder_checkpoint_area = torch.load(encoder_path_area, map_location=device)
    saved_decoder_checkpoint_area = torch.load(decoder_path_area, map_location=device)
    encoder_area.load_state_dict(saved_encoder_checkpoint_area)
    decoder_area.load_state_dict(saved_decoder_checkpoint_area)

    saved_encoder_checkpoint_shot = torch.load(encoder_path_shot, map_location=device)
    saved_decoder_checkpoint_shot = torch.load(decoder_path_shot, map_location=device)
    encoder_shot.load_state_dict(saved_encoder_checkpoint_shot)
    decoder_shot.load_state_dict(saved_decoder_checkpoint_shot)

    encode_length = config_shot['encode_length']
    saved_model_path = './MulMINET_IJCAI'
    performance_log = open(f"{saved_model_path}prediction_final.csv", "a")

    performance_log.write(
        'rally_id,sample_id,ball_round,landing_x,landing_y,short service,net shot,lob,clear,drop,push/rush,smash,defensive shot,drive,long service')
    performance_log.write('\n')

    # get all testing rallies
    testing_rallies = test_matches['rally_id'].unique()

    for rally_id in tqdm(testing_rallies):
        # read data
        selected_matches = test_matches.loc[(test_matches['rally_id'] == rally_id)][
            ['rally_id', 'set', 'rally_length', 'ball_round', 'type', 'landing_x', 'landing_y', 'landing_height',
             'aroundhead', 'backhand', 'player', 'player_location_area', 'opponent_location_area']].reset_index(
            drop=True)

        generated_length = selected_matches['rally_length'][0]  # fetch the length of the current rally
        players = [selected_matches['player'][0], selected_matches['player'][1]]
        target_players = torch.tensor([players[shot_index % 2] for shot_index in
                                       range(generated_length - len(selected_matches))])  # get the predicted players

        given_seq = {
            'given_player': torch.tensor(selected_matches['player'].values).to(device),
            'given_shot': torch.tensor(selected_matches['type'].values).to(device),
            'given_x': torch.tensor(selected_matches['landing_x'].values).to(device),
            'given_y': torch.tensor(selected_matches['landing_y'].values).to(device),
            'given_ball_height': torch.tensor(selected_matches['landing_height'].values).to(device),
            'given_ball_aroundhead': torch.tensor(selected_matches['aroundhead'].values).to(device),
            'given_ball_backhand': torch.tensor(selected_matches['backhand'].values).to(device),
            'given_ball_player_area': torch.tensor(selected_matches['player_location_area'].values).to(device),
            'given_ball_opponent_area': torch.tensor(selected_matches['opponent_location_area'].values).to(device),
            'target_player': target_players.to(device),
            'rally_length': generated_length
        }

        # feed into the model for shot prediction
        generated_shot_final, generated_area = shotgen_generator_MuLMINet(given_seq=given_seq, encoder=encoder_shot,
                                                                    decoder=decoder_shot, config=config_shot, samples=SAMPLES,
                                                                    device=device)
        # feed into the model for area prediction
        generated_shot, generated_area_final = shotgen_generator_MuLMINet(given_seq=given_seq, encoder=encoder_area,
                                                                    decoder=decoder_area, config=config_area, samples=SAMPLES,
                                                                    device=device)

        # print('length of generated area : ', len(generated_area))

        # store the prediction results
        for sample_id in range(len(generated_area_final)):
            for ball_round in range(len(generated_area_final[0])):
                # print('ball_round : ', ball_round + config['encode_length'] + 1)
                performance_log.write(
                    f"{rally_id},{sample_id},{ball_round + config_area['encode_length'] + 1},{generated_area_final[sample_id][ball_round][0]},{generated_area_final[sample_id][ball_round][1]},")
                for shot_id, shot_type_logits in enumerate(generated_shot_final[sample_id][ball_round]):
                    performance_log.write(f"{shot_type_logits}")
                    if shot_id != len(generated_shot_final[sample_id][ball_round]) - 1:
                        performance_log.write(",")
                performance_log.write("\n")

if __name__ == "__main__":
    model_path = '../../../MulMINET_IJCAI/'
    os.environ['CUDA_LAUNCH_BLOCKING'] = "1"
    import os
    optimal_parameter_area = {}
    optimal_parameter_shot = {}
    for model_type in os.listdir(model_path):
        record_score_files = []

        for file_name in os.listdir(model_path + model_type + '/'):
            if "record_score" in file_name:
                record_score_files.append(model_path + model_type + '/' + file_name)

        average_vector = calculate_average_column_vectors(record_score_files)
        # Save the parameter and average value with 5-fold cross validation
        optimal_parameter_area.update({model_type : average_vector[1]})
        optimal_parameter_shot.update({model_type: average_vector[0]})

    key_with_min_value_area = min(optimal_parameter_area, key=lambda x: optimal_parameter_area[x])
    key_with_min_value_shot = min(optimal_parameter_shot, key=lambda x: optimal_parameter_shot[x])
    print('Optimal paramter for predicting area distribution')
    print(key_with_min_value_area)
    area_model_path = '../../../MulMINET_IJCAI/' + key_with_min_value_area

    print('Optimal paramter for predicting shot type')
    print(key_with_min_value_shot)
    shot_model_path = '../../../MulMINET_IJCAI/' + key_with_min_value_shot

    optimal_area_fold = find_optimal_area_fold(area_model_path)
    optimal_shot_fold = find_optimal_shot_fold(shot_model_path)

    generate_final_csv(area_model_path, shot_model_path, area_fold=optimal_area_fold, shot_fold=optimal_shot_fold)


## Bibliotecas necessárias
import pandas as pd
import seaborn as sn

import statsmodels.formula.api as sm
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

## Leitura do Arquivo
dataframe = pd.read_csv('https://raw.githubusercontent.com/REXDES/N1-IA-Projeto/main/2%20Year%20IBM%20Stock%20Data.csv')
dataframe = dataframe.dropna()      # Tira os dados nulos

# Adicionando coluna superavit, que classifica binariamente se uma ação
# subiu ou não, em relação ao preço de abertura e fechamento do dia.
dataframe['superavit'] = (dataframe['close'] > dataframe['open'])/1
dataframe.head()

## Regressão Linear

# Criando os dados
x = dataframe.drop(columns=['superavit'])
y = dataframe['superavit']

# Treinamento
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, stratify=y, random_state=1984)

# Base de teste para ser carregada no Modelo
new_dataframe = pd.concat([X_train, y_train], axis=1)

# Modelo
model = sm.ols(formula="superavit ~ open + close", data=new_dataframe)

# Resultados
result = model.fit()
print(result.summary())

y_pred = result.predict(X_test)

# Retornando os dados float para binariedade
y_pred = (y_pred > 0.5)/1

acuracidade = sum(y_pred == y_test)*100 / len(y_pred)
print(f"\n\nAcuracidade: {acuracidade:.3f}.")

### Regressão Logísitca
df_filtrado = dataframe[['superavit', 'open', 'close']]

x = df_filtrado.drop(columns=['superavit'])
y = df_filtrado['superavit']

#separando modelo para treinamento/teste
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, stratify=y, random_state=1984)

reg_logistica = LogisticRegression()
reg_logistica.fit(X_train, y_train)

y_pred = reg_logistica.predict(X_test)

acuracidade = sum(y_pred == y_test)*100 / len(y_pred)
print(f"Acuracidade: {acuracidade:.3f}.")

confusion_matrix = pd.crosstab(y_test, y_pred, rownames=['Actual'], colnames=['Predicted'])
sn.heatmap(confusion_matrix, annot=True)


import os
import shutil
import pytest

REPODIR = '/root/repoid/yum.repos.d'
REPOFILENAME = 'repoid.repo'
REPONAME = "repoid-test"
WORKDIR = '/root/repoid/workdir'

@pytest.fixture(scope='function', autouse=True)
def setup_test(utils):
    yield
    teardown_test(utils)

def teardown_test(utils):
    if os.path.isdir(REPODIR):
        shutil.rmtree(REPODIR)
    if os.path.isdir(WORKDIR):
        shutil.rmtree(WORKDIR)
    filename = os.path.join(utils.config['repo_path'], "yum.repos.d", REPOFILENAME)
    if os.path.isfile(filename):
        os.remove(filename)

def test_repoid(utils):
    utils.makedirs(REPODIR)
    utils.create_repoconf(os.path.join(REPODIR, REPOFILENAME),
                          "http://foo.bar.com/packages",
                          REPONAME)
    ret = utils.run(['tdnf',
                     '--setopt=reposdir={}'.format(REPODIR),
                     '--repoid={}'.format(REPONAME),
                     'repolist'])
    assert ret['retval'] == 0
    assert REPONAME in "\n".join(ret['stdout'])

def test_repo(utils):
    utils.makedirs(REPODIR)
    utils.create_repoconf(os.path.join(REPODIR, REPOFILENAME),
                          "http://foo.bar.com/packages",
                          REPONAME)
    ret = utils.run(['tdnf',
                     '--setopt=reposdir={}'.format(REPODIR),
                     '--repo={}'.format(REPONAME),
                     'repolist'])
    assert ret['retval'] == 0
    assert REPONAME in "\n".join(ret['stdout'])

# reposync a repo and install from it
def test_repoid_created_repo(utils):
    reponame = 'photon-test'
    workdir = WORKDIR
    utils.makedirs(workdir)

    ret = utils.run(['tdnf', '--repo={}'.format(reponame),
                     '--download-metadata',
                     'reposync'],
                    cwd=workdir)
    assert ret['retval'] == 0
    synced_dir = os.path.join(workdir, reponame)
    assert os.path.isdir(synced_dir)
    assert os.path.isdir(os.path.join(synced_dir, 'repodata'))
    assert os.path.isfile(os.path.join(synced_dir, 'repodata', 'repomd.xml'))

    filename = os.path.join(utils.config['repo_path'], "yum.repos.d", REPOFILENAME)
    baseurl = "file://{}".format(synced_dir)

    utils.create_repoconf(filename, baseurl, "synced-repo")

    ret = utils.run(['tdnf',
                     '--repo=synced-repo',
                     'makecache'],
                    cwd=workdir)
    assert ret['retval'] == 0

    pkgname = utils.config["mulversion_pkgname"]
    utils.erase_package(pkgname)
    ret = utils.run(['tdnf',
                     '-y', '--nogpgcheck',
                     '--repo=synced-repo',
                     'install', pkgname],
                    cwd=workdir)
    assert ret['retval'] == 0
    assert utils.check_package(pkgname)


from __future__ import print_function, absolute_import

import sys

from docopt import docopt
import six

import camel_tools as camelt
from camel_tools.utils.stringutils import force_encoding, force_unicode
from camel_tools.utils.charmap import CharMapper

__version__ = camelt.__version__

def _open_files(finpath, foutpath):
    if finpath is None:
        fin = sys.stdin
    else:
        try:
            fin = open(finpath, 'r', encoding='utf-8')
        except Exception:
            sys.stderr.write('Error: Couldn\'t open input file {}.'
                             '\n'.format(repr(finpath)))
            sys.exit(1)

    if foutpath is None:
        fout = sys.stdout
    else:
        try:
            fout = open(foutpath, 'w', encoding='utf-8')
        except Exception:
            sys.stderr.write('Error: Couldn\'t open output file {}.'
                             '\n'.format(repr(foutpath)))
            if finpath is not None:
                fin.close()
            sys.exit(1)

    return fin, fout

def _arclean(mapper, fin, fout):
    for line in fin:
        line = force_unicode(line)

        if six.PY3:
            fout.write(mapper.map_string(line))
        else:
            fout.write(force_encoding(mapper.map_string(line)))
    fout.flush()

def main():  # pragma: no cover
    try:
        version = ('CAMeL Tools v{}'.format(__version__))
        arguments = docopt(__doc__, version=version)

        # Open files (or just use stdin and stdout)
        fin, fout = _open_files(arguments['FILE'], arguments['--output'])

        try:
            mapper = CharMapper.builtin_mapper('arclean')
            _arclean(mapper, fin, fout)

        # If everything worked so far, this shouldn't happen
        except Exception:
            sys.stderr.write('Error: An error occured during cleaning.\n')
            fin.close()
            fout.close()
            sys.exit(1)

            # Cleanup
            if arguments['FILE'] is not None:
                fin.close()
            if arguments['--output'] is not None:
                fout.close()

        sys.exit(0)
    except KeyboardInterrupt:
        sys.stderr.write('Exiting...\n')
        sys.exit(1)
    except Exception:
        sys.stderr.write('Error: An unknown error occurred.\n')
        sys.exit(1)

if __name__ == '__main__':  # pragma: no cover
    main()


from types import NoneType

import cloudinary.uploader
import cloudinary.api
from io import BytesIO
from PIL.Image import Image as ImageClass

from django.core.handlers.wsgi import WSGIRequest
from django.http import HttpResponse
from django.http.response import ResponseHeaders, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods

from webpeditor_app.api.api_utils.response_presets import unauthorized_access_response
from webpeditor_app.models.database.models import EditedImage
from webpeditor_app.services.image_services.image_service import (
    get_original_image,
    get_edited_image,
)
from webpeditor_app.services.external_api_services.request_service import (
    extract_image_edit_data_from_request_body,
)
from webpeditor_app.services.other_services.session_service import (
    get_unsigned_user_id,
    update_session,
)

@csrf_exempt
@require_http_methods(["POST"])
def image_save_api(request: WSGIRequest):
    if request.method == "POST":
        user_id: str | None = get_unsigned_user_id(request)
        if isinstance(user_id, NoneType):
            return unauthorized_access_response()

        if request.session.get_expiry_age() == 0:
            return unauthorized_access_response()

        original_image = get_original_image(user_id)
        if original_image is None or original_image.user_id != user_id:
            return unauthorized_access_response()

        edited_image = get_edited_image(user_id)
        if edited_image is None:
            return unauthorized_access_response()

        request_body = extract_image_edit_data_from_request_body(request)
        if isinstance(request_body, JsonResponse):
            return request_body

        image_file: ImageClass = request_body[3]

        buffer = BytesIO()
        image_file.save(buffer, format=image_file.format)
        buffer.seek(0)

        resources = cloudinary.api.resources(prefix=f"{user_id}/edited", type="upload")

        public_ids: list = [
            resource["public_id"] for resource in resources["resources"]
        ]

        # Save image to Cloudinary
        cloudinary_parameters: dict = {"public_id": public_ids[0], "overwrite": True}
        cloudinary_image = cloudinary.uploader.upload_image(
            file=buffer, **cloudinary_parameters
        )

        update_session(request=request, user_id=user_id)
        # Update edited image in DB
        EditedImage.objects.filter(user_id=user_id).update(
            image_url=cloudinary_image.url
        )

        response = HttpResponse()
        response.status_code = 200
        response.headers = ResponseHeaders({"Content-Type": "text/javascript"})

        return response


import sys
import json
import transformers
import csv
import re
import random

def normtext(t):
    t=re.sub("<i>|</i>|<br ?/>"," ",t)
    t=re.sub("[0-9]","N",t)
    return t

def proc_text(d):
    d["text"]=" ".join(normtext(d[f]) for f in ("name","synopsis","contentdescription")).strip()

def csv2dict(inp):
    r=csv.DictReader(inp,dialect="excel-tab",fieldnames="filmiri,year,name,synopsis,contentdescription".split(","))
    for i,d in enumerate(r):
        if d["synopsis"]=="None" or d["contentdescription"]=="None":
            continue
        proc_text(d)
        decade=int(d["year"])//10*10
        new_d={"sentence":d["text"], "label":str(decade), "id":str(i)}
        yield new_d

if __name__=="__main__":
    a=[]
    for d in csv2dict(sys.stdin):
        a.append(d)
    random.shuffle(a)
    for d in a:
        print(json.dumps(d,ensure_ascii=False,sort_keys=True))


# -*- coding: utf-8 -*-
"""
Created on Thu Nov 26 21:17:11 2020

@author: Felix
"""
"""this implementation utilizes https://github.com/taynaud/python-louvain/tree/master/community"""

import time
import numpy as np
import networkx as nx
import community as cl

G=nx.read_gpickle("LFR1000")
n=len(G.nodes())
st=time.time()

partition = cl.best_partition(G)

et=time.time()

ex_time=(et-st)*1000 #time in milliseconds

print(ex_time, partition)

communities=[]
for i in range(n):
    comm=[]
    for j in partition:
        if partition[j]==i:
            comm.append(j)
    communities.append(set(comm))

#print(communities)
modularity=nx.algorithms.community.quality.modularity (G, communities)
print(modularity)


from __future__ import (
    division, absolute_import, print_function, unicode_literals)

import argparse
import os
import os.path
import pathlib
import re
import sys

from typing import (
    cast,
    Callable, Iterator, List, NamedTuple, Optional, Sequence, Set, TextIO)
from typing import Dict, Tuple  # NOQA, pylint: disable=unused-import

SYMBOL_SLIDES = 'slides'

COMMAND_RE = '[a-z#]+'
ARGUMENT_RE = '\\S+'

def gen_parse_line(
        regexp_str: str) -> Callable[[str], Optional[Sequence[str]]]:
    """Return a function parsing input lines for commands."""
    regexp = re.compile(regexp_str.format(COMMAND_RE, ARGUMENT_RE))

    def result(line: str) -> Optional[Sequence[str]]:
        """Syntax specific line parser."""
        match_obj = regexp.match(line)
        if not match_obj:
            return None
        return match_obj.groups()

    return result

LINE_PARSER = {
    'hash': gen_parse_line(r'^\s*#({})(?:\s+({}))?$'),
    'html': gen_parse_line(r'^\s*<!--\s*({})(?:\s+({}))?\s*-->\s*$'),
}

class FileInfo(object):
    """Wrap a file object to gain more data about reading it."""

    def __init__(self, prev: Optional['FileInfo'], fileobj: TextIO) -> None:
        """Intitialize with a file object."""
        self.prev = prev
        self._fileobj = fileobj
        self.name = fileobj.name
        self.directory = None if self.name == "<stdin>" \
            else os.path.dirname(self.name)
        self.line = 0
        self._eof = False

    def readline(self) -> str:
        """Return the next line.

        IMPORTANT: if an end-of-file is detected, an empty line is returned.
        This is because of some markdown problems if several include commands
        are in subsequent lines. This situation can be detected by a double
        line number.
        """
        if self._eof:
            return ''
        line = self._fileobj.readline()
        if not line:
            self._eof = True
            return "\n"
        self.line += 1
        return line

    def close(self) -> Optional['FileInfo']:
        """Close the file object."""
        self._fileobj.close()
        return self.prev

    def is_already_open(self, filename: str) -> bool:
        """Determine whether the given file is already opened."""
        current = self  # type: Optional[FileInfo]
        while current is not None:
            if current.name == filename:
                return True
            current = current.prev
        return False

    def __str__(self) -> str:
        """Return printable information about file object."""
        return "{}, line {}".format(self._fileobj, self.line)

    def __repr__(self) -> str:
        """Return representation of file object."""
        return "<{}>".format(str(self))

Config = NamedTuple('Config', [
    ('root', str),
    ('base', str),
    ('includes', List[str]),
    ('symbols', Set[str])])

class SlidePreprocessor(object):
    """Read all given input files and pre-process them."""

    def __init__(
            self,
            config: Config,
            files: List[TextIO],
            parse_line: Callable) -> None:
        """Initialize a slide preprocessor."""
        self._config = config
        self._files = list(files)
        self._current = None  # type: Optional[FileInfo]
        self._next_file()
        self._parse_line = parse_line
        self._do_emit = True
        self._if_stack = []  # type: List[bool]

        if config.base:
            parts = pathlib.PurePath(config.base).parts
            self._reldir = os.path.join(
                '', *['..' for i in range(len(parts) - 1)])
        else:
            self._reldir = ''

    def _next_file(self) -> None:
        self._current = FileInfo(None, self._files[0])
        self._files = self._files[1:]

    def _emit(self, line: str) -> None:
        if self._do_emit:
            # print(self._current.name, self._current.line, line)
            print(line)

    def _has_symbol(self, symbol: str) -> bool:
        return symbol.lower() in self._config.symbols

    def _handle_comment(  # pylint: disable=no-self-use
            self, _ignored: str) -> None:  # pylint: disable=unused-argument
        """Ignore the whole line."""

    def _handle_page(self) -> None:
        self._emit('')
        if self._has_symbol(SYMBOL_SLIDES):
            self._emit('----')
            self._emit('')

    def _handle_pause(self) -> None:
        if self._has_symbol(SYMBOL_SLIDES):
            self._emit('')
            self._emit('. . .')
            self._emit('')

    def _handle_ifdef(self, symbol: str) -> None:
        self._if_stack.append(self._do_emit)
        self._do_emit = self._has_symbol(symbol)

    def _handle_ifndef(self, symbol: str) -> None:
        self._if_stack.append(self._do_emit)
        self._do_emit = not self._has_symbol(symbol)

    def _handle_elifdef(self, symbol: str) -> None:
        if self._if_stack:
            self._do_emit = self._has_symbol(symbol)

    def _handle_else(self) -> None:
        if self._if_stack:
            self._do_emit = not self._do_emit

    def _handle_endif(self) -> None:
        if self._if_stack:
            self._do_emit = self._if_stack[-1]
            del self._if_stack[-1]

    def _candidate_names(self, filepath: str) -> Iterator[str]:
        """Iterate through al search directories, yielding candidate names."""
        current_dir = cast(FileInfo, self._current).directory
        if current_dir is not None:
            search_list = [current_dir] + self._config.includes
        else:
            search_list = list(self._config.includes)
        for dir_name in search_list:
            candidate_name = os.path.join(dir_name, filepath)
            yield candidate_name

    def _handle_include(self, filepath: str) -> None:
        recursive = False
        for candidate_name in self._candidate_names(filepath):
            if cast(FileInfo, self._current).is_already_open(candidate_name):
                recursive = True
                continue
            try:
                new_file = open(candidate_name, "r", encoding="utf-8")
                self._current = FileInfo(self._current, new_file)
                return
            except FileNotFoundError:
                pass
        if recursive:
            self._emit("Recursive include: {}".format(filepath))
        else:
            self._emit("File not found: {}".format(filepath))

    def _handle_image(self, filepath: str) -> None:
        for candidate_name in self._candidate_names(filepath):
            if not os.path.isfile(candidate_name):
                continue
            assert candidate_name.startswith(self._config.root)
            candidate_name = candidate_name[len(self._config.root)+1:]
            # TODO this needs to be elaborated
            # Preprocessor must know sliders Filename<->URL mapping
            candidate_url = "." + candidate_name
            if self._reldir:
                candidate_url = os.path.join(self._reldir, candidate_name)
            else:
                candidate_url = "/" + candidate_name
            self._emit("![]({})\\ ".format(candidate_url))
            return
        self._emit("Image not found: {}".format(filepath))

    ARG_COMMANDS = {
        '#': (_handle_comment, False),
        'ifdef': (_handle_ifdef, True),
        'ifndef': (_handle_ifndef, True),
        'elifdef': (_handle_elifdef, True),
        'include': (_handle_include, False),
        'image': (_handle_image, False),
    }  # type: Dict[str, Tuple[Callable[[SlidePreprocessor, str], None], bool]]
    NOARG_COMMANDS = {
        'page': (_handle_page, False),
        'pause': (_handle_pause, False),
        'else': (_handle_else, True),
        'endif': (_handle_endif, True),
    }

    def _handle_command(self, command: str, argument: str) -> bool:
        """Process a detected command."""
        arg_handler_info = self.ARG_COMMANDS.get(command)
        if arg_handler_info:
            arg_handler, always = arg_handler_info
            if not self._do_emit and not always:
                return False
            arg_handler(self, argument)
            return True
        noarg_handler_info = self.NOARG_COMMANDS.get(command)
        if noarg_handler_info:
            noarg_handler, always = noarg_handler_info
            if not self._do_emit and not always:
                return False
            noarg_handler(self)
            return True
        return False

    def handle_line(self, line: str) -> None:
        """Process one line."""
        command_tuple = self._parse_line(line)
        if command_tuple:
            command, argument = command_tuple
            if not self._handle_command(command, argument):
                self._emit(line)
        else:
            self._emit(line)

    def _readline(self) -> str:
        """Read one line, switch to the next file."""
        while True:
            if self._current is None:
                if not self._files:
                    return ''
                self._next_file()
            current = cast(FileInfo, self._current)
            result = current.readline()
            if result:
                return result
            self._current = current.close()

    def run(self) -> None:
        """Read and process all input files."""
        while True:
            line = self._readline()
            if not line:
                return
            line = line.rstrip('\n\r')
            self.handle_line(line)

def directory(root: Optional[str], name: Optional[str]) -> str:
    """Handle name of directory."""
    if name is None or not os.path.isdir(name):
        name = os.getcwd()
    result = os.path.abspath(name)
    if root is not None:
        if not result.startswith(root):
            result = root
    return result

def include_directories(includes: List[str]) -> List[str]:
    """Clean all given include directory names."""
    if not includes:
        return []
    result = []
    for name in includes:
        if not os.path.isdir(name):
            continue
        result.append(os.path.abspath(name))
    return result

def main() -> None:
    """Execute the main program."""
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '-R', '--root', metavar='root', help='root directory')
    parser.add_argument(
        '-B', '--base', metavar='root', help='base directory')
    parser.add_argument(
        '-D', '--define', metavar='sym', action='append', help='define symbol')
    parser.add_argument(
        '-s', '--slides', action='store_true',
        help='define symbol "{}"'.format(SYMBOL_SLIDES))
    parser.add_argument(
        '-I', '--include', metavar='dir', action='append',
        help='add directory to include search path')
    parser.add_argument(
        '-P', '--parser', default='hash', choices=('hash', 'html'),
        help='select line parser')
    parser.add_argument(
        'file', nargs='*',
        type=argparse.FileType('r', encoding='utf-8'),
        help='file to read')
    args = parser.parse_args()

    root = directory(None, args.root)
    symbols = set(symbol.lower() for symbol in args.define or [])
    if args.slides:
        symbols.add(SYMBOL_SLIDES)
    config = Config(
        root=root,
        base=directory(root, args.base)[len(root):],
        includes=include_directories(args.include),
        symbols=symbols)
    files = list(args.file) if args.file else [sys.stdin]
    app = SlidePreprocessor(config, files, LINE_PARSER[args.parser])
    try:
        app.run()
    except KeyboardInterrupt:
        pass

if __name__ == '__main__':
    main()


import os
import time
import re
import json
from pathlib import Path
import requests
# because xml is dangerous
from defusedxml import ElementTree as ET
from xml.etree.ElementTree import Element
import pandas as pd
from collections import namedtuple, defaultdict
from tqdm.notebook import tqdm

BASE_URL = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/'

EMAIL = os.environ['NCBI_EMAIL_ADDR']
TOOL = os.environ['NCBI_TOOL_NAME']
# The length of time we wait between making requests to the API.
# The docs imply that you can make up to 3 requests per second without an API
# key, but .5 is a bit safer (and more polite)
SLEEP_INTERVAL = .5
RETURN_RANKS = ['order', 'family', 'genus', 'species']
JSON_PATH = 'data/IDA/json'

# TaxonInfo object
TaxonInfo = namedtuple('TaxonInfo', ['rank', 'sci_name', 'taxon_id', 'lineage'])

def check_ncbi_param(ncbi_param: str, param_name: str):
    """ Input:
            ncbi_param: str - the value of a parameter we're going to pass to
                the NCBI API ("email" or "tool").
            param_name: str - the name of the NCBI parameter
        Output:
            Either reurns ncbi_param unaltered, or raises a value error.

    The NCBI API docs specify that the parameters "email" and "tool" should be
    strings with no internal spaces. I think it's probably fine (they get
    encoded by the requests library - so spaces become "+"), maybe I'll get rid
    of this later when I can test more.
    """
    if (not isinstance(ncbi_param, str)) or (' ' in ncbi_param.strip()):
            raise ValueError(f'"{param_name}" parameter must be a string \
                             containing no internal spaces')
    return ncbi_param

def default_preprocessor(raw_name: str):
    """ Input:
            raw_name: str - the name of an organism. i.e. 'Asellus_aquaticus',
                'Chelifera', 'Ephemerella_aroni_aurivillii', etc.
                The point of this
        Output:
            str - the name of the organism, processed to obviate the issues I
                ran into with the dataset I'm working with right now:
                    - organism names that end with "_sp", "_adult", or "_larva"
                    - organism names that contain more than 2 parts (this may be
                        due to ambiguity - I might reexamine this later)

    This is an example of a preprocessor for the NCBI class. My particular use
    case was processing directory names.
    This isn't perfect, nor is it magic.
    It makes assumptions that might be false (that's why I'm calling it an
    "example preprocessor"). It's moderately likely that you might need to
    write your own processor that's keyed to the idiosyncracies of the
    particular dataset you're working with.
    If there are misspelled names you'll have to go in and change them.

    TODO: think about ading some sensible "space between words" handling other
    than underscores ('+','-', ' ', etc.). Maybe camelcase too
    """
    # if there are numbers in the name, we remove them
    raw_name = re.sub('[0-9]', '', raw_name)
    # gets rid of some extra bits on the end
    # NOTE: might want to add a "suffixes" argument or something to make this
    # configurable
    raw_name = re.sub('_sp$|_adult$|_larva$', '', raw_name)
    parts = raw_name.split('_')
    if len(parts) > 1:
        return '+'.join([parts[0], parts[-1]])
    else:
        return parts[0]

class NCBI:
    def __init__(
            self,
            email: str = EMAIL,
            tool: str = TOOL,
            preprocessor: func = None,
            return_ranks: list = None,
            max_attempts: int = 3,
            timeout: int = 10
            ):
        """ Input:
                email: str - the email address that will be used when making the
                    request to the NCBI API. Must be a valid email with no
                    internal spaces.
                tool: str - the name of application making the E-utility call.
                    Value must be a string with no internal spaces.
                preprocessor: func - a function to pre-process the organism
                    names being fed to this. If None, the default preprocessor
                    is used instead
                return_ranks: list - the ranks that will be kept and returned
                max_attempts: int - the number of retries to make  to the API
                    before giving up
                timeout: int - the length of time in seconds to wait before
                    assuming something went wrong with the request

        Information on parameters, syntax, etc. for the API (including the
        "tool" and "email" parameters for this class) can be found here:
            https://www.ncbi.nlm.nih.gov/books/NBK25499/

        """
        self.email = check_ncbi_param(email, 'email')
        self.tool = check_ncbi_param(tool, 'tool')
        self.return_ranks = return_ranks
        self.disambiguate = []
        self.no_match = []
        self.organisms_known = dict()
        if not preprocessor:
            self.preprocessor = default_preprocessor

    def make_req(
            self,
            url: str,
            payload: dict,
            ):
        """ Input:
                url: str - the url we want to make the request to
                payload: dict - contains the values we want to pass as parameters
                    in the URL's query string

            Output:
                req: requests.Response - the response returned by the NCBI API

        This attempts a GET request, and then waits for a bit and tries again if
        it receives a RequestException. It helps if you've got some transient
        network issues between you and the other fella.
        Also: having all the requests go through this method makes it easier
        to keep track of stuff like the slight delay between requests (to avoid
        being rate limited)
        """
        for i in range(max_attempts + 1):
            try:
                req = requests.get(
                    url,
                    params=payload,
                    timeout=timeout)
                req.raise_for_status()
                time.sleep(SLEEP_INTERVAL)
                return req
            except requests.RequestException as e:
                if i == max_attempts:
                    raise e
                print('connection issue. Waiting 1 second and trying again...')
                time.sleep(1)
        raise requests.HTTPError

    def esearch_req(
            self,
            organism: str):
        """ Input:
                organism: str - the name of the organism whose ID we want.
            Output:
                req: requests.Response - the response returned by the esearch
                    endpoint of the NCBI API for the organism in question.

        The NCBI eutils esearch endpoint can return a list of UIDs that match a
        query.
        Docs are here:
        https://www.ncbi.nlm.nih.gov/books/NBK25499/#chapter4.ESearch
        """
        url = BASE_URL + 'esearch.fcgi'
        payload = {
            'mail': self.email,
            'tool': self.tool,
            'db':'taxonomy',
            'term':organism,
            'rettype':'uilist',
            'retmode':'json'
            }
        req = self.make_req(url, payload)
        return req

    def efetch_req(
            self,
            taxid: int):
        """ Input:
                taxid: int - the taxon ID that we want more information on
            Output:
                req: requests.Response - the response returned by the efetch
                    endpoint of the NCBI API for the taxon in question.

        The NCBI eutils efetch endpoint returns data records for a UID or list of
        UIDs.
        Docs are here:
        https://www.ncbi.nlm.nih.gov/books/NBK25499/#chapter4.EFetch
        """
        url = BASE_URL + 'efetch.fcgi'
        payload = {
            'mail': self.email,
            'tool': self.tool,
            'db':'taxonomy',
            'id':taxid}
        req = self.make_req(url, payload)
        return req

    def organism_to_id(
            self,
            organism: str,
            return_raw: bool = False,
            verbose: bool = False):
        """ Input:
                organism: str - the name of the organism whose ID we want.
                return_raw: bool - if true, returns the raw idlist returned
                    by the API.
                verbose: bool - if true, prints each organism name before
                    starting the process
            Output:
                int - the taxon id for the given organism
        """
        if verbose:
            print('Starting organism:', organism)
        req = self.esearch_req(organism)
        id_list = req.json()['esearchresult']['idlist']
        return id_list

    def etree_from_id(
            self,
            taxid: int):
        """ Input:
                taxid: int - an NCBI taxonomic id.
            Output:
                tree: defusedxml.ElementTree - an element tree containing the
                    lineage information returned by the NCBI efetch API for the
                    organism with the given taxid.
        """
        req = self.efetch_req(taxid)
        tree = ET.fromstring(req.content)
        return tree

    def etree_to_dict(self,
                      root: ET):
        """ Input:
                root: defusedxml.ElementTree - an xml document as returned by
                    the NCBI API efetch endpoint.
            Output:
                taxon_info: TaxonInfo namedtuple - the keys are:
                    - rank: the rank of the organism, i.e. order, phylum, etc.
                    - sci_name: the scientific name of the organism
                    - taxon_id: the taxonomic id (an int)
                    - lineage: a list of dicts. The keys are:
                        - rank:
                        - sci_name:
                        - taxon_id:
        Note: Within taxon_info we have the lineage dictionary, which is a list
        of dicts. We're using a list of dicts because some ranks (specifically
        "clade") can appear multiple times.
        I decided to preserve all of the lineage data at this stage and filter
        out unused entries further down the line, so it will be easy to rework -
        just in case I find a use for the extra data somewhere.
        """
        # This gets us the rank, scientific name, and taxon id for the organism
        root_taxon = root.find('Taxon')
        rank, taxon_info = self.parse_taxon_element(root_taxon)
        taxon_info['rank'] = rank

        # This gets a list of the taxa in the organism's lineage and then
        # creates a dictionary of lists, in which each entry is a dict
        # containing scientific name and taxonomic id
        lineage = defaultdict(list)
        taxa = root_taxon.find('LineageEx').findall('Taxon')
        for taxon in taxa:
            rank, info = self.parse_taxon_element(taxon)
            lineage[rank].append(info)

        taxon_info['lineage'] = lineage
        return TaxonInfo(**taxon_info)

    def parse_taxon_element(
            self,
            taxon: Element):
        ''' Input:
                taxon: Element - the 'Taxon' element from the element tree which
                    we retrieved from the NCBI API
            Output:
                (rank, info): a tuple. "rank" is the rank of the element (i.e.
                    "order", "phylum", etc.), and "info" is a dictionary
                    containing the scientific name and taxon id for that rank.
        '''
        rank = taxon.find('Rank').text
        info = {
            'sci_name': taxon.find('ScientificName').text,
            'taxon_id': int(taxon.find('TaxId').text)
        }
        return rank, info

    def organism_to_dict(
            self,
            organism: str,
            verbose: bool = False):
        ''' Input:
                organism: str - the organism we're interested in
                verbose: bool - if true, some progress info will be printed as
                    the data is retrieved.
            Output:
                tax_dict: dict - a dictionary containing the taxonomy data about
                    "organism" which was returned by the NCBI API
        '''
        taxid_list = self.organism_to_id(organism, verbose)

        # If more than 1 taxon id is returned that means we can't match the name
        # given to a single organism.
        # The user will need to disambiguate the name
        if len(taxid_list) > 1:
            self.disambiguate.append(organism)
            return False

        # If no taxon ids were returned, that means we weren't able to find a
        # match. The user will have to check the spelling of the name.
        if not taxid_list:
            self.no_match.append(organism)
            return False

        if not taxid_list[0].isdigit():
            raise ValueError(f'Expected API to return one taxon id consisting \
                            of all decimal characters. Returned {id_list[0]} \
                            instead.')
        taxid = int(taxid_list[0])
        tree = self.etree_from_id(taxid)
        tax_dict = self.etree_to_dict(tree)
        return tax_dict

    def match(
        self,
        org_name: str,
        verbose: bool = False):
        """ Input:
                org_name: str - the name of the organism we want to try to
                    find a match for in the NCBI database.
                verbose: bool - if True, some information will be printed as
                    the data is retrieved.
            Output:
                taxon_info: TaxonInfo - a named tuple containing the organism's:
                        - rank
                        - scientific name
                        - taxonomic ID
                        - lineage
                    If a match cannot be made, False is returned instead.
        """
        # skips the call to the API if the data for the organism has already
        # been retrieved
        if org_name in self.organisms_known:
            return self.organisms_known[org_name]

        taxon_info = self.organism_to_dict(org_name)
        self.organisms_known[org_name] = taxon_info
        return taxon_info

    def fix(self,
        org_name: str,
        verbose: bool = False):

        pass


#!/usr/bin/env python
# encoding: utf-8

import tweepy #https://github.com/tweepy/tweepy
import csv
import re
import numpy as np
import random
import multiprocessing

#Twitter API credentials
consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""

'''

def list_append(count, id, out_list):
	"""
	Creates an empty list and then appends a
	random number to the list 'count' number
	of times. A CPU-heavy operation!
	"""
	for i in range(count):
		out_list.append(random.random())

if __name__ == "__main__":
	size = 10000000000   # Number of random numbers to add
	procs = 5   # Number of processes to create

	# Create a list of jobs and then iterate through
	# the number of processes appending each process to
	# the job list
	jobs = []
	for i in range(0, procs):
		out_list = list()
		process = multiprocessing.Process(target=list_append,
			                              args=(size, i, out_list))
		jobs.append(process)

	# Start the processes (i.e. calculate the random number lists)
	for j in jobs:
		j.start()

	# Ensure all of the processes have finished
	for j in jobs:
		j.join()

	print ("List processing complete.")
'''

def get_all_tweets(screen_name):
	#Twitter only allows access to a users most recent 3240 tweets with this method

	#authorize twitter, initialize tweepy
	auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
	auth.set_access_token(access_key, access_secret)
	api = tweepy.API(auth)

	#initialize a list to hold all the tweepy Tweets
	alltweets = []

	#make initial request for most recent tweets (200 is the maximum allowed count)
	new_tweets = api.user_timeline(screen_name = screen_name,count=200)
	#print (new_tweets[1])

	#save most recent tweets
	alltweets.extend(new_tweets)

	#save the id of the oldest tweet less one
	oldest = alltweets[-1].id - 1

	#keep grabbing tweets until there are no tweets left to grab
	while len(new_tweets) > 0:
		#print ("getting tweets before %s") %(oldest)

		#all subsiquent requests use the max_id param to prevent duplicates
		new_tweets = api.user_timeline(screen_name = screen_name,count=200,max_id=oldest)

		#save most recent tweets
		alltweets.extend(new_tweets)

		#update the id of the oldest tweet less one
		oldest = alltweets[-1].id - 1

		#print ("...%s tweets downloaded so far") % (len(alltweets))

	#transform the tweepy tweets into a 2D array that will populate the csv
	outtweets = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in alltweets]

	#write the csv
	with open('.../dest.csv' % screen_name, 'w') as f:
		writer = csv.writer(f)
		writer.writerow(["id","created_at","text"])
		writer.writerows(outtweets)

	pass

def get_data(user_ids):
	for item in user_ids:
		get_all_tweets(item)

if __name__ == '__main__':
	username = ''
	#source = csv.reader(source_handle)
	procs = 8   # Number of processes to create

	# Create a list of jobs and then iterate through
	# the number of processes appending each process to
	# the job list
	list_names = []
	for t in range(8):
		temp = []
		temp.append(username[range((t * int(len(username)/8)), ((t + 1) * int(len(username)/8)))])
		list_names.append(temp)

	jobs = []
	for i in range(0, procs):
		out_list = list()
		process = multiprocessing.Process(target=get_data,
			                              args=(list_names[i]))
		jobs.append(process)

	# Start the processes (i.e. calculate the random number lists)
	for j in jobs:
		j.start()

	# Ensure all of the processes have finished
	for j in jobs:
		j.join()

	print ("List processing complete.")

Python MultiThreaded code/File - 1 User Tweets Collect.py


from typing import Type, TYPE_CHECKING, Tuple, Union, List, NewType  # , Any, NewType, TypeVar
from PIL import Image
import numpy as np
from typing_extensions import Literal

if TYPE_CHECKING:  # for mypy
    class ndarray(np.ndarray): ...
    imageType = NewType('imageType', np._ArrayLike[np.ndarray[np.ndarray[int]]])
    videoType = NewType('videoType', np._ArrayLike[imageType])
    contourType = NewType('contourType', np._ArrayLike[np.ndarray[np.ndarray[int]]])
    intArray = np.ndarray[int]
    floatArray = np.ndarray[float]
else:  # python doesnt allow ndarray subscriptable yet
    imageType = np.ndarray
    videoType = np.ndarray
    contourType = np.ndarray
    intArray = np.ndarray
    floatArray = np.ndarray

class PILImage(Image.Image): ...

colorHSV = Tuple[float, float, float]
colorRGB = Tuple[int, int, int]
colorType = Union[colorHSV, colorRGB]

RGBLit = Literal["RGB", "rgb"]
HSVLit = Literal["HSV", "hsv"]
colorSystemLit = Union[RGBLit, HSVLit]

pointType = Tuple[int, int]


#!/usr/bin/env python

import os
from setuptools import setup, find_packages
from pip.req import parse_requirements

def get_requirements(fname):
    return [str(r.req) for r in parse_requirements(fname, session=False)]

def read(fname):
    return open(os.path.join(os.path.dirname(__file__), fname)).read()

setup(
    name="anneal",
    version="0.0",
    author="ohtaman",
    author_email="ohtamans@gmail.com",
    description="Simulated/Quantum Annealing Library",
    long_description=read('README.md'),
    url="http://www.github.com/ohtaman/anneal",
    license="MIT",
    packages=find_packages(),
    install_requires=get_requirements('requirements.txt'),
    setup_requires=get_requirements('requirements/setup.txt'),
    tests_require=get_requirements('requirements/test.txt')
)


import functools

import pandas as pd
from typing import List, Dict, Type, Iterator
import re

from .uniformat import UNIBaseParser
from .misc import open_filename
from .filename import match_filename

def parse_mig_file(filepath: str) -> 'MigParser':
    parser_cls = get_mig_parser_cls(filepath=filepath)
    parser = parser_cls(file=filepath)
    return parser

def get_mig_parser_cls(filepath: str) -> Type['MigParser']:
    r = match_filename(filename=filepath)
    if not r:
        raise ValueError(f'Not a valid MIG file: {filepath}')

    parsers = {
        '91': Mig3Export91Parser,
        '92': Mig3Export92Parser,
        '93': Mig3Export93Parser,
        '94': Mig3Export94Parser,
        '95': Mig3Export95Parser,
        '96': Mig3Export96Parser
    }
    parser = parsers[r['export_no']]
    return parser

class MigParser(UNIBaseParser):
    date_columns: List[int]  = NotImplemented
    column_names: Dict = NotImplemented

    def _parse_dataframe(self) -> pd.DataFrame:
        with open_filename(filename=self.file, mode='r') as f:
            df = pd.read_csv(
                filepath_or_buffer=f,
                sep=';',
                header=None,
                engine='python',
                skiprows=self.body_start_line,
                parse_dates=self.date_columns,
                date_parser=self._date_parser,
                decimal=',',
                skipfooter=len(self.raw) - self.body_end_line - 1
            )
        df.rename(columns=self.column_names, inplace=True)
        if 'Description' in df.columns:
            df['Description'] = df['Description'].str.strip(' ')

        df.drop_duplicates(inplace=True)

        return df

class Mig3Export91Parser(MigParser):
    date_columns = [0, 1]
    column_names = {
        0: 'Start',
        1: 'End',
        2: 'AccessEAN',
        3: 'Serial',
        4: 'CounterID',
        5: 'EnergyType',
        6: 'Direction',
        7: 'Unit',
        8: 'Reason',
        209: 'Interval',
        210: 'Description',
        211: 'CityEAN',
        212: 'GasConversionFactor',
        213: 'GasConversionUnit',
        214: 'GasConversionFactorQuality',
        215: 'RequestSenderRef',
        216: 'RequestReceiverRef'
    }

    @functools.lru_cache(maxsize=128, typed=False)
    def get_timeseries_frame(self, index_shift: str = 'right') -> pd.DataFrame:
        df = self.get_dataframe()

        def parse_columns(frame: pd.DataFrame) -> Iterator[pd.DataFrame]:
            for _, group in frame.groupby([
                'AccessEAN', 'EnergyType', 'Unit', 'Serial'
            ], dropna=False):
                parsed_rows = (self._parse_row_to_timeseries(
                    row,
                    index_shift=index_shift)
                for _, row in group.iterrows())
                parsed_rows = [row for row in parsed_rows if not row.empty]
                if len(parsed_rows) == 0:
                    continue
                column = pd.concat(parsed_rows)
                yield column
        columns = parse_columns(frame=df)
        df_t = pd.concat(columns, axis=1)
        return df_t

    @staticmethod
    def _parse_row_to_timeseries(
            row: pd.Series, index_shift: str = "right") -> pd.DataFrame:
        interval = row['Interval']
        if pd.isna(interval):
            return pd.DataFrame()
        index = pd.date_range(start=row.Start, end=row.End,
                              freq=f'{interval}min', inclusive=index_shift)
        step = int(5 - (60 / interval))
        start_slice = 9 + step - 1

        values = pd.Series(data=row[start_slice:start_slice + len(index) * step:step].values, index=index)
        meta_v = (
            row['AccessEAN'], row['Description'], row['Serial'],
            row['Direction'], row['CounterID'], row['EnergyType'],
            row['Unit'], 'value')

        quality_codes = pd.Series(data=row[start_slice + 100:start_slice + 100 + len(index) * step: step].values,
                                  index=index)
        meta_q = (
            row['AccessEAN'], row['Description'], row['Serial'],
            row['Direction'], row['CounterID'], row['EnergyType'],
            row['Unit'], 'quality')

        meta_names = ['AccessEAN', 'Description', 'Serial', 'Direction',
                      'CounterID','EnergyType', 'Unit', None]

        # if the quality code equals "?", we want the value to result in NaN
        # by multiplying it with float('NaN')
        # if the quality code is anything else, we retain it by multiplying it with 1
        quality_multiplier = quality_codes.map(lambda x: float('NaN') if x == '?' else 1)
        values = values * quality_multiplier

        ts = pd.concat([values, quality_codes], axis=1)
        ts.columns = pd.MultiIndex.from_tuples([meta_v, meta_q], names=meta_names)
        return ts

class Mig3Export92Parser(Mig3Export91Parser):
    pass

class Mig3Export93Parser(Mig3Export91Parser):
    pass

class Mig3Export94Parser(MigParser):
    calculated_columns = [
        'AccessEAN',
        'Serial',
        'RegisterID',
        'EnergyType',
        'TimeFrame',
        'Start',
        'End',
        'QualityCode',
        'QualityReason',
        'Unit',
        'Reason',
        'Value',
        'Estimate',
        'EstimateStart',
        'SwitchingCategory',
        'Description',
        'CityEAN',
        'Blank1',
        'Blank2'
    ]

    phyisical_columns = [
        'AccessEAN',
        'Serial',
        'RegisterID',
        'EnergyType',
        'MeteringMethod',
        'Unit',
        'TimeFrame',
        'PreviousDateTime',
        'PreviousValue',
        'PreviousQualityCode',
        'PreviousQualityReason',
        'LatestDateTime',
        'LatestValue',
        'LatestQualityCode',
        'LatestQualityReason',
        'Reason',
        'Description',
        'MeterType',
        'GasConversionFactor',
        'GasConversionUnit',
        'Blank1',
        'Blank2'
    ]

    num_cols = ['Value', 'Estimate', 'PreviousValue', 'LatestValue', 'GasConversionFactor']
    datetime_cols = ['Start', 'End', 'EstimateStart', 'PreviousDateTime', 'LatestDateTime']

    def _parse_dataframe(self) -> pd.DataFrame:
        with open_filename(filename=self.file, mode='r') as f:
            df = pd.read_csv(
                filepath_or_buffer=f,
                engine='python',
                header=None,
                skiprows=self.body_start_line,
                skipfooter=len(self.raw) - self.body_end_line - 1,
                sep="|",
                names=['row']
            )

        df['calculated'] = df.row.apply(self._is_calculated_row)

        df_calculated = df[df['calculated']].copy()
        df_physical = df[~df['calculated']].copy()

        df_calculated[self.calculated_columns] = df_calculated['row'].str.split(';', n=18, expand=True)
        df_physical[self.phyisical_columns] = df_physical['row'].str.split(';', n=21, expand=True)

        df = pd.concat([df_calculated, df_physical], sort=False)
        df.drop(axis=1, columns=['row', 'Blank1', 'Blank2'], inplace=True)

        df.replace('', float('NaN'), inplace=True)

        for col in self.num_cols:
            try:
                df[col] = df[col].str.replace(',', '.')
            except AttributeError:  # entire row empty
                pass

            df[col] = df[col].astype(float)

        for col in self.datetime_cols:
            df[col] = df[col].apply(self._date_parser)

        if 'Description' in df.columns:
            df['Description'] = df['Description'].str.strip(' ')

        return df

    @staticmethod
    def _is_calculated_row(row: str) -> bool:
        """Checks if the row concerns a calculated or physical register"""
        match = re.match(string=row, pattern="[0-9]{18};AP LEVEL;.*")
        return match is not None

    def _date_parser(self, datetime_str: str) -> pd.Timestamp:
        """Override the date parser to deal with NaN's"""
        if not isinstance(datetime_str, str):
            return pd.Timestamp('NaT')
        else:
            return super(Mig3Export94Parser, self)._date_parser(datetime_str=datetime_str)

    def get_timeseries_frame(self) -> pd.DataFrame:
        raise NotImplementedError('No timeseries method implemented for MMR '
                                  'and YRM metering data')

class Mig3Export95Parser(MigParser):
    date_columns = [0, 1]
    column_names = {
        0: 'Start',
        1: 'End',
        2: 'AccessEAN',
        3: 'EnergyType',
        4: 'MeteringMethod',
        5: 'TimeFrame',
        6: 'Direction',
        7: 'Unit',
        8: 'Reason',
        9: 'Consumption',
        10: 'QualityCode',
        11: 'Description'
    }

class Mig3Export96Parser(Mig3Export95Parser):
    pass


print("**********Convertor from lowercase to uppercase or reverse**********\n\n\n")
print("""This is a conversion program from lowercase to uppercase or reverse.
Please choose a operation you would like to do;

For conversion from lowercase to uppercase, press    "1"
For conversion from uppercase to lowercase, press    "2"
For capitalization of first word of sentence, press  "3"
For capitalization of each words of sentence, press  "4"
""")
while True:
 operation=input("Please enter a operation code\n")
 if operation=="1":
    uppercase=input("Please enter word or sentence you would like to convert\n")
    print(uppercase.upper())
    q=input("""Would you like to quit? If yes please press "q", if no please press "n".
""")
    if q.lower()=="q":
        print("Program terminated.")
        break
    elif q.lower()=="n":
        continue
 elif operation=="2":
    lowercase=input("Please enter word or sentence you would like to convert\n")
    print(lowercase.lower())
    q=input("""Would you like to quit? If yes please press "q", if no please press "n".
""")
    if q.lower()=="q":
        print("Program terminated.")
        break
    elif q.lower()=="n":
        continue
 elif operation=="3":
    capital_letter=input("Please enter sentence you would like to capitalize first word\n")
    print(capital_letter.capitalize())
    q=input("""Would you like to quit? If yes please press "q", if no please press "n".
""")
    if q.lower()=="q":
        print("Program terminated.")
        break
    elif q.lower()=="n":
        continue
 elif operation=="4":
    capital_letter=input("Please enter sentence you would like to capitalize each words of sentence\n")
    print(capital_letter.title())
    q=input("""Would you like to quit? If yes please press "q", if no please press "n".
""")
    if q.lower()=="q":
        print("Program terminated.")
        break
    elif q.lower()=="n":
        continue
 else:
    print("You made a mistake. Please check your operation and follow the instructions.\n")

Homework 1 - Convertor (lowercase - uppercase - capitalize).py


#!/usr/bin/env python3

"""
The entry point of the game.
Initiate pygame, load game generic data & fonts, initiate pygame window
and let the main loop running.
The pygame events are caught here and delegated to the start screen.
"""

import pygame
import pygamepopup

from src.gui.tools import show_fps
from src.services.scene_manager import SceneManager, QuitActionKind

def main_loop(
    game_controller: SceneManager, screen: pygame.Surface, clock: pygame.time.Clock
) -> QuitActionKind:
    """
    Run the game until a quit request happened.
    Pygame events are catch and delegated to the scene manager.
    The state and display of the active scene are updated at each iteration.

    Keyword arguments:
    controller -- the scene manager acting as the main controller of the game
    screen -- the screen on which the current frame rate is displayed
    clock -- the clock regulating the maximum frame rate of the game

    Returns:
    whether quit or restart
    """
    action: QuitActionKind = QuitActionKind.CONTINUE
    while action == QuitActionKind.CONTINUE:
        screen.fill(BLACK)
        action = game_controller.process_game_iteration()
        show_fps(screen, clock, fonts.fonts["FPS_FONT"])
        pygame.display.update()
        clock.tick(FRAME_RATE)
    return action

if __name__ == "__main__":
    import os
    import sys
    import platform
    import subprocess

    from src.constants import (
        MAIN_WIN_WIDTH,
        MAIN_WIN_HEIGHT,
        BLACK,
        FRAME_RATE,
    )
    from src.gui import constant_sprites, fonts
    from src.services.language import STR_GAME_TITLE
    from src.game_entities.movable import Movable
    from src.game_entities.character import Character
    from src.services import load_from_xml_manager as loader
    from src.services.language import *

    pygame.init()
    pygamepopup.init()

    fonts.init_fonts()

    # Configure pygame-popup manager : set default assets to be used
    pygamepopup.configuration.set_info_box_title_font(fonts.fonts["MENU_TITLE_FONT"])
    pygamepopup.configuration.set_info_box_background("imgs/interface/PopUpMenu.png")
    pygamepopup.configuration.set_button_title_font(fonts.fonts["BUTTON_FONT"])
    pygamepopup.configuration.set_dynamic_button_title_font(fonts.fonts["BUTTON_FONT"])
    pygamepopup.configuration.set_button_background(
        "imgs/interface/MenuButtonInactiv.png", "imgs/interface/MenuButtonPreLight.png"
    )
    pygamepopup.configuration.set_text_element_font(fonts.fonts["ITEM_FONT"])
    pygamepopup.configuration.set_close_button_text(STR_CLOSE)

    pygame.display.set_caption(STR_GAME_TITLE)
    main_screen = pygame.display.set_mode((MAIN_WIN_WIDTH, MAIN_WIN_HEIGHT))

    # Make sure the game will display correctly on high DPI monitors on Windows.
    if platform.system() == "Windows":
        from ctypes import windll

        try:
            windll.user32.SetProcessDPIAware()
        except AttributeError:
            pass

    Movable.init_constant_sprites()
    constant_sprites.init_constant_sprites()

    races = loader.load_races()
    classes = loader.load_classes()
    Character.init_data(races, classes)

    scene_manager = SceneManager(main_screen)

    pygame.mixer.music.load(os.path.join("sound_fx", "soundtrack.ogg"))
    pygame.mixer.music.play(-1)

    # Lets the game start!
    quit_action = main_loop(scene_manager, main_screen, pygame.time.Clock())

    pygame.quit()

    if quit_action == QuitActionKind.RESTART:
        # Restart game
        subprocess.Popen([sys.executable, "main.py"]).wait()


def cut_neg(n):
    return n * (n > 0)

def phone(strng, num):
    arr = strng.split('\n')
    arr = [el for el in arr if num in el]
    if len(arr) > 1:
        return f"Error => Too many people: {num}"
    if len(arr) == 0:
        return f"Error => Not found: {num}"
    s = arr[0]
    name = s[s.index('<')+1:s.index('>')]
    s = s[:s.index('<') ] + s[s.index('>')+2:]  # remove name
    s = s[:cut_neg(s.index(num)-2)] + s[s.index(num)+len(num)+1:]   # remove phone
    s = s.replace(';', ' ')
    s = s.replace(',', ' ')
    s = s.replace('/', ' ')
    s = s.replace('_', ' ')
    s = s.replace('  ', ' ')
    s = s.strip()
    return f"Phone => {num}, Name => {name}, Address => {s}"


from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
from keras.models import load_model
from keras.models import Model

import numpy as np
import os

model = load_model('trained/model-vgg16.h5')
model.summary()
model_extractfeatures = Model(input=model.input, output=model.get_layer('fc2').output)

sources = ['data/sorted/train/1','data/sorted/train/2','data/sorted/train/3','data/sorted/train/W','data/sorted/train/R']
destines = ['data/features/train/1','data/features/train/2','data/features/train/3','data/features/train/W','data/features/train/R']

batch_size = 5
batch = np.zeros((batch_size, 224,224,3),dtype='uint8')
for i in range(len(sources)):
	s = sources[i]
	d = destines[i]
	print("In source : ",s)
	#if(s != 'ImbalanceClasses/2'):
	#	continue
	files = os.listdir(s)
	files_len = len(files);
	count = 0
	while(count<files_len):
		print("Begin loop")
		#batch = []
		b_count = 0
		for j in range(batch_size):
			if(count+j>= files_len):
				break
			f = files[count+j]
			print("In folder: "+s +" and in file: "+f)
			#if(os.path.isfile(d+'/'+f+'.npy')):
			#	continue
			img = image.load_img(s+'/'+f, target_size=(224, 224))
			#x = image.img_to_array(img)
			#x = np.expand_dims(x, axis=0)
			#x = preprocess_input(x)
			#print(x.shape)
			#print(batch.shape)
			#batch.append(x)
			batch[j,:,:,:]= img
			b_count+=1
		count+=b_count
		features = model_extractfeatures.predict_on_batch(batch)
		#print(features.shape)
		t = -batch_size
		#print("Saving features")
		for feat in features:
			if(count+t> files_len):
				break

			f = files[count+t]
			np.save(d+'/'+f+'.npy',feat.reshape((4096,1)))
			t+=1
		#a = 2/0


#!/usr/bin/python3
"""Task 100 Project 0x09 """

class Magic():
    prt = 0

    @classmethod
    def sum_ptr(cls):
        cls.prt += 1

def magic_string():
    new = Magic()
    new.sum_ptr()

    printable = ""

    for i in range(new.prt):
        printable += "BestSchool"
        if i + 1 < new.prt:
            printable += ", "


from concurrent import futures
from typing import Any

import random
import grpc
import os
import yaml

import backend_service_pb2 as bs
import backend_service_pb2_grpc as bsg

from team_policy import TeamPolicy, policy_from_msg
from teams import Team, TeamsRepo
from meetings import Meeting, MeetingsRepo, meeting_from_msg
from mongo_collection import MongoCollection

import calendar_service_pb2 as cs
import calendar_service_pb2_grpc as css

class BackendServiceHandler(bsg.BackendServiceServicer):
    def __init__(self, teams_repo: TeamsRepo, meetings_repo: MeetingsRepo, calendar_service: Any):
        super().__init__()
        self.__teams_repo = teams_repo
        self.__meetings_repo = meetings_repo
        self.__calendar_service = calendar_service

    def CreateTeam(self, request, context):
        team_id = self.__teams_repo.add_team(Team(request.owner, request.name))
        return bs.EntityId(id=team_id)

    def GetOwnedTeams(self, request, context):
        for team in self.__teams_repo.get_teams_by_owner(request.id):
            yield bs.NamedInfo(id=team.id, name=team.name)

    def GetTeamInfo(self, request, context):
        team = self.__teams_repo.get_team(request.id)
        if team is None:
            bs.NamedInfo(id=-1, name='')

        return bs.NamedInfo(id=team.id, name=team.name)

    def AddTeamMember(self, request, context):
        self.__teams_repo.add_member_to_team(request.object, request.subject)
        return bs.SimpleResponse(ok=True)

    def CreateMeeting(self, request, context):
        meeting_id = self.__meetings_repo.add_meeting(Meeting(request.creator, request.team))
        return bs.EntityId(id=meeting_id)

    def UpdateMeetingInfo(self, request, context):
        self.__meetings_repo.update_meeting(meeting_from_msg(request))
        m = self.__meetings_repo.get_meeting(request.id)
        if m is None:
            return bs.SimpleResponse(ok=False)
        if m.time > 0 and m.desc != '':
            for uid in set(m.participants):
                self.__calendar_service.PushEvent(cs.CalendarEvent(
                    user_id=uid,
                    meeting_id=m.id,
                    desc=m.desc,
                    time=m.time
                ))
        return bs.SimpleResponse(ok=True)

    def GetOwnedMeetings(self, request, context):
        for meeting in self.__meetings_repo.get_meetings_by_owner(request.id):
            if meeting.approved:
                yield bs.NamedInfo(id=meeting.id, name=meeting.desc)

    def AddParticipant(self, request, context):
        self.__meetings_repo.add_participant(request.object, request.subject)
        m = self.__meetings_repo.get_meeting(request.object)
        if m is None:
            return bs.SimpleResponse(ok=False)
        for uid in set(m.participants):
            self.__calendar_service.PushEvent(cs.CalendarEvent(
                user_id=uid,
                meeting_id=m.id,
                desc=m.desc,
                time=m.time
            ))
        return bs.SimpleResponse(ok=True)

    def GetMeetingInfo(self, request, context):
        m = self.__meetings_repo.get_meeting(request.id)
        if m is None:
            return Meeting(-1, -1).to_proto()
        return m.to_proto()

    def GetTeamMembers(self, request, context):
        team = self.__teams_repo.get_team(request.id)
        if team is not None:
            for member in team.members:
                yield bs.EntityId(id=member)

    def ApproveMeeting(self, request, context):
        self.__meetings_repo.approve_meeting(request.id)
        m = self.__meetings_repo.get_meeting(request.id)
        for uid in set(m.participants):
            self.__calendar_service.PushEvent(cs.CalendarEvent(
                user_id=uid,
                meeting_id=m.id,
                desc=m.desc,
                time=m.time
            ))
        return bs.SimpleResponse(ok=True)

    def GetGroupMeetings(self, request, context):
        for meeting in self.__meetings_repo.get_meetings_by_group(request.id):
            yield bs.NamedInfo(id=meeting.id, name=meeting.desc)

    def GetGroupOwner(self, request, context):
        return bs.EntityId(id=self.__teams_repo.get_team(request.id).owner)

    def GetGroupPolicy(self, request, context):
        t = self.__teams_repo.get_team(request.id)
        if t is None:
            policy = TeamPolicy()
            gi = -1
        else:
            policy = t.policy
            gi = request.id
        policy = self.__teams_repo.get_team(request.id).policy
        return bs.TeamPolicy(
            groupId=gi,
            allowUsersToCreateMeetings=policy.allow_users_to_create_meetings,
            needApproveForMeetingCreation=policy.need_approve_for_meeting_creation,
            propagatePolicy=policy.propagate_policy,
            parentVisible=policy.parent_visible,
            propagateAdmin=policy.propagate_admin
        )

    def SetGroupPolicy(self, request, context):
        self.__teams_repo.set_team_policy(request.groupId, policy_from_msg(request))
        return bs.SimpleResponse(ok=True)

    def GetGroupsToCreateMeeting(self, request, context):
        for team in self.__teams_repo.get_grups_by_member(request.id):
            if team.owner == request.id or team.policy.allow_users_to_create_meetings:
                yield bs.NamedInfo(id=team.id, name=team.name)

    def AddParentTeam(self, request, context):
        self.__teams_repo.add_parent(request.subject, request.object)
        return bs.SimpleResponse(ok=True)

    def GetInvitableMembers(self, request, context):
        for member in self.__teams_repo.get_invitable_members(request.id):
            yield bs.EntityId(id=member)

    def GetGroupOwners(self, request, context):
        for admin in self.__teams_repo.get_admins(request.id):
            yield bs.EntityId(id=admin)

    def GetUserMeetings(self, request, context):
        uid = request.id
        for meeting in self.__meetings_repo.get_meetings_my_user(uid):
            yield bs.NamedInfo(id=meeting.id, name=meeting.desc)

    def GetMeetingMembers(self, request, context):
        mid = request.id
        meeting = self.__meetings_repo.get_meeting(mid)
        if meeting is not None:
            for uid in set(meeting.participants):
                yield bs.EntityId(id=uid)

    def AddFileToTeam(self, request, context):
        print(request)
        self.__teams_repo.add_file_to_team(request.object, request.subject)
        return bs.SimpleResponse(ok=True)

    def GetAvailableFiles(self, request, context):
        for fid in self.__teams_repo.get_available_files(request.id):
            yield bs.EntityId(id=fid)

    def GetTeamsByUser(self, request, context):
        for team in self.__teams_repo.get_teams_by_user(request.id):
            yield bs.NamedInfo(id=team.id, name=team.name)

def serve():
    random.seed()
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

    if os.environ.get("BackendTestEnv") is not None:
        from test_mock_collection import MockCollection
        teams_repo = TeamsRepo(MockCollection())
        meetings_repo = MeetingsRepo(MockCollection())
    elif os.environ.get("BackendSysTestEnv") is not None:
        teams_repo = TeamsRepo(MongoCollection('mongodb://admin:password@database:27017/MeetingBotDB', 'MeetingBotDB', 'Teams'))
        meetings_repo = MeetingsRepo(MongoCollection('mongodb://admin:password@database:27017/MeetingBotDB', 'MeetingBotDB', 'Meetings'))
    else:
        with open('config.yml', 'r') as config_file:
            config = yaml.safe_load(config_file)

        teams_repo = TeamsRepo(MongoCollection(config['teams_repo_url'], 'MeetingBotDB', 'Teams'))
        meetings_repo = MeetingsRepo(MongoCollection(config['meetings_repo_url'], 'MeetingBotDB', 'Meetings'))

    channel_calendar = grpc.insecure_channel('calendar-service:50053')
    calendar_stub = css.CalendarServiceStub(channel_calendar)

    bsg.add_BackendServiceServicer_to_server(BackendServiceHandler(teams_repo, meetings_repo, calendar_stub), server)

    server.add_insecure_port('[::]:50062')
    server.start()
    server.wait_for_termination()


# https://www.youtube.com/watch?v=l3hda49XcDE
# https://leetcode.com/problems/regular-expression-matching/description/
class Solution(object):
    def isMatch(self, s, p):
        """
        :type s: str
        :type p: str
        :rtype: bool
        """
        m = len(s)
        n = len(p)

        arr = [[False] * (n + 1) for x in range(m + 1)]
        # empty pattern empty string
        arr[0][0] = True
        # first column is all false except [0][0] because you're matching an empty pattern with a string

        # remember that because we have added a '' to both pattern and string,
        # hence current index is i-1, j-1 for 's' and 'p'
        # for arr, current index is i,j
        for i in range(m + 1):
            for j in range(n + 1):
                if i == j == 0:
                    continue

                # empty string with a given pattern
                if i == 0:
                    # first row needs to be handled specially if string is 'aa' and pattern is 'a*' or 'a*b*' etc.
                    # if pattern is not *, it cant match with a character as our input string is empty
                    if p[j - 1] != '*':
                        arr[0][j] = False
                    else:
                        # consider a case like '' vs 'a*', when we do j-2,
                        # we are matching empty input string with empty pattern, ie arr[0][0], which is true
                        arr[0][j] = arr[0][j - 2]
                    continue

                # if current element of pattern is same as current element of string,
                # then just remove these 2 and check this for rest of result [i-1][j-1]
                if i > 0 and j > 0 and ((s[i - 1] == p[j - 1]) or p[j - 1] == '.'):
                    arr[i][j] = arr[i - 1][j - 1]
                    continue

                if i > 0 and j > 1 and p[j - 1] == '*':
                    # include 0 occurances of the element corresponding to *
                    # example, xa* vs x => remove a*, ie, column j-2
                    arr[i][j] = arr[i][j - 2]
                    if arr[i][j]:
                        continue

                    # watch minute 11 of the above video (alternate explanation below)
                    # if arr[i][j] is still false, say xa* vs xa => remove a* means p = 'x' and s = 'xa', which is false
                    # if element before * matches the last element of string
                    # then we have to check if last char of string, ie, 'a' is same as char before '*', ie 'a'
                    # the p[j-2] == '.' condition handles the following case 'x.*' vs 'xa', here '.' == 'a' is true
                    if p[j - 2] == s[i - 1] or p[j - 2] == '.':
                        # look above and "or" it with current
                        arr[i][j] = arr[i][j] or arr[i - 1][j]

                    continue

                # default case, doesnt matter,
                # we've already initialized everything to false, so having/not having this has no effect
                arr[i][j] = False

        return arr[m][n]


"""Code pour obtenir (Get) les valeurs de l'accélération (a) envoyées par la carte Arduino."""

import serial						####################
from time import time					# Nos importations #
							####################

def Read():
	serial_port = '/dev/ttyACM0';			#Le port sur lequel est branchée l'Arduino
	baud_rate = 115200; 				#Dans le code arduino, Serial.begin(baud_rate)
	ser = serial.Serial(serial_port, baud_rate);	#ser est le port série

	print("Prise de donées : \n Nom du fichier");	##############################################
	write_to_file_path = str(input())+".txt";	# On récupère le chemin et le nom du fichier #
	output_file = open(write_to_file_path, "w+");	##############################################

	data = [];					#liste des donées
	times = [];					#liste du temps

	Apdata = data.append;				#fonction append in data
	Aptimes = times.append;				#fonction append in times

	Sread = ser.readline;				#fonction serial read line

	print("Temps de captation (en secondes)");	##################################################
	Dt = float(input());				# On recupère le temps de prise de donées voulue #
	t0 = time();					##################################################
	tmax = t0+Dt;
	fline = time();
	while fline < tmax:				#Pour la periode voulue
		fline = time();				#On récupère la valeur du temps
		Apdata(Sread());			#On stocke les valeurs envoyées par l'arduino
		Aptimes(fline);				#On stocke la valeur du temps

	n = 0;						#On initialise le nombre d'erreur à 0
	Wrinfile = output_file.write;			#fonction write in file

	for i in range(len(data)):			#Pour toutes les valeurs stockées
		try:					#On essaie (il peut y avoir des erreurs)
			val = data[i].decode("utf-8");	#On decode la valeur binaire de data -> str
			dt = str(times[i]-t0);		#On calcul la valeur du temps écoulé à cet instant
			Wrinfile(dt+" "+val);		#On l'écrit dans le fichier
		except:					#Si il y as une erreur
			n += 1;				#On rajoute +1 à notre compteur

	print("Nombre d'erreur : "+str(n));		#On print le nombre d'erreur

	f = (len(times)-n)/(times[-1]-times[0]);	#On calcul la fréquence
	pms = str(1000/f);				#On calcul la periode en ms

	print(f," Hz -> "+pms+" ms");			#On affiche la fréquence d'aquisition et la periode
	return();

Read();							#On lance le code