import argparse
import numpy as np
import torch
from torchtext.data import Iterator as BatchIter
import torch.nn.functional as F

from Beam import Beam
import causalchains.utils.data_utils as du
from causalchains.utils.data_utils import EOS_TOK, SOS_TOK, PAD_TOK
from causalchains.train.masked_cross_entropy import masked_cross_entropy
from decode_utils import transform, get_tups, get_pred_events

#from data.generate_tups import VERB, SUB, OBJ, PREP
VERB = "__V__"
SUB = "__S__"
OBJ = "__O__"
PREP = "__P__"

def generate(use_cuda=False, device=-1):

    vocab = du.load_vocab(args.vocab)
    eos_id = vocab.stoi[EOS_TOK]
    pad_id = vocab.stoi[PAD_TOK]
    sos_id = vocab.stoi[SOS_TOK]
    tup_id = vocab.stoi[TUP_TOK]

    assert False == (args.perplexity and args.seed and args.ranking), "Only 1 can be True at a time."
    # Batch size during decoding is set to 1
    assert args.batch_size == 1, "Set batch size to 1 during decoding."

    # Load the model.
    with open(args.model, 'rb') as f:
        model = torch.load(f, map_location=lambda s, loc: s)

    # set the eval mode
    model.eval()
    # to decode without cuda
    model.use_cuda = False

    # TEMP FIX to work with old models without this parameter.
    #model.type_emb = None

    # TASK SPECIFIC FUNCTION CALLS
    if args.ranking:
        # HARD only. Easy one has been deactivated.
        do_ranking(model, vocab)
    elif args.perplexity:
        get_perplexity(model, vocab)
    elif args.seed:
        gen_from_seed(model, vocab, eos_id, pad_id, sos_id, tup_id)
    else:
        print("NOT IMPLEMENTED. RETURNING.")
        return

def do_ranking(model, vocab):

    dataset = du.NarrativeClozeDataset(args.data, vocab, src_seq_length=MAX_EVAL_SEQ_LEN, min_seq_length=MIN_EVAL_SEQ_LEN)
    batches = BatchIter(dataset, args.batch_size, sort_key=lambda x:len(x.actual), train=False, device=device)

    ranked_acc = 0.0
    if args.emb_type:
        print("RANKING WITH ROLE EMB")
        vocab2 = du.load_vocab(args.vocab2)
        role_dataset = du.NarrativeClozeDataset(args.role_data, vocab2, src_seq_length=MAX_EVAL_SEQ_LEN, min_seq_length=MIN_EVAL_SEQ_LEN)
        role_batches = BatchIter(role_dataset, args.batch_size, sort_key=lambda x:len(x.actual), train=False, device=device)

        assert len(dataset) == len(role_dataset), "Dataset and Role dataset must be of same length."

        for iteration, (bl, rbl) in enumerate(zip(batches, role_batches)):

            if (iteration+1)%25 == 0:
                print("iteration {}".format(iteration+1))

            ## DATA STEPS
            all_texts = [bl.actual, bl.actual_tgt, bl.dist1, bl.dist1_tgt, bl.dist2, bl.dist2_tgt, bl.dist3, bl.dist3_tgt,
            bl.dist4, bl.dist4_tgt, bl.dist5, bl.dist5_tgt] # each is a tup

            all_roles = [rbl.actual, rbl.dist1, rbl.dist2, rbl.dist3, rbl.dist4, rbl.dist5] # tgts are not needed for role
            assert len(all_roles) == 6, "6 = 6 * 1."

            assert len(all_texts) == 12, "12 = 6 * 2."

            all_texts_vars = []
            all_roles_vars = []

            if use_cuda:
                for tup in all_texts:
                    all_texts_vars.append((Variable(tup[0].cuda(), volatile=True), tup[1]))
                for tup in all_roles:
                    all_roles_vars.append((Variable(tup[0].cuda(), volatile=True), tup[1]))

            else:
                for tup in all_texts:
                    all_texts_vars.append((Variable(tup[0], volatile=True), tup[1]))
                for tup in all_roles:
                    all_roles_vars.append((Variable(tup[0], volatile=True), tup[1]))

            # will itetrate 2 at a time using iterator and next
            vars_iter = iter(all_texts_vars)
            roles_iter = iter(all_roles_vars)

            # run the model and collect ppls for all 6 sentences
            pps = []
            for tup in vars_iter:
                ## INIT AND DECODE before every sentence
                hidden = model.init_hidden(args.batch_size)
                next_tup = next(vars_iter)
                role_tup = next(roles_iter)
                nll = calc_perplexity(args, model, tup[0], vocab, next_tup[0], next_tup[1], hidden, role_tup[0])
                pp = torch.exp(nll)
                #print("NEG-LOSS {} PPL {}".format(nll.data[0], pp.data[0]))
                pps.append(pp.data.numpy()[0])

            # low perplexity == top ranked sentence- correct answer is the first one of course
            assert len(pps) == 6, "6 targets."
            #print("\n")
            all_texts_str = [transform(text[0].data.numpy()[0], vocab.itos) for text in all_texts_vars]
            #print("ALL: {}".format(all_texts_str))
            min_index = np.argmin(pps)
            if min_index == 0:
                ranked_acc += 1
                #print("TARGET: {}".format(transform(all_texts_vars[1][0].data.numpy()[0], vocab.itos)))
                #print("CORRECT: {}".format(transform(all_texts_vars[1][0].data.numpy()[0], vocab.itos)))
            #else:
                # print the ones that are wrong
                #print("TARGET: {}".format(transform(all_texts_vars[1][0].data.numpy()[0], vocab.itos)))
                #print("WRONG: {}".format(transform(all_texts_vars[min_index+2][0].data.numpy()[0], vocab.itos)))

            if (iteration+1) == args.max_decode:
                print("Max decode reached. Exiting.")
                break

        ranked_acc /= (iteration+1) * 1/100 # multiplying to get percent
        print("Average acc(%): {}".format(ranked_acc))
        return ranked_acc

    else: # THIS IS FOR MODEL WITHOUT ROLE EMB

        print("RANKING WITHOUT ROLE EMB.")
        for iteration, bl in enumerate(batches):

            if (iteration+1)%25 == 0:
                print("iteration {}".format(iteration+1))

            ## DATA STEPS
            all_texts = [bl.actual, bl.actual_tgt, bl.dist1, bl.dist1_tgt, bl.dist2, bl.dist2_tgt, bl.dist3, bl.dist3_tgt,
            bl.dist4, bl.dist4_tgt, bl.dist5, bl.dist5_tgt] # each is a tup

            assert len(all_texts) == 12, "12 = 6 * 2."

            all_texts_vars = []
            if use_cuda:
                for tup in all_texts:
                    all_texts_vars.append((Variable(tup[0].cuda(), volatile=True), tup[1]))
            else:
                for tup in all_texts:
                    all_texts_vars.append((Variable(tup[0], volatile=True), tup[1]))

            # will itetrate 2 at a time using iterator and next
            vars_iter = iter(all_texts_vars)

            # run the model for all 6 sentences
            pps = []
            for tup in vars_iter:
                ## INIT AND DECODE before every sentence
                hidden = model.init_hidden(args.batch_size)
                next_tup = next(vars_iter)

                nll = calc_perplexity(args, model, tup[0], vocab, next_tup[0], next_tup[1], hidden)
                pp = torch.exp(nll)
                #print("NEG-LOSS {} PPL {}".format(nll.data[0], pp.data[0]))
                pps.append(pp.data.numpy()[0])

            # low perplexity == top ranked sentence- correct answer is the first one of course
            assert len(pps) == 6, "6 targets."
            #print("\n")
            all_texts_str = [transform(text[0].data.numpy()[0], vocab.itos) for text in all_texts_vars]
            #print("ALL: {}".format(all_texts_str))
            min_index = np.argmin(pps)
            if min_index == 0:
                ranked_acc += 1
                #print("TARGET: {}".format(transform(all_texts_vars[1][0].data.numpy()[0], vocab.itos)))
                #print("CORRECT: {}".format(transform(all_texts_vars[1][0].data.numpy()[0], vocab.itos)))
            #else:
                # print the ones that are wrong
                #print("TARGET: {}".format(transform(all_texts_vars[1][0].data.numpy()[0], vocab.itos)))
                #print("WRONG: {}".format(transform(all_texts_vars[min_index+2][0].data.numpy()[0], vocab.itos)))

            if (iteration+1) == args.max_decode:
                print("Max decode reached. Exiting.")
                break

        ranked_acc /= (iteration+1) * 1/100 # multiplying to get percent
        print("Average acc(%): {}".format(ranked_acc))
        return ranked_acc

def gen_from_seed(model, vocab, eos_id, pad_id, sos_id, tup_id):

    if args.emb_type: # GEN FROM SEED WITH ROLE EMB
        print("GEN SEED WITH ROLE EMB")
        vocab2 = du.load_vocab(args.vocab2)
        # will use this to feed in role ids in beam decode
        ROLES = [vocab2.stoi[TUP_TOK], vocab2.stoi[VERB], vocab2.stoi[SUB], vocab2.stoi[OBJ], vocab2.stoi[PREP]]
        dataset = du.LMRoleSentenceDataset(args.data, vocab, args.role_data, vocab2, src_seq_length=MAX_EVAL_SEQ_LEN, min_seq_length=MIN_EVAL_SEQ_LEN) #put in filter pred later
        dataset = du.LMRoleSentenceDataset(args.data, vocab, args.role_data, vocab2) #put in filter pred later
        batches = BatchIter(dataset, args.batch_size, sort_key=lambda x:len(x.text), train=False, device=device)

        for iteration, bl in enumerate(batches):

            if (iteration+1)%25 == 0:
                print("iteration {}".format(iteration+1))

            ## DATA STEPS
            batch, batch_lens = bl.text
            target, target_lens = bl.target
            role, role_lens = bl.role

            if use_cuda:
                batch = Variable(batch.cuda(), volatile=True)
                role = Variable(role.cuda(), volatile=True)
            else:
                batch = Variable(batch, volatile=True)
                role = Variable(role, volatile=True)

            ## INIT AND DECODE
            hidden = model.init_hidden(args.batch_size)
            #run the model first on t-1 events, except last word. we know corresponding role ids as well.
            seq_len = batch.size(1)
            for i in range(seq_len-1):
                inp = batch[:, i]
                inp = inp.unsqueeze(args.batch_size)
                typ = role[:, i]
                typ = typ.unsqueeze(1)
                _, hidden = model(inp, hidden, typ)

            #print("seq len {}, decode after {} steps".format(seq_len, i+1))
            # beam set current state to last word in the sequence
            beam_inp = batch[:, i+1]
            # do not need this anymore as assuming last sequence role obj is prep.
            #role_inp = role[:, i+1]
 #           print("ROLES LIST: {}".format(ROLES))
 #           print("FIRST ID: {}".format(role[:, i+1]))

            # init beam initializes the beam with the last sequence element. ROLE is a list of roe type ids.
            outputs = beam_decode(model, beam_inp, hidden, args.max_len_decode, args.beam_size, pad_id, sos_id, eos_id, tup_idx=tup_id, init_beam=True, roles=ROLES)
            predicted_events = get_pred_events(outputs, vocab)

            print("CONTEXT: {}".format(transform(batch.data.squeeze(), vocab.itos)))
            print("PRED_t: {}".format(predicted_events)) # n_best stitched together.

            if (iteration+1) == args.max_decode:
                print("Max decode reached. Exiting.")
                break

    else:
        print("GEN SEED WITHOUT ROLE EMB")
        dataset = du.LMSentenceDataset(args.data, vocab, src_seq_length=MAX_EVAL_SEQ_LEN, min_seq_length=MIN_EVAL_SEQ_LEN) #put in filter pred later
        batches = BatchIter(dataset, args.batch_size, sort_key=lambda x:len(x.text), train=False, device=device)
        for iteration, bl in enumerate(batches):

            if (iteration+1)%25 == 0:
                print("iteration {}".format(iteration+1))

            ## DATA STEPS
            batch, batch_lens = bl.text
            target, target_lens = bl.target

            if use_cuda:
                batch = Variable(batch.cuda(), volatile=True)
            else:
                batch = Variable(batch, volatile=True)

            ## INIT AND DECODE
            hidden = model.init_hidden(args.batch_size)

            #run the model first on t-1 events, except last word
            seq_len = batch.size(1)
            for i in range(seq_len-1):
                inp = batch[:, i]
                inp = inp.unsqueeze(args.batch_size)
                _, hidden = model(inp, hidden)

            #print("seq len {}, decode after {} steps".format(seq_len, i+1))
            # beam set current state to last word in the sequence
            beam_inp = batch[:, i+1]

            # init beam initializesthe beam with the last sequence element
            outputs = beam_decode(model, beam_inp, hidden, args.max_len_decode, args.beam_size, pad_id, sos_id, eos_id, tup_idx=tup_id, init_beam=True)
            predicted_events = get_pred_events(outputs, vocab)

            print("CONTEXT: {}".format(transform(batch.data.squeeze(), vocab.itos)))
            print("PRED_t: {}".format(predicted_events)) # n_best stitched together.

            if (iteration+1) == args.max_decode:
                print("Max decode reached. Exiting.")
                break

def get_perplexity(model, vocab):
    total_loss = 0.0
    if args.emb_type: # GET PERPLEXITY WITH ROLE EMB
        print("PERPLEXITY WITH ROLE EMB")
        vocab2 = du.load_vocab(args.vocab2)
        dataset = du.LMRoleSentenceDataset(args.data, vocab, args.role_data, vocab2, src_seq_length=MAX_EVAL_SEQ_LEN, min_seq_length=MIN_EVAL_SEQ_LEN) #put in filter pred later
        batches = BatchIter(dataset, args.batch_size, sort_key=lambda x:len(x.text), train=False, device=device)

        print("DATASET {}".format(len(dataset)))
        for iteration, bl in enumerate(batches):

            if (iteration+1)%25 == 0:
                print("iteration {}".format(iteration+1))

            ## DATA STEPS
            batch, batch_lens = bl.text
            target, target_lens = bl.target
            role, role_lens = bl.role

            if use_cuda:
                batch = Variable(batch.cuda(), volatile=True)
                target = Variable(target.cuda(), volatile=True)
                role = Variable(role.cuda(), volatile=True)
            else:
                batch = Variable(batch, volatile=True)
                target = Variable(target, volatile=True)
                role = Variable(role, volatile=True)

            ## INIT AND DECODE
            hidden = model.init_hidden(args.batch_size)
            ce_loss = calc_perplexity(args, model, batch, vocab, target, target_lens, hidden, role)
            #print("Loss {}".format(ce_loss))
            total_loss = total_loss + ce_loss.data[0]

            if (iteration+1) == args.max_decode:
                print("Max decode reached. Exiting.")
                break

        # after iterating over all examples
        loss = total_loss / (iteration+1)
        print("Average Loss: {}".format(loss))
        return loss

    else:
        print("PERPLEXITY WITHOUT ROLE EMB")
        dataset = du.LMSentenceDataset(args.data, vocab, src_seq_length=MAX_EVAL_SEQ_LEN, min_seq_length=MIN_EVAL_SEQ_LEN) #put in filter pred later
        batches = BatchIter(dataset, args.batch_size, sort_key=lambda x:len(x.text), train=False, device=device)
        for iteration, bl in enumerate(batches):

            if (iteration+1)%25 == 0:
                print("iteration {}".format(iteration+1))

            ## DATA STEPS
            batch, batch_lens = bl.text
            target, target_lens = bl.target

            if use_cuda:
                batch = Variable(batch.cuda(), volatile=True)
                target = Variable(target, volatile=True)
            else:
                batch = Variable(batch, volatile=True)
                target = Variable(target, volatile=True)

            ## INIT AND DECODE
            hidden = model.init_hidden(args.batch_size)
            ce_loss = calc_perplexity(args, model, batch, vocab, target, target_lens, hidden)
            #print("Loss {}".format(ce_loss))
            total_loss = total_loss + ce_loss.data[0]

            if (iteration+1) == args.max_decode:
                print("Max decode reached. Exiting.")
                break

        # after iterating over all examples
        loss = total_loss / (iteration+1)
        print("Average Loss: {}".format(loss))
        return loss

def calc_perplexity(args, model, batch, vocab, target, target_lens, hidden, role=None):

    logits = []
    for i in range(batch.size(1)): #decode input
        inp = batch[:, i]
        inp = inp.unsqueeze(1)
        if args.emb_type:
            typ = role[:, i]
            typ = typ.unsqueeze(1)
            logit, hidden = model(inp, hidden, typ) # add in the role here
        else:
            # keep updating the hidden state accordingly
            logit, hidden = model(inp, hidden)
        logits += [logit]

    logits = torch.stack(logits, dim=1)
    # loss for the sequence
    # making changes to not consider the EOS when calculating the scores.
    target_lens = target_lens - 1
    loss = masked_cross_entropy(logits, target, Variable(target_lens))
    return loss

def beam_decode(model, input, hidden, max_len_decode, beam_size, pad_id, sos_id, eos_id, tup_idx=4, batch_size=1, use_constraints=True, init_beam=False, roles=None):
    # hidden [1, 1, hidden_size]

    assert beam_size > 0 and batch_size == 1, "Beam decoding batch size must be 1 and Beam size greater than 0."

    # Helper functions for working with beams and batches
    def var(a): return Variable(a, volatile=True)

    def bottle(m):
        return m.view(batch_size * beam_size, -1)

    def unbottle(m):
        return m.view(beam_size, batch_size, -1)

    def beam_update(e, idx, positions, beam_size):
        sizes = e.size() # [1, beam_size, hidden_size]
        br = sizes[1]
        if len(sizes) == 3:
            sent_states = e.view(sizes[0], beam_size, br // beam_size,
                                 sizes[2])[:, :, idx]
        else:
            sent_states = e.view(sizes[0], beam_size,
                                 br // beam_size,
                                 sizes[2],
                                 sizes[3])[:, :, idx]

        # [1, beam_size, hidden_size]
        indexed_before = sent_states.data.index_select(1, positions)
        sent_states.data.copy_(
            sent_states.data.index_select(1, positions))
        indexed_after = sent_states.data.index_select(1, positions)

    # 1 beam object as we have batch_size 1 during decoding
    beam = [Beam(beam_size, n_best=args.n_best,
                cuda=use_cuda,
                pad=pad_id,
                eos=eos_id,
                bos=sos_id,
                min_length=10)]

    if init_beam:
        # id of last element in seq to init the beam
        for b in beam:
            b.next_ys[0][0] = np.asscalar(input.data.numpy()[0])

    # [1, beam_size, hidden_size]
    hidden = hidden.repeat(1, beam_size, 1)

    # this comes from the known role id of the last seqence object.
    #if args.emb_type:
        #inp2 = role.repeat(1, beam_size)

    verb_list = [[]]*beam_size #for constraints
    # run the decoder to generate the sequence
    for i in range(max_len_decode):

        # one all beams have EOS break
        if all((b.done() for b in beam)):
            break

        # No need to explicitly set the input to previous output - beam advance does it. Make sure.
        inp = var(torch.stack([b.get_current_state() for b in beam])
                              .t().contiguous().view(-1)) #[beam_size]

        # Tested that the last output is the input in the next time step.
        # Run one step of the decoder
        # dec_out: beam x rnn_size
        inp = inp.unsqueeze(1)

        if args.emb_type:
            curr_idx = i%5
            # this gives the index of the role type: [tup, v, s, o, prep]
            curr_role = roles[curr_idx]
            # wrap into a tensor and make a var. repeat beam times
            inp2 = var(torch.LongTensor([curr_role])).repeat(beam_size, 1)
            logit, hidden = model(inp, hidden, inp2)
        else:
            logit, hidden = model(inp, hidden)

        # [1, beam_size, hidden_size]
        logit = torch.unsqueeze(logit, 0)
        probs = F.log_softmax(logit, dim=2).data
        out = unbottle(probs) # [beam_size, 1, vocab_size]
        out.log()

        # Advance each beam. We have 1 beam object.
        for j, b in enumerate(beam):
            #print("OUT: {}".format(out[:, j])) # [beam_size, vocab_size]
            if use_constraints:
                b.advance(ge.schema_constraint(out[:, j], b.next_ys[-1], verb_list))
            else:
                b.advance(out[:, j])

            beam_update(hidden, j, b.get_current_origin(), beam_size)
            if use_constraints:
                verb_list = ge.update_verb_list(verb_list, b, tup_idx)

    # extract sentences from beam and return
    ret = _from_beam(beam, args.n_best)
    return ret

def _from_beam(beam, n_best=1):
    ret = {"predictions": [],
           "scores": []}
    for b in beam: # Only 1 beam object.
        scores, ks = b.sort_finished(minimum=n_best)
        hyps = []
        for i, (times, k) in enumerate(ks[:n_best]):
            hyp = b.get_hyp(times, k)
            hyps.append(hyp)
        ret["predictions"].append(hyps)
        ret["scores"].append(scores)

    return ret['predictions']

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='PyTorch Wikitext-2 Language Model')

    # Model parameters.
    parser.add_argument('--data', type=str, help='location of the data corpus')
    parser.add_argument('--vocab', type=str)
    parser.add_argument('--model', type=str, help='model checkpoint to use')
    parser.add_argument('--batch_size', type=int, default=1, metavar='N', help='batch size')
    parser.add_argument('--random_seed', type=int, default=11, help='random seed')
    parser.add_argument('--cuda', action='store_true', help='use CUDA')
    parser.add_argument('-max_len_decode', type=int, default=50, help='Maximum prediction length.')
    # beam related
    parser.add_argument('--beam_size',  type=int, default=-1, help='Beam size')
    parser.add_argument('--n_best', type=int, default=1, help="""outputs the n_best decoded sentences""")
    # types of evaluation
    # THESE NARRATIE CLOZE TASKS ARE NOT USED ANYMORE
    #parser.add_argument('--n_cloze', action='store_true', help="""narrative cloze recall based evaluation""")
    #parser.add_argument('--easy', action='store_true', help="""narrative cloze ranking: easy. predict last event.""")
    parser.add_argument('--perplexity',  action='store_true')
    parser.add_argument('--ranking', action='store_true', help="""narrative cloze ranking""")
    parser.add_argument('--seed', action='store_true', help="""seed based testing""")
    # max decoded
    parser.add_argument('--max_decode', type=int, default=2000, help="""max sentences to be evaluated/decoded.""")
    # role data related
    parser.add_argument('--emb_type', action='store_true')
    parser.add_argument('--role_data', type=str, help='location of the role data corpus')
    parser.add_argument('--vocab2', type=str, help='location of role vocab')
    args = parser.parse_args()

    # Set the random seed manually for reproducibility.
    torch.manual_seed(args.random_seed)
    if torch.cuda.is_available():
        if not args.cuda:
            print("WARNING: You have a CUDA device, so you should probably run with --cuda")

    if args.cuda and torch.cuda.is_available():
        device = 0
        use_cuda = True
    elif args.cuda and not torch.cuda.is_available():
        print("You do not have CUDA, turning cuda off")
        device = -1
        use_cuda = False
    else:
        device = -1
        use_cuda=False

    generate(use_cuda=use_cuda, device=device)


from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from Grid import Grid

class GraphicGrid(object):
    colours = ['b', 'g', 'r', 'c', 'm', 'y', 'k']
    colours_redundant = [el + '--' for el in colours]

    def __init__(self, grid=None, union_find = None, **kwargs):
        self.grid = grid
        if grid != None:
            self.info = grid.State_info(grid, union_find)
        if kwargs.has_key('representations'):
            self.representations = kwargs['representations']
        if kwargs.has_key('zlim'):
            self.zlim = kwargs['zlim']
        else:
            self.zlim = None

    def update_frame(self, framedata, patches, lines, redundant_lines,  axes):
        result = self.update_figures(self.representations[framedata], patches, lines, redundant_lines, axes)
        return result

    def update_figures(self, representation, patches, lines, redundant_lines,  helper_axes):
        xes = [-1]*(representation.dimension**2)
        yes = [-1]*(representation.dimension**2)
        zes = [0]*(representation.dimension**2)
        if len(representation.z_s) > 0:
            zes = representation.z_s

        c = [0]*(representation.dimension**2)

        if len(representation.colours) > 0:
            c = representation.colours

        for index in range(0, representation.dimension ** 2):
                    y, x = self.grid.index_map(index)
                    xes[index] = x
                    yes[index] = y

        helper_axes.cla()
        helper_axes.grid(False)
        if self.zlim != None:
            helper_axes.set_zlim3d([0.0, self.zlim])
        patches = helper_axes.scatter(xes, yes, zes, c=c, s=2.8)

        lines = []
        for connection in representation.connections:
                    points = list(connection)

                    x_f, y_f =  points[0]
                    x_s, y_s = points[1]

                    zf_index, zs_index = self.grid.get_index_by_coords(x_f, y_f), self.grid.get_index_by_coords(x_s, y_s)
                    z_f = zes[zf_index]
                    z_s = zes[zs_index]
                    component_num = c[zf_index]
                    colour = GraphicGrid.colours[component_num%len(GraphicGrid.colours)]
                    lines.append(helper_axes.plot([x_f, x_s], [y_f, y_s], [z_f, z_s], colour, linewidth=0.55))

        redundant_lines = []
        for connection in representation.redundant_connections:
                    points = list(connection)

                    x_f, y_f =  points[0]
                    x_s, y_s = points[1]

                    zf_index, zs_index = self.grid.get_index_by_coords(x_f, y_f), self.grid.get_index_by_coords(x_s, y_s)
                    z_f = zes[zf_index]
                    z_s = zes[zs_index]
                    component_num = c[zf_index]
                    colour = GraphicGrid.colours_redundant[component_num%len(GraphicGrid.colours_redundant)]
                    redundant_lines.append(helper_axes.plot([x_f, x_s], [y_f, y_s], [z_f, z_s], colour, linewidth=0.55))
        return patches, lines, redundant_lines

    def drawGrid_alt(self):
        representation = self.info.get_represantation()
        fig = plt.figure(figsize=(16,9), dpi=200)
        ax = plt.axes(projection='3d')
        ax.set_xlabel('X')
        ax.set_ylabel('Y')
        ax.set_zlabel('Z')
        ax.set_title('3D Test')
        patches = ax.scatter(0, 0, 0)
        lines = ax.plot([0, 1], [0, 1], [0, 1], linewidth=0.55)
        redundant = ax.plot([0, 1], [0, 1], [0, 1], linewidth=0.55)
        self.update_figures(representation, patches, lines, redundant, ax)

        plt.show()

gridishe = GraphicGrid(Grid(5), representations=[])

# grid = Grid(5)
# element = 1, 6
# grid.connections.add(element)
# element = 1, 7
# grid.connections.add(element)
# element = 6, 7
# grid.redundant_connections.add(element)
#
# graphical =  GraphicGrid(grid)
# graphical.drawGrid()


import os
from nlplingo.oregon.event_models.uoregon.tools.global_constants import *

def get_rsync_files(path):
    ignore_list = [
        'python/clever/event_models/uoregon/tools/stanford_resources',
        'python/clever/event_models/uoregon/tools/bert_resources',
        'python/clever/event_models/uoregon/datasets',
        'python/clever/event_models/uoregon/models/pipeline/_01/output/predictions',
        'python/clever/event_models/uoregon/logs'
    ]

    for ignore_dir in ignore_list:
        if ignore_dir in path:
            return []

    if os.path.isfile(path):
        return [path]
    else:
        objects = [os.path.join(path, object_name) for object_name in os.listdir(path)]
        subpaths = []
        for obj in objects:
            subpaths += get_rsync_files(obj)
        return subpaths

def create_uo_rsync_list():
    start_dir = './python/clever/'
    all_uo_files = get_rsync_files(start_dir)
    all_uo_files = ['.' + uo_file for uo_file in all_uo_files if '.pyc' != uo_file[-len('.pyc'):]]

    all_uo_files += ['../docker/commands.json']
    with open('./docker/rsync.list', 'w') as f:
        f.write('\n'.join(all_uo_files))

if __name__ == '__main__':
    create_uo_rsync_list()


# string_adv_review.py

# unique_english_letters takes a word as a parameter
# and returns the total number of unique characters,
# uppercase and lowercase characters should be counted
# indivually

letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

def unique_english_letters(word):
    counter = 0
    for letter in letters:
        for char in word:
            compare_str = ""
            if (len(compare_str) > 0):
                for sub in compare_str:
                    if(char != sub):
                        counter += 1
                        compare_str += char
                    else:
                        continue
            elif(char == letter):
                counter += 1
                compare_str += char
                break

    return counter

print(unique_english_letters("mississippi"))

# Count X

def count_char_x(word, x):
    count = 0
    for letter in word:
        if (letter == x):
            count += 1
    return count

print(count_char_x("mississippi", "m"))

# Count Multi X

def count_multi_char_x(word, x):
    occurances = word.split(x)
    return (len(occurances) - 1)

print(count_multi_char_x("Mississippi", "iss"))

# Substring Between

def substring_between_letters(word, start, end):
    if(word.find(start) != -1 and word.find(end) != -1):
        word_index = list(word)
        word_final = ""
        for i in range(len(word_index)):
            if (i > word.find(start) and i < word.find(end)):
                word_final += word_index[i]
        return word_final
    else:
        return word

print(substring_between_letters("apple", "p", "e"))

# X String

def x_length_words(sentence, x):
    sentence_list = sentence.split()
    for word in sentence_list:
        if (len(word) < x):
            return False
    return True

print(x_length_words("he like apples", 2))


import pwd
import subprocess

from datetime import datetime, timedelta

import iso8601  # https://bitbucket.org/micktwomey/pyiso8601
from enum import Enum  # https://pypi.python.org/pypi/enum34

QuotaType = Enum('block', 'inode')
QuotaState = Enum('no_quota', 'under_quota', 'soft_limit', 'hard_limit', 'grace_expired')

def parse_datetime(field):
    """Parses an ISO-8601-formatted datetime.  Returns None for None."""
    if field:
        return iso8601.parse_date(field).replace(tzinfo=None)
    else:
        return None

def list_quota_filesystems():
    filesystems = set()
    mtab = open('/etc/mtab')
    try:
        for line in mtab:
            dev, mountpoint, fstype, options, dump, fsck = line.split()
            if 'usrquota' in options.split(','):
                filesystems.add(mountpoint)
    finally:
        mtab.close()
    return filesystems

class QuotaInfo:
    """Information about a specific type of quota (block or inode) for a
    specific account on a specific filesystem.

    Attributes:
        uid         The UID of the account to which this quota applies.
        filesystem  The filesystem to which this quota applies.
        quota_type  A member of the QuotaType enum indicating whether this is a
                    block quota or inode quota.
        used        The number of resources (blocks or inodes) currently in use.
        soft_limit  The soft limit for this resource.
        hard_limit  The hard limit for this resource.
        grace_expires  A datetime indicating when the grace period for exceeding
                       the soft limit will expire.  If the soft limit is not
                       exceeded, the value of this attribute will be None.
        last_notify_date  A datetime indicating the date and time when the
                          account owner was last contacted about their quota
                          usage.
        last_notify_state  A member of the QuotaState enum indicating the state
                           of the account's quota at the time given by
                           last_notify_date.
        current_state  A member of the QuotaState enum indicating the current
                       state of this quota.
        bytes_used  For block quotas, the number of bytes currently in use.
        byte_soft_limit  For block quotas, the soft limit in bytes.
        byte_hard_limit  For block quotas, the hard limit in bytes.
    """

    def __init__(self, uid, filesystem, quota_type, db_cursor):
        self.uid = uid
        self.filesystem = filesystem
        self.quota_type = quota_type
        self.db_cursor = db_cursor
        self.refresh()

    def __repr__(self):
        return '<QuotaInfo(%s, %s, %s): %s %s/%s/%s>' % (self.uid, self.filesystem, self.quota_type.key, self.current_state.key, self.used, self.soft_limit, self.hard_limit)

    def refresh(self):
        self.db_cursor.execute('SELECT * FROM entry WHERE uid = ? AND filesystem = ? AND quota_type = ?',
                               (self.uid, self.filesystem, self.quota_type.index))
        # RHEL 5's pysqlite is too old to have indexing by field name, so we
        # have to fake it.
        indices = {}
        for i in xrange(0, len(self.db_cursor.description)):
            indices[self.db_cursor.description[i][0]] = i

        row = self.db_cursor.fetchone()
        if row:
            self.used = row[indices['used']]
            self.soft_limit = row[indices['soft_limit']]
            self.hard_limit = row[indices['hard_limit']]
            self.grace_expires = parse_datetime(row[indices['grace_expires']])
            self.last_notify_date = parse_datetime(row[indices['last_notify_date']])
            last_notify_state = row[indices['last_notify_state']]
            if last_notify_state:
                self.last_notify_state = QuotaState[last_notify_state]
            else:
                # Pretend everything's okay.
                self.last_notify_state = QuotaState.under_quota
        else:
            self.used = None
            self.soft_limit = None
            self.hard_limit = None
            self.grace_expires = None
            self.last_notify_date = None
            self.last_notify_state = None

    def set_from_quotatool(self, used, soft_limit, hard_limit, grace):
        """Update this object's quota information from the strings parsed from
        quotatool's output."""
        self.used = int(used)
        self.soft_limit = int(soft_limit)
        self.hard_limit = int(hard_limit)
        if int(grace) == 0:
            self.grace_expires = None
        else:
            self.grace_expires = \
                datetime.now().replace(microsecond=0) + timedelta(seconds=int(grace))

    @property
    def current_state(self):
        if self.soft_limit == 0:
            return QuotaState.no_quota
        if self.used < self.soft_limit:
            return QuotaState.under_quota
        if self.used < self.hard_limit and datetime.now() < self.grace_expires:
            return QuotaState.soft_limit
        if self.used >= self.hard_limit:
            return QuotaState.hard_limit
        return QuotaState.grace_expired

    @property
    def bytes_used(self):
        return self.used * 1024

    @property
    def byte_soft_limit(self):
        return self.soft_limit * 1024

    @property
    def byte_hard_limit(self):
        return self.hard_limit * 1024

    def update(self):
        self.db_cursor.execute(
            """UPDATE entry
                 SET used = ?,
                     soft_limit = ?,
                     hard_limit = ?,
                     grace_expires = ?,
                     last_notify_date = ?,
                     last_notify_state = ?,
                     last_update = ?
                 WHERE uid = ?
                   AND filesystem = ?
                   AND quota_type = ?""",
            (self.used, self.soft_limit, self.hard_limit,
             self.grace_expires and self.grace_expires.isoformat(),
             self.last_notify_date and self.last_notify_date.isoformat(),
             self.last_notify_state and self.last_notify_state.index,
             datetime.now().isoformat(),
             self.uid, self.filesystem, self.quota_type.index))
        if self.db_cursor.rowcount == 0:
            self.db_cursor.execute(
                """INSERT INTO entry
                     (used, soft_limit, hard_limit, grace_expires,
                      last_notify_date, last_notify_state, last_update,
                      uid, filesystem, quota_type)
                     VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
                (self.used, self.soft_limit, self.hard_limit,
                 self.grace_expires and self.grace_expires.isoformat(),
                 self.last_notify_date and self.last_notify_date.isoformat(),
                 self.last_notify_state and self.last_notify_state.index,
                 datetime.now().isoformat(),
                 self.uid, self.filesystem, self.quota_type.index))

    def set_notify(self):
        """Update this object with a new notification date."""
        self.last_notify_state = self.current_state
        self.last_notify_date = datetime.now()

class AccountInfo:
    """Quota information about a particular account.

    Attributes:
        uid     The UID of the account.
        quotas  A nested dictionary of QuotaInfo objects.  The first set of keys
                are the filesystems for which quota information exists.  The
                second set of keys are the values of the QuotaType enum.  Thus,
                to access the block quotas for the /home filesystem, you would
                use `obj.quotas['/home'][QuotaType.block]`.
    """

    @staticmethod
    def all(db_cursor):
        """Generator that yields all entries in the cache."""
        db_cursor.execute('SELECT DISTINCT uid FROM entry')
        results = db_cursor.fetchall()
        for row in results:
            yield AccountInfo(row[0], db_cursor)

    def __init__(self, uid, db_cursor):
        self.uid = uid
        self.db_cursor = db_cursor

        self.refresh_from_db()

    @property
    def username(self):
        try:
            return pwd.getpwuid(self.uid).pw_name
        except KeyError:
            return '#%d' % self.uid

    @property
    def iter_quotas(self):
        """Generator to iterate through all quota objects in a flat manner."""
        for filesystem in sorted(self.quotas.keys()):
            for area in QuotaType:
                yield self.quotas[filesystem][area]

    def refresh_from_db(self):
        self.quotas = {}
        self.db_cursor.execute(
            'SELECT DISTINCT filesystem FROM entry WHERE uid = ?', (self.uid,))
        rows = self.db_cursor.fetchall()
        for row in rows:
            self.quotas[row[0]] = {}
            self.quotas[row[0]][QuotaType.block] = QuotaInfo(self.uid, row[0], QuotaType.block, self.db_cursor)
            self.quotas[row[0]][QuotaType.inode] = QuotaInfo(self.uid, row[0], QuotaType.inode, self.db_cursor)

    def refresh_from_system(self):
        self.quotas = {}
        for filesystem in list_quota_filesystems():
            self.quotas[filesystem] = {}

            bqi = QuotaInfo(self.uid, filesystem, QuotaType.block, self.db_cursor)
            iqi = QuotaInfo(self.uid, filesystem, QuotaType.inode, self.db_cursor)

            qt = subprocess.Popen(['quotatool', '-u', str(self.uid),
                                   '-d', filesystem],
                                  stdout=subprocess.PIPE)
            stdout, stderr = qt.communicate()
            qt_uid, qt_fs, \
                blocks_used, block_quota, block_limit, block_grace, \
                inodes_used, inode_quota, inode_limit, inode_grace = \
                stdout.split()

            bqi.set_from_quotatool(blocks_used, block_quota, block_limit, block_grace)
            iqi.set_from_quotatool(inodes_used, inode_quota, inode_limit, inode_grace)

            self.quotas[filesystem][QuotaType.block] = bqi
            self.quotas[filesystem][QuotaType.inode] = iqi

    def update(self):
        """Update the cache file with the current object's information."""
        for quota in self.iter_quotas:
            quota.update()

    def set_notify(self, quotas_notified):
        """Update all supplied quotas with a new notification date."""
        self.refresh_from_db()
        for q in quotas_notified:
            self.quotas[q.filesystem][q.quota_type].set_notify()
        self.update()


import serial
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
import datetime as dt

fig=plt.figure()
axl=fig.add_subplot(1,1,1)

ser=serial.Serial("COM4",115200)
beat=[]
avg_beat=[]
xs=[]
ys=[]
y_range=[30,150]

def mov_avg (mylist):
    N = 3
    cumsum, moving_aves = [0], []

    for i, x in enumerate(mylist, 1):
        cumsum.append(cumsum[i-1] + x)
        if i>=N:
            moving_ave = (cumsum[i] - cumsum[i-N])/N
            if (moving_ave)<140:
                moving_aves.append(moving_ave)
    return moving_aves

while 1:
    while(ser.inWaiting() >0):
        def animate(i, xs, ys):
            # Read temperature (Celsius) from TMP102
            #temp_c = round(tmp102.read_temp(), 2)

            dat= round(int(ser.readline()),2)

            beat.append(dat)
            if len(beat)>15:
                beat.pop(0)

            # Add x and y to lists
            xs.append(dt.datetime.now().strftime('%H:%M:%S.%f'))
            ys.append(dat)

            # Limit x and y lists to 20 items
            xs = xs[-20:]
            ys = ys[-20:]

            # Draw x and y lists
            axl.clear()
            axl.plot(xs, ys)

            # Format plot
            plt.xticks(rotation=45, ha='right')
            plt.subplots_adjust(bottom=0.30)
            plt.title('Heart pulse rate moniter')
            plt.ylabel('Heart Pulse')
            axl.set_ylim(y_range)

            print(beat)
            m_beat=mov_avg(mov_avg(beat))

            print (m_beat)

            for i in m_beat:
                if (i>m_beat[(m_beat.index(i))-1] and i>120):
                    plt.text(4,3,"drowsy pulse region",ha='left',fontsize=15,wrap=True)
                    print ('drowsy')
        ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys), interval=1000)
        plt.show()

        ##        ab= sum(beat)/len(beat)
        ##        avg_beat.append(ab)
        ##        for i in avg_beat:
        ##            if i>avg_beat[avg_beat.index(i)-1] and i>135:
        ##                print ('drowsy')


import requests
from random import randint
class Person:

    def __init__(self,imya="", falimia="", login="", parol=""):
        self.__data = requests.get("https://api.randomdatatools.ru/").json()
        self.__lorem="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris tempor dictum quam, ut tincidunt purus vestibulum sed. Proin mattis hendrerit pharetra. Cras fermentum est magna, ac congue enim bibendum in. Curabitur luctus fermentum lacus, ac laoreet augue tempus a. Sed tempus vulputate ornare. Phasellus accumsan volutpat urna sed pretium. Mauris."
        self.imya = self.__data['FirstName'] if not imya else imya
        #  имя = случайному если атрибут =""(пустое if not) иначе передаваем
        self.falimia = self.__data['LastName']if not falimia else falimia
        self.login = self.__data['Login']if not login else login
        self.__parol = self.__data['FirstName']if not parol else parol
        self.podpiski = []
        self.podpiskishiki = []
        self.posts = [self.__lorem[randint(1,20):randint(20,40)] for i in range(1, 10)]

    def log_in(self, parol, login):
        if self.login == login and self.__parol == parol:
            return True
        else:
            return False


import fcsparser
import numpy as np
from numpy import genfromtxt
from auto_encoder_updated import file_io as io
import os.path
import sklearn.preprocessing as prep
from sklearn.model_selection import train_test_split

class Sample:
    x = None
    y = None

    def __init__(self, x, y=None):
        self.x = x
        self.y = y

def preprocess_samples(sample):
    sample.x = np.log(1 + np.abs(sample.x))
    return sample

def standard_scale(sample, preprocessor=None):
    if preprocessor is None:
        preprocessor = prep.StandardScaler().fit(sample.x)
    sample.x = preprocessor.transform(sample.x)
    return sample, preprocessor

def load_data(data_path, data_index, relevant_markers, mode, skip_header=0):
    if mode == 'CSV':
        data_filename = data_path + '/sample' + str(data_index) + '.csv'
        x = genfromtxt(os.path.join(io.deep_learning_root(), data_filename), delimiter=',', skip_header=skip_header)
    if mode == 'FCS':
        files = [file for file in os.listdir(data_path) if '.fcs' in file]
        data_filename = os.path.join(data_path, files[data_index])
        _, x = fcsparser.parse(os.path.join(io.deep_learning_root(), data_filename), reformat_meta=True)
        x = x.as_matrix()
    x = x[:, relevant_markers]
    #label_filename = data_path + '/labels' + str(data_index) + '.csv'
    #labels = genfromtxt(os.path.join(io.deep_learning_root(), label_filename), delimiter=',')
    #labels = np.int_(labels)
    sample = Sample(x)

    return sample

def load_data_tsv(data_path, data_index, relevant_markers, skip_header=0):
    print('Loading data from .tsv')
    files = [os.path.join(data_path, file) for file in os.listdir(data_path) if '.tsv' in file]
    samples = []
    for i in data_index:
        x = genfromtxt(os.path.join(io.deep_learning_root(), files[i]), delimiter='\t', skip_header=skip_header)
        x = x[:, relevant_markers]
        sample = Sample(x)
        samples.append(sample)
    return samples

def split_data(sample, test_size):
    data_train, data_test, label_train, label_test = train_test_split(sample.x, sample.y, test_size=test_size)

    train_sample = Sample(data_train, label_train)
    test_sample = Sample(data_test, label_test)
    return train_sample, test_sample

def choose_reference_sample(data_path, relevant_markers, mode):
    samples = load_data(data_path, relevant_markers, mode)
    samples = [preprocess_samples(sample) for sample in samples]

    num_samples = len(samples)
    norms = np.zeros(shape=[num_samples, num_samples])
    for i in range(num_samples):
        cov_i = np.cov(samples[i].x, rowvar=False)
        for j in range(num_samples):
            cov_j = np.cov(samples[j].x, rowvar=False)
            cov_diff = cov_i - cov_j
            norms[i, j] = np.linalg.norm(cov_diff, ord='fro')
            norms[j, i] = norms[i, j]
            avg = np.mean(norms, axis=1)
            ref_sample_ind = np.argmin(avg)
    return ref_sample_ind


import random
import time

summa_hum = 0   # ход игрока
print("Your turn")
for i in range(3):
    k1 = random.randint(1, 6)
    print("Выпало", k1)
    summa_hum += k1
    time.sleep(0.5)
print("Total score", summa_hum)

summa_comp = 0  # ход компа
print("Comp turn")
for i in range(3):
    k2 = random.randint(1, 6)
    print("Выпало", k2)
    summa_comp += k2
    time.sleep(0.5)
print("Total score", summa_comp)

if (summa_hum > summa_comp) and (summa_hum != 18) or (summa_comp == 18):  # кто победил
    print("You win!")
elif summa_hum < summa_comp:
    print("Comp win!")
else:
    print("Draw")

Exercises/Игра в кости с компом.py

Игра в кости с компом.py


import math

area = float(input('Insira a área a ser pintada (m²): '))

# 1L de tinta pinta 3m², portanto, 18L (1 lata de tinta) cobre 54m².
volume_tinta = area / 54
quantidade_latas = math.ceil(volume_tinta)

custo = quantidade_latas * 80

print(f'Serão necessárias {quantidade_latas} latas de tinta no valor de R$ 80.00 cada.')
print(f'O total a ser pago é de R$ {custo:.2f}.')


import datetime
import json
import os

import pandas as pd
import twitter

def create_twitter_api():
    """Check environment and return appropriate twitter API"""

    # API CREDENTIALS
    path = "twitter-credentials.json"
    # if running from local machine
    if os.path.exists(path):
        with open(path, "r") as file:
            creds = json.load(file)
        CONSUMER_KEY = creds['CONSUMER_KEY']
        CONSUMER_SECRET = creds['CONSUMER_SECRET']
        ACCESS_KEY = creds['ACCESS_TOKEN']
        ACCESS_SECRET = creds['ACCESS_SECRET']
    # if running from Heroku
    else:
        CONSUMER_KEY = os.environ['CONSUMER_KEY']
        CONSUMER_SECRET = os.environ['CONSUMER_SECRET']
        ACCESS_KEY = os.environ['ACCESS_TOKEN']
        ACCESS_SECRET = os.environ['ACCESS_SECRET']
    api = twitter.Api(consumer_key=CONSUMER_KEY,
                      consumer_secret=CONSUMER_SECRET,
                      access_token_key=ACCESS_KEY,
                      access_token_secret=ACCESS_SECRET,
                      tweet_mode='extended',
                      sleep_on_rate_limit=True)

    return api

def fix_dates(df):
    """fixes dates for twitter dataframe"""
    df['date_time'] = pd.to_datetime(df['created_at'])
    df['date'] = pd.to_datetime(df['date_time'].dt.date)

    def find_monday(d):
        """Receives a date and returns the date of the preceding Monday."""
        while d.weekday() != 0:
            d += datetime.timedelta(-1)
        return d
    df['date_week'] = df['date'].apply(find_monday)
    return df

def load_tweets(api, user_name, num, n_max_id=0):
    """
    Get raw data from twitter. See api.GetUserTimeline
    https://developer.twitter.com/en/docs/tweets/timelines/api-reference/get-statuses-user_timeline.html

    Paramaters
    ----------
    api : twitter.Api
        Twitter api class
    user_name : str
        A twitter handle. Do not include the "@"
    num : int
        The number of tweets to get (max 200)
    n_max_id : int
        Returns only statuses with an ID less than (that is, older than) or
        qual to the specified ID. By default, 0.

    Returns
    -------
    twitter_data
    """
    raw = api.GetUserTimeline(
        screen_name=user_name,
        count=num,
        exclude_replies=True,
        include_rts=False,
        trim_user=True,
        max_id=n_max_id
    )
    return raw

def tweets_get(user_name, num=200, start_date=datetime.date(2019, 9, 11)):
    '''
    Gets tweets and returns a DataFrame.

    Parameters
    ----------
    user_name : str
        The user you would like to get twitter data from (e.g. "JustinTrudeau")
    num : int
        The number of tweets you would like to return (max is 200)
    loops: int
        Because the max number of tweets is only 200, loops will indicate how many sets of "num" tweets you would like to return.
        For example num=200 and loops=2 would get 400 tweets

    Returns
    -------
    Dataframe with twitter data
    '''

    api = create_twitter_api()

    # get the first batch of twitter data
    print(f"Getting tweets for: {user_name}...")
    raw = load_tweets(api=api, user_name=user_name, num=num)
    df = pd.DataFrame.from_dict([i.AsDict() for i in raw])
    df = fix_dates(df)

    # loop through the dataframe x number of times based on the smallest id
    # from the dataframe
    max_id = df['id'].min()-1
    min_date = df['date'].min()
    while min_date > start_date:
        raw = load_tweets(
            api=api, user_name=user_name, num=num, n_max_id=max_id
        )
        temp_df = pd.DataFrame.from_dict([i.AsDict() for i in raw])
        try:
            temp_df = fix_dates(temp_df)
        except KeyError:
            print(f"\terror fixing dates for {user_name}...")
            return df
        df = pd.concat([df, temp_df], sort=False)
        max_id = df['id'].min()-1
        min_date = df['date'].min()

    df = df[df['date'] >= pd.to_datetime(start_date)]
    df = df[df['lang'] == 'en']  # keep only english langauge tweets
    return df


import os
import sys
import time
import json
import threading

class GitManager:
    def __init__(self, __dir_path=os.path.dirname(os.path.realpath(__file__))):
        self.__dir_path = __dir_path
        self.__folder_list = os.listdir(self.__dir_path)
        self.__repositories_list_name = "repositoriy_list.txt"
        self.__git_url = "ssh://qin@cq.qinyupeng.com:10022/Repositories/qin/"
        self._list_repositories()

    def _push(self):
        for folder_name in self.__folder_list:
            if os.path.isdir(self.__dir_path+'/'+folder_name):
                os.chdir(self.__dir_path+'/'+folder_name)
                print(f"Project:{'['+folder_name}]")
                os.system("pwd")
                os.system("git init")
                os.system(
                    f"git remote add cqhome {self.__git_url}/"+folder_name+".git")
                os.system("git add .")
                os.system("git commit -m \"new update\"")
                os.system("git push --set-upstream cqhome master")
                os.chdir(self.__dir_path)
                os.system("pwd")
                print("updated repositoriy:"+folder_name)

    def _git_list(self):
        repositoriy_list = []
        exist_repositoriy_list = []
        with open(f"{self.__dir_path}/repositoriy_list.txt", "r") as f:
            repositoriy_list = f.readlines()
        exist_repositoriy_list = os.listdir(self.__dir_path)
        for folder_name in exist_repositoriy_list:
            if folder_name.find(".") != -1:
                exist_repositoriy_list.remove(folder_name)
        new_repositoriy_list = list(
            set(repositoriy_list+exist_repositoriy_list))
        print(str(new_repositoriy_list))
        with open(f"{self.__dir_path}/repositoriy_list.txt", "w+") as f:
            f.seek(0)
            for line in new_repositoriy_list:
                if line != "\n" and line != "repositoriy_list.txt":
                    f.write(str(line))

    def _pull(self):
        with open(f"{self.__dir_path}/{self.__repositories_list_name}") as f:
            repositoriy_list = f.readlines()
        # folder_list = os.listdir(repositoriy_list)
        for repositoriy in repositoriy_list:
                repositoriy = repositoriy.replace("\n", "")
                # os.chdir(self.__dir_path+'/'+folder_name)
                print(f"[_pull]pulling[{repositoriy}]")
                # os.system("git init")
                os.system(f"git clone {self.__git_url}" +
                          repositoriy+".git")
                # os.system("git add .")
                # os.system("git commit -m \"new update\"")
                # os.system("git push git")
                # os.chdir(self.__dir_path)
                print("[_pull]Updated repositoriy:"+repositoriy)

    def _list_repositories(self):
        folder_list = []
        if os.path.isfile(f"{self.__dir_path}/{self.__repositories_list_name}"):
            with open(f"{self.__dir_path}/{self.__repositories_list_name}", "r") as f:
                folder_list = f.readlines()
        with open(f"{self.__dir_path}/{self.__repositories_list_name}", "a") as f:
            for folder_name in os.listdir(self.__dir_path):
                if os.path.isdir(self.__dir_path+'/'+folder_name):
                    if folder_name+"\n" not in folder_list:
                        f.write(folder_name+"\n")

if __name__ == '__main__':
    gm = GitManager()
    gm = gm._pull()
    # gm._push()
    # gm._list_repositories()


class ColoringProblem:
    def __init__(self, adj, N, C):
        self.adj = adj  # Adjecency matrix
        self.N = N  # nodes
        self.C = C  # colors

        # Colors assigned to each node
        self.colors = [None for x in range(N)]

    def solveProblem(self):
        # First node is colored 0
        self.colors[0] = 0

        if self.solve(1):
            self.printSolution()
        else:
            print("No feasible solution is found 💥")

    def printSolution(self):
      colorMap = {
        0: "❤",
        1: "🧡",
        2: "💛",
        3: "💚",
        4: "💙",
      }

      for idx, c in enumerate(self.colors):
        print(f"Node: {idx} Color: {colorMap[c]}")

    # Node index is valid
    def isValid(self, nodeIndex):
        return nodeIndex > 0 and nodeIndex < self.N

    # Feasible to color node index with color ?

    def isFeasible(self, nodeIndex, colorIndex):
        # Return False if any adjacent node has the same color
        for adjNodeIndex in range(self.N):
            # Same Color
            if self.isConnected(nodeIndex, adjNodeIndex) and self.colors[adjNodeIndex] == colorIndex:
                return False

        return True

    def isConnected(self, first, second):
        return adj[first][second] is 1

    def solve(self, nodeIndex):

        # Base case
        # All nodes are colored
        if (nodeIndex == self.N):
            return True

        for colorIndex in range(self.C):
            if self.isFeasible(nodeIndex, colorIndex):
                self.colors[nodeIndex] = colorIndex

                # Solve subproblem
                if self.solve(nodeIndex + 1):
                    return True

            # Backtrack
            self.colors[nodeIndex] = None

if __name__ == "__main__":
    # Adjacency matrix
    adj = [[0, 1, 1, 1, 0],
           [1, 0, 1, 0, 1],
           [1, 1, 0, 1, 0],
           [1, 1, 1, 0, 1],
           [0, 0, 0, 1, 0]
           ]

    # Try to solve with 3 colors
    coloringProblem = ColoringProblem(adj, 5, 3)
    coloringProblem.solveProblem()

    # Try to solve with 4 colors
    coloringProblem = ColoringProblem(adj, 5, 4)
    coloringProblem.solveProblem()


def calculate_average(list_of_numbers):
    sum_of_numbers=0
    length_of_list=len(list_of_numbers)
    for r in range(length_of_list):
        sum_of_numbers=sum_of_numbers+list_of_numbers[r]
    mean_of_list=sum_of_numbers/length_of_list
    print('The average is {}.'.format(mean_of_list))

def concatenate_strings(first, last):
      return first+" "+last

def check_if_list_contains_item(list_of_things, item):
    second_list=[]
    for i in range(len(list_of_things)):
        if list_of_things[i]==item:
            second_list.append("Yes")
        else:
            second_list.append("No")
    return second_list


from utils import *

def read_data(filename):
    with open(filename, 'r') as f:
        data = f.read()

    return [
        (cmd, int(value))
        for cmd, value in map(str.split, data.strip().splitlines())
    ]

def task1(filename):
    depth = 0
    x = 0
    commands = read_data(filename)
    for cmd, value in commands:
        if cmd == 'forward':
            x += value
        elif cmd == 'up':
            depth -= value
            depth = max(depth, 0)
        elif cmd == 'down':
            depth += value
    return x * depth

def task2(filename):
    depth = 0
    aim = 0
    x = 0
    commands = read_data(filename)
    for cmd, value in commands:
        if cmd == 'forward':
            x += value
            depth += aim * value
        elif cmd == 'up':
            aim -= value
        elif cmd == 'down':
            aim += value
    return x * depth

assert task1('test.txt') == 150
assert task1('data.txt') == 1690020
assert task2('test.txt') == 900
assert task2('data.txt') == 1408487760


#!/usr/bin/env python2.7

import sys
import os
from CParser import CParser

LIBNAME = 'example'

# Correlation table for direct types
cor =    {
    'bool': 'ctypes.c_bool',
    'char': 'ctypes.c_char',
    'byte': 'ctypes.c_byte',
    'unsigned byte': 'ctypes.c_ubyte',
    'short': 'ctypes.c_short',
    'unsigned short': 'ctypes.c_ushort',
    'int': 'ctypes.c_int',
    'unsigned int': 'ctypes.c_uint',
    'long': 'ctypes.c_long',
    'unsigned long': 'ctypes.c_ulong',
    'long long': 'ctypes.c_longlong',
    'unsigned long long': 'ctypes.c_ulonglong',
    'float': 'ctypes.c_float',
    'double': 'ctypes.c_double',
    'void': 'None',

    'int8_t': 'ctypes.c_int8',
    'int16_t': 'ctypes.c_int16',
    'int32_t': 'ctypes.c_int32',
    'int64_t': 'ctypes.c_int64',

    'uint8_t': 'ctypes.c_uint8',
    'uint16_t': 'ctypes.c_uint16',
    'uint32_t': 'ctypes.c_uint32',
    'uint64_t': 'ctypes.c_uint64',

    'size_t': 'ctypes.c_int',
    'sem_t': 'ctypes.c_int',
    }

# Correlation table for pointer types
corp =    {
    'char': 'ctypes.c_char_p',
    'void': 'ctypes.c_void_p',
    }

def getCor(member_type):
    if '*' in member_type:
        try:
            pytype = corp[member_type[0]]
        except KeyError:
            pytype = 'ctypes.c_void_p'
    else:
        try:
            pytype = cor[member_type[0]]
        except KeyError:
            pytype = 'ctypes.c_void_p'

    return pytype

def getMult(member_type):
    try:
        if member_type[2] is not None:
            value = member_type[2][0]
            return str(value)
        else:
            return None
    except Exception:
        return None

########################### STRUCTURES #########################################
def doStruct(struct, name):
    print('class ' + name + '(Structure):')
    print('_fields_ = [')
    for member in struct['members']:
        pytype = getCor(member[1])
        mult = getMult(member[1])
        if mult is not None:
            print('\t("' + member[0] + '", ' + pytype + ' * ' + mult + '),')
        else:
            print('\t("' + member[0] + '", ' + pytype + '),')
    print('\t]\n')

def doStructs(structs):
    for struct in structs:
        doStruct(structs[struct], struct)

########################### FUNCTIONS ##########################################
def getArgs(arguments):
    pyargs = []

    for argument in arguments:
        pyargs.append(getCor(argument[1]))

    return pyargs

def doArgs(arguments):
    pyargs = getArgs(arguments)
    print('['),
    for pyarg in pyargs:
        print(pyarg + ',' ),
    print(']')

def doFunction(function, name):
    print('self.' + name + ' = ' + 'self._lib.' + name)
    print('self.' + name + '.argtypes = '),
    doArgs(function[1])
    print('self.' + name + '.restype = ' + getCor(function[0]))
    print

def doFunctions(functions):
    for function in functions:
        doFunction(functions[function], function)

########################### TYPES ##############################################
def doType(c_type):
    cor[c_type] = c_type
    corp[c_type] = 'POINTER(' + c_type + ')'

def doTypes(c_types):
    print(c_types)
    for c_type in c_types:
        doType(c_type)

################################################################################
if __name__ == '__main__':
    cheader = sys.argv[1]
    p = CParser(cheader)
    p.processAll()

    for k in p.dataList:
        if bool(p.defs[k]):
            if k is 'types':
                doTypes(p.defs[k])
            if k is 'structs':
                doStructs(p.defs[k])
            if k is 'functions':
                doFunctions(p.defs[k])


from numba import jit
from jit_talib import jit_ma
import numpy as np
from datetime import datetime

@jit(nopython=True)
def jit_ttm_strategy(tick, chart, position, global_data):
    now_pos = global_data['chart_pos']
    open_volume = 0.05
    open_array, high_array, low_array, close_array = chart[1][:
                                                              ], chart[2][:], chart[3][:], chart[4][:]
    ret_signal = np.zeros((4))
    #signal = None
    hp = -9999
    pos = position
    if tick[0] < 1e-5:
        return ret_signal
    if pos['status'] > 0:  # holding
        direction = pos['direction']
        op = pos['avg_price']
        index = now_pos
        hp = direction * (close_array[index] - op)
        [para1, para2, para3, para4, para5, atr_stop_gain] = [0.2, 0.55, 0.33, 90, 90, 3]
        para30 = 30
        if 0:
            print(datetime.now().strftime('%H:%M:%S'), ' hp / op %.5f' % (hp / op), 'open_price:', op, 'last:',
                  close_array[index])
        if now_pos < 1440 + para30:
            return ret_signal
        open_distance = int((tick[0] - pos['entry_time']) / 60)
        peak_price = np.max(high_array[now_pos - open_distance: now_pos + 1]) if direction > 0 else np.min(
           high_array[now_pos - open_distance: now_pos + 1])

        last_30_index = int(now_pos / para30) * para30
        min_30_high = np.max(high_array[last_30_index - para30:last_30_index])
        min_30_low = np.min(low_array[last_30_index - para30:last_30_index])
        min_30_close = close_array[last_30_index - 1]
        Ssetup = min_30_high + para1 * (min_30_close - min_30_low)
        Bsetup = min_30_low - para1 * (min_30_high - min_30_close)
        Senter = (1 + para3) / 2 * (min_30_high +
                                    min_30_low) - para3 * min_30_low
        Benter = (1 + para3) / 2 * (min_30_high +
                                    min_30_low) - para3 * min_30_high
        Sbreak = Bsetup - para2 * (Ssetup - Bsetup)
        Bbreak = Ssetup + para2 * (Ssetup - Bsetup)

        #long stop gain
        if direction > 0 and close_array[now_pos] > op + direction * chart[9][now_pos-1] * atr_stop_gain:# and hp/op > 0.01:
            ret_signal[0], ret_signal[1], ret_signal[2], ret_signal[3] = - \
                                                                             1, (-1) * direction, pos['volume'], \
                                                                         close_array[now_pos]
            return ret_signal

        if direction < 0 and close_array[now_pos] < op + direction * chart[9][now_pos-1] * atr_stop_gain:# and hp/op > 0.01:
            ret_signal[0], ret_signal[1], ret_signal[2], ret_signal[3] = - \
                                                                             1, (-1) * direction, pos['volume'], \
                                                                         close_array[now_pos]
            return ret_signal

        # if direction > 0 and close_array[now_pos] < peak_price - chart[9][now_pos - 1] * atr_stop_gain:
        #     ret_signal[0], ret_signal[1], ret_signal[2], ret_signal[3] = - \
        #                                                                          1, (-1) * direction, pos['volume'], \
        #                                                                      close_array[now_pos]
        #     return ret_signal
        #
        # if direction < 0 and close_array[now_pos] > peak_price + chart[9][now_pos - 1] * atr_stop_gain:
        #     ret_signal[0], ret_signal[1], ret_signal[2], ret_signal[3] = - \
        #                                                                          1, (-1) * direction, pos['volume'], \
        #                                                                      close_array[now_pos]
        #     return ret_signal

        if direction < 0 and close_array[now_pos] > np.max(high_array[now_pos-60:now_pos]):# and hp/op < -0.005:
            ret_signal[0], ret_signal[1], ret_signal[2], ret_signal[3] = - \
                                                                             1, (-1) * direction, pos['volume'], \
                                                                         close_array[now_pos]
            return ret_signal

        if direction > 0 and close_array[now_pos] < np.min(low_array[now_pos-60:now_pos]):# and hp/op < -0.005:
            ret_signal[0], ret_signal[1], ret_signal[2], ret_signal[3] = - \
                                                                             1, (-1) * direction, pos['volume'], \
                                                                         close_array[now_pos]
            return ret_signal

        # if  hp / op < -0.01:
        #     ret_signal[0], ret_signal[1], ret_signal[2], ret_signal[3] = - \
        #         1, (-1) * direction, pos['volume'], close_array[now_pos]
        #     return ret_signal
        #
        if (tick[0] - pos['entry_time'] >= 60 * 60):
            ret_signal[0], ret_signal[1], ret_signal[2], ret_signal[3] = - \
                1, (-1) * direction, pos['volume'], close_array[now_pos]
            return ret_signal

    else:
        if now_pos < 1440 + 100:
            return ret_signal
        k_center = chart[10][:]
        k_low_now = k_center[now_pos] - 1.5 * chart[9][now_pos - 1]
        k_high_now = k_center[now_pos] + 1.5 * chart[9][now_pos - 1]
        k_low_last = k_center[now_pos - 1] - 1.5 * chart[9][now_pos - 2]
        k_high_last = k_center[now_pos - 1] + 1.5 * chart[9][now_pos - 2]
        b_center = k_center
        b_std_now = np.std(close_array[now_pos - 19:now_pos+1])
        b_std_last = np.std(close_array[now_pos - 20:now_pos])
        b_high_now = b_center[now_pos] + 2 * b_std_now
        b_low_now = b_center[now_pos] - 2 * b_std_now
        b_high_last = b_center[now_pos - 1] + 2 * b_std_last
        b_low_last = b_center[now_pos - 1] - 2 * b_std_last
        if b_high_now > k_high_now and b_high_last < k_high_last and close_array[now_pos] > close_array[now_pos - 5]:
            ret_signal[0], ret_signal[1], ret_signal[2], ret_signal[3] = 1, 1, open_volume, \
                                                                         tick[1]
        if b_low_now < k_low_now and b_low_last > k_low_last and close_array[now_pos] < close_array[now_pos - 5]:
            ret_signal[0], ret_signal[1], ret_signal[2], ret_signal[3] = 1, -1, open_volume, \
                                                                         tick[1]

    return ret_signal


from os import system

def get_menus(title, menus):
    res = -1
    while res == -1:
        # if clear_:
        #     clear()
        print("\n" + title)
        for i, menu in enumerate(menus):
            row = str(i + 1) + " - " + menu['name']
            if 'value' in menu:
                row += " => " + str(menu['value'])
            print(row)
        value = input("\nChoose an option: ")
        if (value == "") or (value == "0"):
            return ""
        try:
            value = int(value)
            if 0 < value <= len(menus):
                return menus[value - 1]['name']
        except:
            print("\nPlease enter a valid option")

def apply_type_of_var(string, value, type_of_var=None):
    if type_of_var is None:
        return type(value)(string)
    else:
        return type_of_var(string)

def ask_question(question, actual_var, type_of_var=None, clear_=False):
    if clear_:
        clear()
    res = -1
    while res == -1:
        value = input(f"\n{question}: {actual_var} => ")
        if value == "":
            return actual_var
        try:
            value = apply_type_of_var(value, actual_var, type_of_var)
            return value
        except:
            print("\nPlease enter a valid value")

def clear():
    system('cls')

def update_value_of_key(obj: dict, key, type_of_var=None):
    if key in obj:
        obj[key] = ask_question(f"{key}", obj[key], type_of_var)
    return obj

def get_menus_elements(obj: dict = None, names=None):
    if names is None:
        names = []
    res = []
    if obj is not None:
        for k, v in obj.items():
            res.append({'name': k, 'value': v})

    for idx, _ in enumerate(names):
        res.append({"name": _})
    return res


from typing import Callable, Dict, List
from redis import StrictRedis
import time
from slackclient import SlackClient
from .data import Bot, Command, Event

class PyBot(SlackClient):

    def __init__(self, token: str, bot: Bot, db: StrictRedis) -> None:
        super().__init__(token)
        if (not self.rtm_connect()):
            raise IOError('Connection to Slack failed, check your token')
        self.bot = bot
        self.db = db
        self._triggers: Dict = {}
        self._USER_CACHE: Dict[str, object] = {}

    def _getCachedUser(self, user_id: int):
        # store a cache of userId -> userName mappings,
        # so we don't need to make API calls every time
        if not self._USER_CACHE.get(user_id):
            self._USER_CACHE[user_id] = self.api_call(
                'users.info', user=user_id
            )['user']
        return self._USER_CACHE[user_id]

    def postMessage(self, channel: str, message: str, thread: str):
        self.api_call(
            'chat.postMessage',
            channel=channel,
            text=message,
            thread_ts=thread,
            username=self.bot.name,
            icon_emoji=self.bot.icon_emoji
        )

    def register(self, trigger: str, callback, condition):
        # registers a trigger, which fires a callback if condition is true
        maybeCallback = _MaybeCallback(callback, condition)
        self._triggers.setdefault(trigger, [maybeCallback])

    def notify(self, command: Command):
        # notifies all subscribers when command triggers, if condition is true
        for mc in (self._triggers.get(command.trigger, [])):
            if mc.condition(command.event):
                mc.callback(command)

    def listen(self):
        # listens for commands, and process them in turn
        while True:
            events = filter(lambda e: e.get('type') ==
                            'message' and 'text' in e, self.rtm_read())
            for event in events:
                command = self._messageEventToCommand(event)
                if command:
                    self.notify(command)  # notifies all listeners
            time.sleep(0.5)

    def _messageEventToCommand(self, event: Event):
        for trigger in self._triggers.keys():
            if event['text'].startswith(trigger):
                args = event['text'][len(trigger):].strip().split()
                return Command(
                    trigger,
                    args,
                    Event(
                        event.get('type'),
                        event.get('subtype'),
                        event.get('channel'),
                        event.get('user'),
                        event.get('text'),
                        event.get('ts'),
                        event.get('thread_ts')
                    ),
                    self._getCachedUser(event.user_id)
                )

        return None

def threaded(event):
    return allMessageEvents(event) and event.thread is not None

def messageEvents(event):
    return allMessageEvents(event) and event.thread is None

def allMessageEvents(event):
    return (
        event.type == 'message' and
        event.subtype is None and
        event.text is not None
    )

class _MaybeCallback(object):
    def __init__(self, callback, condition):
        self.callback = callback
        self.condition = condition


import unittest
from unittest.mock import MagicMock
from maliput_sim.core.sim import (Behavior, SimulationConfig, SimulationState, AgentInitialState)

class TestSimulationConfig(unittest.TestCase):

    def test_default_config(self):
        config = SimulationConfig()
        self.assertEqual(config.real_time_factor, 1.0)
        self.assertEqual(config.time_step, 0.01)

    def test_custom_config(self):
        config = SimulationConfig(real_time_factor=2.0, time_step=0.05)
        self.assertEqual(config.real_time_factor, 2.0)
        self.assertEqual(config.time_step, 0.05)

class TestAgentInitialState(unittest.TestCase):

    def test_agent_initial_state(self):
        initial_state = AgentInitialState(name="Agent1", position=[1, 2, 3],
                                          rotation=[0, 0, 0, 0], linear_vel=2.0, angular_vel=1.0)
        self.assertEqual(initial_state.name, "Agent1")
        self.assertEqual(initial_state.position, [1, 2, 3])
        self.assertEqual(initial_state.rotation, [0, 0, 0, 0])
        self.assertEqual(initial_state.linear_vel, 2.0)
        self.assertEqual(initial_state.angular_vel, 1.0)

class TestSimulationState(unittest.TestCase):

    def test_simulation_state(self):
        ecm_state = {"entity1": {"component1": {"key": "value"}}}
        sim_state = SimulationState(sim_time=10.0, ecm_state=ecm_state)

        self.assertEqual(sim_state.sim_time, 10.0)
        self.assertEqual(sim_state.ecm_state, ecm_state)

class TestBehavior(unittest.TestCase):

    def test_behavior_update(self):
        # Mocking the behavior function
        behavior_mock = MagicMock()

        # Creating a Behavior instance with the mocked behavior
        behavior_component = Behavior(behavior=behavior_mock)

        # Mocking the necessary parameters for update()
        delta_time = 0.1
        sim_state = MagicMock()
        entity = MagicMock()
        ecm = MagicMock()

        # Calling the update() method
        behavior_component.update(delta_time, sim_state, entity, ecm)

        # Assert that the behavior function is called
        behavior_mock.assert_called_once_with(delta_time, sim_state, entity, ecm)

    def test_behavior_get_state(self):
        # Mocking the get_state function
        get_state_mock = MagicMock(return_value={"key": "value"})

        # Creating a Behavior instance with the mocked get_state function
        behavior_component = Behavior(behavior=MagicMock(), get_state=get_state_mock)

        # Calling the get_state() method
        state = behavior_component.get_state()

        # Assert that the get_state function is called and returns the expected state
        get_state_mock.assert_called_once()
        self.assertEqual(state, {"key": "value"})


import fnmatch
import os
import argparse
import uuid

# PARAMETERS
parser = argparse.ArgumentParser(description='Rearrange a Unity\'s project folder guids.')
parser.add_argument('-i, --input',  metavar='FOLDER', type=str, nargs=1, required=True, dest='input_folder',
    help='input folder')
args = parser.parse_args()
input_folder = None if args.input_folder is None else args.input_folder[0]
if not os.path.isdir(input_folder):
    print('ERROR: Path not found.')
    quit(1)

# SCAN FILES

register_list = []
print('')
metas = dict()
mats = []
prefabs = []
controllers = []
scenes = []
anims = []
spriteAtlases = []
scriptableObjects = []

for root, folders, files in os.walk(input_folder):
    for fname in files:
        if fnmatch.fnmatch(fname, '*.meta'):
            metaname = root + os.sep + fname
            file = open(metaname, 'r')
            for line in file:
                elements = line.split(': ')
                if elements[0] == 'guid':
                    id = elements[1].replace('\n', '')
                    metas.update({id: [metaname, uuid.uuid4().hex]})
            file.close()
        if fnmatch.fnmatch(fname, '*.mat'):
            mats += [root + os.sep + fname]
        if fnmatch.fnmatch(fname, '*.prefab'):
            prefabs += [root + os.sep + fname]
        if fnmatch.fnmatch(fname, '*.controller'):
            controllers += [root + os.sep + fname]
        if fnmatch.fnmatch(fname, '*.unity'):
            scenes += [root + os.sep + fname]
        if fnmatch.fnmatch(fname, '*.anim'):
            anims += [root + os.sep + fname]
        if fnmatch.fnmatch(fname, '*.spriteatlas'):
            spriteAtlases += [root + os.sep + fname]
        if fnmatch.fnmatch(fname, '*.asset'):
            scriptableObjects += [root + os.sep + fname]

# CHANGE METAS

for k, v in metas.items():
    file = open(v[0], 'r')
    met = file.readlines()
    for i, l in enumerate(met):
        if 'guid:' in l:
            met[i] = l.replace(k, v[1])

    file.close()
    file = open(v[0], 'w')
    file.writelines(met)
    file.close()

#CHANGE EVERYTHING [Prefab,Texture,Sound,Material,Animation:Controller+File+Keyframes,Scene,SpriteAtlas,ScriptableObject]

for pname in prefabs + mats + controllers + scenes + anims + spriteAtlases + scriptableObjects:
    file = open(pname, 'r')
    changed = False
    segregate = file.readlines()
    for i, l in enumerate(segregate):
        if 'guid:' in l:
            key = None
            for k in metas.keys():
                if k in l:
                    key = k
                    changed = True
            if key is not None:
                segregate[i] = l.replace(key, metas[key][1])
                print(segregate[i], end='')
                print(key, '->', metas[key][1])
            else:
                print(segregate[i])
    file.close()
    if changed:
        file = open(pname, 'w')
        file.writelines(segregate)
        file.close()


# 세수의 합
# 배열을 입력받아 합으로 0을 만들수 있는 3개의 엘리먼트를 출력하라

nums = [-1,0,1,2,-1,-4]
def threeSum(nums):
    nums.sort()  # [-4,-1,-1,0,1,2]
    result = []

    for i in range(len(nums)-2):
        left , right = i+1 , len(nums)-1
        if i>0 and nums[i] == nums[i-1]:  # 중복을 방지하기 위함
            continue

        while left<right :
            sum = nums[i] + nums[left] + nums[right]

            if sum< 0 :
                left+=1
            elif sum > 0 :
                right -=1
            else :  #sum = 0인경우
                result.append((nums[i], nums[left], nums[right]))

                while left <right and nums[left] == nums[left+1] :   # 중복을 방지하기 위함
                    left += 1
                while left< right and nums[right] == nums[right-1]:  # 중복을 방지하기 위함
                    right -=1
                left +=1
                right -=1

    return result

threeSum(nums)


"""Backend supported: tensorflow.compat.v1, tensorflow, pytorch, paddle"""
import os

os.environ["DDEBACKEND"] = "pytorch"
import numpy as np

import deepxde as dde

# For plotting
import matplotlib.pyplot as plt

x_lower = -5
x_upper = 5
t_lower = 0
t_upper = np.pi / 2
nx = 256
nt = 201
# Creation of the 2D domain (for plotting and input)
x = np.linspace(x_lower, x_upper, nx)
t = np.linspace(t_lower, t_upper, nt)
X, T = np.meshgrid(x, t)

# The whole domain flattened
X_star = np.hstack((X.flatten()[:, None], T.flatten()[:, None]))

# Space and time domains/geometry (for the deepxde model)
space_domain = dde.geometry.Interval(x_lower, x_upper)  # 先定义空间
time_domain = dde.geometry.TimeDomain(t_lower, t_upper)  # 再定义时间
geomtime = dde.geometry.GeometryXTime(space_domain, time_domain)  # 结合一下，变成时空区域

# The "physics-informed" part of the loss
def pde(x, y):  # 这里x其实是x和t，y其实是u和v
    """
    INPUTS:
        x: x[:,0] is x-coordinate
           x[:,1] is t-coordinate
        y: Network output, in this case:
            y[:,0] is u(x,t) the real part
            y[:,1] is v(x,t) the imaginary part
    OUTPUT:
        The pde in standard form i.e. something that must be zero
    """

    u = y[:, 0:1]
    v = y[:, 1:2]

    # In 'jacobian', i is the output component and j is the input component
    u_t = dde.grad.jacobian(u, x, i=0, j=1)  # 一阶导用jacobian，二阶导用hessian
    v_t = dde.grad.jacobian(v, x, i=0, j=1)

    u_x = dde.grad.jacobian(u, x, i=0, j=0)
    v_x = dde.grad.jacobian(v, x, i=0, j=0)

    u_xx = dde.grad.jacobian(u_x, x, i=0, j=0)
    v_xx = dde.grad.jacobian(v_x, x, i=0, j=0)

    f_u = u_t + 0.5 * v_xx + (u**2 + v**2) * v
    f_v = v_t - 0.5 * u_xx - (u**2 + v**2) * u

    return [f_u, f_v]

# Boundary and Initial conditions

# Periodic Boundary conditions
bc_u_0 = dde.icbc.PeriodicBC(
    geomtime, 0, lambda _, on_boundary: on_boundary, derivative_order=0, component=0
)
bc_u_1 = dde.icbc.PeriodicBC(
    geomtime, 0, lambda _, on_boundary: on_boundary, derivative_order=1, component=0
)
bc_v_0 = dde.icbc.PeriodicBC(
    geomtime, 0, lambda _, on_boundary: on_boundary, derivative_order=0, component=1
)
bc_v_1 = dde.icbc.PeriodicBC(
    geomtime, 0, lambda _, on_boundary: on_boundary, derivative_order=1, component=1
)

# x=x[:, 0:1]
# t=x[:, 1:2]
# Initial conditions
def init_cond_u(x):
    "2 sech(x)"
    return 2 / np.cosh(x[:, 0:1])

def init_cond_v(x):
    return 0

ic_u = dde.icbc.IC(
    geomtime, init_cond_u, lambda _, on_initial: on_initial, component=0
)  # 所有初始条件上的点都应用初始条件
ic_v = dde.icbc.IC(
    geomtime, init_cond_v, lambda _, on_initial: on_initial, component=1
)  # 所有初始条件上的点都应用初始条件
# 初始条件满足init_cond_u或init_cond_v函数
data = dde.data.TimePDE(
    geomtime,
    pde,
    [bc_u_0, bc_u_1, bc_v_0, bc_v_1, ic_u, ic_v],
    num_domain=10000,
    num_boundary=20,
    num_initial=200,
    train_distribution="pseudo",
)  # 在内部取10000个点，在边界取20个点，在初始取200个点

# Network architecture
net = dde.nn.FNN([2] + [64] * 4 + [2], "tanh", "Glorot normal")

model = dde.Model(data, net)
# To employ a GPU accelerated system is highly encouraged.

model.compile("adam", lr=0.001, loss="MSE")
losshistory, train_state = model.train(iterations=8000, display_every=100)

dde.optimizers.config.set_LBFGS_options(
    maxcor=50,
    ftol=1.0 * np.finfo(float).eps,
    gtol=1e-08,
    maxiter=2000,
    maxfun=None,
    maxls=50,
)
model.compile("L-BFGS")
model.train(display_every=100)

# Make prediction
prediction = model.predict(X_star, operator=None)

u = prediction[:, 0].reshape(nt, nx)
v = prediction[:, 1].reshape(nt, nx)

h = np.sqrt(u**2 + v**2)

# Plot predictions
fig, ax = plt.subplots(3)

ax[0].set_title("Results")
ax[0].set_ylabel("Real part")
ax[0].imshow(
    u.T,
    interpolation="nearest",
    cmap="jet",
    extent=[t_lower, t_upper, x_lower, x_upper],
    origin="lower",
    aspect="auto",
)
ax[1].set_ylabel("Imaginary part")
ax[1].imshow(
    v.T,
    interpolation="nearest",
    cmap="jet",
    extent=[t_lower, t_upper, x_lower, x_upper],
    origin="lower",
    aspect="auto",
)
ax[2].set_ylabel("Amplitude")
ax[2].imshow(
    h.T,
    interpolation="nearest",
    cmap="jet",
    extent=[t_lower, t_upper, x_lower, x_upper],
    origin="lower",
    aspect="auto",
)

fig5 = plt.figure("预测演化图", dpi=130, facecolor=None, edgecolor=None)
ax = fig5.add_subplot(projection="3d")
surf = ax.plot_surface(
    X,
    T,
    h,
    rstride=3,  # 指定行的跨度
    cstride=3,  # 指定列的跨度
    cmap="coolwarm",  # 设置颜色映射（这里是绿化的意思）
    linewidth=0,
    antialiased=False,
)  # 抗锯齿
# ax.grid(False)#关闭背景的网格线
ax.set_xlabel("$x$")
ax.set_ylabel("$t$")
ax.set_zlabel("$|h(x,t)|$")

dde.saveplot(losshistory, train_state, issave=True, isplot=True)
# model.save(save_path = "./ddexuedinge_model/model")

plt.show()

src/PINN/标准非线性薛定谔方程/deepxde-非线性薛定谔-呼吸子.py


import tensorflow.python.eager.context
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.datasets import mnist
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import confusion_matrix, accuracy_score
import numpy as np
import matplotlib.pyplot as plt

if __name__ == '__main__':
    with tensorflow.device('/CPU:0'):
        (x_train, y_train), (x_test, y_test) = mnist.load_data()

        X_train = x_train.reshape(60000, 784)
        X_test = x_test.reshape(10000, 784)
        X_train = X_train.astype('float32')
        X_test = X_test.astype('float32')

        X_train /= 255
        X_test /= 255

        # model = MultinomialNB()
        #
        # model.fit(X_train, y_train)
        #
        # predict = model.predict(X_test)
        #
        # print(confusion_matrix(y_test, predict))
        # print(accuracy_score(y_test, predict))

        print(X_train.shape)
        print(X_test.shape)

        Y_train = to_categorical(y_train, 10)
        Y_test = to_categorical(y_test, 10)

        print(Y_train.shape)
        print(Y_test.shape)

        model = Sequential()
        model.add(Dense(512, input_shape=(784,)))
        model.add(Activation('relu'))
        model.add(Dense(256))
        model.add(Activation('relu'))
        model.add(Dense(10))
        model.add(Activation('softmax'))
        model.summary()

        model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
        model.fit(X_train, Y_train, batch_size = 128, epochs = 10, verbose = 1)

        score = model.evaluate(X_test, Y_test)
        print('Test score:', score[0])
        print('Test accuracy:', score[1])

        predicted_classes = np.argmax(model.predict(X_test), axis = 1)
        print(predicted_classes)
        correct_indices = np.nonzero(predicted_classes == y_test)[0]
        incorrect_indices = np.nonzero(predicted_classes != y_test)[0]

        # plt.figure()
        # for i in range(25):
        #     plt.subplot(5, 5, i + 1)
        #     correct = correct_indices[i]
        #     plt.imshow(X_test[correct].reshape(28, 28), cmap = 'gray')
        #     plt.title("Predicted {}, Class {}".format(predicted_classes[correct], y_test[correct]))
        # plt.tight_layout()
        # plt.show()

        plt.figure()
        for i in range(9):
            plt.subplot(3, 3, i + 1)
            incorrect = incorrect_indices[i]
            plt.imshow(X_test[incorrect].reshape(28, 28), cmap = 'gray')
            plt.title("Predicted {}, Class {}".format(predicted_classes[incorrect], y_test[incorrect]))
        plt.tight_layout()
        plt.show()


class Player:
    def __init__(self):
        self.active_pokemon = ''
        self.pokemon_list = []
        self.money = 0
        self.healing_items = []
        self.pokeball_bag = []

    def playerturn(self,enemy):
        action = ''
        print("\nWhat would you like to do?\n")
        print("1. Fight")
        print("2. Use Item")
        while action not in ['1','2']:
            action = input("\nEnter a number.\n")
        if action == '1':
            action = ''
            while action not in range(1,len(self.active_pokemon.moves)+2):
                for i in self.active_pokemon.moves:
                    print(str(self.active_pokemon.moves.index(i)+1) + ". " + i.name)
                print(str(self.active_pokemon.moves.index(i)+2) + ". " + "Return")
                while True:
                    try:
                        action = int(input("Which move would you like to use? Enter a number.\n"))
                        break
                    except ValueError:
                        continue
                if action in range(1,len(self.active_pokemon.moves)+1):
                    self.active_pokemon.moves[action-1].damagepokemon(self.active_pokemon,enemy)
                elif action == len(self.active_pokemon.moves)+1:
                    self.playerturn(enemy)
        elif action == '2':
            action = ''
            while action not in range(1,len(self.healing_items)+2):
                for i in self.healing_items:
                    print(str(self.healing_items.index(i)+1) + ". " + i.name + " -- x" + str(i.amount))
                print(str(self.healing_items.index(i)+2) + ". " + "Return")
                while True:
                    try:
                        action = int(input("Which healing item would you like to use? Enter a number.\n"))
                        break
                    except ValueError:
                        continue
                if action in range(1,len(self.healing_items)+1):
                    self.healing_items[action-1].healpokemon(self.active_pokemon)
                    if self.healing_items[action-1].amount == 0 or self.active_pokemon.temphealth == self.active_pokemon.maxhealth:
                        self.playerturn(enemy)
                elif action == len(self.healing_items)+1:
                    self.playerturn(enemy)


import sqlite3

'''LETS KNOW THE DATA TYPES WE HAVE IN sqlite3
Null  --> means doesnot exist
integers --> means real numbers
REAL --> means numbers with decimal
TEXT  --> means just a Text
BLOB  --> means kind of images mp3file or music file '''

'''STEPS IN CREATING A DATA IN THE DATABASE'''
# #STEP ONE:
# # creating a connection

# # con = sqlite3.connect('customer.db')  #when you run this program like this it is going to create a db file\

# #STEP ONE:
# # creating a connection
# '''creating a table and adding table to the table'''
# conn = sqlite3.connect('customer.db')
# # first we need to create a cursor
# # cursor points what we want to do with our table

# #STEP TWO:
# # creating a cusor
# c = conn.cursor()

# #STEP THREE:
# # creata a table
# #when we want to create a table we use our cursor (command of what our cosor will do)
# #sqlite3 IS CASE SENSITIVE SO  FOLLOW THE SAME STEP Like UPPERCASE CREATE TABLE and lowercase customer

# '''using a single quotaion mark can make ur code not to be readable'''

# # c.execute("CREATE TABLE customers(  first_name DATATYPE, last_name DATATYPE, email_address DATATYPE) ")

'''using the three quotation mark here make ur code readale'''

# c.execute("""CREATE TABLE customers(
#         first_name text,
#         last_name text,
#         email_address text

# ) """)

# #STEP FOUR:
# # COMMIT OUR COMMAND TO THE DATABASE(push what ever we have to the database)

# conn.commit()

#STEP FIVE:
# # close our  connection (anytime we create a connection we should always close it best practice)
# conn.close()

'''INSERT ONE RECORD TO THE DATABASE'''
conn = sqlite3.connect('customer.db')

c = conn.cursor()

# # EXECUTE OUR cusor
c.execute("INSERT INTO customers VALUES ('Mary', 'Doe', 'mary@gmail.com') ")  #sqlite3 COMMAND TO INSERT TABLE

print('command executed successfully...')
conn.commit()
conn.close()

'''INSERT MANY  TABLE TO THE DATABASE'''

# conn = sqlite3.connect('customer.db')

# c = conn.cursor()

# many_customers = [('chuks', 'emeka', 'chuls@gmail.com'),
#                  ('emy', 'joe', 'joe@gmail.com'),
#                  ('ebuka', 'elvin', 'e@gmail.com')]
# # EXECUTE OUR cusor
# c.executemany("INSERT INTO customers VALUES (?,?,?)", many_customers )  # ??? are the place holders for ech field

# print('command executed successfully...')
# conn.commit()
# conn.close()

'''QUERY AND FETCH ALL IN THE DATABASE'''

conn = sqlite3.connect('customer.db')

c = conn.cursor()

'''QUERY THE DATABASE'''
# c.execute("SELECT * FROM customers")
'''Primary ID'''
# c.execute("SELECT rowid, * FROM customers")

'''GET THE DATAS'''
# print(c.fetchone()[1])
# c.fetchmany(3)
# items = c.fetchall()
# for item in items:
#     print(item)
# EXECUTE OUR cusor

'''TO SEARCH AND FETCH USING THE WHERE CLAUSE'''

# c.execute("SELECT  * FROM customers WHERE last_name = 'Doe' ")
# c.execute("SELECT  * FROM customers WHERE email_address LIKE 'c%' ")
# item = c.fetchall()
# print(item)

'''UPDATE RECORD'''
# c.execute("""UPDATE customers SET first_name = 'ezinne'
#           WHERE last_name = 'emeka'

#           """)
'''a better way  todo this is to use a roll id'''
# c.execute("""UPDATE customers SET first_name = 'Chisom'
#           WHERE rowid = '5'

#           """)

'''DELETE RECORD'''
# c.execute("DELETE from customers WHERE rowid = 6")

'''we need to commit this'''
# conn.commit()

# c.execute("SELECT rowid, * FROM customers")

'''QUERY THE DATABASE ORDER BY'''
#by accending
c.execute("SELECT rowid, * FROM customers ORDER BY rowid")
# #by decending
# c.execute("SELECT rowid, * FROM customers ORDER BY rowid DESC")  #-rowid
# #by lastname with assending
# c.execute("SELECT rowid, * FROM customers ORDER BY last_name ")

# #by lastname with decending
# c.execute("SELECT rowid, * FROM customers ORDER BY last_name DESC")

'''QUERY THE DATABASE ORDER BY AND and OR'''
# c.execute("SELECT rowid, * FROM customers WHERE last_name LIKE 'D%' AND rowid = 2 ")
# c.execute("SELECT rowid, * FROM customers WHERE last_name LIKE 'D%' OR rowid = 2 ")

'''LIMITING RESULTS'''
# c.execute("SELECT rowid, * FROM customers  ")

'''CREATING A FUNCTION TO USE IN OUR SIMPLEAPP.PY'''
# c.execute("SELECT rowid, * FROM customers ORDER BY rowid LIMIT 1")

# item = c.fetchall()
# # for i in item:
# #     print(i)

# print(item)

# # print('command executed successfully...')
# '''every database created must end with commit and close'''
# conn.commit()
# conn.close()

'''CREATING A FUNCTION TO USE IN OUR SIMPLEAPP.PY'''
# query the database and return all records
def show_all():

    #connecting to the database
    conn = sqlite3.connect('customer.db')

    #creating a cursor
    c = conn.cursor()
    # query the database
    c.execute("SELECT rowid, * FROM customers ")

    item = c.fetchall()
    for i in item:
        print(i)

    # print('command executed successfully...')
    '''every database created must end with commit and close'''
    conn.commit()
    conn.close()

'''ADD A NEW RECORD TO OUR TABLE'''
def add_one(first_name, last_name, email_address):
     #connecting to the database
    # conn = sqlite3.connect('customer.db')
    c = conn.cursor()
    # executing and passing in the  parameters
    c.execute("INSERT INTO customers VALUES (?,?,?)", (first_name, last_name, email_address))
    conn.commit()
    conn.close()

'''ADD A NEW MANY TO OUR TABLE'''
def add_many(list):

    conn = sqlite3.connect('customer.db')
    c = conn.cursor()
    c.executemany("INSERT INTO customers VALUES (?,?,?)", (list))

    conn.commit()
    conn.close()

# 1:24:45