from APIDownloader import APIDownloader
from argparse import ArgumentParser
from pyspark import SparkContext, StorageLevel
from pyspark.sql import HiveContext
import json

if __name__ == "__main__":

    sc = SparkContext()
    sqlContext = HiveContext(sc)

    parser = ArgumentParser()
    parser.add_argument("-f", "--outputFolder", type=str, help="Output foldername", required=True)
    parser.add_argument("-t", "--team", type=str, help="Team Name", required=True)
    parser.add_argument("-d", "--date", type=str, help="Greater than equal date", required=True)
    parser.add_argument("-p", "--password", type=str, help="password for connecting to hyperion gray api", required=True)

    args = parser.parse_args()
    print ("Got arguments:", args)

    url = "https://effect.hyperiongray.com/api/leaked-source/email?query=*@"
    domains = [ "alaska.edu",
                "apple.afsmith.bm",  #No results
                "bremertonhousing.org",
                "clixsense.com",
                "Empireminecraft.com", #No results
                "eurekalert.org",
                "feverclan.com",
                "floridabar.org", #No results
                "i-dressup.com",
                "jivesoftware.com",
                "justformen.com",  #No result
                "Last.fm",
                "manaliveinc.org",
                "newseasims.com",
                "saintfrancis.com",
                "ssctech.com",  #No result
                "unm.edu",  #No result
                "usc.edu", #No result
                "wpcapital.com"
            ]

    apiDownloader = APIDownloader(sc, sqlContext)

    result_rdds = list()
    for domain in domains:
        results = apiDownloader.download_api(url + domain, "isi", args.password)
        if results is not None:
            if "results" in results:
                if len(results["results"]) > 0:
                    rdd = sc.parallelize(results["results"])
                    apiDownloader.load_into_cdr(results["results"], "hg_leaked_source", args.team, "hg-leaked-source")
                    result_rdds.append(rdd)

    if len(result_rdds) > 0:
        all_rdd = result_rdds[0]
        for rdd in result_rdds[1:]:
            all_rdd = all_rdd.union(rdd)

        all_rdd.map(lambda x: ("hg-leaked-source", json.dumps(x))).saveAsSequenceFile(args.outputFolder + "/hg-leaked-source")


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Jesse Rubin - project Euler
"""
Poker hands
Problem 54
In the card game poker, a hand consists of five cards and are ranked, from
lowest to highest, in the following way:

High Card: Highest value card.
One Pair: Two cards of the same value.
Two Pairs: Two different pairs.
Three of a Kind: Three cards of the same value.
Straight: All cards are consecutive values.
Flush: All cards of the same suit.
Full House: Three of a kind and a pair.
Four of a Kind: Four cards of the same value.
Straight Flush: All cards are consecutive values of same suit.
Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.

The cards are valued in the order:
2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.

If two players have the same ranked hands then the rank made up of the highest
value wins; for example, a pair of eights beats a pair of fives (see example 1
below). But if two ranks tie, for example, both players have a pair of queens,
then highest cards in each hand are compared (see example 4 below); if the
highest cards tie then the next highest cards are compared, and so on.

Consider the following five hands dealt to two players:

Hand	 	Player 1	 	Player 2	 	Winner
1	 	5H 5C 6S 7S KD   2C 3S 8S 8D TD     Player 2
        Pair of Fives    Pair of Eights

2	 	5D 8C 9S JS AC   2C 5C 7D 8S QH     Player 1
        High card Ace    High card Queen

3	 	2D 9C AS AH AC   3D 6D 7D TD QD     Player 2
        Three Aces       Flush w/ Diamonds

4	 	4D 6S 9H QH QC   3D 6D 7H QD QS     Player 1
        Pair of Queens   Pair of Queens
        High card Nine   High card Seven

5	 	2H 2D 4C 4D 4S   3C 3D 3S 9S 9D     Player 1
        Full House       Full House
        w/ Three Fours    w/ Three Threes

The file, poker.txt, contains one-thousand random hands dealt to two players.
Each line of the file contains ten cards (separated by a single space): the
first five are Player 1's cards and the last five are Player 2's cards. You can
assume that all hands are valid (no invalid characters or repeated cards),
each player's hand is in no specific order, and in each hand there is a clear
winner.

How many hands does Player 1 win?
"""
from collections import Counter

class Card(object):

    def __init__(self, val, suit, strang):
        self.val = val
        self.suit = suit
        self.strang = strang

    @classmethod
    def string_to_card(self, strang):
        chars = [c for c in strang]
        val = 0
        suit = chars[1]
        try:
            val = int(chars[0])
        except ValueError:
            if chars[0] == 'T':
                val = 10
            elif chars[0] == 'J':
                val = 11
            elif chars[0] == 'Q':
                val = 12
            elif chars[0] == 'K':
                val = 13
            elif chars[0] == 'A':
                val = 14
        return Card(val, suit, strang)

    def __str__(self):
        return self.strang

    def __repr__(self):
        return self.strang

class PokerHand(object):

    def __init__(self, cards):
        self.cards = cards
        self.suits_counter = Counter(card.suit for card in cards)
        self.vals_counter = Counter(card.val for card in cards)
        self.rank_counter = self.evaluate_rank()

    def evaluate_rank(self):
        """
        0 --- High Card: Highest value card.
        1 --- One Pair: Two cards of the same value.
        2 --- Two Pairs: Two different pairs.
        3 --- Three of a Kind: Three cards of the same value.
        4 --- Straight: All cards are consecutive values.
        5 --- Flush: All cards of the same suit.
        6 --- Full House: Three of a kind and a pair.
        7 --- Four of a Kind: Four cards of the same value.
        8 --- Straight Flush: All cards are consecutive values of same suit.
        8.5 --- Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.
        """
        rank_counter = Counter()
        rank_counter[0] = max(v for v in self.vals_counter.keys())
        low_card = min(v for v in self.vals_counter.keys())

        for val, count in self.vals_counter.items():
            if count == 4:
                rank_counter[7] = val
            if count == 3:
                rank_counter[3] = val
            if count == 2 and 1 not in rank_counter:
                rank_counter[1] = val
            if count == 2 and 1 in rank_counter:
                if val > rank_counter[1]:
                    rank_counter[2] = val
                elif val < rank_counter[1]:
                    temp = rank_counter[1]
                    rank_counter[1] = val
                    rank_counter[2] = temp

        # check for full house
        if 3 in rank_counter and 1 in rank_counter:
            rank_counter[6] = rank_counter[3]

        # check for flushes and straights
        flush = True if len(self.suits_counter) == 1 else False

        if 14 in self.vals_counter.keys() and 2 in self.vals_counter.keys():
            straight = True if set(self.vals_counter.keys()) == {2, 3, 4, 5, 14} else False
        else:
            straight = True if len(self.vals_counter) == 5 and rank_counter[0]-(low_card-1) == 5 else False

        if flush and straight:  # don't really need to deal with rank 9
            if rank_counter[0] == 14 and low_card == 10:
                rank_counter[9] = rank_counter[0]
            else:
                rank_counter[8] = rank_counter[0]
        elif flush and not straight:
            rank_counter[5] = rank_counter[0]
        elif not flush and straight:
            if 14 in self.vals_counter.keys() and 2 in self.vals_counter.keys():
                rank_counter[4] = 5
            else:
                rank_counter[4] = rank_counter[0]
        return rank_counter

    def __str__(self):
        return " ".join(c.__str__() for c in self.cards)

    def __repr__(self):
        return " ".join(c.__str__() for c in self.cards)

    def __gt__(self, p2_hand):
        for rank in range(9, -1, -1):
            if rank in self.rank_counter and rank not in p2_hand.rank_counter:
                return True
            if rank not in self.rank_counter and rank in p2_hand.rank_counter:
                return False
            if rank in self.rank_counter and rank in p2_hand.rank_counter:
                if self.rank_counter[rank] > p2_hand.rank_counter[rank]:
                    return True
                elif self.rank_counter[rank] < p2_hand.rank_counter[rank]:
                    return False
        return None

with open("../txt_files/p054_poker.txt") as f:
    games = [game.strip('\n').split(' ') for game in f.readlines()]

def p054():
    p1_wins = 0
    for g in games:
        for c in g:
            Card.string_to_card(c)
        player_1 = PokerHand([Card.string_to_card(card_string) for card_string in g[:5]])
        player_2 = PokerHand([Card.string_to_card(card_string) for card_string in g[5:]])
        if player_1 > player_2:
            p1_wins += 1
    return p1_wins

if __name__ == '__main__':
    answer = p054()
    print("Player 1 wins {} times".format(answer))


"""New character dialog"""

from PyQt5 import QtCore, QtGui, QtWidgets
from os import path

from . import commands, util
from npc.character import Character
from npc.commands.util import create_path_from_character
from .uis.new_character import Ui_NewCharacterDialog

class NewCharacterDialog(QtWidgets.QDialog, Ui_NewCharacterDialog):
    """Dialog for creating a new character"""

    def __init__(self, parent, prefs):
        """
        Create the new character dialog

        User inputs are stored in the dialog's `values` variable. They are
        updated immediately when the user makes a change.

        args:
            parent (QtWindow): Parent for the dialog
            prefs (Settings): Settings object to use for commands
        """

        QtWidgets.QDialog.__init__(self, parent)
        Ui_NewCharacterDialog.__init__(self)

        self.prefs = prefs
        self.type_specific_widgets = []
        self.current_vbox_height_offset = 0
        self.values = {
            "command": commands.create_standard,
            "name": "",
            "ctype": "",
            "dead": False,
            "foreign": False,
            "location": "",
            "groups": [],
        }

        self.setupUi(self)

        self.path_dislpay = QtWidgets.QLabel(self)
        self.infoForm.insertRow(5, 'Path:', self.path_dislpay)
        self.path_timer = QtCore.QTimer()
        self.path_timer.setSingleShot(True)
        self.path_timer.setInterval(300)
        self.path_timer.timeout.connect(self.update_path)

        self.typeSelect.currentTextChanged.connect(lambda text: self.set_value("ctype", text))
        self.characterName.textChanged.connect(lambda text: self.set_value("name", text))
        self.groupName.textChanged.connect(lambda text: self.set_value("groups", [text]))
        self.foreignBox.toggled.connect(self.set_foreign)
        self.foreignText.textChanged.connect(self.set_foreign)
        self.deceasedBox.toggled.connect(self.set_deceased)
        self.deceasedText.textChanged.connect(self.set_deceased)
        self.locName.textChanged.connect(lambda text: self.set_value("location", text))

        self._setup_type_select()

    def _setup_type_select(self):
        """
        Populate type selector and set default state
        """
        for type_key in sorted(self.prefs.get_available_types()):
            item = self.typeSelect.addItem(type_key.title(), userData=type_key)
        default_index = self.typeSelect.findText(self.prefs.get('gui.defaults.character_type').title())
        if default_index != -1:
            self.typeSelect.setCurrentIndex(default_index)
            self.update_type_specific_controls(default_index)
        self.typeSelect.currentIndexChanged.connect(self.update_type_specific_controls)

    def set_value(self, key, value):
        """
        Set a value

        This helper is designed to be called from a Qt signal connection using a
        lambda.

        Args:
            key (str): Key to set
            value (varies): Value to store
        """

        self.values[key] = value
        self.path_timer.start()

    def update_path(self):
        """Update the path label based on current values"""

        values = self.values.copy()

        tags = {}
        tags['type'] = values['ctype']
        if values['groups']:
            tags['group'] = values.pop('groups')

        tags.update({k: v for (k, v) in values.items() if v})

        temp_char = Character()
        temp_char.merge_all({**self.prefs.get('tag_defaults'), **tags})

        template_path = self.prefs.get('types.{}.sheet_template'.format(temp_char.type_key))
        char_name = values['name']
        if char_name:
            filename = char_name + path.splitext(template_path)[1]
        else:
            filename = ''
        base_path = create_path_from_character(temp_char, prefs=self.prefs)
        final_path = path.join(base_path, filename)

        path_exists = path.exists(final_path)

        if path_exists:
            self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(False)
        else:
            self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(True)

        if path_exists and char_name:
            self.path_dislpay.setText("Character already exists")
        else:
            self.path_dislpay.setText(final_path)

    def set_foreign(self, _):
        """Special handling for the compound `foreign` value"""

        if self.foreignBox.isChecked():
            if self.foreignText.text():
                self.set_value("foreign", self.foreignText.text())
            else:
                self.set_value("foreign", True)
        else:
            self.set_value("foreign", False)

    def set_deceased(self, _=None):
        """Special handling for the compound `deceased` value"""

        if self.deceasedBox.isChecked():
            if self.deceasedText.toPlainText():
                self.set_value("dead", self.deceasedText.toPlainText())
            else:
                self.set_value("dead", True)
        else:
            self.set_value("dead", False)

    def update_type_specific_controls(self, index):
        """
        Change the visible form fields based on the selected character type

        Args:
            index (int): Index of the type selection
        """

        for widget in self.type_specific_widgets:
            self.infoForm.labelForField(widget).deleteLater()
            widget.deleteLater()
        self.type_specific_widgets = []

        new_vbox_height_offset = 0
        type_key = self.typeSelect.itemData(index)
        if type_key == 'changeling':
            new_vbox_height_offset = self.create_changeling_controls()
        elif type_key == 'werewolf':
            new_vbox_height_offset = self.create_werewolf_controls()
        else:
            self.create_basic_controls()

        new_vbox_height_offset += len(self.type_specific_widgets)*6

        self.resize(
            self.width(),
            self.height() - self.current_vbox_height_offset + new_vbox_height_offset)
        self.current_vbox_height_offset = new_vbox_height_offset

    def new_row(self, index, title, widget):
        """
        Add a new row of controls to the form

        Args:
            index (int): Where to place the row in the form
            title (str): Label text for the row
            widget (QtWidget): Widget for the row

        Returns:
            The height of the row, as gotten from the widget
        """

        self.infoForm.insertRow(index, title, widget)
        self.type_specific_widgets.append(widget)
        return widget.height()

    def create_basic_controls(self):
        """
        Set up the base controls

        This just means resetting the tab order and internal data structures.

        All create_*_controls methods should return the height of their controls.

        Returns:
            Zero, since this doesn't create anything
        """
        self.set_value("command", commands.create_standard)
        self.setTabOrder(self.characterName, self.groupName)
        return 0

    def create_changeling_controls(self):
        """
        Set up the changeling-specific controls

        Adds the seeming, kith, and court rows
        """
        new_vbox_height_offset = 0
        seeming_select = QtWidgets.QComboBox(self)
        new_vbox_height_offset += self.new_row(2, '&Seeming', seeming_select)
        self.setTabOrder(self.characterName, seeming_select)

        kith_select = QtWidgets.QComboBox(self)
        new_vbox_height_offset += self.new_row(3, '&Kith', kith_select)
        self.setTabOrder(seeming_select, kith_select)

        courtInput = QtWidgets.QLineEdit(self)
        new_vbox_height_offset += self.new_row(4, '&Court', courtInput)
        self.setTabOrder(kith_select, courtInput)
        self.setTabOrder(courtInput, self.groupName)

        def update_kiths(_=0):
            """Update the kith options from the selected seeming"""
            kith_select.clear()
            kith_select.addItems(seeming_select.currentData())

        seeming_select.currentIndexChanged.connect(update_kiths)
        seeming_select.currentTextChanged.connect(lambda text: self.set_value('seeming', text))
        kith_select.currentTextChanged.connect(lambda text: self.set_value('kith', text))
        courtInput.textChanged.connect(lambda text: self.set_value("court", text))

        for seeming in self.prefs.get('changeling.seemings'):
            seeming_select.addItem(seeming.title(), userData=[kith.title() for kith in self.prefs.get('changeling.kiths.{}'.format(seeming))])

        self.set_value("command", commands.create_changeling)

        return new_vbox_height_offset

    def create_werewolf_controls(self):
        """
        Set up the werewolf-specific controls

        Adds the auspice and tribe rows
        """
        new_vbox_height_offset = 0

        auspice_select = QtWidgets.QComboBox(self)
        new_vbox_height_offset += self.new_row(2, '&Auspice', auspice_select)
        for auspice in self.prefs.get('werewolf.auspices'):
            auspice_select.addItem(auspice.title())
        self.setTabOrder(self.characterName, auspice_select)

        tribe_select = QtWidgets.QComboBox(self)
        new_vbox_height_offset += self.new_row(2, '&Tribe', tribe_select)
        for tribe in self.prefs.get('werewolf.tribes.moon'):
            tribe_select.addItem(tribe.title())
        tribe_select.insertSeparator(tribe_select.count())
        for tribe in self.prefs.get('werewolf.tribes.pure'):
            tribe_select.addItem(tribe.title())
        self.setTabOrder(auspice_select, tribe_select)
        self.setTabOrder(tribe_select, self.groupName)

        auspice_select.currentTextChanged.connect(lambda text: self.set_value('auspice', text))
        tribe_select.currentTextChanged.connect(lambda text: self.set_value('tribe', text))

        return new_vbox_height_offset

    def run(self):
        """
        Show the dialog

        Returns:
            True if the OK button was pressed, False if not. Use the values
            variable to retrieve the user's inputs.
        """
        self.characterName.setFocus()
        result = self.exec_()
        return result == self.Accepted and self.values['name']


'''
This is the default app controller for portality.
For inclusion in your own project you should make your own version of this controller
and include the views you require, as well as writing new ones. Of course, views must
also be backed up by models, so have a look at the example models and use them / write
new ones as required too.
'''

from flask import Flask, request, abort, render_template
from flask.views import View
from flask.ext.login import login_user, current_user

import portality.models as models
from portality.core import app, login_manager

from portality.view.account import blueprint as account
from portality.view.graph import blueprint as graph
from portality.view.query import blueprint as query
from portality.view.stream import blueprint as stream
from portality.view.question import blueprint as question
from portality.view.answer import blueprint as answer
from portality.view.mine import blueprint as mine
from portality.view.leviathan import blueprint as leviathan

app.register_blueprint(account, url_prefix='/account')
app.register_blueprint(graph, url_prefix='/graph')
app.register_blueprint(query, url_prefix='/query')
app.register_blueprint(stream, url_prefix='/stream')
app.register_blueprint(question, url_prefix='/question')
app.register_blueprint(answer, url_prefix='/answer')
app.register_blueprint(mine, url_prefix='/mine')
app.register_blueprint(leviathan)

@login_manager.user_loader
def load_account_for_login_manager(userid):
    out = models.Account.pull(userid)
    return out

@app.context_processor
def set_current_context():
    """ Set some template context globals. """
    return dict(current_user=current_user, app=app)

@app.before_request
def standard_authentication():
    """Check remote_user on a per-request basis."""
    remote_user = request.headers.get('REMOTE_USER', '')
    if remote_user:
        user = models.Account.pull(remote_user)
        if user:
            login_user(user, remember=False)
    # add a check for provision of api key
    elif 'api_key' in request.values:
        res = models.Account.query(q='api_key:"' + request.values['api_key'] + '"')['hits']['hits']
        if len(res) == 1:
            user = models.Account.pull(res[0]['_source']['id'])
            if user:
                login_user(user, remember=False)

@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

@app.errorhandler(401)
def page_not_found(e):
    return render_template('401.html'), 401

if __name__ == "__main__":
    app.run(host='0.0.0.0', debug=app.config['DEBUG'], port=app.config['PORT'])


# 01/05/2020
# Testing training with Keras API

from keras.models import Sequential
from keras.layers import Dense
import numpy as np
import tensorflow as tf

# random seed for reproducibility
np.random.seed(7)
# print(np.__version__)

# loading the raag dataset
dataset = np.loadtxt("raag_dataset.csv", delimiter=',')
outputs = np.loadtxt("outputs.csv", delimiter=',')
# split into input (X) and output (Y) variables, splitting csv data

X = dataset[:, 0:2686764]
Y = outputs[:, 0:]
print("break point 1")

# create model, add dense layers one by one specifying activation function
model = Sequential()
model.add(Dense(22, input_dim=2686764, activation='relu'))
print("break point 2")
model.add(Dense(15, activation='relu'))
model.add(Dense(20, activation='relu'))
print("break point 3")
model.add(Dense(8, activation='relu'))
model.add(Dense(10, activation="relu"))
model.add(Dense(4, activation='sigmoid'))
print("break point 4")

# compile the model, adam gradient descent (optimized)
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
print("break point 5")
# call the function to fit to the dat (training the network)
model.fit(X, Y, epochs=200, batch_size=10)
print("break point 6")

# evaluate the model
scores = model.evaluate(X, Y)
print("%s: %0.2f%%" %(model.metrics_names[1], scores[1]*100))
print("break point 7")

# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)

# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")

# importing a csv of a clip of Yemen Raag
testClip = np.loadtxt("C1_fmod.csv", delimiter=',')
print(np.shape(testClip))
print(type(testClip))

if testClip.ndim == 1:
    testClip = np.array([testClip])
    print(np.shape(testClip))
    print(type(testClip))

# make class predictions with the model
predictions = model.predict(testClip)
print(predictions)
print(np.shape(predictions))
print(type(predictions))
#
# # summarize the first 5 cases
# for i in range(5):
#     print('%d ' % (predictions[i]))

# (2686764,) but got array with shape (1,)


####################
# Project 1
# FYS-STK 3155/4155
# Fall 2018
####################

####################
# Franke function - given in exercise
####################

# Import necessary packages
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
from random import random, seed
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression, Ridge, Lasso
from sklearn.metrics import mean_squared_error, r2_score
from time import time
import matplotlib.mlab as mlab
from imageio import imread
"""
# Load the terrain
#terrain1 = imread('terrainone.tif')
#x, y = imread('terrainone.tif')
terrain1 = imread('terrainone.tif')
[n,m] = terrain1.shape

    ## Find some random patches within the dataset and perform a fit

patch_size_row = 100
patch_size_col = 50

    # Define their axes
rows = np.linspace(0,1,patch_size_row)
cols = np.linspace(0,1,patch_size_col)

[C,R] = np.meshgrid(cols,rows)

x = C.reshape(-1,1)
y = R.reshape(-1,1)

print(np.shape(x), np.shape(y))

num_data = patch_size_row*patch_size_col

    # Find the start indices of each patch

num_patches = 5

np.random.seed(4155)

row_starts = np.random.randint(0,n-patch_size_row,num_patches)
col_starts = np.random.randint(0,m-patch_size_col,num_patches)
# Show the terrain
print(np.shape(terrain1))
"""
"""
plt.figure()
plt.title('Terrain over Norway 1')
plt.imshow(terrain1, cmap='gray')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
"""

def FrankeFunction(x,y):
    term1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))
    term2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))
    term3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))
    term4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)
    return term1 + term2 + term3 + term4

# Make the dataset
n = 100					# number of datapoints
x = np.random.uniform(0.0,1.0, n)       # create a random number for x-values in dataset
y = np.random.uniform(0.0,1.0, n)       # create a random number for y-values in dataset with noise
#print(np.shape(x), np.shape(y))
def polynomialfunction(x, y, type):
    if type==1:
        X = np.c_[np.ones((n,1)) , x, y]

    elif type==2:
        X = np.c_[np.ones((n,1)) , x, y, x**2, x*y, y**2]

    elif type==3:
        X = np.c_[np.ones((n,1)) , x, y, x**2, x*y, y**2, \
                x**3, x**2*y, x*y**2, y**3]

    elif type==4:
        X = np.c_[np.ones((n,1)) , x, y, x**2, x*y, y**2, \
                x**3, x**2*y, x*y**2, y**3, \
                x**4, x**3*y, x**2*y**2, x*y**3,y**4]

    elif type==5:
        X = np.c_[np.ones((n,1)) , x, y, x**2, x*y, y**2, \
                x**3, x**2*y, x*y**2, y**3, \
                x**4, x**3*y, x**2*y**2, x*y**3,y**4, \
                x**5, x**4*y, x**3*y**2, x**2*y**3,x*y**4, y**5]
    else:
        print('Degree out of range!')

    return X

#X = polynomialfunction(x,y,type=5)# Give your wish for degree as type

z = FrankeFunction(x, y) + 0.9*np.random.randn(1) # z with noise

def OLS(X, z):
    beta = np.linalg.pinv(X.T.dot(X)).dot(X.T).dot(z)
    zpredict = X.dot(beta)
    return quality(z,zpredict)

#z, zpredict = OLS(X,z)
# The mean squared error

def quality(z,zpredict):

    mse = mean_squared_error(z,zpredict)
    #print('Mean square error: %.5f' % mse)
    #print("Mean squared error scikitlearn: %.5f" % mean_squared_error(z, zpredict))

    # Explained variance score: 1 is perfect prediction
    R2 = 1- (np.sum((z-zpredict)**2))/(np.sum((z-np.mean(z))**2))
    #print('R2 score: %.5f' % R2)
    #print('R2 score scitkitlearn: %.5f' % r2_score(z, zpredict))

    #Some other variances:
    #var=1.0/z.shape[0] *np.sum((z - np.mean(z))**2)
    #betavar=1.0/z.shape[0] *np.sum((beta - np.mean(beta))**2)
    #print('Variance: %.5f'% var)
    #print('Variance of beta', betavar)
    return mse, R2

# Ridge and Lasso:
def ridge(x, y, z, X, lmb):
    #np.random.seed(4155)

    n_samples = 100

    x_ = x-np.mean(x)
    y_ = y-np.mean(y)
    z_ = z-np.mean(z) #Needed?

    X_ = np.delete(X,0,1)
    #print(np.shape(X_))
    """
    X_ = np.c_[x, y, x**2, x*y, y**2, \

                    x**3, x**2*y, x*y**2, y**3, \

                    x**4, x**3*y, x**2*y**2, x*y**3,y**4, \

                    x**5, x**4*y, x**3*y**2, x**2*y**3,x*y**4, y**5] # Check this! What is this?
    """

    #lmb_values = [1e-1]#, 1e-3, 1e-2, 10, 1e2, 1e4]
    #num_values = len(lmb_values)

    ## Ridge-regression of centered and not centered data
    #beta_ridge = np.zeros((X.shape[1],num_values))
    #beta_ridge_centered = np.zeros((X.shape[1],num_values))

    IX = np.eye(X.shape[1])
    IX_ = np.eye(X_.shape[1])
    #print(np.shape(z),np.shape(X_))

    beta_ridge = (np.linalg.pinv( X.T @ X + lmb*IX) @ X.T @ z).flatten() #maybe change to pinv
    beta_ridge_centered = (np.linalg.pinv( X_.T @ X_ + lmb*IX_) @ X_.T @ z_).flatten() #pinv?

    pred_ridge =  X @ beta_ridge # Shape: 100x6 from 6 lambda-values
    #print(np.shape(pred_ridge))
    pred_ridge_centered =  X_ @ beta_ridge_centered + z_

    ### R2-score of the results
    #print('lambda = %g'%lmb)
    #print('r2 for scikit: %g'%r2_score(z,pred_ridge_scikit[:,i]))
    #print('r2 for own code, not centered: %g'%r2_score(z,pred_ridge))
    #print('r2 for own, centered: %g\n'%r2_score(z,pred_ridge_centered))

    return quality(z, pred_ridge)

#Lasso:
def lasso(X,z, alpha):
    lasso=Lasso(alpha)
    lasso.fit(X,z)
    predl=lasso.predict(X)
    #print("Lasso Coefficient: ", lasso.coef_)
    #print("Lasso Intercept: ", lasso.intercept_)
    #print("R2 score:", r2_score(z,predl))
    return quality(z, predl)

"""
plt.scatter(X,z,color='green', label="Training Data")
plt.plot(X, predl, color='blue', label="Lasso")
plt.legend()
plt.show()
"""
"""
print(np.shape(x_train))
poly5 = PolynomialFeatures(5)
#X_train = poly5.fit_transform()

X_train = np.c_[x_train,y_train]
X_test = np.c_[x_test, y_test]
lasso=Lasso(alpha=0.1)
lasso.fit(X_train,z_train)
predl=lasso.predict(z_test)
print("Lasso Coefficient: ", lasso.coef_)
print("Lasso Intercept: ", lasso.intercept_)
"""

# Plotting:
# Importing necessary packages for plotting

def plotFrankeFunction(x, y, z, type):
    #x = np.sort(x)
    #y = np.sort(y)
    x, y = np.meshgrid(x, y)
    #z = z.reshape(-1,1)
    print(x.shape)
    print(z.shape)
    z = FrankeFunction(x,y)
    #n,m = x.shape
    #z = z.reshape(n,m)

    fig = plt.figure()
    ax = fig.gca(projection='3d')
    surf = ax.plot_surface(x, y, z, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_zlabel('z')
    if type==1:
        plt.title('Franke function with actual z')
    elif type==2:
        plt.title('Franke function with our prediction of z')
    # Add a color bar which maps values to colors.
    fig.colorbar(surf, shrink=0.5, aspect=5)
    plt.show()

    # Customize the z axis.
    #ax.set_zlim(-0.10, 1.40)
    #ax.zaxis.set_major_locator(LinearLocator(10))
    #ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
    return surf

# Plotting Franke function with actual z:
#plotFrankeFunction(x, y, z, type=1)

# Plotting Franke function with our prediction of z:
#plotFrankeFunction(x, y, zpredict, type=2)
"""
# Plotting Ridge:
def plotRidge(x,y):
    # Sorting
    sort_ind = np.argsort(x[:,0])

    x_plot = x[sort_ind,0]
    x_centered_plot = x_[sort_ind,0]

    pred_ls_plot = pred_ls[sort_ind,0]
    pred_ridge_plot = pred_ridge[sort_ind,:]
    pred_ridge_centered_plot = pred_ridge_centered[sort_ind,:]

    # Plott not centered
    plt.plot(x_plot,pred_ls_plot,label='ls')

    for i in range(num_values):
        plt.plot(x_plot,pred_ridge_plot[:,i],label='ridge, lmb=%g'%lmb_values[i])

    plt.plot(x,y,'ro')

    plt.title('linear regression on un-centered data')
    plt.legend()

    # Plott centered
    plt.figure()

    for i in range(num_values):
        plt.plot(x_centered_plot,pred_ridge_centered_plot[:,i],label='ridge, lmb=%g'%lmb_values[i])

    plt.plot(x_,y,'ro')

    plt.title('linear regression on centered data')
    plt.legend()

    # 2.

    pred_ridge_scikit =  np.zeros((n_samples,num_values))
    for i,lmb in enumerate(lmb_values):
        pred_ridge_scikit[:,i] = (Ridge(alpha=lmb,fit_intercept=False).fit(X,y).predict(X)).flatten() # fit_intercept=False fordi bias er allerede i X

    plt.figure()

    plt.plot(x_plot,pred_ls_plot,label='ls')

    for i in range(num_values):
        plt.plot(x_plot,pred_ridge_scikit[sort_ind,i],label='scikit-ridge, lmb=%g'%lmb_values[i])

    plt.plot(x,y,'ro')
    plt.legend()
    plt.title('linear regression using scikit')

    plt.show()
    return

plotRidge(x, y)
"""

def bootstrap(data, percent):
    size = percent*len(data)
    train = np.random.choice(len(data),int(size))
    test = list(set(range(len(data))) - set(train))
    return train, test

iterations = 100

mse_OLS = np.zeros((5,iterations))
mse_Ridge = np.zeros((5,iterations))
mse_Lasso = np.zeros((5,iterations))
r2score_OLS = np.zeros((5,iterations))
r2score_Ridge = np.zeros((5,iterations))
r2score_Lasso = np.zeros((5,iterations))

#mse_OLS[4][10] = 5
#print(mse_OLS)
lmb = 0.01
alpha = 0.0001

for i in range(iterations):
    train_indices, test_indices = bootstrap(z, 0.7)

    for j in range(5):
        X = polynomialfunction(x,y,type=(j+1))
        X_train = X[train_indices]; #print(X_train.shape)
        X_test = X[test_indices]; #print(X_test.shape)
        z_train = z[train_indices];# print(z_train.shape)
        z_test = z[test_indices]; #print(z_test.shape)

        mse_OLS[j][i], r2score_OLS[j][i] = OLS(X_train,z_train)

        mse_Ridge[j][i], r2score_Ridge[j][i] = ridge(x,y,z_train,X_train,lmb)

        mse_Lasso[j][i], r2score_Lasso[j][i] = lasso(X_train,z_train,alpha)

"""
train_indices, test_indices = bootstrap(z, 0.7)
X = polynomialfunction(x,y,type=5)

X_train = X[train_indices]; #print(X_train.shape)
X_test = X[test_indices]; #print(X_test.shape)
z_train = z[train_indices];# print(z_train.shape)
z_test = z[test_indices]; #print(z_test.shape)
mse_Ridge[0][0], r2score_Ridge[0][0] = ridge(x,y,z_train,X_train)
mse_Lasso[0][0], r2score_Lasso[0][0] = lasso(X_train,z_train,alpha)
mse_OLS[0][0], r2score_OLS[0][0] = OLS(X_train,z_train)
print(z.shape[0])
print(mse_Ridge[0][0])
print(mse_Lasso[0][0])
print(mse_OLS[0][0])
"""
mse_OLS_average1 = np.mean(mse_OLS[0])
mse_OLS_average2 = np.mean(mse_OLS[1])
mse_OLS_average3 = np.mean(mse_OLS[2])
mse_OLS_average4 = np.mean(mse_OLS[3])
mse_OLS_average5 = np.mean(mse_OLS[4])

mse_Ridge_average1 = np.mean(mse_Ridge[0])
mse_Ridge_average2 = np.mean(mse_Ridge[1])
mse_Ridge_average3 = np.mean(mse_Ridge[2])
mse_Ridge_average4 = np.mean(mse_Ridge[3])
mse_Ridge_average5 = np.mean(mse_Ridge[4])

mse_Lasso_average1 = np.mean(mse_Lasso[0])
mse_Lasso_average2 = np.mean(mse_Lasso[1])
mse_Lasso_average3 = np.mean(mse_Lasso[2])
mse_Lasso_average4 = np.mean(mse_Lasso[3])
mse_Lasso_average5 = np.mean(mse_Lasso[4])

r2score_OLS_average1 = np.mean(r2score_OLS[0])
r2score_OLS_average2 = np.mean(r2score_OLS[1])
r2score_OLS_average3 = np.mean(r2score_OLS[2])
r2score_OLS_average4 = np.mean(r2score_OLS[3])
r2score_OLS_average5 = np.mean(r2score_OLS[4])

r2score_Ridge_average1 = np.mean(r2score_Ridge[0])
r2score_Ridge_average2 = np.mean(r2score_Ridge[1])
r2score_Ridge_average3 = np.mean(r2score_Ridge[2])
r2score_Ridge_average4 = np.mean(r2score_Ridge[3])
r2score_Ridge_average5 = np.mean(r2score_Ridge[4])

r2score_Lasso_average1 = np.mean(r2score_Lasso[0])
r2score_Lasso_average2 = np.mean(r2score_Lasso[1])
r2score_Lasso_average3 = np.mean(r2score_Lasso[2])
r2score_Lasso_average4 = np.mean(r2score_Lasso[3])
r2score_Lasso_average5 = np.mean(r2score_Lasso[4])

print("Lambda = ", lmb, "\n")
print("alpha = ", alpha, "\n")

print("The average mean sqared error for the different polynomial powers: \n")
print("OLS: ")
print("1. order: ", mse_OLS_average1, "2. order: ", mse_OLS_average2, "3. order: ", mse_OLS_average3, "4. order: ", mse_OLS_average4, "5. order: ", mse_OLS_average5, "\n")
print("Ridge: ")
print("1. order: ", mse_Ridge_average1, "2. order: ", mse_Ridge_average2, "3. order: ", mse_Ridge_average3, "4. order: ", mse_Ridge_average4, "5. order: ", mse_Ridge_average5, "\n")
print("Lasso: ")
print("1. order: ", mse_Lasso_average1, "2. order: ", mse_Lasso_average2, "3. order: ", mse_Lasso_average3, "4. order: ", mse_Lasso_average4, "5. order: ", mse_Lasso_average5, "\n")

print("")
print("----------------------------------------------------------------------------------------------------------")
print("")

print("The average R2 score for the different polynomial powers: \n")
print("OLS: ")
print("1. order: ", r2score_OLS_average1, "2. order: ", r2score_OLS_average2, "3. order: ", r2score_OLS_average3, "4. order: ", r2score_OLS_average4, "5. order: ", r2score_OLS_average5, "\n")
print("Ridge: ")
print("1. order: ", r2score_Ridge_average1, "2. order: ", r2score_Ridge_average2, "3. order: ", r2score_Ridge_average3, "4. order: ", r2score_Ridge_average4, "5. order: ", r2score_Ridge_average5, "\n")
print("Lasso: ")
print("1. order: ", r2score_Lasso_average1, "2. order: ", r2score_Lasso_average2, "3. order: ", r2score_Lasso_average3, "4. order: ", r2score_Lasso_average4, "5. order: ", r2score_Lasso_average5, "\n")

var=1.0/z.shape[0] *np.sum((z - np.mean(z))**2)
#print(var)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('embarazos', '0006_auto_20150104_1159'),
    ]

    operations = [
        migrations.AddField(
            model_name='fichaviolenciafamiliar',
            name='cefalea',
            field=models.NullBooleanField(default=None, verbose_name='Cefalea problemas de sue\xf1o (mucho sue\xf1o, interrupci\xf3n del sue\xf1o)'),
            preserve_default=True,
        ),
        migrations.AlterField(
            model_name='fichaviolenciafamiliar',
            name='quejas_cronicas',
            field=models.NullBooleanField(default=None, verbose_name='Quejas cr\xf3nicas sin causa f\xedsica'),
            preserve_default=True,
        ),
    ]


# Copyright 2011 Contributors (Jordan Andersen)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
A brute force Map/Reduce solution to the Travelling Salesman Problem. The
purpose of this example is to demonstrate how to use Map/Reduce on
computationally intense problems that involve a relatively small input.

See the Wikipedia article for details of the problem:
http://en.wikipedia.org/wiki/Travelling_salesman_problem

The solution works by having each mapper find the longest/shortest tour in a
chunk of the full range of the possible factorial(N-1) tours. (Where N is the
number of nodes in the graph). The reducers then pick the winners from each
mapper.
"""
__author__ = 'Jordan Andersen <jordandandersen@gmail.com>'

from mrjob.job import MRJob
from scipy.misc.common import factorial
import sys
import numpy

try:
    import simplejson as json
    json  # quiet "redefinition of unused ..." warning from pyflakes
except ImportError:
    import json

def map_int_to_tour(num_nodes, i, start_node):
    """Gets a unique tour through a graph given an integer and starting node.

    Args:
    num_nodes -- the number of nodes in the graph being toured
    i -- the integer to be mapped to the set of tours for the graph
    start_node -- the node index to begin and end the tour on
    """
    nodes_remaining = range(0,start_node) + range(start_node + 1, num_nodes)
    tour = []

    while len(nodes_remaining) > 0:
        num_nodes = len(nodes_remaining)
        next_step = nodes_remaining[i % num_nodes]
        nodes_remaining.remove(next_step)
        tour.append(next_step)
        i = i / num_nodes

    tour = [start_node] + tour + [start_node]
    return tour

def cost_tour(graph, tour):
    """Calculates the travel cost of given tour through a given graph.

    Args:
    graph -- A square numpy.matrix representing the travel cost of each edge on
            the graph.
    tour -- A list of integers representing a tour through the graph where each
            entry is the index of a node on the graph.
    """
    steps = zip(tour[0:-1], tour[1:])
    cost = sum([ graph[step_from,step_to] for step_from, step_to in steps])
    return cost

class MRSalesman(MRJob):

    def steps(self):
        """Defines the two steps, which are as follows:

        1.  Mapper splits the problem into reasonable chunks by mapping each
            possible tour to the integers and assigning each Step 2 mapper a
            range of tours to cost.
        2.  The mapper takes a range of tours and a description of the trip and
            yields the longest and shortests tours. The reduces yields the
            longest of the long and the shortest of the short tours.

        Notice the first step has no reducer. This allows all of the keys put
        out by the first step to be inputs to step 2's mappers without having
        to be reduced.
        """
        return ([self.mr(mapper=self.splitter),
                self.mr(mapper=self.mapper,
                        reducer=self.reducer,
                        mapper_final = self.mapper_final)]
                )

    def __init__(self, *args, **kwargs):
        """Initializes an instance of the MRSalesman class. See MRJob for
        arguments.

        Some instance variables are initialized here that will be modified
        with while mapping in step 2 and output but the step 2 mapper_final.
        """
        super(MRSalesman, self).__init__(*args, **kwargs)
        self.shortest_length = sys.maxint
        self.shortest_path = []
        self.longest_length = 0
        self.longest_path = []

    def splitter(self, key, line):
        """The mapper for step 1. Splits the range of possible tours into
        reasonably sized chunks for the consumption of the step 2 mappers.

        At this point the 'line' input should come directly from the first line
        of the one-line json file contains the edge cost graph and the starting
        node. The key is not relevant.
        """
        #loading the json description of the trip to get at the size
        #of the edge costgraph
        sales_trip = json.loads(line)
        m = numpy.matrix(sales_trip['graph'])
        num_nodes = m.shape[0]
        num_tours = factorial(num_nodes - 1)

        #Here we break down the full range of possible tours into smaller
        #pieces. Each piece is passed along as a key along with the trip
        #description.
        step_size = int(100 if num_tours < 100**2 else num_tours / 100)
        steps = range(0, num_tours, step_size) + [num_tours]
        ranges = zip(steps[0:-1], steps[1:])

        for range_low, range_high in ranges:
            #The key prepresents the range of tours to cost
            yield( ("%d-%d"%(range_low,range_high), sales_trip ))

    def mapper(self, key, sales_trip):
        """Mapper for step 2. Finds the shortest and longest tours through a
        small range of all possible tours through the graph.

        At this step the key will contain a string describing the range of
        tours to cost. The sales_trip has the edge cost graph and the starting
        node in a dict.
        """
        #This first line makes this function a generator function rather than a
        #normal function, which MRJob requires in its mapper functions. You need
        #to do this when all the output comes from the mapper_final.
        if False: yield
        matrix = numpy.matrix(sales_trip['graph'])
        num_nodes = matrix.shape[0]

        #The key prepresents the range of tours to cost
        range_low, range_high = map(int,key.split('-'))
        for i in range(range_low,range_high):

            tour = map_int_to_tour(num_nodes, i, sales_trip['start_node'])
            cost = cost_tour(matrix, tour)

            if cost < self.shortest_length:
                self.shortest_length = cost
                self.shortest_path = tour

            if cost > self.longest_length:
                self.longest_length = cost
                self.longest_path = tour

    def mapper_final(self):
        """Mapper_final for step 2. Outputs winners found by mapper."""
        yield ('shortest', (self.shortest_length, self.shortest_path))
        yield ('longest', (self.longest_length, self.longest_path))

    def reducer(self, key, winners):
        """Reducer for Step 2. Takes the shortest and longest from several
        mappers and/or reducers and yields the overall winners in each category.

        The winners are a list of winners from several mappers OR reducers for
        the given key.

        Run this reducer enough and eventually you get to the final winner in
        each key/category.
        """
        if key == "shortest":
            yield (key ,min(winners))
        if key == "longest":
            yield (key ,max(winners))

if __name__ == '__main__':
    MRSalesman.run()


from django.conf import settings
from django.conf.urls import url
from web.views import *

from django.contrib.auth import views as auth_views

urlpatterns = [
    url(r'^$', view=main_view, name="home"),
    url(r'^group_list/(?P<page>\d+)$', group_list),
    url(r'^signup/$', regist, name='signup'),
    url(r'^search/$', search_view, name='search'),
    url(r'^mypage/$', mypage_view, name='mypage'),
    url(r'^group/$', group_view,name="group"),
    url(r'^group/(?P<field>\d+)$', group_view,name="group"),
    url(r'^detail/(?P<pk>\d+)$', detail_view, name="detail"),
    url(r'^add_study/$', add_study_view, name="add_study"),
    url(r'^check_click/$', check_click, name="check_click"),
    url(r'^group_manage/(?P<pk>\d+)$', group_manage, name="group_manage"),
    url(r'^regist_group/(?P<pk>\d+)$', regist_group, name="regist_group"),
    url(r'^add_member/(?P<pk>\d+)$', add_member, name="add_member"),
    url(r'^member_manage/(?P<pk>\d+)$', member_manage, name="member_manage"),

    url(r'^userpage_view/$', userpage_view, name="userpage_view"),
    url(
        r'^login/$',
        view=auth_views.login,
        name='login',
        kwargs={
            'template_name': 'login.html'
        }
    ),
    url(
        r'^logout/$',
        auth_views.logout,
        name='logout',
        kwargs={
            'next_page': settings.LOGIN_URL,
        }
    ),

]


# -*- coding: utf-8 -*-
"""
Author  : Jas
Time    : 2018/5/31 20:07
"""
import requests
import re

def get_html_text(url):
    try:
        r = requests.get(url, timeout=30)
        r.raise_for_status()
        r.encoding = r.apparent_encoding

        return r.text
    except:
        return "爬取数据失败"

def parse_page(ls, html):
    try:
        price_pattern = re.compile(r'\"view_price\"\:\"\d+.\d{2}\"')
        title_pattern = re.compile(r'\"raw_title\"\:\".*?\"')

        price_ls = re.findall(price_pattern, html)
        title_ls = re.findall(title_pattern, html)

        for i in range(len(price_ls)):
            price = eval(price_ls[i].split(':')[1])
            title = eval(title_ls[i].split(':')[1])
            ls.append([price, title])
    except:
        print("")

def print_goods_list(ls):
    tpls = "{:8}\t{:16}"
    print(tpls.format("价格", "商品名称"))

    for g in ls:
        print(tpls.format(g[0], g[1]))

def main():
    goods = "电视"
    url = "https://s.taobao.com/search?q=" + goods
    info_list = []

    for i in range(4):
        try:
            url = url + "&s=" + str((i*44))
            html = get_html_text(url)
            parse_page(info_list, html)
        except:
            print("")

    print_goods_list(info_list)

if __name__ == '__main__':
    main()


"""掘金 单线程同步 rpc
"""

# coding: utf-8
# client.py

import json
import time
import struct
import socket
import logging

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

logger = logging.getLogger("server")

class Client(object):
    def __init__(self, host: str, port: int):
        self.host = host
        self.port = port
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect(("localhost", 8080))
        self.conn = s

    def run(self, conn_num: int):
        for i in range(conn_num):
            out, result = self.rpc("ping", f"ireader {i}")
            logger.info(f"out: {out}, ret:{result}")
            time.sleep(1)
        # 完成任务之后连接关闭
        self.conn.close()

    def rpc(self, action, params):
        request = json.dumps({"action": action, "params": params}).encode("utf-8")
        length_prefix = struct.pack("I", len(request))
        self.conn.sendall(length_prefix)
        self.conn.sendall(request)
        length_prefix = self.conn.recv(4)
        length, = struct.unpack("I", length_prefix)
        body = self.conn.recv(length)
        response = json.loads(body)
        return response["out"], response["result"]

if __name__ == '__main__':
    client = Client("127.0.0.1", 8080)
    client.run(10)


# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.db import transaction

@transaction.commit_on_success
@login_required
def sendPrivateMessage(request):
	from ask.message.forms import MessageForm
	from ask.models import PrivateMessage

	data = {}

	if request.POST:
		form = MessageForm(user=request.user, data=request.POST)
		if not request.POST.get("anonymous"):
			anonymousCheck = False
		else:
			anonymousCheck = True
		subject = request.POST.get("subject")
		content = request.POST.get("content")
		if form.is_valid():
			from ask.models import AppUser, BlockedUserHistory, FilteredWord
			from django.core.urlresolvers import reverse
			from django.shortcuts import HttpResponseRedirect
			from django.utils.html import strip_tags

			receiver = form.cleaned_data.get("receiver")
			anonymous = form.cleaned_data.get("anonymous")

			receiverUser = AppUser.objects.get(id=receiver)

			pm = PrivateMessage()

			pm.anonymous = anonymous
			pm.receiver = receiverUser
			pm.subject = subject
			pm.content = strip_tags(content)
			pm.user = request.user
			pm.receiver_anonymous = False

			if anonymous:
				buhs = BlockedUserHistory.objects.filter(user=receiverUser,anonymous=pm.receiver_anonymous,blocked_user=pm.user,blocked_user_anonymous=True,active=True)
				buhs_count = buhs.count()
			else:
				buhs = BlockedUserHistory.objects.filter(user=receiverUser,blocked_user=pm.user,blocked_user_anonymous=False,active=True)
				buhs_count = buhs.count()
				if buhs_count > 0:
					pm.blocked = True

			if pm.passesFilter(buhs_count):
				from django.db.models import F
				pm.filtered = False
				pm.receiver.unreaded_messages = F('unreaded_messages') + 1
				pm.receiver.save()
			else:
				pm.filtered = True

			pm.save()

			return HttpResponseRedirect(reverse("user_message_inbox_view")+"?filtro=Enviados&status=3")
		else:
			data["status"] = "Datos inválidos"

		data["subject"] = subject
		data["content"] = content

	else:
		anonymousCheck = request.user.configuration.message_anonymous_default
		form = MessageForm()

	data["anonymous"] = anonymousCheck
	data["form"]= form
	return render_to_response("message/send-private-message.html",data,context_instance = RequestContext(request))

@transaction.commit_on_success
@login_required
def messageInbox(request):
	from ask.models import PrivateMessage, Denounce

	data = {}
	pms = PrivateMessage.objects

	data["status"] = request.GET.get("status")

	#Filters
	filter = request.GET.get("filtro")
	if filter:
		filter = filter.lower()
		if filter != 'recibidos' and filter != 'enviados' and filter !='no-deseados':
			filter = 'recibidos'
	else:
		filter = 'recibidos'

	if filter == 'recibidos':
		#get only received messages
		pms = PrivateMessage.objects.filter(receiver=request.user,receiver_deleted=False,filtered=False)
		data['title'] = 'Mensajes Recibidos'
	elif filter == 'enviados':
		#get only sended messages
		pms = PrivateMessage.objects.filter(user=request.user,deleted=False)
		data['title'] = 'Mensajes Enviados'
	elif filter == 'no-deseados':
		#get only received filtered messages
		pms = PrivateMessage.objects.filter(receiver=request.user,receiver_deleted=False,filtered=True)
		data['title'] = 'No deseados / Spam'
	data['filter'] = filter

	#Aside search
	searchUser = request.GET.get("usuario")
	searchAnonymous = request.GET.get("anonymous")
	if searchAnonymous is not None and searchAnonymous == "on":
		data["anonymous"] = True
		if filter == 'recibidos':
			#get only received from anonymous messages
			pms = pms.filter(anonymous=True)
		elif filter == 'enviados':
			#get only sended to anonymous messages
			pms = pms.filter(receiver_anonymous=True)
	else:
		if searchUser is not None and searchUser != ""  and len(searchUser) < 21:
			try:
				if filter == 'recibidos':
					#get only received from searched user messages
					pms = pms.filter(user__nick__icontains=searchUser,anonymous=False)
				elif filter == 'enviados':
					#get only sended to searched user messages
					pms = pms.filter(receiver__nick__icontains=searchUser,receiver_anonymous=False)
				data["searchUser"] = searchUser
			except:
				searchUser = None
		else:
			searchUser = None
	searchSubject = request.GET.get("asunto")
	if searchSubject is not None and searchSubject != "" and len(searchSubject) < 71:
		try:
			#get only subject searched messages
			pms = pms.filter(subject__icontains=searchSubject)
			data["searchSubject"] = searchSubject
		except:
			searchSubject = None
	else:
		searchSubject = None

	data["messages"] = pms.order_by("-date")
	#delete messages
	if request.POST:
		error = False

		#view validation
		message_ids = request.POST.getlist("message_ids")
		message_id = request.POST.get("message_id")

		#many ids case
		if message_ids is not None and len(message_ids) > 0:
			if len(message_ids) == 1:
				message_id = message_ids[0]
			else:
				data["status"] = "Mensajes Eliminados"
				try:
					pms = PrivateMessage.objects.filter(id__in=message_ids)
					for pm in pms:
						if not pm.delete_message(request.user):
							error = True
							data["status"] = "Error de validación1"
							break
				except:
					error = True
					data["status"] = "Error de validación"
		else:
			data["status"] = "Error de validación"

		#only one id case
		if message_id is not None and message_id != "":
			data["status"] = "Mensaje Eliminado"
			try:
				pm = PrivateMessage.objects.get(id=message_id)
				if not pm.delete_message(request.user):
					error = True
					data["status"] = "Error de validación"
			except:
				error = True
				data["status"] = "Error de validación"

		if error:
			transaction.rollback()

	data["message_entity"] = Denounce.MESSAGE

	return render_to_response("message/message-inbox.html",data,context_instance = RequestContext(request))

@transaction.commit_on_success
@login_required
def readMessage(request,message_id):
	from ask.models import PrivateMessage,Denounce
	from django.shortcuts import HttpResponseRedirect

	data = {}

	message = PrivateMessage.objects.get(id=message_id)

	if message.user == request.user or message.receiver == request.user:
		from ask.message.forms import ReplyMessageForm

		if request.POST:

			user_deleted = False
			if message.receiver == request.user and message.user.deleted:
				user_deleted = True
				if not message.anonymous:
					data["status"] = "El usuario al que quieres enviar el mensaje ya no existe"
				#if message user is anonymous its send the message anyway to hide his identity
				#both cases mark new message receiver deleted as true to delete message when new message user delete it
			else:
				form = ReplyMessageForm(request.POST)

				content = request.POST.get("content")

				subject = request.POST.get("subject")

				if form.is_valid():
					from django.shortcuts import HttpResponseRedirect
					from django.core.urlresolvers import reverse
					from ask.models import BlockedUserHistory
					from django.utils.html import strip_tags

					if message.user == request.user:
						user = message.user
						receiver = message.receiver
						anonymous = False
						if message.anonymous:
							anonymous = True
						receiver_anonymous = False
						if message.receiver_anonymous:
							receiver_anonymous = True
					else:
						user = message.receiver
						receiver = message.user
						anonymous = False
						if message.receiver_anonymous:
							anonymous = True
						receiver_anonymous = False
						if message.anonymous:
							receiver_anonymous = True

					newmsg = PrivateMessage()
					newmsg.content = strip_tags(content)
					newmsg.receiver = receiver
					newmsg.user = user
					newmsg.anonymous = anonymous
					newmsg.receiver_anonymous = receiver_anonymous
					newmsg.subject = subject

					if anonymous:
						buhs = BlockedUserHistory.objects.filter(user=receiver,anonymous=receiver_anonymous,blocked_user=user,blocked_user_anonymous=True,active=True)
						buhs_count = buhs.count()
					else:
						buhs = BlockedUserHistory.objects.filter(user=receiver,blocked_user=user,blocked_user_anonymous=False,active=True)
						buhs_count = buhs.count()
						if buhs_count > 0:
							newmsg.blocked = True

					if newmsg.passesFilter(buhs_count):
						from django.db.models import F
						newmsg.filtered = False
						newmsg.receiver.unreaded_messages = F('unreaded_messages') + 1
						newmsg.receiver.save()
					else:
						newmsg.filtered = True

					if user_deleted:
						newmsg.receiver_deleted = True

					newmsg.save()

					return HttpResponseRedirect(reverse("user_message_inbox_view")+"?filtro=Enviados&status=3")
				else:
					data["replyMessage"] = content
					data['subject'] = subject
					data["status"] = "Error de validación"
		else:
			import re
			subject = message.subject
			if re.match("RE: ",subject) is None:
				subject = "RE: "+subject
			data['subject'] = subject

			form = ReplyMessageForm()
			if message.receiver == request.user:
				if not message.receiver_readed_message:
					if not message.filtered:
						request.user.unreaded_messages = request.user.unreaded_messages - 1
						request.user.save()
					message.receiver_readed_message = True
					message.save()

		data["message"] = message
		data["form"] = form

		data["message_entity"] = Denounce.MESSAGE

		return render_to_response("message/message-read.html",data,context_instance = RequestContext(request))
	else:
		return HttpResponseRedirect(settings.ASK_DEFAULT_URLS.get("user_not_authorized_url"))

@transaction.commit_on_success
@login_required
def blockUser(request):
	from ask.models import PrivateMessage, BlockedUserHistory
	from django.shortcuts import HttpResponseRedirect
	from django.core.urlresolvers import reverse

	if request.POST:

		try:
			message_id = request.POST.get("message_id")
			if message_id:
				pm = PrivateMessage.objects.get(id=message_id)
			else:
				return HttpResponseRedirect(reverse("user_message_inbox_view"))
		except PrivateMessage.DoesNotExist:
			return HttpResponseRedirect(reverse("user_message_inbox_view"))

		if request.user == pm.receiver:
			if not pm.blocked:
				#for anonymous identity reasons, if the message is anonymous then the history can create multiples rows to the
				#same anonymous user, if not, the user knows that that anonymous user has posted a previous message
				#if the message isnt anonymous, then the user cannot create multiples rows in history, because his identity could be revealed in
				#previous messages
				#user,blocked_user,blocked_user_anonymous,message_id are the restrictions
				#CARE WHEN CHANGE THIS PART
				if pm.anonymous:
					try:
						from django.db import IntegrityError

						BlockedUserHistory.objects.create(user=request.user,anonymous=pm.receiver_anonymous,blocked_user=pm.user,blocked_user_anonymous=True,message_id=pm.id,message_subject=pm.subject)
						pm.blocked = True
						pm.save()
						return HttpResponseRedirect(reverse("user_blocked_list_view")+"?status=1")
					except IntegrityError:
						return HttpResponseRedirect(reverse("user_blocked_list_view")+"?status=2")
				else:
					try:
						BlockedUserHistory.objects.get(user=request.user,blocked_user=pm.user,blocked_user_anonymous=False)
						return HttpResponseRedirect(reverse("user_blocked_list_view")+"?status=2")
					except BlockedUserHistory.DoesNotExist:
						BlockedUserHistory.objects.create(user=request.user,anonymous=pm.receiver_anonymous,blocked_user=pm.user,blocked_user_anonymous=False,message_id=pm.id,message_subject=pm.subject)
						PrivateMessage.objects.filter(user=pm.user,receiver=request.user,anonymous=False).update(blocked=True)
						pm.blocked = True
						pm.save()
						return HttpResponseRedirect(reverse("user_blocked_list_view")+"?status=1")
			else:
				return HttpResponseRedirect(reverse("user_message_inbox_view"))
		else:
			return HttpResponseRedirect(reverse("user_message_inbox_view"))
	else:
		return HttpResponseRedirect(reverse("user_message_inbox_view"))

@transaction.commit_on_success
@login_required
def deleteBlockedUser(request):
	from ask.models import PrivateMessage, BlockedUserHistory
	from django.shortcuts import HttpResponseRedirect
	from django.core.urlresolvers import reverse

	if request.POST:
		try:
			blockeduserhistory_id = request.POST.get("blockeduserhistory_id")
			if blockeduserhistory_id:
				buh = BlockedUserHistory.objects.get(id=blockeduserhistory_id)
			else:
				return HttpResponseRedirect(reverse("user_blocked_list_view"))
		except BlockedUserHistory.DoesNotExist:
			return HttpResponseRedirect(reverse("user_message_inbox_view"))

		if request.user == buh.user:
			#if its an anonymous block history then deactivate another anonymous blocks from that user
			if buh.blocked_user_anonymous:
				BlockedUserHistory.objects.filter(user=buh.user,blocked_user=buh.blocked_user,blocked_user_anonymous=True,anonymous=buh.anonymous).update(active=False)
				PrivateMessage.objects.filter(id=buh.message_id).update(blocked=False)
			else:
				PrivateMessage.objects.filter(user=buh.blocked_user,receiver=buh.user,anonymous=False,blocked=True).update(blocked=False)

			buh.delete()
			return HttpResponseRedirect(reverse("user_blocked_list_view")+"?status=3")
	else:
		return HttpResponseRedirect(reverse("user_blocked_list_view"))

@login_required
def blockedUserList(request):
	from ask.models import BlockedUserHistory

	data = {}

	data["status"] = request.GET.get("status")

	data["blocked_messages"] = BlockedUserHistory.objects.filter(user=request.user).order_by("-date")

	return render_to_response("message/blocked-users-history.html",data,context_instance = RequestContext(request))


# -*- coding: utf-8 -*-

import ast
import inspect
import os

import numpy as np
import scipy.io as sio

from sklearn.preprocessing import normalize

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

SKIP_LEVEL = 3

def set_base_indent_level():
    """Set base indentation stack level for `iprint()?  calls."""
    global SKIP_LEVEL

    level = 0

    frame = inspect.currentframe()
    while frame.f_back:
        level += 1
        frame = frame.f_back

    # print('Computed level: {}, result:{}'.format(level, level + 1))

    SKIP_LEVEL = level + 1

def get_indent(skip, more_level=0):
    """Get indentation string.

    :param skip: skip some levels (Default value = SKIP_LEVEL)
    :param more_level: indent more (Default value = 0)

    """
    level = 0

    frame = inspect.currentframe()
    while frame.f_back:
        level += 1
        frame = frame.f_back

    if skip > 0:
        level -= min(max(0, skip), level)
    if more_level > 0:
        level += more_level

    indent = "  " * level
    return indent

def iprint(msg, level=0, *args, **kwargs):
    """Write indented.

    :param msg: strint to write
    :param level: indent more (Default value = 0)
    :param *args: print args
    :param **kwargs: print kwargs

    """
    print("{}{}".format(get_indent(SKIP_LEVEL, more_level=level), msg), *args, **kwargs)

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

def compute_emb_norms(emb_all, time_range):
    """Compute embedding vector norms per timepoint and also normalize a copy of the embeddings.

    :param emb_all: embeddings dictionary for each timepoint (Key: "U_%d")
    :param time_range: range of timepoints
    :returns: normalized embeddings, norms per timepoint

    """
    emb_norms = dict()
    norm_all = list()

    # https://github.com/scikit-learn/scikit-learn/blob/7b136e9/sklearn/utils/extmath.py#L63
    # https://github.com/scikit-learn/scikit-learn/blob/7b136e9/sklearn/preprocessing/data.py#L1513

    for tx, time_point in enumerate(time_range):
        key = "U_{}".format(tx)
        emb = emb_all[key]  # embedding matrix

        # norms = np.linalg.norm(emb, axis=1)  # row norms
        # norms_nonzero = norms[norms == 0.0] = 1.0
        # ...
        emb_norm, norms = normalize(emb, return_norm=True)

        emb_norms[key] = emb_norm
        norm_all = norms

    # re-scale norms
    norm_all = np.array(norm_all)
    norm_all /= np.sum(norm_all, axis=0)  # TODO: scaling between [0,1] ?

    return emb_norms, norm_all

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

def find_extremes(
    emb_all,
    time_range,
    words,
    num=10,
    filter_years=None,
    inverse=False,
    add_dists=False,
):
    """Finds words with extreme sum of vector differences between the years.
    With param `filter_years` you can filter for first-last year instead of over all years.

    :param emb_all: embeddings dictionary
    :param time_range: range of timepoints
    :param words: list of words (position is id)
    :param num: number of words to return, if negative then all (Default value = 10)
    :param filter_years: filter for only those years in `time_range` (Default value = None)
    :param inverse: inverse sorting (return un-extremes) (Default value = False)
    :param add_dists: return tuple of word, distance instead of only word (Default value = False)
    :returns: list of words (if `add_dists` then tuple of word and distance)

    """
    if num == 0:
        return list()
    elif num < 0:
        num = len(words)

    diffs_all = list()  # list of diffs per vector
    # diffs_all = np.zeros((len(words), len(time_range) - 1))
    # iprint("? diffs_all: {}".format(diffs_all.shape))

    years = time_range
    if filter_years is not None:
        years = filter_years

    # compute differences between vectors between each year
    last_emb = None
    for year in years:
        key = "U_{}".format(time_range.index(year))
        emb = emb_all[key]  # embedding matrix
        # iprint("? emb: {}".format(emb.shape))

        if last_emb is None:
            last_emb = emb
            continue

        diffs = np.linalg.norm(last_emb - emb, axis=1)
        # iprint("? diffs: {}".format(diffs.shape))
        # diffs_all[:, tx - 1] = diffs  # TODO: assignment does not work???
        diffs_all.append(diffs)

    last_emb = None
    diffs_all = np.array(diffs_all)
    # iprint("? diffs_all: {}".format(diffs_all.shape))
    diffs_all = diffs_all.T
    # iprint("? diffs_all: {}".format(diffs_all.shape))

    # compute sum of distances between years
    dists_all = np.sum(np.absolute(diffs_all), axis=1)
    # iprint("? dists_all: {}".format(dists_all.shape))

    # sort
    if not inverse:
        dists_all *= -1.0
    sort_inds = np.argsort(dists_all)
    inds = sort_inds[:num]

    # get words
    words = [words[i] for i in inds]

    if add_dists:
        sort_dist = np.absolute(dists_all[sort_inds][:num])
        words = list(zip(words, sort_dist))
    # iprint("? Words: {}".format(list(zip(words, sort_dist))))

    return words

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

def main(embeddings_filename, time_range, words_file, result_dir):
    """Load data and compute things (distances).

    :param embeddings_filename: file with embeddings
    :param time_range: range of timepoints
    :param words_file: file with word list
    :param result_dir: output directory for results

    """
    set_base_indent_level()

    emb_all = sio.loadmat(embeddings_filename)
    # iprint("? emb_all.keys(): {}".format(emb_all.keys()))
    with open(words_file, "r", encoding="utf-8") as fin:
        words = [w.strip() for w in fin]

    iprint("* Compute norms ...")
    emb_norms, norm_all = compute_emb_norms(emb_all, time_range)

    # TODO: compute distances with projected 2D trajectory?
    # may need to TSNE for each word?

    iprint("* Find extreme words ...")
    extremes = find_extremes(emb_norms, time_range, words, num=10)
    iprint("# Extremes: {}".format(extremes), level=1)
    unextremes = find_extremes(emb_norms, time_range, words, num=10, inverse=True)
    iprint("# Un-Extremes: {}".format(unextremes), level=1)

    iprint("* Find extreme words first-last ...")
    extremes_fl = find_extremes(
        emb_norms,
        time_range,
        words,
        filter_years=(time_range[0], time_range[-1]),
        num=10,
    )
    iprint("# Extremes: {}".format(extremes_fl), level=1)
    unextremes_fl = find_extremes(
        emb_norms,
        time_range,
        words,
        filter_years=(time_range[0], time_range[-1]),
        num=10,
        inverse=True,
    )
    iprint("# Un-Extremes: {}".format(unextremes_fl), level=1)

def parse_args():
    """Parse arguments. (Has defaults.)

    :returns: Parsed (final) arguments.

    """
    embeddings_filename = "results/embeddings.mat"
    words_file = "data/wordlist.txt"
    result_dir = "results"
    time_range = (1990, 2009)  # 2015)  # range, total number of time points

    import argparse

    parser = argparse.ArgumentParser()

    parser.add_argument(
        "-e",
        "--emb-file",
        default=embeddings_filename,
        help="filename with embeddings, default: {}".format(embeddings_filename),
    )

    parser.add_argument(
        "--time-range",
        type=str,
        default=str(time_range),
        help='time range (years?), format: "year_start,year_end" default: {}'.format(
            str(time_range)
        ),
    )
    parser.add_argument(
        "-w",
        "--words-file",
        default=words_file,
        help="input filename with list of words, default: {}".format(words_file),
    )
    parser.add_argument(
        "--result-dir",
        default=result_dir,
        help="Folder with result and intermediate training files, default: {}".format(
            result_dir
        ),
    )

    args = parser.parse_args()

    try:
        time_range2 = range(*ast.literal_eval(args.time_range))
        args.time_range = time_range2
    except Exception as ex:
        print("! Default to default value for time_range, {}".format(ex))
        args.time_range = range(*time_range)

    args.time_range = range(args.time_range[0], args.time_range[-1] + 2)

    return args

if __name__ == "__main__":
    args = parse_args()

    # make results dir
    if not os.path.exists(args.result_dir):
        print("Make results dir: {}".format(args.result_dir))
        os.mkdir(args.result_dir)

    main(args.emb_file, args.time_range, args.words_file, args.result_dir)


#resizes all images in the directory

import cv2
import glob

all_file_names = glob.glob("sample_images/*.jpg")
print(all_file_names)

for filename in all_file_names:
    img= cv2.imread(filename, 1 )
    resized_image = cv2.resize(img,(100, 100))
    cv2.imwrite(f"{filename[:-4]}_resized.jpg", resized_image)


import re
from datetime import datetime

COLOR_NAMES = [
    'fg',
    'bg',
    'cc',
    'black',
    'light_black',
    'red',
    'light_red',
    'green',
    'light_green',
    'yellow',
    'light_yellow',
    'blue',
    'light_blue',
    'magenta',
    'light_magenta',
    'cyan',
    'light_cyan',
    'white',
    'light_white'
]

def make_colors_js(colors):
    """..."""

    js = []
    js_color_names = COLOR_NAMES[3::2]

    # Special colors
    js.append(f"foregroundColor: '{colors[0]}',")
    js.append(f"backgroundColor: '{colors[1]}',")
    js.append(f"cursorColor: '{colors[2]}',")
    js.append("colors: {")

    # All other colors
    colors = colors[3:]

    color_pairs = [
        tuple(colors[i * 2 - 2:i * 2])
        for i in range(1, len(colors[3:]) - 4)
    ]
    for i, color in enumerate(js_color_names):
        js.append(f"  {color}: '{color_pairs[i][0]}',")
        js.append(f"  light{color.capitalize()}: '{color_pairs[i][1]}',")

    # Close JSON
    js.append('}')

    return '\n'.join(js)

def get_colors(lines):
    """Return hex colors as a list."""

    patt = re.compile('\#\w+')

    return [
        patt.search(line).group(0)
        for line in lines
        if patt.search(line)
    ]

def get_file_lines(filename):
    f = open(filename)
    lines = f.read().splitlines()
    f.close()

    return lines

def xterm_to_hyper(filename):

    x_file = open(filename)
    xterm = x_file.read().splitlines()
    x_file.close()

    colors = get_colors(xterm)

    with open('colors.json', 'w') as json:
        json.write(make_colors_js(colors))

def xterm_to_kitty(filename):
    """Create a file for kitty prefs."""
    kitty_lines = []
    lines = get_file_lines(filename)

    for line in lines:
        if len(line) >= 1:
            if line[0] == '!':
                # convert to a kitty style comment
                line = '#{}'.format(line[1:])
                print(line)
                kitty_lines.append(line)
            elif line[0] == '*':
                # get rid of *.
                line = line[2:].replace(':', '')
                print(line)

                kitty_lines.append(line)

    with open('to_kitty_out', 'w') as output:
        output.write('\n'.join(kitty_lines))

def xterm_to_yaml(filename):
    date = datetime.now().strftime('%Y-%m-%d')
    outfile_name = f"{date}-{filename}.md"

    yaml_lines = [
        '---',
        'layout: color-scheme',
        'title: <TITLE>',
        'tags: color-scheme',
        'parent:',
        '    title: Terminal Themes',
        '    url: /term-themes/',
        'colors:',
    ]

    yaml_meta = [
        'downloads:',
        f"    kitty: /assets/colors/{filename}/kitty.conf",
        f"    xresources: /assets/colors/{filename}/.Xresources",
        'screenshots: <SCREENSHOT>',
        '---',
    ]

    lines = get_file_lines(filename)

    colors = get_colors(lines)

    for i, color in enumerate(COLOR_NAMES):
        color_line = "    {}: '{}'".format(color, colors[i])
        print(color_line)
        yaml_lines.append(color_line)

    yaml_lines.extend(yaml_meta)

    with open(outfile_name, 'w') as output:
        output.write('\n'.join(yaml_lines))

if __name__ == "__main__":
    import sys

    options = {
        '--yaml': xterm_to_yaml,
        '--kitty': xterm_to_kitty,
        '--json': xterm_to_hyper
    }
    mode = sys.argv[1]
    filename = sys.argv[2]

    options[mode](filename)


from utils import IO

class Intcode():
  """ Class containing the intcode computer."""

  def __init__(self,file_location,input):
    """ Read file and give input to the intcode computer."""
    self.x = IO.read_file(file_location)
    self.n = len(self.x)
    self.i = 0
    self.input = input

  def parse_opcode(self,op):
    """ Opcode parsing - analyzes the opcode string. """
    oper = op[-1]
    mode = '00'+op[:-2]

    # Parse position vs immediate mode for each operation
    j = self.i
    if oper == '1' or oper == '2' or oper == '7' or  oper == '8':
      p1 = self.x[self.x[j+1]] if mode[-1]=='0' else self.x[j+1]
      p2 = self.x[self.x[j+2]] if mode[-2]=='0' else self.x[j+2]
      out = self.x[j+3]
      self.i = self.i + 4
      return oper,p1,p2,out
    elif oper == '5' or oper  == '6':
      p1 = self.x[self.x[j+1]] if mode[-1]=='0' else self.x[j+1]
      p2 = self.x[self.x[j+2]] if mode[-2]=='0' else self.x[j+2]
      out = self.x[j+3]
      self.i = self.i + 3
      return oper,p1,p2,None
    elif oper == '4':
      p = self.x[self.x[j+1]] if mode[-1]=='0' else self.x[j+1]
      self.i = self.i + 2
      return oper,p,None,None
    else:
      p = self.x[j+1]
      self.i = self.i + 2
      return oper,p,None,None

  def operate(self,oper,p1,p2,out):
    """ Apply operation on the intcode sequence. """

    if oper=='1':
      self.x[out] = p1 + p2
    elif oper=='2':
      self.x[out] = p1 * p2
    elif oper=='3':
      self.x[p1] = self.input
    elif oper=='4':
      print(p1)
    elif oper=='5':
      self.i = p2 if p1 != 0 else self.i
    elif oper=='6':
      self.i = p2 if p1 == 0 else self.i
    elif oper=='7':
      self.x[out] = 1 if p1 < p2 else 0
    elif oper=='8':
      self.x[out] = 1 if p1 == p2 else 0

  def __call__(self):
    """ Call to solve the intcode. """

    while(self.i < self.n):
      if(self.x[self.i]==99):
        break
      oper,p1,p2,out = self.parse_opcode(str(self.x[self.i]))
      self.operate(oper,p1,p2,out)

def main():

  part_one = Intcode("../data.csv",1)
  print("Solution for part one:")
  part_one()

  part_two = Intcode("../data.csv",5)
  print("Solution for part two:")
  part_two()

if __name__ == "__main__":
  main()


import numpy as np
from scipy.linalg import norm
from numpy.linalg import qr, solve

# modified from part of 6

def LanczosTri(A):
    '''Tridiagonalize Matrix A via Lanczos Iterations'''

    # Check if A is symmetric
    # if((A.transpose() != A).any()):
    #    print("WARNING: Input matrix is not symmetric")
    n = A.shape[0]
    x = np.ones(n)  # Random Initial Vector
    V = np.zeros((n, 1))  # Tridiagonalizing Matrix

    # Begin Lanczos Iteration
    q = x / norm(x)
    V[:, 0] = q
    r = A @ q
    a1 = q.T @ r
    r = r - a1 * q
    b1 = norm(r)
    ctr = 0
    # print("a1 = %.12f, b1 = %.12f"%(a1,b1))
    for j in range(2, n + 1):
        v = q
        q = r / b1
        r = A @ q - b1 * v
        a1 = q.T @ r
        r = r - a1 * q
        b1 = norm(r)

        # Append new column vector at the end of V
        V = np.hstack((V, np.reshape(q, (n, 1))))

        # Reorthogonalize all previous v's
        V = qr(V)[0]

        ctr += 1

        if b1 == 0:
            print("WARNING: Lanczos ended due to b1 = 0")
            return V  # Need to reorthonormalize

        # print(np.trace(V.T@V)/j)
    # Check if V is orthonormal
    # print("|V.T@V - I| = ")
    # print(np.abs((V.T@V)-np.eye(n)))
    # if((V.T@V != np.eye(n)).any()):
    #    print("WARNING: V.T @ V != I: Orthonormality of Transform Lost")

    # Tridiagonal matrix similar to A
    T = V.T @ A @ V

    return T

if __name__ == '__main__':
    # find leading ev
    A = np.array([[1, 2, 3], [2, 4, 5], [3, 5, 6]])
    T = (LanczosTri(A))
    print(T)

    # check A = VTV*


import csv

import cx_Oracle

connection = cx_Oracle.connect("test", "test", "DESKTOP-0Q2I46R/xe")

cursor_music = connection.cursor()

cursor_music.execute("""
SELECT
    TRIM(MUSIC_TITLE) as MUSIC_TITLE,
    TRIM(MUSIC_GENRE) as MUSIC_GENRE
FROM
    MUSIC""")

for MUSIC_TITLE, MUSIC_GENRE in cursor_music:

    with open("MUSIC_TITLE_" + MUSIC_TITLE + ".csv", "w", newline="") as file:
        writer = csv.writer(file)

        writer.writerow(["TITLE", MUSIC_TITLE])
        writer.writerow(["GENRE", MUSIC_GENRE])

        cursor_music_info = connection.cursor()

        query = """
                    SELECT
                      TRIM(MUSIC_AUTHOR2) as MUSIC_AUTHOR2

                    FROM
                        MUSIC NATURAL JOIN "info about music"
                    WHERE TRIM(MUSIC_TITLE) = :title"""

        cursor_music_info.execute(query, title=MUSIC_TITLE)
        writer.writerow([])
        writer.writerow(["MUSIC_AUTHOR2"])
        for info_row in cursor_music_info:
            writer.writerow(info_row)

cursor_music.close()


from flask import Flask, request, abort
from helper import config, setup_database, mongo_query
from model import review
from routes import review
from flask import send_file, jsonify

app = Flask(__name__)
config = config.Config()
database_setup = setup_database.SetupDatabase(config.database_ip,
                                              config.database_port,
                                              config.database_name)

db = database_setup.get_instance()

img_dir = config.image_dir

@app.route('/review', methods=['GET', 'POST'])
@app.route('/review/<product_id>', methods=['GET'])
def get_review(product_id=None):
    review_route = review.ReviewRoute(db, request, product_id)
    return jsonify(review_route.get_response())

@app.route('/img/<id>', methods=['GET'])
def get_image(id):
    try:
        name = id + '.png'
        file = open(img_dir + name, 'rb')

        return send_file(file, attachment_filename=name, mimetype='image/png')
    except Exception as e:
        abort(404, e)

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


#!/usr/bin/env python
# run it as:
#   python -m unittest test_example.TestClientCase

from aiohttp import web
from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop
from aiohttp.test_utils import TestClient, TestServer

class TestClientCase(AioHTTPTestCase):
    def _app_index(self, request):
        return web.Response(body="<html><body>Here we are</body></html",
                            content_type='text/html')

    async def get_application(self):
        app = web.Application()
        app.router.add_get('/', self._app_index)

        return app

    @unittest_run_loop
    async def test_using_class_attribute(self):
        request = await self.client.request("GET", "/")
        print(request.status)
        assert request.status == 200

    @unittest_run_loop
    async def test_using_client(self):
        tc = TestClient(
            TestServer(self.app, loop=self.loop),
            loop=self.loop)
        request = await tc.request("GET", "/")
        print(request.status)
        assert request.status == 200


from tkinter import ttk, Tk, Canvas, VERTICAL, ALL, SOLID, LEFT, RIGHT, Y, X, TOP, BOTTOM, CENTER
import fitz
from PIL import Image, ImageTk
import os

class PdfViewer:
    def __init__(self, parent):
        self.parent = parent
        self.currentPageNum = 0

        # GUI
        """
        self.parent
            |---> self.mainContainerFrame
                    |---> self.navigationFrame
                            |---> self.fileNameLabel
                            |---> self.frameWithNavigationButtons
                                    |---> self.leftButton
                                    |---> self.pageNumberLabel
                                    |---> self.rightButton
                    |---> self.viewerFrame
                            |---> self.verticalScrollbar
                            |---> self.canvas
                                    |---> self.frameInCanvas
                                            |---> self.imageHolder
        """

        self.mainContainerFrame = ttk.Frame(self.parent)

        self.navigationFrame = ttk.Frame(self.mainContainerFrame)
        self.navigationFrame.pack(fill=X)
        self.viewerFrame = ttk.Frame(self.mainContainerFrame)
        self.viewerFrame.pack()
        self.fileNameLabel = ttk.Label(self.navigationFrame)
        self.fileNameLabel.pack(side=LEFT)
        self.frameWithNavigationButtons = ttk.Frame(self.navigationFrame)
        self.frameWithNavigationButtons.pack(side=RIGHT)
        self.leftButton = ttk.Button(
            self.frameWithNavigationButtons, text="🡸", takefocus=False, command=lambda: self.showPreviousPage(None))
        self.leftButton.pack(side=LEFT)
        self.pageNumberLabel = ttk.Label(
            self.frameWithNavigationButtons,
            anchor=CENTER,
            width=8)
        self.pageNumberLabel.pack(side=LEFT)
        self.rightButton = ttk.Button(
            self.frameWithNavigationButtons, text="🡺", takefocus=False, command=lambda: self.showNextPage(None))
        self.rightButton.pack(side=LEFT)

        self.isInitiallyHidden = True
        self.canvas = Canvas(self.viewerFrame)
        self.verticalScrollbar = ttk.Scrollbar(
            self.viewerFrame, orient=VERTICAL)
        self.verticalScrollbar.config(command=self.canvas.yview)
        self.canvas.config(yscrollcommand=self.verticalScrollbar.set)
        self.verticalScrollbar.pack(side=RIGHT, fill=Y)
        self.canvas.pack(side=LEFT)

        self.frameInCanvas = ttk.Frame(self.canvas)

        self.canvas.create_window(
            (0, 0), window=self.frameInCanvas, anchor="nw")

        self.frameInCanvas.bind("<Configure>", lambda event, canvas=self.canvas: canvas.configure(
            scrollregion=canvas.bbox("all")))

        self.imageHolder = ttk.Label(
            self.frameInCanvas, border=1, relief=SOLID)
        self.imageHolder.pack()

        self.parent.focus_set()
        self.parent.bind("<Left>", self.showPreviousPage)
        self.parent.bind("<Prior>", self.showPreviousPage)

        self.parent.bind("<Right>", self.showNextPage)
        self.parent.bind("<Next>", self.showNextPage)

        self.parent.bind("<Home>", self.showFirstPage)
        self.parent.bind("<End>", self.showLastPage)

    def toPhotoImage(self, page):
        return ImageTk.PhotoImage(self.pageToImage(page))

    def pageToImage(self, page):
        pix = page.getPixmap()
        # set the mode depending on alpha
        mode = "RGBA" if pix.alpha else "RGB"
        img = Image.frombytes(mode, [pix.width, pix.height], pix.samples)
        return img

    def show(self):
        image = self.toPhotoImage(self.document.loadPage(self.currentPageNum))
        self.imageHolder.configure(image=image)
        self.imageHolder.image = image

        self.pageNumberLabel.config(text=str(self.currentPageNum+1))

    def showPdf(self, filePath):
        self.filePath = filePath
        self.document = fitz.open(self.filePath)
        self.lastPageNumber = self.document.pageCount-1
        width, height = self.pageToImage(self.document.loadPage(0)).size
        if self.isInitiallyHidden:
            self.mainContainerFrame.pack()
            self.isInitiallyHidden = False
        self.canvas.configure(width=width, height=height)
        self.canvas.configure(scrollregion=(0, 0, 0, height))
        fileName = " "*3 + os.path.splitext(os.path.split(self.filePath)[1])[0]
        self.fileNameLabel.config(
            text=f"{fileName[:50]}..." if len(fileName) > 50 else fileName)
        self.show()

    def showNextPage(self, event):
        self.currentPageNum += 1
        if (self.currentPageNum > self.lastPageNumber):
            self.currentPageNum = self.lastPageNumber
        else:
            self.show()

    def showPreviousPage(self, event):
        self.currentPageNum -= 1
        if (self.currentPageNum < 0):
            self.currentPageNum = 0
        else:
            self.show()

    def showFirstPage(self, event):
        self.currentPageNum = 0
        self.show()

    def showLastPage(self, event):
        self.currentPageNum = self.lastPageNumber
        self.show()


#!/usr/bin/python3

import sqlite3

def create(db, row):
    db.execute('insert into MyFirstSQLiteTable (username, id) values (?, ?)', (row['username'], row['id']))
    db.commit()

def retrieve(db, username):
    cursor = db.execute('select * from MyFirstSQLiteTable where username = ?', (username,))
    return cursor.fetchone()

def update(db, row):
    db.execute('update MyFirstSQLiteTable set id = ? where username = ?', (row['id'], row['username']))
    db.commit()

def delete(db, username):
    db.execute('delete from MyFirstSQLiteTable where username = ?', (username,))
    db.commit()

def DisplayRows(db):
    cursor = db.execute('select * from MyFirstSQLiteTable order by username')
    for row in cursor:
        print('  {}: {}'.format(row['username'], row['id']))

def main():
    db = sqlite3.connect('MyFirstSQLiteDB.db')
    db.row_factory = sqlite3.Row
    print('Create table MyFirstSQLiteTable')
    db.execute('drop table if exists MyFirstSQLiteTable')
    db.execute('create table MyFirstSQLiteTable ( username text, id int )')

    print('Create rows')
    create(db, dict(username = 'babu', id = 1))
    create(db, dict(username = 'mana', id = 2))
    create(db, dict(username = 'pata', id = 3))
    create(db, dict(username = 'gopal', id = 4))
    create(db, dict(username = 'bappa', id = 5))
    create(db, dict(username = 'babua', id = 6))
    create(db, dict(username = 'buddhu', id = 7))
    DisplayRows(db)

    print('Retrieve rows')
    print(dict(retrieve(db, 'babu')), dict(retrieve(db, 'mana')))

    print('Update rows')
    update(db, dict(username = 'Tapas', id = 7))
    # update(db, dict(username = 'three', id = 103))
    DisplayRows(db)

    # print('Delete rows')
    # delete(db, 'one')
    # delete(db, 'three')
    # DisplayRows(db)

if __name__ == "__main__":
    main()