import cv2 as cv

img = cv.imread('Images/1.jpg')
cv.imshow('image', img)

video = cv.VideoCapture(0)
while True:
    check, frame = video.read()
    cv.imshow('video', frame)
    key = cv.waitKey(1)
    if key == ord('q'):
        break
video.release()
cv.destroyAllWindows()
cv.waitKey(0)


from django.shortcuts import redirect, render, get_object_or_404
from .models import Post
from profiles.models import School,User
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, HttpResponse
import json
# Create your views here.

@login_required()
def school_feed(request, slug):
	context = {}
	user = request.user
	if user.is_student:
		context['student'] = user
	school = School.objects.filter(user__slug = slug)[0]
	posts = Post.objects.filter(school = school)
	context['posts'] = posts
	context['school'] = school
	context['user'] = user
	if request.method == 'POST':
		if 'image' in request.FILES:
			im = request.FILES['image']
		else:
			im = None
		title = request.POST.get('title')
		content = request.POST.get('content')
		post = Post.objects.create(title=title.strip(),content=content.strip(),school=request.user.school,image=im)
		post.save()
		return render(request, 'feed/school_feed.html',context)

	return render(request, 'feed/school_feed.html',context)

def delete_feed_post(request, school, slug):
	post = Post.objects.filter(slug = slug)[0]
	print(post)
	if post.school == request.user.school:
		print('ayy')
		post.delete()
	return redirect('school-feed', school)

def upvote_ajax(request):
	post = get_object_or_404(Post, slug=request.POST.get('post_slug'))
	print(post)
	up_status = ''
	if post.upvote.filter(id=request.user.id).exists():
		post.upvote.remove(request.user)
		up_status = 'disliked'
	else:
		post.upvote.add(request.user)
		up_status = 'liked'
	up_count = post.number_of_upvotes()
	print(up_count)
	ctx = {'up_status':up_status,'up_count':up_count,'post_slug':post.slug}
	return HttpResponse(json.dumps(ctx), content_type='application/json')


import requests
import sys

from bs4 import BeautifulSoup
import urllib
import urllib.request
from urllib.parse import urlparse
from urllib.parse import urljoin
from urllib.request import urlretrieve

from os import makedirs
import os.path, time, re

savepath_root = "./root/"

def get_soup(path):
    #time.sleep(1)
    #rr = requests.get(url)
    #html = rr.content
    with open(path, 'r', encoding="UTF-8") as f:
        html = f.read()
    soup = BeautifulSoup(html, "lxml")

    return soup, html

def get_html_links(soup):
    s = soup.find("box-newEntry")
    if s is not None:
        soup = s.decompose()
    link_list = [all_a.get("href") for all_a in soup.find_all("a")]
    return link_list

def get_content_links(soup):
    link_list = []
    for all_img in soup.find_all("img"):
        link = all_img.get("src")
        if not link is None:
            link_list.append(link)

    return link_list

ignore_query_list = [
        "ima",
        "dy",
        ]
def to_localpath(url, root = "./"):
    parse = urlparse(url)
    sp_path = os.path.splitext(parse.path)
    query = '&'.join([q for q in parse.query.split('&') if not q.split('=')[0] in ignore_query_list])
    query = '&' + query if not query == '' else query
    return root + parse.netloc + sp_path[0] + query + sp_path[1]

def to_absolute_url(url, baseurl):
    return urljoin(baseurl, url)

def save_contents(url, path, sleeptime = 1):
    print(url)
    time.sleep(sleeptime)

    prepare_dir(path)

    rr = requests.get(url)
    with open(path, 'wb') as f:
        f.write(rr.content)

    #urlretrieve(url, path)

def prepare_dir(path):
    savedir = os.path.dirname(path)
    if not os.path.exists(savedir):
        makedirs(savedir)

def is_external_site(url, baseurl):
    return not urlparse(url).netloc == urlparse(baseurl).netloc

search_list = [
        "http://www\.keyakizaka46.com/s/k46o/search/",
        "http://www\.keyakizaka46.com/s/k46o/diary/member/.*&ct=19",
        #"http://www\.keyakizaka46.com/s/k46o/diary/member/.*&ct=1[09]",
        "http://www\.keyakizaka46.com/s/k46o/diary/detail/",
        "http://www\.keyakizaka46.com/s/k46o/artist/19",
        #"http://www\.keyakizaka46.com/s/k46o/artist/1[09]",
        ]
def is_search_url(url):
    for search_url in search_list:
        #if urlparse(search_url).path in urlparse(url).path:
        if re.match(search_url, url):
            return True
    return False

exclusion_path = [
        "http://www.keyakizaka46.com/s/k46o/diary/tv",
        "http://www.keyakizaka46.com/s/k46o/artist/00/discography",
        ]
exclusion_quety = [
        "cd=report",
        "cd=event",
        ]
def is_exclusion_url(url):
    for ex_path in exclusion_path:
        if urlparse(ex_path).path in urlparse(url).path:
            return True
    for ex_query in exclusion_quety:
        if ex_query in urlparse(url).query:
            return True
    return False

def replace_link2localpath(html, link, localpath_dist, localpath_src):
    relpath_dist = os.path.relpath(localpath_dist, os.path.dirname(localpath_src))
    return html.replace(link, relpath_dist)

def save_replaced_html(html, localpath):
    if os.path.splitext(localpath)[1] == '':
        localpath = localpath + ".html"
    prepare_dir(localpath)
    with open(localpath, "w", encoding = "UTF-8") as f:
        f.write(html)

    return localpath

exclusion_path_list = set()
def download(url, baseurl):
    url = to_absolute_url(url, baseurl)
    if is_external_site(url, baseurl):return
    if not is_search_url(url) or is_exclusion_url(url):
        exclusion_path_list.add(urlparse(url).path)
        return

    html_src_path = to_localpath(url, savepath_root + "src/")
    if os.path.exists(html_src_path):return html_src_path
    html_local_path = to_localpath(url, savepath_root + "local/")

    save_contents(url, html_src_path, sleeptime = 1)
    soup, html = get_soup(html_src_path)

    content_links = get_content_links(soup)
    for link in content_links:
        localpath = to_localpath(link, savepath_root + "src/")
        if not os.path.exists(localpath):
            save_contents(link, localpath)

        html = replace_link2localpath(html, link, localpath, html_local_path)

    html_links = get_html_links(soup.body)
    for link in html_links:
        localpath_link_dist = download(link, baseurl)
        if localpath_link_dist is not None:
            html = replace_link2localpath(html, link, localpath_link_dist, html_src_path)

    link_replaced_path = save_replaced_html(html, html_local_path)
    return link_replaced_path

if __name__ == "__main__":
    url = "http://www.keyakizaka46.com/s/k46o/search/artist"
    url2 = "http://www.keyakizaka48.com/s/k46o/search/artist"
    url3 = "http://www.keyakizaka46.com/s/k46o/page/newmember?ima=0000#member-43"
    url4 = "http://www.keyakizaka46.com/s/k46o/diary/member/list?ima=0000&ct=02"
    url5 = "http://www.keyakizaka46.com/s/k46o/diary/member/list?ima=0000&page=2&cd=member&ct=02"
    url6 = "http://www.keyakizaka46.com/s/k46o/diary/member/list?ima=0000&ct=07"
    url7 = "http://www.keyakizaka46.com/s/k46o/diary/member/list?ima=0000&ct=19"

    #print(urljoin(url, "https://www.keyakizaka48.com/member/02"))
    #extract_link(url)
    #print(is_external_site(url, url2))
    #print(is_search_url(url3))
    #print(to_localpath(url6))

    #print(is_search_url(url))

    download(url, url)
    [print(url) for url in exclusion_path_list]


from typing import Any

import scrapy
from scrapy import Selector
from trialing.items import HospitalItem

class TrialingSpider(scrapy.Spider):
    name = "trialing_talent_spider"
    allowed_domains = ["trialing-talent.s3-website-eu-west-1.amazonaws.com"]
    start_urls = ["http://trialing-talent.s3-website-eu-west-1.amazonaws.com/"]

    def parse(self, response, **kwargs: Any):
        # Predefined DTO
        item = HospitalItem()
        # .card - main selector of each card on HTML page
        for card in response.css('.card'):
            # Extract the ID
            item['hospital_card_id'] = card.xpath('@id').get()
            # Extract the name
            item['name'] = card.css('h5::text').get()

            # Extract the address, ensuring it starts with 'Address:'
            address_paragraph_html = card.css('.card-body p:contains("Address:")').get()
            address_paragraph = Selector(text=address_paragraph_html)
            address_data = " ".join(address_paragraph.css('::text').extract()).replace('Address:', '').strip()

            # Extract the Google Maps link and parse latitude and longitude
            maps_link = card.css('.card-body a::attr(href)').get()
            item['latitude'], item['longitude'] = maps_link.split('@')[1].split(',')[0:2]

            # Split the address to get country, region, and city
            address_parts = address_data.split(' | ')
            item['address'] = address_parts[0]
            item['region'], item['country'] = address_parts[-1].split(',')[0], address_parts[-1].split(',')[1].strip()
            item['city'] = address_parts[-2]

            # Extract the contact information if available
            phone = card.css('p strong::text').re(r'Phone:\s*(.*)')
            item['contact_data'] = phone[0] if phone else None

            yield item


from pprint import pprint
from pathlib import Path

from loguru import logger as log
from funcy import lflatten

from .contest import solve

def load(data):
    tokens = lflatten([d.split(" ") for d in data])
    tokens.pop(0)

    return dict(tokens=tokens)

if __name__ == "__main__":
    level, quests = 4, 5
    for quest in range(quests + 1):
        if quest == 0:
            quest = "example"

        base_path = Path("data")
        input_file = base_path / f"level{level}" / f"level{level}_{quest}.in"
        output_file = input_file.with_suffix(".out")

        if not input_file.exists():
            log.warning(f"file not found, skip: {input_file}")
            continue

        with open(input_file, "r") as fi:
            data = load(fi.read().splitlines())
            # pprint(data)

            log.info(f"evaluating {input_file}")

            result = solve(data)
            # pprint(result)

            with open(output_file, "w+") as fo:
                fo.write(result)


import os
import argparse
import pandas as pd
from astropy.io import fits
from astropy.table import Table
from astropy.coordinates import SkyCoord
from astropy.coordinates import FK5
from astropy import wcs

"""
Script that takes an object detection catalog and converts it to an LDAC catalog.
"""

def prepare_input_catalog(data):
    # Data should be an astropy Table
    # COMPUTE PIXEL AND WCS PARAMETERS
    coordlist = SkyCoord(data["RA"], data["DEC"], unit="deg", frame=FK5)
    # UPDATE THIS LATER
    # It literally does not matter what the template img is,
    # as long as it has a working WCS and you are consistent
    # when u use it later to convert back from the calibrated scamp output
    with fits.open("templates/template_img.fits") as hdul:
        header = hdul[0].header
    wcs_obj = wcs.WCS(header=header)
    out = wcs.utils.skycoord_to_pixel(coordlist, wcs_obj)

    # ADD NECESSARY COLUMNS
    data["ALPHA_J2000"] = coordlist.ra.value
    data["DELTA_J2000"] = coordlist.dec.value
    data["XWIN_IMAGE"] = out[0]
    data["YWIN_IMAGE"] = out[1]
    # Choosing some arbitrary parameters here...
    data["FLUX_AUTO"] = 10 ** ((data["MAG"] - 22.5) / (-2.5))
    data["FLUXERR_AUTO"] = data["FLUX_AUTO"] * 0.05

    return data

def update_template_values(template, data):
    # UPDATE VALUES IN THE TABLE
    template["Y_IMAGE"] = list(data["YWIN_IMAGE"])
    template["X_IMAGE"] = list(data["XWIN_IMAGE"])
    template["X_WORLD"] = list(data["XWIN_IMAGE"])
    template["Y_WORLD"] = list(data["YWIN_IMAGE"])
    template["XWIN_IMAGE"] = list(data["XWIN_IMAGE"])
    template["YWIN_IMAGE"] = list(data["YWIN_IMAGE"])
    template["ALPHA_J2000"] = list(data["ALPHA_J2000"])
    template["DELTA_J2000"] = list(data["DELTA_J2000"])
    # Choosing some arbitrary parameters here...
    template["ERRAWIN_IMAGE"] = 0.01
    template["ERRBWIN_IMAGE"] = 0.01
    template["ERRA_WORLD"] = 0.01
    template["ERRB_WORLD"] = 0.01
    template["ERRTHETAWIN_IMAGE"] = 25
    template["FLUX_AUTO"] = list(data["FLUX_AUTO"])
    template["FLUXERR_AUTO"] = list(data["FLUXERR_AUTO"])
    template["FLAGS"] = 0
    template["FLAGS_WEIGHT"] = 0

    return template

def copy_to_template(data, output_name):
    with fits.open("templates/template_catalog.fits.cat") as n:
        temp = n[2]

        if len(data) <= len(temp.data):
            template = temp.data[: len(data)]
        else:
            print("Input dataframe too long; functionality not implemented yet")

        # NOW SAVE THE FILE
        temp.data = update_template_values(template, data)
        output_fits = fits.HDUList([n[0], n[1], temp])
        output_fits.writeto(f"{output_name}", overwrite=True)

def argument_parser():
    # PARSES COMMAND LINE ARGUMENTS TO SCRIPT
    result = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    # Where is the list of catalogs to convert to LDAC?
    result.add_argument(
        "-c",
        dest="cat_list",
        type=str,
        default="config_txt_files/catalog_from_catalog.txt",
    )
    # Where should we place the extracted catalogs?
    result.add_argument("-d", dest="output_dir", type=str, default=None)

    result.add_argument("-m", dest="multithread", type=bool, default=False)
    result.add_argument("-v", dest="verbose", type=bool, default=False)

    return result

def setup_environment(cats, output_dir):
    # LOADS VARIABLES, INITIALIZES INPUT VALUES
    directory = os.getcwd()
    # Load the image list
    cat_list = pd.read_csv(f"{directory}/{cats}")
    if len(cat_list) == 0:
        raise ValueError("No fits files found in image list file.")
    # Set the output directory
    outputs = directory if args.output_dir is None else f"{directory}/{output_dir}"
    return directory, cat_list, outputs

if __name__ == "__main__":
    args = argument_parser().parse_args()
    directory, cat_list, outputs = setup_environment(args.cat_list, args.output_dir)
    for i in range(len(cat_list)):
        if args.verbose:
            print(
                f'---------------\nConverting {cat_list.iloc[i]["catalog"]} to LDAC\n---------------'
            )
        with fits.open(cat_list.iloc[i]["catalog"]) as hdul:
            datatable = Table(hdul[2].data)
        data = prepare_input_catalog(datatable)
        copy_to_template(
            data,
            cat_list.iloc[i]["output_name"],
        )


'''
Author: lmio 2091319361@qq.com
Date: 2023-07-08 10:20:31
LastEditors: lmio 2091319361@qq.com
Description: 25. K 个一组翻转链表
'''

from typing import Optional

from utils.node import ListNode

def reverseKGroup(head: Optional[ListNode], k: int) -> Optional[ListNode]:
    def reverse(node: ListNode, k: int) -> ListNode:
        prev = node
        if not node.next:
            return None
        cur = prev.next
        # 不断把next插入到prev后面
        i = 1
        while i < k:
            if not cur.next:
                return reverse(prev, i)
            next = cur.next
            cur.next = next.next
            next.next = prev.next
            prev.next = next
            i += 1
        return cur
    node = ListNode()
    node.next = head
    dummy = node
    while node:
        node = reverse(node, k)
    return dummy.next


from flask import Flask, render_template,request, redirect , session
app = Flask(__name__)
app.secret_key = " This's highly calssified"
@app.route('/')
def index():
    return render_template("index.html")

@app.route('/results', methods=['POST'])
def create_user():
    print(request.form)
    session['Your_Name'] = request.form['Your_Name']
    session['Dojo_Loc'] = request.form['Dojo_Loc']
    session['Fav_Lan'] = request.form['Fav_Lan']
    session['comment']= request.form['text_box']
    return redirect('/show')

@app.route('/show')
def show_form():
    print(request.form)
    return render_template("show.html")

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


import io
import base64
import json
import math
from trace import Trace

import numpy as np
import cv2

def points_to_lines(points):
    res = []
    for i in range(len(points) - 1):
        res.append((points[i], points[i + 1]))
    res.append((points[-1], points[0]))
    return res

def get_distance_point(p1, p2):
    x = int(abs(p1[0] + p2[0])/2)
    y = int(abs(p1[1] + p2[1])/2)

    return [x, y]

def get_distance(p1, p2):
    x = p1[0] - p2[0]
    y = p1[1] - p2[1]

    return int(math.sqrt((x * x) + (y * y)))

def get_point(label):
    with open("point.json", "r") as st_json:
        st_python = json.load(st_json)
        points = st_python[label]

    return points

def load_image(file_name):
    image = cv2.imread(file_name)
    return image

def save_image(image, file_name, ext="png"):
    cv2.imwrite(file_name + "." + ext, image)
    return

def base64_to_image(data):
    data = data.encode()
    image_data = base64.decodestring(data)

    image_stream = io.BytesIO()
    image_stream.write(image_data)
    image_stream.seek(0)

    file_bytes = np.asarray(bytearray(image_stream.read()), dtype=np.uint8)
    image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)

    return image

def image_to_base64(image):
    retval, temp = cv2.imencode(".jpg", image)
    data = base64.b64encode(temp)
    return data

def crop_image(image, points, bg_value="black"):
    """
    points = [ [x1, y1], [x2, y2], ... , [xn, yn] ]
    """
    ## (1) Crop the bounding rect

    points = np.array(points, dtype=np.int)

    rect = cv2.boundingRect(points)
    x, y, w, h = rect
    croped = image[y : y + h, x : x + w].copy()

    ## (2) make mask
    points = points - points.min(axis=0)

    mask = np.zeros(croped.shape[:2], np.uint8)
    cv2.drawContours(mask, [points], -1, (255, 255, 255), -1)

    ## (3) do bit-op
    dst = cv2.bitwise_and(croped, croped, mask=mask)

    ## (4) add the white background
    if bg_value == "black":
        bg = np.zeros_like(croped, np.uint8) # black
    elif bg_value == "white":
        bg = np.ones_like(croped, np.uint8) * 255  # withe
    else:
        bg = np.ones_like(croped, np.uint8) * bg_value

    cv2.bitwise_not(bg, bg, mask=mask)
    dst2 = bg + dst

    return dst2

def color_balancing(image, points):
    ret = image.copy()

    _, _, mean_r = get_mean_from_masked_image(image[:, :, 2], points)
    _, _, mean_g = get_mean_from_masked_image(image[:, :, 1], points)
    _, _, mean_b = get_mean_from_masked_image(image[:, :, 0], points)

    mVr = 1 / mean_r
    mVg = 1 / mean_g
    mVb = 1 / mean_b

    M = max(mVr, mVg, mVb)
    sr = mVr / M
    sg = mVg / M
    sb = mVb / M

    ret[:, :, 2] = image[:, :, 2] * sr
    ret[:, :, 1] = image[:, :, 1] * sg
    ret[:, :, 0] = image[:, :, 0] * sb

    return ret

def estimation_of_AC(alpha, max_value):
    mA = alpha / max_value

    # ret, binary_image = cv2.threshold(mA, 0.18, 255, cv2.THRESH_BINARY_INV)
    # ret, binary_image = cv2.threshold(mA, 0.17, 255, cv2.THRESH_BINARY)
    ret, binary_image = cv2.threshold(mA, 0.8, 255, cv2.THRESH_BINARY)
    return binary_image, mA

def get_mean_from_masked_image(image, points):

    points = np.array(points, dtype=np.int)

    rect = cv2.boundingRect(points)
    x, y, w, h = rect
    croped = image[y : y + h, x : x + w].copy()

    ## (2) make mask
    points = points - points.min(axis=0)

    mask = np.zeros(croped.shape, np.uint8)
    # print("=====================")
    # print(image.shape)
    # print(points.shape)
    # print(croped.shape)
    # print(mask.shape)

    cv2.drawContours(mask, [points], -1, 255, -1)

    # temp = croped[:, :, 1]
    min_value, max_value, min_loc, max_loc = cv2.minMaxLoc(croped, mask=mask)
    mean_value = cv2.mean(croped, mask=mask)

    return min_value, max_value, mean_value[0]

def morphology(image, k_size=3):

    # Erosion
    k = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))
    erosion = cv2.erode(image, k)

    # Dilation
    kernel = np.ones((k_size, k_size), np.uint8)
    result = cv2.dilate(erosion, kernel, iterations=1)

    return result


# -*- coding: utf-8 -*-
'''
@author:    Wengang.Zheng
@email:     zwg0606@gmail.com
@fielName:  单词拆分.py
@Time:      2023-11-05-18:04:44
@Des:       单词拆分
'''

from typing import List, Dict

class Solution1:
    """
    @brief      单词拆分一

    @details    https://leetcode.cn/problems/word-break/
    """
    def __init__(self):
        self.worddict = {}  # 字典
        self.mem = []  # 备忘录

    def word_break1(self, worddict: List[str], s: str):
        """
        @brief      单词拆分一

        @details    https://leetcode.cn/problems/word-break/
        """

        self.worddict = dict(worddict)
        # 备忘录初始化
        self.mem = [-1 for _ in range(len(s))]
        result = self.dp(s, 0)
        return result

    def word_break2(self, worddict: List[str], s: str):
        """
        @brief      单词拆分二

        @details    https://leetcode.cn/problems/word-break-ii/
        """
        self.worddict = dict(worddict)
        # 备忘录初始化
        self.mem = [-1 for _ in range(len(s))]
        result = self.dp2(s, 0)
        return result

    def dp2(self, target, i):
        res = []
        length = len(target)
        if i == length:
            res.append("")
            return res

        if self.mem[i] != -1:
            return self.mem[i]

        for length in range(1, length + 1 - i):
            word_prefix = target[i:i+length-1]
            if word_prefix in self.worddict:
                sub_res = dp(target, i+length, res)
                # 需要遍历得到所有结果
                for sub in sub_res:
                    if sub == "":
                        res.append(prefix)
                    else:
                        res.append(prefix + " " + sub)

        self.mem[i] = res
        return res

    def dp(self, target, i):
        length = len(target)
        if i == length:
            return True

        if self.mem[i] != -1:
            return self.mem[i]

        for length in range(1, length + 1 - i):
            word_prefix = target[i:i+length-1]
            if word_prefix in self.worddict:
                res = dp(target, i+length)
                if res:
                    self.mem[i] = True
                    return True
        self.mem[i] = False
        return False


"""Projector-Camera with simple gray code
"""
import os
import time
import numpy as np
import cv2
import structuredlight as sl # https://github.com/elerac/structuredlight
from fullscreen import FullScreen

def main():
    # Output directory names
    OUTPUT_DIRNAME_BASE = "procam_capture"
    OUTPUT_DIRNAME_CODE = f"{OUTPUT_DIRNAME_BASE}/code"
    OUTPUT_DIRNAME_OBSERVE = f"{OUTPUT_DIRNAME_BASE}/observe"
    os.makedirs(OUTPUT_DIRNAME_CODE, exist_ok=True)
    os.makedirs(OUTPUT_DIRNAME_OBSERVE, exist_ok=True)

    # Camera setup (use your USB camera)
    CAMERA_INDEX = 0
    camera = cv2.VideoCapture(CAMERA_INDEX)

    # Projector setup
    projector = FullScreen()
    height, width = projector.shape[:2]

    # Generate gray code
    gray = sl.Gray()
    imlist_code = gray.generate((width, height))

    # Projection and Capture
    imlist_observe = []
    for i, img_code in enumerate(imlist_code):
        # Projection
        projector.imshow(img_code)
        time.sleep(0.2)

        # Capture
        ret, img_observe_bgr = camera.read()
        img_observe = cv2.cvtColor(img_observe_bgr, cv2.COLOR_BGR2GRAY)
        imlist_observe.append(img_observe)

        # Save images
        cv2.imwrite(f"{OUTPUT_DIRNAME_CODE}/code-{i+1}.png", img_code)
        cv2.imwrite(f"{OUTPUT_DIRNAME_OBSERVE}/observe-{i+1}.png", img_observe)

    # Decode gray code
    img_index = gray.decode(imlist_observe, thresh=127)

    # Save decoded result
    img_index_u8 = np.clip(img_index / width * 255.0, 0, 255).astype(np.uint8)
    cv2.imwrite(f"{OUTPUT_DIRNAME_BASE}/index.png", img_index_u8)

if __name__=="__main__":
    main()


from django.urls import path
from .views import *

urlpatterns = [
    path('', root),
    path('mascots/', mascots_list, name="mascots"),
    path('mascots/<str:mascotas_id>/delete', mascots_delete, name="mascots-delete"),
    path('mascots/detail/<str:mascotas_id>', mascots_detail, name="mascots-detail"),
    path('mascots/edit/<str:mascotas_id>', mascots_update, name="mascots-edit"),
    path('mascots/new/', mascots_new, name="mascots-new"),
    path('mascots/mascotType/<mascotType>', mascots_by_mascotType, name="mascots-mascotType"),
    path('mascots/gender/<gender>', mascots_by_gender, name="mascotas-gender"),
    path('contact/', contact, name='contact'),
    path('mascotsType/', mascotsType_list, name="mascotasType"),
    path('mascotsType/<str:mascotasTipo_id>/delete', mascotsType_delete, name="mascotasType-delete"),
    path('mascotsType/detail/<str:mascotasTipo_id>', mascotsType_detail, name="mascotasType-detail"),
    path('mascotsType/edit/<str:mascotasTipo_id>', mascotsType_update, name="mascotasType-edit"),
    path('mascotsType/new/', mascotsType_new, name="mascotasType-new"),
    path('gender/', gender_list, name="genero"),
    path('gender/<str:genero_id>/delete', gender_delete, name="genero-delete"),
    path('gender/detail/<str:genero_id>', gender_detail, name="genero-detail"),
    path('gender/edit/<str:genero_id>', gender_update, name="genero-edit"),
    path('gender/new/', gender_new, name="genero-new"),
]


"""say_hello_module.py的测试用例"""

from json import load, dump
import unittest
import os

from mod.say_hello_module import User

class TestUser(unittest.TestCase):
    """测试User类"""
    def setUp(self):
        """实例化User类"""
        self.user = User('test.json')

    def test_add_user(self):
        """测试添加用户存档功能"""
        with open('test.json','w') as file_object:
            dump('',file_object)
        self.assertTrue(self.user.add_user('test'))
        with open('test.json','r') as file_object:
            user_name = load(file_object)
        self.assertEqual('test',user_name)
        self.assertFalse(self.user.add_user('test2'))

    def test_del_user(self):
        """测试删除用户存档功能"""
        with open('test.json','w') as file_object:
            dump('',file_object)
        self.assertFalse(self.user.del_user())
        self.user.add_user('test')
        self.assertTrue(self.user.del_user())

    def test_who(self):
        """测试读取用户存档功能"""
        with open('test.json','w') as file_object:
            dump('',file_object)
        self.assertEqual(None,self.user.who())
        self.user.add_user('test')
        self.assertEqual('test',self.user.who())

if __name__ == '__main__':
    unittest.main()


# Databricks notebook source
from __future__ import print_function
from pyspark.sql import SparkSession
from pyspark.sql import Row
from pyspark.sql.types import *
from pyspark import SparkContext
from pyspark.sql import SQLContext, functions

# sc.stop()
sc=SparkContext.getOrCreate()
sqlContext = SQLContext(sc)

df = sqlContext.read.json("/FileStore/tables/tweets.json")
df.show()

# COMMAND ----------

df.printSchema()

# COMMAND ----------

df.select("country").show()

# COMMAND ----------

df.select(df['country']).where(df.sentiment == "negative").show()
df.select(df['country']).where(df.sentiment == "neutral").show()
df.select(df['country']).where(df.sentiment == "positive").show()

# COMMAND ----------

df.groupBy("sentiment").count().show()

# COMMAND ----------

df.groupBy("country").count().show()
df.groupBy("state").count().show()

# COMMAND ----------

df.createOrReplaceTempView("tweet")

sqlDF = spark.sql("SELECT country as Country, sentiment FROM tweet")
spark.sql("SELECT country as Country FROM tweet GROUP BY country").show()

sqlDF.show()

# COMMAND ----------

#show sentiment for countries
df.groupBy("country").agg(functions.count('sentiment')).show()

#show sentiment for state
df.groupBy("state").agg(functions.count('sentiment')).show()

df.groupBy("country", "state").agg(functions.count('sentiment')).show()

# COMMAND ----------

#Trump
df.groupBy("tweet").agg(functions.count('tweet')).filter(df.tweet.contains("Trump")).show()

df.groupBy("tweet").agg(functions.count('tweet')).filter(df.tweet.contains("fakenews")).show()


import os
import tornado.ioloop
import tornado.web

class MyFileHandler(tornado.web.StaticFileHandler):
    def initialize(self, path):
        self.dirname, self.filename = os.path.split(path)
        super(MyFileHandler, self).initialize(self.dirname)
    def get(self, path = None, include_body = True):
        self.set_header('Content-Type', 'text/javascript')
        super(MyFileHandler, self).get(self.filename, include_body)

app = tornado.web.Application([
    (r'/final\.json', MyFileHandler, {'path': './data/worldCupFinals.json'}),
    (r'/winner\.json', MyFileHandler, {'path': './data/worldCupMatches.json'})
])

app.listen(8888)
tornado.ioloop.IOLoop.current().start()


from __future__ import absolute_import

import os
import re
import sys

from baron.utils import python_version

import redbaron

if python_version == 3:
    from io import StringIO
else:
    from StringIO import StringIO

def baron_type_to_redbaron_classname(baron_type):
    return "".join(map(lambda x: x.capitalize(), baron_type.split("_"))) + "Node"

def redbaron_classname_to_baron_type(name):
    name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name.replace("Node", ""))
    return re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()

def log(string, *args):
    if redbaron.DEBUG:
        sys.stdout.write("%s\n" % (string % args))

def in_a_shell():
    # the isinstance here is for building sphinx doc
    if redbaron.DEBUG or isinstance(sys.stdout, StringIO):
        return True
    try:
        if hasattr(sys.stdout, 'fileno') and os.isatty(sys.stdout.fileno()):
            return True
    except Exception:
        # someone is doing strange things with stdout (eg: bpython or ipython notebook)
        return False

    return False

def indent(block_of_text, indentation):
    """
    Helper function to indent a block of text.

    Take a block of text, an indentation string and return the indented block.
    """
    return "\n".join(map(lambda x: indentation + x, block_of_text.split("\n")))

def truncate(text, n):
    if n < 5 or len(text) <= n:
        return text

    truncated = list(text)
    truncated[-3:-1] = ['.', '.', '.']
    del truncated[n-4 : -4]
    return "".join(truncated)


"""
文件conv.py实现的卷积是我们直观理解的卷积，虽然用for循环实现的卷积容易理解，但是速度低下（PS:你要是这么写，
别人肯定会认为你是个low逼）。numpy可以快速实现矩阵运算，所以在实现卷积过程中应尽量避免使用for循环，而是通过
矩阵运算实现。
"""
import numpy as np
from functools import reduce
from layers.parameter import parameter

class conv():
    def __init__(self, filter_shape, stride=1, padding='SAME', bias=True, requires_grad=True):
        """
        :param filter_shape:元组（O, C, K, K）
        :param stride: 步长
        :param padding: 填充方式:{"SAME", "VALID"}
        :param bias:是否有偏置
        :param requires_grad:是否计算梯度
        """
        self.weight = parameter(np.random.randn(*filter_shape) * (2/reduce(lambda x,y:x*y, filter_shape[1:]))**0.5)  #kaiming初始化
        self.stride = stride
        self.padding = padding
        self.requires_grad = requires_grad
        self.output_channel = filter_shape[0]   # 输出通道数
        self.input_channel = filter_shape[1]    # 输入通道数
        self.filter_size = filter_shape[2]  # 卷积核大小
        if bias:
            self.bias = parameter(np.random.randn(self.output_channel))
        else:
            self.bias =None

    def forward(self, input):
        """
        :param input:feature map 形状：[N,C,H,W]
        :return:
        """
        # 第一步边缘填充
        if self.padding == "VALID":
            self.x = input
        if self.padding == "SAME":
            p = self.filter_size // 2
            self.x = np.lib.pad(input, ((0,0),(0,0),(p,p),(p,p)), "constant")
        # 第二步处理输入的宽高不能恰好的被卷积核的大小和选定的步长所整除
        x_fit = (self.x.shape[2] - self.filter_size) % self.stride
        y_fit = (self.x.shape[3] - self.filter_size) % self.stride

        if self.stride > 1:
            if x_fit != 0:
                self.x = self.x[:, :, 0:self.x.shape[2] - x_fit, :]
            if y_fit != 0:
                self.x = self.x[:, :, :, 0:self.x.shape[3] - y_fit]

        # 实现卷积
        N, _, H, W = self.x.shape
        O, C, K, K = self.weight.data.shape
        weight_cols = self.weight.data.reshape(O, -1).T
        x_cols = self.img2col(self.x, self.filter_size, self.filter_size, self.stride)
        result = np.dot(x_cols, weight_cols) + self.bias.data
        output_H, output_W = (H-self.filter_size)//self.stride + 1, (W-self.filter_size)//self.stride + 1
        result = result.reshape((N, result.shape[0]//N, -1)).reshape((N, output_H, output_W, O))
        return result.transpose((0, 3, 1, 2))

    def backward(self, eta, lr):
        """
        :param eta:上一层返回的梯度[N,O,output_H, output_W]
        param lr:学习率
        :return:
        """
        # 在eta行行列列之间插入插入0后，计算W,b的梯度；然后进行padding， padding后计算返回上一层的梯度

        # 第一步步长大于1要在行行和列列之间插0
        if self.stride > 1:
            N, O, output_H, output_W = eta.shape
            inserted_H, inserted_W = output_H + (output_H-1)*(self.stride-1), output_W + (output_W-1)*(self.stride-1)
            inserted_eta = np.zeros((N, O, inserted_H, inserted_W))
            inserted_eta[:,:,::self.stride, ::self.stride] = eta
            eta = inserted_eta

        # 计算本层的W,b的梯度
        N, _, output_H, output_W = eta.shape
        self.b_grad = eta.sum(axis=(0,2,3))
        self.W_grad = np.zeros(self.weight.data.shape)      # 形状[O, C, K, K]
        for i in range(self.filter_size):
            for j in range(self.filter_size):
                self.W_grad[:,:,i,j] = np.tensordot(eta, self.x[:,:,i:i+output_H,j:j+output_W], ([0,2,3], [0,2,3]))
        # 权重更新
        self.weight.data -= lr * self.W_grad / N
        self.bias.data -= lr * self.b_grad / N

        # 第二步边缘填充
        if self.padding == "VALID":
            p = self.filter_size - 1
            pad_eta = np.lib.pad(eta, ((0,0),(0,0),(p,p),(p,p)), "constant", constant_values=0)
            eta = pad_eta
        elif self.padding == "SAME":
            p = self.filter_size // 2
            pad_eta = np.lib.pad(eta, ((0, 0), (0, 0), (p, p), (p, p)), "constant", constant_values=0)
            eta = pad_eta

        # 计算传到上一层的梯度
        _, C, _, _ = self.weight.data.shape
        weight_flip = np.flip(self.weight.data, (2,3))  # 卷积核旋转180度
        weight_flip_swap = np.swapaxes(weight_flip, 0, 1)  # 交换输入、输出通道的顺序[C,O,H,W]
        weight_flip = weight_flip_swap.reshape(C, -1).T
        x_cols = self.img2col(eta, self.filter_size, self.filter_size, self.stride)
        result = np.dot(x_cols, weight_flip)
        N, _, H, W = eta.shape
        output_H, output_W = (H - self.filter_size) // self.stride + 1, (W - self.filter_size) // self.stride + 1
        result = result.reshape((N, result.shape[0] // N, -1)).reshape((N, output_H, output_W, C))
        self.weight.grad = result.transpose((0, 3, 1, 2))

        return self.weight.grad

    def img2col(self, x, filter_size_x, filter_size_y, stride):
        """
        现代计算机运算中矩阵运算已经极为成熟（无论是速度还是内存），因此思路是将x中的每个卷积单位[N,C,K,K]展开为行向量，然后与组成二维矩阵
        与展开的权重进行矩阵乘法。最后把结果reshape一下就可以了。
        缺点：虽然提升了速度，但是增大的内存开销（因为x展开成的二维矩阵，存在大量重复元素）
        :param x:输入的feature map形状：[N,C,H,W]
        :param filter_size_x:卷积核的尺寸x
        :param filter_size_y:卷积核的尺寸y
        :param stride:卷积步长
        :return:二维矩阵 形状：[(H-filter_size+1)/stride * (W-filter_size+1)/stride*N, C * filter_size_x * filter_size_y]
        """

        N, C, H, W = x.shape
        output_H, output_W = (H-filter_size_x)//stride + 1, (W-filter_size_y)//stride + 1
        out_size = output_H * output_W
        x_cols = np.zeros((out_size*N, filter_size_x*filter_size_y*C))
        for i in range(0, H-filter_size_x+1, stride):
            i_start = i * output_W
            for j in range(0, W-filter_size_y+1, stride):
                temp = x[:,:, i:i+filter_size_x, j:j+filter_size_y].reshape(N,-1)
                x_cols[i_start+j::out_size, :] = temp
        return x_cols


from flask import Blueprint, request, jsonify, send_from_directory
from flasgger import swag_from
import logging, os, shutil
from pathlib import Path
from natsort import natsorted
from webapi import app
from .common.utils import success_msg, error_msg, exists, get_classes_list
from .common.config import ROOT, YAML_MAIN_PATH
from .common.upload_tool import create_class_dir, Upload_DB, compare_classes
from .common.display_tool import count_dataset, get_img_path_db, check_unlabeled_images
from .common.database import delete_data_table_cmd, execute_db, update_data_table_cmd
from .common.inspection import Check
from .common.init_tool import get_project_info

app_dy_dt = Blueprint( 'display_dataset', __name__)
# Define API Docs path and Blue Print
YAML_PATH       = YAML_MAIN_PATH + "/display_dataset"
chk = Check()

@app_dy_dt.route('/<uuid>/get_dataset', methods=['GET'])
@swag_from("{}/{}".format(YAML_PATH, "get_dataset.yml"))
def get_dataset(uuid):
    # Check uuid is/isnot in app.config["PROJECT_INFO"]
    if not ( uuid in app.config["PROJECT_INFO"].keys()):
        return error_msg(400, {}, "UUID:{} does not exist.".format(uuid), log=True)
    # Get project name
    prj_name = app.config["PROJECT_INFO"][uuid]["project_name"]
    # Get all dataset/iteration from project
    prj_path = ROOT + '/' +prj_name
    if not exists(prj_path):
        return error_msg(400, {}, "The project does not exist:[{}]".format(prj_name), log=True)
    folder_name = [ dir for dir in natsorted(os.listdir(prj_path)) if os.path.isdir(prj_path+"/"+dir)]
    # Arrange folder name from the index
    folder_name = [ "iteration"+str(idx+1) for idx, dir in enumerate(natsorted(folder_name)) if "iteration" in dir]
    folder_name.append("workspace")
    return success_msg(200, {"folder_name":folder_name}, "Success", "Get path of dataset from project:[{}]".format(prj_name))

@app_dy_dt.route('/<uuid>/filter_dataset', methods=['POST'])
@swag_from("{}/{}".format(YAML_PATH, "filter_dataset.yml"))
def filter_dataset(uuid):
    # Check uuid is/isnot in app.config["PROJECT_INFO"]
    if not ( uuid in app.config["PROJECT_INFO"].keys()):
        return error_msg(400, {}, "UUID:{} does not exist.".format(uuid), log=True)
    # Check key of front
    if not "iteration" in request.get_json().keys():
        return error_msg(400, {}, "KEY:iteration does not exist.", log=True)
    elif not "class_name" in request.get_json().keys():
        return error_msg(400, {}, "KEY:class_name does not exist.", log=True)
    # Get project name
    prj_name = app.config["PROJECT_INFO"][uuid]["project_name"]
    # Get value of front
    iteration = request.get_json()['iteration']
    class_name = request.get_json()['class_name']
    # Mapping iteration
    if iteration != "workspace":
        iteration = chk.mapping_iteration(uuid, prj_name, iteration, front=True)
        if "error" in iteration:
            return error_msg(400, {}, str(iteration[1]), log=True)
    # Get img path
    all_img_path_db = get_img_path_db(uuid, prj_name, iteration, class_name, front=False)
    # Prevent error
    if "error" in all_img_path_db:
        return error_msg(400, {}, str(all_img_path_db[1]))
    return success_msg(200, all_img_path_db, "Success", "Get the image path of this class in the project:[{}:{}]".format(prj_name, class_name))

@app_dy_dt.route('/<uuid>/display_url', methods=['POST'])
@swag_from("{}/{}".format(YAML_PATH, "display_url.yml"))
def display_url(uuid):
    # Check uuid is/isnot in app.config["PROJECT_INFO"]
    if not ( uuid in app.config["PROJECT_INFO"].keys()):
        return error_msg(400, {}, "UUID:{} does not exist.".format(uuid), log=True)
    # Check key of front
    if not "iteration" in request.get_json().keys():
        return error_msg(400, {}, "KEY:iteration does not exist.", log=True)
    elif not "class_name" in request.get_json().keys():
        return error_msg(400, {}, "KEY:class_name does not exist.", log=True)
    # Get project name
    prj_name = app.config["PROJECT_INFO"][uuid]["project_name"]
    # Get value of front
    iteration = request.get_json()['iteration']
    class_name = request.get_json()['class_name']
    # Mapping iteration
    if iteration != "workspace":
        iteration = chk.mapping_iteration(uuid, prj_name, iteration, front=True)
        if "error" in iteration:
            return error_msg(400, {}, str(iteration[1]), log=True)
    # Get img path
    all_img_path_db = get_img_path_db(uuid, prj_name, iteration, class_name, front=False)
    # Prevent error
    if "error" in all_img_path_db:
        return error_msg(400, {}, str(all_img_path_db[1]))
    # Setting url
    url = "http://{}:{}".format(app.config["HOST"], request.environ['SERVER_PORT'])
    img_path = [ url + "/display_img/" + path.split("./")[-1] for path in all_img_path_db["img_path"]]
    return success_msg(200, {"img_path":img_path}, "Success", "Get the image path of this class in the project:[{}:{}]".format(prj_name, class_name))

@app_dy_dt.route('/display_img/<path:path>', methods=['GET'])
def display_img(path):
    path_list= path.split("/")
    path = str(Path(__file__).resolve().parents[1]/"")
    # combination path
    for num, string in enumerate(path_list):
        if num != len(path_list)-1:
            path = path+"/"+string
    if exists(path+"/"+path_list[-1]):
        return send_from_directory(path, path_list[-1])
    else:
        return error_msg(400, {}, "This image does not exist:{}".format(path+"/"+path_list[-1]), log=True)

@app_dy_dt.route('/<uuid>/delete_img', methods=['DELETE'])
@swag_from("{}/{}".format(YAML_PATH, "delete_img.yml"))
def delete_img(uuid):
    # Check uuid is/isnot in app.config["PROJECT_INFO"]
    if not ( uuid in app.config["PROJECT_INFO"].keys()):
        return error_msg(400, {}, "UUID:{} does not exist.".format(uuid), log=True)
    # Check key of front
    if not "image_info" in request.get_json().keys():
        return error_msg(400, {}, "KEY:image_info does not exist.", log=True)
    # Get project name
    prj_name = app.config["PROJECT_INFO"][uuid]["project_name"]
    # Get type
    type = app.config["PROJECT_INFO"][uuid]['type']
    # Get value of front
    image_info_list = request.get_json()['image_info']
    # Loop dictionary key
    for key in image_info_list.keys():
        if key == "Unlabeled" or type == "object_detection":
            label = ""
        else:
            label = key
        # Check folder does exist
        if os.path.isdir(ROOT + '/' + prj_name + "/workspace/" + label):
            for name in image_info_list[key]:
                # Remove image
                img_path = ROOT + '/' + prj_name + "/workspace/" + label + '/' + name
                if os.path.isfile(img_path):
                    logging.info("Removed:[{}]".format(img_path))
                    os.remove(img_path)
                    # Delete data in Database
                    error_db = delete_data_table_cmd("workspace", "project_uuid=\'{}\' AND img_path=\'{}\'".format(uuid, label+'/'+name))
                    if error_db:
                        return error_msg(400, {}, str(error_db[1]))
                    # Update project of Database
                    update = Upload_DB(uuid, prj_name, type, label, "", "")
                    error_db = update.update_prj_db()
                    # Prevent error
                    if error_db:
                        return error_msg(400, {}, str(error_db[1]))
                else:
                    logging.error("This image does not exist in workspace:[{}]".format(label + '/' + name))
                # Object_detection remove txt
                txt_path = ROOT + '/' + prj_name +"/workspace/" + label + '/' + os.path.splitext(name)[0] + '.txt'
                if type == 'object_detection' and os.path.isfile(txt_path):
                    logging.info("Removed:{}".format(txt_path))
                    os.remove(txt_path)
        else:
            return error_msg(400, {}, "This class does not exist in the Project:[{}:{}]".format(prj_name, key), log=True)
    # Update project dataset numbers
    error_db = get_project_info(uuid)
    # Error
    if error_db:
        return error_msg(400, {}, str(error_db[1]))
    return success_msg(200, {}, "Success", "Deleted images in project:[{}:{}]".format(prj_name, image_info_list))

@app_dy_dt.route('/<uuid>/delete_all_img', methods=['DELETE'])
@swag_from("{}/{}".format(YAML_PATH, "delete_all_img.yml"))
def delete_all_img(uuid):
    # Check uuid is/isnot in app.config["PROJECT_INFO"]
    if not ( uuid in app.config["PROJECT_INFO"].keys()):
        return error_msg(400, {}, "UUID:{} does not exist.".format(uuid), log=True)
    # Get project name
    prj_name = app.config["PROJECT_INFO"][uuid]["project_name"]
    # Get type
    type = app.config["PROJECT_INFO"][uuid]['type']
    # Check folder does exist
    main_path = os.path.join(ROOT, prj_name)
    ws_path = os.path.join(main_path, "workspace")
    if os.path.isdir(ws_path):
        temp_classes_path = os.path.join(main_path, "classes.txt")
        # Catch classes.txt
        shutil.move(os.path.join(ws_path, "classes.txt"), temp_classes_path)
        # Delete workspace
        shutil.rmtree(ws_path)
        # Create new folder
        create_class_dir("Unlabeled", prj_name, type)
        # Create new class foler
        if type == "classification":
            classes_list = get_classes_list(temp_classes_path)
            for cls in classes_list:
                create_class_dir(cls, prj_name, type)
        # move classes.txt
        shutil.move(temp_classes_path, os.path.join(ws_path, "classes.txt"))
        # Delete data in ws table of db
        error_db = delete_data_table_cmd("workspace", "project_uuid=\'{}\'".format(uuid))
        if error_db:
            return error_msg(400, {}, str(error_db[1]))
        # Remove cover images
        cover_list = [ main_path + "/" + name for name in os.listdir(main_path) if "cover" in name]
        if len(cover_list) > 0 and exists(cover_list[0]):
            os.remove(cover_list[0])
        # Update prj table of db
        values = "effect_img_nums=0, unlabeled_img_nums=0, show_image_path=\'\'"
        select = "project_uuid=\'{}\'".format(uuid)
        error_db = update_data_table_cmd("project", values, select)
        if error_db:
            return error_msg(400, {}, str(error_db[1]))
        # Update project dataset numbers
        error_db = get_project_info(uuid)
        # Error
        if error_db:
            return error_msg(400, {}, str(error_db[1]))
        return success_msg(200, {}, "Success", "Deleted all images in Project:[{}]".format(prj_name))
    else:
        return error_msg(400, {}, "The workspace dose not exist in Project:[{}].".format(prj_name), log=True)

@app_dy_dt.route('/<uuid>/iter_class_num', methods=['POST'])
@swag_from("{}/{}".format(YAML_PATH, "iter_class_num.yml"))
def iter_class_num(uuid):
    # Check uuid is/isnot in app.config["PROJECT_INFO"]
    if not ( uuid in app.config["PROJECT_INFO"].keys()):
        return error_msg(400, {}, "UUID:{} does not exist.".format(uuid), log=True)
    # Check key of front
    if not "iteration" in request.get_json().keys():
        return error_msg(400, {}, "KEY:iteration does not exist.", log=True)
    # Get project name
    prj_name = app.config["PROJECT_INFO"][uuid]["project_name"]
    # Get type
    type = app.config["PROJECT_INFO"][uuid]['type']
    # Get value of front
    iteration = request.get_json()['iteration']
    # Check unlabeled images -> ??
    logging.warning("Get API of iter_class_num :[iteration:{}, type:{}]".format(iteration, type))
    if iteration== "workspace":
        # Same classes.txt processing
        dir_path = os.path.join(ROOT,  prj_name, iteration)
        error_db = compare_classes(dir_path, uuid)
        if error_db:
            return error_msg(400, {}, str(error_db[1]))
        # Check Unlabeled images
        error_db = check_unlabeled_images(uuid, prj_name)
        if error_db:
            return error_msg(400, {}, str(error_db[1]))
    else:
        # Mapping iteration
        iteration = chk.mapping_iteration(uuid, prj_name, iteration, front=True)
        if "error" in iteration:
            return error_msg(400, {}, str(iteration[1]), log=True)
    # Get class number
    all_classes_info_db = count_dataset(uuid, prj_name, iteration, front=False)
    if "error" in all_classes_info_db:
        return error_msg(400, {}, str(all_classes_info_db[1]))
    return success_msg(200, all_classes_info_db, "Success", "Get numbers of dataset in project:[{}:{}]:{}".format(prj_name, iteration, all_classes_info_db))


from __future__ import absolute_import, division, print_function
__metaclass__ = type

DOCUMENTATION = r'''
---
module: service_facts
short_description: Return service state information as fact data
description:
     - Return service state information as fact data for various service management utilities.
version_added: "2.5"
requirements: ["Any of the following supported init systems: systemd, sysv, upstart, AIX SRC"]
extends_documentation_fragment:
  -  action_common_attributes
  -  action_common_attributes.facts
attributes:
    check_mode:
        support: full
    diff_mode:
        support: none
    facts:
        support: full
    platform:
        platforms: posix
notes:
  - When accessing the C(ansible_facts.services) facts collected by this module,
    it is recommended to not use "dot notation" because services can have a C(-)
    character in their name which would result in invalid "dot notation", such as
    C(ansible_facts.services.zuul-gateway). It is instead recommended to
    using the string value of the service name as the key in order to obtain
    the fact data value like C(ansible_facts.services['zuul-gateway'])
  - AIX SRC was added in version 2.11.
author:
  - Adam Miller (@maxamillion)
'''

EXAMPLES = r'''
- name: Populate service facts
  ansible.builtin.service_facts:

- name: Print service facts
  ansible.builtin.debug:
    var: ansible_facts.services
'''

RETURN = r'''
ansible_facts:
  description: Facts to add to ansible_facts about the services on the system
  returned: always
  type: complex
  contains:
    services:
      description: States of the services with service name as key.
      returned: always
      type: complex
      contains:
        source:
          description:
          - Init system of the service.
          - One of C(rcctl), C(systemd), C(sysv), C(upstart), C(src).
          returned: always
          type: str
          sample: sysv
        state:
          description:
          - State of the service.
          - 'This commonly includes (but is not limited to) the following: C(failed), C(running), C(stopped) or C(unknown).'
          - Depending on the used init system additional states might be returned.
          returned: always
          type: str
          sample: running
        status:
          description:
          - State of the service.
          - Either C(enabled), C(disabled), C(static), C(indirect) or C(unknown).
          returned: systemd systems or RedHat/SUSE flavored sysvinit/upstart or OpenBSD
          type: str
          sample: enabled
        name:
          description: Name of the service.
          returned: always
          type: str
          sample: arp-ethers.service
'''

import platform
import re
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.locale import get_best_parsable_locale

class BaseService(object):

    def __init__(self, module):
        self.module = module
        self.incomplete_warning = False

class ServiceScanService(BaseService):

    def gather_services(self):
        services = {}
        service_path = self.module.get_bin_path("service")
        if service_path is None:
            return None
        initctl_path = self.module.get_bin_path("initctl")
        chkconfig_path = self.module.get_bin_path("chkconfig")

        # sysvinit
        if service_path is not None and chkconfig_path is None:
            rc, stdout, stderr = self.module.run_command("%s --status-all 2>&1 | grep -E \"\\[ (\\+|\\-) \\]\"" % service_path, use_unsafe_shell=True)
            for line in stdout.split("\n"):
                line_data = line.split()
                if len(line_data) < 4:
                    continue  # Skipping because we expected more data
                service_name = " ".join(line_data[3:])
                if line_data[1] == "+":
                    service_state = "running"
                else:
                    service_state = "stopped"
                services[service_name] = {"name": service_name, "state": service_state, "source": "sysv"}

        # Upstart
        if initctl_path is not None and chkconfig_path is None:
            p = re.compile(r'^\s?(?P<name>.*)\s(?P<goal>\w+)\/(?P<state>\w+)(\,\sprocess\s(?P<pid>[0-9]+))?\s*$')
            rc, stdout, stderr = self.module.run_command("%s list" % initctl_path)
            real_stdout = stdout.replace("\r", "")
            for line in real_stdout.split("\n"):
                m = p.match(line)
                if not m:
                    continue
                service_name = m.group('name')
                service_goal = m.group('goal')
                service_state = m.group('state')
                if m.group('pid'):
                    pid = m.group('pid')
                else:
                    pid = None  # NOQA
                payload = {"name": service_name, "state": service_state, "goal": service_goal, "source": "upstart"}
                services[service_name] = payload

        # RH sysvinit
        elif chkconfig_path is not None:
            # print '%s --status-all | grep -E "is (running|stopped)"' % service_path
            p = re.compile(
                r'(?P<service>.*?)\s+[0-9]:(?P<rl0>on|off)\s+[0-9]:(?P<rl1>on|off)\s+[0-9]:(?P<rl2>on|off)\s+'
                r'[0-9]:(?P<rl3>on|off)\s+[0-9]:(?P<rl4>on|off)\s+[0-9]:(?P<rl5>on|off)\s+[0-9]:(?P<rl6>on|off)')
            rc, stdout, stderr = self.module.run_command('%s' % chkconfig_path, use_unsafe_shell=True)
            # Check for special cases where stdout does not fit pattern
            match_any = False
            for line in stdout.split('\n'):
                if p.match(line):
                    match_any = True
            if not match_any:
                p_simple = re.compile(r'(?P<service>.*?)\s+(?P<rl0>on|off)')
                match_any = False
                for line in stdout.split('\n'):
                    if p_simple.match(line):
                        match_any = True
                if match_any:
                    # Try extra flags " -l --allservices" needed for SLES11
                    rc, stdout, stderr = self.module.run_command('%s -l --allservices' % chkconfig_path, use_unsafe_shell=True)
                elif '--list' in stderr:
                    # Extra flag needed for RHEL5
                    rc, stdout, stderr = self.module.run_command('%s --list' % chkconfig_path, use_unsafe_shell=True)
            for line in stdout.split('\n'):
                m = p.match(line)
                if m:
                    service_name = m.group('service')
                    service_state = 'stopped'
                    service_status = "disabled"
                    if m.group('rl3') == 'on':
                        service_status = "enabled"
                    rc, stdout, stderr = self.module.run_command('%s %s status' % (service_path, service_name), use_unsafe_shell=True)
                    service_state = rc
                    if rc in (0,):
                        service_state = 'running'
                    # elif rc in (1,3):
                    else:
                        if 'root' in stderr or 'permission' in stderr.lower() or 'not in sudoers' in stderr.lower():
                            self.incomplete_warning = True
                            continue
                        else:
                            service_state = 'stopped'
                    service_data = {"name": service_name, "state": service_state, "status": service_status, "source": "sysv"}
                    services[service_name] = service_data
        return services

class SystemctlScanService(BaseService):

    def systemd_enabled(self):
        # Check if init is the systemd command, using comm as cmdline could be symlink
        try:
            f = open('/proc/1/comm', 'r')
        except IOError:
            # If comm doesn't exist, old kernel, no systemd
            return False
        for line in f:
            if 'systemd' in line:
                return True
        return False

    def gather_services(self):
        BAD_STATES = frozenset(['not-found', 'masked', 'failed'])
        services = {}
        if not self.systemd_enabled():
            return None
        systemctl_path = self.module.get_bin_path("systemctl", opt_dirs=["/usr/bin", "/usr/local/bin"])
        if systemctl_path is None:
            return None

        # list units as systemd sees them
        rc, stdout, stderr = self.module.run_command("%s list-units --no-pager --type service --all" % systemctl_path, use_unsafe_shell=True)
        for line in [svc_line for svc_line in stdout.split('\n') if '.service' in svc_line]:

            state_val = "stopped"
            status_val = "unknown"
            fields = line.split()
            for bad in BAD_STATES:
                if bad in fields:  # dot is 0
                    status_val = bad
                    fields = fields[1:]
                    break
            else:
                # active/inactive
                status_val = fields[2]

            # array is normalize so predictable now
            service_name = fields[0]
            if fields[3] == "running":
                state_val = "running"

            services[service_name] = {"name": service_name, "state": state_val, "status": status_val, "source": "systemd"}

        # now try unit files for complete picture and final 'status'
        rc, stdout, stderr = self.module.run_command("%s list-unit-files --no-pager --type service --all" % systemctl_path, use_unsafe_shell=True)
        for line in [svc_line for svc_line in stdout.split('\n') if '.service' in svc_line]:
            # there is one more column (VENDOR PRESET) from `systemctl list-unit-files` for systemd >= 245
            try:
                service_name, status_val = line.split()[:2]
            except IndexError:
                self.module.fail_json(msg="Malformed output discovered from systemd list-unit-files: {0}".format(line))
            if service_name not in services:
                rc, stdout, stderr = self.module.run_command("%s show %s --property=ActiveState" % (systemctl_path, service_name), use_unsafe_shell=True)
                state = 'unknown'
                if not rc and stdout != '':
                    state = stdout.replace('ActiveState=', '').rstrip()
                services[service_name] = {"name": service_name, "state": state, "status": status_val, "source": "systemd"}
            elif services[service_name]["status"] not in BAD_STATES:
                services[service_name]["status"] = status_val

        return services

class AIXScanService(BaseService):

    def gather_services(self):
        services = {}
        if platform.system() != 'AIX':
            return None
        lssrc_path = self.module.get_bin_path("lssrc")
        if lssrc_path is None:
            return None
        rc, stdout, stderr = self.module.run_command("%s -a" % lssrc_path)
        for line in stdout.split('\n'):
            line_data = line.split()
            if len(line_data) < 2:
                continue  # Skipping because we expected more data
            if line_data[0] == "Subsystem":
                continue  # Skip header
            service_name = line_data[0]
            if line_data[-1] == "active":
                service_state = "running"
            elif line_data[-1] == "inoperative":
                service_state = "stopped"
            else:
                service_state = "unknown"
            services[service_name] = {"name": service_name, "state": service_state, "source": "src"}
        return services

class OpenBSDScanService(BaseService):
    def query_rcctl(self, cmd):
        svcs = []

        rc, stdout, stderr = self.module.run_command("%s ls %s" % (self.rcctl_path, cmd))
        if 'needs root privileges' in stderr.lower():
            self.incomplete_warning = True
            return []

        for svc in stdout.split('\n'):
            if svc == '':
                continue
            else:
                svcs.append(svc)

        return svcs

    def gather_services(self):
        services = {}
        self.rcctl_path = self.module.get_bin_path("rcctl")
        if self.rcctl_path is None:
            return None

        for svc in self.query_rcctl('all'):
            services[svc] = {'name': svc, 'source': 'rcctl'}

        for svc in self.query_rcctl('on'):
            services[svc].update({'status': 'enabled'})

        for svc in self.query_rcctl('started'):
            services[svc].update({'state': 'running'})

        # Based on the list of services that are enabled, determine which are disabled
        [services[svc].update({'status': 'disabled'}) for svc in services if services[svc].get('status') is None]

        # and do the same for those are aren't running
        [services[svc].update({'state': 'stopped'}) for svc in services if services[svc].get('state') is None]

        # Override the state for services which are marked as 'failed'
        for svc in self.query_rcctl('failed'):
            services[svc].update({'state': 'failed'})

        return services

def main():
    module = AnsibleModule(argument_spec=dict(), supports_check_mode=True)
    locale = get_best_parsable_locale(module)
    module.run_command_environ_update = dict(LANG=locale, LC_ALL=locale)
    service_modules = (ServiceScanService, SystemctlScanService, AIXScanService, OpenBSDScanService)
    all_services = {}
    incomplete_warning = False
    for svc_module in service_modules:
        svcmod = svc_module(module)
        svc = svcmod.gather_services()
        if svc is not None:
            all_services.update(svc)
            if svcmod.incomplete_warning:
                incomplete_warning = True
    if len(all_services) == 0:
        results = dict(skipped=True, msg="Failed to find any services. Sometimes this is due to insufficient privileges.")
    else:
        results = dict(ansible_facts=dict(services=all_services))
        if incomplete_warning:
            results['msg'] = "WARNING: Could not find status for all services. Sometimes this is due to insufficient privileges."
    module.exit_json(**results)

if __name__ == '__main__':
    main()


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020-03-31 01:05
# @Author  : cxw
# @File    : terminal
# @Software: PyCharm

import math
from . import terminal_router
from app.model.history import History
from app.model.summary import Summary
from app.comm.utils import *
from flask import jsonify
from app.validators.terminal import ListForm,ChartForm,Real_time

@terminal_router.route('/meslist',methods=["POST"])
def meslist():
    form = ListForm().check_param()
    query =History.get_mes(form)
    page = int(form.page_index.data)
    page_size = form.page_size.data
    result= [x.to_dict() for x in query.items]
    length_query = query.total
    total_count = math.ceil(length_query / page_size)
    pagination = {
        'pagination': {
            'total_count': length_query,
            'page_count': total_count,
            'count': page_size,
            "page_index": int(page),
        },
    }
    if result:
        return jsonify(true_return(msg="请求成功",data={"pagination":pagination["pagination"],"data":result}))
    else:
        return jsonify(false_return(msg="请求失败"))

@terminal_router.route('today_detection',methods=["POST"])
def today_detection():
    query = Summary.get_mes()
    return jsonify(true_return(msg="请求成功",data=query))

@terminal_router.route('/chart',methods=["POST"])
def chart():
    form =ChartForm().check_param()
    # if form.date.data:
    #     start =form.date.data[0][:10]
    #     end =form.date.data[1][:10]
    #     date_list = []
    #     date_list = func_days(start, end, date_list)[::-1]
    #     result, result1, result2 = History.get_chat(date_list)
    # else:
    today = datetime.date.today()
    date_list=[x for x in range(24)]
    result,result1,result2,result3,sy = History.get_chat1(today.__str__())
    if result:
        return jsonify(true_return(msg="请求成功",data={"data":result,"date_list":date_list,"data1":result1,"data2":result2,"data3":result3,"sy":sy}))
    else:
        return jsonify(false_return(msg="请求失败"))

@terminal_router.route('/chart2',methods=["POST"])
def chart2():
    form =ChartForm().check_param()
    today = datetime.date.today()
    target = (today - datetime.timedelta(days=8))
    target_res = target.strftime("%Y-%m-%d")
    date_list = func(today,[])[::-1]
    result= History.get_chat2(target_res,today,date_list)
    if result:
        return jsonify(true_return(msg="请求成功",data={"data":result,"date_list":date_list}))
    else:
        return jsonify(false_return(msg="请求失败"))


import numpy as np
import pandas as pd
from shapely import geometry
import geopandas as gpd

#### MAKE GRID

x = np.arange(-130,-65, 0.125)
y = np.arange(24, 50, 0.125)

ssize = 0.125

xy_ll = np.vstack(np.dstack(np.meshgrid(x, y)))
xy_ur = xy_ll + ssize
xy_lr = np.column_stack([xy_ll[:,0] + ssize, xy_ll[:,1]])
xy_ul = np.column_stack([xy_ll[:,0], xy_ll[:,1] + ssize])

cs = np.column_stack([xy_ur, xy_ul, xy_ll, xy_lr, xy_ur])
cs_shape = cs.shape
cs = cs.reshape(cs_shape[0], cs_shape[1]/2, 2)

#CHEESY WAY
gs = gpd.GeoSeries(pd.Series(cs.tolist()).apply(lambda x: geometry.Polygon(x)))

#MORE ELEGANT, BUT SLOWER
#gpd.GeoSeries(map(geometry.Polygon, cs))

#ABOUT AS SLOW AS MAP
#[geometry.Polygon(i) for i in cs]


# encoding: utf-8

'''
今日练习：selenium 爬虫 + excel表格操作（读写、修改）

demo：搜集web自动化原创微博(前5页)的 发送人，标题，发布时间，来源，收藏数，转发数，评论数，点赞数写入excel表格

'''

import time, xlrd, xlwt
from selenium import webdriver
from xlutils.copy import copy

driver = webdriver.Chrome()

driver.get('https://s.weibo.com/')

def append_data(init_row, sheet, data_list):
    count = init_row
    for ele in data_list:
        count += 1
        userName = ele.find_element_by_css_selector('a.name').text
        title = ele.find_element_by_css_selector('p.txt').text
        time = ele.find_element_by_css_selector('p.from > a:nth-of-type(1)').text
        source = ele.find_element_by_css_selector('p.from > a:nth-of-type(2)').text
        collect = ele.find_element_by_css_selector('div.card-act ul li:nth-of-type(1)').text.split('收藏')[1] or 0
        share = ele.find_element_by_css_selector('div.card-act ul li:nth-of-type(2)').text.split('转发')[1] or 0
        comment = ele.find_element_by_css_selector('div.card-act ul li:nth-of-type(3)').text.split('评论')[1] or 0
        like = ele.find_element_by_css_selector('div.card-act ul li:nth-of-type(4)').text or 0

        sheet.write(count, 0, title)
        sheet.write(count, 1, userName)
        sheet.write(count, 2, time)
        sheet.write(count, 3, source)
        sheet.write(count, 4, collect)
        sheet.write(count, 5, share)
        sheet.write(count, 6, comment)
        sheet.write(count, 7, like)

def create_excel():
    driver.find_element_by_xpath('//*[@id="pl_homepage_search"]/div/div[2]/div/input').send_keys('web自动化')
    driver.find_element_by_xpath('//*[@id="pl_homepage_search"]/div/div[2]/button').click()
    ele_search_high = driver.find_element_by_xpath('//*[@id="pl_feedtop_top"]/div[3]/a')
    if (ele_search_high.is_displayed()):
        ele_search_high.click()
        driver.find_element_by_xpath('//*[@id="radio03"]').click()
        driver.find_element_by_xpath('/html/body/div[7]/div[2]/div/div[2]/a[1]').click()

        # 建表
        excel = xlwt.Workbook()
        sheet1 = excel.add_sheet('web自动化原创微博基本信息收集')
        sheet1.write(0, 0, '标题')
        sheet1.write(0, 1, '发送人')
        sheet1.write(0, 2, '发布时间')
        sheet1.write(0, 3, '来源')
        sheet1.write(0, 4, '收藏数')
        sheet1.write(0, 5, '转发数')
        sheet1.write(0, 6, '评论数')
        sheet1.write(0, 7, '点赞数')

        # 写入微博第1页列表数据
        list = driver.find_elements_by_css_selector("#pl_feedlist_index div[action-type='feed_list_item']")
        append_data(0, sheet1, list)

        # 保存第1页数据
        excel.save('./weboList.xls')

        # 切换页并将数据追加到已经建好的数据表格中
        eles_page_list = driver.find_elements_by_css_selector('#pl_feedlist_index > div.m-page > div > span > ul > li')
        for i in range(len(eles_page_list)):
            if (i < 4):
                switch_page(i + 2)

def switch_page(pageNum):
    driver.find_element_by_xpath('//*[@id="pl_feedlist_index"]/div[3]/div/span/a').click()

    ele_page = '#pl_feedlist_index > div.m-page > div > span > ul > li:nth-of-type({})'.format(pageNum)

    # 切换到指定页
    driver.find_element_by_css_selector(ele_page).click()

    # 复制已经建好的表将新数据写入再重新保存
    excel = xlrd.open_workbook('./weboList.xls')
    sheet = excel.sheet_by_name('web自动化原创微博基本信息收集')
    rows_old = sheet.nrows
    excel_new = copy(excel)
    sheet_new = excel_new.get_sheet(0)
    list = driver.find_elements_by_css_selector("#pl_feedlist_index div[action-type='feed_list_item']")
    append_data(rows_old - 1, sheet_new, list)
    excel_new.save('./weboList.xls')
    if (pageNum == 5):
        driver.quit()

create_excel()


import json
import es
import zlib
import base64
import datetime

def lambda_handler(event, context):
    payloads = []
    for record in event['Records']:
        sns_message = json.loads(record["body"])
        try:
            decoded = json.loads(zlib.decompress(base64.b64decode(sns_message["Message"]), 16 + zlib.MAX_WBITS))
        except:
            decoded = json.loads(sns_message["Message"])
        if type(decoded) == dict:
            incoming_payloads = [decoded]
        else:
            incoming_payloads = decoded
        year, week = datetime.datetime.now().isocalendar()[:2]
        index = f"{year}-{week}"
        payloads += incoming_payloads

    body=""
    for payload in payloads:
        body += "{\"index\":{}}\n" + json.dumps(payload) + "\n"
    body += "\n"

    result = es.request(body, f"telm-{index}/_bulk", "POST")
    if 'errors' in result and result['errors'] == True:
        error_types = [x['index']['error']['type'] for x in result['items'] if 'error' in x['index']] # get all the error types
        print(event)
        print(result)
        error_types = [a for a in error_types if a != 'mapper_parsing_exception'] # filter out mapper failures since they will never succeed
        if error_types:
            raise RuntimeError


## Code in order to read waypoints from log

# Call tools file to create an LogeFileData object and
# create graphs
import numpy as np
import matplotlib.pyplot as plt
from tkinter import *
from tkinter import filedialog
from tools import LogFileData
path_file = ''

# Fuction that is called when user selects a log file
def process_log():
	global path_file
	global log_num
	global data_log
	global window

	# Create var to get and store de file's path
	path_file = filedialog.askopenfilename(filetypes=[("log",".log")])

	# Get log number to create a folder with that name
	log_num = path_file.split('/')[-1].split('.')[0]
	path = path_file[0:path_file.find(log_num)]
	data_log = LogFileData(path_file)
	window.withdraw()
	data_log.save_waypoints(fname = path + log_num)
	print("File saved")
	del data_log
	window.deiconify()

def main():
	global window
	window = Tk()
	window.title('Select File')
	window.resizable(0, 0)
	# window.geometry('350x200')
	btn = Button(window, text = 'Read Log', width = 25, command = process_log)
	btn.grid(column = 0, row = 0, padx = 0, pady = 5)
	window.mainloop()

if __name__ == '__main__':
	main()