import argparse
from time import sleep as sl
import ete3

def parseargs():
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    # parser.add_argument("-d", "--database_name", required=True,
    #                     help="sql database to search (eg. vcseventh)")

    args = parser.parse_args()

    return args

def main():
    args = parseargs()

    tree = ete3.Tree('/Users/liamcheneyy/Desktop/ODC566_C19/grapetree.nwk', format=0)

    ##all lengths
    print('ID','Clade',sep='\t')
    count = 1
    with open('/Users/liamcheneyy/Desktop/all_isolates_lengths.txt','w') as out:
        for node in tree.traverse():
            if node.dist > 0:
                out.write(str(count) + '\t' + str(node.dist) + '\n')
                count += 1

    # ###finding_length
    # strains = len(tree.get_leaf_names())
    # print('ID','Clade',sep='\t')
    #
    # lengths_list = [x for x in range(0,1000,1)]
    # for length in lengths_list:
    #     clade_list = []
    #     clade_num = 1
    #     done = []
    #
    #     # print(f'Trying length {length}')
    #     for node in tree.traverse():
    #         if node.dist > length:
    #             for leaf in node:
    #                 if leaf not in done:
    #                     clade_list.append([leaf.name, clade_num])
    #                     done.append(leaf)
    #                 clade_num += 1
    #     # print(length, len(clade_list), sep='\t')

    ##for node in tree:
        #if all distances are < length:
            ##then save that subtree
        #else go back and recurvsively

    # # using defined length
    print('ID', 'Clade', sep='\t')
    clade_num = 1
    length = 255 #255
    freq_dict = {}
    # print(f'Trying length {length}')
    for node in tree.traverse():
        if (node.dist > length):
            leaves = node.get_leaf_names()
            for leaf in leaves:
                if leaf not in freq_dict.keys():
                    freq_dict[leaf] = {'count':1,'clade':clade_num}

                else:
                    freq_dict[leaf]['count'] += 1
                    freq_dict[leaf]['clade'] = clade_num

                # print(leaf, clade_num, sep='\t')
            clade_num += 1

    for key, value in freq_dict.items():
        print(key, value['count'],value['clade'], sep='\t')

if __name__ == '__main__':
    main()


import random

def randomized_partition(arr, left, right):
    pivot_idx = random.randint(left, right - 1)
    arr[pivot_idx], arr[right - 1] = arr[right - 1], arr[pivot_idx]
    return partition(arr, left, right)

""" """

def partition(arr, left, right):
    pivot = arr[right - 1]
    i = left
    j = right - 2
    while i <= j:
        if arr[i] >= pivot:
            arr[i], arr[j] = arr[j], arr[i]
            j -= 1
        else:
            i += 1
    arr[i], arr[right - 1] = arr[right - 1], arr[i]
    return i

def quick_sort(arr, left, right):
    if (left < right):
        pivot_idx = randomized_partition(arr, left, right)
        quick_sort(arr, left, pivot_idx)
        quick_sort(arr, pivot_idx + 1, right)
    return arr

def main():
    while True:
        n = random.randint(1, 400)
        arr = [random.randint(1, 400) for i in range(n)]
        print("whwhhw")
        if (quick_sort(arr.copy(), 0, len(arr)) != sorted(arr)):
            print("arr", arr)
            print("quick_sort(arr, 0, len(arr))",
                  quick_sort(arr.copy(), 0, len(arr)))
            print("sorted(arr)", sorted(arr))
            input()

if __name__ == "__main__":
    main()


from flask import Flask
from flask import render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/contact')
def contact():
    value = "Rodolfo"
    # value = None
    return render_template('user_display.html', name=value)

@app.route('/person/<string:name>')
def hello_person(name):
    html = """

Hello world!

Welcome to the matrix, {}.

    """.format(name)

    return html

@app.route('/products')
def products():
    products = [
        {
            'name': 'Disfraz avenger alpha',
            'url': 'https://i.ebayimg.com/images/g/4wMAAOSwVuNbB9lY/s-l300.jpg',
            'cost': 'sinko peso'
        },
        {
            'name': 'Tenis bien nice del Spidey',
            'url': 'https://www.kicksonfire.com/wp-content/uploads/2018/11/Air-Jordan-1-Retro-High-OG-Origin-Story-Into-the-Spider-Verse.jpg?x98166',
            'cost': 'beinte peso'
        }
    ]
    return render_template('productos.html', cosas=products)

if __name__ == '__main__':
    app.run(port=5000, debug=True)


import argparse
import codecs
import json
import jieba
import os
import logging
import pickle
from sklearn import metrics
from sklearn.metrics import hamming_loss
import tensorflow as tf
import numpy as np
import matplotlib
matplotlib.use('Agg')
from matplotlib import cm
from matplotlib import pyplot as plt
from sklearn.preprocessing import MultiLabelBinarizer
from gensim.models.word2vec import Word2Vec
from sklearn.model_selection import train_test_split
# from bn_lstm import LSTMCell, BNLSTMCell
# from model_components import task_specific_attention, bidirectional_rnn, relation, soft_max_with_t
import tensorflow.contrib.layers as layers
try:
  from tensorflow.contrib.rnn import RNNCell
except ImportError:
  RNNCell = tf.nn.rnn_cell.RNNCell
try:
    from tensorflow.contrib.rnn import LSTMStateTuple
except ImportError:
    LSTMStateTuple = tf.nn.rnn_cell.LSTMStateTuple
try:
    from tensorflow.contrib.rnn import GRUCell, MultiRNNCell, DropoutWrapper
except ImportError:
    MultiRNNCell = tf.nn.rnn_cell.MultiRNNCell
    GRUCell = tf.nn.rnn_cell.GRUCell

class Configs(object):
    def __init__(self):
        # ------parsing input arguments"--------
        parser = argparse.ArgumentParser()

        parser.add_argument('--data_dir', type=str, default="data/small")
        parser.add_argument('--save_name', type=str, default="rmsc")
        parser.add_argument('--random_seed', type=int, default=1)
        parser.add_argument('--use_bn', type=bool, default=False)
        parser.add_argument('--gpu', type=str, default='1')
        parser.add_argument('--kp', type=float, default=1.0)
        parser.add_argument('--batch_size', type=int, default=128)
        parser.add_argument('--use_birnn', type=bool, default=True)
        parser.add_argument('--cut_prob', type=float, default=0.2,
                            help='output a label if its probability is above cut_prob')

        parser.add_argument('--epoch_len', type=int, default=100)
        parser.add_argument('--start_eval_epoch', type=int, default=20)
        parser.add_argument('--optim', type=str, default='adam')
        parser.add_argument('--learning_rate', type=float, default=1e-3, )
        parser.add_argument('--decay_rate', type=float, default=1,
                            help='')
        parser.add_argument('--decay_steps', type=int, default=1000,
                            help='')

        # label graph
        parser.add_argument('--use_rel',  default=False, type=bool, help='whether use label graph ')
        parser.add_argument('--pre_rel', default=False, type=bool,
                            help='whether use label graph before sigmoid')
        parser.add_argument('--lg_init_noise', type=float, default=0,
                            help='add noise to the identity matrix during the label graph initialization')
        parser.add_argument('--ratio2', type=float, default=0,
                            help=' rel loss rate aggregate the label graph by adding  -||G-I||^2 to the loss function')

        # soft training
        parser.add_argument('--soft_target', default=False, type=bool,
                            help='whether use a continuous target as the second supervisor')
        parser.add_argument('--stop_gradient', default=True,
                            help="whether stop gradient (y'= stop_gradient(y*G))")
        parser.add_argument('--ratio1', type=float, default=1.0, help='loss += ratio1 * soft_target_loss')
        parser.add_argument('--temperature', type=int, default=0,
                            help='whether use temperature to soften the continuous target, default 0 is not to soft')

        parser.set_defaults(shuffle=True)
        self.args = parser.parse_args()
        #  ---- to member variables -----
        for key, value in self.args.__dict__.items():
            if key not in ['test', 'shuffle']:
                exec('self.%s = self.args.%s' % (key, key))

cfg = Configs()

# label graph
use_rel=cfg.use_rel
pre_rel = cfg.pre_rel
ratio2 = cfg.ratio2
lg_init_noise = cfg.lg_init_noise

# second signal
soft_target = cfg.soft_target
ratio1 = cfg.ratio1  # soft target loss
stop_gradient = cfg.stop_gradient
temperature = cfg.temperature

# base hyper params
random_seed = cfg.random_seed
learning_rate = cfg.learning_rate
use_bn = cfg.use_bn
cut_prob = cfg.cut_prob
data_dir = cfg.data_dir
epoch_len = cfg.epoch_len
start_eval_epoch = cfg.start_eval_epoch
kp = cfg.kp
sentence_out_size = 100
word_level_out = 100
units_num = 128
use_birnn = cfg.use_birnn
batch_size = cfg.batch_size
comments_num = 40
embed_dim = 100

# save dir
save_name = cfg.save_name

save_name = save_name
if cut_prob != 0.2:
    save_name += "_prob-"+str(cut_prob)
if learning_rate != 0.001:
    save_name += "_lr-" + str(learning_rate)
if cfg.optim != 'adam':
    save_name += '_optim-' + str(cfg.optim)
if cfg.random_seed != 1:
    save_name += '_seed-' + str(random_seed)
if cfg.decay_rate < 1:
    save_name += '_dr-' + str(cfg.decay_rate)
    save_name += '_ds-' + str(cfg.decay_steps)
if use_rel:
    save_name += "_ur"
    if pre_rel:
        save_name += "_pr"
    if lg_init_noise != 0:
        save_name += "_n-" + str(lg_init_noise)
    if ratio2 > 0:
        save_name += '_r2-' + str(ratio2)
    if soft_target:
        save_name += '_st'
        save_name += "_r1-" + str(ratio1)
        if temperature > 0:
            save_name += "_t-" + str(temperature)
        if not stop_gradient:
            save_name += "_nst"
save_dir = os.path.join("checkpoints", save_name)
if not os.path.exists(save_dir):
    os.mkdir(save_dir)
f_log = codecs.open(os.path.join(save_dir, 'log.txt'), 'w+', encoding='utf-8')

os.environ["CUDA_VISIBLE_DEVICES"] = cfg.gpu
tf.set_random_seed(random_seed)
np.random.seed(random_seed)
songs = os.listdir(data_dir)
total_tags = []
all_comments_list = []
total_song_comments_and_tags = []
len_songs = len(songs)

for song in songs:
    with codecs.open(data_dir+'/'+song, 'r+', encoding='utf-8') as f:
        a = f.read()
        dict_f = json.loads(a)
        every_song_comment_for_lstm_train = []
        all_comments_dict = dict_f['all_short_comments'][0:40]
        for comment_dict in all_comments_dict:
            every_comment = comment_dict["comment"]
            every_comment_cut = "/".join(jieba.cut(every_comment, cut_all=False)).split('/')    # cur comment
            every_song_comment_for_lstm_train.append(every_comment_cut)     # [[pl1],[pl2],...]
        every_song_comments_and_tags = {"tags": dict_f['tags'], "comments": every_song_comment_for_lstm_train}
        all_comments_list.extend(every_song_comment_for_lstm_train)     # get all comments
        total_tags.extend(dict_f['tags'])  # get all classes
        total_song_comments_and_tags.append(every_song_comments_and_tags)  # all tags and comments
print(len(songs))
print(len(total_song_comments_and_tags))
sorted_total_tags = sorted(list(set(total_tags)))   # all tags
del total_tags
tags_num = len(sorted_total_tags)

# word2vec
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
model = Word2Vec(all_comments_list, workers=4, min_count=1, seed=random_seed)
model.save("data/word2vec.model")
del all_comments_list
print("model info :", model)

# map
dict_tag = {}
for i in range(tags_num):
    dict_tag[sorted_total_tags[i]] = i+1

def give_embedding(word):
    return model[word]

def tag_emb(tag):
    return dict_tag[tag]

# labels_origin = np.zeros((len_songs, 8))
labels_origin = []
sentence_long = 20
embed_input = np.zeros((len_songs, comments_num, sentence_long, embed_dim))
# get input and label
# word embedding
len_every_comment_cutted = np.zeros((len_songs, comments_num))
sum_comment_count = 0
for i in range(len_songs):
    every_song_comment_words_embedding = np.zeros((comments_num, sentence_long, embed_dim))
    all_comments_in_every_song = total_song_comments_and_tags[i]["comments"]
    for j in range(len(all_comments_in_every_song)):
        every_comment_in_one_song = np.array(list(map(give_embedding, all_comments_in_every_song[j])))
        every_comment_embedding = np.zeros((sentence_long, embed_dim))
        count_cur_comment = len(every_comment_in_one_song)
        sum_comment_count += count_cur_comment
        if count_cur_comment < sentence_long:
            len_every_comment_cutted[i, j] = count_cur_comment
            every_comment_embedding[0:count_cur_comment] = every_comment_in_one_song
        else:
            every_comment_embedding = every_comment_in_one_song[0:sentence_long]
            len_every_comment_cutted[i, j] = sentence_long
            # print(count_cur_comment)
        every_song_comment_words_embedding[j] = every_comment_embedding
    embed_input[i] = every_song_comment_words_embedding
    every_song_comment_tags_embedding = np.array(list(map(tag_emb, total_song_comments_and_tags[i]["tags"])))
    labels_origin.append(every_song_comment_tags_embedding)
del total_song_comments_and_tags
print("average comment length", sum_comment_count/(len_songs*comments_num))
labels = MultiLabelBinarizer().fit_transform(labels_origin)
del labels_origin
de_rate = 0
soft_labels = (1-de_rate) * labels + de_rate*(1-labels)
print(labels)
len_labels = len(labels)
# split the data
x_train, x_test_valid, y_train, y_test_valid, sequence_train, sequence_test_valid, songs_train, songs_test_valid = \
    train_test_split(embed_input, soft_labels, len_every_comment_cutted, songs, test_size=0.3, random_state=random_seed)
del soft_labels
del embed_input
del len_every_comment_cutted
x_test, x_valid, y_test, y_valid, sequence_test, sequence_valid, songs_test, songs_valid = train_test_split(x_test_valid, y_test_valid, sequence_test_valid, songs_test_valid, test_size=0.3, random_state=random_seed)
label_train, label_test_valid = train_test_split(labels, test_size=0.3, random_state=random_seed)
label_test, label_valid = train_test_split(label_test_valid, test_size=0.3, random_state=random_seed)

f_label_train = open('data/label_train.pickle', 'wb')
pickle.dump(label_train, f_label_train)
f_label_valid = open('data/label_valid.pickle', 'wb')
pickle.dump(label_valid, f_label_valid)
f_label_test = open('data/label_test.pickle', 'wb')
pickle.dump(label_test, f_label_test)

f_x_train = open('data/x_train.pickle', 'wb')
pickle.dump(x_train, f_x_train)
f_x_valid = open('data/x_valid.pickle', 'wb')
pickle.dump(x_valid, f_x_valid)
f_x_test = open('data/x_test.pickle', 'wb')
pickle.dump(x_test, f_x_test)

f_y_train = open('data/y_train.pickle', 'wb')
pickle.dump(y_train, f_y_train)
f_y_valid = open('data/y_valid.pickle', 'wb')
pickle.dump(y_valid, f_y_valid)
f_y_test = open('data/y_test.pickle', 'wb')
pickle.dump(y_test, f_y_test)

f_sorted_total_tags = open('data/sorted_total_tags.pickle', 'wb')
pickle.dump(sorted_total_tags, f_sorted_total_tags)

f_sequence_train = open('data/sequence_train.pickle', 'wb')
pickle.dump(sequence_train, f_sequence_train)
f_sequence_valid = open('data/sequence_valid.pickle', 'wb')
pickle.dump(sequence_valid, f_sequence_valid)
f_sequence_test = open('data/sequence_test.pickle', 'wb')
pickle.dump(sequence_test, f_sequence_test)

f_songs_test = open('data/songs_test.pickle', 'wb')
pickle.dump(songs_test, f_songs_test)
f_songs_valid = open('data/songs_valid.pickle', 'wb')
pickle.dump(songs_valid, f_songs_valid)
f_songs_train = open('data/songs_train.pickle', 'wb')
pickle.dump(songs_train, f_songs_train)


"""Module with variety of network utils"""

from bs4 import BeautifulSoup, Comment
import requests

def get_html_comments(url):
    """
    Fetch html commentaries located on specified source page
    :param url: URL of html page
    :return: list of fetched html comments
    """
    response = requests.get(url=url)
    if response.status_code != 200:
        raise Exception("Invalid status code != 200")
    else:
        soup = BeautifulSoup(response.content, 'html.parser')
        comments = soup.find_all(string=lambda text: isinstance(text, Comment))
        return list(comments)

if __name__ == "__main__":
    get_html_comments("http://www.pythonchallenge.com/pc/def/ocr.html")


from collections import deque

class Solution:
    def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:

        n = len(deck)
        index = [i for i in range(n)]
        index = deque(index)
        deck.sort()
        ans = [-1 for _ in range(n)]
        current = 0

        while index:

            popped = index.popleft()
            ans[popped] = deck[current]

            if index:
                index.append(index.popleft())
                current += 1

        return ans


# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
#
# Code generated by aaz-dev-tools
# --------------------------------------------------------------------------------------------

# pylint: skip-file
# flake8: noqa

from azure.cli.core.aaz import *

@register_command(
    "sentinel alert-rule show",
    is_experimental=True,
)
class Show(AAZCommand):
    """Get the alert rule.
    """

    _aaz_info = {
        "version": "2022-06-01-preview",
        "resources": [
            ["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.operationalinsights/workspaces/{}/providers/microsoft.securityinsights/alertrules/{}", "2022-06-01-preview"],
        ]
    }

    def _handler(self, command_args):
        super()._handler(command_args)
        self._execute_operations()
        return self._output()

    _args_schema = None

    @classmethod
    def _build_arguments_schema(cls, *args, **kwargs):
        if cls._args_schema is not None:
            return cls._args_schema
        cls._args_schema = super()._build_arguments_schema(*args, **kwargs)

        # define Arg Group ""

        _args_schema = cls._args_schema
        _args_schema.resource_group = AAZResourceGroupNameArg(
            required=True,
        )
        _args_schema.rule_name = AAZStrArg(
            options=["-n", "--name", "--rule-name"],
            help="Name of alert rule.",
            required=True,
            is_experimental=True,
            id_part="child_name_1",
        )
        _args_schema.workspace_name = AAZStrArg(
            options=["-w", "--workspace-name"],
            help="The name of the workspace.",
            required=True,
            is_experimental=True,
            id_part="name",
        )
        return cls._args_schema

    def _execute_operations(self):
        self.AlertRulesGet(ctx=self.ctx)()

    def _output(self, *args, **kwargs):
        result = self.deserialize_output(self.ctx.vars.instance, client_flatten=True)
        return result

    class AlertRulesGet(AAZHttpOperation):
        CLIENT_TYPE = "MgmtClient"

        def __call__(self, *args, **kwargs):
            request = self.make_request()
            session = self.client.send_request(request=request, stream=False, **kwargs)
            if session.http_response.status_code in [200]:
                return self.on_200(session)

            return self.on_error(session.http_response)

        @property
        def url(self):
            return self.client.format_url(
                "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.OperationalInsights/workspaces/{workspaceName}/providers/Microsoft.SecurityInsights/alertRules/{ruleId}",
                **self.url_parameters
            )

        @property
        def method(self):
            return "GET"

        @property
        def error_format(self):
            return "ODataV4Format"

        @property
        def url_parameters(self):
            parameters = {
                **self.serialize_url_param(
                    "resourceGroupName", self.ctx.args.resource_group,
                    required=True,
                ),
                **self.serialize_url_param(
                    "ruleId", self.ctx.args.rule_name,
                    required=True,
                ),
                **self.serialize_url_param(
                    "subscriptionId", self.ctx.subscription_id,
                    required=True,
                ),
                **self.serialize_url_param(
                    "workspaceName", self.ctx.args.workspace_name,
                    required=True,
                ),
            }
            return parameters

        @property
        def query_parameters(self):
            parameters = {
                **self.serialize_query_param(
                    "api-version", "2022-06-01-preview",
                    required=True,
                ),
            }
            return parameters

        @property
        def header_parameters(self):
            parameters = {
                **self.serialize_header_param(
                    "Accept", "application/json",
                ),
            }
            return parameters

        def on_200(self, session):
            data = self.deserialize_http_content(session)
            self.ctx.set_var(
                "instance",
                data,
                schema_builder=self._build_schema_on_200
            )

        _schema_on_200 = None

        @classmethod
        def _build_schema_on_200(cls):
            if cls._schema_on_200 is not None:
                return cls._schema_on_200

            cls._schema_on_200 = AAZObjectType()

            _schema_on_200 = cls._schema_on_200
            _schema_on_200.etag = AAZStrType()
            _schema_on_200.id = AAZStrType(
                flags={"read_only": True},
            )
            _schema_on_200.kind = AAZStrType(
                flags={"required": True},
            )
            _schema_on_200.name = AAZStrType(
                flags={"read_only": True},
            )
            _schema_on_200.system_data = AAZObjectType(
                serialized_name="systemData",
                flags={"read_only": True},
            )
            _schema_on_200.type = AAZStrType(
                flags={"read_only": True},
            )

            system_data = cls._schema_on_200.system_data
            system_data.created_at = AAZStrType(
                serialized_name="createdAt",
                flags={"read_only": True},
            )
            system_data.created_by = AAZStrType(
                serialized_name="createdBy",
                flags={"read_only": True},
            )
            system_data.created_by_type = AAZStrType(
                serialized_name="createdByType",
                flags={"read_only": True},
            )
            system_data.last_modified_at = AAZStrType(
                serialized_name="lastModifiedAt",
                flags={"read_only": True},
            )
            system_data.last_modified_by = AAZStrType(
                serialized_name="lastModifiedBy",
                flags={"read_only": True},
            )
            system_data.last_modified_by_type = AAZStrType(
                serialized_name="lastModifiedByType",
                flags={"read_only": True},
            )

            disc_fusion = cls._schema_on_200.discriminate_by("kind", "Fusion")
            disc_fusion.properties = AAZObjectType(
                flags={"client_flatten": True},
            )

            properties = cls._schema_on_200.discriminate_by("kind", "Fusion").properties
            properties.alert_rule_template_name = AAZStrType(
                serialized_name="alertRuleTemplateName",
                flags={"required": True},
            )
            properties.description = AAZStrType(
                flags={"read_only": True},
            )
            properties.display_name = AAZStrType(
                serialized_name="displayName",
                flags={"read_only": True},
            )
            properties.enabled = AAZBoolType(
                flags={"required": True},
            )
            properties.last_modified_utc = AAZStrType(
                serialized_name="lastModifiedUtc",
                flags={"read_only": True},
            )
            properties.scenario_exclusion_patterns = AAZListType(
                serialized_name="scenarioExclusionPatterns",
            )
            properties.severity = AAZStrType(
                flags={"read_only": True},
            )
            properties.source_settings = AAZListType(
                serialized_name="sourceSettings",
            )
            properties.tactics = AAZListType(
                flags={"read_only": True},
            )
            properties.techniques = AAZListType(
                flags={"read_only": True},
            )

            scenario_exclusion_patterns = cls._schema_on_200.discriminate_by("kind", "Fusion").properties.scenario_exclusion_patterns
            scenario_exclusion_patterns.Element = AAZObjectType()

            _element = cls._schema_on_200.discriminate_by("kind", "Fusion").properties.scenario_exclusion_patterns.Element
            _element.date_added_in_utc = AAZStrType(
                serialized_name="dateAddedInUTC",
                flags={"required": True},
            )
            _element.exclusion_pattern = AAZStrType(
                serialized_name="exclusionPattern",
                flags={"required": True},
            )

            source_settings = cls._schema_on_200.discriminate_by("kind", "Fusion").properties.source_settings
            source_settings.Element = AAZObjectType()

            _element = cls._schema_on_200.discriminate_by("kind", "Fusion").properties.source_settings.Element
            _element.enabled = AAZBoolType(
                flags={"required": True},
            )
            _element.source_name = AAZStrType(
                serialized_name="sourceName",
                flags={"required": True},
            )
            _element.source_sub_types = AAZListType(
                serialized_name="sourceSubTypes",
            )

            source_sub_types = cls._schema_on_200.discriminate_by("kind", "Fusion").properties.source_settings.Element.source_sub_types
            source_sub_types.Element = AAZObjectType()

            _element = cls._schema_on_200.discriminate_by("kind", "Fusion").properties.source_settings.Element.source_sub_types.Element
            _element.enabled = AAZBoolType(
                flags={"required": True},
            )
            _element.severity_filters = AAZObjectType(
                serialized_name="severityFilters",
                flags={"required": True},
            )
            _element.source_sub_type_display_name = AAZStrType(
                serialized_name="sourceSubTypeDisplayName",
                flags={"read_only": True},
            )
            _element.source_sub_type_name = AAZStrType(
                serialized_name="sourceSubTypeName",
                flags={"required": True},
            )

            severity_filters = cls._schema_on_200.discriminate_by("kind", "Fusion").properties.source_settings.Element.source_sub_types.Element.severity_filters
            severity_filters.filters = AAZListType()
            severity_filters.is_supported = AAZBoolType(
                serialized_name="isSupported",
                flags={"read_only": True},
            )

            filters = cls._schema_on_200.discriminate_by("kind", "Fusion").properties.source_settings.Element.source_sub_types.Element.severity_filters.filters
            filters.Element = AAZObjectType()

            _element = cls._schema_on_200.discriminate_by("kind", "Fusion").properties.source_settings.Element.source_sub_types.Element.severity_filters.filters.Element
            _element.enabled = AAZBoolType(
                flags={"required": True},
            )
            _element.severity = AAZStrType(
                flags={"required": True},
            )

            tactics = cls._schema_on_200.discriminate_by("kind", "Fusion").properties.tactics
            tactics.Element = AAZStrType(
                flags={"read_only": True},
            )

            techniques = cls._schema_on_200.discriminate_by("kind", "Fusion").properties.techniques
            techniques.Element = AAZStrType(
                flags={"read_only": True},
            )

            disc_ml_behavior_analytics = cls._schema_on_200.discriminate_by("kind", "MLBehaviorAnalytics")
            disc_ml_behavior_analytics.properties = AAZObjectType(
                flags={"client_flatten": True},
            )

            properties = cls._schema_on_200.discriminate_by("kind", "MLBehaviorAnalytics").properties
            properties.alert_rule_template_name = AAZStrType(
                serialized_name="alertRuleTemplateName",
                flags={"required": True},
            )
            properties.description = AAZStrType(
                flags={"read_only": True},
            )
            properties.display_name = AAZStrType(
                serialized_name="displayName",
                flags={"read_only": True},
            )
            properties.enabled = AAZBoolType(
                flags={"required": True},
            )
            properties.last_modified_utc = AAZStrType(
                serialized_name="lastModifiedUtc",
                flags={"read_only": True},
            )
            properties.severity = AAZStrType(
                flags={"read_only": True},
            )
            properties.tactics = AAZListType(
                flags={"read_only": True},
            )
            properties.techniques = AAZListType(
                flags={"read_only": True},
            )

            tactics = cls._schema_on_200.discriminate_by("kind", "MLBehaviorAnalytics").properties.tactics
            tactics.Element = AAZStrType(
                flags={"read_only": True},
            )

            techniques = cls._schema_on_200.discriminate_by("kind", "MLBehaviorAnalytics").properties.techniques
            techniques.Element = AAZStrType(
                flags={"read_only": True},
            )

            disc_microsoft_security_incident_creation = cls._schema_on_200.discriminate_by("kind", "MicrosoftSecurityIncidentCreation")
            disc_microsoft_security_incident_creation.properties = AAZObjectType(
                flags={"client_flatten": True},
            )

            properties = cls._schema_on_200.discriminate_by("kind", "MicrosoftSecurityIncidentCreation").properties
            properties.alert_rule_template_name = AAZStrType(
                serialized_name="alertRuleTemplateName",
            )
            properties.description = AAZStrType()
            properties.display_name = AAZStrType(
                serialized_name="displayName",
                flags={"required": True},
            )
            properties.display_names_exclude_filter = AAZListType(
                serialized_name="displayNamesExcludeFilter",
            )
            properties.display_names_filter = AAZListType(
                serialized_name="displayNamesFilter",
            )
            properties.enabled = AAZBoolType(
                flags={"required": True},
            )
            properties.last_modified_utc = AAZStrType(
                serialized_name="lastModifiedUtc",
                flags={"read_only": True},
            )
            properties.product_filter = AAZStrType(
                serialized_name="productFilter",
                flags={"required": True},
            )
            properties.severities_filter = AAZListType(
                serialized_name="severitiesFilter",
            )

            display_names_exclude_filter = cls._schema_on_200.discriminate_by("kind", "MicrosoftSecurityIncidentCreation").properties.display_names_exclude_filter
            display_names_exclude_filter.Element = AAZStrType()

            display_names_filter = cls._schema_on_200.discriminate_by("kind", "MicrosoftSecurityIncidentCreation").properties.display_names_filter
            display_names_filter.Element = AAZStrType()

            severities_filter = cls._schema_on_200.discriminate_by("kind", "MicrosoftSecurityIncidentCreation").properties.severities_filter
            severities_filter.Element = AAZStrType()

            disc_nrt = cls._schema_on_200.discriminate_by("kind", "NRT")
            disc_nrt.properties = AAZObjectType(
                flags={"client_flatten": True},
            )

            properties = cls._schema_on_200.discriminate_by("kind", "NRT").properties
            properties.alert_details_override = AAZObjectType(
                serialized_name="alertDetailsOverride",
            )
            _build_schema_alert_details_override_read(properties.alert_details_override)
            properties.alert_rule_template_name = AAZStrType(
                serialized_name="alertRuleTemplateName",
            )
            properties.custom_details = AAZDictType(
                serialized_name="customDetails",
            )
            properties.description = AAZStrType()
            properties.display_name = AAZStrType(
                serialized_name="displayName",
                flags={"required": True},
            )
            properties.enabled = AAZBoolType(
                flags={"required": True},
            )
            properties.entity_mappings = AAZListType(
                serialized_name="entityMappings",
            )
            _build_schema_entity_mappings_read(properties.entity_mappings)
            properties.incident_configuration = AAZObjectType(
                serialized_name="incidentConfiguration",
            )
            _build_schema_incident_configuration_read(properties.incident_configuration)
            properties.last_modified_utc = AAZStrType(
                serialized_name="lastModifiedUtc",
                flags={"read_only": True},
            )
            properties.query = AAZStrType(
                flags={"required": True},
            )
            properties.severity = AAZStrType(
                flags={"required": True},
            )
            properties.suppression_duration = AAZStrType(
                serialized_name="suppressionDuration",
                flags={"required": True},
            )
            properties.suppression_enabled = AAZBoolType(
                serialized_name="suppressionEnabled",
                flags={"required": True},
            )
            properties.tactics = AAZListType()
            properties.techniques = AAZListType()
            properties.template_version = AAZStrType(
                serialized_name="templateVersion",
            )

            custom_details = cls._schema_on_200.discriminate_by("kind", "NRT").properties.custom_details
            custom_details.Element = AAZStrType()

            tactics = cls._schema_on_200.discriminate_by("kind", "NRT").properties.tactics
            tactics.Element = AAZStrType()

            techniques = cls._schema_on_200.discriminate_by("kind", "NRT").properties.techniques
            techniques.Element = AAZStrType()

            disc_scheduled = cls._schema_on_200.discriminate_by("kind", "Scheduled")
            disc_scheduled.properties = AAZObjectType(
                flags={"client_flatten": True},
            )

            properties = cls._schema_on_200.discriminate_by("kind", "Scheduled").properties
            properties.alert_details_override = AAZObjectType(
                serialized_name="alertDetailsOverride",
            )
            _build_schema_alert_details_override_read(properties.alert_details_override)
            properties.alert_rule_template_name = AAZStrType(
                serialized_name="alertRuleTemplateName",
            )
            properties.custom_details = AAZDictType(
                serialized_name="customDetails",
            )
            properties.description = AAZStrType()
            properties.display_name = AAZStrType(
                serialized_name="displayName",
                flags={"required": True},
            )
            properties.enabled = AAZBoolType(
                flags={"required": True},
            )
            properties.entity_mappings = AAZListType(
                serialized_name="entityMappings",
            )
            _build_schema_entity_mappings_read(properties.entity_mappings)
            properties.event_grouping_settings = AAZObjectType(
                serialized_name="eventGroupingSettings",
            )
            properties.incident_configuration = AAZObjectType(
                serialized_name="incidentConfiguration",
            )
            _build_schema_incident_configuration_read(properties.incident_configuration)
            properties.last_modified_utc = AAZStrType(
                serialized_name="lastModifiedUtc",
                flags={"read_only": True},
            )
            properties.query = AAZStrType(
                flags={"required": True},
            )
            properties.query_frequency = AAZStrType(
                serialized_name="queryFrequency",
                flags={"required": True},
            )
            properties.query_period = AAZStrType(
                serialized_name="queryPeriod",
                flags={"required": True},
            )
            properties.severity = AAZStrType(
                flags={"required": True},
            )
            properties.suppression_duration = AAZStrType(
                serialized_name="suppressionDuration",
                flags={"required": True},
            )
            properties.suppression_enabled = AAZBoolType(
                serialized_name="suppressionEnabled",
                flags={"required": True},
            )
            properties.tactics = AAZListType()
            properties.techniques = AAZListType()
            properties.template_version = AAZStrType(
                serialized_name="templateVersion",
            )
            properties.trigger_operator = AAZStrType(
                serialized_name="triggerOperator",
                flags={"required": True},
            )
            properties.trigger_threshold = AAZIntType(
                serialized_name="triggerThreshold",
                flags={"required": True},
            )

            custom_details = cls._schema_on_200.discriminate_by("kind", "Scheduled").properties.custom_details
            custom_details.Element = AAZStrType()

            event_grouping_settings = cls._schema_on_200.discriminate_by("kind", "Scheduled").properties.event_grouping_settings
            event_grouping_settings.aggregation_kind = AAZStrType(
                serialized_name="aggregationKind",
            )

            tactics = cls._schema_on_200.discriminate_by("kind", "Scheduled").properties.tactics
            tactics.Element = AAZStrType()

            techniques = cls._schema_on_200.discriminate_by("kind", "Scheduled").properties.techniques
            techniques.Element = AAZStrType()

            disc_threat_intelligence = cls._schema_on_200.discriminate_by("kind", "ThreatIntelligence")
            disc_threat_intelligence.properties = AAZObjectType(
                flags={"client_flatten": True},
            )

            properties = cls._schema_on_200.discriminate_by("kind", "ThreatIntelligence").properties
            properties.alert_rule_template_name = AAZStrType(
                serialized_name="alertRuleTemplateName",
                flags={"required": True},
            )
            properties.description = AAZStrType(
                flags={"read_only": True},
            )
            properties.display_name = AAZStrType(
                serialized_name="displayName",
                flags={"read_only": True},
            )
            properties.enabled = AAZBoolType(
                flags={"required": True},
            )
            properties.last_modified_utc = AAZStrType(
                serialized_name="lastModifiedUtc",
                flags={"read_only": True},
            )
            properties.severity = AAZStrType(
                flags={"read_only": True},
            )
            properties.tactics = AAZListType(
                flags={"read_only": True},
            )
            properties.techniques = AAZListType(
                flags={"read_only": True},
            )

            tactics = cls._schema_on_200.discriminate_by("kind", "ThreatIntelligence").properties.tactics
            tactics.Element = AAZStrType(
                flags={"read_only": True},
            )

            techniques = cls._schema_on_200.discriminate_by("kind", "ThreatIntelligence").properties.techniques
            techniques.Element = AAZStrType(
                flags={"read_only": True},
            )

            return cls._schema_on_200

_schema_alert_details_override_read = None

def _build_schema_alert_details_override_read(_schema):
    global _schema_alert_details_override_read
    if _schema_alert_details_override_read is not None:
        _schema.alert_description_format = _schema_alert_details_override_read.alert_description_format
        _schema.alert_display_name_format = _schema_alert_details_override_read.alert_display_name_format
        _schema.alert_severity_column_name = _schema_alert_details_override_read.alert_severity_column_name
        _schema.alert_tactics_column_name = _schema_alert_details_override_read.alert_tactics_column_name
        return

    _schema_alert_details_override_read = AAZObjectType()

    alert_details_override_read = _schema_alert_details_override_read
    alert_details_override_read.alert_description_format = AAZStrType(
        serialized_name="alertDescriptionFormat",
    )
    alert_details_override_read.alert_display_name_format = AAZStrType(
        serialized_name="alertDisplayNameFormat",
    )
    alert_details_override_read.alert_severity_column_name = AAZStrType(
        serialized_name="alertSeverityColumnName",
    )
    alert_details_override_read.alert_tactics_column_name = AAZStrType(
        serialized_name="alertTacticsColumnName",
    )

    _schema.alert_description_format = _schema_alert_details_override_read.alert_description_format
    _schema.alert_display_name_format = _schema_alert_details_override_read.alert_display_name_format
    _schema.alert_severity_column_name = _schema_alert_details_override_read.alert_severity_column_name
    _schema.alert_tactics_column_name = _schema_alert_details_override_read.alert_tactics_column_name

_schema_entity_mappings_read = None

def _build_schema_entity_mappings_read(_schema):
    global _schema_entity_mappings_read
    if _schema_entity_mappings_read is not None:
        _schema.Element = _schema_entity_mappings_read.Element
        return

    _schema_entity_mappings_read = AAZListType()

    entity_mappings_read = _schema_entity_mappings_read
    entity_mappings_read.Element = AAZObjectType()

    _element = _schema_entity_mappings_read.Element
    _element.entity_type = AAZStrType(
        serialized_name="entityType",
    )
    _element.field_mappings = AAZListType(
        serialized_name="fieldMappings",
    )

    field_mappings = _schema_entity_mappings_read.Element.field_mappings
    field_mappings.Element = AAZObjectType()

    _element = _schema_entity_mappings_read.Element.field_mappings.Element
    _element.column_name = AAZStrType(
        serialized_name="columnName",
    )
    _element.identifier = AAZStrType()

    _schema.Element = _schema_entity_mappings_read.Element

_schema_incident_configuration_read = None

def _build_schema_incident_configuration_read(_schema):
    global _schema_incident_configuration_read
    if _schema_incident_configuration_read is not None:
        _schema.create_incident = _schema_incident_configuration_read.create_incident
        _schema.grouping_configuration = _schema_incident_configuration_read.grouping_configuration
        return

    _schema_incident_configuration_read = AAZObjectType()

    incident_configuration_read = _schema_incident_configuration_read
    incident_configuration_read.create_incident = AAZBoolType(
        serialized_name="createIncident",
        flags={"required": True},
    )
    incident_configuration_read.grouping_configuration = AAZObjectType(
        serialized_name="groupingConfiguration",
    )

    grouping_configuration = _schema_incident_configuration_read.grouping_configuration
    grouping_configuration.enabled = AAZBoolType(
        flags={"required": True},
    )
    grouping_configuration.group_by_alert_details = AAZListType(
        serialized_name="groupByAlertDetails",
    )
    grouping_configuration.group_by_custom_details = AAZListType(
        serialized_name="groupByCustomDetails",
    )
    grouping_configuration.group_by_entities = AAZListType(
        serialized_name="groupByEntities",
    )
    grouping_configuration.lookback_duration = AAZStrType(
        serialized_name="lookbackDuration",
        flags={"required": True},
    )
    grouping_configuration.matching_method = AAZStrType(
        serialized_name="matchingMethod",
        flags={"required": True},
    )
    grouping_configuration.reopen_closed_incident = AAZBoolType(
        serialized_name="reopenClosedIncident",
        flags={"required": True},
    )

    group_by_alert_details = _schema_incident_configuration_read.grouping_configuration.group_by_alert_details
    group_by_alert_details.Element = AAZStrType()

    group_by_custom_details = _schema_incident_configuration_read.grouping_configuration.group_by_custom_details
    group_by_custom_details.Element = AAZStrType()

    group_by_entities = _schema_incident_configuration_read.grouping_configuration.group_by_entities
    group_by_entities.Element = AAZStrType()

    _schema.create_incident = _schema_incident_configuration_read.create_incident
    _schema.grouping_configuration = _schema_incident_configuration_read.grouping_configuration

__all__ = ["Show"]


"""
@brief      test log(time=1s)
"""
import os
import sys
import unittest
import warnings

if sys.version_info[0] == 2:
    from Tkinter import TclError
else:
    from tkinter import TclError

try:
    import src
except ImportError:
    path = os.path.normpath(
        os.path.abspath(
            os.path.join(
                os.path.split(__file__)[0],
                "..",
                "..")))
    if path not in sys.path:
        sys.path.append(path)
    import src

from src.pyquickhelper.loghelper.flog import fLOG
from src.pyquickhelper.pycode import fix_tkinter_issues_virtualenv
from src.pyquickhelper.funcwin import open_window_params, open_window_function
from src.pyquickhelper.funcwin.function_helper import get_function_list, has_unknown_parameters, private_get_function

def my_tst_function(a, b):
    """
    return a+b
    @param      a   (float) float
    @param      b   (float) float
    @return         a+b
    """
    return (a or 1) + (b or 1)

class TestWindows (unittest.TestCase):

    def test_open_window_params(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")
        fix_tkinter_issues_virtualenv(fLOG=fLOG)
        params = dict(p1="p1", p2=3)
        try:
            win = open_window_params(params, do_not_open=True)
        except TclError as e:
            warnings.warn("TclError" + str(e))
            return
        fLOG(type(win))
        assert isinstance(
            win,
            src.pyquickhelper.funcwin.frame_params.FrameParams)

    def test_open_window_function(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")
        fix_tkinter_issues_virtualenv(fLOG=fLOG)
        func = my_tst_function
        try:
            win = open_window_function(func, do_not_open=True)
        except TclError as e:
            warnings.warn("TclError" + str(e))
            return
        fLOG(type(win))
        assert isinstance(
            win,
            src.pyquickhelper.funcwin.frame_function.FrameFunction)
        win.refresh()
        win.update()
        win.run_function(0, 1)

    def test_get_function_list(self):
        fLOG(
            __file__,
            self._testMethodName,
            OutputPrint=__name__ == "__main__")
        fix_tkinter_issues_virtualenv(fLOG=fLOG)
        funcs = get_function_list(src.pyquickhelper)
        assert isinstance(funcs, dict)
        assert len(funcs) > 0
        if "load_ipython_extension" not in funcs:
            raise Exception("\n".join(sorted(funcs.keys())))
        r = has_unknown_parameters(funcs["load_ipython_extension"])
        assert not r
        f = private_get_function("os.listdir")
        fLOG("**", f)

if __name__ == "__main__":
    unittest.main()


import string
import itertools as it

def execute():
    print("HELLO WORLD".isupper())
    print(string.ascii_uppercase)
    whitespace_set = set(string.whitespace)
    word = "".join(letter for letter in "HELLO WORLD" if letter not in whitespace_set)
    print(word)

def ittertools():
    a = map(pow, range(10), it.repeat(2))
    print(list(a))
    ones = list(it.repeat(1, times=8))
    print(ones)
    friends = ["Jack", "Jill", "Joe"]
    permutations = it.permutations(friends, r=2)
    print(list(permutations))
    combinations = it.combinations(friends, r=2)
    print(list(combinations))

if __name__ == '__main__':
    ittertools()


###############################################################################
#
# Tests for XlsxWriter.
#
# Copyright (c), 2013-2021, John McNamara, jmcnamara@cpan.org
#

import unittest
from ...compatibility import StringIO
from ...worksheet import Worksheet

class TestCalculateSpans(unittest.TestCase):
    """
    Test the _prepare_formula Worksheet method for different formula types.

    """

    def test_prepare_formula(self):
        self.fh = StringIO()
        self.worksheet = Worksheet()
        self.worksheet._set_filehandle(self.fh)

        testcases = [
            ['=foo()', 'foo()'],
            ['{foo()}', 'foo()'],
            ['{=foo()}', 'foo()'],
            ['SEQUENCE(10)', '_xlfn.SEQUENCE(10)'],
            ['UNIQUES(A1:A10)', 'UNIQUES(A1:A10)'],
            ['UNIQUE(A1:A10)', '_xlfn.UNIQUE(A1:A10)'],
            ['_xlfn.SEQUENCE(10)', '_xlfn.SEQUENCE(10)'],
            ['SORT(A1:A10)', '_xlfn._xlws.SORT(A1:A10)'],
            ['RANDARRAY(10,1)', '_xlfn.RANDARRAY(10,1)'],
            ['ANCHORARRAY(C1)', '_xlfn.ANCHORARRAY(C1)'],
            ['SORTBY(A1:A10,B1)', '_xlfn.SORTBY(A1:A10,B1)'],
            ['FILTER(A1:A10,1)', '_xlfn._xlws.FILTER(A1:A10,1)'],
            ['XMATCH(B1:B2,A1:A10)', '_xlfn.XMATCH(B1:B2,A1:A10)'],
            ['COUNTA(ANCHORARRAY(C1))', 'COUNTA(_xlfn.ANCHORARRAY(C1))'],
            ['XLOOKUP("India",A22:A23,B22:B23)', '_xlfn.XLOOKUP("India",A22:A23,B22:B23)'],
            ['XLOOKUP(B1,A1:A10,ANCHORARRAY(D1))', '_xlfn.XLOOKUP(B1,A1:A10,_xlfn.ANCHORARRAY(D1))'],
            ['LAMBDA(_xlpm.number, _xlpm.number + 1)(1)', '_xlfn.LAMBDA(_xlpm.number, _xlpm.number + 1)(1)'],
        ]

        for testcase in testcases:
            formula = testcase[0]
            exp = testcase[1]
            got = self.worksheet._prepare_formula(formula)

            self.assertEqual(got, exp)


# -*- coding: utf-8 -*-
"""
Created on Wed Jul 10 15:58:42 2019

@author: abibeka
"""

#0.0 Housekeeping. Clear variable space
from IPython import get_ipython  #run magic commands
ipython = get_ipython()
ipython.magic("reset -f")
ipython = get_ipython()

import os

import pandas as pd
import multiprocessing as mp
import time
import numpy as np
from sys import getsizeof
import dask.dataframe as dd
import snappy
import fastparquet

# Write to Parquet Combine data for June, July and August
########################################################################################################################
#mnt = ["June","July","August"]
#i="August"
#LiDat = []
#for i in mnt:
#    fi="D:/hi-res_data/hi_res_2018-"+i+".csv"
#    df = dd.read_csv(fi)
#    SigIDs_HighActuations= [334,  329,  344,  342,  346, 4404,   77,  328,  327,
#           1222, 4601, 1223, 1935, 2869, 1224,  352,  345,  325,  332, 1221]
#    da1 = df[df['signal_id'].isin(SigIDs_HighActuations)]
#    #https://stackoverflow.com/questions/32940416/methods-for-writing-parquet-files-using-python
#    da1.columns
#    #https://github.com/dask/dask/issues/3717
#    #https://stackoverflow.com/questions/39584118/dask-dataframe-how-to-convert-column-to-to-datetime
#    meta = ('event_timestamp', 'datetime64[ns]')
#    da1= dd.from_pandas(da1,npartitions=1)
#    da1.event_timestamp = da1.event_timestamp.map_partitions(pd.to_datetime,
#                                                   format='%Y-%m-%d %H:%M:%S',
#                                                   meta=meta)
#    LiDat.append(da1)
#Dat_All = dd.concat(LiDat,axis=0)
#Dat_All.head(100)
#Dat_All.dtypes

##https://docs.dask.org/en/latest/dataframe-best-practices.html
#fCom="C:/Users/abibeka/OneDrive - Kittelson & Associates, Inc/Documents/SignalTiming-NonMotorizedUsers/Dask/SigHighActuations"
#fOut = fCom+"Complete20IntDat.snap.parquet"
##https://github.com/dask/dask/issues/3842
#Dat_All.to_parquet(fOut, engine='fastparquet',write_index=False, partition_on=['signal_id'])
#

#Save data seperately for August, July, June
########################################################################################################################
#Months = ["August","July","June"]
#for i in Months:
#    start_time = time.time()
#    fi="D:/hi-res_data/hi_res_2018-"+i+".csv"
#    df = dd.read_csv(fi)
#    SigIDs_HighActuations= [334,  329,  344,  342,  346, 4404,   77,  328,  327,
#           1222, 4601, 1223, 1935, 2869, 1224,  352,  345,  325,  332, 1221]
#    df1 = df[df['signal_id'].isin(SigIDs_HighActuations)]
#    #https://stackoverflow.com/questions/32940416/methods-for-writing-parquet-files-using-python
#    fname="C:/Users/abibeka/OneDrive - Kittelson & Associates, Inc/Documents/SignalTiming-NonMotorizedUsers/Dask/SigHighActuations"+i+"Data.snap.parquet"
#    df1.to_parquet(fname,compression='snappy')
#    endtime= time.time()
#    print(endtime-start_time)
#
# Read the parquet file
########################################################################################################################
i="August"
fname="C:/Users/abibeka/OneDrive - Kittelson & Associates, Inc/Documents/SignalTiming-NonMotorizedUsers/Dask/SigHighActuations"+i+"Data.snap.parquet"
da1=pd.read_parquet(fname)
gb = da1.groupby(['signal_id'])
gb = da1.groupby(['signal_id','event_code_id'])
counts = gb.size().to_frame(name='counts').reset_index()
countAug= counts
i="June"
fname="C:/Users/abibeka/OneDrive - Kittelson & Associates, Inc/Documents/SignalTiming-NonMotorizedUsers/Dask/SigHighActuations"+i+"Data.snap.parquet"
da1=pd.read_parquet(fname)
# Get Total # of actuations by Signal ID
gb = da1.groupby(['signal_id','event_code_id'])
counts = gb.size().to_frame(name='counts').reset_index()
countJune= counts

i="July"
fname="C:/Users/abibeka/OneDrive - Kittelson & Associates, Inc/Documents/SignalTiming-NonMotorizedUsers/Dask/SigHighActuations"+i+"Data.snap.parquet"
da1=pd.read_parquet(fname)
# Get Total # of actuations by Signal ID
gb = da1.groupby(['signal_id','event_code_id'])
counts = gb.size().to_frame(name='counts').reset_index()
countJuly= counts
del da1
#Convert Parquet file to csv format
########################################################################################################################
#Months = ["August","July","June"]
#commonPa="C:\\Users\\abibeka\\OneDrive - Kittelson & Associates, Inc\\Documents\\SignalTiming-NonMotorizedUsers"
#for i in Months:
#    fin="C:/Users/abibeka/OneDrive - Kittelson & Associates, Inc/Documents/SignalTiming-NonMotorizedUsers/Dask/SigHighActuations"+i+"Data.snap.parquet"
#    SigIDs_HighActuations= [334,  329,  344,  342,  346, 4404,   77,  328,  327,
#           1222, 4601, 1223, 1935, 2869, 1224,  352,  345,  325,  332, 1221]
#    da1=pd.read_parquet(fin)
#    for SigID in SigIDs_HighActuations:
#        da2=da1[da1['signal_id'].isin(SigID)]
#        fout= os.path.join(commonPa,"DataNEUniv",i+"_"+str(SigID)+"_SPMs20IntHighPedAct.csv")
#        da2.to_csv(fout,index=False)
#

# Read the csv files
########################################################################################################################
import glob
#Don't want to loop over all the signals. Do it one by one. Easy to stop
#Use Yield
def ReturnLen(List1):
    for l in List1:
        df=dd.read_csv(l)
        a = int(df.head(1).signal_id)
        yield a,len(df)

ListFiles=glob.glob('C:\\Users\\abibeka\\OneDrive - Kittelson & Associates, Inc\\Documents\\SignalTiming-NonMotorizedUsers\\DataNEUniv\\August*')
l1= ListFiles[0]

ListFiles=glob.glob('C:\\Users\\abibeka\\OneDrive - Kittelson & Associates, Inc\\Documents\\SignalTiming-NonMotorizedUsers\\DataNEUniv\\July*')

#See if we get 20 values
for l1 in ListFiles:
    da = pd.read_csv(l1,nrows=1)
    print(da)

#Don't want to loop over all the signals. Do it one by one. Easy to stop
#Use Yield
Res = ReturnLen(ListFiles)
X,Y = next(Res)
X
Y


#!/usr/bin/env python
#
# -*- coding: utf-8 -*-
#
#  humanlogformat.py
#
#  Copyright 2016 Jelle Smet <development@smetj.net>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
#

from wishbone import Actor
from wishbone.event import Log
from time import strftime, localtime
import os
import sys

class HumanLogFormat(Actor):

    '''**Converts the internal log format into human readable form.**

    Logs are formated from the internal wishbone format into a more
    pleasing human readable format suited for STDOUT or a logfile.

    Internal Wishbone format:

    (6, 1367682301.430527, 3342, 'Router', 'Received SIGINT. Shutting down.')

    Sample output format:

    2013-08-04T19:54:43 pid-3342 informational dictgenerator: Initiated
    2013-08-04T19:54:43 pid-3342 informational metrics_null: Started

    Parameters:

        n/a

    Queues:

        - inbox
           |  Incoming messages

        - outbox
           |  Outgoing messges
    '''

    def __init__(self, actor_config, colorize=True, ident=None):
        Actor.__init__(self, actor_config)
        self.levels = {
            0: "emergency",
            1: "alert",
            2: "critical",
            3: "error",
            4: "warning",
            5: "notice",
            6: "informational",
            7: "debug"
        }
        self.colors = {
            0: "\x1B[0;35m",
            1: "\x1B[1;35m",
            2: "\x1B[0;31m",
            3: "\x1B[1;31m",
            4: "\x1B[1;33m",
            5: "\x1B[1;30m",
            6: "\x1B[1;37m",
            7: "\x1B[1;37m"
        }

        if self.kwargs.colorize:
            self.colorize = self.doColorize
        else:
            self.colorize = self.doNoColorize

        self.pool.createQueue("inbox")
        self.pool.createQueue("outbox")
        self.registerConsumer(self.consume, "inbox")

        if self.kwargs.ident is None:
            self.kwargs.ident = os.path.basename(sys.argv[0])

    def consume(self, event):

        data = event.get('@data')
        if isinstance(data, Log):
            log = ("%s %s %s %s: %s" % (
                strftime("%Y-%m-%dT%H:%M:%S", localtime(data.time)),
                "%s[%s]:" % (self.kwargs.ident, data.pid),
                self.levels[data.level],
                data.module,
                data.message))
            event.set(self.colorize(log, data.level))
            self.submit(event, self.pool.queue.outbox)
        else:
            raise Exception("Incoming data needs to be of type <wishbone.event.Log>. Dropped event.")

    def doColorize(self, message, level):
        return self.colors[level] + message + "\x1B[0m"

    def doNoColorize(self, message, level):
        return message


def __monte_carlo(sample_size, generator, success_criterion):
    '''
    Runs a monolithic Monte-Carlo simulation for input |generator|
    and |success_criterion|.
    '''
    bulleyes = 0
    for i in range(sample_size):
        if success_criterion(generator()):
            bulleyes += 1
    return float(bulleyes) / sample_size

def __get_monte_carlo_results(sample_size, simulation):
    '''
    Runs a Monte-Carlo simulation for specified inputs:
    |sample_size|, integer number of samples to simulate.
    |simulation|, containing a generator and a success criterion.
    '''
    results = []
    for simulator in simulation:
        monte_carlo_result = __monte_carlo(sample_size,
                                           generator=simulator[0],
                                           success_criterion=simulator[1])
        results.append(monte_carlo_result)
    return results

def monte_carlo_simulation_results(sample_size, simulation_framework):
    '''
    Uses the input |simulation_framework| and its generators and success
    criteria to aggregate Monte-Carlo results for |sample_size|.
    '''
    results = []
    for simulation in simulation_framework:
        results.append(__get_monte_carlo_results(sample_size, simulation))
    return results


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 30 17:54:58 2019

@author: ethangreig
"""

from constants import next_dict, value_dict, trump_value_dict
from random import shuffle

class Round:
    def __init__(self, deal):
        self.hands = deal.hands
        self.played = []
        self.tricks = 0
        self.select_trump()
        self.hand = self.hands[0]

    def select_trump(self, trump='H'):
        self.trump = trump
        self.hands = trumpify(self.hands, self.trump)

    def play(self, trick=None):
        if trick==None:
            t = Trick()

def trumpify(hands, trump):
    next_suit = next_dict[trump]
    for i in range(len(hands)):
        hands[i] = ['L'+trump if card=='J'+next_suit else card for card in hands[i]]

class Trick:
    def __init__(self, card, trump='H', players=4, leader=1):
        self.trump = trump
        self.num_players = players
        self.leader = leader
        self.winner = None
        self.follow = card[1]
        self.num_cards = 1
        self.cards = [card]

    def add(self, card):
        self.cards.append(card)
        self.num_cards += 1
        if self.num_cards==self.num_players:
            self.decide_trick()

    def decide_trick(self):
        ranks = [card[0] for card in t.cards]
        suits = [card[1] for card in t.cards]
        if t.trump in suits:
            trump_cards = [i for i in range(t.num_cards) if suits[i]==t.trump]
            trump_rank_values = [trump_value_dict[ranks[i]] for i in trump_cards]
            t.winner = trump_ranks.index(max(trump_ranks))

    def compare_trump(self, card_a, card_b):
        card_a

ranks = [card[0] for card in t.cards]
suits = [card[1] for card in t.cards]
if t.trump in suits:
    trump_rank_values = [trump_value_dict[ranks[i]] if suits[i]==t.trump else -1 for i in range(t.num_cards)]
    t.winner = trump_rank_values.index(max(trump_rank_values))
else:
    follow_rank_values = [value_dict[ranks[i]] if suits[i]==t.follow else -1 for i in range(t.num_cards)]
    t.winner = follow_rank_values.index(max(follow_rank_values))


def decimal(numbin):
    numbin=str(numbin)
    decimal=0
    exp=len(numbin)-1
    for i in numbin:
        decimal +=(int(i)*2**(exp))
        exp=exp-1
    return decimal

entrada=input("ingrese numero binario:")
conv=decimal(entrada)
print(f" {entrada}  su coversion es: {conv} a numero entero")


#!/usr/local/bin/python3
#-*-coding:utf-8 -*-

############### VERSION 2 ###############
#version2: ajout de la fonction offset
#version2.1: modification des legendes. Inclusion de la frequence et amplitude à chaque fois
############### VERSION 3 ###############
#version3: ajout de la fonction auto_offset
#version3.1: ajout de liste des courbes à tracer
#version3.2(20151103): ajustement de l'échelle aux courbes tracées uniquement
#version3.3(20151105): permet de traiter des .dat avec des quantités variables de colonnes
############### VERSION 4 ###############
#version4.0(20151105): refonte de l'organisation du code source pour corriger certains bugs:
#		les courbes non tracées (non nommées dans tracer() ne sont pas traitées du tout et donc
#		évite certains messages d'erreurs
#version4.1(20151105): permet de sélectionner des fichiers avec une extension autre que .dat
#version4.2(20151123): permet le choix de l'unité de l'amplitude du signal d'entrée
#version4.3(20151207): correction mineures des informations affichées dans la console
#version4.4(20160113): change la façon de titrer les graphs .png pour permettre un .png par combinaison de courbes tracées
#version4.5(20160122): permet la désactivation de l'option print graph in .png
############### VERSION 5 ###############
#version5.0(20160122): permet de tracer plusieurs colonnes d'un même fichier (ou de plusieurs fichiers) sur un même graph
#version5.1(20160205): correction de la matrice donnees_exp qui n'était pas remplie correctement dans le code

############### AMELIORATIONS POSSIBLES ###############
# utilisation de l'option '0' en id courbe pour appliquer offset à toutes les courbes pour la colonne souhaitée
# utilisation de la fonction interpolation de scipy pour manipuler des fonctions plutôt que des matrices de données
from scipy import *
from pylab import *
import matplotlib.pyplot as plt
import glob, inspect, os

# NE MODIFIER QUE LES VARIABLES ENTRES LES BARRES #
#######################################################
################## PEUT ETRE MODIFIE ##################
##################VVVVVVVVVVVVVVVVVVV##################

extension='.dat'	#extension des fichiers à considérer pour tracer les courbes (inclure le .)
print_graph='non'	#'oui'/'non' pour générer ou non une copie .png
delimiteur=''		#rien par default
tracer=[0]			#tout tracer: 0, sinon liste [1,2,3,5,8,...] AVEC CROCHETS

#informations relatives aux fichiers de données
data_set_up=5	#nombre de premières lignes ignorées dans le fichier de données .dat
data_set_low=0	#nombre de dernières lignes ignorées dans le fichier de données .dat
data_x_axis=1	#colonne de données correspondant aux abscisses
data_y_axis=[4]	#colonne(s) de données correspondant aux ordonnées (garder les crochets)
col_freq=6		#colonne où est indiquée la fréquence des mesures
col_amp=8		#colonne où est indiquée l'amplitude des mesures
unit_amp='V'	#unité de l'amplitude du signal en entrée

#informations relatives aux tracés
titref="default title: x-axis --> column " +str(data_x_axis) + " y-axis --> column " +str(data_y_axis) + " of " + extension + " files in folder"
nom_axe_x='Temperature (K)'
nom_axe_y='Magnitude (V)'
grille='oui'

#informations relative au traitement des données
liste_offset=['non',(2,2,3,2),(2,3,0,3)] #si offset[0]='oui', 1er chiffre du tuple --> id courbe, 2e --> id colonne, 3e --> offset X, 4e --> offset Y
								# le nombre de tuples ne peux pas être supérieur on nombre de .dat dans le dossier
auto_offset=('non', 0) #pour mettre un offset à toutes les courbes de telle sorte qu'elles commencent toutes
							# au point d'ordonnée indiqué. Cette option est maîtresse sur liste_offset (y seulement).

###/ \#################################################/ \###  ||
##/ | \############### NE PAS MODIFIER ###############/ | \##  ||
#/__°__\#############VVVVVVVVVVVVVVVVVVV#############/__°__\# _||_
															# \  /
															#  \/

# supprime les doublons dans tracer et data_y_axis s'il y a
tracer=list(set(tracer))
data_y_axis=list(set(data_y_axis))
liste_offset=list(set(liste_offset))

script_dir=os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))+'/'	# to get the path to the directory containing the script and the data
liste_fichiers=glob.glob(script_dir+'*'+extension)	#list of all .dat files in script_dir

######## VARIABLES DES QUANTITES A TRACER ########
nombre_fichiers=len(liste_fichiers)	#number of files in directory
nombre_colonnes=len(data_y_axis)	#number of columns to plot

######## OPERATION SUR TRACER ########
# on sort de tracer tous les élément >nombre_fichiers et <0
if max(tracer)>nombre_fichiers:
	while max(tracer)>nombre_fichiers:
		tracer.remove(max(tracer))
if min(tracer)<0:
	while min(tracer)<0:
		tracer.remove(min(tracer))

# si alors il ne reste plus aucun élément dans tracer ou si 0 dans tracer, on trace toutes les courbes
if len(tracer)==0 or (0 in tracer):
	tracer=list(arange(nombre_fichiers)+1)

nombre_courbes=len(tracer)			#number of files to plot

######## AFFICHAGE INFO ########
print('---> '+str(nombre_courbes)+' '+extension+' to plot')
print('---> '+str(nombre_colonnes)+' columns to plot per '+extension)

######## COLONNES A TRACER ########
plot_cols=[data_x_axis-1]	# creates the list of the columns that must be ploted with the x column

for i in data_y_axis:	# adds the other columns that must be ploted
	plot_cols.append(i-1)

######## DEFINITION FONCTIONS TRAITEMENT ########
# fonction importation des données des axes à tracer:
def import_axe_data(nom_fichier, delimiteur, data_set_up, data_set_low, plot_cols):
	# Import the data from a text file and save as a 2D matrix. Adapter le 'skip_header' au .dat
	print("---> fichier", extension, "en cours de traitement :", nom_fichier)
	return genfromtxt(nom_fichier, delimiter=delimiteur, skip_header=data_set_up, skip_footer=data_set_low, usecols=plot_cols)#stock que les données utilisées

# fonction importation des données relatives aux conditions expérimentales:
def import_exp_data(nom_fichier, delimiteur, data_set_up, data_set_low, col_freq, col_amp):
	matrice_donnees_exp=zeros((2,1)) #stock la fréquence (ligne0) et l'amplitude (ligne1)
	#on utilise genfromtxt à 2 endroits différents du fichier données usecols(col_freq-1 et col_amp-1) car ces colonnes peuvent être de longueur différentes
	matrice_donnees_exp[0] = genfromtxt(nom_fichier, delimiter=delimiteur, skip_header=data_set_up, skip_footer=data_set_low, usecols=(col_freq-1))[0]#stock la fréquence
	matrice_donnees_exp[1] = genfromtxt(nom_fichier, delimiter=delimiteur, skip_header=data_set_up, skip_footer=data_set_low, usecols=(col_amp-1))[0]#stock l'amplitude
	return matrice_donnees_exp

######## REMPLISSAGE LISTE DE MATRICES DONNEES ########
donnees=[]	# initialisation de la liste des matrices de données des courbes à tracer
# autant de matrices sont crées qu'il y a de courbes à tracer et elles sont rangées dans donnees
# pour accéder à la matrice d'une courbe: donnees[tracer.index(i)]
for i in tracer:
	donnees.append(import_axe_data(liste_fichiers[i-1], delimiteur, data_set_up, data_set_low, plot_cols))

######## SI IL FAUT AJOUTER UN OU DES OFFSET ########
#fabrication et initialisation matrice des offsets
offset=array(zeros((len(tracer)+1,nombre_colonnes+2)),str)	# crée une matrice de nombre de courbes à tracer +1 lignes et nombre de colonnes à tracer +2 colonnes
offset[0,0]='id courbe'	# titre 1ere colonne
offset[0,1]='offset X'	# titre 2e colonne
for i in data_y_axis:
	offset[0,data_y_axis.index(i)+2]='offset Y'+str(i)	# titre des autres colonnes

for i in tracer:
	offset[tracer.index(i)+1,0]=i	#remplit l'id fichier dans chacunes des matrices offset

# cas où liste_offset[0]=='oui' et auto_offset[0]!='oui'
if ('oui' in liste_offset or 'Oui' in liste_offset or 'OUI' in liste_offset or 'O' in liste_offset or 'o' in liste_offset) and ('oui' not in auto_offset and 'Oui' not in auto_offset and 'OUI' not in auto_offset and 'O' not in auto_offset and 'o' not in auto_offset):
	for i in liste_offset[1:]:
		if (i[0] in tracer) and (i[1] in data_y_axis):		# seulement si demandé dans data_y_axis et tracer
			offset[tracer.index(i[0])+1,1]=i[2]	# remplit offset X
			offset[tracer.index(i[0])+1,data_y_axis.index(i[1])+2]=i[3]	# remplit offset Y pour la colonne donnée par i[1]

# cas où auto_offset[0]=='oui'
if 'oui' in auto_offset or 'Oui' in auto_offset or 'OUI' in auto_offset or 'O' in auto_offset or 'o' in auto_offset:
	for i in tracer:		# pour chacune des courbes à tracer (indiquées dans tracer)
		for j in data_y_axis:	# pour chaque colonne à tracer (EXCLUANT X)
			# on change les offset en y seulement dans la matrice offset
			offset[tracer.index(i)+1,data_y_axis.index(j)+2]= auto_offset[1]-donnees[tracer.index(i)][0][data_y_axis.index(j)+1]

######## AJOUT OFFSET AUX DONNEES ########
donnees_off=donnees	# duplication de donnees pour creer donnees_off qui est la matrice des données avec offset
# il faut maintenant ajouter les offset à donnees_off si nécessaire (i.e. si offset ou auto_offset est 'oui')
if ('oui' in liste_offset or 'Oui' in liste_offset or 'OUI' in liste_offset or 'O' in liste_offset or 'o' in liste_offset) or ('oui' in auto_offset or 'Oui' in auto_offset or 'OUI' in auto_offset or 'O' in auto_offset or 'o' in auto_offset):
	for i in tracer:
		donnees_off_temp=array(donnees_off[tracer.index(i)])	# étape nécessaire pour manipuler les objets de la liste donnees_off comme de vraies matrices et permettre les sommes sur les colonnes/lignes
		for j in plot_cols:
			donnees_off_temp[:,plot_cols.index(j)]+=float(offset[tracer.index(i)+1,plot_cols.index(j)+1])	# on travaille sur donnees_off_temp qui est de type array et permet donc les sommes sur ligne/colonne entière
			donnees_off[tracer.index(i)]=donnees_off_temp	# on remplace la matrice de la courbe i par les données avec offset
# exemple pour sommer sur colonnes :
#>>> A
#array([[1, 1, 1],
#       [2, 2, 2],
#       [3, 3, 3]])
#>>> B=A
#>>> B[:,0]=B[:,0]+4	# ou bien B[:,0]+=4
#>>> B
#array([[5, 1, 1],
#       [6, 2, 2],
#       [7, 3, 3]])

######## OPERATION SUR LES AXES ########
# création des 2 matrices de stockage des échelles (une pour x et l'autre pour y)
xscale=zeros((nombre_courbes, 2))	# matrice des X min et max pour chaque fichier à tracer
yscale=zeros((nombre_courbes, nombre_colonnes, 2))	# matrices des Y min et max de chaque colonnes à tracer pour chaque fichier
# xscale=	[[xmin_courbe1, xmax_courbe1],
#			 [xmin_courbe4, xmax_courbe4],
#			 [xmin_courbe3, xmax_courbe3],...] si tracer=[1,4,3,...]

# yscale=	[[[y1min_courbe1, y1max_courbe1],[y3min_courbe1, y3max_courbe1]],
#			 [[y1min_courbe4, y1max_courbe4],[y3min_courbe4, y3max_courbe4]],
#			 [[y1min_courbe3, y1max_courbe3],[y3min_courbe3, y3max_courbe3]],...] si tracer=[1,4,3,...] et data_y_axis=[1,3]

# remplissage matrices échelles
for i in tracer:	# on traite fichier par fichier
	# on remplit xscale
	xscale[tracer.index(i),0]=min(array(donnees_off[tracer.index(i)])[:,0])
	xscale[tracer.index(i),1]=max(array(donnees_off[tracer.index(i)])[:,0])
	# on remplit yscale
	for j in data_y_axis:
		yscale[tracer.index(i),data_y_axis.index(j),0]=min(array(donnees_off[tracer.index(i)])[:,data_y_axis.index(j)+1])
		yscale[tracer.index(i),data_y_axis.index(j),1]=max(array(donnees_off[tracer.index(i)])[:,data_y_axis.index(j)+1])

######## TRAÇAGE COURBES ########
for i in tracer:
	#on importe les données des conditions expérimentales dans la matrice donnees_exp
	donnees_exp=import_exp_data(liste_fichiers[i-1], delimiteur, data_set_up, data_set_low, col_freq, col_amp)
	#obtention nom fichier sans chemin absolu (trop long)
	#1: on decoupe le chemin vers le fichier .dat en cours de traitement
	decoupage=liste_fichiers[i-1].split('/')	# decoupage est une liste d'éléments de chemin vers le .dat (chaque partie encadrée par / est un element de la liste)
	# coordonnées en X
	x = array(donnees_off[tracer.index(i)])[:,0]
	for j in data_y_axis:
		# coordonnées en Y
		y = array(donnees_off[tracer.index(i)])[:,data_y_axis.index(j)+1]
		# la suite sert à indiquer s'il y a un ou plusieurs offset sur la courbe en cours de traçage et de nommer les courbes en fonction
		if float(offset[tracer.index(i)+1,1])!=0 or float(offset[tracer.index(i)+1,data_y_axis.index(j)+2])!=0:
			label_courbe='graph '+str(tracer.index(i)+1)+'(col '+str(j)+'): '+decoupage[len(decoupage)-1]+'\n ---> f='+str(donnees_exp[0,0])+'Hz | Vo='+str(donnees_exp[1,0])+unit_amp+' | offset X='+offset[tracer.index(i)+1,1]+' | offset Y'+str(j)+'='+offset[tracer.index(i)+1,data_y_axis.index(j)+2]
		else:
			label_courbe='graph '+str(tracer.index(i)+1)+'(col '+str(j)+'): '+decoupage[len(decoupage)-1]+'\n ---> f='+str(donnees_exp[0,0])+'Hz | Vo='+str(donnees_exp[1,0])+unit_amp

		plt.plot(x,y,label=label_courbe) #on trace la courbe pour i
		print("\t ----traitement accompli sans erreur (courbe", i, "|colonne", j, "tracée)----")

#affiche la matrice des offset dans le terminal pour vérification par l'utilisateur
print('------------------------------------')
if 'oui' in liste_offset or 'Oui' in liste_offset or 'OUI' in liste_offset or 'O' in liste_offset or 'o' in liste_offset:
	if 'oui' in auto_offset or 'Oui' in auto_offset or 'OUI' in auto_offset or 'O' in auto_offset or 'o' in auto_offset:
		print('---> le mode liste des offset est ACTIF et les offset sont:\n', liste_offset)
		print('\n---> le mode auto offset est ACTIF et par conséquent les offset sur Y precedents ne sont par considérés.')
		print('\n---> matrice offset:\n', offset)
		titre = titref+'\n offset: ON | auto_offset: ON'
	else:
		print('---> le mode liste des offset est ACTIF et les offsets sont:\n', liste_offset)
		print('\n---> le mode auto offset est INACTIF.')
		print('\n---> matrice offset:\n', offset)
		titre = titref+'\n offset: ON | auto_offset: OFF'
else:
	if 'oui' in auto_offset or 'Oui' in auto_offset or 'OUI' in auto_offset or 'O' in auto_offset or 'o' in auto_offset:
		print('---> le mode liste des offset est INACTIF. Le mode auto offset est ACTIF.')
		print('\n---> matrice offset:\n', offset)
		titre = titref+'\n offset: OFF | auto_offset: ON'
	else:
		print('---> le mode liste des offset est INACTIF. Le mode auto offset est INACTIF.')
		print('\n---> matrice offset:\n', offset)
		titre = titref+'\n offset: OFF | auto_offset: OFF'

print('------------------------------------')

# on récupère les min et max pour x
xscale_up=max(xscale[:,1])
xscale_low=min(xscale[:,0])
# on récupère les min et max pour y pour toutes les colonnes
# mais pour cela il faut tester pour chaque fichier et colonne
yscale_low=[]
yscale_up=[]
for i in tracer:
	yscale_low.append(min(yscale[tracer.index(i),:,0]))
	yscale_up.append(max(yscale[tracer.index(i),:,1]))

# et on prend le min et max de yscale_low/up
yscale_low=min(yscale_low)
yscale_up=max(yscale_up)

#### ICI

xdelta=(xscale_up-xscale_low)/100
ydelta=(yscale_up-yscale_low)/100
plt.legend(fontsize=8, loc='best')
plt.title(titre)
plt.axis([xscale_low-xdelta, xscale_up+xdelta, yscale_low-ydelta, yscale_up+ydelta])
plt.xlabel(nom_axe_x)
plt.ylabel(nom_axe_y)
if grille=='oui' or grille=='OUI' or grille == 'Oui' or grille=='o' or grille=='O':
	plt.grid(True)
else:
	plt.grid(False)

if print_graph=='oui' or print_graph=='OUI' or print_graph == 'Oui' or print_graph=='o' or print_graph=='O':
#pour permettre de créer un .png par combinaison de courbes tracées et axes représentés
	titreplt="courbe"
	for i in tracer:
		titreplt+=str(i)+"-"
	titreplt+="Xaxis_col" +str(data_x_axis) +"__Yaxis_col"
	for j in data_y_axis:
		titreplt+=+str(j)
	plt.savefig(script_dir+titreplt)

plt.show()


# Chris Joakim, 2021/01/11
# See https://scikit-learn.org/stable/tutorial/basic/tutorial.html#machine-learning-the-problem-setting

# https://scikit-learn.org/stable/modules/classes.html  (API)

import os
import pickle
import sys

from sklearn import datasets
from sklearn import svm

from joblib import dump, load

def digits():
    digits = datasets.load_digits()
    print(digits.data)    # The data is always a 2D array, shape (n_samples, n_features)
    print(digits.target)

    clf = svm.SVC(gamma=0.001, C=100.)  # clf = classifier
    print('clf: {} {}'.format(clf, str(type(clf))))

    # C is the Regularization parameter. The strength of the regularization is inversely proportional to C.
    # Must be strictly positive. The penalty is a squared l2 penalty.

    result = clf.fit(digits.data[:-1], digits.target[:-1])  # fit == "train the model"
    print('fit result: {} {}'.format(str(type(result)), result))

    result = clf.predict(digits.data[-1:])
    print('clf predict result -1: {} {}'.format(str(type(result)), result))

    result = clf.predict(digits.data[-2:])
    print('clf predict result -2: {} {}'.format(str(type(result)), result))

    s = pickle.dumps(clf)
    print('s - type: {} len: {}'.format(str(type(s)), len(s)))

    clf2 = pickle.loads(s)
    print('clf2: {} {}'.format(clf2, str(type(clf2))))

    result = clf2.predict(digits.data[-1:])
    print('clf2 predict result -1: {} {}'.format(str(type(result)), result))

    result = clf2.predict(digits.data[-2:])
    print('clf2 predict result -2: {} {}'.format(str(type(result)), result))

    # use joblib’s replacement for pickle which is more efficient on big data but it can only
    # pickle to the disk and not to a string
    dump(clf, 'tmp/clf.joblib')
    clf3 = load('tmp/clf.joblib')
    result = clf3.predict(digits.data[-1:])
    print('clf3 predict result -1: {} {}'.format(str(type(result)), result))

    result = clf3.predict(digits.data[-2:])
    print('clf3 predict result -2: {} {}'.format(str(type(result)), result))

def iris():
    iris = datasets.load_iris()
    print(iris.data)
    print(iris.target)

if __name__ == "__main__":

    if len(sys.argv) > 1:
        func = sys.argv[1].lower()
        if func == 'digits':
            digits()
        elif func == 'iris':
            iris()


import unittest
import os.path
import pandas as pd
import random
import string
import pytest

from analitico import AnaliticoException
from analitico.factory import Factory
from analitico.schema import generate_schema

from .test_mixin import TestMixin

# pylint: disable=no-member

@pytest.mark.django_db
class ExceptionTests(unittest.TestCase, TestMixin):
    """ Unit testing of AnaliticoException """

    def test_exception_basics(self):
        try:
            raise AnaliticoException("A problem", item_id="it_001", extra={"recipe_id": "rx_002"})
        except AnaliticoException as e:
            self.assertEqual(e.message, "A problem")
            self.assertEqual(e.extra["item_id"], "it_001")
            self.assertEqual(e.extra["recipe_id"], "rx_002")

    def test_exception_formatting(self):
        try:
            raise AnaliticoException("A problem %d", 1, item_id="it_001", extra={"recipe_id": "rx_002"})
        except AnaliticoException as e:
            self.assertEqual(e.message, "A problem 1")
            self.assertEqual(e.extra["item_id"], "it_001")
            self.assertEqual(e.extra["recipe_id"], "rx_002")

    def test_exception_formatting_multiple(self):
        try:
            raise AnaliticoException("A problem %d with %s", 1, "this", item_id="it_001", extra={"recipe_id": "rx_002"})
        except AnaliticoException as e:
            self.assertEqual(e.message, "A problem 1 with this")

    def test_exception_with_inner(self):
        try:
            try:
                raise AnaliticoException("Inner exception %d", 1, item_id="it_001", extra={"recipe_id": "rx_002"})
            except AnaliticoException as e1:
                self.assertEqual(e1.message, "Inner exception 1")
                self.assertEqual(e1.extra["item_id"], "it_001")
                self.assertEqual(e1.extra["recipe_id"], "rx_002")
                raise AnaliticoException("Outer exception %d", 2, test_id="tx001") from e1
        except AnaliticoException as e2:
            self.assertEqual(e2.message, "Outer exception 2")
            print(e2)


from random import randint
import PySimpleGUI as sg

lista = [randint(1, 6) for c in range(2)]

class TelaPython():
    def __init__(self):

        #layout
        layout = [
            [sg.Text(f'Maior valor: {max(lista)}')],
            [sg.Text(f'Menor valor: {min(lista)}')]
        ]
        #janela
        janela = sg.Window('Dados').layout(layout)

        #Extrair dados
        self.values = janela.read()

    def Iniciar(self):
        print(self.values)

a = TelaPython()
a.Iniciar()


"""
Takes a user-inputted rating of 0 to 10 and appends it to a file, along with the date and time
By LTeder
"""
from time import strftime
from sys import argv

def rate(args):
    try:
        directory = args[1]
    except IndexError:
        raise IndexError('Must provide a valid source file')
    print
    def get_rating():
        rating = raw_input("On a scale from 0 to 10, how has this day made you feel? ")
        try:
            if 0 <= int(rating) <= 10:
                f = open(str(directory), 'a')
                f.write('\n%s %s' % (strftime("%Y %m %d"), rating))
                f.close()
            else:
                print("\nPlease input an acceptable value.\n")
                get_rating()
        except ValueError:
            print("\nPlease input an acceptable value.\n")
            get_rating()
    get_rating()

if __name__ == "__main__":
    rate(argv)


import os.path
from pymongo import *
from pprint import pprint
from flask import Flask, render_template, request
from dbcontroller import search

app = Flask(__name__)
app.config.from_object(__name__)

@app.route('/')
def render_static():
	return render_template("index.html");

@app.route('/result', methods=['POST'])
def query():
	client = MongoClient()
	db = client.search;
	query = request.form['query'].lower();
	results = search(query, db)
	if(len(results) > 0):
		results = [link[0] for link in results];
	return render_template("results.html", data = results);

@app.route('/page/<string:webpage>/<int:pageid>/<int:docid>')
def loadpage(webpage, pageid, docid):
	path = webpage+ "/" + str(pageid) + "/" + str(docid);
	with open(path, "r", encoding="utf-8") as file:
		content = file.read()
	return render_template("page.html", content=content, pageTitle = path);

if __name__ == '__main__':
	app.run(debug=True, port=3000);


import matplotlib.pyplot as plt
import numpy as np

data = np.loadtxt('att48_roulette.f')
data = np.transpose(data)

plt.figure(1)
plt.plot(data[0, :], data[1, :], 'r')
plt.plot(data[0, :], data[2, :], 'b')
plt.legend(['Fitness of best solution', 'Average population fitness'])
plt.savefig('plots/att48_fit.png')
plt.figure(2)
plt.loglog(data[0, :], data[1, :], 'r')
plt.loglog(data[0, :], data[2, :], 'b')
plt.legend(['Fitness of best solution', 'Average population fitness'])
plt.savefig('plots/att48_fit_log.png')


'''
#Implement LM algorithm only using basic python
#Author:Leo Ma
#For csmath2019 assignment4,ZheJiang University
#Date:2019.04.28
'''
import numpy as np
import matplotlib.pyplot as plt

# input data, whose shape is (num_data,1)
# data_input=np.array([[0.25, 0.5, 1, 1.5, 2, 3, 4, 6, 8]]).T
# data_output=np.array([[19.21, 18.15, 15.36, 14.10, 12.89, 9.32, 7.45, 5.24, 3.01]]).T

tao = 10 ** -3
threshold_stop = 10 ** -20
threshold_step = 10 ** -20
threshold_residual = 10 ** -15
residual_memory = []

# construct a user function
def my_Func(params, input_data):
    x1 = params[0, 0]
    x3 = params[2, 0]
    x2 = params[1, 0]
    xw, yw = input_data[:, 0], input_data[:, 1]
    out_x =  x1 * xw + x2*yw
    out_y =  x1**2*xw + x3
    out_xy = np.zeros(input_data.shape)
    out_xy[:, 0] = out_x
    out_xy[:, 1] = out_y
    return out_xy

# generating the input_data and output_data,whose shape both is (num_data,1)
def generate_data(params, num_data):
    x = np.array((np.linspace(0, 10, num_data), np.linspace(0, 10, num_data))).reshape(num_data, 2)  # 产生包含噪声的数据
    mid, sigma = 0, 5
    noise = np.random.normal(mid, sigma, num_data *2).reshape(num_data, 2)
    y = my_Func(params, x) + noise
    return x, y

# calculating the derive of pointed parameter,whose shape is (num_data,1)
def cal_deriv(params, input_data, param_index):
    params1 = params.copy()
    params2 = params.copy()
    params1[param_index, 0] += 0.000001
    params2[param_index, 0] -= 0.000001
    data_est_output1 = my_Func(params1, input_data)
    data_est_output2 = my_Func(params2, input_data)
    return (data_est_output1 - data_est_output2) / 0.000002

def deriv_f1_x1(params, input_data):
    xw, yw = input_data[:, 0], input_data[:, 1]
    return xw

def deriv_f1_x2(params, input_data):
    x2 = params[1, 0]
    xw, yw = input_data[:, 0], input_data[:, 1]
    return yw

def deriv_f1_x3(params, input_data):
    xw, yw = input_data[:, 0], input_data[:, 1]
    return np.zeros(xw.shape)

def deriv_f2_x1(params, input_data):
    x1 = params[0, 0]
    xw, yw = input_data[:, 0], input_data[:, 1]
    return 2 * x1 *xw

def deriv_f2_x2(params, input_data):
    xw, yw = input_data[:, 0], input_data[:, 1]
    return np.zeros(xw.shape)

def deriv_f2_x3(params, input_data):
    xw, yw = input_data[:, 0], input_data[:, 1]
    return np.ones(xw.shape)

def myderiv_fun():
    f1_fun_list = []
    f2_fun_list = []
    fun_list = []
    f1_fun_list.append(deriv_f1_x1)
    f1_fun_list.append(deriv_f1_x2)
    f1_fun_list.append(deriv_f1_x3)

    f2_fun_list.append(deriv_f2_x1)
    f2_fun_list.append(deriv_f2_x2)
    f2_fun_list.append(deriv_f2_x3)

    fun_list.append(f1_fun_list)
    fun_list.append(f2_fun_list)
    return fun_list

def deriv_function(params, input_data, param_index, fun_index):
    all_deriv_fun = myderiv_fun()

    params1 = params.copy()
    data_est_output = all_deriv_fun[fun_index][param_index](params1, input_data)
    return data_est_output

# calculating jacobian matrix,whose shape is (num_data,num_params)
def cal_Jacobian(params, input_data):
    num_params = np.shape(params)[0]
    num_data = np.shape(input_data)[0]
    J = np.zeros((num_data, num_params))
    residual_u_x, residual_v_y = cal_residual_u_x_v_y(params, input_data)
    for i in range(num_params):
        deriv_for_param_i = residual_u_x * deriv_function(params, input_data, i, 0) + residual_v_y * deriv_function(params, input_data, i, 1)
        J[:, i] = list(deriv_for_param_i)
    return J

# calculating residual, whose shape is (num_data,1)
def cal_residual(params, input_data, output_data):
    data_est_output = my_Func(params, input_data)
    residual = np.linalg.norm(output_data - data_est_output, axis=1).reshape(input_data.shape[0], 1)
    return residual

def cal_residual_u_x_v_y(params, input_data):
    data_est_output = my_Func(params, input_data)
    u_estimation = data_est_output[:, 0]
    v_estimation = data_est_output[:, 1]
    real_u = input_data[:, 0]
    real_v = input_data[:, 1]
    residual_u_x = u_estimation - real_u
    residual_v_y = v_estimation - real_v
    return residual_u_x, residual_v_y

'''
#calculating Hessian matrix, whose shape is (num_params,num_params)
def cal_Hessian_LM(Jacobian,u,num_params):
    H = Jacobian.T.dot(Jacobian) + u*np.eye(num_params)
    return H

#calculating g, whose shape is (num_params,1)
def cal_g(Jacobian,residual):
    g = Jacobian.T.dot(residual)
    return g

#calculating s,whose shape is (num_params,1)
def cal_step(Hessian_LM,g):
    s = Hessian_LM.I.dot(g)
    return s

'''

# get the init u, using equation u=tao*max(Aii)
def get_init_u(A, tao):
    m = np.shape(A)[0]
    Aii = []
    for i in range(0, m):
        Aii.append(A[i, i])
    u = tao * max(Aii)
    return u

# LM algorithm
def LM(num_iter, params, input_data, output_data):
    num_params = np.shape(params)[0]  # the number of params
    k = 0  # set the init iter count is 0
    # calculating the init residual
    residual = cal_residual(params, input_data, output_data)
    # calculating the init Jocobian matrix
    Jacobian = cal_Jacobian(params, input_data)

    A = Jacobian.T.dot(Jacobian)  # calculating the init A
    g = Jacobian.T.dot(residual)  # calculating the init gradient g
    stop = (np.linalg.norm(g, ord=np.inf) <= threshold_stop)  # set the init stop
    u = get_init_u(A, tao)  # set the init u
    v = 2  # set the init v=2

    while ((not stop) and (k < num_iter)):
        k += 1
        while (1):
            Hessian_LM = A + u * np.eye(num_params)  # calculating Hessian matrix in LM
            step = np.linalg.inv(Hessian_LM).dot(g)  # calculating the update step
            if (np.linalg.norm(step) <= threshold_step):
                stop = True
            else:
                new_params = params + step  # update params using step
                new_residual = cal_residual(new_params, input_data, output_data)  # get new residual using new params
                rou = (np.linalg.norm(residual) ** 2 - np.linalg.norm(new_residual) ** 2) / (step.T.dot(u * step + g))
                if rou > 0:
                    params = new_params
                    residual = new_residual
                    residual_memory.append(np.linalg.norm(residual) ** 2)
                    # print (np.linalg.norm(new_residual)**2)
                    Jacobian = cal_Jacobian(params, input_data)  # recalculating Jacobian matrix with new params
                    A = Jacobian.T.dot(Jacobian)  # recalculating A
                    g = Jacobian.T.dot(residual)  # recalculating gradient g
                    stop = (np.linalg.norm(g, ord=np.inf) <= threshold_stop) or (
                                np.linalg.norm(residual) ** 2 <= threshold_residual)
                    u = u * max(1 / 3, 1 - (2 * rou - 1) ** 3)
                    v = 2
                else:
                    u = u * v
                    v = 2 * v
            if (stop or rou > 0):
                break;

    return params

def main():
    # set the true params for generate_data() function
    params = np.zeros((3, 1))
    params[0, 0] = 5.0
    params[1, 0] = 2
    params[2, 0] = 4.0

    num_data = 200  # set the data number
    data_input, data_output = generate_data(params, num_data)  # generate data as requested

    # set the init params for LM algorithm
    params[0, 0] = 0.0
    params[1, 0] = 2.0
    params[2, 0] = 0.0

    # using LM algorithm estimate params
    num_iter = 100  # the number of iteration
    est_params = LM(num_iter, params, data_input, data_output)
    print(est_params)
    a_est = est_params[0, 0]
    b_est = est_params[1, 0]
    c_est = est_params[2, 0]
    print(a_est, b_est, c_est)
    # 老子画个图看看状况
    estimation = my_Func(est_params, data_input)
    plt.scatter(estimation[:, 0], estimation[:, 1], color='b')
    plt.scatter(data_output[:, 0], data_output[:, 1], color='r')
    x = np.arange(0, 100) * 0.1  # 生成0-10的共100个数据，然后设置间距为0.1
    #plt.plot(x, a_est * np.exp(b_est * x), 'r', lw=1.0)
    plt.xlabel("2018.06.13")
    plt.savefig("result_LM.png")
    plt.show()

    plt.plot(residual_memory)
    plt.xlabel("2018.06.13")
    plt.savefig("error-iter.png")
    plt.show()

if __name__ == '__main__':
    main()


class charactercount:
    mystr = ""
    def isstring(self,data):
        if(type(data) == str ):
            self.mystr=data
        else:
            print("Invalid Input")
    def charCount(self):
        return len(self.mystr)

dd = charactercount()
dd.isstring(data="hello world")
print(dd.charCount())


class Solution:
    def pathSum(self, root, target):
        def dfs(node, cur_totals):
            if not node:
                return 0
            res = sum(target == total + node.val for total in cur_totals)
            next_totals = [total + node.val for total in cur_totals] + [0]
            return res + dfs(node.left, next_totals) + dfs(node.right, next_totals)

        return dfs(root, [0])

# recursive
class Solution:
    def pathSum(self, root, target):
        def recur(node, accs):
            if not node:
                return 0
            res = sum([acc == node.val for acc in accs])
            next_accs = [acc - node.val for acc in accs] + [target]
            return res + recur(node.left, next_accs) + recur(node.right, next_accs)
        return recur(root, [target])

# DFS
class Solution:
    def pathSum(self, root, sum):
        total = 0
        stack = [(root, {})] if root else []

        while stack:
            node, vn_map = stack.pop()
            new_map = collections.defaultdict(int, {node.val: 1})

            if node.val == sum:
                total += 1

            for v, n in vn_map.items():
                nsum = v + node.val
                if nsum == sum:
                    total += n
                new_map[nsum] += n

            if node.left:
                stack.append((node.left, new_map))
            if node.right:
                stack.append((node.right, new_map))

        return total