import datetime

from django.db.models import Avg, Case, ExpressionWrapper, F, fields, When
from django.shortcuts import render, get_object_or_404
from django.http import JsonResponse
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from rest_framework.decorators import api_view
from rest_framework import status, viewsets
from .models import Magazine, Author, Publisher, Buyer, BuyerSubscription, PublisherStatsDTO, Purchase, AuthorStatsDTO
from .serializer import MagazineSerializer, AuthorSerializer, PublisherSerializer, BuyerSerializer, \
    BuyerSubscriptionSerializer, PublisherStatsDTOSerializer, AuthorStatsDTOSerializer

@api_view(['GET'])
def magazines_pagination(request):
    page = int(request.GET.get('page', 1))
    per_page = int(request.GET.get('per_page', 10))

    magazines = Magazine.objects.all()
    total_count = magazines.count()

    start_index = (page - 1) * per_page
    end_index = start_index + per_page

    magazines = magazines[start_index:end_index]
    serializer = MagazineSerializer(magazines, many=True)

    return Response({
        'total_count': total_count,
        'per_page': per_page,
        'current_page': page,
        'magazines': serializer.data,
    })

@api_view(['GET'])
def authors_pagination(request):
    page = int(request.GET.get('page', 1))
    per_page = int(request.GET.get('per_page', 10))

    authors = Author.objects.all()
    total_count = authors.count()

    start_index = (page - 1) * per_page
    end_index = start_index + per_page

    authors = authors[start_index:end_index]
    serializer = AuthorSerializer(authors, many=True)

    return Response({
        'total_count': total_count,
        'per_page': per_page,
        'current_page': page,
        'authors': serializer.data,
    })

@api_view(['GET'])
def publishers_pagination(request):
    page = int(request.GET.get('page', 1))
    per_page = int(request.GET.get('per_page', 10))

    publishers = Publisher.objects.all()
    total_count = publishers.count()

    start_index = (page - 1) * per_page
    end_index = start_index + per_page

    publishers = publishers[start_index:end_index]
    serializer = PublisherSerializer(publishers, many=True)

    return Response({
        'total_count': total_count,
        'per_page': per_page,
        'current_page': page,
        'publishers': serializer.data,
    })

@api_view(['GET'])
def buyers_pagination(request):
    page = int(request.GET.get('page', 1))
    per_page = int(request.GET.get('per_page', 10))

    buyers = Buyer.objects.all()
    total_count = buyers.count()

    start_index = (page - 1) * per_page
    end_index = start_index + per_page

    buyers = buyers[start_index:end_index]
    serializer = BuyerSerializer(buyers, many=True)

    return Response({
        'total_count': total_count,
        'per_page': per_page,
        'current_page': page,
        'buyers': serializer.data,
    })

@api_view(['GET'])
def magazines_list(request):
    paginator = PageNumberPagination()
    paginator.page_size = 10  # set the number of items per page
    magazines = Magazine.objects.all()  # complex data
    paginated_magazines = paginator.paginate_queryset(magazines, request)
    serializer = MagazineSerializer(paginated_magazines, many=True)
    return paginator.get_paginated_response(serializer.data)

@api_view(['POST'])
def magazine_create(request):
    serializer = MagazineSerializer(data=request.data)
    if serializer.is_valid():
        magazine = serializer.save()

        buyer_ids = request.data.get('buyers', [])
        purchases = []
        for buyer_id in buyer_ids:
            buyer = get_object_or_404(Buyer, id=buyer_id)
            purchase = Purchase(magazine=magazine, buyer=buyer, price=magazine.price)
            purchases.append(purchase)

        Purchase.objects.bulk_create(purchases)
        return Response(serializer.data)
    else:
        return Response(serializer.errors)

@api_view(['GET', 'PUT', 'DELETE'])
def magazine_detail(request, pk):
    try:
        magazine = Magazine.objects.get(pk=pk)
    except:
        return Response({'error': 'Magazine does not exist!'}, status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = MagazineSerializer(magazine)
        data = serializer.data

        # Get the author and publisher objects and add them to the response
        if magazine.author:
            author_serializer = AuthorSerializer(magazine.author)
            data['author'] = author_serializer.data
        if magazine.publisher:
            publisher_serializer = PublisherSerializer(magazine.publisher)
            data['publisher'] = publisher_serializer.data

        buyers = magazine.buyers.all()
        buyer_serializer = BuyerSerializer(buyers, many=True)
        data['buyers'] = buyer_serializer.data

        return Response(data)

    if request.method == 'PUT':
        serializer = MagazineSerializer(magazine, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    if request.method == 'DELETE':
        magazine.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

@api_view(['GET'])
def magazines_with_pages_above(request, min_pages):
    magazines = Magazine.objects.filter(
        number_of_pages__gte=min_pages)  # filter by number_of_pages greater than or equal to min_pages
    serializer = MagazineSerializer(magazines, many=True)
    return Response(serializer.data)

# Author views
@api_view(['GET'])
def authors_list(request):
    authors = Author.objects.all()
    serializer = AuthorSerializer(authors, many=True)
    return Response(serializer.data)

@api_view(['POST'])
def author_create(request):
    serializer = AuthorSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data)
    else:
        return Response(serializer.errors)

@api_view(['GET', 'PUT', 'DELETE'])
def author_detail(request, pk):
    try:
        author = Author.objects.get(pk=pk)
    except:
        return Response({'error': 'Author does not exist!'}, status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = AuthorSerializer(author)
        data = serializer.data

        magazines = Magazine.objects.filter(author=author)
        magazines_serializer = MagazineSerializer(magazines, many=True)
        data['magazines'] = magazines_serializer.data

        return Response(data)

    if request.method == 'PUT':
        serializer = AuthorSerializer(author, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    if request.method == 'DELETE':
        author.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

# Publisher views
@api_view(['GET'])
def publishers_list(request):
    publishers = Publisher.objects.all()
    serializer = PublisherSerializer(publishers, many=True)
    return Response(serializer.data)

@api_view(['POST'])
def publisher_create(request):
    serializer = PublisherSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data)
    else:
        return Response(serializer.errors)

@api_view(['GET', 'PUT', 'DELETE'])
def publisher_detail(request, pk):
    try:
        publisher = Publisher.objects.get(pk=pk)
    except:
        return Response({'error': 'Publisher does not exist!'}, status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = PublisherSerializer(publisher)
        return Response(serializer.data)

    if request.method == 'PUT':
        serializer = PublisherSerializer(publisher, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    if request.method == 'DELETE':
        publisher.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

@api_view(['GET'])
def buyers_list(request):
    buyers = Buyer.objects.all()
    serializer = BuyerSerializer(buyers, many=True)
    return Response(serializer.data)

@api_view(['POST'])
def buyer_create(request):
    serializer = BuyerSerializer(data=request.data)
    if serializer.is_valid():
        buyer = serializer.save()

        magazine_ids = request.data.get('magazines', [])
        purchases = []
        for magazine_id in magazine_ids:
            magazine = get_object_or_404(Magazine, id=magazine_id)
            purchase = Purchase(buyer=buyer, magazine=magazine, price=magazine.price)
            purchases.append(purchase)

        Purchase.objects.bulk_create(purchases)
        return Response(serializer.data)
    else:
        return Response(serializer.errors)

from rest_framework import status

@api_view(['GET', 'PUT', 'DELETE'])
def buyer_detail(request, pk):
    try:
        buyer = Buyer.objects.get(pk=pk)
    except Buyer.DoesNotExist:
        return Response({'error': 'Buyer does not exist!'}, status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = BuyerSerializer(buyer)
        data = serializer.data
        magazines = Magazine.objects.filter(purchase__buyer=buyer).distinct()
        magazine_serializer = MagazineSerializer(magazines, many=True)
        data['magazines'] = magazine_serializer.data
        return Response(data)

    elif request.method == 'PUT':
        serializer = BuyerSerializer(buyer, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        buyer.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

# BuyerSubscription views
@api_view(['GET'])
def subscriptions_list(request):
    subscriptions = BuyerSubscription.objects.all()
    serializer = BuyerSubscriptionSerializer(subscriptions, many=True)
    return Response(serializer.data)

@api_view(['POST'])
def subscription_create(request):
    serializer = BuyerSubscriptionSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data)
    else:
        return Response(serializer.errors)

@api_view(['GET', 'PUT', 'DELETE'])
def subscription_detail(request, pk):
    try:
        subscription = BuyerSubscription.objects.get(pk=pk)
    except:
        return Response({'error': 'Subscription does not exist!'}, status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = BuyerSubscriptionSerializer(subscription)
        data = serializer.data

        buyer_serializer = BuyerSerializer(subscription.buyer)
        data['buyer'] = buyer_serializer.data

        return Response(data)

    if request.method == 'PUT':
        serializer = BuyerSubscriptionSerializer(subscription, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    if request.method == 'DELETE':
        subscription.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

@api_view(['GET'])
def publisher_stats(request):
    # Calculate the average number of pages for magazines published by each publisher
    publisher_page_stats = (
        Publisher.objects.annotate(average_number_of_pages=Avg('magazine__number_of_pages'))
        .values('name', 'average_number_of_pages')
        .order_by('average_number_of_pages')
    )

    # Create a list of DTO instances
    publisher_stats = [
        PublisherStatsDTO(publisher_name=stat['name'], average_number_of_pages=stat['average_number_of_pages'])
        for stat in publisher_page_stats
    ]

    # Serialize the DTO instances
    serializer = PublisherStatsDTOSerializer(publisher_stats, many=True)

    # Return the serialized data as a JSON response
    return Response(serializer.data)

@api_view(['GET'])
def author_stats(request):
    # Calculate the average number of pages for magazines written by each author
    author_page_stats = (
        Author.objects.annotate(average_number_of_pages=Avg('magazine__number_of_pages'))
        .exclude(average_number_of_pages__isnull=True)
        .values('first_name', 'average_number_of_pages')
        .order_by('average_number_of_pages')
    )

    # Create a list of DTO instances
    author_stats = [
        AuthorStatsDTO(author_first_name=stat['first_name'], average_number_of_pages=stat['average_number_of_pages'])
        for stat in author_page_stats
    ]

    # Serialize the DTO instances
    serializer = AuthorStatsDTOSerializer(author_stats, many=True)

    # Return the serialized data as a JSON response
    return Response(serializer.data)

# @api_view(['POST'])
# def add_buyer_to_magazine(request, id):
#     try:
#         magazine = Magazine.objects.get(pk=id)
#         buyer_id = request.data.get('buyer_id')
#         buyer = Buyer.objects.get(pk=buyer_id)
#         magazine.buyers.add(buyer)
#         magazine.save()
#         return Response({'success': True, 'message': 'Buyer added successfully to the magazine.'},
#                         status=status.HTTP_201_CREATED)
#     except Exception as e:
#         return Response({'success': False, 'message': str(e)}, status=status.HTTP_400_BAD_REQUEST)
#
#
# @api_view(['DELETE'])
# def remove_buyer_from_magazine(request, id, buyer_id):
#     try:
#         magazine = Magazine.objects.get(pk=id)
#         buyer = Buyer.objects.get(pk=buyer_id)
#         magazine.buyers.remove(buyer)
#         magazine.save()
#         return Response({'success': True, 'message': 'Buyer removed successfully from the magazine.'},
#                         status=status.HTTP_204_NO_CONTENT)
#     except Exception as e:
#         return Response({'success': False, 'message': str(e)}, status=status.HTTP_400_BAD_REQUEST)

@api_view(['POST'])
def magazine_add_buyer(request, id):
    magazine = get_object_or_404(Magazine, id=id)
    buyer_id = request.data.get('buyer_id')
    buyer = get_object_or_404(Buyer, id=buyer_id)

    magazine.buyers.add(buyer)
    magazine.save()

    purchase = Purchase(magazine=magazine, buyer=buyer, price=magazine.price)
    purchase.save()

    return Response({'success': True, 'message': 'Buyer added successfully to the magazine.'},
                    status=status.HTTP_201_CREATED)

@api_view(['PATCH'])
def magazine_update_buyers(request, id):
    magazine = get_object_or_404(Magazine, id=id)
    buyer_ids = request.data.get('buyer_ids')

    # Clear all the existing buyers
    magazine.buyers.clear()

    # Delete all the purchases with the specified magazine ID and buyer ID
    for buyer_id in buyer_ids:
        buyer = get_object_or_404(Buyer, id=buyer_id)
        Purchase.objects.filter(magazine=magazine, buyer=buyer).delete()

        # Add the buyer to the magazine
        magazine.buyers.add(buyer)

        # Create a new purchase with the updated buyer and magazine
        purchase = Purchase(magazine=magazine, buyer=buyer, price=magazine.price)
        purchase.save()

    return Response({'success': True, 'message': 'Buyers updated successfully for the magazine.'},
                    status=status.HTTP_200_OK)

@api_view(['PUT', 'DELETE'])
def magazine_update_or_delete_buyer(request, id, buyer_id):
    magazine = get_object_or_404(Magazine, id=id)
    buyer = get_object_or_404(Buyer, id=buyer_id)

    if request.method == 'PUT':
        new_price = request.data.get('price')

        if new_price:
            purchase = get_object_or_404(Purchase, magazine=magazine, buyer=buyer)
            purchase.price = new_price
            purchase.save()
            return Response({'success': True, 'message': 'Purchase updated successfully.'},
                            status=status.HTTP_200_OK)

        return Response({'success': False, 'message': 'Missing price information.'},
                        status=status.HTTP_400_BAD_REQUEST)
    elif request.method == 'DELETE':
        purchase = get_object_or_404(Purchase, magazine=magazine, buyer=buyer)
        purchase.delete()
        magazine.buyers.remove(buyer)
        magazine.save()
        return Response({'success': True, 'message': 'Buyer removed from the magazine.'},
                        status=status.HTTP_204_NO_CONTENT)

@api_view(['POST'])
def buyer_add_magazine(request, id):
    buyer = get_object_or_404(Buyer, id=id)
    magazine_id = request.data.get('magazine_id')

    if magazine_id:
        magazine = get_object_or_404(Magazine, id=magazine_id)
        purchase = Purchase(buyer=buyer, magazine=magazine, price=magazine.price)
        purchase.save()
        return Response({'success': True, 'message': 'Magazine added to the buyer.'},
                        status=status.HTTP_201_CREATED)

    return Response({'success': False, 'message': 'Missing magazine_id information.'},
                    status=status.HTTP_400_BAD_REQUEST)

@api_view(['PUT', 'DELETE'])
def buyer_update_or_delete_magazine(request, id, magazine_id):
    buyer = get_object_or_404(Buyer, id=id)
    magazine = get_object_or_404(Magazine, id=magazine_id)

    if request.method == 'PUT':
        new_price = request.data.get('price')

        if new_price:
            purchase = get_object_or_404(Purchase, magazine=magazine, buyer=buyer)
            purchase.price = new_price
            purchase.save()
            return Response({'success': True, 'message': 'Purchase updated successfully.'},
                            status=status.HTTP_200_OK)

        return Response({'success': False, 'message': 'Missing price information.'},
                        status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        purchase = Purchase.objects.filter(magazine=magazine, buyer=buyer).first()
        if purchase:
            purchase.delete()
            return Response({'success': True, 'message': 'Magazine removed from the buyer.'},
                            status=status.HTTP_204_NO_CONTENT)
        else:
            return Response({'success': False, 'message': 'No matching purchase found.'},
                            status=status.HTTP_404_NOT_FOUND)


import os, hashlib, pytest, math

class CircleCIError(Exception):
    """Raised for problems running the CirleCI py.test plugin"""

def read_circleci_env_variables():
    """Read and convert CIRCLE_* environment variables"""
    circle_node_total = int(os.environ.get("CIRCLE_NODE_TOTAL", "1").strip() or "1")
    circle_node_index = int(os.environ.get("CIRCLE_NODE_INDEX", "0").strip() or "0")

    if circle_node_index >= circle_node_total:
        raise CircleCIError("CIRCLE_NODE_INDEX={} >= CIRCLE_NODE_TOTAL={}, should be less".format(circle_node_index, circle_node_total))

    return (circle_node_total, circle_node_index)

def pytest_report_header(config):
    """Add CircleCI information to report"""
    circle_node_total, circle_node_index = read_circleci_env_variables()
    return "CircleCI total nodes: {}, this node index: {}".format(circle_node_total, circle_node_index)

def _hash_item(item):
    item_location = ':'.join(map(str, item.location)).encode('utf-8')
    return int(hashlib.sha1(item_location).hexdigest(), 16)

@pytest.hookimpl(trylast=True)
def pytest_collection_modifyitems(session, config, items):
    """
    Use CircleCI env vars to determine which tests to run

    - CIRCLE_NODE_TOTAL indicates total number of nodes tests are running on
    - CIRCLE_NODE_INDEX indicates which node this is

    Will run a subset of tests based on the node index.

    """
    circle_node_total, circle_node_index = read_circleci_env_variables()
    hashed_items = [(_hash_item(item), item) for item in list(items)]
    hashed_items.sort(key=lambda i: i[0])
    deselected = []
    for index, (_, item) in enumerate(hashed_items):
        if (index % circle_node_total) != circle_node_index:
            deselected.append(item)
            items.remove(item)
    config.hook.pytest_deselected(items=deselected)


#Call
import os
import re
import glob
import errno
import configparser

import cv2
import pyocr
import numpy as np
import pandas as pd
import japanize_matplotlib
import matplotlib.pyplot as plt
from PIL import Image
from natsort import natsorted
from datetime import date, timedelta

from template_module import temp_PDF as temp

class ReadGraph:

    _defaults = {
        "input_dir": "./src/",
        "output_dir": "./dst/",
        "config_path": "template_module/temp_config_PDF.ini"
    }

    @classmethod
    def get_defaults(cls, key):
        if key in cls._defaults:
            return cls._defaults[key]
        else:
            return "Unrecognized attribute name '" + key + "'"

    def __init__(self, location, target, **kwargs):
        self.__dict__.update(self._defaults)
        self.__dict__.update(kwargs)
        self.location = location
        self.target = target
        self.section = location + "_" + target
        self.config_ini, self.default_conf = self.set_config()

    def set_config(self):
        """
        設定ファイルの読み込み

        Parameters
        ----------

        Returns
        --------

        """
        config_ini = configparser.ConfigParser()
        config_ini_path = self.__dict__["config_path"]

        # 指定したiniファイルが存在しない場合、エラー発生
        if not os.path.exists(config_ini_path):
            raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), config_ini_path)

        config_ini.read(config_ini_path, encoding='utf-8')
        DEFAULT_conf = config_ini["DEFAULT"]

        return config_ini, DEFAULT_conf

    @classmethod
    def get_image_path(self):
        """
        入力画像のパスを取得
        """
        image_paths = natsorted(glob.glob(self.__dict__["input_dir"] + "*.png"))

        return image_paths

    @classmethod
    def get_image_size(self, image_path):
        """
        入力画像のサイズを取得
        """
        img = cv2.imread(image_path)
        size = img.shape

        return size

    @classmethod
    def get_type(self):
        """
        グラフのタイプを取得
        """
        img_type = self.config_ini[self.section]["type"]

        return img_type

    def read_graph(self, image_path):
        """
        mainの関数

        Parameters
        ----------
        image_path : 入力画像

        Returns
        --------
        dfObj : グラフの読み取り結果（pandas.DataFrame）

        Flow
        --------
        1. 年月日の取得＆設定したタイプが合っているか確認
        ↓
        2. タイプが合っていなければ変更して再度年月日を読み取り
        ↓
        3. テンプレートマッチング
        ↓
        4. 縦軸の目盛読み取り
        ↓
        5. 目盛の直線を検出
        ↓
        6. マーカーの値を算出
        ↓
        7. データフレームを作成
        ↓
        8. データフレームを出力

        """
        #1
        img_type = self.get_type()
        crop_range = eval(self.config_ini[self.section]["date_range"])
        padding_range = eval(self.config_ini[self.section]["padding_range"])
        #タイプAとタイプBで前処理が異なる
        if img_type == "A":
            pil_image = temp.img_crop(image_path, crop_range)
        else:
            pil_image = temp.crop2ocr(image_path, crop_range, padding_range)
        ocr_date = temp.OCR(pil_image, lang='jpn')

        date_list, type_check = temp.get_date_type(ocr_date)

        #2
        if type_check:
            pass

        else:
            self.section = self.section + "_B"
            crop_range = eval(self.config_ini[self.section]["date_range"])
            padding_range = eval(self.config_ini[self.section]["padding_range"])
            pil_image = temp.crop2ocr(image_path, crop_range, padding_range)
            ocr_date = temp.OCR(pil_image, lang='jpn')

            date_list, type_check = temp.get_date_type(ocr_date)
            print("change typeA to typeB")

        #3
        img = cv2.imread(image_path)
        y1, y2, x1, x2 = eval(self.config_ini[self.section]["match_range"])
        img_cropped = img[y1:y2, x1:x2, :]

        fill_color = eval(self.default_conf["fill_color"])
        min_list = eval(self.config_ini[self.get_type()]["min_list"])

        list_1, h1, w1 = temp.template_matching(img_cropped, eval(self.default_conf["red_mark_path"]),
                                                [0, 0, 255], fill_color, eval(self.default_conf["red_list"]), min_list)
        list_2, h2, w2 = temp.template_matching(img_cropped, eval(self.default_conf["blue_mark_path"]),
                                                [255, 0, 0], fill_color, eval(self.default_conf["blue_list"]), min_list)
        list_3, h3, w3 = temp.template_matching(img_cropped, eval(self.default_conf["black_mark_path"]),
                                                [0, 0, 0], fill_color, eval(self.default_conf["black_list"]), min_list)

        #template matching 書き出し
        """
        temp.draw(img_cropped, list_1, h1, w1, [0, 0, 255])
        temp.draw(img_cropped, list_2, h2, w2, [255, 0, 0])
        temp.draw(img_cropped, list_3, h3, w3, [0, 255, 0])
        cv2.imwrite(self.__dict__["output_dir"] + os.path.split(image_path)[1], img_cropped)
        """

        #4
        pil_image_axis = temp.img_crop(image_path, eval(self.config_ini[self.section]["axis_range"]))
        ocr_result = temp.OCR(pil_image_axis, lang='digits')
        ticks_list = temp.filter_yaxis(ocr_result)

        #5
        y_list, mean = temp.read_pixel(image_path, eval(self.config_ini[self.section]["match_range"]),
                                       eval(self.default_conf["threshold"]))

        y_base = y_list[-2]
        y_min = ticks_list[-1]

        #6
        y1 = [None if list_1[i][1]==None else y_min + (y_base-list_1[i][1])/mean for i in range(len(list_1))]
        y2 = [None if list_2[i][1]==None else y_min + (y_base-list_2[i][1])/mean for i in range(len(list_2))]
        y3 = [None if list_3[i][1]==None else y_min + (y_base-list_3[i][1])/mean for i in range(len(list_3))]

        #7
        dfObj = pd.DataFrame(columns=['year', 'month', 'day', 'value1', 'value2', 'value3'])
        for i in range(7):
            dfObj = dfObj.append({'year': date_list[i].year, 'month': date_list[i].month,
                                  'day': date_list[i].day, 'value1': y1[i], 'value2': y2[i],
                                  'value3': y3[i]}, ignore_index=True)
        #8
        return dfObj


"""
5. Longest Palindromic Substring

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
"""

class Solution:
    def longestPalindrome(self, s: str) -> str:
        if len(s) == 0:
            return ""
        # Each letter could be the center
        winner = s[0]
        for i, l in enumerate(s):
            left = i - 1
            right = i + 1
            best = l
            while (left >= 0) & (right < len(s)):
                if s[left] == s[right]:
                    best = s[left:(right + 1)]
                    left -= 1
                    right += 1
                else:
                    left = -1
            if len(best) > len(winner):
                winner = best
        # A Pair of letters could be the center
        for i in range(0, len(s) - 1):
            if s[i] == s[i+1]:
                left = i - 1
                right = i + 2
                best = s[i:(i+2)]
                while (left >= 0) & (right < len(s)):
                    if s[left] == s[right]:
                        best = s[left:(right + 1)]
                        left -= 1
                        right += 1
                    else:
                        left = -1
                if len(best) > len(winner):
                    winner = best
        return winner

Solution().longestPalindrome(s="adcdddd")


import cv2

def draw_boxes(image, boxes, labels):
    image_h, image_w, _ = image.shape

    for box in boxes:
        xmin = int(box.xmin*image_w)
        ymin = int(box.ymin*image_h)
        xmax = int(box.xmax*image_w)
        ymax = int(box.ymax*image_h)

        cv2.rectangle(image, (xmin,ymin), (xmax,ymax), (0,255,0), 3)
        cv2.putText(image,
                    labels[box.get_label()] + ' ' + str(box.get_score()),
                    (xmin, ymin - 13),
                    cv2.FONT_HERSHEY_SIMPLEX,
                    1e-3 * image_h,
                    (0,255,0), 2)
    return image


from collections import defaultdict

from odoo import _, models

class EmployeeResumeReport(models.AbstractModel):
    _name = 'report.hr_skills.report_employee_cv'
    _description = 'Employee Resume'

    def _get_report_values(self, docids, data=None):
        show_others = (data or {}).get('show_others')
        employees = self.env['hr.employee'].browse(docids)

        resume_lines = {}
        for employee in employees:
            resume_lines[employee] = defaultdict(self.env['hr.resume.line'].browse)
            for line in employee.resume_line_ids:
                if not show_others and not line.line_type_id:
                    continue
                resume_lines[employee][line.line_type_id.name or _('Other')] |= line

        return {
            'doc_ids': docids,
            'doc_model': 'hr.employee',
            'docs': employees,
            'resume_lines': resume_lines,
        }


from fastapi.testclient import TestClient
from sqlalchemy.orm import Session
from fastapi import WebSocketDisconnect

from app.core.config import settings
from app.schemas.user import UserCreate
from app.schemas.customer import CustomerCreate
from app.models.user import RolUser
from app import crud, schemas

def test_ws_boards(
    client: TestClient, db: Session
) -> None:
    # test web socket
    with client.websocket_connect(f"/ws/v1/boards/") as websocket:
        # get msg that client is connected
        websocket.send_json({"type": "RequestAllBoards"})
        data = websocket.receive_json()
        assert data.get("type") == 'SendAllBoards'
        assert len(data.get("data")) > 0

        # change status from board
        websocket.send_json({"type": "RequestUpdateBoard", "data": {"board_id": 1, "status": "available"} })
        # wait for new msg
        data = websocket.receive_json()
        assert "type" in data
        assert "data" in data
        assert data.get("type") == 'SendAllBoards'
        assert len(data.get("data")) > 0

        # change status from another board
        websocket.send_json({"type": "RequestUpdateBoard", "data": {"board_id": 2, "status": "not_available"} })
        data = websocket.receive_json()
        assert "type" in data
        assert "data" in data
        assert data.get("type") == 'SendAllBoards'
        assert len(data.get("data")) > 0


import sqlite3
from sqlite3 import Error
import sys
import numpy as np

def close_connection(conn):
    conn.close()

def create_connection(db_file):
    conn = None
    try:
        conn=sqlite3.connect(db_file)
        cursor = conn.cursor()
        cursor.execute("PRAGMA foreign_keys=ON")
        print("[INFO] Connection established: " + sqlite3.version)

    except Error as e:
        print(e)

    return conn

def query1(conn):
    try:
        parameter = str(sys.argv[2])

        streetDetected = False

        cur = conn.cursor()
        cur.execute("SELECT * FROM Site")
        rows = cur.fetchall()

        for row in rows:
            row_address = row[2].split()
            thisAddress = row_address[1] + ' ' + row_address[2] + ' ' + row_address[3]

            if parameter == thisAddress:
                print('Site Code:', row[0])
                print('Site Type:', row[1])
                print('Site Address:', row[2])
                print('Site Phone:', row[3])

                streetDetected = True

        if streetDetected is False:
            print("No matching street values found.")

    except IndexError as e:
        print("You need to supply a parameter.")

def query2(conn):
    try:
        parameter = str(sys.argv[2])

        cur = conn.cursor()
        cur2 = conn.cursor()

        cur.execute(f"SELECT * FROM DigitalDisplay WHERE schedulerSystem like '{parameter}'")

        rows = cur.fetchall()

        for row in rows:
            cur2.execute(
                f"SELECT name FROM TechnicalSupport WHERE empId IN(SELECT empId FROM Specializes WHERE modelNo like '{row[2]}')"
            )
            namerow = cur2.fetchall()
            name = namerow[0][0]

            print("Serial Number:", row[0])
            print("Scheduler System:", row[1])
            print("Model Number:", row[2])
            print("Employee name:", name)

    except IndexError as e:
        print("You need to supply a parameter.")

def query3(conn):
    cur = conn.cursor()

    allRows = cur.execute(f"SELECT name FROM Salesman").fetchall()
    allNames = sorted([x[0] for x in allRows])
    uniqueNames = np.unique(allNames)

    for thename in uniqueNames:
        if allNames.count(thename) <= 1:
            print(f"{thename}: {allNames.count(thename)}")
        else:
            relevantRows = cur.execute(f"SELECT * FROM Salesman WHERE name LIKE '{thename}'")
            print(f"{thename}: {allNames.count(thename)} {[i for i in relevantRows]}")

def query4(conn):
    try:
        parameter = str(sys.argv[2])

        cur = conn.cursor()

        allRows = cur.execute(f"SELECT name FROM Client WHERE phone LIKE '{parameter}'").fetchall()

        print(f"Clients with phone number {parameter}: ")
        for row in allRows:
            print(row[0])

    except IndexError as e:
        print("You need to supply a parameter.")
def query5(conn):
     try:
        cursor = conn.cursor()
        cursor.execute("""
            SELECT Administrator.empId, Administrator.name, SUM(AdmWorkHours.hours) AS total_working_hours
            FROM Administrator
            JOIN AdmWorkHours ON Administrator.empId = AdmWorkHours.empId
            GROUP BY Administrator.empId
            ORDER BY total_working_hours ASC
            """)
        rows = cursor.fetchall()
        for row in rows:
            print(row[0], row[1], row[2])
     except Error as e:
        print(e)
def query6(conn):
    try:
        cursor = conn.cursor()
        model_no = sys.argv[2]
        cursor.execute("""
            SELECT TechnicalSupport.name
            FROM TechnicalSupport
            JOIN Specializes ON TechnicalSupport.empId = Specializes.empId
            WHERE Specializes.modelNo = ?
            """, (model_no,))
        rows = cursor.fetchall()
        for row in rows:
            print(row[0])
    except IndexError as e:
       print("You need to supply a parameter.")
def query7(conn):
    try:
        cursor = conn.cursor()
        cursor.execute("""
            SELECT Salesman.name, AVG(Purchases.commissionRate) AS avg_commission_rate
            FROM Salesman
            JOIN Purchases ON Salesman.empId = Purchases.empId
            GROUP BY Salesman.empId
            ORDER BY avg_commission_rate DESC
            """)
        rows = cursor.fetchall()
        max_len_name = max(len(row[0]) for row in rows)
        max_len_rate = max(len(str(row[1])) for row in rows)
        print(f"{'Name'.ljust(max_len_name)} {'Average Commission Rate'}")
        print("------------------".ljust(max_len_name + max_len_rate + 1, "-"))
        for row in rows:
            print(f"{row[0].ljust(max_len_name)} {row[1]:.2f}")
    except Error as e:
       print(e)
def query8(conn):
    try:
        cursor = conn.cursor()
        cursor.execute("""
            SELECT 'Administrator' AS Role, COUNT(*) AS cnt FROM Administrator
            UNION
            SELECT 'Salesmen' AS Role, COUNT(*) AS cnt FROM Salesman
            UNION
            SELECT 'Technicians' AS Role, COUNT(*) AS cnt FROM TechnicalSupport
            """)
        rows = cursor.fetchall()
        max_len = max(len(row[0]) for row in rows)
        print(f"{'Role'.ljust(max_len)} {'cnt'}")
        print("------------------")
        for row in rows:
            print(f"{row[0].ljust(max_len)} {row[1]}")
    except Error as e:
       print(e)

def main():

    database = r"ABC.sqlite"

    conn = create_connection(database)
    if conn is not None:

        queryNumber = int(sys.argv[1])

        if queryNumber == 1:
            query1(conn)
        elif queryNumber == 2:
            query2(conn)
        elif queryNumber == 3:
            query3(conn)
        elif queryNumber == 4:
            query4(conn)
        elif queryNumber == 5:
            query5(conn)
        elif queryNumber==6:
            query6(conn)
        elif queryNumber==7:
            query7(conn)
        elif queryNumber==8:
            query8(conn)
        close_connection(conn)

    else:
        print("Something went wrong with the database connection.")

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    main()


# Import the streamlit library
import streamlit as st

# Import the numpy and skimage libraries
import numpy as np
from skimage import io

# Cache the results of the function for 300 seconds
@st.cache_data(ttl=300)
# Define a function called "plot"
def plot():
    # Read in an image from the specified URL using skimage
    vol = io.imread("https://s3.amazonaws.com/assets.datacamp.com/blog_assets/attention-mri.tif")
    # Transpose the image to create a 3D volume
    volume = vol.T
    # Get the dimensions of the volume
    r, c = volume[0].shape

    # Import the plotly.graph_objects library and define the number of frames
    import plotly.graph_objects as go
    nb_frames = 68

    # Create a new figure with frames
    fig = go.Figure(frames=[go.Frame(data=go.Surface(
        z=(6.7 - k * 0.1) * np.ones((r, c)),
        surfacecolor=np.flipud(volume[67 - k]),
        cmin=0, cmax=200
        ),
        name=str(k) # you need to name the frame for the animation to behave properly
        )
        for k in range(nb_frames)])

    # Add data to be displayed before animation starts
    fig.add_trace(go.Surface(
        z=6.7 * np.ones((r, c)),
        surfacecolor=np.flipud(volume[67]),
        colorscale='Gray',
        cmin=0, cmax=200,
        colorbar=dict(thickness=20, ticklen=4)
        ))

    # Define a function to set the frame arguments for the animation
    st.cache_data(ttl=300)
    def frame_args(duration):
        return {
                "frame": {"duration": duration},
                "mode": "immediate",
                "fromcurrent": True,
                "transition": {"duration": duration, "easing": "linear"},
            }

    # Create a slider for the animation
    sliders = [
                {
                    "pad": {"b": 10, "t": 60},
                    "len": 0.9,
                    "x": 0.1,
                    "y": 0,
                    "steps": [
                        {
                            "args": [[f.name], frame_args(0)],
                            "label": str(k),
                            "method": "animate",
                        }
                        for k, f in enumerate(fig.frames)
                    ],
                }
            ]

    # Define the layout of the plot
    fig.update_layout(
            title='Slices in volumetric data',
            width=600,
            height=600,
            scene=dict(
                        zaxis=dict(range=[-0.1, 6.8], autorange=False),
                        aspectratio=dict(x=1, y=1, z=1),
                        ),
            updatemenus = [
                {
                    "buttons": [
                        {
                            "args": [None, frame_args(50)],
                            "label": "▶", # play symbol
                            "method": "animate",
                        },
                        {
                            "args": [[None], frame_args(0)],
                            "label": "◼", # pause symbol
                            "method": "animate",
                        },
                    ],
                    "direction": "left",
                    "pad": {"r": 10, "t": 70},
                    "type": "buttons",
                    "x": 0.1,
                    "y": 0,
                }
            ],
            sliders=sliders
    )

    # Display the plot
    st.plotly_chart(fig, use_container_width=True)


#!/usr/bin/env python
# -*- encoding: utf8 -*-
'''Input: short_abstracts_en.nt.bz2
'''

import re
import json
import bz2
import fileinput

__author__ = 'noahsark'

types = set(['Place'])

results = []
for line in fileinput.input():
    pat = re.compile("<([^<>]*)> <([^<>]*)> <http://dbpedia.org/ontology/(.*)> .")
    match = pat.match(line)
    if match is not None:
        if match.group(3) in types:
            uri = match.group(1).decode('unicode_escape')
            results.append(uri)

with open('id_list.json', 'w') as fp:
    fp.write(json.dumps(results, indent=4, ensure_ascii=False).encode('utf8'))


__author__ = "Vishvendra Singh"
__version__ = 0.1

import os
import sys
import time

from bs4 import BeautifulSoup
import requests

import progressbar

try:
    import cPickle as pickle
except ImportError:
    import pickle

# global constants
CONFIG_FILE = ".img_dl_links.p"
DOWNLOAD_PATH = ".dl_path.txt"
BASE_URL = "http://simpledesktops.com"

def image_links_from_page(htmlsource):
    """returns images links and address of next page"""
    img_links = []
    soup = BeautifulSoup(htmlsource)
    div_src = soup.find_all("div", {"class": "desktop"})
    for each_div in div_src:
        soup2 = BeautifulSoup(str(each_div))
        img_links.append(soup2.find("img")["src"])

    img_links = extract_img_url(img_links)

    next_link = soup.find("a", {"class": "more"})
    if next_link:
        return img_links, next_link["href"]
    else:
        return img_links, None

def extract_img_url(img_links):
    tmp = []
    for each_link in img_links:
        if "jpg" in each_link:
            tmp.append(each_link[0:each_link.find("jpg") + 3])
        elif "png" in each_link:
            tmp.append(each_link[0: each_link.find("png") + 3])
    return tmp

def file_exist_status(file_name):
    """check existence of a file. used for checking status of config file"""
    if os.path.isfile(file_name) and os.access(file_name, os.R_OK):
        return True
    else:
        return False

def get_all_image_links():
    """retrieves all image links of images available on simpledesktops.com"""
    pbar = progressbar.AnimatedProgressBar(end=47, width=50)
    print("getting all links for image urls")
    pbar.show_progress()
    img_dl_links = []
    next_link = "/browse/1/"
    while next_link:
        fh = requests.get("%s%s" % (BASE_URL, next_link), allow_redirects=False)
        # print fh.url
        result = image_links_from_page(fh.text)
        fh.close()
        img_dl_links.append(result[0])
        next_link = result[1]
        pbar += 1
        pbar.show_progress()
        # print next_link
    # flattening list of links
    print()
    tmp = []
    for x in img_dl_links:
        for y in x:
            tmp.append(y)
    img_dl_links = tmp
    return img_dl_links

def query():
    """Prompts user for a yes/no answer
    if user responds with 'yes', 'ye', or 'y', return True
    if user responds with 'no' or 'n', return False.
    else: return None
    """

    # raw_input returns the empty string for "enter"
    yes = {'yes', 'y', 'ye', ''}
    no = {'no', 'n'}

    choice = input().lower()
    if choice in yes:
        return True
    elif choice in no:
        return False
    else:
        sys.stdout.write("Please respond with 'yes' or 'no'")

def get_dl_path():
    # TODO: Find a way to check input. If using Linux, using something like '~/Wallpapers' doesn't work, but
    # Explicitly using '/home/user/Wallpapers' does work.
    """
    Grabs download path from user, and creates it if it doesn't exist.
    :return: Download path
    """
    PATH = input("input path to save files: ")
    if os.path.isdir(PATH) and os.access(PATH, os.W_OK):
        print("given path accepted")
    else:
        print("path is not valid. Create now? (Y/n): ")
        answer = query()
        if answer:
            os.makedirs(PATH)
        else:
            get_dl_path()
    print(PATH)
    return PATH

def download_images(img_dl_links, PATH):
    """download images and remove downloaded images from img_links"""
    end = len(img_dl_links)
    pbar = progressbar.AnimatedProgressBar(end=end, width=50)
    print('downloading wallpapers..')
    pbar.show_progress()
    for img_url in img_dl_links:
        tmp = requests.request('get', img_url)
        with open(os.path.join(PATH, img_url.split('/')[-1]), 'w') as f:
            f.write(str(tmp.content))
        update_config_file(img_url)
        time.sleep(1)
        pbar += 1
        pbar.show_progress()
    print()

def update_config_file(img_link):
    """remove downloaded links from config file"""
    img_dl_links = pickle.load(open(CONFIG_FILE, "rb"))
    img_dl_links.pop(img_dl_links.index(img_link))
    pickle.dump(img_dl_links, open(CONFIG_FILE, "wb"))

def main():
    if file_exist_status(CONFIG_FILE) and file_exist_status(DOWNLOAD_PATH):
        img_dl_links = pickle.load(open(CONFIG_FILE, "rb"))
        PATH = ""
        with open(DOWNLOAD_PATH) as f:
            PATH = f.read()
        print("[resuming wallpaper download]")
        print("[to stop downloading press Ctrl+z]")
        download_images(img_dl_links, PATH)
        print("wallpapers download complete")
    else:
        PATH = get_dl_path()
        with open(DOWNLOAD_PATH, "w") as f:
            f.write(PATH)
        img_dl_links = get_all_image_links()
        pickle.dump(img_dl_links, open(CONFIG_FILE, "wb"))
        print("[to stop downloading press Ctrl+z]")
        download_images(img_dl_links, PATH)
        print("wallpapers download complete")

if __name__ == "__main__":
    main()


# -*- coding:utf-8 -*-
import os
import re
import json
import urllib.request
import urllib.parse
from bs4 import BeautifulSoup
from tqdm import tqdm
import threading
import requests
import time
import queue

def get_html(url):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                             'Chrome/51.0.2704.63 Safari/537.36'}
    req = urllib.request.Request(url=url, headers=headers)
    res = urllib.request.urlopen(req)
    html = res.read().decode('gbk', errors='ignore')
    return html

def fqa_detail_parser(url):
    html = get_html(url)
    soup = BeautifulSoup(html, 'html.parser')
    faq_info_tag = soup.find(attrs={"class": "fl w880"})
    faq_info_tag.prettify()
    qdetail = faq_info_tag.find(attrs={"class": "details-con clearfix"})
    qdetail = qdetail.get_text().strip()

    adetail_list = []
    ans_list = faq_info_tag.find_all(attrs={"class": "replay-content-box clearfix"})
    for ans_content in ans_list:
        adetail_list.append(str(ans_content.get_text().strip()))  # Tag -> str 格式
    return {'qdetail': str(qdetail), 'adetail_list': adetail_list}

def main_parser(html):
    soup = BeautifulSoup(html, 'html.parser')
    symptom = soup.find(attrs={"class": "jb-name fYaHei gre"})
    symptom_name = symptom.get_text()

    jieshao = soup.find(name='div', attrs={"class": "zz-articl fr f14"})
    jieshao_value = jieshao.get_text()
    jieshao_value = [jv.strip() for jv in jieshao_value.split('\n') if len(jv) > 10]
    jieshao_value = jieshao_value[0]

    xiangguan_zz_tag = soup.find(attrs={"class": "other-zz mt10"})
    xiangguan_zz_list = [xg_sym.get_text() for xg_sym in xiangguan_zz_tag.find_all('li')]

    faq_tag_list = soup.find_all(attrs={"class": "fl replay-note"})
    question_info_list = []
    ques_text_set = set()
    for faq in faq_tag_list:
        faq = faq.find('a')
        href = faq.get('href')
        ques_text = faq.get_text()
        if ques_text in ques_text_set:
            continue
        fqa_detail = fqa_detail_parser(href)
        question_info_list.append(
            {
                'text': ques_text,
                'href': href,
                'fqa_detail': fqa_detail
            }
        )
        ques_text_set.add(ques_text)

    return symptom_name, jieshao_value, xiangguan_zz_list, question_info_list

def attributes_parser(html):
    soup = BeautifulSoup(html, 'html.parser')
    attribute_tag = soup.find(name='div', attrs={"class": "zz-articl fr f14"})
    attribute = attribute_tag.get_text()
    attribute = re.sub(r'[\r\t]+', '\n', attribute)
    attribute = re.sub(r'[\n]+', '\n', attribute)
    return attribute.strip()

def food_parser(html):
    food_infos = {}
    soup = BeautifulSoup(html, 'html.parser')
    match_food_tag = soup.find(attrs={"class": "diet-item clearfix"})
    match_food = match_food_tag.find(attrs={"class": "fl diet-good-txt"})
    food_infos['match_food'] = {
        'describe': match_food.get_text(),
        'food_list': []
    }
    match_food_list = match_food_tag.find_all(attrs={"class": "diet-imgbox pr bor fl mr10"})
    for food in match_food_list:
        food_name = food.find(attrs={"class": "diet-opac-txt pa f12"})
        food_name = food_name.get_text()
        food_url = food.a.get('href')
        food_infos['match_food']['food_list'].append({'name': food_name, 'url': food_url})

    not_match_food_tag = soup.find(attrs={"class": "diet-item none"})
    not_match_food = not_match_food_tag.find(attrs={"class": "fl diet-good-txt"})
    food_infos['not_match_food'] = {
        'describe': not_match_food.get_text(),
        'food_list': []
    }
    not_match_food_list = not_match_food_tag.find_all(attrs={"class": "diet-imgbox pr bor fl mr10"})
    for food in not_match_food_list:
        food_name = food.find(attrs={"class": "diet-opac-txt pa f12"})
        food_name = food_name.get_text()
        food_url = food.a.get('href')
        food_infos['not_match_food']['food_list'].append({'name': food_name, 'url': food_url})
    return food_infos

def dump_json(symptom_tuple, all_attr_value_list, food_infos):
    symptom_name, jieshao_value, xiangguan_zz_list, question_info_list = symptom_tuple
    data_format = {
        "name": symptom_name,
        "jieshao": jieshao_value,
        "xiangguan_zz": xiangguan_zz_list,
        "question_infos": question_info_list,
        "food": food_infos
    }
    for item in all_attr_value_list:
        # print(item)
        data_format.update(item)

    # print(json.dumps(data_format, sort_keys=True, indent=4, separators=(', ', ': '),ensure_ascii=False))

    with open('symptom_data.json', 'a+', encoding='utf8') as f:
        f.write(json.dumps(data_format, ensure_ascii=False))
        f.write('\n')

# 为线程定义一个函数
class myThread(threading.Thread):
    def __init__(self, name, q):
        threading.Thread.__init__(self)
        self.name = name
        self.q = q

    def run(self):
        print("Starting " + self.name)
        while True:
            try:
                crawl(self.name, self.q)
            except:
                break
        print("Exiting " + self.name)

def crawl(q):
    base_url = 'https://zzk.xywy.com/{page}_{attr}.html'
    attri_list = ['yuanyin', 'yufang', 'jiancha', 'zhenduan']
    page = q.get(timeout=2)
    try:
        all_attr_value_list = []
        for attr in attri_list:
            url = base_url.format(page=str(page), attr=attr)
            # print(url)
            html = get_html(url)
            attr_value = attributes_parser(html)
            all_attr_value_list.append({attr: attr_value})

        url = base_url.format(page=str(page), attr='jieshao')
        html = get_html(url)
        symptom_tuple = main_parser(html)

        url = base_url.format(page=str(page), attr='food')
        html = get_html(url)
        food_infos = food_parser(html)

        dump_json(symptom_tuple, all_attr_value_list, food_infos)
    except Exception as e:
        print(e)
        print(url)

def run(num_page):
    URLs = [page for page in range(78, num_page)]

    # 填充队列
    workQueue = queue.Queue(len(URLs))
    for url in URLs:
        workQueue.put(url)

    threads = []
    # 多线程爬取
    for i in range(1, 161):
        # 创建160个新线程
        thread = myThread("Thread-" + str(i), q=workQueue)
        # 开启新线程
        thread.start()
        # 添加新线程到线程列表
        threads.append(thread)

    # 等待所有线程完成
    for thread in threads:
        thread.join()

if __name__ == '__main__':
    run(11000)


from model.contact import Contact
import random

def test_delete_some_contact(app, db, check_ui):
    if len(db.get_contact_list()) == 0:
        app.contact.create(Contact(firstname="testFirstName", middlename="testMiddleName", lastname="testLastName",
                                   nickname="testNickName", title="testTitle", company="testCompany",
                                   address="testAddress",
                                   homephone="testHomeTelephone", mobilephone="testMobileTelephone",
                                   workphone="testWorkTelephone", fax="testFax", email="testE-mail",
                                   email2="testE-Mail2", email3="testE-mail3",
                                   homepage="testHomepage", bday="20", bmonth="May", byear="1992", aday="20",
                                   amonth="May", ayear="1998", secondaryaddress="testSecondaryAddress",
                                   secondary_home="testSecondaryHome", secondarynotes="testSecondaryNotes"))
    old_contacts = db.get_contact_list()
    contact = random.choice(old_contacts)
    app.contact.delete_contact_by_id(contact.id)
    assert len(old_contacts) - 1 == app.contact.count()
    new_contacts = db.get_contact_list()
    old_contacts.remove(contact)
    assert old_contacts == new_contacts
    if check_ui:
        assert sorted(old_contacts, key = Contact.id_or_max) == sorted(app.contact.get_contact_list(), key = Contact.id_or_max)


import numpy as np
import units
from scipy import interpolate

class Thermalization(object):

    def __init__(self,therm_eff_model):
        if (therm_eff_model=='BKWM'):
            self.therm_efficiency = BKWM_therm_efficiency
        # 2-D
            x = [np.log10(1.e-3),np.log10(5e-3),np.log10(1e-2),np.log10(5e-2)]
            y = [0.1,0.2,0.3]
            a = [[2.01,0.81,0.56,0.27],[4.52,1.90,1.31,0.55],[8.16,3.20,2.19,0.95]]
            b = [[0.28,0.19,0.17,0.10],[0.62,0.28,0.21,0.13],[1.19,0.45,0.31,0.15]]
            d = [[1.12,0.86,0.74,0.60],[1.39,1.21,1.13,0.90],[1.52,1.39,1.32,1.13]]
        # define the interpolation functions
            self.fa = interpolate.interp2d(x, y, a, kind='linear')
            self.fb = interpolate.interp2d(x, y, b, kind='linear')
            self.fd = interpolate.interp2d(x, y, d, kind='linear')
        elif (therm_eff_model=='BKWM_1d'):
            self.therm_efficiency = BKWM_1d_therm_efficiency
        # 1-D
        # Uses a single variable [m_ej/(v_ej^2)] instead of using m_ej and v_ej separately.
            x_barnes = [0.011,0.025,0.0556,0.1,0.111,0.125,0.25,0.5,0.5556,1.,1.25,5.]
            a_barnes = [8.16,4.52,3.20,2.01,2.19,1.90,1.31,0.81,0.95,0.56,0.55,0.27]
            b_barnes = [1.19,0.62,0.45,0.28,0.31,0.28,0.21,0.19,0.15,0.17,0.13,0.10]
            d_barnes = [1.52,1.39,1.39,1.12,1.32,1.21,1.13,0.86,1.13,0.74,0.90,0.60]
        # define the interpolation functions
            self.fa_1d=interpolate.interp1d(x_barnes,a_barnes,bounds_error=False,fill_value=(a_barnes[0],a_barnes[-1]))
            self.fb_1d=interpolate.interp1d(x_barnes,b_barnes,bounds_error=False,fill_value=(b_barnes[0],b_barnes[-1]))
            self.fd_1d=interpolate.interp1d(x_barnes,d_barnes,bounds_error=False,fill_value=(d_barnes[0],d_barnes[-1]))
        elif (therm_eff_model=='cnst'):
            self.therm_efficiency = cnst_therm_efficiency
        else:
            print('Unknown thermal efficiency model\n')
            exit(-1)

    def __call__(self, **kwargs):
        return self.therm_efficiency(self, **kwargs)

    def therm_efficiency_params(self, m,omega,v):
        m_iso = units.fourpi/omega * m
        # assign the values of the mass and velocity
        xnew=np.log10(m_iso)   #mass     [Msun]
        ynew=v                 #velocity [c]
        # compute the parameters by linear interpolation in the table
        return (self.fa(xnew,ynew),self.fb(xnew,ynew),self.fd(xnew,ynew))

    def therm_efficiency_params_1d(self, m,omega,v):
        m_iso = units.fourpi/omega * m
        # assign the value of x=m/v^2
        xnew = m_iso/(v*v)
        # compute the parameters by 1-d interpolation
        return (self.fa_1d(xnew),self.fb_1d(xnew),self.fd_1d(xnew))

def BKWM_therm_efficiency(cls, **kwargs):
    t = kwargs['time_sec']   # s
    m = kwargs['mass_ej']    # m_sun
    omega = kwargs['omega']  # rad
    v = kwargs['vel']        # c
    if any( [t is None, m is None, omega is None, v is None] ):
        print('Error. For the heating efficiency, user must specify time, mass, angle and velocity\n')
        exit(-1)
    coeff=cls.therm_efficiency_params(m,omega,v)
    time_days=t*units.sec2day
    if(np.isscalar(time_days)):
        tmp = float(2.*coeff[1]*time_days**coeff[2])
        return float(0.36*(np.exp(-coeff[0]*time_days) + np.log(1.+tmp)/tmp ))
    else:
        tmp = (2.*coeff[1]*time_days**coeff[2])
        return (0.36*(np.exp(-coeff[0]*time_days) + np.log(1.+tmp)/tmp ))

def BKWM_1d_therm_efficiency(cls, **kwargs):
    t = kwargs['time_sec']   # s
    m = kwargs['mass_ej']    # m_sun
    omega = kwargs['omega']  # rad
    v = kwargs['vel']        # c
    if any( [t is None, m is None, omega is None, v is None] ):
        print('Error. For the heating efficiency, user must specify time, mass, angle and velocity\n')
        exit(-1)
    coeff=cls.therm_efficiency_params_1d(m,omega,v)
    time_days=t*units.sec2day
    if(np.isscalar(time_days)):
        tmp = float(2.*coeff[1]*time_days**coeff[2])
        return float(0.36*(np.exp(-coeff[0]*time_days) + np.log(1.+tmp)/tmp ))
    else:
        tmp = (2.*coeff[1]*time_days**coeff[2])
        return (0.36*(np.exp(-coeff[0]*time_days) + np.log(1.+tmp)/tmp ))

def cnst_therm_efficiency(cls,  **kwargs):
    thef = kwargs['cnst_eff']
    if (thef is None):
        print('Error, missing constant for the thermal efficiency\n')
        exit(-1)
    if (thef < 0. or thef > 1.):
            print('Error, fixed heating efficiency must be between 0 and 1\n')
            exit(-1)
    return thef

if __name__=="__main__":
    M = Thermalization("cnst")
    x1 = M(cnst_eff=0.333)
    print(x1)
    M = Thermalization("BKWM")
    x1 = M(time_sec=2e5,mass_ej=0.1,omega=0.3,vel=0.02,cnst_eff=0.333)
    print(x1)


def isPrime(num):
    cnt = 0
    if num == 1:
        return False
    for k in range(1, num+1):
        if num % k == 0:
            cnt += 1
    if cnt == 2:
        return True
    else:
        return False

n = int(input())
cnt = 0
nums = input().split()
for j in nums:
    if isPrime(int(j)):
        cnt += 1
print(cnt)

기본 수학 2/1978번 소수 찾기.py


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def swapNodes(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
        begin = head
        for i in range(k - 1):
            begin = begin.next

        forw = begin
        end = head

        while forw.next:
            forw = forw.next
            end = end.next

        temp = begin.val
        begin.val = end.val
        end.val = temp

        return head


#! /usr/bin/python3

import copy
import pdb
import time

import infra.penscapy.penscapy as penscapy
import infra.common.defs as defs
import infra.common.objects as objects
import infra.common.utils as utils
import infra.engine.comparators as comparators

from infra.factory.store    import FactoryStore
from infra.common.glopts    import GlobalOptions
from infra.common.logging   import logger as logger
from infra.asic.model       import ModelConnector

RTL_RETRY_SCALE     = 20
MAX_RETRIES         = 10
MAX_DROP_RETRIES    = 1

class VerifEngineObject:
    def __init__(self):
        pass

    def _verify(self, step, tc):
        return defs.status.SUCCESS

    def Verify(self, step, tc):
        if GlobalOptions.skipverify:
            logger.info("Run with skipverify=True: SKIPPING VERIFICATION")
            return defs.status.SUCCESS

class DolVerifEngineObject(VerifEngineObject):
    def __init__(self):
        super().__init__()
        # Pending testcase database
        self.ptcdb = objects.ObjectDatabase()
        # Completed testcase database
        self.ctcdb = objects.ObjectDatabase()
        return

    def __compare_pktbuffers(self, epktbuf, apktbuf, tc):
        if GlobalOptions.dryrun:
            return True

        if epktbuf is None:
            logger.info("Packet Compare: Expected Buffer is None. Skipping")
            return True

        if apktbuf is None:
            logger.error("Packet Compare: ExpType:%s, ActType:%s" %\
                      (type(epktbuf), type(apktbuf)))
            return False

        # Make sure we are not accidentally comparing the same object
        if id(apktbuf) == id(epktbuf):
            logger.error("ExpBuf and ActBuf are same objects.")
            return False

        logger.verbose("Comparing Packets")
        pcr = comparators.PacketComparator(tc.GetIgnorePacketFields())
        pcr.AddExpected(epktbuf, None, 1)
        pcr.AddReceived(apktbuf, None)
        pcr.Compare()
        pcr.ShowResults()
        return pcr.IsMatch()

    def __verify_pktbuffers(self, epktbuf, apktbuf, tc):
        match = self.__compare_pktbuffers(epktbuf, apktbuf, tc)
        if match is False:
            logger.error("PacketBuffer Compare Result = Mismatch")
            return defs.status.ERROR
        logger.info("PacketBuffer Compare Result = Match")
        return defs.status.SUCCESS

    def __compare_buffers(self, ebuf, abuf, tc):
        if GlobalOptions.dryrun:
            return True

        if ebuf is None or abuf is None:
            logger.error("Buffer Compare: ExpType:%s, ActType:%s" %\
                      (type(ebuf), type(abuf)))
            return False

        logger.info("Comparing Buffers: %s <--> %s " % (ebuf.GID(), abuf.GID()))
        # Make sure we are not accidentally comparing the same object
        if id(ebuf) == id(abuf):
            logger.error("ExpBuf and ActBuf are same objects.")
            return False

        return ebuf == abuf

    def __get_pktbuffer(self, buf, tc):
        if buf is not None:
            return buf.Read()

        if GlobalOptions.dryrun is False:
            logger.error("Trying to GetBuffer() on a None descriptor.")
            assert(0)
        return None

    def __verify_buffers(self, ebuf, abuf, tc):
        match = self.__compare_buffers(ebuf, abuf, tc)
        if match is False:
            logger.error("Buffer compare result = Mismatch")
            return defs.status.ERROR
        logger.info("Buffer compare result = Match")

        # If this buffer is not a packet, then stop.
        if ebuf.IsPacket() is False:
            logger.info("Buffer is not a packet. Skipping PktCompare.")
            return defs.status.SUCCESS

        epktbuf = self.__get_pktbuffer(ebuf, tc)
        apktbuf = self.__get_pktbuffer(abuf, tc)
        return self.__verify_pktbuffers(epktbuf, apktbuf, tc)

    def __get_buffer(self, descr, tc):
        if descr is not None:
            return utils.SafeFnCall(None, logger, descr.GetBuffer)

        if GlobalOptions.dryrun is False:
            logger.error("Trying to GetBuffer() on a None descriptor.")
            assert(0)
        return None

    def __compare_descriptors(self, edescr, adescr, tc):
        if GlobalOptions.dryrun:
            return True

        egid = None
        if edescr is not None:
            egid = edescr.GID()
        agid = None
        if adescr is not None:
            agid = adescr.GID()

        logger.info("Comparing Descriptors: %s <--> %s " % (egid, agid))
        return edescr == adescr

    def __verify_one_descriptor(self, edescr, adescr, negtest, tc):
        if negtest:
            edescr = None
        match = self.__compare_descriptors(edescr, adescr, tc)
        if match is False:
            logger.error("Descriptor compare result = MisMatch")
            return defs.status.ERROR
        logger.info("Descriptor compare result = Match")
        if negtest:
            return defs.status.SUCCESS
        ebuf = self.__get_buffer(edescr, tc)
        abuf = self.__get_buffer(adescr, tc)
        if ebuf is None:
            logger.info("Expected buffer is None = Buffer Verification skipped!")
            return defs.status.SUCCESS
        return self.__verify_buffers(ebuf, abuf, tc)

    def __retry_wait(self, tc):
        if GlobalOptions.dryrun:
            return
        logger.info("Retry wait.........")
        if GlobalOptions.rtl:
            time.sleep(1*RTL_RETRY_SCALE)
        else:
            time.sleep(1)
        return

    def __get_scaled_retry(self, value):
        if GlobalOptions.rtl:
            return value * RTL_RETRY_SCALE
        return value

    def __get_num_retries(self, tc, rxcount):
        if rxcount == 0:
            return self.__get_scaled_retry(MAX_DROP_RETRIES)
        return self.__get_scaled_retry(MAX_RETRIES)

    def __is_retry_enabled(self, tc):
        if tc.IsRetryEnabled() == False and GlobalOptions.rtl == False:
            return False
        return True

    def __consume_descriptor(self, edescr, negtest, ring, tc):
        if GlobalOptions.dryrun:
            return None

        adescr = copy.copy(edescr)

        max_retries = self.__get_num_retries(tc, None)
        if negtest and max_retries > 2:
            max_retries = 1

        status = ring.Consume(adescr)
        for r in range(max_retries):
            if status != defs.status.RETRY: break
            self.__retry_wait(tc)
            status = ring.Consume(adescr)

        # return None is treated as SUCCESS
        if status is not None and status != defs.status.SUCCESS:
            return None

        return adescr

    def __verify_descriptors(self, step, tc):
        final_status = defs.status.SUCCESS
        for dsp in step.expect.descriptors:
            negtest_obj = dsp.descriptor.negtest
            if objects.IsCallback(negtest_obj):
                negtest = negtest_obj.call(tc)
            else:
                negtest = negtest_obj
            edescr = dsp.descriptor.object
            adescr = self.__consume_descriptor(edescr, negtest,
                                               dsp.descriptor.ring,
                                               tc)
            status = self.__verify_one_descriptor(edescr, adescr, negtest, tc)
            if status == defs.status.ERROR:
                final_status = status
        return final_status

    def __verify_configs(self, step, tc):
        for config in step.expect.configs:
            if config.method is not None:
                method = getattr(config.actual_object, config.method)
                if not method:
                    assert 0
                if not method(config.spec):
                    logger.error("Config object verify result = Failed")
                    return defs.status.ERROR
            if not config.original_object.Equals(config.actual_object, tc):
                logger.error("Config object compare result = MisMatch")
                return defs.status.ERROR
        return defs.status.SUCCESS

    def __receive_packets(self, pcr, step, tc):
        max_retries = self.__get_num_retries(tc, pcr.GetExPacketCount())
        for r in range(max_retries):
            mpkts = ModelConnector.Receive()
            for mpkt in mpkts:
                pcr.AddReceived(mpkt.rawpkt, [ mpkt.port ])
            if pcr.GetExPacketCount() == 0:
                #logger.info("0 Packets expected: Waiting for Excess packets")
                break
            else:
                if pcr.GetRxPacketCount() >= pcr.GetExPacketCount():
                    break
                logger.info("RETRY required: ExPktCount:%d RxPktCount:%d" %\
                        (pcr.GetExPacketCount(), pcr.GetRxPacketCount()))

            if self.__is_retry_enabled(tc) == False:
                break
            self.__retry_wait(tc)
        return

    def __add_expected(self, pcr, step, tc):
        for psp in step.expect.packets:
            pkt = psp.packet
            ports = psp.ports
            pcr.AddExpected(pkt.rawbytes, ports, pkt.GID())
        return

    def __add_dummy_rx(self, pcr, step, tc):
        for psp in step.expect.packets:
            pkt = psp.packet
            ports = psp.ports
            pcr.AddReceived(pkt.rawbytes, ports)
        return

    def __verify_packets(self, step, tc):
        pcr = comparators.PacketComparator(tc.GetIgnorePacketFields())
        # Add Expected
        self.__add_expected(pcr, step, tc)
        # Add RX
        if GlobalOptions.dryrun:
            self.__add_dummy_rx(pcr, step, tc)
        else:
            self.__receive_packets(pcr, step, tc)
        tc.rxpkts = copy.deepcopy(pcr.rxpkts)
        pcr.Compare()
        pcr.ShowResults()
        if pcr.IsMatch() == False:
            #if getattr(step.expect, "ignore_excess_packets", False) and \
            #    pcr.HasOnlyExcessPackets():
            #    return defs.status.SUCCESS
            return defs.status.ERROR
        return defs.status.SUCCESS

    def _verify(self, step, tc):
        verify_pass = True

        pstatus = self.__verify_packets(step, tc)
        if pstatus == defs.status.ERROR:
            verify_pass = False

        dstatus = self.__verify_descriptors(step, tc)
        if dstatus == defs.status.ERROR:
            verify_pass = False

        cstatus = self.__verify_configs(step, tc)
        if cstatus == defs.status.ERROR:
            verify_pass = False

        if verify_pass == False:
            return defs.status.ERROR

        return defs.status.SUCCESS

    def __verify_delay(self, step, tc):
        if GlobalOptions.dryrun:
            return
        if step.expect.delay:
            final_delay = step.expect.delay
            if GlobalOptions.rtl:
                final_delay = step.expect.delay * RTL_RETRY_SCALE
            logger.info("Expectation Delay: %d" % final_delay)
            time.sleep(final_delay)
        return

    def Verify(self, step, tc):
        if GlobalOptions.skipverify:
            logger.info("Run with skipverify=True: SKIPPING VERIFICATION")
            return defs.status.SUCCESS
        self.__verify_delay(step, tc)
        return self._verify(step, tc)

class E2EVerifEngineObject(VerifEngineObject):

    def __init__(self):
        super().__init__()

    def __verify_commands(self, step, tc):
        all_status = []
        for cmd in step.expect.commands:
            cmd_status = defs.status.SUCCESS
            if not cmd.status:
                logger.error("Command Failed %s" % cmd.command)
                cmd_status = defs.status.SUCCESS if cmd.ignore_error else defs.status.ERROR
            else:
                logger.info("Command Success %s" % cmd.command)
            all_status.append(cmd_status)

        if any(status == defs.status.ERROR for status in all_status):
            return defs.status.ERROR
        else:
             return defs.status.SUCCESS

    def _verify(self, step, tc):
        verify_pass = True

        cstatus = self.__verify_commands(step, tc)
        if cstatus == defs.status.ERROR:
           verify_pass = False

        if verify_pass == False:
            return defs.status.ERROR

        return defs.status.SUCCESS

    def Verify(self, step, tc):
        if GlobalOptions.skipverify:
            logger.info("Run with skipverify=True: SKIPPING VERIFICATION")
            return defs.status.SUCCESS
        return self._verify(step, tc)

DolVerifEngine = DolVerifEngineObject()
E2EVerifEngine = E2EVerifEngineObject()


import base64

def post_process(message):
    if message.get('meta') is None or message['meta'].get('raw') is None:
        print(f'[EM500]A message have no meta.raw from Group ID:{message["grpid"]}, Node ID:{message["nid"]}')
        return message

    raw = base64.b64decode(message['meta']['raw'])
    print(f'[EM500]Group ID:{message["grpid"]}, Node ID:{message["nid"]}, type:{message["device"]["type"]}, desc.:{message["device"]["desc"]}, data:{message["data"]}, raw:{raw}')

    index = 0

    while index < len(raw):
        if raw[index + 1] == 0x67:
            message['data'][f'CH{raw[index]}_Temperature_degC'] = int.from_bytes(raw[index + 2:index + 4], 'little', signed=True) * 0.1
            index += 4
        elif raw[index + 1] == 0x68:
            message['data'][f'CH{raw[index]}_Humidity_%RH'] = int.from_bytes(raw[index + 2:index + 3], 'little', signed=False) * 0.5
            index += 3
        elif raw[index + 1] == 0x73:
            message['data'][f'CH{raw[index]}_BarometricPressure_hPa'] = int.from_bytes(raw[index + 2:index + 4], 'little', signed=False) * 0.1
            index += 4
        elif raw[index + 1] == 0x75:
            message['data'][f'CH{raw[index]}_Battery_%'] = int.from_bytes(raw[index + 2:index + 3], 'little', signed=False)
            index += 3
        elif raw[index + 1] == 0x77:
            message['data'][f'CH{raw[index]}_Depth_cm'] = int.from_bytes(raw[index + 2:index + 4], 'little', signed=True)
            index += 4
        elif raw[index + 1] == 0x7B:
            message['data'][f'CH{raw[index]}_Pressure_kPa'] = int.from_bytes(raw[index + 2:index + 4], 'little', signed=False)
            index += 4
        elif raw[index + 1] == 0x7D:
            message['data'][f'CH{raw[index]}_Concentration_ppm'] = int.from_bytes(raw[index + 2:index + 4], 'little', signed=False)
            index += 4
        elif raw[index + 1] == 0x7F:
            message['data'][f'CH{raw[index]}_Conductivity_us/cm'] = int.from_bytes(raw[index + 2:index + 4], 'little', signed=False)
            index += 4
        elif raw[index + 1] == 0x82:
            message['data'][f'CH{raw[index]}_Distance_mm'] = int.from_bytes(raw[index + 2:index + 4], 'little', signed=False)
            index += 4
        elif raw[index + 1] == 0x94:
            message['data'][f'CH{raw[index]}_Light_Lux'] = int.from_bytes(raw[index + 2:index + 6], 'little', signed=False)
            index += 6
        elif raw[index + 1] == 0xCA:
            message['data'][f'CH{raw[index]}_SoilMoisture_%RH'] = int.from_bytes(raw[index + 2:index + 4], 'little', signed=False) * 0.01
            index += 4
        else:
            print(f"Unknown type 0x{raw[index + 1]}")
            break

    print(message['data'])
    return message


import sqlite3

import time

class Ürün():

    def __init__(self,adı,bölüm,marka,fiyat,gr,stt):

        self.adı = adı
        self.bölüm = bölüm
        self.marka = marka
        self.fiyat = fiyat
        self.gr = gr
        self.stt = stt

    def __str__(self):

        return "Ürünün Adı: {}\nBölüm: {}\nMarka: {}\nFiyat: {}\nGr: {}\nSon Tüketim Tarihi: {}\n".format(self.adı,self.bölüm,self.marka,self.fiyat,self.gr,self.stt)

class Ürünler():

    def __init__(self):

        self.baglanti_olustur()

    def baglanti_olustur(self):

        self.baglanti = sqlite3.connect("supermarket.db")

        self.cursor = self.baglanti.cursor()

        sorgu = "Create Table If not exists ürünler (adı TEXT,bölüm TEXT,marka TEXT,fiyat INT,gr INT,stt INT)"

        self.cursor.execute(sorgu)

        self.baglanti.commit()

    def baglantiyi_kes(self):

        self.baglanti.close()

    def urunleri_goster(self):

        sorgu = "Select * From ürünler"

        self.cursor.execute(sorgu)

        ürünler = self.cursor.fetchall()

        if (len(ürünler) == 0):
            print("Ürün bulunmuyor...")

        else:
            for i in ürünler:
                ürün = Ürün(i[0],i[1],i[2],i[3],i[4],i[5])
                print(ürün)

    def ürün_sorgula(self,adı):

        sorgu = "Select * From ürünler where adı = ? "

        self.cursor.execute(sorgu(adı,))

        ürünler = self.cursor.fetchall()

        if(len(ürünler) == 0):
            print("Sepetinizde böyle bir ürün bulunmamaktadır......")

        else:
            ürün = Ürün(ürünler[0][0],ürünler[0][1],ürünler[0][2],ürünler[0][3],ürünler[0][4],ürünler[0][5])

            print(ürün)

    def sepeteürün_ekle(self,ürün):

        sorgu = "Insert into ürünler Values(?,?,?,?,?,?)"
        self.cursor.execute(sorgu,(ürün.adı,ürün.bölüm,ürün.marka,ürün.fiyat,ürün.gr,ürün.stt))
        self.baglanti.commit()

    def sepettekiürünü_sil(self,adı):

        sorgu = "Delete From ürünler where adı = ? "

        self.cursor.execute(sorgu(adı,))
        self.baglanti.commit()


import json
import os
import tqdm
import imagesize
import shutil

class FCN2COCO(object):
    def __init__(self, label_root, img_root, coco_root, datalist_file):
        self.label_root = label_root
        self.img_root = img_root
        self.coco_root = coco_root
        self.datalist_file = datalist_file
        self._makedirs()
        self._init_coco()
        self._init_categories()
        self.img_idx = 0
        self.anno_idx = 0

    def _makedirs(self):
        if not os.path.exists(os.path.join(self.coco_root, 'images')):
            os.makedirs(os.path.join(self.coco_root, 'images'))
        if not os.path.exists(os.path.join(self.coco_root, 'annotations')):
            os.makedirs(os.path.join(self.coco_root, 'annotations'))

    def _init_coco(self):
        self._annos = {}
        self._annos['licenses'] = "Charel Create"
        self._annos['images'] = []
        self._annos['annotations'] = []
        self._annos['categories'] = []

    def _init_categories(self):
        categories2id = json.load(open(os.path.join(self.label_root, 'category.json')))
        for name in categories2id:
            category_dict = {}
            category_dict['name'] = name
            category_dict['id'] = categories2id[name]
            self._annos['categories'].append(category_dict)

    def _cp_imgs(self, datalist):
        for data in datalist:
            src_path = os.path.join(self.img_root, data)
            dst_path = os.path.join(self.coco_root, 'images')
            dst_root = os.path.abspath(os.path.dirname(dst_path))
            if not os.path.exists(dst_root):
                os.makedirs(dst_root)
            shutil.copy(src_path, dst_path)

    def _get_annos(self, data):
        ret = []
        label_file = os.path.join(self.label_root, data[:-4] + '.txt')
        label_info = [line.strip() for line in open(label_file, 'r').readlines()]
        num_insts = int(label_info[1])
        for i in range(num_insts):
            anno = {}
            anno['category_id'] = int(label_info[i * 3 + 2])
            bbox = [int(b) for b in label_info[i * 3 + 3].split(', ')]
            anno['bbox'] = [bbox[0], bbox[1], bbox[2] - bbox[0] + 1, bbox[3] - bbox[1] + 1]
            anno['area'] = anno['bbox'][2] * anno['bbox'][3]
            anno['image_id'] = self.img_idx
            anno['id'] = self.anno_idx
            anno['iscrowd'] = 0
            self.anno_idx += 1
            anno['segmentation'] = [[int(p) for p in label_info[i * 3 + 4].split(', ')]]
            ret.append(anno)
        return ret

    def _get_img(self, data):
        ret = {}
        ret['file_name'] = data
        w, h = imagesize.get(os.path.join(self.img_root, data))
        ret['width'] = w
        ret['height'] = h
        ret['id'] = self.img_idx
        self.img_idx += 1
        return ret

    def toCOCO(self):
        datalist = [line.strip() for line in open(self.datalist_file).readlines()]
        self._cp_imgs(datalist)
        for data in tqdm.tqdm(datalist):
            annos = self._get_annos(data)
            if len(annos) == 0:
                continue
            self._annos['annotations'].extend(annos)
            img_dict = self._get_img(data)
            self._annos['images'].append(img_dict)

    def save_coco(self, json_file):
        with open(os.path.join(self.coco_root, 'annotations', json_file), 'w') as fp:
            json.dump(self._annos, fp, separators=(',', ':'), indent=4, ensure_ascii=False)

if __name__ == '__main__':
    label_root = '/gruntdata/data/severstal-steel-defect-detection/fcn_label'
    img_root = '/gruntdata/data/severstal-steel-defect-detection/train_images'
    coco_root = '/gruntdata/data/coco/steel'
    datalist_file = '/gruntdata/data/severstal-steel-defect-detection/testlist.txt'
    fcn2coco = FCN2COCO(label_root, img_root, coco_root, datalist_file)
    fcn2coco.toCOCO()
    fcn2coco.save_coco('instances_test.json')


#!/usr/bin/env python

# Demonstration GET friendships/incoming
# See https://dev.twitter.com/rest/reference/get/friendships/incoming

from secret import twitter_instance

tw = twitter_instance()

next_cursor = -1 # [1]
while next_cursor: # [1]
    response = tw.friendships.incoming(cursor=next_cursor)
    print(response['ids'])
    next_cursor = response['next_cursor'] # [1]


from hashmap_left_join import __version__

def test_version():
    assert __version__ == '0.1.0'

from hashmap_left_join.hashmap_left_join import *
import pytest

def test_happy_path(prepared_hash1,prepared_hash2):

    actual=left_join(prepared_hash1,prepared_hash2)
    expected=[['wrath', 'anger', 'delight'], ['outfit','grap', None], ['diligent','employed','idle'], ['guide','usher','follow'], ['fond', 'enamored','averse']]
    assert actual==expected

def test_edge_case():
    ht1 =Hash_table()
    ht2 =Hash_table()
    actual=left_join(ht1,ht2)
    expected='hash table is empty'
    assert actual==expected

def test_expected_failure(prepared_hash1):
    ht2 =Hash_table()
    ht2.add('hi','averse')
    ht2.add('wrath','delight')
    ht2.add('diligent','idle')
    ht2.add('guide','follow')
    ht2.add('flow','jam')
    actual=left_join(prepared_hash1,ht2)
    expected=[['wrath', 'anger','delight'], ['outfit', 'grap', None], ['diligent','employed','idle'], ['guide','usher','follow'], ['fond', 'enamored','averse']]
    assert actual!=expected

@pytest.fixture
def prepared_hash1():
    ht1 = Hash_table()
    ht1.add('fond','enamored')
    ht1.add('wrath','anger')
    ht1.add('diligent','employed')
    ht1.add('outfit','grap')
    ht1.add('guide','usher')
    return ht1

@pytest.fixture
def prepared_hash2():
    ht2 = Hash_table()
    ht2.add('fond','averse')
    ht2.add('wrath','delight')
    ht2.add('diligent','idle')
    ht2.add('guide','follow')
    ht2.add('flow','jam')
    return ht2


n,m = map(int,input().split())
matrix = []
script = []
for x in range(n):
    matrix.append(input().split())

for r in range(m):
    for i in range(n):
        s = matrix[i][r]
        if s.isalnum():
            script.append(matrix[i][r])

print(''.join(script),end='')

'''
Question:
Nandini has a complex matrix script. The matrix script is a N X M grid of strings. It consists of alphanumeric characters and symbols (!,@,#,$,%,&). To decode the script, Nandini needs to read each column and select only the alphanumeric characters and connect them. She reads the column from top to bottom and starts reading from the leftmost column. If there are symbols in the decoded script, then Nandini removes them for better readability. Alphanumeric characters consist of: [A-Z, a-z, and 0-9].

Input Format

The first line contains space-separated integers N (rows) and M (columns) respectively.
The next N lines contain the row elements of the matrix script separated by space.

Output Format

Print the decoded matrix script.
Sample Input:
7 3
T s i
h % x
i  # $
s M #
$ a  &
# t %
i r !
Output:
ThisisMatrix
'''


from asyncio import sleep
import time
import re
import pandas
from datetime import datetime
from selenium import webdriver
from PIL import Image
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
#from selenium.webdriver.chrome.options import Options
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from fpdf import FPDF

start_time = datetime.now() #used to time the speed at which the code is executed

def save_screenshot(driver: webdriver.Firefox, path: str = 'screenshot.png') -> None:    #takes screenshot of entire page
    # Ref: https://stackoverflow.com/a/52572919/
    original_size = driver.get_window_size()
    required_width = driver.execute_script('return document.body.parentNode.scrollWidth')
    required_height = driver.execute_script('return document.body.parentNode.scrollHeight')
    driver.set_window_size(required_width, required_height)
    # driver.save_screenshot(path)  # has scrollbar
    driver.find_element(By.TAG_NAME,'body').screenshot(path)  # avoids scrollbar
    driver.set_window_size(original_size['width'], original_size['height'])

Firefoxoptions = webdriver.FirefoxOptions()
Firefoxoptions.add_argument("--headless")   #adds the headless option to the webdriver's options, headless --> opens in background

browser = webdriver.Firefox(executable_path=GeckoDriverManager().install() , options=Firefoxoptions) #define 'browser' to be the firefox browser, here install() is used to get the location used by the manager and pass it into service class

browser.get('http://www.google.com') #acces google
press = browser.find_element(By.ID, "L2AGLb") #find the button to accept cookies
press.click() #click the button

df0 = pandas.read_excel('Name Screening Database 2022.xlsx', sheet_name=0) #read the contents of the first sheet of the excel document and call this dataframe df0
df1 = pandas.read_excel('Name Screening Database 2022.xlsx', sheet_name=1) #analogous to above comment
df2 = pandas.read_excel('motscles.xlsx',sheet_name=0) #analogous to above comment
PPnamelist = df0['PP Name'].tolist() #put names of column 'PP Name' into a list and call this list 'PPnamelist'
PMnamelist = df1['PM name'].tolist() #analogous to above comment
motscles = df2['motscles'].tolist() #analogous to above comment
pagelist=['0','10','20'] #list is used to navigate the first 3 google pages in the news tab
allnameslist = [] #will become a concatination of PPnamelist and PMnamelist
allnameslistwarning = [] #will become a concatination of PPnamelist and PMnamelist with the mention 'warning' if suspicious words were found

def searchcheckandscreenshot(namelist: list) -> None:  #executes the google searches, checks words and screenshots
    for i in namelist:
        allnameslist.append(f'{i}')
        allnameslistwarning.append(f'{i}')
        for j in pagelist:
            time.sleep(1)
            browser.get(f'https://www.google.com/search?q={i}&tbm=nws&start={j}')
            save_screenshot(browser,f'screenshot{i}{j}.png')
            sourcetext = browser.page_source #we look for suspicious word in the sourcetext of the webpage
            sourcetext = sourcetext.lower() #convert everything to lower case letters in order to avoid word matching problems due to capitalization
            for k in motscles:
                boolean = k in sourcetext #check whether word k is in the sourcetext
                if boolean == True:
                    print(f'{k} found in {i}') #to see which words were found in the news of name i
                    for l in range(len(allnameslistwarning)):
                        if allnameslistwarning[l-1] == i: #identify location of name in list allnameswarning and add mention 'warning' to the name
                            allnameslistwarning[l-1] = f'{i} warning'

searchcheckandscreenshot(PMnamelist)
searchcheckandscreenshot(PPnamelist)
browser.quit()

def deletetransparentlayer() -> None: #deletes the transparent layer of the png to speed up the process of adding pictures to the pdf
    for i in allnameslist:
        for j in pagelist:
            im1 = Image.open(f'screenshot{i}{j}.png')
            im1 = im1.convert('RGB')
            im1.save(f'screenshot{i}{j}.png')

deletetransparentlayer()

class PDF(FPDF): #we create a class to use the FPDF library
     pass # nothing happens when it is executed

pdf = PDF() #create a pdf object of the PDF class
pdf.set_font('Arial', '', 9)

def createpdf() ->None: #creates pdf with names (+'warning' mention if necessary) and screenshots in it
    pdf.add_page()
    m=0 #used as counter for number of iterations
    for i in allnameslistwarning:
        pdf.write(0, i) #write the name to the pdf
        m=m+1
        if 'warning' in i:
            i = allnameslist[m-1] #replaces name+warning mention with name without the warning mention to be able to access the correct path of the relevant screenshot
        for j in pagelist:
            pdf.image(f'screenshot{i}{j}.png',x=0,y=15, w=170)
            if m < len(allnameslistwarning)+1:
                pdf.add_page()

createpdf()

pdf.output('test.pdf','F')

end_time = datetime.now()
print('Duration: {}'.format(end_time - start_time)) #print duration the code took to be executed


# -*- coding: utf-8 -*-
"""
@author: jameswang
"""

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def removeElements(head, val):
    if not head:
        return head
    newhead = ListNode(0)
    newhead.next = head
    # check head.val is not val
    slow = newhead
    fast = head

    while fast:
        if fast.val != val:
            slow.next.val = fast.val
            slow = slow.next
        fast = fast.next

    slow.next = None

    return newhead.next


# Name: Alisha Shahane
# StudentId: 200311941
# UnityId: asshahan

# Algorithm:
# 1: Every vertex forms its own community.
# 2: Assign communities to vertices such that the gain in modularity is highest.
#    Repeat till there no changes in communitites.
# 3: Combine vertices in a community and repeat 2.

# ---------- Imports ----------
import sys
import pandas as pd
from igraph import *
from scipy import spatial

'''
Function Description: Compute the cosine similarity of all the vertices
'''
def getCosineSimilarity(g):
    mat = [[0 for _ in range(g.vcount())] for _ in range(g.vcount())]

    for i in range(graph.vcount()):
        first = g.vs[i].attributes().values()

        for j in range(i, graph.vcount()):
            second = g.vs[j].attributes().values()
            dist = 1.0 / (1.0 + spatial.distance.cosine(first, second))
            mat[i][j] = mat[j][i] = dist

    return mat

'''
Function Description: Compute the modularity which would be a result of adding a vertex to a certain community
'''
def getModularity(graph, sim_mat, communities, vertex, alpha):

    sum_weigths = 0
    for node in set(communities):
        if graph.are_connected(vertex, node):
            eid = graph.get_eid(vertex, node)
            sum_weigths += graph.es["weight"][eid]

    expected_edges = (sum(graph.degree(list(set(communities)))) * graph.degree(vertex)) / (2.0 * graph.ecount())
    Q = sum_weigths - expected_edges
    Q = Q / (2.0 * graph.ecount())

    # For attributed graphs
    sum_similarity = 0.0
    for c in set(communities):
        sum_similarity += sim_mat[c][vertex]

    l = len(set(communities))
    sum_similarity = sum_similarity / l / l

    return ((alpha * Q) + ((1 - alpha) * sum_similarity))

'''
Function Description: This function assigns the correct community to all vertices based on modularity
'''
def getChangedCommunity(graph, sim_mat, communities, alpha):
    # We need to check modularity by putting the vertex in all the communities and chose the one with highest modularity
    num_changes = 0

    for v in range(graph.vcount()):

        # Find the original community
        org_com, new_com = [], []
        for c in communities:
            if v in c:
                org_com = c
                break

        max_gain = -1

        # Get the gain by adding the vertex to all other communities
        # Choose the maximum gain and add the vertex to that community.
        for c in communities:
            mod = getModularity(graph, sim_mat, c, v, alpha)

            if mod > 0:
                if mod > max_gain:
                    max_gain = mod
                    new_com = c

        if set(new_com) != set(org_com):
            if max_gain > 0:
                org_com.remove(v)
                new_com.append(v)
                num_changes += 1

                if len(org_com) == 0: communities.remove([])

    return num_changes

'''
Function Description: This function implements the initial pahse of the algorithm
'''
def stepTwo(graph, sim_mat, communities, alpha):
    # Assign vertex to a community which gives the highest positive gain
    changed_comm = getChangedCommunity(graph, sim_mat, communities, alpha)
    iter = 0

    # We need to repeat step 2
    # We will repeat it till changed comminities are more than zero and for a maximum of 15 iterations
    while iter < 15 and changed_comm > 0:
        changed_comm = getChangedCommunity(graph, sim_mat, communities, alpha)
        iter += 1

'''
Function Description: This function combines vertices in a single node and repeats earlier steps
'''
def stepThree(graph, sim_mat, communities, alpha, vertex_community, edges):
    # Community and vertex mapping
    num_coms = 0
    for com in communities:
        for v in com:
            vertex_community[v] = num_coms
        num_coms += 1

    # All vertices in a community contracted to a single vertex
    graph.contract_vertices(vertex_community, combine_attrs = "mean")
    graph.simplify(multiple = True, loops = True)

    # Assign every vertex its own community
    reduced_communities = [[i] for i in range(num_coms)]
    graph.es['weight'] = [0 for j in range(graph.ecount())]

    # Edge weight is equivalent to number of edges between the nodes within two communities.
    for edge in edges:
        left_com = vertex_community[edge[0]]
        right_com = vertex_community[edge[1]]

        if left_com != right_com:
            eid = graph.get_eid(left_com, right_com)
            graph.es['weight'][eid] += 1

    # Get the new cosine similarity
    new_sim_mat = getCosineSimilarity(graph)

    # Repeat step two
    stepTwo(graph, new_sim_mat, reduced_communities, alpha)

# ---------- Main ----------
# Check input choice of algorithm
if len(sys.argv) != 2:
    print("Aplha value not specified (0.0 or 0.5 or 1)")
    exit(0)

# Create grpah
attr_list = pd.read_csv("./data/fb_caltech_small_attrlist.csv")
graph = Graph.Read_Edgelist("./data/fb_caltech_small_edgelist.txt", directed = False)

graph.es['weight'] = [1] * graph.ecount()

# Assign attributes to each vertex
for i in list(attr_list.columns):
    graph.vs[i] = attr_list[i]

# Get cosine similarity
sim_mat = getCosineSimilarity(graph)

# Set aplha value
alpha, filename = 0,  "0"
if float(sys.argv[1]) == 0.5:
    alpha = 0.5
    filename = "5"
elif float(sys.argv[1]) == 0.1:
    aplha = 0.1
    filename = "1"

# Step 1 of SAC algorithm
# Number of communities is equal to number of vertices
communities = [[v] for v in range((graph.vcount()))]
print("Step 1 done.")

# Each vertex is its own community
vertex_community = [0 for v in range(graph.vcount())]

# Step 2 of SAC algorithm
stepTwo(graph, sim_mat, communities, alpha)
print("Step 2 done.")

# Step 3 of SAC algorithm
edges = []
for e in graph.es:
    edges.append([e.source, e.target])
stepThree(graph, sim_mat, communities, alpha, vertex_community, edges)
print("Step 3 done. Writing result in file. ")

with open('./communities_' + filename + '.txt', 'w') as file:
    for com in communities:
        for idx, vertex in enumerate(com):
            if idx != 0:
                file.write(',')
            file.write(str(vertex))
        file.write("\n")

file.close()

Attributed Graph Community Detection/sac1.py


def solution(name, yearning, photo):

    miss = {n:y for n, y in zip(name, yearning)}
    result = [0] * len(photo)

    for i, p in enumerate(photo):
        for people in p:
            try : # if 문으로 변경 가능
                result[i] += miss[people]
            except:
                continue

    return result


import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Slider
import matplotlib
matplotlib.use('Qt5Agg')

'''
This program was written by Alex Hewett during a dynamic meteorology course at the University of Washington.

The program is an educational tool intended to enhance understanding of divergence, vorticity, and deformation.

The program should be run as a script and the user may adjust the winds, divergence, vorticity, and deformation using the provided sliders.
'''

# Set the spatial and wind grids
x = np.arange(-10, 11, 1)
y = np.arange(-10, 11, 1)
xg, yg = np.meshgrid(x,y)
u_m = np.ones([21, 21])
v_m = np.ones([21, 21])

# Inital parameters for the wind
u_0 = 0
v_0 = 0
div = 0
vort = 0
d1 = 0
d2 = 0
# Define the kinematic equations
u = u_0*u_m + div*.5*xg - vort*.5*yg + d1*.5*xg + d2*.5*yg
v = v_0*u_m + div*.5*yg + vort*.5*xg - d1*.5*yg + d2*.5*xg

# Set the inital plot
fig, ax = plt.subplots(figsize=(9, 9))
plt.subplots_adjust(bottom=0.4)
ax.quiver(xg, yg, u, v)
ax.grid()
ax.set_title("Horizontal Wind Field", fontsize=24)

# Define the slider axes
axu_0 = plt.axes([0.25, 0.3, 0.65, 0.03])
axv_0 = plt.axes([0.25, 0.25, 0.65, 0.03])
axdiv = plt.axes([0.25, 0.2, 0.65, 0.03])
axvort = plt.axes([0.25, 0.15, 0.65, 0.03])
axd1 = plt.axes([0.25, 0.1, 0.65, 0.03])
axd2 = plt.axes([0.25, 0.05, 0.65, 0.03])

# Set the sliders
_u_0 = Slider(axu_0, 'u_0', -50, 50, valinit=0, valstep=1)
_u_0.label.set_size(22)
_v_0 = Slider(axv_0, 'v_0', -50, 50, valinit=0, valstep=1)
_v_0.label.set_size(22)
_div = Slider(axdiv, 'divergence', -20, 20, valinit=0, valstep=2)
_div.label.set_size(22)
_vort = Slider(axvort, 'vorticity', -20, 20, valinit=0, valstep=2)
_vort.label.set_size(22)
_d1 = Slider(axd1, 'd1', -20, 20, valinit=0, valstep=2)
_d1.label.set_size(22)
_d2 = Slider(axd2, 'd2', -20, 20, valinit=0, valstep=2)
_d2.label.set_size(22)

def update(val):
    '''
    Updates the quiver plot with the slider values for the wind paramaters
    '''
    u_0 = _u_0.val
    v_0 = _v_0.val
    div = _div.val
    vort = _vort.val
    d1 = _d1.val
    d2 = _d2.val
    # Compute the horizontal winds
    u = u_0*u_m + div*.5*xg - vort*.5*yg + d1*.5*xg + d2*.5*yg
    v = v_0*u_m + div*.5*yg + vort*.5*xg - d1*.5*yg + d2*.5*xg
    # clear the figure and plot new wind field
    ax.clear()
    ax.quiver(xg, yg, u, v)
    ax.grid()
    ax.set_title("Horizontal Wind Field", fontsize=24)

# Call the update function whenever a slider moves
_u_0.on_changed(update)
_v_0.on_changed(update)
_div.on_changed(update)
_vort.on_changed(update)
_d1.on_changed(update)
_d2.on_changed(update)

plt.show()


import json

import xlrd
from datetime import time

# Give the location of the file
loc = "R21 Lagos Geospatial Analysis Dataset.tsv"

file = open(loc, "r")

id_to_time = {}

for line in file:

    data = line.split("\t")

    id_to_time[float(data[0])] = data[5]

print(id_to_time)

data = json.load(open("reported_incidents.json", "r"))

for entry in data:
    time = id_to_time[entry["id"]]

    entry["time"] = time

json.dump(data, open("reported_incidents_v2.json", "w+"))