import time
from collections import defaultdict
from unittest import TextTestResult as _TextTestResult
from unittest import TextTestRunner

from scrapy.commands import ScrapyCommand
from scrapy.contracts import ContractsManager
from scrapy.utils.conf import build_component_list
from scrapy.utils.misc import load_object, set_environ

class TextTestResult(_TextTestResult):
    def printSummary(self, start, stop):
        write = self.stream.write
        writeln = self.stream.writeln

        run = self.testsRun
        plural = "s" if run != 1 else ""

        writeln(self.separator2)
        writeln(f"Ran {run} contract{plural} in {stop - start:.3f}s")
        writeln()

        infos = []
        if not self.wasSuccessful():
            write("FAILED")
            failed, errored = map(len, (self.failures, self.errors))
            if failed:
                infos.append(f"failures={failed}")
            if errored:
                infos.append(f"errors={errored}")
        else:
            write("OK")

        if infos:
            writeln(f" ({', '.join(infos)})")
        else:
            write("\n")

class Command(ScrapyCommand):
    requires_project = True
    default_settings = {"LOG_ENABLED": False}

    def syntax(self):
        return "[options] <spider>"

    def short_desc(self):
        return "Check spider contracts"

    def add_options(self, parser):
        ScrapyCommand.add_options(self, parser)
        parser.add_argument(
            "-l",
            "--list",
            dest="list",
            action="store_true",
            help="only list contracts, without checking them",
        )
        parser.add_argument(
            "-v",
            "--verbose",
            dest="verbose",
            default=False,
            action="store_true",
            help="print contract tests for all spiders",
        )

    def run(self, args, opts):
        # load contracts
        contracts = build_component_list(self.settings.getwithbase("SPIDER_CONTRACTS"))
        conman = ContractsManager(load_object(c) for c in contracts)
        runner = TextTestRunner(verbosity=2 if opts.verbose else 1)
        result = TextTestResult(runner.stream, runner.descriptions, runner.verbosity)

        # contract requests
        contract_reqs = defaultdict(list)

        spider_loader = self.crawler_process.spider_loader

        with set_environ(SCRAPY_CHECK="true"):
            for spidername in args or spider_loader.list():
                spidercls = spider_loader.load(spidername)
                spidercls.start_requests = lambda s: conman.from_spider(s, result)

                tested_methods = conman.tested_methods_from_spidercls(spidercls)
                if opts.list:
                    for method in tested_methods:
                        contract_reqs[spidercls.name].append(method)
                elif tested_methods:
                    self.crawler_process.crawl(spidercls)

            # start checks
            if opts.list:
                for spider, methods in sorted(contract_reqs.items()):
                    if not methods and not opts.verbose:
                        continue
                    print(spider)
                    for method in sorted(methods):
                        print(f"  * {method}")
            else:
                start = time.time()
                self.crawler_process.start()
                stop = time.time()

                result.printErrors()
                result.printSummary(start, stop)
                self.exitcode = int(not result.wasSuccessful())


import textwrap

import yaml

import da.util

# -----------------------------------------------------------------------------
def parse(message):
    """
    Return data parsed from the specified commit message.

    """
    commit_msg = {
        'work_summary':     None,
        'work_notes':       None,
        'work_start_time':  None,
        'job_id':           None,
        'job_description':  None,
        'counterparty_id':  None,
        'project_id':       None,
        'mandate':          []
    }
    encoded_data = yaml.safe_load(next(da.util.iter_yaml_docs(message), ''))
    if encoded_data is not None:
        commit_msg.update(encoded_data)
    return commit_msg

# -----------------------------------------------------------------------------
def compose(daybook_entry, work_start_time):
    """
    Return a new commit message composed using daybook_entry information.

    """
    # Work out the details of the current job:
    # counterparty; project and job identifier
    # information.
    work_summary = daybook_entry['work_summary']
    work_notes   = daybook_entry['work_notes']
    cntrpty_id   = daybook_entry['counterparty_id']
    project_id   = daybook_entry['project_id']
    job_id       = daybook_entry['job_id']
    (job_num, _) = job_id.split(sep = '_', maxsplit = 1)

    # Format the commit message as a YAML document
    # with some customised pretty-printing to make
    # it easier to read.
    #
    # The resulting text will still be valid YAML -
    # but we take extra care over line-wrapping and
    # paragraph indentation to make it look attractive
    # and legible in the git log output.
    #
    # The page width is limited to a rather
    # restrictive 60 characters so that commit
    # messages can appear without wrapping in the
    # pop-up boxes used by the github 'blame' tool.
    #
    page_width   = 60
    indent_size  = 17
    para_width   = page_width - indent_size
    para_prefix  = ' ' * indent_size
    list_prefix  = ' ' * 2

    def _para(text):
        """
        Return a nicely indented YAML format text block.

        """
        return textwrap.indent(
                    text   = textwrap.fill(text = text, width = para_width),
                    prefix = para_prefix).lstrip()

    def _list(listdata):
        """
        Return a nicely indented YAML format list.

        """
        return textwrap.indent(
                    text   = yaml.dump(listdata, default_flow_style = False),
                    prefix = list_prefix)

    commit_msg = textwrap.dedent("""
    {job}|{work_summary_line}

    ---
    work_summary:    {work_summary}

    work_notes:      {work_notes}

    work_start_time: {work_start_time}

    job_id:          {job_id}

    job_description: {job_description}

    counterparty_id: {counterparty_id}
    project_id:      {project_id}
    mandate:
    {mandate_list}
    ...

    """).format(
            job               = job_num,
            work_summary_line = work_summary,
            work_summary      = _para(work_summary),
            work_notes        = _para(work_notes),
            work_start_time   = work_start_time,
            job_id            = _para(job_id),
            job_description   = _para(daybook_entry['job_description']),
            counterparty_id   = _para(cntrpty_id),
            project_id        = _para(project_id),
            mandate_list      = _list(daybook_entry['mandate']))

    return commit_msg


import math
import logging as lg

import numpy as np

# FIXME: IMPORT STATEMENTS DON'T QUITE WORK HERE
from Agents.AbstractAgent import AbstractAgent
from Agents.SelfishAgent import SelfishAgent
# from Agents.HonestAgent import HonestAgent
from collections import deque
from Structure.Block import Block

class BlocktimeOracle:
    INIT_SIZE = 500

    def __init__(self, agents: list[AbstractAgent], difficulty):
        self.difficulty: float = difficulty
        self.agents = agents
        # TODO: Do we actually need this?
        self.current_time = 0.0

        self.remake()

    def remake(self):
        all_times_arr = []

        # Iterate over each agent and create mining times
        for agent in self.agents:
            # Starts generating times after the current global time
            agent_times = [agent.get_block_time(self.difficulty) for _ in range(BlocktimeOracle.INIT_SIZE)]
            # Get the cumulative sums
            agent_times = np.cumsum(agent_times)

            # Start the times from when we last left off
            # self.current_time += fork_delta
            agent_times = agent_times + self.current_time
            # agent_times = agent_times + fork_delta

            # Mark each time with the agent that created it
            # DEBUG
            # agent_times = [(agent.get_type(), agent_times[i]) for i in range(len(agent_times))]
            # ---REAL VERSION---
            agent_times = [(agent, agent_times[i]) for i in range(len(agent_times))]

            all_times_arr += agent_times
        # sorts by agent_times, hence x[1] (second index in tuple)
        all_times_arr.sort(key=lambda x: x[1])

        # We want INIT_SIZE number of block times.
        all_times_arr = all_times_arr[:BlocktimeOracle.INIT_SIZE]

        self.allTimes = deque(all_times_arr)

    def next_time(self) -> Block:
        if self.__is_empty():
            self.remake()
        self.current_time = self.peek_left()[1]  # must index the time in the tuple, hence [1]
        winning_agent, mining_time = self.allTimes.popleft()
        transmission = Block(mining_timestamp=mining_time,
                             # # FIXME: this needs to be accounted for
                             # timestamp_of_last_block= 0.0,
                             # timestamp_of_last_block=self.blockchain.get_global_time_of_chain(),
                             winning_agent=winning_agent)
        return transmission

    def fork(self, difficulty: float, agents: list[AbstractAgent],) -> tuple[AbstractAgent, AbstractAgent, float]:
        min_time = math.inf
        winning_agent = None
        winning_chain_agent = None

        for agent in agents:
            if not agent.is_forking:
                continue
            # Split agent into defector and honest
            if agent.type == "honest":
                agent_mine_time_1 = agent.get_block_time(difficulty, alpha=agent.gamma * agent.alpha)  # honest
                agent_mine_time_2 = agent.get_block_time(difficulty, alpha=(1 - agent.gamma)*agent.alpha) #defectors

                # If the produced times are better than the current recording min_time
                if min(agent_mine_time_1, agent_mine_time_2) < min_time:

                    # honest agents won over the defectors
                    if agent_mine_time_1 < agent_mine_time_2:
                        winning_chain_agent = agent
                        winning_agent = agent
                        min_time = agent_mine_time_1

                    # Defectors won
                    else:
                        min_time = agent_mine_time_2
                        winning_agent = agent
                        # Loop through the agents and choose the other
                        for _agent in agents:
                            if _agent.type != "honest":
                                winning_chain_agent = _agent

            # Otherwise the agent is selfish
            else:
                agent_mine_time = agent.get_block_time(difficulty, alpha=agent.alpha)
                # If the selfish agents time is better than the current recorded min_time
                if agent_mine_time < min_time:
                    min_time = agent_mine_time
                    winning_agent = agent
                    winning_chain_agent = agent

            # add the time to the blockchain oracle global time counter
            # tab back everything that follows
        self.current_time += min_time

        # If a miner is not participating in the fork, they can mine in the meantime.
        # [1] bc we want the float, not the miner
        while self.peek_left()[1] <= self.current_time:
            transmission: Block = self.next_time()
            if not transmission.winning_agent.is_forking:
                transmission.winning_agent.receive_blocks_from_oracle([transmission])

                # Now increment the length of the claimed private chain
                transmission.winning_agent.store_length += 1
                transmission.winning_agent.delta += 1

                transmission.winning_agent.receive_blocks_from_oracle([transmission])
                lg.debug(str(transmission.winning_agent) + " found a block in the meantime")

        # reset the deque of times
        self.remake()

        return winning_chain_agent, winning_agent, min_time

    # def fork(self, difficulty: float, agents: list[AbstractAgent]) -> tuple[AbstractAgent, AbstractAgent, float]:
    #     min_time = math.inf
    #     winning_agent = None
    #     winning_block = None
    #     for agent in agents:
    #         # FIXME: when will is_forking be set to false in another part of the code?
    #         # FIXME: do we even need this?
    #         if agent.is_forking:
    #
    #             winning_agent = agent
    #
    #             if agent.type == "honest":
    #                 agent_mine_time_1 = agent.get_block_time(difficulty, alpha=agent.gamma*agent.alpha) #honest
    #                 agent_mine_time_2 = agent.get_block_time(difficulty, alpha=(1 - agent.gamma)*agent.alpha) #defectors
    #                 agent_mine_time = min(agent_mine_time_2, agent_mine_time_1)
    #
    #                 # if defector wins, it is a selfish win for the prev block
    #                 # if honest wins, winning block is an honest one, if defectors win, it is a selfish win
    #                 if agent_mine_time_1 < agent_mine_time_2:
    #                     winning_block = agent
    #                 else:
    #                     for this_agent in agents:
    #                         # FIXME: not enough to check for the first selfish miner, may be multiple selfish miners
    #                         if this_agent.type == "selfish":
    #                             winning_block = this_agent
    #             else:
    #                 agent_mine_time = agent.get_block_time(difficulty)
    #
    #             if min_time > agent_mine_time:
    #                 min_time = agent_mine_time
    #                 winning_block = agent
    #     return winning_block, winning_agent, min_time

    # scrap the entire existing deque and generate a new deque
    # def fork_next_time(self) -> (AbstractAgent, float):
    #     self.allTimes.clear()
    #
    #     # results = {'time': None, 'winner': None, 'block': None, 'type': None}
    #     # attack_times = {}
    #
    #     def _is_active(agent: AbstractAgent):
    #         # FIXME: this should be cleaner (whether a method of abstract class or a __name__ implementation)
    #         if agent.get_type().split("_")[0] == 'smart':
    #             if not agent.is_mining:
    #                 return False
    #         return True
    #
    #     # TODO: what does this do?
    #     # for agent in self.agents:
    #     #     if _is_active(agent):
    #     #         attack_times[agent.get_type()] = agent.get_block_time(difficulty)
    #     #     else:
    #     #         attack_times[agent.get_type()] = 100000
    #     #
    #     # results['winner'], results['time'] = min(attack_times.items(), key=lambda x: x[1])
    #     # results['type'] = results['winner'].split('_')[0]
    #     #
    #     # return results
    #     winning_time: float = 0  # ?? Some value?
    #     self.extend(fork_delta=winning_time)

    def reset(self) -> (AbstractAgent, float):
        self.allTimes.clear()
        self.remake()

    def __is_empty(self) -> bool:
        return True if len(self.allTimes) == 0 else False

    def peek_right(self) -> (AbstractAgent, float):
        # lookup O(1)
        if self.__is_empty():
            self.remake()
        return self.allTimes[-1]

    def peek_left(self) -> (AbstractAgent, float):
        # lookup O(1)
        if self.__is_empty():
            self.remake()
        return self.allTimes[0]

if __name__ == "__main__":
    pass
    test_deque = BlocktimeOracle(SelfishAgent(.3), difficulty=.4)
    for _ in range(10):
        print(test_deque.next_time())


from fanstatic import Library, Resource, Group
from js.jquery import jquery

library = Library('uvc.bootstraptheme', 'static')

bootstrap_css = Resource(
    library, 'site.css', compiler='less',
    source='site.less')

bootstrap_js = Resource(library,'node_modules/bootstrap/dist/js/bootstrap.min.js', depends=[jquery,])

bootstrap = Group([bootstrap_css, bootstrap_js])

main_css = Resource(library, 'main.css', depends=[bootstrap])


# https://programmers.co.kr/learn/courses/30/lessons/64063?language=python3

def solution(k, room_number):
    parents = {}
    answer = []
    for node in room_number:
        visited = [node]
        while node in parents:
            node = parents[node]
            visited.append(node)
        answer.append(node)
        for visited_node in visited:
            parents[visited_node] = node + 1
    return answer


from datetime import date
ve = 0
no = 0
ano = date.today().year
for n in range(1, 7):
    p = int(input('digite o ano que a {}ª pessoa nasceu'.format(n)))
    x = ano - p
    if x < 18:
        no += 1
    else:
        ve += 1
print('tem {} menores de idade e {} maiores de idade'.format(no, ve))


#
import os, numpy
import Ngl, Nio

#
case="forced"
exp="wrf"

if exp=="wrf":
    dat_dir="~/PROJ_WRF/%s_mixed/T_sfc/diffs" %(case)
if exp=="cesm":
    #dat_dir="~/PROJ_WRF/%s_mixed/T_sfc/diffs" %(case)
    dat_dir="~/PROJ_WRF/%s/SST" %(case)

if case=="forced":

    s_years=[1213,1258,1274,1284,1452,1600,1641,1761,1809,1815]
    e_years=[1223,1274,1283,1294,1461,1609,1650,1770,1818,1825]
    cols=2-len(s_years)%2
    rows=int(len(s_years)/cols)

if case=="ctrl":
    s_years=[1000]
    e_years=[1100]
    rows=1
    cols=1

titles=[]
for i in range(len(s_years)):
    titles.append("SST Anomaly %s-%s" %(s_years[i],e_years[i]))

data={}
for i in range(len(s_years)):
    if exp=="wrf":
        data["dat"+str(i+1)]=dat_dir+"/T_sfc_%s_%s_%s_diff.nc" %(case,s_years[i],e_years[i])
    if exp=="cesm":
        data["dat"+str(i+1)]=dat_dir+"/SST_%s_%s_%s_diff.nc" %(case,s_years[i],e_years[i])

files={}
for i in range(len(s_years)):
    files["f"+str(i+1)]=Nio.open_file(data["dat"+str(i+1)])

temp=[]
for i in range(len(s_years)):
    if exp=="wrf":
        temp.append(files["f"+str(i+1)].variables["T_sfc_monthly"][0,:,:])
    if exp=="cesm":
        temp.append(files["f"+str(i+1)].variables["SST"][:,:])

lat  = files["f1"].variables["lat"][:]
lon  = files["f1"].variables["lon"][:]

rlist = Ngl.Resources()

wks_type = "pdf"
wks = Ngl.open_wks(wks_type,"T_sfc_diff_%s_%s_mixed" %(case,exp),rlist)

resources = Ngl.Resources()
resources.nglDraw  = False
resources.nglFrame   = False
resources.cnFillOn        = True
resources.cnFillPalette   = "BlueYellowRed"
resources.cnLinesOn       = False
resources.cnLineLabelsOn  = False

## Scalar field resources
resources.sfXArray        = lon
resources.sfYArray        = lat

# Map resources

resources.mpFillDrawOrder        = "PostDraw"
resources.mpLandFillColor        = "Gray"
resources.mpOceanFillColor       = "Transparent"
resources.mpInlandWaterFillColor = "Transparent"
resources.mpGridMaskMode         = "MaskLand"
#del resources.tiMainString

resources.vpWidthF   = 0.95
resources.vpHeightF  = 0.95
resources.mpLimitMode = "LatLon"

resources.mpFillOn   = True
resources.mpLabelsOn = False
resources.mpOutlineOn = True
resources.mpGridAndLimbOn = False
resources.tmXBLabelFontHeightF = 0.02
resources.tmYLLabelFontHeightF = 0.02
resources.mpMinLatF      = 0.0 #float(min(lat))
resources.mpMaxLatF      = 55.0 #float(max(lat))
resources.mpMinLonF      = -130.0 #float(min(lon))
resources.mpMaxLonF      = 10.0 #float(max(lon))
resources.mpProjection = "CylindricalEquidistant"
resources.pmLabelBarDisplayMode = "Never"

resources.cnLevelSelectionMode = "ManualLevels"
resources.cnMinLevelValF       = -5
resources.cnMaxLevelValF       = 5
resources.cnLevelSpacingF      = 0.25
resources.mpPerimOn       = True

plot = []
for i in range(len(s_years)):
    resources.tiMainString = titles[i]
    plot.append(Ngl.contour_map(wks,temp[i],resources))

panelres = Ngl.Resources()
panelres.nglPanelYWhiteSpacePercent = 5.
panelres.nglPanelXWhiteSpacePercent = 3.
panelres.nglPanelLabelBar                 = True
panelres.nglPanelLabelBarWidthF           = 0.700
panelres.nglPanelTop                      = 0.975
panelres.nglPanelFigureStrings            = ["A","B","C","D","E","F","G","H","I","J"]
panelres.nglPanelFigureStringsJust        = "BottomRight"

Ngl.panel(wks,plot[0:len(s_years)],[rows,cols],panelres)

Ngl.end()


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

"""
Script to map i* (istar) models to feature models (FMs).

Usage:
    python istar2fm.py --file <istar-model.xmi>

Input:
    <istar-model.xmi> is the i* model conformed to the i* 2.0 metamodel.

Output:
    <fm-model-FM.xmi> is the FM conformed to its metamodel.
    <fm-model-FMconfig.xmi> is a partial configuration of the FM.

References:
    i* 2.0 metamodel: https://arxiv.org/abs/1605.07767
    Feature Model: https://en.wikipedia.org/wiki/Feature_model
"""

import argparse
import istarModel
import featureModel
import configurationFM

def clean_str(s):
    """Remove special characters from a name o ID."""
    s = s.replace('-', ' ')
    s = s.replace('.', ' ')
    s = s.title()
    s = s.replace(' ', '')
    return s

def clean_id(s):
    """IDs begins with an underscore."""
    return '_' + clean_str(s)

def add_exclusive_constraints(fm, ids):
    """Create exclusive constraints in the feature model.

    Constraints are in First-order logic.
    Exclusive constraints are of these types:
    A -> B & C &..., and
    B -> A & C &..., and
    C -> A & B &..., and so on.

    The ID of the constraint starts with an underscore followed by its formula.

    Note:
        Could this create redundancy constraints?.

    Args:
        fm (FeatureModel): The feature model.
        ids (:obj:`list` of :obj:`str`): List of IDs of the entities involved in the constraints.
    """
    for x in ids:
        constraint = x + ' implies not ('
        for y in ids:
            if x != y:
                constraint += y + ' or '
        constraint = constraint[:-4] + ')'
        fm.add_constraint('_' + constraint, 'First-order logic', constraint)

def add_intentional_element(istar, fm, ie, ie_type, parent_feature, variability_type, lower=0, upper=-1):
    """Create new features in the feature model from an intentional element (goal, task, resource, or quality) and its children.

    The mapping for intentional elements is as follow:
        Each intentional element (goal, task, resource, or quality) is mapped to a unique feature.
        The ID of the feature is the ID of the intentional element preceded with an underscore.
        The name of the feature is the name of the intentional element without special chars like dots, dashes, whitespaces, etc. (see `clean_str` method).
        OR Refinements of intentional elements are mapped to `exclusive or` features where only a feature child can be selected.
        Concretely:
            OR Refinements of intentional elements of the same type are mapped to `alternative` features .
            OR Refinements of intentional elements of different types are mapped to `optional` features plus `exclusive constraints` (see `add_exclusive_constraints` method).
        AND Refinements of intentional elements are mapped to:
            `mandatory` features for those intentional elements of the same type being processed.
            `requires (implies)` constraints (A -> B & C) for those intentional elements with different type.

    Args:
        istar (IStarModel): The i* model.
        fm (FeatureModel): The feature model.
        ie (pyecore.ecore.IntentionalElement): The intentional element to be mapped.
        ie_type (str): Type of the intentional element. It can be: 'Goal', 'Task', 'Resource', or 'Quality'.
        parent_feature (pyecore.ecore.Feature): The parent feature of the new feature to be created.
        variability_type (str): Type of variability of the feature to be created. It can be: 'optional', 'mandatory', 'or', 'alternative'.
        lower (int, optional): Lower bound for group cardinality features (only for 'or', or 'alternative' features).
        upper (int, optional): Upper bound for group cardinality features (only for 'or', or 'alternative' features).

    Returns:
        The list of intentional elements that has been mapped.

    """
    processed = []
    id = clean_id(ie.id)
    feature = fm.get_feature(id)
    if feature is not None:     # si la feature ya existía la elimino y la vuelvo a crear para actualizar su información adecuadamente (padre y tipo de variabilidad)
        feature.delete()

    feature = fm.add_feature(id, clean_str(ie.name), parent_feature, variability_type, lower, upper)
    processed.append(ie)
    if isinstance(ie, istar.metamodel.get_class('Goal')) or isinstance(ie, istar.metamodel.get_class('Task')):
        if ie.refines is not None:
            if isinstance(ie.refines, istar.metamodel.get_class('ORRefinement')):
                or_group = []
                hasConstraints = False
                for child in ie.refines.to:
                    if isinstance(child, istar.metamodel.get_class(ie_type)):
                        or_group.append(child)
                    else:
                        hasConstraints = True

                if not hasConstraints:
                    for or_ie in or_group:
                        processed += add_intentional_element(istar, fm, or_ie, ie_type, feature, 'alternative', 1, 1)
                else:
                    for or_ie in or_group:
                        processed += add_intentional_element(istar, fm, or_ie, ie_type, feature, 'optional')

                    constraint = clean_id(ie.id) + ' implies ('
                    for child in ie.refines.to:
                        constraint += clean_id(child.id) + ' or '
                    constraint = constraint[:-4] + ')'
                    fm.add_constraint('_' + constraint, 'First-order logic', constraint)
                    add_exclusive_constraints(fm, [clean_id(e.id) for e in ie.refines.to])
            elif isinstance(ie.refines, istar.metamodel.get_class('ANDRefinement')):
                and_group = []
                ie_constraints = []
                for child in ie.refines.to:
                    if isinstance(child, istar.metamodel.get_class(ie_type)):
                        and_group.append(child)
                    else:
                        ie_constraints.append(child)

                for and_ie in and_group:
                    processed += add_intentional_element(istar, fm, and_ie, ie_type, feature, 'mandatory')
                if len(ie_constraints) > 0:
                    constraint = clean_id(ie.id) + ' implies ('
                    for child in ie_constraints:
                        constraint += clean_id(child.id) + ' and '
                    constraint = constraint[:-5] + ')'
                    fm.add_constraint('_' + constraint, 'First-order logic', constraint)
    return processed

def add_intentional_elements(istar, fm, elements, elements_type, root_feature, variability_type):
    """Map all the intentional elements that are not previously mapped (processed) for a specific type ('Goal', 'Task', 'Resource', or 'Quality').

    Note:
        Each type of intentional element has its own subtree in the feature model.

    Args:
        istar (IStarModel): The i* model.
        fm (FeatureModel): The feature model.
        elements (:obj:`list` of :obj:`IntentionalElement`): List of intentional elements of a type.
        elements_type (str): Type of the intentional elements. It can be: 'Goal', 'Task', 'Resource', or 'Quality'.
        root_feature (pyecore.ecore.Feature): The parent feature of subtree of the intentional elements.
        variability_type (str): Type of variability of the feature to be created. It can be: 'optional', 'mandatory', 'or', 'alternative'.

    Returns:
        The list of intentional elements that has been mapped.

    """
    processed = []
    for e in elements:
        if e not in processed:
            processed += add_intentional_element(istar, fm, e, elements_type, root_feature, variability_type)
    return processed

def add_cognitive_model(istarModel, fm, actor, name, root_feature):
    """Map the cognitive model (the intentional elements) for an actor and all the relationships.

    Relationships includes `neededBy` associations with resources, `contributions` of qualities, (and `qualifies` of qualities)?.
    All relationships are mapped to `requires (implies)` constraints.

    Note:
        Each type of intentional element has its own subtree in the feature model.

    Args:
        istarModel (IStarModel): The i* model.
        fm (FeatureModel): The feature model.
        actor (pyecore.ecore.Actor): The actor/role/agent.
        name (str): The actor's name.
        root_feature (pyecore.ecore.Feature): The parent feature of subtree of the intentional elements.

    """
    goalsF = fm.add_feature(name+'Goals', name+'Goals', root_feature, 'mandatory')
    plansF = fm.add_feature(name+'Plans', name+'Plans', root_feature, 'optional')
    contextF = fm.add_feature(name+'Context', name+'Context', root_feature, 'optional')
    qualityattributesF = fm.add_feature(name+'QAs', name+'QAs', root_feature, 'optional')

    goals = add_intentional_elements(istarModel, fm, istarModel.get_goals(actor), 'Goal', goalsF, 'optional')       # ponerlo mandatory creará deade features
    tasks = add_intentional_elements(istarModel, fm, istarModel.get_tasks(actor), 'Task', plansF, 'optional')
    resources = add_intentional_elements(istarModel, fm, istarModel.get_resources(actor), 'Resource', contextF, 'optional')
    qualities = add_intentional_elements(istarModel, fm, istarModel.get_qualities(actor), 'Quality', qualityattributesF, 'optional')

    for r in resources:
        for t in r.neededBy:
            constraint = clean_id(t.id) + " implies " + clean_id(r.id)
            fm.add_constraint('_' + constraint, 'First-order logic', constraint)

    for ie in goals+tasks+resources+qualities:
        for c in ie.contribution:
            qa = c.contributesTo
            if str(c.type) in ['Make', 'Help']:
                constraint = clean_id(ie.id) + " implies " + clean_id(qa.id)
            elif str(c.type) in ['Hurt', 'Break']:
                constraint = clean_id(ie.id) + " implies not " + clean_id(qa.id)
            fm.add_constraint(clean_str(constraint), 'First-order logic', constraint)

    # Qualification is optional for the user, in other case it would create dead features
    # for qa in qualities:
    #     for ie in qa.qualifies:
    #         constraint = clean_str(ie.name) + " implies " + clean(qa.name)
    #         fm.add_constraint(clean_str(constraint), 'First-order logic', constraint)
    #     for r in qa.qualifiesResource:
    #         constraint = clean_str(r.name) + " implies " + clean(qa.name)
    #         fm.add_constraint(clean_str(constraint), 'First-order logic', constraint)

    # print("-----------------")
    # print("Name: " + actor.name)
    # print("Type: " + str(type(actor)))
    # print("#Goals: " + str(len(goals)))
    # print("#Tasks: " + str(len(tasks)))
    # print("#Resources: " + str(len(resources)))
    # print("#QAs: " + str(len(qualities)))
    # print("-----------------")

def add_roles(istarModel, fm, root_feature):
    """It maps the roles of the i* model including all its intentional elements.

    Roles are mapped to `clonable` features.

    Args:
        istarModel (IStarModel): The i* model.
        fm (FeatureModel): The feature model.
        root_feature: Root of the feature model and parent of all roles.

    """
    for r in istarModel.get_roles():
        name = clean_str(r.name)
        id = clean_id(r.id)
        f = fm.add_clonable_feature(id, name, parent=root_feature, variability_type='or')
        add_cognitive_model(istarModel, fm, r, name, f)

def add_actors_only(istarModel, fm, root_feature):
    """It maps the actors of the i* model including all its intentional elements.

    Actors are mapped to normal features.

    Note:
        Actors are includes only if they are not instances of 'Role' or 'Agent' classes.
        This is because Actor is superclass of Role and Agent.

    Args:
        istarModel (IStarModel): The i* model.
        fm (FeatureModel): The feature model.
        root_feature: Root of the feature model and parent of all actors.

    """
    actors = set(istarModel.get_actors())   # actors includes roles and agents.
    roles = set(istarModel.get_roles())
    agents = set(istarModel.get_agents())
    actors -= roles
    actors -= agents

    for a in actors:
        name = clean_str(a.name)
        id = clean_id(a.id)
        f = fm.add_feature(id, name, parent=root_feature, variability_type='or')
        add_cognitive_model(istarModel, fm, a, name, f)

def add_agents(istarModel, fm, root_feature):
    """It maps the agents of the i* model including all its intentional elements.

    Agents are mapped to instance of the `clonable` features of roles.
    Agents are not in the feature model but in configuration models of the FM.

    Args:
        istarModel (IStarModel): The i* model.
        fm (FeatureModel): The feature model.
        root_feature: Root of the feature model and parent of all agents.

    """
    for a in istarModel.get_agents():
        name = clean_str(a.name)
        id = clean_id(a.id)
        f = fm.add_feature(id, name, parent=root_feature, variability_type='or')
        add_cognitive_model(istarModel, fm, a, name, f)

def generate_feature_model(istarModel):
    """It creates an empty feature model and adds all main i* concepts (roles and actors) including all its intentional elements.

    Args:
        istarModel (IStarModel): The i* model.

    """
    fm = featureModel.FeatureModel()
    root = fm.add_feature(id='MyFeatureRoot', name='MyFeatureRoot', variability_type='mandatory')
    add_roles(istarModel, fm, root)
    add_actors_only(istarModel, fm, root)
    #add_agents(istarModel, fm, root)   # Esto es para las configuraciones
    return fm

def generate_configuration_model(istarModel, fm):
    """It creates an empty configuration model and adds all main i* concepts (agents) including all its intentional elements.

    Args:
        istarModel (IStarModel): The i* model.

    """
    config = configurationFM.FMConfiguration(fm)
    instance_number = 1
    for a in istarModel.get_agents():
        if len(a.participatesIn) > 0:
            role = a.participatesIn[0]
            clonable_feature = fm.get_feature(clean_id(role.id))
            instance = config.add_clonable_selection(clean_str(a.name), instance_number, clonable_feature)
            # Add abstract nodes (Goals, Tasks, Resources, Qualities)
            for child in clonable_feature.children:
                config.add_clonable_selection(child.name, instance_number, child)

            # Add intentional_elements
            ies = []
            ies += istarModel.get_goals(a)
            ies += istarModel.get_tasks(a)
            ies += istarModel.get_resources(a)
            ies += istarModel.get_qualities(a)
            for ie in ies:
                #f = fm.get_feature(clean_id(ie.id))    # no se puede hacer por ID porque en i* cada nodo tiene un ID diferente (i* no diferencia configuraciones).
                name = clean_str(ie.name)
                f = fm.get_feature_by_name(name)
                if f != None:
                    config.add_clonable_selection(name, instance_number, f)
            instance_number += 1
    return config

def istar2fm(filename):
    """It loads the i* model and generates a new feature model by mapping the i* model to the FM concepts.

    Args:
        filename (str): Filename of the i* model (.xmi).

    """
    m = istarModel.IStarModel()
    m.load_model(filename)

    fm = generate_feature_model(m)
    fm.save_model(filename[:-4] + "-FM.xmi")

    config = generate_configuration_model(m, fm)
    config.save_model(filename[:-4] + "-FMconfig.xmi")

    return fm

def agent2configFM(filename):
    """It loads the i* agent model and generates a new configuration of the feature model by mapping the i* model to the FM concepts.

    Args:
        filename (str): Filename of the i* model (.xmi).

    """
    m = istarModel.IStarModel()
    m.load_model(filename)

    fm = generate_feature_model(m)
    fm.save_model(filename[:-4] + "-FM.xmi")

    config = generate_configuration_model(m, fm)
    config.save_model(filename[:-4] + "-FMconfig.xmi")

    return fm

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="""It transforms a i* model (.xmi) to a feature model (.xmi)."""
    )

    parser.add_argument('-f', '--file', required=True, help='i* (istar) model filename',  dest='filename')
    args = parser.parse_args()
    filename = args.filename

    istar2fm(filename)


#!/usr/bin/python3
"""Module that gather data from an API"""
import requests
from sys import argv

if __name__ == "__main__":
    users = requests.get("https://jsonplaceholder.typicode.com/users/{}"
                         .format(argv[1]))
    name = users.json().get("name")

    to_dos = requests.get("https://jsonplaceholder.typicode.com/todos",
                          params={"userId": argv[1]})
    list_dict = to_dos.json()
    total_all_task = len(list_dict)

    done_task = []
    for d in list_dict:
        if d.get("completed"):
            done_task.append(d)

    numbers_done_task = len(done_task)

    print("Employee {} is done with tasks({}/{}):".format(name,
                                                          numbers_done_task,
                                                          total_all_task))
    for task in done_task:
        print("\t {}".format(task.get("title")))


import tensorflow as tf

IOU_THRESHOLDS = [0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95]

def mIOU(y_true,
         y_pred,
         metrics_collections=None,
         updates_collections=None,
         name="iou"):

    with tf.variable_scope(name):

        def iou_internal(y_true_internal, y_pred_internal):

            cm = tf.confusion_matrix(tf.reshape(y_true_internal, (-1,)),  # flatten matrix
                                     tf.reshape(y_pred_internal, (-1,)),  # flatten matrix
                                     num_classes=2,
                                     dtype=tf.float32,
                                     name="confusion_matrix",
                                     )

            # denominator = TP + FP + FN
            denominator = tf.reduce_sum(tf.gather_nd(cm, [[1, 1], [0, 1], [1, 0]]))

            # score = TP / (TP + FP + FN)
            # if TP + FP + FN is 0 then return 1 (correctly identified an image with a constant 0 mask)
            score = tf.expand_dims(tf.cond(tf.greater(denominator, 0),
                                           lambda: tf.gather_nd(cm, [1, 1]) / denominator,
                                           lambda: 1.0), 0)

            return tf.reduce_mean(
                score * tf.to_float(tf.greater(tf.tile(score, [len(IOU_THRESHOLDS)]), IOU_THRESHOLDS)))

        scores = tf.map_fn(lambda x: iou_internal(x[0], x[1]),
                           elems=(y_true, y_pred),
                           dtype=tf.float32)

        # return tf.reduce_mean(scores)

        # based on https://stackoverflow.com/questions/47753736/custom-metrics-with-tf-estimator
        iou, update_op = tf.metrics.mean(scores)

        if metrics_collections:
            tf.add_to_collections(metrics_collections, iou)

        if updates_collections:
            tf.add_to_collections(updates_collections, update_op)

        return iou, update_op

def mean_accuracy(y_true,
                  y_pred,
                  metrics_collections=None,
                  updates_collections=None,
                  name="acc"):
    with tf.variable_scope(name):

        scores = tf.reduce_mean(tf.to_float(tf.equal(y_true, y_pred)), axis=[1, 2, 3])

        # return tf.reduce_mean(scores)
        acc, update_op = tf.metrics.mean(scores)

        if metrics_collections:
            tf.add_to_collections(metrics_collections, acc)

        if updates_collections:
            tf.add_to_collections(updates_collections, update_op)

        return acc, update_op


import math

class IsoscelesRightTriangle:
    def __init__(self, element_number, element_value):
        self.element_number = element_number
        self.element_value = element_value
        self.array_of_elements = []

    def zero_error(self, a, c, h, s):
        temp_list = [a, c, h, s]
        for i in temp_list:
            if i < 0:
                raise ZeroDivisionError

    def the_value_of_the_leg(self):
        a = self.element_value
        c = math.sqrt(2) * a
        h = c / 2
        s = c * h / 2
        return self.add_to_array(a, c, h, s)

    def the_value_of_the_hypotenuse(self):
        c = self.element_value
        a = c / math.sqrt(2)
        h = c / 2
        s = c * h / 2
        return self.add_to_array(a, c, h, s)

    def the_value_of_the_height(self):
        h = self.element_value
        c = 2 * h
        a = c / math.sqrt(2)
        s = c * h / 2
        return self.add_to_array(a, c, h, s)

    def the_value_of_the_area(self):
        s = self.element_value
        c = math.sqrt(s * 4)
        a = c / math.sqrt(2)
        h = c / 2
        return self.add_to_array(a, c, h, s)

    def counting_from_element_number(self):
        if self.element_number == 1:
            elements_value = self.the_value_of_the_leg()
        elif self.element_number == 2:
            elements_value = self.the_value_of_the_hypotenuse()
        elif self.element_number == 3:
            elements_value = self.the_value_of_the_height()
        elif self.element_number == 4:
            elements_value = self.the_value_of_the_area()
        else:
            raise Exception('Incorrect element number')
        return elements_value

    def add_to_array(self, a, c, h, s):
        self.zero_error(a, c, h, s)
        self.array_of_elements.append(a)
        self.array_of_elements.append(c)
        self.array_of_elements.append(h)
        self.array_of_elements.append(s)
        return self.array_of_elements

# triangle = IsoscelesRightTriangle(1, 0.2348)
# print(triangle.counting_from_element_number())


from itertools import combinations
def solution(dwarfs):
  dwarfs.sort()
  for combination in list(combinations(dwarfs,7)):
    if sum(combination)==100:
      combination=map(str,combination)
      print("\n".join(combination))
      break

if __name__ =="__main__":
  dwarfs=[]
  with open("input2309.txt","r") as file:
    for _ in range(9):
      dwarfs.append(int(file.readline()))

  solution(dwarfs)


import sys

import os
import utils as util
import spacy
from sklearn.model_selection import KFold

__K = int(sys.argv[1])
__MODEL_PATH = "es_core_news_lg"
__DATASET_PATH = "breast_oswaldo.spacy"

print("__K=", str(__K))

kf = KFold(n_splits=__K, random_state=8, shuffle=True)

print("k-fold info: " + str(kf))

folds_path = "folds_" + str(__K)
os.mkdir(folds_path)

# carga el modelo y obtiene una lista de docs a partir del fichero .spacy
model = spacy.load(__MODEL_PATH)
docs = util.load_dataset(__DATASET_PATH, model)

i = 1
for train_i, test_i in kf.split(docs):
    train = docs[train_i]
    test = docs[test_i]

    # obtiene estadisticas para los docs del entrenamiento:
    train_stats = util.get_train_stats(train)

    # obtiene las estadisticas para los docs de prueba:
    test_stats = util.get_test_stats(test)

    # crea el i-esimo fold:
    fold_name = "fold_" + str(i) +"/"
    fold_path = folds_path+"/"+ fold_name
    os.mkdir(fold_path)

    # aniade los 4 archivos (train, test, train_stats, test_stats) al i-esimo fold:
    util.write_train_test(fold_path, train, test, train_stats, test_stats)

    i += 1


class Solution:
    '''
    written by amitjha@umich.edu
    '''
    def isHappy(self, n: int) -> bool:
        hash_map = {}
        while(n not in hash_map):
            hash_map[n] = 1
            n = sum([int(num)**2 for num in str(n)])
        return(n==1)


"""Write a program that asks the user for an indefinite set of strings
-
until an empty string
is entered
-
then
prints all of the strings that were repeated.
E.g. if the user entered: “hello”, “world is good”, “hello”, “john”, “good”... the program would print
“Strings repeated: hello”.
If no repeated strings are entered, the program should print “N
o repeated strings entered”."""

setOfStrings = []
emptyCount = 0

while emptyCount == 0:
    stringToAdd = input("Please type a string: ")
    setOfStrings.append(stringToAdd)
    if stringToAdd == "":
        emptyCount += 1

dupCount_times = []
dupCount_values = []
dupCount = 0

for j in range(len(setOfStrings)):

    #If we have already considered this value
    if setOfStrings[j] in dupCount_values:
        pass #donothing
    else:
        for i in range(len(setOfStrings)):
            if setOfStrings[j] == setOfStrings[i] and i != j:

                #Add that value to dupCount_Values:
                if (setOfStrings[i] in dupCount_values) == False:
                    dupCount_values.append(setOfStrings[i])
                    dupCount_times.append(2)
                else:
                    for k in range(len(dupCount_values)):
                        if dupCount_values[k] == setOfStrings[i]:
                            dupCount_times[k] += 1

#print(dupCount_times)
#print(dupCount_values)

if len(dupCount_values) == 0:
    print("No words repeated.")
else:
    for l in range (len(dupCount_values)):
        print("Word {} appeared {} times.".format(dupCount_values[l], dupCount_times[l]))


#!/usr/bin/env python3.4
import sys,re

# show dictionary
def ppdic(d):
    for k in sorted(d.keys()):
        print("%s : %s" % (k, d[k]))

def pp_sigma(g, sigma, all_u, pi):
    for i in sorted(sigma.keys()):
        pi_sum = sum(pi[h][0] for h in g.is2hs[i])
        if pi_sum == 0 :
            print("%s ( - ) : %s" % (i, sigma[i]))
        else:
            allh_sum = float(sum(all_u[h] * pi[h][0] for h in g.is2hs[i]))
            print("%s ( %s ) : %s" % (i, (allh_sum/pi_sum), sigma[i]))

# cfr_plus
def makeSigma(q, isactions):
    sigma = {'' : {'-' : 1}}
    for i in isactions.keys():
        if  i == 'M,4 1 1 2,vR,gRR-,dR,vR,g.W+,dM,vM,gRR-,dC,vC':
            print("i=%s, s=%s" % (i,s))
        s = sum(max(0, v) for v in q[i].values())
        if  i == 'M,4 1 1 2,vR,gRR-,dR,vR,g.W+,dM,vM,gRR-,dC,vC':
            print("i=%s, s=%s" % (i,s))
        if s == 0:
            sigma[i] = {k : (1.0 / len(q[i])) for k in isactions[i] }
        else:
            sigma[i] = {a : max(0, q[i][a])/float(s) for a in q[i].keys()}
    return sigma

def piMul(p1, p2):
    return tuple(p1[i]*p2[i] for i in range(3))
def makePiURec(g, h, p, pi, all_u, sigma):
    pi[h] = p
    if g.isTerminal(h):
        all_u[h] = g.utility(h)
        return all_u[h]
    ns = g.nextWithProb(h, sigma)
    all_u[h] = sum(makePiURec(g, h1, piMul(p, p1), pi, all_u, sigma) * p1[0] for h1, p1 in ns)
    return all_u[h]

def makePiU(g, sigma, pi, all_u):
    all_u[g.init] = makePiURec(g, g.init, (1, 1, 1), pi, all_u, sigma)
    return (pi, all_u)

def makeRDiff(g, h, i, a, sigma, pi, all_u):
    old_u = all_u[h]
    both_i = [g.makeIS(h, p) for p in range(2)]
    b_both = [{a : 1} if g.i2p(i) == p else sigma[both_i[p]] for p in range(2)]
    new_u = sum(all_u[n1] * p1[0] for n1,p1 in g.next(h, b_both))
    r = pi[h][2-g.i2p(i)] * (new_u - old_u)
    return (r if g.i2p(i) == 0 else -r)

def oneIterationCFR(g, count, r, sum_pi, sum_sigma, sigma, all_u, debugprint):
    for p in range(1,-1,-1):
        sigma = makeSigma(r, g.isactions)
        pi = {}
        makePiU(g, sigma, pi, all_u)
        if p==0: # count > 0:
            for i, hs in g.is2hs.items():
                pi_i = sum(pi[h][0] for h in g.is2hs[i])
                sum_pi[i] += pi_i
                for a in g.isactions[i]:
                    sum_sigma[i][a] += pi_i * sigma[i][a]
        if debugprint:
            for h in sorted(g.allh):
                print("%s -> pi = %s all_u = %s" % (h, pi[h], all_u[h]))
        rdiff = {i : {a : 0 for a in actions} for i, actions in g.isactions.items()}
        if debugprint:
            print("count=%s" % count)
            print("sigma=")
            ppdic(sigma)
            print("g.isactions=%s" % g.isactions)
            if p == 0: # count > 0:
                av_sigma = {i : { a : 0 if sum_pi[i] == 0 else sum_sigma[i][a] / float(sum_pi[i]) for a in actions } for i, actions in g.isactions.items() }
                print("av_sigma=")
                ppdic(av_sigma)
            print("pi,all_u=")
        for i, hs in g.is2hs.items():
            if g.i2p(i) != p: continue
            if debugprint:
                print("%s :" % i)
            for h in g.is2hs[i]:
                if debugprint:
                    print("  %s : pi=%s all_u=%s" % (h, pi[h], all_u[h]))
                for a in g.isactions[i]:
                    rd = makeRDiff(g,h, i, a, sigma, pi, all_u)
                    if debugprint:
                        print("    %s -> %s :" % (a, rd))
                    r[i][a] += rd
    return sigma

def cfr(g, iteration=100, debugprint=True):
    r = {i : { a : 0 for a in actions } for i, actions in g.isactions.items() }
    sum_pi = {i : 0 for i in g.isactions.keys() }
    sum_sigma = {i : { a : 0 for a in actions } for i, actions in g.isactions.items() }
    sigma = {}
    all_u = {}
    for count in range(iteration):
        sigma = oneIterationCFR(g, count, r, sum_pi, sum_sigma, sigma, all_u, debugprint)
    av_sigma = {i : { a : sigma[i][a] if sum_pi[i] == 0 else sum_sigma[i][a] / float(sum_pi[i]) for a in actions } for i, actions in g.isactions.items() }
    print("sigma=")
    ppdic(sigma)
    print("av_sigma=")
    ppdic(av_sigma)

def make_av_sigma(sum_sigma):
    r = {}
    for i, v in sum_sigma.items():
        psum = sum(v.values())
        r[i] = {a : 0 if psum == 0 else v[a] / float(psum) for a in v}
    return r
def oneIteration(g, count, q, sum_pi, sum_sigma, sigma, all_u, debugprint):
    for p in range(1,-1,-1):
        sigma = makeSigma(q, g.isactions)
        pi = {}
        makePiU(g, sigma,pi,all_u)
        if True: # p == 0: # count > 0:
            for i, hs in g.is2hs.items():
                if g.i2p(i) == p:
                    continue
                pi_i = sum(pi[h][2 - p] for h in g.is2hs[i])
                for a in g.isactions[i]:
                    sum_sigma[i][a] += pi_i * sigma[i][a] * (count + 1)
        if debugprint:
            print("count=%s" % count)
            print("sigma=")
            ppdic(sigma)
            print("pi=%s, all_u=%s" %(pi, all_u))
            print("g.isactions=%s, sum_pi=%s" % (g.isactions, sum_pi))
            print("pi,all_u=")
            for h in sorted(g.allh):
                print("%s -> pi = %s all_u = %s" % (h, pi[h], all_u[h]))
        rdiff = {i : {a : 0 for a in actions} for i, actions in g.isactions.items()}
        for i, hs in g.is2hs.items():
            if g.i2p(i) != p: continue
            if debugprint: print("%s :" % i)
            for h in g.is2hs[i]:
                if debugprint:
                    print("  %s : pi=%s all_u=%s" % (h, pi[h], all_u[h]))
                rdsum = 0
                for a in g.isactions[i]:
                    rd = makeRDiff(g, h, i, a, sigma, pi, all_u)
                    if debugprint:
                        print("    %s -> %s :" % (a, rd))
                    rdiff[i][a] += rd
                    rdsum += rd
                if debugprint:
                    if abs(rdsum) > 1e-8:
                        for a in g.isactions[i]:
                            print("rdiff %s %s %s %s" %(i, h, a, makeRDiff(g,h, i, a, sigma, pi, all_u)))
        for i, actions in g.isactions.items():
            q[i] = {a : max(0, q[i][a] + rdiff[i][a]) for a in actions}
            if debugprint:
                print("r[%s] = %s" %(i, q[i]))
    if debugprint:
        print("av_sigma=%s" % make_av_sigma(sum_sigma))
    return sigma

def cfrplus(g, iteration = 100, debugprint = True):
    q = {i : { a : 0 for a in actions } for i, actions in g.isactions.items() }
    sum_pi = {i : 0 for i in g.isactions.keys() }
    sum_sigma = {i : { a : 0 for a in actions } for i, actions in g.isactions.items() }
    sigma = {}
    all_u = {}
    for count in range(iteration):
        sigma = oneIteration(g, count, q, sum_pi, sum_sigma, sigma, all_u, debugprint)
    av_sigma = make_av_sigma(sum_sigma)
    pi = {}
    makePiU(g, sigma, pi, all_u)
    print("game value = %s, win percent = %s" % (all_u[g.init], (all_u[g.init] + 1) * 100.0 / 2.0))
    print("av_sigma=")
    pp_sigma(g, av_sigma, all_u, pi)


import numpy as np
import scipy.stats

from .pareto import Pareto

def score(mode, predictor_list, test, training_list, **kwargs):
    if test.X.shape[0] == 0:
        return np.zeros(0)

    if mode == "EHVI":
        pareto = kwargs["pareto"]
        fmean, fstd = _get_fmean_fstd(predictor_list, training_list, test)
        f = EHVI(fmean, fstd, pareto)
    elif mode == "HVPI":
        pareto = kwargs["pareto"]
        fmean, fstd = _get_fmean_fstd(predictor_list, training_list, test)
        f = HVPI(fmean, fstd, pareto)
    elif mode == "TS":
        alpha = kwargs.get("alpha", 1.0)
        reduced_candidate_num = kwargs["reduced_candidate_num"]
        f = TS(
            predictor_list,
            training_list,
            test,
            alpha,
            reduced_candidate_num=reduced_candidate_num,
        )
    else:
        raise NotImplementedError("mode must be EHVI, HVPI or TS.")
    return f

def HVPI(fmean, fstd, pareto):
    """
    Calculate Hypervolume-based Probability of Improvement (HVPI).

    Reference: (Couckuyt et al., 2014) Fast calculation of multiobjective probability of improvement and expected improvement criteria for Pareto optimization
    """

    N = fmean.shape[0]
    n_obj = pareto.num_objectives

    if pareto.reference_min is None:
        pareto.set_reference_min()
    reference_min = pareto.reference_min

    # Pareto front with reference points
    # shape: (front_size, n_obj)
    front = np.r_[
        np.array(reference_min).reshape((1, n_obj)),
        pareto.front,
        np.full((1, n_obj), np.inf),
    ]

    ax = np.arange(n_obj)
    n_cell = pareto.cells.lb.shape[0]

    # convert to minimization problem
    l = front[pareto.cells.ub, ax].reshape((1, n_cell, n_obj)) * -1
    u = front[pareto.cells.lb, ax].reshape((1, n_cell, n_obj)) * -1

    # convert to minimization problem
    fmean = fmean.reshape((N, 1, n_obj)) * -1
    fstd = fstd.reshape((N, 1, n_obj))

    # calculate cdf
    Phi_l = scipy.stats.norm.cdf((l - fmean) / fstd)
    Phi_u = scipy.stats.norm.cdf((u - fmean) / fstd)

    #  calculate PoI
    poi = np.sum(np.prod(Phi_u - Phi_l, axis=2), axis=1)  # shape: (N, 1)

    # calculate hypervolume contribution of fmean point
    hv_valid = np.all(fmean < u, axis=2)  # shape: (N, n_cell)
    hv = np.prod(u - np.maximum(l, fmean), axis=2)  # shape: (N, n_cell)
    hv = np.sum(hv * hv_valid, axis=1)  # shape: (N, 1)

    # HVPoI
    score = hv * poi
    return score

def EHVI(fmean, fstd, pareto):
    """
    Calculate Expected Hyper-Volume Improvement (EHVI).

    Reference: (Couckuyt et al., 2014) Fast calculation of multiobjective probability of improvement and expected improvement criteria for Pareto optimization
    """

    N = fmean.shape[0]
    n_obj = pareto.num_objectives

    if pareto.reference_min is None:
        pareto.set_reference_min()
    if pareto.reference_max is None:
        pareto.set_reference_max()
    reference_min = pareto.reference_min
    reference_max = pareto.reference_max

    # Pareto front with reference points
    # shape: (front_size, n_obj)
    front = np.r_[
        np.array(reference_min).reshape((1, n_obj)),
        pareto.front,
        np.array(reference_max).reshape((1, n_obj)),
    ]

    ax = np.arange(n_obj)

    # convert to minimization problem
    l = front[pareto.cells.ub, ax] * -1
    u = front[pareto.cells.lb, ax] * -1

    n_cell = pareto.cells.lb.shape[0]

    # shape: (n_cell, 1, n_cell, n_obj)
    l = np.tile(l, (n_cell, 1, 1, 1))
    u = np.tile(u, (n_cell, 1, 1, 1))
    a = l.transpose((2, 1, 0, 3))
    b = u.transpose((2, 1, 0, 3))

    # convert to minimization problem
    fmean = fmean.reshape((1, N, 1, n_obj)) * -1
    fstd = fstd.reshape((1, N, 1, n_obj))

    # calculate pdf, cdf
    phi_min_bu = scipy.stats.norm.pdf((np.minimum(b, u) - fmean) / fstd)
    phi_max_al = scipy.stats.norm.pdf((np.maximum(a, l) - fmean) / fstd)
    Phi_l = scipy.stats.norm.cdf((l - fmean) / fstd)
    Phi_u = scipy.stats.norm.cdf((u - fmean) / fstd)
    Phi_a = scipy.stats.norm.cdf((a - fmean) / fstd)
    Phi_b = scipy.stats.norm.cdf((b - fmean) / fstd)

    ## calculate G
    is_type_A = np.logical_and(a < u, l < b)
    is_type_B = u <= a

    # note: Phi[max_or_min(x,y)] = max_or_min(Phi[x], Phi[y])
    EI_A = (
        (b - a) * (np.maximum(Phi_a, Phi_l) - Phi_l)
        + (b - fmean) * (np.minimum(Phi_b, Phi_u) - np.maximum(Phi_a, Phi_l))
        + fstd * (phi_min_bu - phi_max_al)
    )
    EI_B = (b - a) * (Phi_u - Phi_l)

    G = EI_A * is_type_A + EI_B * is_type_B
    score = np.sum(np.sum(np.prod(G, axis=3), axis=0), axis=1)  # shape: (N, 1)
    return score

def TS(predictor_list, training_list, test, alpha=1, reduced_candidate_num=None):
    score = [
        predictor.get_post_samples(training, test, alpha=alpha)
        for predictor, training in zip(predictor_list, training_list)
    ]
    score = np.array(score).reshape((len(predictor_list), test.X.shape[0])).T
    pareto = Pareto(num_objectives=len(predictor_list))

    if reduced_candidate_num is None or score.shape[0] <= reduced_candidate_num:
        use_idx = np.arange(score.shape[0])
    else:
        use_idx = np.arange(reduced_candidate_num)
        use_idx = np.random.choice(use_idx, reduced_candidate_num, replace=False)

    # pareto.update_front(score)
    pareto.update_front(score[use_idx, :])

    # randomly choose candidate from pareto frontiers
    chosen_idx = np.random.choice(pareto.front_num)
    score_res = np.zeros(score.shape[0])
    score_res[use_idx[chosen_idx]] = 1  # only chosen_idx th value is one.

    return score_res

def _get_fmean_fstd(predictor_list, training_list, test):
    fmean = [
        predictor.get_post_fmean(training, test)
        for predictor, training in zip(predictor_list, training_list)
    ]
    fcov = [
        predictor.get_post_fcov(training, test)
        for predictor, training in zip(predictor_list, training_list)
    ]

    # shape: (N, n_obj)
    fmean = np.array(fmean).T
    fstd = np.sqrt(np.array(fcov)).T
    return fmean, fstd


import pygame
from os import path
pygame.init()

# Se crea la ventana donde se va a mostrar todo y se le define el tamaño
win = pygame.display.set_mode((500,480))

# Se le pone un titulo a la ventana
pygame.display.set_caption("Primer Pygame")

bg = pygame.image.load(path.join('Sprites','bg.jpg'))

char = pygame.image.load(path.join('Sprites', 'standing.png'))

score = pygame.font.SysFont('Arial', 30, True)

# Cargamos los efectos de sonido
BulletSound = pygame.mixer.Sound(path.join('Sprites','bullet.wav'))
HitSound    = pygame.mixer.Sound(path.join('Sprites','hit.wav'))

Music = pygame.mixer.music.load(path.join('Sprites','music.mp3'))
pygame.mixer.music.play(-1)

# Clock speed
clock = pygame.time.Clock()

class Player(object):
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 5
        self.isjumping = False
        self.jumpCount = 10
        self.left = False
        self.right = False
        self.walkCount = 0
        self.standing = False
        self.hitbox = (self.x + 20, self.y + 10, 28, 55)
        self.score = 0

        # Cargamos las imagenes que utuilizaremos como sprites
        self.walkRight = [pygame.image.load(path.join('Sprites','R1.png')), pygame.image.load(path.join('Sprites','R2.png')), pygame.image.load(path.join('Sprites','R3.png')),
                          pygame.image.load(path.join('Sprites','R4.png')), pygame.image.load(path.join('Sprites','R5.png')), pygame.image.load(path.join('Sprites','R6.png')),
                          pygame.image.load(path.join('Sprites','R7.png')), pygame.image.load(path.join('Sprites','R8.png')), pygame.image.load(path.join('Sprites','R9.png'))]

        self.walkLeft =  [pygame.image.load(path.join('Sprites','L1.png')), pygame.image.load(path.join('Sprites','L2.png')), pygame.image.load(path.join('Sprites','L3.png')),
                          pygame.image.load(path.join('Sprites','L4.png')), pygame.image.load(path.join('Sprites','L5.png')), pygame.image.load(path.join('Sprites','L6.png')),
                          pygame.image.load(path.join('Sprites','L7.png')), pygame.image.load(path.join('Sprites','L8.png')), pygame.image.load(path.join('Sprites','L9.png'))]

    def draw(self, win):
        # Dibujamos los sprites
        if self.walkCount + 1 >= 27:
            man1.walkCount = 0

        if not(self.standing):
            if self.left:
                win.blit(self.walkLeft[self.walkCount//3], (self.x,man1.y))
                self.walkCount += 1
            elif self.right:
                win.blit(self.walkRight[self.walkCount//3], (self.x,man1.y))
                self.walkCount +=1
        else:
            if self.right:
                win.blit(self.walkRight[0], (self.x, self.y))
            else:
                win.blit(self.walkLeft[0], (self.x, self.y))

        self.hitbox = (self.x + 20, self.y + 10, 28, 55)
        # Se dibuja la hitbox
        #pygame.draw.rect(win, (255, 0, 255), self.hitbox, 1)

        #win.blit(char, (self.x,self.y))

    def hit(self):
        self.x = 60
        self.y = 410
        self.walkCount = 0

        font1 = pygame.font.SysFont('comicsans', 100)
        text  = font1.render('-5', 1, (255, 0, 0))
        win.blit(text, (250 - (text.get_width()/2), 200))
        pygame.display.update()
        i = 0
        while i < 300:
            pygame.time.delay(10)
            i += 1

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    i = 301
                    pygame.quit()

class Projectile(object):
    def __init__(self, x, y, radius, color, facing):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.facing = facing
        self.vel = 8 * facing
        self.hitbox = (self.x + 17, self.y + 11, 29, 52)

    def draw(self,win):
        # ASi se dibuja un circulo en pygame:
        pygame.draw.circle(win, self.color, (self.x, self.y), self.radius)

class Enemy(object):

    walkRight = [pygame.image.load(path.join('Sprites','R1E.png')), pygame.image.load(path.join('Sprites','R2E.png')), pygame.image.load(path.join('Sprites','R3E.png')),
                 pygame.image.load(path.join('Sprites','R4E.png')), pygame.image.load(path.join('Sprites','R5E.png')), pygame.image.load(path.join('Sprites','R6E.png')),
                 pygame.image.load(path.join('Sprites','R7E.png')), pygame.image.load(path.join('Sprites','R8E.png')), pygame.image.load(path.join('Sprites','R9E.png')),
                 pygame.image.load(path.join('Sprites','R10E.png')), pygame.image.load(path.join('Sprites','R11E.png'))]

    walkLeft =  [pygame.image.load(path.join('Sprites','L1E.png')), pygame.image.load(path.join('Sprites','L2E.png')), pygame.image.load(path.join('Sprites','L3E.png')),
                 pygame.image.load(path.join('Sprites','L4E.png')), pygame.image.load(path.join('Sprites','L5E.png')), pygame.image.load(path.join('Sprites','L6E.png')),
                 pygame.image.load(path.join('Sprites','L7E.png')), pygame.image.load(path.join('Sprites','L8E.png')), pygame.image.load(path.join('Sprites','L9E.png')),
                 pygame.image.load(path.join('Sprites','L10E.png')), pygame.image.load(path.join('Sprites','L11E.png'))]

    def __init__(self, x, y, width, height, end):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.pathh = [x, end]
        self.walkCount = 0
        self.vel = 3
        self.hitbox = (self.x + 17, self.y + 2, 31, 57)
        self.health = 10
        self.visible = True

    def draw(self, win):

        self.move()

        if self.visible == True:

            if self.walkCount + 1 >= 33:
                self.walkCount = 0

            if self.vel > 0:
                win.blit(self.walkRight[self.walkCount//3], (self.x, self.y))
                self.walkCount += 1
            else:
                win.blit(self.walkLeft[self.walkCount//3], (self.x, self.y))
                self.walkCount += 1

            pygame.draw.rect(win,  (0, 255,0), (self.hitbox[0], self.hitbox[1] - 20, 50 , 10))
            if self.health < 10:
                pygame.draw.rect(win,  (255, 0, 0), ( self.hitbox[0] + 50 - (5 * (10 - self.health)), self.hitbox[1] - 20,  5 * (10 - self.health) , 10))

            self.hitbox = (self.x + 17, self.y + 2, 31, 57)
            # Dibujar la hitbox
            #pygame.draw.rect(win, (255, 0, 0), self.hitbox , 1)

    def move(self):
        if self.vel > 0:
            if self.x < self.pathh[1] + self.vel:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.x += self.vel
                self.walkCount = 0

        else:
            if self.x > self.pathh[0] - self.vel:
                self.x += self.vel

            else:
                self.vel = self.vel * -1
                self.x += self.vel
                self.walkCount = 0

    def hit(self):
        self.health -= 1 if self.health > 0 else self.health
        if self.health == 0:
            self.visible = False

        print("hit, healt: %d" % self.health)

def redrawGameWindow():
    # Para poder mostrarlo se hacer un refresco a la pantalla
    win.blit(bg, (0,0)) # Colocamos la imagen bg de fondo
    man1.draw(win)
    enemy1.draw(win)

    win.blit(score.render("Score: %d" % man1.score, True, (255, 0, 0)), (10,10))

    for bullet in bullets:
        bullet.draw(win)

    # Codigo de como dibujar un rectangulo
    #pygame.draw.rect(win, (255,0,0), (x, y, width, height))

    pygame.display.update()

# Ceamoos el caracter
#             posx posy
man1 = Player(300, 410, 64, 64)
enemy1 = Enemy(100, 420, 64, 64, 200)
# Cramos las balas
bullets = []
bulletavailable = True
bulletcooldown = 4

# This is the main loop
run = True
while run:
    clock.tick(27)

    if man1.hitbox[1] < enemy1.hitbox[1] + enemy1.hitbox[3] and man1.hitbox[1] + man1.hitbox[1] + man1.hitbox[3] > enemy1.hitbox[1]:
        if man1.hitbox[0] + man1.hitbox[2] > enemy1.hitbox[0] and man1.hitbox[0] < enemy1.hitbox[0] + enemy1.hitbox[2]:
            man1.hit()
            man1.score -= 5

    if bulletavailable == False:
        bulletcooldown -= 1
        bulletavailable = True if bulletcooldown == 0 else False

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    for x, bullet in enumerate(bullets):
        if bullet.y - bullet.radius < enemy1.hitbox[1] + enemy1.hitbox[3] and  bullet.y + bullet.radius > enemy1.hitbox[1]:
            if bullet.x + bullet.radius > enemy1.hitbox[0] and bullet.x - bullet.radius < enemy1.hitbox[0] + enemy1.hitbox[2]:
                enemy1.hit()
                bullets.pop(x)
                HitSound.play()
                man1.score += 1

        if bullet.x < 500 and bullet.x > 0:
            bullet.x += bullet.vel
        else:
            bullets.pop(x)

    keys = pygame.key.get_pressed()

    if keys[pygame.K_SPACE] and bulletavailable:
        BulletSound.play()
        bulletcooldown = 4
        facing = 1 if man1.right else -1
        if len(bullets) < 5:
            bulletavailable = False
            bullets.append(Projectile(round(man1.x + man1.width//2), round(man1.y + man1.height//2), 6, (255,255,255), facing))

    if keys[pygame.K_LEFT] and man1.x > 0:
        man1.x -= man1.vel
        man1.left = True
        man1.right = False
        man1.standing = False

    elif keys[pygame.K_RIGHT] and man1.x < (500 - man1.width):
        man1.x += man1.vel
        man1.right = True
        man1.left = False
        man1.standing = False

    else:
        man1.standing = True
        man1.walkcount = 0

    if not(man1.isjumping):
        if keys[pygame.K_UP]:
            man1.isjumping = True
            man1.right = False
            man1.left = False

    else:
        if man1.jumpCount >= -10:
            neg = 1
            if man1.jumpCount < 0:
                neg = -1
            y_ayer = man1.y
            man1.y -= (man1.jumpCount ** 2) * 0.5 * neg
            y_dif = abs(man1.y - y_ayer)
            man1.jumpCount -= 1

        else:
            man1.isjumping = False
            man1.jumpCount = 10

    # Llamamos la funcion que dibuja
    redrawGameWindow()

pygame.quit()


class Node:
    def __init__(self,key):
        self.next = None
        self.data = key

# This function will take a linkedlist as input and remove all the elements that appear
# more than once in the list.  linkedlist = 1->2->2->2->3->4->4->5->6    :   1->2->3->4->5->6
def removeDuplicates(head):
    if(head == None):
        return None
    if head.next == None:
        return head

    visitedNodes = set()
    visitedNodes.add(head.data)
    current = head.next
    previous = head

    while current != None:
        if current.data in visitedNodes:
            previous.next = current.next
        else:
            visitedNodes.add(current.data)
            previous = current
        current = current.next

    return head

node0 = Node(1)
node1 = Node(2)
node2 = Node(2)
node3 = Node(2)
node4 = Node(3)
node5 = Node(4)
node6 = Node(4)
node7 = Node(5)
node8 = Node(6)

node0.next = node1
node1.next =  node2
node2.next = node3
node3.next = node4
node4.next = node5
node5.next = node6
node6.next = node7
node7.next = node8
node8.next = None

head = removeDuplicates(node0)
current = head
while current != None:
    print(current.data,end=" ")
    current = current.next


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

# In[1]
import openai
import time
import datetime
import logging
import multiprocessing
import pandas as pd

from openai_apikey import api_key

openai.api_key = api_key

def get_prompt(x):
    """给定数据x组装prompt并返回"""
    return x

def get_response(prompt):
    """给prompt调用api，并返回结果，模型及tempture等超参自定"""
    # PROMPT = """请根据给定文章生成航空航天领域相关知识的问题，并从文章中抽取问题的回答。\n
    #             输出格式：\n
    #             问题1；回答1 \n
    #             ...
    #             问题n；回答n \n
    #         """
    PROMPT = """
生成航空航天领域问题
请根据提供的航空航天领域相关文章，从文章中抽取部分文字(15个字内)，并为该文字生成对应的问题。抽取的文字为问题的答案。\n
示例输入：
文章：随着航空航天技术的不断发展，人们对于太空旅行和探索的热情不断高涨。SpaceX公司最近成功发射了一枚载人火箭，将四名宇航员送往国际空间站。这次任务的成功标志着航天技术又迈上了新的台阶。
示例输出：
Q: SpaceX最近发射的是什么类型的火箭？
A: 载人火箭。
Q: SpaceX的最新任务标志着什么？
A: 航天技术迈上了新的台阶。
"""
    completion = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        temperature=0.1,
        top_p=1,
        max_tokens=2048,    # 最大token个数根据model定
        messages=[
            { "role": "system", "content": f"{PROMPT}"},
            { "role": "user", "content": f"{prompt}" },
            { "role": "assistant", "content": "生成的问题和抽取的答案为："}
        ]
      )
    return completion

#In[1]
# debug_text = "这三种在设计、任务运作方式上都有很大差别。美国航天飞机的核心级发动机安装在轨道器上起到重复使用主发动机降低成本的作用，轨道器本身是航天运载器的核心组成部分，发射时出大力。苏俄的暴风雪航天飞机则基本是完全靠能源超重型运载火箭驮上天，轨道器上没主发动机只有最后用来执行远地点轨道插入的小轨道机动发动机，轨道器发射时不出力最后入轨时小推一把。算重复使用的大型载人货运多功能飞船。欧洲被取消的赫尔墨斯航天飞机完全是放在火箭顶端如同载人飞船一样发射到太空，最终版完全取消了载荷舱专心作为可重复使用的载人飞船角色。甚至对接口、太空行走气闸舱和轨道机动发动机放在可分离的尾部的资源舱里，是一次性的在重返大气层前抛掉。"
# completion = get_response(debug_text)

# In[1]
def get_result_from_response(res_context, input_text, paras, Ques, Ans, Ans_startIdx, Ans_EndIdx):
    """分析并处理调用api的返回结果，不同model不同参数，返回结构可能不一样，最好自己在notebook里看看"""
    res_context = res_context.choices[0].message.content

    all_data = res_context.split('\n\n')
    logging.warning("生成结果：{}".format(all_data))
    for idx in range(0, len(all_data)):
        # ques = all_data[idx].strip()
        # ans = all_data[idx + 1].strip()
        return_lst = all_data[idx].split('\n')
        if len(return_lst) <= 1:
            continue
        else:
            ques, ans = return_lst[0], return_lst[-1]
            ques, ans = ques[2:].strip(), ans[2:].strip()
            for i in range(8):
                # s.index('')
                start_idx = input_text.find(ans[i:8])
                # logging.warning(f'debug_start: {ans[i:8]}')
                if start_idx != -1:
                    break
            else:
                continue
            end_idx = start_idx + len(ans) - 1
            text_end_pos = len(input_text) - 1
            if end_idx > text_end_pos:
                end_idx = text_end_pos
            paras.append(input_text)
            Ques.append(ques)
            Ans.append(ans)
            Ans_startIdx.append(start_idx)
            Ans_EndIdx.append(end_idx)

    logging.warning(f'result: {paras}, {Ques}, {Ans}, {Ans_startIdx}, {Ans_EndIdx}')
    return None

# In[1]
def sleep(id_thread):
    t = min(45, 5 + id_thread *3)
    logging.info(f"子进程{id_thread}开始睡眠了， 睡{t}秒")
    time.sleep(t)

def save_result(paras, Ques, Ans, Ans_startIdx, Ans_EndIdx, save_path):
    """保存结果，需要注意，每5次调用会保存一次，不要把之前的覆盖了"""
    # 从现有的 JSON 文件中读取数据
    # with open('./QA_data1.json', 'r') as f:
    #     existing_data = json.load(f)
    # existing_df = pd.DataFrame(existing_data)
    logging.warning('saving...............................')
    # existing_df = pd.read_json(save_path)

    # # 创建一个新的 DataFrame，其中包含要追加的数据
    # new_data = {'paragraph': paras, 'question': Ques, 'answer': Ans, 'start_idx': Ans_startIdx, 'end_idx': Ans_EndIdx}
    # new_df = pd.DataFrame(new_data)

    # # 将新数据追加到现有的 DataFrame
    # updated_df = existing_df.append(new_df, ignore_index=True)

    # # 将更新后的 DataFrame 保存为 JSON 文件
    # updated_df.to_json(save_path, orient='records', force_ascii=False)

    # 重新保存所有
    new_data = {'paragraph': paras, 'question': Ques, 'answer': Ans, 'start_idx': Ans_startIdx, 'end_idx': Ans_EndIdx}
    new_df = pd.DataFrame(new_data)
    new_df.to_json(save_path, orient='records', force_ascii=False)

# In[1]
def split_data(id_thread, i, x, paras, Ques, Ans, Ans_startIdx, Ans_EndIdx):
    breakTurn = 0
    response = None
    while True:                                           # 主要应对速度限制，这里除非报没钱了的错误，否则一直循环
        prompt = get_prompt(x)
        try:
            response = get_response(prompt)
            break
        except Exception as e:
            if isinstance(e, openai.error.RateLimitError) and \
                'You exceeded your current quota, please check your plan and billing details.' in e.user_message:
                logging.critical("你没钱了")                 # logger
                break
            else:                                           #不知名的错误
                logging.error(f"{id_thread} error message {e.user_message}")
                if breakTurn == 5:
                    break
                breakTurn += 1
                sleep(id_thread)

    if i % 3 == 0:
        logging.warning(f"子进程{id_thread}正在第{i}次调用api 已获取结果")

    get_result_from_response(response, x,
                                paras, Ques, Ans, Ans_startIdx, Ans_EndIdx)

def thread_fun(id_thread, data, save_path):
    """
    每个进程所执行的程序，每个进程的处理数据过程写在此处
    x, y 进程函数参数
    """
    paras, Ques, Ans, Ans_startIdx, Ans_EndIdx = [],[],[],[],[]
    for i, x in enumerate(data):
        xlen = len(x)
        if xlen <= 600:
            logging.warning('input_text: ' + x)
            split_data(id_thread, i, x, paras, Ques, Ans, Ans_startIdx, Ans_EndIdx)
        else:
            x_mid = x[:600]
            x_right = x[600:1500]
            logging.warning('input_text: ' + x_mid)
            split_data(id_thread, i, x_mid, paras, Ques, Ans, Ans_startIdx, Ans_EndIdx)
            logging.warning('input_text: ' + x_right)
            split_data(id_thread, i, x_right, paras, Ques, Ans, Ans_startIdx, Ans_EndIdx)

        if i % 5 == 0:
            save_result(paras, Ques, Ans, Ans_startIdx, Ans_EndIdx, save_path)

    save_result(paras, Ques, Ans, Ans_startIdx, Ans_EndIdx, save_path)
    return None

# In[1]
import json

# baike
with open('./dataspace/baike.all_crawled_info.jsonl', 'r', encoding='utf-8') as f:
    baike_cont = []
    for line in f:
        json_data = json.loads(line.strip())

        # 分别提取content1和content2的内容
        if json_data['content'] != '':
            baike_cont.append(json_data['content'])

# print(baike_cont, len(baike_cont))

# zhidao
with open('./dataspace/zhidao.all_crawled_info.jsonl', 'r', encoding='utf-8') as f:
    zhidao_cont1, zhidao_cont2 = [], []
    for line in f:
        json_data = json.loads(line.strip())

        # 分别提取content1和content2的内容
        if json_data['content1'] != '':
            zhidao_cont1.append(json_data['content1'])
        if json_data['content2'] != '':
            zhidao_cont2.append(json_data['content2'])

# print(zhidao_cont1, len(zhidao_cont1))
# print(zhidao_cont2, len(zhidao_cont2))

# In[1]
# 调用api，生成问题和答案
if __name__=='__main__':
    DEBUG = True
    num_thread = 1 if not DEBUG else 1
    thread_src_ref_pre = [zhidao_cont1, zhidao_cont2, baike_cont]                      # 每个元素对应着thread要处理的那一批数据（按下标对应）
    save_path = './output/'                        # save_path

    pool = multiprocessing.Pool(processes=num_thread)
    processes = []
    for i in range(0, num_thread):
        p = pool.apply_async(thread_fun, (i, thread_src_ref_pre[i], save_path+'QA'+str(i)+'.json'))
        processes.append(p)
    pool.close()
    pool.join()
    for p in processes:
        p.get()


#Family Knapsack
#Author: Justin Boyer
#Input: Requires the input file passed as an argument.  The input file cotnains information
#  about the itmes, family and weights. The proper format is detailed in the readme
#Output: written to results.txt
#Outside references: geeksforgeeks.org

import sys
import math

#Class table cell holds info about 2 things: 1) the optimum value for a given item number and weight combination.
#2) The corrisponding items making up that optimum value
class Cell:
    def __init__(self):
        self.optVal = 0
        self.itemList = ""

def knapsackTable(W, vList, wList):
  items = len(vList)
  #initialize table cells to -1
  valTable = [[Cell() for x in range(W+1)] for x in range(items +1)]

  for item in range(items+1):
    for curWeight in range(W+1):

        #3 cases:
        #1)Base case: if the weight is 0 or the item number is 0 then the cell has a value of 0 (Cell defaults to this)
        if item == 0 or curWeight == 0:
            pass
            #valTable[item][curWeight] = 0
        #2)Current item fits in the bag. Check if it is better with or without the new item
        #use i - 1 when getting info from the arrays to account for the 0 col in the table
        elif wList[item-1] <= curWeight:
            newVal = vList[item-1] + valTable[item-1][curWeight - wList[item-1]].optVal
            oldOpt = valTable[item-1][curWeight].optVal
            if newVal > oldOpt:
                #update the opt value
                valTable[item][curWeight].optVal = vList[item-1] + valTable[item-1][curWeight - wList[item-1]].optVal
                #update the item list
                valTable[item][curWeight].itemList = valTable[item-1][curWeight - wList[item-1]].itemList  + str(item) + " "

            #if the oldOptimum is better, just keep that
            else:
              #update the opt value
              valTable[item][curWeight].optVal = valTable[item-1][curWeight].optVal
              #update the item list
              valTable[item][curWeight].itemList = valTable[item-1][curWeight].itemList

        #3)The current item does not fit - just use the optimum value from the previous column
        else:
            #update the opt value
            valTable[item][curWeight].optVal = valTable[item-1][curWeight].optVal
            #update the item list
            valTable[item][curWeight].itemList = valTable[item-1][curWeight].itemList

       # print("ValTable[item][curWeight] ", item, curWeight)
       # print("optVal: ",  valTable[item][curWeight].optVal)
       # print("itemList: ",  valTable[item][curWeight].itemList)
       # print()

  return valTable;

def main():
  #Ensure that the input file is included
  if(len(sys.argv) < 2):
    print("Please include the file containing lists to sort as an argument")
    quit()

  #if the input file is included, continue setting up in/out files
  inFile = sys.argv[1]
  readFile = open(inFile, "r")
  writeFile = open("results.txt","w")

  #The first line is the number of test cases T where T in (1:100)
  Tcount = int(readFile.readline())
  for case in range(Tcount):
    #For each case, reinitialize the arrays to send to the knapsack function
    carryCaps = []
    itemWeight = []
    itemValue = []

    #for each test case, seperate the different parts
	#first get number of items
    itemCount = int(readFile.readline())

    #Next circle through the items to build value and weight arrays
    for item in range(itemCount):
      VandW = readFile.readline()
      VandW = VandW.rstrip().split()
      itemValue.append(int(VandW[0]))
      itemWeight.append(int(VandW[1]))

	#After that get the number of family members
    #and add their carrying capacities to the carryCaps array
    famCount = int(readFile.readline())
    for person in range(famCount):
      carryCaps.append(int(readFile.readline()))

    #Arrays are filled for the current case: itemValue, item Weight, and carryCaps
    #Build the table for the item set and then get optimum solutions for each family member's carryCap
    maxWeight = max(carryCaps)
    table = knapsackTable(maxWeight, itemValue, itemWeight)

    #reset the max value for each case
    maxVal = 0
    indItemLists = []
    for person in range(famCount):
      #optimum value for a given family member is found with table[value][weight]
      maxVal = maxVal + table[len(itemValue)][carryCaps[person]].optVal
      indItemLists.append(table[len(itemValue)][carryCaps[person]].itemList)

    #Write Case info to file
    writeFile.write("Test Case %d \n" % (case +1))
    writeFile.write("Total Price %d \n" % (maxVal))
    writeFile.write("Member Items\n")
    count  = 1
    for i in indItemLists:
      #print(i)
      writeFile.write("   %d: " % (count))
      writeFile.write(indItemLists[count-1])
      writeFile.write("\n")
      count = count+1

    writeFile.write("\n")
    print("Case %d complete" % (case+1))

  writeFile.close()
  readFile.close()
  return;

main()
print("Finished")


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
#  =================================================
# Trigger
#   - Author jouke hijlkema <jouke.hijlkema@onera.fr>
#   - ven. sept. 14:18 2017
#   - Initial Version 1.0
#  =================================================

import gi
from gi.repository import Gtk, GObject, Gdk

class dayTask(GObject.GObject):
    __gsignals__ = {'taskDone': (GObject.SIGNAL_RUN_FIRST, None, (GObject.GObject,)) }
    def __init__(self,t):
        "docstring"
        self.task=t
        self.builder  = Gtk.Builder()
        self.builder.add_from_file("./Gui/Parts/dayTask.glade")

        self.main = self.builder.get_object("main")
        self.builder.get_object("Title").set_text("%s : %s"%(t.project,t.title))
        self.builder.get_object("done").set_value(t.done.get_value())


#实现LetNet5模型
import tensorflow as tf
import tensorflow.contrib.slim as slim
import numpy as np

from tensorflow.examples.tutorials.mnist import input_data

#定义前向传播网络
def lenet5(inputs):
    #输入转化为4维数组,batch 图片大小
    inputs = tf.reshape(inputs,[-1,28,28,1])
    #定义第一层卷积层,深度32 过滤器大小[5，5],使用全0填充
    net = slim.conv2d(inputs,32, [5,5],padding='SAME',scope='layer1-conv')
    #定义最大池化层,过滤器2x2 步长2
    net = slim.max_pool2d(net, 2, stride=2, scope='layer2-max-pool')
    #定义第三次卷积层 64层深度， 5x5过滤器, 全0填充
    net = slim.conv2d(net,64 , [5,5],padding='SAME',scope='layer3-conv')
    #第四层最大池化层 步长2 过滤器 2X2
    net = slim.max_pool2d(net, 2, stride=2, scope='layer4-max-pool')
    #将4维矩阵转化维2维，方便全连接计算
    net = slim.flatten(net,scope='flatten')
    #两次全连接,500和10个隐藏节点
    net = slim.fully_connected(net,500,scope='layer5')
    net = slim.fully_connected(net,10,scope='output')
    return net

def train(mnis):
    x = tf.placeholder(tf.float32,[None,784], name='x-input')
    y_ = tf.placeholder(tf.float32,[None,10],name= 'y-input')

    y = lenet5(x)

    cross_entry = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y,labels=tf.argmax(y_,1))
    loss = tf.reduce_mean(cross_entry)
    train_op = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
    correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
    evaluation_step = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    with tf.Session() as sess:
        tf.global_variables_initializer().run()

        for i in range(10000):
            xs,ys = mnis.train.next_batch(100)
            _,loss_value = sess.run([train_op,loss],feed_dict={x: xs, y_: ys})
            if i % 1000 == 0:
                x_valid,y_valid = mnis.validation.images,mnis.validation.labels
                corr = sess.run(evaluation_step,feed_dict={x: x_valid, y_: y_valid})
                print('经过 %d 轮训练,验证数据正确率为 %.2f %%' % (i,corr * 100))
                print('经过 %d 轮训练,损失为 %s' % (i, loss_value))

        x_test, y_test = mnis.test.images, mnis.test.labels
        test_corr = sess.run(evaluation_step, feed_dict={x: x_test, y_: y_test})
        print('最终测试数据正确率为 %.2f %%' % (test_corr * 100))

def main():
    mnis = input_data.read_data_sets("/root/ML/data/ini_data/mnis_data",one_hot=True)
    train(mnis)

if __name__ == "__main__":
    main()