from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
from django.shortcuts import get_object_or_404
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import status, viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.serializers import ValidationError

from ..assessment.api import (
    AssessmentEditViewset,
    AssessmentLevelPermissions,
    AssessmentViewset,
    DisabledPagination,
    InAssessmentFilter,
    get_assessment_id_param,
)
from ..assessment.models import Assessment, TimeSpentEditing
from ..common.api import CleanupFieldsBaseViewSet, LegacyAssessmentAdapterMixin
from ..common.helper import re_digits, tryParseInt
from ..common.renderers import PandasRenderers
from ..common.serializers import UnusedSerializer
from ..common.views import AssessmentPermissionsMixin
from ..mgmt.models import Task
from ..riskofbias import exports
from ..study.models import Study
from . import models, serializers

class RiskOfBiasAssessmentViewset(
    AssessmentPermissionsMixin, LegacyAssessmentAdapterMixin, viewsets.GenericViewSet
):
    parent_model = Assessment
    model = Study
    permission_classes = (AssessmentLevelPermissions,)
    serializer_class = UnusedSerializer
    lookup_value_regex = re_digits

    def get_queryset(self):

        perms = self.get_obj_perms()
        if not perms["edit"]:
            return self.model.objects.published(self.assessment)
        return self.model.objects.get_qs(self.assessment.id)

    @action(detail=True, methods=("get",), url_path="export", renderer_classes=PandasRenderers)
    def export(self, request, pk):
        self.set_legacy_attr(pk)
        self.permission_check_user_can_view()
        rob_name = self.assessment.get_rob_name_display().lower()
        exporter = exports.RiskOfBiasFlat(
            self.get_queryset(), filename=f"{self.assessment}-{rob_name}"
        )

        return Response(exporter.build_export())

    @action(detail=True, methods=("get",), url_path="full-export", renderer_classes=PandasRenderers)
    def full_export(self, request, pk):
        self.set_legacy_attr(pk)
        self.permission_check_user_can_view()
        rob_name = self.assessment.get_rob_name_display().lower()
        exporter = exports.RiskOfBiasCompleteFlat(
            self.get_queryset(), filename=f"{self.assessment}-{rob_name}-complete"
        )
        return Response(exporter.build_export())

class RiskOfBiasDomain(viewsets.ReadOnlyModelViewSet):
    assessment_filter_args = "assessment"
    model = models.RiskOfBiasDomain
    pagination_class = DisabledPagination
    permission_classes = (AssessmentLevelPermissions,)
    filter_backends = (InAssessmentFilter, DjangoFilterBackend)
    serializer_class = serializers.AssessmentDomainSerializer
    lookup_value_regex = re_digits

    def get_queryset(self):
        return self.model.objects.all().prefetch_related("metrics")

class RiskOfBias(viewsets.ModelViewSet):
    assessment_filter_args = "study__assessment"
    model = models.RiskOfBias
    pagination_class = DisabledPagination
    permission_classes = (AssessmentLevelPermissions,)
    filter_backends = (InAssessmentFilter, DjangoFilterBackend)
    serializer_class = serializers.RiskOfBiasSerializer
    lookup_value_regex = re_digits

    def get_queryset(self):
        return self.model.objects.all().prefetch_related(
            "study", "author", "scores__metric__domain"
        )

    def perform_update(self, serializer):
        super().perform_update(serializer)
        study = serializer.instance.study
        user = self.request.user
        Task.objects.ensure_rob_started(study, user)
        if serializer.instance.final and serializer.instance.is_complete:
            Task.objects.ensure_rob_stopped(study)

        # send time complete task
        if not serializer.errors:
            TimeSpentEditing.add_time_spent_job(
                self.request.session.session_key,
                serializer.instance.get_edit_url(),
                serializer.instance,
                serializer.instance.get_assessment().id,
            )

    def create(self, request, *args, **kwargs):
        study_id = tryParseInt(request.data.get("study_id"), -1)

        try:
            study = Study.objects.get(id=study_id)
        except ObjectDoesNotExist:
            raise ValidationError("Invalid study_id")

        # permission check using the user submitting the request
        if not study.user_can_edit_study(study.assessment, request.user):
            raise PermissionDenied(
                f"Submitter '{request.user}' has invalid permissions to edit Risk of Bias for this study"
            )

        # overridden_objects is not marked as optional in RiskOfBiasScoreSerializerSlim; if it's not present
        # in the payload, let's just add an empty array.
        scores = request.data.get("scores")
        for score in scores:
            if "overridden_objects" not in score:
                score["overridden_objects"] = []

        return super().create(request, *args, **kwargs)

    @action(detail=True, methods=["get"])
    def override_options(self, request, pk=None):
        object_ = self.get_object()
        return Response(object_.get_override_options())

class AssessmentMetricViewset(AssessmentViewset):
    model = models.RiskOfBiasMetric
    serializer_class = serializers.AssessmentMetricChoiceSerializer
    pagination_class = DisabledPagination
    assessment_filter_args = "domain__assessment"

    def get_queryset(self):
        return self.model.objects.all()

class AssessmentMetricScoreViewset(AssessmentViewset):
    model = models.RiskOfBiasMetric
    serializer_class = serializers.AssessmentMetricScoreSerializer
    pagination_class = DisabledPagination
    assessment_filter_args = "domain__assessment"

    def get_queryset(self):
        return self.model.objects.all()

class AssessmentScoreViewset(AssessmentEditViewset):
    model = models.RiskOfBiasScore
    pagination_class = DisabledPagination
    assessment_filter_args = "metric__domain_assessment"
    serializer_class = serializers.RiskOfBiasScoreSerializer

    def get_assessment(self, request, *args, **kwargs):
        assessment_id = get_assessment_id_param(request)
        return get_object_or_404(self.parent_model, pk=assessment_id)

    @action(detail=False)
    def choices(self, request):
        assessment_id = self.get_assessment(request)
        rob_assessment = models.RiskOfBiasAssessment.objects.get(assessment_id=assessment_id)
        return Response(rob_assessment.get_rob_response_values())

    def create(self, request, *args, **kwargs):
        # create using one serializer; return using a different one
        serializer = serializers.RiskOfBiasScoreOverrideCreateSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        new_serializer = serializers.RiskOfBiasScoreSerializer(serializer.instance)
        headers = self.get_success_headers(new_serializer.data)
        return Response(new_serializer.data, status=status.HTTP_201_CREATED, headers=headers)

    def perform_destroy(self, instance):
        if instance.is_default:
            raise PermissionDenied("Cannot delete a default risk of bias score")
        instance.delete()

class ScoreCleanupViewset(CleanupFieldsBaseViewSet):
    model = models.RiskOfBiasScore
    serializer_class = serializers.RiskOfBiasScoreCleanupSerializer
    assessment_filter_args = "metric__domain__assessment"


import re
import glob
import sys

cdhitcluster = sys.argv[1]
#clusters = sys.argv[2]

cluster_seqs_stats_path = "RESULTS/*.cluster.all"
cluster_seqs_stats_files = glob.glob(cluster_seqs_stats_path)

#clusterFiles = clusters.split(',')
repSeqRedSeqdict = {}
repLine = ""
count = 0
first = False

with open(cdhitcluster, 'r+') as f:
    lines = f.readlines()
    for i in range(0, len(lines)):
        line = lines[i]
        if ">Cluster" in line:
            first = True
            count = 0
            if i+1 < len(lines):
                repLine = lines[i+1]
            continue
        elif not first:
            count += 1
            first = False
        else:
            first = False
            lineArr = []
        if count > 0:
            repLine = repLine.strip()
            rep_FullId = repLine.split()[2]
            rep_FullId = rep_FullId.replace(">", "")
            #rep_short_id = re.findall("_".join(["[^_]+"] * 2), rep_FullId)[0]
            rep_FullId = rep_FullId.replace("...", "")
            line = line.strip()
            add_FullId = line.split()[2]
            add_FullId = add_FullId.replace(">", "")
            add_FullId = add_FullId.replace("...", "")
            #add_short_id = re.findall("_".join(["[^_]+"] * 2), add_FullId)[0]
            lineArr.append(add_FullId)
            repSeqRedSeqdict[rep_FullId] = lineArr
            #lineArr.append(add_short_id)
            #repSeqRedSeqdict[rep_short_id] = lineArr

toWrite = ""

for singleFile in sorted(cluster_seqs_stats_files):
    with open(singleFile, "a+") as clFile:
        file_content = clFile.read()
        first_line = file_content.split('\n')[0]
        for key, val in repSeqRedSeqdict.items():
            if key in file_content:
                for i in val:
                    toWrite += first_line.split()[0] + "  " + first_line.split()[1] + "  " + first_line.split()[2] + "  " + " - " + "   " + "CD-Hit" + "    " + first_line.split()[5] + "  " + "ORIGID" + "  "  + str(i) + "\n"
        clFile.write(toWrite)


# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

# %% sklearn version: "LDA" algorithm using sklearn library

import numpy as np
from sklearn.discriminant_analysis import \
    LinearDiscriminantAnalysis

import matplotlib.pyplot as plt

# %% part 1 - load dataset and print summary

training_dataset = np.loadtxt('fld.txt', delimiter=',')
X_training_dataset = training_dataset[::, :2]
y_training_dataset = training_dataset[::, 2]

X_class_0_idx = np.where(y_training_dataset == 0)
X_class_1_idx = np.where(y_training_dataset == 1)
X_class_0 = X_training_dataset[X_class_0_idx]
X_class_1 = X_training_dataset[X_class_1_idx]

print('----------------------------------------------------------------------')
print('----------------------------------------------------------------------')
print('Summary of the Dataset:')
print('Class "0" info: Shape - Head - Tail')
print('Shape:', X_class_0.shape)
print('Head:\n', X_class_0[:5, ::])
print('Tail:\n', X_class_0[-5:, ::])
print('----------------------------------------------------------------------')
print('Class "1" info: Shape - Head - Tail')
print('Shape:', X_class_1.shape)
print('Head:\n', X_class_1[:5, ::])
print('Tail:\n', X_class_1[-5:, ::])
print('----------------------------------------------------------------------')

# %% part 2 - perform Linear Discriminant Analysis (LDA)

LDA_object = LinearDiscriminantAnalysis(store_covariance=True)
LDA_object.fit(X_training_dataset, y_training_dataset)
Weight_vector = LDA_object.coef_[0]; intercept = LDA_object.intercept_
print('----------------------------------------------------------------------')
print('LDA >> Slope and Intercept:')
print('Slope (of log-posterior of LDA) =', Weight_vector,
      ',\nIntercept (of log-posterior of LDA) =', intercept)
print('----------------------------------------------------------------------')

pi_0 = len(X_class_0) / (len(X_class_1) + len(X_class_0))
pi_1 = len(X_class_1) / (len(X_class_1) + len(X_class_0))
Cst = np.log(pi_0/pi_1)
# Note >> covariance_array-like of shape (n_features, n_features)
# Note >> means_array-like of shape (n_classes, n_features)
w = np.dot(np.linalg.inv(LDA_object.covariance_),
           (LDA_object.means_[0] - LDA_object.means_[1]))
DC_term = Cst - 0.5 * np.dot((LDA_object.means_[0] + LDA_object.means_[1]).T, w)

# %% part 3 - prediction

predictions = (np.sign(np.dot(w, X_training_dataset.T) + DC_term) + 1) / 2
error_possibility_1 = sum(predictions != y_training_dataset)
error_possibility_2 = sum((1 - predictions) != y_training_dataset)
rel_error_1 = error_possibility_1 / len(y_training_dataset)
rel_error_2 = error_possibility_2 / len(y_training_dataset)

if rel_error_1 < rel_error_2:
    final_predictions = predictions
else:
    final_predictions = 1 - predictions

num_preds_to_print = 20
print(f'Some of predictions are: [first {num_preds_to_print}]\n',
      final_predictions[:num_preds_to_print])
print(f'Some of predictions are: [last {num_preds_to_print}]\n',
      final_predictions[-num_preds_to_print:])

# %% part 4 - error report

errorIndex = np.argwhere(final_predictions != y_training_dataset)
errorPts = X_training_dataset[errorIndex]
errorPts = np.squeeze(errorPts)

print('----------------------------------------------------------------------')
print('LDA >> Error:', 100 * min(rel_error_2, rel_error_1), '%.')
print('LDA >> LDA_object.score():',
      100 * LDA_object.score(X_training_dataset, y_training_dataset), '%.')
print('----------------------------------------------------------------------')
print('----------------------------------------------------------------------')

# %% part 5 - visualization

#  first plot
figure_width = 20
original_data_linewidth = 5
legend_fontsize = 20
plot_grid_option = False

plt.figure(figsize=(figure_width, figure_width / 1.618))
plt.scatter(X_class_0[:, 0],
            X_class_0[:, 1],
            c='r', marker='.',
            linewidths=original_data_linewidth)
plt.scatter(X_class_1[:, 0],
            X_class_1[:, 1],
            c='b', marker='.',
            linewidths=original_data_linewidth)

k0, k1 = 5, 3
plt.plot([- k0 * w[0], k1 * w[0]],
         [-k0 * w[1], k1 * w[1]],
         'g--', lw=5)

plt.xlabel('first axis (x0)', size=legend_fontsize)
plt.ylabel('second axis (x1)', size=legend_fontsize)
plt.legend(['LDA line', 'original data - class 0', 'original data - class 1'],
           fontsize=legend_fontsize)

plt.savefig('sklearn-improved-img-1.png', dpi=300)
plt.grid(True)
plt.savefig('sklearn-improved-img-1-grid.png', dpi=300)
plt.show()

#  second plot
plt.figure(figsize=(figure_width, figure_width / 1.618))
plt.scatter(X_class_0[:, 0],
            X_class_0[:, 1],
            c='r', marker='.',
            linewidths=original_data_linewidth)
plt.scatter(X_class_1[:, 0],
            X_class_1[:, 1],
            c='b', marker='.',
            linewidths=original_data_linewidth)

k0, k1 = 5, 3
plt.plot([- k0 * w[0], k1 * w[0]],
         [-k0 * w[1], k1 * w[1]],
         'g--', lw=5)

plt.scatter(errorPts[:, 0],
            errorPts[:, 1],
            c='orange',
            marker='o')

plt.xlabel('first axis (x0)', size=legend_fontsize)
plt.ylabel('second axis (x1)', size=legend_fontsize)
plt.legend(['LDA line',
            'original data - class 0',
            'original data - class 1',
            'LDA error samples'],
           fontsize=legend_fontsize)

plt.savefig('sklearn-improved-img-2.png', dpi=300)
plt.grid(True)
plt.savefig('sklearn-improved-img-2-grid.png', dpi=300)
plt.show()


'''
그룹핑된 문자열 재참조
'''

"""
정규식 (\b\w+)\s+\1은 (그룹1) + " " + "그룹1과 동일한 단어" 와 매치됨을 의미합니다.
 이렇게 정규식을 만들게 되면 2개의 동일한 단어가 연속적으로 사용되어야만 매치되게 된다.
 이것을 가능하게 해 주는 것이 바로 재 참조 메타문자인 \1이다. \1은 정규식의 그룹중 첫번째 그룹을 지칭한다.
"""
import re

p = re.compile(r'(\b\w+)\s+\1')
m= p.search('No pain pain no gain').group()
print(m) # pain pain


from nonebot import on_command, CommandSession
from nonebot import on_natural_language, NLPSession, IntentCommand
from .Servers_data_source import *

__plugin_name__ = '服务器查询'
__plugin_usage__ = r"""
服务器查询格式：
【服务器】+空格+服务器名称
【查服务器】+空格+服务器名称
"""

@on_command('Servers', aliases=('服务器', '查服务器'),only_to_me=False)
async def Servers(session: CommandSession):
    Quer_Servers = session.get('Quer_Servers', prompt='你想查询服务器名称是多少？')
    prompt = "查询中稍等片刻"
    await session.send(prompt)
    Servers_report = await get_Servers(Quer_Servers)
    await session.send(Servers_report, at_sender=True)

@Servers.args_parser
async def _(session: CommandSession):
    stripped_arg = session.current_arg_text.strip()
    if session.is_first_run:
        if stripped_arg:
            session.state['Quer_Servers'] = stripped_arg
        return

    if not stripped_arg:
        session.pause('要查询的服务器名称不能为空，请重新输入')

    session.state[session.current_key] = stripped_arg


''' Faça um Programa que leia um vetor de 5 números inteiros, mostre a soma, a multiplicação e
os números.'''
vetor = []
i = 0
soma = 0
while i < 5:
    numero = int(input('Digite numero: '))
    vetor.append(numero)
    soma = soma + numero
    i = i + 1
mult = vetor[0]*vetor[1]*vetor[2]*vetor[3]*vetor[4]
print('Os valores digitados foram: ',vetor)
print('A soma dos valores é',soma)
print('A multiplicação dos valores é',mult)


import pygame
import numpy as np
import random
from Tile import Tile
from global_const import *

RESIZABLE_FLAGS = pygame.RESIZABLE | pygame.HWSURFACE | pygame.DOUBLEBUF
FULLSCREEN_FLAGS = pygame.FULLSCREEN | pygame.HWSURFACE | pygame.DOUBLEBUF

class Game:
    def __init__(self):
        self.play = True
        self.fullscreen = False
        self.clock = pygame.time.Clock()
        self.screen = pygame.display.set_mode(RESOLUTION, RESIZABLE_FLAGS)

        # Needed for tiles background
        self.tiles_background = [[Tile() for _ in range(BOARD_SIZE)]
                                 for _ in range(BOARD_SIZE)]
        self.board = np.array([[Tile() for _ in range(BOARD_SIZE)]
                               for _ in range(BOARD_SIZE)])
        self.board_random(2)

    def run(self):
        """Main game loop"""
        while self.play:
            self.catch_events()
            self.draw()
            self.update()

    def catch_events(self):
        """Catch key events"""
        for event in pygame.event.get():
            # Quit
            if event.type == pygame.QUIT:
                self.play = False

            # Video resize
            if event.type == pygame.VIDEORESIZE:
                if not self.fullscreen:
                    new_width = event.w if event.w >= MIN_WIDTH else \
                        MIN_WIDTH
                    new_height = event.h if event.h >= MIN_HEIGHT else \
                        MIN_HEIGHT
                    self.screen = pygame.display.set_mode(
                        (new_width, new_height),
                        RESIZABLE_FLAGS)
                    self.update_tiles_position(
                        width=new_width,
                        height=new_height)

            # KeyDown events
            if event.type == pygame.KEYDOWN:
                # Check if alt is pressed
                pressed = pygame.key.get_pressed()
                alt_held = pressed[pygame.K_LALT] or pressed[pygame.K_RALT]
                # Quit on alt + F4
                if event.key == pygame.K_F4 and alt_held:
                    self.play = False

                # Fullscreen toggle
                if event.key == pygame.K_f:
                    self.fullscreen = not self.fullscreen
                    if self.fullscreen:
                        self.screen = pygame.display.set_mode(
                            MONITOR_RESOLUTION,
                            FULLSCREEN_FLAGS)
                    else:
                        self.screen = pygame.display.set_mode(
                            RESOLUTION,
                            RESIZABLE_FLAGS)
                    self.update_tiles_position()

                # Move event
                if event.key in [pygame.K_UP, pygame.K_DOWN,
                                 pygame.K_LEFT, pygame.K_RIGHT]:
                    self.move(event.key)
                    self.board_random()

    def move(self, direction):
        '''Movement

        Args:
            direction (pygame.KEY): Pressed key (K_UP, K_DOWN, K_LEFT, K_RIGHT)
        '''
        if direction in (pygame.K_UP, pygame.K_DOWN):
            self.board = self.board.T
        if direction in (pygame.K_RIGHT, pygame.K_DOWN):
            self.board = self.board[:,::-1]

        for i in range(BOARD_SIZE):
            for j in range(BOARD_SIZE - 1):
                for k in range(j + 1, BOARD_SIZE):
                    if self.board[i][k].value == 0:
                        continue
                    if self.board[i][j].value != 0 and \
                       self.board[i][j].value != self.board[i][k].value:
                        break
                    if self.board[i][j].value == 0 or \
                       self.board[i][j].value == self.board[i][k].value:
                        self.board[i][j].value += self.board[i][k].value
                        self.board[i][k].value = 0

        if direction in (pygame.K_RIGHT, pygame.K_DOWN):
            self.board = self.board[:,::-1]
        if direction in (pygame.K_UP, pygame.K_DOWN):
            self.board = self.board.T

    def draw(self):
        """Draws surface"""
        self.screen.fill(COLORS['BACKGROUND'])
        # Draw tiles
        [[col.draw(self.screen) for col in row]
         for row in self.board]

    def update_tiles_position(self, width=0, height=0):
        """Update tiles position

        Args:
            width (int, optional, default=0): Surface width
            height (int, optional, default=0): Surface height
        """
        [[col.set_pos(self.fullscreen, width, height) for col in row]
         for row in self.board]

    def board_random(self, amount=1):
        '''Put value '2' in random place in self.board

        Args:
            amount (int, optional): amount of values to place

        Returns:
            bool: return true if success else false
        '''
        for _ in range(amount):
            if self.check_if_lose():
                self.play = False
                return False
            while True:
                row = random.randint(0, BOARD_SIZE - 1)
                col = random.randint(0, BOARD_SIZE - 1)
                if self.board[row][col].value != 0:
                    continue
                self.board[row][col].value = 2
                break
            return True

    def check_if_lose(self):
        """Checks if a board is full

        Returns:
            BOOL: Return True if board is full else False
        """
        for row in self.board:
            for col in row:
                if not col.value:
                    return False
        return True

    def update(self):
        """Code which update screen"""
        pygame.display.flip()
        self.clock.tick(FPS)


import json
import re
import time
import urllib.parse as urlparser
from pathlib import Path
from pprint import pprint
from time import sleep
from warnings import warn

import requests
from bs4 import BeautifulSoup
from tqdm import tqdm

class PageParser:
    """
    在这个类里面做不同域名的页面解析,
    对于写好的方法在 __init__ 里面的方法表上登记
    """

    def __init__(self):
        self.parse_method_table = {
            # "www.example.com": PageParser.method
            "www.channelnewsasia.com": self.www_channelnewsasia_com,
            "www.straitstimes.com": self.www_straitstimes_com

        }

    def __call__(self, domain, text) -> dict:
        parse_method = self.parse_method_table.get(domain)
        if not parse_method:
            # warn("没有用来解析域名 {} 页面的方法!".format(domain))
            parse_method = self.www_default_com

        try:
            return parse_method(text)
        except Exception:
            warn("页面内容解析失败: {}".format(domain))
            return {
                "Type": "",
                "Time": time.strftime("%Y-%m-%d", time.gmtime()),
                "Headline": "",
                "Text": "",
                "Section": "",
                "Writers": "",
                "URL": "",
                "MainKeyWord": "",
                "AdditionalKeyWord": ""
            }

    def www_default_com(self, text) -> dict:
        result = {
            "Type": "",
            "Time": time.strftime("%Y-%m-%d", time.gmtime()),
            "Headline": "",
            "Text": "",
            "Section": "",
            "Writers": "",
            "URL": "",
            "MainKeyWord": "",
            "AdditionalKeyWord": ""
        }
        soup = BeautifulSoup(text, "lxml")
        text = ""
        ps = soup.find_all("p")
        for p in ps:
            text += p.text
        result["Headline"] = soup.title.string
        result["Text"] = text
        return result

    def www_straitstimes_com(self, text) -> dict:
        result = {
            "Type": "",
            "Time": time.strftime("%Y-%m-%d", time.gmtime()),
            "Headline": "",
            "Text": "",
            "Section": "",
            "Writers": "",
            "MainKeyWord": "",
            "AdditionalKeyWord": ""
        }
        soup = BeautifulSoup(text, "lxml")
        date = soup.find("li", {"class": "story-postdate"})
        if date is not None and date.content is not None:
            result["Time"] = date.content[1]
        result["Headline"] = soup.title.string
        result["Text"] = "".join([i.text for i in soup.find_all("p")])
        return result

    def www_channelnewsasia_com(self, text) -> dict:
        result = {
            "Type": "",
            "Time": time.strftime("%Y-%m-%d", time.gmtime()),
            "Headline": "",
            "Text": "",
            "Section": "",
            "Writers": "",
            "MainKeyWord": "",
            "AdditionalKeyWord": ""
        }
        article = BeautifulSoup(text, "lxml").find("article")
        if article is None:
            return self.www_default_com(text)
        result["Type"] = (
            article.find("header")
            .find("span", {"class": "article__category"})
            .get_text(strip=True)
        )
        result["Headline"] = article.find("h1").get_text(strip=True)
        result["Text"] = "".join([
            p.get_text(strip=True)
            for p in article.find_all("p")
        ])
        result["MainKeyWord"] = ",".join([
            li.get_text(strip=True)
            for li in article.find("footer").find("ul").find_all("li")
        ])

        return result

class GoogleNews(requests.Session):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.url_topstories = "https://news.google.com/topstories"
        self.pageparser = PageParser()

    def get(self, url, **kwargs):
        try:
            return super().get(url, **kwargs)
        except Exception as e:
            print(e)
            warn("Error in response: {}".format(url))
            return None

    def get_article_urls(self, hl) -> list:
        """获得所有文章的 url

        Args:
            hl: 国家地区, 来自于 url 中的 hl 参数, 比如 "en-SG", "en-ID", ...

        Returns:
            一个列表, 包含所有文章的真实 url 字符串
        """

        # 获取首页
        response = self.get(self.url_topstories, params={"hl": hl})

        if not response:
            warn("无响应, 检查网络")
            return []
        sleep(0.1)
        # Path("a.html").write_text(response.text, "utf8")

        # 获取 More Headlines
        Path("./tmp/a.html").write_text(response.text, encoding="utf8")
        url_more_headlines = BeautifulSoup(response.text, "lxml").find(
            text="More Headlines").parent.get("href")
        response = self.get(urlparser.urljoin(
            response.url, url_more_headlines))
        if not response:
            warn("无响应, 检查网络")
            return []
        sleep(0.1)
        # Path("b.html").write_text(response.text, "utf8")

        # 获取 Google 的文章伪链接
        url_articles = [
            # 这里加了一个点, 不知道为什么url的相对路径有问题
            urlparser.urljoin(response.url, "."+e.a["href"])
            for e in BeautifulSoup(response.text, "lxml").find_all("article")
        ]

        # 获取文章的真实链接
        url_real = []
        for url in tqdm(url_articles, desc="获取文章真实链接", ncols=100):
            response = self.get(url)
            if not response:
                continue
            sleep(0.1)
            # Path("c.html").write_text(response.text, "utf8")
            url_real.append(BeautifulSoup(
                response.text, "lxml").find("noscript").a.get("href"))

        return url_real

    def get_article(self, url_article) -> dict:
        """返回结构化的文章数据"""
        # XXX: 这地方如果有requests解决不了的 url, 可以通过urlparse判断域名, 然后用无头浏览器去访问页面
        # 无头浏览器部分暂时没写, 如果很多都没办法用 requests 获取, 可能也要加一个表来记录哪些域名要用无头浏览器解析
        response = self.get(url_article)
        sleep(0.1)

        # 给 parser 解析
        result = self.pageparser(
            urlparser.urlparse(url_article).netloc,
            (response.text if response else "")
        )
        result["URL"] = url_article  # 补充两个数据
        result["Source"] = "google"
        return result

def get_domains(urls) -> set:
    """返回不重复的所有域名集合"""
    return set(urlparser.urlparse(url).netloc for url in urls)

if __name__ == "__main__":
    spider = GoogleNews()
    spider.headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0",
    }

    url_articles = spider.get_article_urls("en-SG")
    pprint(url_articles)
    with Path("urls.json").open("w", encoding="utf8") as f:
        json.dump(url_articles, f)

    text = spider.get_article(url_articles[0])
    with Path("example.json").open("w", encoding="utf8") as f:
        json.dump(text, f, ensure_ascii=False)


import socket
import sys
import struct

# Socket protocol
def send_message(sock, data):
  l = len(data)
  sock.sendall(struct.pack('!I', l))
  sock.sendall(data)

def recv_message(sock):
  lbuf = recv_all(sock, 4)
  if not lbuf:
    return lbuf
  l, = struct.unpack('!I', lbuf)
  return recv_all(sock, l)

def recv_all(sock, count):
  buf = b''
  while count:
    newbuf = sock.recv(count)
    if not newbuf: return None
    buf += newbuf
    count -= len(newbuf)
  return buf

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('0.0.0.0', 10000)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

while True:
    # Wait for a connection
    print >>sys.stderr, 'waiting for a connection'
    connection, client_address = sock.accept()

    try:
        print >>sys.stderr, 'connection from', client_address

        # Receive the data in small chunks and retransmit it
        while True:
            data = recv_message(connection)

            if not data:
              break

            print >>sys.stderr, 'received "%s"' % data

            send_message(connection, data)

    finally:
        # Clean up the connection
        connection.close()


# uncompyle6 version 3.7.4
# Python bytecode 3.7 (3394)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.macosx-10.9-x86_64/egg/dreegdl/smote.py
# Compiled at: 2020-03-04 18:08:43
# Size of source mod 2**32: 2485 bytes
from sklearn.neighbors import NearestNeighbors
import random as rd

class Smote:
    __doc__ = '\n  Implement SMOTE, synthetic minority oversampling technique.\n  Parameters\n  -----------\n  sample      2D (numpy)array\n              minority class samples\n  N           Integer\n              amount of SMOTE N%\n  k           Integer\n              number of nearest neighbors k\n              k <= number of minority class samples\n  Attributes\n  ----------\n  newIndex    Integer\n              keep a count of number of synthetic samples\n              initialize as 0\n  synthetic   2D array\n              array for synthetic samples\n  neighbors   K-Nearest Neighbors model\n  '

    def __init__(self, sample, N, k):
        self.sample = sample
        self.k = k
        self.T = len(self.sample)
        self.N = N
        self.newIndex = 0
        self.synthetic = []
        self.neighbors = NearestNeighbors(n_neighbors=(self.k)).fit(self.sample)

    def over_sampling(self):
        if self.N < 100:
            self.T = self.N / 100 * self.T
            self.N = 100
        self.N = int(self.N / 100)
        for i in range(0, self.T):
            nn_array = self.compute_k_nearest(i)
            self.populate(self.N, i, nn_array)

    def compute_k_nearest(self, i):
        nn_array = self.neighbors.kneighbors([self.sample[i]], (self.k), return_distance=False)
        if len(nn_array) is 1:
            return nn_array[0]
        return []

    def populate(self, N, i, nn_array):
        while N is not 0:
            nn = rd.randint(1, self.k - 1)
            self.synthetic.append([])
            for attr in range(0, len(self.sample[i])):
                dif = self.sample[nn_array[nn]][attr] - self.sample[i][attr]
                gap = rd.random()
                while gap == 0:
                    gap = rd.random()

                self.synthetic[self.newIndex].append(self.sample[i][attr] + gap * dif)

            self.newIndex += 1
            N -= 1


import random

# We're writing a script to write a poem from a random selection of words.

noun_pool = ["fossil", "horse", "aardvark", "judge", "chef", "mango", "cowboy", "gorilla", "helicopter", "robot", "storm trooper", "jedi"]
verb_pool = ["kicks", "jingles", "bounces", "slurps", "meows", "explodes", "curdles", "skateboards", "games", "guesses", "prays", "picks"]
adjective_pool = ["furry", "balding", "incredulous", "fragrant", "exuberant", "glistening", "purple", "high", "good", "new", "first", "last"]
preposition_pool = ["against", "after", "into", "beneath", "upon", "for", "in", "like", "over", "within", "of", "with", "during"]
adverb_pool = ["curiously", "extravagantly", "tantalizingly", "furiously", "sensuously", "quietly", "happily"]

def make_poem():
    nouns = []
    verbs = []
    adjectives = []
    prepositions = []

    # Select the words we need for our poem.

    while len(nouns) < 3:
        selected_noun = random.choice(noun_pool)
        while selected_noun in nouns:
            selected_noun = random.choice(noun_pool)
            continue
        nouns.append(selected_noun)

    while len(verbs) < 3:
        selected_verb = random.choice(verb_pool)
        while selected_verb in verbs:
            selected_verb = random.choice(verb_pool)
            continue
        verbs.append(selected_verb)

    while len(adjectives) < 3:
        selected_adjective = random.choice(adjective_pool)
        while selected_adjective in adjectives:
            selected_adjective = random.choice(adjective_pool)
            continue
        adjectives.append(selected_adjective)

    while len(prepositions) < 2:
        selected_preposition = random.choice(preposition_pool)
        while selected_preposition in prepositions:
            selected_preposition = random.choice(preposition_pool)
            continue
        prepositions.append(selected_preposition)

    # We only need one adverb, so we'll just randomly select when we build the poem.

    # Set the first word (a/an) correctly.
    vowels = ["a", "e", "i", "o", "u"]
    if adjectives[0][0] in vowels:
        first_word = "An"
    else:
        first_word = "A"
    # Set the second instance of a/an correctly.
    if adjectives[2][0] in vowels:
        second_word = "an"
    else:
        second_word = "a"

    poem = f"""{first_word} {adjectives[0]} {nouns[0]}
        {first_word} {adjectives[0]} {nouns[0]} {verbs[0]} {prepositions[0]} the {adjectives[1]} {nouns[1]}
        {random.choice(adverb_pool)}, the {nouns[0]} {verbs[1]}
        the {nouns[1]} {verbs[2]} {prepositions[1]} {second_word} {adjectives[2]} {nouns[2]}"""

    return poem

print(make_poem())

Ch 09 - Tuples, Lists, and Dictionaries/09.5-challenge-wax-poetic.py


"""views for harshp
"""

from django.http import Http404
from django.shortcuts import render_to_response

from blog.models import BlogPost
from stories.models import StoryPost
from poems.models import Poem
from articles.models import Article
from lifeX.models import LifeXWeek
from brainbank.models import BrainBankIdea
from sitedata.social_meta import create_meta

def home(request):
    """homepage for harshp.com

    show all the latest posts in different apps

    Args:
        request(HttpResponse)

    Returns:
        HttpResponse: 200 on success, 404 on error

    Raises:
        Http404: error retrieving posts
    """
    try:
        blogs = BlogPost.objects.order_by('-published')[:3]
        stories = StoryPost.objects.order_by('-published')[:3]
        poems = Poem.objects.order_by('-published')[:3]
        articles = Article.objects.order_by('-published')[:3]
        brainbank_idea = BrainBankIdea.objects.latest('published')
        lifexweek = LifeXWeek.objects.latest('number')
        description = """
            The personal website of Harshvardhan Pandit (coolharsh55)"""
        keywords = ['harshp.com', 'blog', 'stories', 'poems', ]
        meta = create_meta(
            'harshp.com',
            description,
            keywords,
            url=request.build_absolute_uri(),
        )
    except (BlogPost.DoesNotExist,
            StoryPost.DoesNotExist,
            Poem.DoesNotExist,
            Article.DoesNotExist,
            LifeXWeek.DoesNotExist,
            BrainBankIdea.DoesNotExist):
        raise Http404('Error retrieving website data...')
    return render_to_response(
        'harshp/index.html',
        {
            'blogs': blogs,
            'stories': stories,
            'poems': poems,
            'articles': articles,
            'brainbank_idea': brainbank_idea,
            'lifeXweek': lifexweek,
            'meta': meta,
        }
    )

def changelog(request):
    """changelog

    show the project changelog

    Args:
        request(HttpResponse)

    Returns:
        HttpResponse: 200 on success, 404 on error

    Raises:
        None
    """
    return render_to_response('harshp/changelog.html')

def privacypolicy(request):
    """privacy policy

    show the privacy policy for website

    Args:
        request(HttpResponse)

    Returns:
        HttpResponse: 200 on success, 404 on error

    Raises:
        None
    """
    return render_to_response('harshp/privacypolicy.html')


#Utilities.py
#Contains utility methods for the casino simulator
#Created by: Andrew Davis
#Created on: 1/9/2016
#Open source (MIT license)

#chip_color dictionary - translates chip values into colors
chip_color = {
    1 : 'white',
    5 : 'red',
    25 : 'green',
    50 : 'blue',
    100 : 'black',
    500 : 'purple',
    1000 : 'yellow',
    5000 : 'gray'
}

#get_chip_color function - returns the color for a chip value
def get_chip_color(value):
    if chip_color.has_key(value): #if the chip value is in the dictionary
        return chip_color[value] #return its color
    else:
        return '' #the chip value is not in the dictionary, so return an empty string


import pyttsx3
import tkinter as tk
import tkinter.font as font
from tkinter.constants import CENTER, COMMAND, DISABLED, LEFT, NORMAL, VERTICAL, W
from tkinter.ttk import Progressbar
from typing import Text
from pygame import mixer

#set male voice for calling
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voices', voices[1].id)

mixer.init()
#music playing
mixer.music.load('kbc.mp3')
mixer.music.play(-1)

#creating tkinter window
root = tk.Tk()
root.title("Millionaire")
# tkinter window with dimensions 1270x652 at x =0, y=0
root.geometry('1270x652+0+0')
root.configure(background='black')
#--------------------------------------------------frames------------------------
leftFrame = tk.Frame(root, bg='black', padx = 90)
leftFrame.grid(row=0, column=0)

topFrame = tk.Frame(leftFrame, bg='black', pady = 15)
topFrame.grid()

centerFrame = tk.Frame(leftFrame, bg='black', pady = 15)
centerFrame.grid(row=1, column=0)

bottomFrame = tk.Frame(leftFrame)
bottomFrame.grid(row=2, column=0)

rightFrame = tk.Frame(root, padx = 50, pady = 25, bg='black')
rightFrame.grid(row=0, column=1)
#--------------------------------------------------Images------------------------
def live50():
    live50_50.config(image=image50_50used, state=DISABLED)
    if quesArea.get(1.0, 'end-1c') == questions[0]:
        btnOption1.config(text='')
        btnOption2.config(text='')
    if quesArea.get(1.0, 'end-1c') == questions[1]:
        btnOption1.config(text='')
        btnOption3.config(text='')
    if quesArea.get(1.0, 'end-1c') == questions[2]:
        btnOption2.config(text='')
        btnOption4.config(text='')
    if quesArea.get(1.0, 'end-1c') == questions[3]:
        btnOption3.config(text='')
        btnOption4.config(text='')
    if quesArea.get(1.0, 'end-1c') == questions[4]:
        btnOption2.config(text='')
        btnOption4.config(text='')
    if quesArea.get(1.0, 'end-1c') == questions[5]:
        btnOption4.config(text='')
        btnOption2.config(text='')
    if quesArea.get(1.0, 'end-1c') == questions[6]:
        btnOption1.config(text='')
        btnOption2.config(text='')
    if quesArea.get(1.0, 'end-1c') == questions[7]:
        btnOption3.config(text='')
        btnOption4.config(text='')

def livepeo():
    livePeople.config(image=imagePeopleused, state=DISABLED)
    progressbarA.place(x=580, y =190)
    progressbarB.place(x=620, y =190)
    progressbarC.place(x=660, y =190)
    progressbarD.place(x=700, y =190)

    lblprogressbarA.place(x=580,y=320)
    lblprogressbarB.place(x=620,y=320)
    lblprogressbarC.place(x=660,y=320)
    lblprogressbarD.place(x=700,y=320)

    if quesArea.get(1.0, 'end-1c')==questions[0]:
        progressbarA.config(value=30)
        progressbarB.config(value=50)
        progressbarC.config(value=70)
        progressbarD.config(value=30)
    if quesArea.get(1.0, 'end-1c')==questions[1]:
        progressbarA.config(value=10)
        progressbarB.config(value=80)
        progressbarC.config(value=20)
        progressbarD.config(value=15)
    if quesArea.get(1.0, 'end-1c')==questions[2]:
        progressbarA.config(value=50)
        progressbarB.config(value=70)
        progressbarC.config(value=95)
        progressbarD.config(value=30)
    if quesArea.get(1.0, 'end-1c')==questions[3]:
        progressbarA.config(value=85)
        progressbarB.config(value=25)
        progressbarC.config(value=10)
        progressbarD.config(value=5)
    if quesArea.get(1.0, 'end-1c')==questions[4]:
        progressbarA.config(value=75)
        progressbarB.config(value=35)
        progressbarC.config(value=15)
        progressbarD.config(value=10)
    if quesArea.get(1.0, 'end-1c')==questions[5]:
        progressbarA.config(value=70)
        progressbarB.config(value=40)
        progressbarC.config(value=30)
        progressbarD.config(value=10)
    if quesArea.get(1.0, 'end-1c')==questions[6]:
        progressbarA.config(value=10)
        progressbarB.config(value=40)
        progressbarC.config(value=15)
        progressbarD.config(value=65)
    if quesArea.get(1.0, 'end-1c')==questions[7]:
        progressbarA.config(value=95)
        progressbarB.config(value=25)
        progressbarC.config(value=40)
        progressbarD.config(value=30)

def livelinephone():
    mixer.music.load('calling.mp3')
    mixer.music.play()
    btnCall.place(x=70, y=260)
    livePhone.config(image=imagePhoneused, state=DISABLED)

def phoneclick():
    for i in range(8):
        if quesArea.get(1.0, 'end-1c')==questions[i]:
            engine.say(f'Đáp án là {correct_answers[i]}')
            engine.runAndWait()
# engine.stop()

#--------------------------------------------------Images------------------------
centerImage = tk.PhotoImage(file='center.png')
logoCenter = tk.Button(centerFrame, image= centerImage, bg = 'black', width=300, height=200, bd =0)
logoCenter.grid(row=0, column=0)

# 50-50
image50_50 = tk.PhotoImage(file='5050.png')
image50_50used = tk.PhotoImage(file='5050used.png')
live50_50 = tk.Button(topFrame, image= image50_50, bg = 'black', width=180, height=80,bd =0, activebackground='black', command=live50)
live50_50.grid(row=0,column=0)

# ask people
imagePeople = tk.PhotoImage(file='people.png')
imagePeopleused = tk.PhotoImage(file='peopleused.png')
livePeople = tk.Button(topFrame, image= imagePeople, bg = 'black', width=180, height=80,bd =0, activebackground='black', command=livepeo)
livePeople.grid(row=0,column=1)

# phone
imagePhone = tk.PhotoImage(file='phone.png')
imagePhoneused = tk.PhotoImage(file='phoneused.png')
livePhone = tk.Button(topFrame, image= imagePhone, bg = 'black', width=180, height=80,bd =0, activebackground='black', command=livelinephone)
livePhone.grid(row=0,column=2)

imgCall = tk.PhotoImage(file='calling.png')
btnCall = tk.Button(root, image=imgCall, bd=0, bg='black', activebackground='black',cursor='hand2', command=phoneclick)

# money table
imageList = tk.PhotoImage(file='Picture0.png')
imageList1 = tk.PhotoImage(file='Picture1.png')
imageList2 = tk.PhotoImage(file='Picture2.png')
imageList3 = tk.PhotoImage(file='Picture3.png')
imageList4 = tk.PhotoImage(file='Picture4.png')
imageList5 = tk.PhotoImage(file='Picture5.png')
imageList6 = tk.PhotoImage(file='Picture6.png')
imageList7 = tk.PhotoImage(file='Picture7.png')
imageList8 = tk.PhotoImage(file='Picture8.png')

amountImage = [imageList1,imageList2,imageList3,imageList4,imageList5,imageList6,imageList7,imageList8]

imgList = tk.Label(rightFrame, image= imageList, bg = 'black',bd =0)
imgList.grid(row=0,column=0)

# questions
imageLayout = tk.PhotoImage(file = 'lay.png')
imgLayout = tk.Label(bottomFrame, image=imageLayout, bg = 'black')
imgLayout.grid(row=0, column = 0)

#--------------------------------------------------Million questions--------------------
# function to be called when the selection changes
def select(event):
    btnCall.place_forget()
    progressbarA.place_forget()
    progressbarB.place_forget()
    progressbarC.place_forget()
    progressbarD.place_forget()

    lblprogressbarA.place_forget()
    lblprogressbarB.place_forget()
    lblprogressbarC.place_forget()
    lblprogressbarD.place_forget()

    b=event.widget
    value=b['text']
    for i in range(8):
        if value==correct_answers[i]:
            if value == correct_answers[7]:
                def close():
                    root2.destroy()
                    root.destroy()

                def playagain():
                    live50_50.config(state=NORMAL, image = image50_50)
                    livePeople.config(state=NORMAL, image = imagePeople)
                    livePhone.config(state=NORMAL, image = imagePhone)
                    root2.destroy()
                    quesArea.delete(1.0, tk.END)
                    quesArea.insert(tk.END, questions[0])

                    btnOption1.config(text=first_option[0])
                    btnOption2.config(text=second_option[0])
                    btnOption3.config(text=third_option[0])
                    btnOption4.config(text=fourth_option[0])
                    imgList.config(image=imageList)

                mixer.music.load('kbcwon.mp3')
                mixer.music.play()
                root2 = tk.Toplevel()
                root2.overrideredirect(True)
                root2.config(bg="black")
                root2.geometry("1270x652+10+30")
                root2.title("You won 0 VND")
                lblImage = tk.Label(root2, image= centerImage, bd = 0, bg='black')
                lblImage.pack(pady=30)

                lblWin = tk.Label(root2, text="You won", font=('arial',50,'bold'), bg="black", fg='white')
                lblWin.pack()

                btnPlayAgain = tk.Button(root2, text="Play again", font=('arial',30,"bold"),bg="black",fg="white",
                                        activebackground="black", activeforeground="white", bd =0, cursor="hand2", command=playagain)
                btnPlayAgain.pack(side = 'top', fill = 'both', expand = 'yes')

                btnClose = tk.Button(root2, text="close", font=('arial',30,"bold"),bg="black",fg="white",
                                    activebackground="black", activeforeground="white", bd =0, cursor="hand2", command=close)
                btnClose.pack(side = 'bottom', fill = 'both', expand = 'yes')

                root2.mainloop()
                break
            # next question, answers
            quesArea.delete("1.0", tk.END)
            quesArea.insert(tk.END, questions[i+1])

            btnOption1.config(text=first_option[i+1])
            btnOption2.config(text=second_option[i+1])
            btnOption3.config(text=third_option[i+1])
            btnOption4.config(text=fourth_option[i+1])
            imgList.config(image=amountImage[i])

        if value not in correct_answers:
            def close():
                root1.destroy()
                root.destroy()

            def tryagain():
                live50_50.config(state=NORMAL, image = image50_50)
                livePeople.config(state=NORMAL, image = imagePeople)
                livePhone.config(state=NORMAL, image = imagePhone)

                root1.destroy()
                quesArea.delete(1.0, tk.END)
                quesArea.insert(tk.END, questions[0])

                btnOption1.config(text=first_option[0])
                btnOption2.config(text=second_option[0])
                btnOption3.config(text=third_option[0])
                btnOption4.config(text=fourth_option[0])
                imgList.config(image=imageList)

            root1 = tk.Toplevel()
            root1.overrideredirect(True)
            root1.config(bg="black")
            root1.geometry("1270x652+10+30")
            root1.title("You won 0 VND")
            lblImage = tk.Label(root1, image= centerImage, bd = 0, bg='black')
            lblImage.pack(pady=30)

            lblLose = tk.Label(root1, text="You lose", font=('arial',50,'bold'), bg="black", fg='white')
            lblLose.pack()

            btnTryAgain = tk.Button(root1, text="Try again", font=('arial',30,"bold"),bg="black",fg="white",
                                    activebackground="black", activeforeground="white", bd =0, cursor="hand2", command=tryagain)
            btnTryAgain.pack(side = 'top', fill = 'both', expand = 'yes')

            btnClose = tk.Button(root1, text="close", font=('arial',30,"bold"),bg="black",fg="white",
                                activebackground="black", activeforeground="white", bd =0, cursor="hand2", command=close)
            btnClose.pack(side = 'bottom', fill = 'both', expand = 'yes')

            root1.mainloop()
            break

correct_answers = ["Phụ nữ phải được đối xử như nam giới trừ khi áp dụng các trường hợp đặc biệt",
                   "Nội trợ",
                   "Xử lý kỷ luật, xử lý hành chính hoặc bị truy cứu trách nhiệm hình sự",
                   "Thứ 2",
                   "Lựa chọn giới tính thai nhi",
                   "Tổng đài quốc gia bảo vệ trẻ em",
                   "Khi đến nhà cô giáo, cả nam và nữ cùng giúp cô phần nội trợ",
                   "40/53"]

questions = ["Bình đẳng giới nghĩa là gì?",
            "Ở nhiều quốc gia, hoạt động nào chiếm nhiều thời gian của phụ nữ mà nam giới không phải giải quyết?",
            "Luật Bình đẳng giới quy định hình thức xử lý vi phạm pháp luật về bình đẳng giới như thế nào?",
            "Năm 2020, Việt Nam xếp thứ bao nhiêu trong khu vực ASEAN về chỉ số cam kết giảm bất bình đẳng?",
            "Hành vi nào vi phạm pháp luật về bình đẳng giới trong lĩnh vực y tế?",
            "111 là hotline của tổng đài nào",
            "Câu nào sau đây thể hiện bình đẳng giới?",
            "Bao nhiêu dân tộc thiểu số ở nước ta có tỷ lệ tảo hôn từ 20% trở lên?"]

first_option = ["Tất cả phụ nữ đều được đối xử như nhau",
                "Đi học",
                "Góp ý, phê bình",
                "Thứ 2",
                "Lựa chọn giới tính thai nhi",
                "Tổng đài quốc gia bảo vệ trẻ em",
                "Cô chủ nhiệm nói 'Chỉ con gái được để tóc dài', yêu cầu bạn nam để tóc dài trong lớp phải cắt tóc ngắn",
                "40/53"]
second_option = ["Tất cả nam giới đều được đối xử như nhau",
                "Nội trợ",
                "Cảnh cáo, khiển trách",
                "Thứ 7",
                "Thực hiện phong tục tập quán lạc hậu mang tính phân biệt đối xử về giới dưới mọi hình thức",
                "Tổng đài  yêu cầu trợ giúp và tìm kiếm cứu nạn trên phạm vi toàn quốc",
                "Những bạn nam cười đùa, dùng từ ngữ 'đầy đủ điện nước'khi bắt gặp bạn nữ có thân hình đầy đặn",
                "37/53"]
third_option = ["Phụ nữ phải được đối xử như nam giới trừ khi áp dụng các trường hợp đặc biệt",
                "Kiếm thu nhập",
                "Xử lý kỷ luật, xử lý hành chính hoặc bị truy cứu trách nhiệm hình sự",
                "Thứ 4",
                "Từ chối tuyển dụng những người phù hợp vì lý do giới tính hoặc do việc mang thai/sinh con/có con nhỏ",
                "Tổng đài cứu hóa",
                "Hình ảnh minh họa sách giáo khoa luôn lựa chọn nam giới cho các nghề bác sĩ/công an còn nữ làm nội trợ",
                "16/53"]
fourth_option = ["Phụ nữ nên được đối xử như nam giới.",
                "Làm vườn",
                "Chưa có hình thức xử lý",
                "Thứ 5",
                "Cho thôi việc người lao động vì lý do mang thai",
                "Tổng đài cấp cứu",
                "Khi đến nhà cô giáo, cả nam và nữ cùng giúp cô phần nội trợ",
                "28/53"]

# question area
quesArea = tk.Text(bottomFrame, font=('arial',10,'bold'), width = 50, height=3, wrap='word', bg='black',
fg='white', bd=0)
quesArea.place(x =70, y = 10)

quesArea.insert(tk.END, questions[0])

# answer A B C D
lblA = tk.Label(bottomFrame, text='A: ', bg='black', fg ='white', font=('arial',10,'bold'))
lblA.place(x=60,y=110)

btnOption1 = tk.Button(bottomFrame, text=first_option[0], font=('arial',8,'bold'), bg = 'black', fg = 'white', bd = 0, activebackground='black',
activeforeground='white', cursor="hand2", wraplength=150, justify=LEFT)
btnOption1.place(x=100, y =100)

lblB = tk.Label(bottomFrame, text='B: ', bg='black', fg ='white', font=('arial',10,'bold'))
lblB.place(x=330,y=110)

btnOption2 = tk.Button(bottomFrame, text=second_option[0], font=('arial',8,'bold'), bg = 'black', fg = 'white', bd = 0, activebackground='black',
activeforeground='white', cursor="hand2", wraplength=150, justify=LEFT)
btnOption2.place(x=370, y =100)

lblC = tk.Label(bottomFrame, text='C: ', bg='black', fg ='white', font=('arial',10,'bold'))
lblC.place(x=60,y=190)

btnOption3 = tk.Button(bottomFrame, text=third_option[0], font=('arial',8,'bold'), bg = 'black', fg = 'white', bd = 0, activebackground='black',
activeforeground='white', cursor="hand2", wraplength=150, justify=LEFT)
btnOption3.place(x=100, y =180)

lblD = tk.Label(bottomFrame, text='D: ', bg='black', fg ='white', font=('arial',10,'bold'))
lblD.place(x=330,y=190)

btnOption4 = tk.Button(bottomFrame, text=fourth_option[0], font=('arial',8,'bold'), bg = 'black', fg = 'white', bd = 0, activebackground='black',
activeforeground='white', cursor="hand2", wraplength=150, justify=LEFT)
btnOption4.place(x=370, y =180)
#--------------------------------------------------progressbar--------------------
progressbarA = Progressbar(root, orient=VERTICAL, length=120)
progressbarB = Progressbar(root, orient=VERTICAL, length=120)
progressbarC = Progressbar(root, orient=VERTICAL, length=120)
progressbarD = Progressbar(root, orient=VERTICAL, length=120)

lblprogressbarA = tk.Label(root, text ='A', font=('arial', 20,'bold'), bg='black',fg='white')
lblprogressbarB = tk.Label(root, text ='B', font=('arial', 20,'bold'), bg='black',fg='white')
lblprogressbarC = tk.Label(root, text ='C', font=('arial', 20,'bold'), bg='black',fg='white')
lblprogressbarD = tk.Label(root, text ='D', font=('arial', 20,'bold'), bg='black',fg='white')

btnOption1.bind("<Button-1>", select)
btnOption2.bind("<Button-1>", select)
btnOption3.bind("<Button-1>", select)
btnOption4.bind("<Button-1>", select)

#excecute tkinter
root.mainloop()


"""empty message

Revision ID: cffecda0acbb
Revises: 70e1cf8d7a57
Create Date: 2019-06-06 10:41:47.728713

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "cffecda0acbb"
down_revision = "70e1cf8d7a57"
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "users",
        sa.Column(
            "keypair_seed", sa.String(length=128), server_default="Null", nullable=True
        ),
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column("users", "keypair_seed")
    # ### end Alembic commands ###


import tensorflow as tf

slim = tf.contrib.slim

class Preprocessor:
    def __init__(self, target_shape, crop_size, augment_color=False, aspect_ratio_range=(0.7, 1.3), area_range=(0.5, 1.0)):
        self.tile_shape = target_shape
        self.crop_size = crop_size
        self.augment_color = augment_color
        self.aspect_ratio_range = aspect_ratio_range
        self.area_range = area_range

    def crop_3x3_tile_block(self, image, bbox=(0., 0., 1., 1.)):
        crop_size = [self.crop_size[0], self.crop_size[1], 1]
        sample_distorted_bounding_box = tf.image.sample_distorted_bounding_box(
            tf.shape(image),
            [[bbox]],
            aspect_ratio_range=self.aspect_ratio_range,
            area_range=(0.8, 1.0),
            use_image_if_no_bounding_boxes=True,
            min_object_covered=0.5)
        bbox_begin, bbox_size, distort_bbox = sample_distorted_bounding_box

        # Crop the image to the specified bounding box.
        image = tf.slice(image, bbox_begin, bbox_size)
        image = tf.expand_dims(image, 0)

        resized_image = tf.cond(
            tf.random_uniform(shape=(), minval=0.0, maxval=1.0) > 0.5,
            true_fn=lambda: tf.image.resize_bilinear(image, self.crop_size, align_corners=False),
            false_fn=lambda: tf.image.resize_bicubic(image, self.crop_size, align_corners=False))
        image = tf.squeeze(resized_image, axis=0)
        image.set_shape(crop_size)
        return image

    def extract_tiles(self, image):
        tiles = []
        dx = self.crop_size[0]/3
        dy = self.crop_size[1]/3
        for x in range(3):
            for y in range(3):
                tile = tf.slice(image, [dx*x, dy*y, 0], [dx, dy, 1])
                tile = self.extract_random_patch(tile)
                tiles.append(tile)
        return tiles

    def central_crop(self, image):
        # Crop the central region of the image with an area containing 85% of the original image.
        image = tf.image.central_crop(image, central_fraction=0.85)

        # Resize the image to the original height and width.
        image = tf.expand_dims(image, 0)
        image = tf.image.resize_bilinear(image, [self.tile_shape[0], self.tile_shape[1]], align_corners=False)
        image = tf.squeeze(image, [0])

        # Resize to output size
        image.set_shape([self.tile_shape[0], self.tile_shape[1], 3])
        return image

    def extract_random_patch(self, image, bbox=(0., 0., 1., 1.)):
        image = tf.cond(
            tf.random_uniform(shape=(), minval=0.0, maxval=1.0) > 0.5,
            true_fn=lambda: tf.contrib.image.rotate(image, tf.random_uniform((1,), minval=-0.2, maxval=0.2),
                                                    interpolation='BILINEAR'),
            false_fn=lambda: image)

        sample_distorted_bounding_box = tf.image.sample_distorted_bounding_box(
            tf.shape(image),
            [[bbox]],
            aspect_ratio_range=self.aspect_ratio_range,
            area_range=self.area_range,
            use_image_if_no_bounding_boxes=True,
            min_object_covered=0.01)
        bbox_begin, bbox_size, distort_bbox = sample_distorted_bounding_box

        # Crop the image to the specified bounding box.
        image = tf.slice(image, bbox_begin, bbox_size)
        image = tf.expand_dims(image, 0)

        resized_image = tf.cond(
            tf.random_uniform(shape=(), minval=0.0, maxval=1.0) > 0.5,
            true_fn=lambda: tf.image.resize_bilinear(image, self.tile_shape[:2], align_corners=False),
            false_fn=lambda: tf.image.resize_bicubic(image, self.tile_shape[:2], align_corners=False))
        image = tf.squeeze(resized_image, axis=0)
        image.set_shape(self.tile_shape)
        return image

    def color_augment_and_scale(self, image):
        image = tf.to_float(image) / 255.
        image = tf.image.rgb_to_grayscale(image)
        image = tf.to_float(image) * 2. - 1.
        return image

    def process_train(self, image):
        image = self.color_augment_and_scale(image)
        tile_block = self.crop_3x3_tile_block(image)
        tile_block = tf.image.random_flip_left_right(tile_block)
        print('img block: {}'.format(tile_block.get_shape().as_list()))
        tf.summary.image('input/block', tf.expand_dims(tile_block, 0), max_outputs=1)
        tiles = self.extract_tiles(tile_block)
        print('tile {}'.format(tiles[0].get_shape().as_list()))
        for i, tile in enumerate(tiles):
            tf.summary.image('imgs/tile_{}'.format(i), tf.expand_dims(tile, 0), max_outputs=1)

        tiles = tf.stack(tiles)
        print('tiles: {}'.format(tiles.get_shape().as_list()))
        return tiles

    def process_test(self, image):
        image = self.central_crop(image)
        image = self.color_augment_and_scale(image)
        image = tf.image.random_flip_left_right(image)
        return image


#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""Report on the current list of projects"""

import copy
from docutils import nodes
from docutils.parsers import rst
from docutils import statemachine
import os
from sphinx.util import logging
from sphinx.util.nodes import nested_parse_with_titles
import yaml
import yamlordereddictloader

LOG = logging.getLogger(__name__)

IRC_LOG_URL_BASE = 'http://eavesdrop.openstack.org/irclogs/%23'

_projects_yaml = {}

def _get_project_data():
    """Return a copy of the project data."""
    return copy.deepcopy(_projects_yaml)

def _load_project_file(filename='reference/tsc/projects.yaml'):
    with open(filename, 'r', encoding='utf-8') as f:
        return yaml.load(
            f.read(),
            Loader=yamlordereddictloader.Loader,
        )

def _project_to_rst(name, info):

    if 'service' in info:
        title = "{0} ({1})".format(name.title(), info['service'])
    elif name == 'I18n':
        title = name
    else:
        title = name.title()

    yield '.. _project-%s:' % _slugify(name)
    yield ''
    yield '=' * len(title)
    yield title
    yield '=' * len(title)
    yield ''
    yield ':Home Page: ' + info.get('url', '')
    tl = info.get('tl', {'name': '', 'irc': '', 'email': ''})
    yield ':Technical Lead: %(name)s (``%(irc)s``) <%(email)s>' % tl
    pl = info.get('pl', {'name': '', 'irc': '', 'email': ''})
    yield ':Project Lead: %(name)s (``%(irc)s``) <%(email)s>' % pl
    irc_channel = info.get('irc-channel')
    if irc_channel:
        yield ':IRC Channel: `#%s <%s%s>`__' % (
            irc_channel, IRC_LOG_URL_BASE, irc_channel)
    service = info.get('service')
    if service:
        yield ':Service: ' + service
    yield ''
    mission = info.get('mission', '').rstrip()
    if mission:
        yield "Mission"
        yield '-------'
        yield ''
        yield mission
        yield ''
    yield 'Deliverables'
    yield '------------'
    yield ''
    deliverables = info.get('deliverables', [])
    if deliverables:
        for repo_name, deliverable in deliverables.items():
            yield repo_name
            yield '~' * len(repo_name)
            yield ''
            yield ':Repositories: ' + ', '.join(
                ':repo:`%s`' % repo
                for repo in deliverable.get('repos', [])
            )
            yield ''
            tags = deliverable.get('tags', [])
            if tags:
                yield ':Tags:'
                yield ''
                for tag in tags:
                    yield '  - :ref:`tag-%s`' % tag
                yield ''
    else:
        yield 'None'
    yield ''

def _slugify(name):
    """Convert name to slug form for references."""
    return name.lower().replace(' ', '-')

def _write_project_pages(app):
    all_projects = _get_project_data()
    files = []
    for project, info in all_projects.items():
        LOG.info("project: %s" % project)
        slug = _slugify(project)
        filename = 'reference/tsc/projects/%s.rst' % slug
        LOG.info('generating project page for %s' % project)
        with open(filename, 'w', encoding='utf-8') as f:
            f.write('\n'.join(_project_to_rst(project, info)))
        files.append(filename)
    return files

class ProjectListDirective(rst.Directive):

    has_content = False

    def run(self):
        all_projects = _get_project_data()

        # Build the view of the data to be parsed for rendering.
        result = statemachine.ViewList()
        for project_name in sorted(all_projects.keys()):
            project_info = all_projects[project_name]
            for line in _project_to_rst(project_name, project_info):
                result.append(line, '<' + __name__ + '>')

        # Parse what we have into a new section.
        node = nodes.section()
        node.document = self.state.document
        nested_parse_with_titles(self.state, result, node)

        return node.children

def setup(app):
    global _projects_yaml

    LOG.info('loading projects extension')
    app.add_directive('projectlist', ProjectListDirective)

    filename = os.path.abspath('reference/tsc/projects.yaml')
    LOG.info('reading %s' % filename)
    _projects_yaml = _load_project_file(filename)
    _write_project_pages(app)


from dataclasses import dataclass
from typing import Callable, Dict, List, Tuple, Union
from raytkUtil import RaytkContext

# noinspection PyUnreachableCode
if False:
	# noinspection PyUnresolvedReferences
	from _stubs import *

	class _ConfigPar(ParCollection):
		Parammode: 'Union[str, Par]'
		Inlineparameteraliases: 'Union[bool, Par]'
		Inlinereadonlyparameters: 'Union[bool, Par]'
		Simplifynames: 'Union[bool, Par]'
		Generatetypedefs: 'Union[bool, Par]'
		Includemode: 'Union[str, Par]'

	class _OwnerCompPar(_ConfigPar):
		Globalprefix: 'Union[DAT, str, Par]'
		Predeclarations: 'Union[DAT, str, Par]'
		Textureindexoffset: 'Union[int, Par]'
		Globalmacrotable: 'Union[DAT, str, Par]'
		Libraries: 'Union[str, Par]'
		Bodytemplate: 'Union[DAT, str, Par]'
		Outputbuffertable: 'Union[DAT, str, Par]'
		Supportmaterials: 'Union[bool, Par]'
		Shaderbuilderconfig: 'Union[COMP, str, Par]'

	class _OwnerComp(COMP):
		par: '_OwnerCompPar'

	class _ConfigComp(COMP):
		par: '_ConfigPar'

class ShaderBuilder:
	def __init__(self, ownerComp: '_OwnerComp'):
		self.ownerComp = ownerComp

	def configPar(self) -> '_ConfigPar':
		p = self.ownerComp.par['Shaderbuilderconfig']
		if p:
			o = op(p)
			if o:
				# noinspection PyTypeChecker
				return o.par
		return self.ownerComp.par

	def preprocessDefinitions(self, dat: 'scriptDAT'):
		# BEFORE definitions are reversed, so a def's inputs are always BELOW it in the table
		pass

	def definitionTable(self) -> 'DAT':
		# in reverse order (aka declaration order)
		return self.ownerComp.op('definitions')

	def parameterDetailTable(self) -> 'DAT':
		return self.ownerComp.op('param_details')

	def outputBufferTable(self) -> 'DAT':
		return self.ownerComp.op('output_buffer_table')

	def allParamVals(self) -> 'CHOP':
		return self.ownerComp.op('all_param_vals')

	def buildGlobalPrefix(self):
		return wrapCodeSection(self.ownerComp.par.Globalprefix.eval(), 'globalPrefix')

	def _createParamProcessor(self) -> '_ParameterProcessor':
		mode = self.configPar().Parammode.eval()
		if mode == 'uniformarray':
			return _VectorArrayParameterProcessor(
				self.parameterDetailTable(),
				self.allParamVals(),
				self.configPar(),
			)
		else:
			raise NotImplementedError(f'Parameter processor not available for mode: {mode!r}')

	def buildGlobalDeclarations(self):
		defsTable = self.definitionTable()
		if defsTable.numRows < 2:
			code = ['#error No input definition']
		else:
			mainName = defsTable[defsTable.numRows - 1, 'name']
			paramProcessor = self._createParamProcessor()
			code = paramProcessor.globalDeclarations()
			code += [
				f'#define thismap {mainName}'
			]
		return wrapCodeSection(code, 'globals')

	def getOpsFromDefinitionColumn(self, column: str):
		defsTable = self.definitionTable()
		if defsTable.numRows < 2 or not defsTable[0, column]:
			return []
		results = []
		for cell in defsTable.col(column)[1:]:
			if not cell.val.strip():
				continue
			paths = cell.val.strip().split(' ')
			for path in paths:
				o = op(path)
				if o:
					results.append(o)
		return results

	def _getMacros(self) -> 'List[Tuple[str, str]]':
		tables = [self.ownerComp.par.Globalmacrotable.eval()]
		tables += self.getOpsFromDefinitionColumn('macroTable')
		namesAndVals = []
		for table in tables:
			if not table:
				continue
			for row in range(table.numRows):
				if table.numCols == 3:
					if table[row, 0].val in ('0', 'False'):
						continue
					name = table[row, 1].val
					value = table[row, 2].val
				else:
					name = table[row, 0].val
					if table.numCols > 1:
						value = table[row, 1].val
					else:
						value = ''
				if value:
					value = ' ' + value
				if not name.strip():
					continue
				namesAndVals.append((name, value))
		outputBuffers = self.outputBufferTable()
		if outputBuffers.numRows > 1 and outputBuffers.col('macro'):
			for cell in outputBuffers.col('macro')[1:]:
				if cell.val:
					namesAndVals.append((cell.val, ''))
		return namesAndVals

	def buildMacroTable(self, dat: 'DAT'):
		dat.clear()
		dat.appendRows([
			[name, value]
			for name, value in self._getMacros()
		])

	def buildMacroBlock(self):
		decls = []
		for name, value in self._getMacros():
			if name.startswith('#define'):
				decls.append(name + value)
			else:
				decls.append(f'#define {name} {value}')
		decls = _uniqueList(decls)
		code = wrapCodeSection(decls, 'macros')
		# if self.configPar().Inlineparameteraliases:
		# 	processor = self._createParamProcessor()
		# 	return processor.processCodeBlock(code)
		return code

	def getLibraryDats(self, onWarning: Callable[[str], None] = None) -> 'List[DAT]':
		requiredLibNames = self.ownerComp.par.Librarynames.eval().strip().split(' ')  # type: List[str]
		requiredLibNames = [n for n in requiredLibNames if n]
		defsTable = self.definitionTable()
		if defsTable[0, 'libraryNames']:
			for cell in defsTable.col('libraryNames')[1:]:
				if not cell.val:
					continue
				for name in cell.val.split(' '):
					if name not in requiredLibNames:
						requiredLibNames.append(name)
		libraryOps = self.ownerComp.par.Libraries.evalOPs()
		dats = []  # type: List[DAT]
		for libraryOp in libraryOps:
			if libraryOp.isDAT:
				if libraryOp not in dats:
					dats.append(libraryOp)
			elif libraryOp.isCOMP:
				namesToRemove = []
				for name in requiredLibNames:
					dat = libraryOp.op(name)
					if not dat:
						continue
					dats.append(dat)
					namesToRemove.append(name)
				for name in namesToRemove:
					requiredLibNames.remove(name)
		if requiredLibNames and onWarning:
			onWarning('Missing libraries: ' + repr(requiredLibNames))
		return dats

	def buildLibraryIncludes(self, onWarning: Callable[[str], None] = None):
		mode = str(self.configPar()['Includemode'] or 'includelibs')
		supportsInclude = self.ownerComp.op('support_table')['include', 1] == '1'
		if mode == 'includelibs' and not supportsInclude:
			inlineAll = True
		else:
			inlineAll = mode == 'inlineall'
		libraries = self.getLibraryDats(onWarning)
		if inlineAll:
			libBlocks = [
				f'// Library: <{lib.path}>\n{lib.text}'
				for lib in libraries
			]
		else:
			libBlocks = [
				f'#include <{lib.path}>'
				for lib in libraries
			]
		return wrapCodeSection(libBlocks, 'libraries')

	def buildOpDataTypedefBlock(self):
		defsTable = self.definitionTable()
		typedefs = []
		macros = []
		coordTypeAdaptFuncs = {
			'float': 'adaptAsFloat',
			'vec2': 'adaptAsVec2',
			'vec3': 'adaptAsVec3',
		}
		returnTypeAdaptFuncs = {
			'float': 'adaptAsFloat',
			'vec4': 'adaptAsVec4',
		}
		for row in range(1, defsTable.numRows):
			name = str(defsTable[row, 'name'])
			coordType = str(defsTable[row, 'coordType'])
			contextType = str(defsTable[row, 'contextType'])
			returnType = str(defsTable[row, 'returnType'])
			typedefs += [
				f'#define {name}_CoordT    {coordType}',
				f'#define {name}_ContextT  {contextType}',
				f'#define {name}_ReturnT   {returnType}',
			]
			macros += [
				f'#define {name}_COORD_TYPE_{coordType}',
				f'#define {name}_CONTEXT_TYPE_{contextType}',
				f'#define {name}_RETURN_TYPE_{returnType}',
				f'#define {name}_asCoordT {coordTypeAdaptFuncs[coordType]}',
			]
			if returnType in returnTypeAdaptFuncs:
				macros.append(f'#define {name}_asReturnT {returnTypeAdaptFuncs[returnType]}')
		if typedefs:
			lines = typedefs + [''] + macros
		else:
			lines = []
		return wrapCodeSection(lines, 'opDataTypedefs')

	def buildPredeclarations(self):
		return wrapCodeSection(self.ownerComp.par.Predeclarations.eval(), 'predeclarations')

	def _buildParameterExprs(self) -> 'List[Tuple[str, Union[str, float]]]':
		paramDetails = self.parameterDetailTable()
		if paramDetails.numRows < 2:
			return []
		suffixes = 'xyzw'
		partAliases = []  # type: List[Tuple[str, Union[str, float]]]
		mainAliases = []  # type: List[Tuple[str, Union[str, float]]]
		inlineReadOnly = bool(self.configPar()['Inlinereadonlyparameters'])
		paramVals = self.allParamVals()
		paramTuplets = _ParamTupletSpec.fromTableRows(paramDetails)
		for i, paramTuplet in enumerate(paramTuplets):
			shouldInline = inlineReadOnly and paramTuplet.isReadOnly and paramTuplet.isPresentInChop(paramVals)
			size = len(paramTuplet.parts)
			if size == 1:
				if shouldInline:
					mainAliases.append((paramTuplet.parts[0], float(paramVals[paramTuplet.parts[0]])))
				else:
					mainAliases.append((paramTuplet.parts[0], f'vecParams[{i}].x'))
			else:
				if shouldInline:
					partVals = [float(paramVals[part]) for part in paramTuplet.parts]
					valsExpr = ','.join(str(v) for v in partVals)
					mainAliases.append((paramTuplet.tuplet, f'vec{size}({valsExpr})'))
					for partI, partVal in enumerate(partVals):
						partAliases.append((paramTuplet.parts[partI], partVal))
				else:
					if size == 4:
						mainAliases.append((paramTuplet.tuplet, f'vecParams[{i}]'))
					else:
						mainAliases.append((paramTuplet.tuplet, f'vec{size}(vecParams[{i}].{suffixes[:size]})'))
					for partI, partName in enumerate(paramTuplet.parts):
						partAliases.append((partName, f'vecParams[{i}].{suffixes[partI]}'))
		return partAliases + mainAliases

	def buildParameterAliases(self):
		paramProcessor = self._createParamProcessor()
		decls = paramProcessor.paramAliases()
		return wrapCodeSection(decls, 'paramAliases')

	def processParametersInCode(self, code: str):
		paramProcessor = self._createParamProcessor()
		return paramProcessor.processCodeBlock(code)

	def buildTextureDeclarations(self):
		textureTable = self.ownerComp.op('texture_table')
		offset = int(self.ownerComp.par.Textureindexoffset)
		indexByType = {
			'2d': offset,
			'3d': 0,
			'cube': 0,
		}
		arrayByType = {
			'2d': 'sTD2DInputs',
			'3d': 'sTD3DInputs',
			'cube': 'sTDCubeInputs',
		}
		decls = []
		for i in range(1, textureTable.numRows):
			name = str(textureTable[i, 'name'])
			texType = str(textureTable[i, 'type'] or '2d')
			if texType not in indexByType:
				raise Exception(f'Invalid texture type for {name}: {texType!r}')
			index = indexByType[texType]
			decls.append(f'#define {name} {arrayByType[texType]}[{index}]')
			indexByType[texType] = index + 1
		return wrapCodeSection(decls, 'textures')

	def buildBufferDeclarations(self):
		bufferTable = self.ownerComp.op('buffer_table')
		decls = []
		for i in range(1, bufferTable.numRows):
			name = bufferTable[i, 'name']
			dataType = bufferTable[i, 'type']
			uniType = bufferTable[i, 'uniformType']
			n = int(bufferTable[i, 'length'])
			if uniType == 'uniformarray':
				decls.append(f'uniform {dataType} {name}[{n}];')
			elif uniType == 'texturebuffer':
				decls.append(f'uniform samplerBuffer {name};')
		return wrapCodeSection(decls, 'buffers')

	def buildMaterialDeclarations(self):
		if not self.ownerComp.par.Supportmaterials:
			return ' '
		materialTable = self.ownerComp.op('material_table')
		if materialTable.numRows < 2:
			return ' '
		i = 1001
		decls = []
		for name in materialTable.col('material')[1:]:
			decls.append(f'#define {name} {i}')
			i += 1
		return wrapCodeSection(decls, 'materials')

	def buildOutputBufferDeclarations(self):
		outputBuffers = self.outputBufferTable()
		if outputBuffers.numRows < 2:
			return ' '
		decls = [
			f'layout(location = {cell.row - 1}) out vec4 {cell.val};'
			for cell in outputBuffers.col('name')[1:]
		]
		return wrapCodeSection(decls, 'outputBuffers')

	def buildOutputInitBlock(self):
		outputBuffers = self.outputBufferTable()
		return wrapCodeSection(
			[
				'void initOutputs() {'
			] +
			[
				f'{cell.val} = vec4(0.);'
				for cell in outputBuffers.col('name')[1:]
			] + [
				'}'
			],
			'outputInit',
		)

	def buildOpGlobalsBlock(self):
		dats = self.getOpsFromDefinitionColumn('opGlobalsPath')
		return wrapCodeSection(dats, 'opGlobals')

	def buildInitBlock(self):
		dats = self.getOpsFromDefinitionColumn('initPath')
		code = _combineCode(dats)
		if not code.strip():
			return ' '
		return wrapCodeSection([
			'#define RAYTK_HAS_INIT',
			'void init() {',
			code,
			'}',
		], 'init')

	def buildFunctionsBlock(self):
		dats = self.getOpsFromDefinitionColumn('functionPath')
		return wrapCodeSection(dats, 'functions')

	def buildBodyBlock(self, materialTable: 'DAT'):
		bodyDat = self.ownerComp.par.Bodytemplate.eval()
		code = bodyDat.text if bodyDat else ''
		if not code:
			return ' '
		if _materialParagraphPlaceholder in code:
			materialBlock = self._buildMaterialBlock(materialTable)
			code = code.replace(_materialParagraphPlaceholder, materialBlock, 1)
		return wrapCodeSection(code, 'body')

	@staticmethod
	def _buildMaterialBlock(materialTable: 'DAT'):
		if materialTable.numRows < 2:
			return ''
		output = ''
		for nameCell, pathCell in materialTable.rows()[1:]:
			if not nameCell:
				continue
			codeDat = op(pathCell)
			materialCode = codeDat.text if codeDat else ''
			output += f'else if(m == {nameCell.val}) {{\n'
			output += materialCode + '\n}'
		return output

	def buildValidationErrors(self, dat: 'DAT'):
		dat.clear()
		if RaytkContext().develMode():
			return
		toolkitVersions = {}  # type: Dict[str, int]
		defsTable = self.definitionTable()
		if defsTable.numRows < 2 or not defsTable[0, 'toolkitVersion']:
			return
		for i in range(1, defsTable.numRows):
			version = str(defsTable[i, 'toolkitVersion'] or '')
			if version != '':
				toolkitVersions[version] = 1 + toolkitVersions.get(version, 0)
		if len(toolkitVersions) > 1:
			error = f'Toolkit version mismatch ({", ".join(list(toolkitVersions.keys()))})'
			dat.appendRow(['path', 'level', 'message'])
			dat.appendRow([parent().path, 'warning', error])

_materialParagraphPlaceholder = '// #include <materialParagraph>'

@dataclass
class _ParamTupletSpec:
	tuplet: str
	parts: Tuple[str]
	isReadOnly: bool

	def isPresentInChop(self, chop: 'CHOP'):
		return any([chop[part] is not None for part in self.parts])

	@classmethod
	def fromRow(cls, dat: 'DAT', row: int):
		parts = []
		for i in range(1, 5):
			cell = dat[row, 'part' + str(i)]
			if not cell.val:
				break
			parts.append(cell.val)
		return cls(
			tuplet=str(dat[row, 'tuplet']),
			parts=tuple(parts),
			isReadOnly='readOnly' in str(dat[row, 'status'] or ''),
		)

	@classmethod
	def fromTableRows(cls, dat: 'DAT') -> 'List[_ParamTupletSpec]':
		if not dat or dat.numRows < 2:
			return []
		return [
			cls.fromRow(dat, row)
			for row in range(1, dat.numRows)
		]

@dataclass
class _ParamExpr:
	name: str
	expr: Union[str, float]
	type: str

@dataclass
class _UniformSpec:
	pass

class _ParameterProcessor:
	def __init__(
			self,
			paramDetailTable: 'DAT',
			paramVals: 'CHOP',
			configPar: '_ConfigPar',
	):
		self.paramDetailTable = paramDetailTable
		self.hasParams = paramDetailTable.numRows > 1
		self.useConstantReadOnly = configPar.Inlinereadonlyparameters
		self.inlineAliases = configPar.Inlineparameteraliases
		self.paramVals = paramVals
		self.aliasMode = str(configPar['Paramaliasmode'] or 'macro')

	def globalDeclarations(self) -> List[str]:
		raise NotImplementedError()

	def _generateParamExprs(self) -> List[_ParamExpr]:
		raise NotImplementedError()

	def paramAliases(self) -> List[str]:
		raise NotImplementedError()

	def processCodeBlock(self, code: str) -> str:
		raise NotImplementedError()

class _VectorArrayParameterProcessor(_ParameterProcessor):
	def globalDeclarations(self) -> List[str]:
		paramCount = max(1, self.paramDetailTable.numRows - 1)
		return [
			f'uniform vec4 vecParams[{paramCount}];',
		]

	def _generateParamExprs(self) -> List[_ParamExpr]:
		paramExprs = []  # type: List[_ParamExpr]
		suffixes = 'xyzw'
		paramTuplets = _ParamTupletSpec.fromTableRows(self.paramDetailTable)
		for i, paramTuplet in enumerate(paramTuplets):
			useConstant = self.useConstantReadOnly and paramTuplet.isReadOnly and paramTuplet.isPresentInChop(self.paramVals)
			size = len(paramTuplet.parts)
			if size == 1:
				name = paramTuplet.parts[0]
				paramExprs.append(_ParamExpr(
					name,
					repr(float(self.paramVals[name])) if useConstant else f'vecParams[{i}].x',
					'float'
				))
			else:
				if useConstant:
					partVals = [float(self.paramVals[part]) for part in paramTuplet.parts]
					valsExpr = ','.join(str(v) for v in partVals)
					parType = f'vec{size}'
					paramExprs.append(_ParamExpr(paramTuplet.tuplet, f'{parType}({valsExpr})', parType))
					for partI, partVal in enumerate(partVals):
						paramExprs.append(_ParamExpr(paramTuplet.parts[partI], partVal, 'float'))
				else:
					if size == 4:
						paramExprs.append(_ParamExpr(paramTuplet.tuplet, f'vecParams[{i}]', 'vec4'))
					else:
						parType = f'vec{size}'
						paramExprs.append(_ParamExpr(
							paramTuplet.tuplet,
							f'{parType}(vecParams[{i}].{suffixes[:size]})',
							parType
						))
					for partI, partName in enumerate(paramTuplet.parts):
						paramExprs.append(_ParamExpr(partName, f'vecParams[{i}].{suffixes[partI]}', 'float'))
		return paramExprs

	def paramAliases(self) -> List[str]:
		if not self.hasParams:
			return []
		# if self.inlineAliases:
		# 	return []
		if self.aliasMode == 'globalvar':
			return [
				f'{paramExpr.type} {paramExpr.name} = {paramExpr.expr};'
				for paramExpr in self._generateParamExprs()
			]
		else:
			return [
				f'#define {paramExpr.name} {paramExpr.expr}'
				for paramExpr in self._generateParamExprs()
			]

	def processCodeBlock(self, code: str) -> str:
		if not self.inlineAliases or not code:
			return code
		for paramExpr in self._generateParamExprs():
			code = code.replace(paramExpr.name, paramExpr.expr)
		return code

def _stringify(val: 'Union[str, DAT]'):
	if val is None:
		return ''
	if isinstance(val, DAT):
		return val.text
	return str(val)

def _combineCode(code: 'Union[str, DAT, List[Union[str, DAT]]]'):
	if isinstance(code, list):
		combined = ''
		for item in code:
			val = _stringify(item)
			if val:
				combined += val + '\n'
		return combined
	else:
		return _stringify(code)

def wrapCodeSection(code: 'Union[str, DAT, List[Union[str, DAT]]]', name: str):
	code = _combineCode(code)
	if not code:
		# return a non-empty string in order to force DATs to be text when using dat.write()
		return ' '
	return f'///----BEGIN {name}\n{code}\n///----END {name}'

def updateLibraryMenuPar(libsComp: 'COMP'):
	p = parent().par.Librarynames  # type: Par
	libs = libsComp.findChildren(type=DAT, maxDepth=1, tags=['library'])
	libs.sort(key=lambda l: -l.nodeY)
	p.menuNames = [lib.name for lib in libs]

def _uniqueList(items: list):
	results = []
	for item in items:
		if item not in results:
			results.append(item)
	return results


# coding=utf-8
# coding=utf-8
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tools for analyzing the operations and variables in a TensorFlow graph.

To analyze the operations in a graph:

  images, labels = LoadData(...)
  predictions = MyModel(images)

  slim.model_analyzer.analyze_ops(tf.compat.v1.get_default_graph(),
  print_info=True)

To analyze the model variables in a graph:

  variables = tf.compat.v1.model_variables()
  slim.model_analyzer.analyze_vars(variables, print_info=False)
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

def tensor_description(var):
  """Returns a compact and informative string about a tensor.

  Args:
    var: A tensor variable.

  Returns:
    a string with type and size, e.g.: (float32 1x8x8x1024).
  """
  description = '(' + str(var.dtype.name) + ' '
  sizes = var.get_shape()
  for i, size in enumerate(sizes):
    description += str(size)
    if i < len(sizes) - 1:
      description += 'x'
  description += ')'
  return description

def analyze_ops(graph, print_info=False):
  """Compute the estimated size of the ops.outputs in the graph.

  Args:
    graph: the graph containing the operations.
    print_info: Optional, if true print ops and their outputs.

  Returns:
    total size of the ops.outputs
  """
  if print_info:
    print('---------')
    print('Operations: name -> (type shapes) [size]')
    print('---------')
  total_size = 0
  for op in graph.get_operations():
    op_size = 0
    shapes = []
    for output in op.outputs:
      # if output.num_elements() is None or [] assume size 0.
      output_size = output.get_shape().num_elements() or 0
      if output.get_shape():
        shapes.append(tensor_description(output))
      op_size += output_size
    if print_info:
      print(op.name, '\t->', ', '.join(shapes), '[' + str(op_size) + ']')
    total_size += op_size
  return total_size

def analyze_vars(variables, print_info=False):
  """Prints the names and shapes of the variables.

  Args:
    variables: list of variables, for example tf.compat.v1.global_variables().
    print_info: Optional, if true print variables and their shape.

  Returns:
    (total size of the variables, total bytes of the variables)
  """
  if print_info:
    print('---------')
    print('Variables: name (type shape) [size]')
    print('---------')
  total_size = 0
  total_bytes = 0
  for var in variables:
    # if var.num_elements() is None or [] assume size 0.
    var_size = var.get_shape().num_elements() or 0
    var_bytes = var_size * var.dtype.size
    total_size += var_size
    total_bytes += var_bytes
    if print_info:
      print(var.name, tensor_description(var),
            '[%d, bytes: %d]' % (var_size, var_bytes))
  if print_info:
    print('Total size of variables: %d' % total_size)
    print('Total bytes of variables: %d' % total_bytes)
  return total_size, total_bytes


from midistuff.writers.MidiOutFile import MidiOutFile
from midistuff.readers.MidiInFile import MidiInFile

out_file = 'identitied.mid'
midi_out = MidiOutFile(out_file)

#in_file = 'midiout/minimal_type0.mid'
#in_file = 'test/midifiles/Lola.mid'
in_file = 'ISawHerStandingThere.mid'
midi_in = MidiInFile(midi_out, in_file)
midi_in.read()


import time

import pandas as pd
import re

from selenium import webdriver
from bs4 import BeautifulSoup
from lmf.dbv2 import db_write
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException,StaleElementReferenceException
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import requests
import json

from lch.zhulong import est_tbs,est_meta,est_html

# __conp=["postgres","since2015","192.168.3.171","hunan","changsha"]

# #
# url="http://www.bzztb.gov.cn/BZWZ/jyxx/003001/003001001/003001001001/003001001001001/"
# driver=webdriver.Chrome()
# driver.minimize_window()
# driver.get(url)
#

_name_='daqing'

def f1(driver,num):
    locator = (By.XPATH, '//ul[@class="notice-list lf-list1"]/form/li[1]/a')
    WebDriverWait(driver, 10).until(EC.presence_of_element_located(locator))

    cnum = driver.find_element_by_xpath('//div[@class="pagination"]/a[@class="current"]').text

    if cnum != str(num):
        url = driver.current_url
        if num ==1:
            url = url.rsplit('/', maxsplit=1)[0] + '/index.htm'
        else:
            url = url.rsplit('/', maxsplit=1)[0] + '/index_' + str(num) + '.htm'

        val = driver.find_element_by_xpath('//ul[@class="notice-list lf-list1"]/form/li[1]/a').text

        driver.get(url)

        locator = (By.XPATH, '//ul[@class="notice-list lf-list1"]/form/li[1]/a[not(contains(string(),"%s"))]' % val)
        WebDriverWait(driver, 10).until(EC.presence_of_element_located(locator))

    data = []

    html = driver.page_source
    soup = BeautifulSoup(html, 'lxml')
    div = soup.find('div', class_='infor-con2 on')
    divs = div.find_all('li')

    for li in divs:
        href = li.a['href']
        name = li.a.get_text()
        ggstart_time = li.span.get_text()
        if 'http' in href:
            href = href
        else:
            href = 'http://zfcgzx.daqing.gov.cn' + href
        tmp = [name, ggstart_time, href]

        data.append(tmp)

    df=pd.DataFrame(data=data)
    df["info"] = None
    return df

def f2(driver):
    locator = (By.XPATH, '//ul[@class="notice-list lf-list1"]/form/li[1]/a')
    WebDriverWait(driver, 10).until(EC.presence_of_element_located(locator))

    page = driver.find_element_by_xpath('//div[@class="pagination"]/a[last()]').get_attribute('href')

    page = re.findall('index_(\d+).htm', page)[0]
    total = int(page)
    driver.quit()

    return total

def f3(driver, url):
    driver.get(url)

    locator = (
    By.XPATH, '//div[@class="main"] | //div[@class="tab_content"] | //div[@class="officialDoc"] |'
              ' //table[@class="printTable"] | //div[@class="WordSection1"] | //div[@class="content"] | /html/body/div')

    WebDriverWait(driver, 10).until(EC.presence_of_element_located(locator))

    before = len(driver.page_source)
    time.sleep(0.1)
    after = len(driver.page_source)
    i = 0
    while before != after:
        before = len(driver.page_source)
        time.sleep(0.1)
        after = len(driver.page_source)
        i += 1
        if i > 5: break

    page = driver.page_source

    soup = BeautifulSoup(page, 'lxml')

    div = soup.find('div', class_='tab_content')
    if div == None:
        div = soup.find('div', class_="home-detail")
        if div == None:
            div = soup.find('div', class_="officialDoc")
            if div == None:
                div = soup.find('table',class_="printTable")
                if div == None:
                    div=soup.find('div',class_='WordSection1')
                    if div == None:
                        div=soup.find('div',class_="content")
                        if div == None:
                            div = soup.find('body').find('div',class_=None,recursive=False)

                            if div == None:
                                raise ValueError

                            if 'IP已经过了有效期' in div:
                                raise TimeoutError

    return div

data=[
    ["gcjs_zhaobiao_gg","http://zfcgzx.daqing.gov.cn/jyxxJsgcZbgg/index.htm",["name","ggstart_time","href","info"],f1,f2],
    ["gcjs_chengqingbiangeng_gg","http://zfcgzx.daqing.gov.cn/jyxxJsgcBgcggg/index.htm",["name","ggstart_time","href","info"],f1,f2],
    ###包含中标，中标候选人，放弃中标
    ["gcjs_zhong_gg","http://zfcgzx.daqing.gov.cn/jyxxJsgcZbgs/index.htm",["name","ggstart_time","href","info"],f1,f2],

    ["zfcg_zhaobiao_gg","http://zfcgzx.daqing.gov.cn/jyxxZfcgCggg/index.htm",["name","ggstart_time","href","info"],f1,f2],
    ["zfcg_zhongbiaohx_gg","http://zfcgzx.daqing.gov.cn/jyxxZfcgYzbgg/index.htm",["name","ggstart_time","href","info"],f1,f2],
    ["zfcg_zhongbiao_gg","http://zfcgzx.daqing.gov.cn/jyxxZfcgZbgg/index.htm",["name","ggstart_time","href","info"],f1,f2],

    ["zfcg_liubiao_gg","http://zfcgzx.daqing.gov.cn/jyxxZfcgFbgg/index.htm",["name","ggstart_time","href","info"],f1,f2],

]

def work(conp,**args):
    est_meta(conp,data=data,diqu="黑龙江省大庆市",**args)
    est_html(conp,f=f3,**args)

if __name__=='__main__':
    conp = ["postgres", "since2015", "192.168.3.171", "heilongjiang", "daqing"]
    # conp = ["postgres", "since2015", "192.168.3.171", "test", "lch"]

    work(conp=conp)


from xai.brain.wordbase.nouns._honduran import _HONDURAN

#calss header
class _HONDURANS(_HONDURAN, ):
	def __init__(self,):
		_HONDURAN.__init__(self)
		self.name = "HONDURANS"
		self.specie = 'nouns'
		self.basic = "honduran"
		self.jsondata = {}


import random as rnd

class Maze():

    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.nodes = []
        for x in range(0, width):
            yRow = []
            for y in range(0, height):
                yRow.append(Node(x,y))
            self.nodes.append(yRow)

        self.startX = rnd.randint(0, self.width-1)
        self.startY = rnd.randint(0, self.height-1)

        self.currentNode = self.nodes[self.startX][self.startY]
        self.nodeStack = []
        self.started = False

    def generate(self):
        while self.started == False or len(self.nodeStack) > 0:
            self.generateNextStep()

    def generateNextStep(self):
        if self.started == True and len(self.nodeStack) == 0:
            return None, None

        self.started = True
        self.currentNode.visited = True
        neighbours = list(self.getNonVisitedNeighbourNodes(self.currentNode))
        if(len(neighbours) == 0):
            lastNode = self.currentNode
            self.currentNode = self.nodeStack.pop()
            return lastNode, True

        nextRandom = rnd.randint(0, len(neighbours)-1)
        nextNode = neighbours[nextRandom]
        self.currentNode.nodes.append(nextNode)
        nextNode.nodes.append(self.currentNode)
        self.nodeStack.append(self.currentNode)
        self.currentNode = nextNode
        return self.nodeStack[len(self.nodeStack)-1], False

    def getNonVisitedNeighbourNodes(self, node):
        if node.x > 0 and self.nodes[node.x-1][node.y].visited == False:
            yield self.nodes[node.x-1][node.y]
        if node.x+1 < self.width and self.nodes[node.x+1][node.y].visited == False:
            yield self.nodes[node.x+1][node.y]
        if node.y > 0 and self.nodes[node.x][node.y-1].visited == False:
            yield self.nodes[node.x][node.y-1]
        if node.y+1 < self.height and self.nodes[node.x][node.y+1].visited == False:
            yield self.nodes[node.x][node.y+1]

class Node():
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.visited = False
        self.nodes = []