# coding=utf-8
import argparse
import json
import sys
from threading import Lock, Thread
import time
import traceback
import bottle
from bottle import Bottle, request, template, run, static_file
import requests
import random
from leader_election import Election
from data_processor import DataProcessor
from server_data import ServerData
# ------------------------------------------------------------------------------------------------------

def current_milli_time(): return int(round(time.time() * 1000))
def intRandomNumber(): return random.randint(1, 100)
def current_time_in_seconds(): return int(round(time.time()))

prapagation_delay = 5  # in second

class Blackboard:
    def __init__(self):
        self.content = ""
        self.lock = Lock()  # use lock when you modify the content

    def get_content(self):
        with self.lock:
            cnt = self.content
        return cnt

    def set_content(self, new_content):
        with self.lock:
            self.content = new_content
        return

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

class Server(Bottle):

    def __init__(self, ID, IP, servers_list):
        super(Server, self).__init__()
        self.blackboard = Blackboard()
        self.id = int(ID)
        self.ip = str(IP)
        self.servers_list = servers_list
        # list all REST URIs
        # if you add new URIs to the server, you need to add them here
        self.route("/", callback=self.index)
        self.get("/board", callback=self.get_board)
        self.post("/", callback=self.post_index)

        # we give access to the templates elements
        self.get("/templates/<filename:path>", callback=self.get_template)
        self.post("/board", callback=self.add_on_board)
        self.post("/board/<element_id>/", callback=self.modify_delete)
        self.post("/election", callback=self.election)
        self.post("/leader", callback=self.leader)
        self.get("/sync",callback=self.sync)
        self.time_in_seconds_of_first_message = 0
        self.time_in_seconds_of_last_message = 0

        # You can have variables in the URI, here's an example
        # self.post('/board/<element_id:int>/', callback=self.post_board) where post_board takes an argument (integer) called element_id
        # self.board = dict()  # global board
        # self.board_lock = Lock()  # Used for locking the board

        ServerData.leader_ip = self.servers_list[len(
            self.servers_list)-1]  # last ip as leader

        self.leader_id = len(self.servers_list)
        self.electionThread = None
        self.dataprocessThread = None

    def get_board_items(self):
        with ServerData.board_lock:
            board = ServerData.board
            return board

    def delete_key(self, key):
        print("[DELETE] " + str(key))
        with ServerData.board_lock:
            try:
                del ServerData.board[key]
            except KeyError as ex:
                print("[ERROR] " + str(ex))
            return

    def get_value(self, key):
        with ServerData.board_lock:
            value = ServerData.board[key]
            return value

    def insert_or_update_in_board(self, new_content, e_id):
        new_kew = e_id
        with ServerData.board_lock:
            # if len(new_kew.strip()) == 0:
            #     c_time = current_milli_time()
            #     new_kew = (
            #         str(c_time) + "_" + str(e_id)
            #     )  # id = 23423432_1 , (there same data in same mili second in all server, that's why I used _ and server id)
            self.blackboard.set_content(new_content)

            ServerData.board[new_kew] = self.blackboard.get_content()
            if(len(ServerData.board)) == 1 :
                self.time_in_seconds_of_first_message = current_time_in_seconds()
            elif(len(ServerData.board) == 40):
                print("Total time = {}".format((current_time_in_seconds() - self.time_in_seconds_of_first_message)))

        return new_kew

    def do_parallel_task(self, method, args=None):
        # create a thread running a new task
        # Usage example: self.do_parallel_task(self.contact_another_server, args=("10.1.0.2", "/index", "POST", params_dict))
        # this would start a thread sending a post request to server 10.1.0.2 with URI /index and with params params_dict
        thread = Thread(target=method, args=args)
        thread.daemon = True
        thread.start()

    def do_parallel_task_after_delay(self, delay, method, args=None):
        # create a thread, and run a task after a specified delay
        # Usage example: self.do_parallel_task_after_delay(10, self.start_election, args=(,))
        # this would start a thread starting an election after 10 seconds
        thread = Thread(
            target=self._wrapper_delay_and_execute, args=(delay, method, args)
        )
        thread.daemon = True
        thread.start()

    def _wrapper_delay_and_execute(self, delay, method, args):
        time.sleep(delay)  # in sec
        method(*args)

    def contact_another_server(self, srv_ip, URI, req="POST", params_dict=None):
        # Try to contact another serverthrough a POST or GET
        # usage: server.contact_another_server("10.1.1.1", "/index", "POST", params_dict)
        success = False
        try:
            if "POST" in req:
                res = requests.post(
                    "http://{}{}".format(srv_ip, URI), data=params_dict)
            elif "GET" in req:
                res = requests.get("http://{}{}".format(srv_ip, URI))
            # result can be accessed res.json()
            if res.status_code == 200:
                success = True
        except Exception as e:
            print("[ERROR] " + str(e))
        return success

    def propagate_to_all_servers(self, URI, req="POST", params_dict=None):
        for srv_ip in self.servers_list:
            if srv_ip != self.ip:  # don't propagate to yourself
                success = self.contact_another_server(
                    srv_ip, URI, req, params_dict)
                if not success:
                    print("[WARNING ]Could not contact server {}".format(srv_ip))

    def start_leader_election_thread(self):
        if(self.electionThread == None or (not self.electionThread.is_alive())):
            print("+=====start_leader_electionhread==================")
            electionThread = Election(self.ip, self.id, self.servers_list)
            electionThread.start()

    def start_data_processing_thread(self):
        if(self.dataprocessThread == None or not self.dataprocessThread.isRunning()):
            print("+===============start_data_processing_thread=====================")
            self.dataprocessThread = DataProcessor(
                self.id, self.ip, self.servers_list)
            self.dataprocessThread.start()

    # route to ('/')

    def index(self):
        # we must transform the blackboard as a dict for compatiobility reasons
        # board = dict()
        board = self.get_board_items()
        return template(
            "server/templates/index.tpl",
            board_title="Server {} ({})".format(self.id, self.ip),
            board_dict=board.items(),
            server_title = ServerData.server_title,
            members_name_string="Faiz Ahmed & Md Abu Noman Majumdar",
        )

    # get on ('/board')
    def get_board(self):
        # we must transform the blackboard as a dict for compatibility reasons
        # board = dict()
        board = self.get_board_items()
        return template(
            "server/templates/blackboard.tpl",
            board_title="Server {} ({})".format(self.id, self.ip),
            server_title = ServerData.server_title,
            board_dict=board.items(),
        )

    # post on ('/')
    def post_index(self):
        try:
            # we read the POST form, and check for an element called 'entry'
            new_entry = request.forms.get("entry")

            print("Received: {}".format(new_entry))
        except Exception as e:
            print("[ERROR] " + str(e))

    # post on ('/board')
    def add_on_board(self):
        try:
            text = request.forms.get("entry")

            if "id" in request.forms:  # got from Leader
                self.insert_or_update_in_board(text, request.forms.get("id"))
            else:
                if ServerData.leader_ip == self.ip:
                    self.start_data_processing_thread()
                    self.dataprocessThread.pushData(text)
                else:
                    # If Coordinator does not response start election
                    success = self.contact_another_server(
                        ServerData.leader_ip, "/board", "POST", {"entry": text})
                    print("leader : {} , text : {} , success {}".format(
                        ServerData.leader_ip, text, success))
                    if not success: # start election
                        print("Running election")
                        self.start_leader_election_thread()
        except Exception as ex:
            print("[ERROR] " + str(ex))

    # post on ('/election')
    def election(self):
        try:
            self.start_leader_election_thread()
            print("found election from : {}".format(request.forms.get("l_ip")))
        except Exception as ex:
            print("[ERROR] " + str(ex))

    # post on ('/leader')
    def leader(self):
        try:
            ServerData.leader_ip = request.forms.get("l_ip")
            self.leader_id = request.forms.get("l_id")
            print("leader_ip=> {}".format(ServerData.leader_ip))
            if(ServerData.leader_ip != self.ip):
                ServerData.server_title = "Slave"
                if self.dataprocessThread != None:
                    print("Stopping processor thread")
                    self.dataprocessThread.stop()
                    self.dataprocessThread = None

            print(True)
        except Exception as ex:
            print("[ERROR] " + str(ex))

    def modify_delete(self, element_id):
        try:
            print("modify_delete ", element_id)
            isDelete = request.forms.get("delete", type=int)
            entry = request.forms.get("entry")
            print("e_id {} isDelete {} entry {} ".format(
                element_id, isDelete, entry))
            if element_id in self.get_board_items():
                if isDelete == 1:
                    self.delete_key(element_id)
                else:
                    self.insert_or_update_in_board(entry, element_id)
            else:
                print("[ERROR] No Entry")

            # Propagate upadate to other servers, "propagated" flag is used to tackle retransmission again from server
            if not "propagated" in request.forms:
                self.do_parallel_task_after_delay(
                    prapagation_delay,
                    self.propagate_to_all_servers,
                    args=(
                        "/board/" + element_id + "/",
                        "POST",
                        {
                            "entry": entry,
                            "id": element_id,
                            "delete": isDelete,
                            "propagated": 1,
                        },
                    ),
                )
        except Exception as identifier:
            print("[ERROR] " + str(identifier))

    # get "/sync"
    def sync(self):
        return self.get_board_items()

    def get_template(self, filename):
        return static_file(filename, root="./server/templates/")

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

def main():
    PORT = 80
    parser = argparse.ArgumentParser(
        description="Your own implementation of the distributed blackboard"
    )
    parser.add_argument(
        "--id", nargs="?", dest="id", default=1, type=int, help="This server ID"
    )
    parser.add_argument(
        "--servers",
        nargs="?",
        dest="srv_list",
        default="10.1.0.1,10.1.0.2",
        help="List of all servers present in the network",
    )
    args = parser.parse_args()
    server_id = args.id
    server_ip = "10.1.0.{}".format(server_id)
    servers_list = args.srv_list.split(",")
    # lead?er_ip = servers_list[len(servers_list)-1]

    try:
        server = Server(server_id, server_ip, servers_list)
        if(server_id == len(servers_list)):
            time.sleep(3)  # for starting all servers
            ## Starting election at starting
            election = Election(server_ip, server_id, servers_list)
            election.start()
        bottle.run(server, host=server_ip, port=PORT)
    except Exception as e:
        print("[ERROR] " + str(e))

# ------------------------------------------------------------------------------------------------------
if __name__ == "__main__":
    main()


from enum import Enum
import itertools
from typing import Any, Dict, List, Tuple

from ._ruleset import dnd5e

class _DnDRuleset(Enum):
    alignments: object = dnd5e["alignments"]
    backgrounds: object = dnd5e["backgrounds"]
    classes: object = dnd5e["classes"]
    feats: object = dnd5e["feats"]
    languages: object = dnd5e["languages"]
    metrics: object = dnd5e["metrics"]
    multiclasses: object = dnd5e["multiclasses"]
    proficiencies: object = dnd5e["proficiencies"]
    races: object = dnd5e["races"]
    skills: object = dnd5e["skills"]
    spell_lists: object = dnd5e["spell_lists"]
    subclasses: object = dnd5e["subclasses"]
    subraces: object = dnd5e["subraces"]

class DnDRuleset:
    def __init__(self):
        self.rs = self._read()

    @staticmethod
    def _read() -> Dict[str, Any]:
        rulesets = dict()
        for ruleset in _DnDRuleset:
            value = ruleset.value
            if isinstance(value, list):
                value = sorted(value)
            rulesets[ruleset.name] = value
        return rulesets

    def GetAlignmentList(self) -> Tuple[str, ...]:
        return tuple(self.rs["alignments"])

    def GetBackgroundList(self) -> Tuple[str, ...]:
        return tuple(self.rs["backgrounds"].keys())

    def GetBaseHeight(self, race: str) -> str:
        try:
            return self.rs["metrics"][race]["height"]
        except KeyError:
            return None

    def GetBaseWeight(self, race: str) -> str:
        try:
            return self.rs["metrics"][race]["weight"]
        except KeyError:
            return None

    def GetClassList(self) -> Tuple[str, ...]:
        return tuple(self.rs["classes"].keys())

    def GetFeatList(self) -> Tuple[str]:
        return tuple(self.rs["feats"].keys())

    def GetLanguageList(self, excluded_languages=None) -> Tuple[str]:
        all_languages = self.rs["languages"]
        if isinstance(excluded_languages, list):
            all_languages: List[str] = [
                l for l in all_languages if l not in excluded_languages
            ]
        return tuple(all_languages)

    def GetClassSpellList(self, klass: str, level: int, subklass=None) -> List[str]:
        if subklass in (
            "Arcane Trickster",
            "Eldritch Knight",
        ):
            klass = "Wizard"

        try:
            spell_list_by_level = self.rs["spell_lists"][klass][level]
            if len(spell_list_by_level) == 0:
                raise KeyError

            return [f"{s} (lv. {level})" for s in spell_list_by_level]
        except KeyError:
            return list()

    def GetDominantSex(self, race: str) -> str:
        try:
            return self.rs["metrics"][race]["dominant"]
        except KeyError:
            return None

    def GetEntryByBackground(self, background: str) -> Dict[str, Any]:
        try:
            return self.rs["backgrounds"][background]
        except KeyError:
            raise ValueError(f"Cannot find an entry for the '{background}' background.")

    def GetEntryByClass(self, klass: str):
        try:
            return self.rs["classes"][klass]
        except KeyError:
            raise ValueError(f"Cannot find an entry for the '{klass}' class.")

    def GetEntryByMulticlassOption(self, klass: str):
        try:
            return self.rs["multiclasses"][klass]
        except KeyError:
            return None

    def GetEntryByRace(self, race: str):
        try:
            return self.rs["races"][race]
        except KeyError:
            raise ValueError(f"Cannot find an entry for the '{race}' race.")

    def GetEntryBySubclass(self, subclass: str) -> Dict[str, Any]:
        try:
            return self.rs["subclasses"][subclass]
        except KeyError:
            raise ValueError(f"Cannot find an entry for the '{subclass}' subclass.")

    def GetEntryBySubrace(self, subrace: str):
        try:
            return self.rs["subraces"][subrace]
        except KeyError:
            raise ValueError(f"Cannot find an entry for the '{subrace}' subrace.")

    def GetFeatPerks(self, feat_name: str):
        try:
            return self.rs["feats"][feat_name]["perk"]
        except KeyError:
            return None

    def GetFeatProficiencies(self, feat: str, prof_type: str) -> list:
        try:
            return self.rs["feats"][feat]["perk"][prof_type]
        except KeyError:
            return None

    def GetFeaturesByClass(self, klass: str, level: int) -> List[str]:
        try:
            class_features = self.rs["classes"][klass]["features"]
        except KeyError:
            try:
                class_features = self.rs["subclasses"][klass]["features"]
            except KeyError:
                return []

        class_features = list(
            {k: v for k, v in class_features.items() if k <= level}.values()
        )

        return list(itertools.chain(*class_features))

    def GetFeatRequirements(self, feat_name: str):
        try:
            return self.rs["feats"][feat_name]
        except KeyError:
            return None

    def GetHitDieByClass(self, klass: str):
        try:
            return self.rs["classes"][klass]["hit_die"]
        except KeyError:
            return None

    def GetMetricsByRace(self, race: str) -> Dict[str, str]:
        """Returns metric data by race."""
        try:
            return self.rs["metrics"][race]
        except KeyError:
            return None

    def GetRaceList(self) -> Tuple[str]:
        return tuple(self.rs["races"].keys())

    def GetRacialSpellList(self, race: str, level: int):
        try:
            spell_list = self.rs["races"][race]["spells"]
        except KeyError:
            try:
                spell_list = self.rs["subraces"][race]["spells"]
            except KeyError:
                return []

        spell_list = list({k: v for k, v in spell_list.items() if k <= level}.values())

        return list(itertools.chain(*spell_list))

    def GetSkillAbility(self, skill_name: str) -> str:
        try:
            return self.rs["skills"][skill_name]["associated_ability"]
        except KeyError:
            return None

    def GetSkillList(self, excluded_skills=None) -> Tuple[str]:
        all_skills = list(self.rs["skills"].keys())
        if isinstance(excluded_skills, list):
            all_skills = [s for s in all_skills if s not in excluded_skills]
        return tuple(all_skills)

    def GetSpellSlots(self, klass: str, subklass: str, level: int) -> List[str]:
        if klass in (
            "Bard",
            "Cleric",
            "Druid",
            "Paladin",
            "Sorcerer",
            "Ranger",
            "Warlock",
            "Wizard",
        ) or subklass in (
            "Arcane Trickster",
            "Eldritch Knight",
        ):
            if subklass == "Arcane Trickster":
                klass = "Rogue"
            elif subklass == "Eldritch Knight":
                klass = "Fighter"

            spellslots = self.rs["classes"][klass]["spell_slots"][level]
            return [int(s) for s in spellslots.split(",")]
        return []

    def GetSubclassList(self, klass=None) -> Tuple[str]:
        try:
            return tuple(self.rs["classes"][klass]["subclass"])
        except KeyError:
            return tuple(self.rs["subclasses"].keys())

    def GetSubclassSpellList(self, sublass: str, level: int) -> List[str]:
        try:
            spells = self.rs["subclasses"][sublass]["bonus_spells"]
            if not isinstance(spells, dict):
                raise KeyError

            subclass_spells = list(
                {k: v for k, v in spells.items() if k <= level}.values()
            )

            return list(itertools.chain(*subclass_spells))
        except KeyError:
            return None

    def GetSubraceList(self, race=None) -> Tuple[str]:
        try:
            return tuple(self.rs["races"][race]["subrace"])
        except KeyError:
            return tuple(self.rs["subraces"].keys())

    def GetToolList(self, excluded_tools=None) -> Tuple[str]:
        all_tools = self.rs["proficiencies"]["tools"]
        if isinstance(excluded_tools, list):
            all_tools = [t for t in all_tools if t not in excluded_tools]
        return all_tools

    def GetWeaponList(self, excluded_weapons=None) -> Tuple[str]:
        _all_weapons = self.rs["proficiencies"]["weapons"]
        all_weapons = list()
        if not isinstance(excluded_weapons, list):
            martial, simple = _all_weapons.values()
            return tuple(sorted(itertools.chain(martial, simple)))
        else:
            if "Simple" not in excluded_weapons:
                all_weapons += _all_weapons["Simple"]
            if "Martial" not in excluded_weapons:
                all_weapons += _all_weapons["Martial"]
            all_weapons = [w for w in all_weapons if w not in excluded_weapons]
        return tuple(all_weapons)


from django.shortcuts import render, redirect
from django.views import View
from .models import Product, Cart, OrderPlaced
from .forms import CoustomerRegistrationFrom, ShippingAddressForm
from django.contrib import messages
from django.db.models import Q
from django.http import JsonResponse, HttpResponseBadRequest
from django.contrib.auth.decorators import login_required
# def home(request):
#  return render(request, 'app/home.html')

class ProductView(View):
    def get(self, request):
        topwears = Product.objects.filter(category='TM')
        bottomewears = Product.objects.filter(category='BW')
        mobiles = Product.objects.filter(category='M')
        laptops = Product.objects.filter(category='L')
        totalitem = 0
        if request.user.is_authenticated:
            totalitem = len(Cart.objects.filter(user=request.user))

        context = {'topwears':topwears, 'bottomewears':bottomewears, 'mobiles':mobiles, 'laptops':laptops, 'totalitem':totalitem}

        return render(request, 'app/home.html', context)

class ProductDetailView(View):
    def get(self, request, pk):
        product = Product.objects.get(pk=pk)
        item_already_in_cart = False
        if request.user.is_authenticated:
            item_already_in_cart = Cart.objects.filter(Q(product=product.id) & Q(user=request.user)).exists()

        totalitem = 0
        if request.user.is_authenticated:
            totalitem = len(Cart.objects.filter(user=request.user))

        context = {'product':product, 'item_already_in_cart':item_already_in_cart, 'totalitem':totalitem}

        return render(request, 'app/productdetail.html', context)

@login_required
def add_to_cart(request):
    user = request.user
    product_id = request.GET.get('prod_id')
    product = Product.objects.get(id=product_id)

    if Cart.objects.filter(Q(product=product.id) & Q(user=request.user)).exists():
        return redirect(f'/product-detail/{product_id}')

    cart = Cart(user=user, product=product)
    print(cart)

    cart.save()

    return redirect(f'/product-detail/{product_id}')

@login_required
def show_cart(request):
    if request.user.is_authenticated:
        user = request.user
        carts = Cart.objects.filter(user=user)
        amount = 0.0
        shipping_amount = 70.0
        cart_product = [p for p in Cart.objects.all() if p.user == user]

        totalitem = 0
        if request.user.is_authenticated:
            totalitem = len(Cart.objects.filter(user=request.user))

        if cart_product:
            for p in cart_product:
                temp_amount = (p.quantity * p.product.discounted_price)
                amount += temp_amount
        if amount == 0:
            total_amount = 0.0
        else:
            total_amount = shipping_amount + amount

    context={'carts': carts, 'amount':amount, 'total_amount':total_amount, 'totalitem':totalitem}
    return render(request, 'app/addtocart.html', context)

@login_required
def plus_cart(request):
    if request.method =="GET":
        prod_id = request.GET['prod_id']
        c = Cart.objects.get(Q(product=prod_id) & Q(user=request.user))
        c.quantity+=1
        c.save()
        amount = 0.0
        shipping_amount = 70.0
        cart_product = [p for p in Cart.objects.all() if p.user == request.user]
        if cart_product:
            for p in cart_product:
                temp_amount = (p.quantity * p.product.discounted_price)
                amount += temp_amount
        if amount == 0:
            total_amount = 0.0
        else:
            total_amount = shipping_amount + amount

        data = {
            'quantity': c.quantity,
            'amount':amount,
            'total_amount':total_amount
            }
        return JsonResponse(data)

@login_required
def minus_cart(request):
    if request.method =="GET":
        prod_id = request.GET['prod_id']
        c = Cart.objects.get(Q(product=prod_id) & Q(user=request.user))
        if c.quantity != 1:
            c.quantity-=1
            c.save()
        amount = 0.0
        shipping_amount = 70.0
        cart_product = [p for p in Cart.objects.all() if p.user == request.user]
        if cart_product:
            for p in cart_product:
                temp_amount = (p.quantity * p.product.discounted_price)
                amount += temp_amount
        if amount == 0:
            total_amount = 0.0
        else:
            total_amount = shipping_amount + amount

        data = {
            'quantity': c.quantity,
            'amount':amount,
            'total_amount':total_amount
            }
        return JsonResponse(data)

@login_required
def remove_from_cart(request, id):
    p = Cart.objects.get(id=id)
    print(p)
    p.delete()
    return redirect('/cart')

@login_required
def buy_now(request):
 return render(request, 'app/buynow.html')

@login_required
def orders(request):
    op = OrderPlaced.objects.filter(user= request.user)
    totalitem = 0
    if request.user.is_authenticated:
        totalitem = len(Cart.objects.filter(user=request.user))
    context={'op':op, 'totalitem':totalitem}
    return render(request, 'app/orders.html', context)

def mobile(request, data=None):
    if data == None:
        mobile = Product.objects.filter(category='M')
    elif data == 'below':
        mobile = Product.objects.filter(category='M').filter(discounted_price__lt=10000)
    elif data == 'above':
        mobile = Product.objects.filter(category='M').filter(discounted_price__gt=10000)
    else:
        mobile = Product.objects.filter(category='M').filter(brand=data)

    allmobile = Product.objects.filter(category='M').values('brand').distinct()

    totalitem = 0
    if request.user.is_authenticated:
        totalitem = len(Cart.objects.filter(user=request.user))
    context = {
        'mobile':mobile,
        'allmobile':allmobile,
        'totalitem':totalitem
        }

    return render(request, 'app/mobile.html', context)

def laptop(request, data=None):
    if data == None:
        laptop = Product.objects.filter(category='L')
    elif data == 'below':
        laptop = Product.objects.filter(category='L').filter(discounted_price__lt=35000)
    elif data == 'above':
        laptop = Product.objects.filter(category='L').filter(discounted_price__gt=35000)
    else:
        laptop = Product.objects.filter(category='L').filter(brand=data)

    alllaptop = Product.objects.filter(category='L').values('brand').distinct()

    totalitem = 0
    if request.user.is_authenticated:
        totalitem = len(Cart.objects.filter(user=request.user))

    context = {
        'laptop':laptop,
        'alllaptop':alllaptop,
        'totalitem':totalitem
        }

    return render(request, 'app/laptop.html', context)

def topwear(request, data=None):
    if data == None:
        topwear = Product.objects.filter(category='TM')
    elif data == 'below':
        topwear = Product.objects.filter(category='TM').filter(discounted_price__lt=35000)
    elif data == 'above':
        topwear = Product.objects.filter(category='TM').filter(discounted_price__gt=35000)
    else:
        topwear = Product.objects.filter(category='TM').filter(brand=data)

    alltopwear = Product.objects.filter(category='TM').values('brand').distinct()

    totalitem = 0
    if request.user.is_authenticated:
        totalitem = len(Cart.objects.filter(user=request.user))

    context = {
        'topwear':topwear,
        'alltopwear':alltopwear,
        'totalitem':totalitem
        }

    return render(request, 'app/topwear.html', context)

def bottomwear(request, data=None):
    if data == None:
        bottomwear = Product.objects.filter(category='BW')
    elif data == 'below':
        bottomwear = Product.objects.filter(category='BW').filter(discounted_price__lt=35000)
    elif data == 'above':
        bottomwear = Product.objects.filter(category='BW').filter(discounted_price__gt=35000)
    else:
        bottomwear = Product.objects.filter(category='BW').filter(brand=data)

    allbottomwear = Product.objects.filter(category='BW').values('brand').distinct()

    totalitem = 0
    if request.user.is_authenticated:
        totalitem = len(Cart.objects.filter(user=request.user))
    context = {
        'bottomwear':bottomwear,
        'allbottomwear':allbottomwear,
        'totalitem':totalitem
        }

    return render(request, 'app/bottomwear.html', context)

# def login(request):
#  return render(request, 'app/login.html')

# def customerregistration(request):
#  return render(request, 'app/customerregistration.html')

class CustomerRegistrationView(View):
    def get(self, request):
      form = CoustomerRegistrationFrom()
      context = {'form': form}
      return render(request, 'app/customerregistration.html', context)

    def post(self, request):
        form = CoustomerRegistrationFrom(request.POST)
        if form.is_valid():
           messages.success(request, 'Congratulation !! Account Registered Successfully.')
           form.save()
        context = {'form': form}
        return render(request, 'app/customerregistration.html', context)

@login_required
def checkout(request):
    if request.method == 'POST':
        user = request.user
        SAForm = ShippingAddressForm(request.POST)
        if SAForm.is_valid():
            location = SAForm.cleaned_data['location']
            name = SAForm.cleaned_data['name']
            phone_number = SAForm.cleaned_data['phone_number']
            state = SAForm.cleaned_data['state']
            payment_method = SAForm.cleaned_data['payment_method']
        else:
            messages.error(request,'Unvalid')
            return redirect("checkout")

        cart = Cart.objects.filter(user=user)
        for c in cart:
            OrderPlaced(user=user, location=location, name=name, phone_number=phone_number, state=state, payment_method=payment_method, product=c.product, quantity=c.quantity).save()
            c.delete()

        return redirect("orders")

    else:
        SAForm = ShippingAddressForm()
        user = request.user
        cart_items = Cart.objects.filter(user=user)

        totalitem = 0
        if request.user.is_authenticated:
            totalitem = len(Cart.objects.filter(user=request.user))

        amount = 0.0
        shipping_amount = 70.0
        cart_product = [p for p in Cart.objects.all() if p.user == request.user]
        if cart_product:
                for p in cart_product:
                    temp_amount = (p.quantity * p.product.discounted_price)
                    amount += temp_amount
                if amount == 0:
                    total_amount = 0.0
                else:
                    total_amount = shipping_amount + amount
        else:
            messages.error(request,'Please add product before order placed')
            return redirect("show-cart")

        context={'SAForm':SAForm, 'cart_items':cart_items, 'total_amount':total_amount, 'totalitem':totalitem}
        return render(request, 'app/checkout.html', context)


from django.shortcuts import render
from django.contrib.auth.decorators import login_required

from savings.models import Savings
from installment.models import LoanInstallment
from loan.models import Loan

# def login(request):
#     return render(request, 'login.html')

@login_required
def dashboard(request):

    sv = Savings.objects.all()
    ls = Loan.objects.all()

    total_savings_person = sv.count()
    total_loan_person = ls.count()

    total_savings_amount = 0
    for s in sv:
        total_savings_amount += int(s.amount)

    total_loan_amount = 0
    for l in ls:
        total_loan_amount += int(l.amount)

    loan_after_interest = total_loan_amount * 1.2

    total_loan_paid = 0

    for n in ls:
        i = LoanInstallment.objects.filter(name=n.id)
        amount = (int(n.amount) *1.2) / 12
        total_loan_paid += i.count() * int(amount)

    loan_due = loan_after_interest - total_loan_paid

    context = {
        'title': 'Dashboard',
        'total_savings': total_savings_person,
        'total_savings_amount': total_savings_amount,
        'total_loan_person': total_loan_person,
        'total_loan_amount': int(loan_due)
    }

    return render(request, 'dashboard.html', context)

@login_required
def profile(request):
    return render(request, 'profile.html')


from sys import stdin
from functools import cmp_to_key

# 17% 틀립니다

# 반례
# 2 2
# 34
# 344

# ans : 34434
# output : 34344
K, N = map(int, stdin.readline().split())

def sol():
    target = ""

    numbers = []

    for _ in range(K):
        numbers.append(stdin.readline().rstrip())

    # 자릿수가 긴것을 앞으로,자릿수가 같다면 크기가 큰 것
    # 자릿수가 길고 가장 큰것이 가장 많이 사용됨 N-K번
    # 나머지는 다 한번씩만 사용해줘야함
    key = str(max(list(map(int, numbers))))

    for _ in range(N - K):
        numbers.append(key)

    new_numbers = custom_sort(numbers)

    target = "".join(new_numbers)

    # for num in new_numbers:
    #   target+=num

    # if N!=K:
    #   target=target+key*(N-K)

    return int(target)

def custom_digit_compare(a, b):
    if int(str(a) + str(b)) > int(str(b) + str(a)):
        return 1
    else:
        return -1

# 수정필요
def custom_sort(list_):
    # tmp = []

    # max_len = 0

    # for item in list_:
    #   max_len = max(len(item), max_len)

    # for idx, num in enumerate(list_):
    #   diff = 0

    #   list_num = list(num)

    #   first_digit = int(list_num[0])

    #   # 가장 큰 자리수의 숫자로부터
    #   # 나머지 숫자들이 얼마나 떨어져있는지 기록
    #   # 작을수록 큰 값
    #   for i in range(1, len(list_num)):
    #     diff += first_digit - int(list_num[i])

    #   digit = int(num) % 10

    #   if len(num) < max_len:
    #     tmp.append((idx, num + str(digit) * (max_len - len(num)), diff))
    #   else:
    #     tmp.append((idx, num, diff))

    # # 일의 자리를 연장한 값을 먼저 비교하고
    # # 32  322 같이 연장한 값이 같다면
    # # 위에서 계산한 diff를 그다음 기준
    # tmp.sort(key=lambda x: (-int(x[1]), x[2]))
    # ret = []

    list_ = sorted(
        list_, key=cmp_to_key(lambda a, b: -1 if int(str(a) + str(b)) > int(str(b) + str(a)) else 1)
    )

    return list_

    # for idx, num, diff in tmp:
    #   ret.append(list_[idx])

    # return ret

print(sol())


import requests
import re
from flask import current_app

class WebSpider:
    """网络爬虫"""
    def __init__(self, words):
        self.result = []
        self.words = words
        self.url = ''
        self.html = ''
        self.headersParameters = {    # 发送HTTP请求时的HEAD信息，用于伪装为浏览器
            'Connection': 'Keep-Alive',
            'Accept': 'text/html, application/xhtml+xml, */*',
            'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
            'Accept-Encoding': 'gzip, deflate',
            'User-Agent': 'Mozilla/6.1 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
        }

    def get_baidu(self):
        self.url = 'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=monline_dg&wd={}'.format(self.words)
        response = requests.get(self.url, headers=self.headersParameters)
        if response.status_code is not 200:
            current_app.logger.info('爬虫请求链接响应的状态码不是200')
            return self.result
        else:
            self.html = response.text
            self.result = re.findall(r'''href = "(.*?)".*?>(.*?).*?style="text-decoration:none;">(.*?) ''',
                                     self.html, re.MULTILINE | re.DOTALL)
            return self.result


import cv2
import argparse

from ultralytics import YOLO
import supervision as sv
import numpy as np

import pandas as pd

# File path of the Excel file
file_path = r'C:\Users\Администратор\Downloads\Wrenches.xlsx'

# Sheet name to read
sheet_name = 'Лист1'

# Read Excel file
df = pd.read_excel(file_path, sheet_name=sheet_name)

# Display the data in the DataFrame
print(df)

# User input prompt to request information based on ID

ZONE_POLYGON = np.array([
    [0, 0],
    [0.6, 0],
    [0.6, 1],
    [0, 1]
])

def parse_arguments() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="YOLOv8 live")
    parser.add_argument(
        "--webcam-resolution",
        default=[1150, 720],
        nargs=2,
        type=int
    )
    args = parser.parse_args()
    return args

def main():
    args = parse_arguments()
    frame_width, frame_height = args.webcam_resolution

    cap = cv2.VideoCapture(0)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, frame_width)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, frame_height)

    model = YOLO("yolov8l.pt")

    box_annotator = sv.BoxAnnotator(
        thickness=2,
        text_thickness=2,
        text_scale=1
    )

    zone_polygon = (ZONE_POLYGON * np.array(args.webcam_resolution)).astype(int)
    zone = sv.PolygonZone(polygon=zone_polygon, frame_resolution_wh=tuple(args.webcam_resolution))
    zone_annotator = sv.PolygonZoneAnnotator(
        zone=zone,
        color=sv.Color.red(),
        thickness=2,
        text_thickness=4,
        text_scale=2
    )

    while True:
        ret, frame = cap.read()

        result = model(frame, agnostic_nms=True)[0]
        detections = sv.Detections.from_yolov8(result)
        labels = [
            #f"{model.model.names[class_id]} {confidence:0.2f}"
            "wrenches"
            for _, confidence, class_id, _
            in detections
        ]
        frame = box_annotator.annotate(
            scene=frame,
            detections=detections,
            labels=labels
        )

        zone.trigger(detections=detections)
        frame = zone_annotator.annotate(scene=frame)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # https://medium.com/analytics-vidhya/gaussian-blurring-with-python-and-opencv-ba8429eb879b
        blur = cv2.GaussianBlur(gray, (41,41), 0)
        thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
        print(cv2.THRESH_BINARY, cv2.THRESH_OTSU)

        # Morphological transformations https://docs.opencv.org/4.x/d9/d61/tutorial_py_morphological_ops.html
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
        opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
        # Find contours https://learnopencv.com/contour-detection-using-opencv-python-c/
        # CHAIN_APPROX_SIMPLE
        # Алгоритм сжимает горизонтальные, вертикальные и диагональные сегменты вдоль контура и оставляет только их конечные точки.
        # Это означает, что любая из точек вдоль прямых путей будет отклонена, и у нас останутся только конечные точки.
        cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        cnts = cnts[0] if len(cnts) == 2 else cnts[1]

        i=1
        for c in cnts:
            # Find perimeter of contour
            perimeter = cv2.arcLength(c, True)
            # Perform contour approximation
            approx = cv2.approxPolyDP(c, 0.04 * perimeter, True)
            # print(approx)
            # We assume that if the contour has more than a certain
            # number of verticies, we can make the assumption
            # that the contour shape is a circle
            if len(approx) > 0:

                # Obtain bounding rectangle to get measurements
                x, y, w, h = cv2.boundingRect(c)

                # Find measurements
                diameter = w
                radius = w / 2

                # Find centroid
                M = cv2.moments(c)

                if M["m00"] != 0:
                    cX = int(M["m10"] / M["m00"])
                    cY = int(M["m01"] / M["m00"])
                # print(cX,cY)
                # Draw the contour and center of the shape on the image
                # cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),4)
                cv2.drawContours(frame, [c], 0, (36, 255, 12), 1)
                # cv2.circle(image, (cX, cY), 15, (320, 159, 22), -1)

                # Draw line and diameter information
                # cv2.line(image, (x, y + int(h/2)), (x + w, y + int(h/2)), (156, 188, 24), 3)
                # cv2.putText(image, "Diameter: {}".format(diameter), (cX - 150, cY - 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (156, 188, 24), 3)
                font = cv2.FONT_HERSHEY_SIMPLEX

        # org
                org = (cX, cY)

        # fontScale
                fontScale = 1

        # Blue color in BGR
                color = (255, 0, 0)

        # Line thickness of 2 px
                thickness = 2

        # Using cv2.putText() method
                frame = cv2.putText(frame,str(i), org, font, fontScale, color, thickness, cv2.LINE_AA)
                i=i+1
        # Displaying the image
                cv2.imshow("yolov8", frame)

                if (cv2.waitKey(30) == 27):
                    break
            request = i

            try:
                id = int(request)
                result = df[df['id'] == id]
                if result.empty:
                    print("id not found.")
                else:
                    print(result)
            except ValueError:
                print("Invalid input. Please enter a valid integer id.")

if __name__ == "__main__":
    main()


from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import Ridge
from sklearn.feature_extraction import DictVectorizer
import pandas as pd
import re
from scipy.sparse import hstack

dataTrainRaw = pd.read_csv('salary-train.csv', nrows=50000)
dataTestRaw = pd.read_csv('salary-test-mini.csv')

def answer(list, name):
    f = open(name, 'w')
    space=''
    for i in list:
        f.write(space+str(i))
        space=' '

def modify_text(x):
    x = x.lower()
    x = re.sub('[^a-zA-Z0-9]', ' ', x)
    return x

def one_hot_encoding(df, DV, type):
    if type == 'train':
        DV = DictVectorizer()
        X_categ = DV.fit_transform(df.to_dict('records'))
        return X_categ, DV
    else:
        X_categ = DV.transform(df.to_dict('records'))
        return X_categ, DV

def prepare_data(data, vectorizer, DV, type):
    data_fd_list = data["FullDescription"].tolist()
    data_fd_list = list(map(modify_text, data_fd_list))
    if type == 'train':
        vectorizer = TfidfVectorizer(min_df=5)
        data_fd_vector = vectorizer.fit_transform(data_fd_list)
    else:
        data_fd_vector = vectorizer.transform(data_fd_list)
    data['LocationNormalized'].fillna('nan', inplace=True)
    data['ContractTime'].fillna('nan', inplace=True)
    X_categ, DV = one_hot_encoding(data[['LocationNormalized', 'ContractTime']], DV, type)
    X = hstack([data_fd_vector, X_categ])
    if type == 'train':
        return X, vectorizer, DV
    return X

X_train, vectorizer, DV = prepare_data(dataTrainRaw, None, None, 'train')
y_train = dataTrainRaw['SalaryNormalized']
X_test = prepare_data(dataTestRaw, vectorizer, DV, 'test')

Regression = Ridge(alpha=1, random_state=241)
Regression.fit(X_train, y_train)

P = Regression.predict(X_test)

print(P)
answer(P, "1")

Linear regression: salary forecast by job description/main.py


#COMPARADOR V1
#Versión 1 del comparador que permite con un símbolo, precio y hace cuantas horas comprado, chequear las variaciones con
#respecto a las demás monedas

import config
from binance.client import Client
from binance.enums import *
from pandas.core.frame import DataFrame
import pandas as pd
import time
#import vlc
#import threading
#import math
#import csv
#import itertools
import datetime
from scipy import stats
import numpy as np
from colorama import init
from colorama import Fore, Back, Style

#que funcione con un ingreso de fecha especifica y moneda especifica o precio especifioco y moneda para calcular con respecto a todos de una vez

#-----------------------------------------------------------------------------------------------------------
#CONEXIÓN
#-----------------------------------------------------------------------------------------------------------

client = Client(config.API_KEY, config.API_SECRET, tld='com')

#-----------------------------------------------------------------------------------------------------------
#VARIABLES GLOBALES
#-----------------------------------------------------------------------------------------------------------

#Moneda base que será analizada, comparada y quizá cambiada
#moneda_base = 'ETHUSDT'

#Cantidad de horas atras cuando se compró la moneda base
#cantidad_horas_atras = 14

#precio base con el que se compró la moneda, para que sirva de referencia en la comparativa
precio_base = 2120.99

#Cantidad comprada de moneda base, se utilizará  para hacer la transacción con otra moneda
cantidad_moneda_base = 0.01541

#Lista de monedas a analizar
listacoin = ['BTCUSDT',
'ETHUSDT',
'MATICUSDT',
'ADAUSDT',
'BNBUSDT',
'XRPUSDT',
'DOTUSDT',
'ICPUSDT',
'SOLUSDT',
'SHIBUSDT',
'LINKUSDT',
'LTCUSDT',
'THETAUSDT',
'DOGEUSDT',
'VETUSDT',
'EOSUSDT',
'TRXUSDT',
'BCHUSDT',
'NEOUSDT',
'COMPUSDT',
'UNIUSDT'
]
#listacoin = ['BTCUSDT','ETHUSDT','MATICUSDT']

#-----------------------------------------------------------------------------------------------------------
#FUNCIONES
#-----------------------------------------------------------------------------------------------------------

#Esta función proporciona el precio de mercado actual
def precio_mercado_actual(simbolo):

    try:
        list_of_tickers = client.get_all_tickers()

        for tick_2 in list_of_tickers:
            if tick_2['symbol'] == simbolo:
                symbolPrice = float(tick_2['price'])
                return float(symbolPrice)
    except:
        return 'null'

#-----------------------------------------------------------------------------------------------------------
#MAIN
#-----------------------------------------------------------------------------------------------------------

print('---------------------------------------------')
print('COMPARADOR V1')
print('---------------------------------------------')

r_moneda= input('Por favor ingrese la MONEDA base que compró: ')

r_precio= input('Por favor ingrese el PRECIO con el que compró: ')

r_dias= input('Por favor ingrese HACE CUANTAS HORAS lo ha comprado: ')

print('************************************************')

if (r_moneda=='') and (r_precio==''):

    moneda_base = 'NONE'

else:

    moneda_base = str(r_moneda)
    precio_base = float(r_precio)

cantidad_horas_atras = int(r_dias)
#agrupa para convertir en df
agrupador2=[]
fecha_referencia= str(cantidad_horas_atras)+' hour ago UTC'

for coin in listacoin:

    agrupador = []

    #Si moneda es igual a la moneda base, toma el precio base y no lo jala como el resto para la comparativa
    if coin == moneda_base:
        precio_referencia = precio_base

    else:
        precio_referencia = float(client.get_historical_klines(coin, Client.KLINE_INTERVAL_1MINUTE, fecha_referencia )[0][2])

    #precio actual de mercado para el coin en curso
    precio_actual = precio_mercado_actual(coin)

    print('trabajando: ',coin)
    #print(coin)
    #print(precio_referencia)
    #print(precio_actual)
    porcentaje=round(((precio_actual-precio_referencia)/precio_referencia)*100,2)
    #print(porcentaje,'%')
    #print("-------------")

    agrupador.append(coin)
    agrupador.append(precio_referencia)
    agrupador.append(precio_actual)
    agrupador.append(porcentaje)

    agrupador2.append(agrupador)

dfr = pd.DataFrame(agrupador2, columns = ['Symbol', 'precio_inicial','precio_actual','variacion'])
dfr_sorted = dfr.sort_values(by=['variacion'],ascending=True)
print(dfr_sorted)

#Seleccionando la moneda elegida
moneda_destino = dfr_sorted.iloc[0,0]
#Seleccionando precio de la moneda elegida
precio_ref_moneda_destino = dfr_sorted.iloc[0,1]

#Si la moneda a cambiar es la misma no se realiza ninguna transacción transacciòn
if moneda_destino==moneda_base:
    print('No se realizará la transacciòn, porque se trata de la misma moneda')
    transaccion = 0

else:
    transaccion = 1

#-------------------------------------TRANSACCIÒN-------------------------------------------------------


from gpiozero import LED
from time import sleep
import random
import threading
import sys
import pygame
pygame.mixer.init()
pygame.mixer.music.load("SugarPlum.wav")
pygame.mixer.music.play()

bpm= 121
bps = 121*1.0/60/8

relays= [ LED(4),LED(17) ,LED(27),LED(22) ,
         LED(12),LED(16) ,LED(20),LED(21) ]
prev_x= 0
prev_y= 0
x = 0
def relayLed():
  threading.Timer(bps,relayLed).start()
  [l.off() for l in relays]

  x = random.randint(0,7)
  y = random.randint(0,7)
  while y == x and (x == prev_x or y == prev_y) :
    x = random.randint(0,7)
    y = random.randint(0,7)
  prev_x = x
  prev_y = y
  relays[x].on()
  relays[y].on()

relayLed()
while pygame.mixer.music.get_busy() == True:
    continue
sys.exit()


# from https://github.com/dparks1134/UniteM/blob/master/unitem/markers.py
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import logging
from collections import defaultdict

from checkm.defaultValues import DefaultValues
from checkm.markerSets import MarkerSetParser, BinMarkerSets

from unitem_defaults import *

"""
CHECKM_BAC_MS = 'bacteria.ms'
CHECKM_AR_MS = 'archaea.ms'
"""

class Markers():
    """Read marker information."""

    def __init__(self):
        """Initialization."""

        self.logger = logging.getLogger('timestamp')

        parser = MarkerSetParser()
        bin_marker_sets = parser.parseTaxonomicMarkerSetFile(CHECKM_BAC_MS)
        self.bac_ms = bin_marker_sets.mostSpecificMarkerSet()

        bin_marker_sets = parser.parseTaxonomicMarkerSetFile(CHECKM_AR_MS)
        self.ar_ms = bin_marker_sets.mostSpecificMarkerSet()

        self.bac_markers_on_contigs = None
        self.ar_markers_on_contigs = None

    # update for metabinner
    def read_table(self, marker_gene_table):
        """Read table indicating marker genes for each bin."""

        markers = defaultdict(lambda: defaultdict(list))
        if not os.path.exists(marker_gene_table):
            # did not identify any markers
            return markers

        with open(marker_gene_table) as f:
            f.readline()

            for line in f:
                line_split = line.strip().split('\t')

                # bin_id = line_split[0]
                marker_id = line_split[1]
                gene_id = line_split[2]
                if '&&' in gene_id:
                    # indicates a marker gene is identified
                    # in adjacent genes and is likely an
                    # assembly or gene calling error
                    gene_ids = gene_id.split('&&')
                    gene_id = gene_ids[0]
                scaffold_id = gene_id[0:gene_id.rfind('_')]

                if marker_id not in markers:
                    markers[marker_id] = [scaffold_id]
                else:
                    markers[marker_id].append(scaffold_id)

        return markers

    def evaluate_bac(self, gene_table, individual_markers=False):
        """Evaluate completeness and contamination of genome using bacterial marker sets."""

        comp, cont = self.bac_ms.genomeCheck(gene_table, individual_markers)
        return comp, cont

    def evaluate_ar(self, gene_table, individual_markers=False):
        """Evaluate completeness and contamination of genome using archaeal marker sets."""

        comp, cont = self.ar_ms.genomeCheck(gene_table, individual_markers)
        return comp, cont

    def evaluate(self, bac_gene_table, ar_gene_table):
        """Evaluate completeness and contamination of genome using best domain-level marker sets."""

        bac_comp, bac_cont = self.evaluate_bac(bac_gene_table, True)
        ar_comp, ar_cont = self.evaluate_ar(ar_gene_table, True)

        if bac_comp + bac_cont > ar_comp + ar_cont:
            # select domain set with the larget number of identified markers
            # including those present multiple times
            bac_comp, bac_cont = self.evaluate_bac(bac_gene_table, False)
            return 'Bacteria', bac_comp, bac_cont

        ar_comp, ar_cont = self.evaluate_ar(ar_gene_table, False)
        return 'Archaea', ar_comp, ar_cont

    def bin_quality(self, bin):
        """Estimate quality of bin."""

        # create gene tables for bin
        bac_gene_table = defaultdict(list)
        ar_gene_table = defaultdict(list)
        for cid in bin:
            for marker_id in self.bac_markers_on_contigs[cid]:
                bac_gene_table[marker_id].append(cid)

            for marker_id in self.ar_markers_on_contigs[cid]:
                ar_gene_table[marker_id].append(cid)

        domain, comp, cont = self.evaluate(bac_gene_table,
                                           ar_gene_table)

        return domain, comp, cont

    # changed for metabinner
    def marker_gene_tables(self, bac_mg_table, ar_mg_table):
        """Get marker genes for bins across all binning methods."""

        markers = Markers()
        # binning_methods_dir = os.path.join(profile_dir, BINNING_METHOD_DIR)

        # bac_mg_table = os.path.join(binning_methods_dir, bm, CHECKM_BAC_DIR, MARKER_GENE_TABLE)
        # bac_mg_table ='/home1/wangzy/binning_data/sim40_20/output/metabinner_res/unitem/my_profile/binning_methods/X_cov_notrans1/checkm_bac/marker_gene_table.tsv'
        bac_gene_tables = markers.read_table(bac_mg_table)

        # ar_mg_table = os.path.join(binning_methods_dir, bm, CHECKM_AR_DIR, MARKER_GENE_TABLE)
        # ar_mg_table ='/home1/wangzy/binning_data/sim40_20/output/metabinner_res/unitem/my_profile/binning_methods/X_cov_notrans1/checkm_ar/marker_gene_table.tsv'
        ar_gene_tables = markers.read_table(ar_mg_table)

        gene_tables = (bac_gene_tables, ar_gene_tables)

        self._markers_on_contigs(gene_tables)

        return gene_tables

    # update for metabinner
    def _markers_on_contigs(self, gene_tables):
        """Get markers on each contig."""

        self.bac_markers_on_contigs = defaultdict(list)
        self.ar_markers_on_contigs = defaultdict(list)
        processed_contigs = set()
        (bac_gene_tables, ar_gene_tables) = gene_tables
        scaffolds_in_binning_method = set()
        for marker_id, scaffold_ids in bac_gene_tables.items():
            for scaffold_id in scaffold_ids:
                if scaffold_id in processed_contigs:
                    continue
                self.bac_markers_on_contigs[scaffold_id].append(marker_id)
            scaffolds_in_binning_method.update(scaffold_ids)

        for marker_id, scaffold_ids in ar_gene_tables.items():
            for scaffold_id in scaffold_ids:
                if scaffold_id in processed_contigs:
                    continue
                self.ar_markers_on_contigs[scaffold_id].append(marker_id)
            scaffolds_in_binning_method.update(scaffold_ids)

        processed_contigs.update(scaffolds_in_binning_method)

    def create_gene_table(self, contig_ids):
        """Create gene tables for a set of contigs."""

        bac_table = defaultdict(list)
        ar_table = defaultdict(list)
        for cid in contig_ids:
            for mid in self.bac_markers_on_contigs[cid]:
                bac_table[mid].append(cid)

            for mid in self.ar_markers_on_contigs[cid]:
                ar_table[mid].append(cid)

        return bac_table, ar_table


import numpy
import pandas
import openpyxl
from neo4j import GraphDatabase

def loadDataSet():  #加载数据集
    wb = openpyxl.load_workbook('Company_relationship.xlsx')
    # 读取工作表
    ws = wb['Sheet1']
    Company = []
    Ticker = []
    Primary_Industry = []
    Industry_category = []
    for col in ws['A']:
        Company.append(col.value)
    Company.remove('Company Name')
    for col in ws['B']:
        Ticker.append(col.value)
    Ticker.remove('Ticker')
    for col in ws['C']:
        Primary_Industry.append(col.value)
    Primary_Industry.remove('Primary Industry')
    for col in ws['D']:
        Industry_category.append(col.value)
    Industry_category.remove('Industry category')

    print("Company", Company)
    print("Ticker", Ticker)
    print("Primary_Industry", Primary_Industry)
    print("Industry_category", Industry_category)

    return Company, Ticker, Primary_Industry, Industry_category

class Example(object):

    def __init__(self, uri, user, password):
        self._driver = GraphDatabase.driver(uri, auth=(user, password))

    def close(self):
        self._driver.close()
    #创建实体节点
    def create_Company(cls, tx, Name):
        tx.run("CREATE (:Company {Name: $Name})", Name=Name)

    def create_Ticker(cls, tx, Ticker):
        tx.run("CREATE (:Ticker {Name: $Ticker})", Ticker = Ticker)

    def create_Industry(cls, tx, Industry):
        tx.run("CREATE (:Industry {Name: $Industry})", Industry = Industry)

    def create_Industry_Category(cls, tx, Industry_Category):
        tx.run("CREATE (:Industry_Category {Name: $Industry_Category})", Industry_Category = Industry_Category)

    #创建关系
    def Company_Ticker(cls, tx, node_name1, node_name2):
        tx.run("MATCH (node1: Company {Name: $node_name1}) "
               "MATCH (node2: Ticker {Name: $node_name2}) "
               "CREATE (node1)-[:issue]->(node2)",
               node_name1=node_name1, node_name2=node_name2)

    def Company_Industry(cls, tx, node_name1, node_name2):
        print(node_name1, node_name2)
        tx.run("MATCH (node1: Company {Name: $node_name1}) "
               "MATCH (node2: Industry {Name: $node_name2}) "
               "CREATE (node1)-[:engage]->(node2)",
               node_name1=node_name1, node_name2=node_name2)

    def Industry_Classify(cls, tx, node_name1, node_name2):
        print(node_name1, node_name2)
        tx.run("MATCH (node1: Industry {Name: $node_name1}) "
               "MATCH (node2: Industry_Category {Name: $node_name2}) "
               "CREATE (node1)-[:belong_to]->(node2)",
               node_name1=node_name1, node_name2=node_name2)

    #创建Driver
    def create_Company_driver(self, message):
        with self._driver.session() as session:
            session.write_transaction(self.create_Company, message)

    def create_Ticker_driver(self, message):
        with self._driver.session() as session:
            session.write_transaction(self.create_Ticker, message)

    def create_Industry_driver(self, message):
        with self._driver.session() as session:
            session.write_transaction(self.create_Industry, message)

    def create_Industry_Category_driver(self, message):
        with self._driver.session() as session:
            session.write_transaction(self.create_Industry_Category, message)

    def create_Company_Ticker_driver(self, message1, message2):
        with self._driver.session() as session:
            session.write_transaction(self.Company_Ticker, message1, message2)

    def create_Company_Industry_driver(self, message1, message2):
        with self._driver.session() as session:
            session.write_transaction(self.Company_Industry, message1, message2)

    def create_Industry_Classify_driver(self, message1, message2):
        with self._driver.session() as session:
            session.write_transaction(self.Industry_Classify, message1, message2)

exam=Example("bolt://localhost:7687","neo4j","myneo4j")

Company, Ticker, Primary_Industry, Industry_Category = loadDataSet()
Company1 = set(Company)
Ticker1 = set(Ticker)
Primary_Industry1 = set(Primary_Industry)
Industry_Category1 = set(Industry_Category)

#导入节点
for each in Company1:
    exam.create_Company_driver(each)
for each in Ticker1:
    exam.create_Ticker_driver(each)
for each in Primary_Industry1:
    exam.create_Industry_driver(each)
for each in Industry_Category1:
    exam.create_Industry_Category_driver(each)

#导入公司和股票的关系
for i in range(0, len(Company)):
    exam.create_Company_Ticker_driver(Company[i], Ticker[i])

#导入公司和行业的关系
exam.create_Company_Industry_driver(Company[0], Primary_Industry[0])
for i in range(1, len(Company)):
    if(Company[i] != Company[i-1]):
        exam.create_Company_Industry_driver(Company[i], Primary_Industry[i])

#行业分类
s = []
for i in range(0, len(Primary_Industry)):
    if(Primary_Industry[i] not in s):
        exam.create_Industry_Classify_driver(Primary_Industry[i], Industry_Category[i])
        s.append(Primary_Industry[i])


from multiprocessing import Process, Queue
from random import randint
from typing import Union
import pygame
import time
import os

def update_queue(q: Queue, msg: Union[str, bool]) -> None:
    """Put a new message in the multiprocessing queue for main process.

    Will check if the queue is full. If it is it will empty it first.

    Args:
        q (Queue):              The queue to put the new message in.
        msg (Union[str, bool]): The message to put in the queue.
    """

    if q.full():
        q.get()

    q.put(msg)

def lower_song_volume(mixer: pygame.mixer, cutoff_value: float = 0.05,
                      cutoff_speed: float = 0.2) -> None:
    """Lower the volume of the song rather than a sharp cutoff.

    Args:
        mixer (pygame.mixer):           To pygame mixer used to lower the
                                        volume.
        cutoff_value (float, optional): The value used to decrease the volume
                                        after each loop. Defaults to 0.05.
        cutoff_speed (float, optional): The speed used to decrease the volume.
                                        Defaults to 0.2.
    """

    volume_value = 1

    while volume_value > 0:

        volume_value -= cutoff_value
        mixer.music.set_volume(volume_value)
        time.sleep(cutoff_speed)

def song_process(rx_queue: Queue, song_name_queue: Queue,
                 play_state_queue: Queue, song_name: str, loop: bool,
                 timer_length: int, random: bool) -> None:
    """Main process for the audio player.

    Play songs and take care of the various settings set by the user such as
    loop, random, timer cutoff, etc. By default it will play each song in the
    Songs/ directory in order.

    Args:
        rx_queue (Queue):         Queue used by the main process to update
                                  settings of the audio player.
        song_name_queue (Queue):  Queue used by the audio player process to
                                  tell the main process which song is currently
                                  playing.
        play_state_queue (Queue): Queue used by the audio player process to
                                  tell the main process if it stopped playing.
        song_name (str):          The name of the song to play selected by the
                                  user in the web browser.
        loop (bool):              Used to loop or not over all the songs.
        timer_length (int):       Used to cutoff the audio player after a
                                  certain amount of time.
        random (bool):            Used to play songs in a random order or not.
    """

    terminate = False  # Stop the audio player.
    timer_count = 0  # Used to keep track of the time spent playing songs.
    sleep_time = 0.1  # Sleep value inside the loop when the player is playing.
    songs_played = 0  # Used only when random = True.
    new_song = None  # The new song to play.
    user_stop = False

    songs = sorted(os.listdir('Songs/'))  # Sorted songs in 'Songs/'.
    songs_len = len(songs) - 1

    # Initialise Pygame mixer, store it and load the specified song.
    pygame.mixer.init()
    mixer = pygame.mixer
    mixer.music.load('Songs/' + song_name)

    # If rx_queue is not empty we make sure to clear it before.
    while not rx_queue.empty():
        rx_queue.get()

    # Main player loop.
    while True and not terminate:

        # We update the current song name and play it.
        update_queue(song_name_queue, song_name)
        mixer.music.play()

        # Loop while the music is playing. We check if settings have been
        # changed, or if we should stop playing (user stop or cutoff timer).
        while mixer.music.get_busy():

            if not rx_queue.empty():

                dict = rx_queue.get()
                loop = dict.get('loop', loop)
                random = dict.get('random', random)
                timer_length = dict.get('timer', timer_length)
                terminate = dict.get('terminate', terminate)

                if terminate:
                    user_stop = True
                    lower_song_volume(mixer, 0.15, 0.1)
                    break

            time.sleep(sleep_time)
            timer_count += 1

            if timer_length and (timer_count * sleep_time) == timer_length:
                lower_song_volume(mixer)
                terminate = True
                break

        if terminate:
            break

        # If random not selected we play the song in order.
        elif not random:

            for i, song in enumerate(songs):
                if song == song_name and i < songs_len:
                    new_song = songs[i + 1]
                    mixer.music.load('Songs/' + new_song)
                    song_name = new_song
                    break
                elif i == songs_len and loop:
                    new_song = songs[0]
                    mixer.music.load('Songs/' + new_song)
                    song_name = new_song
                    break
                elif i == songs_len and not loop:
                    terminate = True
                    break

        # If random selected we choose random songs
        # (but not the same twice in a row).
        elif random:

            # If loop is not selected we terminate the player when we have
            # played all songs.
            if not loop and songs_played == songs_len:
                break

            if songs_len > 0:
                while True:
                    new_song = songs[randint(0, songs_len)]
                    if new_song != song_name:
                        break
            else:
                new_song = songs[randint(0, songs_len)]

            mixer.music.load('Songs/' + new_song)
            song_name = new_song

            if not loop:
                # Used to keep track of how many song we have played
                songs_played += 1

    # We tell the main process the audio player has stopped.
    if not user_stop:
        update_queue(play_state_queue, False)
        update_queue(song_name_queue, 'Nothing.')

class Player:
    """Class used to control the audio player process.

    Spawn the process playing songs and sending it settings set by the user in
    AirJukeBox's web page.
    """

    def __init__(self) -> None:

        self.process = None
        self.tx_queue = Queue()
        self.song_name_queue = Queue(maxsize=1)
        self.play_state_queue = Queue(maxsize=1)

    def play_song(self, song_name: str, loop: bool, timer_time: str,
                  istimer: bool, random: bool) -> None:
        """Spawn the audio player process

        It will first check if the audio player process is alive, if yes, it
        will send a command to terminate it.

        Args:
            song_name (str):  Song to play selected by the user.
            loop (bool):      If the loop setting is activated or not.
            timer_time (str): The time used by the cutoff timer.
            istimer (bool):   If the timer setting is activated or not.
            random (bool):    If the random setting is activated or not.
        """

        if self.process and self.process.is_alive():
            self.tx_queue.put({"terminate": True})

        # If timer is activated or timer time setting is 0 we don't activate
        # the timer. Else we convert the time value from minutes to seconds.
        if not istimer or int(timer_time) == 0:
            timer_time = None
        else:
            timer_time *= 60

        self.process = Process(target=song_process,
                               args=(self.tx_queue, self.song_name_queue,
                                     self.play_state_queue, song_name, loop,
                                     timer_time, random))

        self.process.start()

    def stop_song(self) -> None:
        """Send a terminate command to the audio player process by putting it
        in the tx queue.
        """

        if self.process and self.process.is_alive():
            self.tx_queue.put({"terminate": True})

    def change_loop(self, loop: bool) -> None:
        """Update the loop setting in the audio player process by sending it
        the new value in the tx queue.

        Args:
            loop (bool): If the loop setting is activated or not.
        """

        if self.process and self.process.is_alive():
            self.tx_queue.put({"loop": loop})

    def change_random(self, random: bool) -> None:
        """Update the random setting in the audio player process by sending it
        the new value in the tx queue.

        Args:
            random (bool): If the random setting is activated or not.
        """

        if self.process and self.process.is_alive():
            self.tx_queue.put({"random": random})

    def change_timer(self, istimer: bool, timer_time: str) -> None:
        """Update the timer setting in the audio player process by sending it
        the new value in the tx queue.

        Args:
            istimer (bool):   If the timer setting is activated or not.
            timer_time (str): The time selected for the timer cutoff.
        """

        if self.process and self.process.is_alive():
            if istimer and int(timer_time) > 0:
                self.tx_queue.put({"timer": int(timer_time) * 60})
            elif not istimer or int(timer_time) == 0:
                self.tx_queue.put({"timer": None})


from django.contrib import admin
from .models import Vendor, VendorMenu, UserRating

# Register your models here.

class VendorAdmin(admin.ModelAdmin):

    model = Vendor
    list_display = ('id', 'name')

class VendorMenuAdmin(admin.ModelAdmin):

    model = VendorMenu
    list_display = ('item_id', 'vendor_id', 'item_name', 'price', 'description')

class UserRatingAdmin(admin.ModelAdmin):

    model = UserRating
    list_display = ('md5', 'vendor_id', 'rating')

admin.site.register(Vendor, VendorAdmin)
admin.site.register(VendorMenu, VendorMenuAdmin)
admin.site.register(UserRating, UserRatingAdmin)


import torch
import PIL.Image as Image
import requests

# from transformers import CLIPProcessor, CLIPModel

# processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")

feature_dim = 512
# def define_model(device='cuda'):
#     model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32").to(device)
#     return model

import clip

def define_model(device='cuda'):
    model, preprocess = clip.load("ViT-B/16", device=device)
    model = model.eval()
    return model, preprocess

# def get_image_features(model, img_tensor):
#     inputs = processor(images=img_tensor, return_tensors="pt")
#     image_features = model.get_image_features(**inputs)
#     return image_features

def get_image_features(model, img_tensor):
    with torch.no_grad():
        image_features = model.encode_image(img_tensor)
    return image_features.float()

if __name__ == '__main__':
    url = "http://images.cocodataset.org/val2017/000000039769.jpg"
    image = Image.open(requests.get(url, stream=True).raw)
    print(image.size)
    features = get_image_features(image)

    print(features.shape)


'''
Created on 21 may. 2020

@author: julio
'''
import django
django.setup()

from fgserver.atc.controllers import Requests, process_request

def process_queued_request(instance):
    print("request received",instance)
    process_request(instance,instance)

Requests.listen(process_queued_request)


import sys
import configparser
from distutils.util import strtobool
import json
import pandas as pd
import numpy as np
from tabulate import tabulate
import ray
import time

# read csv files containing production and curtailment data into a dataframe
def import_data(production, curtailment):

    production = pd.read_csv(production,
        names=['date','hour','interval','demand','solar','wind',
        'net_load','renewables','nuclear','large_hydro',
        'imports','generation','thermal','load_less'])

    curtailment = pd.read_csv(curtailment,
        names=['date','hour','interval','wind_curtailment','solar_curtailment'],
        index_col=[0,1,2])

    data = production.join(curtailment,
        on=['date', 'hour', 'interval'],
        how='left')

    data.fillna(0, inplace=True)

    data['wind'] += data['wind_curtailment']
    data['solar'] += data['solar_curtailment']

    #print(tabulate(data.head(20), headers='keys', tablefmt='psql'))

    return data

# read the provided JSON configuration file and generate all combinations of inputs
def import_config(config_file_name):

    with open(config_file_name, 'r') as config_file:
        config = json.load(config_file)

    data = import_data(config['data']['production'], config['data']['curtailment'])

    configs = pd.DataFrame(
        columns=['battery','initial_soc','min_soc','max_soc',
        'nuclear','solar','wind'])

    wind_same_as_solar = config['wind']['wind_same_as_solar']
    nuclear_samples = config['nuclear']['nuclear_samples']
    solar_samples = config['solar']['solar_samples']
    battery_samples = config['battery']['battery_samples']

    if wind_same_as_solar:
        wind_samples = 1
    else:
        wind_samples = config['wind']['wind_samples']

    configs['nuclear'] = np.repeat(
        np.linspace(
            config['nuclear']['nuclear_min'],
            config['nuclear']['nuclear_max'],
            nuclear_samples
        ),
        solar_samples * wind_samples * battery_samples
    )

    configs['solar'] = np.tile(
        np.repeat(
            np.linspace(
                config['solar']['solar_min'],
                config['solar']['solar_max'],
                solar_samples
            ),
            wind_samples * battery_samples
        ),
        nuclear_samples
    )

    if wind_same_as_solar:
        configs['wind'] = configs['solar']
    else:
        configs['wind'] = np.tile(
            np.repeat(
                np.linspace(
                    config['wind']['wind_min'],
                    config['wind']['wind_max'],
                    wind_samples
                ),
                battery_samples
            ),
            nuclear_samples * solar_samples
        )

    configs['battery'] = np.tile(
        np.linspace(
            config['battery']['battery_min'],
            config['battery']['battery_max'],
            battery_samples
        ),
        nuclear_samples * solar_samples * wind_samples
    )

    configs['min_soc'] = config['battery']['min_soc']
    configs['max_soc'] = config['battery']['max_soc']
    configs['initial_soc'] = config['battery']['initial_soc']
    configs['wind_same_as_solar'] = config['wind']['wind_same_as_solar']
    configs['export_intermediate'] = config['data']['export_intermediate']
    configs['time_factor'] = config['data']['time_factor']

    # print(tabulate(configs.head(20), headers='keys', tablefmt='psql'))

    config_list = configs.to_dict('records')

    return (config, config_list, data)

# simulate the sources of electricity on the grid at every data point
@ray.remote
def simulate(config, data):

    output = data.copy()

    # these operations can be done without loops
    output['solar'] *= config['solar']
    output['wind'] *= config['wind']
    output['nuclear'] = config['nuclear']
    output['clean'] = output['nuclear'] + output['solar'] + output['wind']
    output['net'] = output['clean'] - output['demand']

    current_value = config['battery'] * config['initial_soc']
    min_value = config['battery'] * config['min_soc']
    max_value = config['battery'] * config['max_soc']

    samples = len(output)
    stored = [0.0] * samples
    battery = [0.0] * samples
    gas = [0.0] * samples
    curtailed = [0.0] * samples
    soc = [0.0] * samples
    net = output['net'].array

    time_factor = config['time_factor']

    # a loop is required because the core simulation depends on the previous row
    for i in range(samples):
        temp = current_value + net[i] / time_factor

        if temp < min_value:
            stored[i] = min_value
            battery[i] = (current_value - min_value) * time_factor
            gas[i] = (min_value - current_value) * time_factor - net[i]
            curtailed[i] = 0.0
        elif temp > max_value:
            stored[i] = max_value
            battery[i] = (current_value - max_value) * time_factor
            gas[i] = 0.0
            curtailed[i] = net[i] - (max_value - current_value) * time_factor
        else:
            stored[i] = current_value + net[i] / time_factor
            battery[i] = -net[i]
            gas[i] = 0.0
            curtailed[i] = 0.0

        if config['battery'] != 0:
            soc[i] = stored[i] / config['battery']
        else:
            soc[i] = 0.0

        current_value = stored[i]

    # calculate totals
    output['gas'] = pd.Series(gas)
    output['curtailed'] = pd.Series(curtailed)
    output['stored'] = pd.Series(stored)
    output['battery'] = pd.Series(battery)
    output['soc'] = pd.Series(soc)

    total_demand = sum(output['demand']) / time_factor
    total_clean = sum(output['clean']) / time_factor
    total_gas = sum(output['gas']) / time_factor
    total_curtailed = sum(output['curtailed']) / time_factor

    if not config['export_intermediate']:
        output = None

    totals = {
        'clean': (1.0 - total_gas / total_demand) * 100,
        'curtailed': (total_curtailed / total_demand) * 100,
        'demand_value': total_demand,
        'clean_value': total_clean,
        'gas_value': total_gas,
        'curtailed_value': total_curtailed
    }

    # result = {
    #     'config': config,
    #     'output': output,
    #     'totals': totals
    # }

    # this dictionary will be used as a row in a new dataframe
    result_flat = config.copy()
    result_flat.update(totals)

    # print(json.dumps(result_flat))

    return result_flat

def generate_plot_data(config, result_list):

    results = pd.DataFrame(result_list)

    # determine how many constants there should be
    constants = 3
    if config['graph']['x2'] != 'none':
        constants -= 1
    if config['wind']['wind_same_as_solar']:
        constants -= 1

    # filter the data by the constants
    # this shouldn't do anything with a proper configuration
    if constants == 1:
        subset = results.groupby(
            results[config['graph']['c1']]
        ).get_group(
            config['graph']['v1']
        )
    elif constants == 2:
        subset = results.groupby(
            [results[config['graph']['c1']],results[config['graph']['c2']]]
        ).get_group(
            (config['graph']['v1'],config['graph']['v2'])
        )
    elif constants == 3:
        subset = results.groupby(
            [results[config['graph']['c1']],results[config['graph']['c2']],results[config['graph']['c3']]]
        ).get_group(
            (config['graph']['v1'],config['graph']['v2'],config['graph']['v3'])
        )

    # generate the plot data objects for plot.ly
    traces = []
    if config['graph']['x2'] == 'none':
        traces.append({
            'x': subset[config['graph']['x1']].tolist(),
            'y': subset[config['graph']['y']].tolist(),
            'type': 'scatter',
            'name': config['graph']['x1'],
        })
    else:
        for name, group in subset.groupby(config['graph']['x2']):
            traces.append({
                'x': group[config['graph']['x1']].tolist(),
                'y': group[config['graph']['y']].tolist(),
                'type': 'scatter',
                'name': config['graph']['x2'] + " = " + str(name),
            })

    # label the axes for plot.ly
    layout = {
        'title': config['graph']['y'] + " vs " + config['graph']['x1'],
        'xaxis': { 'title': config['graph']['x1'] },
        'yaxis': { 'title': config['graph']['y'] }
    }

    plot_data = {
        'traces': traces,
        'layout': layout
    }

    return plot_data

# runs the simulations specified in the config list on the ray cluster
def simulate_distributed(config_list, data):

    data_id = ray.put(data)

    result_ids = []
    for config in config_list:
        result_ids.append(simulate.remote(config, data_id))

    result_list = []
    for result_flat in ray.get(result_ids):
        result_list.append(result_flat)

    return result_list

if __name__ == '__main__':

    # run the simulation locally or on a ray cluster if specified
    argc = len(sys.argv)
    if argc == 5:
        ray.init(address=sys.argv[4])
    elif argc == 4:
        ray.init()
    else:
        print("Usage: python simulate.py <config> <output> <plot> [address]")
        sys.exit()

    print("Generating configs...", end='', flush=True)
    start = time.time()
    config, config_list, data = import_config(sys.argv[1])
    print("Done! ({:.2f}s)".format(time.time()-start))

    print("Running " + str(len(config_list)) + " simuations...", end='', flush=True)
    start = time.time()
    result_list = simulate_distributed(config_list, data)
    print("Done! ({:.2f}s)".format(time.time()-start))

    print("Generating plot data...", end='', flush=True)
    start = time.time()
    plot_data = generate_plot_data(config, result_list)
    print("Done! ({:.2f}s)".format(time.time()-start))

    print("Writing files...", end='', flush=True)
    start = time.time()
    with open(sys.argv[2], 'w') as output_file:
        json.dump(result_list, output_file, indent=4)
    with open(sys.argv[3], 'w') as output_file:
        json.dump(plot_data, output_file, indent=4)
    print("Done! ({:.2f}s)".format(time.time()-start))


import os
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://the-internet.herokuapp.com/upload")

input = driver.find_element(By.XPATH, "//input[@id = 'file-upload']")
file = r"C:\Users\ADMIN\Desktop\Capture9.PNG"
input.send_keys(file)
driver.find_element(By.XPATH, "//input[@id = 'file-submit']").click()
driver.implicitly_wait(10)
uploaded_file_name = driver.find_element(By.XPATH, '//*[@id="uploaded-files"]').text
print(uploaded_file_name)
expected_file_name = os.path.basename(r"C:\Users\ADMIN\Desktop\Capture9.PNG")
print(expected_file_name)
if uploaded_file_name == expected_file_name:
    print(True)
else:
    print(False)


from ewah.dwhooks.base_dwhook import EWAHBaseDWHook
from ewah.constants import EWAHConstants as EC

import os
import csv
import snowflake.connector
from tempfile import NamedTemporaryFile
from airflow.utils.file import TemporaryDirectory
from airflow.models import BaseOperator

class SnowflakeOperator(BaseOperator):
    "Operate to execute SQL on Snowflake"

    def __init__(
        self,
        sql,
        snowflake_conn_id,
        database,
        params=None,
    *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.sql = sql
        self.conn_id = snowflake_conn_id
        self.database = database
        self.params = params

    def execute(self, context):
        hook = EWAHDWHookSnowflake(
            conn_id=self.conn_id,
            database=self.database,
        )
        self.log.info('execute: '+self.sql)
        hook.execute(self.sql, commit=True, params=self.params)
        hook.close()

class EWAHDWHookSnowflake(EWAHBaseDWHook):

    _QUERY_SCHEMA_CHANGES_COLUMNS = """
        SELECT
            column_name
        FROM "{database_name}".information_schema.columns
        WHERE table_catalog = %(database_name)s
            AND table_schema = %(schema_name)s
            AND table_name = %(table_name)s
        ORDER BY ordinal_position ASC;
    """
    _QUERY_SCHEMA_CHANGES_DROP_COLUMN = """
        ALTER TABLE "{database_name}"."{schema_name}"."{table_name}"
        DROP COLUMN IF EXISTS "{column_name}" CASCADE;
    """
    _QUERY_SCHEMA_CHANGES_ADD_COLUMN = """
        ALTER TABLE "{database_name}"."{schema_name}"."{table_name}"
        ADD COLUMN "{column_name}" {column_type};
    """
    _QUERY_TABLE = """
        SELECT * FROM "{database_name}"."{schema_name}"."{table_name}"
    """

    def __init__(self, *args, database=None, **kwargs):
        self.database = database
        super().__init__(EC.DWH_ENGINE_SNOWFLAKE, *args, **kwargs)

    def _create_conn(self, database=None):
        creds = self.credentials
        extra = creds.extra_dejson
        extra['password'] = creds.password
        if creds.host:
            extra['account'] = creds.host
        if creds.login:
            extra['user'] = creds.login
        if database or self.database:
            extra['database'] = database or self.database

        return snowflake.connector.connect(**extra)

    def _create_or_update_table(
        self,
        data,
        table_name,
        schema_name,
        database_name,
        columns_definition,
        columns_partial_query,
        update_on_columns,
        drop_and_replace,
        logging_function,
    ):
        logging_function('Preparing DWH Tables...')
        new_table_name = table_name + '_new'
        self.execute(
            sql="""CREATE OR REPLACE TABLE
                    "{database_name}"."{schema_name}"."{table_name}"
                    ({columns});
            """.format(**{
                'database_name': database_name,
                'schema_name': schema_name,
                'table_name': new_table_name,
                'columns': columns_partial_query,
            }),
            commit=False,
        )

        list_of_columns = self.execute_and_return_result(
            sql="""SELECT
                column_name
                FROM "{0}".information_schema.columns
                WHERE table_catalog = '{0}'
                AND table_schema = '{1}'
                AND table_name = '{2}'
                ORDER BY ordinal_position ASC
            """.format(
                database_name,
                schema_name,
                new_table_name,
            ),
        )
        list_of_columns = [col[0].strip() for col in list_of_columns]

        self.log.info('Writing data to a temporary .csv file')
        with TemporaryDirectory(prefix='uploadtosnowflake') as tmp_dir:
            with NamedTemporaryFile(
                dir=tmp_dir,
                prefix=new_table_name,
                suffix='.csv',
            ) as datafile:
                file_name = os.path.abspath(datafile.name)
                with open(file_name, mode='w') as csv_file:
                    csvwriter = csv.writer(
                        csv_file,
                        delimiter=',',
                        quotechar='"',
                        quoting=csv.QUOTE_MINIMAL,
                    )

                    for _ in range(len(data)):
                        datum = data.pop(0)
                        # Make sure order of csv is the same as order of columns
                        csvwriter.writerow(
                            [datum.get(col) for col in list_of_columns],
                        )

                # now stage and copy into snowflake!
                sql_upload = '''
                    USE DATABASE "{2}";
                    USE SCHEMA "{3}";
                    DROP FILE FORMAT IF EXISTS {1}_format;
                    CREATE OR REPLACE FILE FORMAT {1}_format
                        type = 'CSV'
                        field_delimiter = ','
                        field_optionally_enclosed_by = '"'
                    ;
                    DROP STAGE IF EXISTS {1}_stage;
                    CREATE OR REPLACE STAGE {1}_stage
                        file_format = {1}_format;
                    PUT file://{0} @{1}_stage;
                    COPY INTO "{2}"."{3}"."{1}" FROM '@{1}_stage/{4}.gz';
                '''.format(
                    file_name,
                    new_table_name,
                    database_name,
                    schema_name,
                    file_name[file_name.rfind('/')+1:],
                )
                self.log.info('Uploading data to Snowflake...')
                self.execute(sql_upload)

        if drop_and_replace or (not self.test_if_table_exists(
            table_name=table_name,
            schema_name=schema_name,
            database_name=database_name,
        )):
            sql_final = '''
                USE SCHEMA "{0}"."{1}";
                DROP TABLE IF EXISTS "{0}"."{1}"."{2}" CASCADE;
                ALTER TABLE "{0}"."{1}"."{3}" RENAME TO "{2}";
            '''.format(
                database_name,
                schema_name,
                table_name,
                new_table_name,
            )
        else:
            update_set_cols = []
            for col in columns_definition.keys():
                if not (col in update_on_columns):
                    update_set_cols += [col]

            sql_final = '''
                USE SCHEMA "{0}"."{1}";
                MERGE INTO "{0}"."{1}"."{2}" AS a
                    USING "{0}"."{1}"."{3}" AS b
                    ON {4}
                    WHEN MATCHED THEN UPDATE
                    SET {5}
                    WHEN NOT MATCHED THEN INSERT
                    ({6}) VALUES ({7})
                    ;
                DROP TABLE "{0}"."{1}"."{3}" CASCADE;
            '''.format(
                database_name,
                schema_name,
                table_name,
                new_table_name,
                ' AND '.join([
                    'a."{0}" = b."{0}"'.format(col) for col in update_on_columns
                ]),
                ', '.join([
                    'a."{0}" = b."{0}"'.format(col) for col in update_set_cols
                ]),
                '"'+'", "'.join(list(columns_definition.keys()))+'"',
                ', '.join([
                    'b."{0}"'.format(col) \
                    for col in list(columns_definition.keys())
                ]),
            )

        self.log.info('Final Step: Merging data')
        self.execute(sql_final)

    def commit(self):
        self.cur.execute('COMMIT;')
        self.cur.execute('BEGIN;')

    def rollback(self):
        self.cur.execute('ROLLBACK;')
        self.cur.execute('BEGIN;')

    def test_if_table_exists(
        self,
        table_name,
        schema_name,
        database_name=None,
    ):
        self.execute('USE DATABASE {0}'.format(
            database_name or self.database_name,
        ))
        return 0 < len(self.execute_and_return_result(
            """
            SELECT * FROM "{0}".information_schema.tables
            WHERE table_schema LIKE '{1}'
            AND table_name LIKE '{2}'
            """.format(
                database_name or self.database_name,
                schema_name,
                table_name,
            )
        ))


from __future__ import unicode_literals, absolute_import

import json
import unittest
import shutil
import sys
import tempfile
import time
import os

import pygit2
from mock import patch

sys.path.insert(
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
)

import pagure.lib.query
import tests

from pagure.config import config as pagure_config, reload_config

class PagureFlaskDumpLoadTicketTests(tests.Modeltests):
    """Tests for flask application for dumping and re-loading the JSON of
    a ticket.
    """

    @patch("pagure.lib.notify.send_email")
    @patch("pagure.lib.git._maybe_wait")
    def test_dumping_reloading_ticket(self, mw, send_email):
        """Test dumping a ticket into a JSON blob."""
        mw.side_effect = lambda result: result.get()
        send_email.return_value = True

        tests.create_projects(self.session)

        # Create repo
        self.gitrepo = os.path.join(self.path, "repos", "tickets", "test.git")
        repopath = os.path.join(self.path, "repos", "tickets")
        os.makedirs(self.gitrepo)
        repo_obj = pygit2.init_repository(self.gitrepo, bare=True)

        repo = pagure.lib.query.get_authorized_project(self.session, "test")
        # Create an issue to play with
        msg = pagure.lib.query.new_issue(
            session=self.session,
            repo=repo,
            title="Test issue",
            content="We should work on this",
            user="pingou",
        )
        self.assertEqual(msg.title, "Test issue")

        # Need another two issue to test the dependencie chain
        msg = pagure.lib.query.new_issue(
            session=self.session,
            repo=repo,
            title="Test issue #2",
            content="Another bug",
            user="pingou",
        )
        self.assertEqual(msg.title, "Test issue #2")
        msg = pagure.lib.query.new_issue(
            session=self.session,
            repo=repo,
            title="Test issue #3",
            content="That would be nice feature no?",
            user="foo",
        )
        self.assertEqual(msg.title, "Test issue #3")

        issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
        issue2 = pagure.lib.query.search_issues(self.session, repo, issueid=2)
        issue3 = pagure.lib.query.search_issues(self.session, repo, issueid=3)

        # Add a couple of comment on the ticket
        msg = pagure.lib.query.add_issue_comment(
            session=self.session,
            issue=issue,
            comment="Hey look a comment!",
            user="foo",
        )
        self.session.commit()
        self.assertEqual(msg, "Comment added")
        msg = pagure.lib.query.add_issue_comment(
            session=self.session,
            issue=issue,
            comment="crazy right?",
            user="pingou",
        )
        self.session.commit()
        self.assertEqual(msg, "Comment added")
        # Assign the ticket to someone
        msg = pagure.lib.query.add_issue_assignee(
            session=self.session, issue=issue, assignee="pingou", user="pingou"
        )
        self.session.commit()
        self.assertEqual(msg, "Issue assigned to pingou")
        # Add a couple of tags on the ticket
        msg = pagure.lib.query.add_tag_obj(
            session=self.session,
            obj=issue,
            tags=[" feature ", "future "],
            user="pingou",
        )
        self.session.commit()
        self.assertEqual(msg, "Issue tagged with: feature, future")
        # Add dependencies
        msg = pagure.lib.query.add_issue_dependency(
            session=self.session,
            issue=issue,
            issue_blocked=issue2,
            user="pingou",
        )
        self.session.commit()
        self.assertEqual(msg, "Issue marked as depending on: #2")
        msg = pagure.lib.query.add_issue_dependency(
            session=self.session, issue=issue3, issue_blocked=issue, user="foo"
        )
        self.session.commit()
        self.assertEqual(msg, "Issue marked as depending on: #1")

        # Dump the JSON
        pagure.lib.git.update_git(issue, repo).wait()
        repo = pygit2.Repository(self.gitrepo)
        cnt = len(
            [
                commit
                for commit in repo.walk(
                    repo.head.target, pygit2.GIT_SORT_TOPOLOGICAL
                )
            ]
        )
        self.assertIn(cnt, (9, 10, 11))

        last_commit = repo.revparse_single("HEAD")
        patch = pagure.lib.git.commit_to_patch(repo, last_commit)
        for line in patch.split("\n"):
            if line.startswith("--- a/"):
                fileid = line.split("--- a/")[1]
                break

        newpath = tempfile.mkdtemp(prefix="pagure-dump-load")
        clone_repo = pygit2.clone_repository(self.gitrepo, newpath)

        self.assertEqual(len(os.listdir(newpath)), 4)

        ticket_json = os.path.join(self.path, "test_ticket.json")
        self.assertFalse(os.path.exists(ticket_json))
        shutil.copyfile(os.path.join(newpath, fileid), ticket_json)
        self.assertTrue(os.path.exists(ticket_json))
        jsondata = None
        with open(ticket_json) as stream:
            jsondata = json.load(stream)
        self.assertNotEqual(jsondata, None)

        shutil.rmtree(newpath)

        # Test reloading the JSON

        # Re-create the DB from scratch
        self.session.rollback()
        self._clear_database()
        self.db_session.close()
        del self.session
        del self.db_session

        os.unlink(os.path.join(self.dbfolder, "db.sqlite"))

        self.db_session = pagure.lib.model.create_tables(
            self.dbpath,
            acls=pagure_config.get("ACLS", {}),
        )
        self._prepare_db()
        tests.create_projects(self.session)

        # Create repo
        self.gitrepo = os.path.join(self.path, "tickets", "test.git")
        repopath = os.path.join(self.path, "tickets")
        os.makedirs(self.gitrepo)
        pygit2.init_repository(self.gitrepo, bare=True)

        pagure.lib.git.update_ticket_from_git(
            self.session,
            reponame="test",
            namespace=None,
            username=None,
            issue_uid="foobar",
            json_data=jsondata,
            agent="pingou",
        )

        # Post loading
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
        self.assertEqual(len(repo.issues), 1)
        issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)

        # Check after re-loading
        self.assertEqual(len(issue.comments), 3)
        self.assertEqual(len(issue.tags), 2)
        self.assertEqual(
            sorted(issue.tags_text), sorted(["future", "feature"])
        )
        self.assertEqual(issue.assignee.username, "pingou")
        self.assertEqual(issue.children, [])
        self.assertEqual(issue.parents, [])
        self.assertEqual(issue.status, "Open")

if __name__ == "__main__":
    unittest.main(verbosity=2)