# The data we need to retrieve.

# 1. Open data file
import csv
import os
# Assign a varialbe for the file to load file from path
file_to_load = os.path.join("Resources", "election_results.csv")
#Create a filename variable to save file to a path.
file_to_save = os.path.join("analysis","elections_analysis.txt")
# 2. Assign variables, lists, dictionaries, etc.
#variable to set accumulator to count total votes
total_votes = 0
#create new list to hold canidates in election
candidate_options = []
#create dictionary to hold votes count for each candicate
candidate_votes = {}
#create variables to hold Winning Candidate & count tracker
winning_candidate = ""
winning_count = 0
winning_percentage = 0

# 3. Open election results and read the file
# election_data = open("Resources/election_results.csv","r") replaced with
with open(file_to_load) as election_data:

    # 4. perform analysis
    # 4a. get data to read for analysis
    file_reader = csv.reader(election_data)
    # 4a. remove header row from data to analyze
    headers = next(file_reader)
    # 4b. iterate through rows to determine total votes & get list of candidates
    for row in file_reader:
        #add to the total vote count
        total_votes +=1
        #get candidate names
        candidate_name = row[2]
        #add candidates names to list if not already in list
        if candidate_name not in candidate_options:
            candidate_options.append(candidate_name)
            #start tracking each candidate's vote count
            candidate_votes[candidate_name] =0
            #add vote to candidate total
        candidate_votes[candidate_name] += 1
    # open file to save election results to text file
    with open(file_to_save, "w") as txt_file:
        #Print election results to terminal
        election_results = (
            f"\nElection Results\n"
            f"-------------------------\n"
            f"Total Votes: {total_votes:,}\n"
            f"-------------------------\n")
        print(election_results, end="")
        #Save the total vote count to text file
        txt_file.write(election_results)
    # 4c.iterate through candidate list to detemine percentage of vote for each candidate
        for candidate_name in candidate_votes:
            #get total vote for candidate
            votes = candidate_votes[candidate_name]
            #calculate vote percentage
            vote_percentage = float (votes) / float (total_votes) * 100
            # print each candidate's name, vote percentage, & vote total
            candidate_results = (f"{candidate_name}: {vote_percentage:.1f}% ({votes:,})\n")
            print(candidate_results)
            txt_file.write(candidate_results)
            #print(f"{candidate_name}: {vote_percentage:.1f}% ({votes:,})\n")
        # 4d. determine winning candidate, vote percentage, & vote total
            #check if candidate vote count & percentage is greater than current winner
            if (votes > winning_count) and (vote_percentage > winning_percentage):
                #update winning variable as needed
                winning_count = votes
                winning_percentage = vote_percentage
                winning_candidate = candidate_name
            #create winner summary for printing
            winning_candidate_summary = (
                f"---------------------------\n"
                f"Winner: {winning_candidate}\n"
                f"Winning Vote Count: {winning_count:,}\n"
                f"Winning Percentage: {winning_percentage:.1f}%\n"
                f"---------------------------\n")
#5. Print winner summary
        print(winning_candidate_summary)
        txt_file.write(winning_candidate_summary)

#using the open function with the "w" mode we will write datat to the file


'''
Created on Feb 11, 2018

@author: Kevin
'''
def get_products_of_all_ints_except_at_index(intList):
  if len(intList) < 2:
      raise IndexError('Getting the product of numbers at other '
                       'indices requires at least 2 numbers')

  # the output list to hold the products
  outList = [None] * len(intList)

  # For each index, finds all the products before it
  # and stores the product each iteration
  productSoFar = 1
  for x in range(0, len(intList)):
      outList[x] = productSoFar
      productSoFar *= intList[x]

  # For each index, finds all the products after it.
  # Each index already has the product of all indices
  # before it, so this will yield the final product.
  productSoFar = 1
  for x in range(len(intList)-1, -1, -1):
      outList[x] *= productSoFar
      productSoFar *= intList[x]

  return outList

list = [1, 2, 3, 4, 5]
list = get_products_of_all_ints_except_at_index(list)
print (list)


import unittest
from test import test_support
from google.appengine.api import apiproxy_stub_map
from canvas_models import User
from dataloader import Loader

from test_data import test_users

class TestCaseUsers(unittest.TestCase):

    # Only use setUp() and tearDown() if necessary

    def setUp(self):
        #Wipe DB
        apiproxy_stub_map.apiproxy.GetStub('datastore_v3').Clear()
        #load default data
        l = Loader()
        l.loadDatabase()

    #def tearDown(self):
    #    ... code to execute to clean up after tests ...

    def test_get_users(self):
        # Test create users.
        u1_name=test_users[0]['key_name']
        u1 = User().get_by_key_name(u1_name)
        assert(u1.email == test_users[0]['email'])

    def test_create_users(self):
        # Test create users.
        u1_name="mary"
        u1 = User(key_name=u1_name)
        u1.email="mary@mail.com"
        u1.display_name = u1_name
        u1.pwdHash = ''
        u1.put()
        u2=User().get_by_key_name(u1_name)
        assert(u2.email == u1.email)

    def test_request_buddy(self):
        requester = User().get_by_key_name(test_users[0]['key_name'])
        #User1 requests User2 be a buddy
        requester.add_buddy_request(to_user_key_name=test_users[1]['key_name'])
        target = User().get_by_key_name(test_users[1]['key_name'])
        #Check User1 is on User1's list
        target_buddy_req_list  = target.get_buddy_requests(False)
        found=False
        for x in target_buddy_req_list:
            if(x.key()==requester.key()):
                found=True
                break
        assert(found)

        target = User().get_by_key_name(test_users[1]['key_name'])
        requester = User().get_by_key_name(test_users[0]['key_name'])

        assert(requester.key()  in target.buddies_asked_me)
        assert(requester.key() not in target.buddies)
        assert(target.key() not in requester.buddies)
        assert(target.key()  in requester.buddies_I_asked)
        target.accept_buddy_request(from_user_key_name=test_users[0]['key_name'])
        target = User().get_by_key_name(test_users[1]['key_name'])
        requester = User().get_by_key_name(test_users[0]['key_name'])
        assert(requester.key() not in target.buddies_asked_me)
        assert(requester.key() in target.buddies)
        assert(target.key() in requester.buddies)
        assert(target.key() not in requester.buddies_I_asked)

def test_main():
    test_support.run_unittest(MyTestCase1,
                             )

if __name__ == '__main__':
    test_main()


import socket
import time
import select
import pygame
import pickle
from threading import Thread
pygame.init()

class Client():
	def __init__(self):
		self.serverHost = "127.0.0.1"
		self.serverPort = 5000
		self.s = None
	def connect(self):
		self.s = socket.socket()
		self.s.connect((self.serverHost, self.serverPort))
		self.s.setblocking(0)
	def messageServer(self, message):
		self.s.send(message)
	def recv(self):
		data = self.s.recv(4096)
		#print("Recieved from server:", data)
		game.handleServerMessage(data)
	def close(self):
		self.s.close()

def serverThread(client):
	client.connect()
	print("Client connected")
	while True:
		#Check if there's data ready
		ready = select.select([client.s], [], [], 0.1)
		if ready[0]:
			client.recv()

	s.close()

class Player():
	def __init__(self, posX, posY):
		self.posX = posX
		self.posY = posY
		self.width = 50
		self.height = 50
		self.movX = 0
		self.movY = 0
		self.velocity = 3
		self.playerID = ""
		self.clientID = ""
	def setMov(self, movX, movY):
		self.movX = movX
		self.movY = movY
	def updatePos(self):
		self.posX = self.posX + (self.movX * self.velocity)
		self.posY = self.posY + (self.movY * self.velocity)

class Game():
	def __init__(self):
		self.client = None
		self.serverT = None
		self.fps = 60
		self.fpsClock = pygame.time.Clock()
		self.width = 640
		self.height = 480
		self.screen = pygame.display.set_mode((self.width, self.height))
		self.player = Player(self.width/2, self.height/2)
		self.client = None
		self.otherPlayers = []

	def startGame(self):
		#Setup network thread and client
		self.client = Client()
		serverT = Thread(target=serverThread, args=(self.client,)).start()
		#//TODO: It waits 1 sec for a connection there's a better way to do this
		time.sleep(1)

	def drawScreen(self):
		#Draw other players
		for player in self.otherPlayers:
			pygame.draw.rect(self.screen, (255,0,0), (player.posX, player.posY, player.width, player.height))

		#Draw player rect
		pygame.draw.rect(self.screen, (0,255,255), (self.player.posX, self.player.posY, self.player.width, self.player.height))
		pygame.display.flip()

	def handlePlayerMovement(self):
		#Handle 8 directional movement
		keys_pressed = pygame.key.get_pressed()
		movX = 0
		movY = 0
		if keys_pressed[pygame.K_w]:
			movY = -1
		elif keys_pressed[pygame.K_s]:
			movY = 1
		else:
			movY = 0
		if keys_pressed[pygame.K_d]:
			movX = 1
		elif keys_pressed[pygame.K_a]:
			movX = -1
		else:
			movX = 0
		self.player.setMov(movX, movY)
		self.player.updatePos()

	def handleServerMessage(self, message):
		#The message format is: name, clientID, object
		message = pickle.loads(message)
		if message[0] == "setID":
			self.player.clientID = message[2]
			print(f"Recieved 'setID' from Client Number: {message[1]} -> {message[2]}")
		elif message[0] == "playerObject":
			#print(f"Recieved 'playerObject' from Client Number: {message[1]} -> {message[2]}")
			self.handlePlayerObjectRecieved(message)
		else:
			print("Weird mesage:", message)

	def handlePlayerObjectRecieved(self, message):
		#print(f"Recieved 'playerObject' from Client Number: {message[1]} -> {message[2]}")
		messageClientID = message[1]
		messagePlayerObject = message[2]
		print(self.otherPlayers)
		if messageClientID == self.player.clientID:
			pass
		else:
			#Now we know the data is coming from a different client
			#Remove duplicate object so it isn't interacted  with multiple times
			for otherPlayer in self.otherPlayers:
				if otherPlayer.clientID == messagePlayerObject.clientID:
					self.otherPlayers.remove(otherPlayer)
			self.otherPlayers.append(messagePlayerObject)
			#print(messageClientID, messagePlayerObject.posX, messagePlayerObject.posY)

	def loop(self):
		self.screen.fill((0,0,0))
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				pygame.quit()
		self.fpsClock.tick(self.fps)
		self.handlePlayerMovement()
		self.drawScreen()
		#message = input("-> ")
		toDump = ["playerObject", self.player.clientID, self.player]
		message = pickle.dumps(toDump)
		self.client.messageServer(message)

def Main():
	game.startGame()
	while True:
		game.loop()
if __name__ == "__main__":
	game = Game()
	Main()


"""
This formatter display difference between 2 files as:
Property 'common.follow' was added with value: false
Property 'common.setting2' was removed
Property 'common.setting3' was updated. From true to [complex value]
"""
from gendiff import constants as const
from gendiff.formatters import value_handling as vh

ADDED = 'was added with value:'
REMOVED = 'was removed'
UPDATED = 'was updated. From'
COMPLEX = '[complex value]'

def format_diff(diff):
    return format_root(diff, [], '')

def format_root(diff, result, path):
    """
    Processes the root node and passes control to process the children
    :param diff: type(introduction) == dict. Internal representation
    from diff
    :param result: type(result) == list. Intermediate result.
    :param path: type(path) == str. Intermediate path to the changed node
    :return: type str
    """
    for node in diff:
        if node[const.TYPE] == const.TYPE_CHILDREN:
            result.append(format_child(node, path))
        if node[const.TYPE] == const.TYPE_PARENT and node[const.STATE] == \
                const.STATE_NO_CHANGED:
            format_root(node[const.TYPE_CHILDREN], result,
                        f"{path}{node[const.KEY]}.")
        elif node[const.TYPE] == const.TYPE_PARENT and node[const.STATE] == \
                const.STATE_ADDED:
            result.append(f"Property '{path}{node[const.KEY]}' "
                          f"{ADDED} {COMPLEX}\n")
        elif node[const.TYPE] == const.TYPE_PARENT and node[const.STATE] == \
                const.STATE_REMOVED:
            result.append(f"Property '{path}{node[const.KEY]}' {REMOVED}\n")

    return ''.join(result).strip()

def format_child(node, path):
    """
    Takes control if the previous value is not a parent node. Returns a string
    representation of the changed key with the path from the parent node.
    :param node: type(data) == dict. Node, where doing format
    :param path: type(path) == str. Path from parent's node to our key
    :return: type == str
    """
    if node[const.STATE] == const.STATE_ADDED:
        return f"Property '{path}{node[const.KEY]}' {ADDED} " \
               f"{format_specific_value(node[const.VALUE])}\n"
    elif node[const.STATE] == const.STATE_REMOVED:
        return f"Property '{path}{node[const.KEY]}' {REMOVED}\n"
    elif node[const.STATE] == const.STATE_CHANGED:
        return f"Property '{path}{node[const.KEY]}' {UPDATED} " \
               f"{format_specific_value(node[const.OLD_VALUE])} to " \
               f"{format_specific_value(node[const.NEW_VALUE])}\n"
    else:
        return ''

def format_specific_value(value):
    """
        :param value: type(value) == str
        :return: type str
    """
    if isinstance(value, dict):
        return COMPLEX
    elif isinstance(value, str):
        return f"'{str(value)}'"
    else:
        return vh.format_value(value)


class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid):
        """
        :type obstacleGrid: List[List[int]]
        :rtype: int
        """
        m = len(obstacleGrid)
        n = len(obstacleGrid[0])
        arr = []
        for i in range(m):
            arr.append([1]*n)
        for i in range(m):
            for j in range(n):
                if obstacleGrid[i][j] == 1:
                    arr[i][j] = 0
                elif i == 0 and j == 0:
                    continue
                elif i == 0:
                    arr[i][j] = arr[i][j-1]
                elif j== 0:
                    arr[i][j] = arr[i-1][j]
                else:
                    arr[i][j] = arr[i-1][j] + arr[i][j-1]
        return arr[m-1][n-1]


# -*- coding: utf-8 -*-
"""
Created on Sat Jul 11 11:49:45 2020

@author: Charles
"""
print("Welcome to GitHell")

import pandas as pd
import sqlite3 as s3

# it is better to use DB Browser to load data directly to the db
# this is provided as a round trip example
#
# Read the csv file, write it as a table to an existing sqlite3 db
##################################################################
imp_df = pd.read_csv(r".\data\city_temperature.csv",
                     dtype={'Region':object,
                            'Country':object,
                            'State':object,
                            'City':object,
                            'Month':int,
                            'Day': int,
                            'Year':int,
                            'AvgTemperature':float})

# gotcha!  python renders excessive precision
imp_df.round({'AvgTemperature':1})

con = s3.connect(r".\data\city_temperature.db")
imp_df.to_sql( "ct", con, if_exists='replace', index=False )

con.close()

# query the db
#
# tn = table name
# cs = column(s) string
# ws = 'where' string
##################################################################

tn = 'ct'
cs = "*"
ws = "Year=2012 AND Month=7"

con = s3.connect(r".\data\city_temperature.db")
a_df = pd.read_sql_query("SELECT " + cs + "FROM " + tn + " WHERE " + ws, con)

# write query result to table
##################################################################
a_df.to_sql( "2012temp", con, if_exists="replace", index=False )

con.close()

# read the query data from db to a dataframe
##################################################################
con = s3.connect(r".\data\city_temperature.db")
b_df = pd.read_sql( "SELECT * FROM '2012temp'", con )
con.close()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tkinter as tk
from tkinter import ttk
class GreetingFrame(ttk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.name_entry = ttk.Entry(self)
        self.name_entry.pack()

        self.greet_button = ttk.Button(
            self, text="Saludar", command=self.say_hello)
        self.greet_button.pack()

        self.greet_label = ttk.Label(self)
        self.greet_label.pack()

    def say_hello(self):
        self.greet_label["text"] = \
            "¡Hola, {}!".format(self.name_entry.get())
class AboutFrame(ttk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.label = ttk.Label(self)
        self.label["text"] = ("Visitanos en recursospython.com y "
                              "foro.recursospython.com.")
        self.label.pack()

        self.web_button = ttk.Button(self, text="Visitar web")
        self.web_button.pack(pady=10)

        self.forum_button = ttk.Button(self, text="Visitar foro")
        self.forum_button.pack()
class Application(ttk.Frame):

    def __init__(self, main_window):
        super().__init__(main_window)
        main_window.title("Panel de pestañas en Tcl/Tk")

        self.notebook = ttk.Notebook(self)

        self.greeting_frame = GreetingFrame(self.notebook)
        self.notebook.add(
            self.greeting_frame, text="Saludos", padding=10)

        self.about_frame = AboutFrame(self.notebook)
        self.notebook.add(
            self.about_frame, text="Acerca de", padding=10)

        self.notebook.pack(padx=10, pady=10)
        self.pack()
main_window = tk.Tk()
app = Application(main_window)
app.mainloop()


# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""Benchmarks for this package.

To run all benchmarks and print a report to the console::

    python -m healpix.bench

You can also run the benchmarks first, save the results dict
to disk as a JSON file (or share it with others) and then
print the results later, or compare them with other results.

We should now that this is not very comprehensive / flexible.

If your application depends on performance of HEALPix computations,
you should write benchmarks with cases relevant for that application
and check if HEALPix computations are really the bottleneck and if
this package is fast enough for you or not.
"""

import timeit

from astropy.table import Table

# NOTE: If healpy is installed, we use it in the benchmarks, but healpy is not
# a formal dependency of astropy-healpix.
try:
    import healpy as hp  # noqa
except ImportError:
    HEALPY_INSTALLED = False
else:
    HEALPY_INSTALLED = True

# Copied from https://github.com/kwgoodman/bottleneck/blob/master/bottleneck/benchmark/autotimeit.py
def autotimeit(stmt, setup='pass', repeat=3, mintime=0.2):
    timer = timeit.Timer(stmt, setup)
    number, time1 = autoscaler(timer, mintime)
    time2 = timer.repeat(repeat=repeat - 1, number=number)
    return min(time2 + [time1]) / number

# Copied from https://github.com/kwgoodman/bottleneck/blob/master/bottleneck/benchmark/autotimeit.py
def autoscaler(timer, mintime):
    number = 1
    for i in range(12):
        time = timer.timeit(number)
        if time > mintime:
            return number, time
        number *= 10
    raise RuntimeError('function is too fast to test')

def get_import(package, function):
    if package == 'astropy_healpix':
        return f'from astropy_healpix.healpy import {function}'
    else:
        return f'from healpy import {function}'

def bench_pix2ang(size=None, nside=None, nest=None, package=None, fast=False):
    setup = '\n'.join([
        get_import(package, 'pix2ang'),
        'import numpy as np',
        f'nside={nside}',
        f'ipix=(np.random.random({size}) * 12 * nside ** 2).astype(np.int64)',
        f'nest={nest}'])

    stmt = 'pix2ang(nside, ipix, nest)'

    return autotimeit(stmt=stmt, setup=setup, repeat=1, mintime=0 if fast else 0.1)

def bench_ang2pix(size=None, nside=None, nest=None, package=None, fast=False):
    setup = '\n'.join([
        get_import(package, 'ang2pix'),
        'import numpy as np',
        f'nside={nside}',
        f'lon=360 * np.random.random({size})',
        f'lat=180 * np.random.random({size}) - 90',
        f'nest={nest}'])

    stmt = 'ang2pix(nside, lon, lat, nest, lonlat=True)'

    return autotimeit(stmt=stmt, setup=setup, repeat=1, mintime=0 if fast else 0.1)

def bench_nest2ring(size=None, nside=None, package=None, fast=False):
    setup = '\n'.join([
        get_import(package, 'nest2ring'),
        'import numpy as np',
        f'nside={nside}',
        f'ipix=(np.random.random({size}) * 12 * nside ** 2).astype(np.int64)'])

    stmt = 'nest2ring(nside, ipix)'

    return autotimeit(stmt=stmt, setup=setup, repeat=1, mintime=0 if fast else 0.1)

def bench_ring2nest(size=None, nside=None, package=None, fast=False):
    setup = '\n'.join([
        get_import(package, 'ring2nest'),
        'import numpy as np',
        f'nside={nside}',
        f'ipix=(np.random.random({size}) * 12 * nside ** 2).astype(np.int64)'])

    stmt = 'ring2nest(nside, ipix)'

    return autotimeit(stmt=stmt, setup=setup, repeat=1, mintime=0 if fast else 0.1)

def bench_get_interp_weights(size=None, nside=None, nest=None, package=None, fast=False):
    setup = '\n'.join([
        get_import(package, 'get_interp_weights'),
        'import numpy as np',
        f'nside={nside}',
        f'lon=360 * np.random.random({size})',
        f'lat=180 * np.random.random({size}) - 90',
        f'nest={nest}'])

    stmt = 'get_interp_weights(nside, lon, lat, nest=nest, lonlat=True)'

    return autotimeit(stmt=stmt, setup=setup, repeat=1, mintime=0 if fast else 0.1)

def run_single(name, benchmark, fast=False, **kwargs):

    time_self = benchmark(package='astropy_healpix', fast=fast, **kwargs)
    results_single = dict(function=name, time_self=time_self, **kwargs)

    if HEALPY_INSTALLED:
        time_healpy = bench_ang2pix(package='healpy', fast=fast, **kwargs)
        results_single['time_healpy'] = time_healpy

    return results_single

def bench_run(fast=False):
    """Run all benchmarks. Return results as a dict."""
    results = []

    if fast:
        SIZES = [10, 1_000, 100_000]
    else:
        SIZES = [10, 1_000, 1_000_000]

    for nest in [True, False]:
        for size in SIZES:
            for nside in [1, 128]:
                results.append(run_single('pix2ang', bench_pix2ang, fast=fast,
                                          size=size, nside=nside, nest=nest))

    for nest in [True, False]:
        for size in SIZES:
            for nside in [1, 128]:
                results.append(run_single('ang2pix', bench_ang2pix, fast=fast,
                                          size=size, nside=nside, nest=nest))

    for size in SIZES:
        for nside in [1, 128]:
            results.append(run_single('nest2ring', bench_nest2ring, fast=fast,
                                      size=size, nside=nside))

    for size in SIZES:
        for nside in [1, 128]:
            results.append(run_single('ring2nest', bench_ring2nest, fast=fast,
                                      size=size, nside=nside))

    for nest in [True, False]:
        for size in SIZES:
            for nside in [1, 128]:
                results.append(run_single('get_interp_weights', bench_get_interp_weights,
                                          fast=fast, size=size,
                                          nside=nside, nest=nest))

    return results

def bench_report(results):
    """Print a report for given benchmark results to the console."""

    table = Table(names=['function', 'nest', 'nside', 'size',
                         'time_healpy', 'time_self', 'ratio'],
                  dtype=['S20', bool, int, int, float, float, float], masked=True)
    for row in results:
        table.add_row(row)

    table['time_self'].format = '10.7f'

    if HEALPY_INSTALLED:
        table['ratio'] = table['time_self'] / table['time_healpy']
        table['time_healpy'].format = '10.7f'
        table['ratio'].format = '7.2f'

    table.pprint(max_lines=-1)

def main(fast=False):
    """Run all benchmarks and print report to the console."""
    print('Running benchmarks...\n')
    results = bench_run(fast=fast)
    bench_report(results)

if __name__ == '__main__':
    main()


# an example of .csv files

with open ("class_example - Sheet1.csv","r") as f:
#    data = f.read()
#    print(data)
# read the first line of data and put it away
    headline = f.readline()
    data = f.read()   # read the REST of the file into a single string
    data_list = data.split('\n')
    for i in range(len(data_list)):
        data_list[i] = data_list[i].split(",") # change , to \t if it was tab-separated value

    #find all the baseball fans
    for i in range(len(data_list)):
        if data_list[i][5] == "Baseball":
            print(data_list[i][0], " is a baseball fan")

    #test
#    for i in range(len(data_list)):
#        print(data_list[i])


import os

# Set the directory path where the CSV files are located

directory = '/path/to/csv/files/'

# Loop through all files in the directory
for filename in os.listdir(directory):
    if filename.endswith('.csv'):
        # Open the file in read mode with the encoding set to UTF-8
        with open(os.path.join(directory, filename), 'r', encoding='utf-8') as f:
            # Read the contents of the file and convert it to UTF-16LE encoding
            contents = f.read().encode('utf-16le')

        # Open the same file in write mode with the encoding set to UTF-16LE
        with open(os.path.join(directory, filename), 'wb') as f:
            # Write the contents to the file
            f.write(contents)


## Uncomment for Jupyter notebook:
## %matplotlib inline
import pandas
import numpy as np
import matplotlib.pyplot as plt
# Can use the 'Institution 1 Location' to select "domestic" (US and FN) students:
US_states = ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DC", "DE", "FL", "GA",
          "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD",
          "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ",
          "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC",
          "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"]

plt.rc('font', family='sans-serif', size=16)

#apps = pandas.read_excel('/home/aran/GAC19/GraduateAdmissions/190215_scores_all.xlsx')
#apps = pandas.read_excel('2020_all_applicants_data.xlsx')
apps = pandas.read_excel('2020_scores_all.xlsx')
# Append several files:
#df18=pandas.read_excel('/home/aran/GAC19/GraduateAdmissions/18_data_all.xlsx')
#df19=pandas.read_excel('/home/aran/GAC19/GraduateAdmissions/190215_scores_all.xlsx')
#df20=pandas.read_excel('2020_all_applicants_data.xlsx')
#apps=df20.append([df18,df19],ignore_index=True,sort=False)
#
## Uncomment to view the entire input table.
# apps
# Make cuts:
#apps = apps[apps['New Rank']<76].reset_index(drop = True)
apps = apps[apps['Decision Status']=="ADMIT"].reset_index(drop = True).dropna(how='all', axis=1)
#apps=apps[apps['GRE Subject Total Score %']>0].reset_index(drop = True)
#apps=apps[(apps['Institution 1 GPA Score']>0)].reset_index(drop = True)
#apps=apps[(apps['GRE Quantitative']>0)].reset_index(drop = True)
#apps=apps.dropna(subset=['Recommender 1 Rating','Recommender 2 Rating','Recommender 3 Rating'])
#apps=apps[(apps['Citizenship']=='US') | (apps['Citizenship']=='PR')].reset_index(drop = True)
#apps=apps[(apps['Citizenship']=='FN')].reset_index(drop = True)
#apps=apps[apps['Institution 1 Location'].isin(US_states)].reset_index(drop = True)

def normalize_GPA(gpas):
	''' Normalize GPA to a top score of 4.0'''
	#print('Normalizing GPAs')
	normgpas=[]
	for g in gpas:
		if g>4.0 and g<=5.0:
			#print(g,g*4./5.)
			normgpas.append(g*4./5.)
		elif g>5.0 and g<=10.0:
			#print(g,g*4./10.)
			normgpas.append(g*4./10.)
		elif g>10.0 and g<=20.0:
			#print(g,g*4./20.)
			normgpas.append(g*4./20.)
		elif g>20. and g<=100.:
			#print(g,g*4./100.)
			normgpas.append(g*4./100.)
		elif g>0. and g<1.7: # probably German System 1.0-1.5 is best, 1.6-2.0 is good, 2.6-3.5 is Satisfactory
			normgpas.append(4.-(g-1.0))
			print('Found a GPA below 2.0: probably from Germany? Double check! GPA=%3.2f --> NormGPA=%3.2f'%(g,4.-(g-1.0)))
		else:
			normgpas.append(g)
	return pandas.Series(normgpas,name=gpas.name)

def infoByTopic(dframe, info, topicSorted, whichint='App - physics_focus'):
	''' Return list with values for info, for example: 'Normalized GPA',
	    for each topicsorted, for example ['AP','CM','HEP']'''
	frame = dframe.dropna(subset=[info, whichint])
	infoByInt = {x:[] for x in topicSorted[:,0]}
	for infoval, top in zip(frame[info], frame[whichint]):
		for t in topicSorted[:,0]:
			if t in top:
				infoByInt[t].append(infoval)
	alldat = []
	for i, t in enumerate(topicSorted[:,0]):
		data = infoByInt[t]
		alldat.append(data)

	return alldat

def get_RecLettScore(letters):
    '''Input: A dataframe with columns named "Recommender 1 Rating", etc...
    Each row is one student. We will assign a numerical value to each letter and return the average of all his letters'''
    # Translate the scores given by the recommenders:
    recs={'Among the very best': 1, 'Top 5%': 5., 'Top 10%': 10., 'Top Quarter': 25., 'Average': 50.}
    letters=letters.replace(recs.keys(),recs.values()) # replace text with numbers
    # Take the average of the 4 recommenders:
    letters['AvgRecLet'] = letters[['Recommender 1 Rating', 'Recommender 2 Rating', 'Recommender 3 Rating', 'Recommender 4 Rating']].mean(axis=1)
    #print(apps['AvgRecLet'])
    return letters['AvgRecLet'].dropna()

# Top 100 liberal arts colleges by the Times Higher Education: https://www.timeshighereducation.com/student/best-universities/best-liberal-arts-colleges-united-states
# Doctoral Research Universities: https://en.wikipedia.org/wiki/List_of_research_universities_in_the_United_States
def get_UniversityRank(ulist):
    ''' Input ulist: a dataframe/series wit the names of Universities. Each row is a a student.
    We will look up the UniversitiesbyResearchTier.csv  file and return
    a list for every row in ulist with "R1", "R2", "R3", "TopLA"
    depending on the rank, or "" if it is not on the csv file.
    You can then easily create a new column based on this ranking for each row:
    df['URank']=get_UniversityRank(df['Institution 1 Name'])  '''
    # Read lists of universities:
    unames = pandas.read_csv('UniversitiesbyResearchTier.csv',names=['R1','R2','R3','Top100LiberalArts'])
    # Remove things in the names that could derail a proper match:
    fix_names={'-':' ', ',':'', 'at ':'', 'At ':'', 'of ':'', 'Of ':'', 'the ':'', 'The ':'', 'and ':'', 'And ':'', \
    "'s":"s", 'In ':'', 'SUNY':'Suny', 'CUNY':'Cuny', ' Main Campus':'', ' Endowed Colleges':''}
    # Clean up both lists:
    for k,v in fix_names.items():
        ulist=ulist.str.replace(k,v) # the str.replace acts over a Series replacing a substring inside a string.
        for c in unames.columns:
            unames[c]=unames[c].str.replace(k,v)
    urank=[] # list to store the rank
    for u in ulist:
        found=None
        for c in unames.columns:
            if u in unames[c].values:
                found = c
        urank.append(found)
    #urank=[x.replace('Top100LiberalArts', 'TopLA') for x in urank]
    return urank

def main():
    gres=apps['GRE Subject Total Score %'].fillna(-10.)
    #gpas=apps['Institution 1 GPA Score'].fillna(-10.) #Institution 1 GPA (4.0 Scale) ; Institution 1 GPA Score
    # Now we have the properly normalized right in the spreadsheet:
    apps['Normalized GPA'] = apps['Institution 1 GPA (4.0 Scale)'].fillna(-10.) #normalize_GPA(gpas)
    # definitions for the axes
    left, width = 0.1, 0.65
    bottom, height = 0.1, 0.65
    bottom_h = left_h = left + width + 0.02

    rect_scatter = [left, bottom, width, height]
    rect_histx = [left, bottom_h, width, 0.2]
    rect_histy = [left_h, bottom, 0.2, height]

    # start with a rectangular Figure
    plt.figure(1, figsize=(10,10))

    axScatter = plt.axes(rect_scatter)
    axHistx = plt.axes(rect_histx)
    axHisty = plt.axes(rect_histy)

    #
    # GRE vs GPA scatter plot with projections
    #
    axScatter.scatter(gres, apps['Normalized GPA'])
    axScatter.set_xlabel("GRE Subject Total Score %")
    axScatter.set_ylabel("Undergrad GPA (4.0 Scale)")

    # now determine nice limits by hand:
    binwidth = 0.2
    xymax = np.max([np.max(np.fabs(gres)), np.max(np.fabs(apps['Normalized GPA']))])
    lim = (int(xymax/binwidth) + 1) * binwidth
    axScatter.set_xlim((0, 100))
    axScatter.set_ylim((2.7, 4))
    #print(lim)

    axHistx.hist(gres.dropna(), bins=np.arange(0, lim + 10, 10))
    axHistx.axes.xaxis.set_ticklabels([]) # remove x axis labels on top plot
    axHisty.hist(apps['Normalized GPA'].dropna(), bins=np.arange(0, lim + 0.2, 0.2), orientation='horizontal')
    axHisty.axes.yaxis.set_ticklabels([]) # remove y axis labels on right plot
    axHistx.set_xlim(axScatter.get_xlim())
    axHisty.set_ylim(axScatter.get_ylim())

    plt.savefig('00GAC-GPAGREcorr-projections.png')

    noPGRE=len(gres)-len(gres[gres>0])
    print('\nDid NOT submit PGRE=%3i (%2.0f%%)'%(noPGRE,noPGRE*100./len(gres)))
    print('GRE Mean = %3.0f, Median = %3.0f percentile'%(np.mean(gres[gres>0]),np.median(gres[gres>0])))
    print('GPA Mean = %5.2f, Median = %5.2f'%(np.mean(apps['Normalized GPA'][apps['Normalized GPA']>0]),np.median(apps['Normalized GPA'][apps['Normalized GPA']>0])))

    #
    # GRE vs GPA scatter plot with U names
    #
    plt.figure() # New figure
    fig, ax = plt.subplots(figsize=(10, 10))
    fig.subplots_adjust(right=0.7)
    # Plot correlation between GRE and GPA:
    gres=apps['GRE Subject Total Score %'].fillna(-100) # just in case there are empty values
    gpas=apps['Normalized GPA'].fillna(-100)   # idem
    universities=apps['Institution 1 Name']
    #print(gres[1],gpas[1])
    plt.scatter(gres,gpas,color='blue',s=5,edgecolor='none')
    for i, txt in enumerate(universities):
    	#print(i,gres[i],gpas[i],txt)
    	ax.annotate(txt, (gres[i],gpas[i]), size='x-small')

    plt.xlabel("GRE Subject Total Score %")
    plt.ylabel("Undergrad GPA (4.0 Scale)")
    plt.ylim(3.3,4.02)
    plt.xlim(20,100)
    plt.savefig('00GAC-GPAGREcorr-scatter.png')
    #
    # GRE breakdown
    #
    vpd=apps['GRE Verbal Percentile'].dropna()
    vpl='Mean = %3.0f, Median = %3.0f'%(np.mean(vpd),np.median(vpd))
    pgrel='Mean = %3.0f, Median = %3.0f'%(np.mean(gres[gres>0]),np.median(gres[gres>0]))
    awd=apps['GRE Analytical Writing Percentile'].dropna()
    awl='Mean = %3.0f, Median = %3.0f'%(np.mean(awd),np.median(awd))
    qpd=apps['GRE Quantitative Percentile'].dropna()
    qpl='Mean = %3.0f, Median = %3.0f'%(np.mean(qpd),np.median(qpd))
    axes = apps.hist(column=['GRE Verbal Percentile',
                             'GRE Subject Total Score %',
                             'GRE Analytical Writing Percentile',
                             'GRE Quantitative Percentile'],
                       sharex=True,
                       sharey=True,
                       bins=np.linspace(0,100,11),
                       color='royalblue',
                       histtype='stepfilled',
                       label=[vpl,pgrel,awl,qpl],
                       figsize=(10,7))
    #leg=axes.legend(fontsize=10, loc=2)
    fig = plt.gcf()
    fig.tight_layout()
    fig.savefig("01GAC-GREBreakdown.png")
    #
    # Summary gender and URM percentage:
    #
    Nwomen=len(apps[apps.Sex=='F']['URM'])
    NURM=len(apps[apps.URM=='Yes']['URM'])
    Nhisp=len(apps[apps.Hispanic=='Y']['URM'])
    Ntot=len(apps['URM'])
    print('Women:    %3i/%-3i=%3.0f%%'%(Nwomen,Ntot,float(Nwomen)/float(Ntot)*100.))
    print('URM:      %3i/%-3i=%3.0f%%'%(NURM,Ntot,float(NURM)/float(Ntot)*100.))
    print('Hispanic: %3i/%-3i=%3.0f%%'%(Nhisp,Ntot,float(Nhisp)/float(Ntot)*100.))
    print(pandas.crosstab(apps.Citizenship1,apps.Sex,margins=True))
    us=len(apps[(apps['Citizenship']=='US') | (apps['Citizenship']=='PR')])
    fn=len(apps[apps['Citizenship']=='FN'])
    print('\nUS: %3i/%-3i=%3.0f%%'%(us,us+fn,float(us)/float(us+fn)*100.))
    print('FN: %3i/%-3i=%3.0f%%'%(fn,us+fn,float(fn)/float(us+fn)*100.))
    dom = -10
    if 'Institution 1 Location' in apps.columns:
        dom=len(apps[apps['Institution 1 Location'].isin(US_states)])
        fn_in_dom=len(apps[(apps['Institution 1 Location'].isin(US_states)) & (apps['Citizenship']=='FN')])
        print('From domestic institutions: %3i/%-3i=%3.0f%%'%(dom,Ntot,float(dom)/float(Ntot)*100.))
        print('FN in domestic institutions: %3i/%-3i=%3.0f%%'%(fn_in_dom,dom,float(fn_in_dom)/float(dom)*100.))
    print('\nAverage TOEFL score: %4.1f'%np.mean(apps['TOEFL Total']))
    #
    # Breakdown by Gender
    #
    # Print nice table with summary of gender and race
    # Sometimes Laura uses "Sex" and sometimes "Gender" in her spreadsheets
    print(pandas.crosstab(apps.Sex,apps.Race.fillna('N/R'),margins=True)) # <-- Check name of column: Sex or Gender
    gres = ['GRE Analytical Writing Percentile',
            'GRE Quantitative Percentile',
            'GRE Subject Total Score %',
            'GRE Verbal Percentile']
    fig, axes = plt.subplots(2, 2, figsize=(10,7), sharex=True, sharey=True)

    for ax, gre in zip(axes.flatten(), gres):
        scores = apps.dropna(subset=[gre])
        men = scores['Sex'] == 'M'   # <-- Check name of column: Sex or Gender
        women = np.logical_not(men)
        ax.hist(scores[gre][men],
                bins=np.linspace(0, 100, 11),
                histtype='stepfilled',
                label='Men',
                alpha=0.5)
        ax.hist(scores[gre][women],
                bins=np.linspace(0, 100, 11),
                histtype='stepfilled',
                label='Women %3i/%-3i=%2.0f%%'%(Nwomen,Ntot,float(Nwomen)/float(Ntot)*100.),
                alpha=0.5)
        ax.set(title=gre)
        ax.grid()
        leg=ax.legend(fontsize=12, loc=2)
    fig.tight_layout()
    fig.savefig("02GAC-GenderBreakdown.png")
    #
    # URM Breakdown
    #
    # Print nice table with summary of gender and URM
    print(pandas.crosstab(apps.Sex,apps.URM.fillna('N/R'),margins=True))

    gres = ['GRE Analytical Writing Percentile',
        'GRE Quantitative Percentile',
        'GRE Subject Total Score %',
        'GRE Verbal Percentile']
    fig, axes = plt.subplots(2, 2, figsize=(10,7), sharex=True, sharey=True)

    for ax, gre in zip(axes.flatten(), gres):
        scores = apps.dropna(subset=[gre])
        urm = scores['URM'] == 'Yes'
        rep = np.logical_not(urm)
        ax.hist(scores[gre][rep],
                bins=np.linspace(0, 100, 11),
                histtype='stepfilled',
                label='Not URM',
                alpha=0.5)
        ax.hist(scores[gre][urm],
                bins=np.linspace(0, 100, 11),
                histtype='stepfilled',
                label='URM %3i/%-3i=%2.0f%%'%(NURM,Ntot,float(NURM)/float(Ntot)*100.),
                alpha=0.5)
        ax.set(title=gre)
        ax.grid()

        leg=ax.legend(fontsize=12, loc=2)

    fig.tight_layout()
    fig.savefig("03GAC-URMBreakdown.png")
    #
    # Categorization by Interests
    #
    # Sort students by interest, dropping records where interest is not explicitly mentioned in the students' personal statement.
    # Grab a full table of interests from what they clicked or based on personal statements, dropping empty values
    #interests = apps['App - physics_focus'].dropna()
    whichint= 'App - physics_focus'  ### 'App - physics_focus' or 'Interest narrowed from PS'
    interests = apps[whichint].dropna()

    # Store unique list of student topics (to be used in pie chart)
    topics = []
    for i in interests.values:
    # Various cleanup steps for the topic list. This ensures that
    # entries with multiple topics will be properly split
        i = i.replace(".", ",")
        if not 'A/AP' in i:
            i = i.replace("/", ",")

        # Do the splitting and stuff unique topics into the topics list
        subj = i.split(",")
        for s in subj:
            s = s.strip()
            if not s in topics:
                topics.append(s)
    print('Topics found = ', topics)
    # Print pretty table of topics split by Experiment/Theory/undecided
    print(10*' ' + ' '.join(['%4s']*len(topics)) % tuple(topics))
    thexpint=apps.dropna(subset=[whichint,'App - physics_major']) # Drop empty rows, the physics_major is Exp/Th/Undecided
    for etu in ['Experiment','Theory','Undecided']:
        rownum=[]
        for t in topics:
            #This next command creates an array of True/False requiring that the string etu is found AND that the topic t is also found in that subset
            intetu=thexpint[thexpint[whichint].str.contains(t)]['App - physics_major'].str.contains(etu)
            rownum.append(np.count_nonzero(intetu)) # This just counts how many True's we have in the array intetu
        print('%10s'%etu + ' '.join(['%4i']*len(rownum)) % tuple(rownum)) # print the row with correct formatting

    Nth=len(apps['App - physics_major'][apps['App - physics_major']=='Theory'])
    Nex=len(apps['App - physics_major'][apps['App - physics_major']=='Experiment'])
    Ntot=len(apps['App - physics_major'].dropna())
    print('Theory: %3i/%-3i=%3.0f%% \nExperiment: %3i/%-3i=%3.0f%% \nUndecided: %3i/%-3i=%3.0f%%' %(Nth,Ntot,100.*float(Nth)/float(Ntot),Nex,Ntot,100.*float(Nex)/float(Ntot),Ntot-Nex-Nth,Ntot,100.*float(Ntot-Nex-Nth)/float(Ntot)))

    # Now loop through each student entry and count the interests
    import operator

    topicCount = dict.fromkeys(topics,0)
    total = 0
    for i in interests:
        for t in topics:
            if t in i:
                topicCount[t] += 1
                total += 1

    # Sort the list of student interests to go from most to least popular
    topicSorted = np.asarray(sorted(topicCount.items(),
                                key=operator.itemgetter(1),
                                reverse=True))
    #
    # Make a nice pie chart.
    #
    # Define fancy colors...
    colors = ["salmon",    "lightgreen", "deepskyblue",  "cornflowerblue",
              "lightblue", "papayawhip", "cyan",         "peru",
              "plum",      "gold",       "lemonchiffon", "lightgrey",
              "white"]
    colors = ["#0000bd", "#008000", "#f20019", "#00fefe", "#8f00c7", "#0086fe", "#5d0016", "#827800", "#fe68fe", "#ff6600"]

    fig, ax = plt.subplots(1,1, figsize=(6,5))
    ax.pie(topicSorted[:,1],
           labels=topicSorted[:,0],
           #autopct="%.1f",
           autopct=lambda p: '{:.0f}'.format(p * total / 100),
           colors=colors[:len(topics)],
           startangle=90, wedgeprops={'alpha':0.5})
    ax.set(aspect='equal')
    if whichint =='Interest narrowed from PS':
        ax.set_title('Field interests from PS')
    else:
        ax.set_title('Field interests from application form')
    fig.tight_layout()
    fig.savefig("04GAC-PieChartInterests.png")

    #
    # ### Box/Whisker Plots of GPA and GRE, by Interest
    #
    # #### The box extends from the lower to upper quartile values of the data, with a yellow line at the median. The whisker values of whis=[2.5, 97.5]
    print('Yellow line=median, Box = Top 25%, Tips = (2.5%,97.5%)')
    fig, ax = plt.subplots(1,1, figsize=(8,5))
    ax.boxplot(infoByTopic(apps, 'Normalized GPA', topicSorted, whichint), 0, whis=[2.5, 97.5], sym='')
    ax.set_xticklabels(topicSorted[:,0], rotation=45)
    ax.set(title='Institution 1 GPA (4.0 Scale)',
           ylim=(2.7,4.0),
           ylabel='GPA')
    fig.tight_layout()
    fig.savefig("05GAC-GPAUndergrad.png")

    fig, ax = plt.subplots(1,1, figsize=(8,5))
    ax.boxplot(infoByTopic(apps, 'GRE Subject Total Score %', topicSorted, whichint), 0, whis=[2.5, 97.5], sym='')
    ax.set_xticklabels(topicSorted[:,0], rotation=45)
    ax.set(title='GRE Subject (Physics) Total Score %',
       ylabel='percentile',
       ylim=(0,100))
    fig.tight_layout()
    fig.savefig("06GAC-GREPhysicsPercentile.png")

    fig, ax = plt.subplots(1,1, figsize=(8,5))
    ax.boxplot(infoByTopic(apps, 'GRE Quantitative Percentile', topicSorted, whichint), 0, whis=[2.5, 97.5], sym='')
    ax.set_xticklabels(topicSorted[:,0], rotation=45)
    ax.set(title='GRE Quantitative Percentile',
           ylabel='percentile',
           ylim=(0,100))
    ax.text(0.8,2.8,"Yellow line=median; Box=(25-75)%; Tips=(2.5-97.5)%",color='gray')
    fig.tight_layout()
    fig.savefig("07GAC-GREQuantitativePercentile.png")

    fig, ax = plt.subplots(1,1, figsize=(8,5))
    ax.boxplot(infoByTopic(apps, 'GRE Analytical Writing Percentile', topicSorted, whichint), whis=[2.5, 97.5], sym='')
    ax.set_xticklabels(topicSorted[:,0], rotation=45)
    ax.set(title='GRE Analytical Writing Percentile',
           ylabel='percentile',
           ylim=(0,100))
    fig.tight_layout()
    fig.savefig("08GAC-GREAnalyticalWritingPercentile.png")

    fig, ax = plt.subplots(1,1, figsize=(8,5))
    ax.boxplot(infoByTopic(apps, 'GRE Verbal Percentile', topicSorted, whichint), 0, whis=[50, 50], sym='')
    ax.set_xticklabels(topicSorted[:,0], rotation=45)
    ax.set(title='GRE Verbal Percentile',
           ylabel='percentile',
           ylim=(0,100))
    fig.tight_layout()
    fig.savefig("09GAC-GREAnalyticalWritingPercentile.png")
    #
    # Recommendation Letters
    #
    import scipy.stats as stats
    plt.figure() # New figure
    fig, ax = plt.subplots(figsize=(10, 10))
    x=get_RecLettScore(apps[['Recommender 1 Rating', 'Recommender 2 Rating', 'Recommender 3 Rating', 'Recommender 4 Rating']])
    n, bins, patches = plt.hist(x, 20, range=[0,50], linewidth=3, facecolor='g', alpha=0.75, histtype='stepfilled')
    # alpha is the transparency of the data points (patch)
    #plt.axis([0, 400, 0, 0.01]) # sets the ranges for the x and y axis. This command overrides the range inside hist
    plt.title("RecLetters: AB=1, 5%=5, 10%=10, TopQt=25, Avg=50")
    plt.xlabel("Average of recommendation letters scores per student")
    plt.ylabel("Students")
    plt.grid()
    RLmean = np.mean(x)
    RLmedi = np.median(x)
    RLmode = stats.mode(x)[0][0]
    plt.text(35,0.85*max(n),'%-7s %4i\n%-7s %4.2f \n%-7s %4.2f\n%-7s %4.2f'%('Entries',len(x),'Mean',RLmean,'Median',RLmedi,'Mode',RLmode),fontsize=20)
    #plt.xscale('log')
    plt.savefig("10GAC-RecLettAvgScore.png")
    #
    # US University research tier
    #
    plt.figure() # New figure
    fig, ax = plt.subplots(figsize=(10, 10))
    # Create a new column with the classification for each University:
    apps['URank']=get_UniversityRank(apps['Institution 1 Name']) # returns a list with the rank value
    u_tier_count=apps['URank'].value_counts().to_dict() # This returns something like: {'R1': 117, 'R2': 21, 'Top100LiberalArts': 18, 'R3': 8}
    # Count how many entries make up each category in the original file:
    unames = pandas.read_csv('UniversitiesbyResearchTier.csv',names=['R1','R2','R3','Top100LiberalArts'])
    u_tier_tot={'R1': len(unames['R1'].dropna()), # total counts in each category
                'R2': len(unames['R2'].dropna()),
                'R3': len(unames['R3'].dropna()),
                'Top100LiberalArts': 100}
    print(u_tier_count,u_tier_tot)
    x=np.arange(4)
    bar_width=0.4
    u_counts=[u_tier_count[k] for k in sorted(u_tier_count)] # ordered values alphabetically by key
    ax.bar(x, u_counts, bar_width, color='b')
    if dom > 0:
        for idx, c in enumerate(u_counts):
            perc='{:2}%'.format(int(c*100/dom)) # dom is calculated as: len(apps[apps['Institution 1 Location'].isin(US_states)])
            ax.annotate(perc, (x[idx]-0.1, c+2) , color='green')
    ax.set_xlabel('Carnegie Classification of University of applicant, as of 2019',size = 16)
    ax.set_ylabel('Students',size = 16)
    ax.set_title('Top Liberal Arts colleges from Times Higher Education',size = 18)
    ax.text(0.38,0.95,"R1: Doctoral Highest Research [N=%3i]"%u_tier_tot['R1'],transform=ax.transAxes)
    ax.text(0.38,0.9,"R2: Doctoral Higher Research [N=%3i]"%u_tier_tot['R2'],transform=ax.transAxes)
    ax.text(0.38,0.85,"R3: Doctoral Moderate Research [N=%3i]"%u_tier_tot['R3'],transform=ax.transAxes)
    ax.text(0.38,0.8,"%% of students in US institutions [N=%3i]"%dom, color='green',transform=ax.transAxes)
    ax.set_xticks(x)
    ax.set_xticklabels([k for k in sorted(u_tier_count)])
    plt.savefig("11GAC-UniversityResearchTier.png")
    #
    # Plot boxes of GRE and GPA for each research tier
    #
    tiers = np.asarray(sorted(u_tier_count.items())) # sorted alphabetically by key
    #print(infoByTopic(apps,'GRE Quantitative Percentile', tiers,'URank'))
    fig, (ax1,ax2) = plt.subplots(1,2, figsize=(16,5))
    ax1.boxplot(infoByTopic(apps, 'Normalized GPA', tiers, 'URank'), 0, whis=[2.5, 97.5], sym='')
    ax1.set_xticklabels(tiers[:,0], rotation=0)
    ax1.set(title='Institution 1 GPA (4.0 Scale)',
           ylim=(2.7,4.0),
           ylabel='GPA')
    ax2.boxplot(infoByTopic(apps, 'GRE Subject Total Score %', tiers, 'URank'), 0, whis=[2.5, 97.5], sym='')
    ax2.set_xticklabels(tiers[:,0], rotation=0)
    ax2.set(title='GRE Subject (Physics) Total Score %',
       ylabel='percentile',
       ylim=(0,100))
    fig.tight_layout()
    fig.savefig("12GAC-GPAGREByResearchTier.png")
    # Scatter plot of GRE vs GPA for each research tier:
    fig, ax = plt.subplots()
    groups = apps.groupby('URank')
    for name, group in groups:
        ax.plot(group['GRE Subject Total Score %'], group['Normalized GPA'], marker='o', linestyle='', ms=4, label=name)
    ax.legend()
    ax.set_xlim((0, 100))
    ax.set_ylim((2.7, 4))
    ax.set_xlabel("GRE Subject Total Score %")
    ax.set_ylabel("Undergrad GPA (4.0 Scale)")
    plt.savefig("12GAC-ScatterByResearchTier.png")
    # Histos for each classification
    fig, (ax1,ax2) = plt.subplots(1,2, figsize=(16,5))
    g=0
    for name, group in groups:
        ax1.hist(group['GRE Subject Total Score %'], 20, lw=3, range=[0,100], histtype='step', color=colors[g], label=name)
        g=g+1
    ax1.legend(loc=2)
    ax1.set_xlim((0, 100))
    ax1.set_xlabel("GRE Subject Total Score %")
    g=0
    for name, group in groups:
        ax2.hist(group['Institution 1 GPA (4.0 Scale)'], 20, lw=3, range=[2.7,4], histtype='step', color=colors[g], label=name)
        g=g+1
    ax2.legend(loc=2)
    ax2.set_xlabel("Institution 1 GPA (4.0 Scale)")
    fig.tight_layout()
    fig.savefig("12GAC-GPAGREHistosByResearchTier.png")
    #
    # US states of Institution 1
    #
    if 'Institution 1 Location' in apps.columns:
        plt.figure() # New figure
        CollStates=apps[apps['Institution 1 Location'].isin(US_states)]['Institution 1 Location']
        # Bar or pie plot:
        pandas.Series(CollStates).value_counts().plot('pie')
        #print(list(pandas.Series(CollStates).value_counts().keys()))
        #print(pandas.Series(CollStates).value_counts().values)
        plt.savefig("13GAC-USStateInstitution1.png")
        # Heat map of US:
        #sudo -H pip3 install plotly-geo geopandas pyshp shapely
        # import plotly.graph_objects as go
        # fig = go.Figure(data=go.Choropleth(
            # locations=list(pandas.Series(CollStates).value_counts().keys()),
            # z=list(pandas.Series(CollStates).value_counts().values),
            # locationmode = 'USA-states',
            # colorscale = 'Reds', colorbar_title = "Students"))
        # fig.update_layout(title_text = 'US state of Institution 1', geo_scope='usa')
        #fig.show() # this will open plot in existing browser session

    #
    # Principal Language Spoken at Home
    #
    # This is a dictionary with the languages in two-letter abbreviation:
    # https://gist.githubusercontent.com/carlopires/1262033/raw/1a2d842c7ed2f54502ae5a774d0d2b4df49fcf3c/ISO639_2.py
    plt.close('all')
    if 'Principal Language Spoken at Home' in apps.columns:
        plt.figure() # New figure
        lang=apps['Principal Language Spoken at Home']
        # Bar or pie plot:
        pandas.Series(lang).value_counts().plot('bar')
        plt.savefig("14GAC-LanguageAtHome.png")

if __name__ == "__main__":
    main()


from pymses.core import Filter
import numpy

class PointFunctionFilter(Filter):#{{{
	r"""
	PointFunctionFilter class

	Parameters
	----------
	mask_func : ``function``
		function evaluated to compute the data mask to apply
	source    : ``Source``
		PointDataset data source

	"""
	def __init__(self, mask_func, source):

		self.mask_func = mask_func
		Filter.__init__(self, source)

	def filtered_dset(self, dset):
		msk = self.mask_func(dset)
		return dset.filtered_by_mask(self.mask_func(dset))
#}}}

class PointIdFilter(Filter):#{{{
	r"""
	PointIdFilter class

	Parameters
	----------
	ids_to_keep : ``list`` of ``int``
		list of the particle ids to pick up
	source      : ``Source``
		PointDataset data source

	"""
	def __init__(self, ids_to_keep, source):
		self.ids_to_keep = numpy.sort(ids_to_keep)
		Filter.__init__(self, source)

	def filtered_dset(self, dset):
		ind_sort = numpy.argsort(dset["id"])
		dset = dset.reorder_points(reorder_indices=ind_sort)
		sorted_id = dset["id"]
		id_min = sorted_id[0]
		id_max = sorted_id[-1]
		mask = (self.ids_to_keep >= id_min)*(self.ids_to_keep <= id_max)
		sorted_ids_2k = self.ids_to_keep[mask]
		ind_ids = numpy.searchsorted(sorted_id, sorted_ids_2k)
		mask = (sorted_id[ind_ids] == sorted_ids_2k)
		ind_fids = ind_ids[mask]
		return dset.filtered_by_mask(ind_fids)
#}}}

class PointRandomDecimatedFilter(Filter):#{{{
	r"""
	PointRandomDecimatedFilter class

	Parameters
	----------
	fraction : ``float``
		fraction of the data to keep
	source   : ``Source``
		PointDataset data source

	"""
	def __init__(self, fraction, source):

		self.fraction = float(fraction)
		Filter.__init__(self, source)

	def filtered_dset(self, dset):
		mask = numpy.random.uniform(size=dset.npoints)
		mask = (mask < self.fraction)
		return dset.filtered_by_mask(mask)
#}}}

__all__ = ["PointFunctionFilter", "PointIdFilter", "PointRandomDecimatedFilter"]


from requests.structures import CaseInsensitiveDict

from opentelemetry.propagators.aws.aws_xray_propagator import (
    TRACE_HEADER_KEY,
    AwsXRayPropagator,
)

XRAY_PROPAGATOR = AwsXRayPropagator()

def test_extract_single_header(benchmark):
    benchmark(
        XRAY_PROPAGATOR.extract,
        {
            TRACE_HEADER_KEY: "bdb5b63237ed38aea578af665aa5aa60-00000000000000000c32d953d73ad225"
        },
    )

def test_inject_empty_context(benchmark):
    benchmark(
        XRAY_PROPAGATOR.inject, {}, setter=CaseInsensitiveDict.__setitem__
    )


import requests
import robot
from robot.api.deco import keyword
from robot.libraries.BuiltIn import BuiltIn

from RequestsLibrary import log
from RequestsLibrary.compat import urljoin
from RequestsLibrary.utils import is_file_descriptor, warn_if_equal_symbol_in_url_session_less

class RequestsKeywords(object):
    ROBOT_LIBRARY_SCOPE = 'Global'

    def __init__(self):
        self._cache = robot.utils.ConnectionCache('No sessions created')
        self.builtin = BuiltIn()
        self.debug = 0
        # The following variables are related to session but used in _common_request :(
        self.timeout = None
        self.cookies = None
        self.last_response = None

    def _common_request(
            self,
            method,
            session,
            uri,
            **kwargs):

        if session:
            method_function = getattr(session, method)
        else:
            method_function = getattr(requests, method)

        self._capture_output()

        resp = method_function(
            self._merge_url(session, uri),
            timeout=self._get_timeout(kwargs.pop('timeout', None)),
            cookies=kwargs.pop('cookies', self.cookies),
            **kwargs)

        log.log_request(resp)
        self._print_debug()

        log.log_response(resp)

        self.last_response = resp

        # file descriptors should be closed for files parameter as well
        data = kwargs.get('data', None)
        if is_file_descriptor(data):
            data.close()

        return resp

    @staticmethod
    def _merge_url(session, uri):
        """
        Helper method that join session url and request url.
        It relies on urljoin that handles quite good join urls and multiple /
        but has some counter intuitive behaviours if you join uri starting with /
        It handles also override in case a full url (http://etc) is passed as uri.
        """
        base = ''
        if session:
            base = session.url
        if session and uri and not session.url.endswith('/'):
            base = session.url + '/'
        if session and uri and uri.startswith('/'):
            uri = uri[1:]
        url = urljoin(base, uri)
        return url

    @keyword("Status Should Be")
    def status_should_be(self, expected_status, response=None, msg=None):
        """
        Fails if response status code is different than the expected.

        ``expected_status`` could be the code number as an integer or as string.
        But it could also be a named status code like 'ok', 'created', 'accepted' or
        'bad request', 'not found' etc.

        ``response`` is the output of other requests keywords like `GET` or `GET On Session`.
        If omitted the last response will be used.

        In case of failure an HTTPError will be automatically raised.
        A custom failure message ``msg`` can be added like in built-in keywords.

        New requests keywords like `GET` or `GET On Session` (starting from 0.8 version) already have an implicit assert
        mechanism that, by default, verifies the response status code.
        `Status Should Be` keyword can be useful when you disable implicit assert using ``expected_status=anything``.

        For example when you have a nested keyword that is used for both OK and ERROR responses:

        |   *** Test Cases ***
        |
        |   Test Get Request And Make Sure Is A 404 Response
        |       ${resp}=            GET Custom Keyword That Returns OK or ERROR Response  case=notfound
        |       Status Should Be    404    ${resp}
        |       Should Be Equal As Strings  NOT FOUND  ${resp.reason}
        |
        |   Test Get Request And Make Sure Is OK
        |       ${resp}=            GET Custom Keyword That Returns OK or ERROR Response  case=pass
        |       Status Should Be    200    ${resp}
        |       Should Be Equal As Strings  OK  ${resp.reason}
        |
        |   *** Keywords ***
        |
        |   GET Custom Keyword That Returns OK or ERROR Response
        |   [Arguments]  $case
        |        [...]
        |       IF $case == notfound
        |           $resp=     GET [...] expected_status=Anything
        |           [Return]   $resp
        |       ELSE
        |        [...]
        """
        if not response:
            response = self.last_response
        self._check_status(expected_status, response, msg)

    @keyword("Request Should Be Successful")
    def request_should_be_successful(self, response=None):
        """
        Fails if response status code is a client or server error (4xx, 5xx).

        ``response`` is the output of other requests keywords like `GET On Session`.
        If omitted the last response will be used.

        In case of failure an HTTPError will be automatically raised.

        For a more versatile assert keyword see `Status Should Be`.
        """
        if not response:
            response = self.last_response
        self._check_status(None, response, msg=None)

    @staticmethod
    @keyword("Get File For Streaming Upload")
    def get_file_for_streaming_upload(path):
        """
        Opens and returns a file descriptor of a specified file to be passed as ``data`` parameter
        to other requests keywords.

        This allows streaming upload of large files without reading them into memory.

        File descriptor is binary mode and read only. Requests keywords will automatically close the file,
        if used outside this library it's up to the caller to close it.
        """
        return open(path, 'rb')

    @keyword('GET')
    @warn_if_equal_symbol_in_url_session_less
    def session_less_get(self, url, params=None,
                         expected_status=None, msg=None, **kwargs):
        """
        Sends a GET request.

        The endpoint used to retrieve the resource is the ``url``, while query
        string parameters can be passed as string, dictionary (or list of tuples or bytes)
        through the ``params``.

        By default this keyword fails if a status code with error values is returned in the response,
        this behavior can be modified using the ``expected_status`` and ``msg`` parameters,
        read more about it in `Status Should Be` keyword documentation.
        In order to disable this implicit assert mechanism you can pass as ``expected_status`` the values ``any`` or
        ``anything``.

        Other optional requests arguments can be passed using ``**kwargs`` here is a list:

        | ``data``     | Dictionary, list of tuples, bytes, or file-like object to send in the body of the request. |
        | ``json``     | A JSON serializable Python object to send in the body of the request. |
        | ``headers``  | Dictionary of HTTP Headers to send with the request. |
        | ``cookies``  | Dict or CookieJar object to send with the request. |
        | ``files``    | Dictionary of file-like-objects (or ``{'name': file-tuple}``) for multipart encoding upload. ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')`` or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers to add for the file. |
        | ``auth`` | Auth tuple to enable Basic/Digest/Custom HTTP Auth. |
        | ``timeout`` | How many seconds to wait for the server to send data before giving up, as a float, or a ``(connect timeout, read timeout)`` tuple. |
        | ``allow_redirects`` | Boolean. Enable/disable (values ``${True}`` or ``${False}``) GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``${True}``. |
        | ``proxies`` | Dictionary mapping protocol or protocol and host to the URL of the proxy (e.g. {'http': 'foo.bar:3128', 'http://host.name': 'foo.bar:4012'}) |
        | ``verify``  | Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use. Defaults to ``${True}``. Warning: if a session has been created with ``verify=${False}`` any other requests will not verify the SSL certificate. |
        | ``stream`` | if ``${False}``, the response content will be immediately downloaded. |
        | ``cert`` | if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair. |

        For more updated and complete information verify the official Requests api documentation:
        https://requests.readthedocs.io/en/latest/api/

        """
        response = self._common_request('get', None, url,
                                        params=params, **kwargs)
        self._check_status(expected_status, response, msg)
        return response

    @keyword('POST')
    @warn_if_equal_symbol_in_url_session_less
    def session_less_post(self, url, data=None, json=None,
                          expected_status=None, msg=None, **kwargs):
        """
        Sends a POST request.

        The endpoint used to send the request is the ``url`` parameter, while its body
        can be passed using ``data`` or ``json`` parameters.

        ``data`` can be a dictionary, list of tuples, bytes, or file-like object.
        If you want to pass a json body pass a dictionary as ``json`` parameter.

        By default this keyword fails if a status code with error values is returned in the response,
        this behavior can be modified using the ``expected_status`` and ``msg`` parameters,
        read more about it in `Status Should Be` keyword documentation.
        In order to disable this implicit assert mechanism you can pass as ``expected_status`` the values ``any`` or
        ``anything``.

        Other optional requests arguments can be passed using ``**kwargs``
        see the `GET` keyword for the complete list.
        """
        response = self._common_request('post', None, url,
                                        data=data, json=json, **kwargs)
        self._check_status(expected_status, response, msg)
        return response

    @keyword('PUT')
    @warn_if_equal_symbol_in_url_session_less
    def session_less_put(self, url, data=None, json=None,
                         expected_status=None, msg=None, **kwargs):
        """
        Sends a PUT request.

        The endpoint used to send the request is the ``url`` parameter, while its body
        can be passed using ``data`` or ``json`` parameters.

        ``data`` can be a dictionary, list of tuples, bytes, or file-like object.
        If you want to pass a json body pass a dictionary as ``json`` parameter.

        By default this keyword fails if a status code with error values is returned in the response,
        this behavior can be modified using the ``expected_status`` and ``msg`` parameters,
        read more about it in `Status Should Be` keyword documentation.
        In order to disable this implicit assert mechanism you can pass as ``expected_status`` the values ``any`` or
        ``anything``.

        Other optional requests arguments can be passed using ``**kwargs``
        see the `GET` keyword for the complete list.
        """

        response = self._common_request("put", None, url,
                                        data=data, json=json, **kwargs)
        self._check_status(expected_status, response, msg)
        return response

    @keyword('HEAD')
    @warn_if_equal_symbol_in_url_session_less
    def session_less_head(self, url,
                          expected_status=None, msg=None, **kwargs):
        """
        Sends a HEAD request.

        The endpoint used to retrieve the HTTP headers is the ``url``.

        ``allow_redirects`` parameter is not provided, it will be set to ``${False}`` (as
        opposed to the default behavior ``${True}``).

        By default this keyword fails if a status code with error values is returned in the response,
        this behavior can be modified using the ``expected_status`` and ``msg`` parameters,
        read more about it in `Status Should Be` keyword documentation.
        In order to disable this implicit assert mechanism you can pass as ``expected_status`` the values ``any`` or
        ``anything``.

        Other optional requests arguments can be passed using ``**kwargs``
        see the `GET` keyword for the complete list.
        """
        response = self._common_request('head', None, url, **kwargs)
        self._check_status(expected_status, response, msg)
        return response

    @keyword('PATCH')
    @warn_if_equal_symbol_in_url_session_less
    def session_less_patch(self, url, data=None, json=None,
                           expected_status=None, msg=None, **kwargs):
        """
        Sends a PATCH request.

        The endpoint used to send the request is the ``url`` parameter, while its body
        can be passed using ``data`` or ``json`` parameters.

        ``data`` can be a dictionary, list of tuples, bytes, or file-like object.
        If you want to pass a json body pass a dictionary as ``json`` parameter.

        By default this keyword fails if a status code with error values is returned in the response,
        this behavior can be modified using the ``expected_status`` and ``msg`` parameters,
        read more about it in `Status Should Be` keyword documentation.
        In order to disable this implicit assert mechanism you can pass as ``expected_status`` the values ``any`` or
        ``anything``.

        Other optional requests arguments can be passed using ``**kwargs``
        see the `GET` keyword for the complete list.
        """
        response = self._common_request('patch', None, url,
                                        data=data, json=json, **kwargs)
        self._check_status(expected_status, response, msg)
        return response

    @keyword('DELETE')
    @warn_if_equal_symbol_in_url_session_less
    def session_less_delete(self, url,
                            expected_status=None, msg=None, **kwargs):
        """
        Sends a DELETE request.

        The endpoint used to send the request is the ``url`` parameter.

        By default this keyword fails if a status code with error values is returned in the response,
        this behavior can be modified using the ``expected_status`` and ``msg`` parameters,
        read more about it in `Status Should Be` keyword documentation.
        In order to disable this implicit assert mechanism you can pass as ``expected_status`` the values ``any`` or
        ``anything``.

        Other optional requests arguments can be passed using ``**kwargs``
        see the `GET` keyword for the complete list.
        """
        response = self._common_request("delete", None, url, **kwargs)
        self._check_status(expected_status, response, msg)
        return response

    @keyword('OPTIONS')
    @warn_if_equal_symbol_in_url_session_less
    def session_less_options(self, url,
                             expected_status=None, msg=None, **kwargs):
        """
        Sends a OPTIONS request.

        The endpoint used to retrieve the resource is the ``url``.

        By default this keyword fails if a status code with error values is returned in the response,
        this behavior can be modified using the ``expected_status`` and ``msg`` parameters,
        read more about it in `Status Should Be` keyword documentation.
        In order to disable this implicit assert mechanism you can pass as ``expected_status`` the values ``any`` or
        ``anything``.

        Other optional requests arguments can be passed using ``**kwargs``
        see the `GET` keyword for the complete list.
        """
        response = self._common_request("options", None, url, **kwargs)
        self._check_status(expected_status, response, msg)
        return response


from keras.datasets import mnist
from sklearn.preprocessing import StandardScaler
from Package.PCA import PCA
import matplotlib.pyplot as plt
import seaborn as sns

# CARGAR EL DATASET
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# DISTINGUIR ENTRE UNOS Y OCHOS
X_train = X_train[(y_train==0)|(y_train==8)]
y_train_ = y_train[(y_train==0)|(y_train==8)]
X_test = X_test[(y_test==0)|(y_test==8)]
y_test = y_test[(y_test==0)|(y_test==8)]

#EL DATASET ES DE TRES DIMENSIONES, SE PASA A 2D
X_test = X_test.reshape(X_test.shape[0],X_test.shape[1]*X_test.shape[2])

#SE ESCALAN LOS DATOS
x_test = StandardScaler().fit_transform(X_test)

#MODELO PCA
pca = PCA(n_components=2)
X_test_PCA = pca.fit_transform(X_test)

#GRAFICAR PCA
fig,ax=plt.subplots(1,1,figsize=(9,9))
sns.scatterplot(x = X_test_PCA[:,0]
                , y = X_test_PCA[:,1]
                , hue = y_test
                , palette = 'Paired'
                , legend = 'full')
plt.show()


import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from wd import *

# VIZUALIZE DATA FOR PRESENTATION

def city_groups(x):
    if x in ['21.0', '12.0', '8.0']:
        return 1 # Southwest
    elif x in ['10.0', '5.0', '4.0', '3.0', '6.0', '15.0']:
        return 2 # Midwest
    elif x in ['22.0', '9.0', '13.0', '11.0', '18.0']:
        return 3 # Southeast
    elif x in ['16.0', '14.0', '19.0', '20.0']:
        return 4 # Northwest
    elif x in ['7.0', '17.0']:
        return 5 # Northeast
    else:
        return 6 # South

mem_target = pd.read_csv(data_directory+'clean_members_w_target.csv')
mem_target.shape
mem_target['city'] = mem_target.city.astype(str).fillna('999')
mem_target['registered_via'] = mem_target.registered_via.astype(str).fillna('999')
mem_target = mem_target.drop('registration_init_time', axis=1)

# REGISTRATION METHOD PLOT
via = pd.DataFrame(mem_target.groupby('registered_via').is_churn_final.mean()).reset_index()
via = via.sort_values(by='is_churn_final', ascending=False)
via
plt.bar(x=via.registered_via.values, height=via.is_churn_final.values*100, color='#1e488f')
plt.ylim([0, 40])
plt.savefig(data_directory+'data_viz/registered_via_bar.png', transparent=True);

# AGE PLOT
mem_target_no_null_age = mem_target[(mem_target.bd != 999)]
age = pd.DataFrame(mem_target_no_null_age.groupby('bd').is_churn_final.mean()).reset_index()
age = age[(age.bd >= 15) & (age.bd <= 55)]
age = age.sort_values(by='bd', ascending=False)
age
barplot = plt.bar(x=age.bd.values, height=age.is_churn_final.values*100, color='#1e488f')
plt.ylim([0, 40])
plt.savefig(data_directory+'data_viz/age_bar.png', transparent=True);

# EXPLORE CUSTOMER SEGMENTS BY CHURN RATE
mem_target['is_24_or_older'] = mem_target.apply(lambda row: 1 if row.bd >=24 else 0, axis=1)
mem_target['city_groups'] = mem_target.city.apply(city_groups)
mem_target.info()

segs = pd.DataFrame(mem_target.groupby(['is_24_or_older', 'city_groups', 'registered_via']).is_churn_final.mean())\
.sort_values(by='is_churn_final', ascending=False).reset_index()
segs = segs.sort_values(by='is_churn_final', ascending=False).reset_index()
segs

plt.bar(x=segs.index.values, height=segs.is_churn_final.values, color='#1e488f')
plt.ylim([0, 0.4]);

# EXPLORE GROUPING CITIES BY CHURN RATE
city = pd.DataFrame(mem_target.groupby('city').is_churn_final.mean()).reset_index()
city = city.sort_values(by='is_churn_final', ascending=False)
city
plt.bar(x=city.city.values, height=city.is_churn_final.values*100, color='#1e488f');

cityg = pd.DataFrame(mem_target.groupby('city_groups').is_churn_final.mean()).reset_index()
cityg = cityg.sort_values(by='is_churn_final', ascending=False)
cityg['city_groups'] = cityg.city_groups.astype(str)

plt.bar(x=cityg.city_groups.values, height=cityg.is_churn_final.values*100, color='#1e488f')
plt.ylim([0, 40])
plt.savefig(data_directory+'data_viz/city_group.png', transparent=True);


# 152. Maximum Product Subarray
class Solution(object):
    def maxProduct(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        max_product = nums[0]
        imin = imax = nums[0]
        for i in xrange(1, len(nums)):
            if nums[i] < 0:
                imin, imax = imax, imin
            imax = max(imax * nums[i], nums[i])
            imin = min(imin * nums[i], nums[i])
            max_product = max(max_product, imax)
        return max_product


import random
import logging
import json
from itertools import combinations, filterfalse

logger = logging.getLogger(__name__)

class BalanceService:
    def CombinaisonsJoueurs(self, listeNJEJ):
        logger.info('Création liste équipe possible')
        EquipesPossibles = combinations(listeNJEJ, 4)
        EquipesPossibles = list(EquipesPossibles)

        result = []

        for equipe in EquipesPossibles:
            equipe = list(equipe) \
            + list(filterfalse(lambda joueur: joueur in equipe, listeNJEJ))
            result.append(equipe)
        return result

    def EquilibrageEquipes(self, listeJoueur):
        logger.info('Service Equilibrage d\'équipe')
        combinaisonPossible = self.CombinaisonsJoueurs(listeJoueur)
        listeJoueurRandomiser = list(listeJoueur)
        logger.info('Equipe initiale : ' + json.dumps(listeJoueurRandomiser))

        eloSumSave = self.CalculEloEquipes(listeJoueur)

        for combinaison in combinaisonPossible:
            eloSumCalculated = self.CalculEloEquipes(combinaison)
            if self.differenceEloEquipe(eloSumCalculated) < self.differenceEloEquipe(eloSumSave):
                listeJoueur = list(combinaison)
                eloSumSave = eloSumCalculated
            if eloSumSave == 0:
                return listeJoueur
        self.enregistrementTeam(listeJoueur)
        logger.info('Repartition d\'équipe finale : ' +
                    json.dumps(listeJoueur))
        return listeJoueur

    def enregistrementTeam(self, listeJoueur):
        return 'NotImplemented'

    def differenceEloEquipe(self, EloSumEquipe):
        logger.info('Calcul de la différence de niveau de l\'équipe : ' +
                    str(abs(EloSumEquipe[0]-EloSumEquipe[1])))
        return abs(EloSumEquipe[0]-EloSumEquipe[1])

    def melangeEquipe(self, listeJoueur):
        random.shuffle(listeJoueur)
        logger.info('Melange aléatoire de l\'équipe : ' +
                    json.dumps(listeJoueur))

    def CalculEloEquipes(self, listeJoueur):
        logger.info('Calcul Niveau de l\'équipe')
        SumE1 = 0
        SumE2 = 0
        c = 0
        while c < 4:
            SumE1 += listeJoueur[c]["rating"]
            SumE2 += listeJoueur[c+4]["rating"]
            c += 1
        sortie = [SumE1, SumE2]
        return sortie

    def CalculBonusEco(self, listeJoueur):
        logger.info('Calcul du bonus ECO de l\'équipe')
        liste = [0, 0]
        CompteurEquipe = 0
        while CompteurEquipe < 2:
            CompteurJoueur = 0
            while CompteurJoueur < 4:
                RangCiv = self.ListeCiv.index(
                    listeJoueur[CompteurJoueur+4*CompteurEquipe]['civ'])
                NoteCiv = self.BonusEco[RangCiv][1]
                liste[CompteurEquipe] += NoteCiv
                CompteurJoueur += 1
            CompteurEquipe += 1
        return liste
    # Retourne une liste avec la note des bonus civ de chaque équipe.

    def EquivalenceBonusEco(self, liste):
        logger.info('Calcul Equivalence Bonus Eco')
        if abs(liste[1]-liste[0]) < random.randint(0, 5):
            return 0
        return 1
    # Vérification que les bonus eco des civs sont assez équilibrés.

    def creationEquipe(self, equipeExemple, equipeCible):
        compteurPala = self.compteurCivPala(equipeExemple)
        compteurChalou = self.compteurCivChaLou(equipeExemple)
        return self.creationEquipeCible(equipeCible, compteurPala, compteurChalou)

    def compteurCivPala(self, equipe):
        return 'NotImplemented'

    def compteurCivChaLou(self, equipe):
        return 'NotImplemented'

    def creationEquipeCible(self, equipe, compteurPala, compteurChalou):
        return 'NotImplemented'

    def TirageSortCiv(self, listeJoueurs):
        logger.info('Determination des Civ et position')
        CopieListeJoueurs = list(listeJoueurs)
        CopieCivPocket = list(self.CivPocket)
        CopieCivFlank = list(self.CivFlank)
        config = [[1, CopieCivFlank], [2, CopieCivPocket],
                  [3, CopieCivPocket], [4, CopieCivFlank]]
        listeResultatJoueur = []
        numeroEquipe = 0
        while numeroEquipe < 2:
            numeroJoueur = 0
            while numeroJoueur < 4:
                joueur = CopieListeJoueurs.pop(
                    random.randint(0, 3-numeroJoueur))
                # On va maintenant  supprimer cette civilisation de la liste en question pour ne pas qu'un autre joueur tombe dessus, il y a donc 2 cas (cf CivPocket et CivFlank)
                joueur['color'] = self.calculPositionEquipe(
                    config[numeroJoueur], numeroEquipe)
                joueur['civ'] = self.popCiv(config[numeroJoueur][1])
                logger.info('Joueur : ' + json.dumps(joueur))
                listeResultatJoueur.append(joueur)
                numeroJoueur += 1
            numeroEquipe += 1
        return listeResultatJoueur
    # Fonction permettant d'attribuer au hasard des civs et des positions à chaque joueur en évitant les doublons mais pas forcément les inégalités au niveau des bonus eco. La sortie est sous format (NomJoueur,Couleur,Civ)

    def popCiv(self, listCivilization):
        random.shuffle(listCivilization)
        return listCivilization.pop()

    def calculPositionEquipe(self, config, numeroEquipe):
        return config[0] + 4*numeroEquipe

    # Partie donnnées:

    ListeCiv = ['Aztecs', 'Berbers', 'Britons', 'Bulgarians', 'Burmese', 'Byzantines', 'Celts', 'Chinese', 'Cumans', 'Ethiopians', 'Franks', 'Goths', 'Huns', 'Incas', 'Indians', 'Italians', 'Japanese',
                'Khmer', 'Koreans', 'Lithuanians', 'Magyars', 'Malay', 'Malians', 'Mayans', 'Mongols', 'Persians', 'Portuguese', 'Saracens', 'Slavs', 'Spanish', 'Tatars', 'Teutons', 'Turks', 'Vietnamese', 'Vikings']
    # Simple liste des civilisations par ordre alphabétique.

    BonusEco = [['Aztecs', 10], ['Berbers', 5], ['Britons', 9], ['Bulgarians', 5], ['Burmese', 7], ['Byzantines', 6], ['Celts', 7], ['Chinese', 8], ['Cumans', 6], ['Ethiopians', 6], ['Franks', 8], ['Goths', 5], ['Huns', 9], ['Incas', 6], ['Indians', 9], ['Italians', 7], ['Japanese', 6], [
        'Khmer', 11], ['Koreans', 4], ['Lithuanians', 6], ['Magyars', 4], ['Malay', 8], ['Malians', 9], ['Mayans', 10], ['Mongols', 8], ['Persians', 10], ['Portuguese', 5], ['Saracens', 4], ['Slavs', 8], ['Spanish', 7], ['Tatars', 5], ['Teutons', 5], ['Turks', 6], ['Vietnamese', 7], ['Vikings', 10]]
    # Civilisations notées en fonction de la puissance de leur bonus éco sur une map de type arabia (8 moutons, 2 sangliers des baies et des cerfs, mais pas de poissons).

    CivPocket = ['Berbers', 'Bulgarians', 'Burmese', 'Chinese', 'Cumans', 'Franks', 'Goths', 'Huns', 'Indians', 'Khmer',
                 'Lithuanians', 'Malians', 'Magyars', 'Mongols', 'Persians', 'Slavs', 'Spanish', 'Tatars', 'Teutons', 'Turks']
    # Civilisations que les joueurs experts ont estimé être viable en pocket sur une map type Arabia.

    CivFlank = ['Aztecs', 'Britons', 'Byzantines', 'Celts', 'Chinese', 'Ethiopians', 'Incas', 'Italians', 'Japanese',
                'Koreans', 'Malay', 'Mayans', 'Mongols', 'Portuguese', 'Saracens', 'Tatars', 'Vietnamese', 'Vikings']
    # Civilisations que les joueurs experts ont estimé être viable en flank sur une map type Arabia.

    # Partie entrée:

    def run(self, listeJoueur):
        logger.info('Lancement de l\'équilibrage : ')
        ListeBonus = [0, 3]
        listeJoueur = self.EquilibrageEquipes(listeJoueur)
        listeJoueur = self.TirageSortCiv(listeJoueur)
        ListeBonus = self.CalculBonusEco(listeJoueur)
        while self.EquivalenceBonusEco(ListeBonus) != 0:
            listeJoueur = self.TirageSortCiv(listeJoueur)
            ListeBonus = self.CalculBonusEco(listeJoueur)

        logger.info('Match Final : ' + json.dumps(listeJoueur))

print("Idée de only_ju, codé par Rokafly et Spirit_White sur les conseils de Barney, OGN, Sitaux et Slidou.")


#Stack using array

l=[]
top=0
for i in range(int(input('Enter the number of elements in the stack : '))):
    l.append(int(input()))

yn=input('Do you want to remove an element from the stack (y/n) ').lower()
if yn=='n' :
    quit()
else:
    print(l.pop())
    print("remaing elements in list ->",*l)

#Using linked list

class Node :
    def __init__(self,data):
        self.data=data
        self.next=None

class linkedlist:

    #Declaring top and head node
    def __init__(self):
        self.top=None
        self.head=None

    #To push to the stack
    def push(self,data) :
        node=Node(data)
        if self.top is None:
            self.head=node
            self.top=node
        else :
            self.top.next=node
            self.top=self.top.next

    #To pop from the stack
    def pop(self):
        if self.top is None:
            print('Empty stack ')
        else :
            itr=self.head
            while itr.next is not None:
                prev=itr
                itr=itr.next
            self.top=prev
            self.top.next=None

    #To print the stack
    def printstack(self) :
        if self.head is None:
            print('Empty stack')
        else :
            itr=self.head
            while itr.next is not None :
                print(f"{itr.data}-->",end='')
                itr=itr.next
            print(itr.data)

if __name__=="__main__":
    l=linkedlist()
    l.push(1)
    l.push(2)
    l.push(3)
    l.push(4)
    l.printstack()
    l.pop()
    l.printstack()


import pickle
import logging
from tqdm import tqdm
from argparse import ArgumentParser
import json

import numpy as np

from metax.datasets import get_formatted_data
from metax.models.mybart import MyBart
from metax.models.utils import (convert_model_to_single_gpu, freeze_embeds, trim_batch)
from metax.task_manager.dataloader import GeneralDataset
# ImportError: cannot import name '_prepare_bart_decoder_inputs' from 'transformers.models.bart.modeling_bart'
# from transformers.models.bart.modeling_bart import _prepare_bart_decoder_inputs
from transformers import BartTokenizer

import torch
from torch import Tensor
from torch.nn import functional as F
from torch.nn import Module
import random

class BaseIndexManager():
    def __init__(self, logger, args=None):
        super().__init__()
        self.name = "base_index_manager"
        self.logger=logger
        self.index_args = args
        self.memory_examples = {}   # <-- store_memory()

    def get_memory_size(self):
        return len(self.memory_examples)

    def load_memory_from_path(self, init_memory_cache_path):
        raise NotImplementedError

    def save_memory_to_path(self, memory_pkl_path):
        raise NotImplementedError

    def retrieve_from_memory(self, query_examples, sample_size, **kwargs):
        raise NotImplementedError

    def store_examples(self, examples):
        raise NotImplementedError

class BaseMemoryManager:
    def __init__(self):
        pass

class RandomMemoryManger(BaseMemoryManager):
    def __init__(self, logger, args=None):
        # super().__init__(self, logger, args=args)
        self.logger = logger
        self.name = "random_memory_manager"
        self.memory_examples = {}

    # def set_up_initial_memory(self, initial_memory_path="", formatted_examples=None):
    #     self._load_init_memory_examples(initial_memory_path, formatted_examples)

    def load_memory_from_path(self, init_memory_cache_path):
        with open(init_memory_cache_path, "rb") as f:
            memory_cache = pickle.load(f)
            self.logger.info(f"Load the cache to {f.name}")
        self.memory_examples = memory_cache["memory_examples"]

    def save_memory_to_path(self, memory_pkl_path):
        memory_cache = {}
        memory_cache["memory_examples"] = self.memory_examples
        with open(memory_pkl_path, "wb") as f:
            pickle.dump(memory_cache, f)
            self.logger.info(f"Saved the cache to {f.name}")

    def retrieve_from_memory(self, query_examples=None, sample_size=-1, seed=None, backup_json_path=None, **kwargs):
        assert sample_size > 0
        self.logger.info("Randomly retrieve from the memory. `query_examples` not used")
        key_len = len(self.memory_examples.keys())
        if (sample_size > key_len):
            self.logger.warning(f"Tried to sample {sample_size} items from a population of {key_len}. Sampling all instead.")
            sample_size = key_len
        sample_random = random.Random(seed)
        self.logger.info(f"seed={seed} in retrieve_from_memory")
        retrieved_example_ids = sample_random.sample(sorted(list(self.memory_examples.keys())), sample_size)
        self.logger.info(f"retrieved_example_ids[:5] = {retrieved_example_ids[:5]}")
        retrieved_examples = [self.memory_examples[rid] for rid in retrieved_example_ids]
        return retrieved_examples

    def store_examples(self, examples, mode="input"):
        for item in examples:
            # Note that we only keep the all answers here now.
            self.memory_examples[item[2]] = (item[0], item[1], item[2])
        self.logger.info(f"Save {len(self.memory_examples)} examples to the memory.")


# -*- coding: utf-8 -*-
from setuptools import setup, find_packages

with open('requirements.txt') as f:
    install_requires = f.read().strip().split('\n')

# get version from __version__ variable in hepsiburadacom/__init__.py
from hepsiburadacom import __version__ as version

setup(
    name='hepsiburadacom',
    version=version,
    description='TR hepsiburada.com integration for ErpNext',
    author='Framras AS-Izmir',
    author_email='bilgi@framras.com.tr',
    packages=find_packages(),
    zip_safe=False,
    include_package_data=True,
    install_requires=install_requires
)


import cv2
import os
import argparse
import logging

logging.log(1, 'Using OpenCV {}'.format(cv2.__version__))
parser = argparse.ArgumentParser()
parser.add_argument('--dataroot', type=str,
					required=True,
					help='Path to videos folder to extrat frames from')
parser.add_argument('--outf', type=str,
					required=True,
					help='Output directory')
parser.add_argument('--v', action='store_true')
parser.add_argument('--savefreq', type=int, default=25,
					help='Save one pic every savefreq frames')

opt = parser.parse_args()
outf = opt.outf
dataroot = opt.dataroot
if opt.v:
	print_every = 1000

if not os.path.isdir(dataroot):
	raise OSError('Dataroot directory not found')
if not os.path.isdir(outf):
    os.mkdir(opt.outf)

dataroot_files = os.listdir(dataroot)
dataroot_vid_files = [os.path.join(dataroot, f) for f in dataroot_files if f[-3:] == 'mp4']

for i, vid_path in enumerate(dataroot_vid_files):
	vidcap = cv2.VideoCapture(vid_path)
	success, image = vidcap.read()
	count = 0
	success = True
	vid_name = os.path.split(vid_path)[-1]
	vid_name = vid_name[:-4]

	while success:
		success, image = vidcap.read()
		if count % opt.savefreq == 0:
			out_path = os.path.join(outf, 'vid_{}_frame_{}.png'.format(vid_name, str(count).zfill(10)))
			cv2.imwrite(out_path, image)
		if opt.v and count % print_every == 0:
			print('Saved {} frames'.format(count))
		count += 1


# -*- coding: utf-8 -*-
"""Windows Registry plugin for parsing the last shutdown time of a system."""

from __future__ import unicode_literals

from dfdatetime import filetime as dfdatetime_filetime
from dfdatetime import semantic_time as dfdatetime_semantic_time

from plaso.containers import events
from plaso.containers import time_events
from plaso.lib import definitions
from plaso.lib import errors
from plaso.parsers import winreg
from plaso.parsers.winreg_plugins import dtfabric_plugin
from plaso.parsers.winreg_plugins import interface

class ShutdownWindowsRegistryEventData(events.EventData):
  """Shutdown Windows Registry event data.

  Attributes:
    key_path (str): Windows Registry key path.
    value_name (str): name of the Windows Registry value.
  """

  DATA_TYPE = 'windows:registry:shutdown'

  def __init__(self):
    """Initializes event data."""
    super(ShutdownWindowsRegistryEventData, self).__init__(
        data_type=self.DATA_TYPE)
    self.key_path = None
    self.value_name = None

class ShutdownWindowsRegistryPlugin(
    dtfabric_plugin.DtFabricBaseWindowsRegistryPlugin):
  """Windows Registry plugin for parsing the last shutdown time of a system."""

  NAME = 'windows_shutdown'
  DESCRIPTION = 'Parser for ShutdownTime Registry value.'

  FILTERS = frozenset([
      interface.WindowsRegistryKeyPathFilter(
          'HKEY_LOCAL_MACHINE\\System\\CurrentControlSet\\Control\\Windows')])

  _DEFINITION_FILE = 'filetime.yaml'

  def _ParseFiletime(self, byte_stream):
    """Parses a FILETIME date and time value from a byte stream.

    Args:
      byte_stream (bytes): byte stream.

    Returns:
      dfdatetime.Filetime: FILETIME date and time value or None if no
          value is set.

    Raises:
      ParseError: if the FILETIME could not be parsed.
    """
    filetime_map = self._GetDataTypeMap('filetime')

    try:
      filetime = self._ReadStructureFromByteStream(
          byte_stream, 0, filetime_map)
    except (ValueError, errors.ParseError) as exception:
      raise errors.ParseError(
          'Unable to parse FILETIME value with error: {0!s}'.format(
              exception))

    if filetime == 0:
      return None

    try:
      return dfdatetime_filetime.Filetime(timestamp=filetime)
    except ValueError:
      raise errors.ParseError(
          'Invalid FILETIME value: 0x{0:08x}'.format(filetime))

  def ExtractEvents(self, parser_mediator, registry_key, **kwargs):
    """Extracts events from a ShutdownTime Windows Registry value.

    Args:
      parser_mediator (ParserMediator): mediates interactions between parsers
          and other components, such as storage and dfvfs.
      registry_key (dfwinreg.WinRegistryKey): Windows Registry key.
    """
    shutdown_value = registry_key.GetValueByName('ShutdownTime')
    if not shutdown_value:
      return

    try:
      date_time = self._ParseFiletime(shutdown_value.data)
    except errors.ParseError as exception:
      parser_mediator.ProduceExtractionWarning(
          'unable to determine shutdown timestamp with error: {0!s}'.format(
              exception))
      return

    if not date_time:
      date_time = dfdatetime_semantic_time.SemanticTime('Not set')

    event_data = ShutdownWindowsRegistryEventData()
    event_data.key_path = registry_key.path
    event_data.offset = shutdown_value.offset
    event_data.value_name = shutdown_value.name

    event = time_events.DateTimeValuesEvent(
        date_time, definitions.TIME_DESCRIPTION_LAST_SHUTDOWN)
    parser_mediator.ProduceEventWithEventData(event, event_data)

winreg.WinRegistryParser.RegisterPlugin(ShutdownWindowsRegistryPlugin)


import sys
r = sys.stdin.readline

while True:
    n = r().strip()
    if n != '':
        n = int(n)
        if n == 0 or n == 1: print(1)
        elif n == 2: print(3)
        else:
            result = [0] * (n + 1)
            result[1], result[2] = 1, 3

            for i in range(3, n + 1):
                result[i] += (result[i - 1] + 2 * result[i - 2])

            print(result[n])
    else:
        break


a, b = map(int, input().strip().split(' '))
for i in range(0, b) :
    print("*"*a)

"""
[ 두 번째 풀이 ]

a, b = map(int, input().strip().split(' '))
tmp = ""
for i in range(b):
    for j in range(a):
        tmp += "*"
    print(tmp)
    tmp = ""
"""