__author__ = "duanes (Duane Sand), pdahl (Peter Dahl)"

# A basic cpuset/cgroup container manager for limiting memory use during tests
#   for use on kernels not running some site-specific container manager

import fcntl
import glob
import logging
import os
import re

from autotest.client import utils
from autotest.client.shared import error, utils_memory

SUPER_ROOT = ''      # root of all containers or cgroups
NO_LIMIT = (1 << 63) - 1   # containername/memory.limit_in_bytes if no limit

# propio service classes:
PROPIO_PRIO = 1
PROPIO_NORMAL = 2
PROPIO_IDLE = 3

super_root_path = ''    # usually '/dev/cgroup'; '/dev/cpuset' on 2.6.18
cpuset_prefix = None  # usually 'cpuset.'; '' on 2.6.18
fake_numa_containers = False  # container mem via numa=fake mem nodes, else pages
mem_isolation_on = False
node_mbytes = 0         # mbytes in one typical mem node
root_container_bytes = 0  # squishy limit on effective size of root container

def discover_container_style():
    global super_root_path, cpuset_prefix
    global mem_isolation_on, fake_numa_containers
    global node_mbytes, root_container_bytes
    if super_root_path != '':
        return  # already looked up
    if os.path.exists('/dev/cgroup/tasks'):
        # running on 2.6.26 or later kernel with containers on:
        super_root_path = '/dev/cgroup'
        cpuset_prefix = 'cpuset.'
        if get_boot_numa():
            mem_isolation_on = fake_numa_containers = True
        else:  # memcg containers IFF compiled-in & mounted & non-fakenuma boot
            fake_numa_containers = False
            mem_isolation_on = os.path.exists(
                '/dev/cgroup/memory.limit_in_bytes')
            # TODO: handle possibility of where memcg is mounted as its own
            #       cgroup hierarchy, separate from cpuset??
    elif os.path.exists('/dev/cpuset/tasks'):
        # running on 2.6.18 kernel with containers on:
        super_root_path = '/dev/cpuset'
        cpuset_prefix = ''
        mem_isolation_on = fake_numa_containers = get_boot_numa() != ''
    else:
        # neither cpuset nor cgroup filesystem active:
        super_root_path = None
        cpuset_prefix = 'no_cpusets_or_cgroups_exist'
        mem_isolation_on = fake_numa_containers = False

    logging.debug('mem_isolation: %s', mem_isolation_on)
    logging.debug('fake_numa_containers: %s', fake_numa_containers)
    if fake_numa_containers:
        node_mbytes = int(mbytes_per_mem_node())
    elif mem_isolation_on:  # memcg-style containers
        # For now, limit total of all containers to using just 98% of system's
        #   visible total ram, to avoid oom events at system level, and avoid
        #   page reclaim overhead from going above kswapd highwater mark.
        system_visible_pages = utils_memory.memtotal() >> 2
        usable_pages = int(system_visible_pages * 0.98)
        root_container_bytes = usable_pages << 12
        logging.debug('root_container_bytes: %s',
                      utils.human_format(root_container_bytes))

def need_mem_containers():
    discover_container_style()
    if not mem_isolation_on:
        raise error.AutotestError('Mem-isolation containers not enabled '
                                  'by latest reboot')

def need_fake_numa():
    discover_container_style()
    if not fake_numa_containers:
        raise error.AutotestError('fake=numa not enabled by latest reboot')

def full_path(container_name):
    discover_container_style()
    return os.path.join(super_root_path, container_name)

def unpath(container_path):
    return container_path[len(super_root_path) + 1:]

def cpuset_attr(container_name, attr):
    discover_container_style()
    return os.path.join(super_root_path, container_name, cpuset_prefix + attr)

def io_attr(container_name, attr):
    discover_container_style()
    # current version assumes shared cgroup hierarchy
    return os.path.join(super_root_path, container_name, 'io.' + attr)

def tasks_path(container_name):
    return os.path.join(full_path(container_name), 'tasks')

def mems_path(container_name):
    return cpuset_attr(container_name, 'mems')

def memory_path(container_name):
    return os.path.join(super_root_path, container_name, 'memory')

def cpus_path(container_name):
    return cpuset_attr(container_name, 'cpus')

def container_exists(name):
    return name is not None and os.path.exists(tasks_path(name))

def move_tasks_into_container(name, tasks):
    task_file = tasks_path(name)
    for task in tasks:
        try:
            logging.debug('moving task %s into container "%s"', task, name)
            utils.write_one_line(task_file, task)
        except Exception:
            if utils.pid_is_alive(task):
                raise   # task exists but couldn't move it
            # task is gone or zombie so ignore this exception

def move_self_into_container(name):
    me = str(os.getpid())
    move_tasks_into_container(name, [me])
    logging.debug('running self (pid %s) in container "%s"', me, name)

def _avail_mbytes_via_nodes(parent):
    # total mbytes of mem nodes available for new containers in parent
    free_nodes = available_exclusive_mem_nodes(parent)
    mbytes = nodes_avail_mbytes(free_nodes)
    # don't have exact model for how container mgr measures mem space
    # better here to underestimate than overestimate
    mbytes = max(mbytes - node_mbytes // 2, 0)
    return mbytes

def _avail_bytes_via_pages(parent):
    # Get memory bytes available to parent container which could
    #  be allocated exclusively to new child containers.
    # This excludes mem previously allocated to existing children.
    available = container_bytes(parent)
    mem_files_pattern = os.path.join(full_path(parent),
                                     '*', 'memory.limit_in_bytes')
    for mem_file in glob.glob(mem_files_pattern):
        child_container = unpath(os.path.dirname(mem_file))
        available -= container_bytes(child_container)
    return available

def avail_mbytes(parent=SUPER_ROOT):
    # total mbytes available in parent, for exclusive use in new containers
    if fake_numa_containers:
        return _avail_mbytes_via_nodes(parent)
    else:
        return _avail_bytes_via_pages(parent) >> 20

def delete_leftover_test_containers():
    # recover mems and cores tied up by containers of prior failed tests:
    for child in inner_containers_of(SUPER_ROOT):
        _release_container_nest(child)

def my_lock(lockname):
    # lockname is 'inner'
    lockdir = os.environ['AUTODIR']
    lockname = os.path.join(lockdir, '.cpuset.lock.' + lockname)
    lockfile = open(lockname, 'w')
    fcntl.flock(lockfile, fcntl.LOCK_EX)
    return lockfile

def my_unlock(lockfile):
    fcntl.flock(lockfile, fcntl.LOCK_UN)
    lockfile.close()

# Convert '1-3,7,9-12' to set(1,2,3,7,9,10,11,12)
def rangelist_to_set(rangelist):
    result = set()
    if not rangelist:
        return result
    for x in rangelist.split(','):
        if re.match(r'^(\d+)$', x):
            result.add(int(x))
            continue
        m = re.match(r'^(\d+)-(\d+)$', x)
        if m:
            start = int(m.group(1))
            end = int(m.group(2))
            result.update(set(range(start, end + 1)))
            continue
        msg = 'Cannot understand data input: %s %s' % (x, rangelist)
        raise ValueError(msg)
    return result

def my_container_name():
    # Get current process's inherited or self-built container name
    #   within /dev/cpuset or /dev/cgroup.  Is '' for root container.
    name = utils.read_one_line('/proc/%i/cpuset' % os.getpid())
    return name[1:]   # strip leading /

def get_mem_nodes(container_name):
    # all mem nodes now available to a container, both exclusive & shared
    file_name = mems_path(container_name)
    if os.path.exists(file_name):
        return rangelist_to_set(utils.read_one_line(file_name))
    else:
        return set()

def _busy_mem_nodes(parent_container):
    # Get set of numa memory nodes now used (exclusively or shared)
    #   by existing children of parent container
    busy = set()
    mem_files_pattern = os.path.join(full_path(parent_container),
                                     '*', cpuset_prefix + 'mems')
    for mem_file in glob.glob(mem_files_pattern):
        child_container = os.path.dirname(mem_file)
        busy |= get_mem_nodes(child_container)
    return busy

def available_exclusive_mem_nodes(parent_container):
    # Get subset of numa memory nodes of parent container which could
    #  be allocated exclusively to new child containers.
    # This excludes nodes now allocated to existing children.
    need_fake_numa()
    available = get_mem_nodes(parent_container)
    available -= _busy_mem_nodes(parent_container)
    return available

def my_mem_nodes():
    # Get set of numa memory nodes owned by current process's container.
    discover_container_style()
    if not mem_isolation_on:
        return set()    # as expected by vmstress
    return get_mem_nodes(my_container_name())

def my_available_exclusive_mem_nodes():
    # Get subset of numa memory nodes owned by current process's
    # container, which could be allocated exclusively to new child
    # containers.  This excludes any nodes now allocated
    # to existing children.
    return available_exclusive_mem_nodes(my_container_name())

def node_avail_kbytes(node):
    return node_mbytes << 10  # crude; fixed numa node size

def nodes_avail_mbytes(nodes):
    # nodes' combined user+avail size, in Mbytes
    return sum(node_avail_kbytes(n) for n in nodes) // 1024

def container_bytes(name):
    if fake_numa_containers:
        return nodes_avail_mbytes(get_mem_nodes(name)) << 20
    else:
        while True:
            file = memory_path(name) + '.limit_in_bytes'
            limit = int(utils.read_one_line(file))
            if limit < NO_LIMIT:
                return limit
            if name == SUPER_ROOT:
                return root_container_bytes
            name = os.path.dirname(name)

def container_mbytes(name):
    return container_bytes(name) >> 20

def mbytes_per_mem_node():
    # Get mbyte size of standard numa mem node, as float
    #  (some nodes are bigger than this)
    # Replaces utils_memory.node_size().
    numa = get_boot_numa()
    if numa.endswith('M'):
        return float(numa[:-1])  # mbyte size of fake nodes
    elif numa:
        nodecnt = int(numa)  # fake numa mem nodes for container isolation
    else:
        nodecnt = len(utils_memory.numa_nodes())  # phys mem-controller nodes
    # Use guessed total physical mem size, not kernel's
    #   lesser 'available memory' after various system tables.
    return utils_memory.rounded_memtotal() / (nodecnt * 1024.0)

def get_cpus(container_name):
    file_name = cpus_path(container_name)
    if os.path.exists(file_name):
        return rangelist_to_set(utils.read_one_line(file_name))
    else:
        return set()

def get_tasks(container_name):
    file_name = tasks_path(container_name)
    try:
        tasks = [x.rstrip() for x in open(file_name).readlines()]
    except IOError:
        if os.path.exists(file_name):
            raise
        tasks = []   # container doesn't exist anymore
    return tasks

def inner_containers_of(parent):
    pattern = os.path.join(full_path(parent), '*/tasks')
    return [unpath(os.path.dirname(task_file))
            for task_file in glob.glob(pattern)]

def _release_container_nest(nest):
    # Destroy a container, and any nested sub-containers
    nest_path = full_path(nest)
    if os.path.exists(nest_path):

        # bottom-up walk of tree, releasing all nested sub-containers
        for child in inner_containers_of(nest):
            _release_container_nest(child)

        logging.debug("releasing container %s", nest)

        # Transfer any survivor tasks (e.g. self) to parent container
        parent = os.path.dirname(nest)
        move_tasks_into_container(parent, get_tasks(nest))

        # remove the now-empty outermost container of this nest
        if os.path.exists(nest_path):
            os.rmdir(nest_path)  # nested, or dead manager

def release_container(container_name=None):
    # Destroy a container
    my_container = my_container_name()
    if container_name is None:
        container_name = my_container
    _release_container_nest(container_name)
    displaced = my_container_name()
    if displaced != my_container:
        logging.debug('now running self (pid %d) in container "%s"',
                      os.getpid(), displaced)

def remove_empty_prio_classes(prios):
    # remove prio classes whose set of allowed priorities is empty
    #    e.g  'no:3;rt:;be:3;id:'  -->  'no:3;be:3'
    return ';'.join(p for p in prios.split(';') if p.split(':')[1])

def all_drive_names():
    # list of all disk drives sda,sdb,...
    paths = glob.glob('/sys/block/sd*')
    if not paths:
        paths = glob.glob('/sys/block/hd*')
    return [os.path.basename(path) for path in paths]

def set_io_controls(container_name, disks=[], ioprio_classes=[PROPIO_NORMAL],
                    io_shares=[95], io_limits=[0]):
    # set the propio controls for one container, for selected disks
    # writing directly to /dev/cgroup/container_name/io.io_service_level
    #    without using containerd or container.py
    # See wiki ProportionalIOScheduler for definitions
    # ioprio_classes: list of service classes, one per disk
    #    using numeric propio service classes as used by kernel API, namely
    #       1: RT, Real Time, aka PROPIO_PRIO
    #       2: BE, Best Effort, aka PROPIO_NORMAL
    #       3: PROPIO_IDLE
    # io_shares: list of disk-time-fractions, one per disk,
    #       as percentage integer 0..100
    # io_limits: list of limit on/off, one per disk
    #       0: no limit, shares use of other containers' unused disk time
    #       1: limited, container's use of disk time is capped to given DTF
    # ioprio_classes defaults to best-effort
    # io_limit defaults to no limit, use slack time
    if not disks:  # defaults to all drives
        disks = all_drive_names()
        io_shares = [io_shares[0]] * len(disks)
        ioprio_classes = [ioprio_classes[0]] * len(disks)
        io_limits = [io_limits[0]] * len(disks)
    if not (len(disks) == len(ioprio_classes) and
            len(disks) == len(io_shares) and
            len(disks) == len(io_limits)):
        raise error.AutotestError('Unequal number of values for io controls')
    service_level = io_attr(container_name, 'io_service_level')
    if not os.path.exists(service_level):
        return  # kernel predates propio features
        # or io cgroup is mounted separately from cpusets
    disk_infos = []
    for disk, ioclass, limit, share in zip(disks, ioprio_classes,
                                           io_limits, io_shares):
        parts = (disk, str(ioclass), str(limit), str(share))
        disk_info = ' '.join(parts)
        utils.write_one_line(service_level, disk_info)
        disk_infos.append(disk_info)
    logging.debug('set_io_controls of %s to %s',
                  container_name, ', '.join(disk_infos))

def abbrev_list(vals):
    """Condense unsigned (0,4,5,6,7,10) to '0,4-7,10'."""
    ranges = []
    lower = 0
    upper = -2
    for val in sorted(vals) + [-1]:
        if val != upper + 1:
            if lower == upper:
                ranges.append(str(lower))
            elif lower <= upper:
                ranges.append('%d-%d' % (lower, upper))
            lower = val
        upper = val
    return ','.join(ranges)

def create_container_with_specific_mems_cpus(name, mems, cpus):
    need_fake_numa()
    os.mkdir(full_path(name))
    utils.write_one_line(cpuset_attr(name, 'mem_hardwall'), '1')
    utils.write_one_line(mems_path(name), ','.join(map(str, mems)))
    utils.write_one_line(cpus_path(name), ','.join(map(str, cpus)))
    logging.debug('container %s has %d cpus and %d nodes totalling %s bytes',
                  name, len(cpus), len(get_mem_nodes(name)),
                  utils.human_format(container_bytes(name)))

def create_container_via_memcg(name, parent, bytes, cpus):
    # create container via direct memcg cgroup writes
    os.mkdir(full_path(name))
    nodes = utils.read_one_line(mems_path(parent))
    utils.write_one_line(mems_path(name), nodes)  # inherit parent's nodes
    utils.write_one_line(memory_path(name) + '.limit_in_bytes', str(bytes))
    utils.write_one_line(cpus_path(name), ','.join(map(str, cpus)))
    logging.debug('Created container %s directly via memcg,'
                  ' has %d cpus and %s bytes',
                  name, len(cpus), utils.human_format(container_bytes(name)))

def _create_fake_numa_container_directly(name, parent, mbytes, cpus):
    need_fake_numa()
    lockfile = my_lock('inner')   # serialize race between parallel tests
    try:
        # Pick specific mem nodes for new cpuset's exclusive use
        # For now, arbitrarily pick highest available node numbers
        needed_kbytes = mbytes * 1024
        nodes = sorted(list(available_exclusive_mem_nodes(parent)))
        kbytes = 0
        nodecnt = 0
        while kbytes < needed_kbytes and nodecnt < len(nodes):
            nodecnt += 1
            kbytes += node_avail_kbytes(nodes[-nodecnt])
        if kbytes < needed_kbytes:
            parent_mbytes = container_mbytes(parent)
            if mbytes > parent_mbytes:
                raise error.AutotestError(
                    "New container's %d Mbytes exceeds "
                    "parent container's %d Mbyte size"
                    % (mbytes, parent_mbytes))
            else:
                raise error.AutotestError(
                    "Existing sibling containers hold "
                    "%d Mbytes needed by new container"
                    % ((needed_kbytes - kbytes) // 1024))
        mems = nodes[-nodecnt:]

        create_container_with_specific_mems_cpus(name, mems, cpus)
    finally:
        my_unlock(lockfile)

def create_container_directly(name, mbytes, cpus):
    parent = os.path.dirname(name)
    if fake_numa_containers:
        _create_fake_numa_container_directly(name, parent, mbytes, cpus)
    else:
        create_container_via_memcg(name, parent, mbytes << 20, cpus)

def create_container_with_mbytes_and_specific_cpus(name, mbytes,
                                                   cpus=None, root=SUPER_ROOT, io={}, move_in=True, timeout=0):
    """
    Create a cpuset container and move job's current pid into it
    Allocate the list "cpus" of cpus to that container

    :param name: arbitrary string tag
    :param mbytes: reqested memory for job in megabytes
    :param cpus (None): list of cpu indices to associate with the cpuset defaults to all cpus avail with given root
    :param root: the parent cpuset to nest this new set within, '' unnested top-level container
    :param io: arguments for proportional IO containers
    :param move_in (True): Move current process into the new container now.
    :param timeout (must be 0): persist until explicitly deleted.
    """
    need_mem_containers()
    if not container_exists(root):
        raise error.AutotestError('Parent container "%s" does not exist'
                                  % root)
    if cpus is None:
        # default to biggest container we can make under root
        cpus = get_cpus(root)
    else:
        cpus = set(cpus)  # interface uses list
    if not cpus:
        raise error.AutotestError('Creating container with no cpus')
    name = os.path.join(root, name)  # path relative to super_root
    if os.path.exists(full_path(name)):
        raise error.AutotestError('Container %s already exists' % name)
    create_container_directly(name, mbytes, cpus)
    set_io_controls(name, **io)
    if move_in:
        move_self_into_container(name)
    return name

def get_boot_numa():
    # get boot-time numa=fake=xyz option for current boot
    #   eg  numa=fake=nnn,  numa=fake=nnnM, or nothing
    label = 'numa=fake='
    for arg in utils.read_one_line('/proc/cmdline').split():
        if arg.startswith(label):
            return arg[len(label):]
    return ''


# -*- coding: utf-8 -*-

import time
from datetime import date, datetime, timedelta
import logging
import difflib
from openerp import models, fields, api, _
from openerp.osv import fields, osv
from openerp import models, fields
import re
import codecs
from openerp.tools.translate import _
from openerp.exceptions import except_orm, Warning, RedirectWarning
import math
from openerp.tools import float_compare
import openerp.addons.decimal_precision as dp
import time
from datetime import date, datetime, timedelta
_logger = logging.getLogger(__name__)

class Presupuesto(models.Model):
	_name = 'presupuesto.tools'
	_description = 'Reusable Methods'

	def get_saldo(self, presupuesto_move_id):

		saldo_total = 0
		move_val = 0
		saldo_rel = 0

		if presupuesto_move_id:

			saldo_total = presupuesto_move_id.amount_total

			presupuesto_moverubros_pool = self.env['presupuesto.moverubros']

			presupuesto_moverubros_ids = presupuesto_moverubros_pool.search([('move_id', '=', presupuesto_move_id.id),
										('move_id.state', '=', 'confirm'),
										('move_id.fiscal_year', '=', presupuesto_move_id.fiscal_year.id),
										])

			presupuesto_moverubros_relacionados_ids = presupuesto_moverubros_pool.search([('move_rel_id', '=', presupuesto_move_id.id),
							('move_id.state', '=', 'confirm'),
							('move_id.fiscal_year', '=', presupuesto_move_id.fiscal_year.id)])

			if presupuesto_moverubros_relacionados_ids:

				for x in presupuesto_moverubros_relacionados_ids:
					move_val = move_val + x.ammount

			if presupuesto_moverubros_ids:

				for x in presupuesto_moverubros_ids:
					saldo_rel = saldo_rel + x.ammount

		return (saldo_rel - move_val) if saldo_rel else move_val

	def get_saldo_obligaciones(self, presupuesto_move_id):

		saldo_total = 0
		move_val = 0
		total = 0

		if presupuesto_move_id:

			presupuesto_moverubros_pool = self.env['presupuesto.moverubros']

			presupuesto_move_ids = presupuesto_moverubros_pool.search([('move_rel_id', '=', presupuesto_move_id.id),
							('move_id.state', '=', 'confirm'),
							('move_id.fiscal_year', '=', presupuesto_move_id.fiscal_year.id)])

			saldo_total = presupuesto_move_id.amount_total
			total = saldo_total
			if presupuesto_move_ids:

				for x in presupuesto_move_ids:

					move_val = move_val + x.ammount

				total = saldo_total - move_val if move_val else saldo_total

			presupuesto_move_id.write({'saldo_sin_utilizar': total})
			_logger.info(total)
			return total

	"""
		metodo que nos sirve para saber si en los rubros que tiene el cdp, compromiso
		o obligacion el monto de los gastos es igual.

		recibe los ids de los rubros.

		retorna la diferencia entre el presupuesto y los gastos
	"""

	def _get_diff_money(self, ids):

		if ids:
			saldo_move = 0
			ammount = 0
			for data in ids:
				saldo_move += data.saldo_move
				ammount += data.ammount
			_logger.info(saldo_move)
			_logger.info(ammount)
			return saldo_move - ammount

	def update_old_values(self):

		presupuesto_move_pool = self.env['presupuesto.move']
		presupuesto_moverel_pool = self.env['presupuesto.moverubros']
		presupuesto_move_pool_ids = []

		sql = """
			SELECT id AS id
			FROM presupuesto_move
			WHERE move_rel IS NOT NULL
		"""

		self.env.cr.execute( sql )
		res = self.env.cr.dictfetchall(  )

		if res:

			for x in res:

				move_id = presupuesto_move_pool.browse(x.get('id'))

				if move_id:

					values = {

						'presupuesto_rel_move': [(6, 0, [move_id.move_rel.id])]

					}

					move_id.write(values)

					presupuesto_moverel_id = presupuesto_moverel_pool.search([('move_id', '=', x.get('id'))])

					if presupuesto_moverel_id:

						for z in presupuesto_moverel_id:

							z.write({'move_rel_id': move_id.move_rel.id})

	def uptdate_old_values_account_invoice(self):

		sql = """
			SELECT id AS id
			FROM account_invoice
			WHERE rp IS NOT NULL
		"""

		self.env.cr.execute( sql )
		res = self.env.cr.dictfetchall(  )

		account_invoice_pool = self.env['account.invoice']

		if res:

			for x in res:

				account_invoice_id = account_invoice_pool.browse(x.get('id'))

				if account_invoice_id:

					values = {

						'rp_move_rel': [(6, 0, [account_invoice_id.rp.id])]

					}

					account_invoice_id.write(values)

	def uptdate_old_values_account_voucher(self):

		sql = """
			SELECT id AS id
			FROM account_voucher
			WHERE obl IS NOT NULL
		"""

		self.env.cr.execute( sql )
		res = self.env.cr.dictfetchall(  )

		account_voucher_pool = self.env['account.voucher']

		if res:

			for x in res:

				account_voucher_id = account_voucher_pool.browse(x.get('id'))

				if account_voucher_id:

					values = {

						'obl_move_rel': [(6, 0, [account_voucher_id.obl.id])]

					}

					account_voucher_id.write(values)

	def uptdate_old_values_contact(self):

		sql = """
			SELECT id AS id
			FROM hr_contract
			WHERE cdp IS NOT NULL
		"""

		self.env.cr.execute( sql )
		res = self.env.cr.dictfetchall(  )

		hr_contract_pool = self.env['hr.contract']

		if res:

			for x in res:

				hr_contract_id = hr_contract_pool.browse(x.get('id'))

				if hr_contract_id:

					values = {

						'cdp_move_rel': [(6, 0, [hr_contract_id.cdp.id])]

					}

					hr_contract_id.write(values)

	def uptdate_old_values_payslip(self):

		sql = """
			SELECT id AS id
			FROM hr_payslip
			WHERE obl IS NOT NULL
		"""

		self.env.cr.execute( sql )
		res = self.env.cr.dictfetchall(  )

		hr_payslip_pool = self.env['hr.payslip']

		if res:

			for x in res:

				hr_payslip_id = hr_payslip_pool.browse(x.get('id'))

				if hr_payslip_id:

					values = {

						'obl_move_rel': [(6, 0, [hr_payslip_id.obl.id])]

					}

					hr_payslip_id.write(values)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Nov  5 10:17:24 2020

@author: akimlavrinenko
"""
import time
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import pandas as pd
#data[i,j] - i for row, j for column

start_time = time.time()
# path = '/Users/akimlavrinenko/Documents/coding/data/room_data/thatcher_deposition/'
path = './'
case_name = 'part_refined'
file_ext = 'stuck' #extension

#data proccesing
dataOrig = np.genfromtxt(path + case_name + '.' + file_ext, skip_header = 0, invalid_raise = False)

A = np.unique(dataOrig[:,2])
aList = []
for i in A:
    bList = []
    for j in range(len(dataOrig)):
        if i == dataOrig[j,2]:
            bList.append(dataOrig[j,:])
        bNp = np.asarray(bList)
    aList.append(bNp)

depositedDict = {}

# for data in aList:
for data in range(0,5):
    deposited = {}
    data = aList[data]
    num_rows, num_cols = data.shape
    hot_wall = [data[i,:] for i in range (0, num_rows) if data[i,3] == -0.5]
    cold_wall = [data[i,:] for i in range (0, num_rows) if data[i,3] == 0.5]
    cold_ceiling = [data[i,:] for i in range (0, num_rows) if data[i,4] == 0.5]
    hot_floor = [data[i,:] for i in range (0, num_rows) if data[i,4] == -0.5]
    adiabatic_front = [data[i,:] for i in range (0, num_rows) if data[i,5] == 0.5]
    adiabatic_back = [data[i,:] for i in range (0, num_rows) if data[i,5] == -0.5]

    hot_wall = np.array(hot_wall)
    cold_wall = np.array(cold_wall)
    cold_ceiling = np.array(cold_ceiling)
    hot_floor = np.array(hot_floor)
    adiabatic_front = np.array(adiabatic_front)
    adiabatic_back = np.array(adiabatic_back)

    # deposited.append(str(len(data)))
    deposited['sum'] = len(data)
    deposited['hot_wall'] = len(hot_wall)
    deposited['cold_wall'] = len(cold_wall)
    deposited['cold_ceiling'] = len(cold_ceiling)
    deposited['hot_floor'] = len(hot_floor)
    deposited['adiabatic_front'] = len(adiabatic_front)
    deposited['adiabatic_back'] = len(adiabatic_back)

    depositedDict[str(np.round((data[0,2] * 1.22 * 1e6), 2))] = deposited

    np.savetxt('hw_deposited_' + str(np.round((data[0,2] * 1.22 * 1e6), 2)) + '.dat', hot_wall)
    np.savetxt('cw_deposited_' + str(np.round((data[0,2] * 1.22 * 1e6), 2)) + '.dat', cold_wall)
    np.savetxt('cc_deposited_' + str(np.round((data[0,2] * 1.22 * 1e6), 2)) + '.dat', cold_ceiling)
    np.savetxt('hf_deposited_' + str(np.round((data[0,2] * 1.22 * 1e6), 2)) + '.dat', hot_floor)
    np.savetxt('af_deposited_' + str(np.round((data[0,2] * 1.22 * 1e6), 2)) + '.dat', adiabatic_front)
    np.savetxt('ab_deposited_' + str(np.round((data[0,2] * 1.22 * 1e6), 2)) + '.dat', adiabatic_back)

    # fig, axs = plt.subplots(6, figsize=(5,25))
    # fontP = FontProperties()
    # fontP.set_size('xx-small')
    # # fig.suptitle('Total amount of particles are ' + str(len(data)), fontsize=16)
    # if len(hot_wall) > 0:
    #     axs[0].plot(hot_wall[:,4],hot_wall[:,5],'ro')
    #     axs[0].grid(True)
    #     axs[0].set_xlabel('Z')
    #     axs[0].set_ylabel('Y')
    #     axs[0].set_title('hot wall, ' + str(len(hot_wall)) + ' particles')
    #     axs[0].set_xlim(-0.5,0.5)
    #     axs[0].set_ylim(-0.5,0.5)
    # else:
    #     print('hot wall is empty')

    # if len(cold_wall) > 0:
    #     axs[1].plot(cold_wall[:,4],cold_wall[:,5],'bo')
    #     axs[1].grid(True)
    #     axs[1].set_xlabel('Z')
    #     axs[1].set_ylabel('Y')
    #     axs[1].set_title('cold wall, ' + str(len(cold_wall)) + ' particles')
    #     axs[1].set_xlim(-0.5,0.5)
    #     axs[1].set_ylim(-0.5,0.5)
    # else:
    #     print('cold wall is empty')

    # if len(cold_ceiling) > 0:
    #     axs[2].plot(cold_ceiling[:,3],cold_ceiling[:,5],'bo')
    #     axs[2].grid(True)
    #     axs[2].set_xlabel('Z')
    #     axs[2].set_ylabel('X')
    #     axs[2].set_title('cold ceiling, '  + str(len(cold_ceiling)) + ' particles')
    #     axs[2].set_xlim(-0.5,0.5)
    #     axs[2].set_ylim(-0.5,0.5)
    # else:
    #     print('cold ceiling is empty')

    # if len(hot_floor) > 0:
    #     axs[3].plot(hot_floor[:,3],hot_floor[:,5],'ro', markersize=3)
    #     axs[3].grid(True)
    #     axs[3].set_xlabel('Z')
    #     axs[3].set_ylabel('X')
    #     axs[3].set_title('hot floor, '  + str(len(hot_floor)) + ' particles')
    #     axs[3].set_xlim(-0.5,0.5)
    #     axs[3].set_ylim(-0.5,0.5)
    # else:
    #     print('hot floor is empty')

    # if len(adiabatic_front) > 0:
    #     axs[4].plot(adiabatic_front[:,3],adiabatic_front[:,4],'o')
    #     axs[4].grid(True)
    #     axs[4].set_xlabel('X')
    #     axs[4].set_ylabel('Y')
    #     axs[4].set_title('adiabatic front, '  + str(len(adiabatic_front)) + ' particles')
    #     axs[4].set_xlim(-0.5,0.5)
    #     axs[4].set_ylim(-0.5,0.5)
    # else:
    #     print('adiabatic front wall is empty')

    # if len(adiabatic_back) > 0:
    #     axs[5].plot(adiabatic_back[:,3],adiabatic_back[:,4],'o')
    #     axs[5].grid(True)
    #     axs[5].set_xlabel('X')
    #     axs[5].set_ylabel('Y')
    #     axs[5].set_title('adiabatic back, '  + str(len(adiabatic_back)) + ' particles')
    #     axs[5].set_xlim(-0.5,0.5)
    #     axs[5].set_ylim(-0.5,0.5)
    # else:
    #     print('adiabatic back wall is empty')

    text = 'Total amount of particles are ' + str(len(data))

    size = 'Diameter ' + str(np.round((data[0,2] * 1.22 * 1e6), 2))
    print(size, str(len(data)))
    # fig.tight_layout()
    # plt.text(0, 6.47 , size, horizontalalignment='center',
             # verticalalignment='center')
    # plt.text(0, -0.615, text, horizontalalignment='center',
             # verticalalignment='center')

    # plt.savefig('stuck_particles_' + str(np.round((data[0,2] * 1.22 * 1e6), 2)) + '.png', dpi=150)
    # plt.show()

start_time2 = time.time() - start_time
print("It was: %.5f seconds" % (start_time2))

# plt.hist(depositedDict['0.1'])
#
# cold_c = depositedDict['0.5']['cold_ceiling'] / depositedDict['0.5']['sum']
# cold_w = depositedDict['0.5']['cold_wall'] / depositedDict['0.5']['sum']


#!/usr/bin/python

# (C) 2016 Tyler William H Backman
# Purpose: find PAINS in input compounds

import argparse
from rdkit import Chem
import sys
import string
from rdkit.Chem import FilterCatalog

parser = \
    argparse.ArgumentParser(description='find pains')
parser.add_argument('-i', '--infile', help='input sdf',
    required=True)
parser.add_argument('-o', '--outfile', help='output file',
    required=True)
parser.add_argument('-u', '--unparseable', help='output file for unparsable ids',
    required=True)
args = vars(parser.parse_args())
infile = args['infile']
outfile = args['outfile']
ufile = args['unparseable']

suppl = Chem.SDMolSupplier(infile)

params = FilterCatalog.FilterCatalogParams()
params.AddCatalog(params.FilterCatalogs.PAINS_A)
params.AddCatalog(params.FilterCatalogs.PAINS_B)
params.AddCatalog(params.FilterCatalogs.PAINS_C)
catalog = FilterCatalog.FilterCatalog(params)

f = open(outfile, 'w')
u = open(ufile, 'w')
count = 0
for mol in suppl:
    count += 1
    if mol is None:
        u.write(str(count) + "\n")
        continue
    try:
        if catalog.HasMatch(mol):
            f.write(mol.GetProp("_Name") + "\n")
    except:
        print("error in compound " + mol.GetProp("_Name") + "\n")

f.close()
u.close()


class Solution:
    def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
        graph = {i: [] for i in range(numCourses)}
        in_degrees = {i: 0 for i in range(numCourses)}

        for prerequisite in prerequisites:
            src, dst = prerequisite[1], prerequisite[0]
            graph[src].append(dst)
            in_degrees[dst] += 1

        sources = collections.deque()

        for key in in_degrees:
            if in_degrees[key] == 0:
                sources.append(key)

        course_ordering = []
        while sources:
            vertex = sources.popleft()
            course_ordering.append(vertex)

            for child in graph[vertex]:
                in_degrees[child] -= 1
                if in_degrees[child] == 0:
                    sources.append(child)

        if len(course_ordering) != numCourses:
            return []

        return course_ordering


"""
B - Digital Gifts
https://atcoder.jp/contests/abc119/tasks/abc119_b
高橋くんは N 人の親戚からお年玉をもらいました。
N 個の値 x1,x2,...,xN と N 個の文字列 u1,u2,...,uN が入力されます。各文字列 ui は JPY または BTC であり、xi と ui が i 人目の親戚からのお年玉の内容を表します。
例えば、x1= 10000, u1= JPY であれば 1 人目の親戚からのお年玉は 10000 円であり、x2= 0.10000000, u2= BTC であれば 2 人目の親戚からのお年玉は 0.1 ビットコインです。
ビットコインを 1.0 BTC あたり 380000.0 円として換算すると、高橋くんがもらったお年玉は合計で何円に相当するでしょうか？
"""
"""
N = int(input())
rate = 380000.0
ans = 0.0
for i in range(N):
    x, u = input().split()
    x = float(x)
    ans += x * rate if u == 'BTC' else x
print('{:.11f}'.format(ans))
"""
N = int(input())
ans = 0.0
for _ in range(N):
    x, u = input().split()
    if u == 'JPY':
        ans += float(x)
    elif u == 'BTC':
        ans += float(x) * 380000.0
print(ans)


import pydrake.symbolic as sym
import numpy as np
from pypolycontain.lib.zonotope import *
# from pypolycontain.lib.zonotope import *
from pypolycontain.lib.operations import convex_hull_of_point_and_polytope

def extract_variable_value_from_env(symbolic_var, env):
    # symbolic_var is a vector
    var_value = np.zeros(symbolic_var.shape[0])
    for i in range(symbolic_var.shape[0]):
        var_value[i] = env[symbolic_var[i]]
    return var_value

class Dynamics:
    def __init__(self):
        self.type='undefined'
        pass

    def construct_linearized_system_at(self, env):
        raise NotImplementedError

class ContinuousLinearDynamics(Dynamics):
    def __init__(self, A, B, c):
        Dynamics.__init__(self)
        self.A = A
        self.B = B
        self.c = c
        self.type='continuous'

    def construct_linearized_system_at(self, env):
        print('Warning: system is already linear!')
        return self

    def evaluate_xdot(self, x, u):
        return np.dot(self.A,x)+np.dot(self.B,u)+self.c

class ContinuousDynamics(Dynamics):
    '''
    System described by xdot(t) = f(x(t), u(t))
    '''
    def __init__(self, f, x, u):
        Dynamics.__init__(self)
        self.f = f
        self.x = x
        self.u = u
        self.type='continuous'
        self._linearlize_system()

    def _linearlize_system(self):
        self.A = sym.Jacobian(self.f, self.x)
        self.B = sym.Jacobian(self.f, self.u)
        self.c = -(np.dot(self.A, self.x)+np.dot(self.B, self.u))+self.f

    def construct_linearized_system_at(self, env):
        return ContinuousLinearDynamics(sym.Evaluate(self.A, env), sym.Evaluate(self.B, env), sym.Evaluate(self.c, env))

    def evaluate_xdot(self, env, linearize):
        if linearize:
            linsys = self.construct_linearized_system_at(env)
            x_env = extract_variable_value_from_env(self.x, env)
            u_env = extract_variable_value_from_env(self.u, env)
            return linsys.evaluate_xdot(x_env, u_env)
        else:
            return sym.Evaluate(self.f, env)

class DiscreteLinearDynamics(Dynamics):
    def __init__(self, A, B, c):
        Dynamics.__init__(self)
        self.A = A
        self.B = B
        self.c = c
        self.type='discrete'

    def construct_linearized_system_at(self, env):
        print('Warning: system is already linear!')
        return self

    def evaluate_x_next(self, x, u):
        return np.dot(self.A,x)+np.dot(self.B,u)+self.c

class DiscreteDynamics(Dynamics):
    '''
    System described by x[t+1] = f(x[t], u[t])
    '''
    def __init__(self, f, x, u):
        Dynamics.__init__(self)
        self.f = f
        self.x = x
        self.u = u
        self.type='discrete'
        self._linearlize_system()

    def _linearlize_system(self):
        self.A = sym.Jacobian(self.f, self.x)
        self.B = sym.Jacobian(self.f, self.u)
        self.c = -(np.dot(self.A, self.x)+np.dot(self.B, self.u))+self.f

    def construct_linearized_system_at(self, env):
        return DiscreteLinearDynamics(sym.Evaluate(self.A, env), sym.Evaluate(self.B, env), sym.Evaluate(self.c, env))

    def evaluate_x_next(self, env, linearize=False):
        if linearize:
            linsys = self.construct_linearized_system_at(env)
            x_env = extract_variable_value_from_env(self.x, env)
            u_env = extract_variable_value_from_env(self.u, env)
            return linsys.evaluate_x_next(x_env, u_env)
        return sym.Evaluate(self.f, env)

class DTContinuousSystem:
    def __init__(self, f, x, u, initial_env=None, input_limits = None):
        '''
        Continuous dynamical system x_dot = f(x,u)
        :param f: A symbolic expression of the system dynamics.
        :param x: A list of symbolic variables. States.
        :param u: A list of symbolic variable. Inputs.
        :param initial_env: A dictionary "environment" specifying the initial state of the system
        :param input_limits: Input limits of the system
        '''
        self.dynamics = ContinuousDynamics(f,x,u)
        if input_limits is None:
            self.input_limits = np.vstack([np.full(u.shape[0], -1e9),np.full(u.shape[0], 1e9)]) #stack add rows
        else:
            self.input_limits = input_limits
        self.u_bar = np.average(self.input_limits, axis=0)
        self.u_diff = np.diff(self.input_limits, axis=0)/2.
        if initial_env is None:
            self.env = {}
            for x_i in self.dynamics.x:
                self.env[x_i] = 0.
            for i,u_i in enumerate(self.dynamics.u):
                self.env[u_i]=self.u_bar[i]
        else:
            self.env = initial_env

    def forward_step(self, u=None, linearlize=False, modify_system=True, step_size = 1e-3, return_as_env = False, starting_state=None):
        #propagate state by one time step
        if starting_state is not None:
            new_env = self._state_to_env(starting_state, u)  #just fill in the particular state and control input in discretized system
        elif not modify_system:
            new_env = self.env.copy()
        else:
            new_env = self.env
        if u is not None:
            for i in range(u.shape[0]):
                #bound the control input and add to the system environment info
                new_env[self.dynamics.u[i]] = min(max(u[i],self.input_limits[0,i]),self.input_limits[1,i])
        else:
            for i in range(self.dynamics.u.shape[0]):
                new_env[self.dynamics.u[i]] = self.u_bar[i]
        delta_x = self.dynamics.evaluate_xdot(new_env, linearlize)*step_size
        #assign new xs
        for i in range(delta_x.shape[0]):
            new_env[self.dynamics.x[i]] += delta_x[i]
        if return_as_env:
            return new_env
        else:
            return extract_variable_value_from_env(self.dynamics.x, new_env)

    def get_reachable_polytopes(self, state, step_size = 1e-2, use_convex_hull=False):
        current_linsys = self.get_linearization(state, self.u_bar)
        #x_{k+1} = (A \Delta t+ I)x_k + B\Delta t u_bar + c \Delta
        x = np.ndarray.flatten(np.dot(current_linsys.A*step_size+np.eye(current_linsys.A.shape[0]),state))+\
            np.dot(current_linsys.B*step_size, self.u_bar)+np.ndarray.flatten(current_linsys.c*step_size)
        # print(f"symbolic system: x before :{x}, x shape: {x.shape}")
        x = np.atleast_2d(x).reshape(-1,1) #convert to column vector if a row vector
        # x = np.atleast_2d(x)
        # print(f"symbolic system: x after:{x}, state:{state} ")
        assert(len(x)==len(state))
        G_before = np.dot(current_linsys.B*step_size, np.diag(self.u_diff))
        # print(f"G before:{G_before}")
        # G = np.atleast_2d(np.dot(current_linsys.B*step_size, np.diag(self.u_diff)))
        G = np.atleast_2d(np.dot(current_linsys.B*step_size, np.diag(self.u_diff))).reshape(-1,1)
        if use_convex_hull:
            # print("in convex hull")
            # print(f"x:{x}")
            # print(f"G:{G}")
            return convex_hull_of_point_and_polytope(state.reshape(x.shape),zonotope(x,G))
        return to_AH_polytope(zonotope(x,G)) #this is a bad approximation

    def get_linearization(self, state=None, u_bar = None, mode=None):
        if state is None:
            return self.dynamics.construct_linearized_system_at(self.env)
        else:
            env = self._state_to_env(state, u_bar)
            return self.dynamics.construct_linearized_system_at(env)

    def _state_to_env(self, state, u=None):
        env = {}
        # print('state',state)
        for i, s_i in enumerate(state):
            env[self.dynamics.x[i]] = s_i  #i^th component of state
        if u is None:
            for i, u_i in enumerate(self.dynamics.u):
                env[u_i] = self.u_bar[i]
        else:
            for i, u_i in enumerate(u):
                env[self.dynamics.u[i]] = u[i] # i^th control input
        return env

    def get_current_state(self):
        return extract_variable_value_from_env(self.dynamics.x, self.env)

def in_mode(c_i, env):
    for c_ij in c_i:
        if c_ij.Evaluate(env) is False:
            return False
    return True

class DTHybridSystem:
    def __init__(self, f_list, f_type_list, x, u, c_list, initial_env=None, input_limits=None):
        '''
        Hybrid system with multiple dynamics modes
        :param f_list: numpy array of system dynamics modes
        :param x: pydrake symbolic variables representing the states
        :param u: pydrake symbolic variables representing the inputs
        :param c_list: numpy array of Pydrake symbolic expressions c_i(x,u) describing when the system belong in that mode.
                        c_i(x,u) is a vector of functions describing the mode. All of c_i(x,u) >= 0 when system is in mode i.
                        Modes should be complete and mutually exclusive.
        :param initial_env: A dictionary "environment" specifying the initial state of the system
        :param input_limits: Input limits of the system
        '''
        assert f_list.shape[0] == c_list.shape[0]
        self.mode_count = f_list.shape[0]
        self.x = x
        self.u = u
        dynamics_list = []
        for i, f in enumerate(f_list):
            if f_type_list[i] == 'continuous':
                dynamics_list.append(ContinuousDynamics(f,self.x,self.u))
            elif f_type_list[i] == 'discrete':
                dynamics_list.append(DiscreteDynamics(f, self.x, self.u))
            else:
                raise ValueError
        self.dynamics_list = np.asarray(dynamics_list)

        if input_limits is None:
            self.input_limits = np.vstack([np.full(u.shape[0], -1e9),np.full(u.shape[0], 1e9)])
        else:
            self.input_limits = input_limits
        self.u_bar = np.atleast_2d((self.input_limits[1,:]+self.input_limits[0,:])/2.)
        self.u_diff = np.atleast_2d((self.input_limits[1,:]-self.input_limits[0,:])/2.)
        if initial_env is None:
            self.env = {}
            for x_i in self.x:
                self.env[x_i] = 0.
            for i,u_i in enumerate(self.u):
                self.env[u_i]=self.u_bar[i]
        else:
            self.env = initial_env
        self.c_list = c_list
        # Check the mode the system is in
        self.current_mode = -1
        #TODO

    def do_internal_updates(self):
        pass

    def forward_step(self, u=None, linearlize=False, modify_system=True, step_size = 1e-3, return_as_env = False,
                     return_mode = False, starting_state=None):
        if starting_state is not None:
            new_env = self._state_to_env(starting_state, u)
        elif not modify_system:
            new_env = self.env.copy()
        else:
            new_env = self.env
        if u is not None:
            for i in range(u.shape[0]):
                new_env[self.u[i]] = min(max(u[i],self.input_limits[0,i]),self.input_limits[1,i])
        else:
            for i in range(self.u.shape[0]):
                #ensure u is scalar
                new_env[self.u[i]] = np.ndarray.flatten(np.atleast_1d(self.u_bar))[i]
        # Check for which mode the system is in
        delta_x = None
        x_new = None
        mode = -1
        for i, c_i in enumerate(self.c_list):
            is_in_mode = in_mode(c_i,new_env)
            if not is_in_mode:
                continue
            if self.dynamics_list[i].type == 'continuous':
                delta_x = self.dynamics_list[i].evaluate_xdot(new_env, linearlize)*step_size
            elif self.dynamics_list[i].type == 'discrete':
                x_new = self.dynamics_list[i].evaluate_x_next(new_env, linearlize)
            else:
                raise ValueError
            mode = i
            break
        assert(mode != -1) # The system should always be in one mode
        # print('mode', mode)
        # print(self.env)
        #FIXME: check if system is in 2 modes (illegal)

        #assign new xs
        if self.dynamics_list[mode].type=='continuous':
            for i in range(delta_x.shape[0]):
                new_env[self.x[i]] += delta_x[i]
        elif self.dynamics_list[mode].type=='discrete':
            for i in range(x_new.shape[0]):
                new_env[self.x[i]] = x_new[i]
        else:
            raise ValueError

        self.do_internal_updates()

        #return options
        if return_as_env and not return_mode:
            return new_env
        elif return_as_env and return_mode:
            return new_env, mode
        elif not return_as_env and not return_mode:
            return extract_variable_value_from_env(self.x, new_env)
        else:
            return extract_variable_value_from_env(self.x, new_env), mode

    def get_reachable_polytopes(self, state, step_size=1e-2, use_convex_hull=False):
        polytopes_list = []
        for mode, c_i in enumerate(self.c_list):
            # FIXME: better way to check if the mode is possible
            # Very naive check: if all-min and all-max input lead to not being in mode, assume state is not in mode
            lower_bound_env = self.env.copy()
            upper_bound_env = self.env.copy()
            unactuated_env = self.env.copy()
            for i, u_i in enumerate(self.u):
                lower_bound_env[u_i] = self.input_limits[0,i]
                upper_bound_env[u_i] = self.input_limits[1, i]
                unactuated_env[u_i] = 0
            for i, x_i in enumerate(self.x):
                lower_bound_env[x_i] = state[i]
                upper_bound_env[x_i] = state[i]
                unactuated_env[x_i] = state[i]

            if (not in_mode(c_i, lower_bound_env)) and (not in_mode(c_i, upper_bound_env)) and not in_mode(c_i, unactuated_env):
                # print('dropping mode %i' %mode)
                continue

            current_linsys = self.get_linearization(state, mode=mode)
            if current_linsys is None:
                # this should not happen?
                raise Exception
            u_bar = (self.input_limits[1, :] + self.input_limits[0, :]) / 2.
            u_diff = (self.input_limits[1, :] - self.input_limits[0, :]) / 2.
            # print(mode)
            #     print('A', current_linsys.A)
            # print('B', current_linsys.B)
            #     print('c', current_linsys.c)

            if self.dynamics_list[mode].type == 'continuous':
                x = np.ndarray.flatten(
                    np.dot(current_linsys.A * step_size + np.eye(current_linsys.A.shape[0]), state)) + \
                    np.dot(current_linsys.B * step_size, u_bar) + np.ndarray.flatten(current_linsys.c * step_size)
                x = np.atleast_2d(x).reshape(-1, 1)
                assert (len(x) == len(state))
                G = np.atleast_2d(np.dot(current_linsys.B * step_size, np.diag(u_diff)))

            elif self.dynamics_list[mode].type == 'discrete':
                x = np.ndarray.flatten(
                    np.dot(current_linsys.A, state)) + \
                    np.dot(current_linsys.B, u_bar) + np.ndarray.flatten(current_linsys.c)
                x = np.atleast_2d(x).reshape(-1, 1)
                assert (len(x) == len(state))
                G = np.atleast_2d(np.dot(current_linsys.B, np.diag(u_diff)))
                # print('x', x)
                # print('G', G)
            else:
                raise ValueError
            # if mode==1:
            #     print(G, x)
            if use_convex_hull:
                polytopes_list.append(convex_hull_of_point_and_polytope(state.reshape(x.shape), zonotope(x,G)))
            else:
                polytopes_list.append(to_AH_polytope(zonotope(x, G)))
        return np.asarray(polytopes_list)

    def get_linearization(self, state=None, u_bar = None, mode=None):
        if state is None:
            return self.dynamics_list[self.current_mode].construct_linearized_system_at(self.env)
        else:
            env = self._state_to_env(state, u_bar)
            if mode is not None:
                # FIXME: construct but don't ask questions?
                # assert in_mode(self.c_list[mode], env)
                return self.dynamics_list[mode].construct_linearized_system_at(env)
            for mode, c_i in enumerate(self.c_list):
                if in_mode(c_i, env):
                    return self.dynamics_list[mode].construct_linearized_system_at(env)
            print('Warning: state is not in any mode')
            return None

    def _state_to_env(self, state, u=None):
        env = {}
        # print('state',state)
        for i, s_i in enumerate(state):
            env[self.x[i]] = s_i
        if u is None:
            for i, u_i in enumerate(self.u):
                env[u_i] = self.u_bar[i]
        else:
            for i, u_i in enumerate(u):
                env[self.u[i]] = u[i]
        return env

    def get_current_state(self):
        return extract_variable_value_from_env(self.x, self.env)


#! /usr/bin/env python

import sys
import pysam

# input_file = sys.argv[1]

vcf_header = """\
##fileformat=VCFv4.3
##FILTER=<ID=PASS,Description="All filters passed">
##INFO=<ID=DP,Number=1,Type=Integer,Description="Total read number">
##INFO=<ID=AF,Number=1,Type=Float,Description="Allele Frequency">
##INFO=<ID=AD,Number=1,Type=Integer,Description="Variant read number">
##INFO=<ID=SB,Number=1,Type=Integer,Description="Strand bias (variant read number on the forward strand divided by total variant read number)">
##INFO=<ID=DPF,Number=1,Type=Integer,Description="Total read number on the forward strand">
##INFO=<ID=DPR,Number=1,Type=Integer,Description="Total read number on the reverse strand">
##INFO=<ID=ADF,Number=1,Type=Integer,Description="Variant read number on the forward strand">
##INFO=<ID=ADR,Number=1,Type=Integer,Description="Variant read number on the reverse strand">
##INFO=<ID=GENE,Number=1,Type=String,Description="Gene symbol of affected gene">
##INFO=<ID=MOTIF_POS,Number=1,Type=String,Description="Splicing motif detail">
##INFO=<ID=MOTIF_STRAND,Number=1,Type=String,Description="Strand of the affected gene">
##INFO=<ID=MOTIF_TYPE,Number=1,Type=String,Description="The type of affected splicing site (donor or acceptor)">
##INFO=<ID=SJ_WT,Number=1,Type=Integer,Description="The number of normally spliced reads with the reference allele">
##INFO=<ID=SJ_MT,Number=1,Type=Integer,Description="The number of normally spliced reads with the mutated allele">
##INFO=<ID=IR_WT,Number=1,Type=Integer,Description="The number of intron retained reads with the reference allele">
##INFO=<ID=IR_MT,Number=1,Type=Integer,Description="The number of intron retained reads with the mutated allele">
##INFO=<ID=IR_MT_MOH,Number=1,Type=Integer,Description="The maximum overhang size of intron retained reads with the mutated allele">\
"""

# gnomad_exome_db = pysam.Tabixfile("gnomad.exomes.r2.1.1.sites.vcf.bgz")
# gnomad_genome_db = pysam.Tabixfile("gnomad.genomes.r2.1.1.sites.vcf.bgz")
# exac_db = pysam.Tabixfile("https://storage.googleapis.com/gnomad-public/release/2.1.1/vcf/genomes/gnomad.genomes.r2.1.1.sites.vcf.bgz")

# def filter_irav(input_file, mut_file, output_file, gnomad_exome, gnomad_genome):
def filter_irav(input_file, mut_file, output_file):

    mutkey2info = {}
    with open(mut_file, 'r') as hin:
        for line in hin:
            F = line.rstrip('\n').split('\t')
            mutkey2info['\t'.join(F[:5])] = '\t'.join(F[5:])

    # if gnomad_exome is not None: gnomad_exome_db = pysam.Tabixfile(gnomad_exome)
    # if gnomad_genome is not None: gnomad_genome_db = pysam.Tabixfile(gnomad_genome)

    hout = open(output_file, 'w')
    print(vcf_header, file = hout)
    print('\t'.join(["#CHROM", "POS", "ID", "REF", "ALT", "QUAL", "FILTER", "INFO"]), file = hout)

    with open(input_file, 'r') as hin:
        header2ind = {}
        header = hin.readline().rstrip('\n').split('\t')
        for i, cname in enumerate(header):
            header2ind[cname] = i

        # header_line = '\t'.join(header)
        # header_line = header_line + '\t' + '\t'.join(["Variant_Ratio", "Depth", "Variant_Num", "Strand_Ratio", "Variant_Num_Detail"])

        # if gnomad_exome is not None: header_line = header_line + '\t' + "gnomAD_exome"
        # if gnomad_genome is not None: header_line = header_line + '\t' + "gnomAD_genome"

        # print(header_line, file = hout)
        # print '\t'.join(header) + '\t' + "gnomAD_exome" + '\t' + "gnomAD_genome"

        for line in hin:
            F = line.rstrip('\n').split('\t')
            sj_n = int(F[header2ind["Splice_Junction_Negative"]])
            sj_p = int(F[header2ind["Splice_Junction_Positive"]])
            ir_n = int(F[header2ind["Intron_Retention_Negative"]])
            ir_p = int(F[header2ind["Intron_Retention_Positive"]])
            ir_p_moh = int(F[header2ind["IR_Posivite_Max_Overhang"]])

            if ir_p < 3: continue
            if float(ir_p) / (ir_p + ir_n) < 0.9: continue
            # if float(ir_p) / (ir_p + sj_p) < 0.9: continue

            chr_mut = F[header2ind["Chr_Mut"]]
            start_mut = F[header2ind["Start_Mut"]]
            end_mut = F[header2ind["End_Mut"]]
            ref_mut = F[header2ind["Ref_Mut"]]
            alt_mut = F[header2ind["Alt_Mut"]]

            motif_info = F[header2ind["Chr_Motif"]] + ':' + F[header2ind["Start_Motif"]] + '-' + F[header2ind["End_Motif"]]

            mut_key = '\t'.join([chr_mut, start_mut, end_mut, ref_mut, alt_mut])
            original_ref, variant_ratio, depth, variant_num, strand_ratio, variant_num_info = mutkey2info[mut_key].split('\t')
            variant_num_info_f, variant_num_info_r = variant_num_info.split(';')
            depth_f, variant_num_f = variant_num_info_f.split(',')
            depth_r, variant_num_r = variant_num_info_r.split(',')

            if alt_mut == "-":
                pos = str(int(start_mut) - 1)
                ref = original_ref + ref_mut
                alt = original_ref
            elif ref_mut == "-":
                pos = start_mut
                ref = original_ref
                alt = original_ref + alt_mut
            else:
                pos = start_mut
                ref, alt = ref_mut, alt_mut
            irav_line = '\t'.join([chr_mut, pos, '.', ref, alt, '.', '.'])

            irav_line = irav_line + '\t' + "DP=" + depth
            irav_line = irav_line + ';' + "AF=" + variant_ratio
            irav_line = irav_line + ';' + "AD=" + variant_num
            irav_line = irav_line + ';' + "SB=" + strand_ratio
            irav_line = irav_line + ';' + "DPF=" + depth_f
            irav_line = irav_line + ';' + "DPR=" + depth_r
            irav_line = irav_line + ';' + "ADF=" + variant_num_f
            irav_line = irav_line + ';' + "ADR=" + variant_num_r
            irav_line = irav_line + ';' + "GENE=" + F[header2ind["Gene_Symbol"]]
            irav_line = irav_line + ';' + "MOTIF_POS=" + motif_info
            irav_line = irav_line + ';' + "MOTIF_STRAND=" + F[header2ind["Strand_Motif"]]
            irav_line = irav_line + ';' + "MOTIF_TYPE=" + F[header2ind["Type_Motif"]]
            irav_line = irav_line + ';' + "SJ_WT=" + str(sj_n)
            irav_line = irav_line + ';' + "SJ_MT=" + str(sj_p)
            irav_line = irav_line + ';' + "IR_WT=" + str(ir_n)
            irav_line = irav_line + ';' + "IR_MT=" + str(ir_p)
            irav_line = irav_line + ';' + "IR_MT_MOH=" + str(ir_p_moh)

            print(irav_line, file = hout)

    hout.close()
    # if gnomad_exome is not None: gnomad_exome_db.close()
    # if gnomad_genome is not None: gnomad_genome_db.close()


import argparse
import json
import logging

from torch.utils.data import Dataset
from torch.utils.data.dataset import T_co
from tqdm.auto import tqdm
from transformers import AutoTokenizer, AutoModelForMaskedLM, pipeline

from utils.file_io import read_lm_kbc_jsonl

logging.basicConfig(
    format="%(asctime)s - %(levelname)s - %(name)s -  %(message)s",
    datefmt="%m/%d/%Y %H:%M:%S",
    level=logging.INFO,
)
logger = logging.getLogger(__name__)

class PromptSet(Dataset):
    def __init__(self, prompts):
        self.prompts = prompts

    def __len__(self):
        return len(self.prompts)

    def __getitem__(self, index) -> T_co:
        return self.prompts[index]

def create_prompt(subject_entity: str, relation: str, mask_token: str) -> str:
    """
    Depending on the relation, we fix the prompt
    """

    prompt = mask_token

    if relation == "CountryBordersWithCountry":
        prompt = f"{subject_entity} shares border with {mask_token}."
    elif relation == "CountryOfficialLanguage":
        prompt = f"The official language of {subject_entity} is {mask_token}."
    elif relation == "StateSharesBorderState":
        prompt = f"{subject_entity} shares border with {mask_token} state."
    elif relation == "RiverBasinsCountry":
        prompt = f"{subject_entity} river basins in {mask_token}."
    elif relation == "ChemicalCompoundElement":
        prompt = f"{subject_entity} consists of {mask_token}, " \
                 f"which is an element."
    elif relation == "PersonLanguage":
        prompt = f"{subject_entity} speaks in {mask_token}."
    elif relation == "PersonProfession":
        prompt = f"{subject_entity} is a {mask_token} by profession."
    elif relation == "PersonInstrument":
        prompt = f"{subject_entity} plays {mask_token}, which is an instrument."
    elif relation == "PersonEmployer":
        prompt = f"{subject_entity} is an employer at {mask_token}, " \
                 f"which is a company."
    elif relation == "PersonPlaceOfDeath":
        prompt = f"{subject_entity} died at {mask_token}."
    elif relation == "PersonCauseOfDeath":
        prompt = f"{subject_entity} died due to {mask_token}."
    elif relation == "CompanyParentOrganization":
        prompt = f"The parent organization of {subject_entity} is {mask_token}."

    return prompt

def run(args):
    # Load the model
    model_type = args.model
    logger.info(f"Loading the model \"{model_type}\"...")

    tokenizer = AutoTokenizer.from_pretrained(model_type)
    model = AutoModelForMaskedLM.from_pretrained(model_type)

    pipe = pipeline(
        task="fill-mask",
        model=model,
        tokenizer=tokenizer,
        top_k=args.top_k,
        device=args.gpu
    )

    mask_token = tokenizer.mask_token

    # Load the input file
    logger.info(f"Loading the input file \"{args.input}\"...")
    input_rows = read_lm_kbc_jsonl(args.input)
    logger.info(f"Loaded {len(input_rows):,} rows.")

    # Create prompts
    logger.info(f"Creating prompts...")
    prompts = PromptSet([create_prompt(
        subject_entity=row["SubjectEntity"],
        relation=row["Relation"],
        mask_token=mask_token,
    ) for row in input_rows])

    # Run the model
    logger.info(f"Running the model...")
    outputs = []
    for out in tqdm(pipe(prompts, batch_size=8), total=len(prompts)):
        outputs.append(out)
    results = []
    for row, prompt, output in zip(input_rows, prompts, outputs):
        result = {
            "SubjectEntity": row["SubjectEntity"],
            "Relation": row["Relation"],
            "Prompt": prompt,
            "ObjectEntities": [
                seq["token_str"]
                for seq in output if seq["score"] > args.threshold],
        }
        results.append(result)

    # Save the results
    logger.info(f"Saving the results to \"{args.output}\"...")
    with open(args.output, "w") as f:
        for result in results:
            f.write(json.dumps(result) + "\n")

def main():
    parser = argparse.ArgumentParser(
        description="Probe a Language Model and "
                    "Run the Baseline Method on Prompt Outputs"
    )

    parser.add_argument(
        "-m",
        "--model",
        type=str,
        default="bert-large-cased",
        help="HuggingFace model name (default: bert-large-cased)",
    )
    parser.add_argument(
        "-i",
        "--input",
        type=str,
        default="data/dev.jsonl",
        required=True,
        help="Input test file (required)",
    )
    parser.add_argument(
        "-o",
        "--output",
        type=str,
        default="predictions/baseline.pred.jsonl",
        required=True,
        help="Output file (required)",
    )
    parser.add_argument(
        "-k",
        "--top_k",
        type=int,
        default=100,
        help="Top k prompt outputs (default: 100)",
    )
    parser.add_argument(
        "-t",
        "--threshold",
        type=float,
        default=0.5,
        help="Probability threshold (default: 0.5)",
    )
    parser.add_argument(
        "-g",
        "--gpu",
        type=int,
        default=-1,
        help="GPU ID, (default: -1, i.e., using CPU)"
    )

    args = parser.parse_args()

    run(args)

if __name__ == '__main__':
    main()


from flask import Flask,render_template,request

app=Flask(__name__)
app.config['SECRET_KEY'] = "cc7c83c25c81641609b40dbaf2038e12"

@app.route('/', methods=['GET', 'POST'])
def home():
    return render_template("index.html")

@app.route('/about', methods=['GET', 'POST'])
def about():
    return render_template("about.html")

@app.route('/help', methods=['GET', 'POST'])
def help():
    return render_template("help.html")

@app.route('/CheckExam', methods=['GET', 'POST'])
def CheckExam():
    examtime=0
    result=0
    seatlocation=0
    print(type(request.form.get("Examdatedept")))
    print(request.form.get("Examdateyr"))
    print(request.form.get("checkseatrollno"))
    print(request.form.get("checkseatdept"))
    print(request.form.get("checkseatyr"))

    if request.form.get("Examdatedept")=="BSCIT" or request.form.get("Examdatedept")=="BSCCS":
        if request.form.get("Examdateyr")=="first":
            examtime=[["10-apr","11.00 am-1.00 pm","Maths"],["11-apr","11.00 am-1.00 pm","Microprocessor"],["12-apr","11.00 am-1.00 pm","Graphics"],
                        ["14-apr","11.00 am-1.00 pm","OS"],["15-apr","11.00 am-1.00 pm","Network"],["16-apr","11.00 am-1.00 pm","OOPS"]]
        elif request.form.get("Examdateyr")=="second":
            examtime=[["10-apr","09.00 am-11.00 am","Java"],["11-apr","09.00 am-11.00 am","Python"],["12-apr","09.00 am-11.00 am","embedded"],
                        ["14-apr","09.00 am-11.00 am","Computing"],["15-apr","09.00 am-11.00 am","WebProj"],["16-apr","09.00 am-11.00 am","Network"]]
        elif request.form.get("Examdateyr")=="final":
            examtime=[["10-apr","02.00 pm-04.00 pm","IOT"],["11-apr","02.00 pm-04.00 pm","Cyberlaw"],["12-apr","02.00 pm-04.00 pm","NGT"],
                        ["14-apr","02.00 pm-04.00 pm","SIC"],["15-apr","02.00 pm-04.00 pm","GIS"],["16-apr","02.00 pm-04.00 pm","BI"]]
        else:
            examtime=0
        return render_template("exam.html",exam=1,seat=0,examdetails=examtime,location=seatlocation)

    seatingarr={"bscit":{"first":[{"roomno":"IT-10","rollno":[0,25]},{"roomno":"IT-11","rollno":[25,50]}],
                    "second":[{"roomno":"IT-20","rollno":[0,25]},{"roomno":"IT-21","rollno":[25,50]}],
                    "final":[{"roomno":"IT-30","rollno":[0,25]},{"roomno":"IT-31","rollno":[25,50]}]
                    },
                "bsccs":{"first":[{"roomno":"CS-10","rollno":[0,25]},{"roomno":"CS-11","rollno":[25,50]}],
                        "second":[{"roomno":"CS-20","rollno":[0,25]},{"roomno":"CS-21","rollno":[25,50]}],
                        "final":[{"roomno":"CS-30","rollno":[0,25]},{"roomno":"CS-31","rollno":[25,50]}]
                    }
                }

    if request.form.get("checkseatdept")=="BSCIT":
        if str(request.form.get("checkseatrollno"))!="None" and str(request.form.get("checkseatrollno"))!="":
            rollno=str(request.form.get("checkseatrollno"))
            if rollno[0:2]=="IT":
                result=seatingarr["bscit"]
            if rollno[0:2]=="CS":
                result=seatingarr["bsccs"]
            if rollno[2:4]=="01":
                result=result["first"]
            if rollno[2:4]=="02":
                result=result["second"]
            if rollno[2:4]=="03":
                result=result["final"]
            lastdigit=rollno[4:6]
            for norange in result:
                if int(lastdigit)<=norange["rollno"][1] and int(lastdigit)>norange["rollno"][0]:
                    seatlocation=norange["roomno"]
        return render_template("exam.html",exam=0,seat=1,location=seatlocation, examdetails=examtime)
    return render_template("exam.html",exam=0,seat=0,location=seatlocation,examdetails=examtime)

@app.route('/FillFees', methods=['GET', 'POST'])
def FillFees():
    return render_template("adminfees.html")

@app.route('/RegisterEvents', methods=['GET', 'POST'])
def RegisterEvents():
    return render_template("events.html")

if __name__=="__main__":
    app.run(host='0.0.0.0', port=8080, debug=True)


import re

from setuptools import setup, find_packages

with open('daiquiri_sync/__init__.py') as f:
    metadata = dict(re.findall(r'__(.*)__ = [\']([^\']*)[\']', f.read()))

setup(
    name='django-daiquiri-sync',
    version=metadata['version'],
    author=metadata['author'],
    author_email=metadata['email'],
    maintainer=metadata['author'],
    maintainer_email=metadata['email'],
    license=metadata['license'],
    url='https://github.com/aipescience/django-daiquiri-sync',
    description=u'Daiquiri Sync is a python library meant to be used with the Daiquiri Framework.',
    long_description=open('README.rst').read(),
    install_requires=[
        'ansible',
        'paramiko',
        'PyYAML'
    ],
    classifiers=[],
    packages=find_packages(),
    include_package_data=True
)


# encoding=utf-8
# author : lifuxin1125@gmail.com
# date: 2015-01-08
# version: 0.1
__author__ = 'lifuxin'

if __name__ == "__main__":

    #   output
    attPrefixFile = "../all_asc_tweetsOutput/attPrefix"
    attPrefieWriter = open(attPrefixFile,'w')

    attNo = 1
    while attNo < 20000:
        #@attribute Att_1 numeric
        attPrefieWriter.write("@attribute Att"+str(attNo)+" numeric\n")
        attNo = attNo + 1
    attPrefieWriter.flush(),attPrefieWriter.close()


import sys
def stop(x):
    if x == 0:
        return True
while True:
    M = int(sys.stdin.readline())
    if stop(M) == True:
        break
    sen = sys.stdin.readline().strip()
    check = list()
    left,right = 0 , 0
    cnt, size = 0 , 0

    while left <= right:
        if cnt < M or (cnt == M and sen[right] in check):
            if sen[right] not in check:
                cnt +=1
            check.append(sen[right])
            right += 1
        else:
            if check.count(check[0]) == 1:
                cnt -=1
            check.remove(check[0])
            left +=1

        size = max(right-left,size)
        if right == len(sen)-1:
            break
    print(size)


import tkinter
from tkinter import ttk

ventana = tkinter.Tk()

ventana.geometry('175x300')
ventana.title('Comida Favorita')

v = tkinter.StringVar(ventana, '1')

values = {
    'Pizza': 'Pizza',
    'Hamburguesa': 'Hamburguesa',
    'Papas Fritas': 'Papas Fritas',
    'Ensalada': 'Ensalada',
    'Sushi': 'Sushi',
    'Tacos': 'Tacos',
    'Otro': 'Otro'}

for (text, value) in values.items():
    tkinter.Radiobutton(ventana, text=text, variable=v, value=value).pack(side='top', ipady=5)

# RESET RADIO BUTTON
def reset():
    v.set('1')

tkinter.Button(ventana, text='Reiniciar', command=reset).pack(side='bottom', ipady=5)

ventana.mainloop()


import sys
import os
import time

from urllib.parse import urljoin
from datetime import timedelta
from tzlocal import get_localzone
from dateutil import parser as dateparser
from requests.exceptions import HTTPError

import lxml.html

from .admin import AdminInfo, AdminRedashInfo
from .queue import QueueEntry
from .logs import LogEntry
from .review import Review
from .session import AmoSession
from .validation import ValidationReport
from .utils import AMO_BASE, AMO_CONFIG, AMO_EDITOR_BASE, AMO_DEVELOPER_BASE, \
    AMO_TIMEZONE, VALIDATION_WAIT, UPLOAD_PLATFORM, csspath

class AddonsService:
    def __init__(self, login_prompter=None):
        self.session = AmoSession(self, login_prompter)

    def persist(self):
        self.session.persist()

    def get_review(self, id_or_url, unlisted=False):
        review = Review(self, id_or_url, unlisted)
        review.get()
        return review

    def get_admin_info(self, id_or_url, getall=True):
        admininfo = AdminInfo(self, id_or_url)
        if getall:
            admininfo.get_all_versions()
        else:
            admininfo.get()
        return admininfo

    @staticmethod
    def get_user_addons(user):
        api_key = AMO_CONFIG.get('auth', 'redash_key', fallback=None)
        if not api_key:
            raise Exception("A redash API key is required in the config (auth.redash_key)")

        redash = AdminRedashInfo(api_key)
        return redash.get_user_addons(user)

    def _unpaginate(self, url, func, params=None, limit=sys.maxsize):
        things = []
        lastlen = -1

        while url and len(things) < limit:
            req = self.session.get(url, stream=True, params=params)
            req.raw.decode_content = True
            doc = lxml.html.parse(req.raw).getroot()
            lastlen = len(things)
            nexturl = func(things, doc)
            thislen = len(things)

            if lastlen == thislen:
                break

            if thislen > limit:
                things = things[:limit]

            # Get the next url and make sure to unset parameters, since they
            # will be provided in the next url anyway.
            url = urljoin(AMO_EDITOR_BASE, nexturl) if nexturl else None
            params = None

        return things

    def get_queue(self, name_or_url):
        if name_or_url.startswith(AMO_BASE) or name_or_url.startswith(AMO_EDITOR_BASE):
            name = "/".join(name_or_url.split("/")[-2])
        else:
            name = name_or_url

        def page(queue, doc):
            queuerows = doc.xpath(csspath('#addon-queue > tbody > .addon-row'))
            for row in queuerows:
                queue.append(QueueEntry(self.session, row))

            nextlink = doc.xpath(csspath('.data-grid-top > .pagination > li > a[rel="next"]'))
            return nextlink[0].attrib['href'] if len(nextlink) > 0 else None

        url = '%s/%s' % (AMO_EDITOR_BASE, name)
        return self._unpaginate(url, page)

    def get_logs(self, loglist, start=None, end=None, query=None, limit=sys.maxsize):
        # pylint: disable=too-many-arguments
        payload = {
            'search': query
        }

        dtstart = None
        dtend = None
        localtz = get_localzone()

        # We need to offset the date a bit so the returned results are in the
        # user's local timezone. If specific times were passed then don't
        # expand the end date to the end of the day.
        if start:
            dtstart = localtz.localize(dateparser.parse(start))
            payload['start'] = dtstart.astimezone(AMO_TIMEZONE).strftime('%Y-%m-%d')

        if end:
            dtend = localtz.localize(dateparser.parse(end))
            if dtend.hour == 0 and dtend.minute == 0 and dtend.second == 0:
                dtend += timedelta(days=1)
            payload['end'] = dtend.astimezone(AMO_TIMEZONE).strftime('%Y-%m-%d')

        def page(logs, doc):
            logrows = doc.xpath(csspath('#log-listing > tbody > tr[data-addonid]'))
            for row in logrows:
                entry = LogEntry(self.session, row)
                if (not dtstart or entry.date >= dtstart) and \
                   (not dtend or entry.date <= dtend):
                    logs.append(entry)

            nextlink = doc.xpath(csspath('.pagination > li > a[rel="next"]'))
            return nextlink[0].attrib['href'] if len(nextlink) > 0 else None

        url = '%s/%s' % (AMO_EDITOR_BASE, loglist)
        return self._unpaginate(url, page, params=payload, limit=limit)

    def upload(self, addonid, xpi, platform='all'):
        if platform not in UPLOAD_PLATFORM:
            raise Exception("Unknown platform %s" % platform)

        uploadurl = None
        url = '%s/addon/%s/versions' % (AMO_DEVELOPER_BASE, addonid)
        req = self.session.get(url, stream=True)
        req.raw.decode_content = True
        doc = lxml.html.parse(req.raw).getroot()
        token = doc.xpath(csspath('form input[name="csrfmiddlewaretoken"]'))[0].attrib['value']

        with open(xpi, 'rb') as xpifd:
            payload = {
                'csrfmiddlewaretoken': (None, token),
                'upload': (os.path.basename(xpi), xpifd, 'application/x-xpinstall')
            }

            uploadurl = '%s/addon/%s/upload-listed' % (AMO_DEVELOPER_BASE, addonid)

            req = self.session.post(uploadurl, files=payload, allow_redirects=False, timeout=None)
            if req.status_code != 302:
                raise Exception('Could not upload %s' % xpi)
            uploadurl = req.headers['location']

        return self.wait_for_validation(addonid, uploadurl, platform)

    def wait_for_validation(self, addonid, uploadurl, platform='all', interval=VALIDATION_WAIT):
        if platform not in UPLOAD_PLATFORM:
            raise Exception("Unknown platform %s" % platform)

        report = None
        while not report or not report.completed:
            req = self.session.get(uploadurl, timeout=None)
            report = ValidationReport(addonid, req.json(), platform)

            if not report.success:
                return report
            elif not report.completed:
                print("Waiting for validation...")
                time.sleep(interval)

        return report

    def add_xpi_to_version(self, addonid, report, platform, source=None, beta=False):
        # pylint: disable=too-many-locals,too-many-branches,too-many-arguments
        sourcefd = None
        try:
            url = '%s/addon/%s/versions' % (AMO_DEVELOPER_BASE, addonid)
            req = self.session.get(url, stream=True)
            req.raw.decode_content = True
            doc = lxml.html.parse(req.raw).getroot()
            token = doc.xpath(csspath('form input[name="csrfmiddlewaretoken"]'))[0].attrib['value']

            ver_exists = doc.xpath(csspath(".item_wrapper a") +
                                   "[contains(text(), 'Version %s')]" % report.version)
            if len(ver_exists) > 0:
                final_version_url = urljoin(AMO_DEVELOPER_BASE, ver_exists[0].attrib['href'])
                add_version_url = final_version_url + "/submit-file/"
            else:
                final_version_url = None  # will fill this in later
                add_version_url = '%s/addon/%s/versions/submit/' % (AMO_DEVELOPER_BASE, addonid)

            payload = {
                'csrfmiddlewaretoken': (None, token),
                'upload': (None, report.reportid),
                'supported_platforms': (None, UPLOAD_PLATFORM[platform]),
            }

            if beta:
                payload['beta'] = (None, "on")

            if source:
                sourcefd = open(source, 'rb')
                payload['source'] = (os.path.basename(source), sourcefd, 'application/octet-stream')
            else:
                payload['source'] = ("", "", 'application/octet-stream')

            req = self.session.post(add_version_url, files=payload,
                                    allow_redirects=False, timeout=(60.0, 60.0))

            if not final_version_url:
                locparts = req.headers['location'].split("/")
                final_version_url = urljoin(AMO_DEVELOPER_BASE,
                                            "addon/%s/versions/%s" % (addonid, locparts[-2]))

            return final_version_url
        except HTTPError as e:
            if e.response.status_code != 400:
                raise e
            msg = None
            try:
                msg = "Error: %s" % " ".join(e.response.json()['__all__'])
            except (ValueError, KeyError):
                msg = ("Error: %s" % e.response.text)

            raise Exception(msg) from e
        finally:
            if sourcefd:
                sourcefd.close()


import logging
import numpy as np
import pandas as pd
from pathlib import Path
from src.data.read_dataset import get_data
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.compose import ColumnTransformer
from src.data.text_preprocessing import convert_labels

project_dir = Path(__file__).resolve().parents[2]

class BaselinePredict:
    """
    Provides a classic baseline for comparison
    """

    def __init__(self, model_name, data_source):
        self._data = data_source
        self._model = self.train(model_name)

    def my_predict(self):
        """Predict the binary class of a sentence using a Logistic Regression
        Args:
            Data (pd.DataFrame): DataFrame
        Returns:
            binary class (str): Apartment (class 0) | House (class 1)
        """
        # predict
        return self._model

    def train(self, model) -> str:
        """Train a logistic regression method"""
        #try:

        # Get data
        df_train, _ = get_data(self._data)

        # Removing label
        train = df_train.drop(["label"], axis=1)
        #train = train.drop(["ppl"], axis=1)

        # Numerical transformations
        numeric_transformer = Pipeline(steps=[
            ("imputer", SimpleImputer(strategy="median"))
        ])
        # Categorical transformations
        categorical_transformer = Pipeline(steps=[
            ("encoder", OneHotEncoder(handle_unknown="ignore"))
        ])
        # Text transformations
        text_transformer = Pipeline(steps=[
            ('vect', CountVectorizer())
            #('tfidf', TfidfTransformer())
        ])

        # Numerical features
        # numeric_features = train.select_dtypes(include=['int64', 'float64']).columns

        # Categorical features
        categorical_features = train.select_dtypes(include=['object']).columns

        # Convert in a column
        preprocessor = ColumnTransformer(
            transformers=[
                #('num', numeric_transformer, 'ppl'),
                ('tex', text_transformer, 'text'),
                ('cat', categorical_transformer, categorical_features)
            ])

        # Pipeline
        pipeline = Pipeline(steps=[
            ('preprocessor', preprocessor),
            ('lr', LogisticRegression(max_iter=1000, multi_class="multinomial")) #solver="sag",
        ])

        # labels
        label2int = convert_labels(df_train["label"])
        train_labels = df_train['label'].apply(lambda x: label2int[x])

        # fit
        pipeline.fit(train, train_labels)
        print(pipeline.feature_names_in_)

        return pipeline

        #except Exception:
        #    logging.error(f'directory or model is invalid or does not exist: {self._model}')


import argparse
import os
import pandas as pd
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from torch.nn.utils.rnn import pad_sequence
from torch.optim import Adam
from collections import Counter
import tarfile
import re

def preprocess_text(text):
    text = text.lower()
    text = re.sub(r"[^a-z0-9\s]", '', text)
    text = re.sub(r"\s+", ' ', text)
    return text.strip()

class JokesDataset(Dataset):
    def __init__(self, sequences, labels):
        self.sequences = sequences
        self.labels = labels

    def __len__(self):
        return len(self.sequences)

    def __getitem__(self, idx):
        return self.sequences[idx], self.labels[idx]

class JokeGeneratorModel(nn.Module):
    def __init__(self, vocab_size, embed_dim, hidden_dim):
        super().__init__()
        self.embedding = nn.Embedding(vocab_size, embed_dim)
        self.lstm = nn.LSTM(embed_dim, hidden_dim, batch_first=True)
        self.fc = nn.Linear(hidden_dim, vocab_size)

    def forward(self, x):
        embedded = self.embedding(x)
        lstm_out, _ = self.lstm(embedded)
        logits = self.fc(lstm_out)
        return logits

if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    parser.add_argument('--epochs', type=int, default=10)
    parser.add_argument('--batch-size', type=int, default=64)
    parser.add_argument('--vocabulary-size', type=int, default=5042)
    parser.add_argument('--data-dir', type=str,
                        default=os.environ.get('SM_CHANNEL_TRAIN', '.'))
    parser.add_argument('--model-dir', type=str,
                        default=os.environ.get('SM_MODEL_DIR', '.'))

    args = parser.parse_known_args()[0]

    df = pd.read_csv(os.path.join(args.data_dir, 'normalized_jokes.csv'))
    jokes = df['Normalized Joke'].apply(preprocess_text).tolist()

    token_counts = Counter(word for joke in jokes for word in joke.split())
    vocab = {word: i + 1 for i,
             (word, _) in enumerate(token_counts.most_common(args.vocabulary_size))}
    vocab['<PAD>'] = 0
    vocab['<END>'] = len(vocab)

    encoded_jokes = [[vocab[word] for word in joke.split(
    ) if word in vocab] + [vocab['<END>']] for joke in jokes]
    filtered_encoded_jokes = [seq for seq in encoded_jokes if len(seq) >= 3]

    if not filtered_encoded_jokes:
        raise ValueError(
            "All sequences are shorter than the minimum length after filtering.")

    sequences = pad_sequence([torch.tensor(seq[:-1]) for seq in filtered_encoded_jokes],
                             padding_value=vocab['<PAD>'], batch_first=True)
    labels = pad_sequence([torch.tensor(seq[1:]) for seq in filtered_encoded_jokes],
                          padding_value=vocab['<PAD>'], batch_first=True)

    dataset = JokesDataset(sequences, labels)
    train_loader = DataLoader(
        dataset, batch_size=args.batch_size, shuffle=True)

    model = JokeGeneratorModel(
        args.vocabulary_size, embed_dim=100, hidden_dim=128)
    criterion = nn.CrossEntropyLoss()
    optimizer = Adam(model.parameters())

    for epoch in range(args.epochs):
        for inputs, targets in train_loader:
            optimizer.zero_grad()
            outputs = model(inputs)
            loss = criterion(
                outputs.view(-1, args.vocabulary_size), targets.view(-1))
            loss.backward()
            optimizer.step()
        print(f'Epoch {epoch+1}/{args.epochs} Loss: {loss.item()}')

    model_save_path = os.path.join(args.model_dir, 'model.pth')
    archive_path = os.path.join(args.model_dir, 'model.tar.gz')

    torch.save(model.state_dict(), model_save_path)

    with tarfile.open(archive_path, mode='w:gz') as archive:
        archive.add(model_save_path, arcname='model.pth')


from discord.ext import commands
import discord
import asyncio
import youtube_dl
import logging
import math
from urllib import request
from ..video import Video

async def audio_playing(ctx):
    client = ctx.guild.voice_client
    if client and client.channel and client.source:
        return True
    else:
        raise commands.CommandError("Not currently playing any audio.")

async def in_voice_channel(ctx):
    voice = ctx.author.voice
    bot_voice = ctx.guild.voice_client
    if voice and bot_voice and voice.channel and bot_voice.channel and voice.channel == bot_voice.channel:
        return True
    else:
        raise commands.CommandError(
            "You need to be in the channel to do that.")

async def is_audio_requester(ctx):
    music = ctx.bot.get_cog("Music")
    state = music.get_state(ctx.guild)
    permissions = ctx.channel.permissions_for(ctx.author)
    if permissions.administrator or state.is_requester(ctx.author):
        return True
    else:
        raise commands.CommandError(
            "You need to be the song requester to do that.")

class Music(commands.Cog):

    def __init__(self, bot, config):
        self.bot = bot
        self.config = config[__name__.split(".")[
            -1]]
        self.states = {}
        self.bot.add_listener(self.on_reaction_add, "on_reaction_add")

    def get_state(self, guild):
        if guild.id in self.states:
            return self.states[guild.id]
        else:
            self.states[guild.id] = GuildState()
            return self.states[guild.id]

    @commands.command(aliases=["stop"])
    @commands.guild_only()
    @commands.has_permissions(administrator=True)
    async def leave(self, ctx):
        client = ctx.guild.voice_client
        state = self.get_state(ctx.guild)
        if client and client.channel:
            await client.disconnect()
            state.playlist = []
            state.now_playing = None
        else:
            raise commands.CommandError("Not in a voice channel.")

    @commands.command(aliases=["resume", "p"])
    @commands.guild_only()
    @commands.check(audio_playing)
    @commands.check(in_voice_channel)
    @commands.check(is_audio_requester)
    async def pause(self, ctx):
        client = ctx.guild.voice_client
        self._pause_audio(client)

    def _pause_audio(self, client):
        if client.is_paused():
            client.resume()
        else:
            client.pause()

    @commands.command(aliases=["vol", "v"])
    @commands.guild_only()
    @commands.check(audio_playing)
    @commands.check(in_voice_channel)
    @commands.check(is_audio_requester)
    async def volume(self, ctx, volume: int):
        state = self.get_state(ctx.guild)

        if volume < 0:
            volume = 0

        max_vol = self.config["max_volume"]
        if max_vol > -1:
            if volume > max_vol:
                volume = max_vol

        client = ctx.guild.voice_client

        state.volume = float(volume) / 100.0
        client.source.volume = state.volume

    @commands.command()
    @commands.guild_only()
    @commands.check(audio_playing)
    @commands.check(in_voice_channel)
    async def skip(self, ctx):
        state = self.get_state(ctx.guild)
        client = ctx.guild.voice_client
        if ctx.channel.permissions_for(
                ctx.author).administrator or state.is_requester(ctx.author):
            client.stop()
        elif self.config["vote_skip"]:
            channel = client.channel
            self._vote_skip(channel, ctx.author)
            users_in_channel = len([
                member for member in channel.members if not member.bot
            ])
            required_votes = math.ceil(
                self.config["vote_skip_ratio"] * users_in_channel)
            await ctx.send(
                f"{ctx.author.mention} voted to skip ({len(state.skip_votes)}/{required_votes} votes)"
            )
        else:
            raise commands.CommandError("Sorry, vote skipping is disabled.")

    def _vote_skip(self, channel, member):
        logging.info(f"{member.name} votes to skip")
        state = self.get_state(channel.guild)
        state.skip_votes.add(member)
        users_in_channel = len([
            member for member in channel.members if not member.bot
        ])
        if (float(len(state.skip_votes)) /
                users_in_channel) >= self.config["vote_skip_ratio"]:
            logging.info(f"Enough votes, skipping...")
            channel.guild.voice_client.stop()

    def _play_song(self, client, state, song):
        state.now_playing = song
        state.skip_votes = set()
        source = discord.PCMVolumeTransformer(
            discord.FFmpegPCMAudio(song.stream_url), volume=state.volume)

        def after_playing(err):
            if len(state.playlist) > 0:
                next_song = state.playlist.pop(0)
                self._play_song(client, state, next_song)
            else:
                asyncio.run_coroutine_threadsafe(client.disconnect(),
                                                 self.bot.loop)

        client.play(source, after=after_playing)

    @commands.command(aliases=["np"])
    @commands.guild_only()
    @commands.check(audio_playing)
    async def nowplaying(self, ctx):
        state = self.get_state(ctx.guild)
        message = await ctx.send("", embed=state.now_playing.get_embed())
        await self._add_reaction_controls(message)

    @commands.command(aliases=["q", "playlist"])
    @commands.guild_only()
    @commands.check(audio_playing)
    async def queue(self, ctx):
        state = self.get_state(ctx.guild)
        await ctx.send(self._queue_text(state.playlist))

    def _queue_text(self, queue):
        if len(queue) > 0:
            message = [f"{len(queue)} songs in queue:"]
            message += [
                f"  {index+1}. **{song.title}** (requested by **{song.requested_by.name}**)"
                for (index, song) in enumerate(queue)
            ]
            return "\n".join(message)
        else:
            return "The play queue is empty."

    @commands.command(aliases=["cq"])
    @commands.guild_only()
    @commands.check(audio_playing)
    @commands.has_permissions(administrator=True)
    async def clearqueue(self, ctx):
        state = self.get_state(ctx.guild)
        state.playlist = []

    @commands.command(aliases=["jq"])
    @commands.guild_only()
    @commands.check(audio_playing)
    @commands.has_permissions(administrator=True)
    async def jumpqueue(self, ctx, song: int, new_index: int):
        state = self.get_state(ctx.guild)
        if 1 <= song <= len(state.playlist) and 1 <= new_index:
            song = state.playlist.pop(song - 1)
            state.playlist.insert(new_index - 1, song)

            await ctx.send(self._queue_text(state.playlist))
        else:
            raise commands.CommandError("You must use a valid index.")

    @commands.command(brief="Plays audio from <url>.")
    @commands.guild_only()
    async def play(self, ctx, *, url):

        client = ctx.guild.voice_client
        state = self.get_state(ctx.guild)

        if client and client.channel:
            try:
                video = Video(url, ctx.author)
            except youtube_dl.DownloadError as e:
                logging.warn(f"Error downloading video: {e}")
                await ctx.send(
                    "There was an error downloading your video, sorry.")
                return
            state.playlist.append(video)
            message = await ctx.send(
                "Added to queue.", embed=video.get_embed())
            await self._add_reaction_controls(message)
        else:
            if ctx.author.voice is not None and ctx.author.voice.channel is not None:
                channel = ctx.author.voice.channel
                try:
                    video = Video(url, ctx.author)
                except youtube_dl.DownloadError as e:
                    await ctx.send(
                        "There was an error downloading your video, sorry.")
                    return
                client = await channel.connect()
                self._play_song(client, state, video)
                message = await ctx.send("", embed=video.get_embed())
                await self._add_reaction_controls(message)
                logging.info(f"Now playing '{video.title}'")
            else:
                raise commands.CommandError(
                    "You need to be in a voice channel to do that.")

    async def on_reaction_add(self, reaction, user):
        message = reaction.message
        if user != self.bot.user and message.author == self.bot.user:
            await message.remove_reaction(reaction, user)
            if message.guild and message.guild.voice_client:
                user_in_channel = user.voice and user.voice.channel and user.voice.channel == message.guild.voice_client.channel
                permissions = message.channel.permissions_for(user)
                guild = message.guild
                state = self.get_state(guild)
                if permissions.administrator or (
                        user_in_channel and state.is_requester(user)):
                    client = message.guild.voice_client
                    if reaction.emoji == "⏯":
                        # pause audio
                        self._pause_audio(client)
                    elif reaction.emoji == "⏭":
                        # skip audio
                        client.stop()
                    elif reaction.emoji == "⏮":
                        state.playlist.insert(
                            0, state.now_playing
                        )
                        client.stop()
                elif reaction.emoji == "⏭" and self.config["vote_skip"] and user_in_channel and message.guild.voice_client and message.guild.voice_client.channel:

                    voice_channel = message.guild.voice_client.channel
                    self._vote_skip(voice_channel, user)

                    channel = message.channel
                    users_in_channel = len([
                        member for member in voice_channel.members
                        if not member.bot
                    ])
                    required_votes = math.ceil(
                        self.config["vote_skip_ratio"] * users_in_channel)
                    await channel.send(
                        f"{user.mention} voted to skip ({len(state.skip_votes)}/{required_votes} votes)"
                    )

    async def _add_reaction_controls(self, message):

        CONTROLS = ["⏮", "⏯", "⏭"]
        for control in CONTROLS:
            await message.add_reaction(control)

class GuildState:

    def __init__(self):
        self.volume = 1.0
        self.playlist = []
        self.skip_votes = set()
        self.now_playing = None

    def is_requester(self, user):
        return self.now_playing.requested_by == user