import requests
import re
import igraph
import cairocffi

import unicodedata
from unidecode import unidecode

from bs4 import BeautifulSoup

def get_article_links(url):

    profile_url = url

    r = requests.get(profile_url)

    soup_profile = BeautifulSoup(r.content, "html.parser")

    print(soup_profile)

    data = soup_profile.find_all("a", {"class": "gsc_a_at"})

    article_links = []

    print(data)

    for article_info in data:

        strings = str(article_info).split()
        link_chunk = strings[2]

        link = re.findall(r'\"(.+?)\"', link_chunk)
        article_links.append(link)

    return article_links

def find_coauthors(links):

    coauthors = []

    for link in links:

        link_string = str(link)
        link_string = link_string.replace("[", "")
        link_string = link_string.replace("]", "")
        link_string = link_string.replace("'", "")
        link_string = link_string.replace("amp;", "")

        if link_string == "/citations?view_op=view_citation&hl=en&oe=ASCII&user=1UL3-ocAAAAJ&citation_for_view=1UL3-ocAAAAJ:2osOgNQ5qMEC":
            continue

        print("https://scholar.google.com.hk" + link_string)
        r = requests.get("https://scholar.google.com.hk" + link_string)

        soup_article = BeautifulSoup(r.content, "html.parser")

        author_data = soup_article.find_all("div", {"class": "gsc_value"})

        authors = author_data[0]
        author_string = authors.text

        authors = author_string.split(', ')

        title_data = soup_article.find_all("a", {"class": "gsc_title_link"})

        paper = Paper(title_data[0].text)

        for author in authors:  # Iterate through author strings just found on page

            if len(coauthors) == 0:
                coauthor_new = Coauthor(paper, author)
                coauthors.append(coauthor_new)
                continue

            match_found = False
            for coauthor in coauthors:  # Iterate through the existing coauthor objects

                if coauthor.is_a_match(author):  # If this coauthor already exists
                    coauthor.add_paper(paper)  # Add this paper to their "resume"
                    match_found = True
                    break

            if not match_found:  # If the coauthor was never found
                coauthor_new = Coauthor(paper, author)
                coauthors.append(coauthor_new)

        print(authors)

    print(len(coauthors))

    file = open("coauthors-edge-list.txt", "w")

    for coauthor in coauthors:
        print(coauthor.to_string())
        name = str(coauthor.to_string())

        names = name.split()
        name = "".join(names)

        for paper in coauthor.papers:

            title = str(paper.title)
            titles = title.split()
            title = "".join(titles)

            file.write("%s %s\n" % (name, title))

            print("~~%s", paper.title)

    file.close()

    return coauthors

def create_graph():

    graph: igraph.Graph = igraph.Graph.Read_Ncol("new_list.txt", directed=False)
    graph: igraph.Graph = igraph.Graph.simplify(graph)

    graph.vs["color"] = "#75546E"
    vert = graph.vs.find(name="MichelleReneLowry")
    vert["color"] = "#A3809C"
    vert["label"] = "Me"
    vert["size"] = 40
    vert["label_size"] = 18

    layout = graph.layout("fr")

    igraph.plot(graph, "finished.png", layout=layout, bbox=(1200, 600))

def create_new_list(coauthors):

    file = open("new_list.txt", "w")

    for coauthor in coauthors:

        coauthor1: Coauthor = coauthor
        for coauthor_2 in coauthors:

            coauthor2: Coauthor = coauthor_2

            if coauthor1 != coauthor2:

                for paper in coauthor1.papers:

                    for paper_2 in coauthor2.papers:

                        if paper.title == paper_2.title:
                            names_1 = coauthor1.to_string().split()
                            string_1 = "".join(names_1)

                            names_2 = coauthor2.to_string().split()
                            string_2 = "".join(names_2)

                            file.write(string_1 + " " + string_2 + "\n")

    file.write("PaulBenjaminLowry VernRichardson\n")
    file.write("MichelleReneLowry VernRichardson\n")

class Coauthor(object):

    def __init__(self, paper, name):
        self.papers = [paper]

        name_new = unidecode(name)
        if find_alternate_name(name_new) == "Already Initials":  # If the parameter was initials
            self.initials = name_new
            self.full_name = None
        else:  # If the parameter was a full name
            self.full_name = name_new
            self.initials = None

    def add_paper(self, paper):
        self.papers.append(paper)

    def is_a_match(self, string):

        output = find_alternate_name(string)

        string_s = string

        if len(output) == 2:  # If a new name was found
            alternate = output[0]
            string_s = output[1]
        else:  # If just the alternate was returned
            alternate = output

        string_new = unidecode(string_s)

        # Array of string to check for middle name difference
        name_strings = string_new.split()
        if self.full_name is not None:
            full_name_strings = self.full_name.split()
            if name_strings[0] == full_name_strings[0] and name_strings[-1] == full_name_strings[-1]: # If the first and last names match, then consider it a match
                return True

        if string_new == self.full_name:
            return True
        elif string_new == self.initials:
            return True
        elif alternate == "Already Initials":  # If the name parameter is initials
            if self.full_name is not None:
                if find_alternate_name(self.full_name) == string_new:
                    self.initials = string_new  # Set the initials
                    return True  # The initials match the name
                else:
                    return False
            else:  # If the object only has initials
                if alternate == self.initials:
                    return True
                else:
                    return False
        else:  # If the name parameter was a full name
            if self.initials is not None:
                if alternate == self.initials:
                    self.full_name = string_new  # Set the full name
                    return True
                else:
                    return False
            else:  # If the object only contains a full name
                if string_new == self.full_name:
                    return True
                else:
                    return False

    def to_string(self):
        if self.full_name is None:
            return self.initials
        elif self.initials is None:
            return self.full_name
        else:
            return self.full_name

class Paper(object):

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

def find_alternate_name(name):

    name_chunks = str(name).split()

    first_name = name_chunks[0]

    if first_name == first_name.upper() and len(first_name) < 4:  # If the name is already capitalized (i.e. Initials)
        return "Already Initials"
    else:

        altered_name = None

        if len(first_name) > 3 and first_name == first_name.upper():

            # Properly capitalize the name
            first_letter = first_name[0:1]
            other_letters = first_name[1:len(first_name)]

            first_name = first_letter + other_letters.lower()
            name_chunks[0] = first_name

            altered_name = " ".join(name_chunks)

        if altered_name is None:
            return find_initials(name_chunks)
        else:
            return [find_initials(name_chunks), altered_name]

def find_initials(name_list):

    initial_string = ""

    for name in name_list:

        if name == name_list[-1]:  # If it is the last name
            initial_string += " "
            initial_string += name
        else:
            initial_string += str(name)[0].capitalize()

    return initial_string

links = get_article_links("https://scholar.google.com.hk/citations?hl=en&user=1UL3-ocAAAAJ")
coauthors_list = find_coauthors(links)

create_new_list(coauthors_list)
create_graph()


import random
from contextlib import suppress

import arrow
import discord
from discord.ext import commands, tasks

from bot.utils.setup import update_fishing_prices
from bot.villager_bot import VillagerBotCluster

class Loops(commands.Cog):
    def __init__(self, bot: VillagerBotCluster):
        self.bot = bot

        self.aiohttp = bot.aiohttp
        self.d = bot.d

        self.clear_rcon_cache.start()
        self.change_status.start()
        self.update_fishing_prices.start()

    def cog_unload(self):
        self.clear_rcon_cache.cancel()
        self.change_status.cancel()
        self.update_fishing_prices.cancel()

    @tasks.loop(minutes=45)
    async def change_status(self):
        await self.bot.wait_until_ready()
        await self.bot.change_presence(
            status=discord.Status.online,
            activity=discord.Game(name=random.choice(self.d.playing_list)),
        )

    @tasks.loop(seconds=30)
    async def clear_rcon_cache(self):
        """clear old connections from the rcon cache"""

        for key, connection in self.bot.rcon_cache.copy().items():
            if arrow.utcnow().shift(minutes=-1) > connection[1]:
                with suppress(Exception):
                    await connection[0].close()

                self.bot.rcon_cache.pop(key, None)

    @tasks.loop(hours=24)
    async def update_fishing_prices(self):
        update_fishing_prices(self.d)

async def setup(bot: VillagerBotCluster) -> None:
    await bot.add_cog(Loops(bot))


from questionnaires.models.monthly_questionnaire_instance import (
    MonthlyQuestionnaireInstance,
)
from questionnaires.serializers.monthly_questionnaire_value_serializer import (
    MonthlyQuestionnaireValueSerializer,
)
from rest_framework import serializers

class MonthlyQuestionnaireInstanceListSerializer(serializers.ListSerializer):
    def update(self, instances, validated_data, **kwargs):
        if validated_data is None:
            return instances

        instance_mapping = {instance.id: instance for instance in instances}
        questionnaire = kwargs.get("questionnaire")

        ret = []
        for instance_data in validated_data:
            instance = instance_mapping.get(instance_data.get("id"), None)

            if instance_data.get("id") is None:
                instance_data["questionnaire"] = questionnaire
                ret.append(self.child.create(instance_data))
                continue

            if instance is None:
                instance = MonthlyQuestionnaireInstance.objects.get(
                    pk=instance_data.get("id")
                )
            ret.append(self.child.update(instance, instance_data))

        return ret

class MonthlyQuestionnaireInstanceSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(allow_null=True, required=False)
    comments = serializers.CharField(allow_null=True, allow_blank=True)
    creation_user = serializers.CharField(
        source="creation_user.username", required=False, read_only=True
    )
    values = MonthlyQuestionnaireValueSerializer(many=True)

    class Meta(object):
        model = MonthlyQuestionnaireInstance
        fields = (
            "id",
            "month",
            "year",
            "extended",
            "comments",
            "values",
            "creation_user",
            "created_at",
        )
        list_serializer_class = MonthlyQuestionnaireInstanceListSerializer

    @classmethod
    def many_init(cls, *args, **kwargs):
        # Return MonthlyQuestionnaireInstanceListSerializer when many=True
        kwargs["child"] = cls()
        return MonthlyQuestionnaireInstanceListSerializer(*args, **kwargs)

    def create(self, validated_data):

        values_data = validated_data.pop("values", None)

        validated_data["creation_user"] = self.context["request"].user
        instance = MonthlyQuestionnaireInstance.objects.create(**validated_data)

        instance.values.set(self.fields["values"].create(values_data))

        return instance

    def update(self, instance, validated_data):

        # calling to MonthlyQuestionnaireValueListSerializer.update() we can make all modifications
        # for values inside the instance
        instance.values.set(
            self.fields["values"].update(
                instance.values.all(), validated_data.pop("values", None)
            )
        )

        # nested entities properties were removed in previous methods
        for key in validated_data.keys():
            setattr(instance, key, validated_data.get(key, getattr(instance, key)))

        instance.save()

        return instance


from rich.layout import Layout
from rich.live import Live

from panels.cameron import cameron_panel
from panels.antsthebul import ants_panel
from panels.frontogenesis import frontogenesis_panel

def update_layout(layout):
    pass

def setup(layout):
    layout.split_column(
        Layout(name="top"),
        Layout(name="bottom"),
    )
    layout["top"].split_row(
        Layout(cameron_panel()),
        Layout(ants_panel()),
        Layout(frontogenesis_panel()))

main_layout = Layout()

if __name__ == "__main__":
    setup(main_layout)
    with Live(main_layout, refresh_per_second=10, screen=True):
        while True:
            update_layout(main_layout)


"""Golf solitaire."""
from itertools import zip_longest
from textwrap import dedent
from cards import Deck
import re

class Solitaire:
    """
    Solitaire class representing a game of Golf Solitaire.

    This game has 7 columns and 5 cards in each column,
    but the methods should work with other valid values as well.
    """

    cards_in_column = 1
    columns = 1

    def __init__(self):
        """
        Constructor, do the setup here.

        After setup with Solitaire.columns = 7, Solitaire.cards_in_column = 5
        You should have:
        self.tableau -> 7 columns of cards with 5 cards in each column
        self.stock -> 16 cards
        self.waste -> 1 card
        """
        self.deck = Deck()  # -> Deck instance
        self.deck.shuffle_deck()
        self.tableau = [[self.deck.deal_card() for cards in range(self.cards_in_column)] for x in range(self.columns)]  # -> list of (columns[lists] (where each list -> cards_in_column * Card instances))
        self.waste = [self.deck.deal_card()]  # -> list of Card instances
        self.stock = [self.deck.deal_card() for cards in range(len(re.findall(r"(\[[A-z\d][a-z]\])", str(self.deck))))]
        # -> list of Card instances
        # print(self.tableau)
        # print(self.waste)
        # print(self.stock)
        # print()

    def can_move(self, card) -> bool:
        """
        Validate if a card from the tableau can be moved to the waste pile.

        The card must be last in the column list and adjacent by rank
        to the topmost card of the waste pile (last in waste list).
        Example: 8 is adjacent to 7 and 9. Ace is only adjacent to 2.
        King is only adjacent to Queen.
        """
        card_in_last_columns_check = False
        for column in self.tableau:
            if column and column[-1] == card:
                card_in_last_columns_check = True
                break
        if not card_in_last_columns_check:
            print("Card not last in any column!")
            return False
        value_list = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"]
        if str(card)[1] == "A" and str(self.waste[-1])[1] == "2" or str(card)[1] == "K" and str(self.waste[-1])[1] == "Q":
            return True
        elif str(card)[1] in value_list[1:-1]:
            index = value_list.index(str(card)[1])
            if str(self.waste[-1])[1] == value_list[index - 1] or str(self.waste[-1])[1] == value_list[index + 1]:
                return True
        print("That move can not be made!")  # Will also be printed if stock is empty and lose cond is being checked.
        return False

    def move_card(self, col: int):
        """
        Move a card from the tableau to the waste pile.

        Does not validate the move.
        :param col: index of column
        """
        self.waste.append(self.tableau[col].pop(-1))

    def deal_from_stock(self):
        """
        Deal last card from stock pile to the waste pile.

        If the stock is empty, do nothing.
        """
        if not self.stock:
            pass
        else:
            self.waste.append(self.stock.pop(-1))

    def has_won(self) -> bool:
        """Check for the winning position - no cards left in tableau."""
        if len(re.findall(r"(\[[A-z\d][a-z]\])", str(self.tableau))) == 0:
            return True
        return False

    def has_lost(self) -> bool:
        """
        Check for the losing position.

        Losing position: no cards left in stock and no possible moves.
        """
        if len(self.stock) == 0 and not self.available_moves():
            return True
        return False

    def available_moves(self) -> bool:
        """Check for possible moves in all tableau columns. Return True if any exist."""
        for column in self.tableau:
            if column and self.can_move(column[-1]):
                return True
        return False

    def print_game(self):
        """
        Print the game.

        Assumes:
        Card(decorated=True) by default it is already set to True
        self.tableau -> a list of lists (each list represents a column of cards)
        self.stock -> a list of Card objects that are in the stock
        self.waste_pile -> a list of Card objects that are in the waste pile

        You may modify/write your own print_game.
        """
        print(f" {'    '.join(list('0123456'))}")
        print('-' * 34)
        print("\n".join([(" ".join((map(str, x)))) for x in (zip_longest(*self.tableau, fillvalue="    "))]))
        print()
        print(f"Stock pile: {len(self.stock)} card{'s' if len(self.stock) != 1 else ''}")
        print(f"Waste pile: {self.waste[-1] if self.waste else 'Empty'}")

    @staticmethod
    def rules():
        """Print the rules of the game."""
        print("Rules".center(40, "-"))
        print(dedent("""
                Objective: Move all the cards from each column to the waste pile.

                A card can be moved from a column to the waste pile if the
                rank of that card is one higher or lower than the topmost card
                of the waste pile. Only the first card of each column can be moved.

                You can deal cards from the stock to the waste pile.
                The game is over if the stock is finished and
                there are no more moves left.

                The game is won once the tableau is empty.

                Commands:
                  (0-6) - integer of the column, where the topmost card will be moved
                  (d) - deal a card from the stock
                  (r) - show rules
                  (q) - quit
                  """))

    @staticmethod
    def convert_str_of_int_to_int(command):
        """Convert string of int to int if possible. Return converted command/nonconvertible command, True/False."""
        try:
            return int(command), True
        except ValueError:
            return command, False

    def play(self):
        """
        Play a game of Golf Solitaire.

        Create the game loop here.
        Use input() for player input.
        Available commands are described in rules().
        """
        while True:
            # print(self.stock)
            self.print_game()
            command = input("Next move:").lower()
            command, conversion_success = self.convert_str_of_int_to_int(command)
            if conversion_success and 0 <= command < self.columns and self.can_move(self.tableau[command][-1]):
                self.move_card(command)
            elif command == "d" and self.stock:
                self.waste.append(self.stock.pop(-1))
            elif command == "r":
                self.rules()
                continue
            elif command == "q":
                break
            else:
                print("Invalid input")
                continue
            if self.has_won():
                print("You won!")
                break
            if self.has_lost():
                print("Game over, You lost!")
                break

if __name__ == '__main__':
    s = Solitaire()
    s.play()


import logging
import os

LOGGING_STRING_FORMAT = "%(asctime)s %(name)-12s %(levelname)-8s %(message)s"

def initialize_logger(output_dir, enable):
    if not enable:
        logger = logging.getLogger()
        logger.setLevel(1000)
        return

    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)

    # create console handler and set level to info
    handler = logging.StreamHandler()
    handler.setLevel(logging.DEBUG)
    formatter = logging.Formatter(LOGGING_STRING_FORMAT)
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    if output_dir:
        # create error file handler and set level to error
        handler = logging.FileHandler(os.path.join(output_dir, "error.log"), "wt", encoding="utf8", delay="true")
        handler.setLevel(logging.ERROR)
        formatter = logging.Formatter(LOGGING_STRING_FORMAT)
        handler.setFormatter(formatter)
        logger.addHandler(handler)

        # create debug file handler and set level to debug
        handler = logging.FileHandler(os.path.join(output_dir, "all.log"), "wt", encoding="utf8")
        handler.setLevel(logging.DEBUG)
        formatter = logging.Formatter(LOGGING_STRING_FORMAT)
        handler.setFormatter(formatter)
        logger.addHandler(handler)


from cvzone.HandTrackingModule import HandDetector
import imutils
import cv2
import random

cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)

#Inicializamos la variable imagen
image = cv2.imread('Restaurantes/JuvieJus.png', cv2.IMREAD_UNCHANGED)

#Definimos la cantidad de manos a detectar
detector = HandDetector(detectionCon=0.8, maxHands=1)

#Obtenemos los valores preliminares de detección de rostros
faceClassif = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

#Bucle infinito
while True:

	success, frame = cap.read()
	hands, frame = detector.findHands(frame)
	faces = faceClassif.detectMultiScale(frame, 1.3, 5)

	#Condicional para ver si encontró manos
	if hands:
		#Obtiene la imagen dependiendo de un número aleatorio
		numero = random.randint(1,6)
		if numero == 1:
			image = cv2.imread('Restaurantes/JuvieJus.png', cv2.IMREAD_UNCHANGED)
		if numero == 2:
			image = cv2.imread('Restaurantes/LaBuenaMesa.png', cv2.IMREAD_UNCHANGED)
		if numero == 3:
			image = cv2.imread('Restaurantes/Starbucks.png', cv2.IMREAD_UNCHANGED)
		if numero == 4:
			image = cv2.imread('Restaurantes/dolphy.png', cv2.IMREAD_UNCHANGED)
		if numero == 5:
			image = cv2.imread('Restaurantes/OXXO.png', cv2.IMREAD_UNCHANGED)
		if numero == 6:
			image = cv2.imread('Restaurantes/Subway.png', cv2.IMREAD_UNCHANGED)

	for (x, y, w, h) in faces:
		#cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0),2)
		# Redimensionar la imagen de entrada de acuerdo al ancho del
		# rostro detectado
		resized_image = imutils.resize(image, width=w)
		filas_image = resized_image.shape[0]
		col_image = w

		# Determinar una porción del alto de la imagen de entrada
		# redimensionada
		porcion_alto = filas_image // 4

		dif = 0

		# Si existe suficiente espacio sobre el rostro detectado
		# para insertar la imagen de entrada resimensionada
		# se visualizará dicha imagen
		if y + porcion_alto - filas_image >= 0:

			# Tomamos la sección de frame, en donde se va a ubicar
			# el gorro/tiara
			n_frame = frame[y + porcion_alto - filas_image : y + porcion_alto,
				x : x + col_image]
		else:
			# Determinamos la sección de la imagen que excede a la del video
			dif = abs(y + porcion_alto - filas_image)
			# Tomamos la sección de frame, en donde se va a ubicar
			# el gorro/tiara
			n_frame = frame[0 : y + porcion_alto,
				x : x + col_image]

		# Determinamos la máscara que posee la imagen de entrada
		# redimensionada y también la invertimos
		mask = resized_image[:, :, 3]
		mask_inv = cv2.bitwise_not(mask)

		# Creamos una imagen con fondo negro y el gorro/tiara/2021
		# Luego creamos una imagen en donde en el fondo esté frame
		# y en negro el gorro/tiara/2021
		bg_black = cv2.bitwise_and(resized_image, resized_image, mask=mask)
		bg_black = bg_black[dif:, :, 0:3]
		bg_frame = cv2.bitwise_and(n_frame, n_frame, mask=mask_inv[dif:,:])

		# Sumamos las dos imágenes obtenidas
		result = cv2.add(bg_black, bg_frame)
		if y + porcion_alto - filas_image >= 0:
			frame[y + porcion_alto - filas_image : y + porcion_alto, x : x + col_image] = result

		else:
			frame[0 : y + porcion_alto, x : x + col_image] = result
	cv2.imshow("Frame", frame)
	cv2.waitKey(1)
cap.release()
cv2.destroyAllWindows()


import pandas as pd
from pyclustering.cluster.birch import birch
from pyclustering.cluster.kmeans import kmeans
from pyclustering.utils import read_sample;
from pyclustering.samples.definitions import FCPS_SAMPLES
from pyclustering.cluster import cluster_visualizer
from sklearn.datasets.samples_generator import make_blobs
from sklearn.cluster import KMeans
from matplotlib import pyplot as plt
from sklearn.manifold import TSNE
import numpy as np
import sys
from pandas import DataFrame

a = []
train = pd.read_csv('/Users/xudunyuan/Desktop/data mining individual/web/trainData.csv')
#test = pd.read_csv('trainData.csv')

labels = list(train.columns.values) #this is the first line of the csv file
total_column = train.shape[0]
total_row = train.shape[1]

for i in range(1,len(labels)):
	column = train[labels[i]].tolist()
	Num_vote = 0
	for j in range(0,len(column)):
		Num_vote+=column[j]
	a.append(Num_vote)

lessThan5=[]
for i in range(0, len(a)):
	if a[i] <=5 :
		lessThan5.append(i)
new = train
for i in lessThan5:
	new = new.drop([labels[i+1]],axis=1)

train = new
labels = list(train.columns.values) #this is the first line of the csv file
total_column = train.shape[0]
total_row = train.shape[1]

total_num = [] #this 2D-array is for containing all value in the csv file
for i in range(0,total_column-1):
    num_df = train.iloc[i,:].values.tolist()
    num_df.pop(0)
    total_num.append(num_df)

X = np.array(total_num)
tsne = TSNE(n_components=2)
y = tsne.fit_transform(X)

kmeans = KMeans(n_clusters=30, init='k-means++', max_iter=300, n_init=10, random_state=0)
pred_y = kmeans.fit_predict(y)

data =DataFrame(y)

km = KMeans(n_clusters=30).fit(data)

cluster_map = pd.DataFrame()
cluster_map['data_index'] = data.index.values
cluster_map['cluster'] = km.labels_

total_cluster=[]
for j in range(20):
    cluster = []
    for i in cluster_map[cluster_map.cluster == j].values.tolist():
        cluster.append(i[0])
    total_cluster.append(cluster)

all_sum = [] # 2_D list contains all value
for k in range(0,20):
	cluster_num = [] #2-D list contain k th cluster
	for i in total_cluster[k]:
	    cluster_num.append(total_num[i])
	sum_=[]
	for j in range(0,len(labels)-1): # calculate how many 1s in each column in this cluster
	    sum1 = 0
	    for m in range(0, len(cluster_num)):
	        sum1 = sum1 + cluster_num[m][j]
	    sum_.append(sum1)
	all_sum.append(sum_)

all_prob = []
for k in range(0, len(all_sum)):
    pr = []
    total = 0
    for i in range(0, len(labels)-1):
        total = total + all_sum[k][i]
    for i in range(0, len(labels)-1):
        prob = float(all_sum[k][i]) / total
        pr.append(prob)
    all_prob.append(pr)
p = DataFrame(all_prob)


import datetime
import traceback

log_file = "/var/log/chess/access.log"
error_file = "/var/log/chess/error.log"

def wrap(func):
    def _wrapped(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception:
            with open(error_file, "a") as f:
                f.write("Exception at %s\n" % datetime.datetime.now())
                traceback.print_exc(file=f)
            raise
    _wrapped.__name__ = func.__name__
    return _wrapped


from multiprocessing import Process
from multiprocessing import BoundedSemaphore, Semaphore, Lock
from multiprocessing import current_process
from multiprocessing import Value, Array
from time import sleep
from random import random, randint
import numpy as np

# Cada productor tiene un storage de tamaño K

N = 4 # numero de elementos que genera el productor
K = 10 # tamaño del buffer de cada productor
NPROD = 3
NCONS = 1

def delay(factor = 3):
    sleep(random()/factor)

def add_data(storage, index, data, mutex):
    mutex.acquire()
    try:
        storage[index.value] = data
        delay(6)
        index.value = index.value + 1
    finally:
        mutex.release()

def get_data(storage, index, mutex):
    mutex.acquire()
    try:
        data = storage[0]
        index.value = index.value - 1
        delay()
        for i in range(index.value):
            storage[i] = storage[i + 1]
        storage[index.value] = -1
    finally:
        mutex.release()

def producer(storage, index, empty, non_empty, mutex):
    data = randint(0,10)
    for v in range(N):
        print (f"producer {current_process().name} produciendo")
        delay(6)
        empty.acquire()
        data += randint(0,10)
        add_data(storage, index, data, mutex)
        non_empty.release()
        print (f"producer {current_process().name} almacenado {data}")
    empty.acquire()
    add_data(storage,index, -1, mutex)
    non_empty.release()
    print (f"producer {current_process().name} almacenado {-1}")

def haya_productores(storage):
    for i in range(NPROD):
        if (storage[i][0] != -1):
            return True
    return False

# Coge el menor elemento de la cabeza de cada proceso
def get_min(storage, index):
    n = []
    ind = []
    for i in range(NPROD):
        if (storage[i][0] != -1):
            n.append(storage[i][0])
            ind.append(i)
    value = min(n)
    k = ind[n.index(value)]
    return k, value

def consumer(storage, index, empty, non_empty, mutex, sorted_data):
    # Esperamos a que todos esten produciendo
    for v in range(NPROD):
        non_empty[v].acquire()
    i = 0
    while haya_productores(storage):
        print (f"consumer {current_process().name} desalmacenando")
        ind, value = get_min(storage, index)
        get_data(storage[ind], index[ind], mutex[ind])
        sorted_data[i] = value
        empty[ind].release()
        print (f"consumer {current_process().name} consumiendo {value} del producer {ind}")
        non_empty[ind].acquire()
        delay()
        i += 1

def main():
    # Inicializamos las variables compartidas entre los productores y el consumidor
    storage = [Array('i', K) for i in range(NPROD)]
    index = [Value('i', 0) for i in range(NPROD)]
    numbers = Array('i', NPROD * N)
    for j in range(NPROD):
        for i in range(K):
            storage[j][i] = -1
        print (f"almacen inicial del productor {j} es", storage[j][:], "y su índice es", index[j].value)

    # Tenemos para cada productor un semáforo general, un semáforo acotado y un mutex
    # non_empty: semáforo que avisa al consumidor que hay un elemento disponible
    # empty: semáforo de tamaño K que avisa al productor de que ya puede almacenar un nuevo elemento
    # mutex: protege las variables compartidas: storage e index

    non_empty = [Semaphore(0) for _ in range(NPROD)]
    empty = [BoundedSemaphore(K) for _ in range(NPROD)]
    mutex = [Lock() for _ in range(NPROD)]

    prodlst = [Process(target=producer,
                        name=f'prod_{i}',
                        args=(storage[i], index[i], empty[i], non_empty[i], mutex[i]))
                for i in range(NPROD) ]

    conslst = [Process(target=consumer,
                        name=f"cons",
                        args=(storage, index, empty, non_empty, mutex, numbers))]

    for p in prodlst + conslst:
        p.start()
    for p in prodlst + conslst:
        p.join()

    print("¿La lista", numbers[:], "está ordenada?:", numbers[:] == sorted(numbers[:]))

if __name__ == '__main__':
    main()


# Polynomial curve fitting using Least Squares method
# Does not deal with inputs like: curve passes through origin
# Maybe use sympy?
import numpy as np
import matplotlib.pyplot as plt
from Ax_b import solution

def main():
    deg = int(input('Input degree of curve to fit: '))
    # Get data points from user
    points = []
    while len(points) < deg:
        not_done = True
        print('\nEnter points:')
        print('  No.of points should be more than degree')
        print("  Press 'q' when done.")
        while not_done:
            print('Input data points as (x,y):')
            point = input('> ')
            if point == 'q':
                not_done = False
            else:
                point = list(map(int, point.split(',')))
                points.append(point)

    # Find coefficients using least squares
    coeff = curve_fit(points, deg)
    print('\nCoefficients:')
    for i, c in enumerate(coeff[:, 0]):
        print(f'  a{i} = {c}')

    # ### PLOTTING ###
    # Find x max & min for x_lim of plot
    x_max = x_min = points[0][0]
    for x, _ in points:
        if x > x_max:
            x_max = x
        if x < x_min:
            x_min = x
    min_lim = 1.3*x_min if x_min < 0 else 0.7*x_min
    max_lim = 0.7*x_max if x_max < 0 else 1.3*x_max
    # Form points on nth deg polynomial
    y_val = []
    x_val = np.linspace(max_lim, min_lim, 100)
    for x in x_val:
        y = 0
        for i in range(deg+1):
            y += coeff[i, 0]*x**i
        y_val.append(y)
    # Plot points
    plt.plot(x_val, y_val, linewidth=3.5)
    for point in points:
        plt.scatter(point[0], point[1], color='black')
    plt.show()

def curve_fit(points, deg):
    if len(points) < deg:
        print('Error: No.of points should be more than degree')
        exit(1)

    A = np.empty((len(points), deg+1))
    b = np.empty((len(points), 1))
    # Form matrix A and b
    for i, point in enumerate(points):
        x, y = point
        b[i] = y
        for j in range(deg+1):
            A[i, j] = x**j

    At = np.transpose(A)
    aug = np.concatenate((A, b), axis=1)
    aug = At@aug
    coeff, _ = solution(aug, verbose=False)

    # ALITER: More computation
    # coeff = inv(At@A)@At@b
    # print(coeff)
    return coeff

if __name__ == '__main__':
    main()


# STOCK = "TSLA"
# COMPANY_NAME = "Tesla Inc"

## STEP 1: Use https://www.alphavantage.co
# When STOCK price increase/decreases by 5% between yesterday and the day before yesterday then print("Get News").

## STEP 2: Use https://newsapi.org
# Instead of printing ("Get News"), actually get the first 3 news pieces for the COMPANY_NAME.

## STEP 3: Use https://www.twilio.com
# Send a seperate message with the percentage change and each article's title and description to your phone number.

#Optional: Format the SMS message like this:
"""
TSLA: ðŸ”º2%
Headline: Were Hedge Funds Right About Piling Into Tesla Inc. (TSLA)?.
Brief: We at Insider Monkey have gone over 821 13F filings that hedge funds and prominent investors are required to file by the SEC The 13F filings show the funds' and investors' portfolio positions as of March 31st, near the height of the coronavirus market crash.
or
"TSLA: ðŸ”»5%
Headline: Were Hedge Funds Right About Piling Into Tesla Inc. (TSLA)?.
Brief: We at Insider Monkey have gone over 821 13F filings that hedge funds and prominent investors are required to file by the SEC The 13F filings show the funds' and investors' portfolio positions as of March 31st, near the height of the coronavirus market crash.
"""

import requests
import datetime as dt
from newsapi import NewsApiClient
from twilio.rest import Client

# GET STOCK PRICE
STOCK = "TSLA"
COMPANY_NAME = "Tesla Inc"
STOCK_API_KEY = "YOUR STOCK_API_KEY"
# GETS NEWS
NEWS_API_KEY = "YOUR NEWS_API_KEY"
# SEND EMAIL
TWILIO_SID = "YOUR TWILIO_SID"
TWILIO_AUTH_TOKEN = "YOUR TWILIO_AUTH_TOKEN"
VIRTUAL_TWILIO_NUMBER = "YOUR VIRTUAL_TWILIO_NUMBER"
VERIFIED_NUMBER = "YOUR VERIFIED_NUMBER"

# GET STOCK PRICE
now = dt.datetime.now()
now_year = now.year
now_month = now.month
now_day = now.day

parameters = {
    "function": "TIME_SERIES_INTRADAY",
    "symbol": STOCK,
    "interval": "60min",
    "apikey": STOCK_API_KEY
}
# print stock url
# print(f"https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={STOCK}&interval=60min&apikey={STOCK_API_KEY}")
response = requests.get(url="https://www.alphavantage.co/query", params=parameters)
response.raise_for_status()

data_today = response.json()["Time Series (60min)"][f"{now_year}-{now_month}-{now_day-1} 16:00:00"]["4. close"]
try:
    data_yesterday = response.json()["Time Series (60min)"][f"{now_year}-{now_month}-{now_day-2} 16:00:00"]["1. open"]
except KeyError:
    data_yesterday = response.json()["Time Series (60min)"][f"{now_year}-{now_month}-{now_day-5} 16:00:00"]["1. open"]
data_today = float(data_today)
data_yesterday = float(data_yesterday)
# print(data_today)
# print(data_yesterday)
percentage = round((data_yesterday - data_today) * 100 / data_yesterday, 2)
# print(percentage)
if percentage >= 0:
    percentage_text = f"TSLA ðŸ”º {percentage}%."
    # percentage_text = f"TSLA increase {percentage}%."
else:
    percentage_text = f"TSLA ðŸ”» {percentage}%."
    # percentage_text = f"TSLA decrease {percentage}%."
# print(percentage_text)

# GETS NEWS
parameters = {
    "q": "tesla",
    "from": "2022-12-28",
    "to": "2022-12-28T19:12:26",
    "sortBy": "publishedAt",
    "pageSize": 3, # 3 news
    "page": 1, # content size
    "language": "en",
    # "country": "us", # error with language
    "apiKey": NEWS_API_KEY
}

response = requests.get(url="https://newsapi.org/v2/everything", params=parameters)
# print news url
# q_text = parameters["q"]
# from_text = parameters["from"]
# to_text = parameters["to"]
# sortBy_text = parameters["sortBy"]
# pageSize_text = parameters["pageSize"]
# page_text = parameters["page"]
# language_text = parameters["language"]
# apikey_text = parameters["apiKey"]
# print(f"https://newsapi.org/v2/everything?q={q_text}&from={from_text}&to={to_text}&sortBy={sortBy_text}&pageSize={pageSize_text}&page={page_text}&language={language_text}&apikey={apikey_text}")
response.raise_for_status()

news_content = response.json()["articles"]
# print(news_content)

# SEND EMAIL
account_sid = TWILIO_SID
auth_token = TWILIO_AUTH_TOKEN

client = Client(account_sid, auth_token)
for i in range(0, 3):
    headline = news_content[i]["title"]
    brief = news_content[i]["description"]
    message_body = f"{percentage_text}\nHEADLINE: {headline}\nBrief: {brief}"
    print(message_body)

    message = client.messages \
                    .create(
                        # body=message_body,
                        body="test",
                        from_=VIRTUAL_TWILIO_NUMBER,
                        to=VERIFIED_NUMBER
                    )

    print(message.status)


import enum
import unittest
from test.support import import_helper
from test.support import os_helper

_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
_testcapi = import_helper.import_module('_testcapi')

class Constant(enum.IntEnum):
    Py_CONSTANT_NONE = 0
    Py_CONSTANT_FALSE = 1
    Py_CONSTANT_TRUE = 2
    Py_CONSTANT_ELLIPSIS = 3
    Py_CONSTANT_NOT_IMPLEMENTED = 4
    Py_CONSTANT_ZERO = 5
    Py_CONSTANT_ONE = 6
    Py_CONSTANT_EMPTY_STR = 7
    Py_CONSTANT_EMPTY_BYTES = 8
    Py_CONSTANT_EMPTY_TUPLE = 9

    INVALID_CONSTANT = Py_CONSTANT_EMPTY_TUPLE + 1

class GetConstantTest(unittest.TestCase):
    def check_get_constant(self, get_constant):
        self.assertIs(get_constant(Constant.Py_CONSTANT_NONE), None)
        self.assertIs(get_constant(Constant.Py_CONSTANT_FALSE), False)
        self.assertIs(get_constant(Constant.Py_CONSTANT_TRUE), True)
        self.assertIs(get_constant(Constant.Py_CONSTANT_ELLIPSIS), Ellipsis)
        self.assertIs(get_constant(Constant.Py_CONSTANT_NOT_IMPLEMENTED), NotImplemented)

        for constant_id, constant_type, value in (
            (Constant.Py_CONSTANT_ZERO, int, 0),
            (Constant.Py_CONSTANT_ONE, int, 1),
            (Constant.Py_CONSTANT_EMPTY_STR, str, ""),
            (Constant.Py_CONSTANT_EMPTY_BYTES, bytes, b""),
            (Constant.Py_CONSTANT_EMPTY_TUPLE, tuple, ()),
        ):
            with self.subTest(constant_id=constant_id):
                obj = get_constant(constant_id)
                self.assertEqual(type(obj), constant_type, obj)
                self.assertEqual(obj, value)

        with self.assertRaises(SystemError):
            get_constant(Constant.INVALID_CONSTANT)

    def test_get_constant(self):
        self.check_get_constant(_testlimitedcapi.get_constant)

    def test_get_constant_borrowed(self):
        self.check_get_constant(_testlimitedcapi.get_constant_borrowed)

class PrintTest(unittest.TestCase):
    def testPyObjectPrintObject(self):

        class PrintableObject:

            def __repr__(self):
                return "spam spam spam"

            def __str__(self):
                return "egg egg egg"

        obj = PrintableObject()
        output_filename = os_helper.TESTFN
        self.addCleanup(os_helper.unlink, output_filename)

        # Test repr printing
        _testcapi.call_pyobject_print(obj, output_filename, False)
        with open(output_filename, 'r') as output_file:
            self.assertEqual(output_file.read(), repr(obj))

        # Test str printing
        _testcapi.call_pyobject_print(obj, output_filename, True)
        with open(output_filename, 'r') as output_file:
            self.assertEqual(output_file.read(), str(obj))

    def testPyObjectPrintNULL(self):
        output_filename = os_helper.TESTFN
        self.addCleanup(os_helper.unlink, output_filename)

        # Test repr printing
        _testcapi.pyobject_print_null(output_filename)
        with open(output_filename, 'r') as output_file:
            self.assertEqual(output_file.read(), '<nil>')

    def testPyObjectPrintNoRefObject(self):
        output_filename = os_helper.TESTFN
        self.addCleanup(os_helper.unlink, output_filename)

        # Test repr printing
        correct_output = _testcapi.pyobject_print_noref_object(output_filename)
        with open(output_filename, 'r') as output_file:
            self.assertEqual(output_file.read(), correct_output)

    def testPyObjectPrintOSError(self):
        output_filename = os_helper.TESTFN
        self.addCleanup(os_helper.unlink, output_filename)

        open(output_filename, "w+").close()
        with self.assertRaises(OSError):
            _testcapi.pyobject_print_os_error(output_filename)

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


from itertools import cycle
from helper_functions import (
    create_initial_game_board,
    show,
    make_move,
    determine_game_status
)

players = cycle(["X", "O"])
playing = True

while playing:

    # game preparations
    game_board = create_initial_game_board()
    game_status = determine_game_status(game_board)

    while game_status == "ongoing":
        show(game_board)

        # player move
        current_player = next(players)
        valid_move = False
        while not valid_move:
            try:
                game_board = make_move(game_board, current_player)
                valid_move = True

            except Exception as e:
                print("\n")
                print("Something went wrong:", e)
                print("Hint: The chosen position must be available on the board.")
                print("Hint: The values must be separated by a comma e.g. '0, 1'.")
                print("Try again!", "\n")

        # stopping condition for while-loop
        game_status = determine_game_status(game_board)
        if game_status != "ongoing":
            show(game_board)
            print("Game finished!!!")

            if game_status == "draw":
                print("It is a draw! \n")
            else:
                print(f"Player {current_player} has achieved a {game_status}! \n")

    keep_playing = input("Do you want to play again? (y/n) ")
    if keep_playing != "y":
        playing = False


from rest_framework.views import APIView
from rest_framework.response import Response
from cocktails.helper import Helper

# from TheCocktailDB website, different endpoints for searching drinks based on alcohol type, name, or id number
class API_URLs():
    byAlochol = "http://www.thecocktaildb.com/api/json/v1/1/filter.php?i="
    byName = "http://www.thecocktaildb.com/api/json/v1/1/search.php?api_key=1&s="
    byId = "http://www.thecocktaildb.com/api/json/v1/1/lookup.php?i="

# When frontend calls /generate URL, django uses parameter passed in through URL of the type of alcohol
# and picks 4 (will change this to be dynamic) random drinks from the list that the API response generates
class generateCocktails(APIView):
    def get(self, request):
        url_param = 'drink'
        drink = request.GET.get(url_param)
        Helper.regulateWine(self, drink) # The API has small selections for wine and champagne so I have this choose between one of the two when using wine choice on frontend
        url = API_URLs.byAlochol + drink
        response = Helper.sendRequest(url)
        output = Helper.extractJSON(response)
        package = Helper.pickRandomDrinks(output)
        return Response (package)

# When frontend calls /details URL, django uses parameter passed in through URL of the name of the drink
# and retrieves the id (in the API databse) and ingredient list, glassware, and instructions to display
class getCocktailDetails(APIView):
    def get(self, request):
        url_param = 'name'
        name = request.GET.get(url_param)
        url = API_URLs.byName + name
        response = Helper.sendRequest(url)
        output = Helper.extractJSON(response)
        package = Helper.getDetails(output)
        return Response(package)

# Uses id found in the getCocktailDetials function to do another API request to the endpoint that utilizes
# the unique id, and this has info on the drink's image link (not included when searching by name for some reason)
class getCocktailImage(APIView):
    def get(self, request):
        url_param = 'id'
        id = request.GET.get(url_param)
        url = API_URLs.byId + id
        response = Helper.sendRequest(url)
        output = Helper.extractJSON(response)
        image = Helper.getImage(output)
        return Response(image)


from random import randint
import pygame
import drawTools
import mazeTools
import movingObjects
from time import sleep

#******************
mapSize = int(input("Grid Size: "))
#cellSize =int( input("Pixel size of each square (try like 20):")) #number of pixels for each line that makes up a square
#mapSize = 20
cellSize = 10

BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)
BLUE =  (  0,   0, 255)
GREEN = (  0, 255,   0)
RED =   (255,   0,   0)
pygame.init()
screenSize = 500
#screen = pygame.display.set_mode([(mapSize + 2) * cellSize,(mapSize + 2) * cellSize])
screen = pygame.display.set_mode([screenSize, screenSize])

screen.fill(WHITE)
#************

clock = pygame.time.Clock()

player = movingObjects.Player(10,"graphics/DinoPlayer.png", screenSize/2, screenSize/2)

map = mazeTools.Map(mapSize)
map.makeMaze()

currentR = randint(0, mapSize - 1)
currentC = randint(0, mapSize - 1)

currentCell = map.getCell(currentR, currentC)

drawTools.drawRoom(currentCell, screenSize, screen)

pygame.display.flip()

done = False
while(not done):
    clock.tick(30)
    for event in pygame.event.get():  # User did something
        if event.type == pygame.QUIT:  # If user clicked close
            done = True  # Flag that we are done so we exit this loop

    if event.type == pygame.KEYDOWN:
        cellOffset = player.move(screenSize, event=event)
        currentR+= cellOffset[0]
        currentC+= cellOffset[1]

    currentCell = map.getCell(currentR, currentC)

    drawTools.drawRoom(currentCell, screenSize, screen)
    player.update(screen)
    pygame.display.flip()


import json
from flask import Blueprint, render_template, request, redirect, url_for
from models.alert import Alert
from models.item import Item
from models.store import Store
from models.user import requires_login, requires_admin

store_blueprint = Blueprint('bp_stores', __name__)

@store_blueprint.route('/')
@requires_login
def index():
    stores_present = Store.all()
    print("RKS: STORE INDEX ALL done")
    return render_template('stores/store_index.html', stores=stores_present)

@store_blueprint.route('/new', methods=['GET', 'POST'])
@requires_admin
def new_store():
    if request.method == 'POST':
        # process the Data from the Form.
        # access the Item_ID, & Item_Price from the form
        store_name = request.form['name_of_store']
        url_prefix = request.form['nm_url_prefix']
        tag_name = request.form['name_tag']
        query = json.loads(request.form['nm_query'])

        Store(store_name, url_prefix, tag_name, query).save_to_mongo()

    return render_template('stores/new_store.html')

@store_blueprint.route('/edit/<string:store_id>', methods=['GET', 'POST'])
@requires_admin
def edit_store(store_id):
    store_by_id = Store.get_by_id(store_id)

    if request.method == 'POST':
        store_name = request.form['name_of_store']
        url_prefix = request.form['nm_url_prefix']
        tag_name = request.form['name_tag']
        query = json.loads(request.form['nm_query'])

        # Modify the Store object
        store_by_id.store_name = store_name
        store_by_id.url_prefix = url_prefix
        store_by_id.tag_name = tag_name
        store_by_id.query = query

        # We'll now update the Store Entry in DB.
        store_by_id.save_to_mongo()
        # Call below computes the URL to go to index method inside the current blueprint using '.'
        return redirect(url_for('.index'))

    # somehow if we do not use Dumps below, the Query gets passed to HTML as '' single quotes
    # this then is discarded by json.loads().
    my_query = store_by_id.query
    store_by_id.query = json.dumps(my_query)

    return render_template('stores/edit_store.html',store=store_by_id)

@store_blueprint.route('/delete/<string:store_id>')
@requires_admin
def delete_store(store_id):
    Store.get_by_id(store_id).remove_from_mongo()
    return redirect(url_for('.index'))


from google.appengine.ext import ndb

class Author(ndb.Model):
    """Sub model for representing an author."""
    identity = ndb.StringProperty(indexed=False)
    email = ndb.StringProperty(indexed=False)

class Greeting(ndb.Model):
    """A main model for representing an individual Guestbook entry."""
    author = ndb.StructuredProperty(Author)
    content = ndb.StringProperty(indexed=False)
    date = ndb.DateTimeProperty(auto_now_add=True)

class Game(ndb.Model):
    #user = ndb.ReferenceProperty(Account)
    numberPlayers = ndb.IntegerProperty()
    entryFee = ndb.FloatProperty()
    usersSignedUp = ndb.IntegerProperty()
    #PrizeStructure (Model w/ variety of types)
    #StartTime
    #Duration
    #EndTime
    #GameResultKey (ForeignKey) - 1 to 1

class UserGames(ndb.Model):
    #userKey (Foreign Key) - 1 to 1
    gameKey = ndb.KeyProperty(Game, repeated=True)

class Account(ndb.Model):
    #displayName = ndb.StringProperty()
    email = ndb.StringProperty()
    #userGames = ndb.KeyProperty(kind=UserGames)
    uid = ndb.KeyProperty()
    # phone =
    # Street Address
    # City
    # State
    # Zip
    # Date Joined
    # Last Login
    # UserBalanceKey (Foreign Key) - 1 to 1


import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

df = pd.read_csv('batterylog_28052019.blx', header=None, sep='\s+')
df.head()

x = df[6]
y = df[7]
z = df[3]-1

fig = plt.figure(figsize=(12,3))
ax = fig.subplots()
ax.plot(x,'ro', label = 'A')
ax.plot(y,'bo', label = 'B')

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Volt')
ax.set_title('Status')
##grid(color='r', linestyle='-', linewidth=2)
plt.grid(True)
ax.set_xticks((z))
ax.set_xticklabels((z+1))
ax.legend()
plt.axis([0, 43, 0, 8])
plt.show()


import os
import json

def load_json(filename=''):
    if not os.path.exists(filename):
        print(f'Graph file: {filename} does not exist')
        return None
    with open(filename, 'r') as graph_file:
        data = json.load(graph_file)
    return data


import json5
import pandas as pd
import ccxt
from binance_cfuture.Config import *
from binance_cfuture.Function import *
import sys
import os

pd.set_option('display.max_rows', 1000)
pd.set_option('expand_frame_repr', False)  # 当列太多时不换行
pd.set_option('display.unicode.ambiguous_as_wide', True)  # 设置命令行输出时的列对齐功能
pd.set_option('display.unicode.east_asian_width', True)
# =====常规config信息
# 获取项目根目录
_ = os.path.abspath(os.path.dirname(__file__))  # 返回当前文件路径
root_path = os.path.abspath(os.path.join(_, '..'))  # 返回根目录文件夹
data_save_dir = os.path.join(root_path, 'data', 'binance_trading_data')

# ===读取程序运行所需的子账号相关参数
if len(sys.argv) > 1:
    account_name = sys.argv[1]
else:
    print('未指定account_name参数，程序exit')
    exit()
print('子账户id：', account_name)

# =获取执行的时间间隔

time_interval = json5.load(open('Dynamic_Config'))['account_info'][account_name]['time_interval']
time_interval_re = str(int(kline_interval[:-1]) * 60) + 'm' if kline_interval.endswith('h') else kline_interval
exec_interval_re = str(int(time_interval[:-1]) * 60) + 'm' if time_interval.endswith('h') else time_interval
offset_time_re = str(int(offset_time[:-1]) * 60) + 'm' if offset_time.endswith('h') else offset_time
print('执行时间周期：', time_interval, ',底层K线周期：', kline_interval, '每执行周期偏移时间量：', offset_time)

# =从config中读取相关配置信息
exchange = ccxt.binance(BINANCE_CONFIG_dict[account_name])
main_ex = ccxt.binance(BINANCE_CONFIG_dict['main'])
symbol_config = json5.load(open('Dynamic_Config'))['account_info'][account_name]['symbol_config']
print('symbol_config:\n', pd.DataFrame(symbol_config))
print('交易信息：', symbol_config)

# =获取交易精度
coin_future_exchange_info(exchange, symbol_config)

def main():
    # =判断是否单向持仓，若不是程序退出。币安的持仓模式介绍。双向持仓、单向持仓
    if_coin_future_oneway_mode(exchange)

    # ==========获取需要交易币种的历史数据==========
    # 获取数据
    # max_candle_num = 5  # 每次获取的K线数量
    # symbol_candle_data = get_binance_coin_future_history_candle_data(exchange, symbol_config, time_interval_re,
    #                                                                  max_candle_num, if_print=True)

    trading_initialization(exchange, account_name, funding_config, symbol_config, main_ex)
    # =进入每次的循环
    while True:
        # ==========获取持仓数据==========
        # 初始化symbol_info，在每次循环开始时都初始化，防止上次循环的内容污染本次循环的内容。
        symbol_info_columns = ['账户币数', '原始币数', '持仓方向_'+mode, '合约张数', '持仓均价', '未实现盈亏', '实际美元价值']
        symbol_info = pd.DataFrame(index=symbol_config.keys(), columns=symbol_info_columns)

        # 更新账户信息symbol_info
        symbol_info = binance_update_cfuture_account(exchange, symbol_config, symbol_info, mode)
        print('\nsymbol_info:\n', symbol_info, '\n')

        check_symbol_config_update(exchange, account_name, symbol_config, symbol_info)

        # ==========根据当前时间，获取策略下次执行时间，例如16:15。并sleep至该时间==========
        run_time = sleep_until_run_time(exec_interval_re, if_sleep=False, offset_time=offset_time_re)
        # ==========获取最新的k线数据==========

        symbol_candle_data = {}
        for symbol in symbol_config.keys():
            p = os.path.join(data_save_dir,
                             'data_ready_%s_%s_%s' % (symbol, time_interval, str(run_time).replace(':', '-')))
            print('获取数据地址：', p)
            while True:
                if os.path.exists(p):
                    print('数据已经存在：', datetime.now())
                    break
                if datetime.now() > run_time + timedelta(minutes=2):
                    print('时间超过2分钟，获取K线超时，无法计算信号，请及时检查获取K线脚本！')
                    send_dingding_and_raise_error('币本位择时框架获取K线超时，无法计算信号，请及时检查获取K线脚本！')
                    break
            symbol_candle_data[symbol] = pd.read_csv(os.path.join(data_save_dir, '%s_%s.csv' % (symbol, time_interval)))
            symbol_info.loc[symbol, '当前价格'] = symbol_candle_data[symbol].iloc[-1]['close']  # 该品种的最新价格
            print(symbol_candle_data[symbol].tail(5))

        # 将symbol_candle_data和最新获取的recent_candle_data数据合并
        # symbol_candle_data = symbol_candle_data_append_recent_candle_data(symbol_candle_data, recent_candle_data,
        #                                                                   symbol_config, max_candle_num)

        # ==========计算每个币种的交易信号==========
        symbol_signal, save_signals_dict = calculate_signal(symbol_info, symbol_config, symbol_candle_data, mode)
        print('\n产生信号时间:\n', symbol_info[['持仓方向_'+mode, '目标持仓', '信号', '信号时间']])
        print('\n本周期交易计划:', symbol_signal)

        # ==========下单==========
        exchange.timeout = exchange_timeout  # 下单时需要增加timeout的时间，将timout恢复正常
        # 计算下单信息
        symbol_order_params = cal_all_order_info(symbol_signal, symbol_info, symbol_config, exchange, mode)
        print('\n订单参数\n', symbol_order_params)

        # 开始批量下单
        order_info_list = place_binance_cfuture_batch_order(exchange, symbol_order_params)
        if len(order_info_list) == 0:
            symbol_order = pd.DataFrame()
        else:
            symbol_order = pd.concat(order_info_list)

        # ==========检测是否需要adl==========
        time.sleep(medium_sleep_time)
        if deal_adl:
            deal_with_binance_adl(exchange, symbol_info, symbol_config)
        # 更新账户信息symbol_info
        symbol_info = pd.DataFrame(index=symbol_config.keys(), columns=symbol_info_columns)
        symbol_info = binance_update_cfuture_account(exchange, symbol_config, symbol_info, mode)
        print('\nsymbol_info:\n', symbol_info, '\n')
        print('\n############\n')

        # 发送钉钉
        dingding_report_every_loop(symbol_info, symbol_signal, symbol_order, run_time, robot_id_secret, account_name)

        # 利润提取
        if take_profit_rate > 0:
            take_info = pd.DataFrame(take_profit(exchange, account_name, symbol_config, main_ex, symbol_info, take_profit_rate))
            if not take_info.empty:
                dingding_take_profit_report(take_info)

        # 保存signals日志
        if save_signals_dict:
            save_signal_data(save_signals_dict, time_interval, offset_time, account_name, root_path)

        # 本次循环结束
        print('\n', '-' * 20, '本次循环结束，%f秒后进入下一次循环' % long_sleep_time, '-' * 20, '\n\n')
        time.sleep(long_sleep_time)

if __name__ == '__main__':
    while True:
        # try:
            main()
        # except Exception as err:
        #     print('系统出错，10s之后重新运行，出错原因：' + str(err))
        #     time.sleep(long_sleep_time)

币安_币本位合约择时策略实盘.py


import time
import os
import json
import sys

OUT_DATA_ROOT = './OpenFMZ/webpage/static/data/'
OUT_DATA_INFO = 'info/latest.info'
INFO = {
    "start": 0,
    "file": "py_filename",
    "logSize": 0,
    "value": 'value/0.value',
    "custom": {
        "file": 'custom/0.custom',
        "cfgs": {}
    },
    "log": 'log/0.log'
}

class Chart:
    def __init__(self, cfglist):
        global INFO
        self.cfgs = cfglist
        self.writebuffer = []
        self.buffersize = 10
        self.filelines = 0
        self.filelineslimit = 500000

        INFO["custom"]["cfgs"] = cfglist
        WriteINFO()

    def add(self, seriesId, d, usebuffer=True):
        groupId = self.seriesId2groupId(seriesId)
        s = str(groupId) + ' ' +\
            str(seriesId) + ' ' +\
            str(d[0]) + ' ' +\
            str(d[1]) + '\n'
        self.writebuffer.append(s)

        if len(self.writebuffer) > self.buffersize or not usebuffer:
            with open(OUT_DATA_ROOT + INFO["custom"]["file"], 'a') as f:
                f.write(''.join(self.writebuffer))
            self.filelines += len(self.writebuffer)
            self.writebuffer.clear()

        #如果文件过大，新建文件
        if self.filelines > self.filelineslimit:
            self.reset()
            self.filelines = 0

    def reset(self):
        global INFO
        nowstr = str(int(time.time()*1000))
        INFO["custom"]["file"] = 'custom/' + nowstr + '.custom'
        WriteINFO()

    def seriesId2groupId(self, seriesId):
        r = 0
        for i in range(len(self.cfgs)):
            for j in range(len(self.cfgs[i]['series'])):
                if r == seriesId:
                    return i
                else:
                    r += 1
        return -1

_log_buffer = []
def Log(*arg):
    now = int(time.time()*1000)
    s = str(now) + ' 信息 ' +\
            ' '.join([str(i) for i in arg]) + '\n'
    _log_buffer.append(s)

    if len(_log_buffer) > 10:
        with open(OUT_DATA_ROOT + INFO["log"], 'a', encoding='utf8') as f:
            f.write(''.join(_log_buffer))
        _log_buffer.clear()

def LogProfit(p):
    now = int(time.time()*1000)
    with open(OUT_DATA_ROOT + INFO["value"], 'a', encoding='utf8') as f:
        f.write(str(now) + ' ' + str(p) + '\n')

    with open(OUT_DATA_ROOT + INFO["log"], 'a', encoding='utf8') as f:
        s = str(now) + ' 估值 ' +\
            str(p) + '\n'
        f.write(s)

def WriteINFO():
    with open(OUT_DATA_ROOT + OUT_DATA_INFO, 'w', encoding='utf8') as f:
        f.write(json.dumps(INFO))

def Sleep(ms):
    time.sleep(ms/1000)

def N_(v, n):
    return round(v, n)

def _init():
    now = int(time.time()*1000)
    nowstr = str(now)

    #备份旧的latest.info文件
    try:
        os.rename(OUT_DATA_ROOT + OUT_DATA_INFO, OUT_DATA_ROOT + 'info/' + nowstr + '.info')
    except:
        pass

    #写入latest.info
    back_frame = sys._getframe().f_back
    back_filename = os.path.basename(back_frame.f_code.co_filename)
    INFO["start"] = now
    INFO["file"] = back_filename
    INFO["value"] = 'value/' + nowstr + '.value'
    INFO["custom"]["file"] = 'custom/' + nowstr + '.custom'
    INFO["log"] = 'log/' + nowstr + '.log'
    WriteINFO()

_init()


import requests
from lxml import etree
import time
import json

#https://www.cnblogs.com/supershuai/p/12297312.html
def get_all_proxy(page):
    url = 'https://www.xicidaili.com/nn/%s' % page
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
    }
    response = requests.get(url, headers=headers)
    html_ele = etree.HTML(response.text)
    ip_eles = html_ele.xpath('//table[@id="ip_list"]/tr/td[2]/text()')
    port_ele = html_ele.xpath('//table[@id="ip_list"]/tr/td[3]/text()')
    print(ip_eles)
    proxy_list = []
    for i in range(0, len(ip_eles)):
        check_all_proxy(ip_eles[i], port_ele[i])
    return proxy_list

def check_all_proxy(host, port):
    type = 'http'
    proxies = {}
    proxy_str = "%s://@%s:%s" % (type, host, port)
    valid_proxy_list = []
    url = 'http://www.baidu.com/'
    proxy_dict = {
        'http': proxy_str,
        'https': proxy_str
    }
    try:
        start_time = time.time()
        response = requests.get(url, proxies=proxy_dict, timeout=5)
        if response.status_code == 200:
            end_time = time.time()
            print('代理可用：' + proxy_str)
            print('耗时:' + str(end_time - start_time))
            proxies['type'] = type
            proxies['host'] = host
            proxies['port'] = port
            proxiesJson = json.dumps(proxies)
            with open('verified_y.json', 'a+') as f:
                f.write(proxiesJson + '\n')
            print("已写入：%s" % proxy_str)
            valid_proxy_list.append(proxy_str)
        else:
            print('代理超时')
    except:
        print('代理不可用--------------->' + proxy_str)

if __name__ == '__main__':
    for i in range(1, 2):  # 选取前十页数据使用
        proxy_list = get_all_proxy(i)
        time.sleep(20)
        print(proxy_list)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
import csv
import time
from tqdm import tqdm
from django.shortcuts import render
from django.conf import settings
from .database import dbs as db

# Create your views here.

def home(request):
    return render(request,'home_page.html')

def check_new_entry(request):
    if request.method == 'GET':
        count = db.test.find({}).count()
        if (count == 0):
            return render(request, 'upload_new_winedata.html')
        else:
            context = {
                'count' : count
            }
            return render(request, 'query.html',context)
    elif request.method == 'POST':
        files = request.FILES['datafile']
        with open(str(files),'rb') as fl:
            spamreader = csv.reader(files, delimiter=str(','))
            row_count = sum(1 for row in fl )
        count = 0
        data = []
        head_list = []
        iterator = tqdm(spamreader,total=row_count)
        for row in iterator:
            time.sleep(0.1)
            if ( count == 0 ):
                head_list = row
                count = count + 1
            else:
                data.append(dict(zip(head_list, row)))
                count= count +1
        db.test.insert(data)
        context = {
            'count':count-1
        }
        return render(request, 'query.html',context)

def filter_item(request):
    global item
    if request.method == 'POST' :
        search_field = request.POST.get('query')
        search_data = request.POST.get('Search')
        if len(str(search_data)) != 2 :
            search_data = search_data.title()
        else:
            search_data = search_data.upper()
        dbs =db.test
        if search_field == 'all' or search_data == '' :
            item = list(dbs.find({}))
        else:
            item = list(dbs.find({ search_field : search_data }))
        if item == []:
            return render(request,'result_error.html')
        else:
            context = {
                'items':item,
                'field':search_field,
                'data' : search_data
            }
            return render(request, 'result.html',context)
    else :
        return render(request, 'result.html', {'items': item})

def clear_database(request):
    db.test.remove({})
    return render(request,'upload_new_winedata.html')

def details(request,id):
    detail = db.test.find_one({ 'id' : id })        # column name can be anything
    return render(request,'details.html',{'detail': detail})


# -*- coding: utf-8 -*-
from __future__ import (absolute_import, print_function, unicode_literals)
from acli.output import (output_ascii_table, output_ascii_table_list, dash_if_none)
from colorclass import Color, Windows
Windows.enable(auto_colors=True, reset_atexit=True)

def output_route53_list(zones=None):
    """
    @type zones: list | dict
    """
    td = list()
    table_header = list()
    if isinstance(zones, dict):
        zones = [zones]
    for hosted_zone_dict in zones:
        td = list()
        table_header = [Color('{autoblue}id{/autoblue}'), Color('{autoblue}name{/autoblue}'),
                        Color('{autoblue}count{/autoblue}'), Color('{autoblue}comment{/autoblue}'),
                        Color('{autoblue}private zone{/autoblue}')]
        hosted_zones = sorted(hosted_zone_dict.get('HostedZones'), key=lambda k: k['Name'])
        for hosted_zone in hosted_zones:
            zone_id = dash_if_none(hosted_zone.get('Id'))
            zone_name = dash_if_none(hosted_zone.get('Name'))
            record_count = str(hosted_zone.get('ResourceRecordSetCount'))
            comment = dash_if_none(hosted_zone.get('Config').get('Comment'))
            private_zone = dash_if_none(hosted_zone.get('Config').get('PrivateZone'))
            td.append([zone_id,
                       zone_name,
                       record_count,
                       comment,
                       private_zone])
    output_ascii_table_list(table_title=Color('{autowhite}route53 zones{/autowhite}'),
                            table_data=td,
                            table_header=table_header,
                            inner_heading_row_border=True)
    exit(0)

def get_record_set_values(resource_records):
    """
    @type resource_records: list
    """
    out = list()
    for record in resource_records:
        out.append(record.get('Value'))
    return out

def output_route53_info(zone=None, record_sets=None):
    """
    @type zone: zone
    @type record_sets: ResourceRecordSets
    """
    td = list()
    td.append([Color('{autoblue}id{/autoblue}'),
               dash_if_none(zone.get('HostedZone').get('Id'))])
    td.append([Color('{autoblue}name{/autoblue}'),
               dash_if_none(zone.get('HostedZone').get('Name'))])
    td.append([Color('{autoblue}records{/autoblue}'),
               dash_if_none(zone.get('HostedZone').get('ResourceRecordSetCount'))])
    td.append([Color('{autoblue}comment{/autoblue}'),
               dash_if_none(zone.get('HostedZone').get('Config').get('Comment'))])
    td.append([Color('{autoblue}private{/autoblue}'),
               dash_if_none(zone.get('HostedZone').get('Config').get('PrivateZone'))])
    td.append([Color('{autoblue}name servers{/autoblue}'),
               "\n".join(zone['DelegationSet']['NameServers'])])
    td.append([Color('{autoblue}records{/autoblue}'), ' '])
    td.append(['{0}'.format("-" * 12), '{0}'.format("-" * 20)])
    for record_set in record_sets['ResourceRecordSets']:
        td.append([Color('{autoblue}name{/autoblue}'),
                   record_set['Name']])
        td.append([Color('{autoblue} type{/autoblue}'),
                   record_set['Type']])
        td.append([Color('{autoblue} ttl{/autoblue}'),
                   str(record_set['TTL'])])
        td.append([Color('{autoblue} values{/autoblue}'),
                   "\n".join(get_record_set_values(record_set['ResourceRecords']))])
    output_ascii_table(table_title=Color('{autowhite}zone info{/autowhite}'),
                       table_data=td)
    exit(0)


#!/usr/bin/python

# Tested and implemented on Python 2.5.4 and Pygame 1.9.1
from __future__ import with_statement # might want to detect <2.6
import sys
import os
import pygame
import random
#import threading
from pygame.locals import *

import tilemap
if not pygame.font: print('warning, fonts disabled')
if not pygame.mixer: print('warning, sound disabled')

#! /usr/bin/env python
pygame.init()

def ableToJump():
     if (playerRect.left < 3  # wall jumps
            or playerRect.right > SCRWIDTH - 3
            or playerRect.bottom > SCRHEIGHT - 2):
         # might need px perfect collision first, depends on implementation
         return True
     else:
         return False

SCRWIDTH, SCRHEIGHT = 640, 480

clock = pygame.time.Clock()
fpslimit = 60 # fps limit
PAUSEFPS = 15 # fps when paused

# length of a game in seconds
GAMELENGTH = 15

class Player:
    def __init__(self, size = 5, startcoords = 460, prevCoord = 5, speed = 0):
        self.RECTLEFT = self.RECTTOP = startcoords
        self.RECTWIDTH = self.RECTHEIGHT = 5
        self.playerRect = pygame.Rect(self.RECTLEFT, self.RECTTOP,
                                        self.RECTWIDTH, self.RECTHEIGHT)
        self.prevLeft = self.prevTop = 5  # prev coords. used for collisions
        self.movementspeed = speed
        self.playerXspeed = self.playerYspeed = 0
        self.jumping = False # True while rising from jump

        # airborne is better than jumped because its state depends solely on
        # player position rather than an abstract game concept
        self.airborne = (False if self.bottom > SCRHEIGHT else True)

        # how long i've been rising from the jump. gotta stop rising from jump
        # eventually
        self.jumpframes = 0

    def update(self):
        #__________________________________________________________
        #
        # Wall/ground/ceiling collision
        #__________________________________________________________
        #
        # check wall collision, move back inside if needed
        if self.playerRect.top < 0:
            self.playerRect.top = 1
        elif self.playerRect.bottom > SCRHEIGHT:
            self.playerRect.top = SCRHEIGHT - RECTHEIGHT
            self.airborne = False  # reset jumped since i've hit the ground

# player Rect coords, dimensions
#===================================

RECTLEFT = RECTTOP = RECTWIDTH = RECTHEIGHT = 32
playerRect = pygame.Rect(RECTLEFT, RECTTOP, RECTWIDTH, RECTHEIGHT)
blockcolor = (255, 0, 0) # red
playercolor = (255, 255, 255) # white
goalcolor = (0, 0, 255) # blue

# color constants
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

prevLeft = prevTop = 5  # hold prev coords. used for collisions
playerXspeed = playerYspeed = 0
jumping = False # True while rising from jump
jumped = False  # indicate already jumped, prevent infinite jumps
jumpframes = 0  # how long i've jumped. gotta stop rising from jump eventually
movespeed = 5   # horizontal movement speed

# controls
#====================
JUMPKEY = K_k
LEFTKEY = K_s
RIGHTKEY = K_f
DOWNKEY = K_d
QUITKEY = K_ESCAPE
PAUSEKEY = K_p

MOVEKEYSSTR = (pygame.key.name(LEFTKEY) + pygame.key.name(DOWNKEY)
                + pygame.key.name(RIGHTKEY))

# set up tilemap
tilemap = tilemap.Tilemap()
solidtiles = []

for i, row in enumerate(tilemap.maplines):
    for j, col in enumerate(tilemap.maplines[i]):
        if col == '1':
            block = pygame.Rect(j * tilemap.tilewidth, i * tilemap.tileheight,
                                tilemap.tilewidth,       tilemap.tileheight)
            solidtiles.append(block)

# array of numbers to test with goal rect moving.
goalRectPositions = []

random.seed(1)

# fill goalRectPositions with coords to try
for i in range(5):
    maxX = SCRWIDTH / RECTWIDTH - 2
    maxY = SCRHEIGHT / RECTHEIGHT - 2

    newGoalX, newGoalY = random.randint(1, maxX), random.randint(1, maxY)

    # always check if legal
    while tilemap.at(newGoalX * RECTWIDTH,
                     newGoalY * RECTHEIGHT) == "1":
        print("rejected: " + str(newGoalX) + ", " + str(newGoalY))
        newGoalX = random.randint(1, maxX)
        newGoalY = random.randint(1, maxY)

    goalRectPositions.append((newGoalX, newGoalY))

goalRect = pygame.Rect( RECTWIDTH*newGoalX, RECTHEIGHT*newGoalY,
                        RECTWIDTH, RECTHEIGHT)
goalRectsHit = 0;
goalRectPosIndexToTry = goalRectsHit;

timepassed = 0
#print(str(playerRect.top))
#print(str(playerRect.left))

screen = pygame.display.set_mode((SCRWIDTH, SCRHEIGHT))

# game state constants
STATE_PAUSE = 0
STATE_GAME_IN_PROGRESS = 1
STATE_GAMEOVER = 2

gamestate = STATE_GAME_IN_PROGRESS

# Pause loop. Come in here when pause button is pressed.
# Variable number of arguments. Each argument is a separate line. This is due
# to pygame not handling line wrapping or newlines.
#=======================================================
def pause(*pausetext):
    # print("paused")
    global playerXspeed
    global gamestate
    playerXspeed = 0 # prevent weirdness when pausing with a movement key down
    gamestate = STATE_PAUSE

    # stay in this pause loop till game resumes.
    while gamestate == STATE_PAUSE:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gamestate = STATE_GAME_IN_PROGRESS
                # running = False
            elif event.type == KEYDOWN:
                if event.key == QUITKEY:
                    gamestate = STATE_GAME_IN_PROGRESS
                    # running = False
                elif event.key == K_p:
                    gamestate = STATE_GAME_IN_PROGRESS

        # pause msg
        if pygame.font:
            font = pygame.font.Font(None, 36)
            i = 0

            for eachline in pausetext:
                pausemsg = font.render(str(eachline), False, WHITE, BLACK)
                msgpos = pausemsg.get_rect(center=(SCRWIDTH/2,
                                                    SCRHEIGHT/2 + i * 50))
                screen.blit(pausemsg, msgpos)
                i += 1

            pygame.display.flip()

        # PAUSEFPS can be less than 40 because there's no need for instant
        # input response.
        clock.tick(PAUSEFPS)

def checkGoalCollision():
    global playerRect
    global playerXspeed
    global movespeed
    global goalRect
    global maxX
    global maxY
    global newGoalX
    global newGoalY
    global goalRectsHit
    global goalRectPositions
    global goalRectPosIndexToTry

    # hit goal, create new goal
    if playerRect.colliderect(goalRect):
        maxX = SCRWIDTH / RECTWIDTH - 2
        maxY = SCRHEIGHT / RECTHEIGHT - 2

        newGoalX, newGoalY = (goalRectPositions[goalRectPosIndexToTry][0],
                                goalRectPositions[goalRectPosIndexToTry][1])
        goalRectsHit = goalRectsHit + 1
        goalRectPosIndexToTry = goalRectsHit % len(goalRectPositions)
        # print("Goals hit: " + str(goalRectsHit) +
                # ". Goal array index: " + str(goalRectPosIndexToTry))

        goalRect.topleft = (RECTWIDTH * newGoalX,
                            RECTHEIGHT * newGoalY)

        movespeed += 1
        # print("newgoalx, newgoaly: " + str(newGoalX) + " " + str(newGoalY))
        # print("maxX, maxY: " + str(maxX) + " " + str(maxY))
        # print("new goalRect.topleft: " + str(goalRect.topleft))

#pause("HIT AS MANY BLUE SQUARES AS YOU CAN! CONTROLS: esdf TO MOVE, k TO JUMP")
pause("HIT THE BLUE SQUARES! " + MOVEKEYSSTR + " TO MOVE, "
        + pygame.key.name(JUMPKEY) + " TO JUMP.",
        "PRESS " + pygame.key.name(PAUSEKEY) + " TO START.",
        "YOU HAVE " + str(GAMELENGTH) + " SECONDS.")

# gameloop
#============
frames = 0
running = True
while running:
    # event = pygame.event.poll()
    for event in pygame.event.get():
        # pygame.event.clear()
        if event.type == pygame.QUIT:
            running = False
        # elif event.type == pygame.MOUSEMOTION:
            # print "mouse at (%d, %d)" % event.pos
        # elif event.type == pygame.MOUSEBUTTONDOWN:
            # print "mouse at (%d, %d)" % event.pos # DEBUG
        #______________________________________________________________________
        #
        # Key events and control
        #______________________________________________________________________
        #
        elif event.type == KEYDOWN:
            # print "key pressed: " + str(event.key) # ??? # DEBUG
            if event.key == QUITKEY:
                running = False

            if event.key == LEFTKEY:
                # print "a pressed, leftspeed +" # DEBUG
                playerXspeed += -movespeed # playerRect.move_ip(-5, 0)
            if event.key == RIGHTKEY:
                # print "d pressed, rightspeed +" # DEBUG
                playerXspeed += movespeed
                # playerRect.move_ip(5, 0)

            # jump. the Gravity section prevents jump craziness. collision
            # section resets the jumped variable to allow jumping again
            if event.key == JUMPKEY and ((not jumped) or ableToJump()):
                # print "w pressed, upspeed +" # DEBUG
                jumping = True
                jumpframes = 0
                playerYspeed = -10
            if event.key == DOWNKEY:
                # print "s pressed, downspeed +" # DEBUG
                playerYspeed += 5

            # Pause. Key down/up does not mess up movement when coming out
            # of pause because they check for speed before trying to move
            # strangely.
            if event.key == PAUSEKEY:
                pause("GAME PAUSED")
        if event.type == KEYUP:
            # print "key released: " + str(event.key) # ??? # DEBUG
            if event.key == LEFTKEY and playerXspeed < -4:
                # print "a released, leftspeed -" # DEBUG
                # playerXspeed += movespeed # playerRect.move_ip(-5, 0)
                playerXspeed = 0
            if event.key == RIGHTKEY and playerXspeed > 4:
                # print "d released, rightspeed -" # DEBUG
                # playerXspeed += -movespeed
                playerXspeed = 0
            if event.key == JUMPKEY:
                # print "w released, upspeed -" # DEBUG
                jumping = False
                jumpframes = 0
            if event.key == DOWNKEY:
                # print "s released, downspeed -" # DEBUG
                playerYspeed += -5

    # regardless of whether or not i jumped. it should become false again
    # within this loop if i'm in contact with something.
    jumped = True

    if jumping:
        jumpframes += 1

    #__________________________________________________________
    #
    # Gravity
    #__________________________________________________________
    # gravity if i've jumped too long
    # no gravity during jump or if too fast
    if (playerYspeed < 3 and not jumping) or jumpframes > 10:
        playerYspeed += 1 # gravity
        jumpframes = 0
        jumping = False
        # print('gravity!') # DEBUG

    #__________________________________________________________
    #
    # Tile collision
    #__________________________________________________________
    #
    playerRect.move_ip(0, playerYspeed) # move based on speed
    if tilemap.in_collision(playerRect): # Y axis collision
        if playerYspeed > 0: # came from top
            # move player up to fit the tile instead of overlapping the bottom
            # tile
            playerRect.top = int((playerRect.top)
                                / tilemap.tileheight) * tilemap.tileheight
            jumped = False
        elif playerYspeed < 0: # came from bottom
            # move player down instead of overlapping the top tile
            playerRect.top = int((playerRect.top + tilemap.tileheight)
                                / tilemap.tileheight) * tilemap.tileheight
            # jumped = False    # this would be interesting ceiling jumping

    playerRect.move_ip(playerXspeed, 0) # move based on speed
    if tilemap.in_collision(playerRect): # X axis collision
        if playerXspeed > 0: # came from left
            playerRect.left = int((playerRect.left)
                                / tilemap.tilewidth) * tilemap.tilewidth
            jumped = False
        elif playerXspeed < 0: # came from right
            playerRect.left = int((playerRect.left + tilemap.tilewidth)
                                / tilemap.tilewidth) * tilemap.tilewidth
            jumped = False

    #__________________________________________________________
    #
    # Wall/ground/ceiling collision
    #__________________________________________________________
    #
    # check wall collision, move back inside if needed
    if playerRect.top < 0:
        playerRect.top = 0
    elif playerRect.bottom > SCRHEIGHT:
        playerRect.top = SCRHEIGHT - RECTHEIGHT
        jumped = False  # reset jumped since i've hit the ground

    # left/right boundaries
    if playerRect.left < 0:
        playerRect.left = -1
    if playerRect.right > SCRWIDTH:
        playerRect.left = SCRWIDTH - playerRect.width - 1
    prevTop = playerRect.top
    prevLeft = playerRect.left

    checkGoalCollision();

    # redraw whole screen
    screen.fill(BLACK)
    for eachtile in solidtiles:
        screen.fill(blockcolor, eachtile)
    #pygame.draw.rect(screen, (255, 0, 0), collisionbox)
    pygame.draw.rect(screen, playercolor, playerRect)
    pygame.draw.rect(screen, goalcolor, goalRect)
    pygame.display.flip()

    timepassed += clock.tick(fpslimit)

    frames += 1

    # every 1 second (every 60 frames, actually. which should be 1 second)
    if frames > 60:
        pygame.display.set_caption(str(clock.get_fps()) + ' fps')
        frames = 0

        if playerXspeed < -5 or playerXspeed > 5:
            playerXspeed *= 0.9

    if timepassed > GAMELENGTH * 1000:
        pause("GAME OVER. SCORE: " + str(goalRectsHit) + ". PRESS " +
                pygame.key.name(PAUSEKEY) + " TO CONTINUE.")
        timepassed = 0
        goalRectsHit = 0
        movespeed = 5

# be idle friendly
pygame.quit()

    # K_a           a
    # K_b           b
    # K_c           c
# http://eli.thegreenplace.net/2008/12/13/writing-a-game-in-python-with-pygame-part-i/
# http://lorenzod8n.wordpress.com/2007/05/25/pygame-tutorial-1-getting-started/
# http://en.wordpress.com/tag/pygame-tutorial/
# locals has commonly used constants / functions
# import for convenience, so that the func / constants are in global


from PIL import Image
import random
end=100
def show_board():
    image=Image.open(r'C:\NPTEL_PYTHON\board.jpg')
    image.show()
def check_ladder(points):
    if points==3:
        print('Ladder')
        return 20
    elif points==6:
        print('Ladder')
        return 14
    elif points==11:
        print('Ladder')
        return 28
    elif points==15:
        print('Ladder')
        return 34
    elif points==17:
        print('Ladder')
        return 74
    elif points==22:
        print('Ladder')
        return 37
    elif points==49:
        print('Ladder')
        return 67
    elif points==57:
        print('Ladder')
        return 76
    elif points==61:
        print('Ladder')
        return 78
    elif points==73:
        print('Ladder')
        return 86
    elif points==81:
        print('Ladder')
        return 98
    elif points==88:
        print('Ladder')
        return 91
    else:
        return points
def check_snake(points):
    if points==18:
        print("snake")
        return 1
    elif points==8:
        print("snake")
        return 4
    elif points==39:
        print("snake")
        return 5
    elif points==51:
        print("snake")
        return 6
    elif points==26:
        print("snake")
        return 10
    elif points==59:
        print("snake")
        return 17
    elif points==64:
        print("snake")
        return 36
    elif points==69:
        print("snake")
        return 33
    elif points==73:
        print("snake")
        return 1
    elif points==83:
        print("snake")
        return 19
    elif points==92:
        print("snake")
        return 51
    elif points==95:
        print("snake")
        return 24
    elif points==98:
        print("snake")
        return 28
    else:
        return points
def reached_end(points):
    if points==end:
        return True
    else:
        return False
def play():
    p1_name=input("Player1 please enter your name:")
    p2_name=input("Player2 please enter your name:")
    pp1=0
    pp2=0
    turn=0
    while(1):
        if turn%2==0:
            print(p1_name,'This is your turn')
            c=input("Enter 1 continue and 0 to quick")
            if c==0:
                print(p1_name, 'scored:' ,pp1)
                print(p2_name, 'scored:' ,pp2)
                print("Quitting game,thanks for playing!!!")
                break
            dice=random.randint(1,6)
            print('dice showed', dice)
            pp1=pp1+dice
            pp1=check_ladder(pp1)
            pp1=check_snake(pp1)
            if pp1>end:
                pp1=end
            print(p1_name,'Your score is:',pp1)
            if reached_end(pp1):
                print(p1_name, 'won')
                break
        else:
            print(p2_name,'This is your turn')
            c=input("Enter 1 continue and 0 to quick")
            if c==0:
                print(p1_name, 'scored:' ,pp1)
                print(p2_name, 'scored:' ,pp2)
                print("Quitting game,thanks for playing!!!")
                break
            dice=random.randint(1,6)
            print('dice showed', dice)
            pp2=pp2+dice
            pp2=check_ladder(pp2)
            pp2=check_snake(pp2)
            if pp2>end:
                pp2=end
            print(p2_name,'Your score is:',pp2)
            if reached_end(pp2):
                print(p2_name, 'won')
                break
        turn=turn+1
show_board()
play()


import numpy as np
import matplotlib.pyplot as plt

data=np.loadtxt('output.txt')
data_t=np.transpose(data)

x_axis=data_t[11]

plt.hist(x_axis,bins=1000)
#plt.title('Kepler Magnitudes for objects around KOI 3158')
plt.xlim(0,np.max(data_t))
plt.xlabel('Magnitude')
plt.ylabel('Number of objects')
plt.show()


import pytest

from wemake_python_styleguide.violations.consistency import (
    ConstantConditionViolation,
)
from wemake_python_styleguide.visitors.ast.compares import (
    WrongConditionalVisitor,
)

create_variable = """
variable = 1
{0}
"""

if_statement = 'if {0}: ...'
ternary = 'ternary = 0 if {0} else 1'

list_comprehension = """
def container():
    [x for x in [1, 2, 3] if {0}]
"""

set_comprehension = """
def container():
    {{
        x
        for x in [1, 2, 3]
        if {0}
    }}
"""

dict_comprehension = """
def container():
    {{
        x: '1'
        for x in [1, 2, 3]
        if {0}
    }}
"""

gen_comprehension = """
def container():
    (x for x in [1, 2, 3] if {0})
"""

@pytest.mark.parametrize('code', [
    if_statement,
    ternary,
    list_comprehension,
    set_comprehension,
    dict_comprehension,
    gen_comprehension,
])
@pytest.mark.parametrize('comparators', [
    'variable < 3',
    'variable',
    'variable is True',
    'variable is False',
    '[1, 2, 3].size > 3',
    'variable is None',
    'variable is int or not some()',
    '(unique := some()) is True',
])
def test_valid_conditional(
    assert_errors,
    parse_ast_tree,
    code,
    comparators,
    default_options,
    mode,
):
    """Testing that conditionals work well."""
    tree = parse_ast_tree(
        mode(create_variable.format(code.format(comparators))),
    )

    visitor = WrongConditionalVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [])

@pytest.mark.parametrize('code', [
    if_statement,
    ternary,
    # list_comprehension,
    # set_comprehension,
    # dict_comprehension,
    # gen_comprehension,
])
@pytest.mark.parametrize('comparators', [
    'True',
    'False',
    'None',
    '4',
    '-4.8',
    '--0.0',
    '"test"',
    "b'bytes'",
    '("string in brackets")',
    '{test : "1"}',
    '{"set"}',
    '("tuple",)',
    '["list"]',
    'variable or False',
    'variable and False',
    'variable or True',
    'variable and True',
    '(unique := True)',
    '(unique := -1)',
])
def test_constant_condition(
    assert_errors,
    parse_ast_tree,
    code,
    comparators,
    default_options,
    mode,
):
    """Testing that violations are when using invalid conditional."""
    tree = parse_ast_tree(
        mode(create_variable.format(code.format(comparators))),
    )

    visitor = WrongConditionalVisitor(default_options, tree=tree)
    visitor.run()

    assert_errors(visitor, [ConstantConditionViolation])


import cv2
import numpy as np
import sys

#inverse gamma correction
def invgamma(x):
    for i in range(0, x.shape[0]):
        if (x[i] < 0.03928):
            x[i] /= 12.92
        else:
            x[i] = pow(((x[i] + 0.055) / 1.055), 2.4)
    return x

#restrict the RGB value to be in the range of [0,1]
def limit(x):
    for i in range(0, x.shape[0]):
        if x[i] > 1:
            x[i] = 1
        else:
            if x[i] < 0:
                x[i] = 0
    return x

def XYZ2Luv(x):
    #in case x=[0,0,0]
    if (x[0] == 0 and x[1] == 0 and x[2] == 0):
        return np.array([0, 0, 0], dtype = np.float_)
    if (x[1] > 0.008856):
        L = 116*pow(x[1], 1/3) -16
    else:
        L = 903.3 * x[1]
    d = x[0] + 15 * x[1] + 3 * x[2]
    up1 = 4 * x[0]/d
    vp1 = 9 * x[1]/d
    u = 13 * L * (up1-0.19771071800208116545265348595213)
    v = 13 * L * (vp1-0.46826222684703433922996878251821)
    return np.array([L, u, v], dtype = np.float_)

def Luv2XYZ(x):
    #in case L is 0
    if (x[0] == 0):
        return np.array([0, 0, 0], dtype = np.float_)
    up2 = (x[1] + 13 * 0.19793943 * x[0]) / (13 * x[0])
    vp2 = (x[2] + 13 * 0.46831096 * x[0]) / (13 * x[0])
    if (x[0] > 7.9996):
        Y = pow(((x[0] + 16) / 116), 3)
    else:
        Y = x[0] / 903.3
    if (vp2 == 0):
        X = 0
        Z = 0
    else:
        X = Y * 2.25 * up2 / vp2
        Z = Y * (3 - 0.75 * up2 - 5 * vp2) / vp2
    return np.array([X, Y, Z], dtype = np.float_)

#gamma correction
def gamma(x):
    for i in range(0,x.shape[0]):
        if x[i] < 0.00304:
            x[i] *= 12.92
        else:
            x[i] = 1.055*pow(x[i], 1/2.4)-0.055
    return x

'''
For the main function part:
including read image, arguments setting, etc
'''

if(len(sys.argv) != 7) :
    print(sys.argv[0], ": takes 6 arguments. Not ", len(sys.argv)-1)
    print("Expecting arguments: w1 h1 w2 h2 ImageIn ImageOut.")
    print("Example:", sys.argv[0], " 0.2 0.1 0.8 0.5 fruits.jpg out.png")
    sys.exit()

w1 = float(sys.argv[1])
h1 = float(sys.argv[2])
w2 = float(sys.argv[3])
h2 = float(sys.argv[4])
name_input = sys.argv[5]
name_output = sys.argv[6]

#convert matrix references to https://docs.opencv.org/3.1.0/de/d25/imgproc_color_conversions.html
RGB2XYZ=np.array([[0.412453, 0.357580, 0.180423], [0.212671, 0.715160, 0.072169], [0.019334,0.119193,0.950227]], np.float_)
XYZ2RGB=np.array([[3.240479, -1.53715, -0.498535], [-0.969256, 1.875991, 0.041556], [0.055648,-0.204043,1.057311]], np.float_)

if(w1<0 or h1<0 or w2<=w1 or h2<=h1 or w2>1 or h2>1) :
    print(" arguments must satisfy 0 <= w1 < w2 <= 1, 0 <= h1 < h2 <= 1")
    sys.exit()

inputImage = cv2.imread(name_input, cv2.IMREAD_COLOR)
if(inputImage is None):
    print(sys.argv[0], ": Failed to read image from: ", name_input)
    sys.exit()

rows, cols, bands = inputImage.shape # bands == 3
sumP = rows * cols
W1 = round(w1 * (cols-1))
H1 = round(h1 * (rows-1))
W2 = round(w2 * (cols-1))
H2 = round(h2 * (rows-1))

# The transformation should be based on the
# historgram of the pixels in the W1,W2,H1,H2 range.
# The following code goes over these pixels
mL = 2147483647
ML = -2147483648
tmp = np.copy(inputImage)

#for specific area of the original image
area = np.zeros([H2-H1+1, W2-W1+1, bands], dtype = np.float_)
for i in range(H1, H2+1):
    for j in range(W1, W2+1):

        #identify the area selected in the original image
        if (i == H1 or i == H2 or j == W1 or j == W2):
           tmp[i, j] = [255, 255, 255]
        b, g, r = inputImage[i, j]
        area[i-H1, j-W1] = r, g, b
        area[i-H1, j-W1, 0], area[i-H1, j-W1, 1], area[i-H1, j-W1, 2] = area[i-H1, j-W1, 0]/255, area[i-H1, j-W1, 1]/255, area[i-H1, j-W1, 2]/255

        area[i-H1, j-W1] = np.array(limit(invgamma(area[i-H1, j-W1])), np.float_).T
        area[i-H1, j-W1] = np.array(np.dot(RGB2XYZ, area[i-H1, j-W1]))
        area[i-H1, j-W1] = np.array(XYZ2Luv(area[i-H1, j-W1]).T,np.float_)

        #find the min value of L
        if (area[i-H1, j-W1, 0] < mL):
            mL = area[i-H1, j-W1, 0]
        #find out the max value of L
        if (area[i-H1, j-W1, 0] > ML):
            ML = area[i-H1, j-W1, 0]

cv2.imshow('Selected Area', tmp)

#calculate L interval between discretized values
intervalL = (ML - mL) / 100
sum = np.zeros([101], dtype = np.int_)
mapValue = np.zeros([101], dtype = np.int_)

inputTemp = np.zeros([inputImage.shape[0], inputImage.shape[1], inputImage.shape[2]], dtype=np.float_)
outputTemp = np.zeros([inputImage.shape[0], inputImage.shape[1], inputImage.shape[2]], dtype=np.uint8)

for i in range(0,inputTemp.shape[0]):
    #apply scaling to entire image
    for j in range(0,inputTemp.shape[1]):
        b, g, r = inputImage[i, j]
        inputTemp[i, j] = np.array([r, g, b], dtype = np.float_)
        inputTemp[i, j] = np.dot(1/255, inputTemp[i, j])
        inputTemp[i, j] = np.array(limit(invgamma(inputTemp[i, j])), dtype=np.float_).T
        inputTemp[i, j] = np.array(np.dot(RGB2XYZ, inputTemp[i, j]))
        inputTemp[i, j] = np.array(XYZ2Luv(inputTemp[i, j]))
        if (inputTemp[i, j, 0] < mL):
            sum[0] += 1
        else:
            if (inputTemp[i, j, 0] > ML):
                sum[100] += 1
            else:
                #in case all L have same value
                if (mL != ML):
                    #calculate the index to which current pixel belong
                    interTemp = int(round((inputTemp[i, j, 0] - mL) / intervalL))
                    sum[interTemp] += 1
#print("Count:\n",sum)

mapValue[0] = sum[0] * 101 / (2 * sumP)
#calculate distribution
for i in range(1, sum.shape[0]):
    sum[i] = sum[i-1] + sum[i]
    mapValue[i] = round((sum[i-1] + sum[i]) * 101 / (2 * sumP) - 0.5)
    if (mapValue[i] == 101):
        mapValue[i] -= 1

for i in range(0, inputTemp.shape[0]):
    for j in range(0, inputTemp.shape[1]):
        if (inputTemp[i, j, 0] < mL):
            inputTemp[i, j, 0] = 0
        else:
            if (inputTemp[i, j, 0] > ML):
                inputTemp[i, j, 0] = 100
            else:
                if (mL != ML):
                    inputTemp[i, j, 0] = mapValue[int(round((inputTemp[i, j, 0] - mL) / intervalL))]
        inputTemp[i,j] = np.array(Luv2XYZ(inputTemp[i, j]), np.float_)
        inputTemp[i,j] = np.array(np.dot(XYZ2RGB,inputTemp[i, j].T), np.float_)
        inputTemp[i,j] = limit(inputTemp[i, j])
        inputTemp[i,j] = np.array(gamma(inputTemp[i, j]), np.float_)
        inputTemp[i,j] = np.array(np.dot(255, inputTemp[i,j].T), np.float_)
        outputTemp[i,j] = np.array([inputTemp[i, j, 2], inputTemp[i, j, 1], inputTemp[i, j, 0]], np.uint8).T

cv2.imshow('Final Result', outputTemp)

# end of example of going over area

outputImage = np.zeros([rows, cols, bands], dtype=np.uint8)

for i in range(0, rows) :
    for j in range(0, cols) :
        b, g, r = inputImage[i, j]
        outputImage[i,j] = [b, g, r]
cv2.imwrite(name_output, outputTemp);
# wait for key to exit
cv2.waitKey(0)
cv2.destroyAllWindows()


import os
import pkg_resources
from mako.template import Template
from . action import Action

__all__ = ['render']

class render(Action):

    def __init__(self, template_name, target, deferred=False,
                 skip_rollback=False, perms=None, **params):
        super(render, self).__init__()
        self.params = params
        self.template_name = template_name
        self.perms = perms
        self.template = Template(
            pkg_resources.resource_stream('aybu.manager.templates',
                                          template_name).read()
        )
        self.skip_rollback = skip_rollback
        self.target = target
        self.written = False
        if not deferred:
            self.write()
        else:
            self.log.debug("rendering of %s has been deferred", self.target)

    def render(self):
        if "instance" in self.params:
            settings = self.params['instance'].environment.settings
        else:
            from aybu.manager.models import Environment
            settings = Environment.settings

        return self.template.render(
                        settings=settings,
                        **self.params
        )

    def write(self):
        self.written = True
        self.log.debug("RENDERING %s to %s", self.template_name, self.target)
        try:
            with open(self.target, "w") as target:
                target.write(self.render())
            if self.perms:
                os.chmod(self.target, self.perms)
        except:
            self.rollback()
            raise

    def commit(self):
        if not self.written:
            self.write()

    def rollback(self):
        if self.skip_rollback:
            self.log.debug("Skipping rollback on %s", self.target)
            return

        if self.written:
            self.log.info("ROLLBACK: unlink %s", self.target)
            os.unlink(self.target)