import os

import waflib
from waflib import Errors, Options
from waflib.Logs import pprint

def build(ctx):

    libntp_source = [
        "authkeys.c",
        "authreadkeys.c",
        "clocktime.c",
        "decodenetnum.c",
        "dolfptoa.c",
        "getopt.c",
        "initnetwork.c",
        "isc_interfaceiter.c",
        "isc_net.c",
        "macencrypt.c",
        "ntp_endian.c",
        "numtoa.c",
        "refidsmear.c",
        "socket.c",
        "socktoa.c",
        "ssl_init.c",
        "syssignal.c",
    ]

    libntp_source_sharable = [
        "assert.c",
        "clockwork.c",
        "emalloc.c",
        "hextolfp.c",
        "lib_strbuf.c",
        "msyslog.c",
        "ntp_calendar.c",
        "ntp_random.c",
        "prettydate.c",
        "statestr.c",
        "systime.c",
        "timespecops.c",
    ]

    if not ctx.env.HAVE_STRLCAT or not ctx.env.HAVE_STRLCPY:
        libntp_source_sharable += ["strl_obsd.c"]

    # C library
    ctx(
        features="c cstlib",
        includes=[ctx.bldnode.parent.abspath(), "../include"],
        source=libntp_source + libntp_source_sharable,
        target="ntp",
        use="CRYPTO SSL",
    )

    if ctx.env['ntpc'] == 'ffi':
        # Loadable FFI stub
        ctx(
            features="c cshlib",
            includes=[ctx.bldnode.parent.abspath(), "../include"],
            source=["ntp_c.c", "pymodule-mac.c"] + libntp_source_sharable,
            target="../pylib/ntpc",  # Put the output in the pylib directory
            use="M RT CRYPTO",
            vnum=ctx.env['ntpcver'],
        )
        ctx.add_post_fun(post)
    elif ctx.env['ntpc'] == 'ext':
        # Loadable Python extension
        ctx(
            features="c cshlib pyext",
            install_path='${PYTHONARCHDIR}/ntp',
            includes=[ctx.bldnode.parent.abspath(), "../include"],
            source=["pymodule.c", "pymodule-mac.c"] + libntp_source_sharable,
            target="../pylib/ntpc",  # Put the output in the pylib directory
            use="M RT CRYPTO",
        )

def post(ctx):
    if not (ctx.cmd == 'install' and ctx.env.BIN_LDCONFIG):
        return

    destdir = Options.options.destdir
    if destdir:
        # When --destdir is set, we are by definition not installing to the
        # real location, so running ldconfig is pointless.  It will need to
        # be run manually by the user (or the package install process, if
        # this is a package build).
        pprint("YELLOW", "WARNING:")
        pprint("YELLOW", "WARNING: Run ldconfig manually.")
        pprint("YELLOW", "WARNING:")
        return

    # Try to run ldconfig.  It is not a fatal error here if it fails, as the
    # install could be run by a non-root user.
    ldconfig = ' '.join(ctx.env.BIN_LDCONFIG)
    try:
        out = ctx.cmd_and_log(
            ctx.env.BIN_LDCONFIG, output=waflib.Context.STDOUT,
            quiet=waflib.Context.BOTH)
        pprint("GREEN", "running: %s  OK" % ldconfig)
    except Errors.WafError as e:
        pprint("RED", "running: %s  FAILED" % ldconfig)
        if e.stdout:
            pprint("RED", e.stdout)
        if e.stderr:
            pprint("RED", e.stderr)


from rpython.rlib.rarithmetic import r_singlefloat, r_uint
from rpython.rtyper.lltypesystem import lltype, rffi
from rpython.translator.tool.cbuild import ExternalCompilationInfo

r_uint32 = rffi.r_uint
assert r_uint32.BITS == 32
UINT32MAX = 2 ** 32 - 1

# keep in sync with the C code in pypy__decay_jit_counters below
ENTRY = lltype.Struct('timetable_entry',
                      ('times', lltype.FixedSizeArray(rffi.FLOAT, 5)),
                      ('subhashes', lltype.FixedSizeArray(rffi.USHORT, 5)))

class JitCounter:
    """A process translated with the JIT contains one prebuilt instance
    of this class.  It is used for three things:

    * It maps greenkey hashes to counters, to know when we have seen this
      greenkey enough to reach the 'threshold' or 'function_threshold'
      parameters.  This is done in a lossy way by a fixed-size 'timetable'.

    * It handles the counters on the failing guards, for 'trace_eagerness'.
      This is done in the same 'timetable'.

    * It records the JitCell objects that are created when we compile
      a loop, in a non-lossy dictionary-like strurcture.  This is done
      in the 'celltable'.

    The 'timetable' is a table of DEFAULT_SIZE entries, each of which
    containing 5 entries.  From a hash value, we use the index number
    '_get_index(hash)', and then we look in all five entries for a
    matching '_get_subhash(hash)'.  The five entries are roughly kept
    sorted by decreasing recorded time.  The hash value itself should be
    computed accordingly: we only use bits 21:32 for _get_index and
    bits 0:16 for _get_subhash.  (This organization is "probably good"
    to get not-too-random behavior; another motivation for it was for
    the STM branch, to avoid pointless conflicts between threads.)

    The time value stored in the timetable is a (short-precision)
    floating-point number.  The idea is that a value of 0.0 means
    absent, and values go up to the maximum of 1.0.

    'compute_threshold(threshold)' returns basically the fraction
    1.0/threshold, corresponding to the 'increment' value for the
    following APIs.

    'tick(hash, increment)' adds 'increment' to the time value stored
    with the 'hash'.  Remember that only bits 0:16,21:32 of the hash
    are used; in case of collision between two hashes, they will grow
    twice as fast, because each tick() call will contribute to the
    colliding time value.

    'fetch_next_hash()' returns a "random" hash value suitable for
    using in tick() later.  Used when compiling guards; when the
    guard actually fails, we'll tick() the guard's stored random hash.

    'reset(hash)', 'change_current_fraction(hash, new_time_value)'
    change the time value associated with a hash.  The former resets
    it to zero, and the latter changes it to the given value (which
    should be a value close to 1.0).

    'set_decay(decay)', 'decay_all_counters()' is used to globally
    reduce all the stored time values.  They all get multiplied by
    a fraction close to (but smaller than) 1.0, computed from the
    'decay' parameter.

    'install_new_cell(hash, newcell)' adds the new JitCell to the
    celltable, at the index given by 'hash' (bits 21:32).  Unlike
    the timetable, the celltable stores a linked list of JitCells
    for every entry, and so it is not lossy.

    'lookup_chain(hash)' returns the first JitCell at 'hash'.  You can
    then walk the chain by following the '.next' attributes until you
    reach None.

    'cleanup_chain(hash)' resets the timetable's 'hash' entry and
    cleans up the celltable at 'hash'.  It removes those JitCells
    for which 'cell.should_remove_jitcell()' returns True.
    """
    DEFAULT_SIZE = 2048

    def __init__(self, size=DEFAULT_SIZE, translator=None):
        "NOT_RPYTHON"
        self.size = size
        self.shift = 16
        while (UINT32MAX >> self.shift) != size - 1:
            self.shift += 1
            assert self.shift < 999, "size is not a power of two <= 2**16"
        #
        # The table of timings.  This is a 5-ways associative cache.
        # We index into it using a number between 0 and (size - 1),
        # and we're getting a 32-bytes-long entry; then this entry
        # contains 5 possible ways, each occupying 6 bytes: 4 bytes
        # for a float, and the 2 lowest bytes from the original hash.
        self.timetable = lltype.malloc(rffi.CArray(ENTRY), self.size,
                                       flavor='raw', zero=True,
                                       track_allocation=False)
        self._nexthash = r_uint(0)
        #
        # The table of JitCell entries, recording already-compiled loops
        self.celltable = [None] * size
        #
        if translator is not None:
            class Glob:
                step = 0
            glob = Glob()
            def invoke_after_minor_collection():
                # After 32 minor collections, we call decay_all_counters().
                # The "--jit decay=N" option measures the amount the
                # counters are then reduced by.
                glob.step += 1
                if glob.step == 32:
                    glob.step = 0
                    self.decay_all_counters()
            if not hasattr(translator, '_jit2gc'):
                translator._jit2gc = {}
            translator._jit2gc['invoke_after_minor_collection'] = (
                invoke_after_minor_collection)

    def compute_threshold(self, threshold):
        """Return the 'increment' value corresponding to the given number."""
        if threshold <= 0:
            return 0.0   # no increment, never reach 1.0
        return 1.0 / (threshold - 0.001)

    def _get_index(self, hash):
        """Return the index (< self.size) from a hash.  This truncates
        the hash to 32 bits, and then keep the *highest* remaining bits.
        Be sure that hash is computed correctly, by multiplying with
        a large odd number or by fetch_next_hash()."""
        hash32 = r_uint(r_uint32(hash))  # mask off the bits higher than 32
        index = hash32 >> self.shift     # shift, resulting in a value < size
        return index                     # return the result as a r_uint
    _get_index._always_inline_ = True

    @staticmethod
    def _get_subhash(hash):
        return hash & 65535

    def fetch_next_hash(self):
        result = self._nexthash
        # note: all three "1" bits in the following constant are needed
        # to make test_counter.test_fetch_next_index pass.  The first
        # is to increment the "subhash" (lower 16 bits of the hash).
        # The second is to increment the "index" portion of the hash.
        # The third is so that after 65536 passes, the "index" is
        # incremented by one more (by overflow), so that the next
        # 65536 passes don't end up with the same subhashes.
        self._nexthash = result + r_uint(1 | (1 << self.shift) |
                                         (1 << (self.shift - 16)))
        return result

    def _swap(self, p_entry, n):
        if float(p_entry.times[n]) > float(p_entry.times[n + 1]):
            return n + 1
        else:
            x = p_entry.times[n]
            p_entry.times[n] = p_entry.times[n + 1]
            p_entry.times[n + 1] = x
            x = p_entry.subhashes[n]
            p_entry.subhashes[n] = p_entry.subhashes[n + 1]
            p_entry.subhashes[n + 1] = x
            return n
    _swap._always_inline_ = True

    def _tick_slowpath(self, p_entry, subhash):
        if p_entry.subhashes[1] == subhash:
            n = self._swap(p_entry, 0)
        elif p_entry.subhashes[2] == subhash:
            n = self._swap(p_entry, 1)
        elif p_entry.subhashes[3] == subhash:
            n = self._swap(p_entry, 2)
        elif p_entry.subhashes[4] == subhash:
            n = self._swap(p_entry, 3)
        else:
            n = 4
            while n > 0 and float(p_entry.times[n - 1]) == 0.0:
                n -= 1
            p_entry.subhashes[n] = rffi.cast(rffi.USHORT, subhash)
            p_entry.times[n] = r_singlefloat(0.0)
        return n

    def tick(self, hash, increment):
        p_entry = self.timetable[self._get_index(hash)]
        subhash = self._get_subhash(hash)
        #
        if p_entry.subhashes[0] == subhash:
            n = 0
        else:
            n = self._tick_slowpath(p_entry, subhash)
        #
        counter = float(p_entry.times[n]) + increment
        if counter < 1.0:
            p_entry.times[n] = r_singlefloat(counter)
            return False
        else:
            # when the bound is reached, we immediately reset the value to 0.0
            self.reset(hash)
            return True
    tick._always_inline_ = True

    def change_current_fraction(self, hash, new_fraction):
        """Change the value stored for 'hash' to be the given 'new_fraction',
        which should be a float equal to or slightly lower than 1.0.
        """
        p_entry = self.timetable[self._get_index(hash)]
        subhash = self._get_subhash(hash)

        # find in 'n' the index that will be overwritten: the first within
        # range(5) that contains either the right subhash, or a null time
        # (or, if there isn't any, then just n == 4 will do).
        n = 0
        while n < 4 and (p_entry.subhashes[n] != subhash and
                         float(p_entry.times[n]) != 0.0):
            n += 1

        # move one step to the right all elements [n - 1, n - 2, ..., 0],
        # (this overwrites the old item at index 'n')
        while n > 0:
            n -= 1
            p_entry.subhashes[n + 1] = p_entry.subhashes[n]
            p_entry.times[n + 1]     = p_entry.times[n]

        # insert the new hash at index 0.  This is a good approximation,
        # because change_current_fraction() should be used for
        # new_fraction == value close to 1.0.
        p_entry.subhashes[0] = rffi.cast(rffi.USHORT, subhash)
        p_entry.times[0]     = r_singlefloat(new_fraction)

    def reset(self, hash):
        p_entry = self.timetable[self._get_index(hash)]
        subhash = self._get_subhash(hash)
        for i in range(5):
            if p_entry.subhashes[i] == subhash:
                p_entry.times[i] = r_singlefloat(0.0)

    def lookup_chain(self, hash):
        return self.celltable[self._get_index(hash)]

    def cleanup_chain(self, hash):
        self.reset(hash)
        self.install_new_cell(hash, None)

    def install_new_cell(self, hash, newcell):
        index = self._get_index(hash)
        cell = self.celltable[index]
        keep = newcell
        while cell is not None:
            nextcell = cell.next
            if not cell.should_remove_jitcell():
                cell.next = keep
                keep = cell
            cell = nextcell
        self.celltable[index] = keep

    def set_decay(self, decay):
        """Set the decay, from 0 (none) to 1000 (max)."""
        if decay < 0:
            decay = 0
        elif decay > 1000:
            decay = 1000
        self.decay_by_mult = 1.0 - (decay * 0.001)

    def decay_all_counters(self):
        # Called during a minor collection by the GC, to gradually decay
        # counters that didn't reach their maximum.  Thus if a counter
        # is incremented very slowly, it will never reach the maximum.
        # This avoids altogether the JIT compilation of rare paths.
        # We also call this function when any maximum bound is reached,
        # to avoid sudden bursts of JIT-compilation (the next one will
        # not reach the maximum bound immmediately after).  This is
        # important in corner cases where we would suddenly compile more
        # than one loop because all counters reach the bound at the same
        # time, but where compiling all but the first one is pointless.
        p = rffi.cast(rffi.CCHARP, self.timetable)
        pypy__decay_jit_counters(p, self.decay_by_mult, self.size)

# this function is written directly in C; gcc will optimize it using SSE
eci = ExternalCompilationInfo(post_include_bits=["""
static void pypy__decay_jit_counters(char *data, double f1, long size) {
    struct rpy_jitcnt { float times[5]; unsigned short subhashes[5]; };
    struct rpy_jitcnt *p = (struct rpy_jitcnt *)data;
    float f = (float)f1;
    long i;
    for (i=0; i<size; i++) {
        p->times[0] *= f;
        p->times[1] *= f;
        p->times[2] *= f;
        p->times[3] *= f;
        p->times[4] *= f;
        ++p;
    }
}
"""])

pypy__decay_jit_counters = rffi.llexternal(
    "pypy__decay_jit_counters", [rffi.CCHARP, lltype.Float, lltype.Signed],
    lltype.Void, compilation_info=eci, _nowrapper=True, sandboxsafe=True)

# ____________________________________________________________
#
# A non-RPython version that avoids issues with rare random collisions,
# which make all tests brittle

class DeterministicJitCounter(JitCounter):
    def __init__(self):
        from collections import defaultdict
        JitCounter.__init__(self, size=8)
        def make_null_entry():
            return lltype.malloc(ENTRY, immortal=True, zero=True)
        self.timetable = defaultdict(make_null_entry)
        self.celltable = defaultdict(lambda: None)

    def _get_index(self, hash):
        "NOT_RPYTHON"
        return hash

    def decay_all_counters(self):
        "NOT_RPYTHON"
        pass

    def _clear_all(self):
        self.timetable.clear()
        self.celltable.clear()


#sql requesting in python,
import sqlite3
import pandas

conn = sqlite3.connect("database.db")
cur = conn.cursor()
cur.execute("SELECT * FROM countries WHERE area >= 2000000")
rows = cur.fetchall()
conn.close()

for i in rows:
    print(i)

#put in csv
df = pandas.DataFrame.from_records(rows)
df.columns = ["Rank", "Country", "Area", "Population"]
print(df)
df.to_csv("countries_big_area.csv", index=False)

#insert into db
data = pandas.read_csv("ten_more_countries.txt")
conn = sqlite3.connect("database.db")
cur = conn.cursor()
for index,row in data.iterrows():
    print(row["Country"],row["Area"])
    cur.execute("INSERT INTO countries VALUES(NULL,?,?,NULL)",(row["Country"],row["Area"]))
conn.commit()
conn.close()

#files counter
import glob

file_list = glob.glob1("files", "*.py")
print(len(file_list))

#recursive files counter
file_list = glob.glob("files/subdirs/**/*.py", recursive=True)
print(len(file_list))


import sqlite3

conn = sqlite3.connect('coffee_main.db')
c = conn.cursor()
with conn:
    c.execute("SELECT * FROM coffee_products")
    coffees = c.fetchall()
    c.execute("SELECT * FROM coffee_components")
    component_quantity = c.fetchall()

quantity = []
name = []
for comp_name, comp_quant in component_quantity:
    quantity.append(comp_quant)
    name.append(comp_name)

coffees_dict = {}
price_list = []
coffee_id = []
for c_id, price, coffee, sugar, water in coffees:
    price_list.append(price)
    coffee_id.append(c_id)
    d = {c_id:(coffee,sugar,water)}
    coffees_dict.update(d)

print(coffee_id)

for i in quantity:
    if i == 0:
        print("Your supplies of coffee/water/sugar has ended.")
        print("Please refill your supplies")
        exit()


"""empty message

Revision ID: 0e84780c08ce
Revises:
Create Date: 2023-06-25 00:48:55.259558

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '0e84780c08ce'
down_revision = None
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('puzzles',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('quote', sa.String(length=255), nullable=True),
    sa.Column('created_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('users',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('username', sa.String(), nullable=True),
    sa.Column('email', sa.String(), nullable=True),
    sa.Column('password', sa.String(), nullable=True),
    sa.Column('score', sa.Integer(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('messages',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('body', sa.String(), nullable=True),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('score', sa.Integer(), nullable=True),
    sa.Column('created_at', sa.DateTime(), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.Column('updated_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('messages')
    op.drop_table('users')
    op.drop_table('puzzles')
    # ### end Alembic commands ###


from collections import OrderedDict

order = OrderedDict()

for i in range(int(input())):
   a = input().split()
   order[' '.join(a[:-1])] = order.get(' '.join(a[:-1]), 0) + int(a[-1])

for i, j in order.items():
   print(i, j)

'''
input:

9
BANANA FRIES 12
POTATO CHIPS 30
APPLE JUICE 10
CANDY 5
APPLE JUICE 10
CANDY 5
CANDY 5
CANDY 5
POTATO CHIPS 30
'''

'''
output:

BANANA FRIES 12
POTATO CHIPS 60
APPLE JUICE 20
CANDY 20
'''


import unittest

from extensions.middle.ReplacePNorm import ReplacePNormNodePattern
from mo.utils.unittest.graph import build_graph, compare_graphs

class ReplacePNormNodePatternTests(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.nodes_attributes = {
            'placeholder': {'kind': 'op', 'op': None},
            'in_node': {'kind': 'data', 'shape': [1, 3500]},
            'pnorm': {'kind': 'op', 'op': 'pnorm', 'group': 10, 'p': 2.0},
            'pnorm_data': {'kind': 'data', 'shape': [1, 350]},
            'out_placeholder': {'kind': 'op', 'op': 'placeholder'},
        }

    def test_pnorm(self):
        graph = build_graph(self.nodes_attributes,
                            [('placeholder', 'in_node'),
                             ('in_node', 'pnorm'),
                             ('pnorm', 'pnorm_data'),
                             ('pnorm_data', 'out_placeholder')])
        ReplacePNormNodePattern().find_and_replace_pattern(graph)

        ref_graph = build_graph({'in_placeholder': {'kind': 'op', 'op': None},
                                 'in_node': {'kind': 'data', 'shape': [1, 3500]},
                                 'power': {'kind': 'op', 'op': 'Power', 'power': 2.0},
                                 'power_data': {'kind': 'data'},
                                 'reshape':  {'kind': 'op', 'op': 'Reshape'},
                                 'reshape_data': {'kind': 'data'},
                                 'const': {'kind': 'op', 'op': 'Const', 'value': [1, 350, 10]},
                                 'const_data': {'kind': 'data'},
                                 'reduce': {'kind': 'op', 'op': 'ReduceSum'},
                                 'reduce_data': {'kind': 'data'},
                                 'const_1': {'kind': 'op', 'op': 'Const', 'value': 2},
                                 'const_data_1': {'kind': 'data'},
                                 'invpower': {'kind': 'op', 'op': 'Power', 'power': 0.5},
                                 'invpower_data': {'kind': 'data'},
                                 'out_placeholder': {'kind': 'op', 'op': 'placeholder'},
                                 },
                                [
                                    ('in_placeholder', 'in_node'),
                                    ('in_node', 'power'),
                                    ('power', 'power_data'),
                                    ('power_data', 'reshape', {'in': 0}),
                                    ('reshape', 'reshape_data'),
                                    ('const', 'const_data'),
                                    ('const_data', 'reshape', {'in': 1}),
                                    ('reshape_data', 'reduce', {'in': 0}),
                                    ('const_1', 'const_data_1'),
                                    ('const_data_1', 'reduce', {'in': 1}),
                                    ('reduce', 'reduce_data'),
                                    ('reduce_data', 'invpower'),
                                    ('invpower', 'invpower_data'),
                                    ('invpower_data', 'out_placeholder'),
                                ]
                                )

        (flag, resp) = compare_graphs(graph, ref_graph, 'out_placeholder')
        self.assertTrue(flag, resp)


import os

lstA = ['시퀀스 자료형이란?','시퀀스 자료형이란?','시퀀스 자료형이란?','종류','공통 연산','공통 연산','공통 연산','공통 연산','공통 연산','공통 연산']
lstB = ['여러 객체를 저장','각 객체들은 순서를 갖는다','각 객체들은 첨자를 이용하여 참조가능 하다','list, tuple, string','인덱싱(indexing) [1]','슬라이싱(slicing) [1:4]','연결하기 str1 + str2','반복하기 str1 * 4','멤버십 테스트 "a" in str','길이 정보 len(str)']

i=0
while i<len(lstA):
	os.system('clear')
	print('>>>',lstB[i])
	data = input('{} >> '.format(lstA[i]))
	print('>>>',lstB[i])
	if data == '': break
	i += 1

os.system('clear')
print('시퀀스 자료형을 학습 하였습니다.')


# https://leetcode.com/problems/maximum-number-of-words-you-can-type/

class Solution:
    def canBeTypedWords(self, text: str, brokenLetters: str) -> int:

        count = 0
        a = set(brokenLetters)
        print(a)
        text = text.split()
        for x in text:
            found = False
            for y in x:
                if y in a:
                    found = True
                    break

            if found == False:
                count = count+1

        return count


#!/usr/bin/env python3
import gym
from gym import wrappers
import gym_gazebo
import time
import numpy
import random
import time

import qlearn
import liveplot

from matplotlib import pyplot as plt

def render():
    render_skip = 0  # Skip first X episodes.
    render_interval = 50  # Show render Every Y episodes.
    render_episodes = 10  # Show Z episodes every rendering.

    if (x % render_interval == 0) and (x != 0) and (x > render_skip):
        env.render()
    elif (((x-render_episodes) % render_interval == 0) and (x != 0) and
          (x > render_skip) and (render_episodes < x)):
        env.render(close=True)

if __name__ == '__main__':

    env = gym.make('Gazebo_Lab06-v0')

    outdir = '/tmp/gazebo_gym_experiments'
    env = gym.wrappers.Monitor(env, outdir, force=True)
    plotter = liveplot.LivePlot(outdir)

    last_time_steps = numpy.ndarray(0)

    qlearn = qlearn.QLearn(actions=range(env.action_space.n),
                           alpha=0.2, gamma=0.8, epsilon=0.9)

    # qlearn.loadQ("QValues_A+")

    initial_epsilon = qlearn.epsilon

    epsilon_discount = 0.9986 # each episode, we're going to reduce epsilon by this much (transition from explore to greedy)

    start_time = time.time()
    total_episodes = 10000
    highest_reward = 0

    for x in range(total_episodes):
        done = False
        cumulated_reward = 0
        observation = env.reset()

        #decrease chance of random action
        if qlearn.epsilon > 0.05: #stop reducing epsilon at a certain point (don't want all greedy)
            qlearn.epsilon *= epsilon_discount #reduce epsilon each episode

        state = ''.join(map(str, observation)) #state holds "old" observation (state we're in before we take action)

        # To change max episode steps, go to gym_gazebo/__init__.py
        i = -1
        while True:
            i += 1

            # Pick an action based on the current state
            action = qlearn.chooseAction(state)
            # Execute the action and get feedback
            observation, reward, done, info = env.step(action)
            #new state after action, reward associated with action, whether we're done or not, and any info parameters
            cumulated_reward += reward

            if highest_reward < cumulated_reward:
                highest_reward = cumulated_reward

            nextState = ''.join(map(str, observation)) #if observation was [0,0,1], this would give 001 (single string)
            #nextState is state we've transitioned into (the current state)

            #learn based on start state, action we took, reward from taking that action, and state we've transitioned into from that action
            qlearn.learn(state, action, reward, nextState)

            env._flush(force=True)

            if not(done):
                state = nextState #set current state to state we've transitioned into
            else:
                last_time_steps = numpy.append(last_time_steps, [int(i + 1)])
                break

        print("===== Completed episode {}".format(x))

        if (x > 0) and (x % 5 == 0):
            qlearn.saveQ("QValues")
            plotter.plot(env)

        m, s = divmod(int(time.time() - start_time), 60)
        h, m = divmod(m, 60)
        print ("Starting EP: " + str(x+1) +
               " - [alpha: " + str(round(qlearn.alpha, 2)) +
               " - gamma: " + str(round(qlearn.gamma, 2)) +
               " - epsilon: " + str(round(qlearn.epsilon, 2)) +
               "] - Reward: " + str(cumulated_reward) +
               "     Time: %d:%02d:%02d" % (h, m, s))

    # Github table content
    print ("\n|"+str(total_episodes)+"|"+str(qlearn.alpha)+"|" +
           str(qlearn.gamma)+"|"+str(initial_epsilon)+"*" +
           str(epsilon_discount)+"|"+str(highest_reward) + "| PICTURE |")

    l = last_time_steps.tolist()
    l.sort()

    # print("Parameters: a="+str)
    print("Overall score: {:0.2f}".format(last_time_steps.mean()))
    print("Best 100 score: {:0.2f}".
          format(reduce(lambda x, y: x + y, l[-100:]) / len(l[-100:])))

    env.close()


from django.conf.urls import url
from.import views
# from django.contrib.auth.decorators import permission_required

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^nova-conta$', views.novaConta, name='novaConta'),
    url(r'^cadastra-nova-conta$', views.novoUsuario, name='novoUsuario'),
    url(r'^entrar$', views.logar, name='logar'),
    url(r'^sair$', views.sair, name='sair'),
    url(r'^correntista$', views.correntista, name='correntista'),
    url(r'^operacao$', views.operacao, name='operacao'),
    url(r'^administracao$', views.administracao, name='administracao'),
    url(r'^relatorio-cliente/(?P<cliente_id>[0-9]+)/$', views.relatorioCliente, name='relatorioCliente'),
]


from app_ml.functionalities.preprocessing_mongo import read_from_mongo_as_dataframe
from app_ml.functionalities.constants import SAVED_MODEL_PATH
from app_ml.models.RNN import RNN
import pymongo

def main():
    cars_db = pymongo.MongoClient('mongodb://localhost:27017')['cars']
    data, labels = read_from_mongo_as_dataframe(cars_db)
    rnn_model = RNN(data.iloc[0][0].shape, 2)
    x_train, y_train, x_test, y_test = rnn_model.split_train_test(data, labels)
    rnn_model.train(x_train, y_train)
    rnn_model.test(x_test, y_test)
    rnn_model.model.save(SAVED_MODEL_PATH)

if __name__ == '__main__':
    main()


import argparse
import logging
import pathlib

import matplotlib.pyplot as plt

# from train import test

plt.style.use("ggplot")
import gc
from pprint import pformat

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F

# from functorch import grad, make_functional_with_buffers, vmap
from torch.utils.data import DataLoader, Subset
from torchsummary import summary
from tqdm import trange
from tqdm import tqdm

from utils.common import (
    AlexNet,
    DatasetwithIndices,
    create_config,
    get_dataset,
    get_dataset_with_indices,
    get_logger,
    get_model,
    get_optimizer,
    get_parser,
    get_test_dataset,
    get_train_dataset,
    seed_everything,
)

# def get_mean_gradients(model, loader, use_all_params=False):
#     num_params = len(
#         list(model.parameters() if use_all_params else model.fc.parameters())
#     )
#     mean_gradients = [None for i in range(num_params)]
#     num_iter = len(loader)
#     progress_bar = tqdm(
#         loader, total=num_iter, desc="Mean Gradients", leave=False, position=2
#     )
#     for batch in progress_bar:
#         images, labels, _ = batch
#         torch.cuda.empty_cache()
#         images, labels = images.to(device), labels.to(device)
#         output = model(images)
#         gradient = torch.autograd.grad(
#             F.nll_loss(output, labels),
#             model.parameters() if use_all_params else model.fc.parameters(),
#         )
#         if mean_gradients[0] is not None:
#             for j in range(num_params):
#                 mean_gradients[j] += gradient[j].detach()  # .cpu().numpy()
#         else:
#             for j in range(len(gradient)):
#                 mean_gradients[j] = gradient[j].detach()  # .cpu().numpy()

#         for j in range(len(gradient)):
#             mean_gradients[j] /= num_iter

#     return mean_gradients

# def get_similarities(model, dataset, batch_size, mean_gradients, use_all_params=False):
#     slmodel, params, buffers = make_functional_with_buffers(
#         model if use_all_params else model.fc
#     )
#     loader = DataLoader(
#         dataset, batch_size, shuffle=True, num_workers=2, pin_memory=True
#     )

#     def loss_function(params, buffers, x, y):
#         x = x.unsqueeze(0)
#         y = y.unsqueeze(0)
#         preds = slmodel(params, buffers, x)
#         return F.nll_loss(preds, y)

#     batched_loss = vmap(
#         grad(loss_function),
#         (None, None, 0, 0),
#     )

#     similarities = []
#     img_indices = []
#     progress_bar = tqdm(
#         enumerate(loader),
#         total=len(loader),
#         desc="Per Sample Gradient Similarity",
#         leave=False,
#         position=2,
#     )
#     for i, batch in progress_bar:
#         imgs, labels, inds = batch
#         torch.cuda.empty_cache()
#         imgs, labels, inds = imgs.to(device), labels.to(device), inds.numpy()
#         with torch.no_grad():  ### TODO: Add if else for if use_all_params
#             hidden_state = model.features(imgs)
#         gradient = batched_loss(params, buffers, hidden_state, labels)
#         gc.collect()
#         torch.cuda.empty_cache()
#         # gradient = torch.autograd.grad(F.nll_loss(model(imgs), labels), model.parameters())

#         sim = (
#             torch.stack(
#                 [
#                     F.cosine_similarity(a.view(a.shape[0], -1), b.view(1, -1))
#                     for a, b in zip(gradient, mean_gradients)
#                 ],
#                 dim=-1,
#             )
#             .sum(dim=-1)
#             .detach()
#             .cpu()
#             .numpy()
#         )
#         # sim = torch.stack(list(map(lambda, gradient, mean_gradients))).sum()
#         similarities.append(sim)
#         img_indices.append(inds)
#     return np.concatenate(similarities), np.concatenate(img_indices)

# def cosinesimilarity(a,b):
#     return np.divide(np.dot(a, b.T), (np.linalg.norm(a, axis=-1, keepdims=True) * np.linalg.norm(b, keepdims=True)))

def cosinesimilarity(a, b):
    return torch.divide(
        torch.mm(a, b.T),
        (
            torch.linalg.norm(a, dim=-1, keepdims=True)
            * torch.linalg.norm(b, keepdims=True)
        ),
    )

def get_sims(gradients):
    mean_gradients = gradients.mean(dim=0, keepdims=True)
    return cosinesimilarity(gradients, mean_gradients)

def get_mean_gradients(p, model, loader, criterion, optimizer):
    num_params = len(list(model.get_last_layer().parameters()))
    num_iter = len(loader)
    embedding_dim = model.get_last_layer().in_features
    sample_num = len(loader.dataset)
    gradients = torch.zeros(
        [sample_num, p.num_classes * (embedding_dim + 1)],
        requires_grad=False,
        device="cuda",
    )
    img_indices = torch.zeros([sample_num], requires_grad=False, device="cuda")
    # gradients = np.zeros([sample_num, p.num_classes * (embedding_dim + 1)])
    # img_indices = np.zeros(sample_num)
    progress_bar = tqdm(
        loader, total=num_iter, desc="Mean Gradients", leave=False, position=2
    )
    model.eval()
    model.no_grad = True
    with model.embedding_recorder:
        for i, batch in enumerate(progress_bar):
            optimizer.zero_grad(set_to_none=True)
            images, labels, inds = batch
            torch.cuda.empty_cache()
            images, labels = images.to(device), labels.squeeze().to(device)
            output = model(images).requires_grad_(True)
            # logger.info((output.shape, labels.shape, inds.shape))
            loss = criterion(output, labels).sum()
            batch_num = labels.shape[0]
            with torch.no_grad():
                bias_parameters_grads = torch.autograd.grad(
                    loss, output, retain_graph=True
                )[0].cpu()
                weight_parameters_grads = model.embedding_recorder.embedding.cpu().view(
                    batch_num, 1, embedding_dim
                ).repeat(1, p.num_classes, 1) * bias_parameters_grads.view(
                    batch_num, p.num_classes, 1
                ).repeat(
                    1, 1, embedding_dim
                )

                gradients[
                    i * p.batch_size : min((i + 1) * p.batch_size, sample_num)
                ] = torch.cat(
                    (bias_parameters_grads, weight_parameters_grads.flatten(1)), dim=1
                )
            img_indices[
                i * p.batch_size : min((i + 1) * p.batch_size, sample_num)
            ] = inds.squeeze()

    return gradients, img_indices

def train_epoch(
    loader: torch.utils.data.DataLoader,
    model: nn.Module,
    criterion: nn.NLLLoss,
    optimizer: torch.optim.Optimizer,
    device: torch.device,
) -> None:
    """Trains model for one epoch

    Args:
        loader (torch.utils.data.DataLoader): Dataloader
        model (nn.Module): model
        criterion (nn.NLLLoss): Loss Function
        optimizer (torch.optim.Optimizer): Optimizer
        device (torch.device): device
    """
    model.train()
    model.no_grad = False
    # losses, accs = [], []
    for images, labels, _ in loader:
        images, labels = images.to(device), labels.to(device)
        optimizer.zero_grad(set_to_none=True)
        output = model(images)
        loss = criterion(output, labels)
        loss.backward()
        optimizer.step()
        # losses.append(loss.item())
        # acc = output.argmax(dim=1).eq(labels).float().mean().item()
        # accs.append(acc)
    model.eval()
    # return np.mean(losses), np.mean(accs)

# def train_loop(p, model):
#     # train_loader = DataLoader(data, p.batch_size, shuffle=True)

#     train_loader = DataLoader(get_train_dataset(p), p.batch_size, shuffle=True, num_workers=2, pin_memory=True)
#     test_data = get_test_dataset(p)
#     test_loader = DataLoader(test_data, p.batch_size)

#     criterion = nn.NLLLoss()
#     optimizer = get_optimizer(p, model)

#     losses, accs = [], []
#     for epoch in trange(p.train_epochs, leave=True):
#         model.train()
#         loss, acc = train_epoch(
#             train_loader, model, criterion, optimizer, device
#         )
#         losses.append(loss)
#         accs.append(acc)
#         gc.collect()
#         torch.cuda.empty_cache()

#     test_correct = test(test_loader, model, device)
#     test_acc = test_correct / len(test_data) * 100
#     return test_acc

def gradient_mathcing(p, data, logger):
    """Calculated mean gradient for the given dataset and find per sample similarity with mean gradients

    Args:
        p (EasyDict): Hyperparameters
        data (Dataset): Dataset
        logger

    Returns:
        tuple[np.ndarray, np.ndarray]: Arrays of shape (iter, len(dataset)) for similarities calculated for each sample for every iteration and corresponding indices
    """
    iterations = p.iter
    logger.debug(len(data))
    # assert len(data) % p.batch_size == 0, "All batches are not of same shape"

    if p.per_class:
        logger.info("Finding Mean Gradients for each class individually.")
        train_labels = torch.as_tensor(data.targets)
        cls_data = [
            Subset(data, torch.argwhere(train_labels == c))
            for c in range(p.num_classes)
        ]
        logger.debug(f"len datasets: {len(data)}")
    else:
        logger.info("Finding Mean Gradients for whole dataset at once.")

    seed_everything(p.seed)
    model = get_model(p, device)
    logger.info(
        "Model Summary\n"
        + str(summary(model, (p.channel, *p.im_size), verbose=0, device=device))
    )

    train_loader = DataLoader(
        data, p.batch_size, shuffle=True, num_workers=2, pin_memory=True
    )
    criterion = nn.CrossEntropyLoss().to(device)
    optimizer = get_optimizer(p, model)

    all_similarities, all_imginds = [], []
    progressbar = trange(iterations, desc="Iterations", position=0, leave=True)
    for k in progressbar:
        # if p.with_train:
        # moving to the end of loop
        if not p.with_train:
            seed_everything(p.seed + k)
            model = get_model(p, device)
            # slmodel, params, buffers = make_functional_with_buffers(model.fc)

        if not p.per_class:
            gradients, img_indices = get_mean_gradients(
                p, model, train_loader, criterion, optimizer
            )
            # similarities, img_indices = get_similarities(
            #     model, data, p.batch_size, mean_gradients
            # )
            similarities = get_sims(gradients).cpu().numpy()
            img_indices = img_indices.cpu().numpy()
        elif p.per_class:
            similarities, img_indices = [], []
            progressbar2 = tqdm(
                cls_data, desc="Per CLass Gradient Mathcing", position=1, leave=False
            )
            for dataset in progressbar2:
                loader = DataLoader(
                    dataset,
                    p.batch_size,
                    shuffle=True,
                    num_workers=2,
                    pin_memory=True,
                    # drop_last=True,
                )
                gradients, cls_all_inds = get_mean_gradients(
                    p, model, loader, criterion, optimizer
                )
                # cls_all_sims, cls_all_inds = get_similarities(
                #     model, dataset, p.batch_size, mean_gradients,
                # )
                cls_all_sims = get_sims(gradients).cpu().numpy().squeeze()
                cls_all_inds = cls_all_inds.cpu().numpy().squeeze()
                similarities.append(cls_all_sims)
                img_indices.append(cls_all_inds)
            try:
                similarities, img_indices = np.stack(similarities), np.stack(
                    img_indices
                )
            except ValueError:
                similarities, img_indices = np.array(
                    similarities, dtype=object
                ), np.array(img_indices, dtype=object)

        all_similarities.append(similarities)
        all_imginds.append(img_indices)

        if p.with_train:
            train_epoch(train_loader, model, criterion, optimizer, device)
            gc.collect()
            torch.cuda.empty_cache()

    if p.per_class:
        logger.info(progressbar2)
    logger.info(progressbar)
    all_similarities, all_imginds = np.stack(all_similarities), np.stack(all_imginds)
    return all_similarities, all_imginds

def main(p, logger):
    # p = create_config(args.config, args)

    # logger.info("Hyperparameters\n" + pformat(vars(p)))

    global device
    if torch.cuda.is_available:
        device = torch.device("cuda")
    else:
        device = torch.device("cpu")
        logger.warning("Using CPU to run the program.")

    seed_everything(p.seed)

    # dataset
    train_data, _ = get_dataset(p)
    train_data = DatasetwithIndices(train_data)
    logger.info(f"Dataset\n{str(train_data)}")

    logger.info("Hyperparameters\n" + pformat(vars(p)))
    # p.num_classes = len(train_data.classes)
    # logger.debug(f"Num Classes: {p.num_classes}")

    all_similarities, all_imginds = gradient_mathcing(p, train_data, logger)
    logger.info(
        f"All similarities shape: {all_similarities.shape}, All imgindices shape: {all_imginds.shape}"
    )
    np.save(
        p.output_dir
        / f"all_similarities{'_perclass' if p.per_class else ''}{'_withtrain' if p.with_train else ''}.npy",
        all_similarities,
    )
    np.save(
        p.output_dir
        / f"all_imginds{'_perclass' if p.per_class else ''}{'_withtrain' if p.with_train else ''}.npy",
        all_imginds,
    )

if __name__ == "__main__":
    # parser = argparse.ArgumentParser(
    #     description="Getting gradient similarity for each sample."
    # )
    # parser.add_argument("--config", help="Location of config file", required=True)
    # parser.add_argument("--seed", default=0, help="Seed")
    # parser.add_argument("--dataset", default="cifar100", help="Dataset to use")
    # parser.add_argument("--dataset_dir", default="./data", help="Dataset directory")
    # parser.add_argument("--topn", default=1000, type=int, help="Size of Coreset")
    # parser.add_argument(
    #     "--iter", default=100, type=int, help="Number of iterations for finding coreset"
    # )
    # parser.add_argument("-bs", "--batch_size", default=1000, help="BatchSize", type=int)
    # parser.add_argument(
    #     "--per_class",
    #     action="store_true",
    #     help="Specify whether to find Mean Gradients classwise",
    # )
    # parser.add_argument(
    #     "--with_train",
    #     action="store_true",
    #     help="No. of epochs to train before finding Gmean",
    # )
    # parser.add_argument(
    #     "--temp",
    #     action="store_true",
    #     help="Specify whether to use temp folder",
    # )
    # parser.add_argument(
    #     "--use_all_params",
    #     help="Specify if all model parameters' gradients to be used. Defaults: (FC layers only)",
    #     action="store_true",
    # )
    # parser.add_argument(
    #     "--resume", default=None, help="path to checkpoint from where to resume"
    # )

    parser = get_parser()

    args = parser.parse_args()
    if args.output_dir is not None:
        args.output_dir = pathlib.Path(args.dataset.lower()) / args.output_dir
    else:
        args.output_dir = pathlib.Path(args.dataset.lower())
    if args.pretrained:
        args.output_dir = args.output_dir / "pretrained"
    if args.temp:
        args.output_dir = args.output_dir / f"temp"
    args.logdir = args.output_dir / "logs"
    args.logdir.mkdir(parents=True, exist_ok=True)

    # temporary fix
    args.class_balanced = None
    args.augment = None
    logger = get_logger(args, "gradmatch")
    try:
        main(args, logger)
    except Exception:
        logger.exception("A Error Occurred")


import json
import os
import time
from tqdm import tqdm
from easydict import EasyDict

import pandas as pd

from .index_compression import restore_dict

def find_pos_in_str(zi, mu):
    len1 = len(zi)
    pl = []
    for each in range(len(mu) - len1):
        if mu[each:each + len1] == zi:  # 找出与子字符串首字符相同的字符位置
            pl.append(each)
    return pl

def insert_term2dict(term, _dict, doc_id, pos_id):
    if term != "":
        if term not in _dict.keys():
            _dict[term] = dict()
            _dict[term]['doc_feq'] = 1
            _dict[term]['posting_list'] = dict()  # This is for future modification
            _dict[term]['posting_list'][doc_id] = [pos_id]
        else:
            if doc_id not in _dict[term]['posting_list'].keys():
                _dict[term]['doc_feq'] += 1
                _dict[term]['posting_list'][doc_id] = [pos_id]
            else:
                _dict[term]['posting_list'][doc_id].append(pos_id)

def write_term_dict2disk(term_dict, filename):
    print("\tI'm writing {} to disk...".format(filename))
    start = time.time()
    term_dict = dict(sorted(term_dict.items(), key=lambda x: x[0]))
    term_col = list(term_dict.keys())
    doc_feq_col = list()
    posting_list_col = list()
    for term in tqdm(term_dict.keys()):
        doc_feq_col.append(term_dict[term]['doc_feq'])
        posting_list = dict(sorted(term_dict[term]['posting_list'].items(), key=lambda x: x[0]))
        term_dict[term]['posting_list'] = posting_list
        posting_list_col.append(posting_list)

    data_frame = pd.DataFrame({'term': term_col, 'doc_feq': doc_feq_col, 'posting_list': posting_list_col})
    data_frame.to_csv(filename, index=False, sep=',')

    end = time.time()
    print("\tFile {} has been successfully wrote to disk in {:.4f} seconds.".format(filename, end - start))

    return term_dict

def get_engine_from_csv(file_path, name, mode="vb"):
    filename = name + ".csv"
    file_name = os.path.join(file_path, name + ".csv")
    if filename not in os.listdir(file_path):
        raise NameError("No such file : {}.".format(file_name))

    print("\tI'm Loading the {} from {}...".format(name, file_name))
    start = time.time()
    dict_map = dict()
    if "compressed" in name:
        end = time.time()
        print("\tSuccessfully load {} in {:.4f} seconds.".format(name, end - start))
        return restore_dict(file_name, mode)
    if "dict" in name and "vector_model" not in name and "spell" not in name:
        df = pd.read_csv(file_name)
        for i, term in enumerate(df['term']):
            term = str(term)
            dict_map[term] = dict()
            dict_map[term]['doc_feq'] = df['doc_feq'][i]
            dict_map[term]['posting_list'] = eval(df['posting_list'][i])
    if "vector_model" in name:
        df = pd.read_csv(file_name)
        for i, doc_id in enumerate(df['doc_id']):
            dict_map[doc_id] = eval(df['values'][i])
    if "spell" in name or "rotation" in name:
        df = pd.read_csv(file_name)
        for i, key in enumerate(df['key']):
            key = str(key)
            dict_map[key] = eval(df['value'][i])

    end = time.time()
    print("\tSuccessfully load {} in {:.4f} seconds.".format(name, end - start))
    return dict_map

def parsing_json(file_path):
    args_dict = json.load(open(file_path, "rb"))
    args = EasyDict()
    for key, value in args_dict.items():
        args[key] = value
    return args

def get_doc_name_from_doc_id(data_path, doc_id):
    filenames = os.listdir(data_path)
    filenames = sorted(filenames, key=lambda x: int(x.split(".")[0]))
    return filenames[doc_id]

def display_query_result(data_path, term, pos):
    """

    :param data_path:
    :param term:
    :param pos:
    :return:
    """
    filenames = os.listdir(data_path)
    filenames = sorted(filenames, key=lambda x: int(x.split(".")[0]))
    for doc_id, pos_list in pos.items():
        doc_name = filenames[doc_id]
        print("{}: {}".format(doc_name, term))

def display_query_result_detailed(data_path, term, pos, k=10):
    """

    :param data_path:
    :param term:
    :param pos:
    :param k: Display k words before and after the sentence
    :return:
    """
    filenames = os.listdir(data_path)
    filenames = sorted(filenames, key=lambda x: int(x.split(".")[0]))
    for doc_id, pos_list in pos.items():
        doc_name = filenames[doc_id]
        print("{}: {}".format(doc_name, term))
        print("----------------------------------------------------------")
        with open(os.path.join(data_path, doc_name), "r") as file:
            content = file.read()
            raw_term_list = content.split(" ")
            for pos_id in pos[doc_id]:
                display_content = " ".join(raw_term_list[pos_id - k:pos_id + k + 1])
                print(display_content)
        file.close()


"""Find out how to 'clear the board' in Pyramid Solitaire.

The design is meant to be simple to understand so it is less likely to have
bugs, but to make Pyramid Solitaire solvable for the worst case scenarios, we
must do a bit of optimization work on the state representation.

This implementation skips all of the precalculations of the Java/Lisp versions
to keep things as simple as possible while still using the optimization of
cramming the entire state into an integer value.

This still needs more than 8GB RAM in the worst case because this algorithm
skips some of the features like unwinnable state detection."""
import collections

import solvers.deck

def card_value(card):
    """Return the card's numeric value according to Pyramid Solitaire rules.

    Aces are always 1, Jacks are 11, Queens are 12, and Kings are 13."""
    return 1 + "A23456789TJQK".index(solvers.deck.card_rank(card))

def cards_are_removable(card1, card2=None):
    """Return true if the card or cards can be removed together.

    Kings can be removed by themselves, and pairs of cards that add to 13."""
    values = [card_value(c) if c else 0 for c in [card1, card2]]
    return sum(values) == 13

class State:
    """A state in Pyramid Solitaire, represented by a 60-bit integer value.

    This class only has static methods, meant to be called on integer values.
    The reason is to save as much memory as possible (we'll be creating tens
    of millions of these).

    It's tempting to represent the state as lists of cards in the tableau,
    stock, and waste piles, but it's too slow and memory usage is too high.

    The trick to this state representation is that it holds data that refers
    to the deck of cards, without containing a reference to the deck. So we
    need the deck of cards to understand the state of the game.

    Bits  0-51: "deck_flags" - 52 bits representing whether or not each card
                in the deck remains in the game.
    Bits 52-57: "stock_index" - 6 bits containing a number from 28 to 52,
                an index into the deck for the card at the top of the stock
                pile. Cards with index higher than this are the remainder of
                the stock pile. Cards with index below this (and above 27) are
                the cards in the waste pile. Hint for understanding how it
                works: incrementing this stock index moves the top card of the
                stock pile to the top of the waste pile.
    Bits 58-59: 2 bits to indicate how many times the waste pile has been
                recycled."""

    EMPTY_STOCK = 52
    EMPTY_WASTE = 27
    INITIAL_STATE = (28 << 52) | ((2**52) - 1)

    # bits set on the Nth tableau card and the cards covering it from below
    UNCOVERED_MASKS = [
        0b1111111111111111111111111111,
        0b0111111011111011110111011010,
        0b1111110111110111101110110100,
        0b0011111001111001110011001000,
        0b0111110011110011100110010000,
        0b1111100111100111001100100000,
        0b0001111000111000110001000000,
        0b0011110001110001100010000000,
        0b0111100011100011000100000000,
        0b1111000111000110001000000000,
        0b0000111000011000010000000000,
        0b0001110000110000100000000000,
        0b0011100001100001000000000000,
        0b0111000011000010000000000000,
        0b1110000110000100000000000000,
        0b0000011000001000000000000000,
        0b0000110000010000000000000000,
        0b0001100000100000000000000000,
        0b0011000001000000000000000000,
        0b0110000010000000000000000000,
        0b1100000100000000000000000000,
        0b0000001000000000000000000000,
        0b0000010000000000000000000000,
        0b0000100000000000000000000000,
        0b0001000000000000000000000000,
        0b0010000000000000000000000000,
        0b0100000000000000000000000000,
        0b1000000000000000000000000000,
    ]

    @staticmethod
    def deck_flags(state):
        """Return the state's deck flags."""
        return state & 0xFFFFFFFFFFFFF

    @staticmethod
    def is_tableau_empty(state):
        return (state & 0xFFFFFFF) == 0

    @staticmethod
    def stock_index(state):
        """Return the state's stock index, the top card of the stock pile.

        If the stock index is 52, it means the stock pile is empty."""
        return (state >> 52) & 0b111111

    @staticmethod
    def cycle(state):
        """Return the state's cycle, the times the waste pile was recycled."""
        return (state >> 58) & 0b11

    @staticmethod
    def waste_index(state):
        """Return the state's waste index, the top card of the waste pile.

        If the waste index is 27, it means the waste pile is empty."""
        index = State.stock_index(state) - 1
        mask = 1 << index
        while index > State.EMPTY_WASTE:
            if (state & mask) != 0:
                break
            mask >>= 1
            index -= 1
        return index

    @staticmethod
    def _adjust_stock_index(state):
        """Return the state with its stock index adjusted correctly.

        Basically the stock index must point to a card that remains in the
        game or else be 52 to indicate the stock pile is empty. This makes sure
        every state has a single unique representation - you can't have two
        states that are effectively the same but have different stock indexes
        because one points to the actual top card and the other points to
        some card that no longer remains in the game."""
        index = State.stock_index(state)
        state = state & 0xC0FFFFFFFFFFFFF  # remove the stock index
        mask = 1 << index
        while index < State.EMPTY_STOCK:
            if (state & mask) != 0:
                break
            mask <<= 1
            index += 1
        return state | (index << 52)

    @staticmethod
    def _uncovered_indexes(deck_flags):
        """Return deck indexes of uncovered tableau cards."""
        flags = deck_flags & 0xFFFFFFF

        def is_uncovered(index):
            return (1 << index) == (flags & State.UNCOVERED_MASKS[index])

        return [i for i in range(28) if is_uncovered(i)]

    @staticmethod
    def successors(state, deck):
        """Return a list of successor states to this state.

        Actions that can be performed (if applicable):
        1. Recycle the waste pile.
        2. Draw a card from the stock pile to the waste pile.
        3. Remove a King from the tableau.
        4. Remove a King from the stock pile.
        5. Remove a King from the waste pile.
        6. Remove a pair of cards from the tableau.
        7. Remove a pair of cards, one each from the tableau and stock pile.
        8. Remove a pair of cards, one each from the tableau and waste pile.
        9. Remove a pair of cards, one each from the stock and waste piles."""

        def remove(deck_flags, *indexes):
            """Remove the cards at the indexes from the deck_flags value."""
            for index in indexes:
                deck_flags ^= (1 << index)
            return deck_flags

        results = []
        deck_flags = State.deck_flags(state)
        uncovered = State._uncovered_indexes(deck_flags)
        stock_index = State.stock_index(state)
        waste_index = State.waste_index(state)
        cycle = State.cycle(state)

        def create(deck_flags=deck_flags, stock_index=stock_index, cycle=cycle):
            """Create a new state given the individual parts of the state."""
            new_state = (cycle << 58) | (stock_index << 52) | deck_flags
            return State._adjust_stock_index(new_state)

        is_stock_empty = stock_index == State.EMPTY_STOCK
        is_waste_empty = waste_index == State.EMPTY_WASTE
        stock_card = deck[stock_index] if not is_stock_empty else None
        waste_card = deck[waste_index] if not is_waste_empty else None
        has_both = stock_card and waste_card

        if not stock_card and cycle < 2:
            # 1. recycle the waste pile
            results.append(create(stock_index=28, cycle=cycle+1))
        if stock_card:
            # 2. draw a card from stock to waste
            results.append(create(stock_index=stock_index+1))
        if stock_card and cards_are_removable(stock_card):
            # 4. remove a King from the stock pile
            results.append(create(deck_flags=remove(deck_flags, stock_index)))
        if waste_card and cards_are_removable(waste_card):
            # 5. remove a King from the waste pile
            results.append(create(remove(deck_flags, waste_index)))
        if has_both and cards_are_removable(stock_card, waste_card):
            # 9. remove the cards on the stock and waste piles
            results.append(create(remove(deck_flags, stock_index, waste_index)))
        for i in uncovered:
            if cards_are_removable(deck[i]):
                # 3. remove a King from the tableau
                results.append(create(remove(deck_flags, i)))
            else:
                if stock_card and cards_are_removable(deck[i], stock_card):
                    # 7. remove the cards from the tableau/stock pile
                    results.append(create(remove(deck_flags, i, stock_index)))
                if waste_card and cards_are_removable(deck[i], waste_card):
                    # 8. remove the cards from the tableau/waste pile
                    results.append(create(remove(deck_flags, i, waste_index)))
                for j in uncovered:
                    if cards_are_removable(deck[i], deck[j]):
                        # 6. remove two cards from the tableau
                        results.append(create(remove(deck_flags, i, j)))
        return results

def path(state, seen_states, deck):
    """Return the actions to take to get to this state from the start."""

    def is_bit_set(bits, n):
        """Return true if the nth bit of bits is equal to 1."""
        return (bits & (1 << n)) != 0

    def action(state, next_state):
        """Return the action taken to go from state to next_state."""
        diffs = state ^ next_state  # XOR to see which bits changed
        deck_diff = State.deck_flags(diffs)
        cycle_diff = State.cycle(diffs)
        if cycle_diff:
            return 'Recycle'
        elif deck_diff:
            cards = [deck[i] for i in range(52) if is_bit_set(deck_diff, i)]
            return f"Remove {' and '.join(cards)}"
        else:
            return 'Draw'

    actions = []
    while state in seen_states:
        prev_state = seen_states[state]
        actions.append(action(prev_state, state))
        state = prev_state
    return list(reversed(actions))

def solve(deck):
    """Return a solution to removing all tableau cards in Pyramid Solitaire."""
    fringe = collections.deque()
    seen_states = dict()
    fringe.append(State.INITIAL_STATE)
    while fringe:
        state = fringe.popleft()
        if State.is_tableau_empty(state):
            return path(state, seen_states, deck)
        for next_state in State.successors(state, deck):
            if next_state not in seen_states:
                seen_states[next_state] = state
                fringe.append(next_state)
    return []


#import sys
#import pprint

from Player.util import print_board
from Player.game import Board
from Player.newAI import AI  #3 detlte not needed....
from copy import deepcopy
import random

class Player:

    # this player class is a reimplementation of game class"

    """
    A class to represent a AI player agent. This needs to have function:
    _init_  -  initiates inself and a board in start position. and colour
    update  -  take a turn info and updates board
    action  -  sends an action in correct format
    """

    def __init__(self, colour):
        self.colour = colour
        if colour == "white":
            self.opp_colour = "black"
        else:
            self.opp_colour = "white"
        self.board = Board()
        #self.AI = AI()
        self.AIFunction = "random"

    def update(self,colour,action):

        ## assumes the following input
        ### actoin =  ("MOVE", n, (xa, ya), (xb, yb))
        ###("BOOM", (x, y))

        if action[0] == "MOVE":
            n_tokens = action[1]
            x_from, y_from = action[2][0], action[2][1]
            x_to, y_to = action[3][0], action[3][1]
            self.board.move(colour, n_tokens, x_from, y_from, x_to, y_to)

        if action[0] == "BOOM":
            x, y = action[1][0], action[1][1]
            self.board.boom(colour, x, y)

    def action(self):
        #AImethod = "random"   ##
        AImethod = "MinMax"

        if AImethod == "random":
            # get all the peices avaliable and randomly pick one:

            boardDict = deepcopy(self.board.get_board_dict())

            print(boardDict)

            pieces = []
            for k, v in boardDict.items():
                if v[-2:] == "wh":
                    pieces.append(k)

            print(pieces)
            randomPiece = random.choice(pieces)
            print(randomPiece)

            actionList = self.board.get_candidate_actions(self.colour, 1, randomPiece[0], randomPiece[1])

            print(actionList)

            randomMove = random.choice(actionList)
            print(randomMove)
            if randomMove[0] == 1:
                return ("BOOM",(randomMove[0],randomMove[0]))

            if randomMove[0] == 0:
                return ("MOVE", 1, (randomMove[1], randomMove[2]), (randomMove[3], randomMove[4]))

## ------------------------------------------------------------------------------------------------

##   class Node(self, board, parent, action, depth, cost=0):

        if AImethod == "MinMax":

            boardCopy = deepcopy(self.board)                  #create a copy of board

            start_node = Node(boardCopy, None, None,self.colour,cost=0)     #creates the starting node
            main_values = []                                         #holds max unitily of each action
            nodeAction = []                                     #hold description of each action to return
            main_child_nodes = start_node.get_child_nodes(self.colour)

            for each in main_child_nodes:
                utility = self.min_max_value(each)
                main_values.append(utility)
                nodeAction.append(each.action)

            max_value = max(main_values)
            max_index = main_values.index(max_value)
            chosenAction = nodeAction[max_index]

            #print(max_value)
            #print(main_values)
            #print(nodeAction)
            if chosenAction[0] == 1:
                return ("BOOM",(chosenAction[0],chosenAction[0]))

            if chosenAction[0] == 0:
                return ("MOVE", 1, (chosenAction[1], chosenAction[2]), (chosenAction[3], chosenAction[4]))

    def min_max_value(self,node):

        maxDepth = 2
        if node.board.terminal_state() or node.depth_ok(maxDepth) == False:
            return node.board.utility(5,node.playerColour,node.opp_colour)

        elif node.maxs_turn():
            #print('maxs turn')
            #print('depth')
            #print(node.depth)
            values = []
            childNodes = node.get_child_nodes(node.playerColour)
            for each in childNodes:
                utility = self.min_max_value(each)
                values.append(utility)

            #print(values)
            #print("max: ", max(values))
            return max(values)

        else:
            values = []
            #print("mins turn")
            #print('depth')
            #print(node.depth)
            childNodes = node.get_child_nodes(node.opp_colour)
            for each in childNodes:
                utility = self.min_max_value(each)
                values.append(-utility)
            #print(values)
            #print("min: ", min(values))
            return min(values)

## worth it to not add

### previous solution stops if board state has been seen before, so it records board states.

        ## update to use old heuristic of distance between things with a small weighting,
        ## then end states with much bigger weighting.

# -------------------------------------------------------------------------------------------------
        # alpha beta pruning

# -------------------------------------------------------------------------------------------------

class Node():
    def __init__(self, board, parent, action, playerColour,cost=0):
        self.board = board
        self.parent = parent
        self.action = action
        self.playerColour = playerColour

        if self.playerColour == "white":
            self.opp_colour = "black"
        else:
            self.opp_colour = "white"

        self.cost = cost
        if parent == None:
            self.depth = 0
        else:
            self.depth = self.parent.depth + 1

    # Sort nodes
    def __lt__(self, other):
         return self.cost < other.cost

    def depth_ok(self,maxDepth):
        if self.depth >= maxDepth:
            return False
        else:
            return True

    def maxs_turn(self):
        if self.depth == 0 or self.depth % 2 == 0:
            return True
        else:
            return False

    def get_child_nodes(self,colour):

        all_candidate_actions = self.board.get_all_candidate_actions(colour)       # get candidate actions
        child_nodes=[]

        for each in all_candidate_actions:
            boardCopy = deepcopy(self.board)
            new_board = boardCopy.apply_action(each,colour)
            child_node = Node(new_board,self,each,colour)
            child_nodes.append(child_node)

        return child_nodes


nome = str((input("Nome Completo:")))

up = nome.upper()
low = nome.lower()
let = nome.split()
joi = ''.join(let)
cou = len(joi)
qua = len(let[0])

print('Nome com todas as letras maiúsculas:', up)
print('Nome com todas as letras minúsculas:', low)
print('Quantidade de letras:', cou)
print('Quantida de letras do primeiro nome:', qua)


from pyrogram import Client, Filters, InlineKeyboardMarkup, InlineKeyboardButton, Emoji
from config import Messages as tr

@Client.on_message(Filters.private & Filters.incoming & Filters.command(['start']))
async def _start(client, message):
    await client.send_message(chat_id = message.chat.id,
        text = tr.START_MSG.format(message.from_user.first_name),
        parse_mode = "markdown",
        disable_notification = True,
        reply_to_message_id = message.message_id
    )

@Client.on_message(Filters.private & Filters.incoming & Filters.command(['help']))
async def _help(client, message):
    await client.send_message(chat_id = message.chat.id,
        text = tr.HELP_MSG[1],
        parse_mode = "markdown",
        disable_notification = True,
        reply_markup = InlineKeyboardMarkup(map(1)),
        reply_to_message_id = message.message_id
    )

help_callback_filter = Filters.create(lambda _, query: query.data.startswith('help+'))

@Client.on_callback_query(help_callback_filter)
async def help_answer(c, callback_query):
    chat_id = callback_query.from_user.id
    message_id = callback_query.message.message_id
    msg = int(callback_query.data.split('+')[1])
    await c.edit_message_text(chat_id = chat_id,    message_id = message_id,
        text = tr.HELP_MSG[msg],    reply_markup = InlineKeyboardMarkup(map(msg))
    )

def map(pos):
    if(pos==1):
        button = [
            [InlineKeyboardButton(text = '-->', callback_data = "help+2")]
        ]
    elif(pos==len(tr.HELP_MSG)-1):

        button = [
            [InlineKeyboardButton(text = 'Support Chat', url = "https://www.github.com/cdfxscrq")],
            [InlineKeyboardButton(text = 'Feature Request', url = "https://t.me/Akshayan1")],
            [InlineKeyboardButton(text = '<--', callback_data = f"help+{pos-1}")]

        ]
    else:
        button = [
            [
                InlineKeyboardButton(text = '<--', callback_data = f"help+{pos-1}"),
                InlineKeyboardButton(text = '-->', callback_data = f"help+{pos+1}")
            ],
        ]
    return button


from django.shortcuts import render

from articles.models import Article

def articles_list(request):
    template = 'articles/news.html'
    acricles = Article.objects.all().prefetch_related('scopes')
    ordering = '-published_at'
    context = {'object_list': acricles.order_by(ordering)}
    return render(request, template, context)


def find_it(seq):
    hashtable = {}
    for x in seq:
        if x in hashtable:
            hashtable[x] += 1
        else:
            hashtable[x] = 1
    for key,val in hashtable.items():
        if val % 2 != 0:
            return key

"""
def find_it(seq):
    for i in seq:
        if seq.count(i)%2!=0:
            return i

CLEVER SOLUTION
"""


from nonebot import on_command, on_message
from nonebot.params import CommandArg
from nonebot.adapters.onebot.v11 import (
    GroupMessageEvent,
    Bot,
    Message,
    MessageSegment
)
from nonebot.adapters.onebot.v11.permission import GROUP

from src.plugins.song_guess.data_source import generate_problem
from src.plugins.song_guess.plugins.guess.player import (
    Player,
    get_player,
    add_player,
    remove_player
)
from src.plugins.song_guess.model import check_answer, get_alias_list

async def timeout_reaction(player: Player):
    bot = player.server_bot
    event = player.event
    await bot.send(event, MessageSegment.record(player.original))
    message = f"超时啦！答案是{player.answer}！\n"
    message += "这些答案也同样正确：\n"
    message += "\n".join(await get_alias_list(player.answer)) + "\n"
    message += "如果您认为您回答的结果也是正确答案，可以联系bot管理员添加歌曲别名哦~"
    await bot.send(event, message, at_sender=True)
    await remove_player(player)

async def start_guess_rule(event: GroupMessageEvent):
    return event.is_tome() and (await get_player(event.get_user_id()) is None)

start_guess = on_command("猜歌", rule=start_guess_rule, aliases={"guess",}, priority=5, permission=GROUP, block=True)

@start_guess.handle()
async def _(bot: Bot, event: GroupMessageEvent, args: Message=CommandArg()):
    answer, original, instrumental = await generate_problem(10)
    player = Player(event, bot, 1, answer, original, instrumental)
    await start_guess.send(MessageSegment.record(instrumental))
    await start_guess.send("这段音乐对应哪首歌呢，请@小步梦并回答该歌曲的名称或者别名~（不必考虑标点符号和英文字符的大小写），您有一分钟时间思考和回答~", at_sender=True)
    player.timer_start()
    await add_player(player)

async def do_guess_rule(event: GroupMessageEvent):
    return event.is_tome() and (await get_player(event.user_id) is not None)

do_guess = on_message(rule=do_guess_rule, priority=10, permission=GROUP)

@do_guess.handle()
async def _(event: GroupMessageEvent):
    player_answer = event.get_plaintext().lower()
    player = await get_player(event.user_id)
    if await check_answer(player_answer, player.answer):
        await do_guess.send(MessageSegment.record(player.original))
        message = f"猜对啦！答案就是{player.answer}！\n"
        message += "这些答案也同样正确：\n"
        message += "\n".join(await get_alias_list(player.answer))
        await do_guess.send(message, at_sender=True)
        player.timer_stop()
        await remove_player(player)
    else:
        await do_guess.send(f"不对哦！再想想吧！", at_sender=True)


import asyncio
import logging
import os
from time import time

import aiohttp

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

async def read_text_file(directory, file):
    """
    Async version of the download_link method we've been using in the other examples.
    :param session: aiohttp ClientSession
    :param directory: directory to save downloads
    :param link: the url of the link to download
    :return:
    """

    # Check whether file is in text format or not
    if file.endswith(".txt"):
        file_path = f"{directory}/{file}"

        # call read text file function
        read_text_content(file_path)
        logger.info('Downloaded %s', file)

def read_text_content(file_path):
    with open(file_path, 'r') as f:
        for index, line in enumerate(f):
            print("Line {}: {}".format(index, line.strip().replace("COLUMNS", "SUCCESS")))

# Main is now a coroutine
async def main():
    download_dir = "C:/Suganya/test"
    os.chdir(download_dir)
    tasks = [(read_text_file(download_dir, file)) for file in os.listdir()]
    # gather aggregates all the tasks and schedules them in the event loop
    await asyncio.gather(*tasks, return_exceptions=True)

if __name__ == '__main__':
    ts = time()
    # Create the asyncio event loop
    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(main())
    finally:
        # Shutdown the loop even if there is an exception
        loop.close()
    logger.info('Took %s seconds to complete', time() - ts)


from sys import stdin
input = stdin.readline
n = int(input())
q = list()
for _ in range(n):
    a = list(map(str, input().split()))
    if a[0] == "push":
        q.append(a[1])
    if a[0] == "pop":
        if len(q) != 0:
            print(q.pop(0))
        else:
            print(-1)
    if a[0] == "size":
        print(len(q))
    if a[0] == "empty":
        if len(q) != 0:
            print(0)
        else :
            print(1)
    if a[0] == "front":
        if len(q) != 0:
            print(q[0])
        else:
            print(-1)
    if a[0] == "back":
        if len(q) != 0:
            print(q[-1])
        else:
            print(-1)
# 돚거했다.
# import sys
# import queue
# input=lambda:sys.stdin.readline().rstrip()
#
# n=int(input())
# q=queue.Queue()
#
# for _ in range(n):
#     com,*num=input().split()
#     if com=='push':
#         q.put(int(num[0]))
#     elif com=='pop':
#         print(q.get() if not q.empty() else -1)
#     elif com=='size':
#         print(q.qsize())
#     elif com=='empty':
#         print(1 if q.empty() else 0)
#     elif com=='front':
#         print(q.queue[0] if not q.empty() else -1)
#     elif com=='back':
#         print(q.queue[-1] if not q.empty() else -1)


import scipy.io
import pylab
import numpy
from math import *
import scipy.misc
import scipy.sparse
import sys
import time
import matplotlib.ticker
#import HCIL_model # make sure this model accounts for alignments etc
from matplotlib import pyplot as plt
import socket
import time
from InitializationPY import *
from EKF_MAT2PY import mat2py, recvall

# G = numpy.concatenate([numpy.concatenate([numpy.real(G1),
# 	numpy.imag(G1)], axis=1), numpy.concatenate([numpy.real(G2), numpy.imag(G2)],
# 	axis=1)], axis=0).T # real matrix [Re(G1),Im(G1); Re(G2), Im(G2)], for dark hole area

# Only use G2 for control right now
G = numpy.concatenate([numpy.concatenate([numpy.real(G2), numpy.imag(G2)],
	axis=1)], axis=0).T
# G = numpy.concatenate([numpy.real(G2), numpy.imag(G2)],
# 	axis=1).T
print(G.shape)

## DM setting after creating the dark hole

u0 = command #numpy.concatenate([DM1command, DM2command])

## Correction on top of u0
u = u0*0.0

## Drift covariances (might be a bit off, but the filter is not very sensitive to this)
Qlarge = G.dot(numpy.eye(len(u))).dot(G.T)* drift_std**2 # this needs to be number of actuators available, not of full control

## The Extended Kalman Filter class which stores its state
class Filter:
    def __init__(self, index, x0 = numpy.zeros(2)):
        self.index = index #pixel index
        self.x_hat = numpy.array(x0) #electric field (state) estimate (real 2x1 vector with real and imag parts)

        # self.Q = drift_stats["covs"][self.index] #process noise covariance
        self.Q = Qlarge[2*index:2*index+2, 2*index:2*index+2]
        self.P = 16*self.Q #initial state covariance

    def advance(self, Gu, I):
        ## "predic" step
        E_hat = complex(self.x_hat[0] + Gu[self.index], self.x_hat[1] + Gu[self.index+G.shape[0]//2]) #closed loop electric field estimate (open loop field + Jacobian * controls)
        z_hat = abs(E_hat)**2 + i_inco_avg#*scale # intensity estimate
        z = I[self.index] # intensity measurement

        H = [2*Gu[self.index] + 2*self.x_hat[0], 2*Gu[self.index+G.shape[0]//2] + 2*self.x_hat[1]] #observation equation linearization

        ## Kalman gain:
        S = self.P.dot(H).dot(H) + (dark_curr)**2
        K = self.P.dot(H)/S

        ## "update" step
        self.x_hat = self.x_hat + K.dot(z - z_hat) #closed loop electric field estimate
        self.P = self.P - numpy.outer(K,H).dot(self.P) #electric field covariance

        ## "advance" step
        self.P = self.P + self.Q #covariance increases due to druft
        self.x_hat = self.x_hat*1.0 #mean remains the same (the drift is zero mean)

        return E_hat

## Computing EFC gain
M = G.T.dot(G)
EFC_gain = numpy.linalg.pinv(M + alpha*numpy.eye(len(M))).dot(G.T) #EFC function in runEFC

## Sample random wavefront drift and dithering
numpy.random.seed(0)
u_drift = numpy.zeros(n_act_drift)
u_drifts = []
dus = []

for j in range(T):
    ## Random walk drift
    u_drifts.append(u_drift) #change drift
    u_drift = u_drift + drift_std*(numpy.random.random(u_drift.shape)-0.5) # in nm?

    ## Zero mean dithering
    dus.append(numpy.random.normal(0, sigma_u, u.shape))

## Initialize filters
#I0, E0_init = mat2py(DM1command, u0 ,len(DM1command),G1.shape[1])
fs = [Filter(index,[numpy.real(E0_init[index]), numpy.imag(E0_init[index])]) for index in range(G.shape[0]//2)]

## Run closed loop
for j in range(T):

	cont_power = j>4
	drift_power = j>4

	I_OL, E_OL = mat2py(DM1command + drift_power*u_drifts[j], u0 ,len(DM1command),G1.shape[1])
	I, E = mat2py(DM1command + drift_power*u_drifts[j], u0 + u,len(DM1command),G1.shape[1])

	E_OL_split  = numpy.zeros(2*len(E_OL))
	E_OL_split[0:n_px] = numpy.real(E_OL)
	E_OL_split[n_px:2*n_px] = numpy.imag(E_OL)

	E_split  = numpy.zeros(2*len(E))
	E_split[0:n_px]  = numpy.real(E)
	E_split[n_px:2*n_px]= numpy.imag(E)
	# plt.plot(u0+u, label="DM2 EFC+Dither+DH")
	# plt.plot(u0, label="DM2 DH command")
	# plt.plot(u, label="DM2 EFC+Dither")
	# plt.plot(DM1command, label="DM1 DH command")
	# plt.plot(u_drifts[j], label="DM1 Drift")
	# plt.legend()
	# plt.show()
	# print("Iteration %d: avg closed loop intensity %.01f, avg open loop intensity %.01f"%(j, numpy.linalg.norm(E)**2/scale, numpy.linalg.norm(E0)**2/scale))

	# print("Iteration %d: avg closed loop intensity %.01f"%(j, numpy.linalg.norm(I)/scale))

	# I = measurement(numpy.abs(E)**2) # get from getImage in lab, dark hole area only
	I2D_temp = numpy.zeros(99*83)
	# print('red',len(I2D_temp))
	I2D_temp[pixel_indices] = I
	I2D = I2D_temp.reshape(99,83)
	# print(I2D.shape)

	Gu = G.dot(u) #precompute the difference between closed and open loop fields
	Iperf = numpy.abs(E)**2
	E_hat_hat = []
	for f in fs:
			f.advance(Gu,I) #advance the filters
			# E_hat_hat.append(f.advance(Gu,Iperf+dark_curr))

    # Get field estimate
	x_hats = numpy.array([f.x_hat for f in fs])
	E_hat = numpy.concatenate([x_hats[:,0], x_hats[:,1]])

	# x_hat_hat = numpy.array(E_hat_hat)
	# print(len(E_hat_hat))
	# x_hathat = numpy.concatenate([x_hat_hat, x_hat_hat[:,1]])

	#print("mean estimated intensity")
	# E_hat2 = numpy.concatenate([numpy.real(E0), numpy.imag(E0)]) #for debugging only

	# print(numpy.linalg.norm(E_hat-E_hat2)/numpy.linalg.norm(E_hat2))

	## Record iteration data
	I_arr.append(I)
	I_OL_arr.append(I_OL)
	u_arr.append(u)
	E_hat_arr.append(E_hat)
	E_arr.append(E)
	E_OL_arr.append(E_OL)

	print("Iteration: ",j, "\navg closed loop intensity: ", numpy.mean(I),
		"\navg open loop intensity: ", numpy.mean(I_OL), "\nmean estimated OL intensity: ",
		numpy.mean(E_hat[0:n_px]**2+ E_hat[n_px:2*n_px]**2))

	# fig = plt.figure()
    # self.ax1 = fig.add_subplot(1, 2, 1)
	deltaE = G2.T.dot(u)
	deltaE_split = numpy.zeros(2*len(deltaE))
	deltaE_split[::2] = numpy.real(deltaE)
	deltaE_split[1::2] = numpy.imag(deltaE)

	Gu_comb = Gu[0:n_px] + Gu[n_px:2*n_px]*complex(0,1)

	# plt.plot(-Gu,label='Split -Gu')
	# # plt.plot(-Gu_comb,label='-Gu combined')
	# plt.plot(E_OL_split-E_split,lw=3,alpha = 0.5,label='Split OL-CL')
	# plt.plot(-G2.T.dot(u),label='-G2u')
	# plt.plot(E_OL-E,lw=2,alpha = 0.3,label='OL-CL')
	# plt.plot(E_hat,label = 'Ehat')
	# plt.plot(E_OL_split,lw=2,alpha = 0.5,label = "E_OL")

	# plt.plot(E,label = 'E')
	# plt.plot(E_hat_hat,lw=2,alpha = 0.5,label = "E_hat_hat")
	# plt.legend()
	# plt.title("Iteration %d"%(j))
	# plt.show()

    # Compute next command (EFC + dithering)

	u = dus[j] - cont_power*EFC_gain.dot(E_hat)
	# u = -EFC_gain.dot(E0) + dus[j]
	# ADD A SEND DM COMMAND

	#
	# print("Iteration %d: avg closed loop intensity %.01f, \
	# 	avg open loop intensity %.01f"%(j, numpy.mean(I), \
	# 	numpy.mean(I_OL)))

print("saving data")
to_save = ["I_arr","I_OL_arr","E_hat_arr","u_arr", "dus", "u_drifts","pixel_indices","i_inco_avg","dark_curr"]
scipy.io.savemat(fname, dict((s, numpy.array(eval(s))) for s in to_save))
print("data saved")


import os
import random
import time
from tkinter import *

import os
import random
import time
from tkinter import *

class decisionMaker():
    def __init__(self, master):
        #创建主要变量和UI界面
        self.parent = master
        self.parent.title("decisionMaker")
        self.frame = Frame(self.parent)
        self.frame.pack(fill=BOTH, expand=1)
        self.parent.resizable(width = False, height = False)
        #通过flag和time参数，实现一小时只能抽一次签
        self.flag = 0
        self.time = 0#记录上一次抽取的时间
        #初始化全局变量
        self.events = ["生命不息，脑洞不止", "来一发PAT吧", "忘记工作，玩会游戏", "阅读图书","现在是英语听力时间","艺群相关工作"]#事件列表
        self.numEvents = len(self.events)
        #文本路径
        self.recordPath = 'record.txt'

        #标题
        self.lb1 = Label(self.frame, text = '往期回顾', justify=CENTER)
        self.lb1.grid(row = 1, column = 1)
        #消息框1
        self.listbox1 = Listbox(self.frame, width = 28, height = 6)
        self.listbox1.grid(row = 2, column = 1, sticky = N, padx=10, pady=5)#padx和pady决定文本框上下左右空出多大像素的间隔
        #消息框2
        self.listbox2 = Listbox(self.frame, width = 28, height = 1, justify=CENTER)#justify参数决定了文本显示是靠左右对齐还是居中
        self.listbox2.grid(row = 3, column = 1, sticky = N, padx=10, pady=5)
        #抽选按钮
        self.btnChoice = Button(self.frame, text = 'make choice', command = self.makeChoice)
        self.btnChoice.grid(row = 4, column = 1, sticky = W+E+N, padx=5, pady=5)
        #若记录文本不存在，则创立
        if not os.path.exists(self.recordPath):
            f = open(self.recordPath, 'w')
            f.close()

        self.loadRecord()

    def loadRecord(self):
        '''
            读取与py文件在同一目录下的record文本
        '''
        f=open(self.recordPath)
        lines = f.readlines()
        f.close()
        #若为空文本，则将上一次抽选时间设为0，并退出函数
        if lines == []:
            self.time = 0
            return

        #否则从第一行获取上次抽取的时间
        self.time = int(lines[-1].replace('\n', '').split(' ')[-1])#从第一行获取上一次抽取的时间
        #讲record中的记录显示在listbox1中
        for line in lines:
            #时间格式转换
            line = line.replace('\n', '').split(' ')
            t = list(time.localtime(int(line[1])))[0:6]
            a=('{}-{}-{}-{}-{}-{}'.format(t[0], t[1], t[2], t[3], t[4], t[5]))
            #先显示时间，再显示内容
            self.listbox1.insert(END, a)
            self.listbox1.insert(END, line[0])
        self.listbox1.itemconfig

        #在listbox2中显示欢迎语
        self.listbox2.insert(END, "welcome")
        self.listbox2.itemconfig

    def updateRecord(self):
        '''
        每次发生有效抽取后，讲记录写入record中，并更新listbox1的显示
        '''

        f=open(self.recordPath, 'a')#追加模式写文件
        intTime = str(self.time).split('.')[0]#保存time的整数部分
        f.write(self.list2 + ' ' + intTime + '\n')
        f.close()

        #时间格式转换
        t = list(time.localtime(int(intTime)))[0:6]
        a =('{}-{}-{}-{}-{}-{}'.format(t[0], t[1], t[2], t[3], t[4], t[5]))

        self.listbox1.insert(END, a)
        self.listbox1.insert(END, self.list2) #然后显示内容
        self.listbox1.itemconfig

    def makeChoice(self):
        '''
        随机抽取时间并显示出来
        '''
        self.index = random.choice(list(range(self.numEvents)))#随机抽取
        self.list2 = self.events[self.index]
        if time.time()-self.time <3600:
            #如果在1小时内多次抽取，则拒绝请求
            self.list2 = "请不要在1小时内重复抽取"
        else:
            #抽取间隔大于1小时，则更新时间并保存记录
            self.time = time.time()
            self.updateRecord()

        self.listbox2.delete(0)#清除上一次显示
        self.listbox2.insert(END, self.list2)

        self.listbox2.itemconfig

if __name__ == '__main__':
    root = Tk()
    tool = decisionMaker(root)
    root.mainloop()