"""
File name:  group_1_gdpr_project.py

Purpose of code:  CPCO will use information extracted from 5 data sources to create 5 dashboards, each with distinct information but that is all pertinent to data privacy regulations and the consequences of violating those regulations.  CPCO obtained its data by scraping JavaScript and html websites, and parsing a word file.  We also used an unusual source--Google Forms--to survey friends, family, and colleagues about their data privacy preferences.  We obtained 68 responses to 5 focused questions, each question offering the user the opportunity to select multiple answer choices or to provide additional comments.  We will use Python to delineate each of the unique answers that the users provided to each question and conduct calculations, including the percentage of users who selected each particular answer choice.

-  Dashboard A identifies specific US laws that meet the requirements of particular GDPR articles.

-  Dashboard B highlights European countries that have instituted privacy laws supplementing those mandated in the GDPR.  To view, please enter B into the input box
-  Dashboard C displays fines levied by the UK's Information Commissioner's Office (ICO) against British companies violating data privacy regulations, an example of one European country's domestic prosecutions citing GDPR.

-  Dashboard D demonstrates the most common methods hackers used in breaching data, the industries targeted most heavily, and the average number of records compromised per organization.

- Dashboard E provides a sample of customers' growing concerns about data privacy, based on a survey conducted by CP CO.

"""
#group_1_gdpr_project.py
# Carly Harris, Darshan Tina, Lokesh Nandiraju, Heather Manganello, Kopal Agrawal

#import statements
import textwrap
import pandas as pd
import csv
import datetime
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table as dt
from dash.dependencies import Input, Output
import matplotlib.pyplot as plt
import plotly.graph_objs as go
import numpy as np
from decimal import Decimal
import io
from docx import Document
import re

#dashboard styling
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

colors = {
    'background': '#D3E0F5'
}

# ************************** User Interface of Dash Board 1 ********************
#Read data from Word document into Dataframe
def read_docx(filename, tab_id=None, **kwargs):
    def read_docx_tab(tab, **kwargs):
        vf = io.StringIO()
        writer = csv.writer(vf)
        for row in tab.rows:
            writer.writerow(cell.text for cell in row.cells)
        vf.seek(0)
        return pd.read_csv(vf, **kwargs)

    doc = Document(filename)
    if tab_id is None:
        return [read_docx_tab(tab, **kwargs) for tab in doc.tables]
    else:
        try:
            return read_docx_tab(doc.tables[tab_id], **kwargs)
        except IndexError:
            print('Error: specified [tab_id]: {}  does not exist.'.format(tab_id))
            raise

#Parse raw data from .docx file
my_pan_file = read_docx('DS1_Uncleaned.docx')[0]

#Export read .docx content to a CSV file
my_pan_file.to_csv('DS_1_Cleaned.csv', sep=',')

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

#Read data from CSV file
my_panda = pd.read_csv('DS_1_Cleaned.csv')
del my_panda['Unnamed: 0']

### Take article number as input
print('\n\n##################################################################################################')
print('\nBeginning of execution on Data Source 1\n\n')

wanna_run_again=True
while wanna_run_again==True:
    art_num_good =False
    while art_num_good==False:
        try:
            art_no = int(input('\n\nPlease enter the article number: '))
            if 1<=art_no<=99:
                art_num_good=True
            else:
                print('Article number '+str(art_no)+' doesn\'t exist. Enter between 1 to 99 only')
        except:
            print('Bad format!. Please enter GDPR article number between 1 to 99 only')

    print('Article number '+str(art_no)+' is called \"'+str(my_panda.iloc[art_no,0])[3:]+'\"')

    ### Checking corresponding US LAW exists or not
    check_law_flag=False
    while check_law_flag==False:
        print()
        check_law=input('Do you want to check whether corresponding United States Law exists for the GDPR article number ' +str(art_no)+' ?\nEnter Y or N (Not case sensitive): ')
        if check_law=='Y' or check_law=='y':
            if str(my_panda.iloc[art_no,2])[0:3]=='N/A':
                print('\nNo corresponding U.S Law exists\nNON-COMPLIANT\n')
                check_law_flag=True
            else:
                #print Law exists and ask whether they want to see the text of that law
                print('\nRelevant U.S Law exists!!\nCOMPLIANT\n')
                check_display_law=False
                while check_display_law==False:
                    display_law=input('Do you want to view corresponding United States Law for GDPR article number ' +str(art_no)+' ?\nEnter Y or N (Not case sensitive): ')
                    if  display_law=='Y' or display_law=='y':
                        print('\nThe corresponding U.S Law for GDPR article number '+str(art_no)+' is: \n')
                        print('\"'+my_panda.iloc[art_no,2]+'\""')
                        check_law_flag=True
                        check_display_law=True
                    elif display_law=='N' or display_law=='n':
                        check_law_flag=True
                        check_display_law=True
                        print()
                    else:
                        print('Bad Input! Enter either Y or N: (Not case sensitive)')
        elif check_law=='N' or check_law=='n':
            print()
            check_law_flag=True
        else:
            print('Bad Input! Enter either Y or N (Not case sensitive): ')

    ### Ask user whether they want to see text for the GDPR article number they entered and display if they want to
    check_article_flag=False
    while check_article_flag==False:
        check_article=input('Do you want to view GDPR Regulation for article number '+str(art_no)+'?\n'+'Enter Y or N (Not case sensitive): ')
        if check_article=='Y' or check_article=='y':
            print()
            print('GDPR article number '+str(art_no)+' states that:')
            print()
            print('\"'+str(my_panda.iloc[art_no,1])+'\"')
            check_article_flag=True
            run_again_flag=False
            while run_again_flag==False:
                run_again=input('\nDo you want to run this again???\nEnter Y or N (Not case sensitive): \n')
                if run_again=='Y' or run_again=='y':
                    wanna_run_again=True
                    run_again_flag=True
                elif run_again=='N' or run_again=='n':
                    wanna_run_again=False
                    run_again_flag=True
                else:
                    run_again_flag=False
                    print('Bad input. Enter either Y or N (Not case sensitive): ')
        elif check_article=='N' or check_article=='n':
            check_article_flag=True
            run_again_flag=False
            while run_again_flag==False:
                run_again=input('\nDo you want to run this again???\nEnter Y or N (Not case sensitive): \n')
                if run_again=='Y' or run_again=='y':
                    wanna_run_again=True
                    run_again_flag=True
                elif run_again=='N' or run_again=='n':
                    wanna_run_again=False
                    run_again_flag=True
                else:
                    run_again_flag=False
                    print('Bad input. Enter either Y or N (Not case sensitive): ')

desc_lst=[]
for val in my_panda['GDPR & U.S Law Data.2']:
    if val[:3]=='N/A':
        desc_lst.append('Compliant')
    else:
        desc_lst.append('Not Compliant')
desc_pan=pd.Series(desc_lst)
print('\n\n\nCompliance Summary: \nThe number of GDPR articles for which compliant/corresponding U.S Laws exist are: '+str(desc_pan.describe()[3])+'.')
print('\nEnd of execution on Data Source 1')
print('##################################################################################################\n\n')

# ******************************* APPLICATION LAYOUT ********************************
# header layout
app.layout = html.Div(style={'backgroundColor': colors['background']}, children=[
    html.H1(children='CP CO',
            style={
                'textAlign': 'center'
                }),
    html.Div(children='GDPR and Information Security Consultancy',
             style={
                 'textAlign': 'center'
                 }),
    html.Div(children='Our mission: ',
             style={
                 'textAlign': 'center',
                 'font-weight': 'bold'
                 }),
    html.Div(children='To help organizations understand their vulnerabilities in data privacy protection and information security',
             style={
                 'textAlign': 'center'
                 }),
    html.Div(children='requirements for compliance with European regulations consequences for perceived violations, &',
             style={
                 'textAlign': 'center'
                 }),
    html.Div(children='avoid massive fines of up to 4% of revenue in order to effectively manage the use of European individuals personal information within their operations.',
             style={
                 'textAlign': 'center'
                 }),

    html.Div(children='Enter:'),
    html.Div(children='1-99 = Dashboard A: Correlation of US Laws to European Union\'s General Data Protection Regulation (GDPR) Articles '),
    html.Div(children='B = Dashboard B: Additional Data Privacy Requirements Per European Union Country'),
    html.Div(children='C = Dashboard C: Companies Fined by GDPR Law'),
    html.Div(children='D = Dashboard D: Statistics on Worldwide Data Breaches'),
    html.Div(children='E = Dashboard E: Surveys on Customers\' Preferences on Data Privacy'),
    html.Div(children=' '),
    html.Div(children='''
        Dashboard to Graph:
    '''),
    dcc.Input(id='input', value='', type='text'),
    html.Div(id='output-graph'),

])

#user input component
@app.callback(
    Output(component_id='output-graph', component_property='children'),
    [Input(component_id='input', component_property='value')]
)

#function to display graphs based on user input
def update_value(input_data):

    #-----------------------Dashboard A--------------------
    if(input_data.isdigit()):
        if(int(input_data) >= 0 and int(input_data) <= 99):

            # read cleaned csv data set
            df = pd.read_csv('DS_1_Cleaned.csv')
            df1 = pd.DataFrame(df)
            new = pd.DataFrame()
            new['Article'] = df1['GDPR & U.S Law Data']
            new['GDPR Law'] = df1['GDPR & U.S Law Data.1']
            new['US Law'] = df1['GDPR & U.S Law Data.2']

            temp = []
            vals = new.iloc[int(input_data)]
            t1 = pd.DataFrame(vals)
            for i in vals:
                temp.append(i)

            t1 = t1.transpose()
            # dash table of db1
            return dt.DataTable(
                    id='table',
                    columns=[{"name": i, "id": i} for i in new.columns],
                    data = t1.to_dict("rows"),
                    style_data={'whiteSpace': 'normal'},
                    css=[{
                        'selector': '.dash-cell div.dash-cell-value',
                        'rule': 'display: inline; white-space: inherit; overflow: inherit; text-overflow: inherit;'
                        }],
                    style_table={'overflowX': 'scroll'},

                    style_cell={
                        'textAlign': 'center',
                        'minWidth': '50px', 'maxWidth': '180px'
                        },
                    style_cell_conditional=[
                        {
                            'if': {'column_id': 'Region'},
                            'textAlign': 'center',
                            'if': {'row_index': 'odd'},
                            'backgroundColor': 'rgb(248, 248, 248)'
                            }
                        ],
                    style_header={
                        'backgroundColor': 'white',
                        'fontWeight':'bold'
                        }
                    )

    #-----------------------Dashboard B--------------------
    elif(input_data == 'B' or input_data == 'b'):

        # Web Scraping by implementing ParseHub API
        # http://www.alstongdprtracker.com/
        # Cleaning Raw Data Below
        """
        df = pd.read_csv('DataSet4_cleaned.csv')
        gdpr = pd.DataFrame()
        l1= []
        df = df.replace('Yes', 1)
        df = df.replace('No', 0)
        df = df.fillna(0)
        """
        # read csv DataSet and parse values
        df = pd.read_csv('DataSet4_cleaned.csv')
        gdpr = pd.DataFrame()
        df = df.replace('Yes', 1)
        df = df.replace('No', 0)
        df = df.fillna(0)
        gdpr = df.pivot(index='country', columns='article', values='deviation')
        #one_gdpr = np.arange(8)
        #one_gdpr = np.reshape(2,4)

        gdpr = gdpr.fillna(0)

        # get sum of supplemental laws for each country
        y_list = []
        for j in gdpr.values:
            sum = 0
            for i in j:
                sum += i
            y_list.append(sum)

        # get list of country whose article deviation value equals 1
        ls1 = []
        for i in gdpr.index:
            l1 = []
            for j in gdpr.columns:
                if(gdpr.loc[i][j] == 1.0):
                    l1.append(j)

            ls1.append(l1)

        # first trace value of bar graph
        trace1 = go.Bar(x=gdpr.index, y=y_list)

        # dictionary of gdpr article deviation per country
        valid = {}
        i=0
        for j in gdpr.index:
            valid[j] = ls1[i]
            i = i+1

        # fill empty values
        f1 = pd.DataFrame(dict([ (k,pd.Series(v)) for k,v in valid.items() ]))

        # matplotlib
        '''obj = gdpr.index
        y_pos = np.arange(len(obj))
        plt.bar(y_pos, ls1)
        plt.xticks(y_pos, obj)'''

        #return graphs - bar chart and data table
        return (dcc.Graph(
            id='example_graph',
            figure={
                'data':[trace1],
                'layout':
                go.Layout(title='Number of Supplemental Privacy Laws Enacted by European Union Country',
                          yaxis={'title':'Supplemental Privacy Laws'},
                          xaxis={'title':'Country'},
                                 barmode='stack', hovermode='closest')
                }),
                html.H4(children='Supplemental Laws to GDPR Articles per Country'),
                dt.DataTable(
                    id='table',
                    columns=[{"name": i, "id": i} for i in f1.columns],
                    data=f1.to_dict("rows"),
                    style_table={'overflowX': 'scroll'},
                    style_cell={
                        'textAlign': 'center',
                        },
                    style_cell_conditional=[
                        {
                            'if': {'column_id': 'Region'},
                            'textAlign': 'center',
                            'if': {'row_index': 'odd'},
                            'backgroundColor': 'rgb(248, 248, 248)'
                            }
                        ],
                    style_header={
                        'backgroundColor': 'white',
                        'fontWeight':'bold'
                        }
                    )

                )

    #-----------------------Dashboard C--------------------
    elif(input_data == 'C' or input_data == 'c'):

        # Web Scraping by implementing ParseHub API
        # https://ico.org.uk/action-weve-taken/enforcement/
        # Cleaning Raw Data Below
        """
        pd = pd.read_csv('C:\\Users\Darshan\Desktop\Dataset5_raw - Copy.csv')
        l = []
        l1 = []
        l = pd['Company_Fines']
        l1 = pd['Company_Dates']

        a = []
        for i in l1:
            a.append(i.split(',')[0])

        pd['Refined_Dates'] = a

        def isNumber(inputstr):
            if inputstr.isdigit():
                return True
            else:
                return False
        b = []
        for i in l:
            i=i.replace(' ','')
            s = "";
            if('£' in i):
                s = s+'£'
                j = 1
                while((i[i.find('£')+j])==',' or isNumber(i[i.find('£')+j])) :
                    s = s+i[i.find('£')+j]
                    j = j+1
                b.append(s)
            else:
                b.append(' ')

        pd['Fine_In_Euros'] = b
        New = pd.copy(deep = True)
        New = New.drop('Company_Fines',1)
        New = New.drop('Company_Dates',1)
        New.to_csv('DataSet5_cleaned.csv')
        New.to_json('test.json')
        """
        df = pd.read_csv('DataSet5_cleaned.csv')
        df.columns = ['Sr.no','Company_Name','Refined_Dates','Fine_In_Euros']

        company_name_list = df['Company_Name']
        date_list = df['Refined_Dates']
        fine_list = df['Fine_In_Euros']

        fine_in_numbers = []
        ind = 0
        index = []
        # get list of fines
        for i in fine_list:
            if(i!=' '):
                t = i[1:]
                x = t.find(',')
                t = t[0:x] + t[x+1:]
                fine_in_numbers.append(float(t))
                index.append(ind)
            ind+=1

        company_fined = []

        # specific companies fined
        for i in index:
            company_fined.append(company_name_list[i])

        # dict of companies and amount fined
        d = dict(zip(company_fined,fine_in_numbers))
        company_fined = []
        for i in d.keys():
            company_fined.append(i)
        fine_in_numbers = []
        for i in d.values():
            fine_in_numbers.append(i)

        # line graph
        return dcc.Graph(id='example',
                         figure ={
                             'data': [
                                 {'x':company_fined, 'y':fine_in_numbers, 'type':'line', 'name':'Fines Levied','xlabel':'Companies'},
                                 ],
                             'layout':{
                                 'title':'Companies Fined by GDPR Law',
                                 'xaxis':{
                                     'title':'Company',
                                     'showgrid':False,
                                     'zeroline':False,
                                     'tickmode':'array',
                                     'tickvals':[]
                                     },
                                 'yaxis':{
                                     'title':'Fine Levied'
                                     }
                                 }
                             })

    #-----------------------Dashboard D--------------------

    elif(input_data == 'D' or input_data == 'd'):

        """from bs4 import BeautifulSoup
        import requests
        website_data = requests.get('https://en.wikipedia.org/wiki/List_of_data_breaches').text
        soup = BeautifulSoup(website_data,'html.parser')
        table = soup.find('table',{'class':'wikitable sortable'})

        headers = [th.text for th in table.select("tr th")]

        with open("out_cleaned.csv", "w") as f:
            wr = csv.writer(f)
            wr.writerow(headers[:-1])
            wr.writerows([[td.text for td in row.find_all("td")][:-1] for row in table.select("tr + tr")])
            """

        #reading a csv file using pandas
        data=pd.read_csv('datasource6_cleaned.csv',index_col=False)
        for x in range(0,len(data['Method'])):
            #removing redundant values and characters
            data['Method'][x]=str(data['Method'][x]).replace('unknown\n','unknown')
            data['Method'][x]=str(data['Method'][x]).replace('nan','unknown')
            data['Method'][x]=str(data['Method'][x]).replace('poor security\n','poor security')
            data['Method'][x]=str(data['Method'][x]).replace('hacked\n','hacked')
            data['Method'][x]=str(data['Method'][x]).replace('social engineering\n','social engineering')
            data['Method'][x]=str(data['Method'][x]).replace('lost / stolen media\n','lost / stolen media')
        #extracting distinct Method values
        setMethod=set(data['Method'])
        setMethod=list(setMethod)
        listZero=[0,0,0,0,0,0,0,0,0,0]
        dictMethod=dict(zip(setMethod,listZero))
        for x in range(0,len(data['Method'])):
            #calculating frequency of each method
            for y in setMethod:
                if data['Method'][x]==y:
                    dictMethod[y]+=1

        key_list = []
        for i in dictMethod.keys():
            key_list.append(i)
        val_list = []
        for j in dictMethod.values():
            val_list.append(j)

        setOT=set(data['Organization type'])

        for x in range(0,len(data['Organization type'])):
            #removing redundant characters and values
            data['Organization type'][x]=str(data['Organization type'][x]).replace('web, tech','tech, web')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('consulting, accounting\n','consulting, accounting')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('social network\n','social network')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('web\n','web')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('ticket distribution\n','ticket distribution')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('government, database\n','government, database')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('transport\n','transport')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('genealogy\n','genealogy')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('healthcare\n','healthcare')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('financial\n','financial')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('credit reporting\n','credit reporting')
            data['Organization type'][x]=str(data['Organization type'][x]).replace('retail\n','retail')
        setOT=set(data['Organization type'])
        setOT=list(setOT)
        listZero=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
        dictOT=dict(zip(setOT,listZero))
        for x in range(0,len(data['Organization type'])):
            #calculating frequency of each Organiztion Type attacked
            for y in setOT:
                if data['Organization type'][x]==y:
                    dictOT[y]+=1

        k2 = []
        v2 = []
        for i in dictOT.keys():
            k2.append(i)
        for j in dictOT.values():
            v2.append(j)

        # data frame
        df = pd.read_csv('datasource6_cleaned.csv')
        n=[]
        j=0
        x = []
        #cleaning Records to only obtain integer values
        for i in df['Records']:
            j+=1
            if re.match('^[1-9][0-9]*',i):
                n.append(i)
                x.append(j)
        t=[]
        j=0
        for i in n:
            j+=1
            if re.search(r'^[^a-zA-Z]*$',i):
                t.append(i)
            else:
                x.remove(j)

        s = []
        for i in t:
            if '\n' in i:
                i = i[:i.find('\n')]
            s.append(i)
        q = []
        m=[]
        for i in s:
            q = i.split(',')
            string =""
            for i in q:
                string += i
            m.append(float(string))
        df.columns = ['Entity', 'Year', 'Records', 'Organization type', 'Method']
        year = []
        for i in df['Year']:
            if '\n' in i:
                i = i[:i.find('\n')]
            year.append(i)
        new_year =[]
        count =0
        for i in range(len(year)):
            if i not in x:
                year = year[:i]+year[i+1:]
        year = year[1:]
        d ={}

        for i in range(len(year)):
            if i not in d.keys():
                d[year[i]]=m[i]
            else:
                d[year[i]] += m[i]
        new_year = []
        new_values = []
        for i in d.keys():
            new_year.append(i)
        for i in d.values():
            new_values.append(i)

        a=np.array(df['Year'])
        b=np.array(df['Entity'])
        c=np.array([])
        for x in a:
            x.replace('\n','')

            c=np.append(c,[x])
        attack=pd.crosstab(b,c)
        attack[attack==1]='Compromised'
        e = []
        kopal = pd.DataFrame(attack)
        for i in kopal.index:
            e.append(i)
        kopal['Company_Names'] = e
        kopal = kopal.iloc[:,[16,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]]

        # bar graphs and data table
        return(html.Div([
    html.Div([
        html.Div([
            dcc.Graph(id='g1', figure={
                'data': [
                    {'x': key_list, 'y': val_list, 'type': 'bar'}],

                 'layout':
                 go.Layout(title='Number of Each Attack Vector Used',
                           yaxis={'title':'# of Attacks'},
                           barmode='stack', hovermode='closest')
                })
            ],
                 className="six columns"),

        html.Div([
            dcc.Graph(id='g2', figure={
                'data': [
                    {'x': k2, 'y': v2, 'type': 'bar'}],
                'layout':
                 go.Layout(title='Number of Attacks per Organization',
                           yaxis={'title':'# of Attacks'},
                           barmode='stack', hovermode='closest')
                })
        ], className="six columns"),
    ], className="row"),
    ]),
               dcc.Graph(
        id='example-graph',
        figure={
            'data': [
                {'x': new_year, 'y': new_values, 'type': 'bar'},
            ],
            'layout': {
                'title': 'Number of Records Compromised per Year',
                'xaxis':{
                    'title': 'Year'
                    },
                'yaxis':{
                    'title': '# of Records'
                    }
            }
        }
    ),
    html.H4(children='Years in which Companies Data were Compromised'),
                dt.DataTable(
                    id='table',
                    columns=[{"name": i, "id": i} for i in kopal.columns],
                    data=attack.to_dict("rows"),
                    style_table={'overflowX': 'scroll'},
                    style_cell={
                        'textAlign': 'center',
                        },
                    style_cell_conditional=[
                        {
                            'if': {'column_id': 'Region'},
                            'textAlign': 'center',
                            'if': {'row_index': 'odd'},
                            'backgroundColor': 'rgb(248, 248, 248)'
                            }
                        ],
                    style_header={
                        'backgroundColor': 'white',
                        'fontWeight':'bold'
                        }
                    )

)

    #-----------------------Dashboard E--------------------
    elif(input_data == 'E' or input_data == 'e'):

        pd1 = pd.read_csv('data_source3_raw_68.csv') #import raw data file, downloaded from Google Forms
        # Google Forms outoupts its information in a csv file, not requiring any scraping or parsing
        # This file contains responses from 68 individuals surveyed about their privacy preferences.
        # The form contained 5 questions, and respondents could select more than one answer to each.

        ShinyTbl = pd1.copy(deep=True)
        ShinyTbl = ShinyTbl.drop('Timestamp',1) #don't need the dates or times of the responses

        #ShinyTbl.describe()

        ShinyTbl.columns = ['Advertisements','Not_In_Favor','Feelings_PI','Companies_Know_You','GDPR_provisions'] #rename column headers

        # Note that respondents can select one or MORE of these options (so cells can contain several answers).
        # While most answers are distinct, searches for "no" responses would inaccurately also count "not sure" responses.
        # Therefore, need to adjust responses for the "Advertisements" and "Companies_Know_You" columns.

        for b in range(0,len(ShinyTbl['Advertisements'])):
            ShinyTbl['Advertisements'][b]=str(ShinyTbl['Advertisements'][b]).replace('Not sure','Unsure') #replace "not sure" with "unsure"

        for f in range(0,len(ShinyTbl['Companies_Know_You'])):
            ShinyTbl['Companies_Know_You'][f]=str(ShinyTbl['Companies_Know_You'][f]).replace('Not sure','Unsure')

        ShinyTbl.to_csv('DataSource_3_cleaned.csv') #outputing cleaned file

        # Counting Specific Responses to 1st question ("Advertisements")

        L1 = ShinyTbl['Advertisements']

        opt1_1 = 'protect'; opt2_1 = 'careful'; opt3_1 = 'No'; opt4_1 = 'Unsure'
        ctopt1_1 = 0; ctopt2_1 = 0; ctopt3_1 = 0; ctopt4_1 = 0

        for a in L1:
                a = (str)(a)
                if opt1_1 in a:
                    ctopt1_1 += 1
                if opt2_1 in a:
                    ctopt2_1 += 1
                if opt3_1 in a:
                    ctopt3_1 += 1
                if opt4_1 in a:
                    ctopt4_1 += 1
        totalAns1 = ctopt1_1 + ctopt2_1 + ctopt3_1 + ctopt4_1

        dict1 = {'Yes, even if they\'re not being careful with my data; I don\'t care': ctopt2_1,
                'Yes, as long as they protect and use my data responsibly' : ctopt1_1,
                'No' : ctopt3_1,
                'Not Sure' : ctopt4_1,
                'Total Responses' : totalAns1}

        # Counting Specific Responses to 2nd question ("Not In Favor")

        L2 = ShinyTbl['Not_In_Favor']

        opt1_2 = 'misusing'; opt2_2 = 'not protecting'; opt3_2 = 'watching'
        ctopt1_2 = 0; ctopt2_2 = 0; ctopt3_2 = 0

        for c in L2:
                c = (str)(c)
                if opt1_2 in c:
                    ctopt1_2 += 1
                if opt2_2 in c:
                    ctopt2_2 += 1
                if opt3_2 in c:
                    ctopt3_2 += 1

        totalAns2 = ctopt1_2 + ctopt2_2 + ctopt3_2

        dict2 = {'I am concerned about companies misusing my data' : ctopt1_2,
                'I am concerned about companies not protecting my data from hackers' : ctopt2_2,
                'I just don\'t like companies watching what I do online' : ctopt3_2,
                'Total Responses' : totalAns2}

        # Counting Specific Responses to 3rd question ("Feelings_PI")

        L3 = ShinyTbl['Feelings_PI']

        opt1_3 = 'mostly good'; opt2_3 = 'bad'; opt3_3 = 'care about'; opt4_3 = 'Not sure'
        ctopt1_3 = 0; ctopt2_3 = 0; ctopt3_3 = 0; ctopt4_3 = 0

        for d in L3:
                d = (str)(d)
                if opt1_3 in d:
                    ctopt1_3 += 1
                if opt2_3 in d:
                    ctopt2_3 += 1
                if opt3_3 in d:
                    ctopt3_3 += 1
                if opt4_3 in d:
                    ctopt4_3 += 1

        totalAns3 = ctopt1_3 + ctopt2_3 + ctopt3_3 + ctopt4_3

        dict3 = {'I think that they are mostly good at using our data in responsible and secure ways' : ctopt1_3,
                'I think that they mostly want to use our data in a responsible and secure way . . . but they are bad at it' : ctopt2_3,
                'I don\'t think that they care about using our data responsibly or securely at all' : ctopt3_3,
                'Not sure' : ctopt4_3,
                'Total Responses' : totalAns3}

        # Counting Specific Responses to 4th question ("Companies_Know_You")

        L4 = ShinyTbl['Companies_Know_You']

        opt1_4 = 'Yes'; opt2_4 = 'No'; opt3_4 = 'Unsure'
        ctopt1_4 = 0; ctopt2_4 = 0; ctopt3_4 = 0

        for e in L4:
                e = (str)(e)
                if opt1_4 in e:
                    ctopt1_4 += 1
                if opt2_4 in e:
                    ctopt2_4 += 1
                if opt3_4 in e:
                    ctopt3_4 += 1

        totalAns4 = ctopt1_4 + ctopt2_4 + ctopt3_4

        dict4 = {'Yes' : ctopt1_4,
                'No' : ctopt2_4,
                'Not sure' : ctopt3_4,
                'Total Responses' : totalAns4}

        # Counting Specific Responses to 5th question ("Feelings_PI")

        L5 = ShinyTbl['GDPR_provisions']

        opt1_5 = 'delete'; opt2_5 = 'being done'; opt3_5 = 'control'; opt4_5 = 'complain'
        ctopt1_5 = 0; ctopt2_5 = 0; ctopt3_5 = 0; ctopt4_5 = 0

        for g in L5:
                g = (str)(g)
                if opt1_5 in g:
                    ctopt1_5 += 1
                if opt2_5 in g:
                    ctopt2_5 += 1
                if opt3_5 in g:
                    ctopt3_5 += 1
                if opt4_5 in g:
                    ctopt4_5 += 1

        totalAns5 = ctopt1_5 + ctopt2_5 + ctopt3_5 + ctopt4_5

        dict5 = {'The right to ask companies to delete the data they have on me' : ctopt1_5,
                'The right to ask what is being done with my data' : ctopt2_5,
                'The ability to control how my data is being used' : ctopt3_5,
                'The ability to complain when my data is misused, which could result in steep fines for the offending company' : ctopt4_5,
                'Total Responses' : totalAns5}

        # question 1
        d1key = []
        d1Pct = []

        for i in dict1.keys():
            d1key.append(i)
        del d1key[-1]

        for i in dict1.values():
            d1Pct.append(i)

        total = d1Pct[-1]
        for i in range(4):
            d1Pct[i] = (d1Pct[i]/total) *100
            d1Pct[i] = round(d1Pct[i],2)

        del d1Pct[-1]

        #question 2
        d2key = []
        d2Pct = []
        for i in dict2.keys():
            d2key.append(i)
        del d2key[-1]

        for i in dict2.values():
            d2Pct.append(i)

        total = d2Pct[-1]

        for i in range(4):
            d2Pct[i] = (d2Pct[i]/total) *100
            d2Pct[i] = round(d2Pct[i],2)

        del d2Pct[-1]

        #question 3
        d3key = []
        d3Pct = []
        for i in dict3.keys():
            d3key.append(i)
        del d3key[-1]

        for i in dict3.values():
            d3Pct.append(i)

        total = d3Pct[-1]

        for i in range(4):
            d3Pct[i] = (d3Pct[i]/total) *100
            d3Pct[i] = round(d3Pct[i],2)

        del d3Pct[-1]

        #question 4
        d4key = []
        d4Pct = []

        for i in dict4.keys():
            d4key.append(i)
        del d4key[-1]

        for i in dict4.values():
            d4Pct.append(i)

        total = d4Pct[-1]
        for i in range(3):
            d4Pct[i] = (d4Pct[i]/total) *100
            d4Pct[i] = round(d4Pct[i],2)

        del d4Pct[-1]

        #question 5
        d5key = []
        d5Pct = []

        for i in dict5.keys():
            d5key.append(i)
        del d5key[-1]

        for i in dict5.values():
            d5Pct.append(i)

        total = d5Pct[-1]

        for i in range(4):
            d5Pct[i] = (d5Pct[i]/total) *100
            d5Pct[i] = round(d5Pct[i],2)

        del d5Pct[-1]

        q1 = 'Are you in favor of websites or apps using what they learn about you to show you advertisements that you might find interesting?'
        q1 = textwrap.wrap(q1)
        q2 = 'If you are not in favor of receiving targeted advertisements, why not?'
        q2 = textwrap.wrap(q2)
        q3 = 'In general, what are your feelings toward companies that host websites today and have access to our personal information?'
        q3 = textwrap.wrap(q3)
        q4 = 'Do you think companies hosting websites know too much about you?'
        q4 = textwrap.wrap(q4)
        q5 = 'Which of these types of privacy provisions--which already have been enacted in Europe--would you most like see applied in the US?'
        q5 = textwrap.wrap(q5)

        return (
            dcc.Graph(id='pi',
                         figure={
                             'data': [
                                 {
                                     'labels': d1key,
                                     'values': d1Pct,
                                     'type': 'pie',
                                     'title': '<br>'.join(q1),
                                     'marker': {'colors': ['rgb(114,119,121)',
                                                           'rgb(18, 36, 37)',
                                                           'rgb(34, 53, 101)',
                                                           'rgb(51,155,255)',
                                                           'rgb(6, 4, 4)']},
                                     'domain': {'x': [0, .48],
                                                'y': [0, .49]},
                                     'hoverinfo':'label+percent+name',
                                     'textinfo':'none'
                                     },
                                 {
                                     'labels': d2key,
                                     'values': d2Pct,
                                     'title': '<br>'.join(q2),
                                     'marker': {'colors': ['rgb(114,119,121)',
                                                           'rgb(18, 36, 37)',
                                                           'rgb(34, 53, 101)',
                                                           'rgb(51,155,255)',
                                                           'rgb(6, 4, 4)']},
                                     'type': 'pie',

                                     'domain': {'x': [.52, 1],
                                                'y': [0, .49]},
                                     'hoverinfo':'label+percent+name',
                                     'textinfo':'none'
                                     },
                                 {
                                     'labels': d3key,
                                     'values': d3Pct,
                                     'title':'<br>'.join(q3),
                                     'marker': {'colors': ['rgb(114,119,121)',
                                                           'rgb(18, 36, 37)',
                                                           'rgb(34, 53, 101)',
                                                           'rgb(51,155,255)',
                                                           'rgb(6, 4, 4)']},
                                     'type': 'pie',

                                     'domain': {'x': [0, .33],
                                                'y': [.5, 1]},
                                     'hoverinfo':'label+percent+name',
                                     'textinfo':'none'
                                     },
                                 {
                                     'labels': d4key,
                                     'values': d4Pct,
                                     'title': '<br>'.join(q4),
                                     'marker': {'colors': ['rgb(114,119,121)',
                                                           'rgb(18, 36, 37)',
                                                           'rgb(34, 53, 101)',
                                                           'rgb(51,155,255)',
                                                           'rgb(6, 4, 4)']},
                                     'type': 'pie',

                                     'domain': {'x': [.35, .67],
                                                'y': [.51, 1]},
                                     'hoverinfo':'label+percent+name',
                                     'textinfo':'none'
                                     },
                                 {
                                     'labels': d5key,
                                     'values': d5Pct,
                                     'title': '<br>'.join(q5),
                                     'marker': {'colors': ['rgb(114,119,121)',
                                                           'rgb(18, 36, 37)',
                                                           'rgb(34, 53, 101)',
                                                           'rgb(51,155,255)',
                                                           'rgb(6, 4, 4)']},
                                     'type': 'pie',

                                     'domain': {'x': [.70, 1],
                                                'y': [.51, 1]},
                                     'hoverinfo':'label+percent+name',
                                     'textinfo':'none'
                                     }
                                 ],
                             'layout': {'title': 'Customers\' Preferences on Data Privacy',
                                        'height':700,
                                        'showlegend': False}
                             }
                         )
            )

    #------------------Invalid User input------------------
    else:
        return html.Div('Please enter: 1-99 or B,C,D,E')

#run server on main module
if __name__ == '__main__':
    app.run_server(debug=False)


"""
Reverse a linked list from position m to n. Do it in-place and in one-pass.

For example:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
"""

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    def reverseBetween(self, head, m, n):
        """
        :type head: ListNode
        :type m: int
        :type n: int
        :rtype: ListNode
        """
        new_head = ListNode(0)
        new_head.next = head
        node = new_head

        for i in range(m-1):
            node = node.next

        prev = node.next
        curr = prev.next

        for i in range(n-m):
            temp = curr.next
            curr.next = prev
            prev = curr
            curr = temp
        node.next.next = curr
        node.next = prev
        return new_head.next


'''
@author: Jake Ross
@copyright: 2009
@license: Educational Community License 1.0
'''
#=============enthought library imports=======================
#=============standard library imports ========================
import time
#=============local library imports  ==========================
from src.scripts.file_script import FileScript
class CalibrationScript(FileScript):
    '''
        G{classtree}
    '''
    def load_file(self):
        '''
        '''
        self.step_interval = int(self._file_contents_[0])
        sp, ep, stp = [int(i) for i in self._file_contents_[1].split(',')]
        self.power_steps = xrange(sp, ep + 1, stp)

    def kill_script(self):
        '''
        '''
        super(CalibrationScript, self).kill_script()

        m = self.manager
        m.disable_laser()
        m.stream_manager.stop_streams()

    def _run_(self):
        '''
        '''

        sm = self.manager.stream_manager

        for name in sm.streamids:
            self.data_manager.add_group(name)
            table = 'stream'
            self.data_manager.add_table(table, parent = 'root.%s' % name)
            sm.set_stream_tableid(name, 'root.%s.%s' % (name, table))

        self.progress.add_text('Starting streams')
        sm.start_streams()

        time.sleep(2)

        self.progress.add_text('Firing laser')
        self.manager.enable_laser()

        for pi in self.power_steps:
            self.progress.add_text('Set laser power %0.2f' % pi)
            self.manager.set_laser_power(pi, 'open')
            time.sleep(self.step_interval)

        self.manager.disable_laser()

        cd = 10
        self.progress.add_text('Cooling down for %i (secs)' % cd)
        time.sleep(cd)

        sm.stop_streams()
        #sm.edit_traits()


# Merge Sort is a divide-and-conquer algorithm. It divides the input list of length n in half successively until there are n lists of size 1. Then, pairs of lists are merged together with the smaller first element among the pair of lists being added in each step. Through successive merging and through comparison of first elements, the sorted list is built.

#  [10,20] [15,25]
def merge_lists(list1, list2):
    print("merge_lists:b")
    print(list1)
    print(list2)
    print("merge_lists:e")
    rv = []
    i, j = 0, 0
    while i < len(list1) and j < len(list2):
        if list1[i] < list2[j]:
            rv.append(list1[i])
            i += 1
        else:
            rv.append(list2[j])
            j += 1
    rv += list1[i:] + list2[j:]

    return rv

def merge_sort(numbers):
    length = len(numbers)
    if length == 1:
        return numbers

    print(numbers)

    mid = length // 2

    return merge_lists(merge_sort(numbers[:mid]), merge_sort(numbers[mid:]))

if __name__ == "__main__":
    print(merge_sort([6, 5, 3, 1, 8, 7, 2, 4]))


from flask import Flask
from flask_restful import Api, Resource, reqparse, abort, fields, marshal_with
from flask_sqlalchemy import SQLAlchemy

app=Flask(__name__)
api=Api(app)
app.config['SQLALCHEMY_DATABASE_URI']='sqlite:///tmp/database.db'
db=SQLAlchemy(app)

class VideoModel(db.Model):
	id=db.Column(db.Integer,primary_key=True)
	name=db.Column(db.String(100),nullable=False)
	views=db.Column(db.Integer,nullable=False)
	likes=db.Column(db.Integer,nullable=False)

	def __repr__(self):
		return f'Video (name= {name}, views={views}, likes={likes})'

#db.create_all() #ONly run first time

video_put_args=reqparse.RequestParser()
video_put_args.add_argument('name',type=str,help='Name of the video',required=True)
video_put_args.add_argument('views',type=int,help='views of the video',required=True)
video_put_args.add_argument('likes',type=int,help='likes of the video',required=True )

video_up_args=reqparse.RequestParser()
video_up_args.add_argument('name',type=str,help='Name of the video')
video_up_args.add_argument('views',type=int,help='views of the video')
video_up_args.add_argument('likes',type=int,help='likes of the video')

resource_fields={
	'id':fields.Integer,
	'name':fields.String,
	'likes':fields.Integer,
	'views':fields.Integer
}

class Video(Resource):
	@marshal_with(resource_fields)
	def get(self,vid_id):
		result=VideoModel.query.filter_by(id=vid_id).first()
		if not result:
			abort(404,message='Vid id not exists...')
		return result, 200

	@marshal_with(resource_fields)
	def post(self,vid_id):
		result=VideoModel.query.filter_by(id=vid_id).first()
		if result:
			abort(409,message=f'Video id {vid_id} taken')
		args=video_put_args.parse_args()
		video=VideoModel(id=vid_id,name=args['name'],likes=args['likes'],views=args['views'])
		db.session.add(video)
		db.session.commit()
		return video, 201

	@marshal_with(resource_fields)
	def patch(self,vid_id):
		result=VideoModel.query.filter_by(id=vid_id).first()
		if not result:
			abort(404,'video does not exists...')
		args=video_up_args.parse_args()
		if args['name']:
			result.name=args['name']
		if args['likes']:
			result.likes=args['likes']
		if args['views']:
			result.views=args['views']

		db.session.commit()
		return result, 200

	def delete(self,vid_id):
		result=VideoModel.query.filter_by(id=vid_id).first()
		if not result:
			abort(404,'video does not exists...')

		db.session.delete(result)
		db.session.commit()
		return {"message":f"deleted video {vid_id}"}, 201

api.add_resource(Video,'/video/<int:vid_id>')

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


import FWCore.ParameterSet.Config as cms
import sys

#if len(sys.argv) > 2:
#    file=open(sys.argv[2])
#    outfile=sys.argv[3]

process = cms.Process("Forward")

process.load("FWCore.MessageService.MessageLogger_cfi")
process.load("Configuration.StandardSequences.FrontierConditions_GlobalTag_cff")
process.load('Configuration/StandardSequences/Reconstruction_cff')
process.load("RecoLocalCalo.HcalRecProducers.HcalHitReconstructor_hf_cfi")
process.load("RecoLocalCalo.HcalRecProducers.HcalHitReconstructor_zdc_cfi")
process.load("EventFilter.CastorRawToDigi.CastorRawToDigi_cfi")
process.load("RecoLocalCalo.CastorReco.CastorSimpleReconstructor_cfi")
process.load("CondCore.DBCommon.CondDBSetup_cfi")

process.maxEvents = cms.untracked.PSet( input = cms.untracked.int32(-1) )

process.MessageLogger.cerr.FwkReport.reportEvery = 1000

#process.source = cms.Source("PoolSource",
#                           fileNames = cms.untracked.vstring(file.readlines()))

process.source = cms.Source("PoolSource",
#                            fileNames = cms.untracked.vstring('file:/home/jgomez2/EAFE4330-EB64-E211-896F-BCAEC5329719.root')
                            fileNames = cms.untracked.vstring('file:/home/jgomez2/CMSSW_5_3_8_HI_patch2/src/Analyzers/ForwardAnalyzer/2011run182398_10_1_6Lr.root')
                            )

process.TFileService = cms.Service("TFileService",
                                   #  fileName = cms.string(outfile)
                                   fileName = cms.string('bleh.root')
                                   )

#process.GlobalTag.globaltag = 'GR_R_44_V10::All'
process.GlobalTag.globaltag = 'GR_R_53_V19::All'

process.HeavyIonGlobalParameters=cms.PSet(centralityVariable= cms.string("PixelHits"),#HFhits"),#"PixelHits"),
                                          centralitySrc = cms.InputTag("hiCentrality")
                                          )

process.hltbitanalysis = cms.EDAnalyzer("HLTBitAnalyzer",
                                        ### Trigger objects
                                        l1GctHFBitCounts                = cms.InputTag("gctDigis"),
                                        l1GctHFRingSums                 = cms.InputTag("gctDigis"),
                                        l1GtObjectMapRecord             = cms.InputTag("hltL1GtObjectMap::HLT"),
                                        l1GtReadoutRecord               = cms.InputTag("gtDigis::RECO"),

                                        l1extramc                       = cms.string('l1extraParticles'),
                                        l1extramu                       = cms.string('l1extraParticles'),
                                        hltresults                      = cms.InputTag("TriggerResults::HLT"),
                                        HLTProcessName                  = cms.string("HLT"),
                                        UseTFileService                 = cms.untracked.bool(True),

                                        ### Run parameters
                                        RunParameters = cms.PSet(
    HistogramFile = cms.untracked.string('hltbitanalysis.root')
    )
                                        )##end of HLT

process.fwdana = cms.EDAnalyzer('ForwardAnalyzer')

process.upcvertexana = cms.EDAnalyzer('UPCVertexAnalyzer',
                                      vertexCollection=cms.string("offlinePrimaryVerticesWithBS")
                                      )
process.upcgeneraltrackana = cms.EDAnalyzer('UPCTrackAnalyzer',
                                            trackCollection=cms.string("generalTracks")
                                            )

process.upcpixeltrackana = cms.EDAnalyzer('UPCTrackAnalyzer',
                                          trackCollection=cms.string("pixelTracks")
                                          )
process.upccentralityana = cms.EDAnalyzer('UPCCentralityAnalyzer',
                                          centralityVariable=process.HeavyIonGlobalParameters.centralityVariable
                                          )

process.trackSequence = cms.Sequence(process.upcgeneraltrackana+process.upcpixeltrackana+process.upcvertexana)
process.forwardSequence = cms.Sequence(process.fwdana)
process.triggerSequence = cms.Sequence(process.hltbitanalysis)
process.centralitySequence = cms.Sequence(process.upccentralityana)

process.path = cms.Path(process.trackSequence+
                        process.forwardSequence+
                        process.triggerSequence
                        )


import networkx as nx

# ########################################################################
# ########################################################################
#      Functions   START
# ########################################################################
# ########################################################################

def get_LCC_size(G):
    LCC = sorted(nx.connected_components(G), key=len, reverse=True)[0]
    return len(LCC)

def get_number_of_connected_components(G):
    return nx.number_connected_components(G)

def get_pairwise_connectivity(G):
    connected_components = nx.connected_components(G)
    _list = [len(c) for c in connected_components]
    pairwise_connectivity = 0
    for n in _list:
        pairwise_connectivity += n * (n - 1) / 2
    return pairwise_connectivity

def get_centrality(G, centrality):
    if (centrality == 'degree'):
        return nx.degree_centrality(G)
    if (centrality == 'betweenness'):
        return nx.betweenness_centrality(G)
    if (centrality == 'closeness'):
        return nx.closeness_centrality(G)
    if (centrality == 'eigenvector'):
        return nx.eigenvector_centrality(G, max_iter=5000)

def get_alalysis(G, budget, centrality):
    temp_G = G.copy()
    i = 0
    _list = {}
    _list['largest_connected_component_size'] = []
    _list['number_of_connected_components'] = []
    _list['pairwise_connectivity'] = []
    _removing_nodes = []
    _removing_nodes_centrality = []
    print(centrality, ': ')
    centrality_dictionary = get_centrality(temp_G, centrality)
    centrality_sequence = [(k, v) for k, v in centrality_dictionary.items()]
    centrality_sequence = sorted(centrality_sequence, key=lambda item: item[1], reverse=True)
    while i < (budget):
        # print(centrality_sequence)
        _removing_nodes.append(centrality_sequence[i][0])
        if (centrality != 'degree'):
            _removing_nodes_centrality.append(centrality_sequence[i][1])
        else:
            _removing_nodes_centrality.append(G.degree[centrality_sequence[i][0]])

        temp_G.remove_node(centrality_sequence[i][0])
        _list['largest_connected_component_size'].append(get_LCC_size(temp_G))
        _list['number_of_connected_components'].append(get_number_of_connected_components(temp_G))
        _list['pairwise_connectivity'].append(get_pairwise_connectivity(temp_G))
        print("%.0f%%" % (i / budget * 100) , end='\r')
        i = i + 1
        if (i % 50 == 0):
            print(_list['largest_connected_component_size'][i-1])

    i = 0
    removing_nodes_file = open('.\\Saccharomyces cerevisiae\\DIP\\output\\global_centrality_analysis\\removed_nodes_based_on_' + centrality + '_centrality.budget_' + str(budget) + '.txt', 'w')
    # removing_nodes_file = open('.\\Escherichia coli\\DIP\\output\\global_centrality_analysis\\removed_nodes_based_on_' + centrality + '_centrality.budget_' + str(budget) + '.txt', 'w')
    removing_nodes_file.write('nodes\tcentrality_value\tlargest_connected_component_size\tnumber_of_connected_components\tpairwise_connectivity\n')
    while i < (budget):
        removing_nodes_file.write(_removing_nodes[i] + '\t' + str(_removing_nodes_centrality[i]) + '\t' +
                            str(_list['largest_connected_component_size'][i]) + '\t' +
                            str( _list['number_of_connected_components'][i]) + '\t' +
                            str(_list['pairwise_connectivity'][i]) + '\n')
        i += 1
    removing_nodes_file.close()
    return _list

def centrality_analysis(G, budget):
    analysis = {}
    centrality_list = ['degree', 'betweenness', 'closeness', 'eigenvector']
    for centrality in centrality_list:
        analysis[centrality] = get_alalysis(G, budget, centrality)
    return analysis

# ########################################################################
# ########################################################################
#      Functions   END
# ########################################################################
# ########################################################################

original_PPIs_file_name = ".\\Saccharomyces cerevisiae\\DIP\\output\\original_PPIs_redundancies_N_self_loops_removed_N_Connected.txt"
# original_PPIs_file_name = ".\\Escherichia coli\\DIP\\output\\original_PPIs_redundancies_N_self_loops_removed_N_Connected.txt"
G = nx.read_edgelist(original_PPIs_file_name, nodetype=str, data=(('weight', int),)) #reading a graph as edge listed
print(G.number_of_nodes())
print(G.number_of_edges())
_result = centrality_analysis(G, 950)


a = "my"
b = 1
c = 1.5
# print(a, b, c)
#
d = b'0-123'
#
# print(a, b, c, d)
#
# print (type(d), d, type(a), a, type(b), b, type(c), c )

e = ['eli', 'myaso', 'muzhiki']
# print(type(e), e)

f = ("mein", "herz", "brennt")
# print (f, type(f))

g = {"eins", "zwei", "drei", "uno", "dos", "uno", "drei"}
# print (g, type(g))

h = frozenset("elka")

# print(h, type(h))

i = {}

# print(i, type(i))

print(a, type(a), b, type(b),c, type(c),d, type(d), e, type(e),f, type(f), g, type(g), h, type(h),i, type(i))

j = "uno"
k = "dos"
l = k+j

print(l)

m = 123
n = 2
o = m+n
print(o)

p = m%n
print (p)

q = n*m
print(q)

r = m//n
print(r)

s = m/n
print(s)


import logging
import sys
from pathlib import Path
import shutil
from zensols.persist import (
    PersistedWork,
    persisted
)
from zensols.garmdown import (
    Backuper,
    Persister,
    Fetcher,
)

logger = logging.getLogger(__name__)

class Manager(object):
    """Manages downloading and database work.  This includes downloading data from
    the Garmin connect website and persisting status data in an SQLite
    database.

    """
    def __init__(self, config):
        """Initialize

        :param config: the application configuration
        """
        self.config = config
        self.download = config.download
        self._fetcher = PersistedWork('_fetcher', self, True)

    @property
    @persisted('_fetcher')
    def fetcher(self):
        return Fetcher(self.config)

    @property
    @persisted('_persister')
    def persister(self):
        return Persister(self.config)

    @property
    @persisted('_backuper')
    def backuper(self):
        return Backuper(self.config, self.persister)

    def environment(self, writer=sys.stdout):
        writer.write(f'database={self.config.db_file}\n')
        writer.write(f'activities={self.config.activities_dir}\n')
        writer.write(f'backup={self.config.db_backup_dir}\n')

    def sync_activities(self, limit=None, start_index=0):
        """Download and add activities to the SQLite database.  Note that this does not
        download the TCX files.

        :param limit: the number of activities to download
        :param start_index: the 0 based activity index (not contiguous page
            based)

        """
        # acts will be an iterable
        acts = self.fetcher.get_activities(limit, start_index)
        self.persister.insert_activities(acts)

    @staticmethod
    def _tcx_filename(activity):
        """Format a (non-directory) file name for ``activity``."""
        return f'{activity.start_date_str}_{activity.id}.tcx'

    def sync_tcx(self, limit=None):
        """Download TCX files and record each succesful download as such in the
        database.

        :param limit: the maximum number of TCX files to download, which
            defaults to all

        """
        dl_dir = self.config.activities_dir
        persister = self.persister
        if not dl_dir.exists():
            logger.info(f'creating download directory {dl_dir}')
            dl_dir.mkdir(parents=True)
        acts = persister.get_missing_downloaded(limit)
        logger.info(f'downloading {len(acts)} tcx files')
        for act in acts:
            dl_path = Path(dl_dir, self._tcx_filename(act))
            if dl_path.exists():
                logger.warning(f'activity {act.id} is downloaded ' +
                               f'but not marked--marking now')
            else:
                logger.debug(f'downloading {dl_path}')
                with open(dl_path, 'wb') as f:
                    self.fetcher.download_tcx(act, f)
                sr = dl_path.stat()
                logger.debug(f'{dl_path} has size {sr.st_size}')
                if sr.st_size < self.download.min_size:
                    m = f'downloaded file {dl_path} has size ' + \
                        f'{sr.st_size} < {self.download.min_size}'
                    raise ValueError(m)
            persister.mark_downloaded(act)

    def import_tcx(self, limit=None):
        """Download TCX files and record each succesful download as such in the
        database.

        :param limit: the maximum number of TCX files to download, which
            defaults to all

        """
        persister = self.persister
        dl_dir = self.config.activities_dir
        import_dir = self.config.import_dir
        if not import_dir.exists():
            logger.info(f'creating imported directory {import_dir}')
            import_dir.mkdir(parents=True)
        acts = persister.get_missing_imported(limit)
        logger.info(f'importing {len(acts)} activities')
        for act in acts:
            fname = self._tcx_filename(act)
            dl_path = Path(dl_dir, fname)
            import_path = Path(import_dir, fname)
            if import_path.exists():
                logger.warning(f'activity {act.id} is imported ' +
                               'but not marked--marking now')
            else:
                logger.info(f'copying {dl_path} -> {import_path}')
                shutil.copy(dl_path, import_path)
            persister.mark_imported(act)

    def sync(self, limit=None):
        """Sync activitives and TCX files.

        :param limit: the number of activities to download and import, which
            defaults to the configuration values

        """
        self.sync_activities(limit)
        self.sync_tcx(limit)
        self.import_tcx()

    def clean_imported(self, limit=None):
        """Delete all TCX files from the import directory.  This is useful so that
        programs like GoldenCheetah that imports them don't have to re-import
        them each time.

        """
        import_dir = self.config.import_dir
        logger.info(f'removing import files from {import_dir}')
        if import_dir.exists():
            for path in import_dir.iterdir():
                logger.info(f'removing {path}')
                path.unlink()

    def write_not_downloaded(self, detail=False, limit=None,
                             writer=sys.stdout):
        """Write human readable formatted data of all activities not yet downloaded.

        :param detail: whether or to give full information about the activity
        :param limit: the number of activities to report on
        :param writer: the stream to output, which defaults to stdout

        """
        for act in self.persister.get_missing_downloaded(limit):
            act.write(writer, detail=detail)

    def write_not_imported(self, detail=False, limit=None, writer=sys.stdout):
        """Write human readable formatted data of all activities not yet imported.

        :param detail: whether or to give full information about the activity
        :param limit: the number of activities to report on
        :param writer: the stream to output, which defaults to stdout

        """
        for act in self.persister.get_missing_imported(limit):
            act.write(writer, detail=detail)

    def close(self):
        """Close all allocated resources byt by the manager."""
        self.fetcher.close()
        self._fetcher.clear()


from core import SpeakersCSP
from inference import inference, InferenceType

def backtracking(assignments, csp):
    if csp.is_complete(assignments):
        return assignments

    var = csp.select_unassigned_variable(assignments)

    for value in csp.order_domain_values(var):
        inferences = {}

        if csp.is_consistent(var, value):
            assignments[var] = value

            inferences = inference(var, value, assignments, csp, InferenceType.AC3)
            for key, val in inferences.items():
                assignments[key] = val

            res = backtracking(assignments, csp)
            if res:
                return res

        assignments.pop(var, None)
        for key, _ in inferences.items():
            assignments.pop(key, None)

    return False

def main():
    csp = SpeakersCSP()
    csp.make_node_consistent()

    print(backtracking({}, csp))

    csp.display_csp()

if __name__ == '__main__':
    main()


import telegram
import json
import requests
from flask import Flask, request
from pymongo import MongoClient

BOT_TOKEN = "235229329:AAE-lvn60UEhF_Q0ePD6UtJy8IaRSPl20ZU"

app = Flask(__name__)
app.debug = True
bot = telegram.Bot(token=BOT_TOKEN)
db = MongoClient("localhost", 27017).chats

@app.route("/hook", methods=["POST"])
def message_hook():
    message = telegram.Message.de_json(json.loads(request.get_data().decode())["message"])

    if message.text.strip().lower() in ["hello", "hi"]:
        bot.sendMessage(chat_id=message.chat_id, text="Ask me something?", reply_markup=telegram.ReplyKeyboardMarkup([[
                telegram.KeyboardButton(text="Where i can eat?", request_location=True)
            ]]))
    elif message.text == "Where i can eat?":
        bot.sendMessage(chat_id=message.chat_id, text="Here!")
    return "OK"

@app.route("/")
def index():
    return "OK"


import numpy as np

from ge.classify import read_node_label, Classifier
from ge import LINE
from sklearn.linear_model import LogisticRegression

import matplotlib.pyplot as plt
import networkx as nx
from sklearn.manifold import TSNE
import sys
sys.path.append("./")
from layout.emb_fr import embedding_fr, COLOR_MAP, kmeans
import datetime

def evaluate_embeddings(embeddings):
    X, Y = read_node_label('./data/miserables/miserables_labels.txt')
    tr_frac = 0.8
    print("Training classifier using {:.2f}% nodes...".format(
        tr_frac * 100))
    clf = Classifier(embeddings=embeddings, clf=LogisticRegression())
    clf.split_train_evaluate(X, Y, tr_frac)

def plot_embeddings(embeddings,):
    X, Y = read_node_label('./data/miserables/miserables_labels.txt')

    emb_list = []
    for k in X:
        emb_list.append(embeddings[k])
    emb_list = np.array(emb_list)

    model = TSNE(n_components=2)
    node_pos = model.fit_transform(emb_list)

    color_idx = {}
    color_dict = {}
    for i in range(len(X)):
        color_idx.setdefault(Y[i][0], [])
        color_idx[Y[i][0]].append(i)
        color_dict[X[i]] = COLOR_MAP[int(Y[i][0])]

    plt.figure(figsize=(10, 10))
    ax = plt.axes([0.05, 0.05, 1 - 0.05 * 2, 1 - 0.05 * 2])
    ax.spines["top"].set_visible(False)
    ax.spines["right"].set_visible(False)
    ax.spines["bottom"].set_visible(False)
    ax.spines["left"].set_visible(False)

    pos = {}
    for i, x in enumerate(X):
        pos[x] = node_pos[i]
    color_list = []
    cluster_color_list = []
    for node in G.nodes:
        color_list.append(color_dict[node])
    nx.draw_networkx_nodes(G, pos, node_size=80, node_color=color_list, edgecolors='white', linewidths=0.7)
    #for c, idx in color_idx.items():

        #plt.scatter(node_pos[idx, 0], node_pos[idx, 1], label=c)  # c=node_colors)

    # plt.legend()

    t = datetime.datetime.now()
    plt.savefig("./pics/miserables/{}-{}-{}/line-{}-{}-{}-emb-real.png".format(
        t.year, t.month, t.day,
        t.hour, t.minute, int(t.second)
    ))
    plt.cla()
    cluster = kmeans(embeddings, K=4)
    cluster_color = []
    for node in G.nodes:
        cluster_color.append(COLOR_MAP[cluster[node]])
    nx.draw_networkx_nodes(G, pos, node_size=80, node_color=cluster_color, edgecolors='white', linewidths=0.7)
    plt.savefig("./pics/miserables/{}-{}-{}/line-{}-{}-{}-emb-cluster.png".format(
        t.year, t.month, t.day,
        t.hour, t.minute, int(t.second)
    ))
    plt.cla()
    return color_dict, cluster

def plot_layout(G, embeddings, color_dict, cluster):
    color_list = []
    for node in G.nodes:
        color_list.append(color_dict[node])
    pos = embedding_fr(G, embeddings, cluster=cluster)
    plt.figure(figsize=(10, 10))
    ax = plt.axes([0.05, 0.05, 1 - 0.05 * 2, 1 - 0.05 * 2])
    ax.spines["top"].set_visible(False)
    ax.spines["right"].set_visible(False)
    ax.spines["bottom"].set_visible(False)
    ax.spines["left"].set_visible(False)
    t = datetime.datetime.now()
    nx.draw_networkx_nodes(G, pos, node_size=80, node_color=color_list, edgecolors='white', linewidths=0.7)
    nx.draw_networkx_edges(G, pos, width=0.7, alpha=0.2)
    plt.savefig("./pics/miserables/{}-{}-{}/line-{}-{}-{}-real.png".format(
        t.year, t.month, t.day,
        t.hour, t.minute, int(t.second)
    ))
    plt.cla()
    cluster_color = []
    for node in G.nodes:
        cluster_color.append(COLOR_MAP[cluster[node]])
    nx.draw_networkx_nodes(G, pos, node_size=80, node_color=cluster_color, edgecolors='white', linewidths=0.7)
    nx.draw_networkx_edges(G, pos, width=0.7, alpha=0.2)
    plt.savefig("./pics/miserables/{}-{}-{}/line-{}-{}-{}-cluster.png".format(
        t.year, t.month, t.day,
        t.hour, t.minute, int(t.second)
    ))

if __name__ == "__main__":
    G = nx.read_edgelist('./data/miserables/miserables_edgelist.txt',
                         create_using=nx.Graph(), nodetype=None, data=[('weight', int)])

    model = LINE(G, embedding_size=128, order='second')
    model.train(batch_size=1024, epochs=50, verbose=2)
    embeddings = model.get_embeddings()

    evaluate_embeddings(embeddings)
    color_dict, cluster = plot_embeddings(embeddings)
    plot_layout(G, embeddings, color_dict, cluster)


import  subprocess
from time import ctime
def appium_start(host,port):

    bootstrap_port=str(port+1)
    cmd='start/b appium -a'+host+' -p '+str(port)+' -bp '+str(bootstrap_port)

    print('%s at %s' %(cmd,ctime()))
    subprocess.Popen(cmd,shell=True,stdout=open('./log'+str(port)+'.log','a'),stderr=subprocess.STDOUT)


import pygame
import math

from controls import Controls
from character import Character

class Player(Character):
    def __init__(self, pos, controls, name):
        Character.__init__(self, pos)
        self.controls = controls
        self.name = name
        self.moving = False

    def stop(self):
        self.moving = False

    def change_direction(self, direction):
        self.direction = direction
        self.frame = 0
        self.moving = True

    def step(self, new_direction, grid, ene_blocks):
        if new_direction != self.direction:
            self.change_direction(new_direction)
        if self.direction == 0:
            self.move(0, 1, grid, ene_blocks)
            self.next_frame()
        elif self.direction == 1:
            self.move(1, 0, grid, ene_blocks)
            self.next_frame()
        elif self.direction == 2:
            self.move(0, -1, grid, ene_blocks)
            self.next_frame()
        elif self.direction == 3:
            self.move(-1, 0, grid, ene_blocks)
            self.next_frame()

    def move(self, dx, dy, grid, enemys):
        tempx = int(self.posX/4)
        tempy = int(self.posY/4)

        map = []

        for i in range(len(grid)):
            map.append([])
            for j in range(len(grid[i])):
                map[i].append(grid[i][j])

        for x in enemys:
            if x == self:
                continue
            elif not x.life:
                continue
            else:
                map[int(x.posX/4)][int(x.posY/4)] = 2

        if self.posX % 4 != 0 and dx == 0:
            if self.posX % 4 == 1:
                self.posX -= 1
            elif self.posX % 4 == 3:
                self.posX += 1
            return
        if self.posY % 4 != 0 and dy == 0:
            if self.posY % 4 == 1:
                self.posY -= 1
            elif self.posY % 4 == 3:
                self.posY += 1
            return

        # right
        if dx == 1:
            if map[tempx+1][tempy] in [0, 4]:
                self.posX += 1
        # left
        elif dx == -1:
            tempx = math.ceil(self.posX / 4)
            if map[tempx-1][tempy] in [0, 4]:
                self.posX -= 1

        # bottom
        if dy == 1:
            if map[tempx][tempy+1] in [0, 4]:
                self.posY += 1
        # top
        elif dy == -1:
            tempy = math.ceil(self.posY / 4)
            if map[tempx][tempy-1] in [0, 4]:
                self.posY -= 1

    def load_animations(self, scale, secondPlayer):
        image_path = "images/hero/p"
        if secondPlayer:
            image_path += "2/p"
        else:
            image_path += "1/p"

        Character.load_animations(self, scale, image_path)

    def next_frame(self):
        if self.frame == 2:
            self.frame = 0
        else:
            self.frame += 1


import cv2

import tkinter as tk
from PIL import Image
from PIL import ImageTk
from tkinter import messagebox

class Screen():

	def __init__(self, master):
		self.master = master
		self.create_ui()

	def create_ui(self):
		frame1 = tk.Frame(self.master, borderwidth=2, relief='ridge')
		frame2 = tk.Frame(self.master, borderwidth=2, relief='ridge')
		frame3 = tk.Frame(self.master, borderwidth=2, relief='ridge')
		frame4 = tk.Frame(self.master, borderwidth=2, relief='ridge')
		frame5 = tk.Frame(self.master, borderwidth=2, relief='ridge')
		frame6 = tk.Frame(self.master, borderwidth=2, relief='ridge')
		frame7 = tk.Frame(self.master, borderwidth=2, relief='ridge')

		frame1.grid(column=0, row=0, sticky="nsew")
		frame2.grid(column=0, row=1, sticky="nsew")
		frame3.grid(column=1, row=0, sticky="nsew")
		frame4.grid(column=1, row=1, sticky="nsew")
		frame5.grid(column=2, row=0, sticky="nsew")
		frame6.grid(column=2, row=1, sticky="nsew")
		frame7.grid(column=3, row=0, sticky="nsew", rowspan=2)

		# Image
		self.image1 = self.create_image_label(frame1, 300, 300)
		self.image2 = self.create_image_label(frame2, 300, 300)
		self.image3 = self.create_image_label(frame3, 300, 300)
		self.image4 = self.create_image_label(frame4, 300, 300)
		self.image5 = self.create_image_label(frame5, 300, 300)
		self.image6 = self.create_image_label(frame6, 300, 300)

		self.image1.pack(fill=tk.BOTH, expand=tk.YES)
		self.image2.pack(fill=tk.BOTH, expand=tk.YES)
		self.image3.pack(fill=tk.BOTH, expand=tk.YES)
		self.image4.pack(fill=tk.BOTH, expand=tk.YES)
		self.image5.pack(fill=tk.BOTH, expand=tk.YES)
		self.image6.pack(fill=tk.BOTH, expand=tk.YES)

		# Information
		label_name = self.create_infor_label(frame7, "Họ tên:")
		label_position = self.create_infor_label(frame7, "Vị trí:")
		label_plate_number = self.create_infor_label(frame7, "Biển số xe:")
		label_timein = self.create_infor_label(frame7, "Thời gian vào:")
		label_timeout = self.create_infor_label(frame7, "Thời gian ra:")

		self.name = self.create_infor_label(frame7, "Nguyễn Văn Đức", font=("Helvetica", 16, 'bold'), width=20)
		self.position = self.create_infor_label(frame7, "DEV", font=("Helvetica", 16, 'bold'), width=20)
		self.plate_number = self.create_infor_label(frame7, "03829", font=("Helvetica", 16, 'bold'), width=20)
		self.timein = self.create_infor_label(frame7, "07:13:21", font=("Helvetica", 16, 'bold'), width=20)
		self.timeout = self.create_infor_label(frame7, "18:06:03", font=("Helvetica", 16, 'bold'), width=20)

		label_name.grid(row=0, column=0, pady=10, padx=20, sticky=tk.W)
		label_position.grid(row=1, column=0, pady=10, padx=20, sticky=tk.W)
		label_plate_number.grid(row=2, column=0, pady=10, padx=20, sticky=tk.W)
		label_timein.grid(row=3, column=0, pady=10, padx=20, sticky=tk.W)
		label_timeout.grid(row=4, column=0, pady=10, padx=20, sticky=tk.W)
		self.name.grid(row=0, column=1, sticky=tk.W)
		self.position.grid(row=1, column=1, sticky=tk.W)
		self.plate_number.grid(row=2, column=1, sticky=tk.W)
		self.timein.grid(row=3, column=1, sticky=tk.W)
		self.timeout.grid(row=4, column=1, sticky=tk.W)

		button = tk.Button(frame7, text="hello", width=20, command=self.show_error)
		button.grid(row=5, columnspan=2, pady=80)

	def create_image_label(self, container, width, height, background='#e8e8e8'):
		# default no_image path
		default_image_path = 'image/no_image.png'
		image = cv2.imread(default_image_path)
		image = cv2.resize(image, (width, height))
		image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
		image = Image.fromarray(image)
		image = ImageTk.PhotoImage(image)
		label = tk.Label(container, image=image, width=width, height=height, background=background)
		# keep reference for display image
		label.image = image
		return label

	def create_infor_label(self, container, text, font=("Helvetica", 16), fg="#282828", width=15):
		label = tk.Label(container, text=text, font=font, fg=fg, width=width, anchor='w')
		return label

	def set_image(self, location, image, width=300, height=300):
		# if image is None
		# set default image is no_image
		if len(image) == 0:
			default_image_path = 'image/no_image.jpg'
			image = cv2.imread(default_image_path)
		# resize and convert image to tkinter image type
		image = cv2.resize(image, (width, height))
		image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
		image = Image.fromarray(image)
		image = ImageTk.PhotoImage(image)
		# check localion image and put image to label
		if location == 1:
			self.image1.configure(image=image)
			self.image1.image = image
		elif location == 2:
			self.image2.configure(image=image)
			self.image2.image = image
		elif location == 3:
			self.image3.configure(image=image)
			self.image3.image = image
		elif location == 4:
			self.image4.configure(image=image)
			self.image4.image = image
		elif location == 5:
			self.image5.configure(image=image)
			self.image5.image = image
		else:
			self.image6.configure(image=image)
			self.image6.image = image

	def show_alert(self):
		messagebox.showwarning("Infor", "Could not open file")

	def show_error(self):
		top = tk.Toplevel()
		top.title('ALERT')
		tk.Message(top, text="ERROR", padx=20, pady=20).pack()
		top.after(2000, top.destroy)

	def show_success(self):
		top = tk.Toplevel()
		top.title('ALERT')
		tk.Message(top, text="SUCCESS", padx=20, pady=20).pack()
		top.after(2000, top.destroy)

def main():
	master = tk.Tk()
	screen = Screen(master)
	screen.show_success()
	master.mainloop()

if __name__ == '__main__':
	main()


import numpy as np
import cv2
kernel=np.array([[-1,-1,-1],[-1, 9,-1],[-1,-1,-1]])
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
smile_cascade = cv2.CascadeClassifier('haarcascade_smile.xml')

cap = cv2.VideoCapture(0)

while 1:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    sharpened=cv2.filter2D((img),-1,kernel)
    gray_sharp=cv2.cvtColor(sharpened, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.2, 5)
    faces1= face_cascade.detectMultiScale(gray_sharp, 1.2, 5)
    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]

        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,0,255),2)
    for (x,y,w,h) in faces1:
        cv2.rectangle(sharpened,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray1 = gray_sharp[y:y+h, x:x+w]
        roi_color1 = sharpened[y:y+h, x:x+w]

        eyes = eye_cascade.detectMultiScale(roi_gray1)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color1,(ex,ey),(ex+ew,ey+eh),(0,0,255),2)
    #cv2.line(img,(a,b),(a+c,b+d),(255,0,0),2)
    cv2.imshow('img',img)
    cv2.imshow('sharp',sharpened)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()


import json
import csv
import boto3
def lambda_handler(event, context):
	region = 'us-east-1'
	records = []
	try:
		s3 = boto3.client('s3')
		dynamodb = boto3.client('dynamodb', region_name = region)
		bucket = event['Records'][0]['s3']['bucket']['name']
		file = event['Records'][0]['s3']['object']['key']
		print('Bucket: ', bucket, 'key: ', file)

		csv_file = s3.get_object(Bucket= bucket, Key=file)
		records = csv_file['Body'].read().decode('utf-8').split('\n')
		csv_reader = csv.reader(records, delimiter= ',', quotechar='"')

		for row in csv_reader:
			ind = row[0]
			X1_ActualPosition = row[1]
			X1_ActualVelocity = row[2]
			X1_ActualAcceleration=row[3]
			X1_CommandPosition=row[4]
			X1_CommandVelocity=row[5]
			X1_CommandAcceleration = row[6]
			X1_CurrentFeedback=row[7]
			X1_DCBusVoltage=row[8]
			X1_OutputCurrent=row[9]
			X1_OutputVoltage=row[10]
			X1_OutputPower=row[11]
			Y1_ActualPosition=row[12]
			Y1_ActualVelocity=row[13]
			Y1_ActualAcceleration=row[14]
			Y1_CommandPosition=row[15]
			Y1_CommandVelocity=row[16]
			Y1_CommandAcceleration=row[17]
			Y1_CurrentFeedback=row[18]
			Y1_DCBusVoltage=row[19]
			Y1_OutputCurrent=row[20]
			Y1_OutputVoltage=row[21]
			Y1_OutputPower=row[22]
			Z1_ActualPosition=row[23]
			Z1_ActualVelocity=row[24]
			Z1_ActualAcceleration=row[25]
			Z1_CommandPosition=row[26]
			Z1_CommandVelocity=row[27]
			Z1_CommandAcceleration=row[28]
			Z1_CurrentFeedback=row[29]
			Z1_DCBusVoltage=row[30]
			Z1_OutputCurrent=row[31]
			Z1_OutputVoltage=row[32]
			S1_ActualPosition=row[33]
			S1_ActualVelocity=row[34]
			S1_ActualAcceleration=row[35]
			S1_CommandPosition=row[36]
			S1_CommandVelocity=row[37]
			S1_CommandAcceleration=row[38]
			S1_CurrentFeedback=row[39]
			S1_DCBusVoltage=row[40]
			S1_OutputCurrent=row[41]
			S1_OutputVoltage=row[42]
			S1_OutputPower=row[43]
			S1_SystemInertia=row[44]
			M1_CURRENT_PROGRAM_NUMBER=row[45]
			M1_sequence_number=row[46]
			M1_CURRENT_FEEDRATE=row[47]
			Machining_Process=row[48]
			Experiment = row[49]
			Experiment_owner = row[50]

			add_to_db = dynamodb.put_item(
				TableName = 'cnc_machine_experiments',
				Item = {
				'Event_Id' : {'N': str(ind)},
				'X1_ActualPosition' : {'S': str(X1_ActualPosition)},
				'X1_ActualVelocity' : {'S': str(X1_ActualVelocity)},
			'X1_ActualAcceleration' : {'S': str(X1_ActualAcceleration)},
				'X1_CommandPosition' : {'S': str(X1_CommandPosition)},
				'X1_CommandVelocity' : {'S': str(X1_CommandVelocity)},
				'X1_CommandAcceleration' : {'S': str(X1_CommandAcceleration)},
				'X1_CurrentFeedback' : {'S': str(X1_CurrentFeedback)},
				'X1_DCBusVoltage' : {'S': str(X1_DCBusVoltage)},
				'X1_OutputCurrent' : {'N': str(X1_OutputCurrent)},
				'X1_OutputVoltage' : {'N': str(X1_OutputVoltage)},
				'X1_OutputPower' : {'S': str(X1_OutputPower)},
				'Y1_ActualPosition' : {'S': str(Y1_ActualPosition)},
				'Y1_ActualVelocity' : {'S': str(Y1_ActualVelocity)},
			'Y1_ActualAcceleration' : {'S': str(Y1_ActualAcceleration)},
				'Y1_CommandPosition' : {'S': str(Y1_CommandPosition)},
				'Y1_CommandVelocity' : {'S': str(Y1_CommandVelocity)},
			'Y1_CommandAcceleration' : {'S': str(Y1_CommandAcceleration)},
				'Y1_CurrentFeedback' : {'S': str(Y1_CurrentFeedback)},
				'Y1_DCBusVoltage' : {'S': str(Y1_DCBusVoltage)},
				'Y1_OutputCurrent' : {'S': str(Y1_OutputCurrent)},
				'Y1_OutputVoltage' : {'S': str(Y1_OutputVoltage)},
				'Y1_OutputPower' : {'S': str(Y1_OutputPower)},
				'Z1_ActualPosition' : {'S': str(Z1_ActualPosition)},
				'Z1_ActualVelocity' : {'S': str(Z1_ActualVelocity)},
			'Z1_ActualAcceleration' : {'S': str(Z1_ActualAcceleration)},
				'Z1_CommandPosition' : {'S': str(Z1_CommandPosition)},
				'Z1_CommandVelocity' : {'S': str(Z1_CommandVelocity)},
				'Z1_CommandAcceleration' : {'S': str(Z1_CommandAcceleration)},
				'Z1_CurrentFeedback' : {'S': str(Z1_CurrentFeedback)},
				'Z1_DCBusVoltage' : {'S': str(Z1_DCBusVoltage)},
				'Z1_OutputCurrent' : {'N': str(Z1_OutputCurrent)},
				'Z1_OutputVoltage' : {'N': str(Z1_OutputVoltage)},
				'S1_ActualPosition' : {'S': str(S1_ActualPosition)},
				'S1_ActualVelocity' : {'S': str(S1_ActualVelocity)},
				'S1_ActualAcceleration' : {'S': str(S1_ActualAcceleration)},
				'S1_CommandPosition' : {'S': str(S1_CommandPosition)},
				'S1_CommandVelocity' : {'S': str(S1_CommandVelocity)},
				'S1_CommandAcceleration' : {'S': str(S1_CommandAcceleration)},
				'S1_CurrentFeedback' : {'S': str(S1_CurrentFeedback)},
				'S1_DCBusVoltage' : {'S': str(S1_DCBusVoltage)},
				'S1_OutputCurrent' : {'N': str(S1_OutputCurrent)},
				'S1_OutputVoltage' : {'N': str(S1_OutputVoltage)},
				'S1_OutputPower' : {'S': str(S1_OutputPower)},
				'S1_SystemInertia' : {'S': str(S1_SystemInertia)},
			'M1_CURRENT_PROGRAM_NUMBER' : {'S': str(M1_CURRENT_PROGRAM_NUMBER)},
				'M1_sequence_number' : {'S': str(M1_sequence_number)},
				'M1_CURRENT_FEEDRATE' : {'S': str(M1_CURRENT_FEEDRATE)},
				'Machining_Process' : {'S': str(Machining_Process)},
				'Experiment' : {'S': str(Experiment)},
				'Experiment_owner' : {'S': str(Experiment_owner)}
				})
		print('Records were added')

	except Exception as e:
		print(str(e))
	return {
			'statusCode': 200,
			'body': json.dumps('Data Successfully Loaded into DynamoDB')
	}


#!/usr/bin/env python
# coding: utf-8

# In[1]:
import math
import pandas as pd

# In[2]:
df1=pd.read_csv('C:/Users/muham/new/train.csv')
df2=pd.read_csv('C:/Users/muham/new/test.csv')

# In[3]:
print(df1)

# In[4]:
#print(df2)

# In[5]:
df1=df1.sort_values(by=['Qmc'])
print(df1)

# In[6]:

# In[7]:
def getDoughertyBin(length):
    log_result = math.log(int(length), 10)
    nilai = 1 if log_result < 1 else math.floor(2*log_result)
    return float(nilai)

# In[8]:
getDoughertyBin(126)

# %%


import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import pandas as pd

from libs.useful_fcts import fig_creator_CC

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv')

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

### Figure ###

app.layout = html.Div(children=[
    dcc.Graph(
        id='graph',
        figure=fig_creator_CC(["France"], 'Non')
    ),
    html.Div(id='Country_value', style={'margin-top': 20}),
    dcc.Dropdown(
        id='Country_choice',
        options=[
            # {'label': 'Afghanistan', 'value': 'Afghanistan'},
            # {'label': 'Afrique du Sud', 'value': 'Afrique du Sud'},
            # {'label': 'Albanie', 'value': 'Albanie'},
            # {'label': 'Algérie', 'value': 'Algérie'},
            {'label': 'Allemagne', 'value': 'Allemagne'},
            # {'label': 'Andorre', 'value': 'Andorre'},
            # {'label': 'Angola', 'value': 'Angola'},
            # {'label': 'Arabie sahoudite', 'value': 'Arabie sahoudite'},
            # {'label': 'Argentine', 'value': 'Argentine'},
            # {'label': 'Aruba', 'value': 'Aruba'},
            # {'label': 'Australie', 'value': 'Australie'},
            {'label': 'Autriche', 'value': 'Autriche'},
            # {'label': 'Azerbaïdjan', 'value': 'Azerbaïdjan'},
            # {'label': 'Bangladesh', 'value': 'Bangladesh'},
            # {'label': 'Barbades', 'value': 'Barbades'},
            # {'label': 'Barheïn', 'value': 'Barheïn'},
            {'label': 'Belgique', 'value': 'Belgique'},
            # {'label': 'Belize', 'value': 'Belize'},
            # {'label': 'Bénin', 'value': 'Bénin'},
            # {'label': 'Bermudes', 'value': 'Bermudes'},
            # {'label': 'Bhoutan', 'value': 'Bhoutan'},
            # {'label': 'Biélorussie', 'value': 'Biélorussie'},
            # {'label': 'Birmanie', 'value': 'Birmanie'},
            # {'label': 'Bolivie', 'value': 'Bolivie'},
            # {'label': 'Bosnie-Herzégovine', 'value': 'Bosnie-Herzégovine'},
            # {'label': 'Botswana', 'value': 'Botswana'},
            # {'label': 'Brésil', 'value': 'Brésil'},
            # {'label': 'Brunei', 'value': 'Brunei'},
            # {'label': 'Bulgarie', 'value': 'Bulgarie'},
            # {'label': 'Burkina Faso', 'value': 'Burkina Faso'},
            # {'label': 'Burundi', 'value': 'Burundi'},
            # {'label': 'Cambodge', 'value': 'Cambodge'},
            # {'label': 'Cameroun', 'value': 'Cameroun'},
            # {'label': 'Canada', 'value': 'Canada'},
            # {'label': 'Cap-Vert', 'value': 'Cap-Vert'},
            # {'label': 'Chili', 'value': 'Chili'},
            # {'label': 'Chine', 'value': 'Chine'},
            # {'label': 'Chypre', 'value': 'Chypre'},
            # {'label': 'Colombie', 'value': 'Colombie'},
            # {'label': 'Congo', 'value': 'Congo'},
            # {'label': 'Corée du Sud', 'value': 'Corée du Sud'},
            # {'label': 'Costa Rica', 'value': 'Costa Rica'},
            # {'label': 'Côte d\'Ivoire', 'value': 'Côte d\'Ivoire'},
            # {'label': 'Croatie', 'value': 'Croatie'},
            # {'label': 'Cuba', 'value': 'Cuba'},
            {'label': 'Danemark', 'value': 'Danemark'},
            # {'label': 'Djibouti', 'value': 'Djibouti'},
            # {'label': 'Dominique', 'value': 'Dominique'},
            # {'label': 'Égypte', 'value': 'Égypte'},
            # {'label': 'Émirats arabes unis', 'value': 'Émirats arabes unis'},
            # {'label': 'Équateur', 'value': 'Équateur'},
            # {'label': 'Érythrée', 'value': 'Érythrée'},
            {'label': 'Espagne', 'value': 'Espagne'},
            # {'label': 'Estonie', 'value': 'Estonie'},
            # {'label': 'Eswatini', 'value': 'Eswatini'},
            # {'label': 'États-Unis', 'value': 'États-Unis'},
            # {'label': 'Éthiopie', 'value': 'Éthiopie'},
            # {'label': 'Fidji', 'value': 'Fidji'},
            # {'label': 'Finlande', 'value': 'Finlande'},
            {'label': 'France', 'value': 'France'},
            # {'label': 'France2', 'value': 'France2'},
            # {'label': 'Gabon', 'value': 'Gabon'},
            # {'label': 'Gambie', 'value': 'Gambie'},
            # {'label': 'Géorgie', 'value': 'Géorgie'},
            # {'label': 'Ghana', 'value': 'Ghana'},
            # {'label': 'Gibraltar', 'value': 'Gibraltar'},
            # {'label': 'Grèce', 'value': 'Grèce'},
            # {'label': 'Groenland', 'value': 'Groenland'},
            # {'label': 'Guam', 'value': 'Guam'},
            # {'label': 'Guatemala', 'value': 'Guatemala'},
            # {'label': 'Guinée', 'value': 'Guinée'},
            # {'label': 'Guyana', 'value': 'Guyana'},
            # {'label': 'Haïti', 'value': 'Haïti'},
            # {'label': 'Honduras', 'value': 'Honduras'},
            # {'label': 'Hong Kong', 'value': 'Hong Kong'},
            # {'label': 'Hongrie', 'value': 'Hongrie'},
            # {'label': 'Îles Salomon', 'value': 'Îles Salomon'},
            # {'label': 'Inde', 'value': 'Inde'},
            # {'label': 'Indonésie', 'value': 'Indonésie'},
            # {'label': 'Irak', 'value': 'Irak'},
            # {'label': 'Iran', 'value': 'Iran'},
            # {'label': 'Irlande', 'value': 'Irlande'},
            {'label': 'Islande', 'value': 'Islande'},
            # {'label': 'Israël', 'value': 'Israël'},
            {'label': 'Italie', 'value': 'Italie'},
            # {'label': 'Jamaïque', 'value': 'Jamaïque'},
            # {'label': 'Japon', 'value': 'Japon'},
            # {'label': 'Jordanie', 'value': 'Jordanie'},
            # {'label': 'Kazakhstan', 'value': 'Kazakhstan'},
            # {'label': 'Kenya', 'value': 'Kenya'},
            # {'label': 'Kirghizistan', 'value': 'Kirghizistan'},
            # {'label': 'Kosovo', 'value': 'Kosovo'},
            # {'label': 'Koweït', 'value': 'Koweït'},
            # {'label': 'Laos', 'value': 'Laos'},
            # {'label': 'Lesotho', 'value': 'Lesotho'},
            # {'label': 'Lettonie', 'value': 'Lettonie'},
            # {'label': 'Liban', 'value': 'Liban'},
            # {'label': 'Libéria', 'value': 'Libéria'},
            # {'label': 'Libye', 'value': 'Libye'},
            # {'label': 'Lituanie', 'value': 'Lituanie'},
            # {'label': 'Luxembourg', 'value': 'Luxembourg'},
            # {'label': 'Macao', 'value': 'Macao'},
            # {'label': 'Madagascar', 'value': 'Madagascar'},
            # {'label': 'Malaisie', 'value': 'Malaisie'},
            # {'label': 'Malawi', 'value': 'Malawi'},
            # {'label': 'Mali', 'value': 'Mali'},
            # {'label': 'Maroc', 'value': 'Maroc'},
            # {'label': 'Maurice', 'value': 'Maurice'},
            # {'label': 'Mauritanie', 'value': 'Mauritanie'},
            # {'label': 'Mexique', 'value': 'Mexique'},
            # {'label': 'Moldavie', 'value': 'Moldavie'},
            # {'label': 'Mongolie', 'value': 'Mongolie'},
            # {'label': 'Mozambique', 'value': 'Mozambique'},
            # {'label': 'Namibie', 'value': 'Namibie'},
            # {'label': 'Népal', 'value': 'Népal'},
            # {'label': 'Nicaragua', 'value': 'Nicaragua'},
            # {'label': 'Niger', 'value': 'Niger'},
            # {'label': 'Nigéria', 'value': 'Nigéria'},
            # {'label': 'Norvège', 'value': 'Norvège'},
            # {'label': 'Nouvelle-Zélande', 'value': 'Nouvelle-Zélande'},
            # {'label': 'Oman', 'value': 'Oman'},
            # {'label': 'Ouganda', 'value': 'Ouganda'},
            # {'label': 'Ouzbékistan', 'value': 'Ouzbékistan'},
            # {'label': 'Pakistan', 'value': 'Pakistan'},
            # {'label': 'Palestine', 'value': 'Palestine'},
            # {'label': 'Panama', 'value': 'Panama'},
            # {'label': 'Papouasie-Nouvelle-Guinée', 'value': 'Papouasie-Nouvelle-Guinée'},
            # {'label': 'Paraguay', 'value': 'Paraguay'},
            {'label': 'Pays-Bas', 'value': 'Pays-Bas'},
            # {'label': 'Pérou', 'value': 'Pérou'},
            # {'label': 'Philippines', 'value': 'Philippines'},
            # {'label': 'Pologne', 'value': 'Pologne'},
            # {'label': 'Porto Rico', 'value': 'Porto Rico'},
            # {'label': 'Portugal', 'value': 'Portugal'},
            # {'label': 'Qatar', 'value': 'Qatar'},
            # {'label': 'République centrafricaine', 'value': 'République centrafricaine'},
            # {'label': 'République démocratique du Congo', 'value': 'République démocratique du Congo'},
            # {'label': 'République dominicaine', 'value': 'République dominicaine'},
            # {'label': 'Roumanie', 'value': 'Roumanie'},
            {'label': 'Royaume-Uni', 'value': 'Royaume-Uni'},
            # {'label': 'Russie', 'value': 'Russie'},
            # {'label': 'Rwanda', 'value': 'Rwanda'},
            # {'label': 'Saint-Marin', 'value': 'Saint-Marin'},
            # {'label': 'Salvador', 'value': 'Salvador'},
            # {'label': 'Sénégal', 'value': 'Sénégal'},
            # {'label': 'Serbie', 'value': 'Serbie'},
            # {'label': 'Seychelles', 'value': 'Seychelles'},
            # {'label': 'Sierra Leone', 'value': 'Sierra Leone'},
            # {'label': 'Singapour', 'value': 'Singapour'},
            # {'label': 'Slovaquie', 'value': 'Slovaquie'},
            # {'label': 'Slovénie', 'value': 'Slovénie'},
            # {'label': 'Somalie', 'value': 'Somalie'},
            # {'label': 'Soudan', 'value': 'Soudan'},
            # {'label': 'Soudan du Sud', 'value': 'Soudan du Sud'},
            # {'label': 'Sri Lanka', 'value': 'Sri Lanka'},
            {'label': 'Suède', 'value': 'Suède'},
            {'label': 'Suisse', 'value': 'Suisse'},
            # {'label': 'Suriname', 'value': 'Suriname'},
            # {'label': 'Syrie', 'value': 'Syrie'},
            # {'label': 'Taiwan', 'value': 'Taiwan'},
            # {'label': 'Tajikistan', 'value': 'Tajikistan'},
            # {'label': 'Tanzanie', 'value': 'Tanzanie'},
            # {'label': 'Tchad', 'value': 'Tchad'},
            # {'label': 'Tchéquie', 'value': 'Tchéquie'},
            # {'label': 'Thaïlande', 'value': 'Thaïlande'},
            # {'label': 'Timor oriental', 'value': 'Timor oriental'},
            # {'label': 'Togo', 'value': 'Togo'},
            # {'label': 'Trinité-et-Tobago', 'value': 'Trinité-et-Tobago'},
            # {'label': 'Tunisie', 'value': 'Tunisie'},
            # {'label': 'Turkmenistan', 'value': 'Turkmenistan'},
            # {'label': 'Turquie', 'value': 'Turquie'},
            # {'label': 'Ukraine', 'value': 'Ukraine'},
            # {'label': 'Uruguay', 'value': 'Uruguay'},
            # {'label': 'Vanuatu', 'value': 'Vanuatu'},
            # {'label': 'Vénézuela', 'value': 'Vénézuela'},
            # {'label': 'Viêt Nam', 'value': 'Viêt Nam'},
            # {'label': 'Yémen', 'value': 'Yémen'},
            # {'label': 'Zambie', 'value': 'Zambie'},
            # {'label': 'Zimbabwe', 'value': 'Zimbabwe'}
        ],
        value=["Allemagne", "Autriche", "Belgique", "Danemark", "Espagne", "France", "Islande", "Italie", "Pays-Bas",
               "Royaume-Uni", "Suède", "Suisse"],
        multi=True
    ),
    html.Div(id='Normalize_value', style={'margin-top': 20}),
    dcc.Dropdown(
        id='Normalize_choice',
        options=[
            {'label': 'Oui', 'value': 'Oui'},
            {'label': 'Non', 'value': 'Non'}
        ],
        value='Oui'
    )
])

### Mise à jour de la figure ###

@app.callback(Output('Country_value', 'children'),
              [Input('Country_choice', 'value')])
def country(name):
    return 'Pays : '

@app.callback(Output('Normalize_value', 'children'),
              [Input('Normalize_choice', 'value')])
def country(answer):
    return 'Normaliser ? : '

@app.callback(
    Output('graph', 'figure'),
    [Input('Country_choice', 'value'),
     Input('Normalize_choice', 'value')])
def update_graph(name_list, normalize):
    return fig_creator_CC(name_list, normalize)

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


from django.conf.urls import url
from BV_GameServer import settings
from . import views

app_name = 'main'

urlpatterns = [

    url(r'^$', views.index, name='index'),
    url(r'^get/$', views.get_data, name='get'),
    url(r'^help/$', views.help_page, name='help'),

    # Auth
    url(r'^login/$', views.login, name='login'),
    url(r'^logout/$', views.logout, name='logout'),

    # Staff
    url(r'^staff/$', views.staff, name='staff'),
    url(r'^staff/(?P<grade_id>[0-9]+)/$', views.grade_page, name='grade'),
    url(r'^staff/(?P<grade_id>[0-9]+)/(?P<category_id>[0-9]+)$', views.category_page, name='category'),
    url(r'^staff/(?P<grade_id>[0-9]+)/(?P<category_id>[0-9]+)/(?P<question_id>[0-9]+)/delete_question/$',
        views.delete_question, name='delete_question'),

    # Admin
    url(r'^staff/register/$', views.register, name='register'),
    url(r'^staff/bug_reports/$', views.bug_reports, name='bug_reports'),

]

if settings.DEBUG:
    urlpatterns += [
        url(r'^createsuperuser/$', views.createsuperuser),
    ]