'''DOCUMENTATION

Script:     This script contains the functions that will be used to prepare the dataset
            our dataset for the machine learning model
'''

## Import Packages
import pandas as pd
import sklearn
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import BernoulliNB
from sklearn.naive_bayes import MultinomialNB
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt

## Import Project Modules
import Module_7_DataAnalysis as m7
import Module_0_utility_functions as m0

## Directory Object
output_dir = r'/home/ccirelli2/Desktop/Programming/SCA_Web_scaper/ML_Algorithm_Results'

'''We need to modify the first function below to allow the user to limit the data by min
and max year as we did in Module 7.  This will ensure that we choose a data set with
an equivalent number of dismissed and settled cases'''

### DATA RETREIVAL & TRANSFORMATION___________________________________________________________

def sql_query_machine_learning_data_set(min_year, max_year):
    '''Purpose:  Initial query of db to retrieve data for ML application'''
    Query = '''SELECT *
               FROM SCA_data
               WHERE case_status IS NOT NULL
               AND YEAR_FILED > {}
               AND YEAR_FILED < {}
               AND case_status != 'ongoing'
               AND Plaintiff_firm != 'Error'
               AND Judge != 'None'
               AND CHAR_LENGTH(Judge) > 2
               ;'''.format(min_year, max_year)
    return Query

def transform_target_binary(df):
    '''Purpose:  Convert the column with our target value to 1/0.
    1         Dismissed
    0         Settled
    '''
    # Create List to Capture Binary Values
    List_case_status_binary = []
    # Isolate the Case_status column
    Series_case_status = df['case_status']
    # Iterate the series and append values to our the list_case_status object
    for x in Series_case_status:
        if x == 'Dismissed':
            List_case_status_binary.append(1)
        elif x == 'Settled':
            List_case_status_binary.append(0)
        else:
            print('values other than dismissed and settled found in case_summary column')
    # Create a new column with binary representations
    df['Target_case_status_binary'] = List_case_status_binary
    # Drop old column
    df_final= df.drop(labels = 'case_status', axis = 1)

    return df_final

def transform_plaintiff_firm(df):
    '''Purpose is to shorten the name of the plaintiff firm in order to aviod issues differences
    that could arise with punctuation or text variations with a longer name, thereby avoiding
    a situation in which the same plaintiff firm comes up twice.
    '''
    List_plaintiff_firm_modified = []
    series_plaintiff_firm = df['Plaintiff_firm']
    for x in series_plaintiff_firm:
        List_plaintiff_firm_modified.append(x[:25])
    df['Plaintiff_firm_modified'] = List_plaintiff_firm_modified
    df_final = df.drop(labels = 'Plaintiff_firm', axis = 1)
    return df_final

def get_list_attributes_by_type(Type):

    if Type == 'categorical':
        return ['Sector', 'Industry', 'Headquarters', 'Company_market', 'Court',
       'Judge', 'Plaintiff_firm_modified']
    elif Type == 'numerical':
        return ['Breach_Fiduciary_Duties', 'Merger', 'Proxy_violation',
       'Related_parties', 'Stock_Drop', 'Cash_Flow', 'Revenue_Rec',
       'Net_Income', 'Customers', 'Fourth_Quarter', 'Third_Quarter',
       'Second_Quarter', 'Press_Release', '10K_Filling', '10Q_Filling',
       'Corporate_Governance', 'Conflicts_Interest', 'Accounting', 'Fees',
       'Failed_disclose', 'False_misleading', 'Commissions', 'Bankruptcy',
       'Secondary_Offering', 'IPO', '1934_Exchange_Act', 'Derivative', '10b5',
       '1933_Act', 'Heavy_trading', 'Sexual_Misconduct', 'class_action',
       'ERISA', 'FCPA', 'SEC_Investigation', 'Data_breach', 'Proxy',
       'Class_Duration']

### One Hot Encode Data------------------------------------------------------------------
def Encode_categorical_data(df, List_attributes_by_type):
    le = preprocessing.LabelEncoder()
    Attributes_categorical = df[List_attributes_by_type]
    Attributes_encoded = Attributes_categorical.apply(le.fit_transform)
    return Attributes_encoded

### DRIVER FUNCTION - PREPARE DATA SET FOR ML ALGORITHM----------------------------------
'''Includes all the above functions to prepare dataset'''

def prepare_dataset(conn, min_year, max_year):
    '''
    Purpose:    Prepare the dataset that we will use for our machine learning model
    Conn:       mysql connection
    Year:       Min year to be used to limit dataset to years > than this value
    Output:     Dataset prepared for ML algorithm
    '''

    # 1.) Import SCA_data
    '''Input:  Year_Filed to exclude'''
    df_SCA_data_table = m7.sql_query_executor(conn,
                        sql_query_machine_learning_data_set(min_year, max_year))

    # 2.) Drop Columns - Defendant_address, case_summary, page_number
    '''Based on our preliminary analysis, these two columns were not propertly scraped and contain
    too many null values to be included in our final dataset. Therefore, they will be dropped
    '''
    df_drop_columns = df_SCA_data_table.drop(labels = ['defendant_address', 'case_summary',
                                                        'page_number', 'Ref_court', 'Ref_docket',
                                                        'Ref_judge', 'Ref_date_filed',
                                                        'Ref_class_period_start',
                                                        'Ref_class_period_end',
                                                        'filling_date', 'defendant_name',
                                                        'close_date', 'Date_Filed', 'Docket',
                                                        'Class_Period_Start', 'Class_Period_End',
                                                        'Symbol', 'YEAR_FILED', 'Status_2'], axis = 1)

    # 3.) Convert case_status to binary, where 1 = 'dimissed', 0 = 'settled'
    df_transform_case_status = transform_target_binary(df_drop_columns)

    # 4.) Transform Plaintiff Firm - Limit to first 25 Characters
    df_transform_plaintiff_firm = transform_plaintiff_firm(df_transform_case_status)

    # Return Transformed Dataset
    return df_transform_plaintiff_firm

##########################           ALGORITHMS                 ###############################

### KNN____________________________________________________________________________________

# Test Number of Neighbors-----------------------------------------------------------------

def train_KNN_predictor(X, Y, random_state_value, min_year, max_year, write_2_excel = False,
                        plot = False, results = 'DataFrame'):
    '''Documentation:
    random_state:      seed used by the random generator.
    stratify:          separation of data into homogenious groups before sampling.
    range:             range over which to iterate to generate predictions
    lists:             capture predictions

    '''
    # Split dataset
    x_train, x_test, y_train, y_test = train_test_split(
                                        X, Y,
                                        stratify = Y,
                                        random_state = random_state_value,
                                        test_size = .15)

    # Ratio Dissmissed to Settled;
    Dismissal_percentage = round(sum(Y) / len(Y), 2)

    # Case Count
    Case_count = len(Y)

    # Lists to Capture Predictions
    accuracy_training_list = []
    accuracy_test_list = []
    dismissal_percentage_list = [Dismissal_percentage for x in range(1,10)]

    # Range of Nearest Neighbors
    num_range_neighbors = range(1,10)
    # Run Loop
    for num in num_range_neighbors:
        # Instantiate KNN Algorithm
        knn = KNeighborsClassifier(n_neighbors = num)
        # Fit algorithm to training data
        knn.fit(x_train, y_train)
        y_predict = knn.predict
        accuracy_training_list.append(knn.score(x_train, y_train))
        accuracy_test_list.append(knn.score(x_test, y_test))

    # Create DataFrame for scores
    df = pd.DataFrame({}, index = [2,3,4,5,6,7,8,9,10])
    df['Accuracy_Training'] = accuracy_training_list
    df['Accuracy_Test'] = accuracy_test_list
    df['Dismissal_Percentage'] = dismissal_percentage_list

    # Write Results To Excel
    if write_2_excel == True:
        m0.write_to_excel(df, 'KNN_output', output_dir)

    # Plotting
    if plot == True:
        plt.plot(num_range_neighbors, accuracy_training_list, label = 'Accuracy of training')
        plt.plot(num_range_neighbors, accuracy_test_list, label = 'Accuracy of test')
        plt.plot(num_range_neighbors, dismissal_percentage_list, label = 'Dismissal Percentage')
        plt.ylabel('Accuracy', fontsize = 20)
        plt.xlabel('Number of Neighbors' , fontsize = 20)
        plt.title('''Performance KNN Algorithm SCA Dataset
                 For Years: {} to {}
                 Case Count => {}
                 Ratio Dismissed to Total Cases => {}'''.format(min_year, max_year, Case_count,
                 Dismissal_percentage), fontsize = 25)
        plt.legend(fontsize = 15)
        plt.xticks(fontsize = 15)
        plt.yticks(fontsize = 15)
        plt.grid(b=None, which='major')
        plt.show()

    # Confusion Matrix
    if results == 'Confusion_matrix':
        clf_predict_y_test = knn.predict(y_test)
        clf_confusion_matrix = confusion_matrix(y_test, clf_predict_y_test)
        return clf_confusion_matrix

    # Results in Dataframe
    if results == 'DataFrame':
        return df
    #-------------------------------------------------------------------------------

## KNN - Single Neighbor - Generate Classification Report------------------------------------------

def train_KNN_single_neighbor_classifier(X, Y, NN, random_state_value, result):
    '''
    x_tain , y_train:  This represents the training data for our algorithm.  x_train is our
                       features and y_train the target.
    y_pred_class:      Once trained, we input the x_test data, which our model has not yet seen,
                       from which the model generates a prediction.  That prediction is saved to
                       y_pred_class object.  We then compare this prediction to the actual y_values
                       which are saved in y_test.  Its important to remember that x_test is the 'test'
                       data that our model has not yet seen. Hence the word 'test'.
    '''
    # Split Data Into Training & Test Sets:
    x_train, x_test, y_train, y_test = train_test_split(
                                        X, Y,
                                        stratify = Y,
                                        random_state = random_state_value,
                                        test_size = .15)

    # Instantiate KNN Algorithm
    knn = KNeighborsClassifier(n_neighbors = NN)

    # Fit algorithm to training data
    knn.fit(x_train, y_train)

    # Predict for Test Data
    '''Feed our model the x_test data (features) and make a prediction for our y-variable'''
    y_predict = knn.predict(x_test)

    # Generate Results
    if result == 'Classification_report':
        '''We now compare our y-prediction to the actual y saved in the y_test object'''
        knn_class_report_train = sklearn.metrics.classification_report(y_test, y_predict)
        return knn_class_report_train
    elif result == 'f1_score':
        knn_f1_score = sklearn.metrics.f1_score(y_test, y_predict)
        return knn_f1_score
    elif result == 'precision_score':
        knn_precision_score = sklearn.metrics.precision_score(y_test, y_predict)
        return knn_precision_score
    elif result == 'recall_score':
        knn_recall_score = sklearn.metrics.recall_score(y_test, y_predict)
        return knn_recall_score

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

## LOGISTIC REGRESSION________________________________________________________________

def train_log_regressor_classifier(X, Y, random_state_value, result):
    x_train, x_test, y_train, y_test = train_test_split(X, Y,
                                                    stratify = Y,
                                                    random_state = random_state_value)
    # Standardize Data
    from sklearn.preprocessing import StandardScaler
    sc = StandardScaler()

    #x_train_sc = sc.fit_transform(x_train)
    #x_test_sc = sc.fit_transform(x_test)

    # Instantiate Model & Generate Prediction
    log_reg = LogisticRegression()
    log_reg.fit(x_train, y_train)
    y_predict = log_reg.predict(x_test)

    # Generate Results
    if result == 'Classification_report':
        log_reg_class_report = sklearn.metrics.classification_report(y_test, y_predict)
        return log_reg_class_report
    elif result == 'f1_score':
        log_reg_f1_score = sklearn.metrics.f1_score(y_test, y_predict)
        return log_reg_f1_score
    elif result == 'precision_score':
        log_reg_precision_score = sklearn.metrics.precision_score(y_test, y_predict)
        return log_reg_precision_score
    elif result == 'recall_score':
        log_reg_recall_score = sklearn.metrics.recall_score(y_test, y_predict)
        return log_reg_recall_score

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

## NAIVE BAYES______________________________________________________________________

'''DOCUMENTATION:
Bernoulli Naive Bayes:  The binomial model is useful if your feature vectors
                        are binary (i.e., 0s and 1s). One application would be text
                        classification with a bag of words model where the 0s 1s are
                        "word occurs in the document" and "word does not occur
                        in the document"

Multinomial Naive:      Bayes The multinomial naive Bayes model is typically used for discrete counts.                         E.g., if we have a text classification problem, we can take the idea
                        of bernoulli trials one step further and instead of "word occurs in the
                        document" we have "count how often word occurs in the document",
                        you can think of it as "number of times outcome number x_i is observed
                        over the n trials"

Gaussian Naive Bayes:    Here, we assume that the features follow a normal distribution.
                         Instead of discrete counts, we have continuous features (e.g.,
                         the popular Iris dataset where the features are sepal width, petal
                         width, sepal length, petal length).
Source:                  http://users.sussex.ac.uk/~christ/crs/ml/lec02b.html
'''

def train_NaiveBayes_classifier(X,Y, random_state_value, NB_type, result):

    x_train, x_test, y_train, y_test = train_test_split(X, Y,
                                                        stratify = Y,
                                                        random_state = random_state_value)
    # Multinomial Model:-------------------------
    if NB_type == 'Multinomial':
        NB_multi = MultinomialNB(alpha = 1)
        NB_multi.fit(x_train, y_train)
        y_predict = NB_multi.predict(x_test)

        # Generate Results
        if result == 'Classification_report':
            NB_class_report = sklearn.metrics.classification_report(y_test, y_predict)
            return NB_class_report
        elif result == 'f1_score':
            NB_f1_score = sklearn.metrics.f1_score(y_test, y_predict)
            return NB_f1_score
        elif result == 'precision_score':
            NB_precision_score = sklearn.metrics.precision_score(y_test, y_predict)
            return NB_precision_score
        elif result == 'recall_score':
            NB_recall_score = sklearn.metrics.recall_score(y_test, y_predict)
            return NB_recall_score

    # Bernoulli Model----------------------------
    elif NB_type == 'Bernoulli':
        NB_bern = BernoulliNB()
        NB_bern.fit(x_train, y_train)
        y_predict = NB_bern.predict(x_test)

        # Generate Results
        if result == 'Classification_report':
            NB_class_report = sklearn.metrics.classification_report(y_test, y_predict)
            return NB_class_report
        elif result == 'f1_score':
            NB_f1_score = sklearn.metrics.f1_score(y_test, y_predict)
            return NB_f1_score
        elif result == 'precision_score':
            NB_precision_score = sklearn.metrics.precision_score(y_test, y_predict)
            return NB_precision_score
        elif result == 'recall_score':
            NB_recall_score = sklearn.metrics.recall_score(y_test, y_predict)
            return NB_recall_score

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

# RANDOM FOREST_______________________________________________________________________________

def train_RandomForecast_classifier(X,Y, random_state_value, result):

    x_train, x_test, y_train, y_test = train_test_split(X, Y,
                                                        stratify = Y)

    # Generate Prediction
    clf_RF = RandomForestClassifier(n_estimators = 100)
    clf_RF.fit(x_train, y_train)
    y_predict = clf_RF.predict(x_test)

    # Generate Results
    if result == 'Classification_report':
        NB_class_report = sklearn.metrics.classification_report(y_test, y_predict)
        return NB_class_report
    elif result == 'f1_score':
        NB_f1_score = sklearn.metrics.f1_score(y_test, y_predict)
        return NB_f1_score
    elif result == 'precision_score':
        NB_precision_score = sklearn.metrics.precision_score(y_test, y_predict)
        return NB_precision_score
    elif result == 'recall_score':
        NB_recall_score = sklearn.metrics.recall_score(y_test, y_predict)
        return NB_recall_score
    elif result == 'feature_importance':
        Feature_important = clf_RF.feature_importances_
        df = pd.DataFrame({}, index = X.columns)
        df['Feature Importance'] = Feature_important
        m0.write_to_excel(df, 'Feature_Importance', output_dir)
        return clf_RF.score(x_test, y_test)
    #-------------------------------------------------------------------------------

# ELIMINATE ATTRIBUTES & RUN MODEL
'''Documentation

Purpose:        The purpose of this section is to generate lists in order to

'''

list_categorical_features = ['Sector', 'Industry', 'Company_market',
                             'Court', 'Judge', 'Plaintiff_firm_modified',
                             'Target_case_status_binary']

def limit_feature_selection(df, limit_selection):
    if limit_selection == 'Drop_derived_values':
        return df[['Sector', 'Industry', 'Company_market',
                             'Court', 'Judge', 'Plaintiff_firm_modified',
                             'Target_case_status_binary']]
    elif limit_selection == 'Use_all':
        return df

    elif limit_selection == 'Drop_merger_value':
        return df.drop(['Derivative', 'Merger', 'Proxy'], axis = 1)

# Graph Comparison - Average Performance All Features vs Dropping Derived Values

def graph_comparison_performance_features():
    year = [x for x in range(2000, 2017)]
    All_features = [.7, .689, .693, .689, .68, .693, .699, .704, .688, .726, .706,
                    .711, .705, .740, .822, .889, .954]
    Categorical_features_only = [.628, .595, .595, .612, .624, .617, .631, .655, .661,
                                 .680, .674, .696, .716, .757, .834, .876, .954]

    df = pd.DataFrame({}, index = year)
    df['Average_Performance_All_Features'] = All_features
    df['Categorical_features_only'] = Categorical_features_only

    plt.plot(year, df['Average_Performance_All_Features'], label = 'All_Features')
    plt.plot(year, df['Categorical_features_only'], label = 'Categorical_Features_Only')
    plt.ylabel('Average_Score', fontsize = 20)
    plt.xlabel('Range of Years Case Was Filed' , fontsize = 20)
    plt.title('Comparison: All Features vs Categorical Features Only', fontsize = 30)
    plt.legend(fontsize = 15)
    plt.xticks(fontsize = 15)
    plt.yticks(fontsize = 15)
    plt.grid(b=None, which='major')
    plt.show()
    return df

# Feature Importance - Un-groupped Categories
'''
Because of the one-hot-encoding certain features where broken into their subgroups.
Example the feature judge has a column for every single judge.  The below functions
rever these columns back to the single lvl features and then sum the feature importance
exported from our model.
'''

def generate_sum_importance_ungrouped_features(df, Feature):
    Feature_importance_list = []

    for x in df['Category']:
        if Feature in x:
            Feature_importance_list.append(1)
        else:
            Feature_importance_list.append(0)
    df[Feature + '_Importance'] = Feature_importance_list
    df_limit_feature = df[Feature +'_Importance'] == 1
    df_final = df[df_limit_feature]

    Feature_importance_sum = sum(df_final['Feature Importance'])

    return Feature_importance_sum

def record_feature_importance_ungrouped_categories(df, grouping_function, target_dir, target_file):

    # Load File
    os.chdir(target_dir)
    df = pd.read_excel(target_file)

    Dict_feature_importance = {}
    Ungrouped_feature_list = ['Judge', 'Court', 'Plaintiff_firm',
                              'Headquarters', 'Sector', 'Industry']

    for feature in Ungrouped_feature_list:
        feature_importance = round(grouping_function(df, feature), 4)
        Dict_feature_importance[feature] = feature_importance

    df = pd.DataFrame(Dict_feature_importance, index = ['Feature_importance']).transpose()

    return df


import pygame

class Enemy:

    color = (255,255,255)

    # Constructor
    def __init__(self, x_, y_, w_, h_, aSpeed, aWaypointList) -> None:
        """Constructor

        Args:
            x_ (int): initializes x pos
            y_ (int): initializes y pos
            w_ (int): initializes width
            h_ (int): initializes height
            aSpeed (float): initializes speed
            aWaypointList (TBD): [description]
        """
        # initialize vars
        self.hitbox = pygame.rect.Rect(x_, y_, w_, h_)
        self.speed = aSpeed
        # make it a copy so the original list is not later changed
        self.waypointList = aWaypointList.copy()

        # get the starting position
        startingX = self.waypointList[0].x
        startingY = self.waypointList[0].y
        self.currentPos = pygame.Vector2(startingX, startingY)
        # get the index of the target position
        self.waypointIndex = 1

        # boolean representing if the bullet should exist
        self.exists = True

    def updatePos(self):

        # if you have not reached the last waypoint
        if self.waypointIndex < len(self.waypointList):

            # get the target pos
            targetPos = self.waypointList[self.waypointIndex]

            # if you have not reached the current position yet
            # then move towards the target
            if self.currentPos.distance_to(targetPos) >= self.speed:
                # get the velocity
                velocity = targetPos - self.currentPos
                # normalize the velocity to get a unit vector
                velocity = velocity.normalize()
                # update the current position so that it moves towards the target
                self.currentPos += (velocity*self.speed)

            # if it is close enough to the target, then move on to the next target
            else:
                self.waypointIndex += 1

            # update the hitbox position to match currentPos
            self.hitbox.x = self.currentPos.x
            self.hitbox.y = self.currentPos.y

        # if you have reached the last waypoint
        else:
            self.exists = False

    def renderHitbox(self, aSurface):
        """Displays the hitbox of the game object

        Args:
            aSurface (pygame surface object): surface to draw on
        """
        pygame.draw.rect(aSurface, self.color, self.hitbox, 1)

    def display(self, aSurface, imageToDisplay):
        """Displays the hitbox of the game object

        Args:
            aSurface (pygame surface object): surface to draw on
            imageToDisplay (pygame image): image to show at the bullet's location
        """
        aSurface.blit(imageToDisplay, (self.hitbox.x, self.hitbox.y))


# Hangman Game
# -----------------------------------
# Helper code
# You don't need to understand this helper code,
# but you will have to know how to use the functions
# (so be sure to read the docstrings!)
import random
import string

WORDLIST_FILENAME = "words.txt"

def load_words():
    """
    Returns a list of valid words. Words are strings of lowercase letters.

    Depending on the size of the word list, this function may
    take a while to finish.
    """
    print("Loading word list from file...")
    # inFile: file
    inFile = open(WORDLIST_FILENAME, 'r')
    # line: string
    line = inFile.readline()
    # wordlist: list of strings
    wordlist = line.split()
    print("  ", len(wordlist), "words loaded.")
    return wordlist

def choose_word(wordlist):
    """
    wordlist (list): list of words (strings)

    Returns a word from wordlist at random
    """
    return random.choice(wordlist)

# end of helper code

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

# Load the list of words into the variable wordlist
# so that it can be accessed from anywhere in the program
wordlist = load_words()

def is_word_guessed(secret_word, letters_guessed):
    '''
    secret_word: string, the word the user is guessing; assumes all letters are
      lowercase
    letters_guessed: list (of letters), which letters have been guessed so far;
      assumes that all letters are lowercase
    returns: boolean, True if all the letters of secret_word are in letters_guessed;
      False otherwise
    '''
    # FILL IN YOUR CODE HERE AND DELETE "pass"
    for letter_guess in secret_word:
        if letter_guess not in letters_guessed:
            return False
    return True
    #checks to see if letter_guess (the input from user) is in secret_word and returns true if not then false

def get_guessed_word(secret_word, letters_guessed):
    '''
    secret_word: string, the word the user is guessing
    letters_guessed: list (of letters), which letters have been guessed so far
    returns: string, comprised of letters, underscores (_), and spaces that represents
      which letters in secret_word have been guessed so far.
    '''
    # FILL IN YOUR CODE HERE AND DELETE "pass"
    guess = ''

    for letter_guess in range (len(secret_word)):
        if secret_word[letter_guess] in letters_guessed:
            guess += secret_word[letter_guess]
            #adding letter guessed to guess if correct
        else:
            guess += ' _ '
            #adding an underscore if the letter space is empty
    return guess

def get_available_letters(letters_guessed):
    '''
    letters_guessed: list (of letters), which letters have been guessed so far
    returns: string (of letters), comprised of letters that represents which letters have not
      yet been guessed.
    '''
    available_letters = ''

    #string.ascii_lowercase accounts for all the alphabet in lowercase
    for letter in string.ascii_lowercase:
        if letter not in letters_guessed:
            available_letters = available_letters + letter
    return available_letters
    # FILL IN YOUR CODE HERE AND DELETE "pass"

def hangman(secret_word):
    '''
    secret_word: string, the secret word to guess.

    Starts up an interactive game of Hangman.

    * At the start of the game, let the user know how many
      letters the secret_word contains and how many guesses s/he starts with.

    * The user should start with 6 guesses

    * Before each round, you should display to the user how many guesses
      s/he has left and the letters that the user has not yet guessed.

    * Ask the user to supply one guess per round. Remember to make
      sure that the user puts in a letter!

    * The user should receive feedback immediately after each guess
      about whether their guess appears in the computer's word.

    * After each guess, you should display to the user the
      partially guessed word so far.

    Follows the other limitations detailed in the problem write-up.
    '''
    # FILL IN YOUR CODE HERE AND DELETE "pass"
    letters_guessed = []
    guess_remaining = 6
    mistakes = 0 #set mistakes so i can count each time they get a letter wrong and check
    available_letters = get_available_letters(letters_guessed)

    print ("I want to play a game...")
    print ("Guess the word and make it out alive, the amount of letters in the word is: ", len(secret_word),  " good luck.")

    while mistakes < 6:
        print ("") #printing an empty line making it easier on the eye to read when printed

        if is_word_guessed(secret_word, letters_guessed):
            print (f"Congratulations you guessed the word, the word was: {secret_word}")
            break #otherwise it will go through the loop one more time even when you have won.

        print (f"Your remaining guesses: {guess_remaining}")
        print (f"The letters reamining are: {available_letters}")

        letter_guess = input("Please guess a letter: ") #sets new letter guess off users input each time through the loop

        if letter_guess in letters_guessed:
            print ("You've already guessed this letter lose a guess for not paying attention.")
            guess_remaining -= 1
            mistakes += 1
            print (get_guessed_word(secret_word, letters_guessed))
        else:
            letters_guessed.append(letter_guess)
            available_letters = get_available_letters(letters_guessed)

            if letter_guess in secret_word:
                print ("Correct guess it was in the word: " + get_guessed_word(secret_word, letters_guessed))
            else:
                print ("Incorrect guess that letter is not in the word: " + get_guessed_word(secret_word, letters_guessed))
                guess_remaining -= 1
                mistakes += 1
    #only way I could find to exit while loop as mistakes == 6 didnt work as the while loop doesnt let it run that far so else statement is easier way to break out when it reaches 6
    else:
        print("")
        print (f"You lost, you ran out of guesses. The word was: {secret_word}")

secret_word = choose_word(wordlist)
hangman(secret_word)


#!/usr/bin/env python3

"""
get_course_info.py
    gets info associated with a given course ID, then saves it to a local DynamoDB

Usage:
    ./get_course_info [-s] [-w WORKERS]

Output:
    None

Creator:
    Jack Rundle
"""

from bs4 import BeautifulSoup
import requests

import asyncio
import concurrent.futures

from time import time
import sys

from config import Config

def wrapper(args):
    return get_course_info(*args)

def get_course_info(course_id, session):
    """gets tees for a courseId"""
    # load course's page
    s = time()
    url = f"{Config.URL}{Config.COURSE_ENDPOINT}?CourseID={course_id}"
    resp = session.get(url)

    timing = time() - s
    soup = BeautifulSoup(resp.text, "html.parser")

    # load course info
    course_info_table = soup.find(id="gvCourseTees")
    try:
        row = course_info_table.find_all("tr")[1].find_all("td")
        course, club = row[0].text.split(" - ")
        city = row[1].text
        state = row[1].text

        course_info = {
            "course_id": course_id,
            "club_name": course,
            "course_name": club,
            "city": city,
            "state": state
        }

    except (IndexError, AttributeError):
        return

    # load tee table
    tee_table = soup.find(id="gvTee")
    rows = tee_table.find_all("tr")[1:] if tee_table else []

    # extract tee info from each row
    tees = []
    for row in rows:
        color, gender, par, _, bogey, _, front, back = map(lambda info: info.text.strip(), row.find_all("td"))

        front, back = front.split('/'), back.split('/')

        tees.append({
            "tee_name": color,
            "gender": gender,
            "par": par,
            "front_cr": front[0].strip(),
            "front_sr": front[1].strip(),
            "back_cr": back[0].strip(),
            "back_sr": back[1].strip(),
            "bogey_rating": bogey
        })

    course_info["tees"] = tees

    save_course_info(course_info)
    return timing
    # return course_info

def save_course_info(info):
    # save to database
    print(info)
    """
    courses.insert_one(info["course"])
    tees.insert_many(info["tees"])
    """

def process(info_list, count):
    print(f"Average Load Time: {sum(info_list) / count:.2f}")

if __name__ == "__main__":
    use_concurrency = True
    workers = 30

    i = 1
    while i < len(sys.argv):
        arg = sys.argv[i]

        if arg == "-s":
            use_concurrency = False
        elif arg == "-w":
            if i + 1 != len(sys.argv):
                workers = int(sys.argv[i + 1])
                i += 1

        i += 1

    with requests.Session() as session:
        ids = []
        for line in sys.stdin.readlines():
            ids.append(line.rstrip())

        s = time()

        if use_concurrency:
            params = (
                (id, session) for id in ids
            )
            with concurrent.futures.ProcessPoolExecutor(max_workers=workers) as executor:
                info_list = executor.map(wrapper, params)

        else:
            info_list = [
                get_course_info(id, session) for id in ids
            ]
        process(info_list, len(ids))
        print(f"    Total Time: {time() - s:.2f}")


def uni_convert(input):
    """Convert unicode input to utf-8

    Arguments:
    ----------
    input - the unicode input to convert
    """
    if isinstance(input, dict):
        ret = {}
        for k, v in input.iteritems():
            ret[uni_convert(k)] = uni_convert(v)
        return ret
    elif isinstance(input, list):
        return [uni_convert(element) for element in input]
    elif isinstance(input, unicode):
        return input.encode('utf-8')
    else:
        return input

def file_stats(path, human_readable=False):
    import os
    md5 = md5sum(path)
    size = os.path.getsize(path)
    if human_readable:
        size = human_fmt(size,True)
    return (md5,size)

def md5sum(filename, n_blocks=128):
    import hashlib
    md5 = hashlib.md5()
    with open(filename,'rb') as f:
        for chunk in iter(lambda: f.read(n_blocks*md5.block_size), b''):
            md5.update(chunk)
    return md5.hexdigest()

from math import log
unit_list = zip(['', 'k', 'M', 'G', 'T', 'P'], [0, 0, 1, 2, 2, 2])
def human_fmt(num, size=False):
    """Human friendly file size"""
    if num > 1:
        exponent = min(int(log(num, 1024)), len(unit_list) - 1)
        quotient = float(num) / 1024**exponent
        unit, num_decimals = unit_list[exponent]
        format_string = '{0:.%sf}{1}' % (num_decimals)
        out = format_string.format(quotient, unit)
    if size:
        out+='B'
    return out


import sqlite3
#backend

def employeeData():
    con=sqlite3.connect("employee.db")
    cur = con.cursor()
    cur.execute("CREATE TABLE IF NOT EXISTS employee(id INTEGER PRIMARY KEY, EmpID text, Firstname text, Lastname text, DOB text, \
        Gender text, Address text, Mobile text)")
    con.commit()
    con.close()

def addEmpRec(EmpID, Firstname, Lastname, DOB, Gender, Address, Mobile):
    con=sqlite3.connect("employee.db")
    cur = con.cursor()
    cur.execute("INSERT INTO employee VALUES (NULL, ?,?,?,?,?,?,?)",(EmpID, Firstname, Lastname, DOB , Gender, Address, Mobile))
    con.commit()
    con.close()

def viewData():
    con=sqlite3.connect("employee.db")
    cur = con.cursor()
    cur.execute("SELECT * FROM employee")
    rows=cur.fetchall()
    con.close()
    return rows

def deleteRec(id):
    con=sqlite3.connect("employee.db")
    cur = con.cursor()
    cur.execute("DELETE FROM employee WHERE id=?", (id,))
    con.commit()
    con.close()

def searchData(EmpID="", Firstname="", Lastname="", DOB="", Gender="", Address="", Mobile=""):
    con=sqlite3.connect("employee.db")
    cur = con.cursor()
    cur.execute("SELECT * FROM employee WHERE EmpID=? OR Firstname=? OR Lastname=? OR DOB=? OR \
        Gender=? OR Address=? OR Mobile=? ", (EmpID, Firstname, Lastname, DOB, Gender, Address, Mobile))
    rows=cur.fetchall()
    con.close()
    return rows

def dataUpdate(id, EmpID="", Firstname="", Lastname="", DOB="", Gender="", Address="", Mobile=""):
    con=sqlite3.connect("employee.db")
    cur = con.cursor()
    cur.execute("UPDATE employee SET EmpID=?, Firstname=?, Lastname=?, DOB=?, \
        Gender=?, Address=?, Mobile=?, WHERE id=?",(EmpID, Firstname, Lastname, DOB, Gender, Address, Mobile, id))
    con.commit()
    con.close()

employeeData()


import json
import os
from twilio.rest.lookups import TwilioLookupsClient
from twilio.rest import TwilioRestClient
from flask import render_template, url_for, request, jsonify
from flask.ext.login import login_required
from twilio import twiml
from app import csrf
from .. import db
from ..models import EditableHTML, Resource, Rating, Descriptor, OptionAssociation, RequiredOptionDescriptor
from . import main
from wtforms.fields import SelectMultipleField, TextAreaField
from ..single_resource.forms import SingleResourceForm
from datetime import datetime
from collections import OrderedDict

@main.route('/')
def index():
    req_opt_desc = RequiredOptionDescriptor.query.all()
    req_opt_id = -1
    if req_opt_desc:
        req_opt_desc = req_opt_desc[0]
        req_opt_desc = Descriptor.query.filter_by(
            id=req_opt_desc.descriptor_id
        ).first()
        if req_opt_desc is not None:
            req_opt_id = req_opt_desc.id
    options = Descriptor.query.all()
    options = [o for o in options if len(o.text_resources) == 0 and o.id != req_opt_id]
    options_dict = {}
    for o in options:
        # Sort descriptor values alphabetically in filters
        o.values.sort()
        options_dict[o.name] = o.values
    # Sort descriptors alphabetically
    options_dict = OrderedDict(sorted(options_dict.items(), key=lambda x: x[0]))
    req_options = {}
    if req_opt_desc:
        for val in req_opt_desc.values:
            req_options[val] = False
        # Sort required descriptor values alphabetically in filters
        req_options = OrderedDict(sorted(req_options.items(), key=lambda x: x[0]))
    return render_template('main/index.html', options=options_dict, req_options=req_options, req_desc=req_opt_desc)

@main.route('/get-resources')
def get_resources():
    resources = Resource.query.all()
    resources_as_dicts = Resource.get_resources_as_dicts(resources)
    return json.dumps(resources_as_dicts)

@main.route('/search-resources')
def search_resources():
    name = request.args.get('name')
    if name is None:
        name = ""
    req_options = request.args.getlist('reqoption')
    if req_options is None:
        req_options = []
    # case insensitive search
    resource_pool = Resource.query.filter(Resource.name.ilike('%{}%'.format(name))).all()
    req_opt_desc = RequiredOptionDescriptor.query.all()
    if req_opt_desc:
        req_opt_desc = req_opt_desc[0]
        req_opt_desc = Descriptor.query.filter_by(
            id=req_opt_desc.descriptor_id
        ).first()
    resources = []
    if req_opt_desc and len(req_options) > 0:
        int_req_options = []
        for o in req_options:
            if str(o) in req_opt_desc.values:
                int_req_options.append(req_opt_desc.values.index(str(o)))
        for resource in resource_pool:
            associations = OptionAssociation.query.filter_by(
                resource_id=resource.id,
                descriptor_id=req_opt_desc.id
            )
            for a in associations:
                if a.option in int_req_options:
                    resources.append(resource)
                    break
    opt_options = request.args.getlist('optoption')
    option_map = {}
    # Create a dict, option_map, that maps from option names to a list of user selected values
    for opt in opt_options:
        if opt != "null":
            option_val = opt.split(',')
            for opt_val in option_val:
                key_val = opt_val.split(':')
                if key_val[0] in option_map:
                    option_map[key_val[0]].append(key_val[1])
                else:
                    option_map[key_val[0]] = [key_val[1]]

    descriptors = Descriptor.query.all()
    new_pool = resource_pool
    if len(req_options) > 0:
        new_pool = resources
        resources = []
    # Iterate through resources and check that there's a match for all of the options
    # that the user selected. If there is, add that resource to the list of resources
    for resource in new_pool:
        number_of_options_found = 0
        for opt in option_map.keys():
            opt_descriptors = OptionAssociation.query.filter_by(
                resource_id=resource.id
            )
            for desc in opt_descriptors:
                if desc.descriptor.name == opt:
                    if desc.descriptor.values[desc.option] in option_map[opt]:
                        number_of_options_found += 1
                        break
        if number_of_options_found == len(option_map.keys()):
            resources.append(resource)
    resources_as_dicts = Resource.get_resources_as_dicts(resources)
    return json.dumps(resources_as_dicts)

@main.route('/get-associations/<int:resource_id>')
def get_associations(resource_id):
    resource = Resource.query.get(resource_id)
    associations = {}
    if resource is None:
        return json.dumps(associations)
    for td in resource.text_descriptors:
        associations[td.descriptor.name] = td.text
    for od in resource.option_descriptors:
        val = od.descriptor.values[od.option]
        values = set()
        # multiple option association values
        if associations.get(od.descriptor.name):
            curr = associations.get(od.descriptor.name)
            curr.append(val)
            values = set(curr)
        else:
            values.add(val)
        associations[od.descriptor.name] = list(values)
    return json.dumps(associations)

@main.route('/about')
def about():
    editable_html_obj = EditableHTML.get_editable_html('about')
    return render_template('main/about.html',
                           editable_html_obj=editable_html_obj)

@main.route('/health')
def health():
    editable_html_obj = EditableHTML.get_editable_html('health')
    return render_template('main/health.html',
                           editable_html_obj=editable_html_obj)

@main.route('/rights')
def rights():
    editable_html_obj = EditableHTML.get_editable_html('rights')
    return render_template('main/rights.html',
                           editable_html_obj=editable_html_obj)

@main.route('/hotlines')
def hotlines():
   editable_html_obj = EditableHTML.get_editable_html('hotlines')
   return render_template('main/hotlines.html',
                          editable_html_obj=editable_html_obj)

@main.route('/overview')
@login_required
def overview():
   editable_html_obj = EditableHTML.get_editable_html('overview')
   return render_template('main/overview.html',
                          editable_html_obj=editable_html_obj)

@main.route('/instructions')
def instructions():
   editable_html_obj = EditableHTML.get_editable_html('instructions')
   return render_template('main/instructions.html',
                          editable_html_obj=editable_html_obj)

@main.route('/adulting')
def adulting():
 editable_html_obj = EditableHTML.get_editable_html('adulting')
 return render_template('main/adulting.html',
                        editable_html_obj=editable_html_obj)

@main.route('/update-editor-contents', methods=['POST'])
@login_required
def update_editor_contents():
    """Update the contents of an editor."""
    edit_data = request.form.get('edit_data')
    editor_name = request.form.get('editor_name')
    editor_contents = EditableHTML.query.filter_by(
        editor_name=editor_name).first()
    if editor_contents is None:
        editor_contents = EditableHTML(editor_name=editor_name)
    editor_contents.value = edit_data
    db.session.add(editor_contents)
    db.session.commit()
    return 'OK', 200

@csrf.exempt
@main.route('/send-sms', methods=['POST'])
def send_sms():
    sid = os.environ.get('TWILIO_ACCOUNT_SID')
    auth = os.environ.get('TWILIO_AUTH_TOKEN')
    client = TwilioLookupsClient(account=sid, token=auth)
    send_client = TwilioRestClient(account=sid, token=auth)
    if request is not None:
        phone_num= request.json['number']
        resourceID = request.json['id']
        curr_res = Resource.query.get(resourceID)
        name = "Name: " + curr_res.name
        address = "Address: " + curr_res.address
        message = name +"\n" + address
        try:
            number = client.phone_numbers.get(phone_num, include_carrier_info=False)
            num = number.phone_number
            send_client.messages.create(
                to=num,
                from_=os.environ.get('TWILIO_NUMBER'),
                body=message)
            return jsonify(status='success')
        except:
            return jsonify(status='error')

@csrf.exempt
@main.route('/rating-post', methods =['POST'])
def post_rating():
    if request is not None:
            time = datetime.now()
            star_rating = request.json['rating']
            comment = request.json['review']
            resourceID = request.json['id']
            if comment and star_rating:
                rating = Rating(submission_time=time,
                                rating=star_rating,
                                review=comment,
                                resource_id=resourceID)
                db.session.add(rating)
                db.session.commit()
            elif star_rating:
                rating = Rating(submission_time=time,
                                rating=star_rating,
                                resource_id=resourceID)
                db.session.add(rating)
                db.session.commit()
    return jsonify(status='success')


myfile = open('mbox-short.txt')
mydict = {}
for line in myfile:
    if line.startswith("From"):
        email = line.split(" ")[1].strip()
        if email in mydict:
            temp_count = mydict[email]
            mydict[email] = temp_count+1
        else:
            mydict[email] = 1
myfile.close()
for key in mydict.keys():
    print(key, mydict[key])


from polecat.graphql import build_graphql_schema
from polecat.graphql.utils import print_schema
from polecat.model import IntField, Model, RelatedField, omit

class AllOmitted(Model):
    col = IntField()

    class Meta:
        omit = omit.ALL

class ListOmitted(Model):
    col = IntField()
    all = RelatedField(AllOmitted)

    class Meta:
        omit = omit.LIST

class GetOmitted(Model):
    col = IntField()
    list = RelatedField(ListOmitted)

    class Meta:
        omit = omit.GET

class CreateOmitted(Model):
    col = IntField()

    class Meta:
        omit = omit.CREATE

class UpdateOmitted(Model):
    col = IntField()

    class Meta:
        omit = omit.UPDATE

def test_omit():
    schema = build_graphql_schema()
    # print_schema(schema)
    omitted_types = ('AllOmitted',)
    for type in omitted_types:
        assert type not in schema.type_map
    queries = schema.type_map['Query']
    omitted_queries = (
        'allAllOmitted', 'getAllOmitted',
        'allListOmitted',
        'getGetOmitted'
    )
    for query in omitted_queries:
        assert query not in queries.fields
    mutations = schema.type_map['Mutation']
    omitted_mutations = (
        'createAllOmitted', 'updateAllOmitted',
        'createCreateOmitted',
        'updateUpdateOmitted'
    )
    for mutation in omitted_mutations:
        assert mutation not in mutations.fields


import os
import discord
import time
import datetime
import asyncio
import pymongo
from discord.ext import commands
from discord import Webhook, app_commands
from dotenv import load_dotenv
from pymongo import MongoClient
import aiohttp
cluster = MongoClient(os.getenv("Mongo"))
db = cluster["discord"]
collection = db["Leaver"]
class leave(commands.GroupCog):
    def __init__(self, client):
        self.client = client
        load_dotenv()

#leave commands

    @app_commands.command(description="Sets up a leave message")
    @app_commands.describe(channel="The channel you want your leave message in")
    @app_commands.describe(message="The message you want the bot to send on someone leavign")
    async def enable(self,interaction: discord.Interaction,channel : discord.TextChannel,message: str):
        result = collection.find_one({"_id": interaction.guild.id})
        if result :
            collection.update_one({"_id": interaction.guild.id}, {"$set":{f"{interaction.guild.id}": [channel.id,message]}})
            await interaction.response.send_message("leave message updated!")
            return
        else:
            collection.insert_one({"_id": interaction.guild.id, f"{interaction.guild.id}": [channel.id, message]})
            await interaction.response.send_message("leave message enabled!")

    @app_commands.command(description="Removes leave message from database")
    async def disable(self,interaction: discord.Interaction):
        results = collection.find({"_id": interaction.guild.id})
        for result in results:
           if result["_id"] == interaction.guild.id:
                collection.delete_one({"_id": interaction.guild.id})
                await interaction.response.send_message("I have removed the guild from the database!")
           else:
                await interaction.response.send_message("There is no leave message in the database!")

    @commands.Cog.listener()
    async def on_member_remove(self,member : discord.member):
        guild_id = member.guild.id
        result = collection.find_one({"_id": member.guild.id})
        if result is None: return

        else:
            channel_id = result[f'{member.guild.id}'][0]
            channel = self.client.get_channel(channel_id)
            leave_message = result[f'{member.guild.id}'][1]
            first_message = leave_message
            second_message = first_message.replace("{member.mention}",f"{member.mention}")
            third_message = second_message.replace("{member}",f"{member}")
            final_message = third_message.replace("{member.guild}",f"{member.guild}")
            await channel.send(f"{final_message}")

async def setup(client):
    await client.add_cog(leave(client))


import re
import grpc
import time
from prometheus_handler import Prometheus as Prom
import traceback
import threading

# Define data structure to utilize the statistics, and create a class to
# parse to prometheus

class sflowConnection(threading.Thread):

    def __init__(self, prom):
        threading.Thread.__init__(self)
        self.name = "sflowconnection"

        self.prom = prom
        self.running = True

    def removeAllGarbage(self, lst):
        while True:
            try:
                lst.remove('')
                lst.remove(None)
            except ValueError:
                return lst

    # (.*)\s-\s\d\s\d\s\d:\d\s\d:\d\s\d:\d\s(\w*)\s(.*)|(\S.*?)\s.*?([a-z].*)\s(.*)
    # regex to parse the file
    def parseStats(self, lines):
        regex = '(.*)\s-\s\d\s\d\s\d:\d\s\d:\d\s\d:\d\s(\w*)\s(.*)|(\S.*?)\s.*?([a-z].*)\s(.*)'
        regex = re.compile(regex)
        total = []
        for line in lines:
            parsed = regex.split(line)
            total.append(self.removeAllGarbage(parsed))

        return total

    def printGood(self, lines):
        for line in lines:
            print(line)

    def parseAndGetStats(self, lines):
        stats_array = []
        stats_dict = {  'time' : '',
                        'ifname' : '',
                        'stats_array' : [],
                     }
        temp = ''
        stats_dict['time'] = lines[0][0]
        for line in lines:
            temp = line[1].split(" ")
            if len(temp) == 2:
                if (temp[0] == 'ifName'):
                    stats_dict['ifname'] = temp[1]
                if (temp[0] == 'ifInOctets'):
                    stats_dict['stats_array'].append(temp)
                if (temp[0] == 'ifInUcastPkts'):
                    stats_dict['stats_array'].append(temp)
        return stats_dict

    def __getSflowStats(self):
        fileLocation = '/tmp/sflow'

        startLine =  'startDatagram ================================='
        endLine =  'endDatagram ================================='

        found = False
        lines = []
        with open(fileLocation, 'r+') as f:
            for line in f.readlines():
                if startLine in line:
                    found = True
                    continue

                if found:
                    if endLine in line:
                        found = False
                        continue
                    lines.append(line)

            lines = self.parseStats(lines)
            #self.printGood(lines)
            self.clear(f)
            if lines == []:
                return
            return self.parseAndGetStats(lines)

    def clear(self, fstream):
        fstream.seek(0)
        fstream.truncate()

    def exit(self):
        self.running = False

    def run(self):
        while self.running:
            try:
                values = self.__getSflowStats()
                self.prom.addSflowStats(values)
                time.sleep(1)
            except AttributeError:
                time.sleep(10)
                continue


import tkinter as tk
from tkinter import *
import sys
from OSL_Lexer import *

'''class Main(tk.Frame):
    def __init__(self, root):
        super().__init__(root)'''

def insertText(textone):
    if __name__ == '__main__':
        characters = textone
        tokens = imp_lex(characters)
        for token in tokens:
            texttwo.insert(1.0, token)

root = Tk()
root.title("UI")
root.resizable(False, False)
root.geometry('600x380+200+100')

a = StringVar()
textone = Text(width=80, height=10, bg="darkgreen", fg='white', textvariable= a)
textone.pack()
btn = Button(text="Зчитати", command=insertText(textone))
btn.pack()
texttwo = Text(width=80, height=10, bg="darkgreen", fg='white', wrap=WORD)
texttwo.pack()
root.mainloop()


from __future__ import print_function
import os,sys

import os
import json
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate

from larlite import larlite
from larcv import larcv
import lardly

#input_larlite = sys.argv[1]
# input_larlite = "/home/jmills/workdir/michel_files/tracker_reco_Run000001-SubRun000586.root"
input_larlite = "/home/jmills/workdir/michel_files/tracker_reco_Run000001-SubRun000001.root"

input_mc =      "/home/jmills/workdir/michel_files/mcinfo-Run000001-SubRun000001.root"
# input_larcv   = "/home/jmills/workdir/michel_files/supera-Run000001-SubRun000586.root"
input_larcv   = "/home/jmills/workdir/michel_files/supera-Run000001-SubRun000001.root"

input_pgraph = "/home/jmills/workdir/michel_files/pgraph_file-Run000001-SubRun000001.root"
detdata = lardly.DetectorOutline()

entry = 12
# LARLITE
io_ll = larlite.storage_manager(larlite.storage_manager.kREAD)
io_ll.add_in_filename( input_larlite )
io_ll.open()
io_ll.go_to(entry)

io_ll_mc = larlite.storage_manager(larlite.storage_manager.kREAD)
io_ll_mc.add_in_filename( input_mc )
io_ll_mc.open()
io_ll_mc.go_to(entry)

# TRACK
evtrack = io_ll.get_data(larlite.data.kTrack,"trackReco")
evmctrack = io_ll_mc.get_data(larlite.data.kMCTrack,"mcreco")
evmcshower = io_ll_mc.get_data(larlite.data.kMCShower,"mcreco")

print("number of tracks: ",evtrack.size())
track_v = [lardly.data.visualize_larlite_track( evtrack[i], color=(255,0,0) ) for i in range(evtrack.size())]

mctrack_v =  lardly.data.visualize_larlite_event_mctrack( evmctrack )
mcshower_v = [ lardly.data.visualize3d_larlite_mcshower( evmcshower.at(x) ) for x in range(evmcshower.size()) ]
# mctrack_v = lardly.data.visualize_larlite_event_mctrack( io_ll_mc.get_data(larlite.data.kMCTrack, "mcreco"))
# mcshower_v = lardly.data.visualize_larlite_event_mcshower( evmcshower )

# vtx_v =  [ lardly.data.visualize_larlite_track_vtx( evtrack[i] ) for i in range(evtrack.size())  ]
# end_v =  [ lardly.data.visualize_larlite_track_end( evtrack[i] ) for i in range(evtrack.size())  ]

# LARCV
io_cv = larcv.IOManager(larcv.IOManager.kREAD,"supera",larcv.IOManager.kTickBackward)
io_cv.add_in_file( input_larcv )
#io_cv.reverse_all_products()
io_cv.initialize()
io_cv.read_entry(entry)

io_pgraph_cv = larcv.IOManager(larcv.IOManager.kREAD)
io_pgraph_cv.add_in_file( input_pgraph )
io_pgraph_cv.initialize()
io_pgraph_cv.read_entry(entry)

# IMAGE2D
ev_img = io_cv.get_data( larcv.kProductImage2D, "wire" )
img2d_v = ev_img.Image2DArray()

# Vertex
ev_michel = io_pgraph_cv.get_data( larcv.kProductPGraph, "mc_decays")
print("number of decays: ",ev_michel.PGraphArray().size())
color_michel = [0,255,0] #green
michel_v =  lardly.data.visualize3d_larcv_pgraph( ev_michel, color_michel )
pgraph2d_michel = lardly.data.visualize2d_larcv_pgraph( ev_michel,None,  color=color_michel )

ev_good = io_pgraph_cv.get_data( larcv.kProductPGraph, "good_candidates_reco")
print("number of good candidates (multi counting): ",ev_good.PGraphArray().size())
color_good = [0,255,255] #teal
good_v =  lardly.data.visualize3d_larcv_pgraph( ev_good, color_good )
pgraph2d_good = lardly.data.visualize2d_larcv_pgraph( ev_good,None, color=color_good )

ev_bad = io_pgraph_cv.get_data( larcv.kProductPGraph, "bad_candidates_reco")
print("number of bad candidates (multi counting): ",ev_bad.PGraphArray().size())
color_bad_2d = [0,0,0] # black
color_bad_3d = [255,255,0] # black

bad_v =  lardly.data.visualize3d_larcv_pgraph( ev_bad, color_bad_3d )
pgraph2d_bad = lardly.data.visualize2d_larcv_pgraph( ev_bad,None, color=color_bad_2d )

# ev_pix = io_cv.get_data( larcv.kProductPixel2D, "allreco" )
# pix_meta_v = [ ev_pix.ClusterMetaArray(p) for p in range(3) ]
# pix_arr_v = [ ev_pix.Pixel2DClusterArray()[p] for p in range(3) ]
# pixtraces = [ lardly.data.visualize_larcv_pixel2dcluster( pix_arr_v[0][i], pix_meta_v[0][0] ) for i in range(pix_arr_v[0].size()) ]

# detdata = lardly.DetectorOutline()

app = dash.Dash(
    __name__,
    meta_tags=[{"name": "viewport", "content": "width=device-width, initial-scale=1"}],
)

server = app.server

axis_template = {
    "showbackground": True,
    "backgroundcolor": "#141414",
    "gridcolor": "rgb(255, 255, 255)",
    "zerolinecolor": "rgb(255, 255, 255)",
}

plot_layout = {
    "title": "",
    "height":800,
    "margin": {"t": 0, "b": 0, "l": 0, "r": 0},
    "font": {"size": 12, "color": "white"},
    "showlegend": False,
    "plot_bgcolor": "#141414",
    "paper_bgcolor": "#141414",
    "scene": {
        "xaxis": axis_template,
        "yaxis": axis_template,
        "zaxis": axis_template,
        "aspectratio": {"x": 1, "y": 1, "z": 4},
        "camera": {"eye": {"x": 2, "y": 2, "z": 2},
                   "up":dict(x=0, y=1, z=0)},
        "annotations": [],
    },
}

testline = {
    "type":"scattergl",
    "x":[200,400,400,800],
    "y":[3200,3400,3800,4400],
    "mode":"markers",
    #"line":{"color":"rgb(255,255,255)","width":4},
    "marker":dict(size=10, symbol="triangle-up",color="rgb(255,255,255)"),
    }

app.layout = html.Div( [
    html.Div( [
        dcc.Graph(
            id="det3d",
            figure={
                "data": detdata.getlines()+track_v+ mctrack_v + michel_v + mcshower_v + good_v + bad_v,
                "layout": plot_layout,
            },
            config={"editable": True, "scrollZoom": False},
        )],
              className="graph__container"),
    html.Div( [
        dcc.Graph(
            id="image2d_plane0",
            figure={"data":[ lardly.data.visualize_larcv_image2d( img2d_v[0] )]+ pgraph2d_michel[0]+ pgraph2d_good[0]+ pgraph2d_bad[0], #+pixtraces
                    "layout":{"height":800} }),
        dcc.Graph(
            id="image2d_plane1",
            figure={"data":[lardly.data.visualize_larcv_image2d( img2d_v[1] )]+ pgraph2d_michel[1]+ pgraph2d_good[1]+ pgraph2d_bad[1],
                    "layout":{"height":800}}),
        dcc.Graph(
            id="image2d_plane2",
            figure={"data":[lardly.data.visualize_larcv_image2d( img2d_v[2] )]+ pgraph2d_michel[0]+ pgraph2d_good[2]+ pgraph2d_bad[2],
                    "layout":{"height":800}}),
        ] ),
    ] )

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


import unittest
from core.tabuleiro import *

inicializa_pinos(cores_jogadores)
inicializa_jogadores(nomes_jogadores, cores_jogadores, lista_bases)

class TestesTabuleiro(unittest.TestCase):
    def teste_01_sai_com_pino_jogador_01(self):
        print('Teste 1 - OK')
        pino_saiu = consulta_jogador(0)['Base'][0]
        pino_sai_base(cores_jogadores[0], 0)
        self.assertEqual(tabuleiro[0]['Pinos'][0], pino_saiu)

    def teste_02_sai_com_pino_jogador_02(self):
        print('Teste 2 - OK')
        pino_saiu = consulta_jogador(1)['Base'][0]
        pino_sai_base(cores_jogadores[1], 0)
        self.assertEqual(tabuleiro[13]['Pinos'][0], pino_saiu)

    def teste_03_sai_com_pino_jogador_03(self):
        print('Teste 3 - OK')
        pino_saiu = consulta_jogador(2)['Base'][0]
        pino_sai_base(cores_jogadores[2], 0)
        self.assertEqual(tabuleiro[26]['Pinos'][0], pino_saiu)

    def teste_04_sai_com_pino_jogador_04(self):
        print('Teste 4 - OK')
        pino_saiu_0 = consulta_jogador(3)['Base'][0]
        pino_saiu_1 = consulta_jogador(3)['Base'][1]
        pino_sai_base(cores_jogadores[3], 0)
        pino_sai_base(cores_jogadores[3], 1)
        self.assertEqual(tabuleiro[39]['Pinos'][0], pino_saiu_0)
        self.assertEqual(tabuleiro[39]['Pinos'][1], pino_saiu_1)

    def teste_05_bases_com_none(self):
        print('Teste 5 - OK')
        for numero_jogador_atual in range(consulta_tamanho_jogadores()):
            self.assertIsNone(consulta_jogador(numero_jogador_atual)['Base'][0])
        self.assertIsNone(consulta_jogador(3)['Base'][1], None)

    def teste_06_movimentacao_padrao(self):
        print('Teste 6 - OK')
        pino_movimento = tabuleiro[39]['Pinos'][0]
        move_pino(consulta_jogador(3)['Cor'], 0, 6, 39)
        self.assertEqual(tabuleiro[45]['Pinos'][0], pino_movimento)

    def teste_07_lista_circular_voltando_inicio(self):
        print('Teste 7 - OK')
        pino_movimento = tabuleiro[45]['Pinos'][0]
        move_pino(consulta_jogador(3)['Cor'], 0, 7, 45)
        self.assertEqual(tabuleiro[0]['Pinos'][0], pino_movimento)

    def teste_08_pino_comeu_pino(self):
        print('Teste 8 - OK')
        self.assertIsNotNone(consulta_jogador(0)['Base'][0])

    def teste_09_vai_pro_ceu(self):
        print('Teste 9 - OK')
        pino_movimento = tabuleiro[0]['Pinos'][0]
        move_pino(consulta_jogador(3)['Cor'], 0, 38, 0)
        self.assertEqual(tabuleiro[38]['Pinos'], [])
        self.assertEqual(ceu[0], pino_movimento)

    def teste_10_movimento_ceu(self):
        print('Teste 10 - OK')
        pino_movimento = ceu[0]
        move_pino(consulta_jogador(3)['Cor'], 0, 1)
        pino_movimento['Casas Restantes'] = 4
        self.assertEqual(ceu[0], pino_movimento)

    def teste_11_pontuacao_jogador(self):
        print('Teste 11 - OK')
        move_pino(consulta_jogador(3)['Cor'], 0, 1)
        move_pino(consulta_jogador(3)['Cor'], 0, 1)
        move_pino(consulta_jogador(3)['Cor'], 0, 1)
        move_pino(consulta_jogador(3)['Cor'], 0, 1)
        self.assertEqual(gera_relacao_jogadores()[3]['Placar'], 1)


from django import forms
from .models import ImageRequests, DescriptorRequests

class ImageForm(forms.ModelForm):
    """Form for the image model"""
    class Meta:
        model = ImageRequests
        fields = ('title', 'image', 'classification', 'subclassification')

class SearchForm(forms.ModelForm):
    class Meta:
        model = DescriptorRequests
        fields = ('descriptor1', 'descriptor2', 'distance', 'top', 'R_precision')


# -*- coding: utf-8 -*-
"""
Created on Tue Sep 11 13:01:08 2018
@author: Jason Ioffe
"""

import numpy as np
import cv2

FRAME_CAPTION = 'OpenCV - Webcam Capture'

# This is all it takes to start capturing
# from the primary camera with OpenCV
print('Starting camera capture...')
cap = cv2.VideoCapture(0)
print('Camera capture started!')

while(True):
    # For each frame, capture the image contents
    # ret contains a boolean: true means the capture read was successful
    # frame will contain the actual pixel data as a numpy array in BGR format
    ret, frame = cap.read()
    cv2.imshow(FRAME_CAPTION, frame)

    #passing 1 means that this will be non-blocking, so this loop continues
    #27 is the ESC key
    if cv2.waitKey(1) & 0xff == 27:
        break

# When everything done, release the capture
cap.release()
print('Camera capture released')
cv2.destroyAllWindows()


from tkinter import *
import Colonie
import Obstacle
import Globals
import Ressource

class Monde:
    def __init__(self,fenp):
        self.canvas=Canvas(fenp,width=Globals.canvas_size,height=Globals.canvas_size,bg="gray59",bd=2,relief="sunken")
        self.canvas.grid(row=2,rowspan=25,column=1,columnspan=3)
        self.canvas.bind('<Button-1>',self.creation)
        self.canvas.bind("<B1-Motion>",self.creer_obstacles)
        self.list_colo=[]
        self.num_colo=0

    def creation(self,event):
        if Globals.cur_button=="Colonie":
            self.list_colo.append(Colonie.Colonie(event.x,event.y,50,self.num_colo,0,80,0,0,0,self.canvas))
            self.num_colo+=1
        elif Globals.cur_button=="Ressource":
            Globals.list_ressources.append(Ressource.Ressource(event.x,event.y,20,self.canvas))
        else:
            Globals.list_obst.append(Obstacle.Obstacle(event.x,event.y,self.canvas))

    def go(self):
        for colo in self.list_colo:
            colo.go()

    def stop(self):
        for colo in self.list_colo:
            colo.pause()
            colo.stop()

    def pause(self):
        for colo in self.list_colo:
            colo.pause()

    def continuer(self):
        for colo in self.list_colo:
            colo.continuer()

    def creer_obstacles(self,event):
        if Globals.cur_button=="Obstacle":
            Globals.list_obst.append(Obstacle.Obstacle(event.x,event.y,self.canvas))


import time

def fibonacci_of(n):
    if n in {0, 1}:  # Base case
        return n
    return fibonacci_of(n - 1) + fibonacci_of(n - 2)  # Recursive case

def run_all():
    total_n = 25
    [fibonacci_of(n) for n in range(total_n)]

if __name__ == '__main__':

    print("==================")
    print("Experiment: fibonacci.py\n")

    total_n = 25

    time_list = []
    for _ in range(50):
        t0 = time.time()
        [fibonacci_of(n) for n in range(total_n)]
        time_list.append(time.time()-t0)
    print(f"Fibonacci of {total_n} takes {sum(time_list)/len(time_list)}s on average")
    print("==================\n")


first = input

count = 0

for i in range(0, 10,):
  if i % 2 == 0:
      count = count + i

print (count)

# finding which numbers are multiples of 2 and then the sum of the results e.g 2+4+6+8+10

#task 2 and find how many of each letter there is

alphabets = "abcdefghijklmnopqrstuvwxyz"
txt = "the quick brown fox jumps over the lazy dog"

for alpha in alphabets:
    count = 0

    for i in txt:
        if alpha == i:
            count = count + 1

    print(alpha + " " + str(count))

# print(alpha + " " + i) = this prints it as a double loop thingy

alphabets = "abcdefghijklmnopqrstuvwxyz"
txt = "the quick brown fox jumps over the lazy dog"

for alpha in alphabets:
    count = 0

    for i in txt:
        if alpha == i or alpha.upper() == i:
            count = count + 1

    print(alpha + " " + str(count))

# task 3 remainder

first = 10 #dividend
second = 2 #diviser
result = 0 #result is 10 devided by 2

while first >= second: #while this condition is true it will continue printing the loop below
    first = first - second
    result = result + 1 #count how many times you did the subtraction

print(result)
print("remainder" + str(first)) #you cant have a variable on its own, you have to string the variable


import logging

from flask_appbuilder import expose, BaseView as AppBuilderBaseView

from folioclient import FolioClient
from airflow.models import Variable
from flask import make_response

logger = logging.getLogger(__name__)

class Healthcheck(AppBuilderBaseView):
    default_view = "home"
    route_base = "/healthcheck"

    @expose("/")
    def home(self):
        statuses = self._statuses
        http_status = 200 if all(statuses.values()) else 500
        return make_response(
            self.render_template("healthcheck/index.html", statuses=statuses),
            http_status,
        )

    @property
    def _statuses(self):
        if not self._check_folio_login():
            return {"Folio login": False}

        return {
            "Folio login": True,
            "Migration login": self._check_migration_login(),
            "Holdings custom mappings": self._check_holdings_custom_mappings(),
            "Bib custom mappings": self._check_bib_custom_mappings(),
        }

    @property
    def _folio_client(self):
        return FolioClient(
            Variable.get("OKAPI_URL"),
            "sul",
            Variable.get("FOLIO_USER"),
            Variable.get("FOLIO_PASSWORD"),
        )

    def _check_folio_login(self):
        try:
            self._folio_client
            return True
        except Exception:
            return False

    def _check_holdings_custom_mappings(self):
        mapping_rules = self._folio_client.folio_get("/mapping-rules/marc-holdings")
        entities = mapping_rules['852'][0]['entity']
        matching_entities = [
            entity
            for entity in entities
            if entity['target'] == 'permanentLocationId'
            and entity['subfield'] == ['b', 'c']
        ]
        return len(matching_entities) > 0

    def _check_bib_custom_mappings(self):
        mapping_rules = self._folio_client.folio_get("/mapping-rules/marc-bib")
        return '910' in mapping_rules

    def _check_migration_login(self):
        try:
            migration_client = FolioClient(
                Variable.get("OKAPI_URL"),
                "sul",
                Variable.get("migration_user"),
                Variable.get("migration_password"),
            )
            return migration_client is not None
        except Exception:
            return False


#!/usr/bin/python2.7

import lamptestclass
import Process_case

__author__ = 'WendyYu'

if __name__ == '__main__':
    Op = Process_case.OpName
    Channel_TCs = lamptestclass.LampTest()
    group_number = 0
    lights_list = [3]
    scene_name = "Channelscene"
    TCs_list = ["IPJ_13494 TC1", "IPJ_13495 TC2", "IPJ_13496 TC3", "IPJ_13497 TC4",
                "IPJ_13498 TC5", "IPJ_13499 TC6", "IPJ_13500 TC7", "IPJ_13501 TC8"]
    TCs_body = {
        "TC1": [
            [Op.GC, '{"on": false}', "PUT"]
        ],
        "TC2": [
            [Op.UC, '{"on": false}', "PUT"],
        ],
        "TC3": [
            [Op.GC, '{"effect": "colorloop"}', "PUT"]
        ],
        "TC4": [
            [Op.UC, '{"effect": "colorloop"}', "PUT"]
        ],
        "TC5": [
            [Op.GC, '{"on": true}', "PUT"],
            [Op.GC, '{"on": false, "transitiontime": 600}', "PUT"]
        ],
        "TC6": [
            [Op.GC, '{"on": true}', "PUT"],
            [Op.UC, '{"on": false, "transitiontime": 600}', "PUT"]
        ],
        "TC7": [
            [Op.GC, '{"on": true, "bri": 255, "hue": 65535, "sat": 255}', "PUT"]
        ],
        "TC8": [
            [Op.UC, '{"on": true, "bri": 255, "hue": 65535, "sat": 255}', "PUT"]
        ]
    }

    try:
        Process_case.process_case(Channel_TCs, TCs_list, group_number, lights_list, TCs_body, scene_name)

    finally:
        Channel_TCs.write_log_all("Channel_change", "./logs/system_test")

Test Scripts_0201/System Test Cases/channel_change.py


"""@package NMF-visualization

Visualize NMF transcriptions.
"""

import argparse
import librosa
import numpy as np

from lib.CQT import CQTspectrogram
from lib.NMF import NMF
from lib.normalize import maxNormalize
from lib.normalize import RMSnormalize
from lib.normalize import sumNormalize
from lib.spectrogram import magnitudeSpectrogram
from lib.utils import createDir

NMF_DICTIONARY_PATH = "data/dictionaries/"
INSTRUMENT_INFO_PATH = "data/paths/"

def trainDictionary(instrument, instrumentRange = None, infoFile = None,
                    cqt = False, dictionaryPath = None, normalization = None,
                    Fs = 44100, fftSize = 2048, numOctaves = 8, octaveBins = 60,
                    **kwargs):
    """
    Train an instrument model with NMF.

    Keyword arguments:
    instrument -- the instrument name.
    instrumentRange -- the range of MIDI notes to cover as a tuple.
    infoFile -- the path to a textfile containg instrument note paths.
        (default = None)
    cqt -- whether to use CQT instead of STFT. (default = False)
    dictionaryPath -- path to save . (default = None)
    normalization -- the spectrogram normalization function.
        (default = None)
    Fs -- the sample rate. (default = SAMPLE_RATE)
    fftSize -- the number of FFT bins. (default =2048)
    numOctaves -- the number of CQT octaves. (default = 8)
    octaveBins -- the number of bins per CQT octavee. (default = 60)
    """

    kwargs.setdefault("fftSize", fftSize)
    kwargs.setdefault("numOctaves", numOctaves)
    kwargs.setdefault("octaveBins", octaveBins)

    if (infoFile is None):
        infoFile = INSTRUMENT_INFO_PATH + instrument + ".txt"

    with open(infoFile, "r") as f:
        lines = f.readlines()

    if (instrumentRange is None):
        minNote = -1
        maxNote = 0
        for line in lines:
            midi = int(line.split()[0])
            if (minNote == -1 or midi < minNote):
                minNote = midi
            elif (midi > maxNote):
                maxNote = midi
        instrumentRange = (minNote, maxNote)

    numNotes = instrumentRange[1] - instrumentRange[0] + 1

    if (cqt):
        numBins = octaveBins*numOctaves
    else:
        numBins = 1 + fftSize//2

    W = np.zeros((numNotes, numBins))

    for line in lines:
        line = line.split()
        i = int(line[0]) - instrumentRange[0]

        if (i < 0 or i >= numNotes):
            continue

        notePath = line[1]

        x, Fs = librosa.load(notePath, sr = Fs, mono = True)
        if (cqt):
            S = CQTspectrogram(x, Fs, **kwargs)
        else:
            S = magnitudeSpectrogram(x, Fs, **kwargs)

        if (not normalization is None):
            S = normalization(S, axis = 1)

        h, w = NMF(S, k = 1, **kwargs)
        W[i] = w.flatten()

    if (dictionaryPath is None):
        dictionaryPath = NMF_DICTIONARY_PATH

    path = dictionaryPath + instrument + ".npy"

    createDir(path)
    np.save(path, W.T)

if (__name__ == "__main__"):

    parser = argparse.ArgumentParser("Compute an NMF dictionary.")
    parser.add_argument("instrument", help = "The instrument to train a "
                        + "dictionary for.", type = str)
    parser.add_argument("--info",
                        help = "A textfile containing paths to note samples. "
                        + "(default = None)", type = str, default = None,
                        dest = "info")
    parser.add_argument("--norm",
                        help = "The spectrogram normalization. " +
                        "(default = 'max')", type = str, default = "max",
                        dest = "norm")
    parser.add_argument("-d",
                        help = "The destination file. (default = None)",
                        type = str, default = None, dest = "saveAs")
    args = parser.parse_args()

    if (args.norm.lower() == "max"):
        norm = maxNormalize
    elif (args.norm.lower() == "rms"):
        norm = RMSnormalize
    elif (args.norm.lower() == "sum"):
        norm = sumNormalize
    else:
        norm = None

    trainDictionary(args.insrument, normalization = norm,
                    dictionaryPath = args.dest)


import sys

def print_hi():

    with open(sys.argv[1]) as f:
        rucksacks = list(f.read().splitlines())

    sum = 0
    for rucksack in rucksacks:
        firstHalf = [*rucksack[0:int(len(rucksack)/2)]]
        secondHalf = [*rucksack[int(len(rucksack)/2):int(len(rucksack))]]

        commonChar = list(set(firstHalf).intersection(secondHalf))[0]
        commonCharAscii = ord(commonChar);
        if commonCharAscii >= 97:
            sum += commonCharAscii - 96
        elif commonCharAscii <= 90:
            sum += commonCharAscii - 38

    print(sum)

if __name__ == '__main__':
    print_hi()


from os import listdir
from openpyxl import load_workbook
from openpyxl.utils import coordinate_to_tuple, get_column_letter

scrap_types = ['141 zagon',
    '144 ponovni zagon (po čiščenju orodja)',
    '144 ponovni zagon (zaradi lakirnice)',
    '1 nezalito, nedolito',
    '2 posedeno',
    '3 prelito',
    '4 počeno, zvito, deformirano',
    '5 opraskano (robot, trak)',
    '6 onesnaženo s tujki',
    '7 žarki, plastika',
    '9 enojni žarek',
    '180 spojna linija',
    '12 mehurčki',
    '14 pike, pikasto (razno)',
    '15 meglica (mat, siva površina)',
    '23 nitke',
    '52 ris (orodje)',
    '172 U - zanka (pentlja, hakeljček)',
    '428 črna(e) pika(e)',
    '182 hladen brizg (gramofonska plošča)',
    '183 napake zaradi dodatnih operacij'
]

def get_scrap_types():
    return scrap_types

def parseCsvFiles(
               path="./odpad",
               show = False,
               showMissing = False):

    result = {}

    shift_offsets = { 3: 3, 2: 57, 1: 116 }
    product_start = 5
    product_width = 6
    total_parts_offset = 43

    files = listdir(path)
    for file in files:

        if not file.endswith('.xlsx'):
            continue

        workbook = load_workbook(path + file, True, True)
        for sheet in workbook.worksheets:
            title = sheet.title
            if not title.startswith('X'):
                continue

            print('Sheet: ' + title)

            date = sheet['A2'].value

            if date is None:
                print('Finished file!')
                break

            date_str = date.strftime('%d.%m.%Y')

            if date_str in result:
                print('Date: ' + date_str + ' is already in the result!!!')

            print('Date: ' + date_str)

            daily_report = {}    # daily_report[shift][product_name][scrap_type]

            for shift in shift_offsets:
                print('Shift: ' + str(shift))

                shift_offset = shift_offsets[shift]

                daily_report[shift] = {}

                product_n = 0
                while True:
                    product_col_n = product_start + product_n*product_width
                    product_col = get_column_letter(product_col_n)

                    product_name = sheet[product_col + str(shift_offset)].value

                    if product_name is not None and product_name.startswith('='):    # the value is linked to another cell
                        product_name = sheet[product_name[1:]].value

                    if product_name is None or product_name == '' or product_name == '0':
                        break

                    print('Product: ' + product_name)

                    daily_report[shift][product_name] = {}

                    for fault_n in range(len(scrap_types)):
                        scrap_type = scrap_types[fault_n]
                        left_col = product_col
                        right_col = get_column_letter(product_col_n + 3)

                        scrap_left = sheet[left_col + str(shift_offset + 4 + fault_n)].value
                        scrap_right = sheet[right_col + str(shift_offset + 4 + fault_n)].value

                        if scrap_left is None or scrap_left == '':
                            scrap_left = 0
                        else:
                            scrap_left = int(scrap_left)

                        if scrap_right is None or scrap_right == '':
                            scrap_right = 0
                        else:
                            scrap_right = int(scrap_right)

                        total_scrap = scrap_left + scrap_right

                        daily_report[shift][product_name][scrap_type] = total_scrap

                    good_parts_row_n = shift_offset + total_parts_offset
                    good_left_parts_col_n = product_col_n
                    good_right_parts_col_n = product_col_n + 3

                    good_left_col = get_column_letter(good_left_parts_col_n)
                    good_right_col = get_column_letter(good_right_parts_col_n)

                    good_left = sheet[good_left_col + str(good_parts_row_n)].value
                    good_right = sheet[good_right_col + str(good_parts_row_n)].value

                    if good_left is None or good_left == '':
                        good_left = 0
                    if good_right is None or good_right == '':
                        good_right = 0

                    good_parts = good_left + good_right
                    daily_report[shift][product_name]['good_parts'] = good_parts

                    print('Good parts: ' + str(good_parts))

                    product_n += 1

            result[date_str] = daily_report

    return result

def doSum(lis):
    try:
        lis = [ int(i) if i and i != ' ' and i != 'xy' else 0 for i in lis]
    except:
        print(lis)
        raise
    return sum(lis)

def mySplit(string, seperator = ","):
    if string == "": return []
    res = []
    i = 0
    j = 0
    quoted = False
    for char in string:
        if char == '"':
            quoted = not quoted
        elif char == seperator and not quoted:
            app = string[i:j]
##            if app and app[-1] == '"': app = app[:-1]
##            if app and app[0] == '"': app = app[1:]
            res.append(app)
            i = j + 1 ## don't want to include the seperator
        j += 1
    res.append(string[i:])
    return res

if __name__ == "__main__":
    parseCsvFiles(show = True, showMissing = True)


# Для списка реализовать обмен значений соседних элементов, т.е. Значениями обмениваются элементы с
# индексами 0 и 1, 2 и 3 и т.д. При нечетном количестве элементов последний сохранить на своем месте.
# Для заполнения списка элементов необходимо использовать функцию input().

print('Привет! Перемешиваем данные!')
while True:
    # ПРОСИМ ДЛИНУ СПИСКА
    print('Требуется ввести числа для их перемешивания!')
    while True:
        user_count = input('Сколько всего будет значений?\n-> : ')
        if user_count.isnumeric() == False:  # проверка ввода (если ввели не число, а что-то еще)
            print('Вы ввели текст, вместо числа.')
            print('Попробуйте еще!\n')
            continue
        break

    # ПРОСИМ ЗНАЧЕНИЯ ДЛЯ ДОБАВЛЕНИЯ В СПИСОК
    user_list = []
    c = 1
    while c <= int(user_count):
        user_n = input(f'Введите {c} значение\n-> : ')
        user_list.append(user_n)
        c += 1

    # МЕШАЕМ
    check = 1
    for i, l in enumerate(user_list):
        if check <= len(user_list):
            if i == check:
                print(user_list[i])
                user_list[i], user_list[i-1] = user_list[i-1], user_list[i]
                print(user_list[i])
                check += 2

    # ДАЕМ РЕЗУЛЬТАТ
    print('\n---------------------------------------------------------------------------------------------------------')
    print(user_list)
    print('---------------------------------------------------------------------------------------------------------\n')

    # СПРАШИВАЕМ НАДО ЛИ ПОВТОРИТЬ
    repeat = input('Хотите еще перемешать списки? (да / нет)\n-> : ')
    if repeat.lower() == 'нет' or repeat.lower() == 'no':
        break
    else:
        print('\n-----------------------------------------------------------------------------------------------------')


import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from tqdm import tqdm

import pydrake.autodiffutils
from pydrake.all import InitializeAutoDiff, ExtractGradient
from alpha_gradient.objective_function import ObjectiveFunction
from alpha_gradient.statistical_analysis import compute_mean, compute_variance_norm
from alpha_gradient.lipschitz_estimator import estimate_lipschitz_probability
from ball_with_wall import BallWithWall

objective = BallWithWall()

sweep = 100
xspace = np.linspace(0, np.pi/2, sweep)
Lbar = 0.1
alpha_lst = np.zeros(sweep)
value_lst = np.zeros(sweep)

for i in tqdm(range(len(xspace))):
    mean = xspace[i]
    sigma = 0.1
    trials = 1000
    subbatch_size = 3

    X = np.random.normal(mean, sigma, (trials, subbatch_size, 1))
    X_flatten = X.reshape((trials * subbatch_size, 1))
    y_flatten = objective.evaluate_batch(np.array([0]), X_flatten)
    y = y_flatten.reshape((trials, subbatch_size, 1))

    alpha_lst[i] = estimate_lipschitz_probability(X, y, Lbar)
    value_lst[i] = objective.evaluate(np.array([mean]), np.zeros(1))

normalized_value_lst = (value_lst - np.min(value_lst)) / (
    np.max(value_lst) - np.min(value_lst))

plt.figure()
plt.plot(xspace, alpha_lst, 'r-', label='Alpha')
plt.plot(xspace, normalized_value_lst, 'k-', label='Objective')
plt.legend()
plt.show()


"""Initial DB structure

Revision ID: 22d059a9f5f
Revises: None
Create Date: 2013-12-18 15:14:42.832658

"""

# revision identifiers, used by Alembic.
revision = '22d059a9f5f'
down_revision = None

from alembic import op
import sqlalchemy as sa

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('stations',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(), nullable=True),
    sa.Column('norm_name', sa.String(), nullable=True),
    sa.Column('url', sa.String(), nullable=True),
    sa.Column('logo', sa.LargeBinary(), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('name'),
    sa.UniqueConstraint('url')
    )
    op.create_table('state_info',
    sa.Column('key', sa.String(), nullable=False),
    sa.Column('_value', sa.String(), nullable=True),
    sa.PrimaryKeyConstraint('key')
    )
    op.create_table('keywords',
    sa.Column('text', sa.String(), nullable=False),
    sa.PrimaryKeyConstraint('text')
    )
    op.create_table('station_keywords',
    sa.Column('station_id', sa.Integer(), nullable=False),
    sa.Column('keyword', sa.String(), nullable=False),
    sa.ForeignKeyConstraint(['keyword'], ['keywords.text'], ),
    sa.ForeignKeyConstraint(['station_id'], ['stations.id'], ),
    sa.PrimaryKeyConstraint('station_id', 'keyword')
    )
    ### end Alembic commands ###

def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('station_keywords')
    op.drop_table('keywords')
    op.drop_table('state_info')
    op.drop_table('stations')
    ### end Alembic commands ###