from datetime import datetime

from flask_login import current_user, UserMixin
from flaskz import res_status_codes
from flaskz.log import flaskz_logger
from flaskz.models import ModelBase, ModelMixin, BaseModelMixin
from flaskz.rest import get_rest_log_msg
from flaskz.utils import find_list, get_app_cache, set_app_cache, get_ins_mapping
from sqlalchemy import Column, Integer, String, Table, ForeignKey, DateTime, Boolean, Text, desc
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import relationship
from werkzeug.security import generate_password_hash, check_password_hash

from ..modules import AutoModelMixin

# 模块&动作关联
# #module#      #action#
# user          add
# user          update
# user          config_email
sys_module_actions = Table('sys_module_actions', ModelBase.metadata,
                           Column('module', String(100), ForeignKey('sys_modules.module', ondelete='CASCADE', onupdate='CASCADE')),
                           Column('action', String(100), ForeignKey('sys_actions.action', ondelete='CASCADE', onupdate='CASCADE')))

class SysAction(ModelBase, ModelMixin):
    """
    系统所有操作列表

    action: 操作, 用于权限控制和角色管理模块 ex)add/update/edit/config
    label: 操作名称,用于角色管理模块 ex)Config Email
    """
    __tablename__ = 'sys_actions'

    action = Column(String(100), primary_key=True, unique=True, index=True)  # add/delete
    label = Column(String(100), nullable=False)  # Add/Delete
    description = Column(String(255))

class SysModule(ModelBase, ModelMixin):
    """
    系统模块列表，模块不一定是菜单，有path属性的模块才会是菜单(SysRole.get_menus)

    name: 模块名字, 用于角色管理模块和系统菜单导航 ex)角色管理
    parent_id: 父模块id
    module: 模块, 用于权限控制和角色管理模块 ex)roles
    path: 路径, 用于前端页面导航和页面返回 ex)roles
    category: 模块分类, 用于平台多业务管理  ex)VPN
    actions: 模块操作列表
    """
    __tablename__ = 'sys_modules'

    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(255), nullable=False)
    parent_id = Column(Integer)
    module = Column(String(100), unique=True)  # 用于api和权限控制, path可能为空, 但是module不能为空(例如:只通过api调用的模块)
    path = Column(String(100), unique=True)  # 用于页面导航(前端hash & main/page.py)
    category = Column(String(100))  # 用于模块分类, 多系统整合
    description = Column(String(255))

    actions = relationship('SysAction', secondary=sys_module_actions, lazy='joined')  # 模块操作列表, 不同的模块有不同的操作列表

class SysRoleModule(ModelBase, ModelMixin):
    """
    角色对应的模块和操作列表
    一个角色对应多条记录

    role_id: 角色ID
    module_id: 模块ID
    action: 操作权限
    """
    __tablename__ = 'sys_role_modules'

    id = Column(Integer, primary_key=True, autoincrement=True)
    role_id = Column(Integer, ForeignKey('sys_roles.id', ondelete='CASCADE'), nullable=False)
    module_id = Column(Integer, ForeignKey('sys_modules.id', ondelete='CASCADE'), nullable=False)
    action = Column(String(100), ForeignKey('sys_actions.action', ondelete='CASCADE'))

class SysRole(ModelBase, ModelMixin, AutoModelMixin):
    """
    角色列表

    name: 角色名称
    default: 是否是默认角色(常用于第三方用户校验)
    modules: 模块+操作权限列表
    """
    __tablename__ = 'sys_roles'

    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(100), unique=True, nullable=False)
    default = Column(Boolean, default=False)
    description = Column(String(255))
    created_at = Column(DateTime(), default=datetime.now)
    updated_at = Column(DateTime(), default=datetime.now, onupdate=datetime.now)

    modules = relationship('SysRoleModule', cascade='all,delete-orphan')

    def check_permission(self, module, action):
        """
        权限检查
        :param module: 要检查的模块名称
        :param action: 要检查的操作
        :return:
        """
        checked_module_id = SysRole._find_module_id(module)
        if checked_module_id:
            for role_module_ins in self.modules:
                if SysRole._check_module_action_permission(role_module_ins, checked_module_id, action) is True:
                    return True
        return False

    @staticmethod
    def _find_module_id(module_name):
        """返回module名对应的module对象"""
        module_json_list = get_app_cached_modules()
        for item in module_json_list:
            if item.get('module') == module_name:
                return item.get('id')
        return None

    @staticmethod
    def _check_module_permission(menu_json_item, module):
        """检查是否是指定的模块"""
        return menu_json_item.get('module') == module

    @staticmethod
    def _check_module_action_permission(role_module_ins, module_id, action):
        """检查是否包含指定的操作权限"""
        if role_module_ins.module_id != module_id:
            return False

        if action is not None:
            if action == 'add' or action == 'update' or action == 'delete':  # 简化操作，只检查是否有编辑权限，根据需求设置
                action = 'update'
            return role_module_ins.action == action
        return True

    def get_menus(self):
        """
        返回角色的菜单列表(包括父菜单)
        :return:
        """
        all_modules = SysModule.query_all()
        if all_modules[0] is False:
            return all_modules
        all_modules = all_modules[1]
        module_ins_map = get_ins_mapping(all_modules, 'id')

        menus = []
        added_map = {}
        for item in self.modules:
            SysRole._add_menu_item(item.module_id, module_ins_map, menus, added_map)
        menus.sort(key=lambda i: i.id)

        return menus

    @staticmethod
    def _add_menu_item(module_id, module_map, menus, added, is_parent=False):
        if module_id is None:
            return
        module_ins = module_map.get(module_id)
        if module_ins:
            if is_parent is not True and module_ins.path is None:  # @2023-03-10 add to filter menus without path
                return
            if module_id not in added:
                menus.append(module_ins)
                added[module_id] = module_ins
            SysRole._add_menu_item(module_ins.parent_id, module_map, menus, added, True)

    @staticmethod
    def to_client_json(role_model_json):
        modules = []
        for item in role_model_json.get('modules', []):
            module_id = item.get('module_id')
            action = item.get('action')
            _item = find_list(modules, lambda _i: _i.get('module_id') == module_id)
            if _item is None:
                _item = {
                    'module_id': module_id,
                    'actions': []
                }
                modules.append(_item)
            if action is not None:
                _item.get('actions').append(action)

        role_model_json['modules'] = modules
        return role_model_json

    @staticmethod
    def to_server_json(role_client_json):
        model_json = {}
        model_json.update(role_client_json)
        modules = []
        for item in role_client_json.get('modules', []):
            actions = item.get('actions')
            if not actions:
                actions = [None]

            for action in actions:
                modules.append({
                    'module_id': item.get('module_id'),
                    'action': action
                })
        model_json['modules'] = modules
        return model_json

class SysUser(ModelBase, ModelMixin, UserMixin, AutoModelMixin):
    """
    用户列表

    type: 账号类型, local-本地账号, ldap-LDAP账号, aaa-3A认证账号
    username: 账号, 不可重复
    password_: hash以后的密码, 如果不是本地校验的话, 可能为空
    status: 账号状态, 停用或启用
    role_id: 角色ID, 为了简化操作, 使用的是单角色模式
    last_login_at: 上次登录时间(for 管理员)
    """
    __tablename__ = 'sys_users'

    id = Column(Integer, primary_key=True, autoincrement=True)
    type = Column(String(100), nullable=False, default='local')  # local/ldap/aaa
    username = Column(String(100), unique=True, nullable=False)  # account
    password_ = Column('password', String(255))  # hash password
    status = Column(String(100), default='enable')  # disable/enable
    email = Column(String(100))  # @2023-09-18修改，nullable=False-->nullable=True, aaa用户email为空
    name = Column(String(100))  # full name
    phone = Column(String(100))
    role_id = Column(Integer, ForeignKey('sys_roles.id'), nullable=False)
    last_login_at = Column(DateTime())
    description = Column(String(255))
    created_at = Column(DateTime(), default=datetime.now)
    updated_at = Column(DateTime(), default=datetime.now, onupdate=datetime.now)

    role = relationship('SysRole', uselist=False)
    option = relationship('SysUserOption', cascade='all,delete-orphan', uselist=False, lazy='joined')

    @hybrid_property
    def password(self):
        return self.password_

    @password.setter
    def password(self, pwd):
        if self.type == 'local' or self.type is None:
            self.password_ = generate_password_hash(pwd)
        else:
            self.password_ = None

    @staticmethod
    def verify_password(username, password):
        """
        verify username and password

        重载以实现外部登录 & 自动添加账号功能

        :param username:
        :param password:
        :return:
        """
        try:
            user = SysUser.query_by({'username': username.strip()}, return_first=True)
            if not user:
                return False, res_status_codes.account_not_found
            if user.status != 'enable':
                return False, res_status_codes.account_disabled
            if user.password_ is None:  # @2023-09-18添加，密码不存在(aaa)
                return False, res_status_codes.account_verify_err

            if check_password_hash(user.password_, password) is True:  # 移除password.strip()
                return True, user
            else:
                return False, res_status_codes.account_verify_err
        except Exception as e:
            flaskz_logger.exception(e)
            return False, res_status_codes.db_query_err

    @classmethod
    def to_dict_field_filter(cls, field):
        """过滤属性"""
        return field not in ['password'] and super().to_dict_field_filter(field)

    @classmethod
    def check_delete_data(cls, pk_value):
        if current_user and (current_user.id == int(pk_value)):  # 不能删除当前用户
            return res_status_codes.db_data_in_use
        return super().check_delete_data(pk_value)

    @classmethod
    def check_update_data(cls, data):
        if data.get('status') == 'disable' and (current_user and current_user.id == int(data.get('id'))):  # 不能禁用当前用户
            return res_status_codes.db_data_in_use
        return super().check_update_data(data)

    # @classmethod
    # def filter_attrs_by_columns(cls, data):
    #     attrs = super().filter_attrs_by_columns(data)
    #     if data.get('_update_updated_at') is not False:
    #         attrs['updated_at'] = datetime.now()
    #     return attrs

    def can(self, module, action):  # 权限检查
        return self.role.check_permission(module, action)

class SysUserOption(ModelBase, BaseModelMixin):
    """用户选项(内部使用)"""
    __tablename__ = 'sys_user_options'

    id = Column(Integer, primary_key=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey('sys_users.id', ondelete='CASCADE'), nullable=False)
    previous_login_at = Column(DateTime())  # 上次登录时间
    last_login_at = Column(DateTime())  # 最后登录时间
    login_times = Column(Integer, default=0)  # 登录次数
    preferences = Column(Text)  # 偏好设置(replace)

    @classmethod
    def update(cls, option):
        try:
            ins = SysUserOption.query_by({'user_id': option.get('user_id')}, True)
            if ins:
                option['id'] = ins.id
                SysUserOption.update_db(option)
            else:
                ins = SysUserOption.add_db(option)
                flaskz_logger.info(get_rest_log_msg('Update User options', option, True, None))
                return True, ins
        except Exception as e:
            flaskz_logger.exception(e)
        return False, res_status_codes.db_update_err

    @classmethod
    def update_login(cls, user_id, last_login_at=None):
        ins = SysUserOption.query_by({'user_id': user_id}, True)
        previous_login_at = None
        if ins:
            previous_login_at = ins.last_login_at
            login_times = ins.login_times + 1
        else:
            login_times = 1
        return cls.update({
            'user_id': user_id,
            'last_login_at': last_login_at or datetime.now(),
            'previous_login_at': previous_login_at,
            'login_times': login_times
        })

class SysActionLog(ModelBase, ModelMixin):
    """
    操作日志

    username: 操作人员账号
    user_name: 操作人员姓名
    user_ip: 操作IP
    module: 模块名
    action: 操作名
    req_data: 请求数据
    res_data: 结果数据
    result: 操作结果, 成功/失败
    like_columns: 模糊查询列
    """
    __tablename__ = 'sys_action_logs'

    id = Column(Integer, primary_key=True, autoincrement=True)
    username = Column(String(100))  # 如果不登录，账号为空
    user_name = Column(String(100))  #
    user_ip = Column(String(100))  #

    module = Column(String(100))  # The name of the module, ex Role Mgmt/User Mgmt
    action = Column(String(100))  # add/remove/update

    req_data = Column(Text())  # request data
    res_data = Column(Text())  # response data
    result = Column(String(100))  # success/fail

    description = Column(Text())  #

    created_at = Column(DateTime(), default=datetime.now)  #

    like_columns = ['module', 'description']

    @classmethod
    def check_add_data(cls, json_data):
        """not check"""
        return True

    @classmethod
    def get_query_default_order(cls):
        """
        Return the default query order.
        :return:
        """

        return desc(cls.created_at)

def get_app_cached_modules():
    """
    返回系统模块列表，为了提高效率，对系统模块进行了缓存
    :return:
    """
    modules = get_app_cache('sys_modules')
    if modules is None:
        modules = []
        all_modules = SysModule.query_all()
        if all_modules[0] is True:
            all_modules = all_modules[1]
            for item in all_modules:
                modules.append(item.to_dict())
        set_app_cache('sys_modules', modules, expire_minutes=60)
    return modules


class Solution:
    def segregateElements(self, arr, n):
        # Your code goes here
        pos=0
        neg=len(arr)-1
        length=len(arr)-1
        temp_arr=arr.copy()
        for i,ele in enumerate(temp_arr):
            if temp_arr[i] !="#" and ele >0:
                arr[pos]=ele
                pos+=1
                temp_arr[i]="#"
            if temp_arr[length-i] !="#" and temp_arr[length-i]<0 :
                arr[neg]=temp_arr[length-i]
                neg-=1
                temp_arr[length-i]="#"
            if pos>neg:
                return

#{
#  Driver Code Starts
#Initial Template for Python 3

def main():

    T = int(input())

    while(T > 0):
        n = int(input())
        a = [int(x) for x in input().strip().split()]
        ob=Solution()
        ob.segregateElements(a, n)
        print(*a)

        T -= 1

if __name__ == "__main__":
    main()


import os
from elasticsearch import Elasticsearch

def setup(conf):
    os.environ['ES_HOST'] = conf.get_string("es.host")
    os.environ['ES_PORT'] = conf.get_string("es.port")
    os.environ['ES_INDEX'] = conf.get_string("es.index")

def indexing(key, data):
    es = Elasticsearch([{'host': os.environ.get("ES_HOST"), 'port': int(os.environ.get("ES_PORT"))}])
    es.index(index=os.environ.get("ES_INDEX"), doc_type='_doc', id=key, body=data)
    return True


# -*- coding: utf-8 -*-
"""Pyuic replacement"""

import os
import sys
import xml.etree.ElementTree as ET
import logging
import platform

__author__ = 'Laszlo Tamas'
__copyright__ = 'Copyright 2027, Laszlo Tamas'
__license__ = 'GPL'
__version__ = '0.0.1'
__maintainer__ = 'Laszlo Tamas'
__status__ = 'Initial'

LOGGER = logging.getLogger('tlpyuic')
# set level for file handling (NOTSET>DEBUG>INFO>WARNING>ERROR>CRITICAL)
LOGGER.setLevel(logging.DEBUG)

# create file handler which logs even debug messages
LOGGER_FH = logging.FileHandler('tlpyuic.log')

# create console handler with a higher log level
LOGGER_CH = logging.StreamHandler()
LOGGER_CH.setLevel(logging.INFO)

# create FORMATTER and add it to the handlers
FORMATTER = \
    logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'
                      )
LOGGER_FH.setFormatter(FORMATTER)
LOGGER_CH.setFormatter(FORMATTER)

# add the handlers to the LOGGER
LOGGER.addHandler(LOGGER_FH)
LOGGER.addHandler(LOGGER_CH)

class TLPyUIC():
    """Main class.

    """

    def __init__(self):
        self.par_input = ''
        self.output = ''
        self.pyuic_cmd = ''
        self.base_name = ''
        self.class_name = ''
        self.super_class = ''
        self.form_class = ''
        self.form_file = ''
        self.control_output = ''
        self.parent_path = ''
        self.preview_mode = False
        self.pyuic_cmd_win = 'c:\\Python36\\Scripts\\pyuic5.exe'
        self.pyuic_cmd_mac = '/Library/Frameworks/Python.framework/Versions/3.6/bin/pyuic5'
        self.pyuic_cmd_linux = '/usr/bin/pyuic5'
        self.control_file_exists = False

    def compile_form(self):
        """Execute the program by arguments.
        """
        if str(sys.argv[1]) == '-p':
            self.preview_mode = True
            self.par_input = str(sys.argv[2])
        else:
            self.par_input = str(sys.argv[1])
        LOGGER.debug('Preview mode: %s', self.preview_mode)
        self.base_name = os.path.basename(self.par_input)
        self.form_file = os.path.splitext(self.base_name)[0].lower()
        self.parent_path = os.path.abspath(
            os.path.join(self.par_input, os.pardir))
        self.class_name = os.path.splitext(self.base_name)[0]
        self.output = self.parent_path+'/Ui_'+self.form_file+'.py'
        self.control_output = self.parent_path+'/'+self.form_file+'.py.txt'
        self.control_file_exists = os.path.isfile(self.control_output)
        if platform.system() == 'Windows':
            if self.preview_mode:
                self.pyuic_cmd = self.pyuic_cmd_win + ' -p ' + self.par_input
            else:
                self.pyuic_cmd = self.pyuic_cmd_win + ' -o ' + \
                    self.output + ' ' + self.par_input
        if platform.system() == 'Darwin':
            if self.preview_mode:
                self.pyuic_cmd = self.pyuic_cmd_mac + ' -p ' + self.par_input
            else:
                self.pyuic_cmd = self.pyuic_cmd_mac + ' -o ' + self.output + ' ' + self.par_input
        if platform.system() == 'Linux':
            if self.preview_mode:
                self.pyuic_cmd = self.pyuic_cmd_linux + ' -p ' + self.par_input
            else:
                self.pyuic_cmd = self.pyuic_cmd_linux + \
                    ' -o ' + self.output + ' ' + self.par_input
        LOGGER.debug('Command: %s', platform.system() + '>>' + self.pyuic_cmd)
        os.system(self.pyuic_cmd)
        LOGGER.debug('Input: %s', self.par_input)
        LOGGER.debug('Output: %s', self.output)
        LOGGER.debug('Control output: %s', self.control_output)

    def add_executable_part(self):
        """Add executable __mai__ part to compiled file.
        """
        tree = ET.parse(self.par_input)
        root = tree.getroot()
        self.form_class = ''
        for child in root:
            if child.tag == 'class':
                self.form_class = child.text
        self.super_class = 'Q'+self.form_class
        # append execution code to Ui_*.py
        gen_code = '\n'
        gen_code += 'if __name__ == \'__main__\':\n'
        gen_code += '    import sys\n'
        gen_code += '    import os\n'
        gen_code += '    APP = QtWidgets.QApplication(sys.argv)\n'
        gen_code += '    PARENT_PATH = os.path.join(os.path.join(\n'
        gen_code += '        __file__, os.path.pardir), os.path.pardir)\n'
        gen_code += '    DIR_PATH = os.path.abspath(PARENT_PATH)\n'
        gen_code += '    FILE_PATH = os.path.join(DIR_PATH, \'i18n\',  \'hu.qm\')\n'
        gen_code += '    TRANSLATOR = QtCore.QTranslator()\n'
        gen_code += '    TRANSLATOR.load(FILE_PATH)\n'
        gen_code += '    APP.installTranslator(TRANSLATOR)\n'
        gen_code += '    THISWINDOW = QtWidgets.'+self.super_class+'()\n'
        gen_code += '    UI = Ui_'+self.form_class+'()\n'
        gen_code += '    UI.setupUi(THISWINDOW)\n'
        gen_code += '    THISWINDOW.show()\n'
        gen_code += '    sys.exit(APP.exec_())\n'
        with open(self.output, "a") as myfile:
            myfile.write(gen_code)

    def generate_control_head(self):
        """Generate the head part of the control .py file.
        """
        if not self.control_file_exists:
            gen_code = ''
            gen_code += '# -*- coding: utf-8 -*-\n'
            gen_code += '\n'
            gen_code += '"""Module implementing '+self.class_name+'."""\n'
            gen_code += '\n'
            gen_code += 'import logging\n'
            gen_code += 'from PyQt5.QtCore import pyqtSlot, QCoreApplication\n'
            gen_code += 'from PyQt5.QtWidgets import '+self.super_class+', QMessageBox\n'
            gen_code += '\n'
            gen_code += 'from .Ui_'+self.form_file+' import Ui_'+self.form_class+'\n'
            gen_code += '\n'
            gen_code += '\n'
            gen_code += 'class '+self.class_name + \
                '('+self.super_class+', Ui_'+self.form_class+'):\n'
            gen_code += '    """\n'
            gen_code += '    Class documentation goes here.\n'
            gen_code += '    """\n'
            gen_code += '    def __init__(self, arguments, parent=None):\n'
            gen_code += '        """\n'
            gen_code += '        Constructor\n'
            gen_code += '        \n'
            gen_code += '        @param parent reference to the parent widget\n'
            gen_code += '        @type QWidget\n'
            gen_code += '        """\n'
            gen_code += '        super('+self.class_name + \
                ', self).__init__(parent)\n'
            gen_code += '        self.class_name = self.__class__.__name__\n'
            gen_code += '        log_name = \'program.\' + self.class_name\n'
            gen_code += '        self.module_logger = logging.getLogger(log_name)\n'
            gen_code += '        self.module_logger.setLevel(logging.DEBUG)\n'
            gen_code += '        logger_fh = logging.FileHandler(\'program.log\')\n'
            gen_code += '        logger_ch = logging.StreamHandler()\n'
            gen_code += '        logger_ch.setLevel(logging.INFO)\n'
            gen_code += '        self.args = arguments\n'
            gen_code += '\n'
            gen_code += '        # create formatter and add it to the handlers\n'
            gen_code += '\n'
            gen_code += '        formatter = \\\n'
            gen_code += '            logging.Formatter(\n'
            gen_code += '                    '
            gen_code += '\'%(asctime)s - %(name)s - %(levelname)s - %(message)s\'\n'
            gen_code += '                              )\n'
            gen_code += '        logger_fh.setFormatter(formatter)\n'
            gen_code += '        logger_ch.setFormatter(formatter)\n'
            gen_code += '\n'
            gen_code += '        # add the handlers to the logger\n'
            gen_code += '\n'
            gen_code += '        self.module_logger.addHandler(logger_fh)\n'
            gen_code += '        self.module_logger.addHandler(logger_ch)\n'
            gen_code += '        self.setupUi(self)\n'
            with open(self.control_output, 'wb') as myfile:
                myfile.write(gen_code.encode('utf-8'))

    def generate_general_slots(self):
        """Generate generally used slot codes.
        """
        if not self.control_file_exists:
            gen_code = '\n'
            gen_code += '    @pyqtSlot()\n'
            gen_code += '    def closeEvent(self, event):\n'
            gen_code += '        """\n'
            gen_code += '        Override original event.\n'
            gen_code += '\n'
            gen_code += '        @param event original close event\n'
            gen_code += '\n'
            gen_code += '        """\n'
            gen_code += '        _translate = QCoreApplication.translate\n'
            gen_code += '        quit_title = _translate(\'' + \
                self.class_name+'\', \'Confirmation\')\n'
            gen_code += '        quit_msg = _translate(\'' + \
                self.class_name+'\',\n'
            gen_code += '         '
            gen_code += '                     \'Are you sure you want to exit the program?\')\n'
            gen_code += '        reply = QMessageBox.question(self, quit_title, quit_msg,\n'
            gen_code += '                                     QMessageBox.Yes, QMessageBox.No)\n'
            gen_code += '        if reply == QMessageBox.Yes:\n'
            gen_code += '            self.module_logger.debug(\'Confirmed to exit program\')\n'
            gen_code += '            event.accept()\n'
            gen_code += '        else:\n'
            gen_code += '            event.ignore()\n'
            with open(self.control_output, 'ab') as myfile:
                myfile.write(gen_code.encode('utf-8'))

    def generate_slots(self):
        """Generate slot codes.
        """
        gen_code = ''
        action_code = ''
        tree = ET.parse(self.par_input)
        root = tree.getroot()
        for level1 in root:
            for level2 in level1:
                # print('2', level2.tag, level2.attrib)
                # menu items
                if level2.tag == 'action':
                    action_name = level2.get('name')
                    action_code = 'def on_' + \
                        action_name + '_triggered(self):'
                    if not self.control_file_exists or \
                            not action_code in open(self.control_output).read():
                        gen_code += '    \n'
                        gen_code += '    @pyqtSlot()\n'
                        gen_code += '    def on_' + \
                            action_name + '_triggered(self):\n'
                        gen_code += '        """\n'
                        gen_code += '        Slot documentation goes here.\n'
                        gen_code += '        """\n'
                        if action_name == 'actionQuit':
                            gen_code += '        self.close()\n'
                        else:
                            gen_code += '        # TODO: not implemented yet\n'
                            gen_code += '        raise NotImplementedError\n'
                # controls
                for level3 in level2:
                    level_class = level3.get('class')
                    level_name = level3.get('name')
                    if level_class == 'QPushButton':
                        actions = ('clicked', 'pressed', 'released')
                        act_count = 0
                        pre = ''
                        for act in actions:
                            act_count += 1
                            action_code = 'def on_' + \
                                level_name + '_' + act + '(self):\n'
                            if not self.control_file_exists or \
                                    not action_code in open(self.control_output).read():
                                if act_count > 1:
                                    pre = '# '
                                gen_code += '\n'
                                gen_code += pre + '    @pyqtSlot()\n'
                                gen_code += pre + '    def on_' + \
                                    level_name + '_' + act + '(self):\n'
                                gen_code += pre + '        """\n'
                                gen_code += pre + '        Slot documentation goes here.\n'
                                gen_code += pre + '        """\n'
                                gen_code += pre + '        # TODO: not implemented yet\n'
                                gen_code += pre + '        raise NotImplementedError\n'
                    if level_class == 'QCheckBox':
                        action_code = 'def on_' + \
                            level_name + '_toggled(self, checked):\n'
                        if not self.control_file_exists or \
                                not action_code in open(self.control_output).read():
                            gen_code += '\n'
                            gen_code += '    @pyqtSlot(bool)\n'
                            gen_code += '    def on_' + \
                                level_name + '_toggled(self, checked):\n'
                            gen_code += '        """\n'
                            gen_code += '        Slot documentation goes here.\n'
                            gen_code += '        """\n'
                            gen_code += '        \n'
                            gen_code += '        @param checked DESCRIPTION\n'
                            gen_code += '        @type bool\n'
                            gen_code += '        """\n'
                            gen_code += '        # TODO: not implemented yet\n'
                            gen_code += '        raise NotImplementedError\n'
                    if level_class == 'QLineEdit':
                        action_code = 'def on_' + \
                            level_name + '_textChanged(self, p0):\n'
                        if not self.control_file_exists or \
                                not action_code in open(self.control_output).read():
                            gen_code += '\n'
                            gen_code += '    @pyqtSlot(str)\n'
                            gen_code += '    def on_' + \
                                level_name + '_textChanged(self, p0):\n'
                            gen_code += '        """\n'
                            gen_code += '        Slot documentation goes here.\n'
                            gen_code += '        \n'
                            gen_code += '        @param p0 DESCRIPTION\n'
                            gen_code += '        @type str\n'
                            gen_code += '        """\n'
                            gen_code += '        # TODO: not implemented yet\n'
                            gen_code += '        raise NotImplementedError\n'
                            gen_code += '    \n'
                        action_code = 'def on_' + \
                            level_name + '_returnPressed(self):\n'
                        if not self.control_file_exists or \
                                not action_code in open(self.control_output).read():
                            gen_code += '\n'
                            gen_code += '    @pyqtSlot()\n'
                            gen_code += '    def on_' + \
                                level_name + '_returnPressed(self):\n'
                            gen_code += '        """\n'
                            gen_code += '        Slot documentation goes here.\n'
                            gen_code += '        """\n'
                            gen_code += '        # TODO: not implemented yet\n'
                            gen_code += '        raise NotImplementedError\n'
                            gen_code += '    \n'

        with open(self.control_output, 'ab') as myfile:
            myfile.write(gen_code.encode('utf-8'))

if __name__ == '__main__':
    LOGGER.debug('Start program')
    PROG = TLPyUIC()
    PROG.compile_form()
    if not PROG.preview_mode:
        PROG.add_executable_part()
        PROG.generate_control_head()
        PROG.generate_general_slots()
        PROG.generate_slots()
    LOGGER.debug('Exit program')
    sys.exit()


'''
Date - 1/06/2018
@author - Purvak
Timestamp - 23:44

Different approaches considered by me.

first line - (2n -2)

Middle line ??

last line (2n-2)
'''

'''
Using direction

def (s, n):

	ls = [[0] for i in range(n)]
	direction = "down"
	row = 0

	for i in range(len(s)):
		if(direction == "down"):
			ls.append

'''

class Solution:
    def convert(self, s, numRows):
        """
        :type s: str
        :type numRows: int
        :rtype: str
        """
        if numRows==1:
            return s

        ans = ""
        "Finding pattern between elements we get a step_size of 2n-2"
        step_size = 2*numRows - 2

        for i in range(numRows):
            for j in range(i,len(s),step_size):
                ans+= s[j]
                "Checking for diagonal element. Logic of j+step_size-2*i by @kamyu104"
                if(0<i<numRows-1 and j+step_size - 2*i < len(s) ):
                    ans+= s[j+step_size - 2*i]

        return ans


from bob.gradiant.core.classes.evaluation.metrics.metric_provider import metric_provider

class PerformanceExtractor:

    @staticmethod
    def run(metric, scores_container, threshold=None):

        scores, labels = scores_container.get_scores_and_labels()

        PerformanceExtractor.__assert_input(labels, scores)

        try:
            metric_object = metric_provider[metric]
            if threshold is not None:
                metric_object.set_threshold(threshold)
            elif metric_object.metric_needs_threshold():
                raise Exception("Metric " + metric + " needs a threshold value but None has been provided.")
            value, threshold = metric_object.compute(scores, labels)
            return {'value': value, 'threshold': threshold}
        except:
            pass

    @staticmethod
    def __assert_input(labels, scores):
        if not scores.size:
            raise TypeError('Scores are empty')
        if not labels.size:
            raise TypeError('Labels are empty')


from pyspark import SparkContext, SparkConf
from pyspark.mllib.linalg import Vectors
import numpy as np
import time

# Define function to calculate cosine similarity score
def cosine_similarity(query, embedding):
    dot_product = np.dot(query, embedding)
    norm_query = np.linalg.norm(query)
    norm_embedding = np.linalg.norm(embedding)
    return dot_product / (norm_query * norm_embedding)

# Define query and query embedding as RDDs
conf = SparkConf().setAppName("Cosine Similarity")
sc = SparkContext.getOrCreate(conf)
query = sc.parallelize([np.random.rand(786)])
embeddings = sc.parallelize(np.random.rand(1500, 786))

# Define list of number of partitions to try
num_partitions_list = [20]

# Loop over number of partitions and calculate cosine similarity scores
for num_partitions in num_partitions_list:
    # Repartition the embedding RDD with num_partitions partitions
    embeddings_partitioned = embeddings.repartition(num_partitions)

    # Calculate cosine similarity score using RDD zipWithIndex and map functions
    start_time = time.time()
    scores = embeddings_partitioned.zipWithIndex().map(lambda x: (x[1], cosine_similarity(query.collect()[0], x[0]))).collect()
    scores.sort(key=lambda x: x[0])
    scores = [x[1] for x in scores]
    end_time = time.time()

    print(f"Time taken with {num_partitions} partitions:", end_time - start_time, "seconds")
    print("---------------------------------------------------------------------------")

# Stop SparkContext
sc.stop()


from rest_framework import serializers
from .models import Movies,Cast,MoviesCast

class MoviesSerializer(serializers.ModelSerializer):
    class Meta:
        model=Movies
        fields='__all__'

class CastSerializer(serializers.ModelSerializer):
    class Meta:
        model=Cast
        fields='__all__'

class MoviesCastSerializer(serializers.ModelSerializer):
    class Meta:
        model=MoviesCast
        fields='__all__'

def create(self,validated_data):
    data=validated_data('movies')
    data=validated_data('cast')

    Movies=Movies.objecs.create(**validated_data)
    for cast_data in cast_data:
        cast=Cast.objects.get_or_create(**cast_data)
        MoviesCast.objects.create(movies=Movies,cast=Cast)
    return Movies.objects.create(**validated_data)

def update(self,instance,validated_data):
    instance.movies_name=validated_data.get('movies_name',instance.movies_name)
    instance.cast=validated_data.get('cast',instance.cast)
    instance.save()
    return instance


import os
from k2_util import strUtil

class Reader(object):
    def __init__(self):
        pass

    def read(self, **kw):
        pass

    def readln(self, *s):
        self.write(*s)
        self._buff += os.linesep

    def close(self):
        pass

class StringReader(Reader):
    def __init__(self, buff, **kw):
        self._buff = buff
        self._index = kw.get('start_at', 0)

    def _read_chars(self, nchars):
        if nchars < 0:
            raise ValueError('Unable to read backwards! The number of characters to read (%d) must be >= 0' % nchars)
        if self._index >= len(self._buff):
            return None
        elif self._index+nchars <= len(self._buff):
            rval = self._buff[self._index:self._index+nchars]
            self._index += nchars
        else:
            rval = self._buff[self._index:]
            self._index = len(self._buff)
        return rval

    def _read_until(self, *tokens, **kw):
        max_chars = kw.get('max_chars', None)
        rval = ''
        if max_chars != None and max_chars < 0:
            raise ValueError('Unable to read backwards! The maximum number of characters to read (%d) must be >= 0' % max_chars)
        c = self._read_chars(1)
        if c == None:
            return
        rval += c
#        while rval[-len(token):] != token and c != None:
        while not strUtil.ends_with_token(rval, *tokens) and c != None:
            if max_chars != None and len(rval) >= max_chars:
                return rval
            c = self._read_chars(1)
            if c != None:
                rval += c
        return rval

    def _peek_chars(self, nchars):
        if nchars < 0:
            raise ValueError('Unable to read backwards! The number of characters to read (%d) must be >= 0' % nchars)
        if self._index >= len(self._buff):
            return None
        elif self._index+nchars <= len(self._buff):
            rval = self._buff[self._index:self._index+nchars]
        else:
            rval = self._buff[self._index:]
        return rval

    def _peek_until(self, *tokens, **kw):
        max_chars = kw.get('max_chars', None)
        if max_chars != None and max_chars < 0:
            raise ValueError('Unable to read backwards! The maximum number of characters to read (%d) must be >= 0' % max_chars)
        if max_chars != None and max_chars == 0:
            return ''
        i=1
        rval = self._peek_chars(i)
        if rval == None:
            return None
#        while rval[-len(token):] != token and self._index + i < len(self._buff):
        while not strUtil.ends_with_token(rval, *tokens) and self._index + i < len(self._buff):
            if max_chars != None and i >= max_chars:
                return rval
            i += 1
            rval = self._peek_chars(i)
        return rval

    def readln(self):
        if self._index >= len(self._buff):
            return None
        line = ''
        c = self._read_chars(1)
        while  c != None:
            if c != '\n' and c != '\r':
                line += c
            else:
                if c == '\n' and self._peek_chars(1) == '\r':
                    self._read_chars(1)
                elif c == '\r' and self._peek_chars(1) == '\n':
                    self._read_chars(1)
                return line
            c = self._read_chars(1)
        return line

    def read(self, *args, **kw):
        if len(args) > 0:
            return self._read_chars(args[0])
        if kw.get('chars'):
            if kw.get('peek'):
                return self._peek_chars(kw.get('chars'))
            else:
                return self._read_chars(kw.get('chars'))
        if kw.get('until'):
            if kw.get('peek'):
                if kw.get('max_chars'):
                    return self._peek_until(kw.get('until'), max_chars=kw.get('max_chars'))
                else:
                    return self._peek_until(kw.get('until'))
            else:
                if kw.get('max_chars'):
                    return self._read_until(kw.get('until'), max_chars=kw.get('max_chars'))
                else:
                    return self._read_until(kw.get('until'))

    def __str__(self):
        return self._buff


import os
import zipfile

def read_dir(folder2):
    for root, dirs, files in os.walk(folder2):
        level = root.count(os.sep)
        indent = ' ' * 4 * level
        print(f'{indent}[{os.path.basename(root)}]')
        sub_indent = ' ' * 4 * (level + 1)
        print(root, files, level, indent, sub_indent)
        for file in files:
            print(f'{sub_indent}{file}')

read_dir('folder2')
#
# import zipfile
# import os

# z = zipfile.ZipFile('spam.zip', 'w')        # Создание нового архива
# for root, dirs, files in os.walk('folder2'): # Список всех файлов и папок в директории folder
#     for file in files:
#         z.write(os.path.join(root,file))         # Создание относительных путей и запись файлов в архив
#
# z.close()
#
# v = zipfile.ZipFile('spam.zip', 'r')
# v.printdir()
# #
# z.close()
#
# n, m = map(int, input().split())
# matrix = [[0] * m for _ in range(n)]
# dx, dy, x, y = 0, 1, 0, 0

# for i in range(1, n * m + 1):
#     matrix[x][y] = i
#     if matrix[(x + dx) % n][(y + dy) % m]:
#         dx, dy = dy, -dx
#     x += dx
#     y += dy
# for line in matrix:
#     print(*(f'{i:<3}' for i in line), sep='')


"""
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

Input:
11110
11010
11000
00000

Output: 1
Example 2:

Input:
11000
11000
00100
00011

Output: 3
"""

class Solution:
    def numIslands(self, grid):
        """
        :type grid: List[List[str]]
        :rtype: int
        """
        if grid == []:
            return 0

        result  = []

        def DFS(grid, x, y, r, c):
            cond = x < 0 or x >= r or y < 0 or y >= c or grid[x][y] == '0'
            if cond:
                return 0
            count = 1
            grid[x][y] = '0'
            for i, j in ((-1, 0), (1, 0), (0, -1), (0, 1)):
                count += DFS(grid, x+i, y+j, r, c)
            return count

        r = len(grid)
        c = len(grid[0])

        for i in range(r):
            for j in range(c):
                if grid[i][j] == '1':
                        result.append(DFS(grid, i, j, r, c))
        return len(result)


__all__ = (
    'ListFilesRequest',
    'ListFilesResult',
    'ReadFileRequest',
    'ReadFileResult',
    'TailFileRequest',
    'TailFileResult',
    'SaveFileRequest',
    'SaveFileResult',
    'DownloadFilesRequest',
    'DownloadFilesResult',
    'CreateFileRequest',
    'CreateFileResult',
    'DeleteFilesRequest',
    'DeleteFilesResult',
    'OPEN_API_SPEC_ENTRIES_FILE_BROWSER'
)

from ideadatamodel import SocaPayload, SocaListingPayload, IdeaOpenAPISpecEntry
from ideadatamodel.filesystem.filesystem_model import *
from typing import Optional, List

# FileBrowser.ListFiles

class ListFilesRequest(SocaListingPayload):
    cwd: Optional[str]

class ListFilesResult(SocaListingPayload):
    cwd: Optional[str]
    listing: Optional[List[FileData]]

# FileBrowser.ReadFile

class ReadFileRequest(SocaPayload):
    file: Optional[str]

class ReadFileResult(SocaPayload):
    file: Optional[str]
    content_type: Optional[str]
    content: Optional[str]

# FileBrowser.TailFile

class TailFileRequest(SocaPayload):
    file: Optional[str]
    line_count: Optional[int]
    next_token: Optional[str]

class TailFileResult(SocaPayload):
    file: Optional[str]
    next_token: Optional[str]
    lines: Optional[List[str]]
    line_count: Optional[int]

# FileBrowser.SaveFile

class SaveFileRequest(SocaPayload):
    file: Optional[str]
    content: Optional[str]

class SaveFileResult(SocaPayload):
    pass

# FileBrowser.DownloadFiles

class DownloadFilesRequest(SocaPayload):
    files: Optional[List[str]]

class DownloadFilesResult(SocaPayload):
    download_url: Optional[str]

# FileBrowser.DeleteFiles
class DeleteFilesRequest(SocaPayload):
    files: Optional[List[str]]

class DeleteFilesResult(SocaPayload):
    pass

# FileBrowser.CreateFile
class CreateFileRequest(SocaPayload):
    cwd: Optional[str]
    filename: Optional[str]
    is_folder: Optional[bool]

class CreateFileResult(SocaPayload):
    pass

OPEN_API_SPEC_ENTRIES_FILE_BROWSER = [
    IdeaOpenAPISpecEntry(
        namespace='FileBrowser.ListFiles',
        request=ListFilesRequest,
        result=ListFilesResult,
        is_listing=True,
        is_public=False
    ),
    IdeaOpenAPISpecEntry(
        namespace='FileBrowser.ReadFile',
        request=ReadFileRequest,
        result=ReadFileResult,
        is_listing=False,
        is_public=False
    ),
    IdeaOpenAPISpecEntry(
        namespace='FileBrowser.TailFile',
        request=TailFileRequest,
        result=TailFileResult,
        is_listing=False,
        is_public=False
    ),
    IdeaOpenAPISpecEntry(
        namespace='FileBrowser.SaveFile',
        request=SaveFileRequest,
        result=SaveFileResult,
        is_listing=False,
        is_public=False
    ),
    IdeaOpenAPISpecEntry(
        namespace='FileBrowser.DownloadFiles',
        request=DownloadFilesRequest,
        result=DownloadFilesResult,
        is_listing=False,
        is_public=False
    ),
    IdeaOpenAPISpecEntry(
        namespace='FileBrowser.CreateFile',
        request=CreateFileRequest,
        result=CreateFileResult,
        is_listing=False,
        is_public=False
    ),
    IdeaOpenAPISpecEntry(
        namespace='FileBrowser.DeleteFiles',
        request=DeleteFilesRequest,
        result=DeleteFilesResult,
        is_listing=False,
        is_public=False
    )
]


import pandas as pd

with open("speaker.txt", "w") as f:
	f.write("")
myset = {}
xls = pd.ExcelFile(r"gtl.xls") #use r before absolute file path
sheetX = xls.parse(4)
chineseSpeaker = sheetX['講員 Speaker']
englishSpeaker = sheetX['Speaker']
language = sheetX['Language']
chineseLanguage = sheetX['語言']
church = sheetX['出版者 Church organization']
for i in range(0,len(chineseSpeaker)):
    myset[chineseSpeaker[i]] = {
        "speakerEn":englishSpeaker[i],
        "speakerCn":chineseSpeaker[i],
        "church":church[i],
        "language":language[i],
        "languageCn":chineseLanguage[i]
    }
print(myset)


import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import r2_score

def validate_model():
    np.random.seed(2)
    pageSpeeds = np.random.normal(3.0, 1.0, 100)
    purchaseAmount = np.random.normal(50.0, 30.0, 100) / pageSpeeds

    plt.scatter(pageSpeeds, purchaseAmount)
    plt.show()

    trainX = pageSpeeds[:80]
    testX = pageSpeeds[80:]

    trainY = purchaseAmount[:80]
    testY = purchaseAmount[80:]

    plt.scatter(trainX, trainY)
    plt.show()

    plt.scatter(testX, testY)
    plt.show()

    #Try to fit 8th degree
    x = np.array(trainX)
    y = np.array(trainY)

    p4 = np.poly1d(np.polyfit(x, y, 8))

    xp = np.linspace(0, 7, 100)
    axes = plt.axes()
    axes.set_xlim([0, 7])
    axes.set_ylim([0, 200])
    plt.scatter(x, y)
    plt.plot(xp, p4(xp), c='r')
    plt.show()

    r2 = r2_score(y, p4(x))
    print("Training R-square : {}".format(r2))

    #testing
    testx = np.array(trainX)
    testy = np.array(trainY)

    p4 = np.poly1d(np.polyfit(testx, testy, 8))

    xp = np.linspace(0, 7, 100)
    axes = plt.axes()
    axes.set_xlim([0, 7])
    axes.set_ylim([0, 200])
    plt.scatter(testx, testy)
    plt.plot(xp, p4(xp), c='r')
    plt.show()

    r2 = r2_score(testy, p4(testx))
    print("Testing R-square : {}".format(r2))

if __name__ == '__main__':
    validate_model()


# -*- coding: utf-8 -*-
"""
Created on Fri Jul  1 12:32:59 2022

@author: user
"""

from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
import time

#Starts the driver and goes to our starting webpage
driver = webdriver.Chrome(
        'C:/Users/user/Downloads/chromedriver_win32 (2)/chromedriver.exe')

driver.get('https://www.nike.com.hk/su22_summerdelight/list.htm?ds_rl=1272577&cp=hkns_kw_200622_u_CCAT_SU22SeasonEndSale_gg01_brand_pure&gclid=CjwKCAjwk_WVBhBZEiwAUHQCmQepBerjSffXKXfnVMWLCh2VBUrZj4wmcOeYDYrGQjk3TSmLvR6ECBoCxR8QAvD_BwE&gclsrc=aw.ds')

#Will keep scrolling down the webpage until it cannot scroll no more
last_height = driver.execute_script('return document.body.scrollHeight')
while True:
    driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
    time.sleep(3)
    new_height = driver.execute_script('return document.body.scrollHeight')
    if new_height == last_height:
        break
    last_height = new_height

soup = BeautifulSoup(driver.page_source, 'lxml')

product_card = soup.find_all('li', class_ = 'style_liborder_new swiper-slide')

df = pd.DataFrame({'Link':[''], 'Name':[''], 'Subtitle':[''], 'Price':[''], 'Sale Price':['']})

for product in product_card:
    try:
        link = product.find('a', class_ = 'product_list_name').get('href')
        name = product.find('span', class_ = 'up').text
        full_price = product.find('span', class_ = 'origin_price').text
        discount_price = product.find('dd', class_ = 'color666').text
        df = df.append({'Link':link, 'Name':name,'Price':full_price, 'Sale Price':discount_price},
                       ignore_index = True)
    except:
        pass

df.to_csv('C:/Users/user/Downloads/file_name.csv')


from collections.abc import Sequence

from django.core import paginator
from django.db.models import QuerySet
from django.utils.timezone import now
from django.utils.translation import get_language
from elasticsearch_dsl import InnerDoc, AttrList
from marshmallow import pre_dump
from marshmallow.schema import SchemaOpts
from querystring_parser import builder

from mcod.core.api import schemas, fields
from mcod.core.serializers import SerializerRegistry

object_attrs_registry = SerializerRegistry()

class RelationshipData(schemas.ExtSchema):
    id = fields.String(required=True)
    _type = fields.String(required=True, data_key='type')

class RelationshipLinks(schemas.ExtSchema):
    related = fields.String(required=True)

class RelationshipMeta(schemas.ExtSchema):
    count = fields.Integer()

class Relationship(schemas.ExtSchema):
    data = fields.Nested(RelationshipData)
    links = fields.Nested(RelationshipLinks, required=True, many=False)
    meta = fields.Nested(RelationshipMeta, many=False)

    @pre_dump
    def prepare_data(self, data):
        object_url = self.context.get('object_url', None) or self.api_url
        url_template = self.context.get('url_template') or None
        res = {}
        if isinstance(data, (Sequence, QuerySet, AttrList)):
            if url_template:
                related_url = url_template.format(
                    api_url=self.api_url, object_url=object_url
                )
                res['links'] = {
                    'related': related_url
                }
            res['meta'] = {
                'count': len(data)
            }
        else:
            ident = getattr(data, 'id', None) or getattr(data.meta, 'id')
            slug = getattr(data, 'slug', None)
            if isinstance(slug, InnerDoc):
                lang = get_language()
                slug = slug[lang]
            if slug:
                ident = '{},{}'.format(ident, slug)
            if url_template:
                related_url = url_template.format(
                    api_url=self.api_url, object_url=object_url, ident=ident
                )
                res['links'] = {
                    'related': related_url
                }
            res['data'] = {
                'id': ident,
                '_type': self.context['_type']
            }

        return res

class Relationships(schemas.ExtSchema):
    def __init__(
            self, only=None, exclude=(), many=False, context=None,
            load_only=(), dump_only=(), partial=False, unknown=None,
    ):
        super().__init__(only=only, exclude=exclude, many=many, context=context,
                         load_only=load_only, dump_only=dump_only,
                         partial=partial, unknown=unknown)

        for field_name, field in self.fields.items():
            field._schema.context = dict(self.context)
            field._schema.context.update(field.metadata)
            field.many = False

class ObjectAttrsMeta(schemas.ExtSchema):
    pass

class ObjectAttrsOpts(SchemaOpts):
    def __init__(self, meta, **kwargs):
        SchemaOpts.__init__(self, meta, **kwargs)
        self.relationships_schema = getattr(meta, 'relationships_schema', None)

        if self.relationships_schema and not issubclass(self.relationships_schema, Relationships):
            raise Exception("{} must be a subclass of Relationships".format(self.relationships_schema))

        self.meta_schema = getattr(meta, 'meta_schema', None)

        if self.meta_schema and not issubclass(self.meta_schema, ObjectAttrsMeta):
            raise Exception("{} must be a subclass of ObjectAttrsMeta".format(self.meta_schema))

        self.object_type = getattr(meta, 'object_type', None) or 'undefined'
        self.model_name = getattr(meta, 'model', None) or None
        self.url_template = getattr(meta, 'url_template', None) or '{api_url}'

class ObjectAttrs(schemas.ExtSchema):
    OPTIONS_CLASS = ObjectAttrsOpts

    @classmethod
    def prepare(cls):
        object_attrs_registry.register(cls)

class ObjectLinks(schemas.ExtSchema):
    self = fields.String(required=True)

class ObjectOpts(SchemaOpts):
    def __init__(self, meta, **kwargs):
        SchemaOpts.__init__(self, meta, **kwargs)
        self.attrs_schema = getattr(meta, 'attrs_schema', None)
        if self.attrs_schema and not issubclass(self.attrs_schema, ObjectAttrs):
            raise Exception("{} must be a subclass of ObjectAttrs".format(self.attrs_schema))

class Object(schemas.ExtSchema):
    OPTIONS_CLASS = ObjectOpts
    id = fields.Str(required=True)
    links = fields.Nested(ObjectLinks, name='links')
    _type = fields.String(required=True, data_key='type')

    # meta = fields.Nested(ObjectMeta)

    def __init__(
            self, only=None, exclude=(), many=False, context=None,
            load_only=(), dump_only=(), partial=False, unknown=None,
    ):

        self._declared_fields['attributes'] = fields.Nested(
            self.opts.attrs_schema, name='attributes', many=False
        )

        relationships_schema = getattr(self.opts.attrs_schema.opts, 'relationships_schema', None)

        if relationships_schema:
            self._declared_fields['relationships'] = fields.Nested(relationships_schema, many=False,
                                                                   name='relationships')

        meta_schema = getattr(self.opts.attrs_schema.opts, 'meta_schema', None)

        if meta_schema:
            self._declared_fields['meta'] = fields.Nested(meta_schema, many=False,
                                                          name='meta')

        super().__init__(only=only, exclude=exclude, many=many, context=context,
                         load_only=load_only, dump_only=dump_only,
                         partial=partial, unknown=unknown)

    @pre_dump(pass_many=False)
    def prepare_data(self, data):
        ident = getattr(data, 'id', None) or getattr(data.meta, 'id')
        slug = getattr(data, 'slug', None)
        if isinstance(slug, InnerDoc):
            lang = get_language()
            slug = slug[lang]
        if slug:
            ident = '{},{}'.format(ident, slug)

        res = dict(
            attributes=data,
            id=str(ident),
            _type=self.opts.attrs_schema.opts.object_type
        )
        object_url = self.opts.attrs_schema.opts.url_template.format(
            api_url=self.api_url,
            ident=ident,
            data=data
        )
        res['links'] = {
            'self': object_url
        }

        if 'meta' in self._declared_fields:
            res['meta'] = data

        if 'relationships' in self.fields:
            relationships = {}
            for name, field in self.fields['relationships'].schema.fields.items():
                _name = field.attribute or name
                field._schema.context.update(object_url=object_url)
                value = getattr(data, _name, None)
                if value:
                    relationships[_name] = value
            if relationships:
                res['relationships'] = relationships
        return res

class TopLevelMeta(schemas.ExtSchema):
    language = fields.String()
    params = fields.Raw()
    path = fields.String()
    count = fields.Integer()
    relative_uri = fields.String()
    aggregations = fields.Raw()
    server_time = fields.DateTime(default=now)

class TopLevelLinks(schemas.ExtSchema):
    self = fields.String()
    first = fields.String()
    last = fields.String()
    prev = fields.String()
    next = fields.String()

class TopLevelOpts(SchemaOpts):
    def __init__(self, meta, **kwargs):
        SchemaOpts.__init__(self, meta, **kwargs)
        self.attrs_schema = getattr(meta, 'attrs_schema', None)
        self.aggs_schema = getattr(meta, 'aggs_schema', None)

        # self.errors_schema = getattr(meta, 'errors_schema', ResponseErrors)
        self.meta_schema = getattr(meta, 'meta_schema', TopLevelMeta)
        self.links_schema = getattr(meta, 'links_schema', TopLevelLinks)

        self.max_items_num = getattr(meta, 'max_items_num', 10000)

        if self.attrs_schema and not issubclass(self.attrs_schema, ObjectAttrs):
            raise Exception("{} must be a subclass of ObjectAttrs".format(self.attrs_schema))
        if self.meta_schema and not issubclass(self.meta_schema, TopLevelMeta):
            raise Exception("{} must be a subclass of Meta".format(self.meta_schema))
        if self.aggs_schema:
            if not issubclass(self.aggs_schema, schemas.ExtSchema):
                raise Exception("{} must be a subclass of ExtSchema".format(self.aggs_schema))
            self.meta_schema.aggregations = fields.Nested(self.aggs_schema, many=False)
        if self.links_schema and not issubclass(self.links_schema, TopLevelLinks):
            raise Exception("{} must be a subclass of Links".format(self.links_schema))

class Aggregation(schemas.ExtSchema):
    id = fields.String(attribute='key')
    title = fields.String()
    doc_count = fields.Integer()

    @pre_dump(pass_many=True)
    def prepare_data(self, data, many):
        if many:
            for item in data:
                item['title'] = str(item.key).upper()
            return data

class TopLevel(schemas.ExtSchema):
    included = fields.Raw()
    jsonapi = fields.Raw(default={'version': '1.0'})
    errors = fields.Raw()

    OPTIONS_CLASS = TopLevelOpts

    def __init__(
            self, only=None, exclude=(), many=False, context=None,
            load_only=(), dump_only=(), partial=False, unknown=None,
    ):
        data_cls = type(
            '{}Data'.format(self.__class__.__name__),
            (Object,), {}
        )
        setattr(data_cls.opts, 'attrs_schema', self.opts.attrs_schema)

        self._declared_fields['data'] = fields.Nested(data_cls, name='data', many=many)

        if self.opts.meta_schema:
            if self.opts.aggs_schema:
                self.opts.meta_schema._declared_fields['aggregations'] = fields.Nested(self.opts.aggs_schema,
                                                                                many=False)
            self._declared_fields['meta'] = fields.Nested(
                self.opts.meta_schema, name='meta', many=False
            )

        if self.opts.links_schema:
            self._declared_fields['links'] = fields.Nested(
                self.opts.links_schema, name='links', many=False
            )
        context = context or {}
        context['is_listing'] = many

        super().__init__(only=only, exclude=exclude, many=False, context=context,
                         load_only=load_only, dump_only=dump_only,
                         partial=partial, unknown=unknown)

    @pre_dump
    def prepare_top_level(self, c):
        def _get_page_link(page_number):
            cleaned_data['page'] = page_number
            return '{}{}?{}'.format(request.prefix, request.path, builder.build(cleaned_data))

        c.data = getattr(c, 'data', {})
        request = self.context['request']
        c.meta = getattr(c, 'meta', {})
        c.links = getattr(c, 'links', {})
        cleaned_data = dict(request.context.cleaned_data)

        c.meta.update({
            'language': request.language,
            'params': request.params,
            'path': request.path,
            'relative_uri': request.relative_uri,
        })

        c.links['self'] = request.uri

        if self.context['is_listing']:
            c.meta['aggregations'] = getattr(c.data, 'aggregations', {}) or {}
            if isinstance(c.data, paginator.Page):
                items_count = c.data.paginator.count
            else:
                items_count = c.data.hits.total \
                    if hasattr(c.data, 'hits') else 0
            c.meta['count'] = items_count
            page, per_page = cleaned_data.get('page', 1), cleaned_data.get('per_page', 20)
            c.links['self'] = _get_page_link(page)
            if page > 1:
                c.links['first'] = _get_page_link(1)
                c.links['prev'] = _get_page_link(page - 1)
            if items_count:
                max_count = min(items_count, self.opts.max_items_num)
                off = 1 if max_count % per_page else 0
                last_page = max_count // per_page + off
                if last_page > 1:
                    c.links['last'] = _get_page_link(last_page)
                if page * per_page < max_count:
                    c.links['next'] = _get_page_link(page + 1)
        return c


import post_lib
import json

def relay(data):
    if 'type' in data.keys():
        if data['type'] == 'request':
            return json.dumps({"type":"relay","relay":[]})
        else:
            return False
    else:
        return False

if __name__=="__main__":
    server = post_lib.Server(host="127.0.0.1",port=5000,debug=True)
    server.add_response(relay)
    server.run()


from lego_robot import *
from math import sin, cos

# Find the derivative in scan data, ignoring invalid measurements.
def compute_derivative(scan, min_dist):
    jumps = [ 0 ]
    for i in range(1, len(scan) - 1):
        l = scan[i-1]
        r = scan[i+1]
        if l > min_dist and r > min_dist:
            derivative = (r - l) / 2.0
            jumps.append(derivative)
        else:
            jumps.append(0)
    jumps.append(0)
    return jumps

def find_cylinders(scan, scan_derivative, jump, min_dist):
    cylinder_list = []
    on_cylinder = False
    sum_ray, sum_depth, rays = 0.0, 0.0, 0
    cylinder_list = []
    on_cylinder = False
    sum_ray, sum_depth, rays = 0.0, 0.0, 0
    for i in range(len(scan_derivative)):
        if scan_derivative[i] < -jump:
            on_cylinder = True
            print(scan_derivative[i])
            sum_ray, sum_depth, rays = 0.0, 0.0, 0
        elif scan_derivative[i] > jump:
            if on_cylinder and rays > 0:
                cylinder_list.append((sum_ray / rays, sum_depth / rays))
            on_cylinder = False
        if scan[i] > min_dist:
            sum_ray += i
            sum_depth += scan[i]
            rays += 1
    return cylinder_list

def compute_cartesian_coordinates(cylinders, cylinder_offset):
    result = []
    for c in cylinders:
        # --->>> Insert here the conversion from polar to Cartesian coordinates.
        calibrated_range=c[1]+cylinder_offset
        theta=LegoLogfile.beam_index_to_angle(c[0])
        x=calibrated_range*cos(theta)
        y=calibrated_range*sin(theta)
        print(x,y,theta)
        result.append((x,y))

    return result

if __name__ == '__main__':

    minimum_valid_distance = 20.0
    depth_jump = 100.0
    cylinder_offset = 90.0

    # Read the logfile which contains all scans.
    logfile = LegoLogfile()
    logfile.read("robot4_scan.txt")

    out_file = open("cylinders.txt", "w+")
    for scan in logfile.scan_data:
        # Find cylinders.
        der = compute_derivative(scan, minimum_valid_distance)
        cylinders = find_cylinders(scan, der, depth_jump,
                                   minimum_valid_distance)
        cartesian_cylinders = compute_cartesian_coordinates(cylinders,
                                                            cylinder_offset)
        # Write to file.
        out_file.write("D C")
        for c in cartesian_cylinders:
            out_file.writelines(str(c))
        out_file.write("\n")

    out_file.close()


import cv2
import numpy as np
from numpy.core.multiarray import ndarray

inpath = "C:\\Users\\M\\Documents\\MATLAB\\"
outpath = "C:\\Users\\M\\Documents\\MATLAB\\"
# src = "ceshi"
# raw = cv2.imread(inpath + src + ".jpg")
# raw2 = cv2.cvtColor(raw, cv2.COLOR_BGR2GRAY)
# for i in range(raw2.shape[0]):
# 	for j in range(raw2.shape[1]):
# 		if raw2[i,j] >= 100:
# 			raw2[i,j] = 100
# 		else:
# 			raw2[i,j] = 50
# cv2.imwrite(outpath + src + "change" + ".jpg", raw2)
re2 = np.loadtxt(outpath + "csv2" + ".csv", dtype=np.int32, delimiter=",", encoding='utf-8')
re1 = np.loadtxt(outpath + "csv1" + ".csv", dtype=np.int32, delimiter=",", encoding='utf-8')
re = re2 - re1
print(re)
# np.savetxt(outpath + "re.csv", re, fmt="%d", delimiter=',')
np.savetxt(outpath + "re.csv", re ,fmt='%d', delimiter=',')


'''
	Combines Real and Imaginary optical constants using Effective Medium Theory for SCIENCE
	Then calculates emissivites based on the optical constants derived- also for SCIENCE

	Author: Dr. Farisa Morales (original IDL code) transcribed and modified by Cody King

	TODO:
		-Continue to impliment logic flow for choice of materials and such
		-Remove superfluous code
		-Change to usage of numpy readers(?)
		-Generalize variable names to accomodate other substances?
		-Make stylistic changes as per suggestion
		-Add error handling where errors would be common (e.g. file openers)
'''

try:
	from numpy import interp, real, imag, pi
#interp(x-coord of desired interpolates, x-coord of input data, y-coord of input data)
#Returns a numpy array object; this can be quickly converted to a standard python list
#I don't know if it's necessary (probably not) but I do it anyway just in case
	from kmod import whiteSpaceParser, getStart, getCol, listifier, columnizer
	from cmath import sqrt
	from math import log10
except ImportError:
	print('One or more required modules could not be found; check the python distribution library.')
	raw_input('Press ENTER to exit')
	exit()

timey = True
try:
	from os import times
except ImportError:
	timey = False

if timey: TIME1 = times()[0]

print('Initializing...')

#Thinking of adding a default variable loader
DEFAULTDIR = r'C:\Users\Cody\Desktop\School Documents\Physics\Independent Study\DielectricEffect'

#-----------------------------------------------------------
#Tiny simplifying functions that need to be declared earlier
def logicFlow (Q):
	resp = raw_input(Q)
	if resp == '1':
		return True
	elif resp == '2':
		return False
	else:
		print(resp + ' is not valid input.')
		return None
#-----------------------------------------------------------

#--------------------
#- Global Variables -
#--------------------
#For whether or not we will be making our own polluted matrix prior to re-inclusion;
#if true we do not make our own
preDMat = None
while preDMat == None:
	preDMat = logicFlow(r'Use a predetermined matrix composition? 1)Yes 2)No: ')

#For whether or not we will be running MIE theory on the optical constants calculated
#here. The value of this constant should be self-explanitory to what it means.
RUNMIE = None
while RUNMIE == None:
	RUNMIE = logicFlow(r'Run Mie Theory? 1)Yes 2)No: ')

#For whether or not we will be writing new lists to memory. If true, checks if we want to
#write the results of interpolations, EMT, and if running MIE, the results of MIE theory
WRITE = None
while WRITE == None:
	WRITE = logicFlow(r'Write results to disk? 1)Yes 2)No: ')

if WRITE:
	WRITE_INT = None
	while WRITE_INT == None:
		WRITE_INT = logicFlow(r'Write results of interpolates to disk? 1)Yes 2)No: ')
	WRITE_EMT = None
	while WRITE_EMT == None:
		WRITE_EMT = logicFlow(r'Write results of EMT to disk? 1)Yes 2)No: ')
	if RUNMIE:
		WRITE_MIE = None
		while WRITE_MIE == None:
			WRITE_MIE = logicFlow(r'Write results of MIE Theory to disk? 1)Yes 2)No: ')

#For whether or not we will be generating graphs of the results of the intpolations,
#EMT, and if running MIE, the results of MIE theory.
GRAPH = None
while GRAPH == None:
	GRAPH = logicFlow(r'Graph results? 1)Yes 2)No: ')

if GRAPH:
	GRAPH_EMT = None
	while GRAPH_EMT == None:
		GRAPH_EMT = logicFlow(r'Graph results of EMT? 1)Yes 2)No: ')
	if RUNMIE:
		GRAPH_MIE = None
		while GRAPH_MIE == None:
			GRAPH_MIE = logicFlow(r'Graph results of MIE Theory? 1)Yes 2)No: ')
	import matplotlib.pyplot as plt

print('Orbital Variables:\n\tpreDMat-' + str(preDMat) + '\n\tRUNMIE-' + str(RUNMIE) + '\n\tWRITE-' + str(WRITE) + '\n\tGRAPH-' + str(GRAPH) + '\nSuborbital Variables:')
if WRITE:
	print('\tWRITE_INT- ' + str(WRITE_INT) + '\n\tWRITE_EMT- ' + str(WRITE_EMT))
	if RUNMIE:
		print('\tWRITE_MIE- ' + str(WRITE_MIE))
if GRAPH:
	print('\tGRAPH_EMT- ' + str(GRAPH_EMT))
	if RUNMIE:
		print('\tGRAPH_MIE- ' + str(GRAPH_MIE))

#---------------------------------------------
#- File directories for the necessary tables -
#---------------------------------------------

#File directory for written tables
directory = raw_input(r'Please specifiy the file directory (Press ENTER for default): ')
if directory == '':
	directory = DEFAULTDIR

print('File Directory:\n' + directory)

if not preDMat:
	#Amorphous Carbon Optical Constants
	dir_AC = directory + r'\AmorphCarbOptConst.dat'

	#Water Optical Constants
	dir_WA = directory + r'\waterOpticalConstants.dat'

else:
   #Dirty Ice Optical Constants
	dir_DI = directory + r'\DirtyIceOptConst.dat'

#AstroSil Optical Constants
dir_AS = directory+ r'\AstroSilOptConst.dat'

#Desired Wavelengths
dir_W = directory + r'\wav_set.dat'

#Desired Grain Sizes; current table is currently unreadable by my modules so it needs to manually
#be modified; sorry!
dir_G = directory + r'\grain_size_grid_suvsil.list'

#------------------------------------------------------
#- Short functions which simplify (maybe) some things -
#------------------------------------------------------
#----------------------------------------------------------------
#Short function to trim lists based on designated mins and maxes
def trimmer(LIST, mini, maxi):
	tempList = []
	for ele in LIST:
		if ele >= mini and ele <= maxi:
			tempList.append(ele)
	return tempList
#End trimming function
#-------------------------------------------------------------
#Short function to write to a file data from a dictionary.
#Writes to a csv file the keys as a header followed by the values
#associated with each.
#Python doesn't care if we want the string read-in as a raw string
#if it sees a \' at the end of a string it complains about end-of-line errors!
def writerD(DICT, FILENAME):
	tempFile = open(directory + '\\' + FILENAME, 'w')
	for ele in DICT.keys():
		if ele is not 'NAME':
			tempFile.write(str(ele) + ',')
	tempFile.write('\n')
	row = 0
	while row < len(DICT[DICT.keys()[0]]):
		for key in DICT.keys():
			if key is not 'NAME':
				tempFile.write(str((DICT[key][row])) + ',')
		tempFile.write('\n')
		row += 1
	tempFile.close()
#End short writing function
#--------------------------------------------------------
#Short writing function specifically for the emissivities.
#Writes to a csv file a header of wavelengths followed by
#rows of emissivities with the first value in each row being
#the associated grain radius.
#Done as dictionaries are (seemingly) randomly ordered when it
#comes to numerical keys
def writerE(HEADER, ARRAYARRAY, FILENAME):
	tempFile = open(directory + '\\' + FILENAME, 'w')
	tempFile.write(str(None) + ',')	#Include header explaining table and content and generating parameters?
	for ele in HEADER:
		tempFile.write(str(ele) + ',')
	tempFile.write('\n')
	for ARRAY in ARRAYARRAY:
		for ele in ARRAY:
			tempFile.write(str(ele) + ',')
		tempFile.write('\n')
	tempFile.close()
#End short writing function
#------------------------------------------------------------------------------------------
#Short function to calculate the average optical constants based on Effective Medium Theory
#NM: Refractive Index of Matrix, KM: Extinction Coefficient of Matrix
#NI: Refractive Index of Inclusion, KI: Extinction Coefficient of Inclusion
#V: Ratio of pollutant volume to total volume
def EMT(NM, KM, NI, KI, V=.5):
	M, I = (NM + KM*1j) ** 2, (NI + KI*1j) ** 2
	F = (I - M) / (I + 2.0 * M)
	AMOC = sqrt(M * (1 + ((3.0 * V * F) / (1 - V * F))))
	return float(real(AMOC)), float(imag(AMOC))
#End REALLY short EMT function
#-------------------------------------------------------------
#Short function to utilize the above EMT function in recursion
#Argument is a list of tuples, each tuple comprised of the dictionary of the matrix
#material, the dictionary of the inclusion material, and then the decimal volume
#fraction of the matrix to total volume. Keyword arguments are: 'recur' and 'vacuum'.
#'recur' can take the place of the matrix or inclusion except on the first iteration;
#'vacuum' can only take the place of the inclusion.
def EMTRecursion(*mivargs):
	lastIter, iteration = {'Refractive Index':[], 'Extinction Coefficient':[]}, 0
	for miv in mivargs:
		m, i, v = miv
		if m == 'vacuum' or (iteration == 0 and (m == 'recur' or i == 'recur')) or v >= 1 or v <= 0:
			print('An error has occured; vacuum has been designated as the matrix material or\na recur was encountered on the first iteration\nIteration: ' + str(iteration) + ' or an invalid ratio was encountered')
			return None
		if m == 'recur':
			m = {'Refractive Index':[], 'Extinction Coefficient':[]}
			m.update(lastIter)
		if i == 'recur':
			i = {'Refractive Index':[], 'Extinction Coefficient':[]}
			i.update(lastIter)
		lastIter.update({'Refractive Index':[], 'Extinction Coefficient':[]})
		for it, mRI in enumerate(m['Refractive Index']):
			if i == 'vacuum':
				RI, EC = EMT(mRI, m['Extinction Coefficient'][it], 1, 0, v)
			else:
				RI, EC = EMT(mRI, m['Extinction Coefficient'][it], i['Refractive Index'][it], i['Extinction Coefficient'][it], v)
			lastIter['Refractive Index'].append(RI)
			lastIter['Extinction Coefficient'].append(EC)
		iteration += 1
	return lastIter
#End short recursion function

#-----------------------------------------------
#- Writing data from tables in files to memory -
#-----------------------------------------------

print('Writing required files to memory...')

if not preDMat:
	#--------------------
	#- Amorphous Carbon -
	#--------------------
	#Open the Amorphous Carbon table in binary mode
	AC_Tab = open(dir_AC, 'rb')

	#Start the reader at the appropriate postion
	AC_Tab.seek(getStart(AC_Tab))

	#Do the thing! Seperate the values!
	#Assign to mList the list of lists returned from Columnizer which took a single
	#list returned by listifier (as well as the number of columns in the table
	#found using the getCol module) which itself took a tuple returned by
	#whiteSpaceParser which consisted of the modified contents of the read file
	#in addition to the index location of whitespaces in that file.
	#For more information, check kmod.py
	mList = columnizer(listifier(whiteSpaceParser(AC_Tab.read())), getCol(AC_Tab))
	AmorphCarb = {'Wavelength(Microns)':mList[0], 'Refractive Index':mList[1], 'Extinction Coefficient':mList[2], 'NAME':'Amorphous Carbon'}

	#Close the file and delete variables no longer needed
	AC_Tab.close()
	del(AC_Tab, mList, dir_AC)

	#-------------
	#- Water Ice -
	#-------------
	#Similar proceedure for Water
	WA_Tab = open(dir_WA, 'rb')

	WA_Tab.seek(getStart(WA_Tab))

	mList = columnizer(listifier(whiteSpaceParser(WA_Tab.read())), getCol(WA_Tab))

	Water = {'Wavelength(Microns)':mList[0], 'Refractive Index':mList[1], 'Extinction Coefficient':mList[2], 'NAME':'Water Ice'}

	WA_Tab.close()

	del(WA_Tab, mList, dir_WA)

else:
	#-------------
	#- Dirty Ice -
	#-------------
	#Similar proceedure for Dirty Ice
	DI_Tab = open(dir_DI, 'rb')

	DI_Tab.seek(getStart(DI_Tab))

	mList = columnizer(listifier(whiteSpaceParser(DI_Tab.read())), getCol(DI_Tab))

	DirtyIce = {'Wavelength(Microns)':mList[0], 'Refractive Index':mList[1], 'Extinction Coefficient':mList[2], 'NAME':'Dirty Ice'}

	DI_Tab.close()

	del(DI_Tab, mList, dir_DI)

#------------
#- AstroSil -
#------------
#Similar proceedure for AstroSil
AS_Tab = open(dir_AS, 'rb')

AS_Tab.seek(getStart(AS_Tab))

mList = columnizer(listifier(whiteSpaceParser(AS_Tab.read())), getCol(AS_Tab))

#Values must be put in reverse order for ease of interpolation later
#Reversing can't be done in a dictionary assignment because it returns
#a None type for the value for some reason
mList[0].reverse()
mList[3].reverse()
mList[4].reverse()
AstroSil = {'Wavelength(Microns)':mList[0], 'Refractive Index':mList[3], 'Extinction Coefficient':mList[4], 'NAME':'Astro Silicates'}

#Apparently the author of the AstroSil table created the table with the real parts decremented by 1?
#Some of the values in the list now incremented by 1 show floating point arithmetic relics
#I'm pretty confident that's just a visual problem
for i, ele in enumerate(AstroSil['Refractive Index']):
	AstroSil['Refractive Index'][i] = ele + 1.0

AS_Tab.close()

del(AS_Tab, mList, dir_AS, i, ele)

#-----------------
#- Desired Waves -
#-----------------
#Similar proceedure for the desired Wavelengths
W_Tab = open(dir_W, 'rb')

W_Tab.seek(getStart(W_Tab))

mList = columnizer(listifier(whiteSpaceParser(W_Tab.read())), getCol(W_Tab))

Waves = {'Wavelength(Microns)':mList[0]}

W_Tab.close()

del(W_Tab, mList, dir_W)

#-----------------------
#- Desired Grain Sizes -
#-----------------------
#A litte different proceedure for the desired Grain Sizes
G_Tab = open(dir_G, 'rb')

G_Tab.seek(getStart(G_Tab))

mList = columnizer(listifier(whiteSpaceParser(G_Tab.read())), getCol(G_Tab))

Waves.update({'SIZE':mList[0]})

G_Tab.close()

del(G_Tab, mList, dir_G)

#-----------------------------------------------------------
#- Trimming Wave lists for useful Wavelengths; the prequel -
#-----------------------------------------------------------
#New Wavelengths based on Primary Matrix Material; determines Wavelength for all others.
#As this is needed for interpolation it must be done before the interpolation
#but assignment to other dictionaries must be done after.

print('Trimming Wave list...')

if not preDMat:
	Waves.update({'Wavelength(Microns)':trimmer(Waves['Wavelength(Microns)'], min(Water['Wavelength(Microns)']), max(Water['Wavelength(Microns)']))})

else:
	Waves.update({'Wavelength(Microns)':trimmer(Waves['Wavelength(Microns)'], min(DirtyIce['Wavelength(Microns)']), max(DirtyIce['Wavelength(Microns)']))})

#---------------------------------------------------------------------------
#- Interpolate using SciPy's (NumPy's?) interp function for missing values -
#---------------------------------------------------------------------------
#Update the dictionaries with these interpolations, changing complex coefficients
#into complex type. For some reason I get erroneous optical constants from EMT if
#I change these into complex numbers piecewise during EMT, I still can't figure
#out why

print('Interpolating for missing values and updating the appropriate dictionaries...')

if not preDMat:
	#Update Amorphous Carbon Dict with interpolates for reals
	AmorphCarb.update({'Refractive Index':list(interp(Waves['Wavelength(Microns)'], AmorphCarb['Wavelength(Microns)'], AmorphCarb['Refractive Index']))})

	#Update Amorphous Carbon Dict with interpolates for complex
	AmorphCarb.update({'Extinction Coefficient':list(interp(Waves['Wavelength(Microns)'], AmorphCarb['Wavelength(Microns)'], AmorphCarb['Extinction Coefficient']))})

	#Update Water Dict with interpolates for reals
	Water.update({'Refractive Index':list(interp(Waves['Wavelength(Microns)'], Water['Wavelength(Microns)'], Water['Refractive Index']))})

	#Update Water Dict with interpolates for complex
	Water.update({'Extinction Coefficient':list(interp(Waves['Wavelength(Microns)'], Water['Wavelength(Microns)'], Water['Extinction Coefficient']))})

else:
	#Update DirtyIce Dict with interpolates for reals
	DirtyIce.update({'Refractive Index':list(interp(Waves['Wavelength(Microns)'], DirtyIce['Wavelength(Microns)'], DirtyIce['Refractive Index']))})

	#Update DirtyIce Dict with interpolates for complex
	DirtyIce.update({'Extinction Coefficient':list(interp(Waves['Wavelength(Microns)'], DirtyIce['Wavelength(Microns)'], DirtyIce['Extinction Coefficient']))})

#Update AstroSil Dict with interpolates for reals
AstroSil.update({'Refractive Index':list(interp(Waves['Wavelength(Microns)'], AstroSil['Wavelength(Microns)'], AstroSil['Refractive Index']))})

#Update AstroSil Dict with interpolates for complex
AstroSil.update({'Extinction Coefficient':list(interp(Waves['Wavelength(Microns)'], AstroSil['Wavelength(Microns)'], AstroSil['Extinction Coefficient']))})

#----------------------------------------------------------
#- Trimming Wave lists for useful Wavelengths; the sequel -
#----------------------------------------------------------

print('Doin\' some more trimming...')

if not preDMat:
	#New Wavelenghts for Amorphous Carbon
	AmorphCarb.update({'Wavelength(Microns)':Waves['Wavelength(Microns)']})

	#New Wavelenghts for Water
	Water.update({'Wavelength(Microns)':Waves['Wavelength(Microns)']})

else:
	#New Wavelenghts for DirtyIce
	DirtyIce.update({'Wavelength(Microns)':Waves['Wavelength(Microns)']})

#New Wavelenghts for AstroSil
AstroSil.update({'Wavelength(Microns)':Waves['Wavelength(Microns)']})

#--------------------------------------------------------------
#- Computing Optical Constants for Inclusion-Matrix Particles -
#--------------------------------------------------------------
#Finally doing part of what this program is named after! Yay!
#Initialize the Optical Constants dictionary

print('Implimenting EMT to compute Optical Constants...')

IMPOptConst = {'Wavelength(Microns)':Waves['Wavelength(Microns)'], 'Refractive Index':[], 'Extinction Coefficient':[], 'NAME':'Inclusion Matrix Particles'}

#Iterate through designated reals and utilize EMT to generate new
#optical constants

if not preDMat:
	IMPOptConst.update(EMTRecursion((Water, AmorphCarb, 0.5), ('recur', AstroSil, 0.5)))

else:
	IMPOptConst.update(EMTRecursion((DirtyIce, AstroSil, 0.5)))

if WRITE:
	if WRITE_INT:
		print('Writing Interpolate Data to disk...')
		if not preDMat:
			writerD(AmorphCarb, 'AmorphCarbInterp2.csv')
			writerD(Water, 'WaterInterp2.csv')
		else:
			writerD(DirtyIce, 'DirtyIceInterp2.csv')
			writerD(AstroSil, 'AstroSilInterp2.csv')
	if WRITE_EMT:
		print('Writing EMT Data to disk...')
		writerD(IMPOptConst, 'IMPOptConst2.csv')

if GRAPH and GRAPH_EMT:
#--------------------------------------------------
#- Plot appropriate values if designated to do so -
#--------------------------------------------------
	def flotEMT(*flargs):
		plt.xlabel('Wavelength(Microns)')
		plt.ylabel('Exitinction Coefficient')
		plt.title('Extinction Coefficient Vs. Wavelength')
		plt.xscale('log')
		plt.yscale('log')
		plt.grid(True)
		for Exti in flargs:
			plt.plot(Exti['Wavelength(Microns)'], Exti['Extinction Coefficient'], label = Exti['NAME'])
		plt.legend(loc = 0)
		plt.show()

		plt.xlabel('Wavelength(Microns)')
		plt.ylabel('Refractive Index')
		plt.title('Refractive Index Vs. Wavelength')
		plt.xscale('log')
		plt.grid(True)
		for Refracti in flargs:
			plt.plot(Refracti['Wavelength(Microns)'], Refracti['Refractive Index'], label = Refracti['NAME'])
		plt.legend(loc = 0)
		plt.show()

	if not preDMat:
		flotEMT(Water, AmorphCarb, AstroSil, IMPOptConst)
	else:
		flotEMT(DirtyIce, AstroSil, IMPOptConst)

if RUNMIE:
#--------------
#- MIE Theory -
#--------------
#Each refractive index value is associated with a particular Wavelength.
#Inside an iteration of the values of the refractive index, we iterate
#through the grain sizes. We call bhmie with the size parameter associated
#with that particular grain size with that particular Wavelength, along
#with the sum of the refractive index and extinction coefficient associated
#with that Wavelength. The resulting Emiss list is a list of lists; each
#sublist is the emissivity of the grains of the various sizes associated
#with the Wavelength of incident light.

	print('Utilizing MIE Theory to calculate Emissivity of the IMPs.\nDepending on the number of Wavelengths and Grain Sizes,\nthis can take qite a bit of time... [>1hr]')

	if not preDMat:
		del(AmorphCarb, Water, IntermediaryMatrix)
	else:
		del(DirtyIce)
	del(AstroSil)

	from bhmie_herbert_kaiser_july2012_GEOFF_EDITION import bhmie
	Emiss = []
	if timey: Time0 = times()[0]
	sizey = len(Waves['SIZE'])
	for iter, radius in enumerate(Waves['SIZE']):
		print('Emissivites for grains of radius: ' + str(radius) + ' microns. ' + str(iter + 1) + ' of ' + str(sizey))
		if timey:
			print('\nTime for last radius: ' + str(times()[0] - Time0) + ' seconds')
			Time0 = times()[0]
		Emiss.append([radius])
		for i, lamda in enumerate(Waves['Wavelength(Microns)']):
			RI = IMPOptConst['Refractive Index'][i]
			if type(IMPOptConst['Extinction Coefficient'][i]) is not type(1j):
				EC = IMPOptConst['Extinction Coefficient'][i]*1j
			else:
				EC = IMPOptConst['Extinction Coefficient'][i]
			qabs = bhmie((2*pi*radius)/lamda, RI + EC, 2)
			if (qabs <= 0):
				qabs = 1.0e-11
			Emiss[iter].append(qabs)

if WRITE and RUNMIE:
	if WRITE_MIE:
		print('Writing MIE Data to disk...')
		writerE(Waves['Wavelength(Microns)'], Emiss, 'Emissivities.csv')

if GRAPH and RUNMIE:
	if GRAPH_MIE:
		def flotMIE(EMISS):
			plt.xlabel('Wavelength(Microns)')
			plt.ylabel('Emissivity')
			plt.title('Emissivity Vs. Wavelength')
			plt.xscale('log')
			plt.yscale('log')
			plt.grid(True)
			for ARRAY in EMISS:
				if (log10(ARRAY[0]) == int(log10(ARRAY[0]))):
					plt.plot(Waves['Wavelength(Microns)'], ARRAY[1:], label = ARRAY[0])
			plt.legend(loc = 0)
			plt.show()

		flotMIE(Emiss)
if timey:
	print('Run Time: ' + str(times()[0] - TIME1) + ' seconds')
raw_input('Press ENTER to exit')

'''
if not preDMat:
	for i, ele in enumerate(Water['Refractive Index']):

		#Utilize EMT for Amorphous Carbon and Water
		RI, EC = EMT(ele, Water['Extinction Coefficient'][i], AmorphCarb['Refractive Index'][i], AmorphCarb['Extinction Coefficient'][i])

		#Append the appropriate values to the Intermediary Matrix dictionary
		IntermediaryMatrix['Refractive Index'].append(RI)
		if EC < 0:
			EC = 1.0e-11
		IntermediaryMatrix['Extinction Coefficient'].append(EC)

		#Utilize the results from the last utilization of EMT for this run of EMT
		#now with AstroSil
		RI, EC = EMT(IntermediaryMatrix['Refractive Index'][i], IntermediaryMatrix['Extinction Coefficient'][i], AstroSil['Refractive Index'][i], AstroSil['Extinction Coefficient'][i])

		#Append the appropriate values to the IMP dictionary
		IMPOptConst['Refractive Index'].append(RI)
		if EC < 0:
			EC = 1.0e-11
		IMPOptConst['Extinction Coefficient'].append(EC)

else:
	for i, ele in enumerate(DirtyIce['Refractive Index']):

		#Utilize EMT with the Predetermined Matrix and Inclusion
		RI, EC = EMT(ele, DirtyIce['Extinction Coefficient'][i], AstroSil['Refractive Index'][i], AstroSil['Extinction Coefficient'][i])

		#Append the appropriate values to the IMP dictionary
		IMPOptConst['Refractive Index'].append(RI)
		if EC < 0:
			EC = 1.0e-11
		IMPOptConst['Extinction Coefficient'].append(EC)

del(i, ele, RI, EC)
'''


from glob import glob
import os
import tempfile
import tarfile
from urllib.request import urlretrieve
import setuptools
import shutil

VERSION = '3.1'

# Download and unpack the correct tarballs
# Pick those up from http://openrefine.org/download.html
def provision():
    linux_url = f'https://github.com/OpenRefine/OpenRefine/releases/download/{VERSION}/openrefine-linux-{VERSION}.tar.gz'

    dest_path = os.path.join(
        os.path.dirname(os.path.abspath(__file__)),
        'openrefine',
        'extracted'
    )
    shutil.rmtree(dest_path, ignore_errors=True)
    with tempfile.NamedTemporaryFile() as tar_file, tempfile.TemporaryDirectory() as extract_dir:
        urlretrieve(linux_url, tar_file.name)
        tf = tarfile.open(tar_file.name)
        tf.extractall(extract_dir)
        shutil.move(os.path.join(extract_dir, f'openrefine-{VERSION}'), dest_path)

provision()

setuptools.setup(
    name="openrefine-bin",
    version=VERSION,
    url="https://github.com/yuvipanda/openrefine-bin",
    author="Yuvi Panda",
    author_email='yuvipanda@gmail.com',
    description="Binary package distribution of OpenRefine",
    packages=setuptools.find_packages(),
    package_data={
        '*': 'openrefine/extracted/*'
    },
    zip_safe=False,
    include_package_data=True,
    entry_points={
        'console_scripts': [
            'refine = openrefine:main'
        ]
    }
)


#Finding Primes Using FLT
#Luis Loja
#January 1, 2017

#this program will use similar codes for the other types of prime numbers
#using fermats little theorem
ans="y"
while ans=="y":
    print()
    print("Hello, we are doing to be determining if one if a number is prime."
          "  Then we are going to look at the first twin, cousin and sexy primes below the index number of a prime")
    print()

    choice=input("Type(a)if you are trying to check if a number is prime." #Asking the user what he/she wants
    " Type(b) if you are trying to find twin primes."
    " Type(c)if you are trying to find cousin prime."
    " Type (d) if you are trying to find sexy primes:  ")

    if choice=="a":  #If the user wants to check the primality of a number
       print()
       a=int(input("Enter what number you want to determine the primality of: "))  #User enters any number
       if a==1:
           print(a,"is not prime nor composite.")  #This is if they type in one, because it is neither prime nor composite
           break
       if(2**(a-1)%a)==1:   #First check to see if the entered number is prime
        for i in range(2,int(a**0.5)+1):  #check if it is a pseudoprime
             if (a%i)==0:
                print(a,"is not prime!It is a pseudoprime.")
                break
        else:
             print(a,"is prime")
       else:
          print(a,"is composite")

    if choice=="b":   #If the user wants to find twin primes
        print()
        count=2
        p=3
        b=int(input("Enter the max prime index number that the twin primes can be:  "))
        while count<b+2:
            if(2**(p-1)%p)==1:
               count+=1
               if(2**(p+1)%(p+2))==1: #To find twin primes
                for i in range(2,int(p**0.5)+1):  #check if it is a pseudoprime
                    if (p%i)==0:
                        count-=1
                        break
                else:
                    print(p,"and",p+2,"are twin primes") #This prints the twin primes
            p+=1

    if choice=="c": #If the user wants to find cousin primes
        print()
        count=2
        p=3
        c=int(input("Enter the max prime index number that the cousin primes can be:  "))
        while count<c+2:
            if(2**(p-1)%p)==1:
               count+=1
               if(2**(p+3)%(p+4))==1: #To find cousin primes
                    for i in range(2,int(p**0.5)+1):  #check if it is a pseudoprime
                        if (p%i)==0:
                            count-=1
                            break
                    else:
                        print(p,"and",p+4,"are cousin primes") #This prints the cousin primes
            p+=1

    if choice=="d":  #If the user wants to find sexy primes
        print()
        count=2
        p=3
        d=int(input("Enter the max prime index number that the sexy primes can be:  "))
        while count<d+2:
            if(2**(p-1)%p)==1:
               count+=1
               if(2**(p+5)%(p+6))==1: #To find sexy primes
                    for i in range(2,int(p**0.5)+1):  #check if it is a pseudoprime
                        if (p%i)==0:
                            count-=1
                            break
                    else:
                        print(p,"and",p+6,"are sexy primes") #This prints the sexy primes
            p+=1
    print()
    ans=input("Would you like to do this again? (y)es or (n)o: ")  #If the user wants to do this again


#!/usr/bin/env python3

import winreg as reg

# print environment variables from registry (MACHINE)

args = [ reg.HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" ]
# print(r"HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Session Manager/Environment")
kHandle = reg.CreateKey(*args)
kSubCount, kNumValues, time = reg.QueryInfoKey(kHandle)
kValues = []
for i in range(kNumValues):
    vals = reg.EnumValue(kHandle, i)
    print("%32s %s"%(vals[0], str(vals[1])))
reg.CloseKey(kHandle)


# Count the number of negative numbers in a 2D list (both rows and columns are already sorted)

a = [[-4, -3, -1, 0], [-2, -2, 1, 2], [-1, 1, 2, 3], [1, 2, 4, 5]]
a = [[-1, 0], [0, 0]]
a = [[-2, 0], [-1, 0]]

def countnegatives(arr):
    res = 0
    for i in range(len(arr)):
        for j in range(len(arr[0])):
            if arr[i][j] < 0:
                res += 1
            else:
                break
    return res

def countnegatives2(arr):
    count = 0
    for i in range(len(arr)):
        for j in range(1, len(arr)):
            if (arr[i][len(arr) - j - i] > -1
                    and arr[i][len(arr) - j - 1 - i] <= -1):
                count += len(arr) - j - i
                break
    return count

print(countnegatives2(a))


import numpy as np
import scipy.io
import typing
import time
from reAMRC import reAMRC

def sinGen(n_sample: int=150, mean: float=0.0, sigma: float=2.0, omega: float=0.1, bern: float=0.5,
            filename: str=None, rndseed=None):
    """
    Generate a synthetic dataset as described in Section 6 in the paper. Save the data to `filename` if given.

    $$

    x_t = [4 \cos (\pi ((\cos (\omega t) - 3) / 2 + y'_t)) + \epsilon_1, 4 \sin (\pi ((\cos (\omega t) - 3) / 2 + y'_t)) + \epsilon_2]^T

    y_t \sim \text{Bernoulli}(0.5), y'_t = y_t + 1

    \epsilon_1, \epsilon_2 \sim N(mean, sigma)

    $$

    Input:
    ---
    :n_sample: number of samples in total.

    :mean: mean of epsilon 1 and 2

    :sigma: standard deviation of epsilon 1 and 2

    :omega: angular velocity

    :bern: Pr(y_t=0)

    :filename: The filename to store the synthetic dataset

    :rndseed: random seed for the synthetic dataset generation, for reproducibility of experiments.

    Output:
    ---
    :X: the collection of instances: n_sample by 2 matrix.

    :Y: labels: n_sample by 1 vector.

    Output file: if `filename` != None, X, Y would be saved to filename

    """
    if rndseed != None:
        np.random.seed(rndseed)
    # epsilon = np.random.randn(2, )
    epsilon = mean + sigma * np.random.randn(n_sample, 2)
    Y = (np.random.rand(n_sample,1) > bern).astype('int64')

    t = np.arange(start=1, stop=n_sample+1, step=1).reshape(-1,1)
    inside_sin = np.pi * ((np.cos(omega * t) - 3) / 2 + (Y + 1))
    X = np.hstack([4 * np.cos(inside_sin) , 4 * np.sin(inside_sin)]) + epsilon

    if filename:
        mdict = {'X': X, 'Y': Y}
        if rndseed != None:
            mdict['rndseed'] = rndseed
        scipy.io.savemat(filename, mdict)
    return X, Y

if __name__ == '__main__':
    # sinGen()
    for i in range(1,201):
        sinGen(filename='./temp/test' + str(i) + '.mat')
    # sinGen(filename='test.mat')


from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Base, Restaurant, MenuItem

engine = create_engine('sqlite:///restaurantmenu.db')
Base.metadata.bind = engine # connection between classes and tables
Session = sessionmaker(bind=engine)
session = Session()

veggieBurgers = session.query(MenuItem).filter_by(name = 'Veggie Burger')
for veggieBurger in veggieBurgers:
    print(veggieBurger.id)
    print(veggieBurger.price)
    print(veggieBurger.restaurant.name)
    print('\n')

x = session.query(MenuItem).filter_by(id=3).one()
print(x.price)
x.price = '$2.99'
session.add(x)
session.commit()
x = session.query(MenuItem).filter_by(id=3).one()
print(x.price)

veggieBurgers = session.query(MenuItem).filter_by(name = 'Veggie Burger')
for veggieBurger in veggieBurgers:
    if veggieBurger.price != '$2.99':
        veggieBurger.price = '$2.99'
        session.add(veggieBurger)
        session.commit()


from ibm_watson import LanguageTranslatorV3
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

url = 'URL'

try:
    authenticator = IAMAuthenticator('API_KEY')
    language_translator = LanguageTranslatorV3(
        version='2018-05-01',
        authenticator=authenticator
    )
    language_translator.set_service_url(url)

    def english_to_french(text):
        if not text:
            return ''
        translation = language_translator.translate(
            text=text,
            source='en',
            target='fr'
        ).get_result()
        return translation['translations'][0]['translation']

    def english_to_german(text):
        if not text:
            return ''
        translation = language_translator.translate(
            text=text,
            source='en',
            target='de'
        ).get_result()
        return translation['translations'][0]['translation']

finally:
    language_translator.set_disable_ssl_verification(False)

5. Python Projects/IBM Edx Translator Project/translator.py