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

import time
import random

import redis
from functools import lru_cache
from django.conf import settings
from django.urls import reverse
from .models import User
import requests
import os
from PIL import Image
from io import BytesIO
from requests import HTTPError, Timeout, ConnectionError
import logging
from collections import Counter
from urllib.parse import urlparse
import json
import re
from web.stopwords import stopwords
import jieba
from whoosh.fields import Schema, TEXT, ID
from feed.utils import mkdir, get_hash_name
from bs4 import BeautifulSoup

# init Redis connection
R = redis.Redis(host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=settings.REDIS_WEB_DB, decode_responses=True)
logger = logging.getLogger(__name__)

if not settings.DEBUG:
    # init jieba
    jieba.initialize()

UAS = [
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/74.0.835.163 Safari/535.1',
    'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/78.0',
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.50 (KHTML, like Gecko) Version/13.0 Safari/534.50',
    'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Win64; x64; Trident/5.0)',
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0 Safari/537.36',
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 Edg/83.0.478.54',
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Safari/605.1.15',
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0',
    'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1',
    'Mozilla/5.0 (CrKey armv7l 1.5.16041) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.0 Safari/537.36',
]

def get_host_name(url):
    return urlparse(url).netloc

def get_short_host_name(url):
    host = urlparse(url).netloc
    return re.sub(r'^www\.|^blog.', '', host)

def get_random_ua():
    return random.choice(UAS)

def incr_view_uv(uindex):
    """
    文章浏览数统计，保留一年数据
    :param uindex:
    :return:
    """
    key = settings.REDIS_VIEW_KEY % uindex
    ret = R.incr(key)

    if ret == 1:
        R.expire(key, 365 * 86400)

    return ret

def add_register_count():
    """
    注册用户数统计
    :return:
    """
    key = settings.REDIS_REG_KEY % current_day()

    return incr_redis_key(key)

def add_api_profile(api, elapsed):
    """
    按天进行平均耗时计算
    :param api:
    :param elapsed:
    :return:
    """
    day = current_day()
    count = total = 0
    try:
        R.sadd(settings.REDIS_API_KEY, api)

        count = R.incr(settings.REDIS_API_COUNT_KEY % (api, day))
        total = R.incr(settings.REDIS_API_TOTAL_KEY % (api, day), amount=elapsed)
        R.set(settings.REDIS_API_AVG_KEY % (api, day), int(total/count))
    except:
        logger.error(f"api 平均耗时计算异常！`{count}`{total}")

@lru_cache(maxsize=4)
def get_profile_apis():
    return (
        reverse('get_article_update_view'), reverse('get_lastweek_articles'), reverse('get_recent_articles'),
        reverse('get_site_update_view'), reverse('get_site_article_update_view'), reverse('in_site_search')
    )

def set_active_rss(feeds):
    """
    访问过的源，设置一个标识，3天缓存
    :param feeds:
    :return:
    """
    with R.pipeline(transaction=False) as p:
        for feed in feeds:
            p.set(settings.REDIS_ACTIVE_RSS_KEY % feed, 1, 2*24*3600)
        p.execute()

def is_active_rss(feed):
    return R.get(settings.REDIS_ACTIVE_RSS_KEY % feed) == '1'

def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

def current_day():
    return time.strftime("%Y%m%d", time.localtime(time.time()))

def get_xaxis_days(days=60):
    """
    获取 X 坐标，按天。取两个月数据
    """
    xaxis_days = []

    for i in range(0, days):
        xaxis_days.append(time.strftime("%Y%m%d", time.localtime(time.time() - i * 86400)))

    xaxis_days.sort()
    return xaxis_days

def is_visit_today(uid):
    """
    当天是否访问过
    :param uid:
    :return:
    """
    key = settings.REDIS_VISIT_KEY % (current_day(), uid)

    return R.get(key) == '1'

def set_visit_today(uid):
    key = settings.REDIS_VISIT_KEY % (current_day(), uid)

    return R.set(key, 1, 24*3600+100)

def is_old_user(uid):
    """
    是否是老用户（过去一周）
    :param uid:
    :return:
    """
    key = settings.REDIS_WEEK_KEY % uid

    return R.get(key) == '1'

def set_old_user(uid):
    key = settings.REDIS_WEEK_KEY % uid

    return R.set(key, 1, 7*24*3600)

def incr_redis_key(key):
    return R.incr(key, amount=1)

def add_referer_day_host(host):
    key = settings.REDIS_REFER_DAY_KEY % current_day()

    return R.sadd(key, host)

def add_referer_stats(referer):
    if referer:
        host = get_host_name(referer)

        if host and host not in settings.ALLOWED_HOSTS:
            logger.info(f"收到外域来源：`{host}`{referer}")

            add_referer_day_host(host)
            incr_redis_key(settings.REDIS_REFER_PV_DAY_KEY % (host, current_day()))

def reset_recent_articles(articles):
    key = settings.REDIS_ARTICLES_KEY

    R.delete(key)

    R.sadd(key, *articles)

    R.expire(key, 2*86400)

    return True

def get_recent_articles():
    key = settings.REDIS_ARTICLES_KEY
    return R.smembers(key)

def reset_recent_site_articles(site_id, articles):
    key = settings.REDIS_SITE_ARTICLES_KEY % site_id

    R.delete(key)

    R.sadd(key, *articles)

    R.expire(key, 2*86400)

    return True

def get_recent_site_articles(site_id):
    key = settings.REDIS_SITE_ARTICLES_KEY % site_id
    return R.smembers(key)

def set_site_lastid(site_id, uindex):
    key = settings.REDIS_SITE_LASTID_KEY % site_id
    return R.set(key, uindex, 2*86400)

def get_site_last_id(site_id):
    key = settings.REDIS_SITE_LASTID_KEY % site_id
    return R.get(key) or '0'

def reset_sites_lastids(ids):
    key = settings.REDIS_SITES_LASTIDS_KEY
    R.delete(key)
    return R.sadd(key, *ids)

def get_sites_lastids():
    key = settings.REDIS_SITES_LASTIDS_KEY
    return sorted(R.smembers(key), reverse=True)

def add_user_sub_feeds(oauth_id, feeds):
    key = settings.REDIS_USER_SUB_KEY % oauth_id

    return R.sadd(key, *feeds)

def del_user_sub_feed(oauth_id, feed):
    key = settings.REDIS_USER_SUB_KEY % oauth_id

    return R.srem(key, feed)

def set_active_sites(sites):
    """
    批量设置有效的源（未下线的）
    """
    key = settings.REDIS_ACTIVE_SITES_KEY
    R.delete(key)
    return R.sadd(key, *sites)

def set_active_site(site):
    """
    设置单个源有效（未下线的）
    """
    key = settings.REDIS_ACTIVE_SITES_KEY
    return R.sadd(key, site)

def get_active_sites():
    key = settings.REDIS_ACTIVE_SITES_KEY
    return R.smembers(key)

def get_user_subscribe_feeds(oauth_id, from_user=True, user_level=1):
    """
    获取登陆用户订阅源
    """
    key = settings.REDIS_USER_SUB_KEY % oauth_id

    sub_feeds = R.smembers(key) & get_active_sites()

    # 普通用户，限制其订阅数量
    if user_level < 10:
        sub_feeds = list(sub_feeds)[:settings.USER_SUBS_LIMIT]

    # 设置订阅源缓存，来自用户的请求调用
    if from_user:
        set_active_rss(sub_feeds)

        if not sub_feeds:
            logger.warning(f'用户未订阅任何内容：`{oauth_id}')

    return [int(i) for i in sub_feeds]

def set_user_read_article(oauth_id, uindex):
    """
    已登录用户同步已读文章状态
    """
    key = settings.REDIS_USER_READ_KEY % (oauth_id, uindex)

    return R.set(key, 1, settings.RECENT_DAYS * 86400 + 86400)

def set_user_read_articles(oauth_id, ids):
    """
    已登录用户同步已读文章状态
    :param oauth_id:
    :param ids:
    :return:
    """
    with R.pipeline(transaction=False) as p:
        for uindex in ids:
            key = settings.REDIS_USER_READ_KEY % (oauth_id, uindex)
            p.set(key, 1, settings.RECENT_DAYS * 86400 + 86400)
        p.execute()

def get_user_unread_count(oauth_id, articles):
    """
    计算用户对一批文章的未读数
    :return:
    """
    if articles:
        user_read_keys = [settings.REDIS_USER_READ_KEY % (oauth_id, uindex) for uindex in articles]
        return len(articles) - R.mget(*user_read_keys).count('1')
    else:
        return 0

def get_user_unread_articles(oauth_id, articles):
    """
    计算用户未读的文章
    """
    unread_articles = set()

    for uindex in articles:
        key = settings.REDIS_USER_READ_KEY % (oauth_id, uindex)

        if R.get(key) != '1':
            unread_articles.add(uindex)

    return unread_articles

def get_user_unread_sites(oauth_id, sites):
    """
    计算未读的站点
    """
    unread_sites = set()

    for site_id in sites:
        site_articles = get_recent_site_articles(site_id)
        if get_user_unread_count(oauth_id, site_articles) > 0:
            unread_sites.add(site_id)

    return unread_sites

def get_login_user(request):
    """
    获取已登录用户信息
    :param request:
    :return: 获取成功返回 User 对象；用户不存在则返回 None
    """
    oauth_id = request.get_signed_cookie('oauth_id', False)

    # if settings.DEBUG:
    #     oauth_id = 'github/28855629'

    if oauth_id:
        try:
            return User.objects.get(oauth_id=oauth_id, status='active')
        except:
            logger.warning(f'用户不存在：`{oauth_id}')
    return None

def get_user_visit_days(oauth_id):
    """
    计算一个用户登陆的天数（过去一个月）
    """
    user_visit_keys = [settings.REDIS_USER_VISIT_DAY_KEY % (oauth_id, day) for day in get_xaxis_days(31)]
    return R.mget(*user_visit_keys).count('1')

def set_user_ranking_list(ranking):
    return R.set(settings.REDIS_USER_RANKING_KEY, json.dumps(ranking))

def get_user_ranking_list():
    return json.loads(R.get(settings.REDIS_USER_RANKING_KEY))

def save_avatar(avatar, userid, size=100, referer=None):
    """
    保存网络头像
    :param avatar:
    :param userid:
    :param size:
    :param referer:
    :return: 保存后的头像地址
    """
    try:
        headers = {'Referer': referer} if referer else {}

        rsp = requests.get(avatar, timeout=15, headers=headers)

        if rsp.ok:
            img_obj = Image.open(BytesIO(rsp.content))
            img_obj.thumbnail((size, size))
            jpg = get_hash_name(userid) + '.jpg'

            if img_obj.mode != 'RGB':
                img_obj = img_obj.convert('RGB')

            img_obj.save(os.path.join(settings.BASE_DIR, 'assets', 'avatar', jpg))
            return f'/assets/avatar/{jpg}'
        else:
            logger.error(f"同步用户头像出现网络异常！`{userid}`{avatar}")
    except (HTTPError, Timeout, ConnectionError):
        logger.error(f"同步用户头像网络异常！`{userid}`{avatar}")
    except:
        logger.error(f"同步用户头像未知异常`{userid}`{avatar}")

    return '/assets/img/logo.svg'

def cal_cosine_distance(x, y):
    """
    计算两个列表的余弦相似度，0 ~ 1 TODO 目前无用
    :param x:
    :param y:
    :return:
    """
    x_counter = Counter(x)
    y_counter = Counter(y)

    if x_counter == y_counter:
        return 1

    words = list(x_counter.keys() | y_counter.keys())

    x_vect = [x_counter.get(word, 0) for word in words]
    y_vect = [y_counter.get(word, 0) for word in words]

    len_x = sum(av*av for av in x_vect) ** 0.5
    len_y = sum(bv*bv for bv in y_vect) ** 0.5

    dot = sum(av*bv for av, bv in zip(x_vect, y_vect))

    distance = dot / (len_x * len_y)

    return round(distance, 2)

def set_user_visit_day(oauth_id):
    key = settings.REDIS_USER_VISIT_DAY_KEY % (oauth_id, current_day())

    return R.set(key, '1', 30 * 24 * 3600)

def set_feed_ranking_dict(ranking_dict):
    key = settings.REDIS_FEED_RANKING_KEY

    return R.set(key, json.dumps(ranking_dict))

def get_feed_ranking_dict():
    key = settings.REDIS_FEED_RANKING_KEY
    try:
        return json.loads(R.get(key))
    except:
        logger.error(f"读取 Redis 出现异常：`{key}")

    return {}

def set_indexed(tag, key):
    key = settings.REDIS_INDEXED_KEY % (tag, key)
    return R.set(key, '1', 30*86400)

def is_indexed(tag, key):
    key = settings.REDIS_INDEXED_KEY % (tag, key)
    return R.get(key) == '1'

def del_dat2_file(uindex, site_id):
    file = os.path.join(settings.HTML_DATA2_DIR, str(site_id), f"{uindex}.html")
    try:
        os.remove(file)
        return True
    except:
        pass

    return False

def write_dat2_file(uindex, site_id, content):
    """
    写入到文件系统；写入成功或已经存在返回 True
    """
    new_dir = os.path.join(settings.HTML_DATA2_DIR, str(site_id))
    mkdir(new_dir)

    file = os.path.join(new_dir, f"{uindex}.html")

    if os.path.exists(file):
        return True

    if not content.strip():
        logger.warning(f"写入空文件：`{content}`{uindex}")

    try:
        with open(file, 'w', encoding='UTF8') as f:
            f.write(content)

        return True
    except:
        logger.warning(f"写入文件失败：`{uindex}")

    return False

@lru_cache(maxsize=2048)
def get_content(uindex, site_id):
    file = os.path.join(os.path.join(settings.HTML_DATA2_DIR, str(site_id)), f"{uindex}.html")

    if os.path.exists(file):
        return open(file, encoding='UTF8').read()
    else:
        logger.warning(f"文件丢失：`{file}")

    return ''

def get_random_emoji():
    pics = os.listdir(os.path.join(settings.BASE_DIR, 'assets', 'emoji'))
    pics = [pic for pic in pics if pic.endswith('.png')]

    return '/assets/emoji/' + random.choice(pics)

def generate_rss_avatar(link, feed=''):
    """
    生成用户提交 RSS 源的默认头像
    :param link:
    :param feed:
    :return:
    """
    avatar, link_host, feed_host = "", get_host_name(link), get_host_name(feed)

    # 首先根据 link 地址匹配
    if 'weibo.com' in link_host or 'weibo.cn' in link_host:
        avatar = "/assets/img/weibo.jpg"
    elif "jianshu.com" in link_host:
        avatar = "/assets/img/jianshu.png"
    elif link_host in ('blog.sina.com', 'blog.sina.com.cn'):
        avatar = '/assets/img/blogsina.jpg'
    elif 'sina.com.cn' in link_host:
        avatar = '/assets/img/sinanews.png'
    elif 'twitter.com' in link_host:
        avatar = '/assets/img/twitter.png'
    elif 'youtube.com' in link_host:
        avatar = '/assets/img/youtube.png'
    elif link_host == 'music.163.com':
        avatar = '/assets/img/163music.jpg'
    elif 'douban.com' in link_host:
        avatar = '/assets/img/douban.png'
    elif 'zhihu.com' in link_host:
        avatar = '/assets/img/zhihu.png'
    elif 'bilibili.com' in link_host:
        avatar = '/assets/img/bilibili.png'
    elif link_host == 'xueqiu.com':
        avatar = '/assets/img/xueqiu.jpg'
    elif 'tophub.today' in link_host:
        avatar = '/assets/img/tophub.png'
    elif 'github.com' in link_host:
        avatar = '/assets/img/github.png'
    elif 'juejin.im' in link_host:
        avatar = '/assets/img/juejin.png'
    elif 'rsshub.app' in link_host:
        avatar = '/assets/img/rsshub.png'
    elif 'ximalaya.com' in link_host:
        avatar = '/assets/img/ximalaya.png'
    elif 'smzdm.com' in link_host:
        avatar = '/assets/img/smzdm.jpg'
    elif settings.QNMLGB_HOST in link_host:
        avatar = '/assets/img/qnmlgb.png'
    elif link_host == 'weixin.sogou.com':
        avatar = '/assets/img/sogou.png'
    elif link_host == 'pubs.acs.org':
        avatar = '/assets/img/acs.jpg'
    elif 'vreadtech.com' in link_host:
        avatar = '/assets/img/vtechread.jpg'
    elif 'news.qq.com' in link_host:
        avatar = '/assets/img/qqcom.png'
    elif 'werss.app' in link_host:
        avatar = '/assets/img/werss.png'
    # 其次根据 feed 地址匹配
    elif 'feed43.com' in feed_host:
        avatar = '/assets/img/feed43.png'

    # 随机匹配一个 emoji
    if not avatar:
        avatar = get_random_emoji()

    return avatar

def get_with_retry(url):
    """
    普通爬取。重试 2 次
    :param url:
    :return:
    """
    for i in range(0, 2):
        headers = {'User-Agent': get_random_ua()}

        try:
            return requests.get(url, verify=False, timeout=30, headers=headers)
        except (HTTPError, Timeout, ConnectionError):
            logger.warning(f"请求出现网络异常：`{url}")
        except Exception as e:
            logger.warning(f"请求出现未知异常：`{url}`{e}")

        time.sleep(20)

    return None

def set_user_site_cname(oauth_id, site_id, site_name):
    key = settings.REDIS_USER_CONF_SITE_NAME_KEY % (oauth_id, site_id)
    return R.set(key, site_name)

def get_user_site_cname(oauth_id, site_id):
    key = settings.REDIS_USER_CONF_SITE_NAME_KEY % (oauth_id, site_id)
    return R.get(key)

def set_user_site_author(oauth_id, site_id, site_author):
    key = settings.REDIS_USER_CONF_SITE_AUTHOR_KEY % (oauth_id, site_id)
    return R.set(key, site_author)

def get_user_site_author(oauth_id, site_id):
    key = settings.REDIS_USER_CONF_SITE_AUTHOR_KEY % (oauth_id, site_id)
    return R.get(key)

def get_html_text(html):
    bs = BeautifulSoup(html, "html.parser")
    return bs.get_text()

def split_cn_words(cn, join=False, limit=None):
    """
    中文分词；停词不计算
    """
    seg_list, word_list = jieba.cut(cn), []

    for seg in seg_list:
        seg = seg.strip().lower()

        if seg and seg not in stopwords:
            word_list.append(seg)

    if limit:
        word_list = [i[0] for i in Counter(word_list).most_common(limit) if len(i[0]) > 1]

    if join:
        return ','.join(word_list)

    return word_list

def is_podcast_feed(feed_obj):
    try:
        return 'podcast' in feed_obj.namespaces.get('itunes', '')
    except AttributeError:
        pass

    return False

def trim_brief(brief):
    """
    去掉第三方说明性的文字
    """
    brief = re.sub(r' - Made with love by .*$', '', brief)
    return re.sub(r'\(RSS provided by .*$', '', brief).strip()

def to_podcast_duration(duration):
    dest = duration

    try:
        if ':' not in duration:
            seconds = int(duration)

            m, s = divmod(seconds, 60)
            h, m = divmod(m, 60)
            if h > 0:
                return "%02d:%02d:%02d" % (h, m, s)
            else:
                return "%02d:%02d" % (m, s)
    except ValueError:
        logger.warning(f"转换时间失败：`{duration}")

    return dest

# 搜索对象
whoosh_site_schema = Schema(
    id=ID(stored=True, unique=True),

    cname=TEXT(field_boost=5.0),
    author=TEXT(field_boost=3.0),
    brief=TEXT(),
)


from django.contrib import admin
from categories.models import Category

# Register your models here.
@admin.register(Category)
class StageAdmin(admin.ModelAdmin):
    list_display = (
        'id',
        'title',
        'sub_category',
        )
    search_fields = ('title',)


import os
from flask import Flask, jsonify, request, redirect, url_for
from flask_cors import CORS

from stats import getStats, isAveragingTripleDouble
from updateStats import updateStats

app = Flask(__name__)
CORS(app)

app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'my_secret_key')

###
# Routing for your application.
###

@app.route('/')
def home():
    """Is Westbrook averaging a triple double?"""
    return jsonify({
        "stats": getStats(),
        "answer": isAveragingTripleDouble()
    })

@app.route('/update', methods=["POST"])
def update():
    updateStats()
    return jsonify({
        "stats": getStats(),
        "answer": isAveragingTripleDouble()
    })

@app.after_request
def add_header(response):
    """
    Add headers to both force latest IE rendering engine or Chrome Frame,
    and also to cache the rendered page for 10 minutes.
    """
    response.headers['X-UA-Compatible'] = 'IE=Edge,chrome=1'
    response.headers['Cache-Control'] = 'public, max-age=600'
    return response

@app.errorhandler(404)
def page_not_found(error):
    """Custom 404 page."""
    return jsonify("True"), 404

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


import requests
import json

apikey = open("apikey.txt", 'r').readline().strip()

cityname = input("Enter City name: ")

complete_url = f"http://api.openweathermap.org/data/2.5/weather?q={cityname}&appid={apikey}"

reponse = requests.get(complete_url)

weather_data = reponse.json()

if weather_data["cod"] != "404":
    data = weather_data["main"]
    print(f"Weather details of ", weather_data["name"])
    temp = data["temp"]
    print(f"Current temperature is: {int(temp)-273} degree Celcius")

    desc = weather_data["weather"][0]["description"]
    print(f"Weather Description:\n {desc}")
else:
    print("City Not found, make sure you typed the city name correctly.\n")


from fastapi import FastAPI

import models
from database import engine
import Routers.userpost , Routers.users, Routers.Auth, Routers.Vote
from fastapi.middleware.cors import CORSMiddleware

models.Base.metadata.create_all(bind= engine)

app = FastAPI()

# origins = ["https://www.google.com", "https://www.youtube.com/"]
# origins = ["*"]
origins = [
            "https://www.youtube.com",
             "https:www.google.com"
             ]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get("/")
async def root():
    return {"message": "Welcome to FastApi"}

app.include_router(Routers.userpost.router)
app.include_router(Routers.users.router)
app.include_router(Routers.Auth.router)
app.include_router(Routers.Vote.router)


#This is Python Automation Project which takes your basic voice inputs and show the results.
#Python modules used in this project are: smtplib, datetime, wikipedia, pyttsx3, speech_recognition, webbrowser, os
#Module speech_recognition is giving some py_audio module errors which i am not able to correct, if you can please contribute.

import smtplib
import datetime
from time import strftime
import wikipedia
import pyttsx3
import speech_recognition as sr
import webbrowser
import os

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voices',voices[1].id)

def takecommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        r.pause_threshold = 1
        audio = r.listen(source)
    try:
         print("Recognizing your voice...")
         query = r.recognize_google(audio, language='en-in')
         print(f"You Said :{query}\n")
    except Exception as e:
        print("Please can you say it once again..")
        return "None"
    return query

def wishme():
    hour = int(datetime.datetime.now().hour)
    if hour >= 0 and hour < 12:
        speak("Good Morning")
    elif hour >= 12 and hour < 18:
        speak("Good Afternoon")
    else:
        speak("Good Evening")
    speak("Please let me know your query.")

def speak(audio):
    engine.say(audio)
    engine.runAndWait()

def sendEmail(to, connect):
    server = smtplib.SMTP('smtp.gmail.com',587)
    server.ehlo()
    server.starttls()
    server.login('','') # email and password from which email is sent
    server.sendmail('',to,connect) # email mentioned above
    server.close()

if __name__ == "__main__":
    wishme()
    while True:
        query = takecommand().lower()
        if 'open wikipedia' in query:
            speak("Searching Wikipedia...")
            query = query.replace("wikipedia","")
            results = wikipedia.summary(query, sentences = 2)
            speak("According to Wikipedia...")
            print(results)
            speak(results)

        if 'open notepad' in query:
            npath = "C:\\Windows\\System32\\notepad.exe"
            os.startfile(npath)

        elif 'open paint' in query:
            npath = "C:\\Windows\\System32\\mspaint.exe"
            os.startfile(npath)

        elif 'open youtube' in query:
            webbrowser.open('youtube.com')

        elif 'open google' in query:
            webbrowser.open('google.com')

        elif 'tell me the time ' in query:
            strtime = datetime.datetime.now(),strftime("%H:%M:%S")
            speak(f"Time is{strtime}")

        elif 'send email' in query:
            try:
                speak("What is the content for email?")
                content = takecommand()
                speak("What is the email id?")
                to = takecommand()
                sendEmail(to,content)
                speak("Email sent successfully :) ")
            except Exception as e:
                print(e)
                speak("I am unable to send the email... Please check and address the error mentioned")


from django.shortcuts import render
from .serializers import TaskSerializer
from .models import Task
from rest_framework import status
from django.http import JsonResponse
from rest_framework.response import Response
from rest_framework.decorators import api_view

@api_view(['GET'])
def apiOverview(request):
	api_urls = {
	  'List':'/tast-list/',
	  'Detail View:':'/task-detail/<str:pk>/',
	  'Create':'/task-create/',
	  'Update':'/task-update/<str:pk>/',
	  'Delete':'/task-delete/<str:pk>/',
	}

	return Response(api_urls)

@api_view(['GET'])
def taskList(request):
	tasks = Task.objects.all()
	serializer = TaskSerializer(tasks, many = True)
	return Response(serializer.data, status=status.HTTP_200_OK)

@api_view(['GET'])
def taskDetail(request, pk):
	tasks = Task.objects.get(pk = pk)
	serializer = TaskSerializer(tasks , many = False)
	return Response(serializer.data, status=status.HTTP_200_OK)

@api_view(['POST'])
def taskCreate(request):
	serializer = TaskSerializer(data = request.data)
	if serializer.is_valid():
		serializer.save()
		return Response(serializer.data, status =status.HTTP_200_OK)
	else:
		return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST)

@api_view(['GET', 'PUT'])
def taskUpdate(request, pk):
	tasks = Task.objects.get(pk = pk)
	serializer = TaskSerializer(instance= tasks, data = request.data)
	if serializer.is_valid():
		serializer.save()
		return Response(serializer.data, status = status.HTTP_200_OK)
	else:
		return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST)

@api_view(['GET', 'DELETE'])
def taskDelete(request, pk):
	tasks = Task.objects.get(pk = pk)
	tasks.delete()
	return Response(status = status.HTTP_204_NO_CONTENT)


import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
datatrain = pd.read_csv('Sacramento_train.csv')
zenith_rad_train = datatrain.pop('Zenith Angle [degrees]')*np.pi/180
datatrain = pd.concat([datatrain,zenith_rad_train],axis=1)
names = datatrain.columns.tolist()
names[names.index('Zenith Angle [degrees]')] = 'Zenith Angle [Radians]'
datatrain.columns = names
newdatatrain = datatrain[datatrain['Global Horizontal [W/m^2]'] > 0]
newdatatrain = newdatatrain[newdatatrain['Station Pressure [mBar]'] > 800]
newdatatrain = newdatatrain[newdatatrain['CR1000 Temp [deg C]'] > -2000]
newdatatrain = newdatatrain[newdatatrain['RSR Battery [VDC]'] > 2]
X_train = newdatatrain[['Air Temperature [deg C]','Rel Humidity [%]','Station Pressure [mBar]','Zenith Angle [Radians]',
 'CR1000 Temp [deg C]','RSR Battery [VDC]']]
Y_train = newdatatrain[['Global Horizontal [W/m^2]']]
datatest = pd.read_csv('Sacramento_test.csv')
zenith_rad_test = datatest.pop('Zenith Angle [degrees]')*np.pi/180
datatest = pd.concat([datatest,zenith_rad_test],axis=1)
names = datatest.columns.tolist()
names[names.index('Zenith Angle [degrees]')] = 'Zenith Angle [Radians]'
datatest.columns = names
newdatatest = datatest[datatest['Global Horizontal [W/m^2]'] > 0]
X_test = newdatatest[['Air Temperature [deg C]','Rel Humidity [%]','Station Pressure [mBar]','Zenith Angle [Radians]',
 'CR1000 Temp [deg C]','RSR Battery [VDC]']]
Y_test = newdatatest['Global Horizontal [W/m^2]']
decision_tree_regression(X_train, Y_train, X_test, Y_test, 2000)
linear_regression(X_train, Y_train, X_test, Y_test.to_frame(), 2000)
xgboost_regression(X_train.values, Y_train.values, X_test.values, Y_test.values, 2000)
x = newdatatrain[['Global Horizontal [W/m^2]','Air Temperature [deg C]','Rel Humidity [%]','Station Pressure [mBar]',
 'Zenith Angle [Radians]','CR1000 Temp [deg C]','RSR Battery [VDC]']]
corr = np.round(x.corr(), 2)
fig, ax = plt.subplots(figsize=(20, 20))
sns.heatmap(ax=ax,
 data=corr,
 annot=True,
 cmap="coolwarm",
 vmin=-1, vmax=1, center=0)
ax.set_title("Correlation Matrix Graph")
plt.yticks(rotation=0)
plt.show()
cor_target = abs(corr['Global Horizontal [W/m^2]'])


def main(net, filename=None, width=1000, height=800):
  import requests, json
  # from io import StringIO
  from IPython.display import IFrame, display

  try:
      from StringIO import StringIO
  except ImportError:
      from io import StringIO

  clustergrammer_url = 'http://amp.pharm.mssm.edu/clustergrammer/matrix_upload/'

  if filename is None:
    file_string = net.write_matrix_to_tsv()
    file_obj = StringIO(file_string)

    if net.dat['filename'] is None:
      fake_filename = 'Network.txt'
    else:
      fake_filename = net.dat['filename']

    r = requests.post(clustergrammer_url, files={'file': (fake_filename, file_obj)})
  else:
    file_obj = open(filename, 'r')
    r = requests.post(clustergrammer_url, files={'file': file_obj})

  link = r.text

  display(IFrame(link, width=width, height=height))

  return link


# WARNING: This module will only work on UNIX systems.
# Need a method to read stdin without blocking on Windows.

# Library modules
import threading
import select
import signal
import sys

# Third-party modules
import psutil

class StdinListener(threading.Thread):

    def __init__(self, serverInstances, version):
        super(StdinListener, self).__init__(name="Thread-PycraftStdinListener")

        self.daemon = True
        self.serverInstances = serverInstances
        self.stopping = False
        self.version = version

    def displayHelp(self, command=None):
        if command == "exit":
            print("exit:")
            print("Closes the Pycraft server wrapper. Any servers that are currently being")
            print("monitored by Pycraft will remain running inside their respective screen")
            print("sessions. Pycraft can be started again at any time and monitoring of those")
            print("servers will resume.")

        elif command == "help":
            print("help [command]:")
            print("Displays a description of the specified command, or a list of all available")
            print("commands if argument is omitted.")

        elif command == "list":
            print("list:")
            print("Displays a list of all of the Minecraft servers which Pycraft has been")
            print("configured to monitor. Servers are listed using their server nicknames,")
            print("which are unique server identifiers to be used when issuing a Pycraft")
            print("command.")

        elif command == "restart":
            print("restart <serverNick>:")
            print("If the specified server is currently in the online state, this command")
            print("will stop and then start the specified Minecraft server.")

        elif command == "start":
            print("start <serverNick>:")
            print("If the specified server is currently in the offline state, this command")
            print("will switch it into the online state. This will cause the server's start")
            print("script to the started inside a screen session. Server monitoring, automated")
            print("restarts, and other actions will then be performed on the running server")
            print("according to its Pycraft configuration.")

        elif command == "status":
            print("status <serverNick>:")
            print("Shows whether the specified server process is currently running, and if that")
            print("server is responding to network requests.")

        elif command == "stop":
            print("stop <serverNick>:")
            print("If the specified server is currently in the online state, this command")
            print("will switch it into the offline state. Pycraft will attempt to stop the")
            print("server gracefully at first, by issuing the \"stop\" command to the")
            print("Minecraft server console. If the server process does not terminate within")
            print("60 seconds, then a SIGKILL signal will be sent to the process.")

        else:
            print("Welcome to Pycraft version " + self.version + ". Available pycraft commands:")
            print("\texit")
            print("\thelp\t[command]")
            print("\tlist")
            print("\trestart\t<serverNick>")
            print("\tstart\t<serverNick>")
            print("\tstatus\t<serverNick>")
            print("\tstop\t<serverNick>")

    def getServerInstance(self, serverNick):
        for s in self.serverInstances:
            if s.getConfig("SERVER_NICK") == serverNick:
                return s

        print("Server {} was not found in the Pycraft configuration file.".format(serverNick))
        print("Please use the \"list\" command to see the list of currently configured")
        print("servers.")

        return None

    def run(self):
        self.displayHelp()
        sys.stdout.write("\npycraft> ")
        sys.stdout.flush()

        while not self.stopping:
            # NON-PORTABLE CODE: UNIX ONLY
            # (Couldn't find a Windows alternative to select for stdin)

            # Check if stdin has data to be read, otherwise timeout after 1 second.
            # Non-blocking behaviour allows this thread to be terminated from another
            # thread, which allows pycraft to be exited cleanly.

            if select.select([sys.stdin], [], [], 1) == ([sys.stdin], [], []):

                # Yes, stdin does have data
                command = sys.stdin.readline()
                commandList = command.split()

                # Empty command given by user
                if len(commandList) == 0:
                    self.displayHelp()

                else:
                    commandList[0] = commandList[0].lower()

                    if commandList[0] == "exit":
                        # Send SIGTERM to this process, terminating the main thread.
                        process = psutil.Process()
                        process.send_signal(signal.SIGTERM)
                        self.stopping = True
                        continue

                    elif commandList[0] == "help":
                        if len(commandList) == 1:
                            self.displayHelp()
                            continue

                        else:
                            self.displayHelp(commandList[1].lower())

                    elif commandList[0] == "list":
                        print("Pycraft has been configured to monitor the following servers:")

                        for s in self.serverInstances:
                            print("\t" + s.getConfig('SERVER_NICK'))

                    elif commandList[0] == "restart":
                        if len(commandList) != 2:
                            self.displayHelp("restart")

                        else:
                            s = self.getServerInstance(commandList[1])

                            if s is not None:
                                s.restart()

                    elif commandList[0] == "start":
                        if len(commandList) != 2:
                            self.displayHelp("start")

                        else:
                            s = self.getServerInstance(commandList[1])

                            if s is not None:
                                s.start()

                                # TODO catch exceptions

                    elif commandList[0] == "status":
                        if len(commandList) != 2:
                            self.displayHelp("status")

                        else:
                            s = self.getServerInstance(commandList[1])

                            if s is not None:
                                print("Current status of server:\t{}".format(s.getConfig("SERVER_NICK")))

                                if s.getTargetState():
                                    print("Target state:\tonline")
                                else:
                                    print("Target state:\toffline")

                                print("Is online:\t{}".format(s.isOnline()))
                                print("Is responsive:\t{}".format(s.isResponsive()))

                    elif commandList[0] == "stop":
                        if len(commandList) != 2:
                            self.displayHelp("stop")

                        else:
                            s = self.getServerInstance(commandList[1])

                            if s is not None:
                                s.stop()

                                # TODO catch exceptions

                    else:
                        # Unrecognised command
                        self.displayHelp()

                sys.stdout.write("\npycraft> ")
                sys.stdout.flush()

    def stop(self):
        self.stopping = True


from typing import Iterator
from collections import defaultdict, namedtuple
from aoc import run, get_int_numbers, pairwise, sign, batch

ROCK = '#'
SAND = 'o'
AIR = '.'

Point = namedtuple('Point', 'x, y')

def iter_line_points(start: Point, end: Point) -> Iterator[Point]:
    dx = sign(end.x - start.x)
    dy = sign(end.y - start.y)

    current = start
    while True:
        yield current
        if current == end:
            break
        current = Point(current.x + dx, current.y + dy)

def parse_input_as_map(lines):
    map = {}
    for line in lines:
        numbers = get_int_numbers(line)
        points = (Point(x, y) for (x, y) in batch(numbers, batch_size=2))
        for start, end in pairwise(points):
            for point in iter_line_points(start, end):
                map[point] = ROCK
    return map

def print_map(map):
    min_x = min(x for x, _ in map.keys())
    max_x = max(x for x, _ in map.keys())
    min_y = min(y for _, y in map.keys())
    max_y = max(y for _, y in map.keys())

    for y in range(min_y, max_y + 1):
        print(''.join(map.get((x, y)) or AIR for x in range(min_x, max_x + 1)))

def get_top_ys_by_x(map):
    top_ys_by_x = defaultdict(set)
    for (x, y) in map.keys():
        if (x, y - 1) not in map:
            top_ys_by_x[x].add(y - 1)
    return top_ys_by_x

def get_top_point(top_ys_by_x, source):
    top_ys = top_ys_by_x.get(source.x)
    if not top_ys:
        return None

    y = min((top_y for top_y in top_ys if top_y >= source.y), default=-1)
    return Point(source.x, y) if y != -1 else None

def simulate_sand(map, sand_source: Point):
    source = sand_source
    # Holds `y` per column (x) where sand can land
    top_ys_by_x = get_top_ys_by_x(map)

    # Uncomment for printing the source out
    # map[sand_source] = '+'

    while True:
        top_point = get_top_point(top_ys_by_x, source)
        if not top_point:
            break

        left_down_point = Point(top_point.x - 1, top_point.y + 1)
        right_down_point = Point(top_point.x + 1, top_point.y + 1)

        if not map.get(left_down_point):
            source = left_down_point
            continue

        if not map.get(right_down_point):
            source = right_down_point
            continue

        map[top_point] = SAND
        if top_point == sand_source:
            break

        # As sand landed, update with the new available `y`
        tops = top_ys_by_x[top_point.x]
        tops.remove(top_point.y)
        tops.add(top_point.y - 1)

        source = sand_source

def get_first(lines):
    map = parse_input_as_map(lines)
    sand_source = Point(500, 0)

    simulate_sand(map, sand_source=sand_source)
    return sum(1 for v in map.values() if v == SAND)

def get_second(lines):
    map = parse_input_as_map(lines)
    sand_source = Point(500, 0)

    max_y = max(y for _, y in map.keys()) + 2
    start_baseline = Point(sand_source.x - max_y, max_y)
    end_baseline = Point(sand_source.x + max_y, max_y)

    for point in iter_line_points(start_baseline, end_baseline):
        map[point] = ROCK

    simulate_sand(map, sand_source=sand_source)
    return sum(1 for v in map.values() if v == SAND)

if __name__ == '__main__':
    run(get_first, get_second)


import pathlib

import kfactory as kf
import pytest
from kfactory import kdb
from kfactory.conf import logger

from kgeneric import cells_dict

class GeometryDifference(ValueError):
    """Exception for Geometric differences."""

    pass

cell_names = set(cells_dict.keys())

@pytest.fixture(params=cell_names, scope="function")
def cell_name(request):
    """Returns cell name."""
    return request.param

def test_cells(cell_name: str) -> None:
    """Ensure cells have the same geometry as their golden references."""
    gds_ref = pathlib.Path(__file__).parent / "gds" / "gds_ref"
    cell = cells_dict[cell_name]()
    ref_file = gds_ref / f"{cell.name}.gds"
    run_cell = cell
    if not ref_file.exists():
        gds_ref.mkdir(parents=True, exist_ok=True)
        run_cell.write(str(ref_file))
        raise FileNotFoundError(f"GDS file not found. Saving it to {ref_file}")
    kcl_ref = kf.KCLayout(cell_name)
    kcl_ref.read(gds_ref / f"{cell.name}.gds")
    ref_cell = kcl_ref[kcl_ref.top_cell().name]

    for layer in kcl_ref.layer_infos():
        layer = kcl_ref.layer(layer)
        region_run = kdb.Region(run_cell.begin_shapes_rec(layer))
        region_ref = kdb.Region(ref_cell.begin_shapes_rec(layer))

        region_diff = region_run - region_ref

        if not region_diff.is_empty():
            layer_tuple = kcl_ref.layer_infos()[layer]
            region_xor = region_ref ^ region_run
            c = kf.KCell(f"{cell.name}_diffs")
            c_run = kf.KCell(f"{cell.name}_new")
            c_ref = kf.KCell(f"{cell.name}_old")
            c_xor = kf.KCell(f"{cell.name}_xor")
            c_run.shapes(layer).insert(region_run)
            c_ref.shapes(layer).insert(region_ref)
            c_xor.shapes(layer).insert(region_xor)
            c << c_run
            c << c_ref
            c << c_xor
            c.show()

            print(f"Differences found in {cell!r} on layer {layer_tuple}")
            val = input("Save current GDS as new reference (Y)? [Y/n]")
            if not val.upper().startswith("N"):
                logger.info(f"replacing file {str(ref_file)!r}")
                run_cell.write(ref_file.name)

            raise GeometryDifference(
                f"Differences found in {cell!r} on layer {layer_tuple}"
            )


# ======================================================================
# ======================================================================
# There are 5 questions in this exam with increasing difficulty from 1-5.
# Please note that the weight of the grade for the question is relative
# to its difficulty. So your Category 1 question will score significantly
# less than your Category 5 question.
#
# Don't use lambda layers in your model.
# You do not need them to solve the question.
# Lambda layers are not supported by the grading infrastructure.
#
# You must use the Submit and Test button to submit your model
# at least once in this category before you finally submit your exam,
# otherwise you will score zero for this category.
# ======================================================================
#
# Computer vision with CNNs
#
# Create and train a classifier for horses or humans using the provided data.
# Make sure your final layer is a 1 neuron, activated by sigmoid as shown.
#
# The test will use images that are 300x300 with 3 bytes color depth so be sure to
# design your neural network accordingly

import tensorflow as tf
import urllib
import zipfile
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Conv2D, Flatten, Conv1D, MaxPooling1D, MaxPooling2D

def solution_model():
    _TRAIN_URL = "https://storage.googleapis.com/download.tensorflow.org/data/horse-or-human.zip"
    _TEST_URL = "https://storage.googleapis.com/download.tensorflow.org/data/validation-horse-or-human.zip"
    urllib.request.urlretrieve(_TRAIN_URL, 'horse-or-human.zip')
    local_zip = 'horse-or-human.zip'
    zip_ref = zipfile.ZipFile(local_zip, 'r')
    zip_ref.extractall('tmp/horse-or-human/')
    zip_ref.close()
    urllib.request.urlretrieve(_TEST_URL, 'testdata.zip')
    local_zip = 'testdata.zip'
    zip_ref = zipfile.ZipFile(local_zip, 'r')
    zip_ref.extractall('tmp/testdata/')
    zip_ref.close()

    TRAIN_DIR ='./tmp/horse-or-human/'
    TEST_DIR = './tmp/testdata/'

    train_datagen = ImageDataGenerator(rescale=1./255)
        #Your code here. Should at least have a rescale. Other parameters can help with overfitting.)

    validation_datagen = ImageDataGenerator(rescale=1./255)
        #Your Code here)

    train_generator = train_datagen.flow_from_directory(
            TRAIN_DIR,)
        #Your Code Here)

    validation_generator = validation_datagen.flow_from_directory(
            TEST_DIR,)
        #Your Code Here)

    model = tf.keras.models.Sequential([
        # Note the input shape specified on your first layer must be (300,300,3)
        # Your Code here

        tf.keras.layers.Conv2D(128, (3,3), activation='relu', input_shape=(300,300,3)),
        tf.keras.layers.MaxPooling2D(2,2),
        tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
        tf.keras.layers.MaxPooling2D(2,2),
        tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
        tf.keras.layers.MaxPooling2D(2,2),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(64, activation='relu'),
        # This is the last layer. You should not change this code.
        tf.keras.layers.Dense(1, activation='sigmoid')
    ])

    model.compile(loss='binary_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])

    model.fit(train_generator,
              epochs=10,
              validation_data=validation_generator)

    return model

if __name__ == '__main__':
    model = solution_model()
    # model.save("mymodel.h5")
    model.save('./tf_cert/mymodel3.h5')

 # loss: [0.49247410893440247, 0.8289999961853027]


import sys
from pathlib import Path
import uuid
import shutil

from .normalize import normalize

CATEGORIES = {"Audio": [".mp3", ".aiff", ".amr", ".ogg", ".wav"],
              "Documents": [".doc", ".docx", ".rtf", ".xlsx", ".pptx", ".txt", ".pdf"],
              "Images":[".jpeg", ".png", ".jpg", ".svg"],
              "Video":[".avi", ".mp4", ".mov", ".mkv",],
              "Archives":[".zip", ".gz", ".tar"]}

list_know_ext = []
list_not_know_ext = []

def move_file(file: Path, root_dir: Path, categorie: str) -> None:
    target_dir = root_dir.joinpath(categorie)
    if not target_dir.exists():
        target_dir.mkdir()
    new_name = target_dir.joinpath(f"{normalize(file.stem)}{file.suffix}")
    if new_name.exists():
       new_name = new_name.with_name(f"{new_name.stem}-{uuid.uuid4()}{file.suffix}")
    file.rename(new_name)

def get_categories(file: Path) -> str:
    ext = file.suffix.lower()
    for cat, exts in CATEGORIES.items():
        if ext in exts:
            if ext not in list_know_ext:
                list_know_ext.append(ext)
            return cat
    if ext not in list_not_know_ext:
        list_not_know_ext.append(ext)
    return "Other"

def sort_folder(path: Path) -> None:
    for item in path.glob("**/*"):
        if item.is_file():
            if str(item).find('Archives') == -1 and str(item).find('Video') == -1 and str(item).find('Audio') == -1 and str(item).find('Documents') == -1 and str(item).find('Images') == -1:
                cat = get_categories(item)
                move_file(item, path, cat)

def delete_emtpy_dirs(path: Path) -> None:
    for item in path.glob("**/*"):
        if item.is_dir():
            delete_emtpy_dirs(item)
            if len(list(item.iterdir())) == 0:
                item.rmdir()

def upack_archive(path: Path) -> None:
    p = Path(path, "Archives")
    for item in p.glob("*"):
        if item.suffix == ".zip" or item.suffix == ".gz" or item.suffix == ".tar":
            target_dir = p.joinpath(item.stem)
            if not target_dir.exists():
                target_dir.mkdir()
            shutil.unpack_archive(item, target_dir)
            item.unlink()

def list_folder(path: Path) -> None:
    list_all = "\nAll found files have been sorted:\n"
    for item in path.glob("**/*"):
        if item.is_dir():
            list_all += f"\nfolder: {item}:\n"
            for files in item.glob("**/*"):
                if files.is_dir():
                    list_all += f"subfolder: {files.stem}:\n"
                if files.is_file():
                    list_all += f"{files.stem}{files.suffix}\n"
    return list_all

def main(path_str: str = None) -> str:
    if path_str is None:
        path_str = sys.argv[1]
    try:
        path = Path(path_str)
    except IndexError:
        return "No path to folder"
    if not path.exists():
        return f"Folder with path '{path}' dos`n exists."

    sort_folder(path)
    delete_emtpy_dirs(path)
    upack_archive(path)
    result = []
    result.append(list_folder(path))
    result.append(f"\nFound known file extensions: {list_know_ext}")
    result.append(f"\nFound not known file extensions: {list_not_know_ext}")
    result.append("\nFinish")
    return "\n".join(result)

if __name__ == "__main__":
    print(main())


# 京东 papa
import requests
from lxml import etree
import time

class JDDD:
    def __init__(self,book_name,page=1):
        self.page=page
        self.book_name = book_name
        self.data_list = []
    def JD(self):
        url = 'https://search.jd.com/Search'
        headers = {
            'User-Agent': '"Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5"',
            'Connection': 'close',
        }
        params = {
            'keyword': f'{self.book_name}',
            'enc': 'utf-8',
            'qrst': "1",
            'rt': "1",
            'stop': 'stop',
            "vt": "2",
            'suggest': '1.his.0.0',
            'page': f'{self.page}',
            "s": "60",
            'click': '0'
        }
        html = requests.get(url, params=params, headers=headers)
        tree = etree.HTML(html.content)
        info = tree.xpath('//*[@id="J_goodsList"]/ul/li')
        pag = tree.xpath('//*[@id="J_bottomPage"]/span[2]/em[1]/b/text()')
        print(pag)
        for i in info:
            title = i.xpath('./div/div[@class="p-img"]/a/@title')[0]
            img_url = i.xpath('./div/div[@class="p-img"]/a/img/@source-data-lazy-img')[0]
            price = i.xpath('./div/div[@class="p-price"]/strong/i/text()')[0]
            url = i.xpath('./div/div[@class="p-commit"]/strong/a/@href')[0]
            url = f'https:{url}' if 'http' not in url else url
            img_url = f'https:{img_url}' if 'https' not in img_url else img_url
            res = requests.get(url, params=params, headers=headers)
            trees = etree.HTML(res.text)
            data = trees.xpath('//ul[@id="parameter2"]/li/@title')
            if '\n  ' in data: data.remove('\n  ')
            tit = trees.xpath('//ul[@id="parameter2"]/li/text()')
            tit = [i.split('：')[0] for i in tit]
            if '\n    ' in tit:tit.remove('\n    ')
            data = {tit[i]: v for i, v in enumerate(data)}
            user = trees.xpath('//*[@id="p-author"]/a/text()')
            dic_info = {
                'name': title,
                'url': url,
                'user': user,
                'img': img_url,
                'terrace': '京东',
                'price': price,
                'info': data
            }
            self.data_list.append(dic_info)
        return self.data_list
    def dangdang(self):
        url = "http://search.dangdang.com/"
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36',
        }
        params = {
            "key": self.book_name,
            "act": "input",
            "page_index": None

        }

        ret = requests.get(url=url, params=params, headers=headers).text
        tree = etree.HTML(ret)
        pag = tree.xpath('//a[@name="bottom-page-turn"]/text()')
        if pag:
            pag = int(pag[-1])
        else:
            pag = 2
        for now_pag in range(1, pag):
            params["page_index"] = now_pag
            rea = requests.get(url=url, params=params, headers=headers).text

            tree = etree.HTML(rea)
            url_list = tree.xpath('//ul[@id="component_59"]/li/a/@href')

            for i in url_list:
                ref = requests.get(url=i, headers=headers).text
                tree = etree.HTML(ref)
                tit = tree.xpath('//ul[@class="key clearfix"]/li/text()')
                tit = [i.split('：') for i in tit]
                name =tree.xpath('//div[@class="name_info"]/h1/@title')[0] if tree.xpath('//div[@class="name_info"]/h1/@title') else '暂无数据'
                img= tree.xpath('//img[@id="largePic"]/@src')[0] if tree.xpath('//img[@id="largePic"]/@src') else '暂无数据'
                price = tree.xpath('// *[@id="dd-price"]/text()')[-1].strip() if tree.xpath('// *[@id="dd-price"]/text()') else '暂无数据'
                dic = {
                    'name': name ,
                    'url':i,
                    'user': "".join(tree.xpath('//span[@id="author"]//text()')).replace("\u3000", ""),
                    'img': img,
                    'terrace':'当当',
                    'price': price,
                    'info':{i[0]:"否"  if not i[1] else i[1] for i in tit }
                }

                self.data_list.append(dic)
            return self.data_list


# 定义一副扑克牌， 四个花色 红桃，梅花，方块，黑桃。 一个花色13张。 13 * 4 == 52  大小王 两张 一共54张
# 扑克牌要以列表保存， ["红桃",1-13] [[],[],[],[],[]54个] 大小王 ["Big_Joker"] ["Small_Joker"]
# python中有内置的库，叫做随机库
# 导入随机库的方式 import

def shengchan():
    file = open ( file="puke.xlsx", mode="a", encoding="utf8" )
    color = ["红桃", "黑桃", "方块", "梅花"]
    file.write ( f"[Big_Joker]\n[Small_Joker]\n" )
    for c in color:
        for n in range ( 1, 14 ):
            # file.write(str(c)+str(n)+"\n")
            file.write ( f"[花色：{c} 点数：{n}]\n" )
# shengchan()
import random
def chouka():
    with open(file="puke.xlsx",encoding="utf8") as f:
        lines = f.readlines()
        random_line = random.choice(lines)
        return random_line
        # print(type(random_list))
import  datetime
def t_time():
    now=datetime.datetime.now()
    now_str= now.strftime("%Y-%m-%d %H:%M:%S")
    return now_str
def wanjia():
    xuanshou = ["小孙", "小兰", "老赵", "老王"]
    for c in xuanshou:
        wanjia=random.choice(xuanshou)
        return wanjia
def danchou():
    jieguo = wanjia () + "在" + t_time () + "的时间，抽到了一张" + chouka ()+"的牌"
    file = open ( file="jilu.xlsx", mode="a", encoding="utf8" )
    file.write (jieguo)
    print ( jieguo )
import  os

import pandas
path = r'C:\Users\Administrator\Desktop\pythonProject\fanmao123\xiangmu\puke.xlsx'
if not os.path.exists(path):

    raw_data = pandas.read_excel(path, header=0)

# print(raw_data)

while True:
    print("这里是抽卡时间，请输入1开始随机抽卡：")
    number = input("\n1.一发入魂\n2.十连满命\n3.百连\n4.退出")
    if number == "0":
        shengchan()
        print("抽卡系统已经加载")
    elif number == "1":
        danchou()
    elif number == "2":
        for n in range ( 1, 11 ):
            danchou()
    elif number == "3":
        for n in range ( 1, 101):
            danchou()
    elif number == "4":
        break
    else:
        pass

'''''
python
import tkinter as tk
from tkinter import filedialog

# 打开文件选择对话框
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(filetypes=[('Text Files', '*.txt')])
'''''


#!/usr/bin/env python3
"""Iterates all romlist files, reads gamebits from known params, and fills in the XML file."""
import sys
import struct
import os
import os.path
import zlib
import re
import xml.etree.ElementTree as ET

objNames = {}
objIndex = {}
discPath = sys.argv[1]
xmlPath  = sys.argv[2]
gameBits = ET.parse(os.path.join(xmlPath, 'gamebits.xml')).getroot()
objects  = ET.parse(os.path.join(xmlPath, 'objects.xml')).getroot()
dlls     = ET.parse(os.path.join(xmlPath, 'dlls.xml')).getroot()

def dump(data):
    r = []
    for i in range(0, len(data), 4):
        r.append(data[i:i+4].hex().upper())
    return ' '.join(r)

for i, obj in enumerate(objects.findall('./object')):
    objNames[i] = obj.get('name')

# move text to description
#for bit in gameBits.findall('./bit'):
#    text = "".join(bit.itertext()).strip()
#    if text != '':
#        bit.text = ''
#        ET.SubElement(bit, 'description').text = text

# remove existing obj refs and unhelpful names
RE_ENDS_WITH_OBJ_ID = re.compile(r'[0-9A-F]{5}$')
for bit in gameBits.findall('./bit'):
    for ref in list(bit.findall('./objref')):
        bit.remove(ref)
    name = bit.get('name', None)
    if name is not None and RE_ENDS_WITH_OBJ_ID.search(name):
        del bit.attrib['name']

_objParamsCache = {}
def getObjParams(objId):
    """Get list of parameters for given object ID."""
    if objId in _objParamsCache: return _objParamsCache[objId]
    result = []
    obj = objects.find('./object[@id="0x%04X"]' % objId)
    if obj is None:
        raise ValueError("Object 0x%04X not found" % onbjId)
    params = obj.find('./params')
    if params is not None: params = params.findall('./param')
    if params is None or len(params) == 0:
        dllId = obj.get('dll', None)
        if dllId is None: return []
        dll = dlls.find('./dll[@id="%s"]' % dllId)
        if dll is None:
            raise ValueError("DLL '%s' not found" % dllId)
        params = dll.find('./objparams')
    if params is None: return []
    for param in params:
        # offset is unknown in some cases
        if param.get('offset', None) is not None:
            result.append({
                'type':       param.get('type',   None),
                'offset': int(param.get('offset', None), 0),
                'name':       param.get('name',   None),
            })
    _objParamsCache[objId] = result
    return result

def readFile(path):
    s = path.rfind('/')
    name = path[s+1:]
    s = name.find('.')
    name = name[0:s]

    # decompress
    with open(path, 'rb') as file:
        head = file.read(4)
        assert head == b'ZLB\0'
        version, decLen, compLen = struct.unpack('>3I', file.read(12))
        assert version == 1
        data = file.read(compLen)
        fileData = zlib.decompress(data)
        assert len(fileData) == decLen

    # iterate object defs
    offs = 0
    idx  = 0
    while offs < len(fileData):
        typ, size, flags = struct.unpack_from('>hBB', fileData, offs)
        unk4, unk5, unk6, map, x, y, z, id = struct.unpack_from('>BBBBfffi', fileData, offs+4)
        realId = objIndex.get(typ, typ)
        objName = objNames.get(realId, None) # value can be None, so don't pass default here
        if objName is None: objName = "obj%04X" % realId

        uniqueId = struct.unpack_from('>I', fileData, offs+0x14)[0] # grumble
        params = getObjParams(realId)
        for param in params:
            if param['type'] == 'GameBit':
                val = struct.unpack_from('>H', fileData, offs+param['offset'])[0] & 0x7FFF # grumble
                if val not in (0x95, 0x96, 0x7FFF):
                    bit = gameBits.find('./bit[@id="0x%04X"]' % val)
                    if bit is None:
                        sys.stderr.write("Bit 0x%04X not found (map %s obj %s ID %08X)\n" % (
                            val, name, objName, uniqueId))
                    else:
                        paramName = param.get('name', None)
                        if paramName is None: paramName = '0x%02X' % param['offset']
                        ET.SubElement(bit, 'objref', {
                            'map':   name,
                            'obj':   objName,
                            'objId': '0x%08X' % uniqueId,
                            'param': paramName,
                        })
                        #print("%12s %12s %08X %04X %s" % (name, objName, uniqueId, val, param['name']))

        idx += 1
        offs += size * 4

# read OBJINDEX lookup table
with open(os.path.join(discPath, 'OBJINDEX.bin'), 'rb') as file:
    idx = 0
    while True:
        d = file.read(2)
        if len(d) < 2: break
        id = struct.unpack('>h', d)[0] # grumble
        if id != -1: objIndex[idx] = id
        idx += 1

for name in os.listdir(discPath):
    if name.endswith('.romlist.zlb'):
        readFile(os.path.join(discPath, name))

print(ET.tostring(gameBits, encoding='unicode'))


#!python3
import urllib.request
from io import StringIO
import csv
import argparse
import datetime
from pythonds.basic.queue import Queue

# class server and request
class Server:
    def __init__(self):

        self.currentTask = None
        self.timeRemaining = 0

    def tick(self):
        if self.currentTask is not None:
            self.timeRemaining = self.timeRemaining - 1
            if self.timeRemaining <= 0:
                self.currentTask = None

    def busy(self):
        if self.currentTask is not None:
            return True
        else:
            return False

    def startNext(self, newtask):
        self.currentTask = newtask
        self.timeRemaining = newtask.getTime()

class Request:
    def __init__(self, data):
        self.pages = None
        self.timestamp = int(data[0])
        self.timetaken = int(data[2])

    def getStamp(self):
        return self.timestamp

    def getPages(self):
        return self.pages

    def getTime(self):
        return self.timetaken

    def waitTime(self, currenttime):
        return currenttime - self.timestamp

def main():
    """download the contents"""

    # Initializing parser
    commandParser = argparse.ArgumentParser(description="Send a url parameter to the script")
    # add the parameter's for file
    commandParser.add_argument("--file", type=str, help="Link to the csv file")
    # add the parameter's for server's
    commandParser.add_argument("--servers", type=int, help="Link to the csv file")
    # parse the argument's
    args = commandParser.parse_args()
    # if url is not given
    if not args.file:
        exit()
    if not args.servers:
        simulateOneServer(args.file)
    else:  # else simulate multiple servers function
        simulateManyServers(args.file, args.servers)

def simulateOneServer(file):
    """function to handle requests using one server"""

    global averageWait
    content = urllib.request.urlopen(file).read().decode("ascii", "ignore")  # fetch contents
    data = StringIO(content)
    # read csv file
    csv_reader = csv.reader(data, delimiter=',')

    dataList = []  # store data from csv

    for line in csv_reader:
        # Use list to store data
        dataList.append(line)

    requestQueue = Queue()  # queue tor requests
    waitingtimes = []  # list to store's that are  waiting before a request is processed
    server = Server()  # instantiate server class
    # listlength=len(dataList)-1
    for i in dataList:
        # iterated the requests
        request = Request(i)  # pass data to request class
        requestQueue.enqueue(request)  # enqueue the request object
        if (not requestQueue.isEmpty()) and (not server.busy()):  # if server is not busy and queue is not empty
            nexttask = requestQueue.dequeue()  # dequeue first item in queue
            waitingtimes.append(nexttask.waitTime(int(i[0])))  # append to waiting time to list
            server.startNext(nexttask)  # if server is free move to the next task
        server.tick()  # server timer
        averageWait = sum(waitingtimes) / len(waitingtimes)  # calculate average wait time
        print("Average Wait %6.2f secs %3d tasks remaining." % (
            averageWait, requestQueue.size()))  # similar to pritning example in notes

    print("Average latency is {} seconds".format(averageWait))
    return averageWait  # return latency

def simulateManyServers(file, noOfServers):
    global averageWait
    content = urllib.request.urlopen(file).read().decode("ascii", "ignore")  # fetch contents
    data = StringIO(content)
    # read csv file
    csv_reader = csv.reader(data, delimiter=',')

    dataList = []  # store data from csv
    for line in csv_reader:
        # Use list to store data
        dataList.append(line)

    requestQueue = Queue()  # queue for requests
    waitingtimes = []  # array to store waiting time
    servers = [Server() for a in range(noOfServers)]  # create the number of server objects passed as parameter
    # eg if 2 then two servers will be instantiated

    for i in dataList:
        # iterate the data list
        request = Request(i)  # create a request object
        requestQueue.enqueue(request)  # add object to queue

        for server in servers:  # iterate the servers
            if (not server.busy()) and (
                    not requestQueue.isEmpty()):
                nexttask = requestQueue.dequeue()
                waitingtimes.append(nexttask.waitTime(int(i[0])))  # append waiting time to list
                server.startNext(nexttask)  # start new task
            server.tick()  # server ticker
        averageWait = sum(waitingtimes) / len(waitingtimes)
        print("Average Wait %6.2f secs %3d tasks remaining." % (averageWait, requestQueue.size()))

    print("Average latency is {} seconds".format(averageWait))
    return averageWait  # return average latency

# Call main function when script runs
if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
"""
Script to run different scenarios.
"""

# Author: Runsheng Xu <rxx3386@ucla.edu>
# License: TDG-Attribution-NonCommercial-NoDistrib

import argparse
import importlib
import os
import sys
from omegaconf import OmegaConf

from opencda.version import __version__

def arg_parse():
    # create an argument parser
    parser = argparse.ArgumentParser(description="OpenCDA scenario runner.")
    # add arguments to the parser
    parser.add_argument('-t', "--test_scenario", required=True, type=str,
                        help='Define the name of the scenario you want to test. The given name must'
                             'match one of the testing scripts(e.g. single_2lanefree_carla) in '
                             'opencda/scenario_testing/ folder'
                             ' as well as the corresponding yaml file in opencda/scenario_testing/config_yaml.')
    parser.add_argument("--record", action='store_true',
                        help='whether to record and save the simulation process to .log file')
    parser.add_argument("--apply_ml",
                        action='store_true',
                        help='whether ml/dl framework such as sklearn/pytorch is needed in the testing. '
                             'Set it to true only when you have installed the pytorch/sklearn package.')
    parser.add_argument('-v', "--version", type=str, default='0.9.11',
                        help='Specify the CARLA simulator version, default'
                             'is 0.9.11, 0.9.12 is also supported.')
    # parse the arguments and return the result
    opt = parser.parse_args()
    return opt

def main():
    # parse the arguments
    opt = arg_parse()
    # print the version of OpenCDA
    print("OpenCDA Version: %s" % __version__)
    # set the default yaml file
    default_yaml = config_yaml = os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        'opencda/scenario_testing/config_yaml/default.yaml')
    # set the yaml file for the specific testing scenario
    config_yaml = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                               'opencda/scenario_testing/config_yaml/%s.yaml' % opt.test_scenario)
    # load the default yaml file and the scenario yaml file as dictionaries
    default_dict = OmegaConf.load(default_yaml)
    scene_dict = OmegaConf.load(config_yaml)
    # merge the dictionaries
    scene_dict = OmegaConf.merge(default_dict, scene_dict)

    # import the testing script
    testing_scenario = importlib.import_module(
        "opencda.scenario_testing.%s" % opt.test_scenario)
    # check if the yaml file for the specific testing scenario exists
    if not os.path.isfile(config_yaml):
        sys.exit(
            "opencda/scenario_testing/config_yaml/%s.yaml not found!" % opt.test_cenario)

    # get the function for running the scenario from the testing script
    scenario_runner = getattr(testing_scenario, 'run_scenario')
    # run the scenario testing
    scenario_runner(opt, scene_dict)

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print(' - Exited by user.')


'''
Note that the moment accountant/rdp_accountant function from tensorflow_privacy
is an analytic method specifically designed for gaussian pdf.
Here in this code, we follow the definitions of rdp strictly,
that is, we perform integrations to obtain the moments.
'''

import tensorflow as tf
import numpy as np
import mpmath as mp
import scipy as sp
import matplotlib.pyplot as plt

plt.rcParams["font.family"] = "Times New Roman"

from tensorflow_privacy.privacy.analysis.rdp_accountant import compute_rdp
from tensorflow_privacy.privacy.analysis.rdp_accountant import get_privacy_spent

def compute_rdp_general(noise_type,orders,c_type,C,sen,q):

  # note that here the universal parameter for all distributions is the sigma,
  # so if you'd like to use L1-norm, you must choose the approperate sigma!!!

    if noise_type=="cactus":
        # For cactus, we directly use the discrete pmf from file to compute the renyi-divergence with given order

        filename = ('TestData/cactusM_s%.1f_%s_c%.2f_x.csv' %(sen,c_type,C))
        x = np.genfromtxt(filename, dtype=np.float32)
        x = x.reshape(-1)

        filename = ('TestData/cactusM_s%.1f_%s_c%.2f_p.csv' %(sen,c_type,C))
        p = np.genfromtxt(filename, dtype=np.float32)
        p = p.reshape(len(p),1)

        xmax=max(x)
        n = len(x)/2//xmax
        shift_n = int(n*sen)

        ps = p[shift_n:].reshape(-1)
        x=np.linspace(-xmax+sen,xmax,len(ps))

        l1 = sum(ps*np.abs(x))
        print("l1 cost =", l1)
        l2 = sum(ps*np.power(x,2))
        print("l2 cost =", l2)

        qs = q*p[:len(p)-shift_n].reshape(-1)+(1-q)*ps

    else: # For all other distributions, we sample the pdf to generate the Renyi-divergence!

        n=500.0 # quantization rate
        xmax=max(10,10*np.sqrt(C)) # the domin limit
        x=np.linspace(-xmax, xmax, int(2*xmax*n)) # domain of the distribution
        if noise_type=="gaussian":
            sigma=np.sqrt(C) if c_type=="l2" else C*np.sqrt(np.pi/2)
            f = lambda x: 1/np.sqrt(2*np.pi)/sigma*np.exp(-x**2/2/sigma**2)

        if noise_type == "laplace":
            b = np.sqrt(C/2) if c_type=="l2" else C
            f = lambda x:1/2/b*np.exp(-np.abs(x)/b)

        if noise_type == "airy":
            a0=1.01879
            f = lambda x: np.array(sp.special.airy(2*a0/3/C*np.abs(x)-a0)[0])**2.0/3.0/C/np.array(sp.special.airy(-a0)[0])**2.0

        ps = np.array([f(X) for X in x])/n
        l1 = sum(ps*np.abs(x))
        print("l1 cost =", l1)
        l2 = sum(ps*np.power(x,2))
        print("l2 cost =", l2)

        qs = q*np.array([f(X) for X in x-sen])/n+(1-q)*ps
        ps= ps.reshape(-1)
        qs = qs.reshape(-1)

    logmgf = lambda t: np.log(sum(qs*np.power(ps/qs,t)))
    return [logmgf(alpha)/(alpha-1) for alpha in orders]

def compute_epsilon(steps,orders,rdp_array,delta):
    rdp_array = rdp_array*np.array(steps)
    return get_privacy_spent(orders, rdp_array, target_delta=delta)[0]

def compute_delta(steps,orders,rdp_array,eps):
    rdp_array = rdp_array*np.array(steps)
    return get_privacy_spent(orders, rdp_array, target_eps=eps)[1]


# Neetcode: Using Recursion - https://youtu.be/QHH6rIK3dDQ
# merging them in to a 'new' binary tree (as asked in the question)

from typing import Optional

# Definition for a binary tree node.

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
        if not root1 and not root2:
            return None

        v1 = root1.val if root1 else 0
        v2 = root2.val if root2 else 0
        root = TreeNode(v1+v2)

        root.left = self.mergeTrees(root1.left if root1 else None,
                                    root2.left if root2 else None)
        root.right = self.mergeTrees(root1.right if root1 else None,
                                     root2.right if root2 else None)
        return root

# OR

class Solution:
    def mergeTrees(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> Optional[TreeNode]:
        if not root1:
            return root2
        if not root2:
            return root1
        t = TreeNode(root1.val+root2.val)
        t.left = self.mergeTrees(root1.left, root2.left)
        t.right = self.mergeTrees(root1.right, root2.right)
        return t

root1 = [1, 3, 2, 5]
root2 = [2, 1, 3, None, 4, None, 7]
obj = Solution()
print(obj.mergeTrees(root1, root2))

# Complexity Analysis:

# Time complexity: O(m+n) where m and n are the sizes of each of the input trees. actually O(n)
# Space complexity : O(n). extra tree created


#!/usr/bin/env python

from chdrft.cmds import CmdsList
from chdrft.main import app
from chdrft.utils.cmdify import ActionHandler
from chdrft.utils.misc import Attributize
import glog
from collections import defaultdict

global flags, cache
flags = None
cache = None

def args(parser):
  clist = CmdsList().add(test)
  ActionHandler.Prepare(parser, clist.lst)
  parser.add_argument('--n', type=int, default=35)

vec = [[1, 5, [10, -2, 0, 0, 0, 0, 2, 0]],
       [1, 2, [-10, 2, 0, 0, 0, 0, 2, 0]],
       [2, 1, [-10, 2, 0, 0, 0, 0, 2, 0]],
       [2, 3, [0, -4, 0, 0, 0, 0, -3, 1]],
       [3, 2, [0, -4, 0, 0, 0, 0, -3, 1]],
       [3, 4, [0, 0, 0, 0, 0, 0, 2, -2]],
       [4, 3, [0, 0, 0, 0, 0, 0, 2, -2]],
       [4, 5, [0, 4, 0, 0, 0, 0, -3, 1]],
       [5, 4, [0, 4, 0, 0, 0, 0, -3, 1]],
       [5, 1, [10, -2, 0, 0, 0, 0, 2, 0]],]

actions = defaultdict(lambda: [])
for src, dst, v in vec:
  actions[src].append((dst, v))

def add(a, b):
  return tuple([x + y for x, y in zip(a, b)])

def test(ctx):
  zpos = tuple([0] * len(vec[0][2]))
  state = (1, zpos)
  cur = {state: 1}
  for step in range(flags.n):
    print('ON >> ', step, len(cur))
    nxt = defaultdict(lambda: 0)
    for (pos, v), count in cur.items():
      for npos, dv in actions[pos]:
        nstate = (npos, add(v, dv))
        nxt[nstate] += count
    cur = nxt

  tot = 0
  for i in range(1, 6):
    tot += cur[(i, zpos)]
  print(tot)

def main():
  ctx = Attributize()
  ActionHandler.Run(ctx)

# could do meet in the middle
#331951449665644800

app()


import pdb
import os
import sys
import csv
import json
import numpy as np

def split_by_month(data):
    trn, vld, tst = [], [], []

    numdata = len(data)

    data.sort(key=lambda x:x[-1]) # sort data by date (in unix timestamp)

    times = np.array([float(i[-1]) for i in data])

    oldest_time = max(times)
    start_test_time = oldest_time - 30 * 24 * 60 * 60 # (30 days)
    start_valid_time = start_test_time - 30 * 24 * 60 * 60 # (30 days)

    tst_idx = times >= start_test_time
    vld_idx = (times >= start_valid_time) * (times < start_test_time)
    trn_idx = ~(tst_idx + vld_idx)

    trn_end_idx = np.where(trn_idx == True)[0].max()
    vld_end_idx = np.where(vld_idx == True)[0].max()

    trn = data[:trn_end_idx]
    vld = data[trn_end_idx:vld_end_idx]
    tst = data[vld_end_idx:]

    # Filter out new (cold-start) users and items
    trnusers = set([i[0] for i in trn])
    trnitems = set([i[1] for i in trn])

    vld = [row for row in vld if (row[0] in trnusers and row[1] in trnitems)]
    tst = [row for row in tst if (row[0] in trnusers and row[1] in trnitems)]

    print('\nTraining data:\t\t {}'.format(len(trn)))
    print('Validation data:\t {}'.format(len(vld)))
    print('Test data:\t\t {}'.format(len(tst)))
    print('\n# of total data:\t {} / {}'.format(len(trn) + len(vld) + len(tst), len(data)))

    return trn, vld, tst

fn = sys.argv[1]

print('\n'+fn+'\n')

data = [json.loads(l) for l in open(fn)]
# This code assumes Amazon data JSON format. The Amazon data are avaialble at 'http://jmcauley.ucsd.edu/data/amazon/'.
mydata = [[l['reviewerID'], l['asin'], l['overall'], int(float(l['unixReviewTime']))] for l in data]

trndata, vlddata, tstdata = split_by_month(mydata)

dname = fn.split('_')[1].lower()
dname = dname.split('.')[0]

basename = dname
if not os.path.exists(basename): os.makedirs(basename)

dirname = dname+'/split/'
if not os.path.exists(dirname): os.makedirs(dirname)

# Save the dataset in csv format
writer = csv.writer(open(dirname+'trn.csv', 'w'))
writer.writerows(trndata)

writer = csv.writer(open(dirname+'vld.csv', 'w'))
writer.writerows(vlddata)

writer = csv.writer(open(dirname+'tst.csv', 'w'))
writer.writerows(tstdata)

print('\nDone\n')


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

# <b> Car Price Prediction Project
#

# In[53]:

#importing the depedencies

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Lasso
from sklearn import metrics

# In[54]:

#Data Processing and Collection

#loading Data as pandas dataframe

Car_price = pd.read_csv('/home/farhood/Desktop/car data.csv')

#first 10 row of the dataset
Car_price.head(10)

#Note : you can change the number of the appering rows by changing the n in the 'head(n)'

# In[55]:

# how to know all rows and columns in the dataset
Car_price.shape

# In[56]:

#get more informations about the dataset
Car_price.info()

#for describing the dataset
Car_price.describe()

# In[57]:

# check the number of the missing values

Car_price.isnull().sum()

# In[58]:

# check how many cars are petrol or diesel

print(Car_price.Fuel_Type.value_counts())
print(Car_price.Seller_Type.value_counts())
print(Car_price.Transmission.value_counts())

# In[59]:

#Encoding The Categorical Data

# encoding "Fuel_Type" Column
Car_price.replace({'Fuel_Type':{'Petrol':0,'Diesel':1,'CNG':2}},inplace=True)

# encoding "Seller_Type" Column
Car_price.replace({'Seller_Type':{'Dealer':0,'Individual':1}},inplace=True)

# encoding "Transmission" Column
Car_price.replace({'Transmission':{'Manual':0,'Automatic':1}},inplace=True)

# In[60]:

Car_price.head(10)

# In[61]:

#Spliting Data and target

#
X = Car_price.drop(['Car_Name', 'Selling_Price'], axis =1)
Y = Car_price['Selling_Price']

# In[62]:

# Spliting Data into Train and Test

x_train , x_test, y_train , y_test = train_test_split(X,Y, test_size = 0.1, random_state = 2)

# In[63]:

# Model training

#loading the linear regression model

lin_reg_model = LinearRegression()

# In[64]:

lin_reg_model.fit(x_train,y_train)

# In[65]:

# Model Evalution

#prediction on Training data

training_data_prediction = lin_reg_model.predict(x_train)

# In[66]:

# compare the x_trian result with y_train

#R squared Error

error_score = metrics.r2_score(y_train, training_data_prediction)

print("the R error square is equal : ", error_score)

# In[67]:

#visual the actual and predcit prices
plt.scatter(y_train , training_data_prediction, color = "green")
plt.xlabel("Actual prices")
plt.ylabel("predicted prices")
plt.title("comparesion of the Actual and predcited prices")
plt.show()

# In[68]:

#prediction on Test data

test_data_prediction = lin_reg_model.predict(x_test)

# In[69]:

# compare the x_test result with y_test

#R squared Error

error_score = metrics.r2_score(y_test, test_data_prediction)

print("the R error square is equal : ", error_score)

# In[70]:

#visual the actual and predcit prices
plt.scatter(y_test , test_data_prediction, color = "green")
plt.xlabel("Actual prices")
plt.ylabel("predicted prices")
plt.title("comparesion of the Actual and predcited prices")
plt.show()

# In[71]:

#Lasso regression

# Model training

#loading the linear regression model

lass_reg_model = Lasso()

# In[72]:

lass_reg_model.fit(x_train,y_train)

# In[73]:

# Model Evalution

#prediction on Training data

training_data_prediction = lass_reg_model.predict(x_train)

# In[74]:

# compare the x_trian result with y_train

#R squared Error

error_score = metrics.r2_score(y_train, training_data_prediction)

print("the R error square is equal : ", error_score)

# In[75]:

#visual the actual and predcit prices
plt.scatter(y_train , training_data_prediction, color = "green")
plt.xlabel("Actual prices")
plt.ylabel("predicted prices")
plt.title("comparesion of the Actual and predcited prices")
plt.show()

# In[76]:

#prediction on Test data

test_data_prediction = lass_reg_model.predict(x_test)

# In[77]:

# compare the x_test result with y_test

#R squared Error

error_score1 = metrics.r2_score(y_test, test_data_prediction)

print("the R error square is equal : ", error_score1)

# In[78]:

# compare the x_test result with y_test

#R squared Error

error_score = metrics.r2_score(y_test, test_data_prediction)

print("the R error square is equal : ", error_score)

# In[79]:

#visual the actual and predcit prices
plt.scatter(y_test , test_data_prediction, color = "green")
plt.xlabel("Actual prices")
plt.ylabel("predicted prices")
plt.title("comparesion of the Actual and predcited prices")
plt.show()


from torch.utils.data import Dataset, DataLoader
from PIL import Image
import os
class LFWDataset(Dataset):

    """Faces in the wild dataset."""

    def __init__(self, root_dir='lfw/', train=False, test=False, transform=None):
        self.train = train
        self.test = test
        self.root_dir = root_dir
        self.files = []
        self.labels = []
        self.transform = transform

        if (self.train and self.test) or not (self.train or self.test):
            raise ValueError('Exactly one of train and test must be set.')

        """
        Getting Train/Test splits
        """
        dataset = set()
        if self.train:
            filename='train.txt'
        else:
            filename='test.txt'

        with open(filename) as f:
            for line in f:
                line = line.split()
                self.files.append(line[:2])
                self.labels.append(int(line[2]))

        #print(self.files)

    def __len__(self):
        return len(self.files)
#         return sum([len(files) for r, d, files in os.walk(self.root_dir)])
#         https://stackoverflow.com/questions/16910330/return-number-of-files-in-directory-and-subdirectory

    def __getitem__(self, idx):
        im_names = self.files[idx]
        im1 = Image.open(os.path.join(self.root_dir, im_names[0]))
        im2 = Image.open(os.path.join(self.root_dir, im_names[1]))
        label = self.labels[idx]

        if self.transform:
            im1 = self.transform(im1)
            im2 = self.transform(im2)

        return im1, im2, label


from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///mydatabase.db'
db = SQLAlchemy(app)

class Item(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(150), nullable=False)

@app.route('/')
def index():
    items = Item.query.all()
    return render_template('index.html', items=items)

@app.route('/add', methods=['POST', 'GET'])
def add():
    if request.method == 'POST':
        name = request.form.get('name')
        new_item = Item(name=name)
        db.session.add(new_item)
        db.session.commit()
        return redirect(url_for('index'))
    return render_template('add.html')

@app.route('/edit/<int:id>', methods=['POST', 'GET'])
def edit(id):
    item = Item.query.get(id)
    if request.method == 'POST':
        item.name = request.form.get('name')
        db.session.commit()
        return redirect(url_for('index'))
    return render_template('edit.html', item=item)

@app.route('/delete/<int:id>')
def delete(id):
    item = Item.query.get(id)
    db.session.delete(item)
    db.session.commit()
    return redirect(url_for('index'))

def create_db():
    with app.app_context():
        db.create_all()

if __name__ == '__main__':
    create_db()
    app.run(debug=True, host='0.0.0.0')


from gi.repository import GObject, Gtk
from cambalache import _, CmbObject, CmbUI

class CmbToplevelChooser(Gtk.ComboBox):
    __gtype_name__ = "CmbToplevelChooser"

    object = GObject.Property(type=CmbUI, flags=GObject.ParamFlags.READWRITE)
    derivable_only = GObject.Property(type=bool, default=False, flags=GObject.ParamFlags.READWRITE)

    def __init__(self, **kwargs):
        self.filter = None

        super().__init__(**kwargs)
        self.connect("notify::object", self.__on_object_notify)
        self.connect("changed", self.__on_changed)

        renderer = Gtk.CellRendererText()
        self.pack_start(renderer, True)
        self.set_cell_data_func(renderer, self.__name_cell_data_func, None)

    def __name_cell_data_func(self, column, cell, model, iter_, data):
        obj = model.get_value(iter_, 0)

        if type(obj) != CmbObject:
            return

        name = f"{obj.name} " if obj.name else ""
        extra = _("(template)") if not obj.parent_id and obj.ui.template_id == obj.object_id else obj.type_id
        cell.set_property("markup", f"{name}<i>{extra}</i>")

    def __filter_func(self, model, iter, data):
        obj = model[iter][0]

        if self.object.ui_id != obj.ui_id:
            return False

        if type(obj) == CmbObject:
            if self.derivable_only:
                return obj.info.derivable and obj.parent_id == 0
            else:
                return obj.parent_id == 0

        return False

    def __on_object_notify(self, obj, pspec):
        self.props.model = None
        self.filter = None

        if self.object is None:
            return

        project = self.object.project
        iter = project.get_iter_from_object(self.object)
        path = project.get_path(iter)

        # Create filter and set visible function before using it
        self.filter = project.filter_new(path)
        self.filter.set_visible_func(self.__filter_func)

        # Use filter as model
        self.props.model = self.filter

    def __on_changed(self, combo):
        self.notify("cmb-value")

    @GObject.Property(type=int)
    def cmb_value(self):
        if self.filter is None:
            return 0

        iter = self.get_active_iter()
        if iter is None:
            return 0

        row = self.filter[iter]
        return row[0].object_id if row else 0

    @cmb_value.setter
    def _set_cmb_value(self, value):
        if self.object is None:
            return

        iter = self.object.project.get_iter_from_object_id(self.object.ui_id, value)
        if iter:
            valid, filter_iter = self.filter.convert_child_iter_to_iter(iter)
            self.set_active_iter(filter_iter if valid else None)


import numpy as np
from numpy.linalg import matrix_rank
from solution_options import *
from scipy.optimize import minimize
import cvxpy as cp

get_idx_gencost = lambda results, threshold: (results['gen'][:, 0][results['gencost'][:, 4] > threshold] - 1).astype(int)

def Rank_info(matrix):
    print('#Var = %d'% matrix.shape[1])
    print('#Eq = %d'% matrix.shape[0])
    r = matrix_rank(matrix)
    #print('Rank A = %d'% r)
    print('Underdetermined: ', matrix.shape[1] >= r)

def mu_sigma_pi(muls):
    mu = np.concatenate((muls['mu_v_max'], muls['mu_v_min']))
    sigma = np.concatenate((muls['mu_s_from'], muls['mu_s_to']))
    pi = np.concatenate((muls['mu_p_max'], muls['mu_p_min']))
    #rho = np.concatenate((muls['mu_q_max'], muls['mu_q_min']))
    return mu, sigma, muls['mu_p_max'], muls['mu_p_min'], muls['mu_q_max'], muls['mu_q_min']

def A_sigmas(Js, sigma, mode, A_blocks, b, xs):
    if mode == fixed:
        b -= np.dot(Js.T.toarray(), sigma)
    elif mode == zero_fixed:
        idx_sigma = sigma > 0
        A_blocks.append((Js.T[:, idx_sigma]).toarray())
        xs['sigma'] = A_blocks[-1].shape[1]
    else:
        A_blocks.append((Js.T).toarray())
        xs['sigma'] = A_blocks[-1].shape[1]

def A_mu(mu, mode, A_blocks, b, xs):

    Nbus = len(mu)//2
    if mode == fixed:
        b -= mu
    elif mode == zero_fixed:
        idx_mu = mu > 0
        Jmu = np.block([[np.zeros((Nbus, Nbus)), np.zeros((Nbus, Nbus))],
                        [np.eye(Nbus), -np.eye(Nbus)]])
        A_blocks.append(Jmu[:, idx_mu])
        xs['mu'] = A_blocks[-1].shape[1]
    else:
        Jmu = np.block([[np.zeros((Nbus, Nbus)), np.zeros((Nbus, Nbus))],
                        [np.eye(Nbus), -np.eye(Nbus)]])
        A_blocks.append(Jmu)
        xs['mu'] = A_blocks[-1].shape[1]

def ModCost(C, idx, new_C):
    tmp_C = C.copy()
    tmp_C[idx] = new_C
    return tmp_C

def LS_NNLS(A, b, alg, verbose=False):
    x = cp.Variable(A.shape[1])
    cost = cp.sum_squares(A*x - b)
    if alg == LS:
        prob = cp.Problem(cp.Minimize(cost))
        #res = np.linalg.lstsq(A, b)
    else:
        prob = cp.Problem(cp.Minimize(cost), [x >= np.zeros_like(x)])
        #res = nnls(A, b)
    prob.solve()
    if verbose:
        print('f(x^*) = %f' % prob.value)
    return np.array(x.value).ravel()#res[0]

def B_LS(A, b, bounds, threshold, verbose=False):
    #x = cp.Variable(A.shape[1])
    #cost = cp.sum_squares(A*x - b)
    #b_min, b_max = bounds
    #constraints = []
    #if any(b_min != None):
    #    constraints.append(x[b_min != None] >= b_min[b_min != None])
    #if any(b_max != None):
    #    constraints.append(x[b_max != None] <= b_max[b_max != None])

    #prob = cp.Problem(cp.Minimize(cost), constraints)
    #prob.solve(solver=cp.ECOS_BB, verbose=True)
    f = lambda x: np.dot(np.dot(A, x) - b, np.dot(A, x) - b)
    np.random.seed(0)
    x0 = threshold*np.random.rand(A.shape[1])#np.random.randint(low=0., high=threshold, size=A.shape[1])
    res = minimize(f,
                   x0,
                   method='SLSQP',
                   bounds=bounds,
                   tol=1e-8,
                   options={'maxiter': 10**10,})
    if verbose:
        print('f(x^*) =', res.fun)
    #print(np.array(x.value).ravel())
    return res.x#np.array(x.value).ravel()#


"""
Bir klasör icindeki tüm dosya ve klasörleri os modülü ile okuruz
Bunlarin tipi:
*dosya
*klasör

"""

#Klasör icerigi okuma
import os
"""
#1nci yol
dosya_yolu=os.getcwd()
icerik=os.listdir(dosya_yolu)
print(icerik)
for ic in icerik:
    print(ic)

#2nci Yol
folder=os.scandir(dosya_yolu)
for eleman in folder:
    print(eleman)

#3ncü yol
from pathlib import Path
icerik=Path(dosya_yolu)
for ic_eleman in icerik.iterdir():
    print(ic_eleman)
"""
dosya_yolu=os.getcwd()

icerik=os.listdir(dosya_yolu)
for ic in icerik:
    if os.path.isfile(ic):
        print(ic)

#2nci Yol
with os.scandir(dosya_yolu) as scans:
    for scan in scans:
        if scan.is_file():
            print(scan)

#3ncü Yol
from pathlib import Path
ana_dosya=Path(dosya_yolu)
dosya_icerigi=ana_dosya.iterdir()

for dos in dosya_icerigi:
    if dos.is_file():
        print(dos)


from keras.utils import to_categorical
import numpy as np
import os
from keras.models import Sequential
from keras.layers import LSTM, Dense
from keras.callbacks import TensorBoard, EarlyStopping

r'''.\venv\Scripts\activate'''
r'''tensorboard --logdir=.'''

actions = ["Happy", "Sad", "Dizzy", "Thinking", "ThankYou", "Greeting", "Victory"]
label_map = {label: num for num, label in enumerate(actions)}

sequences, labels = [], []
no_videos = 30
no_frames = 30
DATA_PATH = "Dataset"

for action in actions:
    for sequence in range(no_videos):
        window = []
        for frame_num in range(no_frames):
            res = np.load(os.path.join(DATA_PATH, action, str(sequence), "{}.npy".format(frame_num)))
            window.append(res)
        sequences.append(window)
        labels.append(label_map[action])

x = np.array(sequences)
y = to_categorical(labels).astype(int)

print(np.array(sequences, dtype="object").shape)

log_dir = os.path.join("Logs")
tb_callback = TensorBoard(log_dir=log_dir)
es = EarlyStopping(monitor='loss', mode='auto', baseline=0.1, restore_best_weights=True,patience=50)

model = Sequential()
model.add(LSTM(64, return_sequences=True, activation='relu', input_shape=(x.shape[1], x.shape[2])))
model.add(LSTM(128, return_sequences=True, activation='relu'))
model.add(LSTM(64, return_sequences=False, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(len(actions), activation='softmax'))

model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['categorical_accuracy'])
model.fit(x, y, epochs=500, callbacks=tb_callback)
model.save("model.h5")


from django.http import HttpResponse,HttpResponseRedirect
from django.shortcuts import render,redirect
from .forms import usersform

def homepage(request):
    data={
        'value':2,
        'f':[10,20,30,40],
        's':[10,30,40,50],
        'v':'''welcom to wscubetech'''
    }
    return render(request,"index.html")

def submitform(request):
    try:
        if request.method=="POST":
        # n1=int(request.GET['num1'])
        # n2=int(request.GET['num2'])
            n1=int(request.POST.get('num1'))
            n2=int(request.POST.get('num2'))
            finalans=n1+n2
            data={
                'n1':n1,
                'n2':n2,
                'output':finalans
            }

            #return HttpResponseRedirect(url)
            return HttpResponse(finalans)
    except:
        pass

        return redirect(request,"userform.html")

def aboutUS(request):
    if request.method=='GET':
        output=request.GET.get('output')
    return render(request,"aboutus.html",{'output':output})

def service(request):
    return render(request,"services.html")

def contact(request):
    return render(request,"contact.html")

def gallery(request):
    return render(request,"gallery.html")

def fact_us(request):
    return render(request,"fact.html")

def pageus(request):
    return render(request,"page.html")

def calculator(request):
    c=''
    try:
        if request.method=="POST":
            n1=eval(request.POST.get('num1'))
            n2=eval(request.POST.get('num2'))
            opr=request.POST.get('opr')
            if opr=="+":
                c=n1+n2
            elif opr=="-":
                c=n1-n2
            elif opr=="*":
                c=n1*n2
            elif opr=="/":
                c=n1/n1
    except:
        c="Invalid opr ....."
    print(c)
    return render(request,"calculator.html",{'c':c})

def userform(request):
    finalans=0
    fu=usersform()
    data={'form':fu}
    try:
        if request.method=="POST":
        # n1=int(request.GET['num1'])
        # n2=int(request.GET['num2'])
            n1=int(request.POST.get('num1'))
            n2=int(request.POST.get('num2'))
            finalans=n1+n2
            data={
                'form':fu,
                'output':finalans
            }
            url="/contant/?output={}".format(finalans)

            return HttpResponseRedirect(url)
            #return redirect(url)
    except:
        pass
    return render(request,"userform.html",data)

def generic(request):
    return render(request,"generic.html")

def elements(request):
    return render(request,"elements.html")

def employeeforms(request):
    return render(request,"forms.html")


"""Module provides callback query handlers."""

from aiogram import types
from aiogram.dispatcher import Dispatcher

from src.handlers._messages import start_style_transfer_cmd_handler
from src.states import StartStyleTransferForm

async def start_style_transfer_callback_handler(callback: types.CallbackQuery):
    """
    Branch of the "/start" command dialogue that leads to sending style and content images by the user.
    """
    await callback.message.answer("Great! We're starting!")
    await start_style_transfer_cmd_handler(callback.message)

async def postpone_style_transfer_callback_handler(callback: types.CallbackQuery):
    """
    Branch of the "/start" command dialogue that ends the conversation with the user.
    """
    msg = (
        "Ok, maybe next time... Anyway I'll be here.\n"
        "In case you change your mind, just send me this \"/transfer\"."
    )
    await callback.message.answer(msg)

async def receive_chosen_style_callback_handler(callback: types.CallbackQuery):
    """
    Receive a chosen style from the user.
    """
    if callback.data != "style_custom":
        await StartStyleTransferForm.send_content_images.set()
        async with Dispatcher.get_current().current_state().proxy() as data:
            data["style"] = callback.data
            data["style_image"] = None

        await callback.message.answer(
            f"Please send me up to {StartStyleTransferForm.max_images} images that you would like to transform.\n"
            "Please make sure that it will be a single image or a gallery, "
            "otherwise only the first image will be processed."
        )

    else:
        await StartStyleTransferForm.next()
        async with Dispatcher.get_current().current_state().proxy() as data:
            data["style"] = callback.data

        await callback.message.answer(
            "Please send me a single image with the style you would like to transfer. "
            "For better results use a square image."
        )

def register_callback_query_handlers(dp: Dispatcher) -> None:
    """
    Register callback query handlers to be used by the application.
    """
    dp.register_callback_query_handler(
        start_style_transfer_callback_handler,
        text="start_style_transfer"
    )
    dp.register_callback_query_handler(
        postpone_style_transfer_callback_handler,
        text="postpone_style_transfer"
    )
    dp.register_callback_query_handler(
        receive_chosen_style_callback_handler,
        state=StartStyleTransferForm.choose_style
    )