from constants import pickle_path
from gensim import models, corpora
from util import *
import pickle

class Books(object):
    def __init__(self):
        self.book_name = ""
        self.dict = None #loaded dict should be attached here
        self.input_type = "" #url # text file
        self.input_link = None
        self.input_file_path = None
        self.steps = [self.preprocess,
                      self.baseline,
                      self.log_baseline,
                      self.topic_modelling,
                      ]
        self.bias = 0.000001
        self.num_topics = 26

    def fetch_data(self):
        """
        :return: returns a dict by chapters implemented by each book
        """
        return {}

    def get_dict(self):
        if self.dict:
            return self.dict
        f = open(pickle_path+self.book_name+".pickle","rb")
        self.dict = pickle.load(f)
        f.close()
        return self.dict

    def preprocess(self):
        output_dict = self.fetch_data()
        filtered_dict = dict()
        for k,v in output_dict.items():
            filtered_dict[k] = clean_data(v)
        chapter_labels=[k for k in sorted(output_dict.keys())]
        final_dict = dict()
        final_dict['raw_text'] = output_dict
        final_dict['filtered_text'] = filtered_dict
        final_dict['chapter_labels'] = chapter_labels
        self.dict =final_dict
        of = open(pickle_path+self.book_name+".pickle","wb")
        pickle.dump(final_dict, of)

    def baseline(self):
        if not self.dict:
            self.get_dict()
        filtered_text=dict()
        for k,v in self.dict['raw_text'].items():
            filtered_text[k] = clean_data(v)
        self.dict['filtered_text'] = filtered_text
        #bag of words by chapter
        bag_of_words = get_bag_of_words_by_chapter(self.dict['filtered_text'])
        self.dict["bag_of_words"] = bag_of_words
        total_counter = sum([v for v in bag_of_words.values()])
        DTM, unique_words = get_DTM_from_counter(total_counter,dicts=[bag_of_words], return_words=True)
        self.dict["DTM"] = DTM
        row_labels=["ch{}".format(k) for k in self.dict['chapter_labels']]
        headers = sorted(unique_words, key=unique_words.get)
        self.dict["unique_words"] = headers
        save_to_csv('{}_baseline_DTM.csv'.format(self.book_name),DTM, headers)
        execute_similatity_matrix(DTM,  type=self.book_name, label="baseline", col_row_labels=row_labels)
        of = open(pickle_path+self.book_name+".pickle","wb")
        pickle.dump(self.dict, of)

    def log_baseline(self):
        if not self.dict:
            self.get_dict()

        DTM = self.dict["DTM"]
        row_labels=["ch{}".format(k) for k in self.dict['chapter_labels']]
        headers = self.dict["unique_words"]
        DTM = np.array(DTM) + self.bias
        DTM = np.log(DTM)
        DTM[DTM<0] = 0.0
        save_to_csv('{}_baseline_log_DTM.csv'.format(self.book_name),DTM, headers)
        self.dict["Log_DTM"] = DTM
        execute_similatity_matrix(DTM,  type=self.book_name, label="baseline_log", col_row_labels=row_labels)
        of = open(pickle_path+self.book_name+".pickle","wb")
        pickle.dump(self.dict, of)

    def topic_modelling(self):
        if not self.dict:
            self.get_dict()
        texts = self.dict["filtered_text"]
        documents = [texts[k] for k in sorted(texts.keys())]
        all_tokens = []
        for tokens in documents:
            all_tokens.extend(tokens)
        all_tokens = Counter(all_tokens)
        unique_tokens = set(filter(lambda x: all_tokens[x] ==1,all_tokens))
        documents = [[word for word in doc if word not in unique_tokens] for doc in documents]
        # Create Dictionary.
        id2word = corpora.Dictionary(documents)
        # Creates the Bag of Word corpus.
        mm = [id2word.doc2bow(text) for text in documents]
        lda = models.ldamodel.LdaModel(corpus=mm, id2word=id2word, num_topics=self.num_topics, \
                               update_every=1, chunksize=10000, passes=1)
        lda_corpus = lda[mm]
        lda_DTM = [[0]*self.num_topics for i in range(len(texts))]
        for doc_num, doc in enumerate(lda_corpus):
            for topic in doc:
                topic_id,score=topic
                lda_DTM[doc_num][topic_id] = score
        topics = lda.print_topics()
        topic_names = ["Topic{}".format(topic[0]) for topic in topics]
        self.dict["topics"] = topics
        self.dict["topic_names"] = topic_names
        self.dict["LDA_DTM"] = lda_DTM
        row_labels=["ch{}".format(k) for k in self.dict['chapter_labels']]
        save_to_csv('{}_lda_DTM.csv'.format(self.book_name),lda_DTM, topic_names)
        execute_similatity_matrix(lda_DTM,  type=self.book_name, label="lda", col_row_labels=row_labels)
        of = open(pickle_path+self.book_name+".pickle","wb")
        pickle.dump(self.dict, of)


#!/usr/bin/env python3

import time
from argparse import ArgumentParser

from alsa_midi import READ_PORT, Address, MidiBytesEvent, SequencerClient

def make_events():
    # 3 ticks per quarternote, swung
    return [
            # D-on
            MidiBytesEvent(b"\x90\x3e\x7f", tick=0),

            # D-off, E-on
            MidiBytesEvent(b"\x80\x3e\x00\x90\x40\x7f", tick=2),

            # E-off, F-on
            MidiBytesEvent(b"\x80\x40\x00\x90\x41\x7f", tick=3),

            # F-off, G-on
            MidiBytesEvent(b"\x80\x41\x00\x90\x43\x7f", tick=5),

            # G-off, E-on
            MidiBytesEvent(b"\x80\x43\x00\x90\x40\x7f", tick=6),

            # E-off, C-on
            MidiBytesEvent(b"\x80\x40\x00\x90\x3c\x7f", tick=9),

            # C-off, D-on
            MidiBytesEvent(b"\x80\x3c\x00\x90\x3e\x7f", tick=11),

            # D-off
            MidiBytesEvent(b"\x80\x3e\x00", tick=24),
    ]

def main():
    parser = ArgumentParser(description="Play the lick (by sending bytes)")
    parser.add_argument("--port", "-p", type=Address, action="append",
                        help="Connect to the specific port(s)")
    parser.add_argument("--bpm", type=float, default=120.0,
                        help="Tempo in beats per minute (default: 120)")

    args = parser.parse_args()

    client = SequencerClient("play_the_lick.py")
    port = client.create_port("output", READ_PORT)
    queue = client.create_queue()

    queue.set_tempo(int(60.0 * 1000000 / args.bpm), 3)

    if args.port:
        targets = args.port
    else:
        target = client.list_ports(output=True)[0]
        print(f"Playing on {target.client_id}:{target.port_id}"
              f" '{target.name}' of '{target.client_name}'")
        targets = [target]

    for target in targets:
        port.connect_to(target)

    queue.start()
    try:
        for event in make_events():
            client.event_output(event, port=port, queue=queue)
        client.drain_output()
    except KeyboardInterrupt:
        pass

    time.sleep(60.0 * 8 / args.bpm)

    client.close()

if __name__ == '__main__':
    main()


from multiprocess import Pool
import numpy as np
from s3_create import upload_files_s3, get_key_secret, create_bucket, get_data_files, get_bucket

def get_process_number(category_list):
    if len(category_list)%30000 == 0:
        additional_process = 0
    else:
        additional_process = 1
    return int(np.floor(len(category_list)/30000) + additional_process)

def get_parallel_process_list(pool, number_of_processes, category_list):
    parallel_processes = []
    index = 0
    factor = 30000
    for i in range(1, number_of_processes+1):
        last_index = i*factor
        if len(category_list) - last_index < 30000:
            last_index = len(category_list)%30000
        parallel_processes.append(
            pool.apply_async(upload_files_s3, [category_list[index: last_index],
                                               'capstone-project-2187',
                                               s3_bucket])
        )
        index += last_index
    return parallel_processes

def finalize_processes(list_of_processes):
    for process in list_of_processes:
        process.get()

def execute_parallel_uploads(plant_files_list, animal_files_list, human_files_list):
    num_processes = get_process_number(plant_files_list) + get_process_number(animal_files_list) + get_process_number(human_files_list)
    print(num_processes)
    pool = Pool(processes= num_processes)

    plants = get_parallel_process_list(pool,
                                       get_process_number(plant_files_list),
                                       plant_files_list)

    animals = get_parallel_process_list(pool,
                                       get_process_number(animal_files_list),
                                       animal_files_list)

    humans = get_parallel_process_list(pool,
                                       get_process_number(human_files_list),
                                       human_files_list)

    pool.close()
    pool.join()

    finalize_processes(plants)
    finalize_processes(animals)
    finalize_processes(humans)

if __name__ == '__main__':

    KEY, SECRET = get_key_secret()

    main_directory = "../../capstone data"
    plant_files_list, animal_files_list, human_files_list = get_data_files(main_directory)

    s3_client = create_bucket('capstone-project-2187',
                              KEY,
                              SECRET,
                              'us-west-2')

    s3_resource = get_bucket('capstone-project-2187',
                                  KEY,
                                  SECRET,
                                  'us-west-2')

    s3_bucket = s3_resource.Bucket('capstone-project-2187')

    execute_parallel_uploads(plant_files_list,
                             animal_files_list,
                             human_files_list)


"""
Module contains classes for calculating metrics and logging them to tensorboard,
plus related util functions.
"""
import os
from collections import defaultdict

import numpy as np
import torch
from torch.utils.tensorboard.writer import SummaryWriter

from .general_utils import get_logger

class Evaluation:
    """
    Base class for calculating model metrics and logging them to tensorboard.
    """

    def __init__(self, log_folder: str):
        self.logger = get_logger(
            "evaluation", os.path.join(log_folder, "evaluation.txt")
        )
        self.train_results, self.val_results = [], []
        self.dev_results, self.test_results = [], []
        self._epoch_reset()

    def set_fold(self, _):
        """
        Prepare evaluation for new cv fold.
        """
        self.train_results.append(defaultdict(list))
        self.val_results.append(defaultdict(list))
        self.dev_results.append(defaultdict(list))
        self.test_results.append(defaultdict(list))

    def evaluate(self, predicted, labels):
        raise NotImplementedError("Override in custom evaluation class.")

    def log_evaluation(self, epoch, dataset: str, writer: SummaryWriter):
        """
        Calculates metrics from running totals. Saves and logs them to tensorboard.
        """
        raise NotImplementedError("Override in custom evaluation class.")

    def _get_results(self, dataset: str, index=-1):
        if dataset == "train":
            return self.train_results[index]
        elif dataset == "val":
            return self.val_results[index]
        elif dataset == "test":
            return self.test_results[index]
        elif dataset == "dev":
            return self.dev_results[index]
        else:
            raise ValueError(
                f"Incorrect `dataset` value, got {dataset}, accept: train, test, dev, val."
            )

    def _epoch_reset(self):
        raise NotImplementedError("Override in custom evaluation class.")

    def get_experiment_results(self, dataset: str):
        """
        Get mean and std of results.
        """
        results = self._get_results(dataset, index=slice(None))
        return aggregate_results(results)

def aggregate_results(results):
    "Averages over list of dictionaries with results."
    agg_results = {}
    for k in results[0].keys():
        val_list = [res[k] for res in results]
        # Take mean and standard deviation across runs.
        agg_results[k] = (
            np.mean(val_list, axis=0),
            np.std(val_list, axis=0),
        )

    return agg_results

class BinaryClassificationEvaluation(Evaluation):
    """
    Class for calculating binary classification metrics and logging them to tensorboard.

    Metrics: accuracy, precision, recall.
    """

    def evaluate(self, predicted, labels):
        """
        Calculates confusion matrix between predicted and true labels for single batch.
        Saves running totals.
        """
        pred_positives = predicted > 0.5
        label_positives = labels > 0.5

        self.tp += (pred_positives & label_positives).sum().item()
        self.tn += (~pred_positives & ~label_positives).sum().item()
        self.fp += (pred_positives & ~label_positives).sum().item()
        self.fn += (~pred_positives & label_positives).sum().item()

        self.total += len(labels)

    def log_evaluation(self, epoch, dataset: str, writer: SummaryWriter):
        # Calculate.
        accuracy = (self.tp + self.tn) / self.total
        recall = self.tp / (self.tp + self.fp) if (self.tp + self.fp) > 0 else 0
        precision = self.tp / (self.tp + self.fn)

        # Log.
        writer.add_scalar(f"{dataset} accuracy", accuracy, epoch)
        writer.add_scalar(f"{dataset} precision", recall, epoch)
        writer.add_scalar(f"{dataset} recall", precision, epoch)
        self.logger.debug(f"Epoch {epoch}: {dataset} accuracy = {accuracy}")
        self.logger.debug(f"Epoch {epoch}: {dataset} precision = {precision}")
        self.logger.debug(f"Epoch {epoch}: {dataset} recall = {recall}")

        # Save.
        results = self._get_results(dataset)
        results["accuracy"].append(accuracy)
        results["epoch"].append(epoch)
        results["recall"].append(recall)
        results["precision"].append(precision)

        # Reset.
        self._epoch_reset()

    def _epoch_reset(self):
        self.tp = 0
        self.tn = 0
        self.fp = 0
        self.fn = 0
        self.total = 0

class RegressionEvaluation(Evaluation):
    """
    Class for calculating regression metrics and logging them to tensorboard.

    Metrics: root mean square error, mean absolute error.
    """

    def evaluate(self, predicted, labels):
        """
        Calculates diff between predicted and true labels for single batch.
        Saves running totals.
        """
        self.error_squared += torch.sum((predicted - labels) ** 2).item()
        self.error_abs += torch.sum(torch.abs(predicted - labels)).item()
        self.total += len(labels)

    def log_evaluation(self, epoch, dataset: str, writer: SummaryWriter):
        # Calculate.
        rmse = (self.error_squared / self.total) ** 0.5
        mae = self.error_abs / self.total

        # Log.
        writer.add_scalar(f"{dataset} rmse", rmse, epoch)
        writer.add_scalar(f"{dataset} mae", mae, epoch)
        self.logger.debug(f"Epoch {epoch}: {dataset} rmse = {rmse}")
        self.logger.debug(f"Epoch {epoch}: {dataset} mae = {mae}")

        # Save.
        results = self._get_results(dataset)
        results["rmse"].append(rmse)
        results["mae"].append(mae)

        # Reset.
        self._epoch_reset()

    def _epoch_reset(self):
        self.error_squared = 0
        self.error_abs = 0
        self.total = 0


# See also:
# https://click.palletsprojects.com/en/7.x/setuptools/#setuptools-integration
# https://click.palletsprojects.com/en/7.x/arguments/

import sys
from typing import Tuple

import click
from click_default_group import DefaultGroup

from dotenv_linter.checker import DotenvFileChecker
from dotenv_linter.version import pkg_version

@click.group(
    cls=DefaultGroup,
    default='lint',
    default_if_no_args=True,
    invoke_without_command=True,
)
@click.option('--version', is_flag=True, default=False)
def cli(version):
    """
    Main entrypoint to the app.

    Runs ``lint`` command by default if nothing else is not specified.
    Runs ``--version`` subcommand if this option is provided.
    """
    if version:
        print(pkg_version)  # noqa: WPS421

@cli.command()
@click.argument(
    'files',
    nargs=-1,
    required=True,
    type=click.Path(exists=True, dir_okay=False),
)
def lint(files: Tuple[str, ...]):
    """Runs linting process for the given files."""
    checker = DotenvFileChecker(files)

    try:
        checker.run()
    except Exception as ex:
        print(ex, file=sys.stderr)  # noqa: WPS421
        checker.fail()


#!/usr/bin/env python

import os
import re

import wx
import wx.animate
import wx.lib.delayedresult
import wx.lib.scrolledpanel as scrolled

from aqopa.gui.general_purpose_frame_gui import GeneralFrame
from aqopa.model import name_indexes

"""
@file       gui.py
@brief      GUI for the reputation analysis panel
@author     Damian Rusinek <damian.rusinek@gmail.com>
@date       created on 06-09-2013 by Damian Rusinek
@date       edited on 27-06-2014 by Katarzyna Mazur (visual improvements mainly)
"""

class SingleVersionPanel(wx.Panel):
    """
    Frame presenting results for one simulation.
    Simulator may be retrieved from module,
    because each module has its own simulator.
    """

    def __init__(self, module, *args, **kwargs):
        wx.Panel.__init__(self, *args, **kwargs)

        self.module = module
        self.versionSimulator = {}

        self.hostChoosePanels       = []  # Panels used to choose hosts for energy consumptions results
        self.checkBoxInformations   = []  # Tuples with host name, and index ranges widget
        self.hostCheckBoxes         = []  # List of checkboxes with hosts names used for hosts' selection

        #################
        # VERSION BOX
        #################

        versionBox = wx.StaticBox(self, label="Version")
        versionsLabel = wx.StaticText(self, label="Choose Version To See\nAnalysis Results:")
        self.versionsList = wx.ComboBox(self, style=wx.TE_READONLY)
        self.versionsList.Bind(wx.EVT_COMBOBOX, self.OnVersionChanged)
        versionBoxSizer = wx.StaticBoxSizer(versionBox, wx.HORIZONTAL)
        versionBoxSizer.Add(versionsLabel, 0, wx.ALL | wx.ALIGN_CENTER, 5)
        versionBoxSizer.Add(wx.StaticText(self), 1, wx.ALL | wx.EXPAND, 5)
        versionBoxSizer.Add(self.versionsList, 1, wx.ALL | wx.ALIGN_CENTER, 5)

        ##################################
        # ENERGY CONSUMPTION BOX
        ##################################

        self.consumptionsBox = wx.StaticBox(self, label="Reputation results")

        hostsBox, hostsBoxSizer = self._BuildHostsBoxAndSizer()

        reputationBoxSizer = wx.StaticBoxSizer(self.consumptionsBox, wx.VERTICAL)
        reputationHBoxSizer = wx.BoxSizer(wx.HORIZONTAL)
        reputationHBoxSizer.Add(hostsBoxSizer, 1, wx.ALL | wx.EXPAND)

        self.showReputationBtn = wx.Button(self, label="Show")
        self.showReputationBtn.Bind(wx.EVT_BUTTON, self.OnShowReputationButtonClicked)

        reputationBoxSizer.Add(reputationHBoxSizer, 0, wx.ALL | wx.EXPAND)

        buttonsBoxSizer = wx.BoxSizer(wx.HORIZONTAL)
        buttonsBoxSizer.Add(wx.StaticText(self), 1, wx.EXPAND, 5)
        buttonsBoxSizer.Add(self.showReputationBtn, 0, wx.ALL | wx.ALIGN_RIGHT)

        reputationBoxSizer.Add(buttonsBoxSizer, 1, wx.ALL | wx.EXPAND, 5)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(versionBoxSizer, 0, wx.ALL | wx.EXPAND, 5)
        sizer.Add(reputationBoxSizer, 1, wx.ALL | wx.EXPAND, 5)
        self.SetSizer(sizer)

        self.SetVersionsResultsVisibility(False)

    #################
    # REACTIONS
    #################

    def AddFinishedSimulation(self, simulator):
        """ """
        version = simulator.context.version
        self.versionsList.Append(version.name)

        self.versionSimulator[version.name] = simulator

    def OnVersionChanged(self, event):
        """ """
        versionName = self.versionsList.GetValue()
        simulator = self.versionSimulator[versionName]

        self._BuildHostsChoosePanel(simulator)
        self.SetVersionsResultsVisibility(True)

    def OnShowReputationButtonClicked(self, event):
        """ """
        versionName = self.versionsList.GetValue()
        simulator = self.versionSimulator[versionName]
        hosts = self._GetSelectedHosts(simulator)

        if len(hosts) == 0:
            wx.MessageBox("Please select hosts.", 'Error', wx.OK | wx.ICON_ERROR)
            return

        self.ShowHostsReputation(simulator, hosts)

    def RemoveAllSimulations(self):
        """ """
        self.versionsList.Clear()
        self.versionsList.SetValue("")
        self.versionSimulator = {}

        self.hostChoosePanels = []
        self.checkBoxInformations = {}
        self.hostCheckBoxes = []
        self.hostsBoxSizer.Clear(True)

        self.SetVersionsResultsVisibility(False)

    #################
    # LAYOUT
    #################

    def _BuildHostsBoxAndSizer(self):
        """ """
        self.hostsBox = wx.StaticBox(self, label="Host(s)")
        self.hostsBoxSizer = wx.StaticBoxSizer(self.hostsBox, wx.VERTICAL)
        return self.hostsBox, self.hostsBoxSizer

    def _BuildHostsChoosePanel(self, simulator):
        """ """
        for p in self.hostChoosePanels:
            p.Destroy()
        self.hostChoosePanels = []
        self.checkBoxInformations = {}
        self.hostCheckBoxes = []

        self.hostsBoxSizer.Layout()

        hosts = simulator.context.hosts
        hostsIndexes = {}
        for h in hosts:
            name = h.original_name()
            indexes = name_indexes(h.name)
            index = indexes[0]

            if name not in hostsIndexes or index > hostsIndexes[name]:
                hostsIndexes[name] = index

        for hostName in hostsIndexes:

            panel = wx.Panel(self)
            panelSizer = wx.BoxSizer(wx.HORIZONTAL)

            ch = wx.CheckBox(panel, label=hostName, size=(120, 20))
            textCtrl = wx.TextCtrl(panel, size=(200, 20))
            textCtrl.SetValue("0")

            rangeLabel = "Available range: 0"
            if hostsIndexes[hostName] > 0:
                rangeLabel += " - %d" % hostsIndexes[hostName]
            maxLbl = wx.StaticText(panel, label=rangeLabel)

            panelSizer.Add(ch, 0, wx.ALL | wx.ALIGN_CENTER)
            panelSizer.Add(wx.StaticText(self), 1, wx.ALL | wx.ALIGN_RIGHT)
            panelSizer.Add(textCtrl, 0, wx.ALL | wx.ALIGN_CENTER)
            panelSizer.Add(maxLbl, 0, wx.ALL | wx.ALIGN_CENTER)
            panel.SetSizer(panelSizer)
            self.hostsBoxSizer.Add(panel, 1, wx.ALL)

            self.checkBoxInformations[ch] = (hostName, textCtrl)
            self.hostChoosePanels.append(panel)
            self.hostCheckBoxes.append(ch)

        self.hostsBoxSizer.Layout()
        self.Layout()

    def SetVersionsResultsVisibility(self, visible):
        """ """
        widgets = [self.consumptionsBox, self.hostsBox, self.showReputationBtn]

        for w in widgets:
            if visible:
                w.Show()
            else:
                w.Hide()

        self.Layout()

    #################
    # STATISTICS
    #################

    def _GetSelectedHosts(self, simulator):
        """ Returns list of hosts selected by user """

        def ValidateHostsRange(indexesRange):
            """ """
            return re.match(r'\d(-\d)?(,\d(-\d)?)*', indexesRange)

        def GetIndexesFromRange(indexesRange):
            """ Extracts numbers list of hosts from range text """
            indexes = []
            ranges = indexesRange.split(',')
            for r in ranges:
                parts = r.split('-')
                if len(parts) == 1:
                    indexes.append(int(parts[0]))
                else:
                    for i in range(int(parts[0]), int(parts[1])+1):
                        indexes.append(i)
            return indexes

        hosts = []
        for ch in self.hostCheckBoxes:
            if not ch.IsChecked():
                continue
            hostName, hostRangeTextCtrl = self.checkBoxInformations[ch]
            indexesRange = hostRangeTextCtrl.GetValue()
            if not ValidateHostsRange(indexesRange):
                wx.MessageBox("Range '%s' for host '%s' is invalid. Valid example: 0,12,20-25,30."
                              % (indexesRange, hostName), 'Error', wx.OK | wx.ICON_ERROR)
                break
            else:
                indexes = GetIndexesFromRange(indexesRange)
                for h in simulator.context.hosts:
                    hostIndexes = name_indexes(h.name)
                    if h.original_name() == hostName and hostIndexes[0] in indexes:
                        hosts.append(h)
        return hosts

    def ShowHostsReputation(self, simulator, hosts):
        """ """
        lblText = ""

        for h in hosts:
            lblText += "%s: " % (h.name,)
            error = h.get_finish_error()
            if error is not None:
                lblText += " (Not Finished - %s)" % error

            host_vars = self.module.get_host_vars(h)
            for var_name in host_vars:
                lblText = "%s: %s" % (var_name, unicode(host_vars[var_name]))
            lblText += "\n\n"

        # create a new frame to show time analysis results on it
        hostsReputationWindow = GeneralFrame(self, "Reputation Analysis Results", "Host's Reputation", "modules_results.png")

        # create scrollable panel
        hostsPanel = scrolled.ScrolledPanel(hostsReputationWindow)

        # create informational label
        lbl = wx.StaticText(hostsPanel, label=lblText)

        # sizer to align gui elements properly
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(lbl, 0, wx.ALL | wx.EXPAND, 5)
        hostsPanel.SetSizer(sizer)
        hostsPanel.SetupScrolling(scroll_x=True)
        hostsPanel.Layout()

        # add panel on a window
        hostsReputationWindow.AddPanel(hostsPanel)
        # center window on a screen
        hostsReputationWindow.CentreOnScreen()
        # show the results on the new window
        hostsReputationWindow.Show()

class MainResultsNotebook(wx.Notebook):
    """ """
    def __init__(self, module, *args, **kwargs):
        wx.Notebook.__init__(self, *args, **kwargs)

        il = wx.ImageList(24, 24)
        singleVersionImg = il.Add(wx.Bitmap(self.CreatePath4Resource('reputation.png'), wx.BITMAP_TYPE_PNG))
        self.AssignImageList(il)

        self.module = module

        self.oneVersionTab = SingleVersionPanel(self.module, self)
        self.AddPage(self.oneVersionTab, "Single Version")
        self.SetPageImage(0, singleVersionImg)
        self.oneVersionTab.Layout()

    def OnParsedModel(self):
        """ """
        self.oneVersionTab.RemoveAllSimulations()

    def OnSimulationFinished(self, simulator):
        """ """
        self.oneVersionTab.AddFinishedSimulation(simulator)

    def OnAllSimulationsFinished(self, simulators):
        """ """
        pass

    def CreatePath4Resource(self, resourceName):
        """
        @brief      creates and returns path to the
                    given file in the resource
                    ('assets') dir
        @return     path to the resource
        """
        tmp = os.path.split(os.path.dirname(__file__))
        # find last / character in path
        idx = tmp[0].rfind('/')
        # get substring - path for resource
        path = tmp[0][0:idx]
        return os.path.join(path, 'bin', 'assets', resourceName)

class ModuleGui(wx.EvtHandler):
    """
    Class used by GUI version of AQoPA.
    """

    def __init__(self, module):
        """ """
        wx.EvtHandler.__init__(self)

        self.module = module
        self.mainResultNotebook = None

    def get_name(self):
        return "Reputation"

    def get_configuration_panel(self, parent):
        """ Returns WX panel with configuration controls. """

        panel = wx.Panel(parent)

        sizer = wx.BoxSizer(wx.VERTICAL)
        text = wx.StaticText(panel, label="Module does not need to be configured.")
        sizer.Add(text, 0, wx.ALL | wx.EXPAND, 5)
        text = wx.StaticText(panel, label="All result options will be available after results are calculated.")
        sizer.Add(text, 0, wx.ALL | wx.EXPAND, 5)

        panel.SetSizer(sizer)
        return panel

    def get_results_panel(self, parent):
        """
        Create main result panel existing from the beginning
        which will be extended when versions' simulations are finished.
        """
        self.mainResultNotebook = MainResultsNotebook(self.module, parent)
        return self.mainResultNotebook

    def on_finished_simulation(self, simulator):
        """ """
        self.mainResultNotebook.OnSimulationFinished(simulator)

    def on_finished_all_simulations(self, simulators):
        """
        Called once for all simulations after all of them are finished.
        """
        self.mainResultNotebook.OnAllSimulationsFinished(simulators)

    def on_parsed_model(self):
        """ """
        self.mainResultNotebook.OnParsedModel()


import pandas
import ast
import time # if required
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import CountVectorizer

movies = pandas.read_csv('tmbd_5000_movies.csv')    #Loading The CSV files
creditss = pandas.read_csv('tmbd_5000_credits.csv')

cv = CountVectorizer(max_features=5000, stop_words='english')

movies = movies.merge(creditss,on='title')

def convert(obj):
    L = []
    for i in ast.literal_eval(obj):
        L.append(i['name'])
    return L

def convert3(obj):
    L = []
    counter = 0
    for i in ast.literal_eval(obj):
        if counter != 3:
            L.append(i['name'])
            counter+=1
        else:
            break
    return L

def fetch_director(obj):
    L = []
    for i in ast.literal_eval(obj):
        if i['job'] == 'Director':
            L.append(i['name'])
            break
    return L

movies['genres'] = movies['genres'].apply(convert)
movies['keywords'] = movies['keywords'].apply(convert)
movies['cast'] = movies['cast'].apply(convert3)
movies['crew'] = movies['crew'].apply(fetch_director)
movies['tags'] = movies['genres'] + movies['cast'] + movies['crew']
movies['genres'] = movies['genres'].apply(lambda r: [i.replace(" ", "") for i in r])
movies['keywords'] = movies['keywords'].apply(lambda r: [i.replace(" ", "") for i in r])
movies['cast'] = movies['cast'].apply(lambda r: [i.replace(" ", "") for i in r])
movies['crew'] = movies['crew'].apply(lambda r: [i.replace(" ", "") for i in r])
movies['tags'] = movies['tags'].apply(lambda x: " ".join(x))

df = movies[['movie_id', 'title', 'tags']] #needed Data

vectors = cv.fit_transform(movies['tags']).toarray()

similarity = cosine_similarity(vectors)

print('Hi I am a Recommender System')
print('I can give you recommendations for any Hollywold movie.')
print('Ensure that you enter the correct spelling of the movie')
time.sleep(4)
movie_input = input('Enter the name of the movie for which you want Recommandations:- ').title()
print()
print(f'Here are few Recommendation for the movie:- {movie_input} ')
time.sleep(2)
print()

def recommend(movie):
    movie_index = df[df['title'] == movie].index[0]
    distances = similarity[movie_index]
    movie_list = sorted(list(enumerate(distances)), reverse=True, key=lambda x:x[1])

    for i in movie_list[1:6]:
        print(df.iloc[i[0]].title)

recommend(movie_input)


import itertools
import sys
import math
import scipy.special as sp
import numpy as np
import os
import imageio
import natsort

__author__ = 'Marcin Kucharski'

def get_param(filein):
    """
    Function gets data from input file.
    :param filein:
    Name of input file.
    :return:
    Array with data from file.
    """
    modee_freq = []
    a = []
    p = []
    l_ = []
    m = []
    f = open(filein, 'r')
    for lines in f:
        parts = lines.split()
        modee_freq.append(float(parts[0]))
        a.append(float(parts[1]))
        p.append(float(parts[2]))
        l_.append(float(parts[3]))  # degree
        m.append(float(parts[4]))  # order
    return modee_freq, a, p, l_, m

def combinations(n_teta, n_fi):
    """
    Function makes theta and fi combinations.
    :param n_teta:
    Number of theta parts.
    :param n_fi:
    Number of phi parts.
    :return:
    Array with combinations.
    """
    teta = np.linspace(0, math.pi, n_teta)
    fi = np.linspace(0, 2 * math.pi, n_fi)

    comb = list(itertools.product(fi, teta))
    return comb

def r_values(combine, t, delta_t, freq, a, p, l_, m, name):
    """
    Function calculates r values for time t.
    Writes results to .dat file, and calls function with gnuplot commands.
    After making .png images, converts them to gif animation and removes temporary files.
    :param combine:
    Array with combinations.
    :param t:
    Time
    :param delta_t:
    Time period.
    :param freq:
    Frequency from file.
    :param a:
    Amplitude from file.
    :param p:
    Phase from file.
    :param l_:
    Degree of Spherical harmonic.
    :param m:
    Azimuthal order of spherical harmonics.
    :param name:
    Name of the output gif.
    """
    r = []
    time = 0.0
    while time < t:
        f = open('%s.dat' % round(time, 2), 'w+')
        f.write("PHI    THETA   R\n")
        for element in combine:
            i = 0
            sum = 0.0
            while i < len(a):
                sum += (a[i] *
                        np.sqrt((2 * l_[i] + 1 *
                                 np.math.factorial(l_[i] - m[i])) /
                                (4 * np.pi * np.math.factorial(l_[i] + m[i]))) *
                        sp.lpmv(m[i], l_[i], np.cos(element[1])) *
                        np.cos(m[i] * element[0]) *
                        np.cos(2 * np.pi * freq[i] * time + p[i]))
                i += 1
            theta_zamiana = ((math.pi / 2) - float(element[1]))
            r = (1.0 + sum)
            f.write("{}     {}      {}\n".format(str(element[0]), theta_zamiana, r))
        f.close()
        gnuplot_template(time, time)
        os.system("%s.plt" % time)
        os.system("del /f %s.plt" % time)
        os.system("del /f %s.dat" % round(time, 2))
        time += delta_t
    to_gif(name)
    os.system("del /f *.png")

def gnuplot_template(filename, time):
    """
    Function creates gnuplot commands.
    :param filename:
    Name of the file with commands.
    :param time:
    A moment in animation.
    :return:
    File with commands.
    """
    f = open('%s.plt' % filename, 'w+')
    f.write('''set terminal pngcairo size 1600,1000 enhanced
set pm3d
set hidden3d
set palette rgb 23,28,3
set mapping spherical
set border linewidth 2
unset key
set mxtics
set xrange [-1.1:1.3]
set yrange [-1.1:1.3]
set zrange [-1.1:1.3]
set cbrange [0.95:1.1]
set mytics
set mztics
set mcbtics
set xtics font 'Helvetica,13' offset 0,-1,0
set ytics font 'Helvetica,13' offset 2,0,0
set ztics font 'Helvetica,13' offset 0,0,0
set cbtics font 'Helvetica,17'
set format x "%.1f"
set format y "%.1f"
set format z "%.1f"
set format cb "%.2f"
set xlabel '(X/R_{star})' font 'Helvetica,15' offset 0,-1
set ylabel '(Y/R_{star})' font 'Helvetica,15' offset 0,-1
set zlabel '(Z/R_{star})' font 'Helvetica,15' offset -4
set cblabel 'Radial Displacement (1+{/Symbol D}r/R_{star})' font 'Helvetica,15' offset 5''')
    f.write("\nset title 'Elapsed Time = %s d" % round(time, 2))
    f.write("""\nset title  font 'Helvetica,30' offset 0,0,-2
set view 65,45,1.1
set view equal xyz
set ticslevel 0.1""")
    f.write("\nset output '%s.png'" % round(time, 2))
    f.write("\nsplot '%s.dat' u 1:2:3:3 w p lt 7 ps 0.9 palette" % round(time, 2))
    f.close()

def to_gif(name):
    """
    Functon that converts .png files to gif.
    :param name:
    Name of the output file.
    :return:
    Animation in gif.
    """
    gif_name = '%s.gif' % name
    file_list = []
    filenames = sorted(filter(os.path.isfile, [x for x in os.listdir() if x.endswith(".png")]))
    # filenames = natsort.natsorted(filenames)  <--- Uncomment when the file name contains '10'
    for filename in filenames:
        file_list.append(imageio.imread(filename))
    duration = 0.05
    imageio.mimsave(gif_name, file_list, duration=duration)

def print_help():
    """Prints help for user"""
    print("Warning!\n Script syntax:\n app.py <filein.txt> <fileout> <T> <delta_t> <N_teta> <N_fi>")
    exit()

def main():
    print("***************************")
    print("****Spherical Harmonics****")
    print("***************************")
    if len(sys.argv) != 7:
        print_help()
    else:
        o = sys.argv[1]
        freq, a, p, l_, m = get_param(o)
        com = combinations(int(sys.argv[5]), int(sys.argv[6]))
        r_values(com, int(sys.argv[3]), float(sys.argv[4]), freq, a, p, l_, m, sys.argv[2])

if __name__ == '__main__':
    main()


# helper data preprocessor
from src.reader import fetch_data
# custom PCA transformer
from src.pca import PCA
# custom One-Versus-Rest SVM
from src.ovr import OVR
# custom One-Versus-One SVM
from src.ovo import OVO

# scientific computing library
import numpy as np

# matplotlib backtest for missing $DISPLAY
import matplotlib
matplotlib.use('Agg')
# visualization tools
import matplotlib.pyplot as plt
import seaborn as sns
# prettify plots
plt.rcParams['figure.figsize'] = [12.0, 9.0]
sns.set_palette(sns.color_palette("muted"))
_palette = sns.color_palette("muted")
sns.set_style("ticks")

M = 121
standard = True

data = fetch_data(ratio=0.8)

X_train, y_train = data['train']

D, N = X_train.shape

pca = PCA(n_comps=M, standard=standard)

W_train = pca.fit(X_train)

X_test, y_test = data['test']
I, K = X_test.shape

W_test = pca.transform(X_test)

params = {'gamma': 2e-4, 'kernel': 'linear'}

fine = 5

# validate OVR
C_ovr = np.logspace(-5, 10, fine)
accuracy_ovr = []
for c in C_ovr:
    ovr = OVR(C=c, **params)
    ovr.fit(W_train, y_train)
    accuracy_ovr.append(ovr.score(W_test, y_test) * 100)

argmax_ovr = np.argmax(np.array(accuracy_ovr))

plt.semilogx(C_ovr, accuracy_ovr, color=_palette[0])
plt.axvline(C_ovr[argmax_ovr], color=_palette[1], lw=2)
plt.title('Cross Validation of $\mathbf{C}$ for OVR-SVM')
plt.xlabel('$\mathbf{C}$')
plt.ylabel('Recognition Accuracy [%]')
plt.savefig('data/out/c_validation_ovr.pdf', format='pdf', dpi=300)

# validate OVO
C_ovo = np.logspace(-5, 5, fine)
accuracy_ovo = []
for c in C_ovo:
    ovo = OVO(C=c, **params)
    ovo.fit(W_train, y_train)
    accuracy_ovo.append(ovo.score(W_test, y_test) * 100)

argmax_ovo = np.argmax(np.array(accuracy_ovo))

plt.figure()

plt.semilogx(C_ovo, accuracy_ovo, color=_palette[0])
plt.axvline(C_ovo[argmax_ovo], color=_palette[1], lw=2)
plt.title('Cross Validation of $\mathbf{C}$ for OVO-SVM')
plt.xlabel('$\mathbf{C}$')
plt.ylabel('Recognition Accuracy [%]')
plt.savefig('data/out/c_validation_ovo.pdf', format='pdf', dpi=300)


"""
1552. Magnetic Force Between Two Balls
Medium

In universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket.
Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.

Rick stated that magnetic force between two different balls at positions x and y is |x - y|.

Given the integer array position and the integer m. Return the required force.

Example 1:

Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6].
The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.
Example 2:

Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: We can use baskets 1 and 1000000000.

Constraints:

n == position.length
2 <= n <= 10^5
1 <= position[i] <= 10^9
All integers in position are distinct.
2 <= m <= position.length
"""
from typing import List

class Solution:
    def maxDistance(self, position: List[int], m: int) -> int:
        position.sort()
        def fn(d):
            """Return True if d is a feasible distance."""
            ans, prev = 0, float('-inf') # where previous ball is put
            for x in position:
                if x - prev >= d:
                    ans += 1
                    if ans == m: return True
                    prev = x
            return False

		# "last True" binary search (in contrast to "first True" binary search)
        lo, hi = 1, position[-1] - position[0]
        while lo < hi:
            mid = lo + hi + 1 >> 1
            if fn(mid): lo = mid
            else: hi = mid - 1
        return lo


#Julio Alexis González Villa 220839961
#Natalie Xiomara Moreno Bautista  220839996

#----------------------------------Actividad 7_Detección de Colores------------------------------------

import serial
import numpy as np
import pickle

serialPort = serial.Serial(port = "COM3", baudrate=9600)
serialString = ""

color = ['Rojo','Naranja','Amarillo','Verde','Limon','Azul','Marino','Cielo','Rosa','Morado','Cafe','Negro','Gris','Blanco']

model = pickle.load(open('act_7_colores.sav','rb'))

N = 100
i = 0

while(i<N):
    if(serialPort.in_waiting > 0):
        serialString = serialPort.readline()
        # print(serialString.decode('Ascii'))

        data_in = serialString.decode('Ascii')
        data_in = data_in.split(sep=',')
        data_in.pop()

        if(len(data_in)==4):
            data = [float(j) for j in data_in]
            data_array = np.asanyarray(data)

            y = model.predict(data_array.reshape(-1, 4))
            #i = i + 1
            d = y[0]
            print('iteration:',i,', data=',data_in, ', color=', color[d])
            i = i + 1

serialPort.close()

Python/Actividad 7 - Detector de colores/Act_7_Deteccion.py


from project.models import OperatorLog
import datetime
from django.db import models
from project.models import Project,ProjectInvestDataModel
from rest_framework.filters import OrderingFilter

def strformat(ouser,obj,otype,request):#对修改和删除的对象的具体返回格式
    #传入
    if otype=='1':#1是修改,原来是什么，现在是什么
        if isinstance(obj,Project):
            #getdata = request.data #待更新的集合
            #toupdatedata = [(i,obj.__getattribute__(i),getdata[i]) for i in getdata if getdata[i]!=obj.__getattribute__(i)]
            #项目编号和项目名称
            res = (obj.id,obj.name)
            stritem = "项目编号为{},项目名称为{};"
            return stritem.format(*res)

        elif isinstance(obj,ProjectInvestDataModel):
            afterdata = request.data
            beforedata = [obj.__getattribute__(i) for i in afterdata]
            strtest = "投资数据"+"{} "*len(afterdata)*2+"修改为" +"{} "*len(afterdata)
            result = strtest.format(*afterdata,*beforedata,*afterdata.values())
            return result

    elif otype=='0':#0是删除
        if isinstance(obj, Project):
            return "　项目申请　{}".format(repr(obj))
        elif isinstance(obj,ProjectInvestDataModel):
            return "　投资数据　{}".format(repr(obj))

def write_to_log(ouser,obj,otype,request):
    #obj对应的file,otype是operator的对应的操作type
    if isinstance(obj,models.Model):
        oobj = strformat(ouser,obj,otype,request)
    else:
        oobj=obj
    OperatorLog.objects.create(otime=datetime.datetime.now(), oman=ouser, oobj=oobj, otype=otype)


#!/usr/bin/env python3
#!coding=utf-8
#
import sys
print(sys.version)
########################
from selenium import webdriver
import time
import random
#from bs4 import BeautifulSoup
import re
print("-"*10,"import end","-"*10)
########
def requestitem(url, pattern):
    # 爬取特定url页面内容
    driver = webdriver.Chrome(executable_path='/Users/shzy/seleniumwebdriver/chromedriver')
    driver.get(url)
    sleeptime = random.randint(2,5)
    print('time--'+str(sleeptime))
    time.sleep(sleeptime)
    content = driver.page_source
    driver.close()
    ##############
    items = re.findall(pattern, content)
    ######## 爬取收藏夹名称
    pattern0 = r'PageHeader-title">(.*?)
'
    try:
        items2 = re.search(pattern0, content).group(1)
    except:
        items2 = str(random.randint(1,10))
    ############## 打印看效果
    for i1, item in enumerate(items):
        print(i1, item)
    return items, items2
    ###############

def requestPage(url, pattern):
    # 爬取特定url页面内容
    driver = webdriver.Chrome(executable_path='/Users/shzy/seleniumwebdriver/chromedriver')
    driver.get(url)
    sleeptime = random.randint(2,5)
    print('time--'+str(sleeptime))
    time.sleep(sleeptime)
    content = driver.page_source
    driver.close()
    ##############
    items = re.findall(pattern, content)
    ############## 打印看效果
    #for i1, item in enumerate(items):
    #    print(i1, item)
    return items
   #^^^^^^^END

########################
def craw_folder(urlx, allpage, outfile):
    ######## 爬取收藏夹内容
#    pattern = r'(.*?)'
    pattern = r'(.*?)'
    for i1 in range(allpage):
        urli = urlx+str(i1+1)
        # 爬取一页
        print(urli)
        itemi, foldernamei = requestitem(urli, pattern)
        ff = open(outfile, 'a')
        for ii, i11 in enumerate(itemi):
            print(str(i1+1)+'\t'+str(ii)+'\t'+i11[0]+'\t'+i11[1]+'\t'+foldernamei, file=ff)
        ff.close()
   #^^^^^^^END

def readinCollection(filepath):
    # 读入收藏夹名与url清单
    ff = open(filepath, 'r')
    namelst = []
    urllst = []
    for i1 in ff:
        i1 = i1.replace('\n', '')
        print(i1)
        namelst.append(i1.split(' ')[0])
        urllst.append(i1.split(' ')[1])
    ff.close()
    return namelst, urllst
   #^^^^^^^END

###############################################################################
######## 爬取收藏夹内容
#url_collections = 'https://www.zhihu.com/people/wahaha50/collections'
namelst, urllst = readinCollection('./收藏夹信息.txt')
######### 获取每个收藏夹的最大页数
pa = '<button type="button" class="Button PaginationButton Button--plain">(.*?)</button>'
pagelst = []
for ci in urllst:
    items = requestPage(ci, pa)
    allpage = [int(i) for i in items]
    try:
        maxpage = max(allpage)
    except:
        maxpage = 1
    pagelst.append(maxpage)
print(pagelst)
######## 具体爬取每个收藏夹
for ii, urli in enumerate(urllst):
    craw_folder(urli+'?page=', pagelst[ii], 'alllist.txt')


from datetime import datetime

def reduce_request_in_ts_precision(record):
    timestamp = int(record['requestInTs'] / 1000)
    initial_datetime = datetime.fromtimestamp(timestamp)
    altered_datetime = initial_datetime.replace(minute=0, second=0)
    record['requestInTs'] = int(altered_datetime.timestamp()) * 1000
    return record

def force_durations_to_integer_range(record):
    total_duration = record['totalDuration']
    if total_duration:
        record['totalDuration'] = (min(total_duration, 2**31 - 1) if total_duration > 0
                                   else max(total_duration, -(2**31 - 1)))

    producer_duration = record['producerDurationProducerView']
    if producer_duration:
        record['producerDurationProducerView'] = (min(producer_duration, 2**31 - 1) if producer_duration > 0
                                                  else max(producer_duration, -(2**31 - 1)))
    return record


import argparse

import wandb

def parse_args():
    parser = argparse.ArgumentParser(prog='python sweep.py',
                                     description='Intialize a wandb hyperparameter sweep for PopGenAdapt for a given dataset and SSDA method.')

    parser.add_argument('--data', type=str, required=True,
                        help='Path to the dataset JSON file.')
    parser.add_argument('--mme', action='store_true',
                        help='Whether to use minimax entropy.')
    parser.add_argument('--sla', action='store_true',
                        help='Whether to use source label adaptation.')
    parser.add_argument('--suffix', type=str, default='',  # useful for different datasets
                        help='Suffix to append to the project name.')

    return parser.parse_args()

if __name__ == '__main__':
    args = parse_args()

    project_name = f"PopGenAdapt-{'base' if not args.mme else 'mme'}{('-sla' if args.sla else '')}{'-' + args.suffix if args.suffix else ''}"

    sweep_config = {
        'program': 'main.py',
        'method': 'random',
        'metric': {
            'name': 'best_val_auc',
            'goal': 'maximize'
        },
        'parameters': {
            'lr': {
                'distribution': 'log_uniform_values',
                'min': 1e-5,
                'max': 1e-2
            },
        },
        'command': [
            '${env}',
            'python3',
            '${program}',
            '--verbose',
            '--project',
            project_name,
            '--seed',
            '42',
            '--data',
            args.data,
        ] + ([] if not args.mme else ['--mme'])
          + ([] if not args.sla else ['--sla'])
          + ['${args}']
    }

    if args.sla:
        sweep_config['parameters'].update({
            'mme_lambda': {
                'distribution': 'uniform',
                'min': 0.0,
                'max': 1.0
            },
            'sla_warmup': {
                'values': [100, 500, 1000, 2000, 5000]
            },
            'sla_temperature': {
                'distribution': 'uniform',
                'min': 0.0,
                'max': 1.0
            },
            'sla_alpha': {
                'distribution': 'uniform',
                'min': 0.0,
                'max': 1.0
            },
            'sla_update_interval': {
                'values': [5, 10, 100, 500, 1000, 2000, 5000]
            }
        })

    sweep_id = wandb.sweep(sweep_config,
                           project=project_name)
    wandb.agent(sweep_id)


#!/usr/bin/env python3

import sys
import os
from app import root_url
import unittest
from unittest.mock import MagicMock
from tempfile import TemporaryDirectory
import json
from wsgiref.validate import validator
from wsgiref.util import setup_testing_defaults
from io import BytesIO

sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from woof.resource import MetaResource
from woof.server import RESTServer, config

class WSGIMockServer:

    def __init__(self, application):
        self.application = validator(application)
        self.responce = ""
        self.environ = {}
        self.start_response = MagicMock()

    def assert_response_is(self, expected):
        if self.responce != expected:
            raise AssertionError('Expected response: {}\nActual response: {}'
                                 .format(expected, self.response))

    def _run_app(self, path, query_string='', body=''):
        if isinstance(body, str):
            body = body.encode('utf-8')

        self.environ['PATH_INFO'] = path
        self.environ['QUERY_STRING'] = query_string
        self.environ['CONTENT_LENGTH'] = str(len(body))
        self.environ['wsgi.input'] = BytesIO(body)
        self.start_response.reset_mock()
        iterator = self.application(self.environ, self.start_response)
        try:
            self.responce = b''.join(list(iterator))
        except:
            raise
        finally:
            iterator.close()
        return self.responce

    def __getattr__(self, name):
        if name in ('get', 'delete', 'post', 'put',
                    'options', 'head', 'trace', 'connect'):
            self.environ = {}
            setup_testing_defaults(self.environ)
            self.environ['REQUEST_METHOD'] = name.upper()
            return self._run_app
        raise AttributeError("'WSGIMockServer' object has no attribute '{}'"
                             .format(name))

class TestCrud(unittest.TestCase):

    def AssertJsonEqual(self, serialized, expected):
        self.assertEqual(json.loads(serialized.decode('utf-8')), expected)

    def AssertStatusEquals(self, status):
        try:
            self.assertEqual(self.server.start_response.call_args[0][0], status)
        except IndexError:
            self.fail("start_response method hasn't called with status")

    @classmethod
    def setUpClass(cls):
        cls.tmp_dir = TemporaryDirectory()
        with open('config.json', 'w') as conf_file:
            conf_file.write(
                '{'
                    '"database": {'
                        '"provider": "sqlite",'
                        '"database": "%s"'
                    '}'
                '}' % os.path.join(cls.tmp_dir.name, 'test.db'))

        MetaResource.initialize(config.database)
        MetaResource.create_tables()
        application = RESTServer(root_url)
        cls.server = WSGIMockServer(application)
        cls.saved_books = []

    def test_01_select_books(self):
        books = self.server.get('/api/books', '')
        self.assertEqual(books, b'[]')

    def test_02_create_book(self):
        book = dict(title='Martine to the cinema',
                    abstract='Martine go to the cinema with Chuck Noris')

        expected_book = dict(id=1, chapters=[])
        expected_book.update(book)

        created_book = self.server.post('/api/books', '', json.dumps(book))

        self.saved_books.append(expected_book)
        self.AssertJsonEqual(created_book, expected_book)

    def test_03_create_book(self):
        book = dict(title='The life of Martine',
                    abstract='Martine has a hetic life')

        expected_book = dict(id=2, chapters=[])
        expected_book.update(book)

        created_book = self.server.post('/api/books', '', json.dumps(book))

        self.saved_books.append(expected_book)
        self.AssertJsonEqual(created_book, expected_book)

    def test_04_select_books(self):
        books = self.server.get('/api/books', '')
        self.AssertJsonEqual(books, self.saved_books)

    def test_05_select_one_book(self):
        book = self.server.get('/api/books/1', '')
        self.AssertJsonEqual(book, self.saved_books[0])

    def test_06_select_not_existing_book(self):
        book = self.server.get('/api/books/342', '')
        self.AssertStatusEquals('404 Not Found')
        self.AssertJsonEqual(book, {'error': 'Resource not found'})

    def test_07_update_books(self):
        book = dict(title='Martine go to the cinema',
                    abstract='Martine go to the cinema with Chuck Noris')
        updated = self.server.put('/api/books/1', '', json.dumps(book))
        book.update(dict(id=1, chapters=[]))
        self.AssertJsonEqual(updated, book)

    def test_08_create_chapter(self):
        chapter = dict(number=1, title="First step")
        created = self.server.post('/api/books/1/chapters', '', json.dumps(chapter))
        chapter['book_id'] = 1
        self.AssertJsonEqual(created, chapter)

    def test_09_create_chapter(self):
        chapter = dict(number=1, title="Init")
        created = self.server.post('/api/books/2/chapters', '', json.dumps(chapter))
        chapter['book_id'] = 2
        self.AssertJsonEqual(created, chapter)

    def test_10_update_chapter(self):
        chapter = dict(number=1, title="Introduction")
        updated = self.server.put('/api/books/1/chapters/1', '', json.dumps(chapter))
        chapter['book_id'] = 1
        self.AssertJsonEqual(updated, chapter)

    def test_11_select_chapter(self):
        chapter = self.server.get('/api/books/1/chapters/1', '')
        expected = dict(book_id=1, number=1, title="Introduction")
        self.AssertJsonEqual(chapter, expected)

    def test_12_select_chapters(self):
        chapter = self.server.get('/api/books/1/chapters', '')
        expected = [{"book_id": 1, "number": 1, "title": "Introduction"}]
        self.AssertJsonEqual(chapter, expected)

    def test_13_select_chapters(self):
        chapter = self.server.get('/api/books/2/chapters', '')
        expected = [{"book_id": 2, "number": 1, "title": "Init"}]
        self.AssertJsonEqual(chapter, expected)

    def test_14_delete_chapter(self):
        chapter = self.server.delete('/api/books/1/chapters/1', '')
        self.AssertJsonEqual(chapter, '')

    def test_15_select_chapters(self):
        chapter = self.server.get('/api/books/1/chapters', '')
        expected = []
        self.AssertJsonEqual(chapter, expected)

    def test_16_select_chapters(self):
        chapter = self.server.get('/api/books/2/chapters', '')
        expected = [{"book_id": 2, "number": 1, "title": "Init"}]
        self.AssertJsonEqual(chapter, expected)

    @classmethod
    def tearDownClass(cls):
        cls.tmp_dir.cleanup()
        os.remove('config.json')


from metadata import *
from credentails import *
from spark_cluster import *
from Transform import *
from spark_connector import *
import pyspark.sql.functions as F

def main() -> None:
    #spark_cluster._jvm.net.snowflake.spark.snowflake.SnowflakeConnectorUtils.enablePushdownSession(spark_cluster._jvm.org.apache.spark.sql.SparkSession.builder().getOrCreate())
    spark_cluster.sparkContext.setLogLevel("ERROR")
    users_data = read_data(spark=spark_cluster,options={"multiLine": True},mode='permissive',file_format='json',file_path=files['users'])
    transformed_users_data = transform_data(df=users_data,data_mapping=col_dtypes_users)
    transformed_users_data = transformed_users_data.withColumn('email', F.regexp_replace(F.col('email'), pattern='(?s).*@',replacement="@"))
    transformed_users_data = transformed_users_data.withColumn('address', F.md5(col='address'))
    transformed_users_data = transformed_users_data.withColumn('birthDate', F.md5(col='birthDate'))
    transformed_users_data = transformed_users_data.withColumn('firstname', F.md5(col='firstname'))
    transformed_users_data = transformed_users_data.withColumn('lastname', F.md5(col='lastname'))
    #transformed_users_data.show()
    write_data(df=transformed_users_data,write_format='snowflake',credentials=credential_properties,table_name='users',write_mode='overwrite')
    messages_data = read_data(spark=spark_cluster,options={"multiLine": True},mode='permissive',file_format='json',file_path=files['messages'])
    transformed_messages_data = transform_data(df=messages_data,data_mapping=col_dtypes_messages)
    transformed_messages_data = transformed_messages_data.withColumn('message', F.md5(col='message'))
    #transformed_messages_data.show()
    write_data(df=transformed_messages_data,write_format='snowflake',credentials=credential_properties,table_name='messages',write_mode='overwrite')
    connector()

if __name__ == '__main__':
    main()


import copy
import itertools
import os
import sys
from builtins import str

import mobly.config_parser as mobly_config_parser

from acts import keys
from acts import utils

# An environment variable defining the base location for ACTS logs.
_ENV_ACTS_LOGPATH = 'ACTS_LOGPATH'
# An environment variable that enables test case failures to log stack traces.
_ENV_TEST_FAILURE_TRACEBACKS = 'ACTS_TEST_FAILURE_TRACEBACKS'
# An environment variable defining the test search paths for ACTS.
_ENV_ACTS_TESTPATHS = 'ACTS_TESTPATHS'
_PATH_SEPARATOR = ':'

class ActsConfigError(Exception):
    """Raised when there is a problem in test configuration file."""

def _validate_test_config(test_config):
    """Validates the raw configuration loaded from the config file.

    Making sure all the required fields exist.
    """
    for k in keys.Config.reserved_keys.value:
        # TODO(markdr): Remove this continue after merging this with the
        # validation done in Mobly's load_test_config_file.
        if (k == keys.Config.key_test_paths.value
                or k == keys.Config.key_log_path.value):
            continue

        if k not in test_config:
            raise ActsConfigError("Required key %s missing in test config." %
                                  k)

def _validate_testbed_name(name):
    """Validates the name of a test bed.

    Since test bed names are used as part of the test run id, it needs to meet
    certain requirements.

    Args:
        name: The test bed's name specified in config file.

    Raises:
        If the name does not meet any criteria, ActsConfigError is raised.
    """
    if not name:
        raise ActsConfigError("Test bed names can't be empty.")
    if not isinstance(name, str):
        raise ActsConfigError("Test bed names have to be string.")
    for l in name:
        if l not in utils.valid_filename_chars:
            raise ActsConfigError(
                "Char '%s' is not allowed in test bed names." % l)

def _update_file_paths(config, config_path):
    """ Checks if the path entries are valid.

    If the file path is invalid, assume it is a relative path and append
    that to the config file path.

    Args:
        config : the config object to verify.
        config_path : The path to the config file, which can be used to
                      generate absolute paths from relative paths in configs.

    Raises:
        If the file path is invalid, ActsConfigError is raised.
    """
    # Check the file_path_keys and update if it is a relative path.
    for file_path_key in keys.Config.file_path_keys.value:
        if file_path_key in config:
            config_file = config[file_path_key]
            if type(config_file) is str:
                if not os.path.isfile(config_file):
                    config_file = os.path.join(config_path, config_file)
                if not os.path.isfile(config_file):
                    raise ActsConfigError(
                        "Unable to load config %s from test "
                        "config file.", config_file)
                config[file_path_key] = config_file

def _validate_testbed_configs(testbed_configs, config_path):
    """Validates the testbed configurations.

    Args:
        testbed_configs: A list of testbed configuration json objects.
        config_path : The path to the config file, which can be used to
                      generate absolute paths from relative paths in configs.

    Raises:
        If any part of the configuration is invalid, ActsConfigError is raised.
    """
    # Cross checks testbed configs for resource conflicts.
    for name, config in testbed_configs.items():
        _update_file_paths(config, config_path)
        _validate_testbed_name(name)

def gen_term_signal_handler(test_runners):
    def termination_sig_handler(signal_num, frame):
        print('Received sigterm %s.' % signal_num)
        for t in test_runners:
            t.stop()
        sys.exit(1)

    return termination_sig_handler

def _parse_one_test_specifier(item):
    """Parse one test specifier from command line input.

    Args:
        item: A string that specifies a test class or test cases in one test
            class to run.

    Returns:
        A tuple of a string and a list of strings. The string is the test class
        name, the list of strings is a list of test case names. The list can be
        None.
    """
    tokens = item.split(':')
    if len(tokens) > 2:
        raise ActsConfigError("Syntax error in test specifier %s" % item)
    if len(tokens) == 1:
        # This should be considered a test class name
        test_cls_name = tokens[0]
        return test_cls_name, None
    elif len(tokens) == 2:
        # This should be considered a test class name followed by
        # a list of test case names.
        test_cls_name, test_case_names = tokens
        clean_names = [elem.strip() for elem in test_case_names.split(',')]
        return test_cls_name, clean_names

def parse_test_list(test_list):
    """Parse user provided test list into internal format for test_runner.

    Args:
        test_list: A list of test classes/cases.
    """
    result = []
    for elem in test_list:
        result.append(_parse_one_test_specifier(elem))
    return result

def load_test_config_file(test_config_path, tb_filters=None):
    """Processes the test configuration file provided by the user.

    Loads the configuration file into a json object, unpacks each testbed
    config into its own TestRunConfig object, and validate the configuration in
    the process.

    Args:
        test_config_path: Path to the test configuration file.
        tb_filters: A subset of test bed names to be pulled from the config
                    file. If None, then all test beds will be selected.

    Returns:
        A list of mobly.config_parser.TestRunConfig objects to be passed to
        test_runner.TestRunner.
    """
    configs = utils.load_config(test_config_path)

    testbeds = configs[keys.Config.key_testbed.value]
    if type(testbeds) is list:
        tb_dict = dict()
        for testbed in testbeds:
            tb_dict[testbed[keys.Config.key_testbed_name.value]] = testbed
        testbeds = tb_dict
    elif type(testbeds) is dict:
        # For compatibility, make sure the entry name is the same as
        # the testbed's "name" entry
        for name, testbed in testbeds.items():
            testbed[keys.Config.key_testbed_name.value] = name

    if tb_filters:
        tbs = {}
        for name in tb_filters:
            if name in testbeds:
                tbs[name] = testbeds[name]
            else:
                raise ActsConfigError(
                    'Expected testbed named "%s", but none was found. Check'
                    'if you have the correct testbed names.' % name)
        testbeds = tbs

    if (keys.Config.key_log_path.value not in configs
            and _ENV_ACTS_LOGPATH in os.environ):
        print('Using environment log path: %s' %
              (os.environ[_ENV_ACTS_LOGPATH]))
        configs[keys.Config.key_log_path.value] = os.environ[_ENV_ACTS_LOGPATH]
    if (keys.Config.key_test_paths.value not in configs
            and _ENV_ACTS_TESTPATHS in os.environ):
        print('Using environment test paths: %s' %
              (os.environ[_ENV_ACTS_TESTPATHS]))
        configs[keys.Config.key_test_paths.
                value] = os.environ[_ENV_ACTS_TESTPATHS].split(_PATH_SEPARATOR)
    if (keys.Config.key_test_failure_tracebacks not in configs
            and _ENV_TEST_FAILURE_TRACEBACKS in os.environ):
        configs[keys.Config.key_test_failure_tracebacks.
                value] = os.environ[_ENV_TEST_FAILURE_TRACEBACKS]

    # TODO: See if there is a better way to do this: b/29836695
    config_path, _ = os.path.split(utils.abs_path(test_config_path))
    configs[keys.Config.key_config_path.value] = config_path
    _validate_test_config(configs)
    _validate_testbed_configs(testbeds, config_path)
    # Unpack testbeds into separate json objects.
    configs.pop(keys.Config.key_testbed.value)
    test_run_configs = []

    for _, testbed in testbeds.items():
        test_run_config = mobly_config_parser.TestRunConfig()
        test_run_config.testbed_name = testbed[
            keys.Config.key_testbed_name.value]
        test_run_config.controller_configs = testbed
        test_run_config.controller_configs[
            keys.Config.key_test_paths.value] = configs.get(
                keys.Config.key_test_paths.value, None)
        test_run_config.log_path = configs.get(keys.Config.key_log_path.value,
                                               None)
        if test_run_config.log_path is not None:
            test_run_config.log_path = utils.abs_path(test_run_config.log_path)

        user_param_pairs = []
        for item in itertools.chain(configs.items(), testbed.items()):
            if item[0] not in keys.Config.reserved_keys.value:
                user_param_pairs.append(item)
        test_run_config.user_params = dict(user_param_pairs)

        test_run_configs.append(test_run_config)
    return test_run_configs

def parse_test_file(fpath):
    """Parses a test file that contains test specifiers.

    Args:
        fpath: A string that is the path to the test file to parse.

    Returns:
        A list of strings, each is a test specifier.
    """
    with open(fpath, 'r') as f:
        tf = []
        for line in f:
            line = line.strip()
            if not line:
                continue
            if len(tf) and (tf[-1].endswith(':') or tf[-1].endswith(',')):
                tf[-1] += line
            else:
                tf.append(line)
        return tf


from flask import Flask, Blueprint, render_template, request, redirect
from datetime import datetime

from models.animal import Animal
import repositories.animal_repo as animal_repo
import repositories.owner_repo as owner_repo
import repositories.vet_repo as vet_repo

animals_blueprint = Blueprint("animals", __name__)

# animals.html
@animals_blueprint.route("/animals")
def animals():
    animals = animal_repo.select_all()
    no_vet = None
    return render_template("animals/animals.html", animals=animals, no_vet = no_vet, title="Manage Animals")

# new.html
@animals_blueprint.route("/animals/new")
def new_animal():
    owners = owner_repo.select_all()
    vets = vet_repo.select_all()
    current_date = datetime.now()
    return render_template("/animals/new.html", owners=owners, vets=vets, current_date=current_date, title="Create New Animal")

# After form to create a new animal, redirect to that animal's details page
@animals_blueprint.route("/animals", methods=["POST"])
def create_animal():
    name = request.form["name"]
    species = request.form["species"]
    date_of_birth = str(request.form["date_of_birth"])
    owner_id = request.form["owner_id"]
    owner = owner_repo.select(owner_id)
    vet_id = request.form["vet_id"]
    vet = vet_repo.select(vet_id)
    new_animal = Animal(name, species, date_of_birth, owner, vet)
    animal_repo.save(new_animal)
    return redirect(f"/animals/{new_animal.id}")

# Edit animal's details --> edit.html
@animals_blueprint.route("/animals/<id>/edit")
def edit_animal(id):
    animal = animal_repo.select(id)
    vets = vet_repo.select_all()
    owners = owner_repo.select_all()
    current_date = datetime.now()
    return render_template("/animals/edit.html", animal=animal, vets=vets, owners=owners, current_date=current_date, title=f"Edit {animal.name}'s Details")

# Update animal's details after editing
@animals_blueprint.route("/animals/<id>", methods=["POST"])
def update_animal(id):
    name = request.form["name"]
    species = request.form["species"]
    date_of_birth = str(request.form["date_of_birth"])
    owner_id = request.form["owner_id"]
    owner = owner_repo.select(owner_id)
    vet_id = request.form["vet_id"]
    vet = vet_repo.select(vet_id)
    animal = Animal(name, species, date_of_birth, owner, vet, id)
    animal_repo.update(animal)
    return redirect(f"/animals/{id}")

# Delete animal from system
@animals_blueprint.route("/animals/<id>/delete", methods=["POST"])
def delete_animal(id):
    animal_repo.delete(id)
    return redirect("/animals")

# Show details of one animal --> show.html
@animals_blueprint.route("/animals/<id>")
def show_animal(id):
    animal = animal_repo.select(id)
    no_vet = None
    return render_template("/animals/show.html", animal=animal, no_vet=no_vet, title=f"{animal.name}'s Details")


import numpy as np
import math
from typing import Any

from flint import Tensor
from .module import Module
from .. import Parameter, init
from .. import functional as F

class Identity(Module):
    """
    A placeholder identity operator that is argument-insensitive.

    - Input shape: :math:`(*)`, where :math:`*` means any number of dimensions.
    - Output shape: :math:`(*)`, same shape as the input.

    Parameters
    ----------
    args : Any
        Any argument (unused).

    kwargs: Any
        Any keyword argument (unused).
    """
    def __init__(self, *args: Any, **kwargs: Any) -> None:
        super(Identity, self).__init__()

    def forward(self, input: Tensor) -> Tensor:
        return input

class Linear(Module):
    """
    Full connected layer

    .. math::
        y = x A^T + b

    - Input shape: ``(batch_size, in_features)``
    - Output shape: ``(batch_size, out_features)``

    Parameters
    ----------
    in_features : int
        Size of each input sample.

    out_features : int
        Size of each output sample.

    bias : bool, optional, default=True
        Enable bias or not.
    """
    def __init__(self, in_features: int, out_features: int, bias: bool = True) -> None:
        super(Linear, self).__init__()

        self.in_features = in_features
        self.out_features = out_features

        self.weight = Parameter(Tensor.zeros(in_features, out_features))
        if bias:
            self.bias = Parameter(Tensor.zeros(1, out_features))
        else:
            self.register_parameter('bias', None)

        self.init_parameters()

    def init_parameters(self) -> None:
        init.kaiming_uniform_(self.weight, a=math.sqrt(5))
        if self.bias is not None:
            fan_in, _ = init._calculate_fan_in_and_fan_out(self.weight)
            bound = 1 / math.sqrt(fan_in)
            init.uniform_(self.bias, -bound, bound)

    def forward(self, input: Tensor) -> Tensor:
        self.output = F.linear(input, self.weight, self.bias)
        return self.output


#!/usr/bin/python
from scipy.interpolate import interp1d
import matplotlib as mpl
from matplotlib.pyplot import plot,show,hold,grid
from scipy.io import wavfile
import numpy as np
import scipy as sp
import scipy.signal as sig
import allantools
import sys

def db20(x):
	return 20.*np.log10(x)

def db10(x):
	return 10.*np.log10(x)

def zero_cross(k,neg,pos):
	return k - neg/(pos-neg)

def downconvert(x,fc=3.e3,fs=96.e3):
	t = np.linspace(0,(len(x)-1)/fs,len(x))
	x = sig.hilbert(x)

	x_mix = np.exp(-1j*2.*np.pi*fc*t)
	return x*x_mix

fname = sys.argv[1]
try:
	fc = float(sys.argv[2])
except:
	fc = 3.0e3

fs,s = wavfile.read(fname)
print("fc: %8.3f  fs: %8.3f" % (fc,fs) )

if s.ndim == 2:
	#Do left channel
	s = s[:,0]

### Calculate phase

# Downconvert
s = downconvert(s,fc=fc,fs=fs)
#>> sineda=detrend(unwrap(angle(filter(rrc,1,sined)))*180/pi);
pha = sp.unwrap( np.angle( s ))
inst_freq = np.diff(pha)/(2.*np.pi)*fs

t = np.linspace(0,(len(s)-1)/fs,len(s))

mpl.pyplot.figure(1)
plot(t,pha*180./np.pi)
grid(True)

mpl.pyplot.figure(2)
plot(t[1:],inst_freq)
grid(True)
#Allan variance
mpl.pyplot.figure(3)

(t,ad,ade,adn) = allantools.oadev(pha - np.mean(pha), data_type='phase', rate=fs)
(t,ad,ade,adn) = allantools.oadev(inst_freq - np.mean(inst_freq), data_type='freq', rate=fs)
mpl.pyplot.loglog(t,ad)
grid(True)

PLOT = True
#PLOT = False
if PLOT:
	show()


#!/usr/bin/env python
#encoding:utf8

'''  启动脚本  '''

import os

#代码覆盖检测所需的
COV = None
if os.environ.get('FLASK_COVERAGE'):
    import coverage
    COV = coverage.coverage(branch=True, include='app/*')
    COV.start()

from app import create_app, db
from app.models import User, Role, Permission, Post, Comment
from flask.ext.script import Manager, Shell
from flask.ext.migrate import Migrate, MigrateCommand

app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
migrate = Migrate(app, db)

def make_shell_context():
    return dict(app=app, db=db, User=User, Role=Role, Permission=Permission, Post=Post, Comment=Comment)

manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)

#启动单元测试命令
@manager.command
def test(coverage=False):
    ''' Run the unit tests '''

    #代码覆盖检测所需的
    if coverage and not os.environ.get('FLASK_COVERAGE'):
        import sys
        os.environ['FLASK_COVERAGE'] = '1'
        os.execvp(sys.executable, [sys.executable] + sys.argv)

    import unittest
    tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests)

    #代码覆盖检测
    ''' 获取代码覆盖报告:  python manage.py test --coverage   '''
    if COV:
        COV.stop()
        COV.save()
        print(u'报告摘要:')
        COV.report()
        basedir = os.path.abspath(os.path.dirname(__file__))
        covdir = os.path.join(basedir, 'tmp/coverage')
        COV.html_report(directory=covdir)
        print(u'详细报告文件: file://%s/index.html' % covdir)
        COV.erase()

# 在请求分析监视下运行程序 【源码分析】
# 使用: python manage.py profile
@manager.command
def profile(length=25, profile_dir=None):
    """Start the application under the code profiler."""
    from werkzeug.contrib.profiler import ProfilerMiddleware
    app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[length],
                                      profile_dir=profile_dir)
    app.run()

# 部署命令 （每次安装或升级程序时只需要运行deploy命令）
# 使用: python  manage.py  deploy
@manager.command
def deploy():
    ''' Run deployment tasks. '''
    from flask.ext.migrate import upgrade
    from app.models import Role, User

    #把数据库迁移到最新修订版本
    upgrade()

    #创建用户角色
    Role.insert_roles()

    # 让所有用户关注此用户
    User.add_self_follows()

if __name__ == '__main__':
    manager.run()