# -*- coding: utf-8 -*-

from six import string_types
from collections import OrderedDict
import uuid

import pandas as pd
import xarray as xr
import numpy as np

from tvb_multiscale.core.config import initialize_logger

from tvb.contrib.scripts.utils.log_error_utils import raise_value_error
from tvb.contrib.scripts.utils.data_structures_utils import series_loop_generator, is_integer

from tvb_multiscale.core.neotraits import HasTraits

LOG = initialize_logger(__name__)

class SpikingNodesSet(pd.Series, HasTraits):

    """SpikingNodesSet class is an indexed mapping
       (based on inheriting from pandas.Series class)
       between SpikingNodesCollections labels and contents
       residing at a specific brain node's set.
    """

    _number_of_nodes = None

    _collection_name = "Population"

    def __init__(self, nodes=pd.Series(dtype='object'), **kwargs):
        kwargs["name"] = kwargs.pop("label", kwargs.get("name", ""))
        pd.Series.__init__(self, nodes, dtype=np.dtype('O'), **kwargs)
        HasTraits.__init__(self)
        self.configure()

    def __getstate__(self):
        d = super(SpikingNodesSet, self).__getstate__()
        d["_collection_name"] = self._collection_name
        d["gid"] = self.gid
        d["title"] = self.title
        d["tags"] = self.tags
        return d

    def __setstate__(self, d):
        super(SpikingNodesSet, self).__setstate__(d)
        self._collection_name = d.get("_collection_name", self._collection_name)
        self.gid = d.get("gid", uuid.uuid4())
        self.title = d.get("title",
                           '{} gid: {}'.format(self.__class__.__name__, self.gid))
        self.tag = d.get("tags", {})
        self.configure()

    @property
    def label(self):
        return self.name

    @property
    def spiking_simulator_module(self):
        for i_n, nod_lbl, nodes in self._loop_generator():
            if nodes.spiking_simulator_module is not None:
                return nodes.spiking_simulator_module
        return None

    def __len__(self):
        return pd.Series.__len__(self)

    def __getitem__(self, items):
        """If the argument is a sequence, a new SpikingNodesSet instance is returned.
           If the argument is an integer index or a string label index,
           the corresponding SpikingNodeCollection is returned.
        """
        pops = pd.Series.__getitem__(self, items)
        if isinstance(items, string_types) or is_integer(items):
            return pops
        return self.__class__(nodes=pops, label=self.label)

    @property
    def label(self):
        """The node's set label."""
        return self.name

    # Methods to get or set attributes for nodes and/or their connections:

    def _loop_generator(self, inds_or_lbls=None):
        """Method to create a generator looping through the SpikingNodeCollection objects
          and returning the indice, the label, and the SpikingNodeCollection itself.
            Arguments:
             inds_or_lbls: collection (list, tuple, array) of the indices or keys of selected collections.
             Default = None, corresponds to all collections of the node's set.
            Returns:
             The generator object
         """
        return series_loop_generator(self, inds_or_lbls)

    def get_nodes(self, inds_or_lbls=None):
        """Method to get the nodes indices of the SpikingNodesSet's collections.
           Argument:
            inds_or_lbls: collection (list, tuple, array) of the indices or keys of selected collections.
                              Default = None, corresponds to all collections of the SpikingNodesSet.
           Returns:
            tuple of nodes."""
        output = ()
        for id, lbl, nodes in self._loop_generator(inds_or_lbls):
            output += tuple(nodes.nodes)
        return output

    def get_number_of_nodes(self, inds_or_lbls=None):
        return len(self.get_nodes(inds_or_lbls))

    def get_number_of_nodes_per_collection(self, inds_or_lbls=None):
        output = []
        labels = []
        for id, lbl, nodes in self._loop_generator(inds_or_lbls):
            labels.append(nodes)
            output.append(nodes.get_size())
        return xr.DataArray(np.array(output),
                            dims=[self._collection_name], coords={self._collection_name: np.array(labels)})

    def Set(self, values_dict, inds_or_lbls=None):
        """Method to set attributes of the SpikingNodesSet's collections' nodes.
           Arguments:
            values_dict: dictionary of attributes names' and values.
            inds_or_lbls: collection (list, tuple, array) of the indices or keys of selected collections.
                          Default = None, corresponds to all collections of the set.
        """
        for id, lbl, pop in self._loop_generator(inds_or_lbls):
            pop.Set(values_dict)

    def Get(self, attrs=None, inds_or_lbls=None, summary=False):
        """Method to get attributes of the SpikingNodesSet's collections' nodes.
           Arguments:
            attrs: names of attributes to be returned. Default = None, corresponds to all nodes' attributes.
            inds_or_lbls: collection (list, tuple, array) of the indices or keys of selected collections.
                             Default = None, corresponds to all collections of the node's set.
            summary: if integer, return a summary of unique output values
                                 within accuracy of the specified number of decimal digits
                     otherwise, if it is not None or False return
                     either a dictionary of a statistical summary of mean, minmax, and variance for numerical attributes,
                     or a list of unique string entries for all other attributes,
                     Default = None, corresponds to returning all values
           Returns:
            Series of collections' nodes' attributes.
        """
        output = pd.Series(dtype='object')
        for id, lbl, nodes in self._loop_generator(inds_or_lbls):
            output[lbl] = nodes.Get(attrs, summary=summary)
        return output

    def get_attributes(self, inds_or_lbls=None, summary=False):
        """Method to get all attributes of the SpikingNodesSet's collections' nodes.
           Arguments:
            inds_or_lbls: collection (list, tuple, array) of the indices or keys of selected collections.
                             Default = None, corresponds to all collections of the node's set.
            summary: if integer, return a summary of unique output values
                                 within accuracy of the specified number of decimal digits
                     otherwise, if it is not None or False return
                     either a dictionary of a statistical summary of mean, minmax, and variance for numerical attributes,
                     or a list of unique string entries for all other attributes,
                     Default = None, corresponds to returning all values
           Returns:
            Series of collections' nodes' attributes.
        """
        return self.Get(inds_or_lbls=inds_or_lbls, summary=summary)

    def GetConnections(self, inds_or_lbls=None, source_or_target=None):
        """Method to get the connections of the SpikingNodesSet's collections.
           Argument:
            inds_or_lbls: collection (list, tuple, array) of the indices or keys of selected collections.
                              Default = None, corresponds to all collections of the SpikingNodesSet.
            source_or_target: Direction of connections relative to the region's nodes
                              "source", "target" or None (Default; corresponds to both source and target)
           Returns:
            Series of connections.
        """
        output = pd.Series(dtype='object')
        for id, lbl, nodes in self._loop_generator(inds_or_lbls):
            output[lbl] = nodes.GetConnections(source_or_target=source_or_target)
        return output

    def SetToConnections(self, values_dict, inds_or_lbls=None, source_or_target=None):
        """Method to set attributes of the connections from/to the SpikingNodesSet's collections.
           Arguments:
            values_dict: dictionary of attributes names' and values.
            inds_or_lbls: collection (list, tuple, array) of the indices or keys of selected collections.
                              Default = None, corresponds to all collections of the SpikingNodesSet.
            source_or_target: Direction of connections relative to the set's nodes
                              "source", "target" or None (Default; corresponds to both source and target)
        """
        for id, lbl, nodes in self._loop_generator(inds_or_lbls):
            nodes.SetToConnections(values_dict, source_or_target=source_or_target)

    def GetFromConnections(self, attrs=None, inds_or_lbls=None, source_or_target=None, summary=None):
        """Method to get attributes of the connections from/to the SpikingNodesSet's collections.
           Arguments:
            attrs: collection (list, tuple, array) of the attributes to be included in the output.
            inds_or_lbls: collection (list, tuple, array) of the indices or keys of selected collections.
                              Default = None, corresponds to all collections of the SpikingNodesSet.
            source_or_target: Direction of connections relative to the set's nodes
                              "source", "target" or None (Default; corresponds to both source and target)
            summary: if integer, return a summary of unique output values
                                 within accuracy of the specified number of decimal digits
                     otherwise, if it is not None or False return
                     either a dictionary of a statistical summary of mean, minmax, and variance for numerical attributes,
                     or a list of unique string entries for all other attributes,
                     Default = None, corresponds to returning all values
           Returns:
            Series of connections' attributes.
        """
        output = pd.Series(dtype='object')
        for id, lbl, nodes in self._loop_generator(inds_or_lbls):
            output[lbl] = nodes.GetFromConnections(attrs, source_or_target=source_or_target, summary=summary)
        return output

    def get_weights(self, inds_or_lbls=None, source_or_target=None, summary=None):
        """Method to get the connections' weights of the SpikingNodesSet's nodes.
           Argument:
            inds_or_lbls: collection (list, tuple, array) of the indices or keys of selected collections.
                              Default = None, corresponds to all collections of the SpikingNodesSet.
            source_or_target: Direction of connections relative to the set's nodes
                              "source", "target" or None (Default; corresponds to both source and target)
            summary: if integer, return a summary of unique output values
                                 within accuracy of the specified number of decimal digits
                     otherwise, if it is not None or False return
                     either a dictionary of a statistical summary of mean, minmax, and variance for numerical attributes,
                     or a list of unique string entries for all other attributes,
                     Default = None, corresponds to returning all values
           Returns:
            Series of collections' nodes' weights.
        """
        return self.GetFromConnections(self._weight_attr, inds_or_lbls, source_or_target, summary)

    def get_delays(self, inds_or_lbls=None, source_or_target=None, summary=None):
        """Method to get the connections' delays of the SpikingNodesSet's nodes.
           Argument:
            inds_or_lbls: collection (list, tuple, array) of the indices or keys of selected collections.
                              Default = None, corresponds to all collections of the SpikingNodesSet.
            source_or_target: Direction of connections relative to the nodes's nodes
                              "source", "target" or None (Default; corresponds to both source and target)
            summary: if integer, return a summary of unique output values
                                 within accuracy of the specified number of decimal digits
                     otherwise, if it is not None or False return
                     either a dictionary of a statistical summary of mean, minmax, and variance for numerical attributes,
                     or a list of unique string entries for all other attributes,
                     Default = None, corresponds to returning all values
           Returns:
            Series of collections' nodes' delays.
        """
        return self.GetFromConnections(self._delay_attr, inds_or_lbls, source_or_target, summary)

    def get_receptors(self, inds_or_lbls=None, source_or_target=None, summary=None):
        """Method to get the connections' receptors of the SpikingNodesSet's nodes.
           Argument:
            inds_or_lbls: collection (list, tuple, array) of the indices or keys of selected collections.
                               Default = None, corresponds to all collections of the SpikingNodesSet.
            source_or_target: Direction of connections relative to the nodes's nodes
                              "source", "target" or None (Default; corresponds to both source and target)
            summary: if integer, return a summary of unique output values
                                 within accuracy of the specified number of decimal digits
                     otherwise, if it is not None or False return
                     either a dictionary of a statistical summary of mean, minmax, and variance for numerical attributes,
                     or a list of unique string entries for all other attributes,
                     Default = None, corresponds to returning all values
           Returns:
            Series of collections' nodes' receptors.
        """
        return self.GetFromConnections(self._receptor_attr, inds_or_lbls, source_or_target, summary)

    @property
    def collections(self):
        """Method to get the list of collections' labels of the SpikingNodesSet.
           Returns:
            list of collections' labels.
        """
        return list(self.index)

    @property
    def nodes(self):
        """Method to get the nodes of the SpikingNodesSet's collections.
           Returns:
            tuple of nodes."""
        return self.get_nodes()

    @property
    def number_of_nodes(self):
        """Method to get the total number of nodes of the SpikingNodesSet's collections,
           and for setting the respective protected property."""
        if self._number_of_nodes is None or self._number_of_nodes == 0:
            self._number_of_nodes = self.get_number_of_nodes()
        return self._number_of_nodes

    @property
    def number_of_nodes_per_collection(self):
        return self.get_number_of_nodes_per_collection()

    @property
    def attributes(self):
        return self.get_attributes()

    @property
    def connections(self):
        """Method to get the connections of the SpikingNodesSet's nodes.
           Returns:
            Series of connections.
        """
        return self.GetConnections()

    @property
    def weights(self):
        """Method to get the connections' weights of the SpikingNodesSet's nodes.
           Returns:
            Series of collections' nodes' weights.
        """
        return self.get_weights()

    @property
    def delays(self):
        """Method to get the connections' delays of the SpikingNodesSet's nodes.
           Returns:
            Series of collections' nodes' delays.
        """
        return self.get_delays()

    @property
    def receptors(self):
        """Method to get the connections' receptors of the SpikingNodesSet's nodes.
           Returns:
            Series of collections' nodes' receptors.
        """
        return self.get_receptors()

    def _return_by_type(self, values_dict, return_type="dict", concatenation_index_name=None, name=None):
        """This method returns data collected from the SpikingNodeCollections of the SpikingNodesSet
           in a desired output format, among dict (Default), pandas.Series, xarray.DataArray or a list of values,
           depending on user input.
           Arguments:
            values_dict: dictionary of attributes and values
            return_type: string selecting one of the return types ["values", "dict", "Series", "DataArray"].
                         Default = "dict". "values" stands for a list of values, without labelling the output.
            concatenation_index_name: The dimension name along which the concatenation across devices happens.
                                      Default =None, defaults to _collection_name.
            name: Label of the output data. Default = None.
           Returns:
            output data in the selected type.
        """
        if not isinstance(concatenation_index_name, string_types):
            concatenation_index_name = self._collection_name
        if return_type == "values":
            output = list(values_dict.values())
            if len(output) == 1:
                return output[0]
            else:
                return output
        elif return_type == "dict":
            return values_dict
        elif return_type == "Series":
            return pd.Series(values_dict, name=name)
        elif return_type == "DataArray":
            if name is None:
                name = self.name
            for key, val in values_dict.items():
                if isinstance(val, xr.DataArray):
                    val.name = key
                else:
                    raise_value_error("DataArray concatenation not possible! "
                                      "Not all outputs are DataArrays!:\n %s" % str(values_dict))
            dims = list(values_dict.keys())
            values = list(values_dict.values())
            if len(values) == 0:
                return xr.DataArray([])
            output = xr.concat(values, dim=pd.Index(dims, name=concatenation_index_name))
            output.name = name
            return output
        else:
            return values_dict

    def do_for_all(self, attr, *args, nodes=None, return_type="values",
                   concatenation_index_name=None, name=None, **kwargs):
        """This method will perform the required action (SpikingNodeCollection method or property), and
            will return the output data collected from (a subset of) the SpikingNodeCollections of the SpikingNodesSet,
            in a desired output format, among dict (Default), pandas.Series, xarray.DataArray or a list of values,
           depending on user input.
           Arguments:
            attr: the name of the method/property of SpikingNodeCollection requested
            *args: possible position arguments of attr
            nodes: a subselection of Device nodes of the SpikingNodesSet the action should be performed upon
            return_type: string selecting one of the return types ["values", "dict", "Series", "DataArray"].
                         Default = "values". "values" stands for a list of values, without labelling the output.
            concatenation_index_name: The dimension name along which the concatenation across devices happens.
                                      Default = None, which defaults to _collection_name.
            name: Label of the output data. Default = None.
            **kwargs: possible keyword arguments of attr
           Returns:
            output data in the selected type.
        """
        values_dict = OrderedDict()
        for device in self.devices(nodes):
            val = getattr(self[device], attr)
            if hasattr(val, "__call__"):
                values_dict.update({device: val(*args, **kwargs)})
            else:
                values_dict.update({device: val})
        return self._return_by_type(values_dict, return_type, concatenation_index_name, name)

    def info(self, recursive=0):
        info = HasTraits.info(self, recursive)
        info["label"] = self.label
        info['size'] = self.size
        info["%ss" % self._collection_name] = self.collections
        if recursive > 0:
            for pop in self.collections:
                info[pop] = "-" * 20
                for key, val in self[pop].info(recursive - 1).items():
                    info["[%s].%s" % (pop, key)] = val
        return info

    def info_details(self, recursive=0, connectivity=False, source_or_target=None):
        info = super(SpikingNodesSet, self).info_details(recursive)
        info["label"] = self.label
        info['size'] = self.size
        info["%ss" % self._collection_name] = self.collections
        if recursive > 0:
            for pop in self.collections:
                info[pop] = "-" * 20
                for key, val in self[pop].info_details(recursive=recursive-1, connectivity=connectivity,
                                                       source_or_target=source_or_target).items():
                    info["[%s].%s" % (pop, key)] = val
        return info


#!/usr/bin/env python

import numpy as np
import cv2
import glob
import shutil
import os

import argparse

def list_images(source, ext='jpg'):
  srch = os.path.join(source, '*.{}'.format(ext))
  return sorted(glob.glob(srch))

def flow_images(imglist):
  pass

def main(args):
  jpg_list = sorted(glob.glob(os.path.join(args.jpgSource, '*.jpg')))
  mask_list = sorted(glob.glob(os.path.join(args.maskSource, '*.png')))

  assert len(jpg_list) == len(mask_list)
  assert len(jpg_list) > 0
  assert len(mask_list) > 0

  if os.path.exists(args.dest) and args.kill:
    print('Removing {}'.format(args.dest))
    shutil.rmtree(args.dest)
  else:
    print('Warning. {} exists'.format(args.dest))

  if not os.path.exists(args.dest):
    os.makedirs(args.dest)

  img_classes, idx = [], 0
  for img_idx, (jpg, mask) in enumerate(zip(jpg_list, mask_list)):
    y = cv2.imread(mask, -1)
    x = cv2.imread(jpg, -1)[:,:,::-1]

    ht, wt = x.shape[:2]

    x0_vect = np.linspace(0, ht-args.crop_size, args.samples, dtype=np.int)
    y0_vect = np.linspace(0, wt-args.crop_size, args.samples, dtype=np.int)
    # coords = zip(x0_vect, y0_vect)

    for x0 in x0_vect:
      for y0 in y0_vect:

        ## Grab the majority label
        y_ = y[x0:x0+args.crop_size, y0:y0+args.crop_size]
        totals = np.zeros(args.nPossibleClasses)
        for k in range(args.nPossibleClasses):
            totals[k] = (y_==k).sum()
        pcts = totals / np.sum(totals)

        # Check for the presence of a majority
        maj = np.argmax(totals)
        if pcts[maj] > 0.5:
            # check for stroma -- two ways to skip stroma
            if maj==4 and pcts[maj] < 0.95:
                continue
        else:
            continue

        # Take care of the label shift
        if args.class4 and maj > 1:
          maj -= 1

        img_classes.append(maj)
        x_ = x[x0:x0+args.crop_size, y0:y0+args.crop_size, :]
        x_ = cv2.resize(x_, dsize=(0,0), fx=args.resize, fy=args.resize)

        dst = os.path.join(args.dest, '{:04d}.{:05d}.{:02}.jpg'.format(img_idx, idx, maj))
        idx += 1
        if idx % 250 == 0:
            print('[{:05d} / {:05d}]: {} --> {}'.format(img_idx, len(jpg_list), x_.shape, dst))
        cv2.imwrite(dst, x_)

  img_classes = np.asarray(img_classes)
  ic_dst = os.path.join(args.dest, 'y.npy')
  print('img classes', img_classes.shape)
  print('got classes:', np.unique(img_classes))
  print('saving classes to:', ic_dst)

  np.save(ic_dst, img_classes)

if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument('jpgSource')
  parser.add_argument('maskSource')
  parser.add_argument('dest')
  parser.add_argument('--samples', default=5, type=int)
  parser.add_argument('--resize', default=0.5, type=float)
  parser.add_argument('--crop_size', default=448, type=int)
  parser.add_argument('--nPossibleClasses', default=5, type=int)
  parser.add_argument('--kill', default=False, action='store_true')
  parser.add_argument('--class4', default=False, action='store_true')

  args = parser.parse_args()
  # if args.class4:
  #   args.__setattr__('nPossibleClasses', 4)
  print(args)
  main(args)


from selenium import webdriver
import time

# 根据webdriver里面的类去创建一个谷歌浏览器对象
path = r'C:\Users\ZBLi\Desktop\1805\day06\ziliao\chromedriver.exe'
browser = webdriver.Chrome(path)

# 再往下，操作浏览器就是操作对象
# 打开百度
url = 'http://www.baidu.com/'
browser.get(url)
time.sleep(2)

# 查找百度输入框
my_input = browser.find_element_by_id('kw')
# 写内容
my_input.send_keys('清纯美女')
time.sleep(2)

# 查找按钮框
button = browser.find_element_by_id('su')
button.click()

time.sleep(3)

# 查找阳光美女链接
href = browser.find_elements_by_xpath('//div[@class="op-img-covers-divide-high"][2]/a[2]')[0]
href.click()
time.sleep(3)

# 推出浏览器
browser.quit()


#Разделяют тикер и название компании из строки с инвестинга

def ticker_splt(n_t):
    n_t_list = n_t.split("(")
    tickerlist = n_t_list[1].split(")")
    ticker = tickerlist[0]

    return ticker

def name_splt(name_ticker):
    n_t_list = name_ticker.split("(")

    corp_name =""
    corp_name_list = []

    for letter in n_t_list[0]:
        corp_name_list.append(letter)

    corp_name_list.pop()

    for list_letter in corp_name_list:
        corp_name = corp_name+list_letter

    return corp_name

#Удаляет number букв с конца строки
def del_right_letters(string, number):
    corp_name =""
    corp_name_list = []

    for letter in string:
        corp_name_list.append(letter)

    for corp_name_list_deleting in range(0, number):
        corp_name_list.pop()

    for list_letter in corp_name_list:
        corp_name = corp_name+list_letter

    return corp_name

#Выражает капитализацию в миллионах $
def market_cap_int(market_cap_str):
    if "T" in market_cap_str:
        mc_int = del_right_letters(market_cap_str, 1)
        mc_int = float(mc_int)
        mc_int = mc_int*1000000
        mc_int = int(mc_int)

    if "B" in market_cap_str:
        mc_int = del_right_letters(market_cap_str, 1)
        mc_int = float(mc_int)
        mc_int = mc_int*1000
        mc_int = int(mc_int)

    if "M" in market_cap_str:
        mc_int = del_right_letters(market_cap_str, 3)
        mc_int = int(mc_int)

    else:
        return market_cap_str

    return mc_int


#!/usr/bin/python
'''
@author: Maximilian Mehnert <maximilian.mehnert@gmx.de
'''

from	zfs_functions import *

if __name__ == '__main__':
	transfer_marker_prefix="backup"
	# we are transferring / synchronizing the pool all from 192.168.50.20 to the local machine
	src_pool=ZFS_pool(pool="all", remote_cmd="ssh 192.168.50.20", verbose=True)
	dst_pool=ZFS_pool(pool="all", verbose=True)
	# the copy of the remote pool "all" will end up in "all/backup/all" on the local machine
	dst_prefix="all/backup/"

	for fs in src_pool:
		print (fs)
		src_fs=ZFS_fs(fs=fs, pool=src_pool, verbose=True)
		dst_fs=ZFS_fs(fs=dst_prefix+fs,pool=dst_pool, verbose=True)

		if not src_fs.sync_with(dst_fs=dst_fs,target_name=transfer_marker_prefix):
			print ("sync failure for "+fs)
		else:
			# here we are deleting older snapshots starting with transfer_marker_prefix, in this case keeping
			# the youngest three
			src_fs.clean_snapshots(prefix=transfer_marker_prefix, number_to_keep=3)
			# on the destination pool (in this case local machine, see above), clean up snapshots starting with the prefixes below,
			# keeping the youngest n according to the second part of the tuples
			for tuple in [["5min",1],
										["hourly",1],
										["quarterly",1],
										["daily",7],
										["weekly",4],
										["monthly",12]]:
				dst_fs.clean_snapshots(prefix=tuple[0], number_to_keep=tuple[1])
			# now we clean up unrelated snapshots both on the remote and local machine, ignoring those starting with the prefixes
			# configured below, keeping the 10 youngest
			prefixes_to_ignore=["hourly","weekly","quarterly","daily","weekly","monthly","yearly",transfer_marker_prefix]
			dst_fs.clean_other_snapshots(prefixes_to_ignore=prefixes_to_ignore,number_to_keep=10)
			src_fs.clean_other_snapshots(prefixes_to_ignore=prefixes_to_ignore,number_to_keep=10)


from dataclasses import dataclass
from datetime import datetime,timedelta
from typing import List, Sequence

from peewee import Model, AutoField, CharField, IntegerField, DateTimeField, FloatField, TextField, BooleanField, \
    Database, SqliteDatabase, chunked

__all__ = [
    # Super-special typing primitives.
    'OpOrderStatus',
    'OpLogType',
    'OpLogLevel',
    'OpLog',
    'OpOrder',
    'OpProject',
    'OpDataBase', ###数据库
]

class OpOrderStatus:
    """
    Interval of bar data.
    """
    NEW = 0 ##"新建"
    HOLD = 1   ##"已经买入"
    FINISHED_EARN = 2  ## 盈利单
    FINISHED_LOSS = 3  ##亏损单
    INVALID = 4  ## "无效单"  ##即没买入也没卖出

class OpLogType:
    PLAIN = 0   #不处理类型
    BUY_LONG = 1 #做多买入类型
    BUY_SHORT = 2 #做空买入类型
    CROSS_SUCCESS = 3 #预测成功交割单(卖出）类型
    CROSS_FAIL = 4 #预测失败交割单类型
    ABANDON = 5 #废弃单类型
class OpLogLevel:
    VERBASE = 0   #不处理类型
    DEBUG = 100 #做多买入类型
    INFO = 200 #做空买入类型
    WARN = 300 #预测成功交割单(卖出）类型
    ERROR = 400 #预测失败交割单类型

@dataclass
class OpLog:
    project_id:int = None
    id:int = None
    order_id:int = None
    type:int = OpLogType.PLAIN   ## 查看 OpLogType
    level:int = OpLogLevel.VERBASE   ## 查看 OpLogLevel
    time:datetime = None
    price:float = None
    info:str = ""
    extraJasonText:str = None
    def __post_init__(self):
        self.time = datetime.now()

@dataclass
class OpOrder:
    code: str
    code_name:str
    project_id:int
    buy_price: float  ##预测买入价
    sell_price: float
    create_time: datetime;  ##创建时间、发生时间
    id :int = None
    dimen:str = None
    status: int = OpOrderStatus.NEW
    duration: int = 0
    predict_suc: bool = None  ##是否预测成功，只在完成状态有效。
    update_time: datetime = None
    source: int = 0  ##来源：0 为回测数据，1为实盘数据
    desc:str = ""
    buy_price_real:float = None  ##实际买入
    sell_price_real:float = None ##实际卖出

    current_trade_time = None  ##当前交易的时间

    def __post_init__(self):
        self.update_time = self.create_time

@dataclass
class OpProject:
    id:int
    status:str
    name:str
    create_time:datetime

    summary:str = ""
    url:str = ""
    update_time:datetime = None

    def __post_init__(self):
        self.update_time = self.create_time

    pass

@dataclass
class OpStatistic:
    project_id : int
    start_time :datetime  ## 统计开始时间
    type : int  = None   ##主键，1: 最近1个月，2：最近3个月，3: 最近6个月，4：最近1年
    predict_suc_count = 0 ## int | 预测成功总数
    count = 0 ## 产出操作单的总数
    dealCount = 0  ## | 产出交易的个数
    earnCount = 0 ##  盈利总数
    predict_suc_deal_count= 0 ## 预测成功总数（不含无效单)
    totalPct = 0.0 ## 盈利情况
    totalEarnPct = 0.0  ## 盈利情况(不含亏损单)
    maxEarnPct = -999999  ## 最大盈利单
    maxLossPct = 999999  ## 最大亏损单

"""
实时信息
"""
@dataclass
class OpOrederRealInfo:
    order_id:int
    project_id:int
    id:int = None
    update_time = None
    price:float = 0.0
    current_status:str = ""

"""
orm model
"""

class OpBaseModel(Model):

    def to_dict(self):
        return self.__data__

    @staticmethod
    def from_data(data):
        pass

    def to_data(self,db_data):
        pass

class OpProjectModel(OpBaseModel):
    table_name = 'op_project'
    id = AutoField(null=False)
    name = CharField(max_length=512,null=False)
    create_time = DateTimeField(index=True)
    update_time = DateTimeField(index=True)
    status = CharField(max_length=128)
    summary = CharField(max_length=2048,null=True)
    url = CharField(max_length=2048,null=True)

    @staticmethod
    def from_data(data: OpProject):
        db_data = OpProjectModel()
        db_data.id = data.id
        db_data.name = data.name
        db_data.create_time = data.create_time
        db_data.status = data.status
        db_data.url = data.url
        db_data.update_time = data.update_time
        return db_data

    def to_data(self):
        data = OpProject(
            id=self.id,
            status=self.status,
            create_time=self.create_time,
            name=self.name,
        )
        data.summary = self.summary
        data.url = self.url
        data.update_time = self.update_time
        return data

class OpOrderModel(OpBaseModel):
    table_name = 'op_order'
    id = AutoField()
    code = CharField(max_length=48,null=False)
    code_name = CharField(max_length=125,null=False)
    dimen = CharField(max_length=256,null=False,index=True)
    project_id = IntegerField(null=False)
    buy_price = FloatField()
    sell_price= FloatField()
    buy_price_real = FloatField(null=True)
    sell_price_real = FloatField(null=True)
    create_time= DateTimeField()
    status = IntegerField()
    duration = IntegerField()
    predict_suc = BooleanField(null=True)
    update_time = DateTimeField()
    source = IntegerField()
    desc = CharField(max_length=2048,null=True)

    current_trade_time = DateTimeField(null=True)

    @staticmethod
    def from_data(data: OpOrder):
        db_data = OpOrderModel()
        db_data.id = data.id
        db_data.code = data.code
        db_data.dimen = data.dimen
        db_data.code_name = data.code_name
        db_data.project_id = data.project_id
        db_data.buy_price = data.buy_price
        db_data.sell_price = data.sell_price
        db_data.create_time = data.create_time
        db_data.status = data.status
        db_data.duration = data.duration
        db_data.predict_suc = data.predict_suc
        db_data.update_time = data.update_time
        db_data.source = data.source
        db_data.desc = data.desc
        db_data.buy_price_real = data.buy_price_real
        db_data.sell_price_real = data.sell_price_real
        db_data.current_trade_time = data.current_trade_time

        return db_data

    def to_data(self):
        data = OpOrder(
            code=self.code,
            code_name=self.code_name,
            project_id=self.project_id,
            buy_price=self.buy_price,
            sell_price=self.sell_price,
            create_time=self.create_time,
        )
        data.id = self.id
        data.dimen = self.dimen
        data.status = self.status
        data.duration = self.duration
        data.predict_suc = self.predict_suc
        data.update_time = self.update_time
        data.source = self.source
        data.desc = self.desc
        data.buy_price_real = self.buy_price_real
        data.sell_price_real = self.sell_price_real
        data.current_trade_time = self.current_trade_time

        return data

class OpLogModel(OpBaseModel):
    table_name = 'op_log'
    id = AutoField()
    order_id = IntegerField(null=True)
    project_id = IntegerField(null=False)
    type = IntegerField(null=True)
    level = IntegerField(null=False,default=0)
    time = DateTimeField()
    price = FloatField(null=True)
    info = CharField(max_length=2048,null=True)
    extraJasonText = CharField(max_length=2048,null=True)

    @staticmethod
    def from_data(data: OpLog):
        db_data = OpLogModel()
        db_data.id = data.id
        db_data.order_id = data.order_id
        db_data.project_id = data.project_id
        db_data.type = data.type
        db_data.level = data.level
        db_data.time = data.time
        db_data.price = data.price
        db_data.info = data.info
        db_data.extraJasonText = data.extraJasonText
        return db_data

    def to_data(self):
        data = OpLog(
            order_id=self.order_id,
        )
        data.id = self.id
        data.project_id = self.project_id
        data.type = self.type
        data.level = self.level
        data.time = self.time
        data.price = self.price
        data.info = self.info
        data.extraJasonText = self.extraJasonText
        return data

class OpStatisticModel(OpBaseModel):
    table_name = 'op_statistic'

    project_id = IntegerField(null=False)
    start_time = DateTimeField()
    type = AutoField(null=False) ##主键，1: 最近1个月，2：最近3个月，3: 最近6个月，4：最近1年
    predict_suc_count = IntegerField(null=False) ## int | 预测成功总数
    count = IntegerField(null=False) ## 产出操作单的总数
    dealCount = IntegerField(null=False)  ## | 产出交易的个数
    earnCount = IntegerField(null=False) ##  盈利总数
    predict_suc_deal_count= IntegerField(null=False)  ## 预测成功总数（不含无效单)
    totalPct = FloatField(null=False) ## 盈利情况
    totalEarnPct = FloatField(null=False)  ## 盈利情况(不含亏损单)
    maxEarnPct = FloatField(null=False)  ## 最大盈利单
    maxLossPct = FloatField(null=False)  ## 最大亏损单

    @staticmethod
    def from_data(data: OpStatistic):
        db_data = OpStatisticModel()
        db_data.project_id = data.project_id
        db_data.start_time = data.start_time
        db_data.type = data.type
        db_data.predict_suc_count = data.predict_suc_count
        db_data.count = data.count
        db_data.dealCount = data.dealCount
        db_data.earnCount = data.earnCount
        db_data.predict_suc_deal_count = data.predict_suc_deal_count
        db_data.totalPct = data.totalPct
        db_data.totalEarnPct = data.totalEarnPct
        db_data.maxEarnPct = data.maxEarnPct
        db_data.maxLossPct = data.maxLossPct
        return db_data

    def to_data(self):
        data = OpStatistic(
            project_id=self.project_id,
            start_time=self.start_time
        )
        data.type = self.type
        data.predict_suc_count = self.predict_suc_count
        data.count = self.count
        data.dealCount = self.dealCount
        data.earnCount = self.earnCount
        data.predict_suc_deal_count = self.predict_suc_deal_count
        data.totalPct = self.totalPct
        data.totalEarnPct = self.totalEarnPct
        data.maxEarnPct = self.maxEarnPct
        data.maxLossPct = self.maxLossPct
        return data

class OpDataBase:

    def __init__(self,db:Database):
       self.setDatabase(db)

    def setDatabase(self, db:Database):
        class OpProjectModelWrapper(OpProjectModel):
            class Meta:
                database = db
                table_name = OpProjectModel.table_name

        class OpOrderModelWrapper(OpOrderModel):
            class Meta:
                database = db
                table_name = OpOrderModel.table_name

        class OpLogModelWrapper(OpLogModel):
            class Meta:
                database = db
                table_name = OpLogModel.table_name

        class OpStatisticModelWrapper(OpStatisticModel):
            class Meta:
                database = db
                table_name = OpStatisticModel.table_name

        self.db = db
        self.projectModel = OpProjectModelWrapper
        self.logModel = OpLogModelWrapper
        self.orderModel = OpOrderModelWrapper
        self.statisticModel = OpStatisticModelWrapper
        self.checkConneted()
        self.table_list = [OpProjectModelWrapper, OpLogModelWrapper, OpOrderModelWrapper,OpStatisticModelWrapper]
        db.create_tables(self.table_list)

    def checkConneted(self):
        if self.db.is_closed():
            self.db.connect()

    def clearAll(self):
        self.db.drop_tables(self.table_list)
        self.db.create_tables(self.table_list)

    def save_projects(self, datas: List["OpProject"]):
        ds = [self.projectModel.from_data(i) for i in datas]
        dicts = [i.to_dict() for i in ds]
        with self.db.atomic():
            for c in chunked(dicts, 50):
                self.projectModel.insert_many(c).on_conflict_replace().execute()

    def clear_log(self):
        self.logModel.delete().execute()

    def delete_project(self,id:int):
        self.projectModel.delete().where(self.projectModel.id == id).execute()
        self.orderModel.delete().where(self.orderModel.project_id == id).execute()
        self.logModel.delete().where(self.logModel.project_id == id).execute()

    def count_project(self):
        return self.projectModel.select().count()

    def load_project(self,id)->OpProject:
        s = (
            self.projectModel.select()
                .where(self.projectModel.id == id)
        )
        if s:
            data = [db_bar.to_data() for db_bar in s]
            if len(data) > 0:
                assert len(data) == 1
                return data[0]
        return None

    def load_projects(self,offset:int,count:int) -> Sequence["OpProject"]:
        s = (
            self.projectModel.select()
                .offset(offset)
                #.order_by(self.dao.create_time.desc(), self.dao.update_time.desc())
                .limit(count)
        )
        data = [db_bar.to_data() for db_bar in s]
        return data

    def load_logs(self,project_id, order_id)-> Sequence["OpLog"]:
        s = (
            self.logModel.select()
                .where(
                (self.logModel.project_id == project_id)
                &
                (self.logModel.order_id == order_id)
            )
        )
        data = [db_bar.to_data() for db_bar in s]
        return data

    def save_log(self, log:OpLog):
        self.save_logs([log])

    def save_logs(self, datas: List["OpLog"]):
        ds = [self.logModel.from_data(i) for i in datas]
        dicts = [i.to_dict() for i in ds]
        with self.db.atomic():
            for c in chunked(dicts, 50):
                self.logModel.insert_many(c).on_conflict_replace().execute()

    def load_order_by_time(self, project_id:int,code:str, create_time:datetime)->OpOrder:
        s = (
            self.orderModel.select()
                .where(
                (self.orderModel.project_id == project_id)
                &
                (self.orderModel.code == code)
                &
                (self.orderModel.create_time == create_time)
            )
        )
        if s:
            data = [db_bar.to_data() for db_bar in s]
            if len(data) > 0:
                assert len(data) == 1
                return data[0]
        return None

    def load_order_all(self,project_id:int,finishOrder = None,start_time:datetime = None)-> Sequence["OpOrder"]:
        s = None
        if finishOrder is None:
            s = (
                self.orderModel.select()
                    .where(self.orderModel.project_id == project_id)
            )
        elif finishOrder == True:
            s = (
                self.orderModel.select()
                    .where(
                    (self.orderModel.project_id == project_id)
                    &
                    (self.orderModel.status != 0 & self.orderModel.status!=1)
                    &
                    (self.orderModel.create_time >= start_time)
                )
            )
        else:
            s = (
                self.orderModel.select()
                    .where(
                    (self.orderModel.project_id == project_id)
                    &
                    (self.orderModel.status == 0 or self.orderModel.status == 1)
                    &
                    (self.orderModel.create_time >= start_time)
                )
            )
        data = [db_bar.to_data() for db_bar in s]
        return data

    def save_order(self, op_order):
        ds = [self.orderModel.from_data(i) for i in [op_order]]
        dicts = [i.to_dict() for i in ds]
        with self.db.atomic():
            for c in chunked(dicts, 50):
                self.orderModel.insert_many(c).on_conflict_replace().execute()

    def save_statistices(self, datas: List["OpStatistic"]):
        ds = [self.statisticModel.from_data(i) for i in datas]
        dicts = [i.to_dict() for i in ds]
        with self.db.atomic():
            for c in chunked(dicts, 50):
                self.statisticModel.insert_many(c).on_conflict_replace().execute()

    def load_statistic_all(self) -> Sequence["OpStatistic"]:
        s = (
            self.statisticModel.select()
                .order_by(self.statisticModel.type.asc())
        )
        datas = [db_bar.to_data() for db_bar in s]
        return datas

    def close(self):
        self.db.close()

if __name__ == "__main__":

    db_file = SqliteDatabase("op_data.db")
    db = OpDataBase(db_file);
    now = datetime.now()

    def test():
        dt = now - timedelta(minutes=1)
        project = OpProject(
            id=12,
            status="unkonw",
            create_time=dt,
            name="测试",
        )
        project.summary = "summary1"
        project.url = "url"
        db.clearAll()

        assert db.count_project() == 0
        db.save_projects([project])
        assert db.count_project() == 1
        db.save_projects([project])
        db.save_projects([project])
        db.save_projects([project])
        assert db.count_project() == 1
        assert db.load_project(23) == None
        load_project = db.load_project(12)
        assert not load_project is None
        assert load_project.name == project.name

        project.id =13
        db.save_projects([project])
        project.id =14
        db.save_projects([project])
        assert db.count_project() == 3
        pList = db.load_projects(0, 1)
        assert len(pList) == 1
        pList = db.load_projects(0, 2)
        assert len(pList) == 2
        pList = db.load_projects(0, 6)
        assert len(pList) == 3
        pList = db.load_projects(2, 6)
        assert len(pList) == 1
        pList = db.load_projects(3, 6)
        assert len(pList) == 0
        db.delete_project(14)
        db.delete_project(13)

        assert db.count_project() == 1

        op_code = "343422"
        op_time = dt
        op_order_load = db.load_order_by_time(13,op_code, op_time)
        assert op_order_load is None

        op_order = OpOrder(code=op_code, code_name="dxjvkld", project_id=13,
                           create_time=op_time
                           , buy_price=34.6, sell_price=45)
        op_order.dimen = f"opName"
        op_order.status = "新的"
        op_order.duration = 3
        op_order.current_trade_time = datetime.now()
        db.save_order(op_order)
        op_order_load = db.load_order_by_time(13,op_code, op_time)

        assert not op_order_load is None
        assert op_order_load.dimen == op_order.dimen
        assert op_order_load.code == op_code
        assert op_order_load.create_time == op_time
        assert op_order_load.duration == op_order.duration
        assert op_order_load.current_trade_time == op_order.current_trade_time

        db.clear_log()
        oplog1 = OpLog(project_id=3,order_id=4,info="1")
        oplog2 = OpLog(project_id=3,order_id=3,info="2")
        oplog3 = OpLog(project_id=4,order_id=4,info="3")
        oplog4 = OpLog(project_id=3,order_id=6,info="4")
        oplog5 = OpLog(project_id=3,order_id=3,info="5")
        db.save_logs([oplog1,oplog2,oplog3,oplog4,oplog5])

        logs = db.load_logs(3,3)
        assert len(logs) ==2

        statisticData = OpStatistic(project_id = 1, start_time=now)
        statisticData.type = 1
        statisticData.earnCount = 34
        statisticData.totalEarnPct = 54.3
        db.save_statistices([statisticData])

        statisticData.type = 2
        statisticData.earnCount = 53
        db.save_statistices([statisticData])

        statisticData = db.load_statistic_all()
        assert  len(statisticData) == 2
        assert  statisticData[0].type == 1
        assert  statisticData[1].type == 2
        assert statisticData[0].earnCount == 34
        assert statisticData[1].earnCount == 53

    test();


# -*- coding:utf-8 -*-
# Author：YJX

import requests
from bs4 import BeautifulSoup
import traceback
import re

def getHTMLText(url, code="utf-8"):
    """ 获得URL对应的页面"""
    try:
        r = requests.get(url, timeout=60)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return ""

def getStockList(lst, stockURL):
    """获得股票列表，参数lst存储所有股票信息，参数stockURL获得股票列表的URL网站"""
    html = getHTMLText(stockURL, "GB2312")
    soup = BeautifulSoup(html, 'html.parser')
    # 找到所有a标签
    a = soup.find_all('a')
    for i in a:
        try:
            href = i.attrs['href']
            lst.append(re.findall(r"[s][hz]\d{6}", href)[0])
        except:
            continue

def getStockInfo(lst, stockURL, fpath):
    """获得每一支个股的信息"""
    count = 0
    for stock in lst:
        """从lst中获取每个个股的编号，链接由主体加编号加html组成"""
        url = stockURL + stock + ".html"
        html = getHTMLText(url)
        try:
            # 判断页面是否为空
            if html == "":
                continue
            # 字典中存放返回的所有个股信息
            infoDict = {}
            # 解析网页
            soup = BeautifulSoup(html, 'html.parser')
            # 源代码中，所有的股票信息封装在div标签下，属性是stock-bets；所以，搜索标签，找到个股信息
            stockInfo = soup.find('div', attrs={'class':'stock-bets'})
            # 在标签的属性bets-name下查找 股票名称
            name = stockInfo.find_all(attrs={'class':'bets-name'})[0]
            # 将信息增加到字典中
            infoDict.update({'股票名称':name.text.split()[0]})
            # 源代码中标签dt下存键，dd下存值，获取键值对
            keyList = stockInfo.find_all('dt')
            valueList = stockInfo.find_all('dd')
            # 将股票信息还原成键值对，存到字典中
            for i in range(len(keyList)):
                key = keyList[i].text
                val = valueList[i].text
                infoDict[key] = val
            with open(fpath, 'a', encoding='utf-8') as f:
                f.write(str(infoDict) + '\n')
                print("\r当前进度:{:.2f}%".format(count*100/len(lst)), end="")
        except:
            # 获得错误信息
            count = count+1
            print("\r当前进度:{:.2f}%".format(count * 100 / len(lst)), end="")
            # traceback.print_exc()
            continue

def main():
    """Main function of this project"""
    # 获得股票列表的链接
    stock_list_url = 'http://quote.eastmoney.com/stocklist.html'
    # 获得股票信息链接的主体
    stock_info_url = 'https://gupiao.baidu.com/stock/'
    # 文件保存地址
    output_file = 'E:\学习课件\Python网络爬虫与信息提取\DataFiles\BaiDuStockInfo.txt'
    # 股票信息
    slist = []
    getStockList(slist, stock_list_url)
    getStockInfo(slist, stock_info_url, output_file)

main()


#!/usr/bin/env python
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import tweepy
import time

def getSentiment(playerName):

	sid_obj = SentimentIntensityAnalyzer()

	neutral_count = 0
	positive_count = 0
	negative_count = 0

	with open(playerName + ".txt", 'r') as f:
		for tweet in f:
			sentiment_dict = sid_obj.polarity_scores(tweet)

			# decide sentiment as positive, negative and neutral
			if sentiment_dict['compound'] >= 0.05 :
				positive_count += 1

			elif sentiment_dict['compound'] <= - 0.05 :
				negative_count += 1

			else:
				neutral_count += 1

		print("neutral: " + str(neutral_count))
		print("positive: " + str(positive_count))
		print("negative: " + str(negative_count))

		sentDict = dict()
		sentDict['neutral'] = neutral_count
		sentDict['positive'] = positive_count
		sentDict['negative'] = negative_count

		return sentDict

def findTweets(playerName):

	print("donig stuff")

	#SAVE YOUR API KEYS HERE

	API_KEY = 'YOUR API KEY HERE'
	API_SECRET_KEY = 'YOUR SECRET KEY HERE'
	ACCESS_TOKEN = 'YOUR ACCESS TOKEN HERE'
	ACCESS_SECRET_TOKEN = 'YOUR SECRET TOKEN HERE'

	auth = tweepy.OAuthHandler(API_KEY, API_SECRET_KEY)
	auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET_TOKEN)
	api = tweepy.API(auth)

	search_words = playerName + " Everton" + " -filter:retweets"
	#August 1st
	date_since = "2020-01-08"

	time.sleep(2)
	tweets = tweepy.Cursor(api.search, q = search_words, lang='en', since=date_since, tweet_mode='extended').items(10000)
	texts = [tweet.full_text for tweet in tweets]

	with open(playerName.replace(' ', '') + ".txt", "w") as f:
		for line in texts:
			print(line, file=f)

def main():
	print("hello")
	teamDict = dict()

	with open("stats.txt", 'r', encoding="ISO-8859-1") as f:
		for line in f:
			pairs = line.split(',')
			playerDict = dict()
			for i in pairs:
				i = i.replace("'", "").replace("{", "").replace("}", "").strip()
				li = i.split(":")
				for j in range(len(li)):
					li[j] = li[j].replace(' ', '')
				print(i)
				playerDict[li[0]] = int(li[1]) if li[1].isnumeric() else li[1]

			teamDict[playerDict['name']] = playerDict

	nameList = [player for player in teamDict.keys()]
	#for player in nameList[0:12]:
	#	print(player.replace('-', ' '))
	#	findTweets(player.replace('-', ' '))

	#need to split into two groups because of twitter request limits
	#for player in nameList[12:]:
	#	print(player.replace('-', ' '))
	#	findTweets(player.replace('-', ' '))

	for player in teamDict.keys():
		print(player.replace('-', ' '))
		teamDict[player]["sentiment"] = getSentiment(player.replace('-', ''))
		print(teamDict[player]["sentiment"])

if __name__ == "__main__":
	main()


import requests
import os
import time
import xlwt
from bs4 import BeautifulSoup
# 用字典来存用户信息
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
}

# 构造请求URL
urls = ['https://movie.douban.com/j/search_subjects?type=movie&tag=%E8%B1%86%E7%93%A3%E9%AB%98%E5%88%86&sort=rank&page_limit=20&page_start={}'.format(str(i)) for i in range(0, 100, 20)]

def get_url_view(urls):
    data = []
    details = []
    for uri in urls:
        result = requests.get(uri, headers).json()
        data += result['subjects']
        time.sleep(2)
    data = sorted(data, key=lambda item : float(item['rate']),reverse=True)
    for item in data:
        id = item['id']
        url = 'https://movie.douban.com/j/subject_abstract?subject_id={}'.format(str(id))
        result = requests.get(url, headers).json()
        print(result)
        details.append(result['subject'])
    print(len(data))
    print(len(details))
    save_excel(data, details)

def save_excel(data, details):
    wbk = xlwt.Workbook()
    sheet = wbk.add_sheet('豆瓣前100的电影')
    sheet.write(0, 0, '电影名称')
    sheet.write(0, 1, '评分')
    sheet.write(0, 2, '演员列表')
    sheet.write(0, 3, '导演')
    sheet.write(0, 4, '产地')
    sheet.write(0, 5, '上映时间')
    sheet.write(0, 6, '类型')
    sheet.write(0, 7, '详情')
    for index in range(len(data)):
        sheet.write(index + 1, 0, data[index]['title'])
        sheet.write(index + 1, 1, data[index]['rate'])
        sheet.write(index + 1, 2, ','.join(details[index]['actors']))
        sheet.write(index + 1, 3, ','.join(details[index]['directors']))
        sheet.write(index + 1, 4, details[index]['region'])
        sheet.write(index + 1, 5, details[index]['release_year'])
        sheet.write(index + 1, 6, ','.join(details[index]['types']))
        sheet.write(index + 1, 7, data[index]['url'])
    if (os.path.exists('豆瓣.xls')):
        os.remove('豆瓣.xls')
        wbk.save('豆瓣.xls')
    else:
        wbk.save('豆瓣.xls')

if __name__ == "__main__":
    try:
        get_url_view(urls)
    except BaseException as e :
        print('爬取失败', e)


from django import forms
from .models import Board, Topic, Post

class NewTopicForm(forms.ModelForm) :
    message = forms.CharField(
        widget=forms.Textarea(
            attrs={'rows': 5, 'placeholder': 'What is on your mind?'}),
        max_length=4000,
        help_text='The max length of the text is 4000.')

    class Meta :
        model = Topic
        fields = ['subject', 'message']

class NewBoardForm(forms.ModelForm) :
    description = forms.CharField(
        widget=forms.Textarea(
            attrs={'rows': 3, 'placeholder': 'Describe your form.'}),
        label='Board description', max_length=100,
        help_text='The max length of the text is 1000.')

    class Meta :
        model = Board
        fields = ['name', 'description']

class PostForm(forms.ModelForm) :
    class Meta :
        model = Post
        fields = ['message']


from django.contrib import admin
from django.urls import path, include
from . import views

urlpatterns = [
    path('', views.ProjectListView.as_view(), name='project_list'),
    path('project/create', views.ProjectCreateView.as_view(), name='project_create'),
    path('project/edit/<int:pk>', views.ProjectUpdateView.as_view(), name='project_update'),
    path('project/delete/<int:pk>', views.ProjectDeleteView.as_view(), name='project_delete'),

    #لن نستخدم هذا الرابط فقط كبوست POST نضيف خاصية البوست في ملف الفيو
    path('task/create', views.TaskCreateView.as_view(), name='task_create'),
    path('task/edit/<int:pk>', views.TaskUpdateView.as_view(), name='task_update'),
    path('task/delete/<int:pk>', views.TaskDeleteView.as_view(), name='task_delete'),

]


from project.band_members.musician import Musician

class Guitarist(Musician):

    def __init__(self, name, age):
        super().__init__(name, age)
        self.valid_skills = ["play metal", "play rock", "play jazz"]
        self.skills = []

    def learn_new_skill(self, new_skill: str):
        if new_skill not in self.valid_skills:
            raise ValueError(f"{new_skill} is not a needed skill!")
        elif new_skill in self.skills:
            raise Exception(f"{new_skill} is already learned!")
        elif new_skill in self.valid_skills and new_skill not in self.skills:
            self.skills.append(new_skill)
            return f"{self.name} learned to {new_skill}."


import pickle

from sklearn import metrics
from sklearn import tree

from datatransformation import fromCsv
from ml.classifierParamsProvider import ClassifierParamProvider
from ml.datasetSplitter import split

class ModelTrainer(object):
    def __init__(self, trainData):
        self.trainData = trainData

    def train(self, classifier, outcomeFunction, extendedFeatureList):
        features = [instance.getExtendedFeatures(extendedFeatureList) for instance in self.trainData]
        outcomes = [outcomeFunction(instance) for instance in self.trainData]
        treeModel = classifier
        treeModel.fit(features, outcomes)
        return Model(treeModel, extendedFeatureList)

class Model(object):
    def __init__(self, tree, extendedFeatureList):
        self.tree = tree
        self.extendedFeatureList = extendedFeatureList

    @classmethod
    def fromFile(cls, filename):
        fileHandle = open(filename, 'rb')
        return Model(pickle.load(fileHandle), [])

    def predict(self, instance):
        return self.tree.predict([instance.getExtendedFeatures(self.extendedFeatureList)])[0]

    def visualise(self):
        tree.export_graphviz(self.tree, out_file='../visualisation/tree.dot')

    def store(self, filename):
        pickle.dump(self.tree, open(filename, 'wb'))

def generateReports(csv_train_data, reportPath, paramProvider):
    instances = fromCsv.readCsv(csv_train_data)
    trainSet, testSet = split(instances, 0.7)

    chosenWeights = paramProvider.getChosenWeights()
    outcomeTypes = paramProvider.getOutComeTypes()
    classificationWeightFunctionMap = paramProvider.getClassificationWeightFunctionMap()
    extendedFeatureMap = paramProvider.getExtendedFeatureMap()
    extendedFeatureCombinations = paramProvider.getExtendedFeatureCombinations()

    outcomeindex = 0

    for outcomeFunction in paramProvider.getOutcomeFunctions():
        nameindex = 0
        for classificationWeight in classificationWeightFunctionMap[outcomeTypes[outcomeindex]]:
            for maxDepth in paramProvider.getMaxDepthRange():
                classifiers = paramProvider.getClassifiers(maxDepth, classificationWeight)
                for classifierKey, classifierValue in classifiers.items():
                    create_and_test_model(reportPath, chosenWeights, classifierKey, classifierValue,
                                          extendedFeatureCombinations, extendedFeatureMap, maxDepth,
                                          nameindex, outcomeFunction, outcomeTypes, outcomeindex, testSet,
                                          trainSet)
            nameindex += 1

        outcomeindex += 1

def create_and_test_model(reportPath, chosenWeights, classifierKey, classifierValue, extendedFeatureCombinations,
                          extendedFeatureMap, maxDepth, nameindex, outcomeFunction, outcomeTypes,
                          outcomeindex, testSet, trainSet):
    for extendedFeatureCombinationKey, extendedFeatureCombinationValue in extendedFeatureCombinations.items():
        reportName = generateReportName(classifierKey, maxDepth, extendedFeatureCombinationKey, outcomeTypes,
                                        chosenWeights, outcomeindex, nameindex)
        print(reportName)
        model = train_model(classifierValue, extendedFeatureCombinationValue, extendedFeatureMap, outcomeFunction,
                            trainSet)

        test_model(model, outcomeFunction, reportName, reportPath, testSet)

def test_model(model, outcomeFunction, reportName, reportPath, testSet):
    trueOutcomes = []
    predictedOutcomes = []
    for testInstance in testSet:
        trueOutcomes.append(outcomeFunction(testInstance))
        predictedOutcomes.append(model.predict(testInstance))
    reportToFile(predictedOutcomes, reportName, reportPath, trueOutcomes)

def train_model(classifierValue, extendedFeatureCombinationValue, extendedFeatureMap, outcomeFunction, trainSet):
    modelTrain = ModelTrainer(trainSet)
    model = modelTrain.train(classifierValue, outcomeFunction,
                             [extendedFeatureMap[feature] for feature in
                              extendedFeatureCombinationValue])
    return model

def reportToFile(predictedOutcomes, reportName, reportPath, trueOutcomes):
    reportFile = open(reportPath + reportName + '.txt', 'w')
    reportFile.write(metrics.classification_report(trueOutcomes, predictedOutcomes) + "\n")
    reportFile.write(
        "accuracy: " + str(metrics.accuracy_score(trueOutcomes, predictedOutcomes)) + "\n")
    reportFile.write(
        "confusion matrix:\n" + str(metrics.confusion_matrix(trueOutcomes, predictedOutcomes)))
    reportFile.close()

def generateReportName(classifierKey, maxDepth, extendedFeatureCombinationKey, outcomeTypes, chosenWeights,
                       outcomeindex, nameindex):
    return str(classifierKey) + \
           "_maxDepth" + str(maxDepth) + \
           "_" + extendedFeatureCombinationKey + \
           "_" + str(outcomeTypes[outcomeindex]) + "_" + str(chosenWeights[nameindex])

def generateModel():
    instances = fromCsv.readCsv('../../data/train_preprocessed_split_mix_sex_color_breed.csv')
    trainSet, testSet = split(instances, 0.7)

    modelTrain = ModelTrainer(trainSet)
    model = modelTrain.train(tree.DecisionTreeClassifier(class_weight='balanced'),
                             lambda instance: instance.getBinaryOutcome(), [])
    model.visualise()
    model.store('../models/decisionTree.sav')

if __name__ == '__main__':
    # generateModel()
    generateReports('../../data/train_preprocessed_split_mix_sex_color_breed.csv', "../reports/", ClassifierParamProvider())


while True:
    a = list(map(int, input().split()))
    if a[0] == 0 and a[1] == 0 and a[2] == 0:
        break
    maxi = a.index(max(a))
    hap = 0
    for i in range(3):
        if maxi != i:
             hap += (a[i]**2)
    if hap == (a[maxi]**2):
        print("right")
    else: print("wrong")


import uuid
from threading import Thread

import pika
from thrift.TSerialization import serialize, deserialize

from calc.ttypes import InvalidOperation, Operation, Work, Result

def calculate(work):
    print(f'Calculate({work})')

    if work.op == Operation.ADD:
        val = work.num1 + work.num2
    elif work.op == Operation.SUBTRACT:
        val = work.num1 - work.num2
    elif work.op == Operation.MULTIPLY:
        val = work.num1 * work.num2
    elif work.op == Operation.DIVIDE:
        if work.num2 == 0:
            raise InvalidOperation(work.op, 'Cannot divide by 0')
        val = work.num1 / work.num2
    else:
        raise InvalidOperation(work.op, 'Invalid operation')

    return val

class RpcClient(object):
    def __init__(self):
        self.id1 = '1'  # to run on same device names of queues should differ
        self.id2 = '2'  # todo: remove

        self.response = None
        self.corr_id = None

        self.connection_client = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
        self.channel_client = self.connection_client.channel()
        self.callback_queue_client = self.channel_client.queue_declare(queue='client_queue' + self.id1).method.queue

        self.channel_client.basic_consume(
            queue=self.callback_queue_client,
            on_message_callback=self.on_response,
            auto_ack=True,
        )

        rpc_server = Thread(target=RpcClient.start_server, args=(self,))
        rpc_server.start()

    def start_server(self):
        connection_server = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
        channel_server = connection_server.channel()
        channel_server.queue_declare(queue='server_queue' + self.id1)

        channel_server.basic_qos(prefetch_count=1)
        channel_server.basic_consume(queue='server_queue' + self.id1, on_message_callback=self.on_request)
        channel_server.start_consuming()

    # noinspection PyUnusedLocal
    def on_response(self, ch, method, props, body):
        if self.corr_id == props.correlation_id:
            self.response = body

    # noinspection PyMethodMayBeStatic
    def on_request(self, ch, method, props, body):
        work = deserialize(Work(), body)

        result = Result()
        try:
            result.val = calculate(work)
        except InvalidOperation as ouch:
            result.ouch = ouch

        ch.basic_publish(
            exchange='',
            routing_key=props.reply_to,
            properties=pika.BasicProperties(correlation_id=props.correlation_id),
            body=serialize(result)
        )
        ch.basic_ack(delivery_tag=method.delivery_tag)

    def call(self, work):
        self.response = None
        self.corr_id = str(uuid.uuid4())
        self.channel_client.basic_publish(
            exchange='',
            routing_key='server_queue' + self.id2,
            properties=pika.BasicProperties(
                reply_to=self.callback_queue_client,
                correlation_id=self.corr_id,
            ),
            body=serialize(work)  # binary by default
        )
        while self.response is None:
            self.connection_client.process_data_events()
        return deserialize(Result(), self.response)

if __name__ == '__main__':
    client = RpcClient()

    while True:
        expression = input('Enter expression: ')
        client_work = Work()

        symbol_operator = {
            '+': Operation.ADD,
            '-': Operation.SUBTRACT,
            '*': Operation.MULTIPLY,
            '/': Operation.DIVIDE,
        }

        has_operator = False
        for symbol, operator in symbol_operator.items():
            if symbol in expression:
                if has_operator:
                    raise ValueError(expression)

                split = expression.split(symbol)
                assert len(split) == 2, split

                client_work.num1 = int(split[0])
                client_work.op = operator
                client_work.num2 = int(split[1])

                has_operator = True
        if not has_operator:
            raise ValueError(expression)

        client_result = client.call(client_work)
        print(client_result.val or client_result.ouch)


import numpy as np
import taichi as ti
import taichi.math as tm
from rigid_sim import *

vec2 = tm.vec2
vec3 = tm.vec3

@ti.data_oriented
class SimGUI:
    def __init__(self, sim):
        self.collision = sim.collision
        self.scene = sim.scene
        self.colors = ti.Vector.field(3, shape=(4 * self.scene.N,), dtype=ti.f32)
        self.tri_colors = ti.Vector.field(3, shape=(4 * self.scene.N,), dtype=ti.f32)
        self.widths = ti.field(shape=(4 * self.scene.N,), dtype=ti.f32)
        self.widths.fill(0.1)

        self.init_random_colors()

    @ti.kernel
    def init_random_colors(self):
        for i in range(self.scene.N):
            self.tri_colors[i * 4] = vec3(ti.random(float) * 0.5 + 0.5,
                                          ti.random(float) * 0.5 + 0.5,
                                          ti.random(float) * 0.5 + 0.5)
        for i in self.colors:
            self.tri_colors[i] = self.tri_colors[i // 4 * 4]

    @ti.kernel
    def set_colors(self):
        for i in self.scene.vertices:
            self.colors[i] = tm.vec3(self.collision.coll[i // 4] / 255.0)

    def restart(self):
        self.scene.startScene()

@ti.data_oriented
class A3GUI:
    def __init__(self, width, simGUIs, INITTEST, record=False):
        self.win_width = width
        self.simGUIs = simGUIs
        assert(len(simGUIs) != 0)
        self.currSim = simGUIs[INITTEST]
        self.record = record
        self.iter = 0

        if self.record:
            self.video_manager = ti.tools.VideoManager(output_dir="./output", framerate=60, automatic_build=False)

    def startGUI(self):
        # Create Taichi UI
        self.window = ti.ui.Window("Rigid bodies...", (self.win_width, self.win_width), vsync=True)
        self.canvas = self.window.get_canvas()
        self.canvas.set_background_color((0.3, 0.3, 0.3))

    def is_window_running(self):
        if self.record:
            return self.window.running and self.iter < 300
        else:
            return self.window.running

    def update_iter(self):
        self.iter += 1

    def drawUI(self):
        gui = self.window.get_gui()
        name = "Tests"
        x, y, width, height = 0.02, 0.02, 0.20, 0.20
        with gui.sub_window(name, x, y, width, height):
            is_bw = gui.button(Init.BW_C.value)
            is_bb = gui.button(Init.BB_C.value)
            is_bc = gui.button(Init.BC_C.value)
            is_stack = gui.button(Init.STACK.value)
            is_random = gui.button(Init.RANDOM.value)

            hash = None

            if is_bw:
                hash = Init.BW_C
            elif is_bc:
                hash = Init.BC_C
            elif is_bb:
                hash = Init.BB_C
            elif is_stack:
                hash = Init.STACK
            elif is_random:
                hash = Init.RANDOM

            if hash is not None:
                self.simGUIs[hash].restart()
                self.iter = 0
                self.currSim = self.simGUIs[hash]

    def drawCanvas(self):
        self.canvas.lines(self.currSim.scene.vertices, width=0.004, indices=self.currSim.scene.edge_indices, per_vertex_color=self.currSim.colors)
        self.canvas.lines(self.currSim.scene.boundaries.p, width=0.002, indices=self.currSim.scene.boundary_indices, color=(1.0, 1.0, 0.0))
        if self.currSim.scene.initial_state == Init.RANDOM:
            self.canvas.triangles(self.currSim.scene.vertices,
                                  indices=self.currSim.scene.indices,
                                  per_vertex_color=self.currSim.tri_colors)
        else:
            self.canvas.circles(self.currSim.collision.collPs, radius=0.01, color=(1, 0, 1))

        if self.record and self.iter % 1 == 0:
            img = self.window.get_image_buffer_as_numpy()
            self.video_manager.write_frame(img)
        self.window.show()

    def endGUI(self):
        if self.record:
            self.video_manager.make_video(gif=False, mp4=True)


"""Write a program that reads a matrix from the console and prints the sum for each column on separate lines.
On the first line, you will get matrix sizes in format "{rows}, {columns}". On the next rows, you will get
elements for each column separated with a single space."""

### Variant 1 ###
# row, column = map(int, input().split(", "))
# matrix = [[int(num) for num in input().split()] for _ in range(row)]
# for column_index in range(column):
#     current_sum = 0
#     for row_index in range(row):
#         current_sum += matrix[row_index][column_index]
#     print(current_sum)

### Variant 2 ###
row, column = map(int, input().split(", "))
matrix = [[int(num) for num in input().split()] for _ in range(row)]
column_sums = [0] * row
for row_index in range(row):
    for column_index in range(column):
        value = matrix[row_index][column_index]
        column_sums[column_index] += value
print(column_sums)

Python Advanced/Multidimensional Lists - LAB/04_Sum_Matrix_Columns.py


#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
# @Time       : 2019/12/12
# @Author     : S1mple
#
# Description :

def usr_input():
	unsort_list = []
	s = input("Start to enter numbers (y/n):")
	if s == "y" or s == "Y":
		while True:
			t = input("enter a number (q to quit):")
			if t == "q" or t == "Q": return unsort_list
			try:
				t = int(t)
				unsort_list.append(t)
			except Exception as e:
				print("%s is not a number, stop!"%t)
				return unsort_list
	else:
		exit(0)

def merge(a, b):
	c = []
	i = j = 0
	while i < len(a) and j < len(b):
		if a[i] < b[j]:
			c.append(a[i])
			i += 1
		else:
			c.append(b[j])
			j += 1
	c.extend(a[i:])
	c.extend(b[j:])
	return c

def merge_sort(lts):
	if len(lts) <= 1:
		return lts
	middle = len(lts)//2
	left = merge_sort(lts[:middle])
	right = merge_sort(lts[middle:])
	return merge(left, right)

if __name__ == "__main__":
	print("This is a script used to sort a list of numbers by insertion sort.")
	unsort_list = usr_input()
	print("your numbers list looks like this:")
	for i in unsort_list: print(str(i)+' ', end='')
	sorted_list = merge_sort(unsort_list)
	print('')
	print("After insertion sort, your numbers list looks like this:")
	for i in sorted_list: print(str(i)+' ', end='')
	print('')


from typing import List

class Solution:
    def findPairs(self, nums: List[int], k: int) -> int:
        """

        :param nums: array of numbers
        :param k: difference
        :return: number of suitable pairs
        use conjugate/pair tagging in a hash set to track what has been seen
        return the length of the set of pairs
        """

        pairs: set = set()
        seen: set = set()

        for i in nums:
            c1 = k - i
            c2 = k + i
            print(i, k, c1, c2)
            if c1 in seen and abs(c1 - i) == k:
                pairs.add((max(c1, i), min(c1, i)))
            elif -c1 in seen and abs(-c1 - i) == k:
                pairs.add((max(-c1, i), min(-c1, i)))

            if c2 in seen and abs(c2 - i) == k:
                pairs.add((max(c2, i), min(c2, i)))
            elif -c2 in seen and abs(-c2 - i) == k:
                pairs.add((max(-c2, i), min(-c2, i)))
            seen.add(i)
        print(pairs)
        return len(pairs)


import sys, getopt
from gan import train_gan

if __name__ == '__main__':
    try:
        opts, args = getopt.getopt(sys.argv[1:],
                                   't:v:o:d:g:b:r:n:l:s:eac:i:',
                                   ['train_data_dir=', 'val_data_dir=', 'output_dir=',
                                    'D_lr=', 'G_lr=', 'beta1=', 'reg=', 'num_epochs=', 'loss=', 'batch_size=',
                                    'eval_val', 'save_eval_img', 'device=', 'num_eval_img='])
    except getopt.GetoptError:
        print ('train_gan.py -t <train_data_dir> -v <val_data_dir> -o <output_dir> -d <D_lr> -g <G_lr> -b <beta1> -r <reg> -n <num_epochs> -l <loss> -s <batch_size> -e -a -c <device>')
        sys.exit(2)
    for opt, arg in opts:
        if opt in ('-t','--train_data_dir'):
            train_data_dir = arg
        elif opt in ('-v', '--val_data_dir'):
            val_data_dir = arg
        elif opt in ('-o', '--output_dir'):
            output_dir = arg
        elif opt in ('-d', '--D_lr'):
            D_lr = float(arg)
        elif opt in ('-g', '--G_lr'):
            G_lr = float(arg)
        elif opt in ('-b', '--beta1'):
            beta1 = float(arg)
        elif opt in ('-r', '--reg'):
            reg = float(arg)
        elif opt in ('-n', '--num_epochs'):
            num_epochs = int(arg)
        elif opt in ('-l', '--loss'):
            loss = arg
        elif opt in ('-s', '--batch_size'):
            batch_size = int(arg)
        elif opt in ('-e', '--eval_val'):
            eval_val = True
        elif opt in ('-a', '--save_eval_img'):
            save_eval_img = True
        elif opt in ('-c', '--device'):
            device = arg
        elif opt in ('-i', '--num_eval_img'):
            num_eval_img = int(arg)

    print('Running train_gan.py with parameters: --train_data_dir=' + train_data_dir + \
          ', --val_data_dir=' + val_data_dir + ', --output_dir=' + output_dir + \
          ', --D_lr=' + str(D_lr) + ', --G_lr=' + str(G_lr) + \
          ', --beta1=' + str(beta1) + ', --reg=' + str(reg) + ', --num_epochs=' + str(num_epochs) + \
          ', --loss=' + loss + ', --batch_size=' + str(batch_size) + ', --eval_val=' + str(eval_val) + \
          ', --save_eval_img=' + str(save_eval_img) + ', --device=' + device + ', --num_eval_img=' + str(num_eval_img))

    train_gan(train_data_dir=train_data_dir, val_data_dir=val_data_dir, output_dir=output_dir,
              D_lr=D_lr, G_lr=G_lr, beta1=beta1, reg=reg, num_epochs=num_epochs, loss=loss,
              batch_size=batch_size, eval_val=eval_val, save_eval_img=save_eval_img, device=device,
              num_eval_img=num_eval_img)
    print('Finished training GAN!')


from ultralytics import YOLO
import os

input_model_list = ["yolov8n.pt", "yolov8s.pt", "yolov8m.pt", "yolov8l.pt", "yolov8x.pt"]
base_dir = os.path.dirname(os.path.abspath(__file__))

# input_model_list = [input_model_list[0]] #only take one model

for input_model in input_model_list:
    print("Beginning converting model: ", input_model)

    input_model_path = os.path.join(base_dir, input_model)
    output_model_path = input_model_path.replace(".pt", ".onnx")
    model = YOLO(input_model_path)

    #Delete old output model
    if os.path.exists(output_model_path):
        os.remove(output_model_path)
    path = model.export(format="onnx", imgsz=640, opset=12)  # export the model to ONNX format
    os.rename(path, output_model_path)
    print("Finish converting model: ", output_model_path)


# -*- coding: utf-8 -*-

"""
Implementation of the RASA engine.

Repository:

    https://github.com/RasaHQ/rasa

"""
import asyncio
import shutil
from pathlib import Path
from typing import Dict

import rasa
from pylogics.syntax.base import Formula
from rasa.core.agent import Agent

from nl2ltl.engines.base import Engine
from nl2ltl.engines.rasa import ENGINE_ROOT
from nl2ltl.engines.rasa.helpers import _get_latest_model
from nl2ltl.engines.rasa.output import RasaOutput, parse_rasa_output, parse_rasa_result
from nl2ltl.filters.base import Filter

engine_root = ENGINE_ROOT
DATA_DIR = engine_root / "data"
DOMAIN_PATH = engine_root / DATA_DIR / "domain.yml"
CONFIG_PATH = engine_root / DATA_DIR / "config.yml"
TRAINING_PATH = engine_root / DATA_DIR / "nlu_training.yml"
MODEL_OUTPUT_PATH = engine_root / "models"

class RasaEngine(Engine):
    """The RASA engine."""

    def __init__(
        self,
        model: Path = None,
    ):
        """Rasa NLU Engine initialization."""
        self._load_model(model)

        self._check_consistency()

    def _load_model(self, model: Path = None):
        if model:
            self.agent = Agent.load(model)
        else:
            self.agent = Agent.load(_get_latest_model(path=MODEL_OUTPUT_PATH))

    def _check_consistency(self) -> None:
        """Run consistency checks."""
        self.__check_rasa_available()
        self.__check_rasa_version()

    def __check_rasa_available(self):
        """Check that the Rasa tool is available."""
        is_rasa_present = shutil.which("rasa") is not None
        if is_rasa_present is None:
            raise Exception(
                "Rasa is not installed. Please follow"
                "the installation instructions at https://rasa.com/docs/rasa/installation.\n"
            )

    def __check_rasa_version(self):
        """Check that the Rasa tool is at the right version."""
        is_right_version = rasa.__version__ == "3.5.11"
        if not is_right_version:
            raise Exception(
                "Rasa needs to be at version 3.5.11. "
                "Please install it manually using:"
                "\n"
                "pip install rasa==3.5.11"
            )

    @staticmethod
    def train(
        domain: Path = DOMAIN_PATH,
        config: Path = CONFIG_PATH,
        training: Path = TRAINING_PATH,
    ) -> Path:
        """Train a Rasa model."""
        result = rasa.train(
            domain=str(domain),
            config=str(config),
            training_files=[str(training)],
            output=str(MODEL_OUTPUT_PATH),
            force_training=True,
        )
        return Path(result.model)

    def translate(
        self, utterance: str, filtering: Filter = None
    ) -> Dict[Formula, float]:
        """From NL to best matching LTL formulas with confidence."""
        return _process_utterance(utterance, self.agent, filtering)

def _process_utterance(
    utterance: str, rasa_agent: Agent, filtering: Filter
) -> Dict[Formula, float]:
    """
    Process NL utterance.

    :param utterance: the natural language utterance
    :return: a dict with matching formulas and confidence
    """
    prediction = asyncio.run(rasa_agent.parse_message(utterance.strip()))
    rasa_result: RasaOutput = parse_rasa_output(prediction)
    matching_formulas: Dict[Formula, float] = parse_rasa_result(rasa_result, filtering)
    return matching_formulas


from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from rest_framework.parsers import JSONParser
from django.http.response import JsonResponse

from .models import Departments, Employees
from .serializer import DepartmentSerializer, EmployeeSerializer

from django.core.files.storage import default_storage

@csrf_exempt
def department(request, id=0):
    if request.method =='GET':
        departments = Departments.objects.all()
        departments_serializer = DepartmentSerializer(departments, many=True)
        return JsonResponse(departments_serializer.data, safe=False)

    elif request.method == "POST":
        department_data = JSONParser().parse(request)
        department_serializer = DepartmentSerializer(data=department_data)
        if department_serializer.is_valid():
            department_serializer.save()
            return JsonResponse("Added sucessfully!", safe=False)
        return JsonResponse("Falied to add", safe=False)

    elif request.method =="PUT":
        department_data = JSONParser().parse(request)
        department = Departments.objects.get(DepartmentId=department_data['DepartmentId'])
        department_serializer = DepartmentSerializer(department, data=department_data)
        if department_serializer.is_valid():
            department_serializer.save()
            return JsonResponse("Updated Sucessfully", safe=False)
        return JsonResponse("Failed to Update", safe=False)

    elif request.method == 'DELETE':
        department = Departments.objects.get(DepartmentId=id)
        department.delete()
        return JsonResponse("Deleted Sucessfully", safe=False)

@csrf_exempt
def employee(request, id=0):
    if request.method =='GET':
        employees = Employees.objects.all()
        employees_serializer = EmployeeSerializer(employees, many=True)
        return JsonResponse(employees_serializer.data, safe=False)

    elif request.method == "POST":
        employees_data = JSONParser().parse(request)
        employees_serializer = EmployeeSerializer(data=employees_data)
        if employees_serializer.is_valid():
            employees_serializer.save()
            return JsonResponse("Added sucessfully!", safe=False)
        return JsonResponse("Falied to add", safe=False)

    elif request.method =="PUT":
        employees_data = JSONParser().parse(request)
        employee = Employees.objects.get(EmployeeId=employees_data['EmployeeId'])
        employees_serializer = EmployeeSerializer(employee, data=employees_data)
        if employees_serializer.is_valid():
            employees_serializer.save()
            return JsonResponse("Updated Sucessfully", safe=False)
        return JsonResponse("Failed to Update", safe=False)

    elif request.method == 'DELETE':
        employee = Employees.objects.get(EmployeeId=id)
        employee.delete()
        return JsonResponse("Deleted Sucessfully", safe=False)

@csrf_exempt
def savefile(request):
    file=request.FILES['uploadedFile']
    file_name = default_storage.save(file.name, file)
    return JsonResponse(file_name, safe=False)


from lib.movie import Movie
from lib.rental import Rental
from lib.customer import Customer

""" Define the class of statement """
class Statement:

    def __init__(self, customer: Customer, amounts: float=0.0, points: int=0, content: list=[]):
        self.customer_name = customer.name
        self.amounts = amounts
        self.points = points
        self.content = content

        """ generate statement content in initialization """
        rentals = customer.rentals

        self.content = [f"Rental Record for {customer.name}\n"] # customer info
        self.content += [f"\t {each.movie.title} \t {each.get_amount()}\n"
                          for each in rentals] # rental infos
        total_amount = sum([each.get_amount() for each in rentals]) # total amount due
        renter_points = sum([each.get_point() for each in rentals]) # total rewards point

        self.content.append(f"Amount owed is ${total_amount}\n") # amount summary
        self.content.append(f"You earned {renter_points} frequent renter points.\n") # rewards summary

    def print_content(self):
        print("".join(self.content))


""" O(n) 622 ms (beats 74.97%), 30.42 MB (beats 83.1%) """

from typing import List

def solve(nums: List[int]) -> int:
    max_v = max(nums)

    if max_v <= 0:
        return max_v

    nums = compress(nums)
    max_unit = max(nums)  # get max positive unit

    # Onion Transform

    max_seq = 0

    while len(nums) >= 3:
        if nums[-1] <= 0:
            nums.pop()
        else:
            layer = nums.pop() + nums.pop()

            if layer > 0:
                nums[-1] += layer

            max_seq = max(max_seq, nums[-1])

    max_seq = max(max_seq, sum(nums))

    return max(max_unit, max_seq)

def compress(nums: List[int]) -> List[int]:
    n = len(nums)

    nums2 = []
    i = 0

    while i < n:
        acc = 0

        if nums[i] >= 0:
            while i < n and nums[i] >= 0:
                acc += nums[i]
                i += 1
        else:
            while i < n and nums[i] < 0:
                acc += nums[i]
                i += 1

        nums2.append(acc)

    return nums2

if __name__ == '__main__':
    def test(input, expect):
        found = solve(input)
        assert found == expect, f"expect: {expect}, however found: {found}"

    test([-2,1,-3,4,-1,2,1,-5,4], 6)
    test([1], 1)
    test([5,4,-1,7,8], 23)


import pyxel
import random

class SnakeGame:
    def __init__(self):
        # Pyxelウィンドウの初期化（160x120のサイズで）
        pyxel.init(50, 50)
        # ゲームの初期状態を設定
        self.reset_game()
        # Pyxelのアップデート（ロジック）とドロー（描画）メソッドを設定
        pyxel.run(self.update, self.draw)

    def reset_game(self):
        # ヘビの初期位置と初期方向を設定
        self.snake = [(10, 10)]
        self.direction = (1, 0)
        # 食べ物の初期位置を生成
        self.food = self.spawn_food()
        # スコアとゲームオーバーフラグの初期化
        self.score = 0
        self.game_over = False
        # ゲームのタイマーを設定（30秒）
        self.timer = 30

    def spawn_food(self):
        # 食べ物をランダムな位置に生成
        while True:
            food = (random.randint(0, 49), random.randint(0, 49))
            # ヘビの体と重ならない位置に食べ物を配置
            if food not in self.snake:
                return food

    def update(self):
        self.update_snake()
        # ゲームオーバー時の処理
        if self.game_over:
            # Rキーでゲームをリセット
            if pyxel.btnp(pyxel.KEY_R):
                self.reset_game()
                return

        # ヘビの頭が食べ物に触れた場合の処理
        if self.snake[0] == self.food:
            self.score += 1
            self.timer += 1
            # ヘビの長さを増やす
            self.snake.append(self.snake[-1])
            # 新しい食べ物を生成
            self.food = self.spawn_food()

        # スコアが一定値に達した時のステージ変更は省略

    def update_snake(self):

        # ヘビの新しい頭の位置を計算
        new_head_x = (self.snake[0][0] + self.direction[0]) % pyxel.width
        new_head_y = (self.snake[0][1] + self.direction[1]) % pyxel.height
        new_head = (new_head_x, new_head_y)
        # ヘビが自分自身に触れた場合、ゲームオーバー
        if new_head in self.snake[1:]:
            self.game_over = True

        # ヘビの位置を更新
        self.snake = [new_head] + self.snake[:-1]

        # キーボード入力によるヘビの方向の変更
        if pyxel.btnp(pyxel.KEY_LEFT): self.direction = (-1, 0)
        if pyxel.btnp(pyxel.KEY_RIGHT): self.direction = (1, 0)
        if pyxel.btnp(pyxel.KEY_UP): self.direction = (0, -1)
        if pyxel.btnp(pyxel.KEY_DOWN): self.direction = (0, 1)

    def draw(self):
        # 画面をクリア
        pyxel.cls(0)
        # ゲームオーバー時の表示
        if self.game_over:
            pyxel.text(55, 45, "GAME OVER", pyxel.frame_count % 16)
            pyxel.text(48, 55, "Press R to Restart", 7)
            return

        # スコアとタイマーの表示
        pyxel.text(2.5, 2.5, f"Score: {self.score}", 7)
        pyxel.text(2.5,2.5, f"Score: {self.score}", 7)
        pyxel.text(2.5, 7.5, f"Time: {int(self.timer)}", 7)

        # ヘビと食べ物の描画
        for x, y in self.snake:
            pyxel.rect(x, y, 1, 1, 11)
        pyxel.rect(self.food[0], self.food[1], 1, 1, 8)

SnakeGame()


from random import uniform

class Arbitrator:
    def __init__(self, bbcon):
        self.bbcon = bbcon  # En pointer til bbcon-objektet

    def choose_action(self):
        # hent ut en tilfeldig aksjon fra lista med en vektet sannsynlighet
        recommendations = self.bbcon.recommendations
        r_sum = 0
        for recommendation in recommendations:
            # print ('checking recommendation:',recommendation)
            if recommendation[1] == 1:  # Returnerer recommendationen dersom den har prioritet 1
                return recommendation
            else:
                r_sum += recommendation[1]
        rand_num = uniform(0, r_sum)
        for recommendation in recommendations:
            if rand_num <= recommendation[1]:
                return recommendation
            else:
                rand_num -= recommendation[1]

# class Bbcon:
#     def __init__(self):
#         self.recommendations = [["a", 0.5], ["b", 0.7], ["c", 0.9], ["d", 1]]
#         arb = Arbitrator(self)
#         print(arb.choose_action())

# if __name__ == "__main__":
#     bbcon = Bbcon()


from datetime import datetime

from flask import Blueprint, jsonify, request

from app.dao.fact_notification_status_dao import (
    get_total_notifications_for_date_range,
)
from app.dao.fact_processing_time_dao import (
    get_processing_time_percentage_for_date_range,
)
from app.dao.services_dao import get_live_services_with_organisation
from app.errors import register_errors
from app.performance_dashboard.performance_dashboard_schema import (
    performance_dashboard_request,
)
from app.schema_validation import validate

performance_dashboard_blueprint = Blueprint("performance_dashboard", __name__, url_prefix="/performance-dashboard")

register_errors(performance_dashboard_blueprint)

@performance_dashboard_blueprint.route("")
def get_performance_dashboard():
    # All statistics are as of last night this matches the existing performance platform
    # and avoids the need to query notifications.
    if request.args:
        # Is it ok to reuse this? - should probably create a new one
        validate(request.args, performance_dashboard_request)

    # If start and end date are not set, we are expecting today's stats.
    today = str(datetime.utcnow().date())

    start_date = datetime.strptime(request.args.get("start_date", today), "%Y-%m-%d").date()
    end_date = datetime.strptime(request.args.get("end_date", today), "%Y-%m-%d").date()
    total_for_all_time = get_total_notifications_for_date_range(start_date=None, end_date=None)
    total_notifications, emails, sms, letters = transform_results_into_totals(total_for_all_time)
    totals_for_date_range = get_total_notifications_for_date_range(start_date=start_date, end_date=end_date)
    processing_time_results = get_processing_time_percentage_for_date_range(start_date=start_date, end_date=end_date)
    services = get_live_services_with_organisation()
    stats = {
        "total_notifications": total_notifications,
        "email_notifications": emails,
        "sms_notifications": sms,
        "letter_notifications": letters,
        "notifications_by_type": transform_into_notification_by_type_json(totals_for_date_range),
        "processing_time": transform_processing_time_results_to_json(processing_time_results),
        "live_service_count": len(services),
        "services_using_notify": transform_services_to_json(services),
    }

    return jsonify(stats)

def transform_results_into_totals(total_notifications_results):
    total_notifications = 0
    emails = 0
    sms = 0
    letters = 0
    for x in total_notifications_results:
        total_notifications += x.emails
        total_notifications += x.sms
        total_notifications += x.letters
        emails += x.emails
        sms += x.sms
        letters += x.letters
    return total_notifications, emails, sms, letters

def transform_into_notification_by_type_json(total_notifications):
    j = []
    for x in total_notifications:
        j.append({"date": x.bst_date, "emails": x.emails, "sms": x.sms, "letters": x.letters})
    return j

def transform_processing_time_results_to_json(processing_time_results):
    j = []
    for x in processing_time_results:
        j.append({"date": x.date, "percentage_under_10_seconds": x.percentage})

    return j

def transform_services_to_json(services_results):
    j = []
    for x in services_results:
        j.append(
            {
                "service_id": x.service_id,
                "service_name": x.service_name,
                "organisation_id": x.organisation_id,
                "organisation_name": x.organisation_name,
            }
        )
    return j