from birch.cells.cell import Cell
from birch.util import BG_COLOR
from random import shuffle

class ConnectableCell(Cell):
    _masks = {
        '': '00000000',
        '_h': '01102222',
        '_v': '10012222',
        '_tl': '00112220',
        '_tr': '01012202',
        '_bl': '10102022',
        '_br': '11000222',
        '_x': '11110000',
        '_o': '00000000',
        '_i_tl': '11111110',
        '_i_tr': '11111101',
        '_i_bl': '11111011',
        '_i_br': '11110111',
        '_u_tl': '00112221',
        '_u_tr': '01012212',
        '_u_br': '11001222',
        '_u_bl': '10102122',
        '_f': '11111111',
        '_hl': '00102222',
        '_hr': '01002222',
        '_vt': '00012222',
        '_vb': '10002222',
        '_vl': '10112121',
        '_vr': '11011212',
        '_hb': '11101122',
        '_ht': '01112211',
        '_ltee': '10112020',
        '_ttee': '01112200',
        '_rtee': '11010202',
        '_btee': '11100022',
        '_yt': '11110011',
        '_yb': '11111100',
        '_yl': '11110101',
        '_yr': '11111010',
        '_ytlt': '10112021',
        '_ytll': '01112201',
        '_ytrt': '11010212',
        '_ytrr': '01112210',
        '_ybrb': '11011202',
        '_ybrr': '11101022',
        '_yblb': '10112120',
        '_ybll': '11100122',
        '_ydr': '11110110',
        '_ydl': '11111001',
        '_ytlx': '11110001',
        '_ytrx': '11110010',
        '_yblx': '11110100',
        '_ybrx': '11111000'
        }

    _default_texture = ''
    _connection_mask_map = {}
    _connection_masks = {}

    def __init__(self, name, textures, position, size=[16, 16]):
        if len(self._connection_masks) == 0:
            for k in self._masks:
                self._connection_masks[k] = []
                mask = self._masks[k]
                z = []
                for item in list(mask):
                    z.append(int(item))
                self._connection_masks[k].append(z)

        texture_name = self._default_texture
        super().__init__(name, textures, position, texture_name, size=size)
        self.base_texture_name = name
        self.next_tick = 0

    # expects order to be [top, left, right, bottom]
    def cache_texture(self, surrounding):
        # create a list of suffixes, including none, to check for textures
        # with some variation
        suffixes = ['_0', '_1']
        shuffle(suffixes)
        items = ['']
        items.extend(suffixes)
        zoro = [0, 0, 0, 0, 0, 0, 0, 0]
        mask = list(zoro)
        for cell in surrounding:
            if type(cell) != type(self) or cell == self:
                continue
            tb = cell.top == self.bottom
            bb = cell.bottom == self.bottom
            bt = cell.bottom == self.top
            ll = self.left == cell.left
            lr = self.left == cell.right
            rl = self.right == cell.left
            mask[0] = mask[0] or ll and tb
            mask[1] = mask[1] or lr and bb
            mask[2] = mask[2] or rl and bb
            mask[3] = mask[3] or ll and bt
            mask[4] = mask[4] or lr and tb
            mask[5] = mask[5] or rl and tb
            mask[6] = mask[6] or lr and bt
            mask[7] = mask[7] or rl and bt
            mask = list(map(lambda item: int(item), mask))
        if mask == zoro:
            return

        """
        ok here's how this works.

        first, we enumerate the keys of self._connection_masks. We are looking for
        a case where the True/False settings of this match to what we deduced above.
        If we find 'k' that matches, we also check to see if there is an included
        mapping of other _suffixes to k. In this manner there can be a default
        texture for many kinds of connections between cells.

        Otherwise, if there's no mapping the mapping is deduced based on the original
        k value.

        After this, we check for two types of textures: ones that end in k/sfx, and then
        those that end in that plus _0 or _1. This way there can be some variation in
        textures.
        """

        for k in self._connection_masks:
            for checkmask in self._connection_masks[k]:
                mapped = [k]
                matched = True
                for (i, z) in enumerate(mask):
                    if checkmask[i] == 2:
                        continue
                    if z != checkmask[i]:
                        matched = False
                        break
                if not matched:
                    continue
                # look for other suffixes that should be checked instead of this
                # name.
                if k in self._connection_mask_map:
                    mapped = self._connection_mask_map[k]
                for sfx in mapped:
                    tex_key = '%s%s' % (self.base_texture_name, sfx)
                    # cycle through our prebuilt list of suffixes and see if a tex exists
                    for item in items:
                        retex_key = '%s%s' % (tex_key, item)
                        if retex_key in self.textures:
                            self.texture_name = retex_key
                            self.image = self.textures[self.texture_name]
                            return
        self.texture_name = self._default_texture
        self.image = self.textures[self.texture_name]


# -*- coding: utf-8 -*-
import pandas as pd

def DropCols():
    for num in range(1,2,1):
        print("now is dropping columns in---"+"HO_",num,".csv")
        df=pd.read_csv("HO_{}.csv".format(str(num) ) )

        #df=df.drop['Unnamed: 0',1]

        x=[0]
        df.drop(df.columns[x],axis=1,inplace=True)

        print('save start---')

        #date_1[['mobile', 'plan_id']].to_csv(output_file, sep=',', header=True,index=False)
        df=df.to_csv("HO_{}.csv".format(str(num)))
        print('save end')

def GetCols():
     for num in range(8,9,1):
        print("now is showing---",num)
        df=pd.read_csv("HO_{}.csv".format(str(num) ) )
        cols=df.columns.values.tolist()
        #cols=df.columns[0]
        print(cols)
        print(df)

if __name__ =="__main__":
    #DropCols()
    GetCols()


class CalibrationAlert:
    def __init__(
            self,
            device_id,
            command,
            session,
            step,
            direction,

    ):
        self.device_id = device_id
        self.command = command
        self.session = session
        self.step = step
        self.direction = direction

    def prepare_to_send(self):
        return {
            "device_id": self.device_id,
            "command": self.command,
            "session": self.session,
            "step": self.step,
            "direction": self.direction
        }


import numpy as np
import pandas as pd
import datetime as dt

import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func, inspect

from flask import Flask, jsonify

app = Flask(__name__)

engine = create_engine("sqlite:///Resources/hawaii.sqlite")
Base = automap_base()
Base.prepare(engine, reflect=True)

measurement = Base.classes.measurement
station = Base.classes.station

session = Session(engine)

@app.route("/")
def home():

    return (
        f"Welcome to Sofie's OFFICIAL Climate App API!
"
        f"
"
        f"Available Routes are:
"
        f"/api/v1.0/precipitation
"
        f"/api/v1.0/stations
"
        f"/api/v1.0/tobs
"
        f"/api/v1.0/start (start date must be in mm-dd format)
"
        f"/api/v1.0/start/end (start & end dates must be in yyyy-mm-dd format)
"
        f"
"
        f"May Your Days Be Bright & Sunny, but Your Hair NEVER Frizzy!"
    )

@app.route("/api/v1.0/precipitation")
def precipitation():
    lastDate = session.query(func.max(measurement.date)).all()[0][0]
    lastDate = dt.datetime.strptime(lastDate, '%Y-%m-%d')
    priorYear = lastDate - dt.timedelta(365)
    result = session.query(measurement.date, measurement.prcp).filter(measurement.date>=priorYear).all()
    precipitation = []
    for date, prcp in result:
        precipitation_dict = {}
        precipitation_dict["date"] = date
        precipitation_dict["prcp"] = prcp
        precipitation.append(precipitation_dict)

    return jsonify(precipitation)

@app.route("/api/v1.0/stations")
def stations():
    results = session.query(station.station,station.name)\
    .group_by(station.name)\
    .order_by(station.name)\
    .all()

    stations = list(np.ravel(results))

    return jsonify(stations)

@app.route("/api/v1.0/tobs")
def TObs():
    lastDate = session.query(func.max(measurement.date)).all()[0][0]
    lastDate = dt.datetime.strptime(lastDate, '%Y-%m-%d')
    priorYear = lastDate - dt.timedelta(365)
    results = session.query(measurement.tobs, measurement.date)\
    .filter(measurement.station == 'USC00519281', measurement.date>=priorYear).all()

    TObs = list(np.ravel(results))

    return jsonify(TObs)

@app.route("/api/v1.0/<start>")
def start(start):
    tmin = func.min(measurement.tobs)
    tavg = func.avg(measurement.tobs)
    tmax = func.max(measurement.tobs)
    sel = [tmin, tavg, tmax]
    result = session.query(*sel).filter(func.strftime("%m-%d", measurement.date) >= start).all()
    start = []
    for tmin, tavg, tmax in result:
        start_dict = {}
        start_dict["tmin"] = tmin
        start_dict["tavg"] = tavg
        start_dict["tmax"] = tmax
        start.append(start_dict)

    return jsonify(start)

@app.route("/api/v1.0/<start>/<end>")
def SnE(start, end):
    tmin = func.min(measurement.tobs)
    tavg = func.avg(measurement.tobs)
    tmax = func.max(measurement.tobs)
    sel = [tmin, tavg, tmax]
    result = session.query(*sel).filter(measurement.date >= start).filter(measurement.date <= end).all()
    end = []
    for tmin, tavg, tmax in result:
        end_dict = {}
        end_dict["tmin"] = tmin
        end_dict["tavg"] = tavg
        end_dict["tmax"] = tmax
        end.append(end_dict)

    return jsonify(end)

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


import random

###############################################################################
#       Name: recombine
#
#       Assumption: None
#
#       Purpose: Recombine takes two lists and change the contents of a third
#		 to corresponding chunks of the first two lists. This is done
#		 by copying over elements from either the first or second list
#		 to the third in a random fashion.
#
#       Arguments: recombined - the array that is made of chunks of the
#				first two arrays#
#
#		   listOne - the 1st list that contributes to the recombined
#			     list
#
#		   listTwo - the 2nd list that contributes to the recombined
#			     list
#
#       Returns: Nothing
#
###############################################################################
def recombine(sites, listOne, listTwo, dest, length):
	# Choose the indices where the segments will alternate
	indices = [0, length] + [int(random.random() * length) for i in range(sites)]
	indices.sort()

	# Build the new list. Take a section from listTwo first.
	for i in range(sites + 1):
		if random.random() < .5:
			dest[indices[i]:indices[i + 1]] = listOne[indices[i]:indices[i + 1]]
		else:
			dest[indices[i]:indices[i + 1]] = listTwo[indices[i]:indices[i + 1]]

################################################################################
#       Name: myCopy
#
#       Assumptions: It is assumed that lists passed to this function will
#                    only integers, lists, or boolean types.
#
#       Purpose: myCopy makes a copy of a list by recursively appending each
#                item in the original list to a new list.
#
#       Arguements: origList is a the list to be copied
#
#       Returns: Nothing if the list contains a type that is not one of the
#                three stated in the assumptions. If the list contanins valid
#                types, then a copy of the original list is returned.
################################################################################
def myCopy(origList):
	newList = []
	for i in range(len(origList)):
    	# Append if the item in the list is an integer, string,
     	# or boolean
		if type(origList[i]) is int or type(origList[i]) is str or type(origList[i]) is bool or type(origList[i]) is float:
			newList.append(origList[i])
        # Recursively copy the item if it is a list
		elif type(origList[i]) is list:
			newList.append(myCopy(origList[i]))
        # Freak out if some other type of object is in the list
		else:
			print("Error: List contains invalid types")
			exit(1)

	return newList

##########################################################################################
#	Name: pickParents
#
#	Assumptions: It is assumed that the population has entries 0-populationSize - 1
#
#	Purpose: pickParents exists for the sake of picking parents to produce an
#		 offspring. If selection is turned off, then two distinct individuals are
#		 chosen at random to be parents. If recombination is turned on, the first
#		 two distinct individuals with a fitness higher than a random number are
#		 chosen to the parents.
#
#	Arguments: selection - a boolean corresponding to whether or not selection is
#			       turned on
#
#		   population - a list containing the individuals of the population
#
#		   fitnessIndex - the index of an individuals fitness.
#				  population[i][fitnessIndex] gives the fitness of
#				  individual i
#
#		   populationSize - the number of individuals contained the population.
#				    len(population) == populationSize
#
#	Returns: A tuple containing two distinct integers in the interval
#		 [0, populationSize - 1. Each integer corresponds to the the entry of the
#		 population containg the genetic information of a parent.
#
##########################################################################################
def pickParents(selection, population, fitnessIndex, populationSize):
	# If selection is on, discriminate against low fitness
	if selection:
		# Pick the first mom who has a fitness greater than a random number
		while True:
			mom = int(random.random() * populationSize)
			if population[mom][fitnessIndex] > random.random():
				break

		# Pick the first dad who has a fitness greater than a random number and isn't already the mom
		while True:
			dad = int(random.random() * populationSize)
			if population[dad][fitnessIndex] > random.random() and dad != mom:
				break

	# If slection is off, don't consider fitness
	else:
		# Pick a mom at random
		mom = int(random.random() * populationSize)

		# Pick the first dad who isn't already the mom
		dad = mom
		while dad == mom:
			dad = int(random.random() * populationSize)

	return (mom, dad)

##########################################################################################
#	Name: replaceDeadWithOffspring
#
#	Assumptions: It is assumed that the likelihood of getting a rho gene from either
#		     in the case of recombination is .5; it is also assumed that sections
#		     of the lDel contributed from both parents can differ in size. In the case
#		     of no recombination, it is assumed that the person who died cannot be
#                    the parent of the offspring replacing them.
#
#
#		 is off, then the offspring gets copies of each of the genes of another
#		 individual in the population who is randomly chosen. If it is on, then
#		 the offspring gets a combination of different genes from each parent and
# 		 a fitness reflecting the difference in gene.
#
#
#	Arguments: deadIndex - the index in the population list of the individual who
#			       died. This will be the index of the offspring.
#		   population - a list containing the individuals of the population and all of
#				their genetic information
#		   recombination - a boolean corresponding to whether or not recombination
#				   is allowed when developing offspring.
#
#	Returns: Nothing
#
##########################################################################################
def replaceDeadWithOffspring(deadIndex, recombination, population, populationSize, selection,
			     fitnessIndex):
	if recombination:
		# Pick two parents
		parents = pickParents(selection, population, fitnessIndex, populationSize)
		mateOne = parents[0]
		mateTwo = parents[1]

		# Randomly assign one of the rho values from the parents to the individual
		population[deadIndex][0] = population[mateOne][0]
		if random.random() < .5:
			population[deadIndex][0] = population[mateTwo][0]

		# Recombine the parents and then give the offspring the result for
		# the lDel, alpha, and beta gene of the osspring
		recombine(5, population[mateOne][1], population[mateTwo][1],
				  population[deadIndex][1], population[deadIndex][1].size)

		recombine(2, population[mateOne][3], population[mateTwo][3],
				  population[deadIndex][3], population[deadIndex][3].size)

		recombine(2, population[mateOne][4], population[mateTwo][4],
				  population[deadIndex][4], population[deadIndex][4].size)

	else:
		# Pick an individual to be the parent who isn't the person who just died
		mateOne = deadIndex
		while mateOne == deadIndex:
			mateOne = int(random.random() * populationSize)
		# Copy the the parent into the offsprings slot recursively
		population[deadIndex] = myCopy(population[mateOne])

##########################################################################################
#	Name: mutateIndividual
#
#	Assumptions: Beta mutations only occur when an lDel mutation has already occured.
#
#	Purpose: This method checks to see if there's a rho, lDel, alpha, or beta
#		 mutation according to the mutation rates giveen by the user. Let k
# 		 be the number of beta locus. If the index of an lDel mutation is less
#		 or equal to k, then the beta gene is also mutated in the beta gene. If
#		 the lDel mutation loci is greater than k, then only that lDel loci is
#		 changed.
#
#	Arguments: population - a reference to the array containing the genetic
#				information of each inividual
#
#		   plDelmutation - the probability of there being at least one mutation
#				   in the given lDel gene
#
#		   pRhoMutation - the porbability of a mutation of the read through
#				  rho
#
#		   pDelToNonDel - the probability of a given lDel loci to go from
#				  deleterious to benign
#
#		   mutantIndex - the index in the population array that holds the geneitc
#				 information on the individual to be me mutated
#
#		   pAlphaMutations - the mutation rate for a given alplha gene
#
#	Returns: Nothing
#
##########################################################################################
def mutateIndividual(mutantIndex, population, pRhoMutation, plDelMutation, pAlphaMutation,
		     pBetaMutation, pDelToNonDel, pNonDelToDel, pCooption):
	if random.random() < pRhoMutation:
		population[mutantIndex][0] *= 10**random.gauss(0, .2)

	if random.random() < plDelMutation:
		# Pick the locus to change
		betaLength = population[mutantIndex][4].size
		lDelLength = population[mutantIndex][1].size
		changeLoci = random.randint(0, lDelLength - 1)

		# The lDel locus has a corresponding beta locus
		if changeLoci < betaLength:
			mutationOccured = 0
			# Loci to be changed is a 1
			if population[mutantIndex][1][changeLoci]:
				if random.random() < pDelToNonDel:
					population[mutantIndex][1][changeLoci] = 0
					mutationOccured += 1
			# Loci to ba changed is a 0
			else:
				if random.random() < pNonDelToDel:
					population[mutantIndex][1][changeLoci] = 1
					mutationOccured += 1

			# Only mutat a beta if an lDel was changed
			if mutationOccured:
				# Add a number drawn out of a normal distribution
				population[mutantIndex][4][changeLoci] += \
				random.gauss(-1 * population[mutantIndex][4][changeLoci] / 50.0,
                		10 / float(betaLength))

				# Check for a cooption mutation
				if random.random() < pCooption:
					population[mutantIndex][3][changeLoci] += population[mutantIndex][4][changeLoci]
					population[mutantIndex][4][changeLoci] = random.gauss(0.0, 16.0 / (9.0/25.0))

		else:
			# Change an lDel locus in the same way as above
			if population[mutantIndex][1][changeLoci]:
				if random.random() < pDelToNonDel:
					population[mutantIndex][1][changeLoci] = 0
			else:
				if random.random() < pNonDelToDel:
                              		population[mutantIndex][1][changeLoci] = 1

	# THIF PART DOES ALPHA MUTATION
	if random.random() < pAlphaMutation:
		# Pick a an alpha locus to change
		alphaLength = population[mutantIndex][3].size
		changeLoci = random.randint(0, alphaLength - 1)

		# Add another number drawn from a normal distribution
		delta = random.gauss(-1 * population[mutantIndex][3][changeLoci] / 50.0,
                10 / float(alphaLength))

		population[mutantIndex][3][changeLoci] += delta


# 웹에서 검색자료 읽은 후 워드 클라우드로 출력
from bs4 import BeautifulSoup
import urllib.request
from urllib.parse import quote
from boto.dynamodb import item

#keyword = input('검색어:')
keyword = '장마'
print(keyword)
print(quote(keyword))

# 동아일보 검색 기능 사용
target_url = "http://www.donga.com/news/search?query=" + quote(keyword)
sou_code = urllib.request.urlopen(target_url)

soup = BeautifulSoup(sou_code, 'lxml', from_encoding='utf-8')
#print(soup)

########################

msg = ""
for title in soup.find_all('p', 'tit'):
    title_link = title.select('a')
    #print(title_link)
    article_url = title_link[0]['href']
    #print(article_url)

    sou_article = urllib.request.urlopen(article_url)
    soup = BeautifulSoup(sou_article,'lxml', from_encoding='utf-8')

    contents = soup.select('div.article_txt')
    for imsi in contents:
        item = str(imsi.find_all(text=True))
        #print(item)
        msg = msg + item

print(msg)

from konlpy.tag import Okt
from collections import Counter

okt = Okt()
nouns = okt.nouns(msg)
result = []
for imsi in nouns:
    if len(imsi) > 1:  # 2글자 이상만 참여
        result.append(imsi)

print(result)

count = Counter(result)
tag = count.most_common(50) # 상위 50개만 참여
print(tag)

##########################################

import pytagcloud
# (min)maxsize : 글꼴크기,
taglist = pytagcloud.make_tags(tag, maxsize=100)
print(taglist)

pytagcloud.create_tag_image(taglist, "word.png", size=(1000,600),
                            fontname="Korean", rectangular=False)

# 이미지 읽기
# import matplotlib.pylab as plt
# import matplotlib.image as mpimg
# #%matplotlib inline
# img = mpimg.imread("word.png")
# plt.imshow(img)
# plt.show()

# 이미지 브라우저로 읽기
import webbrowser
webbrowser.open("word.png")


import requests

__author__ = "Griffith Asare Awuah (@gwuah)"

class ogma():
	"""Language Detection Library For Pythonistas"""

	def __init__(self, accessKey):
		self.payload = {'access_key': str(accessKey)}

	def detect(self, phrase) :
		self.payload['query'] = str(phrase)
		try :
			r = requests.get('http://apilayer.net/api/detect', self.payload)
			self.response = r.json()
			if (r.status_code == requests.codes.ok) and (self.response['success'] != False) :
				# connection successful! You were able to get meaningful data from the endpoint
				return "{}".format(self.response['results'][0]['language_name'])
			else :
				if r.status_code[0] == 4 :
					# couldn't connect to language layer due to no inetrnet access
					print("Detection wasn't sucessful. \nThere was an error from your side. \nCheck Your Internet Connection.")
				elif r.status_code[0] == 5 :
					# Youre connected to a network, but theres no internet access
					print("Detection wasn't sucessful \nThere was an error from your server \nTry again later")
				elif (self.response['success'] == False) and (self.response['error']['code'] == 101) :
					# You didnt submit a correct payload probably
					return self.response['error']['info'][:-41]
				elif (self.response['success'] == False) and (self.response['error']['code'] == 210) :
					# You didnt submit a correct payload probably
					return self.response['error']['info'][:-43]
		except requests.exceptions.ConnectionError :
				print("Detection wasn't sucessful. \nYou are not connected to the internet Connection.")


from flask import Flask
from flask import render_template
from flask import Response, request, jsonify
app = Flask(__name__)

current_id = 4
sales = [
	{
		"id": 1,
		"salesperson": "James D. Halpert",
		"client": "Shake Shack",
		"reams": 1000
	},
	{
		"id": 2,
		"salesperson": "Stanley Hudson",
		"client": "Toast",
		"reams": 4000
	},
	{
		"id": 3,
		"salesperson": "Michael G. Scott",
		"client": "Computer Science Department",
		"reams": 10000
	},
]
clients = [
	"Shake Shack",
	"Toast",
	"Computer Science Department",
	"Teacher's College",
	"Starbucks",
	"Subsconsious",
	"Flat Top",
	"Joe's Coffee",
	"Max Caffe",
	"Nussbaum & Wu",
	"Taco Bell",
];

non_ppc_people = [
"Phyllis",
"Dwight",
"Oscar",
"Creed",
"Pam",
"Jim",
"Stanley",
"Michael",
"Kevin",
"Kelly"
]
ppc_people = [
"Angela"
]

@app.route('/infinity')
def infinity(name=None):
	return render_template('cu-paper-infinity.html', sales=sales, clients=clients)

@app.route('/ppc')
def ppc(name=None):
	return render_template('ppc.html', non_ppc_people=non_ppc_people, ppc_people=ppc_people)

@app.route('/save_sale', methods=['GET', 'POST'])
def save_sale():
	global current_id
	global sales
	global clients

	json_data = request.get_json()
	salesperson = json_data["salesperson"]
	client = json_data["client"]
	reams = json_data["reams"]

	current_id += 1
	new_sale_log = {
		"id":  current_id,
		"salesperson": salesperson,
		"client": client,
		"reams": reams
	}
	sales.append(new_sale_log)
	if client not in clients:
		clients.append(client)

	return jsonify(sales=sales, clients=clients)

@app.route('/delete_sale', methods=['GET', 'POST'])
def delete_sale():
	global sales

	delete_id = request.get_json()

	del sales[delete_id]

	return jsonify(sales=sales)

@app.route('/move_to_non_ppc', methods=['GET', 'POST'])
def move_to_non_ppc():
	global non_ppc_people
	global ppc_people

	name = request.get_json()

	if name not in non_ppc_people:
		non_ppc_people.append(name)
		ppc_people.remove(name)

	return jsonify(non_ppc_people=non_ppc_people, ppc_people=ppc_people)

@app.route('/move_to_ppc', methods=['GET', 'POST'])
def move_to_ppc():
	global non_ppc_people
	global ppc_people

	name = request.get_json()

	if name not in ppc_people:
		ppc_people.append(name)
		non_ppc_people.remove(name)

	return jsonify(non_ppc_people=non_ppc_people, ppc_people=ppc_people)

if __name__ == '__main__':
   app.run(debug = True)


'''
Created For Mega Projects Repository

Turtle Graphics - This is a common project where you create a floor of 20 x 20 squares.
Using various commands you tell a turtle to draw a line on the floor. You have move forward, left or right, lift or drop pen etc.
Do a search online for "Turtle Graphics" for more information.

@author: Sambit

'''

#imports
import turtle

#Functions

#Function to draw a square
def draw_square(some_turtle):

    """

        @param Input: Turtle to perform task
        @console Output: Square pattern

    """

    for i in range(4):
        some_turtle.forward(250)
        some_turtle.right(90)

#Function to draw different shapes
def draw_art():

    """

        @param Input: None
        @console Output: Pattern

    """
    window = turtle.Screen()
    window.bgcolor("black")

    don  = turtle.Turtle()
    mike = turtle.Turtle()

    don.shape("turtle")
    don.color("purple", "green")
    don.speed(20)

    mike.shape("turtle")
    mike.color("orange", "green")
    mike.speed(20)

    for i in range(360):
        don.circle(150)
        don.right(1)

    for i in range(360):
        draw_square(mike)
        mike.right(1)

    window.exitonclick()

draw_art()


from PIL import Image
import os
from tkinter import filedialog
import tkinter as tk

def convert_pdf():
    index = 0
    path_picture = filedialog.askdirectory()
    dire = 'Converted'
    path_pdf = os.path.join(path_picture , dire)
    os.mkdir(path_pdf)

    my_list = os.listdir(path_picture)
    for i in my_list:
        image = Image.open(r'' + path_picture+'//' + i)
        im = image.convert('RGB')
        im.save(r''+ path_pdf+'//' + i[:-4] +'.pdf', quality=15, optimze=True)
        index = index + 1

root = tk.Tk()
convert_pdf()
tk.mainloop()


import os
import random

import numpy as np
import torch
from scipy import ndimage as ndi
from torch.nn import functional as F
from torch.utils.data import Dataset

from my_utils import normalize

class UNetDataset(Dataset):
    def __init__(self, data_dir, shape, train, transform):
        self.shape = shape
        self.transform = transform
        self.sample_path = os.path.join(data_dir, 'samples')
        self.sample_files = os.listdir(self.sample_path)
        self.sample_files.sort()
        self.mask_path = os.path.join(data_dir, 'masks')
        self.train = train

    def __len__(self):
        return len(self.sample_files)

    def __getitem__(self, index):
        sample = np.load(os.path.join(self.sample_path, self.sample_files[index]))
        mask = np.load(os.path.join(self.mask_path, self.sample_files[index]))
        mask = mask.astype(np.float32)

        if int(self.sample_files[index][-6:-4]) == 0:
            rand = random.randrange(3, len(sample) - 3)
            sample = sample[rand - 3:rand + 4]
            mask = mask[rand]

        if self.transform is not None:
            sample = self.transform(sample)
            sample = np.concatenate((sample[0], sample[1]))

        if self.train:
            htranslation = random.randint(-10, 10)
            vtranslation = random.randint(-10, 10)
            angle = random.randint(-10, 10)

            sample = ndi.shift(sample, (0, htranslation, vtranslation), mode='nearest')
            sample = ndi.rotate(sample, angle, (-1, -2), mode='nearest', reshape=False)
            mask = ndi.shift(mask, (htranslation, vtranslation), mode='nearest')
            mask = ndi.rotate(mask, angle, (-1, -2), mode='nearest', reshape=False)

            if random.randint(0, 1) == 1:
                sample = np.flip(sample, -1)
                mask = np.flip(mask, -1)

        sample = torch.from_numpy(sample[np.newaxis, ...].copy())
        sample = F.interpolate(sample, self.shape, mode='bilinear', align_corners=False)
        mask = torch.from_numpy(mask[np.newaxis, np.newaxis, ...].copy())
        mask = F.interpolate(mask, self.shape, mode='nearest')
        mask2 = F.interpolate(mask, scale_factor=0.5, mode='nearest', recompute_scale_factor=False)
        mask3 = F.interpolate(mask, scale_factor=0.25, mode='nearest', recompute_scale_factor=False)

        return sample[0], mask[0], mask2[0], mask3[0]

class GenesisDataset2D(Dataset):
    def __init__(self, data_dir, shape, transform, flip_rate):
        self.shape = shape
        self.transform = transform
        self.flip_rate = flip_rate
        self.sample_path = os.path.join(data_dir, 'samples')
        self.sample_files = os.listdir(self.sample_path)
        self.sample_files.sort()

    def __len__(self):
        return len(self.sample_files)

    def __getitem__(self, index):
        x = np.load(os.path.join(self.sample_path, self.sample_files[index]))

        rand = random.randrange(3, len(x) - 3)
        x = x[rand - 3:rand + 4]

        if random.random() < self.flip_rate:
            x = np.flip(x, -1)

        x = normalize(x)
        x = np.concatenate((x[0], x[1]))
        x = ndi.zoom(x, (1, self.shape[0] / x.shape[1], self.shape[1] / x.shape[2]), order=2, mode="nearest")

        y = self.transform(x)

        return torch.from_numpy(y.copy().astype(np.float32)), torch.from_numpy(x.copy().astype(np.float32))

class PPos2DDataset(Dataset):
    def __init__(self, data_dir, shape, num_classes, transform):
        self.shape = shape
        self.transform = transform
        self.sample_path = os.path.join(data_dir, 'samples')
        self.sample_files = os.listdir(self.sample_path)
        self.sample_files.sort()
        self.num_classes = num_classes

    def __len__(self):
        return len(self.sample_files)

    def __getitem__(self, index):
        sample = np.load(os.path.join(self.sample_path, self.sample_files[index]))

        rand = random.randrange(3, len(sample) - 3)
        target = (rand - 3) / (len(sample) - 6)
        sample = sample[rand - 3:rand + 4]

        if self.transform is not None:
            sample = self.transform(sample)
            sample = np.concatenate((sample[0], sample[1]))

        return torch.from_numpy(sample), torch.tensor([target])

class UNetClassifierDataset(Dataset):
    def __init__(self, data_dir, train, transform):
        self.transform = transform
        self.sample_path = os.path.join(data_dir, 'samples')
        self.sample_files = os.listdir(self.sample_path)
        self.sample_files.sort()
        self.mask_path = os.path.join(data_dir, 'masks')
        self.train = train

    def __len__(self):
        return len(self.sample_files)

    def __getitem__(self, index):
        sample = np.load(os.path.join(self.sample_path, self.sample_files[index]))
        mask = np.load(os.path.join(self.mask_path, self.sample_files[index]))
        mask = mask.astype(np.float32)

        if self.transform is not None:
            sample = self.transform(sample)

        if self.train:
            htranslation = random.randint(-10, 10)
            vtranslation = random.randint(-10, 10)
            dtranslation = random.randint(-2, 2)
            angle = random.randint(-10, 10)

            sample = ndi.shift(sample, (0, dtranslation, htranslation, vtranslation), mode='nearest')
            sample = ndi.rotate(sample, angle, (-1, -2), mode='nearest', reshape=False)
            mask = ndi.shift(mask, (dtranslation, htranslation, vtranslation), mode='nearest')
            mask = ndi.rotate(mask, angle, (-1, -2), mode='nearest', reshape=False)

            if random.randint(0, 1) == 1:
                sample = np.flip(sample, -1)
                mask = np.flip(mask, -1)

        mask2 = ndi.zoom(mask, 0.5, order=0, mode='nearest')
        mask3 = ndi.zoom(mask, 0.25, order=0, mode='nearest')

        return torch.from_numpy(sample.copy()), torch.from_numpy(mask[np.newaxis, ...].copy()), torch.from_numpy(
            mask2[np.newaxis, ...].copy()), torch.from_numpy(mask3[np.newaxis, ...].copy()), torch.tensor(
            [self.sample_files[index][:5].isdigit()], dtype=torch.float)

        # return torch.from_numpy(sample.copy()), torch.from_numpy(mask[np.newaxis, ...].copy()), torch.from_numpy(
        #     mask2[np.newaxis, ...].copy()), torch.from_numpy(mask3[np.newaxis, ...].copy()), torch.tensor(
        #     [self.sample_files[index][6:11].isdigit()], dtype=torch.float)

class ClassifierDataset(Dataset):
    def __init__(self, data_dir, shape, train, transform=None):
        self.shape = shape
        self.train = train
        self.transform = transform
        self.sample_path = os.path.join(data_dir, 'samples')
        self.sample_files = os.listdir(self.sample_path)
        self.sample_files.sort()

        if train:
            self.mask_path = os.path.join(data_dir, 'masks')

    def __len__(self):
        return len(self.sample_files)

    def __getitem__(self, index):
        sample = np.load(os.path.join(self.sample_path, self.sample_files[index]))
        if self.train and self.sample_files[index][-5] == '1':
            mask = np.load(os.path.join(self.mask_path, self.sample_files[index]))
            indices = mask.nonzero()
            nodule_length = [0, 0, 0]
            scale_length = [0, 0, 0]
            for i in range(3):
                start = np.min(indices[i])
                end = np.max(indices[i]) + 1
                nodule_length[i] = end - start
            while True:
                for i in range(3):
                    while True:
                        scale_length[i] = round(nodule_length[i] * random.uniform(1, 3))
                        if scale_length[i] < sample.shape[i]:
                            break
                depth = random.randint(0, sample.shape[0] - scale_length[0])
                height = random.randint(0, sample.shape[1] - scale_length[1])
                width = random.randint(0, sample.shape[2] - scale_length[2])
                if depth > np.max(indices[0]) or depth + scale_length[0] < np.min(indices[0]) or height > np.max(
                        indices[1]) or height + \
                        scale_length[1] < np.min(indices[1]) or width > np.max(indices[2]) or width + scale_length[2] < \
                        np.min(indices[2]):
                    sample = sample[depth:depth + scale_length[0], height:height + scale_length[1],
                             width:width + scale_length[2]]
                    break

        if self.transform is not None:
            sample = self.transform(sample)

        sample = torch.from_numpy(sample[np.newaxis, ...].copy())
        sample = F.interpolate(sample, self.shape, mode='trilinear', align_corners=True)

        return sample[0], torch.tensor([self.sample_files[index][-5] == '0'], dtype=torch.float)

class PCL2DDataset(Dataset):
    def __init__(self, data_dir, shape, transform):
        self.shape = shape
        self.transform = transform
        self.sample_path = os.path.join(data_dir, 'samples')
        self.sample_files = os.listdir(self.sample_path)
        self.sample_files.sort()

    def __len__(self):
        return len(self.sample_files)

    def __getitem__(self, index):
        sample = np.load(os.path.join(self.sample_path, self.sample_files[index]))

        rand = random.randrange(3, len(sample) - 3)
        slice_position = (rand - 3) / (len(sample) - 6)
        partition = int((rand - 3) / (len(sample) - 6) * 4) + 1
        sample = sample[rand - 3:rand + 4]

        img1 = self.transform(sample)
        img2 = self.transform(sample)
        img1 = np.concatenate((img1[0], img1[1]))
        img2 = np.concatenate((img2[0], img2[1]))

        return torch.from_numpy(img1), torch.from_numpy(img2), torch.tensor(slice_position), torch.tensor(partition)


"""
Script Description
    - train NADA model
Usage
    - $ python train_nada.py --config_path [config_path] --name [exp_name] --suppress
    - $ cat [config_path] | python train_nada.py --pipe --name [exp_name] --suppress
Author
    - Minsu Kim
    - Dongha Kim
History
    - 230419 : MINSU , init
        - adaptation loop
        - code skeleton
    - 230422 : MINSU , implement
        - code corresponding to GET3D application 4.3.2
    - 230422 : DONGHA , convert as distributed script
Reference
    - StyleGAN-NADA Github
        https://github.com/rinongal/StyleGAN-nada/blob/main/ZSSGAN/train.py
"""

import sys
import os

import time
import tempfile
import yaml
import numpy as np
import torch
from torchvision.utils import save_image
import logging

import dist_util
from model_engine import find_get3d
from nada import YAIverseGAN
from functional import unfreeze_generator_layers, generate_custom

if find_get3d():
    from torch_utils import custom_ops

_SEED = 0
_SELECT = 50

def get_logger(exp_name, outdir, rank=0):
    logger = logging.getLogger(exp_name)
    if rank != 0:
        logger.disabled = True
    else:
        logger.setLevel(logging.DEBUG)
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        stream_handler = logging.StreamHandler()
        stream_handler.setFormatter(formatter)
        stream_handler.setLevel(logging.DEBUG)
        logger.addHandler(stream_handler)
        file_handler = logging.FileHandler(f'{outdir}/{exp_name}_{time.strftime("%Y-%m-%d-%H-%M", time.gmtime())}.log')
        file_handler.setFormatter(formatter)
        file_handler.setLevel(logging.DEBUG)
        logger.addHandler(file_handler)
    return logger

def subprocess_fn(rank, config, args, temp_dir):

    if config['GLOBAL']['gpus'] > 1:
        dist_util.setup_dist(temp_dir, rank, config['GLOBAL']['gpus'])

    if rank != 0:
        custom_ops.verbosity = 'none'

    if rank == 0:
        print("START ! EXP NAME : ", args.name)
        print("SETTING : LOAD YaiverseGAN")

    with dist_util.synchronized_ops():
        net = YAIverseGAN(config)
        unfreeze_generator_layers(net.generator_trainable, [], [])

    if dist_util.get_world_size() > 1:
        ddp_net = torch.nn.parallel.DistributedDataParallel(
            net,
            device_ids=[dist_util.dev()],
            output_device=dist_util.dev(),
            broadcast_buffers=True,
            bucket_cap_mb=256,
            find_unused_parameters=True,
        )
    else:
        ddp_net = net

    device, outdir, batch, n_vis, sample_1st, sample_2nd, iter_1st, iter_2nd, lr, \
        output_interval, save_interval, gradient_clip_threshold = net.get_loop_settings()

    g_optim = torch.optim.Adam(
        net.generator_trainable.parameters(),
        lr=lr,
        betas=(0.9, 0.99),
    )

    with dist_util.synchronized_ops():
        if rank == 0:
            sample_dir = os.path.join(outdir, "sample")
            ckpt_dir = os.path.join(outdir, "checkpoint")
            os.makedirs(outdir, exist_ok=True)
            os.makedirs(sample_dir, exist_ok=True)
            os.makedirs(ckpt_dir, exist_ok=True)

    torch.manual_seed(_SEED)
    np.random.seed(_SEED)

    logger = get_logger(args.name, outdir, rank=rank)
    logger.info(f'EXP NAME : {args.name} | CONFIG : {args.config_path}  | SEED : {_SEED} | BATCH : {batch}')

    z_dim = 512  # Fixed value
    fixed_z_geo = torch.randn(n_vis, z_dim, device=device)  # for eval
    fixed_z_tex = torch.randn(n_vis, z_dim, device=device)
    grid_rows = int(n_vis ** 0.5)

    eval_camera = net.generator_frozen.synthesis.generate_rotate_camera_list(n_batch=1)[4].repeat(n_vis, 1, 1, 1)
    # ------------------ Training 1st --------------

    # latent z should be 2 -> for geo , tex
    # different n_batch latents per gpu <- equals: seeing n_batch * n_gpu latents
    latent_generator = torch.Generator(device)
    latent_generator.manual_seed(rank)
    sample_z_geo = torch.randn(sample_1st, z_dim, device=device, generator=latent_generator)
    sample_z_tex = torch.randn(sample_1st, z_dim, device=device, generator=latent_generator)

    sample_z_geo_chunks = torch.split(sample_z_geo, batch, dim=0)
    sample_z_tex_chunks = torch.split(sample_z_tex, batch, dim=0)
    logger.info(f'START TRAINING LOOP')

    min_loss_store = []

    for epoch in range(iter_1st):
        for i, (z_geo_chunk, z_tex_chunk) in enumerate(zip(sample_z_geo_chunks, sample_z_tex_chunks)):
            # training
            ddp_net.train()

            # memory-efficient forward : support n_view rendering
            _, loss = ddp_net(z_tex_chunk, z_geo_chunk)

            if epoch == iter_1st - 1:  # to choose 50 latents with low loss value
                loss_val = loss.cpu().detach().numpy().tolist()
                min_loss_store += loss_val

            loss = loss.mean()
            ddp_net.zero_grad()
            loss.backward()

            if gradient_clip_threshold == -1:
                pass
            else:
                torch.nn.utils.clip_grad_norm_(net.generator_trainable.parameters(), gradient_clip_threshold)

            g_optim.step()
            logger.info(f'EPOCH : {epoch} | STEP : {i:0>4} | LOSS : {loss:.5f}')

            # evaluation & save results | save checkpoints
            with dist_util.synchronized_ops():
                if rank == 0:
                    if i % output_interval == 0:
                        ddp_net.eval()
                        with torch.no_grad():
                            sampled_dst, _ = generate_custom(
                                net.generator_trainable,
                                fixed_z_tex, fixed_z_geo,
                                use_mapping=True, mode='layer', camera=eval_camera
                            )

                        rgb = sampled_dst[:, :-1]
                        mask = sampled_dst[:, -1:]
                        bg = torch.ones(rgb.shape, device=device)
                        bg *= 0.0001    # for better background
                        new_dst = rgb*mask + bg*(1-mask)

                        save_image(
                            new_dst,
                            os.path.join(sample_dir, f"Iter1st_Epoch-{epoch}_Step-{i:0>4}.png"),
                            nrow=grid_rows,
                            normalize=True,
                            range=(-1, 1),
                        )
                        logger.info(f'ITER 1st | EPOCH : {epoch} | STEP : {i:0>4} | >> Save images ...')

                    if i % save_interval == 0 and not args.suppress:
                        torch.save(
                            {
                                "g_ema": net.generator_trainable.state_dict(),
                                "g_optim": g_optim.state_dict(),
                            },
                            f"{ckpt_dir}/Iter1st_Epoch-{epoch}_Step-{i:0>4}.pt",
                        )
                        logger.info(f'ITER 1st | EPOCH : {epoch} | STEP : {i:0>4} | >> Save checkpoint ...')

            torch.cuda.empty_cache()  # added

            dist_util.barrier()

    logger.info(f"SELCT TOP {_SELECT} Latents")
    # min_topk_val, min_topk_idx = torch.topk(torch.tensor(min_loss_store), _SELECT) #previous
    min_topk_val, min_topk_idx = torch.topk(torch.tensor(min_loss_store), _SELECT, largest=False)
    print("SELECT : ", min_topk_val, min_topk_idx)

    # ------------------ Training 2nd --------------

    selected_z_geo = sample_z_geo[min_topk_idx]
    selected_z_tex = sample_z_tex[min_topk_idx]

    selected_z_geo_chunks = torch.split(selected_z_geo, batch, dim=0)
    selected_z_tex_chunks = torch.split(selected_z_tex, batch, dim=0)

    min_loss = 1000

    for epoch in range(iter_2nd):
        for i, (z_geo_chunk, z_tex_chunk) in enumerate(zip(selected_z_geo_chunks, selected_z_tex_chunks)):
            # training
            ddp_net.train()

            _, loss = ddp_net(z_tex_chunk, z_geo_chunk)

            loss = loss.mean()
            ddp_net.zero_grad()
            loss.backward()

            if gradient_clip_threshold == -1:
                pass
            else:
                torch.nn.utils.clip_grad_norm_(net.generator_trainable.parameters(), gradient_clip_threshold)

            logger.info(f'ITER 2nd | EPOCH : {epoch} | STEP : {i:0>4} | LOSS : {loss:.5f}')

            # evaluation & save results | save checkpoints
            with dist_util.synchronized_ops():
                if rank == 0:
                    if (i == len(selected_z_geo_chunks) - 1) and (epoch == iter_2nd - 1):
                        torch.save(
                            {
                                "g_ema": net.generator_trainable.state_dict(),
                                "g_optim": g_optim.state_dict(),
                            },
                            f"{ckpt_dir}/latest.pt",
                        )

                    if i % output_interval == 0:
                        ddp_net.eval()

                        with torch.no_grad():
                            sampled_dst, _ = generate_custom(
                                net.generator_trainable,
                                fixed_z_tex, fixed_z_geo, use_mapping=True, mode='layer', camera=eval_camera
                            )

                        rgb = sampled_dst[:, :-1]
                        mask = sampled_dst[:, -1:]
                        bg = torch.ones(rgb.shape, device=device)
                        bg *= 0.0001    # for better background
                        new_dst = rgb*mask + bg*(1-mask)

                        save_image(
                            new_dst,
                            os.path.join(sample_dir, f"Iter2nd_Epoch-{epoch}_Step-{i:0>4}.png"),
                            nrow=grid_rows,
                            normalize=True,
                            range=(-1, 1),
                        )

                        logger.info(f'ITER 2nd | EPOCH : {epoch} | STEP : {i:0>4} | >> Save images ...')

                    if i % save_interval == 0:
                        if not args.suppress:
                            torch.save(
                                {
                                    "g_ema": net.generator_trainable.state_dict(),
                                    "g_optim": g_optim.state_dict(),
                                },
                                f"{ckpt_dir}/Iter2nd_Epoch-{epoch}_Step-{i:0>4}.pt",
                            )

                            logger.info(f'ITER 2nd | EPOCH : {epoch} | STEP : {i:0>4} | >> Save checkpoint ...')

                        if loss < min_loss:
                            min_loss = loss
                            torch.save(
                                    {
                                        "g_ema": net.generator_trainable.state_dict(),
                                        "g_optim": g_optim.state_dict(),
                                    },
                                    f"{ckpt_dir}/best.pt",
                                )

            torch.cuda.empty_cache()
            dist_util.barrier()

    logger.info("TRAINING DONE ...")

    # Check final results
    with dist_util.synchronized_ops():
        if rank == 0:
            net.eval()

            with torch.no_grad():
                last_z_geo = torch.randn(n_vis, z_dim, device=device)
                last_z_tex = torch.randn(n_vis, z_dim, device=device)
                sampled_dst, _ = generate_custom(
                    net.generator_trainable,
                    last_z_tex, last_z_geo, use_mapping=True, mode='layer', camera=eval_camera
                )

            save_image(
                sampled_dst,
                os.path.join(sample_dir, "params_latest_images.png"),
                nrow=grid_rows,
                normalize=True,
                range=(-1, 1),
            )

    logger.info("FINISH !")

def launch_training(args):  # Multiprocessing spawning function
    # Load config and parse the number of GPUs.
    if args.pipe:
        config = yaml.safe_load(sys.stdin)
    else:
        with open(args.config_path, 'r') as f:
            config = yaml.safe_load(f)
    gpus = config['GLOBAL']['gpus']

    # In case of single GPU, directly call the training function.
    if gpus == 1:
        subprocess_fn(0, config, args, None)
        return

    # Otherwise, launch processes.
    print('Launching processes...')
    torch.multiprocessing.set_start_method('spawn', force=True)
    with tempfile.TemporaryDirectory() as temp_dir:
        torch.multiprocessing.spawn(fn=subprocess_fn, args=(config, args, temp_dir), nprocs=gpus)

def parse_args():
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('--config_path', type=str, default='experiments/default_dist.yaml')
    parser.add_argument('--name', type=str, default='default_dist')
    parser.add_argument('--pipe', action='store_true', help='read config from stdin instead of file')
    parser.add_argument('--suppress', action='store_true')
    return parser.parse_args()

if __name__ == '__main__':
    launch_training(parse_args())


import sys
import random

class HashSolution:
	def __init__(self, string):
		# Store string input and length
		self.string = string
		self.string_len = len(string) + 1

		# Initialize hash tables
		self.hash_table_1 = [0] * self.string_len
		self.hash_table_2 = [0] * self.string_len

		# Initialize coef tables
		self.coef_table_1 = [1] * self.string_len
		self.coef_table_2 = [1] * self.string_len

		# Initialize modulo primes and multiplier x
		self.power = 10 ** 9
		self.mod_1 = self.power + 7
		self.mod_2 = self.power + 9
		self.x = random.randint(1, self.power - 1)

		# Initialize blank query inputs
		self.a_index = -1
		self.b_index = -1
		self.sub_len = -1

		# Precompute string hashes and coefficients for both modulos
		self.precompute_hashes_coefs()

	def precompute_hashes_coefs(self):
		for i in range(1, self.string_len):
			# Calculate the hash for each substring, growing in size from first letter
			self.hash_table_1[i] = (self.x * self.hash_table_1[i-1] + ord(self.string[i-1])) % self.mod_1
			self.hash_table_2[i] = (self.x * self.hash_table_2[i-1] + ord(self.string[i-1])) % self.mod_2

			# Large number exponents are time-consuming. Solution is to store the modulo of the coefficient at each step
			self.coef_table_1[i] = (self.coef_table_1[i-1] * self.x) % self.mod_1
			self.coef_table_2[i] = (self.coef_table_2[i-1] * self.x) % self.mod_2

	def query_input(self, a_i, b_i, length):
		# Store query input values
		self.a_index = a_i
		self.b_index = b_i
		self.sub_len = length

	def calc_substring_hash(self, hash_table, prime, coef_table):
		# Calculate the substring hash using some polynomial properties (i.e. by subtracting away common parts)
		a_hash = hash_table[self.a_index + self.sub_len] - (coef_table[self.sub_len] * hash_table[self.a_index])
		b_hash = hash_table[self.b_index + self.sub_len] - (coef_table[self.sub_len] * hash_table[self.b_index])

		# Used to get a clean modulo answer in case of negative hash values
		a_hash = (a_hash + prime) % prime
		b_hash = (b_hash + prime) % prime
		return a_hash == b_hash

	def check_substrings(self):
		# Use two different hash checks to ensure strings match (very low probability of collisions with two hashes)
		if self.calc_substring_hash(self.hash_table_1, self.mod_1, self.coef_table_1):
			if self.calc_substring_hash(self.hash_table_2, self.mod_2, self.coef_table_2):
				print("Yes")
			else:
				print("No")
		else:
			print("No")

class SolverNaive:
	def __init__(self, s):
		self.s = s

	def ask(self, a, b, l):
		return s[a:a+l] == s[b:b+l]

s = sys.stdin.readline()
q = int(sys.stdin.readline())

hashbrowns = HashSolution(s)
for _ in range(q):
	a, b, l = map(int, sys.stdin.readline().split())
	hashbrowns.query_input(a, b, l)
	hashbrowns.check_substrings()

# naive_solver = SolverNaive(s)
# for _ in range(q):
# 	a, b, l = map(int, sys.stdin.readline().split())
# 	print("Yes" if naive_solver.ask(a, b, l) else "No")


from replit import clear
print("Welcome to the secret auction program!")

def highest_bidder(bid_record):
  highest = 0
  winner = ""
  for bidder in bid_record:
    bid_amount = bid_record[bidder]
    if bid_amount > highest:
      highest = bid_amount
      winner = bidder
  print(f"The winner is {winner} with a bid of ${highest}")

mapp = {}

restart = True
while restart:
  name = input("What is your name?: ")
  bid = int(input("What is your bid?: $"))
  mapp[name] = bid
  other_bidders = input("Are there any other bidders? Type 'Yes' or 'No'").lower()
  if other_bidders == "yes":
    restart = True
    clear()
  else:
    restart = False
    highest_bidder(mapp)


def is_valid(board, row):
    if row in board:
        return False

    column = len(board)
    for occupied_column, occupied_row in enumerate(board):
        if abs(occupied_row - row) == abs(occupied_column - column):
            return False

    return True

def n_queens(n, board=[]):
    if n == len(board):
        return board

    for row in range(n):
        if is_valid(board, row):
            res = n_queens(n, board + [row])
            if res:
                return res

    return None

def main():
    res = n_queens(5)
    result = []
    for row, col in enumerate(res):
        result.append((row,col))
    assert result == [(0, 0), (1, 2), (2, 4), (3, 1), (4, 3)]

# Q . . . .
# . . . Q .
# . Q . . .
# . . . . Q
# . . Q . .

if __name__ == '__main__':
    main()


import os
import math
import glob
import time
import random
import torch

from PIL import Image
from torch.utils import data
from torchvision.transforms import RandomCrop

import numpy as np
import core.io as io
import core.clip_utils as cu
import multiprocessing as mp

class CachedAVSource(data.Dataset):
    def __init__(self):
        # Cached data
        self.entity_data = {}
        self.speech_data = {}
        self.entity_list = []

        #Reproducibilty
        random.seed(42)
        np.random.seed(0)

    def _postprocess_speech_label(self, speech_label):
        speech_label = int(speech_label)
        if speech_label == 2:  # Remember 2 = SPEAKING_NOT_AUDIBLE
            speech_label = 0
        return speech_label

    def _postprocess_entity_label(self, entity_label):
        entity_label = int(entity_label)
        if entity_label == 2:  # Remember 2 = SPEAKING_NOT_AUDIBLE
            entity_label = 0
        return entity_label

    def _cache_entity_data(self, csv_file_path):
        entity_set = set()

        csv_data = io.csv_to_list(csv_file_path)
        csv_data.pop(0)  # CSV header
        for csv_row in csv_data:
            video_id = csv_row[0]
            entity_id = csv_row[-3]
            timestamp = csv_row[1]

            speech_label = self._postprocess_speech_label(csv_row[-2])
            entity_label = self._postprocess_entity_label(csv_row[-2])
            minimal_entity_data = (entity_id, timestamp, entity_label)

            # Store minimal entity data
            if video_id not in self.entity_data.keys():
                self.entity_data[video_id] = {}
            if entity_id not in self.entity_data[video_id].keys():
                self.entity_data[video_id][entity_id] = []
                entity_set.add((video_id, entity_id))
            self.entity_data[video_id][entity_id].append(minimal_entity_data)

            #Store speech meta-data
            if video_id not in self.speech_data.keys():
                self.speech_data[video_id] = {}
            if timestamp not in self.speech_data[video_id].keys():
                self.speech_data[video_id][timestamp] = speech_label

            #max operation yields if someone is speaking.
            new_speech_label = max(self.speech_data[video_id][timestamp], speech_label)
            self.speech_data[video_id][timestamp] = new_speech_label

        return entity_set

    def _cache_entity_data_forward(self, csv_file_path, target_video):
        entity_list = list()

        csv_data = io.csv_to_list(csv_file_path)
        csv_data.pop(0)  # CSV header
        for csv_row in csv_data:
            video_id = csv_row[0]
            if video_id != target_video:
                continue

            entity_id = csv_row[-3]
            timestamp = csv_row[1]
            entity_label = self._postprocess_entity_label(csv_row[-2])

            entity_list.append((video_id, entity_id, timestamp))
            minimal_entity_data = (entity_id, timestamp, entity_label) # sfate to ingore label here

            if video_id not in self.entity_data.keys():
                self.entity_data[video_id] = {}

            if entity_id not in self.entity_data[video_id].keys():
                self.entity_data[video_id][entity_id] = []
            self.entity_data[video_id][entity_id].append(minimal_entity_data)

        return entity_list

    def _entity_list_postprocessing(self, entity_set):
        print('Initial', len(entity_set))

        # filter out missing data on disk
        all_disk_data = set(os.listdir(self.video_root))
        for video_id, entity_id in entity_set.copy():
            if entity_id not in all_disk_data:
                entity_set.remove((video_id, entity_id))
        print('Pruned not in disk', len(entity_set))
        self.entity_list = sorted(list(entity_set))

class AudioVideoDatasetAuxLosses(CachedAVSource):
    def __init__(self, audio_root, video_root, csv_file_path, clip_lenght,
                 target_size, video_transform=None, do_video_augment=False):
        super().__init__()

        # Data directories
        self.audio_root = audio_root
        self.video_root = video_root

        # Post-processing
        self.video_transform = video_transform
        self.do_video_augment = do_video_augment

        # Clip arguments
        self.clip_lenght = clip_lenght
        self.half_clip_length = math.floor(self.clip_lenght/2)
        self.target_size = target_size

        entity_set = self._cache_entity_data(csv_file_path)
        self._entity_list_postprocessing(entity_set)

    def __len__(self):
        return int(len(self.entity_list)/1)

    def __getitem__(self, index):
        #Get meta-data
        video_id, entity_id = self.entity_list[index]
        entity_metadata = self.entity_data[video_id][entity_id]

        audio_offset = float(entity_metadata[0][1])
        mid_index = random.randint(0, len(entity_metadata)-1)
        midone = entity_metadata[mid_index]
        target = int(midone[-1])
        target_audio = self.speech_data[video_id][midone[1]]

        clip_meta_data = cu.generate_clip_meta(entity_metadata, mid_index,
                                               self.half_clip_length)
        video_data, audio_data = io.load_av_clip_from_metadata(clip_meta_data,
                                 self.video_root, self.audio_root, audio_offset,
                                 self.target_size)

        if self.do_video_augment:
            # random flip
            if bool(random.getrandbits(1)):
                video_data = [s.transpose(Image.FLIP_LEFT_RIGHT) for s in video_data]

            # random crop
            width, height = video_data[0].size
            f = random.uniform(0.5, 1)
            i, j, h, w = RandomCrop.get_params(video_data[0], output_size=(int(height*f), int(width*f)))
            video_data = [s.crop(box=(j, i, w, h)) for s in video_data]

        if self.video_transform is not None:
            video_data = [self.video_transform(vd) for vd in video_data]

        video_data = torch.cat(video_data, dim=0)
        return (np.float32(audio_data), video_data), target, target_audio

class AudioVideoDatasetAuxLossesForwardPhase(CachedAVSource):
    def __init__(self, target_video, audio_root, video_root, csv_file_path, clip_lenght,
                 target_size, video_transform=None, do_video_augment=False):
        super().__init__()

        # Data directories
        self.audio_root = audio_root
        self.video_root = video_root

        # Post-processing
        self.video_transform = video_transform
        self.do_video_augment = do_video_augment
        self.target_video = target_video

        # Clip arguments
        self.clip_lenght = clip_lenght
        self.half_clip_length = math.floor(self.clip_lenght/2)
        self.target_size = target_size

        self.entity_list = self._cache_entity_data_forward(csv_file_path, self.target_video )
        print('len(self.entity_list)', len(self.entity_list))

    def _where_is_ts(self, entity_metadata, ts):
        for idx, val in enumerate(entity_metadata):
            if val[1] == ts:
                return idx

        raise Exception('time stamp not found')

    def __len__(self):
        return int(len(self.entity_list))

    def __getitem__(self, index):
        #Get meta-data
        video_id, entity_id, ts = self.entity_list[index]
        entity_metadata = self.entity_data[video_id][entity_id]

        audio_offset = float(entity_metadata[0][1])
        mid_index = self._where_is_ts(entity_metadata, ts)
        midone = entity_metadata[mid_index]
        gt = midone[-1]

        clip_meta_data = cu.generate_clip_meta(entity_metadata, mid_index,
                                               self.half_clip_length)
        video_data, audio_data = io.load_av_clip_from_metadata(clip_meta_data,
                                 self.video_root, self.audio_root, audio_offset,
                                 self.target_size)

        if self.do_video_augment:
            # random flip
            if bool(random.getrandbits(1)):
                video_data = [s.transpose(Image.FLIP_LEFT_RIGHT) for s in video_data]

            # random crop
            width, height = video_data[0].size
            f = random.uniform(0.5, 1)
            i, j, h, w = RandomCrop.get_params(video_data[0], output_size=(int(height*f), int(width*f)))
            video_data = [s.crop(box=(j, i, w, h)) for s in video_data]

        if self.video_transform is not None:
            video_data = [self.video_transform(vd) for vd in video_data]

        video_data = torch.cat(video_data, dim=0)
        return np.float32(audio_data), video_data, video_id, ts, entity_id, gt

#ASC Datasets
class ContextualDataset(data.Dataset):
    def get_speaker_context(self, ts_to_entity, video_id, target_entity_id,
                            center_ts, candidate_speakers):
        context_entities = list(ts_to_entity[video_id][center_ts])
        random.shuffle(context_entities)
        context_entities.remove(target_entity_id)

        if not context_entities:  # nos mamamos la lista
            context_entities.insert(0, target_entity_id)  # make sure is at 0
            while len(context_entities) < candidate_speakers:
                context_entities.append(random.choice(context_entities))
        elif len(context_entities) < candidate_speakers:
            context_entities.insert(0, target_entity_id)  # make sure is at 0
            while len(context_entities) < candidate_speakers:
                context_entities.append(random.choice(context_entities[1:]))
        else:
            context_entities.insert(0, target_entity_id)  # make sure is at 0
            context_entities = context_entities[:candidate_speakers]

        return context_entities

    def _decode_feature_data_from_csv(self, feature_data):
        feature_data = feature_data[1:-1]
        feature_data = feature_data.split(',')
        return np.asarray([float(fd) for fd in feature_data])

    def get_time_context(self, entity_data, video_id, target_entity_id,
                         center_ts, half_time_length, stride):
        all_ts = list(entity_data[video_id][target_entity_id].keys())
        center_ts_idx = all_ts.index(str(center_ts))

        start = center_ts_idx-(half_time_length*stride)
        end = center_ts_idx+((half_time_length+1)*stride)
        selected_ts_idx = list(range(start, end, stride))
        selected_ts = []
        for idx in selected_ts_idx:
            if idx < 0:
                idx = 0
            if idx >= len(all_ts):
                idx = len(all_ts)-1
            selected_ts.append(all_ts[idx])

        return selected_ts

    def get_time_indexed_feature(self, video_id, entity_id, selectd_ts):
        time_features = []
        for ts in selectd_ts:
            time_features.append(self.entity_data[video_id][entity_id][ts][0])
        return np.asarray(time_features)

    def _cache_feature_file(self, csv_file):
        entity_data = {}
        feature_list = []
        ts_to_entity = {}

        print('load feature data', csv_file)
        csv_data = io.csv_to_list(csv_file)
        for csv_row in csv_data:
            video_id = csv_row[0]
            ts = csv_row[1]
            entity_id = csv_row[2]
            features = self._decode_feature_data_from_csv(csv_row[-1])
            label = int(float(csv_row[3]))

            # entity_data
            if video_id not in entity_data.keys():
                entity_data[video_id] = {}
            if entity_id not in entity_data[video_id].keys():
                entity_data[video_id][entity_id] = {}
            if ts not in entity_data[video_id][entity_id].keys():
                entity_data[video_id][entity_id][ts] = []
            entity_data[video_id][entity_id][ts] = (features, label)
            feature_list.append((video_id, entity_id, ts))

            # ts_to_entity
            if video_id not in ts_to_entity.keys():
                ts_to_entity[video_id] = {}
            if ts not in ts_to_entity[video_id].keys():
                ts_to_entity[video_id][ts] = []
            ts_to_entity[video_id][ts].append(entity_id)

        print('loaded ', len(feature_list), ' features')
        return entity_data, feature_list, ts_to_entity

class ASCFeaturesDataset(ContextualDataset):
    def __init__(self, csv_file_path, time_lenght, time_stride,
                 candidate_speakers):
        # Space config
        self.time_lenght = time_lenght
        self.time_stride = time_stride
        self.candidate_speakers = candidate_speakers
        self.half_time_length = math.floor(self.time_lenght/2)

        # In memory data
        self.feature_list = []
        self.ts_to_entity = {}
        self.entity_data = {}

        # Load metadata
        self._cache_feature_data(csv_file_path)

    # Parallel load of feature files
    def _cache_feature_data(self, dataset_dir):
        pool = mp.Pool(int(mp.cpu_count()/2))
        files = glob.glob(dataset_dir)
        results = pool.map(self._cache_feature_file, files)
        pool.close()

        for r_set in results:
            e_data, f_list, ts_ent = r_set
            print('unpack ', len(f_list))
            self.entity_data.update(e_data)
            self.feature_list.extend(f_list)
            self.ts_to_entity.update(ts_ent)

    def __len__(self):
        return int(len(self.feature_list))

    def __getitem__(self, index):
        video_id, target_entity_id, center_ts = self.feature_list[index]
        entity_context = self.get_speaker_context(self.ts_to_entity, video_id,
                                                  target_entity_id, center_ts,
                                                  self.candidate_speakers)

        target = self.entity_data[video_id][target_entity_id][center_ts][1]
        feature_set = np.zeros((self.candidate_speakers, self.time_lenght, 1024))
        for idx, ctx_entity in enumerate(entity_context):
            time_context = self.get_time_context(self.entity_data,
                                                 video_id,
                                                 ctx_entity, center_ts,
                                                 self.half_time_length,
                                                 self.time_stride)
            features = self.get_time_indexed_feature(video_id, ctx_entity,
                                                     time_context)
            feature_set[idx, ...] = features

        feature_set = np.asarray(feature_set)
        feature_set = np.swapaxes(feature_set, 0, 2)
        return np.float32(feature_set), target

class ASCFeaturesDatasetForwardPhase(ContextualDataset):
    def __init__(self, csv_file_path, time_lenght, time_stride,
                 candidate_speakers):
        # Space config
        self.time_lenght = time_lenght
        self.time_stride = time_stride
        self.candidate_speakers = candidate_speakers
        self.half_time_length = math.floor(self.time_lenght/2)

        # In memory data
        self.feature_list = []
        self.ts_to_entity = {}
        self.entity_data = {}

        # Single video metdadata
        self.entity_data, self.feature_list, self.ts_to_entity =   self._cache_feature_file(csv_file_path)

    def __len__(self):
        return int(len(self.feature_list))

    def __getitem__(self, index):
        video_id, target_entity_id, center_ts = self.feature_list[index]
        entity_context = self.get_speaker_context(self.ts_to_entity, video_id,
                                                  target_entity_id, center_ts,
                                                  self.candidate_speakers)

        feature_set = np.zeros((self.candidate_speakers, self.time_lenght, 1024))
        for idx, ctx_entity in enumerate(entity_context):
            time_context = self.get_time_context(self.entity_data,
                                                 video_id,
                                                 ctx_entity, center_ts,
                                                 self.half_time_length,
                                                 self.time_stride)
            features = self.get_time_indexed_feature(video_id, ctx_entity,
                                                     time_context)
            feature_set[idx, ...] = features

        feature_set = np.asarray(feature_set)
        feature_set = np.swapaxes(feature_set, 0, 2)
        return np.float32(feature_set), video_id, center_ts, target_entity_id


import os
import numpy as np
import spacy
import re
import json
import pyttsx3 #replace it to librosa
import os
import librosa
import numpy as np
from fastdtw import fastdtw
from gtts import gTTS
from scipy.spatial.distance import euclidean
from fastdtw import fastdtw
import shutil
import config

def create_folder_if_not_exists(folder_path):
    if not os.path.exists(folder_path):
        os.makedirs(folder_path)

def move_files(source_filepath, destination_directory): #note that one is filepath and other is directory
    shutil.move(source_filepath, destination_directory)

    # Optional: Check if the file was moved successfully
    if os.path.exists(source_filepath):
        print("File move failed.")

def return_each_files_path(subfolder_path, return_type='full path'):
    if return_type == 'full path':
        for root, _, files in os.walk(subfolder_path):
            for file in files:
                yield os.path.join(root, file)
    elif return_type == 'filename':
        for root, _, files in os.walk(subfolder_path):
            for file in files:
                yield file

# Usage example:
# subfolder_path = '/path/to/your/subfolder'
# for path in return_each_files_path(subfolder_path):
#     print(path)

def extract_data_from_json(file_path):
    try:
        with open(file_path, "r") as file:
            data = json.load(file)
            return data
    except FileNotFoundError:
        print(f"File '{file_path}' not found.")
    except json.JSONDecodeError as e:
        print(f"Error decoding JSON: {e}")
    except ValueError as e:
        print(e,"file not found")
    except Exception as e:
        print(f"An error occurred: {e}")

def filter_entities(doc, label):
    filtered_entities = [ent.text for ent in doc.ents if ent.label_ == label]
    return filtered_entities

def save_as_json(data, file_name, subdirectory):
    """
    Save a dictionary or list as a JSON file in a subdirectory.
    If a file with the same name exists, append the data to it.

    Args:
    - data: The dictionary or list to be saved.
    - file_name: The name of the JSON file (without the .json extension).
    - subdirectory: The name of the subdirectory where the JSON file will be saved.

    Returns:
    - The full path to the saved JSON file if successful, or None if there was an error.
    """
    try:
        # Create the subdirectory if it doesn't exist
        if not os.path.exists(subdirectory):
            os.makedirs(subdirectory)

        # Construct the full file path
        file_path = os.path.join(subdirectory, f"{file_name}.json")

        # Initialize existing_data as an empty list if the file doesn't exist
        existing_data = []

        # If the file already exists, load its contents
        if os.path.exists(file_path):
            with open(file_path, 'r') as existing_file:
                existing_data = json.load(existing_file)

        # If data is a dictionary, append it as is; if it's a list, extend the list
        if isinstance(data, dict):
            existing_data.update(data)
        elif isinstance(data, list):
            existing_data.extend(data)

        # Write the combined data to the JSON file
        with open(file_path, 'w') as json_file:
            json.dump(existing_data, json_file, indent=4)

        print(f"Data saved or appended to {file_path}")
        return file_path  # Return the saved file path
    except Exception as e:
        print(f"An error occurred in saving json: {str(e)}")
        print(f"An error occurred in saving json: {str(e)}")
        return None

# # Example usage^:
# data_to_append = {"city": "Los Angeles", "zipcode": "90001"}
# subdirectory_name = "data_folder"

# # Save or append the data to a JSON file in the subdirectory and get the saved path
# saved_path = save_as_json(data_to_append, "person_data", subdirectory_name)
# if saved_path:
#     print(f"Data saved or appended at: {saved_path}")
# else:
#     print("Failed to save or append data.")

class TextProcessor:
    def __init__(self, chunksize = 50000):
        self.chunksize = chunksize
        self.nlp = spacy.load("en_core_web_lg",disable=["tagger", "parser", "textcat"])

    def read_file(self):
        with open(self.file_path, "r",errors='ignore') as file:
            file_contents = file.read()
        return file_contents

    def clean_text(self, text):
        cleaned_text = re.sub(r'\n', '. ', text)
        #cleaned_text = re.sub(r'[^\n]', '. ', text)
        cleaned_text = re.sub(r'[&]', ' and ', cleaned_text)
        cleaned_text = re.sub(r'[^a-zA-Z.();,?\'\"]', ' ', cleaned_text)
        cleaned_text = re.sub(r'\n', '. ', text)
        #cleaned_text = re.sub(r'[^\n]', '. ', text)
        cleaned_text = re.sub(r'[&]', ' and ', cleaned_text)
        cleaned_text = re.sub(r'[^a-zA-Z.();,?\'\"]', ' ', cleaned_text)
        return cleaned_text

    def tokenize_text(self, text):
        doc = self.nlp(text)
        return doc

    def process_file(self,file_path):
        self.file_path= file_path
        file_contents = self.read_file()
        cleaned_text = self.clean_text(file_contents)
        splitted_text = cleaned_text[:50000]
        #return self.tokenize_text(splitted_text)

        self.chunksize = 50000

        # # Split the document into chunks of 50,000 characters or less
        # print("splitting book into chunks..")
        # book_chunks = [cleaned_text[i:i + self.chunksize] for i in range(0, len(cleaned_text), self.chunksize)]
        # print("tokenising each chunks..")
        # doc_of_each_chunk=[self.tokenize_text(each_chunk) for each_chunk in book_chunks]
        # return doc_of_each_chunk

        for i in range(0, len(cleaned_text), self.chunksize):
            text_chunk = cleaned_text[i:i + self.chunksize]
            yield text_chunk

class AudioConverter():
    def __init__(self, word, engine, save_directory= config.paths["save_directory"]):
        word = word.lower()
        file_path = save_directory+os.sep+ f"{word}{config.audio_format}"
        self.file_path = file_path
        self.word = word
        self.engine = engine

    def process(self):
        if self.engine == "gtts":
            try: #consider this as an additional filter for collected names
                tts = gTTS(self.word)
                tts.save(self.file_path)
            except Exception as e:
                print(f"unable to save '{self.word}' due to : {str(e)}")

        elif self.engine == "pyttsx3":#note that this have to be deleted lately because the storage is high(ten times larger than gtts, but quicker and offline)
            file_name = f"{self.word}{config.audio_format}"
            engine = pyttsx3.init()
            engine.save_to_file(self.word, self.file_path)
            engine.runAndWait()
        print("sended file path= ", self.file_path)

        return self.file_path

class WordsComparer():
    def __init__(self, audio1_path='',audio2_path='') -> None:
        #print("audio path= ", audio1_path)
        self.audio1_path = audio1_path
        self.audio2_path= audio2_path
        #self.file_name = f"{self.word}.wav"

    # def convert_to_audio(self, word, engine):

    #     if engine == "gtts":
    #         file_path = os.path.join('data_christian','audio_data','doc_audio_data',f"{word}.wav")
    #         try: #consider this as an additional filter for collected names
    #             tts = gTTS(word)
    #             tts.save(file_path)
    #         except Exception as e:
    #             print(f"unable to save '{word}' due to : {str(e)}")

    #     elif engine == "pyttsx3":#note that this have to be deleted lately because the storage is high(ten times larger than gtts, but quicker and offline)
    #         file_name = f"{word}.wav"
    #         engine = pyttsx3.init()
    #         engine.save_to_file(word, file_name)
    #         engine.runAndWait()

    #     return file_path

    def audio_to_mfcc(self, audio_file_path):

        # Load the audio files #r"data\audio_data\sample_audio_data\output_audio1.wav"
        audio, sr_doc = librosa.load(audio_file_path, sr= 24000)

        # Extract MFCC features
        mfcc_doc = librosa.feature.mfcc(y= audio, sr= sr_doc)

        # Transpose MFCCs for compatibility with DTW
        mfcc_doc = mfcc_doc.T
        return mfcc_doc

    def compare_two_MFCC(self,mfcc1,mfcc2):  #get more information about this
        def calculate_distance(mfcc1,mfcc2):
            if mfcc1.shape[1] != mfcc2.shape[1]:
                raise ValueError("Number of features (columns) must be the same for both sequences")

                # Calculate the DTW distance using the fastdtw function
            distance, _ = fastdtw(mfcc1, mfcc2)
            # Calculate DTW distance and path

            #print("mfcc1 shape= ",mfcc1.shape[1],"distance= ",distance)
            #print(distance)
            return distance

        distance = calculate_distance(mfcc1,mfcc2)
        #print(distance)
        # Normalize the distance (optional)
        normalised_distance = distance / min(len(mfcc1) , len(mfcc2))
        normalised_distance = round(normalised_distance,2)

        return normalised_distance

    # def word_to_mfcc(self, word):
    #     audio_converter = AudioConverter(word,'gtts')
    #     file_path = audio_converter.process()
    #     mfcc_file = self.audio_to_mfcc(file_path)
    #     #os.remove(file_name)
    #     return mfcc_file

    def process(self):
        mfcc1 = self.audio_to_mfcc(self.audio1_path)
        mfcc2 = self.audio_to_mfcc(self.audio2_path)

        # mfcc1= self.word_to_mfcc(self.word)
        # mfcc2= self.word_to_mfcc(self.word2)
        distance= self.compare_two_MFCC(mfcc1=mfcc1, mfcc2=mfcc2)
        return distance

if __name__ == "__main__":
    file_path1 = AudioConverter(word='k',engine='gtts').process()
    file_path2 = AudioConverter(word='vanakkam',engine='gtts').process()
    distance = WordsComparer(file_path1,file_path2).process()
    print(distance,file_path2)


"""
@File    : AdaBoost.py
@Time    : 2020-05-26
@Author  : BobTsang
@Software: PyCharm
@Email   : bobtsang@bupt.edu.cn
"""
# 这次用的是乳腺癌数据集做的二分类任务，因为鸢尾花数据集太小，特征较少，对于提升树不太cover
# Minst:596x31
# time:62s

import pandas as pd
import numpy as np
from sklearn import datasets
import random
import time

# 手工实现打乱数据，不采用sklearn调用shuffle打乱数据
def Random_number(data_size):
    """
    该函数使用shuffle()打乱一个包含从0到数据集大小的整数列表。因此每次运行程序划分不同，导致结果不同

    改进：
    可使用random设置随机种子，随机一个包含从0到数据集大小的整数列表，保证每次的划分结果相同。

    :param data_size: 数据集大小
    :return: 返回一个列表
    """
    num_set = []
    random.seed(1)
    for i in range(data_size):
        num_set.append(i)
    random.shuffle(num_set)
    return num_set

def Train_test_split(data_set, target_data, size=0.2):
    """
    说明：分割数据集，我这里默认数据集的0.3是测试集

    :param data_set: 数据集
    :param target_data: 标签数据
    :param size: 测试集所占的比率
    :return: 返回训练集数据、训练集标签、训练集数据、训练集标签
    """
    # 计算训练集的数据个数
    train_size = int((1 - size) * len(data_set))

    # 获得数据
    data_index = Random_number(len(data_set))

    # 分割数据集（X表示数据，y表示标签），以返回的index为下标
    x_train = data_set[data_index[:train_size]]

    x_test = data_set[data_index[train_size:]]

    y_train = target_data[data_index[:train_size]]

    y_test = target_data[data_index[train_size:]]
    return x_train, x_test, y_train, y_test

def Caculation_error_Gx(x_train, y_train, n, div, rule, D):
    """
     计算分类错误率
     :param x_train:训练集数据
     :param y_trian:训练集标签
     :param n:要操作的特征
     :param div:划分点（阈值）
     :param rule:正反例标签
     :param D:权值分布
     :return:预测结果，分类误差
    """
    # 初始化分类误差率为0
    error = 0
    # 将训练数据矩阵中特征为n的那一列单独剥出来做成数组。因为其他元素我们并不需要，
    # 直接对庞大的训练集进行操作的话会很慢
    x = x_train[:, n]
    # 同样将标签也转换成数组格式，x和y的转换只是单纯为了提高运行速度
    # 测试过相对直接操作而言性能提升很大
    y = y_train
    predict = []

    # 依据小于和大于的标签依据实际情况会不同，在这里直接进行设置
    if rule == 'LisOne':    L = 1; H = -1
    else:                   L = -1; H = 1

    # 遍历所有样本的特征m
    for i in range(x_train.shape[0]):
        if x[i] < div:
            # 如果小于划分点，则预测为L
            # 如果设置小于div为1，那么L就是1，
            # 如果设置小于div为-1，L就是-1
            predict.append(L)
            # 如果预测错误，分类错误率要加上该分错的样本的权值（8.1式）
            if y[i] != L:
                error += D[i]
        elif x[i] >= div:
            # 与上面思想一样
            predict.append(H)
            if y[i] != H:
                error += D[i]
    # 返回预测结果和分类错误率e
    # 预测结果其实是为了后面做准备的，在算法8.1第四步式8.4中exp内部有个Gx，要用在那个地方
    # 以此来更新新的D
    return np.array(predict), error

def CreateSingleBoostingTree(x_train, y_train, D):
    """
    创建单层提升树
    :param x_train:训练数据集
    :param y_train:训练标签集
    :param D:权值分布
    :return:单层提升树
    """

    # 获得样本数目及特征数量
    m, n = np.shape(x_train)
    # 单层树的字典，用于存放当前层提升树的参数
    # 也可以认为该字典代表了一层提升树
    singleBoostTree = {}
    # 初始化分类误差率，分类误差率在算法8.1步骤（2）（b）有提到
    # 误差率最高也只能100%，因此初始化为1
    singleBoostTree['error'] = 1

    # 对每一个特征进行遍历，寻找用于划分的最合适的特征
    for i in range(n):
        # 因为特征已经经过二值化，只能为0和1，因此分切分时分为-0.5，0.5，1.5三种进行切割
        for div in [-0.5, 0.5, 1.5]:
            # 在单个特征内对正反例进行划分时，有两种情况：
            # 可能是小于某值的为1，大于某值得为-1，也可能小于某值得是-1，反之为1
            # 因此在寻找最佳提升树的同时对于两种情况也需要遍历运行
            # LisOne：Low is one：小于某值得是1
            # HisOne：High is one：大于某值得是1
            for rule in ['LisOne', 'HisOne']:
                # 按照第i个特征，以值div进行切割，进行当前设置得到的预测和分类错误率
                Gx, error = Caculation_error_Gx(x_train, y_train, i, div, rule, D)
                # 如果分类错误率e小于当前最小的e，那么将它作为最小的分类错误率保存
                if error < singleBoostTree['error']:
                    singleBoostTree['error'] = error
                    # 同时也需要存储最优划分点、划分规则、预测结果、特征索引
                    # 以便进行D更新和后续预测使用
                    singleBoostTree['div'] = div
                    singleBoostTree['rule'] = rule
                    singleBoostTree['Gx'] = Gx
                    singleBoostTree['feature'] = i
    # 返回单层的提升树
    return singleBoostTree

def CreateBoostingTree(x_train, y_train, treeNum = 50):
    """
    创建提升树
    创建算法依据“8.1.2 AdaBoost算法” 算法8.1
    :param x_train:训练数据集
    :param y_train:训练标签
    :param treeNum:树的层数
    :return:提升树
    """
    # 将数据和标签转化为数组形式
    trainDataArr = np.array(x_train)
    trainLabelArr = np.array(y_train)
    # 没增加一层数后，当前最终预测结果列表
    finalpredict = [0] * len(trainLabelArr)
    # 获得训练集数量以及特征个数
    m, n = np.shape(trainDataArr)

    # 依据算法8.1步骤（1）初始化D为1/N
    D = [1 / m] * m
    # 初始化提升树列表，每个位置为一层
    tree = []
    # 循环创建提升树
    for i in range(treeNum):
        # 得到当前层的提升树
        curTree = CreateSingleBoostingTree(trainDataArr, trainLabelArr, D)
        # 根据式8.2计算当前层的alpha
        alpha = 1 / 2 * np.log((1 - curTree['error']) / curTree['error'])
        # 获得当前层的预测结果，用于下一步更新D
        Gx = curTree['Gx']
        # 依据式8.4更新D
        # 考虑到该式每次只更新D中的一个w，要循环进行更新知道所有w更新结束会很复杂（其实
        # 不是时间上的复杂，只是让人感觉每次单独更新一个很累），所以该式以向量相乘的形式，
        # 一个式子将所有w全部更新完。
        # 该式需要线性代数基础，如果不太熟练建议补充相关知识，当然了，单独更新w也一点问题没有
        # np.multiply(trainLabelArr, Gx)：exp中的y*Gm(x)，结果是一个行向量，内部为yi*Gm(xi)
        # np.exp(-1 * alpha * np.multiply(trainLabelArr, Gx))：上面求出来的行向量内部全体
        # 成员再乘以-αm，然后取对数，和书上式子一样，只不过书上式子内是一个数，这里是一个向量
        # D是一个行向量，取代了式中的wmi，然后D求和为Zm
        # 书中的式子最后得出来一个数w，所有数w组合形成新的D
        # 这里是直接得到一个向量，向量内元素是所有的w
        # 本质上结果是相同的
        D = np.multiply(D, np.exp(-1 * alpha * np.multiply(trainLabelArr, Gx))) / sum(D)
        # 在当前层参数中增加alpha参数，预测的时候需要用到
        curTree['alpha'] = alpha
        # 将当前层添加到提升树索引中。
        tree.append(curTree)

        # -----以下代码用来辅助，可以去掉---------------
        # 根据8.6式将结果加上当前层乘以α，得到目前的最终输出预测
        finalpredict += alpha * Gx
        # 计算当前最终预测输出与实际标签之间的误差
        error = sum([1 for i in range(len(x_train)) if np.sign(finalpredict[i]) != trainLabelArr[i]])
        # 计算当前最终误差率
        finalError = error / len(x_train)
        # 如果误差为0，提前退出即可，因为没有必要再计算算了
        if finalError == 0:
            return tree
        # 打印一些信息
        print('iter:%d:%d, single error:%.4f, final error:%.4f'%(i, treeNum, curTree['error'], finalError))
    # 返回整个提升树
    return tree

def predict(x, div, rule, feature):
    """
    输出单层的预测结果
    :param x:预测样本
    :param div:划分点
    :param rule:划分规则
    :param feature:进行操作的特征
    :return:
    """
    #依据划分规则定义小于及大于划分点的标签
    if rule == 'LisOne':
        L = 1; H = -1
    else:
        L = -1; H = 1

    #判断预测结果
    if x[feature] < div:
        return L
    else:
        return H

def model_test(x_test, y_test, tree):
    """
    测试模型
    :param x_test:测试数据集
    :param y_test:测试标签集
    :param tree:提升树
    :return:准确率
    """
    # 错误率计数值
    errorCnt = 0
    # 遍历每一个测试样本
    for i in range(len(x_test)):
        # 预测结果值，初始为0
        res = 0
        # 依据算法8.1式8.6
        # 预测式子是一个求和式，对于每一层的结果都要进行一次累加
        # 遍历每层的树
        for curTree in tree:
            # 获取该层参数
            div = curTree['div']
            rule = curTree['rule']
            feature = curTree['feature']
            alpha = curTree['alpha']
            # 将当前层结果加入预测中
            res += alpha * predict(x_test[i], div, rule, feature)
        #预测结果取sign值，如果大于0 sign为1，反之为0
        if np.sign(res) != y_test[i]:
            errorCnt += 1
    #返回准确率
    return float(1 - errorCnt / len(x_test))

# 将所有数据（不包括标签）进行二值化处理
def find_init_div(data):
    inMat = data.copy()
    # 求每一列的均值
    # axis=0意味着取这列的每一行出来求均值，最终得到所有的列的均值
    inMeans = np.mean(inMat, axis=0)
    # 每一个特征属性标准化
    inMat = inMat - inMeans
    inMat = inMat.applymap(lambda x: int(0) if x <= 0 else 1)
    inMat = np.array(inMat)
    return inMat

if __name__ == '__main__':
    # 开始时间
    start = time.time()

    # 获取训练集和测试集
    breastcancer = datasets.load_breast_cancer()
    # print(breastcancer)
    # 创建一个dataframe表型数据结构
    df = pd.DataFrame(breastcancer.data, columns=breastcancer.feature_names)
    # 列尾添加新的一列'label', 值为iris.target(Series对象)
    df['label'] = breastcancer.target
    print(df)

    # 找到训练数据集的初始阈值，依据初始阈值将数据进行二值化处理，大于v的转换成1，小于v的转换成0，方便后续计算
    # 打乱数据

    data = find_init_div(df.iloc[:, :-1])
    print(data)

    target = np.array(df.iloc[:, -1])
    print(target)
    x_train, x_test, y_train, y_test = Train_test_split(data, target)
    # print(x_train)
    # print(x_test)

    # 转换为二分类任务
    # 替换标签把0，1换成-1，1
    y_train = np.array([int(1) if i == 1 else int(-1) for i in y_train])
    # print(x, y)
    # 替换标签把0，1换成-1，1
    y_test = np.array([int(1) if i == 1 else int(-1) for i in y_test])

    # 创建提升树
    print('start init train')
    tree = CreateBoostingTree(x_train, y_train, 100)

    # 测试
    print('start to test')
    accuracy = model_test(x_test, y_test, tree)
    print('the accuracy is:%.4f' % (accuracy * 100), '%')
    print(accuracy)

    # 结束时间
    end = time.time()
    print('time span:', end - start)

    # the accuracy is:97.0760 %


#!/usr/bin/python

import pygtk
pygtk.require('2.0')
import gtk
from gtk import gdk

def destroy(widget, data=None):
    gtk.main_quit()

w = gtk.Window(gtk.WINDOW_TOPLEVEL)
w.set_title("Hello from python")
w.connect('destroy', destroy)

v = gtk.TextView()
v.set_editable(False)
b = v.get_buffer()

w.add(v)
w.show_all()

b.insert_at_cursor("Hello")
b.insert_pixbuf(b.get_end_iter(), gdk.pixbuf_new_from_file_at_size("1F389.png", -1, 16))

gtk.main()


# Solution comment: Cheating. Found the list of periods on OEIS and counted the
# odds from that list.

from math import sqrt

# First determine how many non squares there are < 10000.
count = 0
for i in range(2, 10000+1):
    if abs(sqrt(i) - int(sqrt(i))) > 1e-6:
        count += 1

# Read count periods from the number file, counting how many odds.
with open('numbers.txt', 'r') as f:
    f.readline()  # Skip first line.
    res = 0
    for i, line in enumerate(f):
        if i == count:
            break
        *_, p = [int(i) for i in line.split()]
        res += (p & 1)
print(res)


#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
import os,time
while True:
	time.sleep(10)
	result = os.system("ping www.google.com -c 5 | grep '0% packet loss'")
	if not result == 0:
		os.system("echo 'No internet connection. shutting down...' >> /home/pi/cam/data/log.txt")
		time.sleep(5)
		os.system("sudo shutdown -h now")


import argparse
from inference import Inference
from model import FashionModel
from train import Trainer
from data import TrainDataset

class ArgumentSelectError(Exception):
    pass

def training():
    train_dataset = TrainDataset(
        image_dir=args.train_data_dir,
        csv_path_train=f'data/dataset_csv/list_combined_{args.train_type}_small_train.tsv',
        csv_path_val=f'data/dataset_csv/list_combined_{args.train_type}_small_val.tsv',
        train_type=args.train_type,
        batch_size=16,
        shuffle=True,
        random_seed=60,
        image_shape=args.input_shape
    )

    fm = FashionModel()
    fm.create_model(num_classes=train_dataset.num_classes, input_shape=[args.input_shape[0], args.input_shape[1], 3])
    if args.checkpoint is not None:
        fm.model.load_weights(args.checkpoint)

    fm.model.summary()

    trainer = Trainer(
        model=fm.model,
        train_gen=train_dataset.train_generator,
        val_gen=train_dataset.validation_generator,
        epoch=args.epoch,
        step=args.step
    )
    trainer.train(log_dir=args.log_dir)

def inference():
    inf = Inference(model_path=f'models/{args.predict_type}.h5',
                    sample_dir='samples',
                    inference_type=args.predict_type,
                    inference_csv=f'data/{args.predict_type}.csv')

    inf.predict(save_result=True)

total_types = ['category', 'attribute', 'attribute1', 'attribute2', 'attribute3', 'attribute4', 'attribute5']

parser = argparse.ArgumentParser(
    prog='Fashion Category and Attribute Prediction',
    add_help=True,
    description='This program predicts categories, textures(attribute1),'
                'fabrics(attribute2), shapes(attribute3), parts(attribute4),'
                'and styles(attribute5).'
)

parser.add_argument('-t', '--train', action='store_true',
                    help='Trains model with `--train-data-dir` and `--train-data-csv`.')

parser.add_argument('--train-type', type=str,
                    help='Selects which type will be trained. eg. `category`, `attribute1`.')

parser.add_argument('--train-data-dir', type=str,
                    help='Locate where is data folder.')

parser.add_argument('--input-shape', type=int, nargs=2,
                    help='Number of epochs to train.')

parser.add_argument('--epoch', type=int,
                    help='Number of epochs to train.')

parser.add_argument('--step', type=int,
                    help='Number of epochs to train.')

parser.add_argument('--checkpoint', type=str, default=None,
                    help='Number of epochs to train.')

parser.add_argument('--log-dir', type=str,
                    help='Locate where will training logs will be saved.')

parser.add_argument('-p', '--predict', action='store_true',
                    help='Inference model with `--sample-folder`.')

parser.add_argument('--predict-type', type=str,
                    help='Selects which type will be predicted. eg. `category`, `attribute1`.')

if __name__ == '__main__':
    args = parser.parse_args()

    try:
        if args.train:
            if args.train_data_dir is None:
                raise ArgumentSelectError('Train data directory not specified. Can not train!')
            elif args.log_dir is None:
                raise ArgumentSelectError('Log directory not specified. Can not train!')
            elif not any([args.train_type == train_type for train_type in total_types]):
                raise ArgumentSelectError('Train type not specified. Can not train!')
            else:
                print('Training!')
                training()
                print('Training Finished!')

        elif args.predict:
            if not any([args.predict_type == pred_type for pred_type in total_types]):
                raise ArgumentSelectError('Predict type not specified. Can not predict.')
            else:
                print('Inference!')
                inference()
                print('Inference Completed!')

    except ArgumentSelectError as err:
        print(err)
        print('Please enter right arguments!')


#coding:utf-8

from math import e
import numpy as np

year=150
def func(x):
    if x<0:
        return e**(g*x)
    else:
        return 1
x_1=[-3/float(40000)*x**2+3/float(200)*x for x in range(1,year)]

x_2=[]
T=3/2*(year-50)
a=1/T**2
for x in range(1,year):
    if(x<=T):
        x_2.append(a*x**2)
    else:
        x_2.append(1)

x_3=[1/float(100)*x for x in range(1,year)]

x_5=[]
halfyear=year/2
for x in range(1,year):
    if(x<=halfyear):
        x_5.append(1/halfyear**2*x*x)
    else:
        x_5.append(-(x-year)**2/halfyear**2+1)

import matplotlib.pyplot as plt
fig=plt.figure(1)
ax={}
number=0
sigma=0.008
for k in [0.01,0.02,0.03]:
    for g in [0.003,0.005,0.008]:
        rand=np.random.normal(0,sigma)
        c_1 = [0.01]
        c_2 = [0.01]
        c_3 = [0.01]
        c_4 = [0.01]
        c_5 = [0.01]

        for i in range(1,year):
            c_1.append(min(k*(1-x_1[i-1])*(1-c_1[i-1])+func(x_1[i-1]-c_1[i-1])*c_1[i-1]+rand,1))
            c_2.append(min(k * (1 - x_2[i-1]) * (1 - c_2[i-1]) + func(x_2[i-1] - c_2[i-1]) * c_2[i-1]+rand,1))
            c_3.append(min(k * (1 - x_3[i-1]) * (1 - c_3[i-1]) + func(x_3[i-1] - c_3[i-1]) * c_3[i-1]+rand,1))
            c_5.append(min(k * (1 - x_5[i-1]) * (1 - c_5[i-1]) + func(x_5[i-1] - c_5[i-1]) * c_5[i-1]+rand,1))

        for i in range(1,year):
            c_4.append(min(k * (1 - c_4[i-1]) * (1 - c_4[i-1]) + func(c_4[i-1] - c_4[i-1]) * c_4[i-1]+rand,1))

        ax[number]=fig.add_subplot(331+number)
        plt.title('k=%.3f,g=%.3f'%(k,g))
        plt.plot(c_1,label='quadric')
        plt.plot(c_2,label='convex')
        plt.plot(c_3,label='static')
        plt.plot(c_4,label='dynamic')
        plt.plot(c_5,label='logistics')
        number=number+1

ax[8].legend(loc='lower center',shadow=True,bbox_to_anchor=(1.2, 1.4),borderaxespad = 0.)
plt.show()


"""
Package for conversion between Julian date and other date/time
representations.
"""

from .api import *

__version__ = '0.1.0'
__author__ = 'Nikita Churilov'
__maintainer__ = __author__
__email__ = 'churilov-ns@yandex.ru'
__license__ = 'MIT'