"""
Translation of SEMAFOR 3.0's edu.cmu.cs.lti.ark.fn.segmentation.RoteSegmenter.java into Python.
This uses a whitelist of n-grams extracted from FrameNet gold frame annotations,
and filters out some of the unigrams according to heuristics.

Argument: path to CoNLL-format file for the input

Legacy output format (default): tab-separated lines like

9_10#true    1#true    11#true    13#true    15#true    0

(underscore-spearated token IDs for each target; last column is sentence ID)

Pass -j to produce JSON output.

We observe a couple of differences from SEMAFOR's RoteSegmenter:
1) In SEMAFOR 3.0, lemmas were not lowercased before being looked up in the whitelist,
   so some additional targets are predicted here. This is now fixed in the Java codebase.
2) The Java WordNet library (JWNL) performs aggressive simplification of words
   during lemmatization, for example stripping out prefixes/suffixes separated by a
   hyphen or period until it finds a dictionary match. More of those lemmas will be
   found in the whitelist than when using NLTK's morphy, which only removes
   inflectional morphology. (For instance, "scud-B missile" matches whitelist entry
   "scud_N missle_N" with the Java implementation, whereas here only "missle" is
   marked as a target.) Note that we are using the same whitelist file, which was
   presumably created with the JWNL lemmatization.

@author: Nathan Schneider (nschneid@cs.cmu.edu)
@since: 2013-05-22
"""

from __future__ import print_function
import sys, os, codecs, json

from semafor.utils.malt_to_conll import read_conll

def get_coarse_pos(pos):
    return pos[0]   # this seems to be what was in the whitelist

with codecs.open(os.path.dirname(os.path.abspath(__file__))+'/semafor3.0_targets_whitelist.txt', 'r', 'utf-8') as whitelistF:
    WHITELIST = frozenset(whitelistF.read().splitlines())

MAX_LEN = 4 # the maximum length of ngrams we'll look for

FORBIDDEN_WORDS = frozenset('a an as for i it so the with'.split())

# if these words precede "of", "of" should not be discarded
PRECEDING_WORDS_OF = frozenset('''%
                                all
                                face
                                few
                                half
                                majority
                                many
                                member
                                minority
                                more
                                most
                                much
                                none
                                one
                                only
                                part
                                proportion
                                quarter
                                share
                                some
                                third'''.split())

# if these words follow "of", "of" should not be discarded
FOLLOWING_WORDS_OF = frozenset('all group their them us'.split())

# all prepositions should be discarded
LOC_PREPS = frozenset('above against at below beside by in on over under'.split())

TEMPORAL_PREPS = frozenset('after before'.split())
DIR_PREPS = frozenset('into through to'.split())
FORBIDDEN_POS_PREFIXES = frozenset(["PR", "CC", "IN", "TO"])

def get_segmentation(sentence):
    numTokens = len(sentence)

    # start indices that we haven't used yet
    remainingStartIndices = set(range(numTokens))

    lemmas = [token.lemma + "_" + get_coarse_pos(token.postag) for token in sentence]

    # look for ngrams, backing off to smaller n
    for n in range(MAX_LEN, 0, -1):
        for start in range(numTokens-n+1):
            if start not in remainingStartIndices:
                continue
            end = start + n
            ngramSpan = slice(start, end)
            ngramLemmas = ' '.join(lemmas[start:end])
            if ngramLemmas in WHITELIST:
                # found a good ngram, add it to results, and remove it from startIndices so we don't overlap later
                candidate_target = sentence[ngramSpan]
                if shouldIncludeToken(candidate_target, sentence):
                    yield candidate_target
                    remainingStartIndices -= set(range(ngramSpan.start, ngramSpan.stop))

def shouldIncludeToken(candidate_target_tokens, sentence):
    """
    Determines whether an ngram should be kept as a target or discarded based on hand-built rules.
    """
    # always include ngrams, n > 1
    if len(candidate_target_tokens)>1: return True

    token = candidate_target_tokens[0]
    idx = token.id-1    # token ID is 1-based
    assert token is sentence[idx]
    # look up the word in pData
    form = token.form.lower()
    pos = token.postag
    lemma = token.lemma
    # look up the preceding and following words in pData, if they exist
    prevToken = sentence[idx-1] if idx-1>=0 else None
    nextToken = sentence[idx+1] if idx+1<len(sentence) else None

    if form in FORBIDDEN_WORDS: return False
    if form in LOC_PREPS: return False
    if form in DIR_PREPS: return False
    if form in TEMPORAL_PREPS: return False
    if pos[:2] in FORBIDDEN_POS_PREFIXES: return False
    # skip "of course" and "in particular"
    if prevToken and (prevToken.form.lower(), form) in [('of','course'), ('in','particular')]: return False

    if form=="of":
        if prevToken.lemma in PRECEDING_WORDS_OF: return True
        if nextToken.form.lower() in FOLLOWING_WORDS_OF: return True
        if prevToken.postag[:2] in ("JJ", "CD"): return True
        if nextToken.postag[:2]=="CD": return True
        if nextToken.postag[:2]=="DT":
            if idx+2<len(sentence):
                nextNextToken = sentence[idx+2]
                if nextNextToken.postag[:2]=="CD": return True
        return nextToken.ne.startswith("GPE") or nextToken.ne.startswith("LOCATION") or nextToken.ne.startswith("CARDINAL")

    if form=="will": return pos!="MD"
    if lemma=="have": return any(1 for t in sentence if t.head==token.id and t.deprel=='OBJ') # 'have' is a target iff it has an object
    return lemma!="be"

def main(fileP, output_format='legacy' or 'json'):
    with codecs.open(fileP, 'r', 'utf-8') as inF:
        for sentId, sentence in enumerate(read_conll(inF, lookup_lemmas=True)):
            assert None not in sentence

            targets = list(get_segmentation(sentence))

            if output_format.lower()=='json':
                sentJ = {"tokens": [tkn.form for tkn in sentence],
                         "frames": []}
                for target in targets:
                    spansJ = []
                    for tkn in sorted(target, key=lambda x: x.id):
                        # group contiguous tokens into spans
                        if spansJ and spansJ[-1]["end"]==tkn.id:
                            spansJ["end"]+=1
                            spansJ["text"]+= ' ' + tkn.form
                        else:
                            spansJ.append({"start": tkn.id-1, "end": tkn.id, "text": tkn.form})
                    sentJ["frames"].append({"target": {"spans": spansJ}})
                print(json.dumps(sentJ))
            else:
                print('\t'.join('_'.join(str(token.id-1) for token in target_tokens)+'#true' for target_tokens in targets) + '\t' + str(sentId))
            # output format: 9_10#true    1#true    11#true    13#true    15#true    0
            # underscore-spearated token IDs for each target; last column is sentence ID
            # (if no targets, will simply be a tab followed by sentence number)

if __name__=='__main__':
    args = sys.argv[1:]
    fmt = 'legacy'
    if args[0]=='-j':
        fmt = 'json'
        args.pop(0)
    fileP, _ = args+[None]
    main(fileP, output_format=fmt)


import numpy as np
from tianshou.data import Batch, ReplayBuffer, VectorReplayBuffer

from marl_comm.data import MAReplayBuffer

def test_replaybuffer():
    obs = np.arange(12)
    next_obs = np.arange(1, 13)
    buffer = MAReplayBuffer(100, ["agent_0", "agent_1"], ReplayBuffer)
    for i in range(100):
        batch = Batch({
            "obs": {
                "obs": [obs],
                "agent_id": [f"agent_{i % 2}"]
            },
            "next_obs": [next_obs],
            "act": [i],
            "rew": [i**2],
            "done": [False],
        })
        buffer.add(batch, [i % 2])
    print(buffer.sample(4))

    vbuffer = MAReplayBuffer(100, ["agent_0", "agent_1"], VectorReplayBuffer,
                             2)
    for i in range(100):
        batch = Batch({
            "obs": {
                "obs": [obs] * 2,
                "agent_id": [f"agent_{i % 2}"] * 2
            },
            "next_obs": [next_obs] * 2,
            "act": [i] * 2,
            "rew": [i**2] * 2,
            "done": [i % 2] * 2,
        })
        vbuffer.add(batch, [(i % 2) * 2, (i % 2) * 2 + 1])
    print(vbuffer.sample(2))

if __name__ == "__main__":
    test_replaybuffer()


import scrapy
from web_scrape.items import ActorItem

# response.xpath('//table[@class="infobox biography vcard"]/tr/td/span[contains(text(),"age")]').extract()

# response.xpath('//span[@class="noprint ForceAgeToShow"]/text()').extract_first()

# https://en.wikipedia.org/wiki/List_of_action_film_actors

# https://en.wikipedia.org/wiki/Category:American_martial_arts_films
class ActorSpider(scrapy.Spider):
    name = "actor"
    allowed_domains = ["wikipedia.org"]
    start_urls = [
        "https://en.wikipedia.org/wiki/List_of_action_film_actors"
    ]

    '''
    def start_requests(self):
        urls = [
            'https://en.wikipedia.org/wiki/Morgan_Freeman'
        ]
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)
    '''

    def parse(self, response):
        # divs = response.xpath('//div[@id="mw-content-text"]')
        for sel in response.xpath('//div[@class="div-col columns column-count column-count-3"]/ul/li'):
            url = sel.xpath('a/@href').extract_first()
            url = response.urljoin(url)
            #yield scrapy.Request(url, self.parse)
            yield scrapy.Request(url, callback=self.parse_content)

    def parse_content(self, response):
        if response.xpath('//table[@class="infobox biography vcard"]') != [] or response.xpath('//table[@class="infobox vcard plainlist"]') != []:
            if response.xpath('//table[@class="infobox biography vcard"]') != []:
                table = response.xpath('//table[@class="infobox biography vcard"]')
            else:
                table = response.xpath('//table[@class="infobox vcard plainlist"]')
            if table.xpath('//tr/td/span[@class="noprint ForceAgeToShow"]') != []:
                actor_item = ActorItem()
                if response.xpath('//table[@class="infobox biography vcard"]') != []:
                    actor_item['actorName'] = table.xpath('//tr/th/span/text()')[0].extract()
                else:
                    actor_item['actorName'] = table.xpath('//tr/th/text()')[0].extract()
                actor_item['actorAge'] = table.xpath('//tr/td/span[@class="noprint ForceAgeToShow"]/text()')[0].extract()
                yield actor_item


from rest_framework.decorators import api_view
from rest_framework.response import Response
from .models import *
from .serializer import *

@api_view(['POST'])
def post_order(request):
    name = request.POST.get('name')
    number = request.POST.get('number')
    Order.objects.create(name=name, number=number)
    order = Order.objects.last()
    data = OrderSerializer(order).data
    return Response(data)

@api_view(['GET'])
def get_Info(request):
    info = Info.objects.last()
    ser = InfoSerializer(info).data
    return Response(ser)

@api_view(['GET'])
def get_order(request):
    order = Order.objects.last()
    ser = OrderSerializer(order).data
    return Response(ser)

@api_view(['GET'])
def get_product(request):
    product = Product.objects.last()
    ser = ProductSerializer(product).data
    return Response(ser)

@api_view(['GET'])
def get_productinfo(request):
    product_info = Productinfo.objects.all()
    ser = ProductinfoSerializer(product_info, many=True).data
    return Response(ser)

@api_view(['GET'])
def get_fakt(request):
    facts = Fakt.objects.all()
    ser = FaktSerializer(facts, many=True).data
    return Response(ser)

@api_view(['GET'])
def get_aboutus(request):
    about = Aboutus.objects.last()
    ser = AboutusSerializer(about).data
    return Response(ser)

@api_view(['GET'])
def get_productfaq(request):
    productfaq = Productfaq.objects.last()
    ser = ProductfaqSerializer(productfaq).data
    return Response(ser)

@api_view(['GET'])
def get_header(request):
    header = Header.objects.last()
    ser = HeaderSerializer(header).data
    return Response(ser)

@api_view(['GET'])
def get_faq(request):
    faq = Faq.objects.all()
    ser = FaqSerializer(faq, many=True)
    return Response(ser)

@api_view(['GET'])
def get_advice(request):
    advice = Advice.objects.all()
    ser = AdviceSerializer(advice, many=True)
    return Response(ser)

@api_view(['GET'])
def get_abouttea(request):
    aboutt = Abouttea.objects.all()
    ser = AboutteaSerializer(aboutt, many=True)
    return Response(ser)

@api_view(['GET'])
def get_ijtimoiy(request):
    ijtimoiy = Ijtimoiy.objects.all()
    ser = IjtimoiySerializer(ijtimoiy, many=True)
    return Response(ser)

@api_view(["GET"])
def get_discount(request):
    discount = Discount.objects.last()
    ser = DiscountSerializer(discount).data
    return Response(ser)


barang = (
    int,
    input("Masukan barang apa saja yang ingin dibeli (pisahkan dengan koma) :"),
)
barang1 = int(input("Berapa harga barang H&M ? :"))
x = barang1 * 25 // 100
x1 = barang1 - x
print("Harga H&M         Rp.", barang1, "Diskon Rp.", x)
barang2 = int(input("Berapa harga barang Nike X Dior ? :"))
y = barang2 * 25 // 100
y1 = barang2 - y
print("Harga Nike X Dior     Rp.", barang2, "Diskon Rp.", y)
barang3 = int(input("Berapa harga Gucci Horsebit ? :"))
z = barang3 * 10 // 100
z1 = barang3 - z
print("Harga Gucci Horsebit    Rp.", barang3, "Diskon Rp.", z)
barang4 = int(input("Berapa harga barang besace tplyo ? :"))
print("Harga barang besace tokyo Rp. ", barang4, "Diskon 0")
barang5 = int(input("Berapa harga barang royal kludge ?:"))
print("Harga royal kludge    Rp.", barang5, "Diskon 24999")
total = x1 + y1 + z1 + barang4 + barang5
print("Total Diskon yang anda dapatkan adalah sebesarRp. :", total)
print("Total uang yang harus anda bayarkan adalah Rp.:", total)


from typing import Tuple

from oop.chess.Broad import Broad
from oop.chess.Player import Player

BLACK = 'black'
WHITE = 'white'

class ChessGame:
    broad = Broad(8)

    player = {
    }

    def __init__(self):
        pass

    def join_game(self, player1: Player, player2: Player):
        self.player[player1.name] = BLACK
        self.player[player2.name] = WHITE
        player1.game = self
        player2.game = self
        chesses_position = self.broad.build()
        player1.achive_chess(chesses_position[BLACK])
        player2.achive_chess(chesses_position[WHITE])

    def move(self, chess, position: Tuple, player: str):
        color_player = self.player[player]
        self.broad.move(color_player, chess, position)
        self.show_borad()
    def show_borad(self):
        self.broad.print()


import random
import numpy as np

solution_found = False
popN = 100  # n number of chromos per population
genesPerCh = 75
max_iterations = 1000
target = 1111.0
crossover_rate = 0.7
mutation_rate = 0.2

U = np.random.uniform

def generatePop():
    chromos, chromo = [], []
    for eachChromo in range(popN):
        chromo = []
        for bit in range(genesPerCh * 4):
            chromo.append(random.randint(0, 1))
        chromos.append(chromo)
    return chromos

def DecimalToBinary(num):
    if num > 1:
        DecimalToBinary(num // 2)
    print(num % 2, end='')

def reverse_sublist(lst, start, end):
    lst[start:end] = lst[start:end][::-1]
    return lst

def mutiraj(chromosomes, mutation_rate):
    for row in chromosomes:
        if U(0, 1) < mutation_rate:

            v1 = np.random.randint(len(row))
            v2 = np.random.randint(len(row))
            while v1 == v2:
                v2 = np.random.randint(len(row))
            # row[v1], row[v2] = row[v2], row[v1]
            reverse_sublist(chromosomes, v1, v2)
    return chromosomes

def dekoduj_bin(hromozom):
    l = []
    d = hromozom.encode()
    for i in range(len(d)):
        for mask in [0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01]:
            if mask & d[i] > 0:
                l.append(1)
            else:
                l.append(0)
    return l

def trosak(hromozom, resenje):
    n = 0
    for i in range(len(hromozom)):
        if hromozom[i] != resenje[i]:
            n += 1
    return n

#    Input, integer M, the number of variables.
#
#    Input, real X(M), the argument of the function.
#
#    Output, real F, the value of the function at X.
#

def beale( x):
    f1 = 1.5 - x[0] * (1.0 - x[1])
    f2 = 2.25 - x[0] * (1.0 - x[1] ** 2)
    f3 = 2.625 - x[0] * (1.0 - x[1] ** 3)

    f = f1 ** 2 + f2 ** 2 + f3 ** 2

    return f

def bejl(position):
    x, y = position
    return (1.5 - x + x*y)**2 + (2.25 - x + x*y**2)**2 + (2.625 - x + x*y**3)**2

def genetski():
    npop_vel = 10

    dekod = dekoduj_bin(popN)

    def _random_n(N, chromosomes):

        shuffle = np.arange(len(chromosomes))
        np.random.shuffle(shuffle)
        return shuffle[:N]

    def jednoTackastoCrossover(ind1, ind2):
        size = min(len(ind1), len(ind2))
        cxpoint = random.randint(1, size - 1)
        ind1[cxpoint:], ind2[cxpoint:] = ind2[cxpoint:], ind1[cxpoint:]

        return ind1, ind2

    for k in range(5):
        print("Pokretanje Algortima")
        best = None
        best_f = None
        t = 0

        populacija = generatePop()
        print(populacija)
        while best_f != 0 and t < max_iterations:
            n_pop = populacija[:]

            while len(n_pop) < popN + npop_vel:
                h1 = _random_n(2, populacija)
                h2 = _random_n(2, populacija)
                h3, h4 = jednoTackastoCrossover(h1, h2)

                mutiraj(h3, mutation_rate)
                mutiraj(h4, mutation_rate)
                n_pop.append(h3)
                n_pop.append(h4)
#Sortirati po trosku populaciju i skrati na popN velicinu, ispis i racunanje proseka
#

########################################################

genetski()


from typing import Any

import pygame

from models.game_models import Board, Path, Square
from ui.config import (
    dis_x,
    dis_y,
    step,
    red,
    green,
    yellow,
    white,
    background_color,
    gray,
    brown,
)

class UI:
    display: Any

    def __init__(self):
        pygame.init()
        self.display = pygame.display.set_mode((dis_x, dis_y))

    def draw(self, board: Board, actor_path: Path, failed_to_move=False):
        if failed_to_move:
            actor_color = brown
        else:
            actor_color = white
        for i, row in enumerate(board):
            for j, square in enumerate(row):
                if square == Square.START:
                    color = green
                elif square == Square.END:
                    color = red
                elif square == Square.COIN:
                    color = yellow
                else:
                    color = background_color
                pygame.draw.rect(
                    self.display, color=color, rect=[j * step, i * step, step, step]
                )

        for square in actor_path[:-1]:
            pygame.draw.rect(
                self.display,
                color=gray,
                rect=[step * square.x, step * square.y, step, step],
            )
        pygame.draw.rect(
            self.display,
            color=actor_color,
            rect=[step * actor_path[-1].x, step * actor_path[-1].y, step, step],
        )
        pygame.display.update()

    @staticmethod
    def check_if_terminated() -> bool:
        """Check and close UI if terminated"""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.display.quit()
                pygame.quit()
                return True


import pygame
from network import Network

width = 500
height = 500

win = pygame.display.set_mode((width, height))

pygame.display.set_caption("Client")

def redrawWindow(window, player, otherPlayers):
    window.fill((255, 255, 255))
    player.draw(window)
    for op in otherPlayers:
        op.draw(window)
    pygame.display.update()

def main():
    run = True
    n = Network()

    p = n.getPlayer()
    a = 0

    clock = pygame.time.Clock()
    while run:
        clock.tick(60)

        otherPlayers = n.send(p)

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

        p.move()
        redrawWindow(win, p, otherPlayers)

main()


import math
import numpy as np
import torch
import torch.nn as nn

from numpy.polynomial.hermite import hermval

from rbm import RBM

class Tomograph(nn.Module):
    """Class for RBM tomography.

    Parameters
    ----------
    vis_size : int
        Number of visible neurons (number of qubits).
    hid_size : int
        Number of hidden neurons.
    gibbs : bool
        If `True`, uses contrastive divergence with Gibbs sampling.
    n_samples : int
        Number of samples to use in one iteration.
    n_gibbs_steps : int
        Number of steps in each Gibbs chain.
    init_sigma : float
        Standard deviation for initialization of RBM parameters.

    Attributes
    ----------
    amplitude_rbm : RBM
        RBM for computing amplitude.
    phase_rbm : RBM
        RBM for computing phase.

    """

    def __init__(self, vis_size, hid_size, gibbs=True, n_samples=5, n_gibbs_steps=1,
                 init_sigma=1, dtype=torch.float64, eps=1e-10):
        super().__init__()

        self.vis_size = vis_size
        self.hid_size = hid_size

        self.gibbs = gibbs
        self.n_gibbs_steps = n_gibbs_steps
        self.n_samples = n_samples

        self.amplitude_rbm = RBM(vis_size, hid_size, init_sigma=init_sigma, dtype=dtype)
        self.phase_rbm = RBM(vis_size, hid_size, init_sigma=init_sigma, dtype=dtype)

        self.temperature = 1

        self.dtype = dtype
        self._eps = eps

    def forward(self, unique_vis):
        amplitude_prob = self.amplitude_rbm.prob(unique_vis)
        amplitude = (torch.sqrt(amplitude_prob / amplitude_prob.sum()))

        phase_prob = self.phase_rbm.prob(unique_vis)
        phase = torch.log(phase_prob + self._eps) / 2

        predicted_state = amplitude, phase

        return predicted_state

    def predict(self, n_samples=10):
        """Returns tuple of amplitudes and phases of the reconstructed state in sampled Fock basis."""
        if not self.gibbs:
            return self.predict_true()

        device = next(self.parameters()).device
        fock_indices = torch.arange(2 ** self.vis_size)
        vis = idx2vis(fock_indices[torch.randint(len(fock_indices), (n_samples,))], self.vis_size,
                      dtype=self.dtype, device=device)
        unique_vis = torch.unique(vis, dim=0)

        return self.forward(unique_vis)

    def predict_true(self):
        """Returns tuple of amplitudes and phases of the reconstructed state in Fock basis."""
        device = next(self.parameters()).device
        fock_indices = torch.arange(2 ** self.vis_size)
        vis = idx2vis(fock_indices, self.vis_size, dtype=self.dtype, device=device)

        return self.forward(vis)

    def llh_loss(self, data_states, predicted_state):
        """Computes negative log-likelihood of reconstruction."""
        data_amplitudes, data_phases = data_states  # [batch_size, n_indices]
        predicted_amplitude, predicted_phase = predicted_state  # [n_indices,]

        amplitudes = data_amplitudes * predicted_amplitude
        phases = data_phases + predicted_phase

        likelihood = torch.sum(amplitudes * torch.cos(phases), dim=1) ** 2 \
                   + torch.sum(amplitudes * torch.sin(phases), dim=1) ** 2

        llh = torch.mean(torch.log(likelihood + self._eps))

        return -llh

    def fit(self, x, theta, n_epochs=1000, lr=1e-1, callbacks=None):
        """Fits RBM to the data."""
        device = next(self.parameters()).device

        fock_indices = torch.arange(2 ** self.vis_size)

        if self.gibbs:
            vis = idx2vis(fock_indices[torch.randint(len(fock_indices), (self.n_samples,))], self.vis_size,
                          dtype=self.dtype, device=device)
        else:
            encoded_data = encode_data(fock_indices, x, theta, self.dtype, device)  # [batch_size, n_indices]
            unique_vis = idx2vis(fock_indices, self.vis_size, dtype=self.dtype, device=device)

        opt = torch.optim.Adam(self.parameters(), lr=lr, betas=(0.9, 0.99))
        scheduler = torch.optim.lr_scheduler.MultiStepLR(opt, [1000, 3000])

        for e in range(n_epochs):

            epoch_log = {
                'epoch': e,
                'n_epochs': n_epochs,
            }

            if self.gibbs:
                vis = idx2vis(fock_indices[torch.randint(len(fock_indices), (self.n_samples,))], self.vis_size,
                              dtype=self.dtype, device=device)
                vis = self.amplitude_rbm.sample(vis, n_gibbs_steps=self.n_gibbs_steps, temperature=self.temperature)
                unique_vis = torch.unique(vis, dim=0)
                sampled_indices = vis2idx(unique_vis)
                epoch_log['sampled_indices'] = sampled_indices
                encoded_data = encode_data(sampled_indices, x, theta, self.dtype, device)

            predicted_state = self.forward(unique_vis)
            loss = self.llh_loss(encoded_data, predicted_state)
            epoch_log['loss'] = loss.cpu().detach().numpy()

            opt.zero_grad()
            loss.backward()
            opt.step()
            scheduler.step()

            if callbacks is not None:
                for callback in callbacks:
                    callback(self, epoch_log)

def encode_data(fock_indices, x, theta, dtype=torch.float32, device=torch.device('cpu')):
    """Computes <n|X, theta> using Hermitian polynomes.

    Parameters
    ----------
    fock_indices : torch.Tensor
        Indices of fock vectors to use in decomposition.
    x : torch.Tensor
        X quadratures of the data.
    theta : torch.Tensor
        Theta quadratures of the data.

    Returns
    -------
    Tuple of amplitudes and phases.

    """

    amplitude = count_hermvals(fock_indices, x, dtype=dtype, device=device) \
              * torch.exp(-x ** 2 / 2).unsqueeze(1) \
              / 2 ** (fock_indices / 2.).unsqueeze(0) \
              / torch.sqrt(factorial(fock_indices)).unsqueeze(0) \
              / math.pi ** 0.25

    phase = theta.unsqueeze(1) * fock_indices.unsqueeze(0)
    return amplitude, phase

def idx2vis(idx, dim, dtype=torch.float64, device=torch.device('cpu')):
    """Encodes Fock vectors."""
    vis = torch.zeros(idx.shape[0], dim, dtype=dtype, device=device)
    for i in range(idx.shape[0]):
        id_bin = torch.as_tensor([int(c) for c in bin(idx[i])[2:]], dtype=dtype, device=device)
        vis[i, -id_bin.shape[0]:] = id_bin
    return vis

def vis2idx(vis):
    """Turns encodings of Fock vectors to corresponding indices."""
    return torch.sum(2 ** reversed(torch.arange(0, vis.shape[1])) * vis, dim=1, dtype=torch.long)

def count_hermvals(n, x, dtype=torch.float64, device=torch.device('cpu')):
    """Returns tensor of shape [len(x), len(n)] with hermitian polynomes values H_n(x)."""
    hermvals = torch.zeros(len(x), len(n), dtype=dtype, device=device)
    for i, nval in enumerate(n):
        coef = torch.zeros(n[-1] + 1, dtype=dtype, device=device)
        coef[nval] = 1
        hermvals[:, i] = hermval(x, coef)
    return hermvals

def factorial(x):
    """Returns element-wise factorial."""
    return torch.exp(torch.lgamma(x + 1.))


# Iterate through a string and print out individual characters
fruit = "banana"
for letter in fruit:
    print(letter)

# Count the number of letter 'a'
count = 0
for letter in fruit:
    if letter == "a":
        count += 1
print(count)

# Check if 'a' is in the word
if "a" in fruit:
    print("Found it!")

# Find and extract school host from email header
header = "From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008"
at_pos = header.find("@")
print(at_pos)
space_pos = header.find(" ", at_pos)
print(space_pos)
domain = header[at_pos + 1 : space_pos]
print(domain)


# csv is used for reading and writing csv files and their rows
import csv
# Imported method is a natural language processor for English language
from spacy.lang.en import English
# Imported Set is a set of stop words which decrease performance of Sentiment Analysis
from spacy.lang.en.stop_words import STOP_WORDS

print("Step 1 of 3")

# Initialize an instance for English Natural Language Processor
nlp = English()

# Initialize a List of redundant symbols
filter_symbols = ['.', '..', '...', ',', '!', '?', '"', '\'', '@', '#', '$', '%', '^', '*', '(', ')', '-', '_', '=',
                  '+', '/', '\\', ';', ':', '[', ']', '{', '}', '|', ' ']

# Open given raw file in read-only mode
raw_input = open("Raw_DataFile.csv", 'r')

# Create and open a datafile for output writer of cleaned up dataset
clean_output = open("Clean_Datafile.csv", 'w', newline="")

print("Importing data in \"Raw_DataFile.csv\"...")

# Read raw datafile as csv and load it to raw_dataset object
raw_dataset = csv.reader(raw_input)

# Initialize a csv writer for clean dataset which gives the ability of
# writing desired rows into clean datafile
clean_dataset = csv.writer(clean_output)

print("Processing comments for stop words and characters...")
print("This process may take a while. Please wait...")

# Iteration through rows of raw dataset in order to filter redundant columns of data,
# clean up comment column, recombine new columns and write generated row into clean dataset
for row in raw_dataset:

    # first column of data is assigned to state column
    state = str(row[0])

    # second column of data is assigned to comment object
    # which is first tokenized by imported NLP
    comment = nlp(str(row[5]))

    # Initializing a list of tokens which contains tokens from comment column
    token_list = []
    for token in comment:
        token_list.append(token.text)

    # Initialize an empty filtered comment list
    filtered_comment = []

    # Iteration through tokens in token list, in order to assign the selected token
    # to lexeme if token is in vocab list in NLP, checking if lexeme is not a stop word or reduntant symbol
    # and appending it to the list of filtered comments
    for selected_token in token_list:
        lexeme = nlp.vocab[selected_token]
        if lexeme.is_stop == False and lexeme not in filter_symbols:
            filtered_comment.append(selected_token)

    clean_dataset.writerow([state, filtered_comment])

print("\"Clean_Datafile\" created successfully!")


import scenario.test

# Steps:
from steps.common import ExecScenario
from steps.common import ParseScenarioLog, CheckScenarioLogExpectations

class KnownIssues010(scenario.test.TestCase):

    def __init__(self):  # type: (...) -> None
        scenario.test.TestCase.__init__(
            self,
            title="Known issues",
            objective="Check known issues generate warnings by default, and that the status of the test is WARNINGS.",
            features=[scenario.test.features.KNOWN_ISSUES],
        )

        self.addstep(ExecScenario(scenario.test.paths.KNOWN_ISSUES_SCENARIO))
        self.addstep(ParseScenarioLog(ExecScenario.getinstance()))
        self.addstep(CheckScenarioLogExpectations(ParseScenarioLog.getinstance(), scenario.test.data.scenarioexpectations(
            scenario.test.paths.KNOWN_ISSUES_SCENARIO,
            # Check all steps are executed.
            steps=True, stats=True,
            # Error details makes the step check the warnings, i.e. known issues.
            error_details=True,
        )))


# poDataInsertion.py
# Created by Nate O, modified by Mickey G for Project flextape use
# To Import PO data to mongodb, within the flextape pipeline

# On 11/14/2018, the script was modified to accomodate the addition
#   of 'PO Approval Date' field.

# On 09/26/2019, the script was modified to accomodate the addition
#   of 'Last_Updated' field.

# On 09/27/2019, the script was modified to acoomodate the addition
#   of 'Vendor_ID' field.

# On 10/09/2019, the script was modified to use the new query name of
#   `PLIT_PO_6`, as well as steps to replace potential N/As in the REQ ID/Line
#   fields since the join is now left outer join.

# On 10/25/2019, the script was modified to use the new data path of
#   `po-6` instead of `po6`.

import pandas as pd
import pymongo
from pymongo import MongoClient
import sys
import time
import os

print("poDataInsertion.py taking over...")

if not sys.argv[1] and not sys.argv[2]:
    raise ValueError('Arguments Needed: Date String (mmddyyyy-hhmmss), Write Location: One of (dev, prod, both)')
date = sys.argv[1]
writelocation = ['dev', 'prod'] if sys.argv[2] == 'both' else [sys.argv[2]]

serverlocation = os.environ['RUBIXLOCATION']

if serverlocation == 'local':
    filepathprefix = "/home/rubix/Desktop/Project-Ducttape/data/"
elif serverlocation == 'ohio':
    filepathprefix = "/home/ubuntu/Projects/flextape/"
else:
    raise EnvironmentError('Environment Variable "RUBIXLOCATION" seems not to be set.')

filepath = filepathprefix + "po-6/" + date + "/PLIT_PO_6-" + date + ".xlsx"

print("--- Reading " + filepath + " ---")

insertionItems = pd.read_excel(filepath, skiprows=1)

client = MongoClient()
insertionItems.columns = [c.replace(' ', '_') for c in insertionItems.columns]
insertionItems.columns = [c.replace('/', '_') for c in insertionItems.columns]
insertionItems.columns = [c.replace('.', '') for c in insertionItems.columns]

# lines.Requisition_Data NA handling
na_table = {"Req_ID": "", "REQ_Line": 0}

insertionItems.fillna(value=na_table)

print("--- Pushing Data to mongo ---")

print(insertionItems.columns)
for location in writelocation:
    dbname = 'rubix-' + serverlocation + '-' + location
    print('Using database ' + dbname)
    db = client[dbname]
    uniquePOIDs = dict((pono, False) for pono in insertionItems['PO_No'].unique().tolist())
    for row in insertionItems.itertuples():
        if not uniquePOIDs[row.PO_No]:
            if pd.isna(row.PO_AprvDate):
                aprvDate = ""
            else:
                aprvDate = row.PO_AprvDate
            db.PO_DATA.update({'PO_No': row.PO_No}, {
                '$set': {
                    'PO_No': row.PO_No,
                    'Business_Unit': row.Business_Unit,
                    'Buyer': row.Buyer,
                    "Status": row.Status,
                    'PO_Date': row.PO_Date,
                    "Origin": row.Origin,
                    "Approved_By": row.Approved_By,
                    "Vendor": row.Vendor_Name,
                    "PO_Approval_Date": aprvDate,
                    "Last_Updated": row.Last_Dttm,
                    "Vendor_ID": row.Vendor,
                    "lines": []
                }
            }, upsert=True)
            uniquePOIDs[row.PO_No] = True

    for row in insertionItems.itertuples():
        db.PO_DATA.update({"PO_No": row.PO_No}, {
            '$push': {
                "lines": {
                    "Line_No": row.PO_Line,
                    "Mfg_ID": row.Mfg_ID,
                    "Mfg_Item_ID": row.Mfg_Itm_ID,
                    "Line_Total": row.Sum_Amount,
                    "Level_1": row.Level_1,
                    "Level_2": row.Level_2,
                    "Descr": row.Descr,
                    "Line_Descr": row.More_Info,
                    "FairMarkIt_Link": row.QuoteLink,
                    "Requisition_Data": {
                        "Req_ID": row.Req_ID,
                        "Req_Line": row.REQ_Line
                    },
                    "Quantity": row.PO_Qty
                }
            }})
        # db.REQ_DATA.find({"lines": {$elemMatch: {"Item": "02545903"}}})

    db.LAST_UPDATED.update({'dbname': "PO_DATA"}, {'$set': {'last_updated_time': time.time()}})

    print("--- PO Update Done ---")


import datetime
import json
from alipay import AliPay
from django.db import transaction
from django.http import JsonResponse
from django.views import View
from django.conf import settings
from goods.models import SKU
from order.models import OrderInfo, OrderGoods
from user.models import Address
from carts.views import CartsView
from goods.views import get_sku_name_and_values

# 公共调用类
from utils.login_dec import login_check

class OrderBaseView(View):
    # 获取存储在购物车中的商品数据
    def get_carts_dict(self, user_id):
        carts_dict = CartsView().get_carts_datas(user_id)
        print("购物车", len(carts_dict))
        # 将选中的商品 转化成 {id:count}
        return {k: v[0] for k, v in carts_dict.items() if v[1] == 1}

    def get_direct_dict(self, sku_id, count):
        # 将直接购买传来的商品 转化成 {id:count}
        return {int(sku_id): int(count)}

    # 获取立即购买的商品列表
    def get_direct_order_list(self, skus, count=None):
        sku_list = []
        for sku in skus:
            # 获取商品属性值，商品属性名
            sku_sale_attr_val, sku_sale_attr_name = get_sku_name_and_values(sku.id)
            sku_list.append({
                'id': sku.id,
                'default_image_url': str(sku.default_image_url),
                'name': sku.name,
                'price': sku.price,
                'count': count,
                'total_amount': sku.price * int(count),
                "sku_sale_attr_name": sku_sale_attr_name,
                "sku_sale_attr_val": sku_sale_attr_val,
            })
        return sku_list

    # 从redis中删除所有已经加入了订单的商品
    def delete_redis_order(self, sku_ids, user_id):
        carts = CartsView()
        for sku_id in sku_ids:
            carts.del_carts_data(user_id, sku_id)

    # 获取用户地址
    def get_address(self, user_id):
        """
        :param request:
        :return: address_list(地址列表)
        """
        print("user:", user_id)
        addresses = Address.objects.filter(is_active=True, user_profile_id=user_id)
        print(addresses)
        addresses_default = []
        addresses_no_default = []
        for address in addresses:
            detail_address = address.province + address.city + address.county + address.detail
            address_dic = {
                "id": address.id,
                "name": address.receiver,
                "mobile": address.receiver_phone,
                "title": address.tag,
                "address": detail_address
            }
            if address.is_default:
                addresses_default.append(address_dic)
            else:
                addresses_no_default.append(address_dic)

        return addresses_default + addresses_no_default

    # 获取在购物车购买的商品列表
    def get_carts_order_list(self, user_id):
        """
        :param skus:
        :param cart_dict:
        :return: sku_list(sku列表)
        """
        sku_list = CartsView().get_carts_list(user_id)
        print(sku_list)
        return [s for s in sku_list if s['selected'] == 1]

    def get_order_string(self, order_id, total_amount, sku_goods):
        """
        :param order_id:
        :param total_amount:
        :return: order_string
        """
        trade_name = []
        for sku_good in sku_goods:
            trade_name.append(sku_good.sku.name + "(" + sku_good.sku.caption + ")")
        alipay = AliPay(
            appid=settings.ALIPAY_APPID,
            app_notify_url=None,  # 默认回调url-　阿里与商户后台交互
            # 使用的文件读取方式,载入支付秘钥
            app_private_key_string=open(settings.ALIPAY_KEYS_DIR + 'app_private_key.pem').read(),
            # 支付宝的公钥，验证支付宝回传消息使用，不是你自己的公钥,
            # 使用文件读取的方式,载入支付报公钥
            alipay_public_key_string=open(settings.ALIPAY_KEYS_DIR + 'alipay_public_key.pem').read(),
            sign_type="RSA2",  # RSA 或者 RSA2
            debug=True  # 默认False
        )
        # 电脑网站支付，需要跳转到https://openapi.alipaydev.com/gateway.do? + order_string
        # 测试方式此为支付宝沙箱环境

        order_string = alipay.api_alipay_trade_page_pay(
            out_trade_no=order_id,
            total_amount=int(total_amount),
            subject=order_id,
            # 回转url,　支付宝与买家业务处理完毕(支付成功)将玩家重定向到此路由,带着交易的参数返回
            return_url=settings.ALIPAY_RETURN_URL,
            notify_url=settings.ALIPAY_RETURN_URL  # 可选, 不填则使用默认notify url
        )
        return order_string

    def get_make_pay_order(self, order, carts_count):
        # 4.生成支付宝支付链接
        sku_goods = OrderGoods.objects.filter(order_info=order)
        order_string = self.get_order_string(order.order_id, order.total_amount + 10, sku_goods)
        # 构建让用户跳转的支付链接
        pay_url = "https://openapi.alipaydev.com/gateway.do?" + order_string
        # TODO:传给前端的数据
        print(pay_url)
        # 5.传入前端数据
        data = {
            'saller': '达达商城',
            'total_amount': order.total_amount + order.freight,
            'order_freight': order.freight,
            'order_id': order.order_id,
            'pay_url': pay_url,
            'carts_count': carts_count
        }
        return data

class OrderInfoView(OrderBaseView):
    @login_check
    def get(self, request, username):
        """
        查询用户订单：
            0,"所有订单"
            1,"待付款"
            2,"待发货"
            3,"待收货"
            4,"订单完成"
            5,"去付款"
        """
        user = request.login_user
        status = request.GET.get("type")
        if int(status) == 0:
            order_list = OrderInfo.objects.filter(user_profile=user)

        # 去付款
        elif int(status) == 5:
            order_id = request.GET.get("order_id")
            try:
                order = OrderInfo.objects.get(order_id=order_id)
            except Exception as e:
                return JsonResponse({'code': 50102, 'errmsg': '商品问题'})

            # 查看redis存储长度
            carts_dict = self.get_carts_dict(user.id)
            carts_count = len(carts_dict)
            data = self.get_make_pay_order(order, carts_count)
            return JsonResponse({"code": 200, "data": data})

        else:
            order_list = OrderInfo.objects.filter(user_profile=user, status=status)
        orders_lists = []
        for order in order_list:
            good_skus = OrderGoods.objects.filter(order_info=order)
            sku_list = []
            # 1.获取订单中商品的信息，即sku数据
            print("good_skus", good_skus)
            for good_sku in good_skus:
                sku = good_sku.sku
                sku_sale_attr_vals, sku_sale_attr_names = get_sku_name_and_values(sku.id)
                sku_list.append({
                    'id': sku.id,
                    'default_image_url': str(sku.default_image_url),
                    'name': sku.name,
                    'price': sku.price,
                    'count': good_sku.count,
                    'total_amount': sku.price * good_sku.count,
                    "sku_sale_attr_names": sku_sale_attr_names,
                    "sku_sale_attr_vals": sku_sale_attr_vals,
                })
                print("sku_list", sku_list)
            # 2.获取订单信息
            orders_lists.append({
                "order_id": order.order_id,
                "order_total_count": order.total_count,
                "order_total_amount": order.total_amount,
                "order_freight": order.freight,
                # TODO 地址查询
                "address": {
                    "title": order.tag,
                    "address": order.address,
                    "mobile": order.receiver_mobile,
                    "receiver": order.receiver
                },
                "status": order.status,
                "order_sku": sku_list,
                "order_time": str(order.created_time)[0:19]
            })
        data = {
            'orders_list': orders_lists,
        }
        print(data)
        return JsonResponse({"code": 200, "data": data, 'base_url': settings.PIC_URL})

    @login_check
    def post(self, request, username):
        # 生成用户订单
        user = request.login_user
        obj_js = json.loads(request.body)
        address_id = obj_js.get("address_id")
        buy_count = obj_js.get("buy_count")
        sku_id = obj_js.get("sku_id")

        # 通过从购物车请求过来 请求不带sku_id
        if not sku_id:
            is_carts = True
            goods_dict = self.get_carts_dict(user.id)
            if not goods_dict:
                return JsonResponse({'code': 50201, 'errmsg': 'cart no goods'})
        else:
            is_carts = False
            goods_dict = self.get_direct_dict(sku_id, buy_count)

        # 检查购物车时，购物车中是否还有数据

        try:
            address = Address.objects.get(id=address_id, is_active=True)
        except:
            return JsonResponse({'code': 50102, 'errmsg': '收货地址无效'})
        now = datetime.datetime.now()

        # 开启事务
        with transaction.atomic():
            # 禁止自动提交
            sid = transaction.savepoint()
            # 创建订单基本对象
            order_id = '%s%02d' % (now.strftime('%Y%m%d%H%M%S'), user.id)
            total_count = 0
            total_amount = 0
            # 地址需要修改
            detail_address = address.province + address.city + address.county + address.detail
            order = OrderInfo.objects.create(
                order_id=order_id,
                user_profile=user,
                address=detail_address,
                receiver=address.receiver,
                receiver_mobile=address.receiver_phone,
                tag=address.tag,
                total_count=0,
                total_amount=0,
                freight=1,
                pay_method=1,
                status=1
            )

            # 购物车生产订单
            skus = SKU.objects.filter(id__in=goods_dict.keys())

            for sku in skus:
                goods_count = int(goods_dict[sku.id])
                # 创建订单
                # 判断库存，不足则提示,如果足够则继续执行
                if sku.stock < goods_count:
                    # 回滚事务
                    transaction.savepoint_rollback(sid)
                    return JsonResponse({'code': 50103, 'errmsg': '商品[%d]库存不足' % sku.id})

                # 使用乐观锁修改商品数量
                version_old = sku.version
                result = SKU.objects.filter(pk=sku.id, version=version_old).update(stock=sku.stock - goods_count,
                                                                                sales=sku.sales + goods_count,
                                                                                version=version_old + 1)
                # result表示sql语句修改数据的个数
                if result == 0:
                    # 库存发生变化，未成功购买
                    transaction.savepoint_rollback(sid)
                    return JsonResponse({'code': 50104, 'errmsg': '操作太快了,请稍后重试'})

                # 创建订单商品对象
                OrderGoods.objects.create(
                    order_info=order,
                    sku_id=sku.id,
                    count=goods_count,
                    price=sku.price
                )
                # 　计算总数量,总金额
                total_count += goods_count
                total_amount += sku.price * goods_count

            # 5.修改订单对象的总金额、总数量
            order.total_count = total_count
            order.total_amount = total_amount
            # 运费默认为10
            order.freight = 10
            order.save()

            # 提交
            transaction.savepoint_commit(sid)

        if is_carts:
            # 3.从购物车中删除所有已经加入了订单的商品
            self.delete_redis_order(goods_dict.keys(), user.id)

        # 查看redis存储长度
        carts_dict = self.get_carts_dict(user.id)
        carts_count = len(carts_dict)

        data = self.get_make_pay_order(order, carts_count)

        return JsonResponse({"code": 200, "data": data})

    @login_check
    def put(self, request, username):
        # 修改用户订单状态(确认收货)
        result = json.loads(request.body)
        if not result:
            return JsonResponse({"code": 50105, "error": "订单不存在！"})
        order_id = result.get("order_id")
        order = OrderInfo.objects.filter(order_id=order_id)[0]
        order.status = 4
        order.save()
        return JsonResponse({"code": 200, 'base_url': settings.PIC_URL})

class AdvanceOrderView(OrderBaseView):
    @login_check
    def get(self, request, username):
        # 预订单（订单确认）
        # 1.获取收货地址列表
        user = request.login_user
        addresses_list = self.get_address(user.id)
        # 2.组织商品数据
        settlement = int(request.GET.get('settlement_type'))
        # 购物车结算
        data = {}
        if settlement == 0:
            # 3.获取购买购物车商品列表
            sku_list = self.get_carts_order_list(user.id)
            print("购物车：", sku_list)
        # 直接购买结算
        else:
            sku_id = request.GET.get('sku_id')
            count = request.GET.get('buy_num')
            skus = SKU.objects.filter(id=sku_id)
            # 获取商品列表
            sku_list = self.get_direct_order_list(skus, count)
            print("立即购买：", sku_list)
            data['buy_count'] = count
            data['sku_id'] = sku_id
        # 3.组织数据
        data['addresses'] = addresses_list
        data['sku_list'] = sku_list
        return JsonResponse({'code': 200, 'data': data, 'base_url': settings.PIC_URL})


import json
import boto3

dynamodb = boto3.client('dynamodb')

def lambda_handler(event, context):
    response = dynamodb.scan(
        TableName='travel'
        )
    if response['ResponseMetadata']['HTTPStatusCode'] != 200 and response['Items'] > 0:
        return { 'statusCode':response['ResponseMetadata']['HTTPStatusCode'] }

    items = response['Items']
    travels = []
    travel = {}
    for item in items:
        travel['travelId'] = item['travelId']['S']
        travel['product'] = item['product']['S']
        travel['status'] = item['status']['BOOL']
        travel['timestamp'] = item['timestamp']['S']
        travel['displayName'] = item['displayName']['S']
        if 'endtimestamp' in item:
            travel['endtimestamp'] = item['endtimestamp']['S']
        if 'description' in item:
            travel['description'] = item['description']['S']
        travel['unity'] = item['unity']['S']
        travels.append(travel)
        travel = {}
    return travels


# pip install requests
# pip install bs4
# pip install html5lib

import requests
from bs4 import BeautifulSoup
url = "https://www.codewithharry.com"

# Get the HTML
content = requests.get(url)
# htmlContent = r.content
htmlContent = content.content
# print(htmlContent)

# Parse the HTML
soup = BeautifulSoup(htmlContent, 'html.parser')
# print(soup.prettify)

# HTML Tree traversal
# objects ->
# print(type(title)) # 1. Tag
# print(type(title.string)) # 2. NavigableString
# print(type(soup)) # 3. BeautifulSoup
# 4. Comment

markup = "

"
soup2 = BeautifulSoup(markup)
print(soup2.p)
print(soup2.p.string)
print(type(soup2.p.string))
# exit()
# Get the title of HTML page
title = soup.title

# Get all The Paragraphs of Html Page
paras =soup.find_all('p')
# print(paras)

# Get first element in the HTML page
print(soup.find('p'))

# Get classesof any element in the HTML page
print(soup.find('p')['class'])

# find all the elements with class lead
print(soup.find_all("p", class_="lead"))

# Get the text from the tags/soup
print(soup.find('p').get_text())
print(soup.get_text())

# Get all The anchor tags of Html Page
anchors =soup.find_all('a')
all_links = set()

# print(anchors)
# Get all thr links on the page
for link in anchors:
    if(link.get('href') != '#'):
        linkText = "https://www.codewithharry.com" +link.get('href')
        all_links.add(link)
        print(linkText)

navbarSupportedContent = soup.find(id='navbarSupportedContent')
# for elem in navbarSupportedContent.children:
#     print(elem)
# for item in navbarSupportedContent.stripped_strings:
#     print(item)

# print(navbarSupportedContent.parent)
# for item in navbarSupportedContent.parents:
#     print(item.name)

# print(navbarSupportedContent.next_sibling.next_sibling)
# print(navbarSupportedContent.previous_sibling.previous_sibling)

# elem = soup.select('.modal-footer')
# print(elem)
elem = soup.select('#loginModal')[0]
print(elem)


import re
import json
import ast
import SharedFunctions

""" Module: Update
    Description: holds all information relating to updates, including
                 processing input
    Classes: Update
"""

class Update:
    """ Update Class holds all information relating to updates, including
    processing input.

    Variables:
    matches -- the passed in raw input from handleInput
    updatedRow -- the new row we update its corresponding key with
    replacedRow -- the row that was replaced (had its information updated)
    typesSet -- the current set of all currently held column types

    Functions:
    handleUpdates: parses matches to input used to update an existing row
                   (if the key and its value don't exist, we print an error
                   message instead).
    """

    def __init__(self, matches, typesSet):
        self.matches = matches
        self.updatedRow = {}
        self.replacedRow = {}
        self.typesSet = typesSet

    def handleUpdates(self, dynamicDB):
        """ handleUpdates handles anything in the form UPDATE [key] WTIH VALUES
        (col=tag, col2=tag2, etc). Any other format causes the file to abandon
        the modification. Through each (col=tag, col2=tag2, etc), we change
        tag type's value to be the new value the user passed in. This set of
        changed values is then set to be the data with the value associated
        with the key. Other irregularities causing abandonment of modification
        include the type not being in the set of accepted types, and having
        tags/values without the other.

        Keyword Argument:
        dynamicDB -- the key value store maintained in the program

        Return Values:
        self.updatedRow, self.replacedRow -- the tuple of a new row to update
                                             with, and the row to be replaced
        """

        error = False
        usage = "Usage:\n UPDATE [key] WITH  VALUES (col=tag, col2=tag2...) \n"
        parser = re.compile(r'[a-z-0-9*!@#$%^&~_.+{}:\'"]+', re.IGNORECASE)
        matches = parser.findall(" ".join(self.matches))
        if matches[2].lower() == 'with' and matches[3].lower() == 'values' \
                and len(matches) >= 5:
            # check if key already exists in the key value store
            key = matches[1].lower()
            if key in dynamicDB and dynamicDB[key]['isFree'] == 'false':
                allVals = dynamicDB[key]['data']
                replaced = json.dumps(dynamicDB[key]['data'])
                matches = SharedFunctions.spaceMatches(4, self.matches)
                # Check that all types/columns are matched correctly
                if len(matches) % 2 == 1:
                    print("Column types must be associated with column values!"
                          " \n ", usage)
                    error = True
                # Check that either the column we want to modify
                # is in the row or is in the set. Otherwise,
                # this is an invalid input.
                if not error:
                    for colIndex in range(0, len(matches), 2):
                        if matches[colIndex] not in self.typesSet:
                            self.typesSet.add(matches[colIndex])
                        allVals[matches[colIndex]] = matches[colIndex + 1]
                    # Now set the data of the row to be the modified values.
                    dynamicDB[key]['data'] = allVals
                    self.updatedRow = {key: {'isFree': 'false', 'data': allVals}}
                    self.replacedRow = {key: {'isFree': 'false', 'data':
                                         ast.literal_eval(replaced)}}
            else:
                print("The key is not in the store!")
        else:
            print("UPDATE format is incorrect. \n", usage)
        return self.updatedRow, self.replacedRow


# -*- coding: utf-8 -*-
"""
Created on Mon Jun  6 23:03:49 2016

@author: yanxin
"""
from sqlalchemy import create_engine
from selenium import webdriver
import time
import datetime
from bs4 import BeautifulSoup
import pandas as pd
import re

email, passwd,db_name,db_user,db_pswd = open('login.secret').read().strip().split(',')

cities = ["Boston, MA","Chicago, IL","Seattle, WA","DALLAS, TX"]

browser = webdriver.Chrome('/home/yanxin/Desktop/chromedriver')
browser.get('https://app.strava.com/login')
browser.find_element_by_name('email').send_keys(email)
browser.find_element_by_name('password').clear()
browser.find_element_by_name('password').send_keys(passwd)
browser.find_element_by_id('login-button').submit()

A=[]
B=[]
C=[]
D=[]
E=[]
F=[]
G=[]
H=[]
I=[]
J=[]
stat1=[]
stat2=[]
stat3=[]
stat4=[]
stat5=[]
Loca=[]
ath_page=[]
latitude = []
longitude = []

for i in xrange(len(cities)):
    browser.get('https://www.strava.com/segments/search')
    browser.find_element_by_name('keywords').clear()
    browser.find_element_by_name('keywords').send_keys(cities[i])
    browser.find_element_by_id('climb-search-button').submit()
    time.sleep(2)

    html_code = browser.page_source.encode('utf-8')
    Soup = BeautifulSoup(html_code, 'html.parser')
    segmentList = []
    urlList = []

    for link in Soup.find_all('div'):
        segment = link.get('data-segment-id')
        if segment:
           segmentList.append(segment)
           urlList.append('https://www.strava.com/segments/'+segment)

    for r in xrange(len(segmentList)-1):

        filename='data/'+cities[i]+'+'+segmentList[r]+'.html'
        f = open(filename,'rU')
        soup = BeautifulSoup(f, 'html.parser')

        statsGroup=soup.find('ul',class_="inline-stats list-stats")

        states = statsGroup.find_all('b',class_="stat-text")
        stata  = states[0].text.strip('mi')
        statb  = states[1].text.strip('%')
        statc  = states[2].text.strip('ft')
        statd  = states[3].text.strip('ft')
        state  = states[4].text.strip('ft')

        jsgroups= soup.find_all('script')
        regex =  re.compile(r'"lat_lng":\[(\d+.\d+),(\-\d+.\d+)]')

        lat= None
        lng= None
        for js in jsgroups:
            text = js.text
            js_text = re.search(regex, text)
            if js_text:
                found=js_text
                lat = float(found.group(1))
                lng = float(found.group(2))

        locations=soup.find_all('span')
        for li in locations:
            if li.get('data-full-name') is not None:
                location = li.get('data-full-name')

        right_table=soup.find_all('table', class_='striped')

        for table in right_table:
            for row in table.findAll('tr'):
                cells = row.findAll('td')
                states = row.findAll('th')
                if len(cells)==8: #Only extract table body not heading
                    A.append(cells[0].text.strip('\n'))
                    B.append(cells[1].find('a').text)
                    ath_link=cells[1].find('a',href=True)['href']
                    ath_page.append('https://www.strava.com'+ath_link)
                    day = cells[2].find('a').text
                    date=datetime.datetime.strptime(day,'%b %d, %Y').date
                    C.append(date)
                    D.append(cells[3].text.strip('mi/h'))
                    E.append(cells[4].text.strip('\n').strip('bpm').strip('-'))
                    F.append(cells[5].text.strip('\n').strip('W'))
                    G.append(cells[6].text.strip('\n').strip('-'))
                    H.append(cells[7].text.strip('s'))
                    I.append(cities[i])
                    J.append(segmentList[r])
                    stat1.append(stata)
                    stat2.append(statb)
                    stat3.append(statc)
                    stat4.append(statd)
                    stat5.append(state)
                    Loca.append(location)
                    latitude.append(lat)
                    longitude.append(lng)

browser.quit()

df=pd.DataFrame(A,columns=['rank'])
df['name']=B
df['date']=C
df['pace']=D
df['hr']=E
df['power']=F
df['vam']=G
df['time']=H
df['city']=I
df['segment']=J
df['distance']=stat1
df['avg_grade']=stat2
df['lowest_elev']=stat3
df['highest_elev']=stat4
df['elev_difference']=stat5
df['segment_name']=Loca
df['athlete_link']=ath_page
df['latitude']=latitude
df['longitude']=longitude
df['date'] = pd.to_datetime(df['date'].astype(str))

engine = create_engine('postgresql://%s:%s@localhost/%s'%(db_user,db_pswd,db_name))

## insert data into database from Python
df.to_sql('running_data_table', engine, if_exists='replace')


#! /usr/bin/env python3 #
# -*- coding: utf-8 -*- #

# Implementações de Funções - - - - - - - - - - - - - - - - - - - - - - - - - #
def soma1():

    soma = float(0)
    sinal = -1

    for i in range(10000, 0, -1):
        soma = soma + float(1/i) * sinal
        sinal = sinal * (-1)

    return soma

def soma2():

    soma = 0
    sinal = 1

    for i in range(1, 10001, 1):
        soma = soma + float(1/i) * sinal
        sinal = sinal * (-1)

    return soma

def soma3():

    soma = 0

    for i in range(1, 10001, 2):
        soma = soma + float(1/i)

    for i in range(2, 10001, 2):
        soma = soma -float(1/i)

    return soma

def soma4():

    soma = 0

    for i in range(10000, 0, -2):
        soma = soma -float(1/i)

    for i in range(9999, 0, -2):
        soma = soma +float(1/i)

    return soma

def main(args):
    """
Faça um programa que calcula a soma

1 -1/2 +1/3 -1/4 + ... + 1/9999 -1/10000

pelas seguintes maneiras:

* adição dos termos da direita para a esquerda;
* adição dos termos da esquerda para a direita;
* adição separada dos termos positivos e dos termos negativos da esquerda
  para a direita;
* adição separada dos termos positivos e dos termos negativos da direita
  para a esquerda.

Compare e discuta os resultados obtidos no computador.
    """

    print(main.__doc__)

    print(soma1())
    print(soma2())
    print(soma3())
    print(soma4())

    return EX_OK

# Executa o script como sendo o módulo principal dele - - - - - - - - - - - - #
if __name__ == "__main__":
    import sys
    from os import EX_OK

    codigo_retorno = main(sys.argv)
    sys.exit(codigo_retorno)


import numpy as np
from rnn_cell_forward import rnn_cell_forward
def rnn_forward(x,a0,parameters):
    caches = []

    n_x,m,T_x = x.shape
    n_a,n_a = parameters["Waa"].shape

    a = np.zeros((n_a,m,T_x))
    a_next = np.zeros((n_a,m))

    for t in range(T_x):
        a_next,cache = rnn_cell_forward(x[:,:,t],a[:,:,t],parameters)
        a[:,:,t] = a_next
        caches.append(cache)

    caches = (caches , x)

    return a,caches


from time import time
import os
import fzpicalc.RB_model as rbm
import fzpicalc.basic_func as basic_func

maindir = os.getcwd()
print('\n')
print('Loading all RB models:')
print('\n')
#########################################################
#########################################################
## define stuff from DwfEl input file:
#########################################################
#########################################################

q_a = 3 #anzahl komponenten stiffness matrix matrix+fz+dynvisc=3
q_f = 2 #anzahl componenten load vector: matrix+fz=2    #alt + liftingfunc+prodrate
q_l = 1 #dimension des ouputs
q_m = 3
n_outputs = 1
n_timesteps = 127
dt = 1.0
euler_theta = 1.0 #backward euler
growth_rate = 1.1
start_time = 1.0
end_time = 1.8e6
varying_timesteps = True
time_dependent_parameters = True
ID_param = [5]
#online_mu_parameters = np.array([1.0e-09, 1.0e-04, 0.00028974, 1.8e3, 1.8e3, 1.0289971393879524e-06])
#([perm_matrix, perm_fault, mu, specific storage_matrix, specific storage_fault, production rate])

RB_MODEL_FOLDER_NAME = 'RB_Modelz_v4'

#load all fault zone models:
os.chdir(os.path.join(maindir,'fzpicalc'))
modelnames = os.listdir(RB_MODEL_FOLDER_NAME)
modelnames.remove('matrix')
models = []
for eachdir in modelnames:
    path = os.path.join(RB_MODEL_FOLDER_NAME,eachdir,'offline_data')
    t = time()
    model = rbm.model_from_offline_data(path,
                                     q_a,q_f,q_l,q_m,
                                     n_outputs,
                                     n_timesteps,
                                     dt,
                                     euler_theta,
                                     varying_timesteps = varying_timesteps,
                                     growth_rate = growth_rate,
                                     time_dependent_parameters = time_dependent_parameters,
                                     ID_param = ID_param, start_time = start_time,
                                     end_time = end_time)
    t_model_gen = basic_func.tidy(((time()- t)), 3)
    print(str(t_model_gen)+'s elapsed to add the '+eachdir+'m fault zone thickness model!')
    models.append(model)

#add matrix model:
modelnames.append('matrix')
t = time()
matrix_model = rbm.model_from_offline_data(os.path.join(RB_MODEL_FOLDER_NAME,'matrix','offline_data'),
                                     2,2,q_l,2,
                                     n_outputs,
                                     n_timesteps,
                                     dt,
                                     euler_theta,
                                     varying_timesteps = varying_timesteps,
                                     growth_rate = growth_rate,
                                     time_dependent_parameters = time_dependent_parameters,
                                     ID_param = [3], start_time = start_time,
                                     end_time = end_time)
models.append(matrix_model)
t_model_gen = basic_func.tidy(((time()- t)), 3)
print(str(t_model_gen)+'s elapsed to add the matrix model!')

#generate dicitonary with all RB models loaded as objects
models_dict = dict(zip(modelnames,models))

#calculate timesteps and times once:
t_list = [0.0,1.0]
dt=1
t=1
for i in range(125):
    dt=dt*1.1
    t+=dt
    t_list.append(t)
t_list.append(1.8e6)
os.chdir(maindir)

'''
#example usage:

RB_outputs_fz100 = load_all_RB_models.models_dict['100'].transient_rb_solve_without_error_bound(online_mu_parameters_fz)
RB_outputs_matrix = load_all_RB_models.models_dict['matrix'].transient_rb_solve_without_error_bound(online_mu_parameters_matrix)

df_fz100 = pd.melt(pd.DataFrame(RB_outputs_fz100)).drop(['variable'],axis=1)
df_fmatrix = pd.melt(pd.DataFrame(RB_outputs_matrix)).drop(['variable'],axis=1)
df_out = pd.concat([df_fz100, df_fmatrix], axis=1)
'''


# best with larger screen or phone sideways

import requests
from io import BytesIO

from PIL import Image

response = requests.get("https://66.media.tumblr.com/b412951f278ecbf5cffe39a6eeb12f57/tumblr_mhrl362GJt1s376ugo1_1280.jpg")
image_url = Image.open(BytesIO(response.content))

print(image_url)

ASCII_CHARS = ['0', '?', '%', '.', 'X', '+', '.', 'Z', ':', '1', '@']

def scale_image(image, new_width=100):
    """Resize image preserving the aspect ratio."""
    original_width, original_height = image.size
    aspect_ratio = original_height / float(original_width)
    new_height = int(aspect_ratio * new_width)
    new_image = image.resize((new_width, new_height))
    return new_image

def convert_to_grayscale(image):
    return image.convert('L')

def map_pixels_to_ascii_chars(image, range_width=25):
    """Map each pixel to an ascii char.

    Based on the range in which the pixel lies. 0-255
    is divided into 11 ranges of 25 pixels each.
    """
    pixels_in_image = list(image.getdata())
    pixels_to_chars = [
        ASCII_CHARS[pixel_value // range_width]
        for pixel_value in pixels_in_image
    ]
    return "".join(pixels_to_chars)

def convert_image_to_ascii(image, new_width=100):
    image = scale_image(image)
    image = convert_to_grayscale(image)
    pixels_to_chars = map_pixels_to_ascii_chars(image)
    len_pixels_to_chars = len(pixels_to_chars)
    image_ascii = [
        pixels_to_chars[index: index + new_width]
        for index in range(0, len_pixels_to_chars, new_width)
    ]
    return "\n".join(image_ascii)

local_image_filename = image_url

image = local_image_filename
image_ascii = convert_image_to_ascii(image)
print(image_ascii)
write: str = image_ascii
text_file = open("/Users/Y-S/Desktop/Output.txt", "w")
text_file.write(str(write))
text_file.close()


from flask import Flask,request,render_template,redirect
import os
app = Flask(__name__, template_folder='template')
from PIL import Image, ImageOps
import io
import base64

app.config["IMAGE_UPLOADS"] = "C:/Users/91932/Downloads/Flask_assign/test_app/test_app/static"

@app.route('/convert',methods = ["GET","POST"])
def upload_convert_image():
	if request.method == "POST":
		image = request.files['file']

		if image.filename == '':
			print("Image must have a file name")
			return redirect(request.url)

		file_name = image.filename

		basedir = os.path.abspath(os.path.dirname(__file__))

		image.save(os.path.join(basedir,"static", file_name))

		img = Image.open(app.config["IMAGE_UPLOADS"] + "/" + file_name)
		gray_image = img.convert('1')
		data = io.BytesIO()
		gray_image.save(data, "JPEG")
		encode_img_data = base64.b64encode(data.getvalue())

		return render_template("main.html",filename = encode_img_data.decode("UTF-8"))

	return render_template('main.html')

app.run(debug=True,port=2000)


import gym
import gym_connect4_twist_n_turn
import random
from agents.random import RandomAgent
from agents.human import HumanAgent
from agents.monde import MondeAgent
from agents.minimax import MinimaxAgent
from agents.monte_DQN import MonteDQNAgent
env = gym.make('Connect4_Twist_n_Turn-v0') # default board size is height=5, width=6
obs = env.reset()
done = False
player = 0
players = [MonteDQNAgent(3000),MinimaxAgent(4)]
for i in players:
    i.game_starts(obs[0])
legal_actions = env.get_moves()
env.render()
while not done:
    # print(f"obs 0 {obs[0]}\n======\nobs 1 {obs[1]}")
    action = players[player].make_move(obs[player],legal_actions)
    obs, reward, done, info = env.step(action)
    legal_actions = info['legal_actions']

    players[1-player].opponent_move(obs[1-player])

    print(f"player {player+1}'s action {action}")
    env.render()
    player = 1 - player # player index 0 or 1 alternates
for i in range(2):
    players[i].game_terminates(reward[i])

print(f"Winner is player {players[env.winner-1]}")
players[0].plot_loss()


"""Script for updating github workflows for all our integration forks that have to be synchronized with original repository"""

import os
import json
import subprocess
import shutil
import glob

from context import UserRepoContext
from scripts import FogRepoManager, BOT_USER, FOG_USER

def dump_readme(repo_dir, man: FogRepoManager):
    title = man.fork.name
    url = man.parent.html_url
    license_type = man.get_parent_license().key
    if license_type == 'mit':
        cp = ''
    else:
        owner = man.parent.owner
        cp = f'Copyright {man.parent.created_at.year} [{owner.name or owner.login}]({owner.html_url})'

    with open(os.path.join('templates', 'README.md'), 'r') as f:
        readme = f.read().format(title=title, url=url, copyright=cp)
        with open(os.path.join(repo_dir, 'README.md'), 'w') as g:
            g.write(readme)

def copy_workflows(repo_dir):
    target = os.path.join(repo_dir, '.github', 'workflows')
    os.makedirs(target, exist_ok=True)
    for file_ in glob.glob(r'templates/.github/workflows/*.yml'):
        shutil.copy(file_, target)

if __name__ == "__main__":

    with open('config.json', 'r') as f:
        names = json.load(f)['forks_to_sync']

    tkn = os.environ['GITHUB_TOKEN']
    proc = subprocess.run(['git', 'show', '-s', '--format=%B', 'HEAD'], text=True, capture_output=True)
    last_commit_msg = proc.stdout.strip()

    for repo_name in names:
        man = FogRepoManager(tkn, f'{FOG_USER.login}/{repo_name}')

        with UserRepoContext(tkn, FOG_USER.login, BOT_USER.login, BOT_USER.email, repo_name) as c:
            print('> copying workflow files')
            copy_workflows(repo_dir=c.cwd)
            dump_readme(repo_dir=c.cwd, man=man)
            c.run(f'git commit -a -m "{last_commit_msg}"')
            c.run(f'git push origin master')


class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        # clever solution
        # 1) reverse the entire array
        # 2) reverse the two individual subdivisions
        # first partition --> [0 : k-1]
        # second partition --> [k : N-1]

        # start and end INDICES
        def reverse(input_array, start, end):
            # size of the array
            N = end - start + 1
            for i in range(N // 2):
                left = start + i
                right = end - i
                input_array[left], input_array[right] =\
                        input_array[right], input_array[left]

        # need to compute the num of unique k shifts.
        unique_k = k % len(nums)

        # reverse the entire array
        reverse(nums, 0, len(nums) - 1)

        # reverse the first subdivision -> interval [0, k-1]
        reverse(nums, 0, unique_k - 1)

        # reverse the second subdivision -> interval [k, N-1]
        reverse(nums, unique_k, len(nums) - 1)


import pytest
import logging
import time
import threading
import parametrize

logger = logging.getLogger(__name__)
logger.setLevel(level=logging.INFO)

@pytest.mark.describe("Client object")
class TestConnectDisconnect(object):
    @pytest.mark.it("Can disconnect and reconnect")
    @pytest.mark.parametrize(*parametrize.connection_retry_disabled_and_enabled)
    @pytest.mark.parametrize(*parametrize.auto_connect_disabled_and_enabled)
    @pytest.mark.quicktest_suite
    def test_sync_connect_disconnect(self, brand_new_client, leak_tracker):
        leak_tracker.set_initial_object_list()

        client = brand_new_client

        client.connect()
        assert client.connected

        client.disconnect()
        assert not client.connected

        client.connect()
        assert client.connected

        leak_tracker.check_for_leaks()

    @pytest.mark.it(
        "Can do a manual connect in the `on_connection_state_change` call that is notifying the user about a disconnect."
    )
    @pytest.mark.parametrize(*parametrize.connection_retry_disabled_and_enabled)
    @pytest.mark.parametrize(*parametrize.auto_connect_disabled_and_enabled)
    # see "This assert fails because of initial and secondary disconnects" below
    @pytest.mark.skip(reason="two stage disconnect causes assertion in test code")
    def test_sync_connect_in_the_middle_of_disconnect(
        self, brand_new_client, service_helper, random_message, leak_tracker
    ):
        """
        Explanation: People will call `connect` inside `on_connection_state_change` handlers.
        We have to make sure that we can handle this without getting stuck in a bad state.
        """
        leak_tracker.set_initial_object_list()

        client = brand_new_client
        assert client

        reconnected_event = threading.Event()

        def handle_on_connection_state_change():
            nonlocal reconnected_event

            if client.connected:
                logger.info("handle_on_connection_state_change connected.  nothing to do")
            else:
                logger.info("handle_on_connection_state_change disconnected.  reconnecting.")
                client.connect()
                assert client.connected
                reconnected_event.set()
                logger.info("reconnect event set")

        client.on_connection_state_change = handle_on_connection_state_change

        # connect
        client.connect()
        assert client.connected

        # disconnect.
        reconnected_event.clear()
        logger.info("Calling client.disconnect.")
        client.disconnect()

        # wait for handle_on_connection_state_change to reconnect
        logger.info("waiting for reconnect_event to be set.")
        reconnected_event.wait()

        logger.info(
            "reconnect_event.wait() returned.  client.connected={}".format(client.connected)
        )
        # This assert fails because of initial and secondary disconnects
        assert client.connected

        # sleep a while and make sure that we're still connected.
        time.sleep(3)
        assert client.connected

        # finally, send a message to makes reu we're _really_ connected
        client.send_message(random_message)
        event = service_helper.wait_for_eventhub_arrival(random_message.message_id)
        assert event

        leak_tracker.check_for_leaks()

    @pytest.mark.it(
        "Can do a manual disconnect in the `on_connection_state_change` call that is notifying the user about a connect."
    )
    @pytest.mark.parametrize(*parametrize.connection_retry_disabled_and_enabled)
    @pytest.mark.parametrize(*parametrize.auto_connect_disabled_and_enabled)
    @pytest.mark.parametrize(
        "first_connect",
        [pytest.param(True, id="First connection"), pytest.param(False, id="Second connection")],
    )
    def test_sync_disconnect_in_the_middle_of_connect(
        self, brand_new_client, service_helper, random_message, first_connect, leak_tracker
    ):
        """
        Explanation: This is the inverse of `test_connect_in_the_middle_of_disconnect`.  This is
        less likely to be a user scenario, but it lets us test with unusual-but-specific timing
        on the call to `disconnect`.
        """
        leak_tracker.set_initial_object_list()

        client = brand_new_client
        assert client
        disconnect_on_next_connect_event = False

        disconnected_event = threading.Event()

        def handle_on_connection_state_change():
            nonlocal disconnected_event

            if client.connected:
                if disconnect_on_next_connect_event:
                    logger.info("connected.  disconnecting now")
                    client.disconnect()
                    disconnected_event.set()
                else:
                    logger.info("connected, but nothing to do")
            else:
                logger.info("disconnected.  nothing to do")

        client.on_connection_state_change = handle_on_connection_state_change

        if not first_connect:
            # connect
            client.connect()
            assert client.connected

            # disconnect.
            client.disconnect()

        assert not client.connected

        # now, connect (maybe for the second time), and disconnect inside the on_connected handler
        disconnect_on_next_connect_event = True
        disconnected_event.clear()
        client.connect()

        # and wait for us to disconnect
        disconnected_event.wait()
        assert not client.connected

        # sleep a while and make sure that we're still disconnected.
        time.sleep(3)
        assert not client.connected

        # finally, connect and make sure we can send a message
        disconnect_on_next_connect_event = False
        client.connect()
        assert client.connected

        client.send_message(random_message)
        event = service_helper.wait_for_eventhub_arrival(random_message.message_id)
        assert event

        leak_tracker.check_for_leaks()

@pytest.mark.dropped_connection
@pytest.mark.describe("Client object with dropped connection")
@pytest.mark.keep_alive(5)
class TestConnectDisconnectDroppedConnection(object):
    @pytest.mark.it("disconnects when network drops all outgoing packets")
    def test_sync_disconnect_on_drop_outgoing(self, client, dropper, leak_tracker):
        """
        This test verifies that the client will disconnect (eventually) if the network starts
        dropping packets
        """
        leak_tracker.set_initial_object_list()

        client.connect()
        assert client.connected
        dropper.drop_outgoing()

        while client.connected:
            time.sleep(1)

        # we've passed the test. Now wait to reconnect before we check for leaks. Otherwise we
        # have a pending ConnectOperation floating around and this would get tagged as a leak.
        dropper.restore_all()
        while not client.connected:
            time.sleep(1)

        leak_tracker.check_for_leaks()

    @pytest.mark.it("disconnects when network rejects all outgoing packets")
    def test_sync_disconnect_on_reject_outgoing(self, client, dropper, leak_tracker):
        """
        This test verifies that the client will disconnect (eventually) if the network starts
        rejecting packets
        """
        leak_tracker.set_initial_object_list()

        client.connect()
        assert client.connected
        dropper.reject_outgoing()

        while client.connected:
            time.sleep(1)

        # we've passed the test. Now wait to reconnect before we check for leaks. Otherwise we
        # have a pending ConnectOperation floating around and this would get tagged as a leak.
        dropper.restore_all()
        while not client.connected:
            time.sleep(1)

        leak_tracker.check_for_leaks()


#class Dog:
 #   def __init__(self, name, birth_Year, bark = "Wuh Wuh"):
  #      self.name = name
   #     self.birth_Year = birth_Year
    #    self.bark = bark

#    def barking(self, times):
 #       for i in range(times):
  #          print(self.bark)
   #     return

#dog1 = Dog("Akseli", 2001)
#dog2 = Dog("Mia", 1995, "dorka")
#dog3 = Dog("Jasmin", 1997, "olen kissa")
#dog4 = Dog("Lutri", 1999, "ne ampu rytkön Villen!")

#dog1.barking(5)
#dog2.barking(8)
#dog3.barking(2)
#dog4.barking(7)

class Car:
    def __init__(self, register_Number, top_Speed):
        self.register_Number = register_Number
        self.top_Speed = top_Speed
        self.velocity = 0
        self.traveled_Distance = 0

    cars = ["Toyota", "BMW", "Mersu", "Honda",
            "lancia", "Volvo", "Volkswagen", "Kia",
            "Hyundai", "Nissan"]
    for i in range(10):
        cars.append(cars("a" + i, 10))

    for car in cars:
     print (cars.print_info())

    def print_info(self):
        print(f"Auton {self.register_Number} "
              f"huippunopeus {self.top_Speed} km/h "
              f"tämänhetkinen nopeus {self.velocity} km/h "
              f"kuljettu matka {self.traveled_Distance} km/h ")

    def accelerate(self, speed_Change):
        if 0 < self.velocity + speed_Change < self.top_Speed:
            self.velocity = self.velocity + speed_Change
        elif self.velocity + speed_Change <= 0:
            self.velocity = 0
        elif self.velocity + speed_Change > self.top_Speed:
            self.velocity = self.top_Speed

    def traveled(self, hours):
        self.traveled_Distance = {self.velocity * hours + self.traveled_Distance}

Toyota = Car("ABC-1", 142)
Toyota.print_info()
Toyota.accelerate(30)
Toyota.accelerate(70)
Toyota.accelerate(50)
Toyota.print_info()
Toyota.accelerate(-200)
Toyota.traveled(1.5)
Toyota.print_info()


from utils.constants import TSD, GENOME_BUFFER

def get_likely_target(
    reads_site, genome, spacer, read_is_fwd_strand, target_distance=40
):
    # this function find the most likely target sequence for a given read
    # looking at a 3bp window within 40bp upstream of the tLR integration site
    # using similarity to spacer as criteria, particularly in seed region
    read_position = reads_site + GENOME_BUFFER
    SPACER_LEN = len(spacer)

    debug = False

    best_match = None

    # evaluate targets
    for i in range(-3, 4, 1):
        if read_is_fwd_strand:
            # The read coming from the left side was forward strand, so
            # tLR implies the offtarget spacer is downstream of the read
            # on the reverse strand

            test_site = read_position + target_distance - i
            test_seq = genome[test_site : test_site + SPACER_LEN].reverse_complement()
            test_PAM = genome[
                test_site + SPACER_LEN : test_site + SPACER_LEN + 3
            ].reverse_complement()
        else:
            # The read coming from the left side was reverse strand, so
            # tLR implies the offtarget spacer is upstream of the read
            # on the forward strand
            test_site = read_position - target_distance - SPACER_LEN + i
            test_seq = genome[test_site : test_site + SPACER_LEN]
            test_PAM = genome[test_site - 3 : test_site]

        # Test if the PAM is "GTN"
        is_PAM_match = test_PAM[0:2] == "GT"
        best_match = evaluate_best_match(
            best_match,
            test_seq,
            test_site,
            read_is_fwd_strand,
            spacer,
            is_PAM_match,
            debug=debug,
        )

    return best_match

def evaluate_best_match(
    existing_best,
    tgt_seq,
    position,
    is_fwd_strand,
    spacer,
    is_PAM_match,
    without_5ths=False,
    debug=False,
):
    effective_tgt = tgt_seq
    effective_spacer = spacer
    if without_5ths:
        effective_spacer = "".join(
            [s for (idx, s) in enumerate(spacer) if ((idx + 1) % 6 != 0)]
        )
        effective_tgt = "".join(
            [s for (idx, s) in enumerate(tgt_seq) if ((idx + 1) % 6 != 0)]
        )

    matches = list(
        tgt_bp == spacer_bp
        for tgt_bp, spacer_bp in zip(effective_tgt, effective_spacer)
    )
    group_size = 7 if without_5ths else 8

    seed_match = sum(matches[:group_size])
    total_match = sum(matches)
    distal_match = sum(matches[-group_size:])
    best_match = existing_best

    is_better_match = False
    if best_match is None:
        is_better_match = True
    elif not best_match["is_PAM_match"]:
        if is_PAM_match:
            is_better_match = True
        else:
            if best_match["total_match"] < total_match:
                is_better_match = True
            elif best_match["total_match"] == total_match:
                if best_match["seed_match"] < seed_match:
                    is_better_match = True

    if is_better_match:
        best_match = {
            "is_PAM_match": is_PAM_match,
            "seed_match": seed_match,
            "total_match": total_match,
            "distal_match": distal_match,
            "seq": tgt_seq,
            "pos": position,
            "strand": "fwd" if is_fwd_strand else "rv",
        }
    return best_match


# 백준 2800번 괄호
import sys
from itertools import combinations

input = sys.stdin.readline().rstrip()
index = -1
stack = []
glst = []
resarr = set()

# 괄호쌍 위치 glst에 넣기
for i in range(len(input)):
    if input[i] == '(':
        stack.append(i)
    elif input[i] == ')':
        s = stack.pop()
        glst.append((s,i))

# 조합을 이용해 괄호 제거
for i in range(1,len(glst)+1):
    for comb in combinations(glst,i):
        tmp = list(input)
        for x in comb:
            tmp[x[0]] = ""
            tmp[x[1]] = ""
            resarr.add("".join(tmp))

for x in sorted(list(resarr)):
    print(x)


from termcolor import colored, cprint
from random import *
from predefined_shapes import *

colors = {
    0:'grey',
    1:'red',
    2:'green',
    3:'yellow',
    4:'blue',
    5:'magenta',
    6:'cyan',
    7:'white',
}
back_colors = {
    0:'on_grey',
    1:'on_red',
    2:'on_green',
    3:'on_yellow',
    4:'on_blue',
    5:'on_magenta',
    6:'on_cyan',
    7:'on_white',
}

#class Board

#class Piece

def solution(b_file, s_file):
    myboard = []
    with open(b_file) as f:
        lines = f.read().splitlines()
    for l in lines:
        myrow = list(map(int, l.split()))
        myboard.append(myrow)
    #myboard = [
    #        [1,1,1,1,1,1,1,1],
    #        [1,1,1,1,1,1,1,1],
    #        [1,1,1,1,1,1,0,0],
    #        [1,1,1,1,1,1,0,0],
    #        [1,1,1,1,0,0,0,0],
    #        [1,1,0,0,0,0,0,0],
    #        [1,1,0,0,0,0,0,0],
    #        ]

    pieces = []
    with open(s_file) as f:
        lines = f.read().splitlines()
    for s in lines:
        pieces.append(eval(s))
    #pieces = [Ls_flipxy, L_m90, L_m90_flipy, L_flipx, I_3_1, L_90, L_flipy, I_3_1, T_m90, ___]#, T_90, L_flipx, L_flipy, L, T_m90, __, Ls_flipx, Ls_flipy]
    #shuffle(pieces)
    #print(myboard)
    #print(pieces)

    if not myboard or not myboard[0]:
        return False

    M = len(myboard)
    N = len(myboard[0])
    #print(M,N)

    ### make first board
    ### or make first piece

    ### make board
    def is_clear(myboard):
        for i,row in enumerate(myboard):
            for j,n in enumerate(row):
                if n==-1:
                    return False
        return True

    def is_valid(p,i,j):
        for x,row in enumerate(p):
            for y,n in enumerate(row):
                if n==0 and not (0<=i+x<M and 0<=j+y<N):
                    return False
                elif n==0 and myboard[i+x][j+y]!=1:
                    return False
        return True

    def update_board(k,p,i,j):
        for x,row in enumerate(p):
            for y,n in enumerate(row):
                if n==0: myboard[i+x][j+y]=k
        return

    def revoke_board(p,i,j):
        for x,row in enumerate(p):
            for y,n in enumerate(row):
                if n==0: myboard[i+x][j+y]=1
        return

    def print_board(myboard):
        for row in myboard:
            for n in row:
                if n==0:
                    print('{:3} '.format(n),end='')
                else:
                    cprint('{:3} '.format(n),colors[(n+3)%7],back_colors[(n+1)%7],end='')
            print()
        return

    def generate_invalid(m,n,s):
        return

    def get_pattern(myboard):
        #for i in range(M):
        #    for j in range(N):
        #        if myboard[i][j]==1:
        return

    def check_bottleneck(myboard):
        #get_pattern(myboard)
        return True

    used = set()
    it = [0]
    def make_board(i,j):
        it[0] += 1
        if it[0]%1000000==0: print(f'Iteration {it[0]}')
        if it[0]%10000000==0: print_board(myboard)

        if not check_bottleneck(myboard): return False

        if i==M:
            #print('moved to last')
            #print(used)
            #print_board(myboard)
            if len(used)==len(pieces) and is_clear(myboard):
                print_board(myboard)
                return True
            else:
                return False

        di,jj = divmod(j+1,N)
        ii = i+di
        #if myboard[i][j]!=1:
        if 0:
            if make_board(ii,jj):
                return True
            else:
                return False
        else: # make valid board
            for k,p in enumerate(pieces):
                if k in used: continue
                if is_valid(p,i,j):
                    used.add(k)
                    update_board(k+2,p,i,j)
                    if make_board(ii,jj):
                        return True
                    else:
                        revoke_board(p,i,j)
                        used.remove(k)
            if make_board(ii,jj):
                return True
            else:
                return False

    return make_board(0,0)

if __name__ == '__main__':
    import sys
    solved = solution(sys.argv[1],sys.argv[2])
    print(solved)


import scrapy
from selenium import webdriver
from JobsPro.items import JobsproItem
from selenium.webdriver.chrome.options import Options
import random

class JobsSpider(scrapy.Spider):
    name = 'jobs'
    start_urls =[]
    detail_urls=[]

    #allowed_domains = ['www.xxx.com']
    for page in range(1,2):
        boss_url = f'https://www.zhipin.com/c101190100/?query=%E7%88%AC%E8%99%AB&page={page}&ka=page-{page}'
        start_urls.append(boss_url)
    def __init__(self):
        PROXY_http = []
        dic = {"ERRORCODE":"0","RESULT":[{"port":"27001","ip":"183.155.118.116"},{"port":"30585","ip":"123.10.64.136"},{"port":"23544","ip":"27.158.47.114"},{"port":"37146","ip":"1.194.32.249"},{"port":"40072","ip":"49.70.17.147"},{"port":"43939","ip":"60.166.75.203"},{"port":"31453","ip":"123.54.22.36"},{"port":"32579","ip":"59.47.230.154"},{"port":"46409","ip":"36.27.142.176"},{"port":"40444","ip":"27.152.193.170"}]}
        for i in range(0, len(dic['RESULT'])):
            ip = f'{dic["RESULT"][i]["ip"]}:{dic["RESULT"][i]["port"]}'
            PROXY_http.append(ip)
        IP = random.choice(PROXY_http)
        proxy='http://' +IP

        chrome_options = Options()
        chrome_options.add_argument(f'--proxy-server={proxy}')
        chrome_options.add_argument(
            f'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36')
        print('当前的IP是：',IP)
        self.bro=webdriver.Chrome(executable_path='D:/SoftwareSpace/python/chromedriver.exe',
                              chrome_options=chrome_options)
    def parse(self, response):
        li_list = response.xpath('//*[@id="main"]/div/div[3]/ul/li')
        for li in li_list:
            item = JobsproItem()
            item['job'] = li.xpath('./div[1]/div[1]/div[1]/div[1]/div[1]/span[1]/a/test()').extract_first()
            item['place'] = li.xpath('./div[1]/div[1]/div[1]/div[1]/div[1]/span[2]/span/test()').extract_first()
            item['salary'] = li.xpath('./div[1]/div[1]/div[1]/div[1]/div[2]/span/test()').extract_first()
            item['year'] = li.xpath('./div[1]/div[1]/div[1]/div[1]/div[2]/p//test()').extract_first()
            item['c_name'] = li.xpath('./div[1]/div[1]/div[2]/div[1]/h3/a/test()').extract_first()
            item['fuli'] = li.xpath('./div[1]/div[2]/div[2]/test()').extract_first()

            detail_url ='https://www.zhipin.com' + li.xpath('./div/div[1]/div[1]/div/div[1]/span[1]/a/@href').extract_first()

            self.detail_urls.append(detail_url)
            yield item


part=input()
n=int(input())
letter1=input()
letter2=input()
newline=""
f=open(part)
for line in f:
    newline=line.upper()
    found=newline.find(letter1.upper())
    while found>0 and n>0:
        print(line[:found],end="")
        print(letter2,end="")
        found=newline.find(letter1.upper(),found)
            #print(line[len(line)-found-len(letter1)-len(letter2):],end="")
        #print(line[len(letter1)+found:],end="")
        n-=1
    else:
        print(line[found+len(letter1):])
#for line in file:
#   print(line,end="")


#initialise tiles
tileIDName = {}
tileID = {}
tileIDImage = {}
# NO_GIF_TILES -- tile numbers which do not correspond to a GIF file
NO_GIF_TILES=[23]

def GetCrossRef():
    from pacman import pygame, thisLevel
    f = open("res/crossref.txt", 'r')
    # ANDY -- edit
    # fileOutput = f.read()
    # str_splitByLine = fileOutput.split('\n')

    lineNum = 0
    useLine = False

    for i in f.readlines():
        # print " ========= Line " + str(lineNum) + " ============ "
        while len(i) > 0 and (i[-1] == '\n' or i[-1] == '\r'): i = i[:-1]
        while len(i) > 0 and (i[0] == '\n' or i[0] == '\r'): i = i[1:]
        str_splitBySpace = i.split(' ')

        j = str_splitBySpace[0]

        if (j == "'" or j == "" or j == "#"):
            # comment / whitespace line
            # print " ignoring comment line.. "
            useLine = False
        else:
            # print str(wordNum) + ". " + j
            useLine = True

        if useLine == True:
            tileIDName[int(str_splitBySpace[0])] = str_splitBySpace[1]
            tileID[str_splitBySpace[1]] = int(str_splitBySpace[0])

            thisID = int(str_splitBySpace[0])
            if not thisID in NO_GIF_TILES:
                tileIDImage[thisID] = pygame.image.load('tiles/' + str_splitBySpace[1] + '.gif')
            else:
                tileIDImage[thisID] = pygame.Surface((16, 16))

            # change colors in tileIDImage to match maze colors
            for y in range(0, 16, 1):
                for x in range(0, 16, 1):

                    if tileIDImage[thisID].get_at((x, y)) == (255, 206, 255, 255):
                        # wall edge
                        tileIDImage[thisID].set_at((x, y), thisLevel.edgeLightColor)

                    elif tileIDImage[thisID].get_at((x, y)) == (132, 0, 132, 255):
                        # wall fill
                        tileIDImage[thisID].set_at((x, y), thisLevel.fillColor)

                    elif tileIDImage[thisID].get_at((x, y)) == (255, 0, 255, 255):
                        # pellet color
                        tileIDImage[thisID].set_at((x, y), thisLevel.edgeShadowColor)

                    elif tileIDImage[thisID].get_at((x, y)) == (128, 0, 128, 255):
                        # pellet color
                        tileIDImage[thisID].set_at((x, y), thisLevel.pelletColor)

                        # print str_splitBySpace[0] + " is married to " + str_splitBySpace[1]
        lineNum += 1


# Enter your code here. Read input from STDIN. Print output to STDOUT

from itertools import product

string1 = input()
list1 = list(map(int,string1.strip().split()))

string2 = input()
list2 = list(map(int,string2.strip().split()))

listFinal = product(list1,list2)

list1 = list(listFinal)

num = len(list1)
#this is used to convert list to string

for item in list1 :
    print(item,end=" ")

PYTHON PART - 2 (Advanced Python)/PYTHON ACCESSORIES/productOfSets.py