import os
import gtk

import pida.core.service as service
import pida.pidagtk.buffertree as buffertree
import pida.pidagtk.contentview as contentview
import pida.pidagtk.contextwidgets as contextwidgets

from pida.core import actions

defs = service.definitions
types = service.types

class BufferView(contentview.content_view):

    HAS_CONTROL_BOX = False
    HAS_DETACH_BUTTON = False
    HAS_CLOSE_BUTTON = False
    HAS_SEPARATOR = False
    HAS_TITLE = False

    SHORT_TITLE = 'Buffers'

    LONG_TITLE = 'List of open buffers'

    def init(self):
        self.__buffertree = buffertree.BufferTree()
        self.__buffertree.set_property('markup-format-string',
                                       '%(markup)s')
        self.__buffertree.connect('clicked',
                                  self.service.cb_plugin_view_clicked)
        self.__buffertree.connect('right-clicked',
                                  self.cb_buffertree_right_clicked)
        self.widget.pack_start(self.__buffertree)

    def get_bufferview(self):
        return self.__buffertree
    bufferview = property(get_bufferview)

    def add_document(self, document):
        self.__buffertree.add_item(document, key=document.unique_id)

    def select_next(self):
        self.__buffertree.view.grab_focus()
        ud = gtk.MOVEMENT_DISPLAY_LINES
        self.__buffertree.view.emit('move-cursor', ud, 1)

    def select_previous(self):
        self.__buffertree.view.grab_focus()
        ud = gtk.MOVEMENT_DISPLAY_LINES
        self.__buffertree.view.emit('move-cursor', ud, -1)

    def search(self):
        self.__buffertree.view.grab_focus()
        self.__buffertree.view.emit('start-interactive-search')

    def goto_index(self, index):
        model = self.__buffertree.model
        try:
            key = model[index][1].key
            self.__buffertree.set_selected(key)
        except IndexError:
            pass

    def cb_buffertree_right_clicked(self, tv, item, event):
        if item is not None:
            document = item.value
        else:
            document = None
        self.__popup_buffer(document, event)

    def __popup_buffer(self, document, event):
        if document is None:
            menu = gtk.Menu()
            oact = self.service.action_group.get_action(
                'buffermanager+open_file')
            mi = oact.create_menu_item()
            menu.add(mi)
            nact = self.service.action_group.get_action(
                'buffermanager+new_file')
            mi = nact.create_menu_item()
            menu.add(mi)
            menu.show_all()
        elif document.is_new:
            menu = gtk.Menu()
            sact = document.handler.action_group.get_action('DocumentSaveAs')
            mi = sact.create_menu_item()
            menu.add(mi)
        else:
            menu = self.service.boss.call_command('contexts',
                'get_context_menu', ctxname='file', ctxargs=[document.filename])
            menu.add(gtk.SeparatorMenuItem())
            clact = self.service.action_group.get_action(
                    'buffermanager+close_buffer')
            mi = clact.create_menu_item()
            menu.add(mi)
        menu.show_all()
        menu.popup(None, None, None, event.button, event.time)

class Buffermanager(service.service):

    display_name = 'Buffer Management'

    #plugin_view_type = BufferView
    class BufferView(defs.View):
        view_type = BufferView
        book_name = 'content'

    class sessions(defs.optiongroup):
        """Session management."""
        class automatically_load_last_session(defs.option):
            """Whether the session will be reloaded from closing PIDA."""
            rtype = types.boolean
            default = True
        class start_with_new_file(defs.option):
            """Whether a new file will be opened on startup if none other is
               specified on the command-line on by a session."""
            rtype = types.boolean
            default = True

    class document_changed(defs.event):
        pass

    class document_modified(defs.event):
        pass

    def bnd_editormanager_started(self):
        if not self.__session_loaded:
            self.__session_loaded = True
            for filename in self.boss.positional_args:
                self.call('open_file', filename=filename)
            if self.opt('sessions', 'automatically_load_last_session'):
                most_recent = os.path.join(self.boss.pida_home,
                                       'most-recent.session')
                if os.path.exists(most_recent):
                    self.call('load_session', session_filename=most_recent)
            def _n():
                if len(self.__documents) == 0 and self.opt('sessions',
                                                  'start_with_new_file'):
                    self.call('new_file')
            gtk.idle_add(_n)

    def init(self):
        self.__currentdocument = None
        self.__documents = {}
        self.__session_loaded = False
        self.__editor = None

    def bind(self):
        self.action_group.get_action('buffermanager+close_buffer').set_sensitive(False)

    @actions.action(stock_id=gtk.STOCK_OPEN, label=None, is_important=True,
                    default_accel='<Shift><Control>o')
    def act_open_file(self, action):
        """Opens a document"""
        chooser = gtk.FileChooserDialog(
                    title='Open a file',
                    parent=self.boss.get_main_window(),
                    buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
                             gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
        def _cb(dlg, response):
            if response == gtk.RESPONSE_ACCEPT:
                filename = chooser.get_filename()
                self.call('open_file', filename=filename)
            chooser.destroy()
        chooser.connect('response', _cb)
        chooser.run()

    @actions.action(stock_id=gtk.STOCK_CLOSE,
                    label='Close Document',
                    default_accel='<Control>w')
    def act_close_buffer(self, action):
        """Close the current buffer."""
        filename = self.__currentdocument.filename
        self.call('close_file', filename=filename)

    @actions.action(stock_id=gtk.STOCK_QUIT, label=None)
    def act_quit_pida(self, action):
        """Quits the application"""
        self.boss.stop()

    @actions.action(stock_id=gtk.STOCK_NEW, label=None,
                    default_accel='<Shift><Control>n')
    def act_new_file(self, action):
        """Creates a document"""
        self.call('new_file')

    @actions.action(stock_id=gtk.STOCK_ADD,
                    label=None,
                    default_accel='<Shift><Control><Alt>n')
    def act_new_file_wizard(self, action):
        """Creates a document from the wizard"""
        self.call('new_file_wizard')

    def act_save_session(self, action):
        """Saves the current session"""
        fdialog = gtk.FileChooserDialog('Please select the session file',
                                 parent=self.boss.get_main_window(),
                                 action=gtk.FILE_CHOOSER_ACTION_SAVE,
                                 buttons=(gtk.STOCK_OK,
                                          gtk.RESPONSE_ACCEPT,
                                          gtk.STOCK_CANCEL,
                                          gtk.RESPONSE_REJECT))
        def response(dialog, response):
            if response == gtk.RESPONSE_ACCEPT:
                self.call('save_session',
                          session_filename=dialog.get_filename())
            dialog.destroy()
        fdialog.connect('response', response)
        fdialog.run()

    def act_load_session(self, action):
        """Loads another session"""
        fdialog = gtk.FileChooserDialog('Please select the session file',
                                 parent=self.boss.get_main_window(),
                                 action=gtk.FILE_CHOOSER_ACTION_OPEN,
                                 buttons=(gtk.STOCK_OK,
                                          gtk.RESPONSE_ACCEPT,
                                          gtk.STOCK_CANCEL,
                                          gtk.RESPONSE_REJECT))
        def response(dialog, response):
            if response == gtk.RESPONSE_ACCEPT:
                self.call('load_session',
                          session_filename=dialog.get_filename())
            dialog.destroy()
        fdialog.connect('response', response)
        fdialog.run()

    @actions.action(stock_id=gtk.STOCK_GO_FORWARD, label='Next Buffer',
                    default_accel='<Alt>Right')
    def act_next_buffer(self, action):
        """Go to the next buffer in the buffer list"""
        self.plugin_view.select_next()

    @actions.action(stock_id=gtk.STOCK_GO_BACK, label='Previous Buffer',
                    default_accel='<Alt>Left')
    def act_previous_buffer(self, action):
        """Go to the previous buffer in the buffer list."""
        self.plugin_view.select_previous()

    @actions.action(stock_id=gtk.STOCK_FIND, label='Change Buffer',
                    default_accel='<Shift><Control>b')
    def act_interactive_buffer_change(self, action):
        """Interactively search for a buffer name."""
        self.plugin_view.search()

    def _switch_index(self, index):
        self.plugin_view.goto_index(index - 1)

    @actions.action(label='Buffer 1',
                    default_accel='<Alt>1')
    def act_1_buffer(self, action):
        self._switch_index(1)

    @actions.action(label='Buffer 2',
                    default_accel='<Alt>2')
    def act_2_buffer(self, action):
        self._switch_index(2)

    @actions.action(label='Buffer 3',
                    default_accel='<Alt>3')
    def act_3_buffer(self, action):
        self._switch_index(3)

    @actions.action(label='Buffer 4',
                    default_accel='<Alt>4')
    def act_4_buffer(self, action):
        self._switch_index(4)

    @actions.action(label='Buffer 5',
                    default_accel='<Alt>5')
    def act_5_buffer(self, action):
        self._switch_index(5)

    @actions.action(label='Buffer 6',
                    default_accel='<Alt>6')
    def act_6_buffer(self, action):
        self._switch_index(6)

    @actions.action(label='Buffer 7',
                    default_accel='<Alt>7')
    def act_7_buffer(self, action):
        self._switch_index(7)

    @actions.action(label='Buffer 8',
                    default_accel='<Alt>8')
    def act_8_buffer(self, action):
        self._switch_index(8)

    @actions.action(label='Buffer 9',
                    default_accel='<Alt>9')
    def act_9_buffer(self, action):
        self._switch_index(9)

    @actions.action(label='Buffer 10',
                    default_accel='<Alt>0')
    def act_10_buffer(self, action):
        self._switch_index(10)

    def act_buffers(self, action):
        """The buffer part of the view menu."""

    def cmd_open_document(self, document):
        if document is not self.__currentdocument:
            if document.unique_id not in self.__documents:
                self.__add_document(document)
            self.__view_document(document)

    def cmd_open_file(self, filename, quiet=False):
        if (len(filename) and (self.__currentdocument is None or
                filename != self.__currentdocument.filename)):
            document = self.__get_filename_document(filename)
            if document is None:
                try:
                    document = self.__open_file(filename)
                except:
                    if not quiet:
                        raise
                    else:
                        document = None
                if document is not None:
                    self.__add_document(document)
            self.__view_document(document)

    def cmd_new_file(self):
        document = self.__new_file()
        self.__add_document(document)
        self.__view_document(document)

    def cmd_save_as(self):
        document = self.__currentdocument
        chooser = gtk.FileChooserDialog(
                    title='Save as file',
                    parent=self.boss.get_main_window(),
                    action=gtk.FILE_CHOOSER_ACTION_SAVE,
                    buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
                             gtk.STOCK_OK, gtk.RESPONSE_ACCEPT))
        # pygtk 2.6 compatibility
        if hasattr(chooser, 'set_do_overwrite_confirmation'):
            chooser.set_do_overwrite_confirmation(True)
        def _cb(dlg, response):
            if response == gtk.RESPONSE_ACCEPT:
                filename = chooser.get_filename()
                document.filename = filename
                self.boss.call_command('editormanager', 'save_as',
                                        filename=filename)
                def v():
                    self.__set_document_project(document)
                    document.reset_markup()
                    self.__view_document(document)
                gtk.timeout_add(200, v)
            chooser.destroy()
        chooser.connect('response', _cb)
        chooser.run()

    def cmd_new_file_wizard(self):
        self.__new_file_wizard()

    def cmd_open_file_line(self, filename, linenumber):
        self.call('open_file', filename=filename)
        self.editor.call('goto_line', linenumber=linenumber)

    def cmd_close_file(self, filename):
        doc = self.__get_filename_document(filename)
        if doc is not None:
            doc.handler.close_document(doc)

    def cmd_save_session(self, session_filename):
        f = open(session_filename, 'w')
        for doc in self.__documents.values():
            if not doc.is_new:
                f.write('%s\n' % doc.filename)
        f.close()

    def cmd_load_session(self, session_filename):
        f = open(session_filename, 'r')
        for line in f:
            filename = line.strip()
            def _o(filename):
                self.call('open_file', filename=filename, quiet=True)
            gtk.idle_add(_o, filename)
        lines = f.readlines()
        f.close()
        return lines

    def cmd_file_closed(self, filename):
        doc = self.__get_filename_document(filename)
        if doc is not None:
            self.__remove_document(doc)
        else:
            self.log.warn('attempt to close file not opened %s', filename)

    def cmd_document_closed(self, document):
        self.__remove_document(document)

    def cmd_reset_current_document(self):
        self.__currentdocument.reset()
        self.events.emit('document_modified', document=self.__currentdocument)

    def __get_filename_document(self, filename):
        for uid, doc in self.__documents.iteritems():
            if doc.filename == filename:
                return doc

    def __remove_document(self, document):
        del self.__documents[document.unique_id]
        model = self.plugin_view.bufferview.model
        for row in model:
            if row[0] == str(document.unique_id):
                model.remove(row.iter)
        document.handler.action_group.set_visible(False)
        self.__currentdocument = None
        self.action_group.get_action('buffermanager+close_buffer').set_sensitive(False)
        def refresh():
            if self.__currentdocument is None:
                if len(model):
                    self.__view_document(model[0][1].value)
        gtk.idle_add(refresh)

    def __open_file(self, filename):
        document = self.boss.call_command('documenttypes',
                                          'create_document',
                                           filename=filename)
        return document

    def __new_file(self):
        return self.__open_file(None)

    def __new_file_wizard(self):
        self.boss.call_command('newfile', 'create_interactive')

    def __add_document(self, document):
        self.__documents[document.unique_id] = document
        self.__set_document_project(document)
        self.plugin_view.add_document(document)

    def __view_document(self, document):
        self.__currentdocument = document
        document.handler.view_document(document)
        self.__disable_all_handlers([document.handler])
        document.handler.action_group.set_sensitive(True)
        document.handler.action_group.set_visible(True)
        if document.is_new:
            self.boss.call_command('languagetypes', 'hide_all_handlers')
        else:
            self.boss.call_command('languagetypes', 'show_handlers',
                                   document=document)
        self.boss.call_command('window', 'update_action_groups')
        if (self.plugin_view.bufferview.get_selected_key()
                != document.unique_id):
            self.plugin_view.bufferview.set_selected(document.unique_id)
        self.action_group.get_action('buffermanager+close_buffer').set_sensitive(True)
        self.events.emit('document_changed', document=document)

    def __set_document_project(self, document):
        proj = self.boss.call_command('projectmanager',
                                      'get_project_for_file',
                                      filename=document.filename)
        document.set_project(proj)

    def __disable_all_handlers(self, handlers=[]):
        for uid, document in self.__documents.iteritems():
            if document.handler not in handlers:
                document.handler.action_group.set_sensitive(False)
                document.handler.action_group.set_visible(False)

    def get_editor(self):
        if self.__editor is None:
            self.__editor = self.get_service('editormanager')
        return self.__editor
    editor = property(get_editor)

    def cb_plugin_view_clicked(self, view, bufitem):
        doc = bufitem.value
        if doc != self.__currentdocument:
            self.__view_document(doc)

    def get_menu_definition(self):
        return  """
                <ui>
                <menubar>
                <menu name="base_file" action="base_file_menu">
                <placeholder name="OpenFileMenu">
                <menuitem name="new" action="buffermanager+new_file" />
                <menuitem name="open" action="buffermanager+open_file" />
                <menuitem action="buffermanager+new_file_wizard" />
                <separator />
                </placeholder>
                <placeholder name="SaveFileMenu" />
                <placeholder name="ExtrasFileMenu">
                <separator />
                <menuitem name="savesess" action="buffermanager+save_session" />
                <menuitem name="loadsess" action="buffermanager+load_session" />
                <separator />
                </placeholder>
                <placeholder name="GlobalFileMenu">
                <menuitem action="buffermanager+close_buffer" />
                <separator />
                <menuitem name="quit" action="buffermanager+quit_pida" />
                </placeholder>
                </menu>
                <menu name="base_view" action="base_view_menu">
                <menuitem name="bufnext"
                          action="buffermanager+next_buffer" />
                <menuitem name="bufprev"
                          action="buffermanager+previous_buffer" />
                <separator />
                <menuitem name="bufsrch"
                          action="buffermanager+interactive_buffer_change" />
                <separator />
                </menu>
                </menubar>
                <toolbar>
                <placeholder name="OpenFileToolbar">
                <toolitem name="New" action="buffermanager+new_file" />
                <toolitem name="Open" action="buffermanager+open_file" />
                <separator />
                </placeholder>
                <placeholder name="SaveFileToolbar">
                </placeholder>
                <placeholder name="EditToolbar">
                <toolitem action="buffermanager+close_buffer" />
                </placeholder>
                <placeholder name="ProjectToolbar">
                </placeholder>
                <placeholder name="VcToolbar">
                </placeholder>
                </toolbar>
                </ui>
                """
    def get_buffer_menu(self):
        s = []
        for i in xrange(1, 11):
            s.append('<menuitem action="buffermanager+%s_buffer" />' % i)
        return '\n'.join(s)

    def stop(self):
        most_recent = os.path.join(self.boss.pida_home, 'most-recent.session')
        self.call('save_session', session_filename=most_recent)

    def get_single_view(self):
        return self.get_first_view('BufferView')
    plugin_view = property(get_single_view)

Service = Buffermanager


import collections
import statistics as st

def load():
    with open("inputs/10.txt") as f:
        return tuple(tuple(c.strip()) for c in f.readlines())

def points_a(c):
    return {
        ")": 3,
        "]": 57,
        "}": 1197,
        ">": 25137,
    }[c]

def points_b(c):
    return {
        ")": 1,
        "]": 2,
        "}": 3,
        ">": 4,
    }[c]

def opening():
    return tuple(pairs().keys())

def closing():
    return tuple(pairs().values())

def pairs():
    return dict(
        (
            ("(", ")"),
            ("[", "]"),
            ("{", "}"),
            ("<", ">"),
        )
    )

def balanced(row):
    stack = collections.deque()
    illegal = []
    for op in row:
        if op in opening():
            stack.append(op)
        else:
            if stack and op != pairs()[stack[-1]]:
                illegal.append(op)
            if stack:
                stack.pop()
    return illegal

def close(row):
    row = list(row)
    while True:
        n = None
        for idx in range(len(row) - 1):
            if row[idx] in opening() and pairs()[row[idx]] == row[idx + 1]:
                n = idx
                break
        if n is not None:
            del row[idx + 1]
            del row[idx]
        if n is None:
            return "".join(pairs()[r] for r in reversed(row))

def b():
    for row in load():
        if not balanced(row):
            acc = 0
            for char in close(row):
                acc *= 5
                acc += points_b(char)
            yield acc

print("a:", sum(sum(points_a(i) for i in balanced(row)) for row in load()))
print("b:", st.median(b()))


#!/usr/bin/env python3

import socket
import os.path
import os
from time import sleep, time
import struct

CONNECT_PORT = 5555
CONNECT_PORT = 6660

boxes = {
    402551434305: 0 ,
    321481224602: 1 ,
    381796144306: 2 ,
    526106124102: 3 ,
    419560323802: 4 ,
    118715543305: 5, # 283693452806: 5 , ? REFURB?
    136068441606: 6 ,
    204025132105: 7 ,
    107585251305: 8 ,
    113498221102: 9 ,
    311689714402: 10,
    500910651205: 11,
    406774733705: 12,
    620477232705: 13,
    304347750505: 14,
    219976124702: 15,

    105581114003: 100
    }

def wait_for_ack(s):
    ack = b''
    while len(ack) < 2:
        ack += s.recv(1)
    ack += b'\x00'
    print('Received: (%d) %s' % (len(ack), ack.decode('utf-8')))

def send_file(s, path, remote_path):
    file_size = os.stat(path).st_size
    print("Sending %s (%d MiB)" % (path, file_size / (1024*1024)))
    s.sendall(struct.pack('4sI32s', b'WRTE', 4+4+32+file_size, remote_path.encode('utf-8')))

    start = time()
    with open(path, 'rb') as f:
        chunk_size = 4096
        while True:
            b = f.read(chunk_size)
            if not b:
                break
            s.sendall(b)

    # Wait for ok
    wait_for_ack(s)

    stop = time()
    duration = stop-start
    print("Transferred %d bytes in %f seconds (%f MBps)" % (file_size, duration, (file_size/duration)/(1024*1024)))

def launch(s, remote_path):
    print("Launching " + remote_path)
    s.sendall(struct.pack('4sI32s', b'LNCH', 4+4+32, remote_path.encode('utf-8')))
    #wait_for_ack(s)

def reboot(s):
    print("Rebooting")
    s.sendall(struct.pack('4sI32x', b'RSET', 4+4+32))
    #wait_for_ack(s)

def send_agent_config_file(s, ip, ip_mask, ip_gw, cs):
    # Gen 32b IP address
    remote_path = "E:\\agent_config.bin"
    file_size = 4*4
    s.sendall(struct.pack('4sI32s', b'WRTE', 4+4+32+file_size, remote_path.encode('utf-8')))

    def pack_ip(addr):
        return struct.pack('BBBB', *map(int, addr.split('.')))
    print('sending payload')
    s.sendall(pack_ip(ip) +
              pack_ip(ip_mask) +
              pack_ip(ip_gw) +
              pack_ip(cs))
    print('waiting for ack')
    wait_for_ack(s)

def deploy(s, team_id):
    # launch(s, "C:\\Dashboard\\dash.xbe")
    # return

    # Re-deploy agent
    # send_file(s, "./bin/default.xbe", "E:\\Dashboard\\default.xbe")
    # reboot(s)
    # return

    send_file(s, "../bin/freedm.wad", "E:\\freedm.wad")
    send_file(s, "../bin/default.xbe", "E:\\doom.xbe")
    # send_file(s, "../bin/default.xbe", "E:\\doom.xbe")
    # send_file(s, "../bin/default.xbe", "E:\\doom.xbe")
    print("About to launch doom!")
    sleep(2)
    launch(s, "E:\\doom.xbe")

def serve():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.bind(('0.0.0.0', 6666))
        s.listen()
        while True:
            try:
                conn, addr = s.accept()
                pid = os.fork()
                if pid != 0:
                    continue
            except KeyboardInterrupt:
                s.close()
                return
            with conn:
                print('Connected by', addr)

                # data = None
                data = conn.recv(32+12)
                #print('Received data from client:' + data.decode('utf-8'))

                if not data:
                    break
                #conn.sendall(data)

                try:
                    serial = int(data[32:].decode('utf-8'))
                    print('Serial:' + str(serial))

                    team_id = None
                    if serial in boxes:
                        team_id = boxes[serial]

                    if team_id == None:
                        print('UNKNOWN BOX NUMBER, UPDATE SCRIPT')
                        # conn.close()
                        # s.close()
                        # exit(1)
                        team_id = 100

                    print('Box #%d Identified' % team_id)
                    print('Deploying to', addr[0])
                    deploy(conn, team_id)
                except Exception as e:
                    print('Ignoring exception: ' + str(e))

                conn.close()
                exit()

# reboot('192.168.1.2')
serve()
# deploy('192.168.1.2')


from time import process_time
import gym
import numpy as np
from mj_envs.envs import env_base
from mj_envs.utils.xml_utils import reassign_parent
import os
import collections

class FMBase(env_base.MujocoEnv):

    DEFAULT_OBS_KEYS = ["qp", "qv", "pose_err"]
    DEFAULT_RWD_KEYS_AND_WEIGHTS = {
        "reach": -1.0,
        "bonus": 4.0,
        "penalty": -50,
    }

    def __init__(self, model_path, **kwargs):

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

        # Process model to ues DManus as end effector
        raw_sim = env_base.get_sim(model_path=curr_dir + model_path)
        raw_xml = raw_sim.model.get_xml()
        processed_xml = reassign_parent(
            xml_str=raw_xml, receiver_node="panda0_link7", donor_node="DManus_mount"
        )
        processed_model_path = curr_dir + model_path[:-4] + "_processed.xml"
        with open(processed_model_path, "w") as file:
            file.write(processed_xml)

        # EzPickle.__init__(**locals()) is capturing the input dictionary of the init method of this class.
        # In order to successfully capture all arguments we need to call gym.utils.EzPickle.__init__(**locals())
        # at the leaf level, when we do inheritance like we do here.
        # kwargs is needed at the top level to account for injection of __class__ keyword.
        # Also see: https://github.com/openai/gym/pull/1497
        gym.utils.EzPickle.__init__(self, model_path, **kwargs)

        # This two step construction is required for pickling to work correctly. All arguments to all __init__
        # calls must be pickle friendly. Things like sim / sim_obsd are NOT pickle friendly. Therefore we
        # first construct the inheritance chain, which is just __init__ calls all the way down, with env_base
        # creating the sim / sim_obsd instances. Next we run through "setup"  which relies on sim / sim_obsd
        # created in __init__ to complete the setup.
        super().__init__(model_path=processed_model_path)
        os.remove(processed_model_path)

        self._setup(**kwargs)

    def _setup(
        self,
        target_pose,
        obs_keys=DEFAULT_OBS_KEYS,
        weighted_reward_keys=DEFAULT_RWD_KEYS_AND_WEIGHTS,
        **kwargs
    ):

        self.target_pose = target_pose

        super()._setup(
            obs_keys=obs_keys,
            weighted_reward_keys=weighted_reward_keys,
            frame_skip=40,
            **kwargs
        )

    def get_obs_dict(self, sim):
        obs_dict = {}
        obs_dict["t"] = np.array([self.sim.data.time])
        obs_dict["qp"] = sim.data.qpos.copy()
        obs_dict["qv"] = sim.data.qvel.copy()
        obs_dict["pose_err"] = obs_dict["qp"] - self.target_pose
        return obs_dict

    def get_reward_dict(self, obs_dict):
        reach_dist = np.linalg.norm(obs_dict["pose_err"], axis=-1)
        far_th = 10

        rwd_dict = collections.OrderedDict(
            (
                # Optional Keys
                ("reach", reach_dist),
                ("bonus", (reach_dist < 1) + (reach_dist < 2)),
                ("penalty", (reach_dist > far_th)),
                # Must keys
                ("sparse", -1.0 * reach_dist),
                ("solved", reach_dist < 0.5),
                ("done", reach_dist > far_th),
            )
        )
        rwd_dict["dense"] = np.sum(
            [wt * rwd_dict[key] for key, wt in self.rwd_keys_wt.items()], axis=0
        )
        return rwd_dict

class FMReachEnvFixed(FMBase):
    def reset(self):
        obs = super().reset(self.init_qpos, self.init_qvel)
        return obs

class FMReachEnvRandom(FMBase):
    def reset(self):
        self.target_pose = self.np_random.uniform(
            low=self.sim.model.actuator_ctrlrange[:, 0],
            high=self.sim.model.actuator_ctrlrange[:, 1],
        )
        obs = super().reset(self.init_qpos, self.init_qvel)
        return obs


# python3

def find_maximum_value(dataset):
    assert 1 <= len(dataset) <= 29

    import math
    import operator

    def Min_And_Max(i,j):
        temp_min=math.inf
        temp_max=-math.inf
        for k in range(i, j):
            #print(M[i][k], M[k+1][j], m[i][k], m[k+1][j])
            a= op[k](M[i][k], M[k+1][j])
            b= op[k](M[i][k], m[k+1][j])
            c= op[k](m[i][k], M[k+1][j])
            d= op[k](m[i][k], m[k+1][j])
            temp_min = min(temp_min, a, b, c, d)
            temp_max = max(temp_max, a, b, c, d)
        return temp_min, temp_max

    a=len(dataset)
    n=math.ceil(a/2)
    m=[[0]*(n+1) for i in range(n+1)]
    M=[[0]*(n+1) for i in range(n+1)]
    d=[0]
    op=[0]

    for i in range(a):
        if i%2==0:
            d.append(int(dataset[i]))
        else:
            if dataset[i]=='+':
                op.append(operator.add)
            elif dataset[i]=='-':
                op.append(operator.sub)
            else:
                op.append(operator.mul)

    for i in range(1, n+1):
        m[i][i] = d[i]
        M[i][i] = d[i]

    for s in range(1, n):
        for i in range(1, n-s+1):
            j = i + s
            m[i][j], M[i][j] = Min_And_Max(i,j)

    return M[1][n]

if __name__ == "__main__":
    print(find_maximum_value(input()))


import os
from typing import List
from sqlalchemy.orm import Session
from fastapi import(
    Depends,
    Response,
    status,
    APIRouter,
    File,
    UploadFile
)
from fastapi.responses import JSONResponse

from app.core.database import get_db

from app.api.deps import(
    allow_create_resource
)

from app.core.torrent import(
    torrentController,
    get_torrent_instance
)

router = APIRouter()

@router.post(
    '/upload',
    status_code=status.HTTP_200_OK,
    #dependencies=[Depends(allow_create_resource)]
)
async def create_upload_file(
    file: UploadFile,
    server: torrentController = Depends(get_torrent_instance)
):
    contents = await file.read()
    path_file = os.path.join(os.getcwd(), 'files', file.filename)
    if os.path.isfile(path_file) == False:
        with open(path_file, 'wb') as f:
            f.write(contents)
    data_upload = server.upload_torrent(path_file)
    return data_upload


#author:vamshi

import os
import sys
import numpy as np
import cv2
import re
import glob
from tqdm import tqdm
import pickle

#file to read

TRAIN_DIR = "../datasets/mammo_cancer/train/"
VAL_DIR  = "../data_mammo/val/"

imsize = 256

def read_imgs(data_dir,label="cancer",d="LEFT", mode="MLO"):
	'''
		returns all images in the folder with dir, mode of photo

	'''
	data = []
	if(mode==None and d!=None):
		file = "*"+d+"_"+"*"+".png"
	elif(mode==None and d==None):
		file = "*.png"
	elif(d==None and mode!=None):
		file = "*"+"_"+mode+".png"
	else:
		file = "*"+d+"_"+mode+".png"

	for file in tqdm(glob.glob(TRAIN_DIR+label+"/"+file)):
		img = cv2.imread(file,0)
		img = cv2.resize(img,(imsize,imsize),interpolation=cv2.INTER_CUBIC)

		#print(img.shape)
		#cv2.imshow('img',img)
		#cv2.waitKey(0)
		img = img.astype(int)
		img = (img.astype(np.float32)/255.0)*2.0 - 1.0
		data.append(img)
	return data

def remove_text(img):
	'''
		removes text present in the images
	'''
	#edge = cv2.Canny(img,10,200)
	#blur = cv2.GaussianBlur(img,(5,5),0)
	#ret, otsu = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

	#cv2.imshow('img',edge)
	#cv2.waitKey(0)
	#print(img)
	return img

cancer = read_imgs(TRAIN_DIR,label="cancer",d=None,mode=None)
print(len(cancer))
normal = read_imgs(TRAIN_DIR,label="normal",d=None,mode=None)
print(len(normal))

save_file = "../datasets/mammo_cancer/dataset_mammo_train_"+str(imsize)+".pickle"
with open(save_file,"wb") as f:
	data  = {'cancer':cancer,
		  'normal':normal}
	pickle.dump(data,f)

print(len(data))
#read_imgs(TRAIN_DIR,d="LEFT",mode="MLO")
#read_imgs(TRAIN_DIR,d="RIGHT",mode="CC")
#read_imgs(TRAIN_DIR,d="LEFT",mode="CC")


import os
import csv
import cache_control

premise_path = "Premise.csv"
status_path = "Status.csv"
effect_path = "Effect.csv"
ability_path = "Ability.csv"
state_path = "CharacterState.csv"
experience_path = "Experience.csv"
juel_path = "Juel.csv"
talent_path = "Talent.csv"

def load_config():
    """载入配置文件"""
    with open(premise_path, encoding="utf-8") as now_file:
        now_read = csv.DictReader(now_file)
        for i in now_read:
            cache_control.premise_data[i["cid"]] = i["premise"]
            cache_control.premise_type_data.setdefault(i["premise_type"], set())
            cache_control.premise_type_data[i["premise_type"]].add(i["cid"])
    with open(status_path, encoding="utf-8") as now_file:
        now_read = csv.DictReader(now_file)
        for i in now_read:
            cache_control.status_data[i["cid"]] = i["status"]
            cache_control.status_type_data.setdefault(i["type"], [])
            cache_control.status_type_data[i["type"]].append(i["cid"])
    with open(effect_path, encoding="utf-8") as now_file:
        now_read = csv.DictReader(now_file)
        for i in now_read:
            cache_control.effect_data[i["cid"]] = i["effect"]
            cache_control.effect_type_data.setdefault(i["effect_type"], set())
            cache_control.effect_type_data[i["effect_type"]].add(i["cid"])
    with open(ability_path, encoding="utf-8") as now_file:
        now_read = csv.DictReader(now_file)
        read_flag = False
        for i in now_read:
            if read_flag == False:
                if i["cid"] != "能力对应类型和文字描述":
                    continue
                else:
                    read_flag = True
                    continue
            cache_control.ability_data[i["cid"]] = i["name"]
    with open(state_path, encoding="utf-8") as now_file:
        now_read = csv.DictReader(now_file)
        read_flag = False
        for i in now_read:
            if read_flag == False:
                if i["cid"] != "角色状态属性表":
                    continue
                else:
                    read_flag = True
                    continue
            cache_control.state_data[i["cid"]] = i["name"]
    with open(experience_path, encoding="utf-8") as now_file:
        now_read = csv.DictReader(now_file)
        read_flag = False
        for i in now_read:
            if read_flag == False:
                if i["cid"] != "经验名字":
                    continue
                else:
                    read_flag = True
                    continue
            cache_control.experience_data[i["cid"]] = i["name"]
    with open(juel_path, encoding="utf-8") as now_file:
        now_read = csv.DictReader(now_file)
        read_flag = False
        for i in now_read:
            if read_flag == False:
                if i["cid"] != "珠名字":
                    continue
                else:
                    read_flag = True
                    continue
            cache_control.juel_data[i["cid"]] = i["name"]
    with open(talent_path, encoding="utf-8") as now_file:
        now_read = csv.DictReader(now_file)
        read_flag = False
        for i in now_read:
            if read_flag == False:
                if i["cid"] != "素质对应类型和文字描述":
                    continue
                else:
                    read_flag = True
                    continue
            cache_control.talent_data[i["cid"]] = i["name"]


import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
from prepare import *
import tqdm
from max_margin_loss import max_margin_loss
from evaluation import Evaluation
from lstm import *

class QA_CNN(nn.Module):

    def __init__(self, num_hidden, emb_size, embeddings, dropout_prob):
        super(QA_CNN, self).__init__()
        self.num_hidden = num_hidden
        self.emb_size = emb_size
        self.embeddings = embeddings
        self.embedding_layer = nn.Embedding(1, self.emb_size)
        self.embedding_layer.weight.data = torch.from_numpy(self.embeddings).float()
        self.dropout = nn.Dropout(dropout_prob)

        self.conv1 = nn.Conv1d(emb_size, num_hidden, 3, padding=1)
        # self.conv2 = nn.Conv1d(6, 16, 5)
        # an affine operation: y = Wx + b

    def forward(self, sent, mask):
        x = self.embedding_layer(sent.long())
        # batch_size, length = sent.size()
        x = torch.transpose(x, 2, 1)
        x = F.relu(self.dropout(self.conv1(x)))
        x = torch.transpose(x, 2, 1)
        masked = torch.mul(x, mask.unsqueeze(2).expand_as(x))
        sum_out = masked.sum(dim=1)
        count = mask.sum(dim=1).unsqueeze(1).expand_as(sum_out)
        x =  torch.div(sum_out, count)
        return x

    def num_flat_features(self, x):
        size = x.size()[1:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features

def main():
    batch_size = 32
    dropout_prob = 0.1
    learning_rate = 0.001
    margin = 0.2
    momentum = 0
    num_epoches = 3
    num_hidden = 667
    emb_size = 200

    params = {'num_epoches' : num_epoches, 'momentum' : momentum,
                'margin' : margin, 'learning_rate' : learning_rate,
                'batch_size' : batch_size}

    train_data = UbuntuSequentialDataSet('ubuntu_data/train_random.txt')
    dev_data = UbuntuEvaluationDataSet('ubuntu_data/dev.txt')
    test_data = UbuntuEvaluationDataSet('ubuntu_data/test.txt')

    model = QA_CNN(num_hidden, emb_size, embeddings, dropout_prob)

    print('Started Training...\n')

    is_training = True

    run_model(train_data, dev_data, model, is_training, params)

    print('\nFinished Training!\n')

    print('Started Evaluating on Dev Set...\n')

    is_training = False

    params['num_epoches'] = 1

    run_model(dev_data, None, model, is_training, params)

    print('\nFinished Evaluating on Dev Set!\n')

    print('Started Evaluating on Test Set...\n')

    is_training = False

    run_model(test_data, None, model, is_training, params)

    print('\nFinished Evaluating on Test Set!\n')

if __name__ == '__main__':
    main()


# *-* coding=utf-8
from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import SubElement

import xml.etree.ElementTree as etree

def createNode(nodeRoot,id, tagname,x,y,w,h,role,ref=''):
    node= Element('node',{'id':id,'tagname':tagname,'x':x,'y':y,'w':w,'h':h,'role':role,'ref':ref})
    data =Element('data')
    node.append(data)
    # SubElement(node,'', attrib)
    nodeRoot.append(node)

def createPValue(pValueRoot,pvid,name,adressPV,accesstypePV,datatypePV,description='empty'):
    pValue = Element('pvalue',{'pvid':pvid,'name':name,'description':description})
    SubElement(pValue,'adress').text=adressPV
    SubElement(pValue, 'accsesstype').text=accesstypePV
    SubElement(pValue,'datatype').text=datatypePV
    pValueRoot.append(pValue)

def createPC(ePCRoot,type,server):
    pConnect=Element('pconnection')
    pConnect.set('type',type)
    pConnect.set('server',server)
    ePCRoot.append(pConnect)
    createPValue(pConnect,'54564218', 'testi','sdf54652434235644','READWRITE','REAL')

def createGraph(graphRoot,id,name,width, height):
    graph=Element('graph')
    graph.set('id',id)
    graph.set('name',name)
    graph.set('w',width)
    graph.set('h',height)
    graphRoot.append(graph)
    createNode(graph, 'sd5s65d', 'L001', '5', '56', '66', '56', 'pump')

def indent(elem,level=0):
    i ="\n"+level*"    "
    print (elem);
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "    "
        for e in elem:
            print (e)
            indent(e,level+1)
        if not e.tail or not e.tail.strip():
            e.tail =i
    if level and (not elem.tail or not elem.tail.strip()):
        elem.tail =i
    return elem

filename='hmi.xml'

root=Element('hmi')
root.set('version', '1.0')
tree=ElementTree(root)

createPC(root,'OPCUA','localhost')
createGraph(root,'5454746484','filter','1920','1024')

#root.set('id','123')
indent(root)
print(etree.tostring(root))
tree.write(open(filename,'wb+'),xml_declaration=True, encoding='UTF-8')

"""

filename="hmi.xml"

def CreateXml():

    hmiXML=ElementTree()
    purOrder =Element("hmi")

    book._setroot(purOrder)

    generated_on = str(datetime.datetime.now())

    list = Element("account",{'id':'2390094'})
    purOrder.append(list)
    item = Element("item1",{"Adresse":"abcd","Attribute":"4"})
    SubElement(item,"Name").text="Ventil1"
    SubElement(item,"Description").text="Wasserleitung1"
    purOrder.append(item)

    item = Element("item2",{"Adresse":"gfhi","attribute":"40"})
    SubElement(item,"Name").text="Ventil2"
    SubElement(item,"Description").text="Wasserleitung2"
    purOrder.append(item)

    indent(purOrder)
    return hmiXML

if __name__ == '__main__':
    hmiXML =CreateXml()
    hmiXML.write(filename,"utf-8")
    #book.write("book2.xml","utf-8",True) #true is with xml declaration
"""


from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from opensearchpy import OpenSearch

from django.conf import settings
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.test import TestCase

from search_client import SearchClient
from search_client.constants import LANGUAGES, DocumentTypes
from search_client.opensearch.configuration import create_open_search_index_configuration

class BaseLiveServerTestCase(StaticLiveServerTestCase):

    selenium = None
    fixtures = ['locales-edusources', 'privacy_statements']

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        chrome_options = Options()
        chrome_options.add_argument("--headless")
        chrome_options.add_argument("--no-sandbox")
        chrome_options.add_argument("window-size=1920,1080")

        cls.selenium = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
        cls.selenium.implicitly_wait(10)
        cls.explicit_wait = 300

    @classmethod
    def tearDownClass(cls):
        cls.selenium.quit()
        super().tearDownClass()

class BaseOpenSearchTestCase(TestCase):

    search = None
    instance = None
    alias_prefix = None

    @classmethod
    def index_body(cls, language):
        return create_open_search_index_configuration(language, DocumentTypes.LEARNING_MATERIAL)

    @classmethod
    def get_alias(cls, language):
        return f"{cls.alias_prefix}-{language}"

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.search = OpenSearch(
            [settings.OPENSEARCH_HOST]
        )
        for language in LANGUAGES:
            cls.search.indices.create(
                cls.get_alias(language),
                ignore=400,
                body=cls.index_body('nl')
            )
        cls.instance = SearchClient(
            f"{settings.PROTOCOL}://{settings.OPENSEARCH_HOST}",
            DocumentTypes.LEARNING_MATERIAL,
            cls.alias_prefix
        )

    @classmethod
    def tearDownClass(cls):
        for language in LANGUAGES:
            cls.search.indices.delete(
                cls.get_alias(language)
            )
        cls.search.close()
        cls.instance.client.close()
        super().tearDownClass()

    fixtures = ['locales-edusources', 'privacy_statements']


from Bank import Bank
from Agent import Agent
from Strategies import Greedy, Cooperative, EmotionalStrategy
import matplotlib.pyplot as plt

class PublicGoods:
    def __init__(self):
        self.bank = Bank(1.5)
        self.iterations = 100
        self.agents = []

    def run(self):
        for i in range(self.iterations):
            self.bank.pay_in(self.agents)
            self.bank.multiply_deposit()
            self.bank.pay_out(self.agents)
            # for agent in self.agents:
            #     agent.get_inspired(self.agents)

        fig, ax = plt.subplots()
        ax.plot(self.agents[0].balances, alpha=0.7, label='smart')
        ax.plot(self.agents[1].balances, alpha=0.6, label='emotional')
        ax.plot(self.agents[20].balances, alpha=0.5, label='greedy')
        ax.legend(loc="upper left")
        plt.show()

    def addEmotionalAgent(self, anger=1, gratitude=1, admiration=1, name=""):
        strategy = EmotionalStrategy(anger, gratitude, admiration)
        self.agents.append(Agent(name, strategy))

    def addLearningAgent(self, name=""):
        strategy = EmotionalStrategy(is_smart=True)
        self.agents.append(Agent(name, strategy))

    def addGreedyAgent(self, name=""):
        strategy = Greedy()
        self.agents.append(Agent(name, strategy))

    def addCooperativeAgent(self, name=""):
        strategy = Cooperative()
        self.agents.append(Agent(name, strategy))

    def reset(self):
        for agent in self.agents:
            agent.reset()


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
import time

s = Service(r'D:\Project\RemoteWork\selenium-test\Browsers\chromedriver.exe')
driver = webdriver.Chrome(service=s)

driver.maximize_window()
driver.get('https://lambdatest.github.io/sample-todo-app/')
time.sleep(5)

# click box
driver.find_element(by=By.NAME, value='li1').click()
driver.find_element(by=By.NAME, value='li2').click()
time.sleep(10)

# create to do
driver.find_element(by=By.ID, value='sampletodotext').send_keys('ketik sendiri')
time.sleep(5)

# click to do
driver.find_element(by=By.ID, value='addbutton').click()
time.sleep(10)

driver.close()

# agustyast@gmail.com
# Scrap131
# adidas yeez


import torch

def dice_similarity_coeff(y, yhat, thresholding=False):
    eps = 0.00001
    dice_th = 0.5

    # If thresholding is true, convert the estimated output to a binary image.
    if (thresholding):
        yhat[yhat > dice_th] = 1
        yhat[yhat <= dice_th] = 0

    yflat = y.contiguous().view(-1)
    yhatflat = yhat.contiguous().view(-1)

    intersection = torch.abs((yflat * yhatflat)).sum()
    y_sum = torch.sum(yflat)
    yhat_sum = torch.sum(yhatflat)

    return 1 - ((2. * intersection + eps) / (y_sum + yhat_sum + eps))


"""Charts for offer response binary classification.

Code is adapted from https://github.com/LailaSabar/Starbucks-Capstone-Challenge/blob/master/Starbucks_Capstone_notebook.ipynb
"""

import numpy as np
import pandas as pd
import seaborn as sns

import matplotlib.pyplot as plt
from sklearn.utils.multiclass import unique_labels

def plot_feature_importance(feature_imp):
    """Plot the top 10 important features
    """
    fig, ax = plt.subplots(figsize=(10, 10), nrows=1, ncols=1)
    sns.barplot(x=feature_imp.loc[0:10, 'imp_perc'],
                y=feature_imp.loc[0:10, 'feature'],
                color='#95DAC1',
                data=feature_imp)
    plt.xlabel('Percentage')
    plt.ylabel('Feature')
    plt.title('Top 10 important features')
    plt.show()

def plot_confusion_matrix(conf_matrix,
                          unique_classes,
                          normalized=False,
                          cmap=plt.cm.Greys):
    """Plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.

    Args:
        conf_matrix: confusion matrix calculated.
        unique_classes: (nd.array) unique labels from the target
        normalize: (boolean) used in chart title

    Returns:
     ax: matplotlib axes object.

    """
    np.set_printoptions(precision=2)

    if normalized:
        title = 'Normalized confusion matrix'
    else:
        title = 'Confusion matrix, without normalization'

    fig, ax = plt.subplots()
    im = ax.imshow(conf_matrix, interpolation='nearest', cmap=cmap)
    ax.figure.colorbar(im, ax=ax)
    # We want to show all ticks...
    ax.set(
        xticks=np.arange(conf_matrix.shape[1]),
        yticks=np.arange(conf_matrix.shape[0]),
        # ... and label them with the respective list entries
        xticklabels=unique_classes,
        yticklabels=unique_classes,
        title=title,
        ylabel='True label',
        xlabel='Predicted label')

    plt.setp(ax.get_xticklabels(),
             rotation=0,
             ha="right",
             rotation_mode="anchor")

    # Loop over data dimensions and create text annotations.
    fmt = '.2f' if normalized else 'd'
    thresh = conf_matrix.max() / 2.
    for i in range(conf_matrix.shape[0]):
        for j in range(conf_matrix.shape[1]):
            ax.text(j,
                    i,
                    format(conf_matrix[i, j], fmt),
                    ha="center",
                    va="center",
                    color="white" if conf_matrix[i, j] > thresh else "black")
    fig.tight_layout()
    return ax


import numpy
from cv2 import cv2
import base
import trackers.obj3dTracker as dec
import trackers.cornerDetect as corner

viewNoiseFilter = False
viewInput = True
viewRegions = False
viewFilter = True

ctrHSV = [0, 0, 0]
ctrBGR = [0, 0, 0]
ctrPos = (0, 0)
boundaries = ([0, 0, 0], [255, 255, 255])  # define the list of color HSV boundaries

blur = 1
trHigh = 255
trLow = 0
trVals = (255, 0, 1)
nVals = (5, 11, 1, 1)

cScr=0

def updateBnd(vals):
    global boundaries
    boundaries = (vals[0], vals[1])

def updateThr(vals):
    global trVals
    print(vals)
    trVals = vals

def updateNoise(vals):
    global nVals
    print(vals)
    nVals = vals

def adder(l, h):
    global boundaries
    boundaries.insert(0, (l, h))

def getCenterVal():  # ===============================
    global ctrPos,cScr
    y = int(len(cScr) / 2)
    x = int(len(cScr[0]) / 2)
    ctrPos = (x, y)
    bgr = numpy.uint8([[cScr[y][x]]])
    hsv = cv2.cvtColor(bgr, cv2.COLOR_BGR2HSV)[0][0]
    bgr = bgr[0][0]
    print("BGR", bgr, "HSV", hsv)
    return hsv

def searchColor(scr, lower, upper, thr, show):
    c = 0
    img = scr.copy()
    img = cv2.circle(img, (320, 240), 10, (0, 0, 255))

    # scr = cv2.resize(scr, (240, 160))
    base.view(show, "Input", img)

    lower = base.format(lower)
    upper = base.format(upper)

    mask = cv2.inRange(scr, lower, upper)

    output = cv2.bitwise_and(scr, scr, mask=mask)

    thresh = cv2.cvtColor(output, cv2.COLOR_HSV2BGR)
    thresh = cv2.cvtColor(thresh, cv2.COLOR_BGR2GRAY)
    ret, thresh = cv2.threshold(thresh, thr[1], thr[0], cv2.THRESH_BINARY)

    base.view(False, "Thresh", thresh)

    c += 1
    return thresh

def findRegions(thresh, area, circ):
    global viewRegions
    params = cv2.SimpleBlobDetector_Params()
    params.minThreshold = 3
    params.maxThreshold = 255
    params.filterByArea = True
    params.minArea = area
    params.filterByCircularity = True
    params.minCircularity = circ
    detector = cv2.SimpleBlobDetector_create(params)

    keypoints = detector.detect(thresh)
    trans_blobs = cv2.drawKeypoints(thresh, \
                                    keypoints, numpy.array([]), (0, 0, 255),
                                    cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    base.view(viewRegions, "Regions", trans_blobs)
    return keypoints

def loop(params, show,scr):
    global cScr
    cScr=scr
    scr = cv2.blur(scr, (params[0][2][2], params[0][2][2]))
    scr = cv2.cvtColor(scr, cv2.COLOR_BGR2HSV)
    threshes = []
    for p in params:
        if len(p) == 4:
            b = params.index(p) == show
            threshes.append(searchColor(scr, lower=p[0], upper=p[1], thr=p[2], show=b))
    #dec.track3d(threshes[0])
    #corner.harrisCorners(threshes[0])
    return threshes


from django.shortcuts import render,redirect
from django.http import HttpResponse
import datetime

x = datetime.datetime.now()
import json

def home(request):
    return render(request,'core/home.html')

def clip(request):
    key=request.POST.get("key")

    data=json.load(open('core/data.json','r',encoding='utf-8'))
    if key[:2] in data and key[2:]==x.strftime('%d%m%y'):
        finaldata=data[key[:2]]
    elif key[:2]=="xx":return redirect("https://prepinsta.com/elite/")
    else:finaldata=data['item']
    context={
        "renderdata":finaldata
    }
    return render(request,'core/yourclipboard.html',context)


from .base import GnuRecipe, SourceMixin

class NngRecipe(GnuRecipe):
    def __init__(self, *args, **kwargs):
        super(NngRecipe, self).__init__(*args, **kwargs)
        self.sha256 = '31dd7f2e7b8e5554a73837f1f2d1be10' \
                      'fc4c0cc75d70a5f491f756c107d81d9f'

        self.name = 'nng'
        self.version = '1.0.0-beta.1'
        self.version_regex = r'(?P<version>\d+\.\d+\.\d+)'
        self.depends = ['cmake']
        self.url = 'https://github.com/nanomsg/nng/archive/v$version.tar.gz'
        self.configure_args = self.cmake_args

class NngSourceRecipe(SourceMixin, NngRecipe):
    def __init__(self, *args, **kwargs):
        super(NngSourceRecipe, self).__init__(*args, **kwargs)


#!/usr/bin/python
'''
This is a module docstring
'''

from pathlib import Path
import paramiko
import netifaces
from ansible.module_utils.basic import AnsibleModule

# Globals
PACKAGE_TO_INSTALL = None
ROUTER = None
USER = None
STATE = None
SSH_IDENTITY = None
CHECK_MODE = False
changed = False
UPDATE = True
DEBUG = False
response = {}

def get_router(router):
    '''
    This is a function docstring
    '''

    if router is None:
        gws = netifaces.gateways()
        return gws['default'][netifaces.AF_INET][0]
    return router

def get_ssh_key():
    '''
    This is a function docstring
    '''

    ssh_key = ''
    if SSH_IDENTITY is not None:
        print("SSH_IDENTITY = {}".format(SSH_IDENTITY))
        ssh_key = paramiko.RSAKey.from_private_key_file(SSH_IDENTITY)
    else:
        keyfile = '{}/.ssh/id_rsa'.format(str(Path.home()))
        ssh_key = paramiko.RSAKey.from_private_key_file(keyfile)
    return ssh_key

def run_command_on_router(command):
    '''
    This is a function docstring
    '''

    global ROUTER

    ssh_key = get_ssh_key()
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=ROUTER, username=USER, pkey=ssh_key)

    _, ssh_stdout, ssh_stderr = ssh.exec_command(command)

    ssh_stdout_lines = ssh_stdout.read().decode('ascii').split('\n')
    ssh_stderr_lines = ssh_stderr.read().decode('ascii').split('\n')

    return (ssh_stdout_lines, ssh_stderr_lines)

def get_package_version():
    '''
    This is a function docstring
    '''

    cmd_to_execute = 'opkg status {}'.format(PACKAGE_TO_INSTALL)
    stdout, _ = run_command_on_router(cmd_to_execute)

    for line in stdout:
        if line.startswith('Version: '):
            return line.split(' ')[1]
    return None

def add_standard_debug_response(params):
    if DEBUG:
        response['module.params'] = params

def update_package_list():
    if UPDATE:
        cmd_to_execute = 'opkg update'
        _, _ = run_command_on_router(cmd_to_execute)

def main():
    '''
    This is a function docstring
    '''

    global PACKAGE_TO_INSTALL
    global ROUTER
    global USER
    global STATE
    global SSH_IDENTITY
    global CHECK_MODE
    global changed
    global UPDATE
    global DEBUG
    global response

    module = AnsibleModule(
        argument_spec={
            "pkg":          {"required": True, "type": "str"},
            "router":       {"required": False, "type": "str"},
            "user":         {"required": False, "default": "root", "type": "str"},
            "state":        {"default": "present", "choices": ['present',
                                                               'absent'], "type": 'str'},
            "version":      {"required": False, "type": "str"},
            "ssh_identity": {"required": False, "type": "str"},
            "refresh":      {"default": "False", "type": "str" },
            "debug":        {"default": "False", "type": "str" }
        },
        supports_check_mode=True
    )

    # Parse the parameters, to make the code more readable
    PACKAGE_TO_INSTALL = module.params['pkg']
    ROUTER = get_router(module.params['router'])
    USER = module.params['user']
    STATE = module.params['state']
    SSH_IDENTITY = module.params['ssh_identity']
    CHECK_MODE = module.check_mode
    changed = False
    UPDATE = module.params['refresh']
    DEBUG = module.params['debug']

    response = {}

    add_standard_debug_response(module.params)

    update_package_list()

    if STATE == 'present':
        if not CHECK_MODE:
            # We're not in check mode, so need to actually do it
            cmd_to_execute = 'opkg install {}'.format(PACKAGE_TO_INSTALL)
            stdout, _ = run_command_on_router(cmd_to_execute)
            response['reason'] = \
                'Requested state was {} and {} was either not installed or had an update' \
                .format(STATE, PACKAGE_TO_INSTALL)
            response['stdout_lines'] = stdout
            changed = True
        else:
            response['reason'] = \
                'Requested state was {} and {} was either not installed or had an update, but \
                    wasn\'t installed because check mode is enabled'.format(
                STATE, PACKAGE_TO_INSTALL)
    else:
        # State should be absent, so check if the package is installed and remove it
        if not CHECK_MODE:
            # The package is installed
            cmd_to_execute = 'opkg remove {}'.format(PACKAGE_TO_INSTALL)
            stdout, _ = run_command_on_router(cmd_to_execute)
            response['reason'] = 'Requested state was {}, so {} was removed'.format(
                STATE, PACKAGE_TO_INSTALL)
            response['stdout_lines'] = stdout
            if stdout[0].startswith('Refusing to remove essential package'):
                module.fail_json(msg=stdout[0], meta=response)
            changed = True
            if stdout[0] == "No packages removed.":
                response['reason'] = \
                    'Requested state was {}, but {} wasn\'t installed so no action was taken'\
                        .format(STATE, PACKAGE_TO_INSTALL)
                changed = False
        else:
            response['reason'] = \
                'Requested state was {}, but check mode is enabled so {} was not removed'.format(
                STATE, PACKAGE_TO_INSTALL)

    module.exit_json(changed=changed, meta=response)

if __name__ == '__main__':
    main()


from libkc3ds.parts import C3, C3_a, C3_0, C3_b, C3_c, C3_d, C3_e, C3_f, C3_g, C3_h, C3_i, C3_j, C3_k, C3_l, C3_m, C3_n

class CSet():
	"""
	A Set covers 56 sprite files for a C3/DS Norn.
	The other 56 (for the other sex) are simply copies. (Checked against ChiChi template)
	self.ages maps age chars to CAge.
	"""
	def __init__(self, name, desc, setup, ages):
		self.name = name
		self.desc = desc
		self.setup = setup
		self.ages = {}
		for age in ages:
			for char in age.chars:
				self.ages[char] = age

class CAge():
	"""
	self.parts maps part names to AgedPart.
	"""
	def __init__(self, chars, scale, parts):
		self.chars = chars
		self.scale = scale
		self.parts = {}
		for part in parts:
			self.parts[part.part_id.name] = part

class AgedPart():
	def __init__(self, part_id, size):
		self.part_id = part_id
		self.size = size


from __future__ import unicode_literals

import frappe
from frappe import _

@frappe.whitelist()
def before_validate(doc, method):
    validate_item_variant_for_meters_uom(doc)
    if doc.is_new():
        set_default_item_attributes(doc)

def validate_item_variant_for_meters_uom(doc):
    if doc.stock_uom == 'Meter':
        if not doc.has_variants and not doc.variant_of:
            frappe.throw(_("Either has_variants should be true, or variant_of should be set, if stock_uom is 'Meters'"))
    else:
        if doc.has_variants:
            frappe.throw(_("has_variants should be false, if stock_uom is not 'Meters'"))

def set_default_item_attributes(doc):
    """
    Set default Item Attribute to "Category" and "Shade" for New Item Template
    """

    if doc.is_new() and doc.has_variants and doc.variant_based_on == "Item Attribute":
        item_attributes = list(map(lambda d: d.attribute, doc.attributes))

        attributes_to_add = ["Shade"]
        for attribute in attributes_to_add:
            if frappe.db.exists("Item Attribute", attribute):
                if attribute not in item_attributes:
                    doc.append("attributes", {
                        "attribute": attribute
                    })


# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from . import __version__ as app_version

app_name = "brew"
app_title = "Brewing"
app_publisher = "Metalman Brewing"
app_description = "App for managing brews from Prep to Package"
app_icon = "octicon octicon-beaker"
app_color = "#589494"
app_email = "admin@metalmanbrewing.com"
app_license = "TBD"

# Includes in <head>
# ------------------

# include js, css files in header of desk.html
# app_include_css = "/assets/brew/css/brew.css"
# app_include_js = "/assets/brew/js/brew.js"

# include js, css files in header of web template
# web_include_css = "/assets/brew/css/brew.css"
# web_include_js = "/assets/brew/js/brew.js"

# Home Pages
# ----------

# application home page (will override Website Settings)
# home_page = "login"

# website user home page (by Role)
# role_home_page = {
#	"Role": "home_page"
# }

# Website user home page (by function)
# get_website_user_home_page = "brew.utils.get_home_page"

# Generators
# ----------

# automatically create page for each record of this doctype
# website_generators = ["Web Page"]

# Installation
# ------------

# before_install = "brew.install.before_install"
# after_install = "brew.install.after_install"

# Desk Notifications
# ------------------
# See frappe.core.notifications.get_notification_config

# notification_config = "brew.notifications.get_notification_config"

# Permissions
# -----------
# Permissions evaluated in scripted ways

# permission_query_conditions = {
# 	"Event": "frappe.desk.doctype.event.event.get_permission_query_conditions",
# }
#
# has_permission = {
# 	"Event": "frappe.desk.doctype.event.event.has_permission",
# }

# Document Events
# ---------------
# Hook on document methods and events

# doc_events = {
# 	"*": {
# 		"on_update": "method",
# 		"on_cancel": "method",
# 		"on_trash": "method"
#	}
# }

# Scheduled Tasks
# ---------------

# scheduler_events = {
# 	"all": [
# 		"brew.tasks.all"
# 	],
# 	"daily": [
# 		"brew.tasks.daily"
# 	],
# 	"hourly": [
# 		"brew.tasks.hourly"
# 	],
# 	"weekly": [
# 		"brew.tasks.weekly"
# 	]
# 	"monthly": [
# 		"brew.tasks.monthly"
# 	]
# }

# Testing
# -------

# before_tests = "brew.install.before_tests"

# Overriding Whitelisted Methods
# ------------------------------
#
# override_whitelisted_methods = {
# 	"frappe.desk.doctype.event.event.get_events": "brew.event.get_events"
# }

#export custom fields as well
fixtures = ["Custom Field", "Custom Script", "Workflow", "Workflow State", "Role", "Property Setter"]


# -*- coding: utf-8 -*-
import re
import os
import django
import scrapy
import requests
import json
import datetime
from os import sys, path
from scrapy.selector import Selector
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

from lxml import etree
from scrapy.utils.log import configure_logging
import pdb
import time
import logging
import ast
import base64
import httplib
import schedule
import threading
from .my_thread import MyThread
import signal

sys.path.append(path.dirname(path.dirname(path.dirname(path.dirname(path.abspath(__file__))))))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "olx_site.settings")
django.setup()

from product.models import *
from product.views import *

class OlxSpider(scrapy.Spider):
    name = "olx-origin"

    url = [
        'https://www.olx.ua/detskiy-mir/?search%5Bprivate_business%5D=private',
        'https://www.olx.ua/nedvizhimost/?search%5Bprivate_business%5D=private',
        'https://www.olx.ua/transport/?search%5Bprivate_business%5D=private',
        'https://www.olx.ua/zhivotnye/?search%5Bprivate_business%5D=private',
        'https://www.olx.ua/dom-i-sad/?search%5Bprivate_business%5D=private',
        'https://www.olx.ua/elektronika/?search%5Bprivate_business%5D=private',
        'https://www.olx.ua/uslugi/?search%5Bprivate_business%5D=private',
        'https://www.olx.ua/moda-i-stil/?search%5Bprivate_business%5D=private',
        'https://www.olx.ua/hobbi-otdyh-i-sport/?search%5Bprivate_business%5D=private',
        'https://www.olx.ua/otdam-darom/?search%5Bfilter_float_price%3Afrom%5D=free&search%5Bprivate_business%5D=private',
        'https://www.olx.ua/obmen-barter/?search%5Bfilter_float_price%3Afrom%5D=exchange&search%5Bprivate_business%5D=private'
    ]

    domain = "olx.ua"

    total_cycle_array = []

    country_code = 'UA'

    check_mobile_prefix = ['39', '38', '50', '63', '66', '67', '68', '91', '92', '93', '94', '95', '96', '97', '98', '99']

    international_code = '380'

    iterator_in_one_cycle = -1

    category_index = 0

    gear = [0, 1, 5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000, 100000, 1000000]

    total_count = 0

    def __init__(self):

        logfile = open('testlog' + datetime.datetime.today().strftime('%Y-%m-%d') + '.log' , 'w')
        configure_logging(install_root_handler = False)
        logging.basicConfig (
           filename = logfile,
           format = '%(levelname)s: %(your_message)s',
           level = logging.INFO
        )
        self.current_page = 1

    def start_requests(self):
        yield scrapy.Request('https://google.com',callback=self.parse)

    def parse(self, response):
        """
            Main scraping module
        """
        self.website = ClassifiedWebsites.objects.filter(domain=self.domain).first()
        self.proxies = self.get_or_create_proxies_for_website(self.website)
        self.sleep_time = 7.5;
        self.scrapy_history = ScrapingHistory.objects.create(scraper=self.domain, links_found=0, links_unique=0, numbers_found=0, numbers_unique=0, numbers_non_matched=0, active_proxies=[], sleep_time=self.sleep_time)
        for i in range(0, len(self.proxies)):
            self.scrapy_history.active_proxies.append(len(self.proxies))
        self.scrapy_history.save()

        update_proxy_thread = MyThread(name='child procs', target=self.update_active_proxies)
        update_proxy_thread.start()

        self.scrapy_cycle_history = None
        try:
            self.scrapy_cycle_history = ScraypingCycleHistory.objects.latest()
            if self.get_difference_days(self.scrapy_cycle_history.updated) > 5:
                self.scrapy_cycle_history = ScraypingCycleHistory.objects.create(scraper=self.domain, category_index=0, current_page=1, first_link='', last_link='', cycle_index=0)
        except:
            self.scrapy_cycle_history = ScraypingCycleHistory.objects.create(scraper=self.domain, category_index=0, current_page=1, first_link='', last_link='', cycle_index=0)

        self.category_index = self.scrapy_cycle_history.category_index
        self.current_page = self.scrapy_cycle_history.current_page

        while self.category_index < len(self.url):
            self.range_first_value = 1
            gear_index = 1

            while (self.range_first_value + self.gear[gear_index]) < 99000000:
                self.total_count = 0
                try:
                    count_proxy_per_cycle = len(self.proxies)

                    self.setup_proxy_check_xpath(self.url[self.category_index], '//*[@id="body-container"]/div[3]/div/div[4]/span[15]//span/text()')
                    content = etree.HTML(self.driver.page_source.encode('utf8'))
                    max_page_number = int(content.xpath('//*[@id="body-container"]/div[3]/div/div[4]/span[15]//span/text()')[0])

                    # count the url
                    current_url_num = 0

                    # count the available mobile number
                    current_mobile_num = 0

                    # cycle number
                    cycle_num = 0

                    while self.current_page <= max_page_number:
                        print(self.url[1]+'&page='+str(self.current_page))

                        self.setup_proxy_check_xpath(self.url[1]+'&page='+str(self.current_page), '//table//tr[@class="wrap"]//a[contains(@class, "thumb")]/@href')
                        content = etree.HTML(self.driver.page_source.encode('utf8'))
                        url_list = content.xpath('//table//tr[@class="wrap"]//a[contains(@class, "thumb")]/@href')

                        for url in url_list:
                            if self.domain not in url:
                                continue
                            if self.check_url_twice(url):
                                continue

                            address = ""
                            phone = ""

                            while address == "":
                                self.setup_proxy_check_xpath(url, '//*[@id="offerdescription"]/div[2]/div[1]/a/strong/text()')

                                try:
                                    phone_elem = self.driver.find_element_by_xpath('//*[@id="contact_methods"]/li[2]/div')
                                    phone_elem.click()
                                except:
                                    break

                                time.sleep(3)
                                main_cont = etree.HTML(self.driver.page_source.encode('utf8'))
                                try:
                                    address = main_cont.xpath('//*[@id="offerdescription"]/div[2]/div[1]/a/strong/text()')[0]
                                except:
                                    print("+++++++++++++++++ address empty +++++++++++++++")
                                    continue

                                try:
                                    phone = main_cont.xpath('//*[@id="contact_methods"]/li[2]/div/strong//text()')[0]
                                except:
                                    print("++++++++++++++++++ phone empty ++++++++++++++")

                            ########## end while
                            self.scrapy_history.links_found += 1

                            if phone is not '':
                                phone = self.filter_mobile(phone)
                                self.scrapy_history.numbers_found += 1

                                if not self.check_phone_number(phone):
                                    self.scrapy_history.numbers_non_matched += 1
                                    continue

                                current_mobile_num += 1

                                ##? self.scrapy_history.numbers_unique
                            else:
                                continue

                            if address is not '':
                                current_url_num += 1
                                self.scrapy_cycle_history.cycle_index = cycle_num

                                try:
                                    self.total_cycle_array[cycle_num][1] = datetime.datetime.today().strftime('%Y-%m-%d') + '&' + url
                                    self.scrapy_cycle_history.last_link = self.total_cycle_array[cycle_num][1]
                                except IndexError:
                                    self.total_cycle_array.append(['', ''])
                                    self.total_cycle_array[cycle_num][0] = datetime.datetime.today().strftime('%Y-%m-%d') + '&' + url
                                    self.scrapy_cycle_history.first_link = self.total_cycle_array[cycle_num][0]

                                self.scrapy_cycle_history.save()

                                it_cycle = self.iterator_in_one_cycle % count_proxy_per_cycle

                                if it_cycle == count_proxy_per_cycle - 1:
                                    """
                                        Optimize the sleep time
                                    """
                                    cycle_num += 1
                                    time.sleep(self.sleep_time)

                                self.scrapy_history.links_unique += 1
                                ##? self.scrapy_history.links_unique

                                self.scrapy_history.save()
                                self.save_data(address, phone)

                                self.total_count += 1

                                # print('======================================')
                                # print(address)
                                # print(phone)
                                # print(url)
                                # print(current_url_num)
                                # print('=========================================')

                        self.current_page += 1
                        self.scrapy_cycle_history.current_page = self.current_page
                        self.scrapy_cycle_history.save()

                    self.driver.quit()

                    if self.total_count >= 20000:
                        gear_index -= 1
                        if self.gear[gear_index] == 0:
                            print('{} category has max count {}'.format(self.category_index, self.total_count))
                            break

                    if self.total_count  <= 2000:
                        gear_index += 1

                    self.range_first_value = self.range_first_value + self.gear[gear_index] + 1

                except Exception as err:
                    print(err)
                    time.sleep(60)
                    yield scrapy.Request('https://google.com',callback=self.parse, errback=self.parse, dont_filter=True)

            self.category_index += 1
            self.scrapy_cycle_history.category_index = self.category_index
            self.scrapy_cycle_history.save()

        update_proxy_thread.stop()
        update_proxy_thread.join()

        signal.signal(signal.SIGINT, self.kill_threads)
        signal.pause()

    def setup_proxy_check_xpath(self, url, xpath_string):
        """
            - Request with @url on webdriver using phantomJS for headless browser

            - confirm with @xpath_string if webpage is full-downloaded

                Loop and request until webpage is full
        """
        while True:
            self.iterator_in_one_cycle = (self.iterator_in_one_cycle + 1) % len(self.proxies)
            proxy = self.proxies[self.iterator_in_one_cycle].proxy
            service_args=[]
            # service_args=['--ignore-ssl-errors=true',
            #                '--ssl-protocol=any',
            #                '--load-images=false']

            # service_args.append('--proxy={}:{}'.format(proxy['ip'], proxy['port']))
            service_args.append('--proxy={}:{}'.format(proxy.ip, proxy.port))
            # service_args.append('--web-security=no')
            # service_args.append('--proxy-type=http')

            if proxy.username and proxy.password:
                service_args.append('--proxy-auth={}:{}'.format(proxy.username, proxy.password))

            print("################ proxy #########################")
            print('Proxy {}:{}'.format(proxy.ip, proxy.port))
            # print('Proxy {}:{}'.format(proxy['ip'], proxy['port']))
            print("################ proxy #########################")

            capabilities = DesiredCapabilities.PHANTOMJS
            # capabilities["phantomjs.page.settings.userAgent"] = ("Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36")
            capabilities['phantomjs.page.settings.resourceTimeout'] = 50000

            # self.driver = webdriver.PhantomJS(service_args=service_args)
            self.driver = webdriver.PhantomJS(service_args=service_args,
                                    desired_capabilities=capabilities,
                                    service_log_path='/tmp/ghostdriver.log')

            self.driver.set_window_size(1120, 1080)
            # self.driver.implicitly_wait(2)
            self.driver.set_page_load_timeout(50)

            try:
                self.driver.get(url)
            except httplib.BadStatusLine as bsl:
                print(bsl)
                self.update_or_remove_proxy(self.proxies[self.iterator_in_one_cycle])
                continue
            except TimeoutException as e:
                print(e)
                self.update_or_remove_proxy(self.proxies[self.iterator_in_one_cycle])
                continue
            except Exception as e:
                print(str(e))
                continue
            except WebDriverException as e:
                print(e)
                continue
            except KeyboardInterrupt as e:
                print(e)
                self.update_or_remove_proxy(self.proxies[self.iterator_in_one_cycle])
                continue
            except Exception as e:
                print(e)
                continue

            content = etree.HTML(self.driver.page_source.encode('utf8'))
            element = content.xpath(xpath_string)

            if len(element) == 0:
                continue

            break

    def kill_threads(self, signal, frame):
        os._exit(1)

    def get_difference_days(self, old_date):
        """
            get different days with old date from today
            @param:
                old_date: old date
            @return:
                different days
        """
        d1 = datetime.datetime.utcfromtimestamp(old_date.created_utc)
        result = datetime.datetime.utcnow() - d1
        return result.days

    def filter_mobile(self, num):
        """
            validating and fixing phone number
            @param:
                num: phone number
            @return: validated number
        """
        if num == '':
            return 0

        num = num.strip().replace(" ", "")
        num = str(int(filter(str.isdigit, num)))

        if len(num) < 11:
            num = self.international_code + str(num)

        if num.startswith(self.international_code) and len(num) == 13:
            num = num.replace('3800', '380')

        return int(num)

    def check_phone_number(self, phone):
        """
            check if given phone number has regular prefix
            @param:
                phone: phone number
            @return: True or False
        """
        # check if international code
        if str(phone).startswith(self.international_code):
            prefix = str(phone)[3:5]
            if prefix in self.check_mobile_prefix:
                return True

        return False

    def check_url_twice(self, url):
        """
            Check if url was already scraped
            @param:
                url: ad full link
            @return:
                True or False
        """
        path = url.replace('https://www.olx.ua', '').split('#')[0]
        hashed_path = path
        count = ScrapedLinks.objects.filter(hashed_path__iexact=hashed_path).count()

        if count > 0:
            return True

        ScrapedLinks.objects.create(scraper=self.domain, path=path, hashed_path=hashed_path)
        return False

    def get_proxies(self, website):
        """
            Get valid proxies from Proxies table which are not status "suspended"

            @param:
                website: scraped domain
            @return:
                valid proxy list
        """
        country_codes = website.proxy_countries
        country_codes = ast.literal_eval(country_codes)

        alt_proxies = Proxies.objects.filter(country_code__in=country_codes).all()

        proxies = []

        for proxy in alt_proxies:
            try:
                if proxy.classifiedwebsitesproxies_set.first().status == 'suspended':
                    continue
            except:
                pass

            proxies.append(proxy)

        if len(proxies) > website.max_proxies:
            proxies = proxies[:website.max_proxies]

        return proxies

    def update_or_remove_proxy(self, classified_proxy):
        """
            Increase suspended_level once proxy is suspended
            Update status, 'suspended' when level is more than 50 ( times )
            At that time, refresh proxy list from table

            @param:
                classified_proxy: proxy which is suspended once.
            @return:
                Null
        """
        classified_proxy.suspended_level += 1
        classified_proxy.save()
        if classified_proxy.suspended_level >= 50:
            classified_proxy.status = 'suspended'
            classified_proxy.save()

            self.proxies = self.get_or_create_proxies_for_website(self.website)

    def get_or_create_proxies_for_website(self, website):
        """
            Insert proxy to classified_website_proxies table

            return proxy list for scraped domain

            @param:
                website: scrap domain
            @return:
                valid proxy list
        """
        proxies = self.get_proxies(website)
        for proxy in proxies:
            try:
                if ClassifiedWebsitesProxies.objects.filter(classified=website, proxy=proxy, status='online').count() > 0:
                    continue

                ClassifiedWebsitesProxies.objects.create(classified=website, proxy=proxy, suspended_level=0, status='online')
            except Exception as ex:
                print(ex);

        return ClassifiedWebsitesProxies.objects.filter(classified=website, status='online').all()

    def _update_active_proxies(self):
        proxies = self.get_or_create_proxies_for_website(self.website)
        del self.scrapy_history.active_proxies[0]
        self.scrapy_history.active_proxies.append(len(proxies))

    def update_active_proxies(self):
        """
            update active proxies per hour after checking online proxies from table.
        """
        schedule.every().hour.do(self._update_active_proxies)
        while True:
            schedule.run_pending()
            time.sleep(1)

    def save_data(self, address, phone):
        """
            Save address and phone and check result OUTPUT
            @param:
                address: scraped address
            @return:
                Null
        """
        city_name = ''
        area_name = ''
        district_name = ''

        if len(address.split(',')) > 0:
            area_name = address.split(',')[0].strip()
        if len(address.split(',')) > 1:
            city_name = address.split(',')[1].strip()
        if len(address.split(',')) > 2:
            district_name = address.split(',')[2].strip()

        city = Cities.objects.filter(display_name__iexact=city_name).first()
        area = Areas.objects.filter(display_name__iexact=area_name).first()
        district = Districts.objects.filter(display_name__iexact=district_name).first()

        if not area:
            self.number_save_and_log(phone, None, None, None, city_name, area_name, district_name, '111')
        elif not city:
            self.number_save_and_log(phone, city.id, None, None, city_name, area_name, district_name, '011')
        elif not district:
            self.number_save_and_log(phone, city.id, area.id, None, city_name, area_name, district_name, '001')
        else:
            self.number_save_and_log(phone, city.id, area.id, district.id, city_name, area_name, district_name, '000')

        if not city and area and district:
            logging.debug('############  country, city, area, district LOG ####################')
            logging.debug('country:'+str(self.country_code) + ' city:' + str(city_name.encode('utf8')) + ' area:' + str(area_name.encode('utf8')) + ' district:' + str(district_name.encode('utf8')) + ' number:' + str(phone) + ' result' + ' 100')

        if city and not area and district:
            logging.debug('############  country, city, area, district LOG ####################')
            logging.debug('country:'+str(self.country_code) + ' city:' + str(city_name.encode('utf8')) + ' area:' + str(area_name.encode('utf8')) + ' district:' + str(district_name.encode('utf8')) + ' number:' + str(phone) + ' result' + ' 010')

        if city and area and not district:
            logging.debug('############  country, city, area, district LOG ####################')
            logging.debug('country:'+str(self.country_code) + ' city:' + str(city_name.encode('utf8')) + ' area:' + str(area_name.encode('utf8')) + ' district:' + str(district_name.encode('utf8')) + ' number:' + str(phone) + ' result' + ' 110')

    def number_save_and_log(self, phone, city_id, area_id, district_id, city_name, area_name, district_name, result):
        """
            Save valid row to mobbile_numbers database and Log
            @param:
                city_id: id of cities table for current address
                area_id: id of areas table for current address
                district_id: id of districts table for current address
                city_name: city name of current address
                area_name: area name of current address
                district_name: district name of current address
                result: OUTPUT

            @return:
                Null
        """
        try:
            MobileNumbers.objects.create(country_code=self.country_code, city_id=city_id, area_id=area_id, district_id=district_id, number=int(phone), postal_code_id=0)
            self.scrapy_history.numbers_unique += 1
        except Exception as e:
            self.scrapy_history.numbers_non_matched += 1
            print(e)

        self.scrapy_history.save()

        logging.info('country:'+str(self.country_code) + ' city:' + str(city_name.encode('utf8')) + ' area:' + str(area_name.encode('utf8')) + ' district:' + str(district_name.encode('utf8')) + ' number:' + str(phone) + ' result' + result)


import sys
input=sys.stdin.readline().rstrip()

def main():
    mingyeom=input
    minimum=find_min(mingyeom)
    maximum=find_max(mingyeom)
    print(maximum)
    print(minimum)

def find_min(text):
    number=''
    plus=0
    for i in text:
        if i=='M':
            plus+=1
        else:
            if plus>0:
                plus=(10**(plus-1))
                number+=str(plus)
                plus=0
            number+='5'
    if plus!=0:
        plus=(10**(plus-1))
        number+=str(plus)
    return int(number)

def find_max(text):
    number=''
    plus=0
    for i in range(len(text)):
        if text[i]=='M':
            plus+=1
        elif text[i]=='K':
            plus=(5*(10**plus))
            number+=str(plus)
            plus=0

    for i in range(plus):
        number+='1'
    return int(number)

if __name__=="__main__":
    main()

'''
MKKMMK

505500
155105
'''


#
#
#
# TextModel project!
#
# Name(s): Ray Song
#
from porter import create_stem
import math

class TextModel(object):
    """A class supporting complex models of text."""

    def __init__(self):
        """Create an empty TextModel."""
        #
        # Create dictionaries for each characteristic
        #
        self.words = {}           # For counting words
        self.wordlengths = {}     # For counting word lengths
        self.stems = {}           # For counting stems
        self.sentencelengths = {} # For counting sentence lengths
        #
        # Create another of your own
        #
        self.punct = {}     # For counting punctuations

    def __repr__(self):
        """Display the contents of a TextModel."""
        s = 'Words:\n' + str(self.words) + '\n\n'
        s += 'Word lengths:\n' + str(self.wordlengths) + '\n\n'
        s += 'Stems:\n' + str(self.stems) + '\n\n'
        s += 'Sentence lengths:\n' + str(self.sentencelengths) + '\n\n'
        s += 'Punctuation:\n' + str(self.punct)
        return s

    # Include other functions here.
    # In particular, you'll need functions that add to the model.
    def readTextFromFile(self,filename):
        """This method reads a text and stores it in the self.text"""
        f=open(filename)
        text=f.read()
        f.close()
        self.text=text

    def makeSentenceLengths(self):
        """This method creates a dictionary of numbers of
           different lengths of sentences and stores in
           self.sentencelengths
        """
        LoW=self.text.split()
        d={}
        count=0
        for w in LoW:
            if not (w[-1]=="." or w[-1]=="?" or w[-1]=="!"):
                count+=1
            elif count==0:
                pass
            elif count+1 not in d:
                d[count+1]=1
                count=0
            else:
                d[count+1]+=1
                count=0
        self.sentencelengths=d

    def cleanString(self,s):
        """This method returns the text without upper case
           letters and punctuations
        """
        text=s.lower()
        new_text=""
        for i in text:
            if i=="," or i==";" or i=="?" or i=="." or i=="!" or i=="'" :
                pass
            else:
                new_text+=i
        self.text=new_text
        return new_text

    def makeWordLengths(self):
        """This method creates a dictionary of numbers of
           word lengths of sentences and stores in
           self.wordlengths
        """
        LoW=self.text.split()
        d={}
        for w in LoW:
            if len(w) in d:
                d[len(w)]+=1
            else:
                d[len(w)]=1
        self.wordlengths=d

    def makeWords(self):
        """This method creates a dictionary of numbers of
           word of sentences and stores in self.words
        """
        LoW=self.text.split()
        d={}
        for w in LoW:
            if w in d:
                d[w]+=1
            else:
                d[w]=1
        self.words=d

    def makeStems(self):
        """This method creates a dictionary of numbers of
           word lengths of sentences and stores in
           self.wordlengths
        """
        LoW=self.text.split()
        d={}
        for w in LoW:
            if create_stem(w) in d:
                d[create_stem(w)]+=1
            else:
                d[create_stem(w)]=1
        self.stems=d

    def isPunct(self,w):
        """This boolean method returns when a string is
           a punctuation mark
        """
        return w=="!" or w=="." or w=="," or w==":" or w=="'"\
        or w=="?"

    def makePunctuation(self):
        """This method creates a dictionary of numbers of
           word lengths of sentences and stores in
           self.punct
        """
        d={}
        for w in self.text:
            if self.isPunct(w):
                if w in d:
                    d[w]+=1
                else:
                    d[w]=1
        self.punct=d

    def normalizeDictionary(self,d):
        """This method should accept any single one of the model
        dictionaries d and return a normalized version
        """
        total=sum(d.values())
        nd={}
        for k in d:
            nd[k]=d[k]/total
        return nd

    def smallestValue(self, nd1, nd2):
        """This method should accept any two model dictionaries
        d1 and nd2 and should return the smallest positive (that
        is, non-zero) value across them both.
        """
        small=sum(nd2.values())+sum(nd1.values())
        for k in nd1:
            if nd1[k]<small:
                small=nd1[k]

        for k in nd2:
            if nd2[k]<small:
                small=nd2[k]
        return small

    def compareDictionaries(self, d, nd1, nd2):
        """This method computes and returns The log-probability that
        the dictionary d arose from the distribution of data in the
        normalized dictionary nd1 and The log-probability that
        dictionary d arose from the distribution of data in normalized
        dictionary nd2
        """

        total_log_prob1=0.0
        total_log_prob2=0.0
        epsilon=self.smallestValue(nd1,nd2)/2
        for k in d:
            if k not in nd1:
                total_log_prob1+=math.log(epsilon)
            else:
                total_log_prob1+=d[k]*math.log(nd1[k])

            if k not in nd2:
                total_log_prob2+=math.log(epsilon)
            else:
                total_log_prob2+=d[k]*math.log(nd2[k])
        return [total_log_prob1,total_log_prob2]

    def createAllDictionaries(self):
        """Create out all five of self's
           dictionaries in full.
        """
        self.makeSentenceLengths()
        self.makeWords()
        self.makeStems()
        self.makePunctuation()
        self.makeWordLengths()

    def compareTextWithTwoModels(self, model1, model2):
        """Print out the comparative results of the log-probabilities
           for all five dictionaries
        """
        self.createAllDictionaries()
        model1.createAllDictionaries()
        model2.createAllDictionaries()
        sL=self.compareDictionaries(self.sentencelengths,\
        self.normalizeDictionary(model1.sentencelengths),\
        self.normalizeDictionary(model2.sentencelengths))
        mW=self.compareDictionaries(self.words,\
        self.normalizeDictionary(model1.words),\
        self.normalizeDictionary(model2.words))
        mS=self.compareDictionaries(self.stems,\
        self.normalizeDictionary(model1.stems),\
        self.normalizeDictionary(model2.stems))
        mP=self.compareDictionaries(self.punct,\
        self.normalizeDictionary(model1.punct),\
        self.normalizeDictionary(model2.punct))
        mL=self.compareDictionaries(self.wordlengths,\
        self.normalizeDictionary(model1.wordlengths),\
        self.normalizeDictionary(model2.wordlengths))

        names=["model1","model2"]
        tot=[]
        tot.append(names)
        tot.append(sL)
        tot.append(mW)
        tot.append(mS)
        tot.append(mP)
        tot.append(mL)

        points1=0
        points2=0
        for i in range(1,6):
            if tot[i][0]>tot[i][1]:
                points1+=1
            elif tot[i][0]<tot[i][1]:
                points2+=1

        tot.append([points1,points2])

        columns=["name","sentencelengths","words","stems","punctuation","wordlengths","total"]
        for i in range(len(columns)):
            print("%-15s %-15s %-15s" %(columns[i],tot[i][0],tot[i][1]))

        if points1>points2:
            print ("Model1 has a larger total possibility. Model1 wins")
        else:
            print ("Model2 has a larger total possibility. Model2 wins")

print(" +++++++++++ Model1 +++++++++++ ")
TM1 = TextModel()
TM1.readTextFromFile("train1.txt")
TM1.createAllDictionaries()  # provided in hw description
print(TM1)

print(" +++++++++++ Model2 +++++++++++ ")
TM2 = TextModel()
TM2.readTextFromFile("train2.txt")
TM2.createAllDictionaries()  # provided in hw description
print(TM2)

print(" +++++++++++ Unknown text +++++++++++ ")
TM_Unk = TextModel()
TM_Unk.readTextFromFile("unknown.txt")
TM_Unk.createAllDictionaries()  # provided in hw description
print(TM_Unk)

TM = TextModel()
d1 = {'a': 5, 'b':1, 'c':2}
nd1 = TM.normalizeDictionary(d1)
d2 = {'a': 15, 'd':1}
nd2 = TM.normalizeDictionary(d2)
print("The normalized dictionaries are:")
print(nd1)
print(nd2)
sm_va = TM.smallestValue(nd1, nd2)
print("and the smallest value between them is", sm_va)

TM1 = TextModel()
TM1.readTextFromFile("train1.txt")
TM2 = TextModel()
TM2.readTextFromFile("train2.txt")
TM_Unk = TextModel()
TM_Unk.readTextFromFile("unknown.txt")
TM_Unk.compareTextWithTwoModels(TM1,TM2)