#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2011  Marek Marczykowski <marmarek@mimuw.edu.pl>
#
# 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 2
# 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.
#

'''Retrieve menu entries from a VM and convert to appmenu templates'''

import re
import os
import sys
import shlex
import pkg_resources
import qubesimgconverter

import qubesadmin.exc
import qubesadmin.tools
import qubesappmenus

parser = qubesadmin.tools.QubesArgumentParser(
    vmname_nargs='?',
    show_forceroot=True,
    description='retrieve appmenus')

parser.add_argument('--force-rpc',
    action='store_true', default=False,
    help="Force to start a new RPC call, even if called from existing one")

parser.add_argument('--regenerate-only',
    action='store_true', default=False,
    help='Only regenerate appmenus entries, do not synchronize with system '
         'in template')

# TODO offline mode

# fields required to be present (and verified) in retrieved desktop file
required_fields_legacy = ["Name", "Exec"]
required_fields = ["Name"]

# limits
appmenus_line_size = 1024
appmenus_line_count = 100000

# regexps for sanitization of retrieved values
std_re = re.compile(r"\A[/a-zA-Z0-9.,:&()_ +-]*\Z")
fields_regexp = {
    "Name": std_re,
    "GenericName": std_re,
    "Comment": std_re,
    "Categories": re.compile(r"\A[a-zA-Z0-9/.;:'() -]*\Z"),
    "Exec": re.compile(r"\A[a-zA-Z0-9()_&>/{}:.= -]*\Z"),
    "Icon": re.compile(r"\A[a-zA-Z0-9/_.-]*\Z"),
}

CATEGORIES_WHITELIST = {
    # Main Categories
    # http://standards.freedesktop.org/menu-spec/1.1/apa.html 20140507
    'AudioVideo', 'Audio', 'Video', 'Development', 'Education', 'Game',
    'Graphics', 'Network', 'Office', 'Science', 'Settings', 'System',
    'Utility',

    # Additional Categories
    # http://standards.freedesktop.org/menu-spec/1.1/apas02.html
    'Building', 'Debugger', 'IDE', 'GUIDesigner', 'Profiling',
    'RevisionControl', 'Translation', 'Calendar', 'ContactManagement',
    'Database', 'Dictionary', 'Chart', 'Email', 'Finance', 'FlowChart', 'PDA',
    'ProjectManagement', 'Presentation', 'Spreadsheet', 'WordProcessor',
    '2DGraphics', 'VectorGraphics', 'RasterGraphics', '3DGraphics', 'Scanning',
    'OCR', 'Photography', 'Publishing', 'Viewer', 'TextTools',
    'DesktopSettings', 'HardwareSettings', 'Printing', 'PackageManager',
    'Dialup', 'InstantMessaging', 'Chat', 'IRCClient', 'Feed', 'FileTransfer',
    'HamRadio', 'News', 'P2P', 'RemoteAccess', 'Telephony', 'TelephonyTools',
    'VideoConference', 'WebBrowser', 'WebDevelopment', 'Midi', 'Mixer',
    'Sequencer', 'Tuner', 'TV', 'AudioVideoEditing', 'Player', 'Recorder',
    'DiscBurning', 'ActionGame', 'AdventureGame', 'ArcadeGame', 'BoardGame',
    'BlocksGame', 'CardGame', 'KidsGame', 'LogicGame', 'RolePlaying',
    'Shooter', 'Simulation', 'SportsGame', 'StrategyGame', 'Art',
    'Construction', 'Music', 'Languages', 'ArtificialIntelligence',
    'Astronomy', 'Biology', 'Chemistry', 'ComputerScience',
    'DataVisualization', 'Economy', 'Electricity', 'Geography', 'Geology',
    'Geoscience', 'History', 'Humanities', 'ImageProcessing', 'Literature',
    'Maps', 'Math', 'NumericalAnalysis', 'MedicalSoftware', 'Physics',
    'Robotics', 'Spirituality', 'Sports', 'ParallelComputing', 'Amusement',
    'Archiving', 'Compression', 'Electronics', 'Emulator', 'Engineering',
    'FileTools', 'FileManager', 'TerminalEmulator', 'Filesystem', 'Monitor',
    'Security', 'Accessibility', 'Calculator', 'Clock', 'TextEditor',
    'Documentation', 'Adult', 'Core', 'KDE', 'GNOME', 'XFCE', 'GTK', 'Qt',
    'Motif', 'Java', 'ConsoleOnly',

    # Reserved Categories (not whitelisted)
    # http://standards.freedesktop.org/menu-spec/1.1/apas03.html
    # 'Screensaver', 'TrayIcon', 'Applet', 'Shell',
}

def sanitise_categories(untrusted_value):
    '''Sanitise Categories= entry of desktop file.
    Allow only categories explicitly listed by the specification
    '''
    untrusted_categories = (c.strip() for c in untrusted_value.split(';') if c)
    categories = (c for c in untrusted_categories if c in CATEGORIES_WHITELIST)

    return ';'.join(categories) + ';'

def get_appmenus(vm):
    '''Get appmenus from a *vm*. *vm* can be :py:obj:None to retrieve data
    from stdin - should be a `qubes.GetAppmenus` service in the VM connected
    to it.'''
    appmenus_line_limit_left = appmenus_line_count
    untrusted_appmenulist = []
    if vm is None:
        while appmenus_line_limit_left > 0:
            untrusted_line = sys.stdin.readline(appmenus_line_size)
            if not untrusted_line:
                break
            untrusted_appmenulist.append(untrusted_line.strip())
            appmenus_line_limit_left -= 1
        if appmenus_line_limit_left == 0:
            raise qubesadmin.exc.QubesException("Line count limit exceeded")
    else:
        p = vm.run_service('qubes.GetAppmenus')
        while appmenus_line_limit_left > 0:
            untrusted_line = p.stdout.readline(appmenus_line_size)
            if not untrusted_line:
                break
            try:
                untrusted_line = untrusted_line.decode('ascii')
                untrusted_appmenulist.append(untrusted_line.strip())
            except UnicodeDecodeError:
                # simply ignore non-ASCII lines
                pass
            appmenus_line_limit_left -= 1
        p.wait()
        p.stdout.close()
        if p.returncode != 0:
            raise qubesadmin.exc.QubesException(
                "Error getting application list")
        if appmenus_line_limit_left == 0:
            raise qubesadmin.exc.QubesException("Line count limit exceeded")

    appmenus = {}
    line_rx = re.compile(
        r"([a-zA-Z0-9._-]+?)(?:\.desktop)?:"
        r"([a-zA-Z0-9-]+(?:\[[a-zA-Z@_]+\])?)\s*=\s*(.*)")
    ignore_rx = re.compile(r"\A.*([a-zA-Z0-9._-]+.desktop):(#.*|\s*)\Z")
    for untrusted_line in untrusted_appmenulist:
        # Ignore blank lines and comments
        if not untrusted_line or ignore_rx.match(untrusted_line):
            continue
        # use search instead of match to skip file path
        untrusted_m = line_rx.search(untrusted_line)
        if untrusted_m:
            name = untrusted_m.group(1)
            assert '/' not in name
            assert '\0' not in name

            untrusted_key = untrusted_m.group(2)
            assert '\0' not in untrusted_key
            assert '\x1b' not in untrusted_key
            assert '=' not in untrusted_key

            untrusted_value = untrusted_m.group(3).strip()
            # TODO add key-dependent asserts

            # Look only at predefined keys
            if untrusted_key in fields_regexp:
                if fields_regexp[untrusted_key].match(untrusted_value):
                    # now values are sanitized
                    key = untrusted_key
                    if key == 'Categories':
                        value = sanitise_categories(untrusted_value)
                    else:
                        value = untrusted_value

                    if name not in appmenus:
                        appmenus[name] = {}

                    appmenus[name][key] = value
                else:
                    print("Warning: ignoring key %r of %s" %
                        (untrusted_key, name), file=sys.stderr)
            # else: ignore this key

    return appmenus

def create_template(path, name, values, legacy):
    '''
    Create desktop entry template based on values in `values` and save it to
    `path`.
    :param path: Path where template should be saved
    :param values: dict with values retrieved from VM (as in Desktop Entry
    specification)
    :param legacy: create legacy template, for VM without qubes.StartApp service
    :return: None
    '''
    # check if all required fields are present
    if legacy:
        req_fields = required_fields_legacy
    else:
        req_fields = required_fields
    for key in req_fields:
        if key not in values:
            print("Warning: not creating/updating '%s' "
                  "because of missing '%s' key" % (path, key),
                  file=sys.stderr)
            return

    desktop_entry = ""
    desktop_entry += "[Desktop Entry]\n"
    desktop_entry += "Version=1.0\n"
    desktop_entry += "Type=Application\n"
    desktop_entry += "Terminal=false\n"
    desktop_entry += "X-Qubes-VmName=%VMNAME%\n"
    desktop_entry += "X-Qubes-AppName=" + name + "\n"

    if 'Icon' in values:
        icon_file = os.path.splitext(os.path.split(path)[1])[0] + '.png'
        desktop_entry += "Icon={0}\n".format(os.path.join(
            '%VMDIR%', qubesappmenus.AppmenusSubdirs.icons_subdir, icon_file))
    else:
        desktop_entry += "Icon=%XDGICON%\n"

    for key in ["Name", "GenericName"]:
        if key in values:
            desktop_entry += "{0}=%VMNAME%: {1}\n".format(key, values[key])

    # force category X-Qubes-VM
    values["Categories"] = values.get("Categories", "") + "X-Qubes-VM;"

    for key in ["Comment", "Categories"]:
        if key in values:
            desktop_entry += "{0}={1}\n".format(key, values[key])

    if legacy:
        desktop_entry += "Exec=qvm-run -q -a %VMNAME% -- {0}\n".format(
            shlex.quote(values['Exec']))
        desktop_entry += \
            "X-Qubes-DispvmExec=qvm-run -q -a --dispvm=%VMNAME% -- {0}\n".\
            format(shlex.quote(values['Exec']))
    else:
        # already validated before, but make sure no one will break it
        assert ' ' not in name
        assert ';' not in name
        assert '%' not in name
        desktop_entry += "Exec=qvm-run -q -a --service -- %VMNAME% " \
                         "qubes.StartApp+{}\n".format(name)
        desktop_entry += \
            "X-Qubes-DispvmExec=qvm-run -q -a --service --dispvm=%VMNAME% " \
            "-- qubes.StartApp+{}\n".format(name)
    try:
        with open(path, "r", encoding='utf-8') as path_f:
            existing_desktop_entry = path_f.read()
    except FileNotFoundError:
        existing_desktop_entry = ''
    if desktop_entry != existing_desktop_entry:
        with open(path, "w", encoding='utf-8') as desktop_file:
            desktop_file.write(desktop_entry)

def process_appmenus_templates(appmenusext, vm, appmenus):
    '''Get parsed appmenus and write appmenus templates from them.

    :param appmenusext: AppmenusExtension instance
    :param vm: VM from which appmenus were extracted
    :param appmenus: appmenus dictionary, indexed with entry basename
    '''
    old_umask = os.umask(0o002)

    legacy_appmenus = vm.features.check_with_template(
        'appmenus-legacy', False)

    templates_dir = appmenusext.templates_dirs(vm)[0]
    if not os.path.exists(templates_dir):
        os.makedirs(templates_dir)

    template_icons_dir = appmenusext.template_icons_dirs(vm)[0]
    if not os.path.exists(template_icons_dir):
        os.makedirs(template_icons_dir)

    # Only create Start shortcut for standalone VMs. Otherwise we will use the
    # one from template VM.
    has_qubes_start = vm.klass != 'AppVM'

    if has_qubes_start:
        qubes_start_fname = os.path.join(templates_dir, 'qubes-start.desktop')
        if not os.path.exists(qubes_start_fname):
            with open(qubes_start_fname, 'wb') as qubes_start_f:
                vm.log.info("Creating Start")
                qubes_start_f.write(pkg_resources.resource_string(
                    __name__,
                    'qubes-start.desktop.template'))

    # Do not create reserved Start entry
    appmenus.pop('qubes-start', None)
    for appmenu_name in appmenus.keys():
        appmenu_path = os.path.join(
            templates_dir,
            appmenu_name) + '.desktop'
        if os.path.exists(appmenu_path):
            vm.log.info("Updating {0}".format(appmenu_name))
        else:
            vm.log.info("Creating {0}".format(appmenu_name))

        # TODO: icons support in offline mode
        # TODO if options.offline_mode:
        # TODO     new_appmenus[appmenu_name].pop('Icon', None)
        if 'Icon' in appmenus[appmenu_name]:
            # the following line is used for time comparison
            icondest = os.path.join(template_icons_dir,
                                    appmenu_name + '.png')

            try:
                icon = qubesimgconverter.Image. \
                    get_xdg_icon_from_vm(vm, appmenus[appmenu_name]['Icon'])
                if os.path.exists(icondest):
                    old_icon = qubesimgconverter.Image.load_from_file(icondest)
                else:
                    old_icon = None
                if old_icon is None or icon != old_icon:
                    icon.save(icondest)
            except Exception as e:  # pylint: disable=broad-except
                vm.log.warning('Failed to get icon for {0}: {1!s}'.
                    format(appmenu_name, e))

                if os.path.exists(icondest):
                    vm.log.warning('Found old icon, using it instead')
                else:
                    del appmenus[appmenu_name]['Icon']

        create_template(appmenu_path, appmenu_name,
            appmenus[appmenu_name], legacy_appmenus)

    # Delete appmenus of removed applications
    for appmenu_file in os.listdir(templates_dir):
        if not appmenu_file.endswith('.desktop'):
            continue

        # Keep the Start shortcut
        if has_qubes_start and appmenu_file == 'qubes-start.desktop':
            continue

        if appmenu_file[:-len('.desktop')] not in appmenus:
            vm.log.info("Removing {0}".format(appmenu_file))
            os.unlink(os.path.join(templates_dir,
                appmenu_file))

    os.umask(old_umask)

def retrieve_appmenus_templates(vm, use_stdin=True):
    '''Retrieve appmenus from the VM. If not running in offline mode,
    additionally retrieve application icons and store them into
    :py:metch:`template_icons_dir`.

    Returns: dict of desktop entries, each being dict itself.
    '''
    if not vm.is_running():
        raise qubesadmin.exc.QubesVMNotRunningError(
            "Appmenus can be retrieved only from running VM")

    new_appmenus = get_appmenus(vm if not use_stdin else None)

    return new_appmenus

def main(args=None):
    '''Main function of qvm-sync-appmenus tool'''
    env_vmname = os.environ.get("QREXEC_REMOTE_DOMAIN")

    args = parser.parse_args(args)

    if env_vmname:
        vm = args.app.domains[env_vmname]
    elif not args.domains:
        parser.error("You must specify at least the VM name!")
    else:
        vm = args.domains[0]

    if env_vmname is None or args.force_rpc:
        use_stdin = False
    else:
        use_stdin = True
    appmenusext = qubesappmenus.Appmenus()
    if not args.regenerate_only:
        new_appmenus = retrieve_appmenus_templates(vm, use_stdin=use_stdin)
        if not new_appmenus and vm.klass != "AppVM":
            vm.log.info("No appmenus received, terminating")
        else:
            process_appmenus_templates(appmenusext, vm, new_appmenus)
    appmenusext.appmenus_update(vm)


import pyfiglet
import time
import os
import ctypes
import random

def welcome():
    print(pyfiglet.Figlet(font = 'slant').renderText('     Wallpaper'));

    print("\nWelcome to Wallpaper modifier !");
    print("   Author: Valentin Le Lièvre \n   Version: 0.1 \n   License: MIT \n");

def bye():
    print("Thanks to use wallpaper modifier. Hope to see you soon ! \n");

    time.sleep(2);

def checkIfWindows():
    if os.name != "nt":
            print("\u001b[31m\nYour are not on Windows operating system. Unfortunately this programm does not support other operating system than Windows.\u001b[0m \n");
            return False;
    else:
        return True;

def getInput():
    command = input(">>> ");

    return command;

def excecuteCommand(command):
    if command == "setPath" or command == "setpath":
        setPath(command);
    elif command == "next":
        changeWallpaper();
    elif command == "help":
        help();
    elif command == "quit":
        return False;
    else:
        error(command);

def setPath(command):
    checkIfDataFileAlreadyExist();

    if checkPathAlreadySet() == True:
        print('fd');

    return True;

def checkIfDataFileAlreadyExist():
    if os.path.isfile("data.txt") == False:
        file = open("data.txt", "a+");
        file.close();

        print("\u001b[32mThe data file does not exist or have been deleted. Creating new one ! Your settings are be reset to default make sure you restore your settings.\u001b[0m \n");
    else:
        return True;

def checkPathAlreadySet():
    file = open("data.txt", "r");

    content = file.read();
    data = content.split(';');
    pathData = data[0].split(',');

    file.close();

    if pathData[1] != " ":
        answer = input("\u001b[33mThe path to your wallpaper folder as already been set. Are you sure you want to change it ?\u001b[0m [yes][no]: ");

        if answer == "yes" or answer == "y":
            path = input("\nWhat is the new path of your wallpaper folder? : ");
        else:
            return True;
    else:
        path = input("\nWhat is the path of your wallpaper folder? : ");

    if path[0] == '"':
        path = path.split(path)[1];

    writeInFile([pathData[0], ",", path, ";"]);

    print();
    return True;

def writeInFile(data):
    file = open('data.txt', 'w');

    for i in data:
        file.write(i);

    file.close();

def changeWallpaper():
    file = open("data.txt", "r");

    content = file.read();
    data = content.split(';');
    pathData = data[0].split(',');
    path = pathData[1];

    file.close();

    files = os.listdir(path);

    wallpaperName = files[random.randint(0, len(files) - 1)];
    ctypes.windll.user32.SystemParametersInfoW(20, 0, path + "\\" + wallpaperName, 0);

def help():
    text = ["Welcome in the wallpaper modifier documentation. Type help --function name for more infos about a specific function.", "", "How to use:", " _ <function name> [arguments] --optinal arguments", "", "List of functions:", " _ test [number to print]", " _ quit", ""];

    for i in text:
        print(i);

def error(command):
    print("\u001b[31mThe function: " + command + " does not exist ! Try to correct the function or use 'help' for more information about the avaible functions.\u001b[0m \n");

def main():
    welcome();

    if checkIfWindows() == False:
        return True;

    print("Type help to see command options.");
    while True:
        command = getInput();
        returnedValue = excecuteCommand(command);

        if returnedValue == False:
            bye();
            break

    """for i in range(50):
        print("\u001b[" + str(i) + "mHi, coders. This is pink output.\u001b[0m", " " + str(i));"""

if __name__ == "__main__":
    main();


# Most of the code in this file comes from PBNT by Elliot Cohen. See
# separate file in this project with PBNT license.

import itertools as it

from inference.InferenceEngine import *
from potentials.DiscreteUniPot import *

class EnumerationEngine(InferenceEngine):
    """
    EnumerationEngine is an InferenceEngine that enumerates all (brute force
    method) possible instantiations (aka stories, histories, Feynman paths)
    consistent with the active states of each node.

    Attributes
    ----------
    bnet_ord_nodes : list[BayesNode]

    bnet : BayesNet
    do_print : bool
    is_quantum : bool

    """

    def __init__(self, bnet, do_print=False, is_quantum=False):
        """
        Constructor

        Parameters
        ----------
        bnet : BayesNet
        do_print : bool
        is_quantum : bool

        Returns
        -------

        """
        InferenceEngine.__init__(self, bnet, do_print, is_quantum)
        self.bnet_ord_nodes = list(self.bnet.nodes)

    def get_unipot_list(self, node_list):
        """
        For each node in node_list, this method returns a uni-potential that
        gives the probabilities for the states of that node. Obviously,
        such a PD has the active states of the node as support.

        Parameters
        ----------
        node_list : list[BayesNode]

        Returns
        -------
        list[DiscreteUniPot]

        """
        assert(set(node_list) <= self.bnet.nodes)
        pot_list = [DiscreteUniPot(self.is_quantum, node, bias=0)
                    for node in node_list]
        story_counter = 0
        total_pot_val = 0.
        for cur_story in self.story_generator():
            story_counter += 1
            annotated_story = dict(zip(
                    self.bnet_ord_nodes, cur_story))
            pot_val = self.get_story_potential_val(
                annotated_story)
            total_pot_val += pot_val
            if self.do_print:
                print("[", story_counter, "] pot_val=", pot_val)
                InferenceEngine.print_annotated_story(
                        annotated_story)
                print("\n")
            state_list = [annotated_story[v] for v in node_list]
            for (pot, state) in zip(pot_list, state_list):
                pot[state] += pot_val
        if self.do_print:
            print("tot_pot_val= ", total_pot_val,
                  "# equals 1 if you comment out the evidence")
            print("\n")
        pot_list1 = []
        for pot in pot_list:
            pot.normalize_self()
            if self.is_quantum:
                pot1 = pot.get_probs_from_amps()
            else:
                pot1 = pot
            pot_list1.append(pot1)
        return pot_list1

    def story_generator(self):
        """
        Generate the next possible story constrained by the evidence (i.e.,
        by the active states of each node).

        Returns
        -------
        itertools.product

        """

        x = [node.active_states for node in self.bnet_ord_nodes]
        return it.product(*x)

    def get_story_potential_val(self, annotated_story):
        """
        Given an annotated story (i.e., a dictionary that maps all nodes to
        their current state), it returns a float for CBnets and a complex
        for QBnet. The returned value is the pot value for that particular
        annotated story.

        Parameters
        ----------
        annotated_story : dict[Node, int]

        Returns
        -------
        complex

        """
        pot_val = 1
        for node in self.bnet.nodes:
            pot = node.potential
            states = tuple(annotated_story[v] for v in pot.ord_nodes)
            pot_val *= pot[states]
        return pot_val

from examples_cbnets.HuaDar import *
if __name__ == "__main__":

    bnet = HuaDar.build_bnet()

    # introduce some evidence
    # bnet.get_node_named("D").active_states = [0]
    # bnet.get_node_named("G").active_states = [1]

    inf_eng = EnumerationEngine(bnet, do_print=True)
    id_nums = sorted([node.id_num for node in bnet.nodes])
    node_list = [bnet.get_node_with_id_num(k) for k in id_nums]

    # this is simpler but erratic
    # node_list = list(bnet.nodes)

    pot_list = inf_eng.get_unipot_list(node_list)
    for pot in pot_list:
        print(pot, "\n")


import pandas as pd
import numpy as np
import time
import scipy
from scipy import stats
import bt
import multiprocessing
import matplotlib.pyplot as plt
import datetime as dt

class backend():

    def __init__(self):
        pass

    class indicators():      #fill with different params to initialized
        def moving_average(self, data, n):
            ma = data.rolling(n, min_periods=n).mean()  # N-mean
            return ma

        def macd(self, data, macd_n, macd_m):
            short_sma = data.rolling(macd_n).mean()
            long_sma = data.rolling(macd_m).mean()
            macd = (short_sma - long_sma)
            return (macd)

        def rsi(self, data, rsi_period):
            chg = data.diff(1)
            gain = chg.mask(chg < 0, 0)
            data['gain'] = gain
            loss = chg.mask(chg > 0, 0)
            data['loss'] = loss
            avg_gain = gain.ewm(com=rsi_period - 1, min_periods=rsi_period).mean()
            avg_loss = loss.ewm(com=rsi_period - 1, min_periods=rsi_period).mean()
            data['avg_gain'] = avg_gain
            data['avg_loss'] = avg_loss
            rs = abs(avg_gain / avg_loss)
            rsi = 100 - (100 / (1 + rs))
            return rsi

        def so_cross(self, x, so_n, so_m):  # n is the high or low window
            low = x.rolling(so_n).min()
            high = x.rolling(so_n).max()
            K = 100 * ((x - low) / (high - low))
            D = self.moving_average(K, so_m)  # we use m here to represent the D rolling window
            signal = K - D
            return signal

        def crtdr(self, high, low, close):
            crtdr = (close - low)/(high-low)
            return crtdr

        def atr(self,high, low, close, open):   # extend to take differential with respect to the underlying stock price.
            a = abs(high - low)
            b = abs(high - close)
            c = abs(low - close)
            x = pd.concat([a, b, c], axis=1)
            x['tr'] = x.max(axis=1)
            x['atr'] = x['tr'].rolling(14).mean()
            x['diff'] = x['atr'].diff()
            x['acc'] = x['diff'].diff()
            x['underlying_diff_close'] = x['diff'] / close.diff()
            x['underlying_diff_open'] = x['diff'] / open.diff()
            x['underlying_diff_gap'] = x['diff']/open-close
            return x

        def ma_rsi_3(self, data):
            rsi_3 = self.rsi(data, 3).iloc[:,0]
            ma_rsi_3 = rsi_3.rolling(3).mean()
            return pd.DataFrame(ma_rsi_3)

        def rsi_mom(self, data):
            rsi_14 = self.rsi(data, 14)
            mom = rsi_14 - rsi_14.shift(9)
            return mom

        def composite_rsi(self, data):
            rsi_mom = self.rsi_mom(data)
            rsi_3 = self.ma_rsi_3(data)
            composite = rsi_mom + rsi_3
            composite= composite.dropna()
            #df = pd.concat([rsi_mom, rsi_3], axis=0).rename(['mom', 'rsi_3'])
            return composite

    class get_values():
        def n_sample(self, data, n, i): #declare the data type given and some x-day spacing.
                slice = data.iloc[0:i]    #THIS RETURNS A CORRECT SLICE ALWAYS.
                data = slice.iloc[-1::-n]  #correct n-day slice from the bottom.
                return data[::-1]    #return the slice in the correct datetime order.

        def get_macd(self,data, n, macd_n, macd_m):
            value =[]
            for j in range(len(data)+1):
                y = gv.n_sample(data,n,j)
                d1 = i.macd(y, macd_n, macd_m)
                d1 = d1.tail(1)
                value.append(d1)
            return value

        def get_rsi(self, data, n, rsi_n):
            value = []
            for j in range(len(data)+1):
                y = gv.n_sample(data, n, j)
                d1 = i.rsi(y, rsi_n)
                d1 = d1.tail(1)
                value.append(d1)
            return value

        def get_soma(self, data, n, so_n, so_m):
            value = []
            for j in range(len(data)+1):
                y = gv.n_sample(data, n, j)
                d1 = i.so_cross(y, so_n, so_m)
                d1 = d1.tail(1)
                value.append(d1)
            return value

        def get_crtdr(self, high, low, close):
            x = i.crtdr(high, low, close)
            return x

    class scaling():
        def scaling_distribution(self, data, fc_window, i):
            dist = data[i-fc_window:i]
            dist = pd.DataFrame(dist)               #take window, turn into a matrix
            return np.diag(dist).reshape(-1,1)      #take diagonal of marix to return values as list

        def minmaxscaling(self, data, fc_window, i):
            X_train = self.scaling_distribution(data, fc_window,i)
            curr_value = data[i]
            scaled_val = (curr_value - X_train.min())/(X_train.max() - X_train.min())
            return scaled_val

        def standardization(self, data, fc_window, i):
            X_train = self.scaling_distribution(data, fc_window, i)
            curr_value = data[i]
            scaled_val = (curr_value - X_train.mean()/(X_train.var()))
            return scaled_val

    class get_weights():
        def get_macd_weight(self, data, n, macd_n, macd_m, fc_window):
            weight = []
            macd = gv.get_macd(data, n, macd_n, macd_m)
            for i in range(len(data) + 1):
                if i >= fc_window:
                    value = sc.minmaxscaling(macd, fc_window, i)                  #need flexible way to change from minmax to normalized
                    weight.append(value)
            df = pd.DataFrame(weight).transpose()
            weight = pd.DataFrame(np.diag(pd.DataFrame(weight)), index=df.index)
            return weight

        def get_rsi_weight(self, data, n, rsi_n, fc_window):
            import numpy as np
            weight = []
            rsi = gv.get_rsi(data, n, rsi_n)
            for i in range(len(data)+1):
                if i>=fc_window:
                    value = rsi[i]
                    weight.append(value)
            df = pd.DataFrame(weight).transpose()
            weight = pd.DataFrame(np.diag(pd.DataFrame(weight)), index=df.index)
            return (weight/100)

        def get_soma_weight(self, data, n, so_n, so_m, fc_window):
            weight = []
            macd = gv.get_soma(data, n, so_n, so_m)
            for i in range(len(data) + 1):
                if i >= fc_window:
                    value = sc.minmaxscaling(macd, fc_window, i)
                    weight.append(value)
            df = pd.DataFrame(weight).transpose()
            weight = pd.DataFrame(np.diag(pd.DataFrame(weight)), index=df.index)
            return weight

        def get_all_weights(self, prices, w1, w2):

            final = pd.DataFrame()
            count = 0
            for column in prices:
                count = count + 1
                rsi = gw.get_rsi_weight(prices[column], 1, 14, 252)
                macd = gw.get_macd_weight(prices[column], 5, 30, 60, 500)
                combined = w1*rsi + w2*macd
                print(count)
                if count <= len(prices.columns):
                    final = pd.concat([final, combined], axis=1)
            final.columns = col_names
            return(final)

#### load in our data ####
col_names = ['fxi', 'spy', 'qqq', 'eem', 'ewl', 'ewi', 'ewu', 'ewq', 'ewp', 'ewg', 'ewa', 'iwm', 'ewz']
close = pd.read_csv((r'C:\Users\justi\OneDrive\Haisun Documents\Jobs\Vector\Pyth\backtestv2\data\close1.csv'), parse_dates=True,
                   index_col=0)
volume = pd.read_csv((r'C:\Users\justi\OneDrive\Haisun Documents\Jobs\Vector\Pyth\backtestv2\data\volume1.csv'), parse_dates=True,
                   index_col=0)
open = pd.read_csv((r'C:\Users\justi\OneDrive\Haisun Documents\Jobs\Vector\Pyth\backtestv2\data\open1.csv'), parse_dates=True,
                   index_col=0)
high = pd.read_csv((r'C:\Users\justi\OneDrive\Haisun Documents\Jobs\Vector\Pyth\backtestv2\data\high1.csv'), parse_dates=True,
                   index_col=0)
low =  pd.read_csv((r'C:\Users\justi\OneDrive\Haisun Documents\Jobs\Vector\Pyth\backtestv2\data\low1.csv'), parse_dates=True,
                   index_col=0)

backend = backend()
i = backend.indicators()
gv = backend.get_values()
sc = backend.scaling()
gw = backend.get_weights()
#
print('composite index')
comp = i.composite_rsi(close[['spy']])
rsi = gv.get_rsi(close[['spy']], 1, 14)
print(rsi)
comp.plot(figsize = (20,10))
plt.savefig('_test_composite_index')

'''
#x = get_all_weights(close, 0.5, 0.5)
x = get_all_weights(close, 0.5, 0.5)
y = get_all_weights(close, 0.3, 0.7)
z = get_all_weights(close, 0.25, 0.75)
x.to_csv("Weights/InverseRSI0.5+MACD0.5.csv")
y.to_csv("Weights/InverseRSI0.3+MACD0.7.csv")
z.to_csv("Weights/InverseRSI0.25+MACD0.75.csv")

1. take the first/second order derivative of atr with respect to a change in time and wrt to a change in the value of the underlying.
2. Add threshold on raw RSI and MACD values.
3. time to start scaling risk is due to divergence dependence, between composite index and the price.
   This divergence triggers us to scale down or stop loss.
4. where we price that stop order is atr dependent. ATR gives us an idea of intra-day volatility.
5. 2x ATR length we can set as our stop distant. (this would be enough damage to signal a trend reversal).
6. ATR is high, close-close low, go long gamma!
7. ATR could be a high, close to close low, leading to value uncertainty.
8. When certainty goes down, intra-day swings increase!
9. will incremental news impact the sentiment of market traders.
10. what is our criteria to start trading again?
11. test size of sell-off correlated to size of divergence.
summmer 18 - early 19, blow up spy and algo on to get more detailed line, get it on the everyday side, convert into excel. add indicators underneath and sec weights.
major divergence, delta signal!

12. Dispersion trading....RV trade, buy rv correlation on constituents sell on ETF. Correlation trading...
13. real money is made running position for longer! - net net index doesnt need to go anywhere.
14. carry trading strategy, wing protection.
15. find way to develop macro backdrop - GDP trend, yield curve, inflation/real, some macro indicators to define regime.

for time testing
        #start = time.time()
         # elapsed = (time.time() - start)
       # print("iter number: {0} , time to iter: {1}" , count, elapsed)
'''


# -*- coding: utf-8 -*-
"""
Created on Sun Jan  6 19:24:22 2019

@author: yaoxinzhi
"""

'''
该代码用于ElasticSearch搜索结果形成 按文章共显统计 pair 文件 / 单个药物所有 pair 对统计文件 / 所有 nodes 统计文件*2
 *_spair 是以文章分割的每个文章内共显的Gene-Chemical pair
 *_all_pair 是一个疾病所有检索结果的 Gene-Chemical pair
 *_nodes 是疾病检索结果中所有pubtator注释结果（Gene， Chemical）在pair中出现的次数统计，及nodes的度

 1.24
 添加根据Homo_sapiens.gene_info 添加 gene id 转为 symbol功能
 并且过滤掉pubtator 注释的 非智人 的 gene
'''

import argparse
import os
from itertools import combinations
#from itertools import product
from functools import reduce
import xml.etree.ElementTree as ET
import re

def Result_pair(rf, out, node_a, node_b, index_file, mesh, ebi, supp):
    single_pair = open('{0}/{1}_spair'.format(out, rf.split('/')[-1]), 'w')

    annotation = {}
    tem_a = []
    tem_b = []
    pair_aa = []
    pair_bb = []
    all_pair = {}
    count_a = {}
    count_b = {}

    _index = {}

    with open(index_file) as f:
        line = f.readline()
        for line in f:
            l = line.split('\t')
            _index[l[1]] = l[2]

    _combinations = lambda x, code='&': reduce(lambda x, y: [str(i)+code+str(j) for i in x for j in y], x)

    single_pair.write('# aa means {0}-{0}, bb means {1}-{1}, ab means {0}-{1}\n'.format(node_a, node_b))

    with open(rf) as f:
        r = 0
        for line in f:
            r += 1
            if r%30000 == 0:
                print(r)

            _json = eval(line)
            pmid = _json['id']
            anns = _json['annotation'].split('&')

            for ann in anns:
                ann = ann.split('|')
                if ann != ['']:
                    if ann[3] not in annotation.keys():
                        annotation[ann[3]] = []
                        annotation[ann[3]].append((ann[2],ann[4]))
                    else:
                        annotation[ann[3]].append((ann[2],ann[4]))

# gene id
            if node_a in annotation.keys():
                for a in annotation[node_a]:
                    a = a[1].split(';')
                    for _a in a:
                        if _a != '':
                            _a = re.sub(u"\\(.*?\\)", "",_a)

                            if _index.get(_a) != None:
                                tem_a.append(_a)
                                if count_a.get(_a) == None:
                                    count_a[_a] = 1
                                else:
                                    count_a[_a] += 1

#chemical
            if node_b in annotation.keys():
                for b in annotation[node_b]:
# chemical id
                    b = b[1].split(';')
                    for _b in b:
                        if _b != '':
                            tem_b.append(_b)
                            if count_b.get(_b) == None:
                                count_b[_b] = 1
                            else:
                                count_b[_b] += 1

#chemical name
#                    _b = str.lower(b[0])
#                    if _b != '':
#                        tem_b.append(_b)
#                        if count_b.get(_b) == None:
#                            count_b[_b] = 1
#                        else:
#                            count_b[_b] += 1

            single_pair.write('pmid: {0}\n'.format(pmid))

# 自由组合
            tmp_dir = {}
            if len(tem_a) != 0:
                pair_aa = list(combinations(tem_a, 2))
                for aa in pair_aa:
                    l = list(aa)
                    l.sort()
                    aa = tuple(l)
                    key = (aa, 'Gene-Gene')
                    if key not in tmp_dir.keys():
                        tmp_dir[key] = 1
                    else:
                        tmp_dir[key] += 1
                    if key[0][0] != key[0][1]:
                        if all_pair.get(key) == None:
                            all_pair[key] = 1
                        else:
                            all_pair[key] += 1
                for k in tmp_dir.keys():
                    single_pair.write('{0}\t{1}\t{2}\t{3}\n'.format(k[0][0], k[0][1], k[1], tmp_dir[k]))
            else:
                pair_aa = []
            tmp_dir = {}

            if len(tem_b) != 0:
                pair_bb = list(combinations(tem_b, 2))
                for bb in pair_bb:
                    l = list(bb)
                    l.sort()
                    bb = tuple(l)
                    key = (bb, 'Chemaical-Chemical')
                    if key not in tmp_dir.keys():
                        tmp_dir[key] = 1
                    else:
                        tmp_dir[key] += 1
                    if key[0][0] != key[0][1]:
                        if all_pair.get(key) == None:
                            all_pair[key] = 1
                        else:
                            all_pair[key] += 1
                for k in tmp_dir.keys():
                        single_pair.write('{0}\t{1}\t{2}\t{3}\n'.format(k[0][0], k[0][1], k[1], tmp_dir[k]))
            else:
                pair_bb = []
            tmp_dir = {}

            if len(tem_b) != 0 and len(tem_a) != 0:
                for ab in _combinations([tem_a, tem_b]):
                    key = (tuple((ab.split('&')[0], ab.split('&')[1])), 'Gene-Chemical')
                    if key not in tmp_dir.keys():
                        tmp_dir[key] = 1
                    else:
                        tmp_dir[key] += 1
                    if key[0][0] != key[0][1]:
                        if all_pair.get(key) == None:
                            all_pair[key] = 1
                        else:
                            all_pair[key] += 1
                for k in tmp_dir.keys():
                    single_pair.write('{0}\t{1}\t{2}\t{3}\n'.format(k[0][0], k[0][1], k[1], tmp_dir[k]))
            tmp_dir = {}
            single_pair.write('\n')

            tem_a = []
            tem_b = []
            annotation = {}
    print(r)

# Chemical_id to name
# 正在将 chemical 和 gene id 转为 name
# 读取 MeSH database
    mesh_index = {}
    tree = ET.parse(mesh)
    root = tree.getroot()
    for record in root.findall('DescriptorRecord'):
        k = record.find('DescriptorUI').text
        for string in record.find('DescriptorName'):
            v = string.text
        mesh_index[k] = v

# 读取 CHEBI database
    ebi_index = {}
    with open(ebi) as f:
        line = f.readline()
        for line in f:
            l = line.split('\t')
            ebi_id = l[2][6:]
            name = l[5]
            if name != 'null':
                ebi_index[ebi_id] = name

# 读取 suplementary term
    supp_index = {}
    tree_supp = ET.parse(supp)
    root = tree_supp.getroot()
    for record in root.findall('SupplementalRecord'):
        k = record.find('SupplementalRecordUI').text
        for string in record.find('SupplementalRecordName'):
            v = string.text
        supp_index[k] = v

# 写文件
    all_pair_file = '{0}/{1}_all_pair'.format(out, rf.split('/')[-1])
    file_node_a = '{0}/{1}_{2}_nodes'.format(out, rf.split('/')[-1], node_a)
    file_node_b = '{0}/{1}_{2}_nodes'.format(out, rf.split('/')[-1], node_b)
#    gene_list = '{0}/{1}_GeneList.txt'.format(out, rf.split('/')[-1])
    GeneSymbol = '{0}/{1}_GeneSymbol'.format(out, rf.split('/')[-1])
    ChemicalName = '{0}/{1}_ChemicalName'.format(out, rf.split('/')[-1])
#    mismatch = open('{0}/{1}_ChemicalName_mismatch'.format(out, rf.split('/')[-1]), 'w')

    with open(all_pair_file, 'w') as wf:
        wf.write('node_a\tnode_b\tinteracts\tedge_count\n')
        for k in sorted(all_pair, key=lambda x: all_pair[x], reverse=True):
            wf.write('{0}\t{1}\t{2}\t{3}\n'.format(k[0][0], k[0][1], k[1], all_pair[k]))
    with open(file_node_a, 'w') as wf:
        wf.write('id\tnode_type\tcount\n')
        for k in sorted(count_a, key=lambda x: count_a[x], reverse=True):
            wf.write('{0}\t{1}\t{2}\n'.format(k, node_a, count_a[k]))
    with open(file_node_b,'w') as wf:
        wf.write('id\tnode_type\tcount\n')
        for k in sorted(count_b, key=lambda x: count_b[x], reverse=True):
            wf.write('{0}\t{1}\t{2}\n'.format(k,node_b, count_b[k]))
    with open(ChemicalName, 'w') as wf:
        wf.write('id\tchemical_name\n')
        for _id in count_b.keys():
            _name = ' '
            if ':' in _id:
                _id = _id.split(':')[1]
            if mesh_index.get(_id) != None:
                _name = mesh_index[_id]
            if ebi_index.get(_id) != None:
                _name = ebi_index[_id]
            if supp_index.get(_id) != None:
                _name = supp_index[_id]
#            if _name == ' ':
#                mismatch.write('{0}\n'.format(_id))
            wf.write('{0}\t{1}\n'.format(_id, _name))

#    mismatch.close()
    single_pair.close()
    with open(GeneSymbol, 'w') as f:
        f.write('id\tGeneSymbol')
        for _id in count_a.keys():
            if _index.get(_id) != None:
                f.write('{0}\t{1}\n'.format(_id, _index[_id]))

#    with open(gene_list, 'w') as wf:
#        for k in count_a.keys():
#            _k = re.sub(u"\\(.*?\\)", "",k)
#            wf.write('{0}\n'.format(_k))

# 分别截取前100, 50行用于做图
    os.system('head -101 {0} > {0}_100'.format(all_pair_file))
    os.system('head -51 {0} > {0}_50'.format(all_pair_file))

# 汇总所有结点的 度 用于绘图
    os.system('cat {0} {1} > {2}/nodes_count'.format(file_node_a, file_node_b, out))
    print ('Done')

if __name__ == '__main__':
    parse = argparse.ArgumentParser()
    parse.add_argument('-r', '--rfile', help='data source, default: ../result/search_pubmed_index_Hepatitis_B',
                       dest='rf', required=False, default='../result/search_pubmed_index_Human_cytomegalovirus_infection')
    parse.add_argument('-o', '--outpath', help='save path, defaule ../result',
                        dest='out', required=False, default='../result')

    parse.add_argument('-a', '-pair_a', help=' a nodes name, default: Gene, Optional: "SNP", "Disease", "Gene", "Chemical", "Species"',
                       dest='a', required=False, default='Gene')
    parse.add_argument('-b', '-pair_b', help=' b nodes name, default: Chemical, Optional: "SNP", "Disease", "Gene", "Chemical", "Species"',
                       dest='b', required=False, default='Chemical')

    parse.add_argument('-i', '--IndexFile', help='gene_id index file, default: ../ncbi/Homo_sapiens.gene_info',
                       dest='IndexFile', required=False, default='../ncbi/Homo_sapiens.gene_info')

    parse.add_argument('-e', '--ebi', help='EBI database, default: ../ncbi/compounds.tsv',
                       dest='EBI', required=False, default='../ncbi/compounds.tsv')
    parse.add_argument('-m', '--mesh', help='MeSH database, default: ../ncbi/desc2019',
                       dest='MeSH', required=False, default='../ncbi/desc2019')
    parse.add_argument('-s', '--supp', help='MeSH database, default: ../ncbi/supp2015',
                       dest='supp', required=False, default='../ncbi/supp2015')
    args = parse.parse_args()

    if not os.path.exists(args.out):
        os.system('madir {0}'.format(args.out))

    Result_pair(args.rf, args.out, args.a, args.b, args.IndexFile, args.MeSH, args.EBI, args.supp)


# Copyright 2013-2020 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

from spack import *

class RocmOpencl(CMakePackage):
    """OpenCL: Open Computing Language on ROCclr"""

    homepage = "https://github.com/RadeonOpenCompute/ROCm-OpenCL-Runtime"
    url      = "https://github.com/RadeonOpenCompute/ROCm-OpenCL-Runtime/archive/roc-3.5.0.tar.gz"

    maintainers = ['srekolam', 'arjun-raj-kuppala']

    version('3.5.0', sha256='511b617d5192f2d4893603c1a02402b2ac9556e9806ff09dd2a91d398abf39a0')
    depends_on('cmake@3.5.2', type='build')
    depends_on('rocclr@3.5.0:', type='build', when='@3.5.0:')
    depends_on('comgr@3.5.0:', type='build', when='@3.5.0:')
    depends_on('mesa~llvm@18.3:', type='link', when='@3.5:')

    resource(name='rocclr',
             url='https://github.com/ROCm-Developer-Tools/ROCclr/archive/roc-3.5.0.tar.gz',
             sha256='87c1ee9f02b8aa487b628c543f058198767c474cec3d21700596a73c028959e1',
             expand=True,
             destination='',
             placement='rocclr-src')

    def cmake_args(self):
        args = ['-DLIBROCclr_STATIC_DIR={}/../rocclr_build'.
                format(self.stage.path),
                '-DUSE_COMGR_LIBRARY=yes',
                '-DCMAKE_BUILD_WITH_INSTALL_RPATH=TRUE',
                '-DCMAKE_SKIP_BUILD_RPATH=TRUE',
                '-DROCclr_DIR={}/rocclr-src'.format(self.stage.source_path)
                ]
        return args


class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class SinglyLinkedList:
    def __init__(self):
        self.head = None

    # TC---->O(n)
    def sizeof(self):
        count = 0
        temp = self.head
        while temp is not None:
            count += 1
            temp = temp.next
        print('Size of Linked List = ', count)

ll = SinglyLinkedList()
ll.head = Node(1)
second = Node(2)
third = Node(3)
four = Node(4)
five = Node(5)
ll.head.next = second
second.next = third
third.next = four
four.next = five
ll.sizeof()


#
# Copyright (C) 2017-2020 Tactical Computing Laboratories, LLC
# All Rights Reserved
# contact@tactcomplabs.com
#
# See LICENSE in the top level directory for licensing details
#
# rev-pan-test.py
#
# ---------------------------------------------------------------
#
#    host0                  pan1
#      |                     |
#     nic0                  nic1
#      |                     |
#    iface0 <-> router <-> iface1
#                  |
#               topology
#
#   <-> is a link
#   | is a subcomponent relationship
#

import os
import sst

# Define SST core options
sst.setProgramOption("timebase", "1ps")
sst.setProgramOption("stopAtCycle", "0s")

# Tell SST what statistics handling we want
sst.setStatisticLoadLevel(4)

max_addr_gb = 1

# Define the simulation components
#-- HOST CPU
host_cpu0 = sst.Component("cpu0", "revcpu.RevCPU")
host_cpu0.addParams({
	"verbose" : 5,                                # Verbosity
        "numCores" : 1,                               # Number of cores
	"clock" : "1.0GHz",                           # Clock
        "memSize" : 1024*1024*1024,                   # Memory size in bytes
        "machine" : "[0:RV64G]",                      # Core:Config; RV64G for core 0
        "startAddr" : "[0:0x00000000]",               # Starting address for core 0
        "memCost" : "[0:1:10]",                       # Memory loads required 1-10 cycles
        "program" : os.getenv("REV_EXE", "pan_test.exe"),  # Target executable
        "pan_nic" : "revcpu.PanNet",                  # Use the PAN NIC
        "enable_pan" : 1,                             # Enable the internal RevNIC
        "enable_test" : 0,                            # Disable the PAN test harness
        "enable_pan_stats" : 1,                       # Enable the PAN statistics
        "testIters" : 10,                             # Number of command packets for each test
        "msgPerCycle" : 5,                            # Number of messages per cycle
        "RDMAPerCycle" : 1,                           # Number of RDMA messages to flush to network per cycle
        "splash" : 1                                  # Display the splash message
})

#-- PAN CPU
pan_cpu1 = sst.Component("cpu1", "revcpu.RevCPU")
pan_cpu1.addParams({
	"verbose" : 5,                                # Verbosity
        "numCores" : 1,                               # Number of cores
	"clock" : "1.0GHz",                           # Clock
        "memSize" : 1024*1024*1024,                   # Memory size in bytes
        "machine" : "[0:RV64GP]",                     # Core:Config; RV64GP for core 0
        "startAddr" : "[0:0x00000000]",               # Starting address for core 0
        "memCost" : "[0:1:10]",                       # Memory loads required 1-10 cycles
        "program" : os.getenv("REV_EXE", "pan_spin.exe"),  # Target executable
        "pan_nic" : "revcpu.PanNet",                  # Use the PAN NIC
        "enable_pan" : 1,                             # Enable the internal RevNIC
        "enable_test" : 0,                            # Disable the PAN test harness
        "enable_pan_stats" : 1,                       # Enable the PAN statistics
        "msgPerCycle" : 5,                            # Number of messages per cycle
        "RDMAPerCycle" : 1,                           # Number of RDMA messages to flush to network per cycle
        "splash" : 1                                  # Display the splash message
})

# setup the NICs
nic0 = host_cpu0.setSubComponent("pan_nic", "revcpu.PanNet")  #-- host
nic1 = pan_cpu1.setSubComponent("pan_nic", "revcpu.PanNet")  #-- pan

iface0 = nic0.setSubComponent("iface", "merlin.linkcontrol")  #-- host
iface1 = nic1.setSubComponent("iface", "merlin.linkcontrol")  #-- pan

# setup the router
router = sst.Component("router", "merlin.hr_router")
router.setSubComponent("topology", "merlin.singlerouter")

# set the network params
#-- host
host_verb_params = {
  "verbose" : 6,
  "host_device" : 1
}

#-- pan
pan_verb_params = {
  "verbose" : 6,
  "host_device" : 0
}

#-- host network config
host_net_params = {
  "input_buf_size" : "512B",
  "output_buf_size" : "512B",
  "link_bw" : "1GB/s"
}

#-- pan network config
pan_net_params = {
  "input_buf_size" : "512B",
  "output_buf_size" : "512B",
  "link_bw" : "10GB/s"
}

nic0.addParams(host_verb_params) #-- host
nic1.addParams(pan_verb_params) #-- pan

iface0.addParams(host_net_params) #-- host
iface1.addParams(pan_net_params)  #-- pan
router.addParams(pan_net_params)  #-- pan router

router.addParams({
    "xbar_bw" : "10GB/s",
    "flit_size" : "32B",
    "num_ports" : "2",
    "id" : 0
})

# setup the links
link0 = sst.Link("link0")
link0.connect( (iface0, "rtr_port", "1ms"), (router, "port0", "1ms") )

link1 = sst.Link("link1")
link1.connect( (iface1, "rtr_port", "1ms"), (router, "port1", "1ms") )

sst.setStatisticOutput("sst.statOutputCSV")
sst.enableAllStatisticsForAllComponents()

# EOF


import pytest
from driverlicense.testing import AgofTesting

@pytest.fixture(scope="module")
def ag():
    return AgofTesting()

def test_size(ag):
    data = ag.get_data()
    assert 90 == len(data)

def test_df(ag):
    df = ag.process_data(n=10)
    columns = ['Analyse', 'Fälle (ungew) auf Basis ZG/Vorfilter', 'Grundgesamtheit',
    'Kontakte  Mio', 'Kontakte Mio', 'Medientyp', 'Studien- teil',
    'Studienteil', 'Titel', 'Ungew Fälle  auf Basis  ZG/Vorfilter',
    'Ungew Fälle auf Basis Gesamt 10+', 'Ungew Fälle auf Basis Gesamt 16+',
    'Ungew Fälle auf Basis ZG/Vorter', 'Unique User  %',
    'Unique User  Mio', 'Unique User %', 'Unique User Mio', 'Vorfilter',
    'Zeitraum', 'Zielgruppe']

    assert all([a == b for a, b in zip(columns, df.columns)])


class Solution:
    def numDistinct(self, s: str, t: str) -> int:
        # dp[i][j]: number of subseq of s[:i] and t[:j]
        dp = [[0 for _ in range(len(t)+1)] for _ in range(len(s)+1)]
        for i in range(len(s)+1):
            dp[i][0] = 1
        for i in range(1, len(s)+1):
            for j in range(1, len(t)+1):
                if s[i-1] == t[j-1]:
                    dp[i][j] = dp[i-1][j] + dp[i-1][j-1]
                else:
                    dp[i][j] = dp[i-1][j]
        return dp[-1][-1]


import datetime as dt
import logging
import time
from contextlib import suppress

from funcy import (
    compose,
    lpluck,
    lkeep,
    flatten,
    merge_with,
    keep,
    lfilter,
    silent,
)
from pymongo import UpdateOne
from pymongo.errors import DuplicateKeyError
from steem import Steem
from steem.blockchain import Blockchain
from steemdata.utils import (
    json_expand,
    typify,
)
from toolz import partition_all

from methods import (
    update_account,
    update_account_ops,
    update_account_ops_quick,
    parse_operation,
    get_comment,
)
from mongostorage import Indexer, Stats
from utils import (
    fetch_price_feed,
    get_usernames_batch,
    strip_dot_from_keys,
    thread_multi,
)

from accounts import myAccounts

logging.basicConfig(filename='scraper.log',level=logging.INFO)
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)

# Operations
# ----------
def scrape_operations(mongo):
    """Fetch all operations (including virtual) from last known block forward."""
    indexer = Indexer(mongo)
    last_block = indexer.get_checkpoint('operations')
    log.info('\n> Fetching operations, starting with block %d...' % last_block)

    blockchain = Blockchain(mode="irreversible")
    history = blockchain.history(
        start_block=last_block,
    )
    for operation in history:
        # insert operation
        with suppress(DuplicateKeyError):
            transform = compose(strip_dot_from_keys, json_expand, typify)
            mongo.Operations.insert_one(transform(operation))

        # if this is a new block, checkpoint it, and schedule batch processing
        if operation['block_num'] != last_block:
            last_block = operation['block_num']
            indexer.set_checkpoint('operations', last_block - 1)

            if last_block % 10 == 0:
                log.info("Checkpoint: %s (%s)" % (
                    last_block,
                    blockchain.steem.hostname
                ))

# Posts, Comments
# ---------------

# Accounts, AccountOperations
# ---------------------------
def scrape_all_users(mongo, quick=False):
    """
    Scrape all existing users
    and insert/update their entries in Accounts collection.

    Ideally, this would only need to run once, because "scrape_accounts"
    takes care of accounts that need to be updated in each block.
    """
    steem = Steem()
    indexer = Indexer(mongo)

    account_checkpoint = indexer.get_checkpoint('accounts')
    usernames = myAccounts()
    # if account_checkpoint:
    #     usernames = list(get_usernames_batch(account_checkpoint, steem))
    # else:
    #     usernames = list(get_usernames_batch(steem))

    for username in usernames:
        log.info('Updating @%s' % username)
        update_account(mongo, username, load_extras=True)
        if quick:
            update_account_ops_quick(mongo, username)
        else:
            update_account_ops(mongo, username)
        indexer.set_checkpoint('accounts', username)
        log.info('Updated @%s' % username)

    # this was the last batch
    if account_checkpoint and len(usernames) < 1000:
        indexer.set_checkpoint('accounts', -1)

# Posts, Comments, Accounts, AccountOperations
# --------------------------------------------
def post_processing(mongo, batch_size=100, max_workers=50):
    indexer = Indexer(mongo)
    start_block = indexer.get_checkpoint('post_processing')

    query = {
        "block_num": {
            "$gt": start_block,
            "$lte": start_block + batch_size,
        }
    }
    projection = {
        '_id': 0,
        'body': 0,
        'json_metadata': 0,
    }
    results = list(mongo.Operations.find(query, projection=projection))
    batches = map(parse_operation, results)

    # handle an edge case when we are too close to the head,
    # and the batch contains no work to do
    if not results and is_recent(start_block, days=1):
        return

    # squash for duplicates
    def custom_merge(*args):
        return list(set(keep(flatten(args))))

    batch_items = merge_with(custom_merge, *batches)

    # only process accounts if the blocks are recent
    # scrape_all_users should take care of stale updates
    if is_recent(start_block, days=10):
        accounts = set(batch_items['accounts_light'] +
                       batch_items['accounts'])
        list(thread_multi(
            fn=update_account,
            fn_args=[mongo, None],
            dep_args=list(accounts),
            fn_kwargs=dict(load_extras=False),
            max_workers=max_workers,
            re_raise_errors=False,
        ))
        list(thread_multi(
            fn=update_account_ops_quick,
            fn_args=[mongo, None],
            dep_args=list(accounts),
            fn_kwargs=None,
            max_workers=max_workers,
            re_raise_errors=False,
        ))

    index = max(lpluck('block_num', results))
    indexer.set_checkpoint('post_processing', index)

    log.info("Checkpoint: %s - %s accounts (+%s full)" % (
        index,
        len(batch_items['accounts_light']),
        len(batch_items['accounts']),
    ))

# Blockchain
# ----------
def scrape_blockchain(mongo):
    s = Steem()
    # see how far behind we are
    missing = list(range(last_block_num(mongo), s.last_irreversible_block_num))

    # if we are far behind blockchain head
    # split work in chunks of 100
    if len(missing) > 100:
        for batch in partition_all(100, missing):
            results = s.get_blocks(batch)
            insert_blocks(mongo, results)

    # otherwise continue as normal
    blockchain = Blockchain(mode="irreversible")
    hist = blockchain.stream_from(start_block=last_block_num(mongo), full_blocks=True)
    insert_blocks(mongo, hist)

def insert_blocks(mongo, full_blocks):
    for block in full_blocks:
        if not block.get('block_num'):
            block['block_num'] = int(block['block_id'][:8], base=16)

        if block['block_num'] > 1:
            assert block_id_exists(mongo, block['previous']), \
                'Missing Previous Block (%s)' % block['previous']

        with suppress(DuplicateKeyError):
            mongo.db['Blockchain'].insert_one(block)

def block_id_exists(mongo, block_id: str):
    # covered query
    return mongo.db['Blockchain'].find_one(
        {'block_id': block_id}, {'_id': 0, 'block_id': 1})

def last_block_num(mongo) -> int:
    last_block = mongo.db['Blockchain'].find_one(
        filter={},
        projection={'_id': 0, 'block_num': 1},
        sort=[('block_num', -1)]
    )
    return last_block.get('block_num', 1) if last_block else 1

def is_recent(block_num, days):
    head_block_num = Steem().steemd.head_block_number
    return block_num > head_block_num - 20 * 60 * 24 * days

# Misc
# ----
def refresh_dbstats(mongo):
    while True:
        Stats(mongo).refresh()
        time.sleep(60)

def scrape_prices(mongo):
    """ Update PriceHistory every hour.
    """
    while True:
        prices = fetch_price_feed()
        mongo.PriceHistory.insert_one(prices)
        time.sleep(60 * 5)

def run():
    from mongostorage import MongoStorage
    from steemdata.helpers import timeit
    m = MongoStorage()
    m.ensure_indexes()
    # with timeit():
        # scrape_operations(m)
        # scrape_comments(m)
        # post_processing(m)
        # update_account(m, 'furion', load_extras=True)
        # update_account_ops(m, 'furion')
        # scrape_all_users(m, False)
        # validate_operations(m)

if __name__ == '__main__':
    with suppress(KeyboardInterrupt):
        run()


from ring import RingInt

class Series:

    def __init__(self, k, x, n):
        k, x, n = int(k), int(x), int(n)
        self.k, self.n = k, n
        self.X = RingInt(x, n)
        self.I = RingInt(0, n)
        self.N = RingInt(1, n)  # N stands for next

    def __iter__(self):
        return self

    def __next__(self):
        if self.I.value >= self.k:
            raise StopIteration
        elif self.I.value == 0:
            self.I.value += 1
            return self.N
        else:
            self.N *= (self.X / self.I)
            self.I.value += 1
            return self.N

if __name__ == "__main__":
    in_str = str(input())
    in_list = in_str.split(' ')
    k, x, n = in_list[0], in_list[1], in_list[2]

    for ele in Series(k, x, n):
        print(ele)


from django import template
from django.conf import settings
from django.template.defaultfilters import date
from django.utils.timezone import localtime

register = template.Library()

@register.inclusion_tag('utils/tags/show_time.html')
def show_time(time, time_display=None, pubdate=False, itemprop=None):
    context = {'time': time, 'pubdate':pubdate}
    if time_display:
        context['time_display'] = time_display
    else:
        context['time_display'] = date(localtime(time), settings.DATETIME_FORMAT) if time else ""
    context['itemprop'] = itemprop
    return context


from django.urls import path

from . import views
#from . import middleware2

#detailing the name of the app this is valid for prevents cross referencing a project with the same view name ("detail", "inex", etc).
app_name = 'polls'
# the "Shortcut" way to do it:
urlpatterns = [

    path('', views.IndexView.as_view(), name='index'),
    path('set_timezone/', views.set_timezone, name='set_timezone'),
    path('<int:pk>/', views.DetailView.as_view(), name='detail'),
    path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
    path('<int:question_id>/vote/', views.vote, name='vote'),
]

# the "hard" way to do it:
#urlpatterns = [
#    # ex: /polls/
#    path('', views.index, name='index'),
    # ex: /polls/5/
#    #also, this serves as the "name" value as called by the {% url %} template tag.. "detail" in this case.
#    path('<int:question_id>/', views.detail, name='detail'),
    # ex: /polls/5/results/
#    path('<int:question_id>/results/', views.results, name='results'),
    # ex: /polls/5/vote/
#    path('<int:question_id>/vote/', views.vote, name='vote'),
#]


import time
import gym
import numpy as np
import random
import torch
from gym import wrappers
from gym_unity.envs import UnityToGymWrapper
from mlagents_envs.environment import UnityEnvironment
from mlagents_envs.side_channel.engine_configuration_channel import EngineConfigurationChannel
from src.commons import NormalizedActions, set_seed_everywhere, WandbLogger, get_yaml_args, get_remaining_time
from src.td3_per import Agent
import pandas as pd

if __name__ == '__main__':
    method = 'td3_per'
    mode = 'test'

    conf = get_yaml_args(method, mode)

    # Seeding
    seed = conf.seed
    no_graphics = conf.no_graphics
    environment_name = conf.environment_name
    fast_forward = conf.fast_forward
    environment_folder = conf.environment_folder

    deterministic = conf.deterministic

    action_dim = conf.action_dim
    state_dim  = conf.state_dim

    # Hyper parameters
    episodes = conf.episodes
    batch_size = conf.batch_size
    hidden_size = conf.hidden_size
    reward_scale = conf.reward_scale
    action_range = conf.action_range
    explore_noise_scale = conf.explore_noise_scale
    policy_target_update_interval = conf.policy_target_update_interval
    update_itr = conf.update_itr
    eval_noise_scale = conf.eval_noise_scale
    critic_lr = conf.critic_lr
    policy_lr = conf.policy_lr

    # Device
    if conf.device == 'cpu':
        device = conf.device
    else:
        device = 'cuda:0' if torch.cuda.is_available() else 'cpu'

    # wandb
    if conf.wandb_log:
        LOGGER = WandbLogger(
                    project=conf.proj,
                    name=conf.name,
                    entity=conf.entity
                )

    # Set Seed
    set_seed_everywhere(conf.seed)

    # Configure Environment
    channel = EngineConfigurationChannel()
    channel.set_configuration_parameters(time_scale = fast_forward)
    unity_env = UnityEnvironment(
        f'./{environment_folder}/{environment_name}/{environment_name}',
        side_channels=[channel], no_graphics=no_graphics, seed=seed)
    env = UnityToGymWrapper(unity_env)

    # Agent
    agent = Agent(
        state_dim=state_dim,
        batch_size=batch_size,
        hidden_size=hidden_size,
        action_dim=action_dim,
        action_range=action_range,
        reward_scale=reward_scale,
        device=device,
        policy_target_update_interval=policy_target_update_interval,
        method=method,
        policy_lr=policy_lr,
        critic_lr=critic_lr
    )

    if conf.pretrain:
        agent.load_models()

    try:
        for episode in range(2):
            observation = env.reset()
            done = False
            score = 0
            while not done:
                action = agent.choose_action(
                    observation,
                    deterministic=deterministic,
                    explore_noise_scale=explore_noise_scale,
                    test=True
                )

                observation_, reward, done, info = env.step(action)
                score += reward
                observation = observation_
            print(f"########## Episode {episode}")
            print(f"\tCumulative score: {score}")
    except KeyboardInterrupt:
        print("Exited!")
    # Safely close the environment
    env.close()


from django.core.management import BaseCommand

from planner.models import *

class Command(BaseCommand):
    help = "find duplicate faculty in iChair db"

    def handle(self, *args, **options):

        subjects = [subj.abbrev for subj in Subject.objects.all()]

        subj_unique = []

        for subject in subjects:
            if subject not in subj_unique:
                subj_unique.append(subject)
        print(subj_unique)
        print(len(subj_unique))

        for abbrev in subj_unique:
            subjects = Subject.objects.filter(abbrev = abbrev)
            if len(subjects) > 1:
                for s in subjects:
                    num_offerings = 0
                    for course in s.courses.all():
                        num_offerings+=len(course.offerings.all())
                    print(s, ' - ', num_offerings, ' ', s.department, '(id: '+str(s.id)+')')


from flask import Flask
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

class Routes(Resource):
    def get(self):
        return {'foo': 'foo'}

api.add_resource(Routes, '/')

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


# клиент отправляет запрос серверу;
# сервер отвечает соответствующим кодом результата.
# Клиент и сервер должны быть реализованы в виде отдельных скриптов, содержащих соответствующие функции.
# Функции сервера:
#   принимает сообщение клиента;
#   формирует ответ клиенту;
#   отправляет ответ клиенту;
#       имеет параметры командной строки:
#       -p <port> — TCP-порт для работы (по умолчанию использует 7777);
#       -a <addr> — IP-адрес для прослушивания (по умолчанию слушает все доступные адреса).

from socket import *
import time
import pickle
import argparse

# для параметров
def createParser():
    parser = argparse.ArgumentParser()
    parser.add_argument('-p', '--port', default=7777, type=int)
    parser.add_argument('-a', '--addr', default='')
    return parser

if __name__ == '__main__':
    parser = createParser()
    namespace = parser.parse_args()

    s = socket(AF_INET, SOCK_STREAM)
    s.bind((namespace.addr, namespace.port))
    print(f'слушаем {namespace.port} порт')
    s.listen(5)

    while True:
        client, addr = s.accept()   # Принять запрос на соединение
        data = client.recv(1024)
        print(pickle.loads(data))
        responce = {
            'responce': 200,
            'alert': 'Привет от Сервера!'
        }
        client.send(pickle.dumps(responce))
        client.close();


#!/usr/bin/env python

import sqlite3

"""
QUERIES FOR THE RPG DATABASE
"""
# Connecting and importing the database
conn = sqlite3.connect('rpg_db.sqlite3')
curs = conn.cursor()

"""
QUERIES
"""

# How many total Characters are there?
chars = curs.execute("""
SELECT COUNT(*) FROM charactercreator_character;"""
).fetchall()[0][0]
print('Total number of characters:', chars)

# How many of each specific subclass?
clerics = curs.execute("""
SELECT COUNT(*) FROM charactercreator_cleric;"""
).fetchall()[0][0]
print('Total number of clerics:', clerics)

fighters = curs.execute("""
SELECT COUNT(*) FROM charactercreator_fighter;"""
).fetchall()[0][0]
print('Total number of fighters:', fighters)

mages = curs.execute("""
SELECT COUNT(*) FROM charactercreator_mage;"""
).fetchall()[0][0]
print('Total number of mages:', mages)

necros = curs.execute("""
SELECT COUNT(*) FROM charactercreator_necromancer;"""
).fetchall()[0][0]
print('Total number of necromancers:', necros)

thieves = curs.execute("""
SELECT COUNT(*) FROM charactercreator_thief;"""
).fetchall()[0][0]
print('Total number of thieves:', thieves)

# How many total Items?
items = curs.execute("""
SELECT COUNT(*) FROM armory_item;"""
).fetchall()[0][0]
print('Total number of items:', items)

# How many of the Items are weapons? How many are not?
weapons = curs.execute("""
SELECT COUNT(*) FROM armory_weapon;"""
).fetchall()[0][0]
print('Total number of weapons:', weapons)
print('Total number of non-weapons:', (items - weapons))

# How many Items and weapons does each character have? (Return first 20 rows)
char_items = ("""
SELECT COUNT (*)
FROM charactercreator_character
JOIN charactercreator_character_inventory
ON charactercreator_character.character_id = charactercreator_character_inventory.character_id
GROUP BY charactercreator_character_inventory.character_id;"""
)

char_weapons = ("""
SELECT COUNT (*)
FROM charactercreator_character
JOIN charactercreator_character_inventory
ON charactercreator_character.character_id = charactercreator_character_inventory.character_id
JOIN armory_weapon
ON charactercreator_character_inventory.item_id = armory_weapon.item_ptr_id
GROUP BY charactercreator_character_inventory.character_id;"""
)
x = range(0, 20)
for _ in x:
    print('Character', _ + 1, 'has', curs.execute(char_items).fetchall()[_][0],
    'items and', curs.execute(char_weapons).fetchall()[_][0], 'weapons')

# On average, how many Items and weapons does each Character have?"""
"""
SELECT AVG(num_items) FROM
(SELECT cc.character_id, COUNT(DISTINCT ai.item_id) AS num_items
FROM charactercreator_character AS cc,
charactercreator_character_inventory AS cci,
armory_item AS ai
WHERE cc.character_id = cci.character_id
AND ai.item_id = cci.item_id
GROUP BY 1);
"""

y = range(0,302)
test = range(0,155)
item_count = 0
weapon_count = 0
for _ in y:
    item_count = item_count + curs.execute(char_items).fetchall()[_][0]

print("The average items per character is: ", item_count / 302)

for _ in test:
    weapon_count = weapon_count + curs.execute(char_weapons).fetchall()[_][0]

print("The average weapons per character is: ", weapon_count / 155)


import os
import random
from dotenv import load_dotenv
from discord.ext import commands
from discord.utils import get
import youtube_dl
from os import system
from discord import FFmpegPCMAudio
import discord

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

bot = commands.Bot(command_prefix='!')

queue_list = []

@bot.command(name='starwars', help='Responds with a random Star Wars quote')
async def star_wars(ctx):

    starwars_quotes = [
        'I have a bad feeling about this.',
        "I'd rather kiss a Wookie!",
        'I find your lack of faith disurbing.',
        'May the Force be with you.',
        'Do.  Or do not.  There is no try',
        'Fear is the path to the dark side. Fear leads to anger; anger leads to hate; hate leads to suffering. I sense much fear in you.',
        'Well, if droids could think, there’d be none of us here, would there?',
        'I’m just a simple man trying to make my way in the universe',
        'So this is how liberty dies. With thunderous applause.',
        'I am one with the Force. The Force is with me.',
    ]

    response = random.choice(starwars_quotes)
    await ctx.send(response)

@bot.command(name="ping", help='Check your ping')
async def ping(ctx):
    await ctx.send(f"Your ping is: {round(bot.latency * 1000)}ms.  I don't know if that is good or bad, so deal "
                   f"with it")

@bot.command(pass_context=True, brief="Make Silbot join the voice channel")
async def join(ctx):
    channel = ctx.message.author.voice.channel
    if not channel:
        await ctx.send("What the what?")
        return
    voice = get(bot.voice_clients, guild=ctx.guild)
    if voice and voice.is_connected():
        await voice.move_to(channel)
    else:
        voice = await channel.connect()
    await voice.disconnect()
    if voice and voice.is_connected():
        await voice.move_to(channel)
    else:
        voice = await channel.connect()
    await ctx.send(f"I get to play with the big kids! Joined '{channel}' channel")

@bot.command(pass_context=True, brief="Make Silbot leave the voice channel", aliases=['quit'])
async def leave(ctx):
    channel = ctx.message.author.voice.channel
    voice = get(bot.voice_clients, guild=ctx.guild)
    if voice and voice.is_connected():
        await voice.disconnect()
        await ctx.send("See ya, wouldn't wanna be ya!")
    else:
        await ctx.send("I don't want to talk to myself!")

@bot.command(pass_context=True, brief="Makes DJ Silbot play a song !play [url]")
async def play(ctx, url: str):

    song_there = os.path.isfile("song.mp3")
    try:
        if song_there:
            os.remove("song.mp3")
    except Exception:
        await ctx.send("Oops, you did it again!")
        return
    await ctx.send("Now Playing: " + url)
    voice = get(bot.voice_clients, guild=ctx.guild)
    ydl_opts = {
        'format': 'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])
    for file in os.listdir("./"):
        if file.endswith(".mp3"):
            os.rename(file, 'song.mp3')
    voice.play(discord.FFmpegPCMAudio)
    voice.play(discord.FFmpegPCMAudio("song.mp3"))
    voice.volume = 100
    voice.is_playing()

@bot.command(pass_context=True, brief="Change the volume")
async def volume(ctx, volume):
    voice = get(bot.voice_clients, guild=ctx.guild)
    voice.volume = volume
    await ctx.send("Pump it up! Volume: " + volume)

bot.run(TOKEN)


import os
filepath='/root/b/d/MFC'
files = os.listdir(filepath)
delete = 0
for f in files:
    #print(f)
    f =filepath+'/'+f
    if os.path.isfile(f):

        fsize = os.path.getsize(f)
        if not fsize:
            print('delete',f)
            os.system('rm {}'.format(f))

    else:
        if not delete:
            ifdp = input('是否删除文件夹：(y/n)')
        if ifdp == 'y':
            delete = 1
        if delete:
            os.system('rm -r {}'.format(f))


import json
import datetime

from decouple import config
from django.contrib.sites.models import Site
from django.shortcuts import render, HttpResponseRedirect, render_to_response, get_object_or_404
from django.core.exceptions import ObjectDoesNotExist
from django.utils.translation import gettext as _
from django.http import HttpResponse, Http404
from django.template.loader import render_to_string
from django.template import RequestContext
from django.forms.utils import ErrorList
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.core.paginator import Paginator, InvalidPage, EmptyPage, PageNotAnInteger
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
from django.core.mail import send_mail, mail_admins
from django.contrib import messages
from django.core.urlresolvers import reverse_lazy
from django.conf import settings
from django.utils import timezone

from django.forms import HiddenInput
from django.db.models import Count
from agenda.models import Event
from agenda.models import EventGuest
from agenda.forms import EventForm, EventGuestForm, InvitationForm, CircleForm, UpdateGuestForm, PostForm, CommentForm, EmailPostForm
from students.models import User

from agenda.models import Circle
from agenda.models import Contact
from agenda.models import Invitation
from agenda.models import UserInfo

from agenda.models import Post
from agenda.models import Comment
from taggit.models import Tag

# Create your views here.
def liste_events(request):
    events = Event.objects.all()
    paginator = Paginator(events, 10)
    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1
    try:
        events = paginator.page(page)
    except (EmptyPage, InvalidPage):
        events = paginator.page(paginator.num_pages)
    return render(request, 'event/liste.html', {'events': events})

class ListEvents(ListView):

    def get_queryset(self):
        events = Event.objects.filter(
            guests = self.request.user,
            date__gte = datetime.datetime.now()
        )
        if 'champ' in self.kwargs:
            events = events.filter(
                (self.kwargs['champ'],
                self.kwargs['terme'])
            )
        return events

@method_decorator([login_required], name='dispatch')
class DetailEventView(DetailView):
    model = Event

    def get_context_data(self, **kwargs):
        context = super(DetailEventView, self).get_context_data(**kwargs)
        form = EventGuestForm(initial={'event': self.object}, user=self.request.user)
        queryset = form.fields['guest'].queryset
        form.fields['guest'].queryset = queryset.all()
        form.fields['event'].widget = HiddenInput()
        context['form'] = form
        return context

    def post(self, request, *args, **kwargs):
        form = EventGuestForm(self.request.POST, user=self.request.user)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.status = 1
            cleaned_data = form.cleaned_data
            if not self.request.user in cleaned_data['event'].guests.all() and not cleaned_data['event'].eventguest_set.filter(status=0).first():
                EventGuest.objects.create(event=cleaned_data['event'],status=0, guest=self.request.user)
            obj.save()
            send_invitation_to_guest(obj)
            if self.request.is_ajax():
                delete_form = render_to_string(
                    "partial/delete_form.html",
                    {'delete_url': form.instance.delete_url()}
                )
                data = {
                    'guest': form.instance.guest.username,
                    'get_status_display' : form.instance.get_status_display(),
                    'delete_form': delete_form
                }
                return HttpResponse(
                    json.dumps(data),
                    content_type="application/json"
                )
            else:
                return HttpResponseRedirect('/calendar/%s/detail/' % form.instance.event.id)
        else:
            if request.is_ajax():
                return render(request,
                    'partial/guest_form.html',
                    { 'event': form.instance.event, 'form': form}
                )
            else:
                return render(request, 'event/detail.html', {'event': form.instance.event, 'form': form})

def send_invitation_to_guest(event_guest):
    sender = event_guest.event.eventguest_set.get(status=0)
    message = _("{0} vous invite à l'évenement {1} qui se déroulera le {2} à {3}: {4} \n Indiquez votre choix sur http://{5}/calendar/participation/{6}/".format(sender.guest, event_guest.event.name, event_guest.event.date, event_guest.event.location, event_guest.event.description,Site.objects.get_current().domain,event_guest.pk))
    sender = sender.guest.email
    subject = _("Vous êtes invité par {0}".format(sender))
    recipient_list = [event_guest.guest.email]
    send_mail(subject, message, sender, recipient_list, fail_silently=False)

def create_event(request):
    if request.method == "POST":
        form = EventForm(request.POST)
        if form.is_valid():
            event = form.save()
            messages.success(request, _('created an event'))
            return HttpResponseRedirect('/calendar/%i/detail/' % event.pk)
    else:
        form = EventForm()
    return render(request, 'event/create.html', {'form': form})

def detail_event(request, id):
    event = Event.objects.get(pk=id)
    if request.method == "POST":
        form = EventGuestForm(request.POST, user=self.request.user)
        if form.is_valid():
            form.save()
            if request.is_ajax():
                delete_form = render_to_string(
                    "partial/delete_form.html",
                    {'delete_url': form.instance.delete_url()}
                )
                data = {
                    'guest': form.instance.guest.username,
                    'get_status_display' : form.instance.get_status_display(),
                    'delete_form': delete_form
                }
                return HttpResponse(
                    json.dumps(data),
                    content_type="application/json"
                )
            return HttpResponseRedirect('/calendar/%s/detail/' % id)
    else:
        form = EventGuestForm(initial={'event': event},user=self.request.user)
        guests = [user.pk for user in event.guests.all()]
        form.fields['guest'].queryset=User.objects.exclude(pk__in=guests)
        form.fields['event'].widget = HiddenInput()
    if request.is_ajax():
        render_to_response(
            'partial/guest_form.html',
            { 'event': event, 'form': form}
        )
    return render(request, 'event/detail.html', {'event': event, 'form': form})

@login_required
def delete_guest(request, id, guest):
    if request.method == "POST":
        event = Event.objects.get(pk=id)
        guest = User.objects.get(pk=guest)
        to_delete = EventGuest.objects.get(
            event = event,
            guest = guest
        )
        to_delete.delete()
        if request.is_ajax():
            return HttpResponse('OK')
    return HttpResponseRedirect('/calendar/%s/detail/' % id)

@login_required
def delete_event(request, id):
    try:
        event = Event.objects.get(pk = id)
        if request.method == "POST":
            event.delete()
    except Event.DoesNotExist:
        raise Http404(_('No event exist'))

    return HttpResponseRedirect('/calendar/liste/')

@login_required
def update_event(request, id):
    try:
        event = Event.objects.get(pk = id)
        if request.method == "POST":
            form = EventForm(request.POST, instance=event)
            if form.is_valid():
                u = form.save()
                return HttpResponseRedirect('/calendar/%i/detail/' % u.pk)
        else:
            form = EventForm(instance = event)
    except Event.DoesNotExist:
        raise Http404(_('This event doesnt exist'))
    return render(request, 'event/create.html', {'form': form})

@method_decorator([login_required], name='dispatch')
class InvitationListView(ListView):
    model = Invitation

    def get_queryset(self):
        return Invitation.objects.filter(sender=self.request.user)

@method_decorator([login_required], name='dispatch')
class InvitationCreateView(CreateView):
    form_class = InvitationForm
    model = Invitation

    def form_valid(self, form):
        obj = form.save(commit=False)
        obj.sender = self.request.user
        try:
            Invitation.objects.get(email=obj.email, sender=obj.sender)
            form._errors['email'] = ErrorList(
                [_('Une invitation a déja été envoyée à cette adresse.')]
            )
            return super(InvitationCreateView, self).form_invalid(form)
        except Invitation.DoesNotExist:
            pass
        obj.save()
        messages.success(self.request, _('created an invitation.'))
        return send_invitation(obj)

def send_invitation(invitation):
    try:
        user = User.objects.get(email=invitation.email)
        message = _('{0} vous a ajouté à ses contacts.'.format(invitation.sender.username))
        contact = Contact(owner=invitation.sender, user=user, invitation_send=True, invitation_accepted=True)
        contact.save()
        invitation.delete()
        return HttpResponseRedirect(contact.get_absolute_url())
    except User.DoesNotExist:
        message = _('{0} vous invite à rejoindre ses contacts. Inscrivez vous sur http://{1}/accounts/signup/ pour accepter son invitation.'.format(invitation.sender.username, Site.objects.get_current().domain))

        send_mail(_('Une invitation a été envoyée'),message,invitation.sender,[invitation.email], fail_silently=False)
        return HttpResponseRedirect(invitation.get_absolute_url())

@method_decorator([login_required], name='dispatch')
class ContactListView(ListView):

    def get_context_data(self, **kwargs):
        context = super(ContactListView, self).get_context_data(**kwargs)
        context['circles'] = Contact().all_circles(self.request.user)
        return context

    def get_queryset(self):
        return Contact().all_contacts(self.request.user)

@method_decorator([login_required], name='dispatch')
class ContactDetailView(DetailView):
    model = Contact

@method_decorator([login_required], name='dispatch')
class CircleDetailView(DetailView):
    model = Circle

@method_decorator([login_required], name='dispatch')
class CircleCreateView(CreateView):
    form_class = CircleForm
    model = Circle

    def form_valid(self, form):
        obj = form.save(commit=False)
        obj.owner = self.request.user
        obj.save()
        return HttpResponseRedirect(obj.get_absolute_url())

@method_decorator([login_required], name='dispatch')
class UserInfoUpdateView(UpdateView):
    model = UserInfo
    fields = ['circle', 'notes',]

    def get_form(self, form_class=None):
        if form_class is None:
            form_class = self.form_class
        form = super(UserInfoUpdateView, self).get_form(form_class)
        form.fields['circle'].queryset = Circle.objects.filter(owner=self.request.user)
        return form

@method_decorator([login_required], name='dispatch')
class UpdateGuestView(UpdateView):
    model = EventGuest
    form_class = UpdateGuestForm

    def form_valid(self, form):
        form.save()
        return HttpResponseRedirect('/calendar/listes/')

@method_decorator([login_required], name='dispatch')
class DeleteContactView(DeleteView):
    model = Contact
    success_url = reverse_lazy('all_contacts')

# blog
class PostListView(ListView):
    queryset = Post.published.all()
    context_object_name = 'posts'
    paginate_by = 4
    template_name = 'blog/list.html'

def post_list(request, tag_slug=None):
    object_list = Post.published.all()
    tag = None

    if tag_slug:
        tag = get_object_or_404(Tag, slug=tag_slug)
        object_list = object_list.filter(tags__in=[tag])

    paginator = Paginator(object_list, 15)
    page = request.GET.get('page')
    try:
        posts = paginator.page(page)
        print(posts)
    except PageNotAnInteger:
        posts = paginator.page(1)
    except EmptyPage:
        posts = paginator.page(paginator.num_pages)

    return render(request, 'blog/list.html', {'page': page, 'posts': posts, 'tag': tag })

def post_detail(request, year, month, day, post):
    post = get_object_or_404(Post, slug=post, status='published', publish__year=year, publish__month=month, publish__day=day)
    comments = post.comments.filter(active=True)
    # list of similar posts
    post_tags_id = post.tags.values_list('id', flat=True)
    similar_posts = Post.published.filter(tags__in=post_tags_id).exclude(id=post.id)
    similar_posts = similar_posts.annotate(same_tags=Count('tags')).order_by('-same_tags', '-publish')[:4]

    if request.method == 'POST':
        comment_form = CommentForm(data=request.POST)
        if comment_form.is_valid():
            new_comment = comment_form.save(commit=False)
            new_comment.post = post
            new_comment.save()
            messages.success(request, _('Comment added.'))
            return HttpResponseRedirect(post.get_absolute_url())
    else:
        comment_form = CommentForm()

    return render(request, 'blog/detail.html', { 'post': post, 'comments': comments, 'comment_form': comment_form, 'similar_posts': similar_posts })

def post_share(request, post_id):
    post = get_object_or_404(Post, id=post_id, status='published')
    sent = False

    if request.method == 'POST':
        form = EmailPostForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            post_url = request.build_absolute_uri(post.get_absolute_url())
            subject = _('{} ({}) recommends you reading "{}"'.format(cd['name'], cd['email'], post.title))
            message = _('Read "{}" at {}\n\n{}\'s comments: {}'.format(post.title, post_url, cd['name'], cd['comments']))
            send_mail(subject, message, config('ADMIN_EMAIL'), [cd['to']])
            sent = True
    else:
        form = EmailPostForm()
    return render(request, 'blog/share.html', { 'post': post, 'form': form, 'sent': sent })

def post_like(request):
    liked = False
    if request.method == 'GET':
        post_id = request.GET['post_id']
        post = Post.objects.get(id=int(post_id))
        if request.session.get('has_liked_' + post_id, False):
            if post.likes > 0:
                likes = post.likes - 1
                try:
                    del request.session['has_liked_' + post_id]
                except KeyError:
                    pass
        else:
            request.session['has_liked_'+post_id] = True
            likes = post.likes + 1
    post.likes = likes
    post.save()
    return HttpResponse(likes, liked)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 28 10:03:12 2018

@author: tom verguts
chapter 11: basic bayes
"""

#%% initialize
import numpy as np
import matplotlib.pyplot as plt

low, high = 0, 1
p = 0.8
sample_size = [0, 50, 500, 50]
n_support = 500
v = np.linspace(low, high, num = n_support) # vector of points where density is calculated
delta_p = 1/n_support
signif = 0.05/2

#%% figure 12.1
alpha, beta = [2, 2, 2, 2], [2, 2, 2, 40] # prior parameters for the beta distribution

fig, axes = plt.subplots(nrows = 2, ncols = 2)
#plt.subplot(121)
for index in range(4):
    r, c = index//2, index%2
    n_heads = np.random.binomial(sample_size[index], p)
    dens = (v**(alpha[index]-1+n_heads))*((1-v)**(beta[index]-1+(sample_size[index]-n_heads)))
    tot = np.sum(dens)*delta_p
    dens = dens/tot # normalize to integral of 1
    mean_theta = np.dot(dens, v)*delta_p
    max_theta = v[np.argmax(dens)]
    left_point =  np.argmin(np.abs(np.cumsum(dens*delta_p)-signif))
    right_point = np.argmin(np.abs(np.cumsum(dens*delta_p)-(1-signif)))
    if (index==0) or (index==2):
        axes[r, c].set_ylabel("posterior belief in p")
    if (index==2) or (index==3):
        axes[r, c].set_xlabel("p")
    axes[r, c].axis([low, high, 0, 25])
    axes[r, c].plot(v, dens, color = "black")
    #axes[r, c].plot(
    #  v[left_point:right_point],np.zeros(right_point-left_point)+0.15, color = "black", linewidth = 7)
    axes[r, c].set_title("sample size = {}".format(sample_size[index]))
    axes[r, c].plot(mean_theta, 2, marker = "o", color = "black")
    axes[r, c].plot(max_theta, 2,  marker = "x", color = "black")

#%%figure 12.2
#fig, axes = plt.subplots(nrows = 2, ncols = 2)
n_trial = 1000
plot_set = [1, 9, 19, 499]
alpha, beta = 2, 2
prior = (v**(alpha-1))*((1-v)**(beta-1))
plot_label = 0
for trial_loop in range(n_trial):
    # sample
    x = np.random.binomial(1, p)
    post = prior * ((v**x)*((1-v)**(1-x)))
#    post = prior
    post = post/(np.sum(post)*delta_p)
    prior = post
    if trial_loop in plot_set:
        r, c = plot_label//2, plot_label%2
        axes[r, c].plot(v, post, color = "black")
        axes[r, c].set_ylim(0, 30)
        axes[r, c].set_title("posterior after {} trials".format(trial_loop+1))
        plot_label += 1


import pandas as pd
import pickle
import numpy as np
import time
import re

import spacy
from spacy.lang.en.stop_words import STOP_WORDS
import sys

# only run one time to download spacy model -- SPECIFICALLY A NOTEBOOK COMMAND
#!{sys.executable} -m spacy download en_core_web_sm

# load saved df.  df['working_abstract'] contains cleaned text.

df = pd.read_pickle("../../../data/prd/Paper/FR_clean_22DEC21.pkl")
df.reset_index(inplace = True)
df.rename(columns={'index':'original index'}, inplace=True)

# Tokenize, POS, Lemmatize, Stop word removal

nlp = spacy.load('en_core_web_sm', disable=['parser', 'ner'])

sentences = df['working_abstract']

print("Lemmatizing...")

t1 = time.time()
lemmas = []

for doc in nlp.pipe(sentences):
    tokens = []
    for token in doc:
        if token.pos_ in ['NOUN', 'VERB', 'ADJ', 'ADV', 'PROPN', 'INTJ', 'NUM', 'X'] and not token.is_stop:
            tokens.append(token.lemma_)
    lemmas.append(tokens)

t2 = time.time()
print(t2-t1)

file = open("../../../data/prd/Paper/FR_lemmas_22DEC21.pkl", 'wb')
pickle.dump(lemmas, file)
file.close()


import datadeal.hfpy1
def sanitize (time_string):
    if '-' in time_string:
        splitter = '-'
    elif ':' in time_string:
        splitter = ':'
    else:
        return time_string
    (mins,sec) = time_string.split(splitter)
    return (mins+'.'+sec.strip())

cleanjames = []
cleanjulie = []
cleanmikey =[]
cleansarah =[]

for each_t in datadeal.hfpy1.james:
    cleanjames.append(sanitize(each_t))
for each_t in datadeal.hfpy1.mikey:
    cleanmikey.append(sanitize(each_t))
for each_t in datadeal.hfpy1.julie:
    cleanjulie.append(sanitize(each_t))
for each_t in datadeal.hfpy1.sarah:
    cleansarah.append(sanitize(each_t))

#集合 函数 自动过滤掉 重复的数

print(sorted(set(cleanjames))[0:3])
print(sorted(set(cleanjulie))[0:3])
print(sorted(set(cleanmikey))[0:3])
print(sorted(set(cleansarah))[0:3])  #[0:3]  0,1,2 不含最后一项数据


#Joshua Gonzales
#jgonz73

import pandas as pd
import matplotlib.pyplot as plt

fileref = open('SCDB_2018_01_justiceCentered_Citation.csv', encoding='ISO-8859-1')
mydataset = pd.read_csv(fileref)

mydataset_subset = mydataset[mydataset.term >= 2000]
f=plt.figure(figsize=(6,6))
mydataset_subset.groupby('term')['caseId'].nunique().plot(kind='bar')

f=plt.figure(figsize=(12,8))
mydataset_subset.groupby(['justiceName','direction'])['caseId'].nunique().plot(kind="bar")
f.tight_layout()

fileref = open('SCDB_2018_01_justiceCentered_Citation.csv', encoding='ISO-8859-1')
mydataset = pd.read_csv(fileref)

mydataset_subset = mydataset[mydataset.term >= 2005]
g=plt.figure(figsize=(8,6))
mydataset_subset.groupby('term')['caseId'].nunique().plot(kind='bar')

m=plt.figure(figsize=(12,8))
mydataset_subset.groupby('caseDisposition')['caseId'].nunique().plot(kind="bar")

mydataset_subset = mydataset[mydataset.term >= 2010]
l = plt.figure(figsize=(15,10))
mydataset_subset.groupby('term')['caseId'].nunique().plot(kind='bar')