import time
import pandas as pd
import numpy as np

CITY_DATA = { 'chicago': 'chicago.csv',
              'new york city': 'new_york_city.csv',
              'washington': 'washington.csv' }

def get_filters():
    """
    Asks user to specify a city, month, and day to analyze.

    Returns:
        (str) city - name of the city to analyze
        (str) month - name of the month to filter by, or "all" to apply no month filter
        (str) day - name of the day of week to filter by, or "all" to apply no day filter
    """
    print('Hello! Let\'s explore some US bikeshare data!')
    # Get user input for city (chicago, new york city, washington)
    while True:
        print('\n')
        city = input('Please tell me the city you would like to receive bikeshare statistics on (Chicago, New York City, Washington): ').lower()
        if city in ['chicago','new york city','washington']:
            break
        else:
            print('\n')
            print('Unfortunately this is not a valid input. Please give me one of the cities shown in parenthesis.')

    # Get user input for month (all, january, february, ... , june)
    while True:
        print('\n')
        month = input('Now, please tell me the month you would like to receive bikeshare statistics from (all, January, February, ... , June): ').lower()
        if month in ['all','january','february','march','april','may','june']:
            break
        else:
            print('\n')
            print('Unfortunately this is not a valid input. Please give me one of the possible month input shown in parenthesis.')

    # Get user input for day of week (all, monday, tuesday, ... sunday)
    while True:
        print('\n')
        day = input('Please tell me now the day of week you would like to receive bikeshare statistics from (all, Monday, Tuesday, ... Sunday): ').lower()
        if day == 'all':
            break
        elif day == 'monday':
            break
        elif day == 'tuesday':
            break
        elif day == 'wednesday':
            break
        elif day == 'thursday':
            break
        elif day == 'friday':
            break
        elif day == 'saturday':
            break
        elif day == 'sunday':
            break
        else:
            print('\n')
            print('Unfortunately this is not a valid input. Please give me one of the possible day of week input shown in parenthesis.')

    print('-'*40)
    return city, month, day

def load_data(city, month, day):
    """
    Loads data for the specified city and filters by month and day if applicable.

    Args:
        (str) city - name of the city to analyze
        (str) month - name of the month to filter by, or "all" to apply no month filter
        (str) day - name of the day of week to filter by, or "all" to apply no day filter
    Returns:
        df - Pandas DataFrame containing city data filtered by month and day
    """
    # Load data file into a dataframe
    df = pd.read_csv(CITY_DATA[city])

    # Convert the Start Time column to datetime
    df['Start Time'] = pd.to_datetime(df['Start Time'])

    # Extract month and day of week from Start Time to create the new columns 'month' and 'day_of_week'
    df['month'] = df['Start Time'].dt.month
    df['day_of_week'] = df['Start Time'].dt.weekday_name

    # Filter by month if applicable
    if month != 'all':
        # Use the index of the months list to get the corresponding int
        months = ['january', 'february', 'march', 'april', 'may', 'june']
        month = months.index(month) + 1

        # Filter by month to create the new dataframe
        df = df[df["month"] == month]

    # Filter by day of week if applicable
    if day != 'all':
        # Filter by day of week to create the new dataframe
        df = df[df['day_of_week'] == day.title()]

    return df

def time_stats(df):
    """Displays statistics on the most frequent times of travel."""

    print('\nCalculating The Most Frequent Times of Travel...\n')
    start_time = time.time()

    # Display the most common month and the corresponding count
    months = ['January', 'February', 'March', 'April', 'May', 'June']
    popular_month = df['month'].mode()[0]
    popular_month_count = (df['month'] == popular_month).sum()

    # Use the popular_month int as an index to get the coresponding month name from the months list
    print('The most common month is: {} (Count: {})\n'.format(months[popular_month - 1], popular_month_count))

    # Display the most common day of week and the corresponding count
    popular_day_of_week = df['day_of_week'].mode()[0]
    popular_dow_count = (df['day_of_week'] == popular_day_of_week).sum()
    print('The most common day of week is: {} (Count: {})\n'.format(popular_day_of_week, popular_dow_count))

    # Display the most common start hour and the corresponding count
    df['hour'] = df['Start Time'].dt.hour
    popular_hour = df['hour'].mode()[0]
    popular_hour_count = (df['hour'] == popular_hour).sum()
    print('The most common hour is: {} (Count: {})\n'.format(popular_hour, popular_hour_count))

    print("\nThis took %s seconds." % (time.time() - start_time))
    print('-'*40)

def station_stats(df):
    """Displays statistics on the most popular stations and trip."""

    print('\nCalculating The Most Popular Stations and Trip...\n')
    start_time = time.time()

    # Display most commonly used start station and the corresponding count
    popular_start_station = df['Start Station'].mode()[0]
    popular_start_station_count = (df['Start Station'] == popular_start_station).sum()
    print('The most commonly used start station is: {} (Count: {})\n'.format(popular_start_station, popular_start_station_count))

    # Display most commonly used end station and the corresponding count
    popular_end_station = df['End Station'].mode()[0]
    popular_end_station_count = (df['End Station'] == popular_end_station).sum()
    print('The most commonly used end station is: {} (Count: {})\n'.format(popular_end_station, popular_end_station_count))

    # Display most frequent combination of start station and end station trip and the corresponding count
    # First creat the new column 'start_end_combination' as a basis to count the most popular start-end-route
    df['start_end_combination'] = df['Start Station'] + ' ---> ' + df['End Station']
    popular_start_end_combination = df['start_end_combination'].mode()[0]
    popular_start_end_combination_count = (df['start_end_combination'] == popular_start_end_combination).sum()
    print('The most frequent combination of start station and end station trip: {} (Count: {})\n'.format(popular_start_end_combination, popular_start_end_combination_count))

    print("\nThis took %s seconds." % (time.time() - start_time))
    print('-'*40)

def trip_duration_stats(df):
    """Displays statistics on the total and average trip duration."""

    print('\nCalculating Trip Duration...\n')
    start_time = time.time()

    # Display total travel time
    total_travel_time = df['Trip Duration'].sum()
    print('The total travel time is: {} seconds.\n'.format(total_travel_time))

    # Display mean travel time
    mean_travel_time = df['Trip Duration'].mean()
    print('The average trip duration is: {} seconds.\n'.format(mean_travel_time))

    print("\nThis took %s seconds." % (time.time() - start_time))
    print('-'*40)

def user_stats(df):
    """Displays statistics on bikeshare users."""

    print('\nCalculating User Stats...\n')
    start_time = time.time()

    # Display counts of user types
    user_types_count = df['User Type'].value_counts()
    print('These are the counts of user types:\n')
    print(user_types_count)
    print('\n')

    # Display counts of gender
    try:
        gender_count = df['Gender'].value_counts()
        print('These are the counts of genders:\n')
        print(gender_count)
        print('\n')
    except KeyError:
        # Handle KeyError if Washington is chosen as there is no gender information
        print('There is no gender information in the data set.\n')

    # Display earliest, most recent, and most common year of birth
    try:
        earliest_yob = df['Birth Year'].min()
        most_recent_yob = df['Birth Year'].max()
        most_common_yob = df['Birth Year'].mode()[0]
        most_common_yob_count = (df['Birth Year'] == most_common_yob).sum()

        print('The earliest year of birth is: {}\n'.format(int(earliest_yob)))
        print('The most recent year of birth is: {}\n'.format(int(most_recent_yob)))
        print('The most common year of birth is: {} (Count: {})\n'.format(int(most_common_yob), most_common_yob_count))

    except KeyError:
        # Handle KeyError if Washington is chosen as there is no birth year information
        print('There is no birth year information in the data set.\n')

    print("\nThis took %s seconds." % (time.time() - start_time))
    print('-'*40)

def show_raw_data(df):
    """Displays raw data of the dataframe to the user."""

    # Prompt the user if they want to see 5 lines of raw data
    view_data = input('\nWould you like to view 5 rows of individual trip data? Please enter yes or no. \n')
    start_loc = 0

    # Display that data if the answer is 'yes' and continue iterating prompts and displaying the next 5 lines of raw data at each iteration
    # Stop the loop when the user says 'no' or there is no more raw data to display
    while(view_data.lower() == 'yes'):
        if(start_loc >= len(df)):
            break
        print(df.iloc[start_loc:(start_loc + 5)])
        start_loc += 5
        view_data = input('Do you wish to continue?: ')

def main():
    while True:
        city, month, day = get_filters()
        print('Thank you. Your input is:\nCity: {}\nMonth: {}\nDay: {}\n'.format(city.title(), month.title(), day.title()))
        df = load_data(city, month, day)

        time_stats(df)
        station_stats(df)
        trip_duration_stats(df)
        user_stats(df)
        show_raw_data(df)

        restart = input('\nWould you like to restart? Enter yes or no.\n')
        if restart.lower() != 'yes':
            break

if __name__ == "__main__":
	main()


import select

from core import log
from event.event import *

class Select(object):
    name = 'select'

    def __init__(self):
        self.r_list = []    # read
        self.w_list = []    # write
        self.x_list = []    # exception

        self.r_datas = {}
        self.w_datas = {}

        self.__ = self

    def __del__(self):
        self.done()

    def done(self):
        for s in self.r_datas:
            ev, c = self.r_datas[s]
            ev.active = False

        for s in self.w_datas:
            ev, c = self.w_datas[s]
            ev.active = False

        self.r_list = []    # read
        self.w_list = []    # write
        self.x_list = []    # exception

        self.r_datas = {}
        self.w_datas = {}

    def add_conn(self, c, event, flag = 0):
        s   = c.socket()
        wev = c.wev
        rev = c.rev
        e   = 0

        if event & READ_EVENT and not rev.active:
            e |= READ_EVENT
            self.r_list.append(s)
            self.r_datas[s] = (rev, c)  # 防止connection过早释放
            rev.active      = self      # 防止select过早释放

        if event & WRITE_EVENT and not wev.active:
            e |= WRITE_EVENT
            self.w_list.append(s)
            self.w_datas[s] = (wev, c)  # 防止connection过早释放
            wev.active      = self      # 防止select过早释放

        return e

    def del_conn(self, c, event, flag = 0):
        s   = c.socket()
        wev = c.wev
        rev = c.rev
        e   = 0

        if not wev.active and not rev.active:
            return

        if event & READ_EVENT and rev.active:
            e |= READ_EVENT
            self.r_list.remove(s)
            del self.r_datas[s]
            rev.active = None

        if event & WRITE_EVENT and wev.active:
            e |= WRITE_EVENT
            self.w_list.remove(s)
            del self.w_datas[s]
            wev.active = None

        return e

    def process_events(self, wait_time = 20):
        if wait_time:
            wait_time = float(wait_time) / 1000
        else:
            wait_time = 0.02
        try:
            r, w, x = select.select(self.r_list, self.w_list, self.x_list, wait_time)
        except Exception as exc:
            log.error(exc, 'select() fail')
            exit(-1)
            return

        if not (r or w or x):
            return

        closed = {}

        for s in r:
            if not self.r_datas.has_key(s):
                continue
            ev, c = self.r_datas[s]
            if not c.socket():
                closed[s] = c
                continue

            ev.process(self.name)

        for s in w:
            if not self.w_datas.has_key(s):
                continue
            ev, c = self.w_datas[s]
            if not c.socket():
                closed[s] = c
                continue

            ev.process(self.name)

        for s in closed:
            c = closed[s]
            if c.wev.active:
                self.w_list.remove(s)
                del self.w_datas[s]
            if c.rev.active:
                self.r_list.remove(s)
                del self.r_datas[s]


#coding:utf-8
"""
经典题：所有可能的出栈顺序
思路：a表示原序列，s表示入栈，s_表示出栈顺序
某一个时刻可能执行两种情况：
1.a内元素压栈到s 模拟压栈
2.s内元素压栈到s_ 模拟出栈
"""
def get_all_seq(a, s, s_):
    # 全部出栈
    if len(a)==0 and len(s)==0:
        print(s_)
    # 第一种情况
    if len(a) != 0:
        s.append(a.pop())
        get_all_seq(a, s, s_)
        a.append(s.pop()) # 回溯，还原状态
    # 第二种情况
    if len(s) != 0:
        s_.append(s.pop())
        get_all_seq(a, s, s_)
        s.append(s_.pop()) # 回溯还原状态

a = [1,2,3,4]
s, s_ = [], []
get_all_seq(list(reversed(a)), s, s_)

互联网公司笔试题/经典算法_所有可能出栈顺序.py

经典算法_所有可能出栈顺序.py


import tkinter as tk
import random
import copy

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.mains = {'暴击':1.555, '暴伤': 3.1, '精通':9.33, '充能':2.6, '大攻击':2.333, '大防御':2.9, '大生命':2.333, '小攻击':15.56, '小生命':239,
                      '治疗':1.797,'火伤':2.333, '水伤':2.333, '雷伤':2.333, '冰伤':2.333, '草伤':2.333, '风伤':2.333, '岩伤':2.333, '物伤':2.9}
        self.subs = {'暴击':0.389, '暴伤':0.777, '精通':2.331, '充能':0.648, '大攻击':0.583, '大防御':0.729, '大生命':0.583, '小攻击':1.945, '小生命':29.875, '小防御':2.315}
        self.enhance_flag = -1
        self.possible_stats = ['暴击', '暴伤', '精通', '充能', '大攻击', '大防御', '大生命', '小攻击', '小生命', '小防御']
        self.main_stat = None
        self.main_stat_value = 0
        self.sub_stats = []
        self.sub_stat_values = []
        self.equipment_type = None
        self.types = ["生之花", "死之羽", "时之沙", "空之杯", "理之冠"]

        self.initialize = tk.Button(self)
        self.initialize["text"] = "初始化装备"
        self.initialize["command"] = self.initialize_equipment
        self.initialize.pack(side="top")

        self.enhance = tk.Button(self)
        self.enhance["text"] = "强化装备"
        self.enhance["command"] = self.enhance_equipment
        self.enhance.pack(side="top")

        self.reset = tk.Button(self)
        self.reset["text"] = "重置装备"
        self.reset["command"] = self.reset_equipment
        self.reset.pack(side="top")

        self.output = tk.Text(self, height=10, width=50)
        self.output.pack(side="bottom")

    def initialize_equipment(self):
        self.enhance_flag = 0
        self.possible_stats = ['暴击', '暴伤', '精通', '充能', '大攻击', '大防御', '大生命', '小攻击', '小生命', '小防御']
        self.main_stat = None
        self.sub_stats = []
        self.sub_stat_values = []
        self.equipment_type = None
        self.types = ["生之花", "死之羽", "时之沙", "空之杯", "理之冠"]

        self.equipment_type = random.choice(self.types)

        if self.equipment_type == "生之花":
            self.main_stat = "小生命"
        elif self.equipment_type == "死之羽":
            self.main_stat = "小攻击"
        elif self.equipment_type == "时之沙":
            items_shalou = ['精通', '充能', '大攻击', '大防御', '大生命']
            weights_shalou = [3, 3, 8, 8, 8]
            sum1 = sum(weights_shalou)
            choice = random.uniform(0, sum1)
            for i in range(len(weights_shalou)):
                choice -= weights_shalou[i]
                if choice <= 0:
                    self.main_stat = items_shalou[i]
                    break
        elif self.equipment_type == "空之杯":
            items_beizi = ['大攻击', '大防御', '大生命', '属伤', '精通']
            weights_beizi = [19.175, 19.15, 19.175, 40, 2.5]
            sum1 = sum(weights_beizi)

            choice = random.uniform(0, sum1)
            for i in range(len(weights_beizi)):
                choice -= weights_beizi[i]
                if choice <= 0:
                    self.main_stat = items_beizi[i]
                    if self.main_stat == '属伤':
                        self.main_stat = random.choice(['火伤', '水伤', '雷伤', '冰伤', '草伤', '风伤', '岩伤', '物伤'])
                    break
        elif self.equipment_type == "理之冠":
            items_tou = ['大攻击', '大防御', '大生命', '暴击', '暴伤', '治疗', '精通']
            weights_tou = [11, 11, 11, 5, 5, 5, 2]
            sum1 = sum(weights_tou)
            choice = random.uniform(0, sum1)
            for i in range(len(weights_tou)):
                choice -= weights_tou[i]
                if choice <= 0:
                    self.main_stat = items_tou[i]
                    break

        self.possible_sub_stats = [stat for stat in self.possible_stats if stat != self.main_stat]
        self.possible_sub_stats_weight = [3, 3, 4, 4, 4, 4, 4, 6, 6, 6]
        if self.main_stat in '暴击暴伤':self.possible_sub_stats_weight.remove(3)
        elif self.main_stat in '精通充能大攻击大防御大生命':self.possible_sub_stats_weight.remove(4)
        elif self.main_stat in '小攻击小生命小防御':self.possible_sub_stats_weight.remove(6)

        num_of_sub_stats = random.choice([3, 3, 3, 3, 4])
        for i in range(num_of_sub_stats):
            sum2 = sum(self.possible_sub_stats_weight)
            choice = random.uniform(0, sum2)
            for j in range(len(self.possible_sub_stats_weight)):
                choice -= self.possible_sub_stats_weight[j]
                if choice <= 0:
                    self.sub_stats.append(self.possible_sub_stats[j])
                    self.sub_stat_values.append(random.choice([7, 8, 9, 10]))
                    self.possible_sub_stats.remove(self.possible_sub_stats[j])
                    self.possible_sub_stats_weight.remove(self.possible_sub_stats_weight[j])
                    break

        self.main_stat_value = 3

        m = copy.deepcopy(self.main_stat)
        mv = copy.deepcopy(self.main_stat_value)
        s = copy.deepcopy(self.sub_stats)
        sv = copy.deepcopy(self.sub_stat_values)
        et = copy.deepcopy(self.equipment_type)

        self.initial_values = {
            "main_stat": m,
            "main_stat_value": mv,
            "sub_stats": s,
            "sub_stat_values": sv,
            "equipment_type": et
        }
        self.update_equipment_status()

    def enhance_equipment(self):
        if self.enhance_flag < 5 and self.enhance_flag >=0 :
            self.main_stat_value += 3.4
            if len(self.sub_stats) == 3:
                sum2 = sum(self.possible_sub_stats_weight)
                choice = random.uniform(0, sum2)
                for j in range(len(self.possible_sub_stats_weight)):
                    choice -= self.possible_sub_stats_weight[j]
                    if choice <= 0:
                        self.sub_stats.append(self.possible_sub_stats[j])
                        self.sub_stat_values.append(random.choice([7, 8, 9, 10]))
                        break
            elif len(self.sub_stats) == 4:
                chosen_sub_stat = random.choice(range(4))
                self.sub_stat_values[chosen_sub_stat] += random.choice([7, 8, 9, 10])
            self.enhance_flag += 1
        self.update_equipment_status()

    def reset_equipment(self):
        if self.enhance_flag == -1:
            self.update_equipment_status()
        else:
            self.enhance_flag = 0
            self.main_stat = self.initial_values["main_stat"]
            self.main_stat_value = self.initial_values["main_stat_value"]
            self.sub_stats = copy.deepcopy(self.initial_values["sub_stats"])
            self.sub_stat_values = copy.deepcopy(self.initial_values["sub_stat_values"])
            self.equipment_type = self.initial_values["equipment_type"]
            self.update_equipment_status()

    def update_equipment_status(self):
        self.output.delete(1.0, tk.END)
        if self.enhance_flag == -1:
            self.output.insert(tk.END, f"尚未初始化装备")

        if self.main_stat and self.sub_stats and self.equipment_type and self.enhance_flag != -1:
            self.output.insert(tk.END, f"装备类型: {self.equipment_type}\n")
            self.output.insert(tk.END, f"主词条: {self.main_stat} {self.main_stat_value*(self.mains[self.main_stat])}\n")
            for i in range(len(self.sub_stats)):
                self.output.insert(tk.END, f"副词条{i+1}: {self.sub_stats[i]} {self.sub_stat_values[i]*(self.subs[self.sub_stats[i]])}\n")
            if self.enhance_flag>4:
                self.output.insert(tk.END, f"已达强化次数上限")

        else:
            self.output.insert(tk.END, "请先初始化装备\n")

root = tk.Tk()
root.title('圣遗物强化模拟器v1')
app = Application(master=root)
app.mainloop()

圣遗物强化模拟器v1.py

圣遗物强化模拟器v1.py


#!/usr/bin/env python2.6

import datetime
import os
import operator
from optparse import OptionParser
import traceback
import subprocess

import dateutil.parser as dup
import spacepy.toolbox as tb

from dbprocessing import dbprocessing
from dbprocessing.runMe import ProcessException
from dbprocessing import runMe, Utils, DButils

"""
This code, Runner.py, is used to demo run codes for certain dates out of the database

THis primarily used in testing can also be used to reprocess files as needed

"""

if __name__ == "__main__":
    usage = \
    """
    Usage: %prog -m db process_id
    """
    parser = OptionParser(usage=usage)
    parser.add_option("-d", "--dryrun", dest="dryrun", action="store_true",
                      help="dryrun, only print what would be done", default=False)
    parser.add_option("-v", "--version", dest="version", type='str',
                      help="NOTIMPLEMENTED set output version", default='1.0.0')
    parser.add_option("-m", "--mission", dest="mission",
                      help="selected mission database", default=None)
    parser.add_option("", "--echo", dest="echo", action="store_true",
                      help="Start sqlalchemy with echo in place for debugging", default=False)
    parser.add_option("-s", "--startDate", dest="startDate", type="string",
                      help="Date to start search (e.g. 2012-10-02 or 20121002)", default=None)
    parser.add_option("-e", "--endDate", dest="endDate", type="string",
                      help="Date to end search (e.g. 2012-10-25 or 20121025)", default=None)
    parser.add_option("", "--nooptional", dest="optional", action="store_false",
                      help="Do not include optional inputs", default=True) # logic is backwards
    parser.add_option("-n", "--num-proc", dest="numproc", type='int',
                      help="Number of processes to run in parallel", default=1)

    (options, args) = parser.parse_args()
    if len(args) != 1:
        parser.error("incorrect number of arguments")

    if options.startDate is not None:
        startDate = dup.parse(options.startDate)
    else:
        startDate = datetime.datetime(2012, 8, 30)
    if options.endDate is not None:
        endDate = dup.parse(options.endDate)
    else:
        endDate = datetime.datetime.now()

    if endDate < startDate:
        parser.error("endDate must be >= to startDate")

    pq = dbprocessing.ProcessQueue(options.mission, dryrun=options.dryrun, echo=options.echo)

    dates = Utils.expandDates(startDate, endDate)
    print('dates', list(dates))

    inproc = args[0]

    # get the input products for a process
    products = pq.dbu.getInputProductID(inproc)
    print('products', products)

    runme = []
    for d in dates:
        print("Processing date: {0}".format(d))
        # we need a file_id that goes into the process
        input_files = []
        for p, opt in products:
            prod_ = pq.dbu.getEntry('Product', p)
            print("    Processing product: {0} : {1}".format((p,opt), prod_.product_name))
            files = pq.dbu.getFilesByProductDate(p, [d]*2, newest_version=True)
            if files:
                fnames = [v.filename for v in files]
            else:
                fnames = []
            print("        Found files: {0}".format(list(fnames)))

            #files = [v.file_id for v in files]
            if files:
                input_files.extend([v.file_id for v in files])
            if not files:
                print("No process to run for {0}".format(d.isoformat()))
                continue
            #files, input_prods = pq._getRequiredProducts(inproc, files[0].filename, d)
        if not input_files:
            print("No files to run for process ({0}) {1} on {2}".format(inproc,
                                                                      pq.dbu.getEntry('Process', inproc).process_name,
                                                                      d.isoformat()))
        else:
            runme.append(runMe.runMe(pq.dbu, d, inproc, input_files, pq, force=True))
    runMe.runner(runme, pq.dbu, MAX_PROC=options.numproc, rundir='.')


from keras.models import Sequential
from keras.layers import Conv2D,MaxPooling2D
from keras.layers import Activation,Flatten,Dense,Dropout
from keras.utils import np_utils
from keras.datasets import mnist
from keras.callbacks import EarlyStopping, ModelCheckpoint, TensorBoard
import time, datetime, os
import matplotlib.pyplot as plt

#加载数据
(X_train,y_train),(X_test,y_test) = mnist.load_data()
#数据形态
#print(X_train.shape)
#print(y_train.shape)
#print(X_test.shape)
#print(y_test.shape)
#打印数据
#plt.imshow(X_train[1])
log_dir = './log_dir'
os.mkdir(log_dir)

es = EarlyStopping(monitor='val_acc', patience=20)
mc = ModelCheckpoint(log_dir + '\\CIFAR10-EP-{epoch:02d}-ACC-{val_acc:.4f}.h5',
                     monitor='val_acc', save_best_only=True)
tb = TensorBoard(log_dir=log_dir, histogram_freq=1, write_graph=True, write_images=False, embeddings_freq=0,
                 embeddings_layer_names=None, embeddings_metadata=None)

#数据预处理，1为通道数
X_train = X_train.reshape(X_train.shape[0], 28, 28 ,1)
X_test = X_test.reshape(X_test.shape[0], 28 ,28, 1)

X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
#对标签进行预处理,每个样本维度为10
y_train = np_utils.to_categorical(y_train, 10)
y_test = np_utils.to_categorical(y_test, 10)

#定义模型架构
model = Sequential() #初始化模型
#添加卷积层，32,3,3 通道数32、3*3的kenerl
model.add(Conv2D(32,(3,3),input_shape=(28,28,1)))
#添加激活层
model.add(Activation('relu'))
#添加卷积层，不需要在输入input_shape
model.add(Conv2D(32,(3,3)))
#添加激活层
model.add(Activation('relu'))
#添加池化层
model.add(MaxPooling2D(pool_size=(2,2),strides = 2))
#添加全连接层
model.add(Flatten()) #展开层,将图片像素展开
model.add(Dense(128,activation='relu'))#全连接层，128个神经元
#dropout层
model.add(Dropout(0.5))
#添加全连接层
model.add(Dense(10,activation='softmax'))

#编译，误差损失：categorical_crossentropy，优化方法：adam， 评估方法：accuracy
model.compile(loss = 'categorical_crossentropy',
              optimizer = 'adam',
              metrics = ['accuracy'])

#训练
history = model.fit(X_train,
             y_train,
            batch_size=32,
            epochs=15,
            verbose=1,
            validation_split=0.2,
		    callbacks = [es, mc, tb])

#评估
score = model.evaluate(X_test,y_test, verbose=1)
print(score[0],score[1])
model.save("./MNIST_MODEL_{0}.h5".format(score[1]))

acc, loss = history.history['acc'], history.history['loss']
val_acc, val_loss = history.history['val_acc'], history.history['val_loss']
epoch = len(acc)
#summarize history for accuracy
plt.figure(figsize=(17, 5))
plt.subplot(121)
plt.plot(range(epoch), acc, label='Train')
plt.plot(range(epoch), val_acc, label='Test')
plt.title("Model Accuracy over " + str(epoch) + ' Epochs', size=15)
plt.ylabel("accuracy")
plt.xlabel("epoch")
plt.legend()
plt.grid(True)
plt.savefig("PerformanceAccuracy_"+str(score[1])+".png")

#summarize history for loss
plt.figure(figsize=(17, 5))
plt.subplot(121)
plt.plot(range(epoch), loss, label='Train')
plt.plot(range(epoch), val_loss, label='Test')
plt.title("Model Loss over " + str(epoch) + ' Epochs', size=15)
plt.ylabel("loss")
plt.xlabel("epoch")
plt.legend()
plt.grid(True)
plt.savefig("PerformanceLoss_"+str(score[1])+".png")
plt.show()


from twilio.rest import Client
import requests
import os

STOCK_NAME = "TSLA"
COMPANY_NAME = "Tesla Inc"

STOCK_ENDPOINT = "https://www.alphavantage.co/query"
NEWS_ENDPOINT = "https://newsapi.org/v2/everything"

ACCOUNT_SID = os.environ["TWILIO_SID"]
AUTH_TOKEN = os.environ["TWILIO_AUTH"]
SENDER_PHONE_NUMBER = os.environ["S_NO"]
RECIEVER_PHONE_NUMBER = os.environ["R_NO"]

STOCK_API_KEY = os.environ["S_KEY"]
NEWS_API_KEY = os.environ["N_KEY"]

stock_params = {
    "function": "TIME_SERIES_DAILY_ADJUSTED",
    "symbol": STOCK_NAME,
    "apikey": STOCK_API_KEY
}

response = requests.get(STOCK_ENDPOINT, stock_params)

data = response.json()["Time Series (Daily)"]

data_list = [value for (key, value) in data.items()]
yesterday_data = data_list[0]
yesterday_market_close = yesterday_data["4. close"]

day_before_yesterday = data_list[1]
day_before_yesterday_close = day_before_yesterday["4. close"]

difference = abs(float(yesterday_market_close)-float(day_before_yesterday_close))

if difference > 0:
    up_down = "ðŸ”º"
else:
    up_down = "ðŸ”»"

percentage_difference = round((difference/float(yesterday_market_close))*100)

if percentage_difference > 2:
    news_params = {
        "apiKey": NEWS_API_KEY,
        "qInTitle": COMPANY_NAME
    }
    news_response = requests.get(NEWS_ENDPOINT, news_params)
    articles = news_response.json()["articles"]

    three_articles = articles[:3]

    formatted_articles = [f"{STOCK_NAME}: {up_down}{percentage_difference}%\nHeadline: {article['title']}. \nBreif: {article['description']}" for article in three_articles]

    client = Client(ACCOUNT_SID, AUTH_TOKEN)

    for article in formatted_articles:
        message = client.messages.create(
            body= article,
            from_= SENDER_PHONE_NUMBER,
            to= RECIEVER_PHONE_NUMBER
        )


import numpy as np
import interior_point_method as ipm
import matplotlib.pyplot as plt

class objective_function:
    def __init__(self, C, var_dim, constraint_num):
        self.C = C
        self.w_dim = var_dim
        self.z_dim = constraint_num
        self.dim = self.w_dim + self.z_dim + 1  # aの次元 + ξの次元 + bの次元

    def func(self, w_z_b):
        return 0.5 * w_z_b[:self.w_dim] @ w_z_b[:self.w_dim] + \
        self.C * np.sum(w_z_b[self.w_dim:self.w_dim + self.z_dim])

    def grad(self, w_z_b):
        return np.concatenate([w_z_b[:self.w_dim],
                               self.C * np.ones(self.z_dim),
                               [0]])

    def hessian(self, w_z_b):
        H = np.zeros((self.dim, self.dim))
        H[:self.w_dim, :self.w_dim] = np.eye(self.w_dim)
        return H

class constraint_factory:
    def __init__(self, X, y):
        self.X = X
        self.y = y

    class function_constraint:
        def __init__(self, X, y, idx):
            self.idx = idx
            self.X = X
            self.y = y
            self.w_dim = X.shape[1]
            self.z_dim = y.shape[0]
            self.dim = self.w_dim + self.z_dim + 1

        def func(self, w_z_b):
            return -w_z_b[self.w_dim + self.idx] + 1 - \
                    self.y[self.idx] * \
                    (w_z_b[:self.w_dim] @ self.X[self.idx, :] + w_z_b[-1])

        def grad(self, w_z_b):
            return np.concatenate([-self.X[self.idx, :] * self.y[self.idx],
                                    np.ones(self.z_dim) * -1,
                                    [-self.y[self.idx]]])

        def hessian(self, w_z_b):
            return np.zeros((self.dim, self.dim))

    def create(self, idx):
        return self.function_constraint(self.X, self.y, idx)

class positive_constraint_factory:
    def __init__(self, X, y):
        self.constraint_num = y.shape[0]
        self.var_dim = X.shape[1]
        self.constraint_num = y.shape[0]
        self.dim = self.var_dim + self.constraint_num + 1

    class function_positive_constraint:
        def __init__(self, var_dim, constraint_num, idx):
            self.idx = idx
            self.w_dim = var_dim
            self.z_dim = constraint_num
            self.dim = self.w_dim + self.z_dim + 1

        def func(self, w_z_b):
            return -w_z_b[self.w_dim + self.idx]

        def grad(self, w_z_b):
            res = np.zeros(self.dim)
            res[self.w_dim + self.idx] = -1
            return res

        def hessian(self, w_z_b):
            return np.zeros((self.dim, self.dim))

    def create(self, idx):
        return self.function_positive_constraint(self.var_dim,
                                                self.constraint_num, idx)

class svm:
    def __init__(self, C):
        self.C = C

    def fit(self, X, y):
        cf = constraint_factory(X, y)
        pcf = positive_constraint_factory(X, y)
        P = ipm.problem(objective_function(self.C, X.shape[1], y.shape[0]),
                    [cf.create(i) for i in range(y.shape[0])] +
                    [pcf.create(i) for i in range(y.shape[0])],
                    [])
        w_z_b_opt, s_opt, u_opt, v_opt = ipm.interior_point(P, eps = 1e-12)
        self.w = w_z_b_opt[:X.shape[1]]
        self.b = w_z_b_opt[-1]
        self.u = u_opt
        self.support_vectors = np.where(self.u[:X.shape[0]] > 1e-4)

    def predict(self, X):
        return np.sign(X @ self.w + self.b)

def main():
    np.random.seed(1111)
    N_p = 40
    mu_p = [3, 20]
    Sig_p = [[8, 4],[4, 7]]
    X_p = np.random.multivariate_normal(mu_p, Sig_p, N_p)

    N_m = 40
    mu_m = [5, 15]
    Sig_m = [[8, -4],[-4, 7]]
    X_m = np.random.multivariate_normal(mu_m, Sig_m, N_m)

    X_train = np.concatenate([X_p[:N_p//2, :], X_m[:N_m//2, :]], axis = 0)
    y_train = np.ones(N_m//2 + N_p//2)
    y_train[N_p//2:] = -1
    sv = svm(50)
    X_test = np.concatenate([X_p[N_p//2:, :], X_m[N_m//2:, :]], axis = 0)
    sv.fit(X_train, y_train)
    y_pred = sv.predict(X_test)
    print(y_pred)

    plt.scatter(X_train[y_train == 1, 0],
                X_train[y_train == 1, 1],
                color = 'r', label='train data (y = 1)')
    plt.scatter(X_train[y_train == -1, 0],
                X_train[y_train == -1, 1],
                color = 'b', label='train data (y = -1)')

    x = np.array([-100 , 0, 100])
    y = -(sv.w[0] * x + sv.b)/sv.w[1]
    plt.plot(x, y, color = 'g')
    plt.xlim(-5, 15)
    plt.ylim(0, 30)

    plt.scatter(X_test[y_pred == 1, 0],
                X_test[y_pred == 1, 1],
                color = 'r', marker='x', label='test data (pred = 1)')
    plt.scatter(X_test[y_pred == -1, 0],
                X_test[y_pred == -1, 1],
                color = 'b', marker='x', label='test data (pred = -1)')

    plt.scatter(X_train[sv.support_vectors, 0],
                X_train[sv.support_vectors, 1], color = 'g',
                marker='^', label = 'support vector')

    plt.legend()
    plt.show()

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
"""
Created on Fri Apr 28 21:24:35 2023

@author: yan
"""
import pandas as pd
import numpy as np
import datetime
from pyecharts.charts import *
from pyecharts.globals import ThemeType
from pyecharts.charts import Bar
from pyecharts.charts import Funnel
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot

#导入数据
df = pd.read_csv("D:/YNUFE/研一下/数据科学与工程/yanyan/data.csv",index_col=None)
df.info()

df.head()

df.describe()

df.drop_duplicates(inplace=True)
df.head()

df.isnull().sum()

df['time']=pd.to_datetime(df['time'])
df.info()

df['date'] = df.time.dt.date
df['hour'] = df.time.dt.hour
df.head()

df['user_id'] = df.user_id.values.astype('str')
df['item_id'] = df.item_id.values.astype('str')
df['behavior_type'] = df.behavior_type.values.astype('str')
df.info()

df['item_category'] = df.item_category.values.astype('str')

#提取数据
pv_day = df[df.behavior_type =='1'].groupby('date')['behavior_type'].count()
uv_day = df[df.behavior_type =='1'].drop_duplicates(['user_id','date']).groupby('date')['user_id'].count()
#转换成图表所需的格式（list）
#1、日期（list.index）
date = pv_day.index
#2、pv、uv（list.values）
pv = np.around(pv_day.values/10000,decimals=2)
uv = np.around(uv_day.values/10000,decimals=2)

# 制作图表
x=list(date)
y1=pv
y2=uv
pvuv_day_line = (Line(init_opts=opts.InitOpts(theme=ThemeType.DARK)) #主题设置
       .add_xaxis(x) #x轴数据源
       .add_yaxis('pv',#图例名字
                  y1, #y1轴数据源
                  label_opts=opts.LabelOpts(is_show=False) #不显示数据标签
                 )
       .add_yaxis('uv',#图例名字
                  yaxis_index=1, #Y的双轴1号索引（区别于y1轴）
                  y_axis=y2, #y2轴数据源
                  label_opts=opts.LabelOpts(is_show=False) #不显示数据标签
                 )
        .extend_axis( #y2的轴设置
                    yaxis=opts.AxisOpts(
                                        name='uv',#轴名字
                                        min_=0,#轴起点值
                                        max_=1.6, #轴最大值
                                        interval=0.4, #轴区间间隔
                                        axislabel_opts=opts.LabelOpts(formatter="{value} 万人") #轴数据标签格式设置
                                         )
                    )
        .set_global_opts( #全局设置
                        tooltip_opts=opts.TooltipOpts(is_show=True,trigger="axis",axis_pointer_type='cross'), #随鼠标位置显示xy轴的数据、聚焦形式(交叉)
                        xaxis_opts=opts.AxisOpts(type_='category',axispointer_opts=opts.AxisPointerOpts(is_show=True,type_="shadow")),#随鼠标位置凸显x轴长条、凸显形式（阴影）
                        yaxis_opts=opts.AxisOpts(name='pv',axislabel_opts=opts.LabelOpts(formatter="{value} 万次")),#y1轴(默认轴)名字、轴数据标签格式设置
                        title_opts=opts.TitleOpts(title="每日pv和uv") #标题
                         )
            )
#pvuv_day_line.render_notebook() #展示图表
make_snapshot(snapshot, pvuv_day_line.render(), "pvuv_day_line.png")

#制作每日用户行为总量的数据表
view_day = df[df.behavior_type =='1'].groupby('date')['behavior_type'].count()
col_day = df[df.behavior_type =='2'].groupby('date')['behavior_type'].count()
add_day = df[df.behavior_type =='3'].groupby('date')['behavior_type'].count()
buy_day = df[df.behavior_type =='4'].groupby('date')['behavior_type'].count()
behavier_day = pd.merge(view_day, col_day, how='outer', on='date', suffixes=('_view', '_col')).merge(add_day, how='outer', on='date', suffixes=('_col', '_add')).merge(buy_day, how='outer', on='date', suffixes=('_add', '_buy'))
behavier_day.columns=['view','col','add','buy']
behavier_day.head()

df['date'] = pd.to_datetime(df['date'])
#提取数据
#日常各时段行为总量
daily_df = df[~df['date'].isin(['2014-12-11','2014-12-12'])]
view_hour = daily_df[daily_df.behavior_type =='1'].groupby('hour')['behavior_type'].count()
col_hour = daily_df[daily_df.behavior_type =='2'].groupby('hour')['behavior_type'].count()
add_hour = daily_df[daily_df.behavior_type =='3'].groupby('hour')['behavior_type'].count()
buy_hour = daily_df[daily_df.behavior_type =='4'].groupby('hour')['behavior_type'].count()
#双12各时段行为总量 （日期说明：因双十二开启时间为12月12号零点，活动期间用户行为主要发生时间在11号到12号，因此本次活动分析时筛选的时间为这两天）
active_df = df[df['date'].isin(['2014-12-11','2014-12-12'])]
view_active_ahour = active_df[active_df.behavior_type =='1'].groupby('hour')['behavior_type'].count()
col_active_ahour = active_df[active_df.behavior_type =='2'].groupby('hour')['behavior_type'].count()
add_active_ahour = active_df[active_df.behavior_type =='3'].groupby('hour')['behavior_type'].count()
buy_active_ahour = active_df[active_df.behavior_type =='4'].groupby('hour')['behavior_type'].count()

# 制作图表
x=col_hour.index.tolist()
#双12
y5=np.around(col_active_ahour.values/2,decimals=0).tolist()
y6=np.around(add_active_ahour.values/2,decimals=0).tolist()
y7=np.around(buy_active_ahour.values/2,decimals=0).tolist()

active_line = (Line()
       .add_xaxis(x)
       .add_yaxis('收藏',
                  y_axis=y5,
                  label_opts=opts.LabelOpts(is_show=False)
                 )
       .add_yaxis('加购',
                  y_axis=y6,
                  label_opts=opts.LabelOpts(is_show=False)
                 )
       .add_yaxis('购买',
                  y_axis=y7,
                  label_opts=opts.LabelOpts(is_show=False)
                 )
        .set_global_opts(
            tooltip_opts=opts.TooltipOpts(is_show=True,trigger="axis",axis_pointer_type='cross'),
            legend_opts=opts.LegendOpts(pos_top='5%'),
            xaxis_opts=opts.AxisOpts(type_='category',axispointer_opts=opts.AxisPointerOpts(is_show=True,type_="shadow")),
            yaxis_opts=opts.AxisOpts(name='人次',
                                     axislabel_opts=opts.LabelOpts(formatter="{value}")),
            title_opts=opts.TitleOpts(title="双12日均各时段用户行为",pos_top='0%')
                        )
            )

#active_line.render_notebook()
make_snapshot(snapshot, active_line.render(), "active_line.png")
# 制作图表
x=col_hour.index.tolist()
y1=np.around(view_hour.values/29,decimals=0).tolist()
y8=np.around(view_active_ahour.values/2,decimals=0).tolist()

bar=(
    Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK))
    .add_xaxis(xaxis_data=x)
    .add_yaxis(
    "日常PV",
        y1,
        stack='stack1',
        label_opts=opts.LabelOpts(is_show=False)
    )
    .add_yaxis(
    "双12PV",
        y8,
        stack='stack1',
        label_opts=opts.LabelOpts(is_show=False)
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="日常和双12每日时段PV走势对比"),
        legend_opts=opts.LegendOpts(pos_top='5%'),
        yaxis_opts=opts.AxisOpts(name='人次',
                                axislabel_opts=opts.LabelOpts(formatter="{value}"))
    )
)

#bar.render_notebook()
make_snapshot(snapshot, bar.render(), "bar.png")

#准备数据
#日常各时段购买率
daily_df = df[~df['date'].isin(['2014-12-11','2014-12-12'])]
view_user_num = daily_df[daily_df.behavior_type =='1'].drop_duplicates(['user_id','date','hour']).groupby('hour')['behavior_type'].count()
buy_user_num = daily_df[daily_df.behavior_type =='4'].drop_duplicates(['user_id','date','hour']).groupby('hour')['behavior_type'].count()
daily_buy_rate = buy_user_num/view_user_num
#双12各时段购买率
active_df = df[df['date'].isin(['2014-12-11','2014-12-12'])]
view_active_user_num = active_df[active_df.behavior_type =='1'].drop_duplicates(['user_id','date','hour']).groupby('hour')['behavior_type'].count()
buy_active_user_num = active_df[active_df.behavior_type =='4'].drop_duplicates(['user_id','date','hour']).groupby('hour')['behavior_type'].count()
acitve_buy_rate = buy_active_user_num/view_active_user_num

x=view_hour.index.tolist()
y1 = np.around(daily_buy_rate,decimals=2).tolist()
y2 = np.around(acitve_buy_rate,decimals=2).tolist()

buy_rate_line = (Line(init_opts=opts.InitOpts(theme=ThemeType.DARK))
       .add_xaxis(x)
       .add_yaxis('日常购买率',
                  y_axis=y1,
                  label_opts=opts.LabelOpts(is_show=True)
                 )
       .add_yaxis('双12购买率',
                  y_axis=y2,
                  label_opts=opts.LabelOpts(is_show=True)
                 )
        .set_global_opts(
            tooltip_opts=opts.TooltipOpts(is_show=True,trigger="axis",axis_pointer_type='cross'),
            xaxis_opts=opts.AxisOpts(type_='category',axispointer_opts=opts.AxisPointerOpts(is_show=True,type_="shadow")),
            yaxis_opts=opts.AxisOpts(name=' ',axislabel_opts=opts.LabelOpts(formatter="{value}")),
            title_opts=opts.TitleOpts(title="不同时段购买率"),
            legend_opts=opts.LegendOpts(pos_top='5%')
                        )
            )
#buy_rate_line.render_notebook()
make_snapshot(snapshot, buy_rate_line.render(), "buy_rate_line.png")
#提取数据
#日常各时段行为数量
daily_df = df[~df['date'].isin(['2014-12-11','2014-12-12'])]
view_num = daily_df[daily_df.behavior_type =='1']['behavior_type'].count()
col_num = daily_df[daily_df.behavior_type =='2']['behavior_type'].count()
add_num = daily_df[daily_df.behavior_type =='3']['behavior_type'].count()
buy_num = daily_df[daily_df.behavior_type =='4']['behavior_type'].count()

#日常转化率
view_rate = view_num/view_num
col_rate = col_num/view_num
add_rate = add_num/view_num
buy_rate = buy_num/view_num

#设置漏斗的data_pair参数的list
behavier_labels=['浏览','加购','收藏','购买'] #行为标签
daily_behavier_rate = [np.around(view_rate*100,2), #转化率
                       np.around(add_rate*100,2),
                       np.around(col_rate*100,2),
                       np.around(buy_rate*100,2),]

daily_conversion_rate =(
            Funnel(init_opts=opts.InitOpts(theme=ThemeType.DARK))
            .add(
                series_name='用户行为',
                data_pair=[[behavier_labels[i],daily_behavier_rate[i]] for i in range(len(behavier_labels))], #list形式!!!
                gap=4, #漏斗间隔
                tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a}
{b} : {c}%",is_show=True), #工具提示的格式设置
                label_opts=opts.LabelOpts(is_show=True, position="ourside")
                )
            .set_global_opts(title_opts=opts.TitleOpts(title="日常用户转化率"))
)
#daily_conversion_rate.render_notebook()
make_snapshot(snapshot, daily_conversion_rate.render(), "daily_conversion_rate.png")

#提取数据
#双12活动各时段行为数量
active_df = df[df['date'].isin(['2014-12-11','2014-12-12'])]
view_active_num = active_df[active_df.behavior_type =='1']['behavior_type'].count()
col_active_num = active_df[active_df.behavior_type =='2']['behavior_type'].count()
add_active_num = active_df[active_df.behavior_type =='3']['behavior_type'].count()
buy_active_num  = active_df[active_df.behavior_type =='4']['behavior_type'].count()

# 活动转化率
view_active_rate = view_active_num/view_active_num
col_active_rate = col_active_num/view_active_num
add_active_rate = add_active_num/view_active_num
buy_active_rate = buy_active_num/view_active_num

#设置漏斗的data_pair参数的list
behavier_labels=['浏览','加购','收藏','购买']
active_behavier_rate =[np.around(view_active_rate*100,2),
                       np.around(add_active_rate*100,2),
                       np.around(col_active_rate*100,2),
                       np.around(buy_active_rate*100,2),]

active_conversion_rate =(
            Funnel(init_opts=opts.InitOpts(theme=ThemeType.DARK))
            .add(
                series_name='用户行为',
                data_pair=[[behavier_labels[i],active_behavier_rate[i]] for i in range(len(behavier_labels))], #list形式!!!
                gap=4, #漏斗间隔
                tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{a}
{b} : {c}%",is_show=True),
                label_opts=opts.LabelOpts(is_show=True, position="ourside")
                )
            .set_global_opts(title_opts=opts.TitleOpts(title="双12用户转化率"))
)

#active_conversion_rate.render_notebook()
make_snapshot(snapshot, active_conversion_rate.render(), "active_conversion_rate.png")

#提取数据
#1、流量：类目的pv top10
active_df = df[df['date'].isin(['2014-12-11','2014-12-12'])]
# 降序
view_active_num = active_df[active_df.behavior_type =='1'].groupby('item_category')['behavior_type'].count().sort_values(ascending=False)
# 取前十名
x1= view_active_num.index.tolist()[0:10]
y1=view_active_num.values.tolist()[0:10]

#2、购买增长率：双12类目下单量top10 及其日均下单量增长率 对比
# 双12购买数
active_df = df[df['date'].isin(['2014-12-11','2014-12-12'])]
buy_active_num =active_df[active_df.behavior_type =='4'].groupby('item_category')['behavior_type'].count().reset_index()
#日常购买数
daily_df = df[~df['date'].isin(['2014-12-11','2014-12-12'])]
buy_num = daily_df[daily_df.behavior_type =='4'].groupby('item_category')['behavior_type'].count().reset_index()
#合并
view_buy_avg = buy_active_num.merge(buy_num,how='inner',left_on='item_category',right_on='item_category')
view_buy_avg.columns=['item_category','buy_active_num','buy_num']
#计算增长率
view_buy_avg['buy_active_avg']=view_buy_avg['buy_active_num']/2
view_buy_avg['buy_avg']=view_buy_avg['buy_num']/29
view_buy_avg['growth_rate']=(view_buy_avg['buy_active_avg']-view_buy_avg['buy_avg'])/view_buy_avg['buy_avg']
# 降序
view_buy_avg.sort_values(by=['buy_active_num'],axis=0,ascending=False,inplace=True)
#取前十名
x2=view_buy_avg.item_category.tolist()[0:10]
y2=view_buy_avg.buy_active_num.tolist()[0:10]
y3=np.around(view_buy_avg.growth_rate*100,0).tolist()[0:10]

#制作图表
view_active_bar=(
    Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK))
    .add_xaxis(xaxis_data=x1)
    .add_yaxis(
    "pv",
        y1,
        color='rgb(300, 0, 100, 0.2)',
        label_opts=opts.LabelOpts(is_show=False)
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="双12商品类目pv TOP10"),
        legend_opts=opts.LegendOpts(pos_top='5%'),
        xaxis_opts=opts.AxisOpts(name=' ',name_location = "middle"),
        yaxis_opts=opts.AxisOpts(name='人次',
                                axislabel_opts=opts.LabelOpts(formatter="{value}"))
    )
)

buy_active_bar=(
    Bar(init_opts=opts.InitOpts(theme=ThemeType.PURPLE_PASSION))
    .add_xaxis(xaxis_data=x2)
    .add_yaxis(
    "下单量",
        y2,
        label_opts=opts.LabelOpts(is_show=False)
                )
     .add_yaxis('日均下单量增长率(与非活动日比较)',
                  yaxis_index=1,
                  y_axis=y3,
                  label_opts=opts.LabelOpts(is_show=False)
                 )
      .extend_axis(
            yaxis=opts.AxisOpts(
                name=' ',
                min_=100,
                max_=600,
                interval=100,
                axislabel_opts=opts.LabelOpts(formatter="{value}%")

                                )
                    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="双12商品类目下单量TOP10及日均下单量增长率",pos_top='48%'),
        legend_opts=opts.LegendOpts(pos_top='53%'),
        xaxis_opts=opts.AxisOpts(name='商品类目',name_location = "middle",name_gap=30),
        yaxis_opts=opts.AxisOpts(name=' ',is_show = True,
                                 min_=0,
                                 max_=800,
                                 interval=200,
                                 axislabel_opts=opts.LabelOpts(formatter="{value}"))

                    )
     .set_series_opts(
                    markpoint_opts=opts.MarkPointOpts(data=[ #标记最值！
                        opts.MarkPointItem(type_="min",value_index=1,name="最小值"),
                        opts.MarkPointItem(type_="max",value_index=1,name="最大值")])
                     )
                )

ggrid = (
    Grid(init_opts=opts.InitOpts(theme=ThemeType.DARK))
    .add(view_active_bar, grid_opts=opts.GridOpts(pos_bottom="60%"))
    .add(buy_active_bar, grid_opts=opts.GridOpts(pos_top="60%"))
        )

#ggrid.render_notebook()
make_snapshot(snapshot, ggrid.render(), "ggrid.png")


class Solution(object):
    def validTree(self, n, edges):
        """
        :type n: int
        :type edges: List[List[int]]
        :rtype: bool

        if there is loop, num_edges > num_vertex - 1
        if there is island, num_edges < num_vertex - 1

        Union-Find
        beats 90.69%
        """
        parent = range(n)

        def find(x):
            return x if parent[x] == x else find(parent[x])

        def union(xy):
            x, y = map(find, xy)
            parent[x] = y
            return x != y
        return len(edges) == n-1 and all(map(union, edges))

    def validTree1(self, n, edges):
        """
        :type n: int
        :type edges: List[List[int]]
        :rtype: bool

        beats 80.86%
        """
        parent = range(n)

        def find(x):
            return x if parent[x] == x else find(parent[x])

        for e in edges:
            x, y = map(find, e)
            if x == y:
                return False
            parent[x] = y
        return len(edges) == n - 1

    def validTree2(self, n, edges):
        """
        :type n: int
        :type edges: List[List[int]]
        :rtype: bool

        DFS
        beats 80.86%
        """
        if len(edges) != n - 1:
            return False
        neighbors = {i: [] for i in range(n)}
        for v, w in edges:
            neighbors[v] += w,
            neighbors[w] += v,

        def visit(v):
            map(visit, neighbors.pop(v, []))

        visit(0)
        return not neighbors

    def validTree3(self, n, edges):
        """
        :type n: int
        :type edges: List[List[int]]
        :rtype: bool

        if there is no loop, num_edges = num_vertex - 1
        if there is no island vertex left after traversal, it's a valid tree

        BFS
        beats 99.31%
        """
        if len(edges) != n - 1:
            return False
        neighbors = {i: [] for i in range(n)}
        for v, w in edges:
            neighbors[v] += w,
            neighbors[w] += v,
        queue = [0]
        for v in queue:
            queue += neighbors.pop(v, [])
        return not neighbors


from dxc import ai
import json
import sys
import pandas as pd
import pickle

def write_mongoDB():
    string = json.loads(sys.argv[1])
    connection_string = string["connection_string"]
    collection_name = string["collection_name"]
    database_name = string["database_name"]
    data_source = string["data_source"]
    cleaner = string["cleaner"]

    data_layer = {
        "connection_string": connection_string,
        "collection_name": collection_name,
        "database_name": database_name,
        "data_source":data_source,
        "cleaner":cleaner
    }

    df=pd.read_csv('clean_df_tmp.csv')

    data2 = ai.write_raw_data(data_layer, df)
    print("
 Please find the above current status of your collections:
")
    print("
 Below is the response from Mongo DB:
 ")
    print(data2)

    return data2

if __name__ == "__main__":
    write_mongoDB()


class Node:
    def __init__(self, value, parent=None):
        self.value = value
        self.parent = parent

    def __str__(self):
        s = [str(self.value)]
        node = self.parent
        while node:
            s.append(str(node.value))
            node = node.parent
        return '<-'.join(s)

def expand_leaves(node):
    v = node.value
    n = node
    while node:
        yield Node(v + node.value, n)
        node = node.parent

def BFS(n):
    res = [0] * (n + 1)
    nodes = [Node(1)]
    depth = 1

    while not all(res[2:]):
        leaves = []
        for node in nodes:
            for leaf in expand_leaves(node):
                leaves.append(leaf)
                if leaf.value <= n and not res[leaf.value]:
                    res[leaf.value] = depth
                    # print(str(leaf))
        nodes = leaves
        depth += 1

    return res

if __name__ == "__main__":
    r = BFS(200)
    # print(str(r[127]))
    print(sum(r))


import arcpy, os, numpy

path = r"C:\Users\chri6962\Verify_GN"
fc = r"c:\users\chri6962\Verify_GN\extFGDB.gdb\extGrids"

sizes = []
grids = {}

for f in os.listdir(path):
    sizes.append(os.path.getsize(os.path.join(path, f)))
    grids.update({'{}'.format(os.path.splitext(f[12:])[0]):os.path.getsize(os.path.join(path, f))})

arr = numpy.array(sizes)
mean = numpy.mean(arr)
std =  numpy.std(arr)

cur = arcpy.da.UpdateCursor(fc, ['OID@', 'Z_SCORE', 'MEAN', 'STD'])

for row in cur:
    row[1] = (grids[str(row[0])] - mean)/std
    row[2] = mean
    row[3] = std
    cur.updateRow(row)


from minecraft.minestat import  MineStat
from rest_framework.response import Response
from rest_framework.decorators import api_view
from django.views.decorators.csrf import csrf_exempt
# Create your views here.
@csrf_exempt
@api_view(['get'])
def minecraft_status(request):
    mc = MineStat('ribes.me', 53)
    if mc.online:
        print(mc.motd)
        return Response({
            "up":True,
            "motd":''.join(mc.motd.split('\u0000')),
            "players":mc.current_players,
            "max":mc.max_players})
    else:
        return Response({'detail':'Offline',
            "up":False}, status=500)

@api_view(['get']) #définit une vue gérée par DRF, et permet d'utiliser Response
def hello_world(request):
    return Response({"message":"Hello World !"})


from django.shortcuts import get_object_or_404, render, redirect
from django.http import HttpResponseRedirect
from django.template import loader
#from django.core.urlresolvers import reverse
from django.urls import reverse
from django.views import generic
from django.views.generic import DetailView
from django.views.generic import ListView
from .models import Material, AWS, Azure, Google, ContactUs, Lists, UpdateText
from django.shortcuts import render
from django.http import HttpResponse
from django.db.models import F
from django.utils import timezone
from .forms import ContactForm
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.auth.models import User
from django.contrib.auth import login
from django.views.decorators.csrf import ensure_csrf_cookie, csrf_exempt
from django.contrib import messages

#@xframe_options_exempt
def index(request):
    Intro = Material.objects.get(title='intro')
    Description = Material.objects.get(title='description')
    DescriptionPostlist = Material.objects.get(title='descriptionPostlist')
    DescriptionList = Lists.objects.filter(section__contains='whatiscloud').order_by('title_date')
    Advantage = Material.objects.get(title='advantage')
    AdvantagePostlist = Material.objects.get(title='advantagePostlist')
    AdvantageList = Lists.objects.filter(section__contains='theclearadvantages').order_by('title_date')
    Publicdis = Material.objects.get(title='publicdis')
    Privatedis = Material.objects.get(title='privatedis')
    Publicorprivate = Material.objects.get(title='publicorprivate')
    Updatetext = UpdateText.objects.order_by('-post_date')[:1]
#   Author = Material.objects.get(title='author')
    Module = "index"
    context = {
        'Module': Module,
	    'Intro': Intro,
        'Description': Description,
	    'DescriptionPostlist': DescriptionPostlist,
	    'DescriptionList': DescriptionList,
        'Advantage': Advantage,
	    'AdvantageList': AdvantageList,
	    'AdvantagePostlist': AdvantagePostlist,
        'Publicdis': Publicdis,
        'Privatedis': Privatedis,
	    'Publicorprivate': Publicorprivate,
	    'Updatetext': Updatetext,
    }
    return render(request, 'CloudasaService/index.html', context)

def virtualisation(request):
    Intro = Material.objects.get(title='history')
    Support = Material.objects.get(title='support')
    Storage = Material.objects.get(title='storage')
#   Author = Material.objects.get(title='author')
    context = {
        'Intro': Intro,
        'Support': Support,
        'Storage': Storage,
#       'Author': Author,
    }
    return render(request, 'CloudasaService/virtualisation.html', context)

def detail(request):
    Amazon = AWS.objects.order_by('title_date')[:11]
    MSA = Azure.objects.order_by('title_date')[:10]
    GCP = Google.objects.order_by('title_date')[:10]
    Publicorprivate = Material.objects.get(title='publicorprivate')
    Services = Material.objects.get(title='services')
    ServicesList = Lists.objects.filter(section__contains='services').order_by('title_date')
    ServicesPostlist = Material.objects.get(title='servicesPostlist')
    Costs = Material.objects.get(title='costs')
#   Author = Material.objects.get(title='author')
    Module = "detail"
    context = {
        'Module': Module,
        'Publicorprivate': Publicorprivate,
        'Services': Services,
        'Costs': Costs,
	'Amazon': Amazon,
	'MSA': MSA,
	'GCP': GCP,
#	'Author': Author,
	'ServicesList': ServicesList,
	'ServicesPostlist': ServicesPostlist,
    }
    return render(request, 'CloudasaService/detail.html', context)

def virtual(request):
    Network = Material.objects.get(title='network')
    History = Material.objects.get(title='history')
    Storage = Material.objects.get(title='storage')
    Scalability = Material.objects.get(title='scalability')
    Provisioning = Material.objects.get(title='provisioning')
#   Author = Material.objects.get(title='author')
    Module = "virtual"
    context = {
        'Module': Module,
        'Network': Network,
        'History': History,
        'Storage': Storage,
        'Scalability': Scalability,
        'Provisioning': Provisioning,
#	'Author': Author,
    }
    return render(request, 'CloudasaService/virtual.html', context)

def author(request):
    Author = Material.objects.get(title='author')
    Module = "author"
    context = {
        'Module': Module,
	'Author': Author,
    }
    return render(request, 'CloudasaService/author.html', context)

def contactview(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            messages.success(request, 'Message saved, thank you!')
            v_firstname = form.cleaned_data['firstname']
#           v_surname = form.cleaned_data['surname']
            v_emailaddr = form.cleaned_data['emailaddr']
            v_comment = form.cleaned_data['post']
            v_comment_date = timezone.now()
            p = ContactUs.objects.create(firstname=v_firstname, emailaddr=v_emailaddr, comment=v_comment)
#           p = ContactUs.objects.create(firstname=v_firstname, surname=v_surname, emailaddr=v_emailaddr, comment=v_comment)
#           return render(request, 'CloudasaService/contact.html')
            return HttpResponseRedirect('/')
#       return self.render_to_response(request, 'CloudasaService/contact.html', {'success':success})
    else:
            form = ContactForm()
    return render(request, 'CloudasaService/contact.html', {'form': form})

def description(request):
    Intro = Material.objects.get(title='description')
    context = {
        'Intro': Intro,
    }
    return render(request, 'CloudasaService/description.html', context)

def difference(request):
    Intro = Material.objects.get(title='whobenefits')
    context = {
        'Intro': Intro,
    }
    return render(request, 'CloudasaService/difference.html', context)

def advantage(request):
    Intro = Material.objects.get(title='advantage')
    Manageability = Material.objects.get(title='manageability')
    Scalability = Material.objects.get(title='scalability')
    context = {
        'Intro': Intro,
        'Manageability': Manageability,
        'Scalability': Scalability,
    }
    return render(request, 'CloudasaService/advantage.html', context)

def businesscontinuity(request):
    Intro = Material.objects.get(title='publicorprivate')
    context = {
        'Intro': Intro,
    }
    return render(request, 'CloudasaService/businesscontinuity.html', context)

##@xframe_options_exempt
#def index(request):
#    if not request.user.is_authenticated():
#        userauth = 'no'
#    else:
#        userauth = 'yes'
#    latest_category_list = SiteCategories.objects.order_by('-category_date').exclude(category_name = 'New Entity')
#    good_category_trend = post.objects.filter(post_rating='Good').order_by('-post_update')[:15]
#    bad_category_trend = post.objects.filter(post_rating__contains='Bad').order_by('-post_update')[:15]
#    entities_list = Entities.objects.order_by('-good_vote_count')[:15]
#   recent_posts = post.objects.order_by('-post_create_date')[:15]
#   speakers_corner = SpeakersCorner.objects.latest('post_date')
#   who_are_we = SiteSpecific.objects.get(tag='About us...')
#   topics = Topical.objects.order_by('-post_date')[:4]
#   template = loader.get_template('GnB/index.html')
#   context = {
#       'userauth': userauth,
#	'good_category_trend': good_category_trend,
#	'bad_category_trend': bad_category_trend,
#        'latest_category_list': latest_category_list,
#	'entities_list': entities_list,
#        'recent_posts': recent_posts,
#        'speakers_corner': speakers_corner,
#	'topics': topics,
#        'who_are_we': who_are_we,
#    }
#    return render(request, 'GnB/index.html', context)

def advantage(request):
    Intro = Material.objects.get(title='advantage')
    Manageability = Material.objects.get(title='manageability')
    Scalability = Material.objects.get(title='scalability')
    context = {
        'Intro': Intro,
        'Manageability': Manageability,
        'Scalability': Scalability,
    }
    return render(request, 'CloudasaService/advantage.html', context)

def businesscontinuity(request):
    Intro = Material.objects.get(title='businesscontinuity')
    context = {
        'Intro': Intro,
    }
    return render(request, 'CloudasaService/businesscontinuity.html', context)

##@xframe_options_exempt
#def index(request):
#    if not request.user.is_authenticated():
#        userauth = 'no'
#    else:
#        userauth = 'yes'
#    latest_category_list = SiteCategories.objects.order_by('-category_date').exclude(category_name = 'New Entity')
#    good_category_trend = post.objects.filter(post_rating='Good').order_by('-post_update')[:15]
#    bad_category_trend = post.objects.filter(post_rating__contains='Bad').order_by('-post_update')[:15]
#    entities_list = Entities.objects.order_by('-good_vote_count')[:15]
#   recent_posts = post.objects.order_by('-post_create_date')[:15]
#   speakers_corner = SpeakersCorner.objects.latest('post_date')
#   who_are_we = SiteSpecific.objects.get(tag='About us...')
#   topics = Topical.objects.order_by('-post_date')[:4]
#   template = loader.get_template('GnB/index.html')
#   context = {
#       'userauth': userauth,
#	'good_category_trend': good_category_trend,
#	'bad_category_trend': bad_category_trend,
#        'latest_category_list': latest_category_list,
#	'entities_list': entities_list,
#        'recent_posts': recent_posts,
#        'speakers_corner': speakers_corner,
#	'topics': topics,
#        'who_are_we': who_are_we,
#    }
#    return render(request, 'GnB/index.html', context)


import dash
from dash import html, dcc, Input, Output
import dash_bootstrap_components as dbc
import plotly.express as px
import pandas as pd

dataframe = pd.read_csv('assets/product_dashboard_data.csv')

sunburst_column_map = {
    'Ratings': {'column': 'overall'},
    'Sentiment': {'column': 'sentiment'}
}

bar_column_map = {
    'Product Category': {'column': 'product'},
    'Ratings': {'column': 'overall'},
    'Sentiment': {'column': 'sentiment'}
}

sentiment_map = {1: 'Positive', 0: 'Negative'}

def generate_product_sunburst():
    dropdown_labels = list(sunburst_column_map.keys())
    return html.Div(children=[
        html.Label('Select one'),
        dcc.Dropdown(dropdown_labels, dropdown_labels[0], id='product-sunburst-dropdown', clearable=False),
        dcc.Graph(id='product-sunburst-graph')
    ])

@dash.callback(Output('product-sunburst-graph', 'figure'), Input('product-sunburst-dropdown', 'value'))
def update_figure(selected_label):
    selected_column = sunburst_column_map[selected_label]['column']

    df_grouped = dataframe.groupby(['product', selected_column]).agg(
        Count=pd.NamedAgg(column=selected_column, aggfunc="count")
    ).reset_index()

    if selected_column == 'sentiment':
        df_grouped['sentiment'] = df_grouped['sentiment'].map(sentiment_map)

    fig = px.sunburst(
        df_grouped,
        path=['product', selected_column],
        values='Count',
        title=f"Division of {selected_label} among product categories",
        width=500,
        height=500
    )
    fig.update_layout(
        font=dict(
            family="PT Sans Narrow",
            size=11,
            color="RebeccaPurple"
        )
    )

    return fig

def generate_feature_bars():
    dropdown_labels = list(bar_column_map.keys())
    return html.Div(children=[
        html.Label('Select one'),
        dcc.Dropdown(dropdown_labels, dropdown_labels[0], id='feature-bar-dropdown', clearable=False),
        dcc.Graph(id='feature-bar-graph')
    ])

@dash.callback(Output('feature-bar-graph', 'figure'), Input('feature-bar-dropdown', 'value'))
def update_figure(selected_label):
    selected_column = bar_column_map[selected_label]['column']

    df_grouped = dataframe.groupby(
        [selected_column])[['review_length', 'token_count', 'stopword_count', 'punctuation_count', 'lemma_count']]\
            .sum()\
            .unstack()\
            .reset_index()\
            .rename(columns={'level_0': 'Feature', 0: 'Sum'})

    if selected_column == 'sentiment':
        df_grouped['sentiment'] = df_grouped['sentiment'].map(sentiment_map)

    fig = px.bar(
            df_grouped,
            x='Sum',
            y='Feature',
            color=selected_column,
            orientation='h',
            barmode='group',
            title=f"Sum of features for each {selected_label} group",
            width=500,
            height=500,
            labels={"Sum":"Sum of features", "Feature": "Feature", selected_column: selected_label}
        )
    fig.update_layout(
        font=dict(
            family="PT Sans Narrow",
            size=11,
            color="RebeccaPurple"
        )
    )

    return fig


#!/usr/bin/python
import RPIO as GPIO
import time

def init(pin):
	GPIO.setup(pin, GPIO.OUT);
	GPIO.output(pin, GPIO.LOW);
	time.sleep(0.002);
	GPIO.output(pin, GPIO.HIGH);
	time.sleep(0.000030);
	GPIO.setup(pin, GPIO.IN);
	time.sleep(0.000020);
	assert GPIO.input(pin) == True, "Sensor did not pull line low after host start signal";
	time.sleep(0.000080);
	assert GPIO.input(pin) == False, "Sensor did not pull line high after host start signal";
	time.sleep(0.000060);
	return;

def receiveBit(pin):
	GPIO.setup(pin, GPIO.IN);
	assert GPIO.input(pin) == False, "Sensor did not pull line low before bit transmission";

	time.sleep(0.000077);

	if GPIO.input(pin):
		time.sleep(0.000041);
		return "1";
	else:
		return "0";

def getData(pin):
    dataStr = "";
    init(pin);
    time.sleep(0.000025);

    for i in range(40):
        dataStr += receiveBit(pin);

    # check checksum

    checksum = int(dataStr[32:], 2);
    check_against = int(dataStr[:8], 2) + \
                    int(dataStr[8:16], 2) + \
                    int(dataStr[16:24], 2) + \
                    int(dataStr[24:32], 2);

    check_against &= 0xff;

    error_str = "Checksum error: " + checksum + " != " + \
            check_against + " ; Data = " + dataStr;
    assert checksum == check_against, error_str;

    return dataStr;

def extractTemp(dataStr):
    temp_part = dataStr[17:32];
    temp = int(temp_part, 2) / 10;
    if dataStr[16] == "1":
        temp *= -1;
    return temp;

def extractRH(dataStr):
    rh_part = dataStr[:16];
    return int(rh_part, 2) / 10;

GPIO.cleanup();
GPIO.setmode(GPIO.BOARD);

data = getData(3);
print(extractTemp(data));
print(extractRH(data));

GPIO.cleanup();


import os

def commit_gspan_input(inputfile, outputfile, i, last_graph):
    print("Creating input for gspan for commits of projects")
    with open(inputfile) as infile:
        relationships = [l.strip() for l in infile.readlines()]

    with open(outputfile, 'a+') as outfile:
        outfile.write("t # " + str(i) + "\n")
        s1 = ""
        s2 = ""
        s3 = ""
        for line in relationships:
            lineSegments = line.split()

            if line.startswith("-"):
                # Type of the edge between two class nodes which corresponds to the type of the relationship between them
                if lineSegments[3] == "inherits": s2 = "2"
                elif lineSegments[3] == "uses": s2 = "3"
                elif lineSegments[3] == "calls": s2 = "4"
                elif lineSegments[3] == "creates": s2 = "5"
                elif lineSegments[3] == "has": s2 = "6"
                elif lineSegments[3] == "references": s2 = "7"
                else: print("Something strange happened!")

            elif line.startswith("+"):
                # Type of the edge between two class nodes which corresponds to the type of the relationship between them
                if lineSegments[3] == "inherits": s2 = "8"
                elif lineSegments[3] == "uses": s2 = "9"
                elif lineSegments[3] == "calls": s2 = "10"
                elif lineSegments[3] == "creates": s2 = "11"
                elif lineSegments[3] == "has": s2 = "12"
                elif lineSegments[3] == "references": s2 = "13"
                else: print("Something strange happened!")

            else:
                print("Something strange happened!")

            # Shape of the class node which corresponds to its abstraction level
            if lineSegments[1] == "Normal": s1 = "2"
            elif lineSegments[1] == "Unknown": s1 = "3"
            elif lineSegments[1] == "Abstract": s1 = "4"
            elif lineSegments[1] == "Interface": s1 = "5"
            elif lineSegments[1] == "Abstracted": s1 = "6"
            elif lineSegments[1] == "Any": s1 = "7"
            else: print("Something strange happened!")

            # Shape of the class node which corresponds to its abstraction level
            if lineSegments[4] == "Normal": s3 = "2"
            elif lineSegments[4] == "Unknown": s3 = "3"
            elif lineSegments[4] == "Abstract": s3 = "4"
            elif lineSegments[4] == "Interface": s3 = "5"
            elif lineSegments[4] == "Abstracted": s3 = "6"
            elif lineSegments[4] == "Any": s3 = "7"
            else: print("Something strange happened!")

            outfile.write("v " + lineSegments[2] + " " + s1 + "\n")
            outfile.write("v " + lineSegments[5] + " " + s3 + "\n")
            outfile.write("e " + lineSegments[2] + " " + lineSegments[5] + " " + s2 + "\n")
        if last_graph == True: outfile.write("t # -1")
        print("Done!")

if __name__ == '__main__':
    """ This main function is not meant to be run. It is used only for testing. """
    commit_relationships = "./other_results/"
    last_graph = False
    outputfile = os.path.join("./graphs_of_commits_8+_40-.txt")
    if os.path.exists(commit_relationships):
        for counter, infile in enumerate(os.listdir(commit_relationships)):
            if counter == len(os.listdir(commit_relationships)) - 1:
                last_graph = True
            commit_gspan_input(os.path.join(commit_relationships, infile), outputfile, counter, last_graph)
    else:
        print("Commit relationships have not been extracted yet!")


"""
Задание 1. Поработайте с переменными, создайте несколько,
выведите на экран.
Запросите у пользователя некоторые числа и строки и сохраните
в переменные, а затем выведите на экран.
"""
name = 'Виталий'
subname = 'Дроздов'
age = '32'

print(name, subname, age)
string1 = input("Введите первую строку ")
number1 = int(input("Введите первое число "))
string2 = input("Введите вторую строку ")
number2 = int(input("Введите второе число "))
print("Введеные значения - %10s %5d %10s %5d" % (string1, number1, string2, number2))

"""
Задание 2.Пользователь вводит время в секундах.
Рассчитайте время и сохраните отдельно в каждую
переменную количество часов, минут и секунд.
Переведите время в часы, минуты, секунды и сохраните в отдельных переменных.
"""

time = int(input("Введите время в секундах "))
hours = time // 3600
minutes = (time - hours * 3600) // 60
seconds = time - (hours * 3600 + minutes * 60)
print(f"Время в формате чч:мм:сс   {hours} : {minutes} : {seconds}")

"""
Задание 3. Запросите у пользователя через консоль число n (от 1 до 9).
Найдите сумму чисел n + nn + nnn.
"""

n = int(input("Введите число - "))
total = (n + int(str(n) + str(n)) + int(str(n) + str(n)+ str(n)))
print("Сумма чисел n + nn + nnn - %d" % total)


from nltk.tokenize import sent_tokenize

def lines(a, b):
    a = set(a.split('\n'))
    b = set(b.split('\n'))
    u = a.intersection(b)
    c = list(u)
    return c

def sentences(a, b):
    a = sent_tokenize(a)
    b = sent_tokenize(b)
    c = set(a)
    d = set(b)
    e = c.intersection(d)
    f = list(e)
    return f

def substrings(a, b, n):
    c = []
    d = []
    x = len(a)
    y = len(b)
    nn = n
    for i in range(x):
        if n <= x:
            c.append(a[i:n])
            n += 1
        else:
            break

    for j in range(y):
        if nn <= y:
            d.append(b[j:nn])
            nn += 1
        else:
            break

    e = set(c)
    f = set(d)
    g = e.intersection(f)
    h = list(g)
    return h


from django.urls import path

from . import views

urlpatterns = [
    path('', views.show_cart, name='cart'),
    path('go', views.cart_detail, name='cart-go'),
    path('remove', views.cart_remove, name='cart-remove'),
    path('success/', views.success, name='success')
]


class Solution:
    def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:
        m, n = len(grid), len(grid[0])
        ans = [[0 for _ in range(n)] for _ in range(m)]

        row_sum = []
        col_sum = []
        for i in range(m):
            row_sum.append( 2 * sum(grid[i]) - m)
            for j in range(n):
                if i > 0:
                    grid[i][j] += grid[i-1][j]
                if i == m -1:
                    col_sum.append(2 * grid[i][j] - n)

        for i in range(m):
            for j in range(n):
                ans[i][j] = row_sum[i] + col_sum[j]

        return ans


import sys

import numpy as np
from scipy import special

from pymoo.util.misc import find_duplicates, cdist

# =========================================================================================================
# Model
# =========================================================================================================

def default_ref_dirs(m):
    if m == 1:
        return np.array([[1.0]])
    elif m == 2:
        return UniformReferenceDirectionFactory(m, n_partitions=99).do()
    elif m == 3:
        return UniformReferenceDirectionFactory(m, n_partitions=12).do()
    else:
        raise Exception("No default reference directions for more than 3 objectives. Please provide them directly:"
                        "https://pymoo.org/misc/reference_directions.html")

class ReferenceDirectionFactory:

    def __init__(self, n_dim, scaling=None, lexsort=True, verbose=False, seed=None, **kwargs) -> None:
        super().__init__()
        self.n_dim = n_dim
        self.scaling = scaling
        self.lexsort = lexsort
        self.verbose = verbose
        self.seed = seed

    def __call__(self):
        return self.do()

    def do(self):

        # set the random seed if it is provided
        if self.seed is not None:
            np.random.seed(self.seed)

        if self.n_dim == 1:
            return np.array([[1.0]])
        else:

            val = self._do()
            if isinstance(val, tuple):
                ref_dirs, other = val[0], val[1:]
            else:
                ref_dirs = val

            if self.scaling is not None:
                ref_dirs = scale_reference_directions(ref_dirs, self.scaling)

            # do ref_dirs is desired
            if self.lexsort:
                I = np.lexsort([ref_dirs[:, j] for j in range(ref_dirs.shape[1])][::-1])
                ref_dirs = ref_dirs[I]

            return ref_dirs

    def _do(self):
        return None

# =========================================================================================================
# Das Dennis Reference Directions (Uniform)
# =========================================================================================================

def get_number_of_uniform_points(n_partitions, n_dim):
    """
    Returns the number of uniform points that can be created uniformly.
    """
    return int(special.binom(n_dim + n_partitions - 1, n_partitions))

def get_partition_closest_to_points(n_points, n_dim):
    """
    Returns the corresponding partition number which create the desired number of points
    or less!
    """

    if n_dim == 1:
        return 0

    n_partitions = 1
    _n_points = get_number_of_uniform_points(n_partitions, n_dim)
    while _n_points <= n_points:
        n_partitions += 1
        _n_points = get_number_of_uniform_points(n_partitions, n_dim)
    return n_partitions - 1

def das_dennis(n_partitions, n_dim):
    if n_partitions == 0:
        return np.full((1, n_dim), 1 / n_dim)
    else:
        ref_dirs = []
        ref_dir = np.full(n_dim, np.nan)
        das_dennis_recursion(ref_dirs, ref_dir, n_partitions, n_partitions, 0)
        return np.concatenate(ref_dirs, axis=0)

def das_dennis_recursion(ref_dirs, ref_dir, n_partitions, beta, depth):
    if depth == len(ref_dir) - 1:
        ref_dir[depth] = beta / (1.0 * n_partitions)
        ref_dirs.append(ref_dir[None, :])
    else:
        for i in range(beta + 1):
            ref_dir[depth] = 1.0 * i / (1.0 * n_partitions)
            das_dennis_recursion(ref_dirs, np.copy(ref_dir), n_partitions, beta - i, depth + 1)

class UniformReferenceDirectionFactory(ReferenceDirectionFactory):

    def __init__(self, n_dim, scaling=None, n_points=None, n_partitions=None, **kwargs) -> None:
        super().__init__(n_dim, scaling=scaling, **kwargs)

        if n_points is not None:
            n_partitions = get_partition_closest_to_points(n_points, n_dim)
            results_in = get_number_of_uniform_points(n_partitions, n_dim)

            # the number of points are not matching to any partition number
            if results_in != n_points:
                results_in_next = get_number_of_uniform_points(n_partitions + 1, n_dim)
                raise Exception("The number of points (n_points = %s) can not be created uniformly.\n"
                                "Either choose n_points = %s (n_partitions = %s) or "
                                "n_points = %s (n_partitions = %s)." %
                                (n_points, results_in, n_partitions, results_in_next, n_partitions + 1))

            self.n_partitions = n_partitions

        elif n_partitions is not None:
            self.n_partitions = n_partitions

        else:
            raise Exception("Either provide number of partitions or number of points.")

    def _do(self):
        return das_dennis(self.n_partitions, self.n_dim)

# =========================================================================================================
# Multi Layer
# =========================================================================================================

class MultiLayerReferenceDirectionFactory:

    def __init__(self, *args) -> None:
        self.layers = []
        self.layers.extend(args)

    def __call__(self):
        return self.do()

    def add_layer(self, *args):
        self.layers.extend(args)

    def do(self):
        ref_dirs = []
        for factory in self.layers:
            ref_dirs.append(factory)
        ref_dirs = np.concatenate(ref_dirs, axis=0)
        is_duplicate = find_duplicates(ref_dirs)
        return ref_dirs[np.logical_not(is_duplicate)]

# =========================================================================================================
# Util
# =========================================================================================================

def get_rng(seed = None):
    if seed is None or type(seed) == int:
        rng = np.random.default_rng(seed)
    return rng

def sample_on_unit_simplex(n_points, n_dim, unit_simplex_mapping="kraemer", seed = None):
    if unit_simplex_mapping == "sum":
        rnd = map_onto_unit_simplex(get_rng(seed).random((n_points, n_dim)), "sum")

    elif unit_simplex_mapping == "kraemer":
        rnd = map_onto_unit_simplex(get_rng(seed).random((n_points, n_dim)), "kraemer")

    elif unit_simplex_mapping == "das-dennis":
        n_partitions = get_partition_closest_to_points(n_points, n_dim)
        rnd = UniformReferenceDirectionFactory(n_dim, n_partitions=n_partitions).do()

    else:
        raise Exception("Please define a valid sampling on unit simplex strategy!")

    return rnd

def map_onto_unit_simplex(rnd, method):
    n_points, n_dim = rnd.shape

    if method == "sum":
        ret = rnd / rnd.sum(axis=1)[:, None]

    elif method == "kraemer":
        M = sys.maxsize

        rnd *= M
        rnd = rnd[:, :n_dim - 1]
        rnd = np.column_stack([np.zeros(n_points), rnd, np.full(n_points, M)])

        rnd = np.sort(rnd, axis=1)

        ret = np.full((n_points, n_dim), np.nan)
        for i in range(1, n_dim + 1):
            ret[:, i - 1] = rnd[:, i] - rnd[:, i - 1]
        ret /= M

    else:
        raise Exception("Invalid unit simplex mapping!")

    return ret

def scale_reference_directions(ref_dirs, scaling):
    return ref_dirs * scaling + ((1 - scaling) / ref_dirs.shape[1])

def select_points_with_maximum_distance(X, n_select, selected=[]):
    n_points, n_dim = X.shape

    # calculate the distance matrix
    D = cdist(X, X)

    # if no selection provided pick randomly in the beginning
    if len(selected) == 0:
        selected = [np.random.randint(len(X))]

    # create variables to store what selected and what not
    not_selected = [i for i in range(n_points) if i not in selected]

    # remove unnecessary points
    dist_to_closest_selected = D[:, selected].min(axis=1)

    # now select the points until sufficient ones are found
    while len(selected) < n_select:
        # find point that has the maximum distance to all others
        index_in_not_selected = dist_to_closest_selected[not_selected].argmax()
        I = not_selected[index_in_not_selected]

        # add the closest distance to selected point
        is_closer = D[I] < dist_to_closest_selected
        dist_to_closest_selected[is_closer] = D[I][is_closer]

        # add it to the selected and remove from not selected
        selected.append(I)
        not_selected = np.delete(not_selected, index_in_not_selected)

    return selected


from rest_framework import serializers

from product.models import Product as ProductModel
from product.models import Review as ReviewModel

from django.db.models import Avg

from datetime import datetime

class ReviewSerializer(serializers.ModelSerializer):
    user = serializers.SerializerMethodField()

    def get_user(self, obj):
        return obj.user.fullname

    class Meta:
        model = ReviewModel
        fields = ["user","content","created",'rating',]

class ProductSerializer(serializers.ModelSerializer):

    review = serializers.SerializerMethodField()

    def get_review(self, obj):
        reviews = obj.review_set
        return {
            "last_review":ReviewSerializer(reviews.last()).data,
            "average_rating":reviews.aggregate(Avg("rating"))["avg"]
        }

    def validate(self, data):
        exposure_end_date = data.get("exposure_end_date","")
        if exposure_end_date and exposure_end_date < datetime.now().date():
            raise serializers.ValidationError (
                detail = {"error": "유효하지 않은 노출 종료 날짜입니다."},
            )
        return data

    def create(self, validated_data):
        product = ProductModel(**validated_data)
        product.save()
        product.description += f"\n\n{product.created.replace(microsecond=0, tzinfo=None)}에 등록된 상품입니다."
        product.save()

        return product

    def update(self, instance, validated_data):
        for key, value in validated_data.items():
            if key == "description":
                value += f"\n\n{instance.created.replace(microsecond=0, tzinfo=None)}에 등록된 상품입니다."
            setattr(instance, key, value)
        instance.save()
        instance.description = f"{instance.modified.replace(microsecond=0, tzinfo=None)}에 수정되었습니다. \n\n" + instance.description

        instance.save()
        return instance

    user = serializers.SlugRelatedField(
        read_only=True,
        slug_field='username' #email도 가능
    )

    class Meta:
        model = ProductModel
        fields = ["user","thumbnail","description","created","modified","exposure_end_date","is_active","price","review"]


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

import nltk
from matplotlib import style
from nltk import pos_tag
from nltk.tokenize import word_tokenize
#from nltk.chunk import conlltags2tree
from nltk.tree import Tree
#pip install langdetect daca nu este instalad modulul
from langdetect import detect
from nltk.corpus import wordnet as wn
from nltk.stem import WordNetLemmatizer
style.use('fivethirtyeight')

# Process text
def process_text():
    raw_text = open("sample_text.txt").read()
    token_text = word_tokenize(raw_text)
    return token_text

token_text=process_text()
tagged_words = nltk.pos_tag(token_text)

# NLTK POS and NER taggers

def nltk_tagger():
    ne_tagged = nltk.ne_chunk(tagged_words)
    return (ne_tagged)

# Parse named entities from tree
def structure_ne(ne_tree):
    ne = []
    for subtree in ne_tree:
        if type(subtree) == Tree: # If subtree is a noun chunk, i.e. NE != "O"
            ne_label = subtree.label()
            ne_string = " ".join([token for token, pos in subtree.leaves()])
            ne.append((ne_string, ne_label))
    return ne

ne_tagged=structure_ne(nltk_tagger())

#Singura functie necesara pentru detectarea limbii(avand ca intrare un paragraf)
def get_language(paragraph):
    return detect(paragraph)
#Determinarea sinonimelor
def penn_to_wn(tag):
    if tag.startswith('J'):
        return wn.ADJ
    elif tag.startswith('N'):
        return wn.NOUN
    elif tag.startswith('R'):
        return wn.ADV
    elif tag.startswith('V'):
        return wn.VERB
    return None

def Synonym(tagged):

    synsets = [set() for _ in range(len(tagged))]
    lemmatzr = WordNetLemmatizer()
    for i,token in enumerate(tagged):
        wn_tag = penn_to_wn(token[1])
        target.write(token[0]+":")

        if not wn_tag:
            target.write('\n')
            continue
        lemma = lemmatzr.lemmatize(token[0], pos=wn_tag)
        for synonym in wn.synsets(lemma, pos=wn_tag):
            synsets[i].add(str(synonym)[8:-7])
        if(len(synsets[i])==0):
            synsets[i].add(lemma)
        target.write(str(synsets[i])[1:-1]+'\n')

#nltk_main()
target=open("output_text.txt", "w")
target.write("LANGUAGE:"+get_language(open("sample_text.txt").read())+"\n")
target.write("SYNONYMS:\n")
Synonym(tagged_words)

target.write("NAMED ENTITIES\n")
for i in ne_tagged:
    target.write(str(i)+"\n")
target.write("Words with POS TAG:\n")
for i in tagged_words:
    target.write(str(i)+"\n")
target.close()


from rest_framework import serializers
from brands.models import PaymentDetails, PaymentDetailsDummy, AccountLedger, TransactionTypes

class PaymentDetailsSerializer(serializers.ModelSerializer):

    class Meta:
        model = PaymentDetails
        fields = ('id','BranchID','PaymentMasterID','PaymentGateway','RefferenceNo',
            'CardNetwork','PaymentStatus','DueDate','LedgerID','Amount','Balance','Discount','NetAmount','Narration','CreatedUserID')

class PaymentDetailsRestSerializer(serializers.ModelSerializer):

    class Meta:
        model = PaymentDetails
        fields = ('id','PaymentDetailsID','BranchID','Action','PaymentMasterID','PaymentGateway','RefferenceNo',
            'CardNetwork','PaymentStatus','DueDate','LedgerID','Amount','Balance','Discount','NetAmount','Narration','CreatedDate','CreatedUserID')

class PaymentDetailsDummySerializer(serializers.ModelSerializer):

    Amount = serializers.SerializerMethodField()
    Balance = serializers.SerializerMethodField()
    Discount = serializers.SerializerMethodField()
    NetAmount = serializers.SerializerMethodField()
    LedgerName = serializers.SerializerMethodField()
    PaymentGatewayName = serializers.SerializerMethodField()
    CardNetworkName = serializers.SerializerMethodField()
    PaymentStatusName = serializers.SerializerMethodField()

    class Meta:
        model = PaymentDetailsDummy
        fields = ('id','BranchID','PaymentMasterID','PaymentGateway','PaymentGatewayName','RefferenceNo',
            'CardNetwork','CardNetworkName','PaymentStatus','PaymentStatusName','DueDate','LedgerID','LedgerName','Amount','Balance','Discount','NetAmount','Narration','CreatedUserID',
        	'detailID')

    def get_Amount(self, instances):

        Amount = instances.Amount

        Amount = round(Amount,2)

        return str(Amount)

    def get_LedgerName(self, instances):

        DataBase = self.context.get("DataBase")

        LedgerID = instances.LedgerID
        BranchID = instances.BranchID

        ledger = AccountLedger.objects.get(LedgerID=LedgerID,BranchID=BranchID)

        LedgerName = ledger.LedgerName

        return LedgerName

    def get_PaymentGatewayName(self, instances):

        DataBase = self.context.get("DataBase")

        PaymentGateway = instances.PaymentGateway
        BranchID = instances.BranchID
        try:
            transType = TransactionTypes.objects.get(TransactionTypesID=PaymentGateway,BranchID=BranchID)
            PaymentGatewayName = transType.Name
        except:
            PaymentGatewayName = None

        return PaymentGatewayName

    def get_CardNetworkName(self, instances):

        DataBase = self.context.get("DataBase")

        CardNetwork = instances.CardNetwork
        BranchID = instances.BranchID
        try:
            transType = TransactionTypes.objects.get(TransactionTypesID=CardNetwork,BranchID=BranchID)
            CardNetworkName = transType.Name
        except:
            CardNetworkName = None

        return CardNetworkName

    def get_PaymentStatusName(self, instances):

        DataBase = self.context.get("DataBase")

        PaymentStatus = instances.PaymentStatus
        BranchID = instances.BranchID
        try:
            transType = TransactionTypes.objects.get(TransactionTypesID=PaymentStatus,BranchID=BranchID)
            PaymentStatusName = transType.Name
        except:
            PaymentStatusName = None

        return PaymentStatusName

    def get_Balance(self, instances):

        Balance = int(instances.Balance)

        Balance = round(Balance,2)

        return str(Balance)

    def get_Discount(self, instances):

        Discount = instances.Discount

        Discount = round(Discount,2)

        return str(Discount)

    def get_NetAmount(self, instances):

        NetAmount = instances.NetAmount

        NetAmount = round(NetAmount,2)

        return str(NetAmount)


import sqlite3

__connection = None

# def get_conne():
#     global __connection
#     if __connection is None:
#         __connection = sqlite3.connect('db/Dic.db')
#         return __connection

def ensure_connection(func):
    """ Декоратор для подключения к СУБД: открывает соединение,
        выполняет переданную функцию и закрывает за собой соединение.
        Потокобезопасно!
    """

    def inner(*args, **kwargs):
        with sqlite3.connect('db/Dic.db') as conn:
            kwargs['conn'] = conn
            res = func(*args, **kwargs)
        return res

    return inner

# Создания бази данных со словами если базы даных нету :З
@ensure_connection
def init_db(conn, force: bool = False):
    # get_conne()
    # c = __connection.cursor()
    c = conn.cursor()

    if force:
        c.execute('DROP TABLE IF EXISTS words')

    c.execute('''
        CREATE TABLE IF NOT EXISTS words (
            id          INTEGER PRIMARY KEY,
            word       TEXT NOT NULL,
            tr       TEXT NOT NULL
        )
    ''')

@ensure_connection
def createFirstWord(conn):
    c = conn.cursor()
    c.execute("SELECT id FROM words ORDER BY id")
    c = c.fetchone()
    if c == None:
        c = conn.cursor()
        c.execute("insert into words values (1, 'word', 'слово')")
    else:
        pass
    # c.execute("insert into words values (2, 'red', 'червоний')")

@ensure_connection
def searchE(conn, word: str):
    c = conn.cursor()
    c.execute("SELECT tr FROM words WHERE word = ?", (word,))
    (wd,) = c.fetchone()
    return wd

@ensure_connection
def searchU(conn, word: str):
    c = conn.cursor()
    c.execute("SELECT word FROM words WHERE tr = ?", (word,))
    (wd,) = c.fetchone()
    return wd

class Check:
    @ensure_connection
    def eCheck(conn):
        c = conn.cursor()
        c.execute("SELECT word FROM words ORDER BY id")
        return c.fetchall()

    @ensure_connection
    def uCheck(conn):
        c = conn.cursor()
        c.execute("SELECT tr FROM words ORDER BY id")
        return c.fetchall()

init_db()
createFirstWord()


from capaDatos.bd import obtener_conexion

def listar_semestres():
    conexion = obtener_conexion()
    semestres = []
    with conexion.cursor() as cursor:
        cursor.execute("select * from fn_listar_semestre_academico()")
        semestres = cursor.fetchall()
    conexion.close()
    return semestres

def grafico_semestres():
    conexion = obtener_conexion()
    datos = []
    semestres = []
    with conexion.cursor() as cursor:
        cursor.execute("select count(*) from fn_listar_semestre_academico() where estado='A'")
        semestres = cursor.fetchone()
        datos.append(semestres[0])
        cursor.execute("select count(*) from fn_listar_semestre_academico() where estado='I'")
        semestres = cursor.fetchone()
        datos.append(semestres[0])
        print(list(datos))
    conexion.close()
    return datos

def agregar_semestre(nombre, fecha_inicio, fecha_fin, estado):
    conexion = obtener_conexion()
    msg = []
    with conexion.cursor() as cursor:
        cursor.execute(
            "SELECT fn_agregar_semestre(%s, %s, %s, %s)",
            (nombre, fecha_inicio, fecha_fin, estado),
        )
        msg = cursor.fetchone()
    conexion.commit()
    conexion.close()
    return msg[0] if msg is not None else None

def eliminar_semestre(id):
    conexion = obtener_conexion()
    msg = []
    with conexion.cursor() as cursor:
        cursor.execute("SELECT fn_eliminar_semestre(%s)", (id,))
        msg = cursor.fetchone()
    conexion.commit()
    conexion.close()
    return msg[0] if msg is not None else None

def buscar_semestreID(id):
    conexion = obtener_conexion()
    semestre = None
    with conexion.cursor() as cursor:
        cursor.execute(
            "select * from fn_consultar_semestre_academico_ID(%s)",
            (id,),
        )
        semestre = cursor.fetchone()
    conexion.close()
    return semestre

def actualizar_semestre(id, nombre, fecha_inicio, fecha_fin, estado):
    conexion = obtener_conexion()
    msg = []
    with conexion.cursor() as cursor:
        cursor.execute(
            "SELECT fn_editar_semestre(%s, %s, %s, %s, %s)",
            (id, nombre, fecha_inicio, fecha_fin, estado),
        )
        msg = cursor.fetchone()
    conexion.commit()
    conexion.close()
    return msg[0] if msg is not None else None

def dar_baja_semestre(id, estado):
    conexion = obtener_conexion()
    msg = []
    new_estado = ""
    if estado == "A":
        new_estado = "I"
    else:
        new_estado = "A"
    with conexion.cursor() as cursor:
        cursor.execute("SELECT fn_actualizar_estado_semestre(%s, %s)", (id, new_estado))
        msg = cursor.fetchone()
    conexion.commit()
    conexion.close()
    return msg[0] if msg is not None else None