# -*- coding: utf-8 -*-
"""
Created on Thu May 30 16:24:17 2019

@author: w
"""
import numpy as np
import datetime
from Data.get_data import read_price # from eod csv file

def est_transacton_prices(tickers, delta_s, tickers_pl, init_cash):
    trans_dates = []
    residual_cash = []
    trans_prices = np.zeros([len(delta_s),len(tickers)])
    shares = np.zeros([len(delta_s),len(tickers)])
    #t=0
    t_date = delta_s.index[0]
    cash = init_cash
    # pre_close_price
    prices = np.zeros(len(tickers))
    pre_t_date = t_date + datetime.timedelta(days=-1)
    for e in range(len(tickers)):
        while pre_t_date.date() not in tickers_pl[tickers[e]].index.date:
            pre_t_date = pre_t_date + datetime.timedelta(days=-1)
        c = read_price(pre_t_date, tickers[e], tickers_pl[tickers[e]])
        prices[e] = c.Close
    #t>=1
    for t in range(len(delta_s.index)):
        pre_prices = prices
        t_date = delta_s.index[t]
        t_date = t_date + datetime.timedelta(days=1)
        s = delta_s.iloc[t].copy()
        for e in range(len(tickers)):
            t_date_e = t_date
            while t_date_e.date() not in tickers_pl[tickers[e]].index.date:
                t_date_e = t_date_e + datetime.timedelta(days=1)
            if s[e] > 0.0: # buy
                transaction = 'BUY'
            elif s[e] < 0.0:
                transaction = 'SELL'
            else:
                transaction = 'No Trade'
            place_price = get_transaction_price(t_date_e, tickers[e], transaction, pre_prices[e], tickers_pl[tickers[e]])
            trans_prices[t,e] = place_price
            fee = _calc_fee(place_price, s[e], transaction)
            delta_cash = -s[e] * place_price - fee
            cash = cash + delta_cash
        if t==0:
            shares[t] = s
        else:
            shares[t] = shares[t-1] + s
        trans_dates.append(t_date_e)
        residual_cash.append(cash)
        prices = get_close_price(tickers, t_date_e, tickers_pl, 'Weekly')
    return trans_dates, trans_prices, shares, residual_cash

def get_transaction_price(t_date, ticker, transaction, pre_Close, pl=None, worst=False):
    p = read_price(t_date, ticker, pl)
    if transaction == 'BUY':
        price = _get_long_price(p, pre_Close)
    elif transaction == 'SELL':
        price = _get_short_price(p, pre_Close)
    else:
        price = p.Close
    return price

def _get_long_price(p, pre_Close, worst=False):
    if worst:
        long_price = p.High
    else:
        if p.Open <= pre_Close:
            long_price = p.Open
        else:
            long_price = p.Close
    return long_price

def _get_short_price(p, pre_Close, worst=False):
    if worst:
        short_price = p.Low
    else:
        if p.Open >= pre_Close:
            short_price = p.Open
        else:
            short_price = p.Close
    return short_price

# IB
# US$1<=fee<=1%
# US$0.005 per share
# transaction fee US$0.0000207*total transaction value
# sell - transaction fee US$0.000119 per share
def _calc_fee(price, delta_s, transaction):
    if delta_s != 0:
        delta_s = abs(delta_s)
        total_value = price*delta_s
        fee = 0.005 * delta_s
        if fee<1:
            fee = 1.0
        elif fee>0.01*total_value:
            fee = 0.01*total_value
        fee = fee + 0.0000207*total_value
        if transaction == 'SELL':
            fee = fee + 0.000119*delta_s
    else:
        fee = 0.0
    return fee

# pre_shares: shares of each asset at t-1
# pre_cash: cash at t-1
# pre_prices: pre_Close at t-1
# delta_s: share change of each asset at t
# return cash and shares at t
def get_allocation(tickers, t_date, delta_s, pre_shares, pre_cash, pre_prices, tickers_pl=None, worse=False):
    shares = pre_shares.copy()
    cash = pre_cash
    prices = shares.copy()
    for e in range(len(tickers)):
        if tickers_pl is None:
            pl = None
        else:
            pl = tickers_pl[tickers[e]]
        delta_cash = 0.0
        shares[e] = pre_shares[e] + delta_s[e]
        if delta_s[e] > 0.0: # buy
            price = get_transaction_price(t_date, tickers[e], 'BUY', pre_prices[e], pl)
            fee = _calc_fee(price, delta_s[e], 'BUY')
            delta_cash = -delta_s[e] * price - fee
        else:
            price = get_transaction_price(t_date, tickers[e], 'SELL', pre_prices[e], pl)
            fee = _calc_fee(price, delta_s[e], 'SELL')
            delta_cash = -delta_s[e] * price - fee
        if e==0:
            pass #print(delta_cash)
        cash = cash + delta_cash
        prices[e] = price
    return shares, cash, prices

def get_value(tickers, t_date, shares, tickers_pl=None):
    value = 0.0
    for e in range(len(tickers)):
        if tickers_pl is None:
            pl = None
        else:
            pl = tickers_pl[tickers[e]]
        p = read_price(t_date, tickers[e], pl)
        value = value + p.Close * shares[e]
    return value

def get_close_price(tickers, t_date, tickers_pl=None, freq='Date'): # 'Daily, Weekly, Monthly
    prices = np.zeros(len(tickers))
    if freq == 'Daily':
        for e in range(len(tickers)):
            if tickers_pl is None:
                pl = None
            else:
                pl = tickers_pl[tickers[e]]
            p = read_price(t_date, tickers[e], pl)
            prices[e] = p.Close
    elif freq == 'Weekly':
        for e in range(len(tickers)):
            if tickers_pl is None:
                pl = None
            else:
                pl = tickers_pl[tickers[e]]
            p = read_price(t_date, tickers[e], pl, 'Weekly')
            prices[e] = p.Close
    return prices


'''
created by goblinM
删除链表的中间节点和a/b处的节点
链表为空或长度为1直接返回
长度为2：删除头结点
长度为3：删除第二个节点
。。。
中间节点：
也就是链表长度每增加2(3,5,7...)删除的节点就我往后移一个节点
给定链表的头结点head,整数a和b,实现删除位于a/b处节点的函数:
先计算r = (a*n)/b的值，然后r向上取整之后的整数值代表该删除的节点是第几个节点
'''
class LinkNode:
    def __init__(self,val,next=None):
        self.val = val
        self.next = next

class DeleteMidAB:
    def delete_middle_node(self,head):
        if not head or head.next == None:
            return head
        if head.next.next == None:
            return head.next
        pre = head.next
        cur = head.next.next
        while cur.next != None and cur.next.next != None:
            pre = pre.next
            cur = cur.next.next
        pre.next = pre.next.next
        return head

    def delete_ab_node(self,head,a,b):
        if a < 1 or a > b:
            return head
        head_len = 0
        cur = head
        while cur:#计算长度
            head_len += 1
            cur = cur.next
        n = int((a*head_len)/b)
        if n == 1:
            head = head.next
        if n > 1:
            cur = head
            while n-1 != 1:
                cur = cur.next
            cur.next = cur.next.next
        return head


# -*- coding: utf-8 -*-
"""
Created on Thu Feb 18 10:36:13 2021

@author: sayers
"""

"""
This is the script to take latest employee data and sort into categories
specifically for the purposes of feeding email bot
This process should be run between twice a day and once every two days.
Input is Current Job Report from CUNYfirst as excel file.
Output is Json file with dict, key is group name, value is list of names
"""

from admin import newest, colclean, rehead ,read_json,to_records,write_json,trydict,fileverify
import pandas as pd
import ast

def subsetter(df,crit_field,criteria):
    try:
        newdf=df[df[crit_field].str.contains(criteria)]
        df=df[~df['empl_id'].isin(list(newdf.empl_id.unique()))]
        return(df,newdf)
    except:
        newdf=df[df[crit_field]==criteria]
        df=df[~df['empl_id'].isin(list(newdf.empl_id.unique()))]
        return(df,newdf)
def update_crit():
    critloc=ast.literal_eval(open('y://reports//program data//critloc.txt','r').read())
    write_json(critloc,'y://reports//program data//emaildata_criteria_list')

def refresh_lists():
    infolder='y://reports//archives//2021'
    outfile='Y://reports//Program Data//emaildata.json'
    #first read in df by opening most recent  CJR
    df=colclean(rehead(pd.read_excel(newest(infolder,'FULL_FILE')),2))

    #ananoymizes function and makes reliant on local data sources
    #this changes out government names for commonly used name (on email acct)
    swap_dict=read_json('y://reports//program data//swapdict.json')
    for k,v in swap_dict.items():
        df.person_nm=df.person_nm.replace(k,v)

    #pull in current department data for depthead fields
    deptfile='Y://reports//Lookup Tables//departments_file.xlsx'
    df2=colclean(pd.read_excel(deptfile))
    chairs=list(df2.chairperson.unique())
    support=list(df2[df2.support_staff.isnull()==False].support_staff.unique())
    support=[x for x in support if x!=0]
    newlist=[]
    for x in support:
        y=x.split(',')
        if type(y)==list:
            newlist.extend(y)
        else:
            newlist.append(y[0])
    [x.strip() for x in newlist]
    #subset df to include only required fields
    #minimally, this should be paygroup, reports to, name, department, title
    df=df[['empl_id','hr_status','person_nm','labor_job_ld','dept_descr_job','union_job_cd','empl_cls_ld','reports_to_emplid']]

    #add a flag for department heads, department secretaries, sueprvisors, etc
    df.loc[(df['person_nm'].isin(chairs)) & (df['hr_status'] == 'Active'),'dept_head'] = 'chair'
    df.loc[(df['person_nm'].isin(newlist)) & (df['hr_status'] == 'Active'),'support'] = 'support'

    #identifying people who ae supervisors
    reports=list(df[df.reports_to_emplid.isnull()==False].reports_to_emplid.unique())
    df.loc[(df['empl_id'].isin(reports)) & (df['hr_status'] == 'Active'),'supervisor'] = 'supervisor'

    #open current json document or prepare to create
    if fileverify(outfile):
        maindict=read_json(outfile)
    else:
        maindict={}
    writedict={}
    #have a standing list of criteria fields and criteria, then iterate
    #have 3 part tuple with crit_field, criteria, and label
    critloc='Y://reports//Program Data//emaildata_criteria_list.json'
    critlist=read_json(critloc)
    for crit_field,criteria,label in critlist:
        df,newdf=subsetter(df,crit_field,criteria)
        #take names from newdf and add to list
        newlist=list(newdf.person_nm.unique())
        #take list and add to maindict
        writedict[label]=newlist

    #compare to discover any updates that were made. This is a late stage
    #error check
    try:
        diff_dict= [v for k,v in writedict.items() if maindict[k]!=v]
    except:
        diff_dict=[]

    if len(diff_dict)!=0:
        print("These are differences from the last run of refresh_lists")
        print(diff_dict)
        write_json(writedict,outfile[:-5])
    else:
        write_json(writedict,outfile[:-5])

if __name__=="__main__":
    refresh_lists()


from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button

class DynamicLabel(App):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.list = ["Renard", "Toh", "JCU", "BIT", "Programming II"]

    def build(self):
        self.title = "Dynamic Labels"
        self.root = Builder.load_file('dynamic_labels.kv')
        self.create_widgets()
        return self.root

    def create_widgets(self):
        for name in self.list:
            temp_button = Button(text=name, id=name)
            self.root.ids.entries_box.add_widget(temp_button)

DynamicLabel().run()


import argparse
import pprint as pp
from keras.models import load_model

parser = argparse.ArgumentParser(description='Print Model Summary')
parser.add_argument(
    'model',
    type=str,
    help='Path to model h5 file. Model should be on the same path.'
)
args = parser.parse_args()

model = load_model(args.model)
model.summary()

pp.pprint(model.get_config())


# 1. Поработайте с переменными, создайте несколько, выведите на экран,
# запросите у пользователя несколько чисел и строк и сохраните в переменные, выведите на экран.

greetings = 'Здравствуйте,'
proposal = 'Предлагаем Вам, взять кредит в размере'
currency = 'руб.'
name = input('Ваше имя: ')
age = input('Ваш возраст: ')

print(f'{greetings} {name}!')
if int(age) > 18:
    salary = input('Размер вашей зарплаты (руб.): ')
    print(f'{proposal} {int(salary) * 12} {currency}')
else:
    print('Вам кредит брать ещё нельзя')


import math
import re
import logging
import time
from django.core.management.base import BaseCommand, CommandError
from exponent_server_sdk import (DeviceNotRegisteredError, PushClient, PushMessage,
                                 PushTicketError, PushServerError)
from search.read_csv_data_from_file import read_csv_data_from_file
from requests.exceptions import ConnectionError, HTTPError

LOGGER = logging.getLogger(__name__)

class Command(BaseCommand):
    help = ('Send push notifications to a set of users identified by their expo push notification tokens')

    def add_arguments(self, parser):
        parser.add_argument('title',
                    metavar='title',
                    help=('path to file containing title text for notification, '
                            'comma separated, locale in first column, title in second column'))
        parser.add_argument('message',
                            metavar='message',
                            help=('path to file containing message text for notification, '
                                  'comma separated, locale in first column, message in second column'))
        parser.add_argument('users',
                            metavar='users',
                            help=('path to file containing users, comma separated, expo push notification '
                                  'token in first column, locale in second column'))
        parser.add_argument('url',
                            metavar='url',
                            nargs='?',
                            help=('optional url to route to in the app, can be "store", "welcome" or '
                                  '"/task/<task id>". "store" is disabled and doesn\'t do anything until '
                                  'we decide what the UX should be around that.'),
                            default=None)

    def handle(self, *args, **options):
        users = options['users']
        users_csv = read_csv_data_from_file(users)
        valid_users = validate_users(users_csv)

        title = options['title']
        notification_titles = read_csv_data_from_file(title)
        valid_notification_titles = validate_localized_notifications(notification_titles)

        message = options['message']
        notifications = read_csv_data_from_file(message)
        valid_notifications = validate_localized_notifications(notifications)

        url = options['url']

        send_push_notifications(valid_users, valid_notification_titles, valid_notifications, url)

def validate_users(users):
    result = []
    for line in users:
        result.append(validate_user(line))
    return result

def validate_user(user):
    locale = validate_locale(user[1])
    token = validate_token(user[0])
    return {'token': token, 'locale': locale}

def validate_locale(locale):
    if not locale in ['ar', 'en', 'fr', 'ko', 'pa', 'tl', 'zh_CN', 'zh_TW']:
        raise CommandError('{}: Invalid locale'.format(locale))
    return locale

def validate_token(token):
    match = re.search(r'^ExponentPushToken\[.*\]$', token)
    if not match:
        raise CommandError('{}: Invalid token'.format(token))
    return token

def validate_localized_notifications(notifications):
    result = {}
    for line in notifications:
        locale = validate_locale(line[0])
        result[locale] = line[1]
    return result

def send_push_notifications(users, localized_notification_titles, localized_notification_messages, url):
    for i in range(0, len(users)):
        user = users[i]
        token = user['token']
        locale = user['locale']
        title = localized_notification_titles[locale]
        message = localized_notification_messages[locale]
        extra = build_extra_data(url)
        send_push_message(token, title, message, extra, 0)
        print_progress(i, len(users))

def print_progress(index, max_index):
    if max_index < 1:
        return
    last_fraction = math.floor(20 * (index - 1)/max_index)
    current_fraction = math.floor(20 * index/max_index)
    if (last_fraction < current_fraction):
        print(f'{5*current_fraction}% done')

def build_extra_data(url):
    if url:
        return {'navigateToRoute': url}
    return None

def send_push_message(token, title, message, extra, retryAttempts):
    delay_in_seconds = 0.05 * (2 ** retryAttempts)
    limit_in_seconds = 10.0
    if (delay_in_seconds > limit_in_seconds):
        LOGGER.error('Error pushing notification: %i failed attempts %s', retryAttempts, token)
        return

    try:
        response = PushClient().publish(PushMessage(to=token, title=title, body=message, data=extra))

    except PushServerError:
        LOGGER.error('Error pushing notification: PushServerError for %s', token)
        return

    except (ConnectionError, HTTPError):
        LOGGER.warn('Error pushing notification: ConnectionError for %s', token)
        time.sleep(delay_in_seconds)
        send_push_message(token, title, message, extra, retryAttempts + 1)
        return

    try:
        response.validate_response()

    except DeviceNotRegisteredError:
        # According to https://github.com/expo/expo-server-sdk-python this error is
        # thrown when users no longer want to receive push notifications.
        LOGGER.warning('DeviceNotRegisteredError, remove this token from our database: %s', token)

    except PushTicketError:
        LOGGER.error('Error pushing notification: PushResponseError for %s', token)


import file
import sys
import getopt
import os

# Try catch block for non integer argument checking
def RepresentsInt(s):
    try:
        int(s)
        return True
    except ValueError:
        return False

# Default values
sa_sampling_rate = 32
isa_sampling_rate = 32
sampling_scheme = 0
npa_sampling_rate = 128
npa_encoding_scheme = 1
type = "file"
inputpath = ""

# Get user input to either load from memory or compress a new file
option = input("Usage: [load/compress] [file]\n")
option = option.split()
if (len(option) != 2):
    print("Usage: [load/compress] [file]\n")
    sys.exit(2)
else:
    inputpath = option[1]
    if (option[0] == "load"):
        # Load file from memory
        print("loading ", inputpath, " from file")
        q = file.File(inputpath)
    elif (option[0] == "compress"):
        # Compress the file
        print("Please enter the sampling rates")
        option = input("Usage: [-s sa_sampling_rate] [-i isa_sampling_rate] [-x sampling_scheme] [-n npa_sampling_rate] [-r npa_encoding_scheme] [-t input_type]\n")
        # Loop through arguments to get sampling rates using getopt
        try:
            optlist, args = getopt.getopt(option, 's:i:x:n:r:t:')
        except getopt.GetoptError as err:
            print("Get opt error")
            sys.exit(2)
        for o, a in optlist:
            if o == "-s":
                sa_sampling_rate = int(a)
            elif o == "-i":
                isa_sampling_rate = int(a)
            elif o == "-x":
                sampling_scheme = int(a)
            elif o == "-n":
                npa_sampling_rate = int(a)
            elif o == "-r":
                npa_encoding_scheme = int(a)
            elif o == "-t":
                type = a
            else:
                printf("Invalid Option")
                sys.exit(2)

        # # Get input size
        # FILE *f = fopen(filename.c_str(), "r")
        # fseek(f, 0, SEEK_END)
        # uint64_t fsize = ftell(f)
        # fseek(f, 0, SEEK_SET)

        # # Read input from file
        # auto *data = (uint8_t *) s_allocator.s_malloc(fsize + 1)
        # fread(data, fsize, 1, f)
        # fclose(f)
        # data[fsize] = 1
        # uint8_t *, size_t, uint32_t, uint32_t, int32_t, uint32_t, int, int, uint32_t>

        # context_len = 3 or sampling_range = 1024

        f = open(inputpath, "r")
        input = f.read()

        q = file.File(0, input, sa_sampling_rate,
            isa_sampling_rate, npa_sampling_rate,
            sampling_scheme, npa_encoding_scheme)

        content = q.GetContent().tobytes()
        # print(str(content,'ISO-8859-1'))
        text_file = open(inputpath + ".succinct", "w")
        text_file.write(content)

    else:
        print("Usage: [load/compress] [file]\n")
        sys.exit(2)


import logging
import uuid
import random
import tornado.escape
import tornado.ioloop
import tornado.websocket
import rule

logger = logging.getLogger()

class Player(object):

    def __init__(self):
        self.pid = Player.gen_id()
        self.socket = None
        self.table = None
        self.ready = False
        self.seat = 0
        self.iscalled = False
        self.rank = 1  # landlord=2 farmer=1
        self.pokers = []

    def send(self, message):
        if not isinstance(message, str):
            message = tornado.escape.json_encode(message)
        self.socket.write_message(message)

    def recv(self, request):
        if request[0] == 110: # request deal poker
            if self.table.state == 2:
                self.ready = True
                self.table.ready()
            return

        if self.seat != self.table.whoseTurn:
            logger.warning('Player[%d] turn cheat', self.pid)
            return

        if request[0] == 101: # request call score
            score = request[1]
            if score > 3 or (score > 0 and score < self.table.callscore):
                logger.warning('Player[%d] callscore[%d] cheat', self.pid, score)
                return
            self.handleCallScore(score)
        elif request[0] == 105: # request shot poker
            shotPoker = request[1]
            if len(shotPoker) > 0:
                if not rule.containsAll(self.pokers, shotPoker):
                    logger.warning('Player[%d] play non-exist poker', self.pid)
                    return

                if self.table.lastShotSeat != self.seat and rule.compare(shotPoker, self.table.lastShotPoker) < 0:
                    logger.warning('Player[%d] play less than poker', self.pid)
                    return

                self.table.lastShotPoker = shotPoker

            self.handleShotPoker(shotPoker)

    def dealPoker(self):
        player = self.table.players[self.table.whoseTurn]
        response = [100, player.pid, self.pokers]
        logger.info('Player[%d] dealPoker[%s]', self.pid, ','.join(str(x) for x in self.pokers))
        self.send(response)

    def handleCallScore(self, score):

        nextseat = (self.seat + 1) % 3
        callend = (score == 3 or self.table.players[nextseat].iscalled)

        response = [102, self.pid, score, callend]
        message = tornado.escape.json_encode(response)
        for p in self.table.players:
            p.send(message)

        logger.info('Player[%d] callscore[%d]', self.pid, score)
        if callend:
            if score == 0:
                self.table.dealPoker()
            else:
                self.rank = 2
                self.table.callscore = score
                response = [104, self.table.pokers]
                message = tornado.escape.json_encode(response)
                for p in self.table.players:
                    p.send(message)
                logger.info('Player[%d] is landlord[%s]', self.pid, ','.join(str(x) for x in self.table.pokers))
        else:
            self.table.whoseTurn = nextseat

    def handleShotPoker(self, shotPoker):
        logger.info('Player[%d] shotPoker[%s]', self.pid, ','.join(str(x) for x in shotPoker))
        for p in shotPoker:
            self.pokers.remove(p)
        shotend = len(self.pokers) == 0

        response = [106, self.pid, shotPoker, shotend]
        message = tornado.escape.json_encode(response)
        for p in self.table.players:
            p.send(message)

        if shotend:
            response = [108, self.pid, self.table.calcCoin(self)]
            for p in self.table.players:
                p.send(response)

    def join_table(self, t):
        t.add(self)
        self.ready = True
        self.table = t

    def open(self, socket):
        self.socket = socket

    counter = 1000
    @classmethod
    def gen_id(cls):
        cls.counter +=1
        return cls.counter

class Table(object):

    def __init__(self):
        self.pid = Table.gen_id()
        self.players = [None, None, None]
        self.state = 0  # 0 waiting  1 playing 2 end 3 closed
        self.pokers = []
        self.multiple = 1
        self.callscore = 0
        self.whoseTurn = 0
        self.lastShotSeat = 0;
        self.lastShotPoker = [];
        self.room = 100
        tornado.ioloop.IOLoop.current().add_callback(self.update)

    def calcCoin(self, winner):
        self.state = 2
        coins = []
        tax = 100
        for p in self.players:
            p.ready = f
            coin = self.room * p.rank * self.callscore * self.multiple
            if p.rank == winner.rank:
                coins.append(coin - tax)
            else:
                coins.append(-coin - tax)
        return coins

    def update(self):
        logger.info('table[%d] update', self.pid)

    def add(self, player):
        for i, p in enumerate(self.players):
            if not p:
                player.seat = i
                self.players[i] = player
                logger.info('Table[%d] add Player[%d]', self.pid, player.pid)
                return True
        logger.error('Player[%d] join a full Table[%d]', player.pid, self.pid)
        return False

    def remove(self, player):
        for i, p in enumerate(self.players):
            if p and p.pid == player.pid:
                self.players[i] = None
        else:
            logger.error('Player[%d] not in Table[%d]', player.pid, self.pid)

        if all(p == None for p in self.players):
            self.state = 3
            logger.error('Table[%d] close', self.pid)
            return True
        return False

    def size(self):
        return 3 - self.players.count(None)

    def dealPoker(self):
        if not all(p and p.ready for p in self.players):
            return

        self.state = 1
        self.pokers = [i for i in range(54)]
        random.shuffle(self.pokers)
        for i in range(51):
            self.players[i % 3].pokers.append(self.pokers.pop())

        self.whoseTurn = random.randint(0, 2)
        for p in self.players:
            p.dealPoker()

    counter = 0
    @classmethod
    def gen_id(cls):
        cls.counter += 1
        return cls.counter

if __name__ == '__main__':
    t = Table()
    print(t.pid)
    t = Table()
    print(t.pid)
    t = Table()
    print(t.pid)
    t = Table()
    print(t.pid)


import os
import re
import operator
import pandas as pd
from collections import defaultdict
from nltk.tokenize import TweetTokenizer
# -*- coding: utf-8 -*-

def read_data(file_path, cols=None):
    if cols is None:
        # Default set of columns
        cols = ['streamer', 'gender', 'time', 'commenter', 'message']
        data = pd.read_csv(file_path, sep=',', header=None, names=cols)
    else:
        data = pd.read_csv(data_file_path, sep=',', header=None, names=cols)
    return data

def write_data(data_frame, file_path, cols=None):
    if cols is None:
        data_frame.to_csv(file_path)
    else:
        data_frame.to_csv(file_path, cols)

def remove_bot_messages(data_frame):
    non_bot_messages = data_frame[
        ~data_frame.commenter.str.contains('.*bot$|.*notify$', flags=re.IGNORECASE, regex=True, na=False)]
    return non_bot_messages

def read_emotes(file_paths):
    emotes = []
    for file_path in file_paths:
        emotes = read_emotes_file(file_path, emotes)
    return emotes

def read_emotes_file(file_path, emotes):
    with open(file_path) as emotes_file:
        while 1:
            this_line = emotes_file.readline()
            if this_line == '':
                break
            if this_line[0] == ";":
                continue
            this_line = this_line.replace('\n', '')
            if len(this_line) > 0:
                emotes.append(this_line)
    return emotes

# Todo update the tokenizing logic to better suit twitch
def tokenize_sentence(sentence):
    tknzr = TweetTokenizer(strip_handles=True, reduce_len=True)
    tokens = tknzr.tokenize(sentence)
    return tokens

def get_emote_tokens_list(messages, emotes):
    tokens = get_word_tokens_list(messages)
    emote_tokens = defaultdict(int)
    for key in tokens:
        if key in emotes:
            emote_tokens[key] = tokens[key]
    return emote_tokens

def get_word_tokens_list(messages, skip_list=None, is_english_only=True):
    tokens = defaultdict(int)
    for message in messages:
        temp = tokenize_sentence(str(message))
        for t in temp:
            if is_english_only:
                if not re.match('[^\x00-\x7F]+', t) and len(t) > 0:
                    tokens[t] += 1
            else:
                tokens[t] += 1

    if skip_list is not None:
        stripped_tokens = defaultdict(int)
        for key in tokens:
            if key not in skip_list:
                stripped_tokens[key] = tokens[key]
        return stripped_tokens

    return tokens

def unique(a):
    return list(set(a))

def intersect(a, b):
    return list(set(a) & set(b))

def union(a, b):
    return list(set(a) | set(b))

def diff(first, second):
    second = set(second)
    return [item for item in first if item not in second]

def make_autopct(values):
    def my_autopct(pct):
        total = sum(values)
        val = int(round(pct * total / 100.0))
        return '{p:.2f}%  ({v:d})'.format(p=pct, v=val)

    return my_autopct


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

from starlette import status
from starlette.requests import Request
from starlette.responses import Response
from starlette.routing import Route

from polyaxon import settings
from polyaxon.api import API_V1_LOCATION
from polyaxon.contexts import paths as ctx_paths
from polyaxon_deploy.endpoints.base import ConfigResponse

async def get_run_details(request: Request) -> Response:
    run_uuid = request.path_params["run_uuid"]
    data_path = os.path.join(
        settings.SANDBOX_CONFIG.store_root,
        run_uuid,
        ctx_paths.CONTEXT_LOCAL_RUN,
    )
    if not os.path.exists(data_path) or not os.path.isdir(data_path):
        return Response(status_code=status.HTTP_404_NOT_FOUND)

    with open(data_path, "r") as config_file:
        config_str = config_file.read()
    return ConfigResponse(config_str)

async def get_run_artifact_lineage(request: Request) -> Response:
    run_uuid = request.path_params["run_uuid"]
    data_path = os.path.join(
        settings.SANDBOX_CONFIG.store_root,
        run_uuid,
        ctx_paths.CONTEXT_LOCAL_LINEAGES,
    )
    if not os.path.exists(data_path) or not os.path.isdir(data_path):
        return Response(status_code=status.HTTP_404_NOT_FOUND)

    with open(data_path, "r") as config_file:
        config_str = config_file.read()
        config_str = f'{{"results": {config_str}}}'

    return ConfigResponse(config_str)

URLS_RUNS_DETAILS = API_V1_LOCATION + "{owner:str}/{project:str}/runs/{run_uuid:str}/"
URLS_RUNS_STATUSES = (
    API_V1_LOCATION + "{owner:str}/{project:str}/runs/{run_uuid:str}/statuses"
)
URLS_RUNS_LINEAGE_ARTIFACTS = (
    API_V1_LOCATION + "{owner:str}/{project:str}/runs/{run_uuid:str}/lineage/artifacts"
)

# fmt: off
runs_routes = [
    Route(
        URLS_RUNS_DETAILS,
        get_run_details,
        name="get_run_details",
        methods=["GET"],
    ),
    Route(
        URLS_RUNS_STATUSES,
        get_run_details,
        name="get_run_details",
        methods=["GET"],
    ),
    Route(
        URLS_RUNS_LINEAGE_ARTIFACTS,
        get_run_artifact_lineage,
        name="get_run_artifact_lineage",
        methods=["GET"],
    ),
]