from rest_framework import viewsets, mixins
from rest_framework.decorators import list_route
from rest_framework.response import Response

from apps.admins.models import *
from apps.admins.serializers import *
from util.errors import ParameterException

# 用户视图
class WhAdminViewSet(viewsets.GenericViewSet, mixins.ListModelMixin,
                     mixins.RetrieveModelMixin, mixins.CreateModelMixin):
    queryset = WhAdmin.objects.all()
    serializer_class = WhAdminSerializer
    # authentication_classes = (WhAdminTokenAuthentication,)

    def list(self, request, *args, **kwargs):
        token = request.query_params.get("token")
        user_id = cache.get(token)
        user = WhAdmin.objects.filter(admin_id=user_id).first()
        if user:  # 如果用户已经登录的情况
            serializer = self.get_serializer(user)  # 返回serializer_class指向的序列化类的对象
            res = {
                "user_info": serializer.data,
                "flag": True  # 是否登录的标识
            }
            return Response(res)
        else:
            res = {
                "flag": False  # 是否登录的标识
            }
            return Response(res)

    # @list_route注册一个新API，形如：/methodname/
    @list_route(methods=["POST"], serializer_class=WhAdminRegisterSerializer)
    def register(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        # raise_exception=False,未通过验证，不抛出异常，返回值，继续往下执行
        # is_valid()会联动调用序列化对象的validate（）方法
        result = serializer.is_valid(raise_exception=True)
        if not result:
            raise ParameterException({"code": "1005", "msg": "注册数据未通过验证！"})

        print("serializer.data=====")
        print(type(serializer.data))
        data = serializer.register_data(serializer.data)  # 调用封装的注册方法，返回字典
        return Response(data)

    @list_route(methods=["POST"], serializer_class=WhAdminLoginSerializer)
    def login(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        result = serializer.is_valid(raise_exception=False)

        if not result:
            raise ParameterException({'code': '1005', 'msg': '登陆数据未通过验证！'})
        res = serializer.login_data(serializer.data)
        return Response(res)

    @list_route(methods=['POST', "GET"], )
    def logout(self, request, *args, **kwargs):
        token = request.data.get("token")
        user_id = cache.get(token)  # 获取token对相应的用户id
        user = WhAdmin.objects.filter(admin_id=user_id).exists()
        data = {}
        if user:
            cache.delete(token)
            data['code'] = 200
            data['msg'] = '账户已退出！！'
        return Response(data)

# 分组视图
class WhAdminGroupViewSet(viewsets.GenericViewSet, mixins.ListModelMixin,
                     mixins.RetrieveModelMixin, mixins.CreateModelMixin):
    queryset = WhAdminGroup.objects.all()
    serializer_class = WhAdminGroupSerializer


import pytest

import torch
from hypothesis import assume, given
from hypothesis.strategies import text
from transformers import Wav2Vec2CTCTokenizer

from tests.utils import mark_slow
from thunder.huggingface.compatibility import _tok_to_transform

@pytest.fixture(scope="session")
def hf_tokenizer():
    return Wav2Vec2CTCTokenizer.from_pretrained("facebook/wav2vec2-base-960h")

@pytest.fixture(scope="session")
def thunder_tokenizer(hf_tokenizer):
    return _tok_to_transform(hf_tokenizer)

@mark_slow
@given(inp=text(min_size=1))
def test_tok(inp: str, hf_tokenizer, thunder_tokenizer):
    assume("|" not in inp)
    out1 = hf_tokenizer([inp], return_tensors="pt").input_ids
    out2 = thunder_tokenizer.encode([inp], return_length=False)
    assert torch.allclose(out1, out2)

@mark_slow
def test_pretrained_problematic_tokens():
    tok = Wav2Vec2CTCTokenizer.from_pretrained(
        "lgris/wav2vec2-large-xlsr-open-brazilian-portuguese-v2"
    )
    text_transform = _tok_to_transform(tok)
    assert text_transform.num_tokens == 44
    assert text_transform.vocab.start_token is None


import datetime
import urllib.parse
import urllib.request
import zlib

#Functions to retrieve schedules
class HubCacheRetriever:
  def __init__(self, appgwUrl, slot):
    self.appgwUrl = appgwUrl
    self.slot = slot

  def getCache(self, timestamp, channelMapId, popularityOrder = False):
    cacheUrl = self._buildCacheUrl(timestamp, channelMapId, popularityOrder)
    print("The channel map hub catchup cache url generated is : \n", cacheUrl)
    cache = None
    self.lastRequestedUrl = cacheUrl
    try:
      compressedCache = urllib.request.urlopen(cacheUrl)
      decompressedData = zlib.decompress(compressedCache.read(), 16+zlib.MAX_WBITS)
      cache = decompressedData.decode('utf-8')
    except urllib.error.HTTPError:
      print("HTTPError when trying to obtain Hub Cache. URL:" + cacheUrl)
    return cache

  def _buildCacheUrl(self, timestamp, channelMapId, popularityOrder = False):
    utcDateStr = datetime.datetime.utcfromtimestamp(int(timestamp)).strftime('%Y-%m-%d')
    blobContainerName = str(self.slot).lower() + "-catchup-data-" + utcDateStr
    strOrderBy = "Popularity-" if popularityOrder else ""
    cacheFileName = "channelmap-hub-" + strOrderBy + channelMapId + ".gz"
    #format of the url: http://{appgwUrl}/catalogcache/{bloblContainerName}/channelmap-hub-[Popularity-]{channelMapId}.gz
    cacheUrl = self.appgwUrl.replace("{type}", "storage") + "catalogcache/" + blobContainerName + "/" + cacheFileName
    return cacheUrl

class PerStationCacheRetriever:
  def __init__(self, appgwUrl, slot):
    self.appgwUrl = appgwUrl
    self.slot = slot

  def getCache(self, requestedTimestamp, countPerStation, stations, top, popularityOrder = False):
    cacheUrl = _buildPerStationCatchupCacheUrl(requestedTimestamp, countPerStation, stations, top, popularityOrder)
    print("The PerStationCatchupSchedules url generated is : \n", cacheUrl)
    cache = None
    self.lastRequestedUrl = cacheUrl
    try:
      cache = urllib.request.urlopen(cacheUrl)
    except urllib.error.HTTPError:
      print("HTTPError when trying to obtain PerStation Cache. URL:" + cacheUrl)
    return cache

  def _buildCacheUrl(self, requestedTimestamp, countPerStation, stations, top, popularityOrder = False):
    params = {}
    dateStr = datetime.datetime.utcfromtimestamp(requestedTimestamp).strftime('%Y-%m-%dT%H:%M:%S.000Z')
    params['requestedTime'] = dateStr
    params['pivots'] = "Language|en"
    params['countPerStation'] = countPerStation
    params['$orderBy'] = "Popularity" if popularityOrder else ""
    params['$stations'] = ",".join(map(str, stations))
    params['$top'] = top
    params['$lang'] = "en-US"
    strUrlEncodedParams = urllib.parse.urlencode(params)
    return appgwUrl.replace("{type}", "client") + slot.upper() + "/discovery/v3/feeds/catchup/pivot-items?" + strUrlEncodedParams

class StationCacheRetriever:
  def __init__(self, appgwUrl, slot):
    self.appgwUrl = appgwUrl
    self.slot = slot

  def getCache(self, requestedTime, stationId, skip, top, popularityOrder = False):
    cacheUrl = _buildStationCatchupCacheUrl(requestedTime, stationId, skip, top, popularityOrder)
    print("The StationCatchupSchedules url generated is : \n", cacheUrl)
    cache = None
    self.lastRequestedUrl = cacheUrl
    try:
      cache = urllib.request.urlopen(cacheUrl)
    except urllib.error.HTTPError:
      print("HTTPError when trying to obtain Station Cache. URL:" + cacheUrl)
    return cache

  def _buildCacheUrl(self, requestedTimestamp, stationId, skip, top, popularityOrder = False):
    params = {}
    dateStr = datetime.datetime.utcfromtimestamp(requestedTimestamp).strftime('%Y-%m-%dT%H:%M:%S.000Z')
    params['requestedTime'] = dateStr
    params['pivots'] = "Language|en"
    params['$skip'] = skip
    params['$orderBy'] = "Popularity" if popularityOrder else ""
    params['$stations'] = stationId
    params['$top'] = top
    params['$lang'] = "en-US"
    strUrlEncodedParams = urllib.parse.urlencode(params)
    return self.appgwUrl.replace("{type}", "client") + self.slot.upper() + "/discovery/v3/libraries/catchup/items?" + strUrlEncodedParams
#End of Functions to retrieve schedules


class Human:
    #class Attribute
    Name = None
    age = None
    country = None

hasan = Human()
hasan.Name = "Hasan Mahamud"
hasan.age = 100
hasan.country = "Bangladesh"

arafat = Human()
arafat.Name = "Arafat Hassan"
arafat.age = 80
arafat.country = "Bangladesh"

print(hasan.Name)
print(arafat.age)


from django.shortcuts import render
from rest_framework.views import APIView
from django.contrib.auth import get_user_model
import json
from rest_framework.response import Response
from rest_framework import status
from .serializers import *
from .models import *
from Kiepe.administrator.models import *
import datetime as dt
import base64
from geopy.geocoders import Nominatim

def reverse_geocoding(coordinates):
    while True:
        try:
            geolocator = Nominatim(user_agent="Kiepe")
            location = geolocator.reverse(coordinates)
            return location.address
        except:
            continue
        break
    return None

# Create your views here.
class CompleteKibandaProfile(APIView):
    def post(self, request):
        try:
            user_id = request.data.get("user_id")
            fname = request.data.get("fname")
            lname = request.data.get("lname")
            idtype = request.data.get("idtype")
            idnumber = request.data.get("idnumber")
            brand = request.data.get("brand")
            profile = request.data.get("profile")
            cover = request.data.get("cover")
            coords = request.data.get("coords")
            physical_address = reverse_geocoding(coords)

            print("coordinates ", coords, type(coords))

            user = get_user_model().objects.get(id=int(user_id))
            if hasattr(user, "kibanda"):
                kibanda = user.kibanda

                kibanda.first_name = fname
                kibanda.last_name = lname
                # i expect to receive coords as array... print it before try using it here
                kibanda.coordinates  = coords
                kibanda.brand_name = brand
                kibanda.aina_ya_ID = idtype
                kibanda.ID_number = idnumber
                kibanda.physical_address = physical_address

                # how to save base64 image https://stackoverflow.com/questions/51514481/typeerror-expected-str-bytes-or-os-pathlike-object-not-imagefieldfile
                if (profile != 'null' and profile != None):
                    kibanda.image = profile
                    # base64_image = base64.b64encode(profile.read())
                    # kibanda.profile_base64 = base64_image

                if (cover != 'null' and cover != None):
                    kibanda.cover_photo = cover
                    # base64_image = base64.b64encode(cover.read())
                    # kibanda.cover_photo_base64 = base64_image
                kibanda.is_active = False
                kibanda.profile_is_completed = True

                kibanda.save()

                serializer = KibandaProfileSerializer(kibanda)

                return Response(serializer.data, status=status.HTTP_200_OK)

            elif hasattr(user, "customer"):
                # delete the user "customer" profile
                # create a new kibanda profile
                customer = user.customer
                customer.delete()

                kibanda = KibandaProfile.objects.create(
                    user=user,
                    first_name=fname,
                    last_name=lname,
                    coordinates=coords,
                    brand_name=brand,
                    aina_ya_ID=idtype,
                    ID_number=idnumber,
                    is_active=False,
                    profile_is_completed=True,
                    physical_address=physical_address,
                )

                if (profile != 'null' and profile != None):
                    kibanda.image = profile
                    # base64_image = base64.b64encode(profile.read())
                    # kibanda.profile_base64 = base64_image

                if (cover != 'null' and cover != None):
                    kibanda.cover_photo = cover
                    # base64_image = base64.b64encode(cover.read())
                    # kibanda.cover_photo_base64 = base64_image

                kibanda.save()

                status_kibanda = KibandaStatus.objects.create(
                    kibanda=kibanda,
                )
                status_kibanda.save()

                serializer = KibandaProfileSerializer(kibanda)

                return Response(serializer.data, status=status.HTTP_200_OK)

        except Exception as e:
            print(e)
            return Response({"message": "An error occurred"}, status=status.HTTP_400_BAD_REQUEST)

complete_kibanda_profile = CompleteKibandaProfile.as_view()

class EditDefaultMenuItem(APIView):
    def post(self, request):
        try:
            default_id = request.data.get("default_id")
            menuItemsMetadata = request.data.get("mt")
            user_id = request.data.get("user_id")
            user = get_user_model().objects.get(id=int(user_id))
            kibanda = user.kibanda
            print('this is mt i receive ', menuItemsMetadata, type(menuItemsMetadata), menuItemsMetadata['existing'])
            defaultMenu = DefaultMenu.objects.get(id=int(default_id))
            # don't wipe all of em... we expect to get data which looks like this
            # { existing: [{id, price}], new: [{item, price}] }
            # defaultMenu.menu.clear()
            # also if menu is not required it should not be in either existing so first get all
            # menu and see if there is not in existing u should remove it...
            existing_menu = menuItemsMetadata['existing']
            print('HEY THIS IS EXISTING MENU... ', existing_menu)
            existing_menu_parentId = []
            for menu in existing_menu:
                existing_menu_parentId.append(int(menu['id']))

            print('THIS ARE EXISTING MENU PARENTID ', existing_menu_parentId)
            # you we need to delte the menu item which is not found in our incoming existing mt
            for item in defaultMenu.menu.all():
                if item.menu.id not in existing_menu_parentId:
                    item.menu.delete()

            # then we should change the price of all menu found in existing_menu
            print('IM TRYING TO UPDATE THE PRICE OF ITEM')
            for emenu in existing_menu:
                # its okay since we're targetting Menuitem bu here we check for administrator menu..
                parentMenu = Menu.objects.filter(id=int(emenu['id']))
                if parentMenu.count() < 1:
                    break
                parentMenu = parentMenu.last()
                print('this is the parent menu ', parentMenu)
                menuitem = None
                for mi in defaultMenu.menu.all():
                    print('MIMI NI ', mi, mi.menu.id, parentMenu.id)
                    if mi.menu.id == parentMenu.id:
                        menuitem = mi
                        break
                # we have that "menu" lets change its price..
                print("THIS IS MENU ITEM WE TARGET ", menuitem)
                menuitem.price = emenu['price']
                menuitem.save()

            # let deal with the new menu, remember new menu should be created..

            print('IM TRYING TO MANIPULATE NEW ADDED DATA')
            for item in menuItemsMetadata['new']:
                price = item["price"]
                id = item["id"]
                adminmenuitem = Menu.objects.get(id=int(id))
                menuItem = MenuItem.objects.create(
                    kibanda=kibanda,
                    menu = adminmenuitem,
                    price = price,
                )

                menuItem.save()

                defaultMenu.menu.add(menuItem)

            defaultMenu.save()

            serializer = KibandaDefaultMenuSerializer(defaultMenu)
            return Response(serializer.data, status=status.HTTP_200_OK)

        except Exception as e:
            print(e)
            return Response({"message": "An error occurred"}, status=status.HTTP_400_BAD_REQUEST)

edit_kibanda_default_menu = EditDefaultMenuItem.as_view()

class CreateDefaultMenuItem(APIView):
    def post(self, request):
        try:
            user_id = request.data.get("user_id")
            menuItemsMetadata = request.data.get("mt")

            # parsed_menuItemsMetadata = json.loads(menuItemsMetadata)
            # print('menuitemsmetadaat ', menuItemsMetadata, type(menuItemsMetadata), menuItemsMetadata[0]["id"])
            user = get_user_model().objects.get(id=int(user_id))
            kibanda = user.kibanda

            defaultMenu = DefaultMenu.objects.create(
                kibanda=kibanda,
            )
            defaultMenu.save()

            for item in menuItemsMetadata:
                price = item["price"]
                id = item["id"]
                adminmenuitem = Menu.objects.get(id=int(id))
                menuItem = MenuItem.objects.create(
                    kibanda=kibanda,
                    menu = adminmenuitem,
                    price = price,
                )

                menuItem.save()

                defaultMenu.menu.add(menuItem)

            defaultMenu.save()
            # print('HAPA NDO NAKWAMA KAKA')
            kibanda.is_default_meal_added = True
            kibanda.save()
            serializer = KibandaDefaultMenuSerializer(defaultMenu)
            return Response(serializer.data, status=status.HTTP_200_OK)

        except Exception as e:
            print(e)
            return Response({"details": str(e)}, status=status.HTTP_400_BAD_REQUEST)

create_default_menu_item = CreateDefaultMenuItem.as_view()

class GetDefaultKibandaMenu(APIView):
    def post(self, request):
        try:
            user_id = request.data.get("user_id")
            user = get_user_model().objects.get(id=int(user_id))
            kibanda = user.kibanda
            defaultMenu = kibanda.defaultmenu
            serializer = KibandaDefaultMenuSerializer(defaultMenu)
            return Response(serializer.data, status=status.HTTP_200_OK)

        except Exception as e:
            print(e, " error catched")
            return Response({"message": "An error occurred"}, status=status.HTTP_400_BAD_REQUEST)

get_default_kibanda_menu = GetDefaultKibandaMenu.as_view()

class UpdateKibandaStatus(APIView):
    def post(self, request):
        try:
            print("SOMEONE CALLED ME TO UPDATE KIBANDA STATUS")
            user_id = request.data.get("user_id")
            new_status = request.data.get("status")
            user = get_user_model().objects.get(id=int(user_id))
            kibanda = user.kibanda
            kibandaStatus = kibanda.kibandastatus
            kibandaStatus.opened = new_status == "true"
            kibandaStatus.save()
            return Response({"details": "Everything is good"}, status=status.HTTP_200_OK)

        except Exception as e:
            print(e)
            return Response({"details": str(e)}, status=status.HTTP_400_BAD_REQUEST)

update_kibanda_status = UpdateKibandaStatus.as_view()

class IsKibandaSetTodayAvailableMenu(APIView):
    def post(self, request):
        try:
            print('IM GET CALLED....')
            user_id = request.data.get("user_id")
            user = get_user_model().objects.get(id=int(user_id))
            kibanda = user.kibanda
            if AvailableMenu.objects.filter(kibanda=kibanda).exists():
                availablemenu = AvailableMenu.objects.get(kibanda=kibanda)
                print('thsi is date ', availablemenu.updated_at.date(), dt.date.today())
                if availablemenu.updated_at.date() == dt.date.today():
                    print('its okay we have today amenu of this length ', availablemenu.menu.count())
                    if availablemenu.menu.count() == 0:
                        return Response({"is_available": False}, status=status.HTTP_200_OK)
                    return Response({"is_available": True}, status=status.HTTP_200_OK)
                else:
                    return Response({"is_available": False}, status=status.HTTP_200_OK)

            else:
                return Response({"is_available": False}, status=status.HTTP_200_OK)
        except Exception as e:
            print(e)
            return Response({"message": "An error occurred"}, status=status.HTTP_400_BAD_REQUEST)

is_menu_available_set = IsKibandaSetTodayAvailableMenu.as_view()

class SetKibandaTodayAvailableMenu(APIView):
    def post(self, request):
        try:
            user_id = request.data.get("user_id")
            menuitems = request.data.get("menuitems")
            print('menuitems ', menuitems)
            user = get_user_model().objects.get(id=int(user_id))
            kibanda = user.kibanda
            availableMenu = None
            if AvailableMenu.objects.filter(kibanda=kibanda).exists():
                availableMenu = AvailableMenu.objects.get(kibanda=kibanda)
                availableMenu.menu.clear()
                '''
                    # sio kila muda tu-create menu_item what if its available, just grab it..
                    # we only need to load the menu_items which are available in default menu
                    # since if we load all of em, we'll have to tell user to add the price of
                    # item which is not available in default menu.. you should do this is UI
                '''
                for item in menuitems:
                    adminmenu = Menu.objects.get(id=int(item['id']))
                    menuitem = MenuItem.objects.filter(kibanda=kibanda, menu=adminmenu)
                    menuitem = menuitem.first()
                    availableMenu.menu.add(menuitem)
                availableMenu.save()
                serialize = TodayAvailableMenuSerializer(availableMenu)
                return Response(serialize.data, status=status.HTTP_200_OK)

            availableMenu = AvailableMenu.objects.create(
                kibanda=kibanda,
            )
            for item in menuitems:
                adminmenu = Menu.objects.get(id=int(item['id']))
                menuitem = MenuItem.objects.filter(kibanda=kibanda, menu=adminmenu)
                '''
                    # sio kila muda tu-create menu_item what if its available, just grab it..
                    # we only need to load the menu_items which are available in default menu
                    # since if we load all of em, we'll have to tell user to add the price of
                    # item which is not available in default menu.. you should do this is UI
                '''
                menuitem = menuitem.first()
                availableMenu.menu.add(menuitem)

            availableMenu.save()
            serialize = TodayAvailableMenuSerializer(availableMenu)
            return Response(serialize.data, status=status.HTTP_200_OK)

        except Exception as e:
            print(e)
            return Response({"message": "An error occurred"}, status=status.HTTP_400_BAD_REQUEST)

set_today_available_menu = SetKibandaTodayAvailableMenu.as_view()

class SetKibandaDefaultMenuAsAvailableMenu(APIView):
    def post(self, request):
        try:
            user_id = request.data.get("user_id")
            user = get_user_model().objects.get(id=int(user_id))
            kibanda = user.kibanda
            defaultMenu = kibanda.defaultmenu
            availableMenu = None
            if AvailableMenu.objects.filter(kibanda=kibanda).exists():
                availableMenu = AvailableMenu.objects.get(kibanda=kibanda)
                # if there is data then in available menu then we clear em all
                availableMenu.menu.clear()
                for item in defaultMenu.menu.all():
                    availableMenu.menu.add(item)
                availableMenu.save()
                serialize = TodayAvailableMenuSerializer(availableMenu)
                return Response(serialize.data, status=status.HTTP_200_OK)

            # by default this is else block
            availableMenu = AvailableMenu.objects.create(
                kibanda=kibanda,
            )
            availableMenu.save()

            for item in defaultMenu.menu.all():
                availableMenu.menu.add(item)
            availableMenu.save()
            serialize = TodayAvailableMenuSerializer(availableMenu)
            return Response(serialize.data, status=status.HTTP_200_OK)

        except Exception as e:
            print(e)
            return Response({"message": "An error occurred"}, status=status.HTTP_400_BAD_REQUEST)

set_default_menu_as_available_menu = SetKibandaDefaultMenuAsAvailableMenu.as_view()

class AvailableOpenedKibandaProfiles(APIView):
    def get(self, request):
        '''
            that is shortcut i get from github copilor for reverse lookup, here i use the
            reverse lookup to get all the kibandas which are opened, and remember kibandastatus
            is found in kibandaStatus model which have relation with kibandaProfile model
            the reverse to kibandastatus is offered by related name of "kibandastatus" here
            you should notice that he used two underscore to get the reverse lookup field
            which is "opened" in this case
        '''
        try:
            kibandas = KibandaProfile.objects.filter(kibandastatus__opened=True)
            print('vibanda ', kibandas)
            serialize = KibandaProfileSerializer(kibandas, many=True)
            return Response(serialize.data, status=status.HTTP_200_OK)
        except Exception as e:
            print(e)
            return Response({"message": "An error occurred"}, status=status.HTTP_400_BAD_REQUEST)

opened_vibanda = AvailableOpenedKibandaProfiles.as_view()

class AllVibandaAPIView(APIView):
    def get(self, request):
        try:
            vibanda = reversed(KibandaProfile.objects.all())
            serialize = KibandaProfileSerializer(vibanda, many=True)
            return Response(serialize.data, status=status.HTTP_200_OK)
        except Exception as e:
            print(e)
            return Response({"message": "An error occurred"}, status=status.HTTP_400_BAD_REQUEST)

all_vibanda = AllVibandaAPIView.as_view()

# this give the kibanda default menu by default if there is no menu set, so this is mainly targetted  to customer
# to see menu but not the "kibanda" coz if you want to get kibanda available menu u should not mix it with default
# menu... so here its not good to call it when we want to get available menu..
class KibandaAvailableMenuForCustomer(APIView):
    def post(self, request):
        kibanda_id = request.data.get("kibanda_id")
        kibanda = KibandaProfile.objects.get(id=int(kibanda_id))
        availableMenu = AvailableMenu.objects.filter(kibanda=kibanda)
        try:
            if availableMenu.exists():
                availableMenu = availableMenu.first()
                # check if available menu is of today
                if availableMenu.updated_at.date() == dt.date.today():
                    serialize = TodayAvailableMenuSerializer(availableMenu)
                    return Response(serialize.data, status=status.HTTP_200_OK)

                # if available menu is not of today then we clear it and add all the menu items from default menu
                availableMenu.menu.clear()
                kibanda_default_menu = kibanda.defaultmenu
                for item in kibanda_default_menu.menu.all():
                    availableMenu.menu.add(item)
                availableMenu.save()
                serialize = TodayAvailableMenuSerializer(availableMenu)
                return Response(serialize.data, status=status.HTTP_200_OK)

            else:
                kibanda_default_menu = kibanda.defaultmenu
                availableMenu = AvailableMenu.objects.create(
                    kibanda=kibanda,
                    set_from_default_menu=True
                )
                for item in kibanda_default_menu.menu.all():
                    availableMenu.menu.add(item)
                availableMenu.save()
                serialize = TodayAvailableMenuSerializer(availableMenu)
                return Response(serialize.data, status=status.HTTP_200_OK)

        except Exception as e:
            print(e)
            return Response({"message": "An error occurred"}, status=status.HTTP_400_BAD_REQUEST)
kibanda_today_available_menu = KibandaAvailableMenuForCustomer.as_view()

# this is good to call to kibanda since its not mix the default menu if  the kibanda is not available..
class KibandaAvailableMenuOriginalAPIView(APIView):
    def post(self, request):
        kibanda_id = request.data.get("kibanda_id")
        try:
            kibanda = KibandaProfile.objects.get(id=int(kibanda_id))
            availableMenu = AvailableMenu.objects.filter(kibanda=kibanda)

            if availableMenu.exists():
                availableMenu = availableMenu.first()

                if availableMenu.updated_at.date() == dt.date.today():
                    print('I HAVE PURE AVAILABLE MENU OF TODAY SO NO NEED TO CLEAR MENU ITEMS')
                    serialize = TodayAvailableMenuSerializer(availableMenu)
                    return Response(serialize.data, status=status.HTTP_200_OK)

                # if available menu is not of today then we should clear it
                # HII NDO API INAYO-CLEAR DUH I DON'T KNOW WHY IT CLEAR ALL OF EM
                print("THIS IS AVAILABLE MENU DATE ", availableMenu.updated_at.date())
                print('THIS IS AVAILABLE MENU ', availableMenu.menu)
                print('available menu IS NOT OF TODAY SO WE SHOULD CLEAR IT')
                availableMenu.menu.clear()
                # then we should send this empty array to the user..
                serialize = TodayAvailableMenuSerializer(availableMenu)
                return Response(serialize.data, status=status.HTTP_200_OK)

            else:
                # it means no available menu array, which it does not make sense..
                # but for this case just return empty array..
                return Response({}, status=status.HTTP_200_OK)

        except Exception as err:
            return Response({"details": str(err)}, status=status.HTTP_400_BAD_REQUEST)

pure_kibanda_available_menu_no_auto_add_default_menu = KibandaAvailableMenuOriginalAPIView.as_view()

class AddOrRemoveMenuFromTodayAvailableMenu(APIView):
    def post(self, request):
        menu_id = request.data.get("menu_id")
        action = request.data.get("status")
        kibanda_id = request.data.get("kibanda_id")

        try:
            kibanda = KibandaProfile.objects.get(id=int(kibanda_id))
            availableMenu = AvailableMenu.objects.filter(kibanda=kibanda)
            if availableMenu.exists():
                availableMenu = availableMenu.first()
                menu = MenuItem.objects.get(id=int(menu_id))
                if action == "add":
                    availableMenu.menu.add(menu)
                    availableMenu.save()
                    serialize = TodayAvailableMenuSerializer(availableMenu)
                    return Response(serialize.data, status=status.HTTP_200_OK)
                elif action == "remove":
                    availableMenu.menu.remove(menu)
                    availableMenu.save()
                    serialize = TodayAvailableMenuSerializer(availableMenu)
                    return Response(serialize.data, status=status.HTTP_200_OK)
                else:
                    return Response({"message": "Invalid status"}, status=status.HTTP_400_BAD_REQUEST)

            else:
                return Response({"message": "No available menu found"}, status=status.HTTP_400_BAD_REQUEST)

        except Exception as err:
            return Response({"details": str(err)}, status=status.HTTP_400_BAD_REQUEST)

add_or_remove_menu_from_today_available_menu = AddOrRemoveMenuFromTodayAvailableMenu.as_view()

class EditKibandaProfile(APIView):
    def post(self, request):
        try:
            user_id = request.data.get("user_id")
            fname = request.data.get("fname")
            lname = request.data.get("lname")
            brand = request.data.get("brand")
            profile = request.data.get("profile", None)
            cover = request.data.get("cover", None)
            coords = request.data.get("coords")
            physical_address = reverse_geocoding(coords)
            phone = request.data.get("phone")
            user = get_user_model().objects.get(id=int(user_id))
            user.phone_number = phone
            user.save()
            if hasattr(user, "kibanda"):
                kibanda = user.kibanda

                kibanda.first_name = fname
                kibanda.last_name = lname
                kibanda.coordinates  = coords
                kibanda.brand_name = brand
                kibanda.physical_address = physical_address

                if (profile != 'null' and profile != None):
                    kibanda.image = profile

                if (cover != 'null' and cover != None):
                    kibanda.cover_photo = cover

                kibanda.save()

                serializer = KibandaProfileSerializer(kibanda)

                return Response(serializer.data, status=status.HTTP_200_OK)

            else:
                print("Here we edit kibanda user profile not other profile")

        except Exception as e:
            print(e)
            return Response({"details": str(e)}, status=status.HTTP_400_BAD_REQUEST)

edit_kibanda_profile = EditKibandaProfile.as_view()


# This Python 3 environment comes with many helpful analytics libraries installed

# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python

# For example, here's several helpful packages to load in

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the "../input/" directory.

# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory

import os

print(os.listdir("../input"))

# Any results you write to the current directory are saved as output.
benign=pd.read_csv('../input/opcode_frequency_benign.csv')

malware=pd.read_csv('../input/opcode_frequency_malware.csv')

test=pd.read_csv('../input/Test_data.csv')
benign['y']=0

malware['y']=1

FileName=test['FileName']

test=test.drop(columns=['FileName','Unnamed: 1809'])

test.head()
data=pd.concat([benign,malware])
data.head()
y=data['y']

X=data.drop(columns=['FileName','y'])
from sklearn.model_selection import train_test_split

X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=10,stratify=y)
from sklearn.linear_model import LogisticRegression

model=LogisticRegression()

model.fit(X_train,y_train)

y_reg=model.predict(X_test)

from sklearn.metrics import accuracy_score

accuracy_score(y_test,y_reg)
from sklearn.tree import DecisionTreeClassifier

model=DecisionTreeClassifier(max_depth=35,max_features=300)

model.fit(X_train,y_train)

y_reg=model.predict(X_test)

accuracy_score(y_test,y_reg)

from sklearn.ensemble  import RandomForestClassifier

model=RandomForestClassifier()

model.fit(X_train,y_train)

y_reg=model.predict(X_test)

accuracy_score(y_test,y_reg)
import matplotlib.pyplot as plt
# class Perceptron:

#     def __init__ (self):

#         self.w = None

#         self.b = None

#     def model(self, x):

#         return 1 if (np.dot(self.w, x) >= self.b) else 0

#     def predict(self, X):

#         Y = []

#         for x in X:

#             result = self.model(x)

#             Y.append(result)

#         return np.array(Y)

#     def fit(self, X, Y, epochs = 1, lr = 1):

#         self.w = np.ones(X.shape[1])

#         self.b = 0

#         accuracy = {}

#         max_accuracy = 0

#         wt_matrix = []

#         for i in range(epochs):

#             for x, y in zip(X, Y):

#                 y_pred = self.model(x)

#                 if y == 1 and y_pred == 0:

#                     self.w = self.w + lr * x

#                     self.b = self.b - lr * 1

#                 elif y == 0 and y_pred == 1:

#                     self.w = self.w - lr * x

#                     self.b = self.b + lr * 1

#             wt_matrix.append(self.w)

#             accuracy[i] = accuracy_score(self.predict(X), Y)

#             if (accuracy[i] > max_accuracy):

#                 max_accuracy = accuracy[i]

#                 chkptw = self.w

#                 chkptb = self.b

#         self.w = chkptw

#         self.b = chkptb

#         print(max_accuracy)

#         plt.plot(accuracy.values())

#         plt.ylim([0, 1])

#         plt.show()

#         return np.array(wt_matrix)
# X_train=X_train.values

# y_train=y_train.values
# perceptron=Perceptron()

# wt_matrix = perceptron.fit(X_train, y_train, 1000, 0.001)
# y_pred=perceptron.predict(X_test.values)

# accuracy_score(y_pred,y_test)
model.fit(X,y)

sub=model.predict(test)

out=sub.astype(int)

output = pd.DataFrame( { 'FileName': FileName , 'Class': out } )

output.to_csv( '../input/dataset.csv' , index = False )


# -*- coding: utf-8 -*-
import pandas as pd
import time
import os
import instrument
import csv
import workdays
import json
import order
import position
from misc import *
from event_engine import *
from vtConstant import *

########################################################################
class Gateway(object):
    """交易接口"""

    #----------------------------------------------------------------------
    def __init__(self, agent, gatewayName = 'Gateway'):
        """Constructor"""
        self.gatewayName = gatewayName
        self.agent = agent
        self.eventEngine = agent.eventEngine
        self.file_prefix = agent.folder + gatewayName + '_'
        self.qry_account = {}
        self.qry_pos = {}
        self.id2order = {}
        self.positions = {}
        self.instruments = []      # 已订阅合约代码
        self.working_orders = []
        self.process_flag = False
        self.eod_report = True
        self.account_info = {'available': 0,
                            'locked_margin': 0,
                            'used_margin': 0,
                            'curr_capital': 1000000,
                            'prev_capital': 1000000,
                            'pnl_total': 0,
                            'yday_pnl': 0,
                            'tday_pnl': 0,
                            }
        self.pl_by_product = {}
        self.order_stats = {'total_submit': 0, 'total_failure': 0, 'total_cancel':0 }
        self.order_constraints = {	'total_submit': 2000, 'total_cancel': 2000, 'total_failure':500, \
                                    'submit_limit': 200,  'cancel_limit': 200,  'failure_limit': 200 }
    #----------------------------------------------------------------------
    def initialize(self):
        pass

    def process_eod_report(self, tday):
        df = pd.DataFrame.from_dict( self.pl_by_product, orient='index' )
        cols = ['yday_mark', 'tday_mark', 'yday_pos', 'yday_pnl', 'new_long', 'new_long_avg', 'new_short', 'new_short_avg', 'tday_pnl', 'used_margin', 'locked_margin']
        df = df[cols]
        logfile = self.file_prefix + 'PNL_attribution_' + tday.strftime('%y%m%d')+'.csv'
        if os.path.isfile(logfile):
            return False
        else:
            df.to_csv(logfile, sep=',')
            return True

    def day_finalize(self, tday):
        self.save_local_positions(tday)
        if self.eod_report:
            self.process_eod_report(tday)
        eod_pos = {}
        for inst in self.positions:
            pos = self.positions[inst]
            eod_pos[inst] = pos.curr_pos
        self.id2order  = {}
        self.working_orders = []
        self.positions = {}
        self.order_stats = {'total_submit': 0, 'total_failure': 0, 'total_cancel':0 }
        for inst in self.instruments:
            self.order_stats[inst] = {'submit': 0, 'cancel':0, 'failure': 0, 'status': True }
            (pos_cls, pos_args) = self.get_pos_class(self.agent.instruments[inst])
            self.positions[inst] = pos_cls(self.agent.instruments[inst], self, **pos_args)
            self.positions[inst].pos_yday = eod_pos[inst]
            self.positions[inst].re_calc()
        self.account_info['prev_capital'] = self.account_info['curr_capital']

    def get_pos_class(self, inst):
        return (position.Position, {})

    def add_instrument(self, instID):
        self.instruments.append(instID)
        if instID not in self.positions:
            (pos_cls, pos_args) = self.get_pos_class(self.agent.instruments[instID])
            self.positions[instID] = pos_cls(self.agent.instruments[instID], self, **pos_args)
        if instID not in self.order_stats:
            self.order_stats[instID] = {'submit': 0, 'cancel':0, 'failure': 0, 'status': True }
        if instID not in self.qry_pos:
            self.qry_pos[instID]   = {'tday': [0, 0], 'yday': [0, 0]}

    def event_subscribe(self):
        pass

    def onTick(self, tick):
        """市场行情推送"""
        # 通用事件
        event1 = Event(type=EVENT_TICK)
        event1.dict['data'] = tick
        self.eventEngine.put(event1)

        # 特定合约代码的事件
        event2 = Event(type=EVENT_TICK + tick.instID)
        event2.dict['data'] = tick
        self.eventEngine.put(event2)

    #----------------------------------------------------------------------
    def onTrade(self, trade):
        """成交信息推送"""
        # 通用事件
        event1 = Event(type=EVENT_TRADE)
        event1.dict['data'] = trade
        self.eventEngine.put(event1)

        # 特定合约的成交事件
        event2 = Event(type=EVENT_TRADE + trade.order_ref)
        event2.dict['data'] = trade
        self.eventEngine.put(event2)

    #----------------------------------------------------------------------
    def onOrder(self, order):
        """订单变化推送"""
        # 通用事件
        event1 = Event(type=EVENT_ORDER)
        event1.dict['data'] = order
        self.eventEngine.put(event1)

        # 特定订单编号的事件
        event2 = Event(type=EVENT_ORDER + order.order_ref)
        event2.dict['data'] = order
        self.eventEngine.put(event2)

    #----------------------------------------------------------------------
    def onPosition(self, position):
        """持仓信息推送"""
        # 通用事件
        event1 = Event(type=EVENT_POSITION)
        event1.dict['data'] = position
        self.eventEngine.put(event1)

        # 特定合约代码的事件
        event2 = Event(type=EVENT_POSITION+position.instID)
        event2.dict['data'] = position
        self.eventEngine.put(event2)

    #----------------------------------------------------------------------
    def onAccount(self, account):
        """账户信息推送"""
        # 通用事件
        event1 = Event(type=EVENT_ACCOUNT)
        event1.dict['data'] = account
        self.eventEngine.put(event1)

        # 特定合约代码的事件
        event2 = Event(type=EVENT_ACCOUNT+account.vtAccountID)
        event2.dict['data'] = account
        self.eventEngine.put(event2)

    #----------------------------------------------------------------------
    def onError(self, error):
        """错误信息推送"""
        # 通用事件
        logContent = error.errorMsg
        self.onLog(logContent, level = logging.WARNING)

    #----------------------------------------------------------------------
    def onLog(self, log_content, level = logging.DEBUG):
        """日志推送"""
        # 通用事件
        event = Event(type=EVENT_LOG)
        event.dict['data'] = log_content
        event.dict['owner'] = "gateway_" + self.gatewayName
        event.dict['level'] = level
        self.eventEngine.put(event)

    #----------------------------------------------------------------------
    def onContract(self, contract):
        """合约基础信息推送"""
        # 通用事件
        event1 = Event(type=EVENT_CONTRACT)
        event1.dict['data'] = contract
        self.eventEngine.put(event1)

    def load_order_list(self, tday):
        logfile = self.file_prefix + 'order_' + tday.strftime('%y%m%d') + '.csv'
        if not os.path.isfile(logfile):
            return {}
        self.id2order = {}
        with open(logfile, 'rb') as f:
            reader = csv.reader(f)
            for idx, row in enumerate(reader):
                if idx > 0:
                    inst = row[3]
                    order_class = eval('order.' + str(row[14]))
                    filled_orders = {}
                    if ':' not in row[7]:
                        continue
                    filled_str = row[7].split('|')
                    for fstr in filled_str:
                        if (':' not in fstr) or ('_' not in fstr):
                            continue
                        forder = fstr.split(':')
                        pair_str = forder[1].split('_')
                        filled_orders[forder[0]] = [float(pair_str[0]), int(pair_str[1])]
                    iorder = order_class(instID = inst, limit_price = float(row[11]), \
                            volume = int(float(row[4])), order_time = int(float(row[12])), \
                            action_type = row[8], direction = row[9], price_type = row[10], \
                            trade_ref = int(float(row[15])), order_ref = int(row[0]), \
                            sys_id = row[1], status = int(row[13]), \
                            local_id = row[2], filled_orders = filled_orders, \
                            filled_volume = int(float(row[5])), filled_price = float(row[6]))
                    self.add_order(iorder)

    def save_order_list(self, tday):
        orders = self.id2order.keys()
        if len(self.id2order) > 1:
            orders.sort()
        order_list = [self.id2order[key] for key in orders if self.id2order[key].volume != 0]
        filename = self.file_prefix + 'order_' + tday.strftime('%y%m%d') + '.csv'
        with open(filename, 'wb') as log_file:
            file_writer = csv.writer(log_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL);
            file_writer.writerow(
                ['order_ref', 'local_id', 'sysID', 'inst', 'volume',
                 'filledvolume', 'filledprice', 'filledorders',
                 'action_type', 'direction', 'price_type',
                 'limitprice', 'order_time', 'status', 'order_class', 'trade_ref'])
            for iorder in order_list:
                forders = [ str(key) + ':' + '_'.join([str(s) for s in iorder.filled_orders[key]]) for key in iorder.filled_orders if len(str(key))>0 ]
                filled_str = '|'.join(forders)
                file_writer.writerow(
                    [iorder.order_ref, iorder.local_id, iorder.sys_id, iorder.instrument, iorder.volume,
                     iorder.filled_volume, iorder.filled_price, filled_str,
                     iorder.action_type, iorder.direction, iorder.price_type,
                     iorder.limit_price, iorder.start_tick, iorder.status, iorder.type, iorder.trade_ref])

    def add_orders(self, orders):
        for iorder in orders:
            self.add_order(iorder)

    def add_order(self, iorder):
        iorder.set_gateway(self)
        iorder.add_pos()
        self.id2order[iorder.local_id] = iorder
        if (iorder.status in order.Alive_Order_Status) and (iorder.local_id not in self.working_orders):
            self.working_orders.append(iorder.local_id)
            self.process_flag = True

    def send_queued_orders(self):
        alive_orders = []
        for local_id in self.working_orders:
            iorder = self.id2order[local_id]
            if iorder.status == order.OrderStatus.Ready:
                self.sendOrder(iorder)
                alive_orders.append(local_id)
            elif iorder.status in [order.OrderStatus.Sent, order.OrderStatus.Waiting]:
                alive_orders.append(local_id)
        self.working_orders = alive_orders
        self.process_flag = False

    def remove_order(self, iorder):
        iorder.remove_pos()
        self.id2order.pop(iorder.local_id, None)

    def load_local_positions(self, tday):
        pos_date = tday
        logfile = self.file_prefix + 'EODPos_' + pos_date.strftime('%y%m%d')+'.csv'
        if not os.path.isfile(logfile):
            pos_date = workdays.workday(pos_date, -1, CHN_Holidays)
            logfile = self.file_prefix + 'EODPos_' + pos_date.strftime('%y%m%d')+'.csv'
            if not os.path.isfile(logfile):
                logContent = "no prior position file is found"
                self.onLog(logContent, level = logging.INFO)
                return False
        else:
            self.agent.eod_flag = True
        with open(logfile, 'rb') as f:
            reader = csv.reader(f)
            for idx, row in enumerate(reader):
                if row[0] == 'capital':
                    self.account_info['prev_capital'] = float(row[1])
                elif row[0] == 'pos':
                    inst = row[1]
                    if inst in self.instruments:
                        if inst not in self.positions:
                            (pos_cls, pos_args) = self.get_pos_class(self.agent.instruments[inst])
                            self.positions[inst] = pos_cls(self.agent.instruments[inst], self, **pos_args)
                        self.positions[inst].update_pos('pos_yday', row[2:])
        return True

    def save_local_positions(self, tday):
        file_prefix = self.file_prefix
        logfile = file_prefix + 'EODPos_' + tday.strftime('%y%m%d')+'.csv'
        if os.path.isfile(logfile):
            return False
        else:
            with open(logfile,'wb') as log_file:
                file_writer = csv.writer(log_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL);
                for inst in self.positions:
                    pos = self.positions[inst]
                    pos.re_calc()
                self.calc_margin()
                file_writer.writerow(['capital', self.account_info['curr_capital']])
                for inst in self.positions:
                    pos = self.positions[inst]
                    if sum([ abs(ppos) for ppos in pos.curr_pos]) > 0:
                        file_writer.writerow(['pos', inst] + pos.curr_pos)
            return True

    def get_order_offset(self, instID, volume, order_num = 1):
        return [(OF_OPEN, volume)]

    def book_order(self, instID, volume, price_type, limit_price, trade_ref = 0, order_num = 1):
        direction = ORDER_BUY if volume > 0 else ORDER_SELL
        order_offsets = self.get_order_offset(instID, volume, order_num)
        new_orders = [order.Order(instID = instID, limit_price = limit_price, volume = v, \
                            order_time = self.agent.tick_id, action_type = action_type, \
                            direction = direction, price_type = price_type, trade_ref = trade_ref) \
                            for (action_type, v) in order_offsets]
        self.add_orders(new_orders)
        self.positions[instID].re_calc()
        return new_orders

    def calc_margin(self):
        for instID in self.positions:
            inst = self.agent.instruments[instID]
            pos = self.positions[instID]
            under_price = 0.0
            if (inst.ptype == instrument.ProductType.Option):
                under_price = self.agent.instruments[inst.underlying].price
            self.pl_by_product[instID] = {}
            self.pl_by_product[instID]['yday_mark'] = inst.prev_close
            self.pl_by_product[instID]['tday_mark'] = inst.price
            self.pl_by_product[instID]['new_long']  = pos.tday_pos[0]
            self.pl_by_product[instID]['new_short'] = pos.tday_pos[1]
            self.pl_by_product[instID]['new_long_avg'] = pos.tday_avp[0]
            self.pl_by_product[instID]['new_short_avg'] = pos.tday_avp[1]
            self.pl_by_product[instID]['locked_margin'] =  abs(pos.locked_pos[0]) \
                                                        * inst.calc_margin_amount(ORDER_BUY if pos.locked_pos[0] > 0 else ORDER_SELL, under_price)
            self.pl_by_product[instID]['used_margin'] =  abs(pos.curr_pos[0]) \
                                                        * inst.calc_margin_amount(ORDER_BUY if pos.curr_pos[0] > 0 else ORDER_SELL, under_price)
            self.pl_by_product[instID]['yday_pos'] = pos.pos_yday[0]
            self.pl_by_product[instID]['yday_pnl'] = pos.pos_yday[0] * (inst.price - inst.prev_close) * inst.multiple
            self.pl_by_product[instID]['tday_pnl'] =  pos.tday_pos[0] * (inst.price-pos.tday_avp[0]) * inst.multiple
            self.pl_by_product[instID]['tday_pnl'] -= pos.tday_pos[1] * (inst.price-pos.tday_avp[1]) * inst.multiple
        for key in ['locked_margin', 'used_margin', 'yday_pnl', 'tday_pnl']:
            self.account_info[key] = sum([self.pl_by_product[instID][key] for instID in self.positions])
        self.account_info['pnl_total'] = self.account_info['yday_pnl'] + self.account_info['tday_pnl']
        self.account_info['curr_capital'] = self.account_info['prev_capital'] + self.account_info['pnl_total']
        self.account_info['available'] = self.account_info['curr_capital'] - self.account_info['locked_margin']

    #----------------------------------------------------------------------
    def connect(self):
        """连接"""
        pass

    #----------------------------------------------------------------------
    def subscribe(self, subscribeReq):
        """订阅行情"""
        pass

    #----------------------------------------------------------------------
    def sendOrder(self, orderReq):
        """发单"""
        pass

    #----------------------------------------------------------------------
    def cancelOrder(self, cancelOrderReq):
        """撤单"""
        pass

    #----------------------------------------------------------------------
    def qryAccount(self):
        """查询账户资金"""
        pass

    #----------------------------------------------------------------------
    def qryPosition(self):
        """查询持仓"""
        pass

    #----------------------------------------------------------------------
    def close(self):
        """关闭"""
        pass

    def register_event_handler(self):
        pass

class GrossGateway(Gateway):
    def __init__(self, agent, gatewayName = 'Gateway'):
        super(GrossGateway, self).__init__(agent, gatewayName)

    def get_order_offset(self, instID, volume, order_num = 1):
        direction = ORDER_BUY if volume > 0 else ORDER_SELL
        vol = abs(volume)
        pos = self.positions[instID]
        if volume > 0:
            can_close = pos.can_close[0]
            can_yclose = pos.can_yclose[0]
        else:
            can_close = pos.can_close[1]
            can_yclose = pos.can_yclose[1]
        is_shfe = (pos.instrument.exchange == 'SHFE')
        n_orders = order_num
        res = []
        if (can_close > 0) and (vol > 0) and ((n_orders > 1) or (can_close >= vol)):
            trade_vol = min(vol, can_close)
            offset = OF_CLOSE_TDAY if is_shfe else OF_CLOSE
            res.append((offset, trade_vol))
            vol -= trade_vol
            n_orders -= 1
        if (can_yclose > 0) and (vol > 0) and is_shfe and ((n_orders > 1) or (can_yclose >= vol)):
            trade_vol = min(vol, can_yclose)
            res.append((OF_CLOSE_YDAY, trade_vol))
            vol -= trade_vol
            n_orders -= 1
        if vol > 0:
            res.append((OF_OPEN, vol))
        return res

    def book_spd_orders(self, instID, volume, price_type, limit_price, trade_ref = 0):
        direction = ORDER_BUY if volume > 0 else ORDER_SELL
        vol = abs(volume)
        instIDs, units = spreadinst2underlying(instID)
        res = [self.get_order_offset(inst, u * volume, 1) for inst, u in zip(instIDs, units)]
        action_type = ''.join([offset[0][0] for offset in res])
        new_order = order.SpreadOrder(inst = instID, limit_price = limit_price, volume = vol, \
                            order_time = self.agent.tick_id, action_type = action_type, \
                            direction = direction, price_type = price_type, \
                            trade_ref = trade_ref)
        self.add_order(new_order)
        for inst in instIDs:
            self.positions[inst].re_calc()
        return [new_order]

    def calc_margin(self):
        for instID in self.positions:
            inst = self.agent.instruments[instID]
            pos = self.positions[instID]
            under_price = 0.0
            if (inst.ptype == instrument.ProductType.Option):
                under_price = self.agent.instruments[inst.underlying].price
            self.pl_by_product[instID] = {}
            self.pl_by_product[instID]['yday_mark'] = inst.prev_close
            self.pl_by_product[instID]['tday_mark'] = inst.price
            self.pl_by_product[instID]['new_long']  = pos.tday_pos[0]
            self.pl_by_product[instID]['new_short'] = pos.tday_pos[1]
            self.pl_by_product[instID]['new_long_avg'] = pos.tday_avp[0]
            self.pl_by_product[instID]['new_short_avg'] = pos.tday_avp[1]
            self.pl_by_product[instID]['locked_margin'] =  pos.locked_pos[0] * inst.calc_margin_amount(ORDER_BUY, under_price)
            self.pl_by_product[instID]['locked_margin'] += pos.locked_pos[1] * inst.calc_margin_amount(ORDER_SELL, under_price)
            self.pl_by_product[instID]['used_margin'] =  pos.curr_pos[0] * inst.calc_margin_amount(ORDER_BUY, under_price)
            self.pl_by_product[instID]['used_margin'] += pos.curr_pos[1] * inst.calc_margin_amount(ORDER_SELL, under_price)
            self.pl_by_product[instID]['yday_pos'] = pos.pos_yday[0] - pos.pos_yday[1]
            self.pl_by_product[instID]['yday_pnl'] = (pos.pos_yday[0] - pos.pos_yday[1]) * (inst.price - inst.prev_close) * inst.multiple
            self.pl_by_product[instID]['tday_pnl'] =  pos.tday_pos[0] * (inst.price-pos.tday_avp[0]) * inst.multiple
            self.pl_by_product[instID]['tday_pnl'] -= pos.tday_pos[1] * (inst.price-pos.tday_avp[1]) * inst.multiple
        for key in ['locked_margin', 'used_margin', 'yday_pnl', 'tday_pnl']:
            self.account_info[key] = sum([self.pl_by_product[instID][key] for instID in self.positions])
        self.account_info['pnl_total'] = self.account_info['yday_pnl'] + self.account_info['tday_pnl']
        self.account_info['curr_capital'] = self.account_info['prev_capital'] + self.account_info['pnl_total']
        self.account_info['available'] = self.account_info['curr_capital'] - self.account_info['locked_margin']

########################################################################
class VtBaseData(object):
    """回调函数推送数据的基础类，其他数据类继承于此"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        self.gatewayName = EMPTY_STRING         # Gateway名称
        self.rawData = None                     # 原始数据

########################################################################
class VtTickData(VtBaseData):
    """Tick行情数据类"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        super(VtTickData, self).__init__()

        # 代码相关
        self.symbol = EMPTY_STRING              # 合约代码
        self.exchange = EMPTY_STRING            # 交易所代码
        self.instID = EMPTY_STRING            # 合约在vt系统中的唯一代码，通常是 合约代码.交易所代码

        # 成交数据
        self.lastPrice = EMPTY_FLOAT            # 最新成交价
        self.lastVolume = EMPTY_INT             # 最新成交量
        self.volume = EMPTY_INT                 # 今天总成交量
        self.openInterest = EMPTY_INT           # 持仓量
        self.time = EMPTY_STRING                # 时间 11:20:56.5
        self.date = EMPTY_STRING                # 日期 20151009

        # 常规行情
        self.openPrice = EMPTY_FLOAT            # 今日开盘价
        self.highPrice = EMPTY_FLOAT            # 今日最高价
        self.lowPrice = EMPTY_FLOAT             # 今日最低价
        self.preClosePrice = EMPTY_FLOAT

        self.upperLimit = EMPTY_FLOAT           # 涨停价
        self.lowerLimit = EMPTY_FLOAT           # 跌停价

        # 五档行情
        self.bidPrice1 = EMPTY_FLOAT
        self.bidPrice2 = EMPTY_FLOAT
        self.bidPrice3 = EMPTY_FLOAT
        self.bidPrice4 = EMPTY_FLOAT
        self.bidPrice5 = EMPTY_FLOAT

        self.askPrice1 = EMPTY_FLOAT
        self.askPrice2 = EMPTY_FLOAT
        self.askPrice3 = EMPTY_FLOAT
        self.askPrice4 = EMPTY_FLOAT
        self.askPrice5 = EMPTY_FLOAT

        self.bidVolume1 = EMPTY_INT
        self.bidVolume2 = EMPTY_INT
        self.bidVolume3 = EMPTY_INT
        self.bidVolume4 = EMPTY_INT
        self.bidVolume5 = EMPTY_INT

        self.askVolume1 = EMPTY_INT
        self.askVolume2 = EMPTY_INT
        self.askVolume3 = EMPTY_INT
        self.askVolume4 = EMPTY_INT
        self.askVolume5 = EMPTY_INT

########################################################################
class VtTradeData(VtBaseData):
    """成交数据类"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        super(VtTradeData, self).__init__()

        # 代码编号相关
        self.symbol = EMPTY_STRING              # 合约代码
        self.exchange = EMPTY_STRING            # 交易所代码
        self.instID = EMPTY_STRING            # 合约在vt系统中的唯一代码，通常是 合约代码.交易所代码

        self.tradeID = EMPTY_STRING             # 成交编号
        self.vtTradeID = EMPTY_STRING           # 成交在vt系统中的唯一编号，通常是 Gateway名.成交编号

        self.orderID = EMPTY_STRING             # 订单编号
        self.order_ref = EMPTY_STRING           # 订单在vt系统中的唯一编号，通常是 Gateway名.订单编号

        # 成交相关
        self.direction = EMPTY_UNICODE          # 成交方向
        self.offset = EMPTY_UNICODE             # 成交开平仓
        self.price = EMPTY_FLOAT                # 成交价格
        self.volume = EMPTY_INT                 # 成交数量
        self.tradeTime = EMPTY_STRING           # 成交时间

########################################################################
class VtOrderData(VtBaseData):
    """订单数据类"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        super(VtOrderData, self).__init__()

        # 代码编号相关
        self.symbol = EMPTY_STRING              # 合约代码
        self.exchange = EMPTY_STRING            # 交易所代码
        self.instID = EMPTY_STRING            # 合约在vt系统中的唯一代码，通常是 合约代码.交易所代码

        self.orderID = EMPTY_STRING             # 订单编号 local order ID
        self.order_ref = EMPTY_STRING           # for Order class object ID
        self.orderSysID = EMPTY_STRING			# remote order ID

        # 报单相关
        self.direction = EMPTY_UNICODE          # 报单方向
        self.offset = EMPTY_UNICODE             # 报单开平仓
        self.price = EMPTY_FLOAT                # 报单价格
        self.totalVolume = EMPTY_INT            # 报单总数量
        self.tradedVolume = EMPTY_INT           # 报单成交数量
        self.status = EMPTY_UNICODE             # 报单状态

        self.orderTime = EMPTY_STRING           # 发单时间
        self.cancelTime = EMPTY_STRING          # 撤单时间

        # CTP/LTS相关
        self.frontID = EMPTY_INT                # 前置机编号
        self.sessionID = EMPTY_INT              # 连接编号

########################################################################
class VtPositionData(VtBaseData):
    """持仓数据类"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        super(VtPositionData, self).__init__()

        # 代码编号相关
        self.symbol = EMPTY_STRING              # 合约代码
        self.exchange = EMPTY_STRING            # 交易所代码
        self.instID = EMPTY_STRING            # 合约在vt系统中的唯一代码，合约代码.交易所代码

        # 持仓相关
        self.direction = EMPTY_STRING           # 持仓方向
        self.position = EMPTY_INT               # 持仓量
        self.frozen = EMPTY_INT                 # 冻结数量
        self.price = EMPTY_FLOAT                # 持仓均价
        self.vtPositionName = EMPTY_STRING      # 持仓在vt系统中的唯一代码，通常是instID.方向

        # 20151020添加
        self.ydPosition = EMPTY_INT             # 昨持仓

########################################################################
class VtAccountData(VtBaseData):
    """账户数据类"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        super(VtAccountData, self).__init__()

        # 账号代码相关
        self.accountID = EMPTY_STRING           # 账户代码
        self.vtAccountID = EMPTY_STRING         # 账户在vt中的唯一代码，通常是 Gateway名.账户代码

        # 数值相关
        self.preBalance = EMPTY_FLOAT           # 昨日账户结算净值
        self.balance = EMPTY_FLOAT              # 账户净值
        self.available = EMPTY_FLOAT            # 可用资金
        self.commission = EMPTY_FLOAT           # 今日手续费
        self.margin = EMPTY_FLOAT               # 保证金占用
        self.closeProfit = EMPTY_FLOAT          # 平仓盈亏
        self.positionProfit = EMPTY_FLOAT       # 持仓盈亏

########################################################################
class VtErrorData(VtBaseData):
    """错误数据类"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        super(VtErrorData, self).__init__()

        self.errorID = EMPTY_STRING             # 错误代码
        self.errorMsg = EMPTY_UNICODE           # 错误信息
        self.additionalInfo = EMPTY_UNICODE     # 补充信息

########################################################################
class VtLogData(VtBaseData):
    """日志数据类"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        super(VtLogData, self).__init__()

        self.logTime = time.strftime('%X', time.localtime())    # 日志生成时间
        self.logContent = EMPTY_UNICODE                         # 日志信息
        self.level = logging.DEBUG

########################################################################
class VtContractData(VtBaseData):
    """合约详细信息类"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        super(VtBaseData, self).__init__()

        self.symbol = EMPTY_STRING              # 代码
        self.exchange = EMPTY_STRING            # 交易所代码
        self.instID = EMPTY_STRING            # 合约在vt系统中的唯一代码，通常是 合约代码.交易所代码
        self.name = EMPTY_UNICODE               # 合约中文名

        self.productClass = EMPTY_UNICODE       # 合约类型
        self.size = EMPTY_INT                   # 合约大小
        self.priceTick = EMPTY_FLOAT            # 合约最小价格TICK

        # 期权相关
        self.strikePrice = EMPTY_FLOAT          # 期权行权价
        self.underlyingSymbol = EMPTY_STRING    # 标的物合约代码
        self.optionType = EMPTY_UNICODE         # 期权类型

########################################################################
class VtSubscribeReq(object):
    """订阅行情时传入的对象类"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        self.symbol = EMPTY_STRING              # 代码
        self.exchange = EMPTY_STRING            # 交易所

        # 以下为IB相关
        self.productClass = EMPTY_UNICODE       # 合约类型
        self.currency = EMPTY_STRING            # 合约货币
        self.expiry = EMPTY_STRING              # 到期日
        self.strikePrice = EMPTY_FLOAT          # 行权价
        self.optionType = EMPTY_UNICODE         # 期权类型

########################################################################
class VtOrderReq(object):
    """发单时传入的对象类"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        self.symbol = EMPTY_STRING              # 代码
        self.exchange = EMPTY_STRING            # 交易所
        self.price = EMPTY_FLOAT                # 价格
        self.volume = EMPTY_INT                 # 数量

        self.priceType = EMPTY_STRING           # 价格类型
        self.direction = EMPTY_STRING           # 买卖
        self.offset = EMPTY_STRING              # 开平

        # 以下为IB相关
        self.productClass = EMPTY_UNICODE       # 合约类型
        self.currency = EMPTY_STRING            # 合约货币
        self.expiry = EMPTY_STRING              # 到期日
        self.strikePrice = EMPTY_FLOAT          # 行权价
        self.optionType = EMPTY_UNICODE         # 期权类型

########################################################################
class VtCancelOrderReq(object):
    """撤单时传入的对象类"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        self.symbol = EMPTY_STRING              # 代码
        self.exchange = EMPTY_STRING            # 交易所

        # 以下字段主要和CTP、LTS类接口相关
        self.orderID = EMPTY_STRING             # 报单号
        self.frontID = EMPTY_STRING             # 前置机号
        self.sessionID = EMPTY_STRING           # 会话号


import pandas as pd
import pickle
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import find_peaks
from scipy.fft import fft, fftfreq
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA

def fourier_classify(U, threshold=2, plot=False, title = 'plot'):

    # Round to 5sf avoid picking up noise.
    U = [np.array([np.format_float_positional(conc, precision=5, unique=False, fractional=False, trim='k') for conc in morphogen]) for morphogen in U]

    # Compute the fourier transforms.
    transforms = [fft(i) for i in U]

    # Check for peaks.
    x_peaks = [abs(i) > threshold for i in transforms[0][1:]]
    y_peaks = [abs(i) > threshold for i in transforms[1][1:]]

    # Peaks must be found in both species to return True.
    peaks_found = False
    if sum(x_peaks) > 0 and sum(y_peaks) > 0:
        peaks_found = True

    # Plot the fourier transforms.
    if plot:
        for i in [0, 1]:

            amplitudes = transforms[i]
            freq = fftfreq(100, 0.2)
            plt.plot(freq[1:], abs(amplitudes)[1:])
            plt.xlim(0, )
            plt.ylim(1,)
            plt.title(f'Prominent frequency: {freq[np.argmax(abs(amplitudes)[1:])]}')
            plt.show()

    return peaks_found

def rmNoise(results):

    filtered = {}
    for i in results:
        if fourier_classify(results[i]['concs']):
            filtered[i] = results[i]

    return(filtered)

##################### IMPORT DATA ############################

# These files are pre-filtered hits from the 10/03 analysis.

infile = open('1003_LSA_hits.pkl', 'rb')
LSA = pickle.load(infile)
infile.close()

infile = open('1003_growth_fourier.pkl', 'rb')
growth = pickle.load(infile)
infile.close()

infile = open('1003_nongrowth_fourier.pkl', 'rb')
nongrowth = pickle.load(infile)
infile.close()

# Parameter set 42.

infile = open('1003_Results_ParamSet42/parameters.pkl', 'rb')
params = pickle.load(infile)
infile.close()

########################## PCA ############################

# Remove noise.
# LSA = rmNoise(LSA)

# Make df with parameters as headings.
category = []
tp_params = []

for i in LSA:

    # Add tp_class.
    category.append(LSA[i]['LSA']['system_class'])

    # Add parameters to list of dictionaries.
    tp_params.append(params[(i[0], 0)][0])

df = pd.DataFrame(tp_params)

# Scale the data.
scaler = StandardScaler()
scaler.fit(df)
df = scaler.fit_transform(df)

# PCA with 2 components.
pca = PCA(n_components = 2)
df_new = pca.fit_transform(df)
principalDf = pd.DataFrame(data = df_new, columns = ['PC1', 'PC2'])
finalDf = pd.concat([principalDf, pd.DataFrame(category, columns = ['Turing_Class'])], axis = 1)

# Show how effective the PCA is at explaining variance.
print('Explained variance: ', pca.explained_variance_ratio_)

# Plot the PCA outcome.
fig = plt.figure(figsize = (8,8))
ax = fig.add_subplot(1,1,1)
ax.set_xlabel('Principal Component 1', fontsize = 15)
ax.set_ylabel('Principal Component 2', fontsize = 15)
ax.set_title('2 component PCA', fontsize = 20)

targets = ['turing I', 'turing II', 'turing I oscillatory', 'hopf', 'turing I hopf', 'turing II hopf']
colors = ['r', 'g', 'b', 'c', 'm', 'y']
for target, color in zip(targets,colors):
    indicesToKeep = finalDf['Turing_Class'] == target
    ax.scatter(finalDf.loc[indicesToKeep, 'PC1'], finalDf.loc[indicesToKeep, 'PC2'], c = color, s = 50)
ax.legend(targets)
ax.grid()


import sys
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(message)s', filename='logfile.txt')

def print_maturity(age):
    if age >= 18:
        logging.info('Adult!')
    else:
        logging.info('Not adult!!!')

if __name__ == '__main__':
    logging.debug('The program was called with these parameters %s' % sys.argv[1:])
    logging.debug('The first parameter is %s' % sys.argv[1])
    age = int(sys.argv[1])
    print_maturity(age)


# Using Mobilenet_V2

# -----------------------------------------------------------------------

# Liên kết danh sách ảnh trong bộ dữ liệu, sau đó khởi tạo
# danh sách dữ liệu và nhãn
from imutils import paths
import config
imagePaths = list(paths.list_images(config.BASE_PATH))
# print("\n".join(imagePaths))

# Khởi tạo biến để lưu label và data
labels = []
dataset = []

import os
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
# Load từng ảnh trong dữ liệu (data)
print("[INFO] Loading image form disk....")
for imagePath in imagePaths:
    # Lấy nhãn
    label = imagePath.split(os.path.sep)[1]
    # print(label)

    # Tải ảnh với đầu vào là 224x224 và xử lý ảnh sang dạng mảng
    image = load_img(imagePath, target_size=config.INPUT_DIMS)
    image = img_to_array(image)
    image = preprocess_input(image)

    # Cập nhật dữ liệu và nhãn
    labels.append(label)
    dataset.append(image)

# -----------------------------------------------------------------------

# Chuyển đổi dữ liệu và nhãn sang kiểu dữ liệu Numpy Array
print("[INFO] Converting data format...")
import numpy as np
dataset = np.array(dataset, dtype="float32")
labels = np.array(labels)
# print(labels)

# -----------------------------------------------------------------------

from sklearn.preprocessing import LabelBinarizer
from tensorflow.keras.utils import to_categorical
# Sử dụng one-hot coding để tăng hiệu xuất phân loại cho class
lb = LabelBinarizer()
labels = lb.fit_transform(labels)
labels = to_categorical(labels)
# print(labels)

# -----------------------------------------------------------------------

from sklearn.model_selection import train_test_split
# Chia tập train và tập validation
(Xtrain, Xtest, Ytrain, Ytest) = train_test_split(dataset, labels, test_size=0.2,
                                                  stratify=labels, random_state=42)
# print(Xtrain.shape)
# print(Ytrain)
# print(Xtest.shape)
# print(Ytest)

# -----------------------------------------------------------------------

# Tạo bộ dữ liệu lớn hơn bằng cách sử dụng ImageGenerater
from tensorflow.keras.preprocessing.image import ImageDataGenerator
aug = ImageDataGenerator(rotation_range=20,
                         zoom_range=0.15,
                         width_shift_range=0.2,
                         height_shift_range=0.2,
                         shear_range=0.15,
                         horizontal_flip=True,
                         fill_mode="nearest"
                         )

# -----------------------------------------------------------------------

# Using transfer learning - Mobilenet_V2 model
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import AveragePooling2D
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Flatten
from tensorflow.keras.models import Model

baseModel = MobileNetV2(weights="imagenet", include_top=False, input_tensor=Input(shape=(224, 224, 3)))
# print(baseModel.summary())

# Tạo phần ourput model để thay thế cho phần output của model gốc
head_Model = baseModel.output
head_Model = AveragePooling2D(pool_size=(7, 7))(head_Model)
head_Model = Flatten()(head_Model)
head_Model = Dense(64, activation="relu")(head_Model)
head_Model = Dropout(0.2)(head_Model)
head_Model = Dense(2, activation="softmax")(head_Model)

# Đặt vào phần FC model và phần cuối của model gốc
model = Model(inputs=baseModel.input, outputs=head_Model)
# print(model.summary())

# Đóng băng các layers để chúng ko bị update trong quá trình training
for layer in baseModel.layers:
    layer.trainable = False

# -----------------------------------------------------------------------

# Biên soạn model
from tensorflow.keras.optimizers import Adam
print("[INFO] Compiling model....")
model.compile(loss="categorical_crossentropy", optimizer=Adam(config.LR), metrics=["acc"])

# Train model
print("[INFO] Training head model....")
BS = 24
epochs = 5
Hist = model.fit(aug.flow(Xtrain, Ytrain, batch_size=BS),
                      steps_per_epoch=len(Xtrain) // BS,
                      validation_data=(Xtest, Ytest),
                      validation_steps=len(Xtest) // BS,
                      epochs=epochs)

# -----------------------------------------------------------------------

# Dự đoán trên tập test
print("[INFO] Evaluating network....")
predIdxs = model.predict(Xtest, batch_size=BS)

# Tìm index của mỗi ảnh trong tập test và dự đoán class cho ảnh đó
predIdxs = np.argmax(predIdxs, axis=1)
# print(predIdxs)

# Show classification report
from sklearn.metrics import classification_report
print(classification_report(Ytest.argmax(axis=1), predIdxs, target_names=lb.classes_))

# -----------------------------------------------------------------------

# Save the model
import pickle

print("[INFO] Saving the model....")
model.save(config.MODEL_PATH)

# Save label sử dụng label encoder

f = open(config.LABEL_PATH, "wb")
f.write(pickle.dumps(lb))
f.close()

# -----------------------------------------------------------------------

# plot the training loss and accuracy
import matplotlib.pyplot as plt
N = epochs
plt.style.use("ggplot")
plt.figure()
plt.plot(np.arange(0, N), Hist.history["loss"], label="train_loss")
plt.plot(np.arange(0, N), Hist.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, N), Hist.history["acc"], label="train_acc")
plt.plot(np.arange(0, N), Hist.history["val_acc"], label="val_acc")
plt.title("Training Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="lower left")
plt.savefig("plot.png")


#!/usr/bin/python

import sys

# get filename `*.txt`
filename=str(sys.argv[1])
# get number to display
n = int(sys.argv[2])
# read file
file = open(filename,"r")
# make dictionary
D = {}
# by line
while True:
    line = file.readline()
    if not line:
        break
    line = line.strip()
    line = line.split(' ')
    for i in range(len(line)):
        if line[i] in D:
            D[line[i]] += 1
        else:
            D[line[i]] = 1
# check for top words
D_sort = dict(sorted(D.items(), key = lambda D: D[1], reverse = True))
# print the result
cnt = 0
for key , value in D_sort.items():
    cnt+=1
    print("{:10}{:>10}".format(key,value))
    if cnt == n:
        break


import os
import logging
import pandas as pd
import click
from sklearn.model_selection import train_test_split

@click.command('data_split')
@click.option('--input-path')
@click.option('--output-path')
@click.option('--train-size', type=float)
@click.option('--shuffle', type=bool)
def data_split(input_path: str, output_path: str, train_size: float,
               shuffle: bool):
    log('Begin split data...')

    if not os.path.exists(input_path):
        raise ValueError(f'Data path {input_path} does not exist')

    data_path = os.path.join(input_path, 'data.csv')
    if not os.path.exists(data_path):
        raise ValueError(f'Data file {data_path} does not exist')

    data = pd.read_csv(data_path)
    data_train, data_valid = train_test_split(data, stratify=data['target'],
                                              train_size=train_size,
                                              shuffle=shuffle, random_state=9)
    data_train.to_csv(os.path.join(output_path, 'data_train.csv'), index=False)
    data_valid.to_csv(os.path.join(output_path, 'data_valid.csv'), index=False)

    log('Data split success')

def log(message):
    logger = logging.getLogger('data_split')
    logger.warning(message)

if __name__ == '__main__':
    data_split()


def Main():
    path = "D:\\dev\\ssuish-repo\\python\\problems\\avc-2022\\data\\day2.txt"

    value = {'X':1, 'Y':2, 'Z':3}
    draw = {'A':'X', 'B':'Y', 'C':'Z'}
    win = {'A': 'Y', 'B': 'Z', 'C': 'X'}
    loss = {'A': 'Z', 'B': 'X', 'C': 'Y'}

    # Opponent 'A X' You

    # Condition 1
    # X = A = Rock
    # Y = B = Paper
    # Z = C = Scissors

    # Condition 2
    # X = Win
    # Y = Draw
    # Z = Lose

    # Pseudocode
    """
        if opponent = A and you = Y
            Y is draw, so you = B
            score = 3 + 2
        else if = A and you = X
            X is win, so you = C
            score = 6 + 1
        else if = A and you = Z
            Z is lose, so you = A
            score = 0 + 3
    """

    score = 0

    with open(path, 'r') as file:

        for line in file:
            pick = line.strip()
            if pick[2] == 'X':
                you = loss[pick[0]]
                score += value[you] + 0
            elif pick[2] == 'Y':
                you = draw[pick[0]]
                score += value[you] + 3
            elif pick[2] == 'Z':
                you = win[pick[0]]
                score += value[you] + 6

    print("Total score: ", score)

if __name__ == '__main__':
    Main()


import boto3
from botocore import UNSIGNED
from botocore.config import Config
from enum import Enum
import os

class OpenImageDataType(Enum):
    TRAIN = "train"
    TEST = "test"
    VALIDATION = "validation"

class OpenImageWrapper:
    def __init__(self, root_dst_folder="image"):
        self.__bucket_name = "open-images-dataset"
        self.__train_folder_name = "train"
        self.__val_folder_name = "validation"
        self.__test_folder_name = "test"
        self.__root_dst_folder = root_dst_folder

        # initialize
        os.makedirs(self.__root_dst_folder, exist_ok=True)
        self.__s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED))

    def download(self, data_type):
        folder_name = data_type.value
        dst_folder = os.path.join(self.__root_dst_folder, folder_name)
        os.makedirs(dst_folder, exist_ok=True)

        # get image file list
        file_key_list = self.__get_file_list(folder_name)

        # download image files.
        file_count = len(file_key_list)
        for i, file_key in enumerate(file_key_list):
            print("{}/{} start to download file..".format(i+1, file_count))
            filename = os.path.basename(file_key)
            with open(os.path.join(dst_folder, filename), "wb") as file:
                self.__s3.download_fileobj(Bucket=self.__bucket_name, Key=file_key, Fileobj=file)

    def __get_file_list(self, prefix):
        file_key_list = []
        next_token = ""

        while True:
            if next_token == "":
                response = self.__s3.list_objects_v2(Bucket=self.__bucket_name, Prefix=prefix)
            else:
                response = self.__s3.list_objects_v2(Bucket=self.__bucket_name, Prefix=prefix, ContinuationToken=next_token)

            if "Contents" in response:
                contents = response["Contents"]

                for content in contents:
                    file_key_list.append(content["Key"])

            if "NextContinuationToken" in response:
                next_token = response['NextContinuationToken']
            else:
                break

        return file_key_list

if __name__ == "__main__":
    wrapper = OpenImageWrapper()
    wrapper.download(OpenImageDataType.VALIDATION)


class MultipleProxyMiddleware:
    FORWARDED_FOR_FIELDS = [
        'HTTP_X_FORWARDED_FOR',
        'HTTP_X_FORWARDED_HOST',
        'HTTP_X_FORWARDED_SERVER',
    ]

    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        """
        Rewrites the proxy headers so that only the most
        recent proxy is used.
        """
        for field in self.FORWARDED_FOR_FIELDS:
            if field in request.META:
                if ',' in request.META[field]:
                    parts = request.META[field].split(',')
                    request.META[field] = parts[-1].strip()
        return self.get_response(request)


import random
import numpy as np
from NNlayer import *
from tool import *
from model import get_net

(train_lable, train_image) = read_data('./train-labels-idx1-ubyte.gz', './train-images-idx3-ubyte.gz')

(test_lable, test_image) = read_data('./t10k-labels-idx1-ubyte.gz', './t10k-images-idx3-ubyte.gz')

net = get_net()

for i in range(15):
    cost = 0.0
    for data, label in data_iter(train_image,train_lable,20):
        data = data.astype('float32') / 255
        data = np.reshape(data,(data.shape[0],data.shape[1],data.shape[2],1))
        l = onehot(label)
        out = net.forward(data)
        minicost,delta = compute_cost(out.T,l)
        cost += minicost
        net.backward(delta)
        net.update_params(0.1)
    tloss,acc =  get_loss_accuracy(net,test_image,test_lable,128)
    print("Epoch %d. Train Loss: %f Test Loss: %f Test Acc: %f" % (i,cost/len(train_lable),tloss,acc))

net.save_params("net.params")


from dataclasses import dataclass

from starknet_py.contract import SentTransaction
from starknet_py.utils.sync import add_sync_methods

@add_sync_methods
@dataclass(frozen=True)
class AccountDeploymentResult(SentTransaction):
    """
    Result of the :meth:`Account.deploy_account <starknet_py.net.account.account.Account.deploy_account>` method.
    """

    account: "Account" = None  # pyright: ignore
    """Account instance created during the deployment."""

    def __post_init__(self):
        if self.account is None:
            raise ValueError(
                "Parameter account cannot be None in AccountDeploymentResult."
            )


###################################################
# THis is a simple python program to run turtle graphics
# Author : Akhtar Jalbani
# Version: 0.3
# This is improved version using function
# Tested on : python IDLE
#################################################
import turtle

# function defination
def square(length,angle):
    for side in range(4):
        myturtle.forward(length)
        myturtle.left(angle)

wn = turtle.Screen()
wn.bgcolor("light green")
wn.title("Turtle")
myturtle = turtle.Turtle()
myturtle.speed(10)
square(100,90)
turtle.done()


"""CRUD utils for RSS posts table"""

import time

from httpx import delete
from sqlalchemy import select, text
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.orm import Session

from app.models.rss_post_model import RSSPostModel
from app.models.rss_source_model import RSSSourceModel
from app.schemas.rss_post_schema import RSSPostBase, RSSPostCreate
from app.settings import settings

class RSSPostsMethods:
    def __init__(self) -> None:
        pass

    def get_all_posts(self, db: Session):
        return (
            db.query(RSSPostModel)
            .filter(RSSPostModel.blacklisted != True)
            .order_by(-RSSPostModel.publish_date)
            .all()
        )

    def get_post_by_id(self, db: Session, post_id: int):
        result = (
            db.execute(select(RSSPostModel).where(RSSPostModel.id == post_id))
            .scalars()
            .first()
        )

        return result

    def get_posts_by_source_id(self, db: Session, source_id: int):
        return db.query(RSSPostModel).filter(RSSPostModel.source_id == source_id).all()

    def get_source_by_post_id(self, db: Session, post_id: int):
        post = self.get_post_by_id(db, post_id)

        return (
            db.query(RSSSourceModel).filter(RSSSourceModel.id == post.source_id).first()
        )

    def create_post(self, db: Session, post: RSSPostCreate):
        db_post = RSSPostModel(**post.dict())

        db.add(db_post)
        db.commit()
        db.refresh(db_post)

        return db_post

    def create_posts(self, db: Session, posts: list[RSSPostCreate]):
        for post in posts:
            add_post = (
                insert(RSSPostModel).values(**post.dict()).on_conflict_do_nothing()
            )

            db.execute(add_post)

        db.commit()

        return

    def update_post(self, db: Session, post: RSSPostBase, post_id: int):
        db.query(RSSPostModel).filter(RSSPostModel.id == post_id).update(
            values=post.dict()
        )
        db.commit()

    def delete_post(self, db: Session, post_id: int):
        db.query(RSSPostModel).filter(RSSPostModel.id == post_id).delete()
        db.commit()

    def block_post(self, db: Session, post_id: int):
        db.query(RSSPostModel).filter(RSSPostModel.id == post_id).update(
            values={"blacklisted": True}
        )
        db.commit()

    def unblock_post(self, db: Session, post_id: int):
        db.query(RSSPostModel).filter(RSSPostModel.id == post_id).update(
            values={"blacklisted": False}
        )
        db.commit()

    def get_blacklisted_posts(self, db: Session):
        return db.query(RSSPostModel).filter(RSSPostModel.blacklisted == True).all()

    def clear_old_posts(self, db: Session):
        db.query(RSSPostModel).filter(
            RSSPostModel.publish_date
            < int(time.time()) - settings.BG_CLEANUP_INTERVAL_SECONDS
        ).delete()
        db.commit()

        posts = db.query(RSSPostModel).all()

        return len(posts)

rss_posts_methods = RSSPostsMethods()