# -*- coding: UTF-8 –*-
import random
from datetime import datetime

"""This is a random address function"""

def address():
    # 小区名,可自行添加
    area_address_name = ['蓝湾上林院', '绿城金华御园(别墅)', '紫金湾', '玫瑰星城', '绿城兰园',
                         '龙庭一品', '江山风华', '中梁首府', '中梁首府', '都市豪园',
                         '光明湖海城市花园', '金色海塘', '天御花园', '广润翰城', '泰地金水湾',
                         '新纪元香湖', '绿城金都美地', '中天学府诚品', '金都美苑', '金都美苑',
                         '香格里拉城市花园', '广天九龙玉府', '中天公元诚品', '南岸名城', '欧景名城',
                         '御园(东区)', '蝶景湾御江山', '滨江金色蓝庭', '书香名邸', '蓝湾国际花园',
                         '丽州一品', '丽州一品', '苏桂院(一期)', '环球春江花园', '冠达东方兰庭',
                         '五星清华园', '鸿基彼岸', '东方明珠花园', '华庭常青墅', '四季尊域',
                         '尖峰郦园', '金地艺境', '保集蓝郡', '保集蓝郡', '泰瑞家园',
                         '泰瑞家园', '和信花园', '环球春江花园', '矿泉花园', '环球春江花园',
                         '福林花园', '海韵嘉园', '万科青岛小镇', '中海蓝庭', '城发长江瑞城',
                         '麦岛金岸', '城建湖光山色', '青岛印象山', '金帝山庄', '保利海上罗兰',
                         '东海路9号', '鲁商蓝岸丽舍', '瑞源名嘉汇', '中海清江华府', '万科魅力之城',
                         '中央国际', '湛园海德公园', '万达悦公馆', '万科如园', '和达璟城紫御',
                         '上实海上海', '温哥华花园', '金秋泰和郡', '海信珠山小镇', '海逸天成',
                         '青特小镇', '中海银海一号', '万科春阳花园', '山水名园二期', '晓港名城(五期)',
                         '浮山后四小区', '万丽海景', '浮山湾花园', '深蓝中心', '万科翡翠长江',
                         '青铁华润城', '左岸风度', '逍遥花园', '鲁商首府', '鲁德海德堡',
                         '海尔山海湾', '龙湖悠山郡', '保利百合花园', '浮山后六小区', '锦绣天成',
                         '万科金色城市', '海尔世纪公馆', '青特赫山', '丽泽花园', '万科城',
                         '御景峰', '柏悦华府', '依云曦城', '上林一品', '蔚蓝创新天地',
                         '融创御府', '广佛颐景园', '荔园新天地', '友谊大街18号街坊', '星海岸',
                         '金地天玺', '翠湖绿洲', '梧桐苑', '弘信山庄', '中海临安府',
                         '东逸湾(别墅)', '世纪华庭', '宝翠花园', '龙光水悦龙湾', '藏珑华府',
                         '半岛碧桂园', '都市豪园', '仙湖别墅', '惠景城', '雅居蓝湾',
                         '尚观嘉园', '阳光山色', '青春小区', '颐山源墅', '颐和国际',
                         '致越优城', '优山美地', '保利海德公园', '星湖湾', '影都学府',
                         '绿岛明珠', '天台山庄', '时代领峰', '国际城名苑', '保集半岛',
                         '保利东御花园', '碧桂园翡翠湾', '保利西雅图', '中恒海晖城', '嘉乐花园',
                         '金海岸花园', '绿地未来城', '尚辉苑', '南江壹号', '南江壹号',
                         '长华国际中心', '丽日豪庭', '北滘海琴水岸', '万科沁园', '丽泽花园',
                         '永盛新阳光', '柳湖花园', '山水庄园', '御景花园', '南江名郡',
                         '紫金玉澜', '长信东海银湾', '丹灶碧桂园', '青春小区', '青春小区',
                         '名汇浩湖湾', '广夏花园', '海琴湾', '保利东景花园', '新城云昱',
                         '天悦湾花园', '美的翰湖苑', '招商臻园', '荟景豪庭', '如意花园',
                         '同济广场', '金地悦荔', '岭南天地璟廷', '龙光水悦云天', '江山瑞城',
                         '红星天悦', '保利外滩一号', '金地九珑璧', '碧桂园钻石湾', '泰地世锦园',
                         '光明花半里', '新君汇花地湾', '鹿璟村', '美的领贤公馆', '君御花园',
                         '恒大帝景', '帝景蓝湾', '雅丽豪庭', '红星天悦', '鲁能公馆',
                         '凤起兰庭', '珠水豪庭', '花苑广场', '雅瑶绿洲', '顺德居小区',
                         '保利花园(六期)', '鼎太风华', '馥室成双(一期)', '二冶小区街坊', '鹿港小镇',
                         '自由路8号街坊', '恒大华府', '保利罗兰香谷', '保利拉菲公馆(二三期)', '滨海名都',
                         '东河国际商住城', '日月豪庭', '光辉佳苑', '文雅苑', '迎宾小区',
                         '万达广场万达小区', '中冶世家', '景苑花园', '保利花园(三期)', '青福新城',
                         '东方俪城', '富力城(EFG区)', '丰盈小区', '富强路七号街坊', '居然新城',
                         '锦尚国际', '奥宇新城', '阿尔丁小区', '三江尊园', '现代城',
                         '文馨苑', '新星壹品', '邻圃道街坊', '惠民小区(昆都仑)', '正翔国际枫景苑',
                         '新星美地', '维多利华府', '口岸花苑', '凡尔赛颐阁(凡尔赛观邸)', '友谊17号街坊',
                         '欧风丽景', '保利花园(一期)', '欧鹿生活城', '文脉苑', '东亚香堤丽舍',
                         '乌兰小区', '佳园小区', '富强路十号街坊', '阳光小区', '翡丽湾',
                         '檀香湾', '维多利摩尔城', '恒大名都', '友谊大街22号东街坊', '文博苑',
                         '青山路1号街坊', '京奥港花园', '凯旋豪庭', '六合新城(二区南)', '富丽佳园',
                         '绿地国际花都', '景富家园(C区)', '中建御澜世家', '阿南小区', '松石国际城石榴花园',
                         '中冶世家水晶城', '华丽家族', '美室层双', '松石名第', '燕赵锦河湾',
                         '牡丹花园', '友谊小区(南区)', '合志家', '园文芳苑', '山水佳苑',
                         '万郡大都城', '华发新城', '包钢友谊十三小区', '丽晶名邸', '金茂豪庭',
                         '少先路31号街坊', '百兴小区', '佳福小区', '首创加州郡府', '锦林花园',
                         '昆河壹号', '馥室成双(二期)', '青山路五号街坊', '恒基景苑', '振华二区',
                         '紫金华府', '保利花园(二期)', '富强路一号街坊', '健康新城', '望园小区',
                         '嘉园泊景湾', '新元华庭', '金沙华府', '育才小区', '龙熙盛景',
                         '呼得木林大街10号街坊', '青东华庭', '黄河小区', '呼得木林大街11号街坊', '中冶世家华庭',
                         '明日星城知情苑', '富力华庭', '锡华世纪花园', '自由路7号街坊', '保利花园(四期)',
                         '水岸花都', '鹿鸣苑', '青年路8号街坊', '龙苑小区(B区)', '富贵佳园',
                         '高新花园', '丰景佳苑', '荣资梦乡', '胜达小区', '检察馨苑',
                         '青山路六号街坊', '居然青年城', '少先路二十二号街坊', '大连新型居住区温馨园',
                         '保利拉菲公馆(一期)',
                         '桐荷嘉苑', '远洲国际城', '青11号街坊', '广基花园', '茂业天地',
                         '和发紫薇园', '城际美景', '丰景御苑', '裕民新城理想城', '东方花园',
                         '天疆骊城', '纺织社区', '惠德花园', '海威小区(二区)', '青松小区(二区)',
                         '铭峰佳苑', '景富家园(B区)', '颐和山庄', '大连新型居住区春意园', '鹿景苑',
                         '青云小区一段', '阳光尚品(南区)', '滨江国际阅江台', '融茂第一城(C1区)', '意城晶华',
                         '富强路三号街坊', '友谊大街19号街坊(一区)', '大连新型居住区长熙园', '幸福路7号街坊',
                         '华天云居',
                         '振翔小区', '神华佳苑', '幸八雅园', '当代左岸绿洲', '江南文枢苑',
                         '滨江国际澜泊湾', '丰产道一号街坊', '青年路10号街坊', '明日星城知乐苑', '新星水岸花园',
                         '北梁新区南二区', '幸福路5号街坊(哈达道)', '向阳花苑', '青年路7号街坊', '闽辉禧瑞都',
                         '友谊大街27号街坊', '瀚星华府', '龙昱华府', '景富家园(F区)', '友谊嘉园(三期)',
                         '怡然苑', '南排小区', '赛音小区(西区)', '天赐新城(B区)', '万达嘉园',
                         '金泰花园', '明日星城德景苑', '通顺东二区', '当代菁英国际', '友谊大街25号街坊',
                         '呼得木林大街7号街坊', '加州郡府融邦', '万新家园', '民馨家园(二区)', '呼得木林新天地2区',
                         '北大恒苑', '万和城(二期)', '东豪国际城', '自由路5号街坊', '明日星城知雅苑',
                         '钢铁大街36号街坊', '海威小区(五区)', '呼得木林大街14号街坊', '西五街房管楼小区(体育场南路)',
                         '碧水嘉苑',
                         '巨力时代', '民主路5号街坊', '汇金小区', '景晟开元', '瑞春园',
                         '金辉华府', '恩和小区', '喜瑞都御府', '钢铁大街18号街坊', '国际新城(南区公寓)',
                         '电力佳苑', '健康阳光城(北区)', '和平路西小区', '祥和苑', '幸福路1号街坊',
                         '龙苑小区(A区)', '北梁新区南四区', '鑫泰豪庭', '天福广场', '友谊大街23号街坊',
                         '海威小区(四区)', '银苑小区', '康乐小区(西区)', '中晟华悦', '保利香槟湾(保利香槟花园)',
                         '兵工华居', '西河景苑', '都兰小区', '友谊大街16号街坊', '公园大道',
                         '自由路4号街坊', '中冶世家荣园', '园林新村', '内蒙古地勘五院', '恒大帝景',
                         '苏宁广场', '朝阳小区(一区)', '佳禾公寓', '滨江国际王俯景', '青松小区(五区)',
                         '一化小区', '民馨家园(六区)', '瑞芬小区', '青年园', '核工业208小区',
                         '沃土阳光住宅小区', '春光小区(六区)', '华清佳苑', '瑞德花园', '北梁新区西一区',
                         '万和城(一期)', '明华学府', '青年路12号街坊住宅小区', '呼得木林大街12号街坊', '少先20号街坊',
                         '友谊大街22号西街坊', '松石国际城', '中和文化广场', '大连新型居住区逸民园', '振华小区',
                         '九郡嘉园', '世纪佳苑', '民主路3号街坊', '富强路十二号街坊', '西四街小区',
                         '夏日花园', '宏源鑫都', '明日星城安景苑', '幸福南路16号街坊', '青松小区(六区)',
                         '龙藏新城福地园', '大连新型居住区怡生园', '钢铁大街37号街坊', '锦绣嘉园', '融茂第一城(A区)',
                         '矿机小区', '保成上元名府(南区)', '幸福路2号街坊', '幸福路10号街坊', '青宾小区',
                         '矿办小区', '金泰丽苑', '民馨家园A区', '海湖豪庭', '赛音道五号街坊',
                         '西脑包康乐小区', '璟华苑', '三电住宅小区', '时代天骄', '明日星城知书苑',
                         '通顺西二区', '友谊小区(北区)', '山水文苑', '富强路四号街坊', '呼得木林新天地1区',
                         '当铺佳苑', '香林美地', '华丰园', '鹿城福满园', '明日星城文景苑(北区)',
                         '青苑小区', '公二街住宅小区', '春阳小区', '边防佳苑', '九中小区',
                         '鹿苑小区', '丰产道2号街坊', '绿都花庭', '顺鑫望潮苑(别墅)', '绿苑豪庭',
                         '傲北上城', '胜源滨河新城', '御融公馆(公寓住宅)', '青年路18号街坊', '总部经济园',
                         '田城康都苑', '赛音道六号街坊', '中慧新城', '美岸华庭(北区)', '呼得木林大街9号街坊',
                         '友谊大街26号街坊', '海威小区十区', '电力小区(青山)', '南海五村', '警官花园',
                         '信德雅居', '神力龙园', '怡景园', '三克拉', '天赐新城(A区)',
                         '贵发山庄(一期)', '步步高东苑', '朝阳小区(二区)', '太阳城', '青年路14号街坊',
                         '绿苑小区(东河)', '颐和山庄(半山湖)', '友谊大街31号小区', '安富小区', '天安雅居',
                         '草原小区', '青六新苑', '青云二段', '团结大街11号街坊', '新春小区',
                         '兵工佳苑', '胜达花苑', '福宇小区', '新桃园小区', '西一街小区',
                         '兰溪花园', '金桂名园', '福泰嘉苑', '安泰华庭', '保利钻石小区',
                         '迎宾道一号街坊', '幸福路9号街坊', '东方嘉苑', '永茂泉阳光小区', '横泰佳苑',
                         '明德花园(公寓住宅)', '少先路29号街坊盛世嘉苑', '友谊大街15号街坊', '御景华庭',
                         '团结大街8号街坊',
                         '自由路10号街坊', '青松小区(七区)', '北新街小区(北新苑东区)', '怡荷豪庭', '信合龙湾半岛',
                         '曹欣小区', '北梁新区西二区', '赛音道一号街坊', '健康阳光城(南区)', '光辉小区(三区)',
                         '古邑人家', '近水楼台', '龙藏新城雅典苑', '东海花园', '景富家园(A区)',
                         '国际新城(北区)', '工业路2号街坊', '松石雅居', '钢铁大街24号街坊', '锦裕园小区',
                         '青甲12号街坊', '团结22号街坊', '景天花园(一期)', '龙丰苑', '保利公园壹号',
                         '海威小区(六区)', '攀宇小区', '宁馨佳园', '丰产道3号街坊', '方兴观澜壹号']
    # 随机选择小区名
    area_name = random.choice(area_address_name)
    # 随机选择楼栋
    build_number = str(random.choice(
        [1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29, 30, 31, 32])
    )
    # 随机选取单元号
    unit_number = str(random.choice(
        [1, 2, 3, 5, 6, 7])
    )
    # 随机选择楼层号
    floor_number = str(random.choice(
        [1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27])
    )
    # 随机选择门牌号
    house_number = str(random.choice(
        [1, 2, 3])
    )
    result_address_name = demo + area_name + build_number + "号" + unit_number + "单元" + floor_number + "0" + house_number + "室"
    return result_address_name

"""this is persion input function"""
str1 = "龙洞堡御景新城大田大道"
str2 = "水川镇转青城镇西巴路"
print("##########请勿乱分享，珍惜劳动付出，谢谢！！！！")
while True:
    print("*  1 : 龙洞堡御景新城大田大道")
    print("*  2 : 水川镇转青城镇西巴路")
    while True:
        try:
            persion_input = int(input('请输入地址头对应的数字：'))
            break
        except ValueError:
            print('!!!!输入有误请重新输入==>[1或者2]!!!')
            continue
    if persion_input == 1:
        demo = str1
        print("==> 需要生成的地址前标记为：  " + demo)
        break
    elif persion_input == 2:
        demo = str2
        print("==> 需要生成的地址前标记为：  " + demo)
        break
    else:
        print('您输入的不正确，请重新输入')
        continue
while True:
    try:
        enter_number = int(input('请输入生成地址个数：'))
        break
    except ValueError:
        print("输入有误请重新输入==>[整数数字]")
        continue
nt = datetime.now()
day_time = nt.strftime('%Y{y}%m{m}%d{d} %H{h}%M{mm}%S{s}').format(y='年', m='月', d='日', h='时', mm='分', s='秒')

"""This is a Main function"""
with open(day_time + "地址.txt", "w", encoding='utf-8') as f:
    for i in range(enter_number):
        person_address = address()
        f.write(person_address)
        f.write('\n')
    f.close()
print(
    """
 _              _    _
| |            | |  ( )
| |       ___  | |_ |/  ___    __ _    ___
| |      / _ \ | __|   / __|  / _` |  / _ \
| |____ |  __/ | |_    \__ \ | (_| | | (_) |
\_____/  \___|  \__|   |___/  \__, |  \___/
                               __/ |
                             |___/
"""
)


# -*- coding: utf-8 -*-
"""
Created on Tue Jul 19 18:41:54 2022

@author: Vikki
"""
class Node:
    def __init__(self, data, next = None, prev=None):
        self.data = data
        self.next = next
        self.prev = prev

class Linkedlist:
    def __init__(self,header = None, tail = None):
        self.header = header
        self.tail = tail

    def insert_values(self, ls):
        self.header = None
        for data in ls:
            self.insert_at_begining(data)

    def insert_at_end(self, data):
        if self.header is None:
            self.header = Node(data, None, None)
            return

        itr = self.header
        while itr.next:
            itr = itr.next

        itr.next = Node(data, None, itr)

    def insert_at_begining(self, data):
        if self.header is None:
            self.header = Node(data, None, None)
        else:
            node = Node(data, self.header, None)
            self.header.prev = node
            self.header = node

    def get_last_node(self):
        itr = self.header
        while itr.next:
            itr = itr.next

        return itr

    def print_forward(self):
         if self.header is None:
             print("Linked list is empty")
             return

         itr = self.header
         llstr = ''
         while itr:
             llstr += str(itr.data) + ' --> '
             itr = itr.next
         print(llstr)

    def print_backward(self):
         if self.header is None:
             print("Linked list is empty")
             return

         itr = self.get_last_node()
         llstr = ''
         while itr:
             llstr += str(itr.data) + ' --> '
             itr = itr.prev
         print(llstr)

    def insert_at_end(self, data):
        itr = self.get_last_node()
        itr.next = Node(data, None, itr.prev)

    def insert_at(self, index, data):
        itr = self.header
        i = 0
        while itr:
            if i == index:
                node = Node(data, itr.next, itr)
                if node.next:
                    node.next.prev = node
                itr.next = node
                break
            i += 1
            itr = itr.next

if __name__ == "__main__":
    ll = Linkedlist()
    ll.insert_values(["banana","mango","grapes","orange"])
    ll.print_forward()
    ll.print_backward()
    ll.insert_at_end("figs")
    ll.print_forward()
    ll.insert_at(3,"jackfruit")
    ll.print_forward()


from pathlib import Path

ROOT_FOLDER = Path("STOCK_VOLATILITY_NEW").resolve().parent

DATASET_DIR = ROOT_FOLDER / "data"

ALL_DATA_DIR = DATASET_DIR / "all_data.csv"
ALL_DATA_NEW_DIR = DATASET_DIR / "all_data_new.csv"
UNPROCESSED_DATA = DATASET_DIR / "index_funds_data.csv"
FORMATTED_DATA = DATASET_DIR / "formatted_data.csv"
DATA_FOR_ANALYSIS = DATASET_DIR / "new_formatted_data.csv"
EARNINGS_DATA = DATASET_DIR / "earnings.csv"
CPI_DATA = DATASET_DIR / "cpi.csv"
MARKET_SCHEDULE = DATASET_DIR / "nasdaq_schedule.csv"
TRAIN_DATA_PATH = DATASET_DIR / "train.csv"
VALID_DATA_PATH = DATASET_DIR / "valid.csv"
TEST_DATA_PATH = DATASET_DIR / "test.csv"

TICKERS = ["XLK", "XLP", "XLF", "XLV", "XLE", "XLI", "XLU"]

FORECAST_HORIZONS = ["one_days", "two_days", "three_days", "four_days", "five_days"]
ADDITIONAL_OUTPUT_COLS = ["volatility_target", "date", "ticker", "idx"]

LIGHTNING_LOGS_DIR = ROOT_FOLDER / "lightning_logs"
IMAGE_PATH = ROOT_FOLDER / "img"
MODEL_DATA = ROOT_FOLDER / "model_data"
BEST_MODEL_PATH = MODEL_DATA / "checkpoint.ckpt"
TIMESERIES_DATASET_PARAMS = MODEL_DATA / "ts_dataset_params.joblib"

PRES_ELECTION_DATES = [
    "2004-11-02",
    "2008-11-04",
    "2012-11-06",
    "2016-11-08",
    "2020-11-03",
]
MIDTERM_ELECTION_DATES = [
    "2006-11-07",
    "2010-11-02",
    "2014-11-04",
    "2018-11-06",
    "2022-11-08",
]
ELECTION_DATES = PRES_ELECTION_DATES + MIDTERM_ELECTION_DATES


import json
import pandas as pd
import yfinance as yf
import pytz
from datetime import datetime, timedelta
import time

# Read forex pairs from JSON file
with open('forex_pairs.json', 'r') as file:
    forex_pairs = json.load(file)

# Define the time frame and time zone
timeframe = '1h'
timezone = 'Africa/Nairobi'

# Define the number of periods for Moving Average calculation
period = 500

# Get the current time in Kenya time zone
kenya_tz = pytz.timezone(timezone)
current_time = datetime.now(kenya_tz)

# Define the start and end times for data retrieval
end_time = current_time
start_time = current_time - timedelta(hours=period)

# Save the signals to a file
filename = 'MA_signals.txt'
with open(filename, 'w') as file:
    file.write(f"Today: {end_time}\n\n")

    # Generate MA signals for each forex pair
    for pair in forex_pairs:
        symbol = pair + '=X'
        retry_count = 0
        success = False
        data = None
        while data is None:
            try:
                # Download historical price data using yfinance
                data = yf.download(symbol, start=start_time, end=end_time, interval=timeframe)
            except Exception as e:
                print("Error occurred:", e)
                print("Retrying...")

        if not data.empty and len(data) >= 1:
            # Extract the 'Close' prices from the downloaded data
            close_prices = data['Close'].tolist()

            # Define the window sizes for the moving averages
            window_short = 20  # Short-term moving average window size
            window_long = 50  # Long-term moving average window size

            # Calculate the moving averages
            moving_avg_short = sum(close_prices[-window_short:]) / window_short
            moving_avg_long = sum(close_prices[-window_long:]) / window_long

            # Get the latest closing price
            latest_price = close_prices[-1]
            entry_price = latest_price  # Replace with your actual entry price
            lot_size = 0.01  # Replace with your actual lot size
            # Define risk-reward ratios
            take_profit_ratio = 0.002  # 0.2%
            stop_loss_ratio = 0.001  # 0.1%

            # Determine the buy, sell, or hold signal based on moving average crossovers and price relationship
            if moving_avg_short > moving_avg_long and latest_price > moving_avg_short:
                signal = "Buy"

            elif moving_avg_short < moving_avg_long and latest_price < moving_avg_short:
                signal = "Sell"

            else:
                signal = "Hold"

            if signal == "Sell":
                current_price = latest_price
                take_profit = current_price - (current_price * take_profit_ratio)
            elif signal == "Buy":
                current_price = latest_price
                take_profit = current_price + (current_price * take_profit_ratio)
            else:
                take_profit = 0.0

            rounded_number = round(take_profit, 6)

            file.write(f"MA Signals for {pair} ({timeframe} timeframe): {signal} take_profit: {rounded_number}\n")
        else:
            print("No data available to generate a signal.")


from django.shortcuts import get_object_or_404, render, redirect, HttpResponseRedirect
from django.views.generic import TemplateView, UpdateView
from django.contrib.auth import get_user_model
from .models import Message
from django.urls import reverse
from django.contrib import messages
from review.models import Review
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from .forms import EditProfileForm
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
# Create your views here.

from django.template import RequestContext

def handler404(request, *args, **argv):
    response = render('404.html', {}, context_instance=RequestContext(request))
    response.status_code = 404
    return response

class HomePageView(TemplateView):
    template_name = 'home.html'

def MessageView(request, username):
    User = get_user_model()
    message_user = get_object_or_404(User, username=username)
    all_messages = message_user.messages.all()
    if request.method == 'POST':
        message = request.POST['message']
        user_new_message = Message.objects.create(
            customuser=message_user, text=message)
        user_new_message.save()
        messages.success(
            request, 'Compliments dropped successfully, Create your own Account below to receive and give Anonymous compliments')
        return redirect('account_signup')

    return render(request, 'message.html', {'all_messages': all_messages,
                                            'message_user': message_user,
                                            })

class UserProfile(LoginRequiredMixin, TemplateView):
    template_name = 'user_profile.html'

CustomUser = get_user_model()

@login_required(login_url='account_login')

def EditProfile(request, username):
    user = get_object_or_404(get_user_model(), username=username)
    if request.method == 'POST':
        form = EditProfileForm(request.POST, instance=user)
        if form.is_valid():
            form.save()
            # user_profile = user.get_absolute_url()
            user_profile = reverse('user_profile')
            return HttpResponseRedirect(user_profile)

    form = EditProfileForm(instance=user)
    return render(request, 'edit_profile.html', {'form': form})

@login_required(login_url='account_login')
def delete_message(request, m_id):
    message_instance = get_object_or_404(Message, id=m_id)
    message_instance_user = message_instance.customuser
    if message_instance_user == request.user:
        message_instance.delete()
        return redirect(message_instance_user.get_absolute_url())
    else:
        return redirect('home')

@login_required(login_url='account_login')
def spam_message(request, m_id):
    # get the Message filtering by the id provided in the url as *args
    message_instance = get_object_or_404(Message, id=m_id)
    # the person who created the message
    message_instance_user = message_instance.customuser
    if message_instance_user == request.user:
        message_instance.text = 'The owner has marked this message as a spam.'
        message_instance.save()
        return redirect(message_instance_user.get_absolute_url())

    else:
        if request.user.is_authenticated:
            url = 'home'
        else:
            url = 'account_login'
        return redirect(url)

class AbouUs(TemplateView):
    template_name = 'about.html'

class ContactUs(TemplateView):
    template_name = 'contact.html'

def ReviewView(request):
    reviews = Review.objects.all()
    template_name = "review.html"
    paginator = Paginator(reviews, 5)
    page_number = request.GET.get('page')
    try:
        page_obj = paginator.get_page(page_number)
    except PageNotAnInteger:
        page_obj = paginator.page(page_number)

    except EmptyPage:
        page_obj = paginator.page(paginator.num_pages)

    return render(request, template_name, {'page_obj': page_obj})

@login_required(login_url='account_login')
def SettingsView(request):

    return render(request, 'settings.html', {})

def AddReview(request):
    if request.method == 'POST':
        name = request.POST['name']
        review = request.POST['review']
        occupation = request.POST['occupation']

        new_review = Review.objects.create(
            name=name, review=review, occupation=occupation)
        new_review.save()
        messages.success(request, 'Review submitted successfully')
        return redirect('review')
    return render(request, 'add_review.html', {})


from __future__ import absolute_import
from __future__ import print_function

import numpy as np
import tensorflow as tf

TOWER_NAME = 'tower'

def sparsity_hook_forward(x_list):
  """Helper to create summaries of sparsity.

  Creates a summary that measures the sparsity of a tensor.

  Args:
    x_list: a list of tensor
  Returns:
    tensor and sparsity tuble
  """

  retrieve_list = []
  for x in x_list:
    tensor_name = x.op.name
    sparsity = tf.nn.zero_fraction(x)
    tf.summary.scalar(tensor_name + '/sparsity', sparsity)
    retrieve_list.append((x, sparsity))
  return retrieve_list

def sparsity_hook_backward(loss, x_list):
  """Helper to create summaries for gradients of intermediate results in
  backward pass.

  Creates a summary that measures the sparsity of gradients of intermediate
  results in backward pass.

  Args:
    loss: the loss
    x_list: a list of Tensors
  Returns:
    a list of tensor and sparsity tuple
  """
  gradient_list = tf.gradients(loss, x_list)
  grad_retrieve_list = []
  for g in gradient_list:
    tensor_name = g.op.name
    sparsity = tf.nn.zero_fraction(g)
    tf.summary.scalar(tensor_name + '/sparsity', sparsity)
    grad_retrieve_list.append((g, sparsity))
  return grad_retrieve_list

def get_non_zero_index(a, shape):
  raw_index = np.where(a != 0)
  n_dim = len(raw_index)
  assert n_dim == 4 or n_dim == 2
  n_data = len(raw_index[0])
  index_list = []
  if n_dim == 4:
    size_chw = shape[1].value * shape[2].value * shape[3].value
    size_hw = shape[2].value * shape[3].value
    size_w = shape[3].value
  elif n_dim == 2:
    size_c = shape[1].value
  for i in range(n_data):
    if n_dim == 4:
      index = raw_index[0][i] * size_chw + raw_index[1][i] * size_hw + raw_index[2][i] * size_w + raw_index[3][i]
    elif n_dim == 2:
      index = raw_index[0][i] * size_c + raw_index[1][i]
    index_list.append(index)
  return index_list

def calc_index_diff_percentage(index_list, ref_index_list, sparsity, all_counts):
  percentage = 1.0
  n_idx = float(len(index_list))
  n_ref_idx = float(len(ref_index_list))
  #print("Current non-zero data size: ", len(index_list))
  #print("Previous non-zero data size: ", len(ref_index_list))
  all_index = np.concatenate((index_list, ref_index_list), axis=0)
  #print("Merged non-zero data size: ", len(all_index))
  #print("Unique non-zero data size: ", len(np.unique(all_index, axis=0)))
  unchanged_counts = len(all_index) - len(np.unique(all_index, axis=0))
  diff_counts = (n_idx - unchanged_counts) + (n_ref_idx - unchanged_counts)
  #print("Differenct counts: ", diff_counts)
  percentage = float(diff_counts) / all_counts
  return percentage

def feature_map_extraction(tensor, data_format, batch_index, channel_index):
  # The feature map returned will be represented in a context of matrix
  # sparsity (1 or 0), in which 1 means non-zero value, 0 means zero
  n_dim = len(tensor.shape)
  if n_dim == 4:
    if data_format == "NCHW":
      extracted_subarray = tensor[batch_index,channel_index,:,:]
    elif data_format == "NHWC":
      extracted_subarray = tensor[batch_index,:,:,channel_index]
  if n_dim == 2:
    extracted_subarray = tensor
  extracted_subarray[np.nonzero(extracted_subarray)] = 1
  return extracted_subarray

def zero_block_ratio_matrix(a, shape, block_size):
  '''
  Args:
    a: a numpy n-d array (tensor)
    shape: the tensor shape
  Return:
    The count of zero blocks
  '''
  n_dim = len(shape)
  if n_dim == 2:
    n_row = shape[0].value
    n_col = shape[1].value
    matrix = a
  elif n_dim == 4:
    n_row = shape[0].value
    n_col = shape[1].value * shape[2].value * shape[3].value
    matrix = a.reshape((n_row, n_col))
  n_block_row = int(n_row + block_size - 1) / int(block_size)
  n_block_col = int(n_col + block_size - 1) / int(block_size)

  n_blocks = n_block_row * n_block_col

  if n_row % block_size != 0:
    n_padded_zeros_in_row = block_size - n_block_row % block_size
  else:
    n_padded_zeros_in_row = 0
  if n_col % block_size != 0:
    n_padded_zeros_in_col = block_size - n_block_col % block_size
  else:
    n_padded_zeros_in_col = 0

  if n_padded_zeros_in_row != 0 or n_padded_zeros_in_col != 0:
    padded_zeros_in_row = np.zeros((n_padded_zeros_in_row, n_col))
    padded_zeros_in_col = np.zeros((n_row+n_padded_zeros_in_row, n_padded_zeros_in_col))
    padded_a = np.concatenate((np.concatenate((matrix, padded_zeros_in_row), axis=0),\
                padded_zeros_in_col), axis=1)
  else:
    padded_a = matrix

  # Reshape the tensor column-wise first
  reshaped_a = padded_a.reshape((n_block_row, block_size, n_col))
  # Sum the elements within each block column-wise
  summed_a_row = np.sum(reshaped_a, axis=1)
  # Reshape the summed array to a new tensor row-wise
  reshaped_a = summed_a_row.reshape((n_block_row, n_block_col, block_size))
  # Sum the elements within each block row-wise
  summed_a = np.sum(reshaped_a, axis=2)
  zero_element_indices = np.where(summed_a == 0)
  zero_counts = len(zero_element_indices[0])

  return float(zero_counts)/float(summed_a.size)


"""
Created by kevin-desktop, on the 18/02/2023
ADD sentiment columns to a sample Excel sheet.
"""
import numpy as np
import pandas as pd
import tqdm

from asba_model import run_models

path = "data/marco_sample.pkl"
df = pd.read_pickle(path)

dic = {}
max_nb_terms = 0
for row in tqdm.tqdm(df.itertuples(name=None)):
    ind = row[0]
    ti = row[1]
    ab = row[4]
    dtkey = row[13].split(", ")

    sentiments = run_models(ab, dtkey)
    if max_nb_terms < len(sentiments):
        max_nb_terms = len(sentiments)
    dic[ind] = sentiments

df_sent = pd.DataFrame.from_dict(dic, orient="index")
df_sent.rename(columns={i: f'Term{i + 1}' for i in range(0, max_nb_terms)}, inplace=True)

df = pd.concat([df, df_sent], axis=1)

lst_columns_w_term = [col for col in df.columns if 'Term' in col]
df.fillna('0', inplace=True)

df.to_excel("data/sample_marco.xlsx")


import struct
from enum import Enum
import typing as ty
from mate.net.nao_data import Data, DebugValue, DebugImage

NO_SUBSCRIBE_KEY = "none"
K = ty.TypeVar('K')

def split(predicate: ty.Callable[[K], bool], dictionary: ty.Dict[K, dict]):
    dict1 = {}
    dict2 = {}
    for key in dictionary:
        if predicate(key):
            dict1[key] = dictionary[key]
        else:
            dict2[key] = dictionary[key]
    return dict1, dict2

class DebugMsgType(Enum):
    subscribe = 0
    unsubscribe = 1
    update = 2
    request_list = 3
    list = 4
    subscribe_bulk = 5
    image = 6

class ConfigMsgType(Enum):
    set = 0             # Sets a given key to a given value (at runtime)
    get_mounts = 1      # ask for send_mounts, containing all mounts
    get_keys = 2        # ask for send_keys of a given key
    save = 3            # saves the current config
    send_keys = 4       # containing key, value
    send_mounts = 5     # containing filename, key

class ConnectionStatusType(Enum):
    disconnected = 0
    connected = 1
    connection_lost = 2
    connection_refused = 3

class Message:
    def __init__(self,
                 type: DebugMsgType,
                 body: str = "",
                 length: int = None,
                 version: int = 1):
        self.type = type
        self.body = body
        self.length = length if length is not None else max(0, len(body))
        self.version = version

    def __str__(self):
        return "{}|v{}|{}|{}|{}".format(
            type(self).__name__, self.version, self.type.name, self.length,
            self.body)

class ConfigMessage(Message):
    def __init__(self,
                 type: DebugMsgType,
                 body: str = "",
                 length: int = None,
                 version: int = 1):
        super(ConfigMessage, self).__init__(type, body, length, version)

    @staticmethod
    def header_from_bytes(msg):
        if len(msg) >= 8:
            fmt = "<4sBBH"
            (msg_head, raw_version, raw_type, msg_size) = struct.unpack(
                fmt, msg[:8])
            return (msg_head, raw_version, ConfigMsgType(raw_type), msg_size)

    def toBytes(self):
        msg_format = "<4sBBH{}s".format(len(self.body))
        return struct.pack(msg_format, b'CONF', self.version, self.type.value,
                           self.length, self.body.encode())

class DebugMessage(Message):
    def __init__(self,
                 type: DebugMsgType,
                 body: str = "",
                 length: int = None,
                 version: int = 1):
        super(DebugMessage, self).__init__(type, body, length, version)

    def toBytes(self):
        fmt = "<4sbbxxIxxxx{}s".format(self.length)
        return struct.pack(fmt, b'DMSG', self.version, self.type.value,
                           self.length, self.body.encode())

    @staticmethod
    def header_from_bytes(msg):
        if len(msg) >= 16:
            fmt = "<4sbbxxIxxxx"
            (msg_head, raw_version, raw_type, msg_size) = struct.unpack(
                fmt, msg[:16])
            return (msg_head, raw_version, DebugMsgType(raw_type), msg_size)

    @staticmethod
    def to_body(type, msg):
        if type == DebugMsgType.image:
            return msg
        else:
            return msg.decode(errors='ignore')

    @staticmethod
    def get_image(body):
        fmt = "<HHH"
        (width, height, key_length) = struct.unpack(fmt, body[:6])
        return body[6:6 + key_length].decode(), width, height, body[
            6 + key_length:]

    @staticmethod
    def parse_data(d: dict) -> Data:
        if d.get("isImage", False):
            return DebugImage(
                d["key"],
                d.get("width", 0),
                d.get("height", 0),
                d.get("value", b'')
            )
        else:
            return DebugValue(
                d["key"],
                d.get("timestamp", 0),
                d.get("value", 0)
            )


# Import packages
import gpxpy
import numpy as np

# Read gpx-file
gpxFile = "yourfile.gpx"
gpx_file = open(gpxFile, 'r')
gpx      = gpxpy.parse(gpx_file)

# Calculate speeds between points
speed = []

for track in gpx.tracks:
  for segment in track.segments:
    for point_no, point in enumerate(segment.points):
        speed.append(point.speed_between(segment.points[point_no - 1]))

# Upper limit is defined as 3x the 75% quantile, this can be tweaked according to the GPS errors encountered
upperLimit = 3*(np.quantile(speed, q = 0.75))

# Find elements above the threshold
indices = [
    index for index, item in enumerate(speed)
    if item > upperLimit
]

pointsRemoved = 0

while len(indices) > 0:
    gpxpy.gpx.GPXTrackSegment.remove_point(gpx,indices[0])
    pointsRemoved = pointsRemoved + 1
    # Calculate speeds between points
    speed = []

    for track in gpx.tracks:
        for segment in track.segments:
            for point_no, point in enumerate(segment.points):
                speed.append(point.speed_between(segment.points[point_no - 1]))

    indices = [
        index for index, item in enumerate(speed)
        if item > upperLimit
    ]

print(pointsRemoved)

# Write the corrected GPX file
outputFile = gpxFile[:-4] + "_corrected.gpx"
with open(outputFile, "w") as f:
    f.write( gpx.to_xml())


# Read values PMS5003 and return as dict
def read_pms5003(pms5003):
    values = {}
    try:
        pm_values = pms5003.read()  # int
        values["pm1"] = pm_values.pm_ug_per_m3(1)
        values["pm25"] = pm_values.pm_ug_per_m3(2.5)
        values["pm10"] = pm_values.pm_ug_per_m3(10)
    except ReadTimeoutError:
        pms5003.reset()
        pm_values = pms5003.read()
        values["pm1"] = pm_values.pm_ug_per_m3(1)
        values["pm25"] = pm_values.pm_ug_per_m3(2.5)
        values["pm10"] = pm_values.pm_ug_per_m3(10)
    return values

# Read values from BME280 and return as dict
def read_bme280(bme280):
    # Compensation factor for temperature
    comp_factor = 2.25
    values = {}
    cpu_temp = get_cpu_temperature()
    raw_temp = bme280.get_temperature()  # float
    comp_temp = raw_temp - ((cpu_temp - raw_temp) / comp_factor)
    values["temperature"] = int(comp_temp)
    values["pressure"] = round(
        int(bme280.get_pressure() * 100), -1
    )  # round to nearest 10
    values["humidity"] = int(bme280.get_humidity())
    data = gas.read_all()
    values["oxidised"] = int(data.oxidising / 1000)
    values["reduced"] = int(data.reducing / 1000)
    values["nh3"] = int(data.nh3 / 1000)
    values["lux"] = int(ltr559.get_lux())
    return values

disp = ST7735.ST7735(
    port=0,
    cs=ST7735.BG_SPI_CS_FRONT,
    dc=9,
    backlight=12,
    rotation=90)

disp.begin()

img = Image.new('RGB', (disp.width, disp.height), color=(0, 0, 0))
draw = ImageDraw.Draw(img)

while True:
    low, mid, high, amp = noise.get_noise_profile()
    low *= 128
    mid *= 128
    high *= 128
    amp *= 64

    img2 = img.copy()
    draw.rectangle((0, 0, disp.width, disp.height), (0, 0, 0))
    img.paste(img2, (1, 0))
    draw.line((0, 0, 0, amp), fill=(int(low), int(mid), int(high)))

    disp.display(img)


# Flatten a Linked List

class Node:
    def __init__(self, head, bottom): # child and down are same
        self.head = head
        self.next = None
        self.bottom = bottom

def flatten(head):
    if head is None:
        return None
    temp = None
    tail = head
    while tail.next is not None:
        tail = tail.next

    curr = head
    while curr is not tail:
        if curr.bottom is not None:
            tail.next = curr.bottom
            temp = curr.bottom

            while temp.next is not None:
                temp = temp.next
            tail = temp

        curr = curr.next
    return head


""" this is a mixture of the best #free twitter sentimentanalysis modules on github.
    i took the most usable codes and mixed them into one because all of them
    where for a linguistical search not usable and did not show a retweet or a full tweet
    no output as csv, only few informations of a tweet, switching language
    or even to compare linguistic features in tweets of two different langauges and etc. etc ...
    special and many many thanks to https://github.com/vprusso/youtube_tutorials who showed on his
    page a tutorial on how to do a sentimentanalysis with python
    i did this for users with not much skills and linguistical background to help them to get a corpus of twitterdata
    and to show them how to do a comparison between sentence based vs document based sentimentanalysis
    credits to all AVAILABLE FREE AND SIMPLE sentimentanalysis programms (dec. 2019) on github.
    many thanks to everybody and of course to github for making this exchange and usage possible!
    cemre koc (Goethe University, Frankfurt) Python3.7
"""

from textblob import TextBlob   #Sentimentlexikon FOR GERMAN (TEXTBLOB_DE import textblob_de
import re                       #modul for regular expressions

from tweepy import API                      #Twitter API modul for more info: look tweepy doc please!
from tweepy import Cursor
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream

import tweepy                           #usage of diffrent feautres of my programm
import sys                              #only if wanted
import csv                              ##only if wanted (see rest of programm)
import pandas as pd                     #pandas for illustration
import authentification                 #access to twitter
import numpy as np                      #collection of tweets via numpy
import matplotlib.pyplot as plt         #if needed (see below for ploting)
import numpy

#output screen (if you use pycharm for full screen view)
#only if needed
pd.set_option('display.max_rows', 1000000000000)
pd.set_option('display.max_columns', 1000000000)
pd.set_option('display.width', 100000000000)
pd.set_option('display.float_format', '{:20,.2f}'.format)

#for maximal OUTPUT!
#pd.set_option('display.max_colwidth', -1)

#TWITTER AUTHENTIFICTION (Twitter development)
#please fill for that the identification.py with your credentials!
#you need a twitter developer account for getting these informations
class TwitterAuthenticator():

    def authenticate_twitter_app(self):
        auth = OAuthHandler(authentification.CONSUMER_KEY, authentification.CONSUMER_SECRET)
        auth.set_access_token(authentification.ACCESS_TOKEN, authentification.ACCESS_TOKEN_SECRET)
        return auth

#TWITTER CLIENT SERVER
class TwitterClient():
    def __init__(self, twitter_user=None):
        self.auth = TwitterAuthenticator().authenticate_twitter_app()
        self.twitter_client = API(self.auth)
        self.twitter_user = twitter_user

    def get_twitter_client_api(self):
        return self.twitter_client

    def get_user_timeline_tweets(self, num_tweets):
        tweets = []
        for tweet in Cursor(self.twitter_client.user_timeline, id=self.twitter_user).items(num_tweets):
            tweets.append(tweet)
        return tweets

    def get_friend_list(self, num_friends):
        friend_list = []
        for friend in Cursor(self.twitter_client.friends, id=self.twitter_user).items(num_friends):
            friend_list.append(friend)
        return friend_list

    def get_home_timeline_tweets(self, num_tweets):
        home_timeline_tweets = []
        for tweet in Cursor(self.twitter_client.home_timeline, id=self.twitter_user).items(num_tweets):
            home_timeline_tweets.append(tweet)
        return home_timeline_tweets

#TWITTER STREAMER FOR STREAMING AND LIVE TWEETS
class TwitterStreamer():

    def __init__(self):
        self.twitter_autenticator = TwitterAuthenticator()

    def stream_tweets(self, fetched_tweets_filename, hash_tag_list):
        # AUTHENTIFICATION AND CONNECTION TO API
        listener = TwitterListener(fetched_tweets_filename)
        auth = self.twitter_autenticator.authenticate_twitter_app()
        stream = Stream(auth, listener)

        #you can use the stream.filter for defining the search for words/hasthags!!!!!!
        #same sentimentanalysis works for words or hashtags!!!
        stream.filter(track=hash_tag_list)

#TWITTER STREAM LISTENER FOR PRINTING TWEETS
class TwitterListener(StreamListener):

    def __init__(self, fetched_tweets_filename):
        self.fetched_tweets_filename = fetched_tweets_filename

    def on_data(self, data):
        try:
            print(data)
            with open(self.fetched_tweets_filename, 'a') as tf:
                tf.write(data)
            return True
        except BaseException as e:
            print("Error on_data %s" % str(e))
        return True

    def on_error(self, status):
        if status == 420:
            #OCCURS IF RATE LIMIT IS PASSED
            return False
        print(status)

#FOR ANALYZING CLEANING TWEETS (TO CONTENT)
class TweetAnalyzer():
#DELTETE ALL UNNECESSARY CHARACTERS
    def clean_tweet(self, tweet):
        return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ", tweet).split())

#SIMPLE SENTIMENTANALYSIS VIA TEXTBLOB (englisch)
    def analyze_sentiment(self, tweet):
        analysis = TextBlob(self.clean_tweet(tweet))

        if analysis.sentiment.polarity > 0:
            return 1
        elif analysis.sentiment.polarity == 0:
            return 0
        else:
            return -1
#You can use the following classification of polarity for sentence based analysis
#since i am using this programm for document level classification I left it with 1 -1  and 0
#       if (polarity == 0):
#            print("Neutral")
#     elif (polarity > 0 and polarity <= 0.3):
#            print("Schwach positiv")
#        elif (polarity > 0.3 and polarity <= 0.6):
#            print("positiv")
#        elif (polarity > 0.6 and polarity <= 1.0):
#            print("Stark positiv")
#        elif (polarity > -0.3 and polarity <= 0):
#            print("schwach negativ")
#        elif (polarity > -0.6 and polarity <= -0.3):
#            print("Negativ")
#        elif (polarity >= -1.0 and polarity <= -0.6):
#            print("Stark negativ")

    def tweets_to_data_frame(self, tweets):
        df = pd.DataFrame(data=[tweet.full_text for tweet in tweets], columns=['tweets'])
#THIS IS FOR RETWEETS OF A CERTAIN TWEET! BUT BE CARFUL ONLY A CERTAIN NUMBER OF TWEETS PER DAY!
#TWITTER RESTRICTION
#remove the """ for usage!

        """replies = []
        non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)
        for full_tweets in tweepy.Cursor(api.user_timeline, screen_name='GretaThunberg', timeout=999999).items(20):
            for tweet in tweepy.Cursor(api.search, q='to:GretaThunberg', since_id=1203618558267273225,
                                       result_type='recent',
                                       timeout=999999).items(100):
                if hasattr(tweet, 'in_reply_to_status_id_str'):
                    if (tweet.in_reply_to_status_id_str == full_tweets.id_str):
                        replies.append(tweet.text)
            print("Tweet :", full_tweets.text.translate(non_bmp_map))
            for elements in replies:
                print("Replies :", elements)
            # replies.clear()"""

#DATA SET VIA DATAFRAME TO SHOW WITH NUMPY
#YOU CAN PRINT GIVEN DATA LIKE LENGTH RETWEET NUMBER LANGUAGE etc. CHOSSE:

#['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__',
# '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '_
# _lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '_
# _sizeof__', '__str__', '__subclasshook__', '__weakref__', '_api', '_json', 'author', 'contributors', 'coordinates',
# 'created_at', 'destroy', 'display_text_range', 'entities', 'favorite', 'favorite_count', 'favorited',
# 'full_text', 'geo', 'id', 'id_str', 'in_reply_to_screen_name', 'in_reply_to_status_id', 'in_reply_to_status_id_str',
# 'in_reply_to_user_id', 'in_reply_to_user_id_str', 'is_quote_status', 'lang', 'parse', 'parse_list', 'place',
# 'possibly_sensitive', 'quoted_status', 'quoted_status_id', 'quoted_status_id_str', 'quoted_status_permalink',
# 'retweet', 'retweet_count', 'retweeted', 'retweets', 'source', 'source_url', 'truncated', 'user']

        df['id'] = np.array([tweet.id for tweet in tweets])
        df['len'] = np.array([len(tweet.full_text) for tweet in tweets])
        df['date'] = np.array([tweet.created_at for tweet in tweets])
        #df['source'] = np.array([tweet.source for tweet in tweets])
        df['likes'] = np.array([tweet.favorite_count for tweet in tweets])
        df['retweets'] = np.array([tweet.retweet_count for tweet in tweets])
        df["lang"] = ([tweet.lang for tweet in tweets])
        #df["in_reply_to_status_id_str"] = ([tweet.replies for tweet in tweets])

        return df

#Programm begins here!!

if __name__ == '__main__':

    twitter_client = TwitterClient()
    tweet_analyzer = TweetAnalyzer()

    api = twitter_client.get_twitter_client_api()
    #TWEET MODE EXTENDED FOR FULL TWEET OUTPUT!! RETWEETS STAY THE SAME!
    #COUNT= LAST TWEET NUMBER OF USER (SCREEN NAME)
    #HERE FOR GRETA THUNBERG, JUST DELETE AND TYPE ACCOUNT NAME TO CHANGE
    #FOR EXAMPLE rtErdogan (for president of turkey), realDonaldTrump (for Trump) etc...
    tweets = api.user_timeline(screen_name="GretaThunberg", count=200, tweet_mode="extended")

    #print DATA
    print(dir(tweets[0]))
    #print(tweets[0].retweet_count)  #retweet count print
    #sentimentanalysis for printing it in a dataframe with the other informations!
    df = tweet_analyzer.tweets_to_data_frame(tweets)
    df['sentiment'] = np.array([tweet_analyzer.analyze_sentiment(tweet) for tweet in df['tweets']])

    #AVARAGE LENGTH OF ALL TWEETS
    #print(np.mean(df['len']))

    # GET NUMBER OF LIKES
    #print(np.max(df['likes']))

    # GET NUMBER OF RETWEETS
    #print(np.max(df['retweets']))

    #EXAMPLE RETWEET STATUS OF A CERTAIN TWEET ID
    #To get ID you need to look on your broswers URL of this CERTAIN TWEET
    #print(np.max(df["lang"]))
    ##print(df.in_reply_to_status_id[1075801005504258061])

    #ANYWAY THERE IS A RESTRICTION SINCE 2019 ON ONLY 200 TWEETS
    #THANK YOU CAMBRIDGE ANALYTICA
    print(df.head(200))

    #    DO CSV FILE (DELETE OR NAME IT NEW TO MAKE IT SEPRATE)
    #df.to_csv('KocSentiment.csv')

    #TIME SERIES FOR CHART VIEW!!! DONT FORGET TO TURN ON MATPLOT LIBRARY
    #time_likes = pd.Series(data=df['len'].values, index=df['date'])
    #time_likes.plot(figsize=(16, 4), color='r')
    #plt.show()

    #time_favs = pd.Series(data=df['likes'].values, index=df['date'])
    #time_favs.plot(figsize=(16, 4), color='r')
    #plt.show()

    #time_retweets = pd.Series(data=df['retweets'].values, index=df['date'])
    #time_retweets.plot(figsize=(16, 4), color='r')
    #plt.show()

    #LAYERED VIEW! FOR COMPARISON !!
    #time_likes = pd.Series(data=df['likes'].values, index=df['date'])
    #time_likes.plot(figsize=(16, 4), label="likes", legend=True)

    #time_retweets = pd.Series(data=df['retweets'].values, index=df['date'])
    #time_retweets.plot(figsize=(16, 4), label="retweets", legend=True)
    #plt.show()


import os
import sys
from PyQt5.QtWidgets import QFrame, QSizePolicy

def isFloat(s: str):
    try:
        float(s)
    except ValueError:
        return False
    return True

def isInt(s: str):
    try:
        int(s)
    except ValueError:
        return False
    return True

def returnFloat(s: str):
    try:
        float(s)
    except ValueError:
        return -1
    return float(s)

def returnInt(s: str):
    try:
        int(s)
    except ValueError:
        return -1
    return int(s)

def convertPressureUnits(value: float, fromUnit: str = "Pa", toUnit: str = "Pa"):
    conversionFactor = 1
    if fromUnit == "Pa":
        conversionFactor *= 1
    elif fromUnit == "kPa":
        conversionFactor *= 1000
    elif fromUnit == "MPa":
        conversionFactor *= 1000000
    elif fromUnit == "bar":
        conversionFactor *= 100000

    if toUnit == "Pa":
        conversionFactor /= 1
    elif toUnit == "kPa":
        conversionFactor /= 1000
    elif toUnit == "MPa":
        conversionFactor /= 1000000
    elif toUnit == "bar":
        conversionFactor /= 100000

    return value * conversionFactor

def convertTimeUnits(value: float, fromUnit: str = "s", toUnit: str = "s"):
    conversionFactor = 1
    if fromUnit == "s":
        conversionFactor *= 1
    elif fromUnit == "min":
        conversionFactor *= 60
    elif fromUnit == "h":
        conversionFactor *= 3600
    elif fromUnit == "ms":
        conversionFactor /= 1000

    if toUnit == "s":
        conversionFactor /= 1
    elif toUnit == "min":
        conversionFactor /= 60
    elif toUnit == "h":
        conversionFactor /= 3600
    elif toUnit == "ms":
        conversionFactor *= 1000

    return value * conversionFactor

def find_nth(haystack, needle, n):
    start = haystack.find(needle)
    while start >= 0 and n > 1:
        start = haystack.find(needle, start+len(needle))
        n -= 1
    return start

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

class QHLine(QFrame):
  def __init__(self):
    super().__init__()
    self.setMinimumWidth(1)
    self.setFixedHeight(20)
    self.setFrameShape(QFrame.HLine)
    self.setFrameShadow(QFrame.Sunken)
    self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Minimum)
    return


# -*- coding: utf-8 -*-
"""
Created on Sun Mar 29 18:27:58 2020

@author: Kollarlab
"""

# import comtypes
import os
import time
#import subprocess

#import re
import scipy
import pylab
#import tarfile
#import struct
#import glob
import numpy
import time

#import pickle
#import datetime
#import itertools
import sys

from Acqiris import Acqiris

def get_color(ind):
    colorlist = ['firebrick',  'mediumblue', 'deepskyblue', 'darkgoldenrod', 'forestgreen', 'indigo', 'dodgerblue']

    nind = numpy.mod(ind, len(colorlist))
    return colorlist[nind]

hardwareAddress = "PXI23::0::0::INSTR"

IVIbinPath = "C:\\Program Files\\IVI Foundation\\IVI\\Bin\\"
sys.path.append(IVIbinPath)

card.activeChannels = [1,2]
card.timeout = 120

avs = 1
segs = 1

card.samples = 1024*125
card.segments = segs
card.averages = 1
#
#card.SetParams() #here is the danger of not using properties to set everything.
##without this here, card.samples isn't

dataMat1 = numpy.zeros((len(delays), card.samples))
dataMat1_av = numpy.zeros((len(delays), card.samples))
dataMat2 = numpy.zeros((len(delays), card.samples))
dataMat2_av = numpy.zeros((len(delays), card.samples))
tMat = numpy.zeros((len(delays), card.samples))

#pretake data to set everything up for test
card.averages = 1
card.triggerDelay = 0
card.SetParams() #pushes default to to card if the fields haven't been edited
card.ArmAndWait() #initiates aquisition and calibrates if need be
if len(card.activeChannels) == 1:
    data1 = card.ReadAllData() #read data for the active channels.
else:
    data1, data2 = card.ReadAllData() #read data for the active channels.

t0 = time.time()

for ind in range(0, avs):
    card.averages = 1
    card.triggerDelay = 0
    card.SetParams() #pushes default to to card if the fields haven't been edited
    card.ArmAndWait() #initiates aquisition and calibrates if need be
    if len(card.activeChannels) == 1:
        data1 = card.ReadAllData() #read data for the active channels.
    else:
        data1, data2 = card.ReadAllData() #read data for the active channels.

    ts =  ( delay + scipy.arange(0, len(data1),1.)*1/card.sampleRate)

t1 = time.time()

card.averages = 50
card.triggerDelay = 0
card.SetParams() #pushes default to to card if the fields haven't been edited
card.ArmAndWait() #initiates aquisition and calibrates if need be

if len(card.activeChannels) == 1:
    avData1 = card.ReadAllData() #read data for the active channels.
else:
    avData1, avData2 = card.ReadAllData() #read data for the active channels.
t2 = time.time()

d1 = numpy.round(t1-t0, 3)
d2 = numpy.round(t2-t1, 3)

print('segments = ' + str(segs))
print('averages = ' + str(avs))
print('time for ' + str(avs) + ' single (possibly multiseg) runs = ' + str(d1) )
print('time for ' + str(avs) + ' averages on card = ' + str(d2) )


from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib.sitemaps.views import sitemap
from django.contrib.sitemaps import Sitemap
from django.contrib import admin
from django.conf import settings
admin.autodiscover()

urlpatterns = i18n_patterns('',
    url(r'^$', 'core.views.home', name='home'),
    url(r'^new/$', 'core.views.new', name='new'),
    url(r'^statistics/$', 'core.views.statistics', name='statistics'),
    url(r'^(?P<key>\w+-\w+-\w+-\w+-\w+)$', 'core.views.tip_redir'),
    url(r'^(?P<key>\w+-\w+-\w+-\w+-\w+)/$', 'core.views.tip'),
    url(r'^(?P<key>\w+-\w+-\w+)$', 'core.views.tip_redir', name='tip_redir'),
    url(r'^(?P<key>\w+-\w+-\w+)/$', 'core.views.tip', name='tip'),
    url(r'^gratuity-example/$', 'core.views.tips_example', name='tips_example'),
    url(r'^w/(?P<key>\w+)/$', 'core.views.wallet', name='wallet'),
    url(r'^w/(?P<key>\w+)/comments/$', 'core.views.comments', name='comments'),
    url(r'^w/(?P<key>\w+)/pdf/$', 'core.views.download', {'format': "pdf"}, name='download'),
    url(r'^w/(?P<key>\w+)/pdf-us/$', 'core.views.download', {'format': "pdf", "page_size":"US"}, name='download'),
    url(r'^w/(?P<key>\w+)/odt/$', 'core.views.download', {'format': "odt"}, name='download'),
    url(r'^w/(?P<key>\w+)/png/$', 'core.views.download', {'format': "png"}, name='download'),
    url(r'^w/(?P<key>\w+)/wajax/$', 'core.views.wajax', name='wajax'),
)

urlpatterns += patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'qrcode/(?P<key>\w+)/$','core.views.qrcode_view', name='qrcode'),
)

if settings.BCTIP_MOD:
    import bctip.urls_custom
    urlpatterns += bctip.urls_custom.urlpatterns


import os
import sys
import json
import torch
import traceback

# def returnFalse():
#     return False
# torch.cuda.is_available = returnFalse

from scipy.io import wavfile

# from python.speaker_diarization.pipeline.speaker_diarization import SpeakerDiarization

class Diarization(object):
    def __init__(self, logger, PROD, device, models_manager):
        super(Diarization, self).__init__()

        self.logger = logger
        self.PROD = PROD
        self.models_manager = models_manager
        self.device = device
        self.ckpt_path = None

        # self.model = torch.hub.load('pyannote/pyannote-audio', 'dia')
        self.model = load_model(f'{"./resources/app" if self.PROD else "."}/python/speaker_diarization/hub/')

        # self.logger.info(str(self.model))
        # self.model = BaseModel.from_pretrained(f'{"./resources/app" if self.PROD else "."}/python/audio_source_separation/assModel.pt')

        self.isReady = True

    def set_device (self, device):
        self.device = device
        self.model = self.model.to(device)
        self.model.device = device

    def runTask (self, data, websocket=None):
        return self.diarize(data, websocket)

    async def diarize (self, data, websocket):

        inPath = data["inPath"]
        mergeSameOutput = data["toolSettings"]["mergeSingleOutputFolder"]
        outputAudacityLabels = data["toolSettings"]["outputAudacityLabels"]

        if websocket is not None:
            await websocket.send(json.dumps({"key": "task_info", "data": "Reading file"}))

        audacity_file = []

        # self.logger.info(f'diarization | {data["inPath"]} | {data["outputAudacityLabels"]} | {data} | {outputAudacityLabels}')

        out_folder = f'{"./resources/app" if self.PROD else "."}/python/speaker_diarization/output/'

        try:
            rate, data = wavfile.read(inPath)

            if websocket is not None:
                await websocket.send(json.dumps({"key": "task_info", "data": "Splitting file"}))

            diarization = self.model({'audio': inPath})

            out_file_counter = 0
            total_tracks = len(diarization._tracks)

            for turn, _, speaker in diarization.itertracks(yield_label=True):

                if websocket is not None:
                    await websocket.send(json.dumps({"key": "task_info", "data": f'Outputting chunks: {out_file_counter+1}/{total_tracks}'}))

                start_s = turn.start
                end_s = turn.end

                # Skip audio chunks less than 1 second long
                if end_s-start_s < 1:
                    continue

                if outputAudacityLabels:
                    audacity_file.append('{:.6f}\t{:.6f}\tSpeaker_{}'.format(start_s, end_s, speaker))

                split_data = data[int(start_s*rate):int(end_s*rate)]

                folder_name = ".".join(inPath.split("/")[-1].split(".")[:-1]).replace(".", "_")
                if not mergeSameOutput:
                    out_folder = f'{"./resources/app" if self.PROD else "."}/python/speaker_diarization/output/{folder_name}/speaker {speaker}'

                os.makedirs(out_folder, exist_ok=True)
                if mergeSameOutput:
                    wavfile.write(f'{out_folder}/{folder_name}_{str(out_file_counter).zfill(7)}.wav', rate, split_data)
                else:
                    wavfile.write(f'{out_folder}/{folder_name}_{speaker}_{str(out_file_counter).zfill(7)}.wav', rate, split_data)
                out_file_counter += 1
        except:
            self.logger.info(traceback.format_exc())
            raise

        if outputAudacityLabels:
            with open(f'{out_folder}/audacity.txt', "w+", encoding="utf8") as f:
                f.write("\n".join(audacity_file))

        if websocket is not None:
            await websocket.send(json.dumps({"key": "tasks_next"}))

#
#
#
#  This is a huge mess, but pyannote very much wants models to be downloaded from the internet
#  For future-proofing reasons, I don't want that, so I had to change a lot of the library code,
#  and the way the models were loaded, such that torchhub isn't used, and instead the local model files are used.
#
#
#

def load_model (_HUB_DIR):
    import typing
    import shutil
    import functools
    import yaml
    import zipfile

    from pyannote.audio.features import Pretrained as _Pretrained
    from pyannote.pipeline import Pipeline as _Pipeline

    dependencies = ['pyannote.audio', 'torch']

    _HUB_REPO = 'https://github.com/pyannote/pyannote-audio-hub'
    _ZIP_URL = f'{_HUB_REPO}/raw/master/{{kind}}s/{{name}}.zip'
    _PRETRAINED_URL = f'{_HUB_REPO}/raw/master/pretrained.yml'

    # path where pre-trained models and pipelines are downloaded and cached
    # _HUB_DIR = f'{"./resources/app" if self.PROD else "."}/python/speaker_diarization/hub'
    # _HUB_DIR = pathlib.Path(os.environ.get("PYANNOTE_AUDIO_HUB",
    #                                        "~/.pyannote/hub")).expanduser().resolve()

    # download pretrained.yml if needed
    _PRETRAINED_YML = _HUB_DIR + 'pretrained.yml'
    print(f'_PRETRAINED_YML, {_PRETRAINED_YML}')

    # if not _PRETRAINED_YML.exists():
    #     msg = (
    #         f'Downloading list of pretrained models and pipelines '
    #         f'to "{_PRETRAINED_YML}".'
    #     )
    #     print(msg)
    #     from pyannote.audio.utils.path import mkdir_p
    #     mkdir_p(_PRETRAINED_YML.parent)
    #     torch.hub.download_url_to_file(_PRETRAINED_URL,
    #                                    _PRETRAINED_YML,
    #                                    progress=True)

    def _generic(name: str,
                 duration: float = None,
                 step: float = 0.25,
                 batch_size: int = 32,
                 device: typing.Optional[typing.Union[typing.Text, torch.device]] = None,
                 pipeline: typing.Optional[bool] = None,
                 force_reload: bool = False) -> typing.Union[_Pretrained, _Pipeline]:
        """Load pretrained model or pipeline

        Parameters
        ----------
        name : str
            Name of pretrained model or pipeline
        duration : float, optional
            Override audio chunks duration.
            Defaults to the one used during training.
        step : float, optional
            Ratio of audio chunk duration used for the internal sliding window.
            Defaults to 0.25 (i.e. 75% overlap between two consecutive windows).
            Reducing this value might lead to better results (at the expense of
            slower processing).
        batch_size : int, optional
            Batch size used for inference. Defaults to 32.
        device : torch.device, optional
            Device used for inference.
        pipeline : bool, optional
            Wrap pretrained model in a (not fully optimized) pipeline.
        force_reload : bool
            Whether to discard the existing cache and force a fresh download.
            Defaults to use existing cache.

        Returns
        -------
        pretrained: `Pretrained` or `Pipeline`

        Usage
        -----
        >>> sad_pipeline = torch.hub.load('pyannote/pyannote-audio', 'sad_ami')
        >>> scores = model({'audio': '/path/to/audio.wav'})
        """
        # print("name", name)

        model_exists = name in _MODELS
        pipeline_exists = name in _PIPELINES

        # print(f'PRE model_exists, {model_exists}')
        # print(f'PRE pipeline_exists, {pipeline_exists}')

        if model_exists and pipeline_exists:
            # print(f'model_exists and pipeline_exists')
            # pass

            # if pipeline is None:
            #     msg = (
            #         f'Both a pretrained model and a pretrained pipeline called '
            #         f'"{name}" are available. Use option "pipeline=True" to '
            #         f'load the pipeline, and "pipeline=False" to load the model.')
            #     raise ValueError(msg)

            if pipeline:
                kind = 'pipeline'
                # zip_url = _ZIP_URL.format(kind=kind, name=name)
                # sha256 = _PIPELINES[name]
                return_pipeline = True

            else:
                kind = 'model'
                # zip_url = _ZIP_URL.format(kind=kind, name=name)
                # sha256 = _MODELS[name]
                return_pipeline = False

        elif pipeline_exists:
        # elif False:
            # print(f'pipeline_exists')
            # pass

            # pass
            if pipeline is None:
                pipeline = True

            if not pipeline:
                msg = (
                    f'Could not find any pretrained "{name}" model. '
                    f'A pretrained "{name}" pipeline does exist. '
                    f'Did you mean "pipeline=True"?'
                )
                raise ValueError(msg)

            kind = 'pipeline'
            # zip_url = _ZIP_URL.format(kind=kind, name=name)
            # sha256 = _PIPELINES[name]
            return_pipeline = True

        elif model_exists:

            # print(f'model_exists')
            # pass
            if pipeline is None:
                pipeline = False

            kind = 'model'
            # zip_url = _ZIP_URL.format(kind=kind, name=name)
            # sha256 = _MODELS[name]
            return_pipeline = pipeline

            if name.startswith('emb_') and return_pipeline:
                msg = (
                    f'Pretrained model "{name}" has no associated pipeline. Use '
                    f'"pipeline=False" or remove "pipeline" option altogether.'
                )
                raise ValueError(msg)

        else:
            # print("ERROR====================")
            pass
            # msg = (
            #     f'Could not find any pretrained model nor pipeline called "{name}".'
            # )
            # raise ValueError(msg)

        # if sha256 is None:
        #     msg = (
        #         f'Pretrained {kind} "{name}" is not available yet but will be '
        #         f'released shortly. Stay tuned...'
        #     )
        #     raise NotImplementedError(msg)

        pretrained_dir = _HUB_DIR + f'/{kind}s'
        pretrained_subdir = pretrained_dir + f'/{name}'
        pretrained_zip = pretrained_dir + f'/{name}.zip'

        # import pathlib
        # pretrained_subdir = pathlib.Path(pretrained_subdir)

        # if not pretrained_subdir.exists() or force_reload:

        #     if pretrained_subdir.exists():
        #         shutil.rmtree(pretrained_subdir)

        #     from pyannote.audio.utils.path import mkdir_p
        #     mkdir_p(pretrained_zip.parent)
        #     try:
        #         msg = (
        #             f'Downloading pretrained {kind} "{name}" to "{pretrained_zip}".'
        #         )
        #         print(msg)
        #         torch.hub.download_url_to_file(zip_url,
        #                                        pretrained_zip,
        #                                        hash_prefix=sha256,
        #                                        progress=True)
        #     except RuntimeError as e:
        #         shutil.rmtree(pretrained_subdir)
        #         msg = (
        #             f'Failed to download pretrained {kind} "{name}".'
        #             f'Please try again.')
        #         raise RuntimeError(msg)

        #     # unzip downloaded file
        #     with zipfile.ZipFile(pretrained_zip) as z:
        #         z.extractall(path=pretrained_dir)

        if kind == 'model':

            params_yml = None
            params_yml_parent = None
            params_yml_c1 = os.listdir(pretrained_subdir)
            for c1 in params_yml_c1:
                params_yml_c2 = [fold for fold in os.listdir(f'{pretrained_subdir}/{c1}') if os.path.isdir(f'{pretrained_subdir}/{c1}/{fold}')]
                for c2 in params_yml_c2:
                    params_yml_c3 = os.listdir(f'{pretrained_subdir}/{c1}/{c2}')
                    for c3 in params_yml_c3:
                        params_yml_c4 = os.listdir(f'{pretrained_subdir}/{c1}/{c2}/{c3}')
                        for c4 in params_yml_c4:
                            if c4=="params.yml":
                                params_yml_parent = f'{pretrained_subdir}/{c1}/{c2}/{c3}'
                                params_yml = f'{pretrained_subdir}/{c1}/{c2}/{c3}/params.yml'
                                break
            # print(f'----------params_yml, {params_yml}')
            # print(f'----------params_yml_parent, {params_yml_parent}')

            # params_yml, = pretrained_subdir.glob('*/*/*/*/params.yml')
            # pretrained =  _Pretrained(validate_dir=params_yml.parent,
            pretrained =  _Pretrained(validate_dir=params_yml_parent,
                                      duration=duration,
                                      step=step,
                                      batch_size=batch_size,
                                      device=device)

            # if return_pipeline:
            #     if name.startswith('sad_'):
            #         from pyannote.audio.pipeline.speech_activity_detection import SpeechActivityDetection
            #         print("HERE PRE SpeechActivityDetection")
            #         pipeline = SpeechActivityDetection(scores=pretrained)
            #         print("HERE POST")
            #     elif name.startswith('scd_'):
            #         from pyannote.audio.pipeline.speaker_change_detection import SpeakerChangeDetection
            #         print("HERE PRE SpeakerChangeDetection")
            #         pipeline = SpeakerChangeDetection(scores=pretrained)
            #         print("HERE POST")
            #     elif name.startswith('ovl_'):
            #         from pyannote.audio.pipeline.overlap_detection import OverlapDetection
            #         print("HERE PRE OverlapDetection")
            #         pipeline = OverlapDetection(scores=pretrained)
            #         print("HERE POST")
            #     else:
            #         # this should never happen
            #         msg = (
            #             f'Pretrained model "{name}" has no associated pipeline. Use '
            #             f'"pipeline=False" or remove "pipeline" option altogether.'
            #         )
            #         raise ValueError(msg)

            #     return pipeline.load_params(params_yml)

            return pretrained

        elif kind == 'pipeline':

            from pyannote.audio.pipeline.utils import load_pretrained_pipeline
            params_yml = None
            params_yml_parent = None
            # print(f'START     pretrained_subdir, {pretrained_subdir}')
            # params_yml_c1 = os.listdir(pretrained_subdir)
            params_yml_c1 = [fold for fold in os.listdir(f'{pretrained_subdir}') if os.path.isdir(f'{pretrained_subdir}/{fold}')]
            for c1 in params_yml_c1:
                # params_yml_c2 = os.listdir(f'{pretrained_subdir}/{c1}'.replace("//","/"))
                params_yml_c2 = [fold for fold in os.listdir(f'{pretrained_subdir}/{c1}') if os.path.isdir(f'{pretrained_subdir}/{c1}/{fold}')]
                for c2 in params_yml_c2:
                    params_yml_c3 = os.listdir(f'{pretrained_subdir}/{c1}/{c2}')
                    for c3 in params_yml_c3:
                        if c3=="params.yml":
                            params_yml_parent = f'{pretrained_subdir}/{c1}/{c2}'
                            params_yml = f'{pretrained_subdir}/{c1}/{c2}/params.yml'
                            break

            # params_yml, *_ = pretrained_subdir.glob('*/*/params.yml')
            # return load_pretrained_pipeline(params_yml.parent)
            # print("=== ptp PRE")
            ptp = load_pretrained_pipeline(params_yml_parent)
            # print("=== ptp POST")
            return ptp

    with open(_PRETRAINED_YML, 'r') as fp:
        _pretrained = yaml.load(fp, Loader=yaml.SafeLoader)
    # print(f'_pretrained, {_pretrained}')

    ___stuff = {}

    _MODELS = _pretrained['models']
    # print(f'_MODELS, {_MODELS}')
    for name in _MODELS:
        # print(f'_MODELS name, {name}')
        # locals()[name] = functools.partial(_generic, name)
        ___stuff[name] = functools.partial(_generic, name)

    _PIPELINES = _pretrained['pipelines']
    # print(f'_PIPELINES, {_PIPELINES}')
    for name in _PIPELINES:
        # print(f'_PIPELINES name, {name}')
        # locals()[name] = functools.partial(_generic, name)
        ___stuff[name] = functools.partial(_generic, name)

    _SHORTCUTS = _pretrained['shortcuts']
    # print(f'_SHORTCUTS, {_SHORTCUTS}')
    for shortcut, name in _SHORTCUTS.items():
        # print(f'_SHORTCUTS name, {name}')
        # locals()[shortcut] = locals()[name]
        ___stuff[shortcut] = ___stuff[name]

    return ___stuff["dia"]()


'''
Created on Oct 15, 2011

@author: waxwing
'''
import os, sys, commands
from com.android.monkeyrunner import MonkeyRunner
if __name__ == '__main__':
    blacklist = ['main.py', 'monkeytools.py']

    monkeys =  os.listdir(os.path.abspath(os.path.dirname(sys.argv[0])))
    monkeys = filter (lambda m: m[-3:] == '.py', monkeys)
    monkeys = filter (lambda m: m not in blacklist, monkeys)
    m = MonkeyRunner.choice('Select a script', monkeys)
    if m != -1:
        commands.getoutput('monkeyrunner ' + monkeys[m])


def shoppingTime(memberId, money):
  item = [
    {'name': 'Sepatu Stacattu', 'price': 1500000},
    {'name': 'Baju Zoro', 'price': 500000},
    {'name': 'Baju H&H', 'price': 250000},
    {'name': 'Sweater Uniklooh', 'price': 175000},
    {'name': 'Casing Handphone', 'price': 50000},
  ]

  if memberId == '':
    return 'anda bukan member!'
  elif money < 50000:
    return 'uang anda tidak cukup'

  shopping_result = {
    'memberId': memberId,
    'money': money,
    'listPurchased': [],
    'changeMoney': money
  }

  for listItem in item:
    if shopping_result['changeMoney'] >= listItem['price']:
      shopping_result['listPurchased'].append(listItem['name'])
      shopping_result['changeMoney'] -= listItem['price']

  return shopping_result

# Driver Code
print(shoppingTime('1820RzKrnWn08', 2475000))
'''
{ memberId: '1820RzKrnWn08',
  money: 2475000,
  listPurchased:
   [ 'Sepatu Stacattu',
     'Baju Zoro',
     'Baju H&N',
     'Sweater Uniklooh',
     'Casing Handphone' ],
   changeMoney: 0 }
'''
print(shoppingTime('82Ku8Ma742', 170000))
'''
{ memberId: '82Ku8Ma742',
 money: 170000,
 listPurchased:
  [ 'Casing Handphone' ],
 changeMoney: 120000 }
'''
print(shoppingTime('', 2475000))
# Mohon maaf, toko X hanya berlaku untuk member saja
print(shoppingTime('234JdhweRxa53', 15000))
# Mohon maaf, uang tidak cukup
print(shoppingTime())
# Mohon maaf, toko X hanya berlaku untuk member saja


from typing import Dict
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from app.routers import visualize

app = FastAPI()

app = FastAPI(
    title="Model Visualizer",
    description="",
    version="0.10.0",
)

app.mount("/home/lirakr/repos/rnd-mermaid/app/static", StaticFiles(directory="/home/lirakr/repos/rnd-mermaid/app/static"), name="static")

@app.get(
    "/",
    summary="Status",
    responses={200: {"content": {"application/json": {"example": {"status": "OK"}}}}},
)
async def index() -> Dict[str, str]:
    """
    Show application status and docker image details
    """
    return {"status": "OK"}

app.include_router(visualize.router)


from copy import deepcopy
import numpy as np

from action_execution.config_keys import ExecutionConfigKeys
from action_execution.execution_models.model import ExecutionModelBase
from action_execution.geometry.vector import Vector2, Vector3
from action_execution.geometry.pose import Pose3

class FreeSpace(ExecutionModelBase):
    def __init__(self, action_id='', data_logger=None, **kwargs):
        super(FreeSpace, self).__init__(model_id='free_space')

        self.frame_id = ''
        self.manipulated_object = None
        self.objects = list()
        self.surface = None

        if ExecutionConfigKeys.FRAME_ID in kwargs:
            self.frame_id = kwargs[ExecutionConfigKeys.FRAME_ID]
        if ExecutionConfigKeys.MANIPULATED_OBJECT in kwargs:
            self.manipulated_object = kwargs[ExecutionConfigKeys.MANIPULATED_OBJECT]
        if ExecutionConfigKeys.OBJECTS_ON_SURFACE in kwargs:
            self.objects = kwargs[ExecutionConfigKeys.OBJECTS_ON_SURFACE]
        if ExecutionConfigKeys.SURFACE in kwargs:
            self.surface = kwargs[ExecutionConfigKeys.SURFACE]

    def generate_data(self, number_of_samples):
        '''Generates a set of samples

        Keyword arguments:
        number_of_samples -- number of samples to generate

        Returns:
        candidate_poses -- a list of 'action_execution.geometry.pose.Pose3' objects

        '''
        candidate_poses = list()

        # we get the z-projections of the objects that are already on the surface
        object_polygons = list()
        for obj in self.objects:
            polygon = obj.get_z_projection()
            object_polygons.append(polygon)

        # we generate samples in the free space on the surface, i.e.
        # we ignore samples that cause collisions with the other objects
        collected_samples = 0
        while collected_samples < number_of_samples:
            obj_copy = deepcopy(self.manipulated_object)

            # we generate a random position on the surface
            position = Vector2.random_vector(self.surface.bbox.min, self.surface.bbox.max)
            obj_copy.planar_translate_to(position)

            # we generate a random orientation around z;
            # we don't rotate the object around the other axes
            z_orientation = np.random.uniform(0., 2.*np.pi)
            obj_copy.rotate_around_z(z_orientation)

            # we check if the object would collide with any of the other
            # objects in the newly generated pose
            manipulated_object_polygon = obj_copy.get_z_projection()
            collision = False
            for obj in object_polygons:
                if obj.intersects(manipulated_object_polygon):
                    collision = True
                    break

            # we take the generated pose as a valid candidate if the
            # object doesn't collide with any of the objects on the surface
            if not collision:
                position = Vector3(position.x,
                                   position.y,
                                   self.surface.pose.position.z)
                orientation = Vector3(obj_copy.pose.orientation.x,
                                      obj_copy.pose.orientation.y,
                                      z_orientation)
                pose = Pose3(self.manipulated_object.pose.frame_id, position, orientation)
                candidate_poses.append(pose)
                collected_samples += 1

        success_probabilities = np.ones(number_of_samples) / (number_of_samples * 1.)
        return {'candidate_poses': candidate_poses,
                'success_probabilities': success_probabilities}

    def process_data(self, data):
        '''Recalculates the success probabilities of the given data
        by only counting poses that are in free space. Returns
        a new data dictionary with the updated probabilities.

        Keyword arguments:
        data -- a dictionary with two keys - "candidate_poses" and "success_probabilities",
                each of which is a list

        '''
        candidate_poses = data['candidate_poses']
        success_probabilities = np.array(data['success_probabilities'])
        free_pose = np.ones(len(success_probabilities), dtype=bool)

        # we get the z-projections of the objects that are already on the surface
        object_polygons = list()
        for obj in self.objects:
            polygon = obj.get_z_projection()
            object_polygons.append(polygon)

        free_pose_count = 0.
        for i, pose in enumerate(candidate_poses):
            obj_copy = deepcopy(self.manipulated_object)
            obj_copy.planar_translate_to(Vector2(pose.position.x, pose.position.y))
            obj_copy.rotate_around_z(pose.orientation.z)

            # we check if the object would collide with any of the other
            # objects in the newly generated pose
            manipulated_object_polygon = obj_copy.get_z_projection()
            collision = False
            for obj in object_polygons:
                if obj.intersects(manipulated_object_polygon):
                    collision = True
                    break

            if not collision:
                free_pose_count += 1.
            else:
                free_pose[i] = False

        p_factor = 0.
        if free_pose_count > 0:
            p_factor = 1. / free_pose_count

        for i, p in enumerate(success_probabilities):
            if free_pose[i]:
                success_probabilities[i] = p * p_factor

        return {'candidate_poses': candidate_poses,
                'success_probabilities': success_probabilities}

    def input_to_dict(self):
        model_dict = {self.id: dict()}
        model_dict[self.id]['frame_id'] = self.frame_id
        model_dict[self.id]['manipulated_object'] = self.manipulated_object.to_dict()
        model_dict[self.id]['static_objects'] = list()
        for obj in self.objects:
            model_dict[self.id]['static_objects'].append(obj.to_dict())
        model_dict[self.id]['surface'] = self.surface.to_dict()
        return model_dict

    def result_to_dict(self, results):
        result_dict = {self.id: dict()}
        result_dict[self.id]['candidate_poses'] = list()
        result_dict[self.id]['success_probabilities'] = list()
        for pose in results['candidate_poses']:
            result_dict[self.id]['candidate_poses'].append(pose.to_dict())
        for prob in results['success_probabilities']:
            result_dict[self.id]['success_probabilities'].append(prob)
        return result_dict


def readOnly_file():
    f = None
    try:
        f = open('my_write_file','w')
        f.write('vb this is the new content .....')

    except ValueError:
        print('unsupported mode error')
    else:
       print('data updated .......')
    finally:
        if f != None:

            f.close()

def writeOnly_file():

    try:
        f = open('myfile.txt','r')
        f.write(' ....i am kavipriya ')

        # f = open('myfile.txt','r')
        # print(f.read())
    except FileNotFoundError:
        print('file not found')
    except:
        print('something is wrong')
    finally:
        f.close()

readOnly_file()
# writeOnly_file()


from django.shortcuts import render
from utils import Word

def home(request):
    context = {}
    if request.method == "POST":
        text = request.POST['text']
        context['results'] = Word(text).result()
        context['text'] = text

    return render(request, 'home.html', context)