import unittest
from datetime import datetime

from arize.exporter.utils.validation import Validator

class MyTestCase(unittest.TestCase):
    def test_valid_data_type(self):
        try:
            Validator.validate_input_value(valid_data_type.upper(), "data_type", data_types)
        except TypeError:
            self.fail("validate_input_type raised TypeError unexpectedly")

    def test_invalid_data_type(self):
        with self.assertRaisesRegex(
            ValueError,
            f"data_type is {invalid_data_type.upper()}, but must be one of PREDICTIONS, "
            f"CONCLUSIONS, EXPLANATIONS, PREPRODUCTION",
        ):
            Validator.validate_input_value(invalid_data_type.upper(), "data_type", data_types)

    def test_invalid_start_end_time(self):
        with self.assertRaisesRegex(
            ValueError,
            "start_time must be before end_time",
        ):
            Validator.validate_start_end_time(start_time, end_time)

valid_data_type = "preproduction"
invalid_data_type = "hello"
data_types = ("PREDICTIONS", "CONCLUSIONS", "EXPLANATIONS", "PREPRODUCTION")
start_time = datetime(2023, 6, 15, 10, 30)
end_time = datetime(2023, 6, 10, 10, 30)

if __name__ == "__main__":
    unittest.main()


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Tonghe Ying

import os, sys, time, random, json, glob
from hashlib import sha1
from remote.dispatcher.SSHContext import SSHSession, SSHContext
from remote.dispatcher.LSF import LSF
from remote.dispatcher.JobStatus import JobStatus

def _split_tasks(tasks,
                 group_size):
    ntasks = len(tasks)
    ngroups = ntasks // group_size
    if ngroups * group_size < ntasks:
        ngroups += 1
    chunks = [[]] * ngroups
    tot = 0
    for ii in range(ngroups):
        chunks[ii] = (tasks[ii::ngroups])
        tot += len(chunks[ii])
    assert (tot == len(tasks))
    return chunks

class Dispatcher(object):
    def __init__(self, remote_profile,
                 context_type='ssh',
                 batch_type='lsf',
                 job_record='jr.json'):
        self.remote_profile = remote_profile

        self.session = SSHSession(remote_profile)
        self.context = SSHContext
        self.uuid_name = True

        self.batch = LSF

        self.jrname = job_record

    def run_jobs(self,
                 resources,
                 command,
                 work_path,
                 tasks,
                 group_size,
                 forward_common_files,
                 forward_task_files,
                 backward_task_files,
                 forward_task_deference=True,
                 mark_failure=False,
                 outlog='log',
                 errlog='err',
                 args=None,
                 natoms=None):
        job_handler = self.submit_jobs(resources,
                                       command,
                                       work_path,
                                       tasks,
                                       group_size,
                                       forward_common_files,
                                       forward_task_files,
                                       backward_task_files,
                                       forward_task_deference,
                                       outlog,
                                       errlog,
                                       args,
                                       natoms=natoms)
        while not self.all_finished(job_handler, mark_failure):
            time.sleep(60)  # check every 5 minutes

    def submit_jobs(self,
                    resources,
                    command,
                    work_path,
                    tasks,
                    group_size,
                    forward_common_files,
                    forward_task_files,
                    backward_task_files,
                    forward_task_deference=True,
                    outlog='log',
                    errlog='err',
                    args=None,
                    natoms=None):
        self.backward_task_files = backward_task_files
        task_chunks = _split_tasks(tasks, group_size)
        task_chunks_str = ['+'.join(ii) for ii in task_chunks]
        task_hashes = [sha1(ii.encode('utf-8')).hexdigest() for ii in task_chunks_str]
        job_record = JobRecord(work_path, task_chunks, fname=self.jrname)
        job_record.dump()
        nchunks = len(task_chunks)

        job_list = []
        for ii in range(nchunks):
            cur_chunk = task_chunks[ii]
            cur_hash = task_hashes[ii]
            if not job_record.check_finished(cur_hash):
                submitted = job_record.check_submitted(cur_hash)
                if not submitted:
                    job_uuid = None
                else:
                    job_uuid = job_record.get_uuid(cur_hash)
                # communication context, batch system
                context = self.context(work_path, self.session, job_uuid)  # every task_chunk has an independent job_uuid
                batch = self.batch(context, uuid_names=self.uuid_name)
                rjob = {'context': context, 'batch': batch}
                # upload files
                if not rjob['context'].check_file_exists(rjob['batch'].upload_tag_name):
                    if forward_common_files[0] != '':
                        rjob['context'].upload('.',
                                               forward_common_files)  # forward_common_files are files to be uploaded

                    rjob['context'].upload(cur_chunk,
                                           forward_task_files[cur_chunk[0]],
                                           dereference=forward_task_deference)
                    if nchunks == 4 and ii == 0:
                        tot = int(cur_chunk[0][-5:-2])
                        num = len(forward_task_files[cur_chunk[0]])
                        for jj in range(tot, tot-num, -1):
                            jj_str = str(jj).zfill(3)
                            rjob['context'].block_call('cd ' + cur_chunk[0] + ' && rm -rf '
                                                                              '../../dataset-ML/N'+str(natoms)+'/N'+str(natoms)+'_dataset/updating_' +
                                                       jj_str + ' && cp -r ../train_'+jj_str+'-1/updating_' + jj_str +
                                                       ' ../../dataset-ML/N'+str(natoms)+'/N'+str(natoms)+'_dataset/')
                    rjob['context'].write_file(rjob['batch'].upload_tag_name, '')
                # submit new or recover old submission
                if not submitted:
                    rjob['batch'].submit(cur_chunk, command, args=args, res=resources, outlog=outlog, errlog=errlog)
                    job_uuid = rjob['context'].job_uuid
                else:
                    rjob['batch'].submit(cur_chunk, command, args=args, res=resources,
                                         outlog=outlog, errlog=errlog, restart=True)
                # record job and its remote context
                job_list.append(rjob)
                ip = None
                instance_id = None
                job_record.record_remote_context(cur_hash,
                                                 context.local_root,
                                                 context.remote_root,
                                                 job_uuid,
                                                 ip,
                                                 instance_id)
                job_record.dump()
            else:
                job_list.append(None)
        assert (len(job_list) == nchunks)
        job_handler = {
            'task_chunks': task_chunks,
            'job_list': job_list,
            'job_record': job_record,
            'command': command,
            'resources': resources,
            'outlog': outlog,
            'errlog': errlog,
            'backward_task_files': backward_task_files,
            'args': args
        }
        return job_handler

    def all_finished(self,
                     job_handler,
                     mark_failure,
                     clean=True):
        task_chunks = job_handler['task_chunks']
        task_chunks_str = ['+'.join(ii) for ii in task_chunks]
        task_hashes = [sha1(ii.encode('utf-8')).hexdigest() for ii in task_chunks_str]  # the same string corresponds the same number
        job_list = job_handler['job_list']
        job_record = job_handler['job_record']
        command = job_handler['command']
        tag_failure_list = ['tag_failure_%d' % ii for ii in range(len(command))]
        resources = job_handler['resources']
        outlog = job_handler['outlog']
        errlog = job_handler['errlog']
        backward_task_files = job_handler['backward_task_files']
        args = job_handler['args']
        print('checking jobs')
        nchunks = len(task_chunks)
        for idx in range(nchunks):
            cur_hash = task_hashes[idx]
            rjob = job_list[idx]
            if not job_record.check_finished(cur_hash):
                # chunk not finished according to record
                status = rjob['batch'].check_status()
                job_uuid = rjob['context'].job_uuid
                print('checked job %s' % job_uuid)
                if status == JobStatus.terminated:
                    job_record.increase_nfail(cur_hash)
                    if job_record.check_nfail(cur_hash) > 3:
                        raise RuntimeError('Job %s failed for more than 3 times' % job_uuid)
                    print('job %s terminated, submit again' % job_uuid)
                    print('try %s times for %s' % (job_record.check_nfail(cur_hash), job_uuid))
                    rjob['batch'].submit(task_chunks[idx], command, args=args, res=resources, outlog=outlog,
                                         errlog=errlog, restart=True)
                elif status == JobStatus.finished:
                    print('job %s finished' % job_uuid)
                    if mark_failure:
                        rjob['context'].download(task_chunks[idx], tag_failure_list, check_exists=True,
                                                 mark_failure=False)
                        rjob['context'].download(task_chunks[idx], backward_task_files[task_chunks[idx]],
                                                 check_exists=True)
                    else:
                        if task_chunks[idx][0].startswith('correction'):
                            rjob['context'].block_call('cd ' + task_chunks[idx][0] + ' && python read_static.py')
                        rjob['context'].download(task_chunks[idx], backward_task_files[task_chunks[idx][0]])
                    if clean:
                        rjob['context'].clean()
                    job_record.record_finish(cur_hash)
                    job_record.dump()
        job_record.dump()
        return job_record.check_all_finished()

class JobRecord(object):
    def __init__(self, path, task_chunks, fname='job_record.json', ip=None):
        self.path = os.path.abspath(path)
        self.fname = os.path.join(self.path, fname)
        self.task_chunks = task_chunks
        if not os.path.exists(self.fname):
            self._new_record()
        else:
            self.load()

    def check_submitted(self, chunk_hash):
        self.valid_hash(chunk_hash)
        return self.record[chunk_hash]['context'] is not None

    def record_remote_context(self,
                              chunk_hash,
                              local_root,
                              remote_root,
                              job_uuid,
                              ip=None,
                              instance_id=None):
        self.valid_hash(chunk_hash)
        self.record[chunk_hash]['context'] = {}
        self.record[chunk_hash]['context']['local_root'] = local_root
        self.record[chunk_hash]['context']['remote_root'] = remote_root
        self.record[chunk_hash]['context']['job_uuid'] = job_uuid
        self.record[chunk_hash]['context']['ip'] = ip
        self.record[chunk_hash]['context']['instance_id'] = instance_id

    def get_uuid(self, chunk_hash):
        self.valid_hash(chunk_hash)
        return self.record[chunk_hash]['context']['job_uuid']

    def check_finished(self, chunk_hash):
        self.valid_hash(chunk_hash)
        return self.record[chunk_hash]['finished']

    def check_all_finished(self):
        flist = [self.record[ii]['finished'] for ii in self.record]
        return all(flist)

    def record_finish(self, chunk_hash):
        self.valid_hash(chunk_hash)
        self.record[chunk_hash]['finished'] = True

    def check_nfail(self, chunk_hash):
        self.valid_hash(chunk_hash)
        return self.record[chunk_hash]['fail_count']

    def increase_nfail(self, chunk_hash):
        self.valid_hash(chunk_hash)
        self.record[chunk_hash]['fail_count'] += 1

    def valid_hash(self, chunk_hash):
        if chunk_hash not in self.record.keys():
            raise RuntimeError('chunk hash %s not in record, an invalid record may be used, please check file %s' % (
            chunk_hash, self.fname))

    def _new_record(self):
        task_chunks_str = ['+'.join(ii) for ii in self.task_chunks]
        task_hash = [sha1(ii.encode('utf-8')).hexdigest() for ii in task_chunks_str]
        self.record = {}
        for ii, jj in zip(task_hash, self.task_chunks):
            self.record[ii] = {
                'context': None,
                'finished': False,
                'fail_count': 0,
                'task_chunk': jj
            }

    def dump(self):
        with open(self.fname, 'w') as fp:
            json.dump(self.record, fp, indent=4)  # write data into json

    def load(self):
        with open(self.fname) as fp:
            self.record = json.load(fp)  # read from json and store with dictionary type

def make_dispatcher(jdata=None):
    context_type = 'ssh'
    batch_type = 'lsf'
    disp = Dispatcher(jdata, context_type=context_type, batch_type=batch_type)
    return disp


# import the necessary packages
import numpy as np
import settings
import time
from datetime import datetime
import json
from _thread import start_new_thread
from modules.retinaface import RetinaFace
from modules.db_storage import StudentStatus, DBStorage
from modules.mobilenetv2 import MobileNetV2
from modules.tracker import Tracker
from modules.db_redis import Rediser
from utils.unknown_processing import Pikachu

def classify_process():
    # connect to Redis server
    db_redis = Rediser(settings)
    db_storage = DBStorage()
    print("*Database connected")
	# load the pre-trained Keras model (here we are using a model
	# pre-trained on ImageNet and provided by Keras, but you can
	# substitute in your own networks just as easily)
    detect_model = RetinaFace(settings.CFG_RETINA)
    recog_model = MobileNetV2(settings.CHECKPOINT_PATH, db_redis)
    print("*All model loaded")
    tracker = Tracker()
    pikachu = Pikachu()
    print("*Tracker connected")
    # continually pool for new images to classify
    while True:
        # attempt to grab a batch of images from the database, then
        # initialize the image IDs and batch of images themselves
        q = db_redis.pop_image()
        if q is None:
            continue
        frameID, image = q
        # check to see if the batch list is None
        b_boxes, faces = detect_model.extract_faces(image)
        # inference the batch
        print("* Batch size: {}".format(faces.shape))
        results = recog_model.inference(faces)
        # loop over the image IDs and their corresponding set of
        # results from our model
        outputs = tracker.add_ids(faces, b_boxes, results)
        for o in outputs:
            obj_sequence = o["seq"]
            inKTX = o["inKTX"]
            label, prob = recog_model.get_sequence_label(np.array(obj_sequence))
            element = datetime.strptime(frameID,'%Y-%m-%d %H:%M:%S')
            lastseen_ts = datetime.timestamp(element) - 5
            lastseen_dt = datetime.fromtimestamp(lastseen_ts)
            if prob <= 0.5:
                label = "Unknown"
                start_new_thread(pikachu.save, (o["tracker_images"], lastseen_dt.strftime('%Y-%m-%d %H:%M:%S'), inKTX))
            else:
                start_new_thread(db_storage.save, (StudentStatus(student_id=label, inKTX=inKTX, detected_at=lastseen_dt.strftime('%Y-%m-%d %H:%M:%S')),))
            print(label, prob)

# if this is the main thread of execution start the model server
# process
if __name__ == "__main__":
	classify_process()


from OpenSave import SaveModeless, OpenModeless
from save import saveSession
from chimera import replyobj, dialogs
import chimera

def _saveCB(okayed, dialog):
	if not okayed:
		return
	paths = dialog.getPaths()
	if not paths:
		replyobj.warning("No save file selected; aborting save.\n")
		return
	saveSession(paths[0])
	chimera.setLastSession(paths[0])

class SaveSessionDialog(SaveModeless):
	name = "Save Session"
	title = "Choose Session Save File"

	def __init__(self):
		SaveModeless.__init__(self, command=_saveCB,
				filters=[("Chimera session", ["*.py"], ".py")],
				historyID="SimpleSession", compressed=True)
dialogs.register(SaveSessionDialog.name, SaveSessionDialog)

def _openCB(okayed, dialog):
	if not okayed:
		return
	for path in dialog.getPaths():
		chimera.openModels.open(path, type="Python")
		chimera.setLastSession(path)

class OpenSessionDialog(OpenModeless):
	name = "Open Session"
	title = "Choose Previously Saved Chimera Session File"

	def __init__(self):
		OpenModeless.__init__(self, command=_openCB,
				filters=[("Chimera session", ["*.py"])],
				defaultFilter="Chimera session", addAll=False,
				historyID="SimpleSession")
dialogs.register(OpenSessionDialog.name, OpenSessionDialog)


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import fasttext_pybind as fasttext

class _FastText(object):
    """
    This class defines the API to inspect models and should not be used to
    create objects. It will be returned by functions such as load_model or
    train.

    In general this API assumes to be given only unicode for Python2 and the
    Python3 equvalent called str for any string-like arguments. All unicode
    strings are then encoded as UTF-8 and fed to the fastText C++ API.
    """

    def __init__(self, model_path=None, args=None):
        self.f = fasttext.fasttext()
        if model_path is not None:
            self.f.loadModel(model_path)
        if args is not None:
            raise RuntimeError('args argument is not supported')

    def predict(self, text, k=1, threshold=0.0, on_unicode_error='strict'):
        """
        Given a string, get a list of labels and a list of
        corresponding probabilities. k controls the number
        of returned labels. A choice of 5, will return the 5
        most probable labels. By default this returns only
        the most likely label and probability. threshold filters
        the returned labels by a threshold on probability. A
        choice of 0.5 will return labels with at least 0.5
        probability. k and threshold will be applied together to
        determine the returned labels.

        This function assumes to be given
        a single line of text. We split words on whitespace (space,
        newline, tab, vertical tab) and the control characters carriage
        return, formfeed and the null character.

        If the model is not supervised, this function will throw a ValueError.

        If given a list of strings, it will return a list of results as usually
        received for a single line of text.
        """

        def check(entry):
            if entry.find('\n') != -1:
                raise ValueError(
                    "predict processes one line at a time (remove \'\\n\')"
                )
            entry += "\n"
            return entry

        if type(text) == list:
            text = [check(entry) for entry in text]
            all_labels, all_probs = self.f.multilinePredict(
                text, k, threshold, on_unicode_error)

            return all_labels, all_probs
        else:
            text = check(text)
            predictions = self.f.predict(text, k, threshold, on_unicode_error)
            if predictions:
                probs, labels = zip(*predictions)
            else:
                probs, labels = ([], ())
            return (labels, probs)

def load_model(path):
    """Load a model given a filepath and return a model object."""
    return _FastText(model_path=path)


import os, random, shutil
from augmentation import do_augmentation
import time

# master_dir = r'C:\Users\Work\PycharmProjects\DlProject\experiments\dataset'

# This contains all the files
train_dir = r'C:\Users\Work\PycharmProjects\DlProject\traintest\train'
test_dir = r'C:\Users\Work\PycharmProjects\DlProject\traintest\test'

dirs = os.walk(train_dir)
for s_dir in dirs:
    dir_path = s_dir[0]
    dir_files = s_dir[2]
    if (dir_files != 0):
        dir_name = os.path.basename(dir_path)
        test_files = random.sample(dir_files, int(0.2 * len(dir_files)))
        os.mkdir(os.path.join(test_dir, str(dir_name)))
        current_dir = os.path.join(test_dir, str(dir_name))
        for fname in test_files:
            srcpath = os.path.join(dir_path, fname)
            shutil.move(srcpath, current_dir)

aug_test_dir = r'C:\Users\Work\PycharmProjects\DlProject\traintest\aug_test'
aug_train_dir = r'C:\Users\Work\PycharmProjects\DlProject\traintest\aug_train'

a = do_augmentation(test_dir, aug_test_dir, 376, '6')
print(f'Test Data Augmentation: {a}')

time.sleep(10)

b = do_augmentation(train_dir, aug_train_dir, 1504, '6')
print(f'Train Data Augmentation: {b}')

Deep Learning - Project 2/18L-1879/P2_18L-1879/Pycharm Project/test_train.py


class RowSlot:

    def __repr__(self):
        return '{} instance of RowSlot'.format(self.__item_name)

    def __init__(self, data):
        # self.slot_number = data['slot_number']
        self.__item_name = data['item_name']
        self.__item_price = data['item_price']
        self.__last_stock = data['last_stock']
        self.__current_stock = data['current_stock']
        self.__current_deficit = 8 - self.__current_stock
        self.__amount_sold_since_last_stock = self.__last_stock - self.__current_stock
        self.__total_sold_dollars = self.__amount_sold_since_last_stock * self.__item_price

    def getTotalSold(self):
        return self.__total_sold_dollars

    def getAmountSold(self):
        return self.__amount_sold_since_last_stock

    def getName(self):
        return self.__item_name

    def getLastStock(self):
        return self.__last_stock

    def getCurrentStock(self):
        return self.__current_stock

    def getPrice(self):
        return self.__item_price

    def getDetails(self):
        return {
            'last_stock': self.__last_stock,
            'current_stock': self.__current_stock,
        }

class VMRow:

    def __repr__(self):
        return '{} instance of VMRow'.format(self.__row)

    def __init__(self, data):
        self.__row = data['row']
        self.__slots = []

        for slotDataDict in data['slots']:
            self.__slots.append(RowSlot(slotDataDict))

    def getSlots(self):
        return self.__slots

class VendingMachine:

    def __repr__(self):
        return '{} instance of VendingMachine'.format(self.__machine_id)

    def __init__(self, data):
        #  one row can hold up to 72 beverages
        self.__machine_id = data['machine_id']
        self.__machine_label = data['machine_label']
        self.__contents = []

        for contentDict in data['contents']:
            rowData = {}
            for key in contentDict:
                rowData[key] = contentDict[key]

            self.__contents.append(VMRow(rowData))

        self.__currentStock = self.__calculateStock()
        percentSold, salesTotal = self.__calculateCurrentInventory()
        self.__percent_sold_since_last_stock = percentSold
        self.__total_sales_since_last_stock = salesTotal

    def getContents(self):
        return self.__contents

    def getLabel(self):
        return self.__machine_label

    def getInventoryData(self):
        return self.__percent_sold_since_last_stock, self.__total_sales_since_last_stock

    def __calculateStock(self):
        currentStock = 0
        for row in self.__contents:
            for slot in row.getSlots():
                currentStock += slot.getCurrentStock()
        return currentStock

    def __calculateCurrentInventory(self):
        """Calculates the current inventory of the vending machine."""
        amountSold = 0
        amountStocked = 0
        salesTotal = 0.0
        for row in self.__contents:
            for slot in row.getSlots():
                amountStockedInSlot = slot.getLastStock()
                amountStocked += amountStockedInSlot

                numberSoldInSlot = slot.getAmountSold()
                amountSold += numberSoldInSlot

                salesTotal += slot.getTotalSold()

        percentSold = (amountSold / amountStocked) * 100
        return percentSold, salesTotal

class VendingMachineMgr:
    """
    A class to perfrom calculations on the managed Vending machines.
    """

    def __init__(self, machines, sortDict):
        self.__machines = machines

    def printInventory(self):
        "Prints our the inventory data of the managed machines."
        print('{}\t\t{}\t{}'.format('Label', 'Pct Sold', 'Sales Total'))
        machineDataDict = self.__getMachineInventoryData()
        for machineLabel in machineDataDict:
            percentSold = machineDataDict[machineLabel]['percent_sold']
            totalSales = machineDataDict[machineLabel]['sales_total']
            print('{0}\t\t{1:0.2f}%\t\t${2:8.2f}'.format(machineLabel, percentSold, totalSales))

    def __getMachineInventoryData(self):
        "Creates a dictionary of machine data for the inventory."
        machineInventoryDict = {}

        for machine in self.__machines:
            percentSold, salesTotal = machine.getInventoryData()
            machineInventoryDict[machine.getLabel()] = {
                'percent_sold': percentSold,
                'sales_total': salesTotal
            }
        return machineInventoryDict

    def sortBy(self, sortBy):
        "Handles a sort request."
        contentDict = self.__calculateStockNeeds(self.__getMachineData())
        sortedData = self.__sortData(contentDict, sortBy)
        self.__printData(sortedData)

    def __getMachineData(self):
        "Adds the vending machine data to a dictionary."
        contentDict = {}

        for machine in self.__machines:
            for content in machine.getContents():
                for slot in content.getSlots():
                    slotDetails = slot.getDetails()
                    slotName = slot.getName()

                    if slotName in contentDict:
                        for key in contentDict[slotName]:
                            if key == 'number_of_slots':
                                contentDict[slotName]['number_of_slots'] += 1
                            else:
                                contentDict[slotName][key] += slotDetails[key]
                    else:
                        contentDict[slotName] = slotDetails
                        contentDict[slotName]['number_of_slots'] = 1

        return contentDict

    def __calculateStockNeeds(self, contentDict):
        "Calculates the number of items required to fill the item's stock to full capacity."
        for itemName in contentDict:
            item = contentDict[itemName]
            totalStockCapacity = item['number_of_slots'] * 8
            item['stock_required'] = totalStockCapacity - item['current_stock']

            numSold = item['last_stock'] - item['current_stock']
            item['number_sold'] = numSold

            percentSold = numSold / item['last_stock']
            item['percent_sold'] = percentSold * 100

            del item['last_stock']
            del item['number_of_slots']

        return contentDict

    def __sortData(self, data, sortBy):
        "Sorts the Vending Machine data."

        def sortByKey(item):
            return item[sortBy]

        if sortBy == 'item_name':
            reverseSort = False
        else:
            reverseSort = True

        reformatted = self.__reformatData(data)
        reformatted.sort(key=sortByKey, reverse=reverseSort)

        return reformatted

    def __reformatData(self, data):
        "Takes the dictionary of contents to a sequence for easier sorting."
        reformatted = []

        for itemName in data:
            newItem = {}
            newItem['item_name'] = itemName
            for key in data[itemName]:
                newItem[key] = data[itemName][key]
            reformatted.append(newItem)
        return reformatted

    def __printData(self, sortedVendingContents):
        "prints the data to the console."
        print('\n{0:21}{1:8}{2:10}{3:10}{4:12}'.format('Item Name', 'Sold', '% Sold', 'In Stock', ' Stock needs'))
        for listItem in sortedVendingContents:
            print('{0:21}{1:7}{2:9.2f}%{3:8}{4:11}'.format(listItem['item_name'], listItem['number_sold'], listItem['percent_sold'], listItem['current_stock'], listItem['stock_required']))
        print('\n')


###################################
# CS B551 Spring 2021, Assignment #3
#
# Your names and user ids:
# Nagatheja Sharaf - nasharaf@iu.edu
# Prasad Hegde     - phegde@iu.edu
# Akhil            - nyeniset@iu.edu
# (Based on skeleton code by D. Crandall)
#

import random
import math

# We've set up a suggested code structure, but feel free to change it. Just
# make sure your code still works with the label.py and pos_scorer.py code
# that we've supplied.
#
class Solver:

    def __init__(self):
        # initialize all values
        self.mini_value = 0.0000001

        self.pos_count = {'adj': 0, 'adv': 0, 'adp': 0, 'conj': 0, 'det': 0,
                          'noun': 0, 'num': 0, 'pron': 0, 'prt': 0, 'verb': 0, 'x': 0, '.': 0}
        self.initial_count = {'adj': self.mini_value, 'adv': self.mini_value, 'adp': self.mini_value, 'conj': self.mini_value, 'det': self.mini_value, 'noun': self.mini_value,
                              'num': self.mini_value, 'pron': self.mini_value, 'prt': self.mini_value, 'verb': self.mini_value, 'x': self.mini_value, '.': self.mini_value}
        self.prob_pos_count = {'adj': 0, 'adv': 0, 'adp': 0, 'conj': 0, 'det': 0,
                               'noun': 0, 'num': 0, 'pron': 0, 'prt': 0, 'verb': 0, 'x': 0, '.': 0}
        self.pos = ['adj', 'adv', 'adp', 'conj', 'det',
                    'noun', 'num', 'pron', 'prt', 'verb', 'x', '.']

        self.prob_word_count = {}
        self.emission_count = {}
        self.word_count = {}
        self.gibbs = {}
        self.word_with_pos = {}
        self.bag_of_words = []
        self.trasition_count = {}
        self.next_line_reached = False

    # gives prosterior probability of all words in a sentence based on model type
    def posterior(self, model, sentence, label):
        """
        1. For simplified version for every word in a sentence calculate its emission probability
        Formulae: = sum(emission probability of words in a sentence)
        2. For Hidden Markov Model for every word in a sentence calculate its emission probability
        transition probability
        if word is first word in a sentence then
        Formulae: sum(emission probability of words in a sentence)
        else
        Formulae: sum(emission probability of words in a sentence)+sum(transition probability of words in a sentence)
        3. For Gibbs algorithm for every word in a sentence calculate its emission probability
        transition probability and gibbs algorithm
        if(length of sentecne is 1):
        Formulae: sum(emission probability of words in a sentence * (initial probability/sum(all initial probability values)))
        else if (word is first word in a sentence)
        Formulae: sum(emission probability of words in a sentence * (initial probability/sum(all initial probability values))*(transition probability)*(gibbs probability))
        else if (word is last word in a sentence)
        Formulae: (gibbs distribution * transition distribution)
        """
        if model == "Simple":
            result = 0
            for i in range(len(sentence)):
                result += math.log(
                    self.emission_distribution_prob(sentence[i])[label[i]])
            return result
        elif model == "HMM":
            result = 0
            for i in range(len(sentence)):
                if i == 0:
                    result += math.log(
                        self.emission_distribution_prob(sentence[i])[label[i]])
                else:
                    result += math.log(self.emission_distribution_prob(sentence[i])[label[i]]) \
                        + math.log(self.transition_distribution_prob(label[i])[label[i - 1]])

            return result

        elif model == "Complex":
            result = 0

            for i in range(len(sentence)):
                if len(sentence) == 1:
                    result += math.log(self.emission_distribution_prob(sentence[i])[label[i]]
                                       * (self.initial_count[label[i]] / sum(self.initial_count.values())))
                elif i == 0:
                    result += math.log(self.emission_distribution_prob(sentence[i])[label[i]]
                                       * (self.initial_count[label[i]] / sum(self.initial_count.values()))
                                       * self.transition_distribution_prob(label[i])[label[i + 1]]
                                       * self.gibbs_distribution_prob(sentence[i + 1], (label[i], label[i + 1])))
                elif i == len(sentence) - 1:
                    result += math.log(self.gibbs_distribution_prob(sentence[i], (label[i - 1], label[i]))
                                       * self.transition_distribution_prob(label[i - 1])[label[i]])
                else:
                    result += math.log(self.gibbs_distribution_prob(sentence[i], (label[i - 1], label[i]))
                                       * self.transition_distribution_prob(label[i - 1])[label[i]]
                                       * self.transition_distribution_prob(label[i])[label[i + 1]]
                                       * self.gibbs_distribution_prob(sentence[i + 1], (label[i], label[i + 1])))

            return result
        else:
            print("Unknown algo!")

    # calculate transition probabilites using transition values
    # This function will calculate probability of a pos(given) using all transition values
    transistion_prob = {}

    def transition_distribution_prob(self, p):
        if p not in self.transistion_prob.keys():
            temp_dict = {}
            # Formulae: transition values of given pos / sum(all transition values - transition value of given pos apperance)
            for i in self.pos:
                temp_dict[i] = self.trasition_count[p][i] / (
                    sum(self.trasition_count[p].values()) - self.trasition_count[p]['pos'])
            self.transistion_prob[p] = temp_dict
        return self.transistion_prob[p]

    # calculate emission probabilites using output of simplified algorithm values
    emission_prob = {}

    def emission_distribution_prob(self, word):

        if word not in self.emission_prob.keys():
            temp_dict = {}
            # Based on word pos count calculate emission probabilites
            for i in self.pos:
                if word in self.emission_count.keys():
                    temp_dict[i] = self.emission_count[word][i] / \
                        self.trasition_count[i]['pos']
                elif i == 'noun':
                    temp_dict[i] = 1 - (11 * self.mini_value)
                else:
                    temp_dict[i] = self.mini_value
            self.emission_prob[word] = temp_dict
        return self.emission_prob[word]

    # calculate gibbs probability
    def gibbs_distribution_prob(self, word, speeches):
        if word in self.gibbs.keys():
            if speeches in self.gibbs[word].keys():
                return self.gibbs[word][speeches] / sum([self.gibbs[word][k] for k in self.gibbs[word].keys() if k[0] == speeches[0]])

        return self.emission_distribution_prob(word)[speeches[1]]

    # start training data
    def train1(self):

        # fetch bag of words and add endofline when every line is ending
        for word, pos in (self.data):

            self.bag_of_words += (list(zip(pos, word)))
            self.bag_of_words.append(("endofline", "endofline"))

        # calculate transition count of all pos
        for idx, i in enumerate(self.bag_of_words):
            w = i[1]
            p = i[0]
            if(idx != len(self.bag_of_words)-1):
                p1 = self.bag_of_words[idx+1][0]
            # calculate until end of sentence is reached
            if(p != "endofline"):
                if(p1 != "endofline"):

                    if(p in self.trasition_count.keys()):
                        if(idx == 0 or self.next_line_reached == True and idx != len(self.bag_of_words)-1):
                            self.initial_count[p] += 1
                            self.next_line_reached = False
                        self.trasition_count[p][p1] += 1
                        self.trasition_count[p]["pos"] += 1
                    else:
                        self.trasition_count[p] = {'pos': 0, 'adj': self.mini_value, 'adv': self.mini_value, 'adp': self.mini_value, 'conj': self.mini_value, 'det': self.mini_value,
                                                   'noun': self.mini_value, 'num': self.mini_value, 'pron': self.mini_value, 'prt': self.mini_value, 'verb': self.mini_value, 'x': self.mini_value, '.': self.mini_value}
                        if(idx == 0 or self.next_line_reached == True and idx != len(self.bag_of_words)-1):
                            self.initial_count[p] += 1
                            self.next_line_reached = False
                        self.trasition_count[p][p1] += 1
                        self.trasition_count[p]["pos"] += 1
            else:
                self.next_line_reached = True

            # calculate word count for bag of words
            if(w != "endofline"):
                self.pos_count[p] += 1
                if w in self.word_count.keys():

                    self.word_count[w] = self.word_count[w]+1
                    self.word_with_pos[w][p] = self.word_with_pos[w][p]+1
                else:
                    self.word_count[w] = 1
                    self.word_with_pos[w] = {'adj': self.mini_value, 'adv': self.mini_value, 'adp': self.mini_value, 'conj': self.mini_value, 'det': self.mini_value,
                                             'noun': self.mini_value, 'num': self.mini_value, 'pron': self.mini_value, 'prt': self.mini_value, 'verb': self.mini_value, 'x': self.mini_value, '.': self.mini_value}
                    self.word_with_pos[w][p] += 1

        for i in self.word_count.keys():
            if(i not in self.prob_word_count.keys()):
                self.prob_word_count[i] = self.word_count[i] / \
                    len(self.word_count)

        for i in self.word_with_pos.keys():
            if i not in self.emission_count.keys():
                self.emission_count[i] = {'adj': self.mini_value, 'adv': self.mini_value, 'adp': self.mini_value, 'conj': self.mini_value, 'det': self.mini_value,
                                          'noun': self.mini_value, 'num': self.mini_value, 'pron': self.mini_value, 'prt': self.mini_value, 'verb': self.mini_value, 'x': self.mini_value, '.': self.mini_value}
                self.emission_count[i]["adj"] = self.word_with_pos[i]["adj"] / \
                    self.word_count[i]
                self.emission_count[i]["adv"] = self.word_with_pos[i]["adv"] / \
                    self.word_count[i]
                self.emission_count[i]["adp"] = self.word_with_pos[i]["adp"] / \
                    self.word_count[i]
                self.emission_count[i]["conj"] = self.word_with_pos[i]["conj"] / \
                    self.word_count[i]
                self.emission_count[i]["det"] = self.word_with_pos[i]["det"] / \
                    self.word_count[i]
                self.emission_count[i]["noun"] = self.word_with_pos[i]["noun"] / \
                    self.word_count[i]
                self.emission_count[i]["num"] = self.word_with_pos[i]["num"] / \
                    self.word_count[i]
                self.emission_count[i]["pron"] = self.word_with_pos[i]["pron"] / \
                    self.word_count[i]
                self.emission_count[i]["prt"] = self.word_with_pos[i]["prt"] / \
                    self.word_count[i]
                self.emission_count[i]["verb"] = self.word_with_pos[i]["verb"] / \
                    self.word_count[i]
                self.emission_count[i]["x"] = self.word_with_pos[i]["x"] / \
                    self.word_count[i]
                self.emission_count[i]["."] = self.word_with_pos[i]["."] / \
                    self.word_count[i]

        for i in self.pos_count.keys():
            self.prob_pos_count[i] = self.pos_count[i]/len(self.word_count)

        for i in self.word_with_pos.keys():
            if i not in self.emission_count.keys():
                self.emission_count[i] = {'adj': 0, 'adv': 0, 'adp': 0, 'conj': 0, 'det': 0, 'noun': 0,
                                          'num': 0, 'pron': 0, 'prt': 0, 'verb': 0, 'x': 0, '.': 0}
                self.emission_count[i]["adj"] = self.prob_pos_count["adj"]*(
                    self.word_with_pos[i]["adj"]/self.word_count[i])
                self.emission_count[i]["adv"] = self.prob_pos_count["adv"]*(
                    self.word_with_pos[i]["adv"]/self.word_count[i])
                self.emission_count[i]["adp"] = self.prob_pos_count["adp"]*(
                    self.word_with_pos[i]["adp"]/self.word_count[i])
                self.emission_count[i]["conj"] = self.prob_pos_count["conj"]*(
                    self.word_with_pos[i]["conj"]/self.word_count[i])
                self.emission_count[i]["det"] = self.prob_pos_count["det"]*(
                    self.word_with_pos[i]["det"]/self.word_count[i])
                self.emission_count[i]["noun"] = self.prob_pos_count["noun"]*(
                    self.word_with_pos[i]["noun"]/self.word_count[i])
                self.emission_count[i]["num"] = self.prob_pos_count["num"]*(
                    self.word_with_pos[i]["num"]/self.word_count[i])
                self.emission_count[i]["pron"] = self.prob_pos_count["pron"]*(
                    self.word_with_pos[i]["pron"]/self.word_count[i])
                self.emission_count[i]["prt"] = self.prob_pos_count["prt"]*(
                    self.word_with_pos[i]["prt"]/self.word_count[i])
                self.emission_count[i]["verb"] = self.prob_pos_count["verb"]*(
                    self.word_with_pos[i]["verb"]/self.word_count[i])
                self.emission_count[i]["x"] = self.prob_pos_count["x"] * \
                    (self.word_with_pos[i]["x"]/self.word_count[i])
                self.emission_count[i]["."] = self.prob_pos_count["."] * \
                    (self.word_with_pos[i]["."]/self.word_count[i])

    def train(self, data):
        self.data = data
        self.train1()

    # Functions for each algorithm. Right now this just returns nouns -- fix this!
    #

    def simplified(self, sentence):
        lis = []
        for i in sentence:
            if i in self.emission_count.keys():
                a_dictionary = self.emission_count[i]
            else:
                a_dictionary = {'adj': 0, 'adv': 0, 'adp': 0, 'conj': 0, 'det': 0,
                                'noun': 1, 'num': 0, 'pron': 0, 'prt': 0, 'verb': 0, 'x': 0, '.': 0}
            max_key = max(a_dictionary, key=a_dictionary.get)
            lis.append(max_key)
        return lis

    def hmm_viterbi(self, sentence):
        # initialize with 0s  in vi table and which table
        vi_table = [[0 for i in range(12)] for t in range(len(sentence))]
        which_table = [[0 for i in range(12)] for t in range(len(sentence))]
        result = []
        for i in range(0, len(sentence)):
            count = 0
            for p in self.pos:
                # if the word in sentence is first word then
                # formulae  = emission probabilites-initial prob
                if i == 0:
                    vi_table[i][count] = -math.log(self.emission_distribution_prob(sentence[i])[p]) \
                        - math.log((self.initial_count[p] / sum(self.initial_count.values())))
                    count = count + 1
                else:
                    # formulae = transition probability - emission prob
                    q = list(range(0, 12))
                    vi_tablevalues = [vi_table[i - 1][l]
                                      - math.log(self.transition_distribution_prob(p2)[p])
                                      - math.log(self.emission_distribution_prob(sentence[i])[p])
                                      for l, p2 in zip(q, self.pos)]
                    # select min as we are taking log values among all vi table values
                    cost = min(vi_tablevalues)
                    vi_table[i][count] = cost
                    # store the index of that min value above into which table
                    which_table[i][count] = self.pos[vi_tablevalues.index(
                        cost)]
                    count = count + 1
        # back tracking
        result = [""]*len(sentence)
        last = self.pos[vi_table[len(
            sentence) - 1].index(min(vi_table[len(sentence) - 1]))]
        result[-1] = last
        for i in range(len(sentence)-1, 0, -1):
            m = vi_table[i].index(min(vi_table[i]))
            result[i-1] = which_table[i][m]
        return result

    # complex monte carlo markov chain
    def complex_mcmc(self, sentence):
        count = []
        sample = []
        sen_len_pos_tags = {}
        prob = {}

        for i in range(0, len(sentence)):
            for p in self.pos:
                sen_len_pos_tags[p] = 0
            count.append(sen_len_pos_tags)
            sen_len_pos_tags = {}

        for i in range(0, len(sentence)):
            sample.append("noun")

        iterations = 1000
        for i in range(iterations):
            for j in range(len(sample)):
                # for every pos calculate prob of every word
                for p in self.pos:
                    # if sample =1
                    # formulae = emission prob * initial count / sum(initial count values)
                    if(len(sample) == 1):
                        prob[p] = self.emission_distribution_prob(sentence[j])[p] * \
                            (self.initial_count[p] /
                             sum(self.initial_count.values()))
                    # if this is first sample then
                    # formulae = emission prob * initial count / sum(initial count values) * transition prob * gibbs prob
                    elif j == 0:
                        prob[p] = self.emission_distribution_prob(sentence[j])[p] * \
                            (self.initial_count[p] / sum(self.initial_count.values())) * \
                            self.transition_distribution_prob(p)[sample[j + 1]] * \
                            self.gibbs_distribution_prob(
                                sentence[j + 1], (p, sample[j + 1]))
                    # if this is last sample then
                    # formulae  = gibbs prob * transition prob
                    elif j == len(sample) - 1:
                        prob[p] = self.gibbs_distribution_prob(sentence[j], (sample[j - 1], p)) * \
                            self.transition_distribution_prob(sample[j - 1])[p]

                    else:
                        prob[p] = self.gibbs_distribution_prob(sentence[j], (sample[j - 1], p)) * \
                            self.transition_distribution_prob(sample[j - 1])[p] * \
                            self.transition_distribution_prob(p)[sample[j + 1]] * \
                            self.gibbs_distribution_prob(
                                sentence[j + 1], (p, sample[j + 1]))

                probs = {v: float(prob[v]) / sum(prob.values())
                         for v in self.pos}

                # sample the probs
                rand = random.random()
                num = 0
                for p in self.pos:
                    num += probs[p]
                    if rand < num:
                        sample[j] = p
                        break
                # first half of iterations
                if i > iterations / 2:
                    for k in range(len(sample)):
                        count[k][sample[k]] += 1
        result = []
        for i in range(len(sample)):
            pos = "noun"
            max = 0
            for p in self.pos:
                if count[i][p] >= max:
                    max = count[i][p]
                    pos = p
            result.append(pos)
        return result

    # This solve() method is called by label.py, so you should keep the interface the
    #  same, but you can change the code itself.
    # It should return a list of part-of-speech labelings of the sentence, one
    #  part of speech per word.
    #

    def solve(self, model, sentence):
        if model == "Simple":
            return self.simplified(sentence)
        elif model == "HMM":
            return self.hmm_viterbi(sentence)
        elif model == "Complex":
            return self.complex_mcmc(sentence)
        else:
            print("Unknown algo!")


# -*- coding: utf-8 -*-
# @Time    : 2019/11/8 11:17
# @Author  : tian
# @Email   : zengdetian@eefung.com
# @File    : 文本预测.py
# @Software: PyCharm
from sklearn.externals import joblib
import jieba

Data_path = "D:\数据\DATA\轻量敏感的数据_过滤.txt"

# 对新文本进行切词处理
stopwords = list()

with open("stop_words.txt", 'r', encoding='utf-8') as f:
    for word in f.readlines():
        stopwords.append(word[:-1])

Tf_idf_Model_save_path = "./tfidf_model.m"
tfidf_model = joblib.load(Tf_idf_Model_save_path)
lr_model = joblib.load('逻辑回归文本分类.model')   #加载模型

def predict_label(data):
    text_list = list()
    seg_text = jieba.cut(data)
    text = [word for word in seg_text if word not in stopwords]
    text_list.append(' '.join(text))

    X_test = tfidf_model.transform(text_list).toarray()
    result = lr_model.predict(X_test)
    print(result)
    return result

if __name__ == "__main__":
    Result_path = "分类结果/轻量敏感文本的分类结果.txt"
    f_w = open(Result_path, "w", encoding="utf8")

    Result_path1 = "分类结果/轻量敏感文本的分类结果-重敏感.txt"
    f_w1 = open(Result_path1, "w", encoding="utf8")

    Result_path2 = "分类结果/轻量敏感文本的分类结果-非敏感.txt"
    f_w2 = open(Result_path2, "w", encoding="utf8")

    for i, line in enumerate(open(Data_path, "r", encoding="utf8").readlines()):
        content = line.split("\t", maxsplit=1)[1]
        result = predict_label(content.strip())
        f_w.write(result[0] + "\t" + content)
        if result[0] == "重敏感":
            f_w1.write(result[0] + "\t" + content)
        else:
            f_w2.write(result[0] + "\t" + content)

    print("共处理完%d" % i)

文本分类器/文本预测.py


import warp
from PIL import Image
from pylab import *
#affine example: im1 to im2
im1 = array(Image.open('/home/wangkai/Pictures/beatles69.jpg').convert('L'))
im2 = array(Image.open('/home/wangkai/Pictures/billboard_for_rent.jpg').convert('L'))

#choice some object points
tp = array([[264,538,540,264],[40,36,605,605],[1,1,1,1]])
im3 = warp.image_in_image(im1,im2,tp)
figure()
gray()
imshow(im3)
axis('equal')
axis('off')
show()


from Data_manager.src.Storage_data import Storage
from Data_manager.src.product_manager import product_manager

class Order_detail_manager:
    def __init__(self):
        self.Storage = Storage()
        self.manage_product = product_manager()

    def insert(self, Order_ID, Product_ID, quantity ):

        if quantity > 0:
            count, result = self.select_product_from_order("quantity", Order_ID, Product_ID, "order_products")
            new_quantity = 0
            print(count, result)
            if count > 0:
                print(True)
                for ori_quantity in result:
                    new_quantity = quantity + ori_quantity[0]

                total_price = self.calculate_total_product_price(Product_ID, new_quantity)
                total_price = self.Storage.add_dot(total_price)

                self.update_table_order_product("total_price", total_price, Order_ID, Product_ID, "order_products")
                self.update_table_order_product('quantity', new_quantity, Order_ID, Product_ID, "order_products")

            else:
                total_price = self.calculate_total_product_price(Product_ID, quantity)
                total_price = self.Storage.add_dot(total_price)
                command = "INSERT INTO order_products (Order_ID, Product_ID, quantity, total_price) VALUE ('%s','%s','%s','%s')"% (Order_ID, Product_ID, quantity, total_price)
                self.Storage.execute_query(command)
                print("Successfully insert !")

    def calculate_total_product_price(self, product_ID, quantity):

        origin_price = self.manage_product.get_price(product_ID)
        origin_price = self.Storage.remove_dot_to_number(origin_price)
        result = quantity * origin_price

        return result

    def select_product_from_order(self, option, Order_ID, Product_ID, table):

        """
        return count, result from order_products table
        """
        command = "select %s from %s where Order_ID = '%s' and Product_ID = '%s'" % (
            option, table, Order_ID, Product_ID)
        result = self.Storage.select_by_query(command)
        # print(result)

        final_result = []
        count = 0
        try:
            for i in result:
                count += 1
                final_result.append(i)
        except:
            pass
        return  count, final_result

    def update_table_order_product(self, option, new_val, Order_ID, Product_ID, table):
        update_command = "UPDATE %s SET %s = '%s' WHERE Order_ID= '%s' and Product_ID = '%s'" % (
            table, option, new_val, Order_ID, Product_ID)
        self.Storage.execute_query(update_command)

    def get_individual_total_price(self, Order_ID, Product_ID, table="order_products"):
        result = self.select_product_from_order("total_price", Order_ID, Product_ID, table)
        return result[1][0]

    def get_all_price_of_order(self, Order_ID, table="order_products"):
        command = "select total_price from %s where Order_id = '%s'" % (table, Order_ID)
        result = self.Storage.select_by_query(command)
        all_price = []
        for price in result:
            all_price.append(price[0])
        return all_price

    def get_quantity(self, Order_ID, Product_ID, table="order_products"):
        result = self.select_product_from_order("quantity", Order_ID, Product_ID, table)
        return result[1][0]

    def update_quantity(self, new_quantity, Order_ID, Product_ID, table="order_products"):
        self.update_table_order_product("quantity", new_quantity, Order_ID, Product_ID, table)

    def update_price(self, new_price, Order_ID, Product_ID, table="order_products"):
        self.update_table_order_product("total_price", new_price, Order_ID, Product_ID, table)

    def delete_product(self, product_id, order_id):
        command = "delete from order_products where Order_ID = '%s' AND Product_ID = '%s'" % (order_id, product_id)
        self.Storage.execute_query(command)
        print("successfully remove %s"%(product_id))

    def delete_product_from_order(self, order_id):
        command = "delete from order_products where Order_ID = '%s'" % (order_id)
        self.Storage.execute_query(command)

    def select_detail_order(self, Order_id):
        command = "SELECT product_id, product_name, product_price, quantity, order_products.total_price FROM " \
                  "order_products INNER JOIN orders USING (Order_ID ) INNER JOIN product_data USING (product_id) " \
                  "where order_products.Order_ID = '%s'" % Order_id

        cur = self.Storage.select_by_query(command)
        result = []
        for item in cur:
            result.append((item[0], item[1], item[2], item[3], item[4]))
        return result

    def select_detail_product_from_order(self, order_id, product_id):
        command = "SELECT product_id, product_name, product_price, quantity, order_products.total_price FROM " \
                  "order_products INNER JOIN orders USING (Order_ID ) INNER JOIN product_data USING (product_id) " \
                  "where order_products.Order_ID = '%s' and order_products.Product_ID = '%s'"%(order_id, product_id)
        result = []
        cur = self.Storage.select_by_query(command)
        for item in cur:
            # print(item)
            result.append(item)
        print(result)
        return result

    def drop(self):
        self.Storage.drop_table("order_products")

    def create(self):
        self.Storage.Create_table("order_product", "order_products")

    def select_all(self):
        return self.Storage.Select_all("order_products")


# import sys
# sys.path.extend(['/home/ubuntu/workspace/scrabble-gan'])

import os
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf

os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'

def main():
    latent_dim = 128
    char_vec = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    path_to_saved_model = '/home/ubuntu/workspace/scrabble-gan/res/out/big_ac_gan/model/generator_15'

    # number of samples to generate
    batch_size = 10
    # sample string
    sample_string = 'machinelearning'
    # load trained model
    imported_model = tf.saved_model.load(path_to_saved_model)

    # inference loop
    for idx in range(1):
        fake_labels = []
        words = [sample_string] * 10
        noise = tf.random.normal([batch_size, latent_dim])
        # encode words
        for word in words:
            fake_labels.append([char_vec.index(char) for char in word])
        fake_labels = np.array(fake_labels, np.int32)

        # run inference process
        predictions = imported_model([noise, fake_labels], training=False)
        # transform values into range [0, 1]
        predictions = (predictions + 1) / 2.0

        # plot results
        for i in range(predictions.shape[0]):
            plt.subplot(10, 1, i + 1)
            plt.imshow(predictions[i, :, :, 0], cmap='gray')
            # plt.text(0, -1, "".join([char_vec[label] for label in fake_labels[i]]))
            plt.axis('off')
        plt.show()

if __name__ == "__main__":
    main()


#BMI Calculator from scratch @jauvany

w = int(input())
h = float(input())

fh = h ** 2
bmi = w / fh
if bmi < 18.5:
  print("Underweight")
else:
  if (bmi >= 18.5) and (bmi < 25):
    print("Normal")
  else:
    if (bmi >= 25) and (bmi < 30):
      print("Overweight ")
    else:
      if bmi > 30:
        print("Obesity")


#!/usr/bin/python3.7

from os.path import dirname, abspath, join, isdir, exists, isfile, split
from os import listdir, mkdir, system, remove
from shutil import rmtree, copyfile
from parsing.classes.cpp_parser import CppParser
from clang.cindex import CursorKind
from operator import itemgetter
import subprocess
import logging
import json
from configurations.utils.helper import get_cpp_version
from utils.compiler import get_gpp_version
from multiprocessing import Process

COMPILER_OPTS = ["-O2", "-O3", "-Os", "-Ofast"]
PARALLEL_PROCESS = 16

"""
Compiler's goal is to compile with the options provided above, parse the sources
to get the mangled names of the functions that are getting called inside the methos,
edit the json to add such knowledge with mangled names and finally generate the gcc
summary of inlining.
"""

def _get_file_offset(calls_info, mang_name):
    for val in calls_info:
        if val[0] == mang_name:
            return val[1]
    return -1

def _remove_duplicates(offsets):
    offsets_indexes = []
    for offset in offsets:
        if offset[0] not in offsets_indexes:
            offsets_indexes.append(offset[0])
    offsets_indexes = list(set(offsets_indexes))
    new_offsets = []
    for offset_index in offsets_indexes:
        for offset in offsets:
            if offset_index == offset[0]:
                new_offsets.append(offset)
                break
    return new_offsets

def _get_functions_called_info(method_definition):
    functions_info = []
    mangled_names = []
    definitions = [method_definition]
    while definitions:
        current_definition = definitions.pop(0)
        list_calls = current_definition.get_called_functions()
        for call in list_calls:
            call_definition = call.get_definition()
            if call_definition is not None:
                if not call_definition.node.mangled_name:
                    raise Exception("Method without mangled name!")
                mangled_name = call_definition.node.mangled_name
                offset = call_definition.node.location.offset
                if (mangled_name not in mangled_names and
                    mangled_name != method_definition.node.mangled_name):
                    mangled_names.append(mangled_name)
                    functions_info.append([mangled_name, offset])
                    definitions.append(call_definition)
    return functions_info

def _get_function_sizes(info_inliner):
    sizes = []
    lines = info_inliner.split("\n")
    i = 0
    for line in lines:
        i += 1
        if "self size:" in line:
            if "int main" in lines[i - 4]:
                continue
            if "void wrapper" in lines[i - 4]:
                continue
            size = int(line.split("self size:")[1].strip(" "))
            sizes.append(size)
    sizes = list(set(sizes))
    sizes.sort(reverse=True)
    return sizes

def _get_inlined_method_definition(cpp_object, preprocessed_src_path, method_name):
    # Retrieving function wrapper
    list_defs = cpp_object.get_function_definitions("wrapper")
    # Retrieving the call to the method
    list_calls = list_defs[0].get_called_functions()
    for call in list_calls:
        definition = call.get_definition()
        if definition != None:
            if definition.name == method_name:
                return definition
    return None

def _compiler_directory_multiprocess(directory_src_path, directory_dst_path, method, gpp_version, cpp_version, compiler_logger):
    method_src_path = join(directory_src_path, method)
    method_dst_path = join(directory_dst_path, method)
    mkdir(method_dst_path)
    # 2nd step: compile and place in folders
    j = 1
    methods = listdir(method_src_path)
    for preprocessed_src in methods:
        # Not considering json directly
        if preprocessed_src[-5:] == ".json":
            continue
        # Loading json output of preprocessor
        old_json_path = join(method_src_path, preprocessed_src[:-2] + ".json")
        with open(old_json_path) as f:
            old_json = json.load(f)
        # Parsing method to get the function called inside
        preprocessed_src_path = join(method_src_path, preprocessed_src)
        cpp_object = CppParser(preprocessed_src_path, include_headers=True)
        method_definition = _get_inlined_method_definition(cpp_object, preprocessed_src_path, old_json["method_name"])
        if method_definition is None:
            compiler_logger.info("[INFO] Compilation error with file: " + preprocessed_src_path)
            continue
        method_mangled_name = method_definition.mangled_name
        calls_info = _get_functions_called_info(method_definition)
        method_offset = method_definition.node.location.offset
        if calls_info:
            old_json['calls'] = [i[0] for i in calls_info]
        else:
            old_json['calls'] = []
        # Compiling and getting info about inlined calls
        for opt in COMPILER_OPTS:
            cmd = ["g++" , "-std=" + cpp_version, "-fdump-ipa-inline-optimized-missed=/dev/stdout", opt, "-fno-access-control", "-w", preprocessed_src_path, "-o","/dev/null" ]
            if "compiler_options" in old_json.keys():
                for opt2 in old_json["compiler_options"]:
                    cmd += [opt2]
            process = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
            stdout, stderr = process.communicate()
            if stderr:
                compiler_logger.error("[ERROR] Compilation error with the command:\n" +
                                            ' '.join(cmd) + '\n'
                                            "Error:\n" + stderr.decode("utf-8"))
                continue
            info_inliner = stdout.decode("utf-8")
            # From the inline info we need to retrieve the sizes of the functions
            sizes = _get_function_sizes(info_inliner)
            # Checking for duplicates
            if set(old_json['calls']) != set(list(set(old_json['calls']))):
                raise Exception("Duplicates in parsing function calls for file " + preprocessed_src_path + "!")
            if not sizes:
                sizes = [-1]
            else:
                sizes = [1, 500]
            for size in sizes:
                binary_name = method + "_" + str(j)
                binary_path = join(method_dst_path,  binary_name)
                # Compiling
                if size == -1:
                    cmd = ["g++" , "-std=" + cpp_version, opt, "-fno-access-control", "-w", preprocessed_src_path, "-o", binary_path]
                else:
                    cmd = ["g++" , "-std=" + cpp_version, opt, "-fno-access-control", "-w", preprocessed_src_path, "-o", binary_path, "--param", "max-inline-insns-auto=" + str(size)]
                if "compiler_options" in old_json.keys():
                    for opt2 in old_json["compiler_options"]:
                        cmd += [opt2]
                process = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=None)
                stderr = process.stderr.read()
                if stderr:
                    compiler_logger.error("[ERROR] Compilation error with the command:\n" +
                                                ' '.join(cmd) + '\n'
                                                "Error:\n" + stderr.decode("utf-8"))
                    raise Exception("Failed in forcing no inline for file " + preprocessed_src_path + ".")
                else:
                    compiler_logger.debug("[DEBUG] " + ' '.join(cmd))
                # Renaming json and adding compile opt
                new_json_name = method + "_" + str(j) + ".json"
                new_json_path = join(method_dst_path,  new_json_name)
                new_json = old_json.copy()
                new_json['optimization'] = opt
                new_json['mangled_name'] = method_mangled_name
                new_json['function_name'] = new_json['method_name']
                new_json['path_name'] = new_json['class_name']
                new_json['compiler_version'] = gpp_version
                new_json['cpp_version'] = cpp_version
                del new_json['class_name']
                del new_json['method_name']
                with open(new_json_path, 'w') as f:
                    json.dump(new_json, f, indent=2)
                j += 1
    if j == 1:
        rmtree(method_dst_path)

def _compile_directory(src_path, dst_path, compiler_logger, directory):
    directory_src_path = join(src_path, directory)
    directory_dst_path = join(dst_path, directory)
    if not exists(directory_src_path) or not isdir(directory_src_path):
        raise Exception("Path " + directory_src_path + " doesn't exists!")
    if exists(directory_dst_path) and isdir(directory_dst_path):
        rmtree(directory_dst_path)
    mkdir(directory_dst_path)
    # Retrieving compiler version
    gpp_version = get_gpp_version()
    # Loading cpp version from configuration file
    cpp_version = get_cpp_version()
    # Spawning Processes
    dir_src_path_list = listdir(directory_src_path)
    for index in range(len(dir_src_path_list))[::PARALLEL_PROCESS]:
        start = index
        if index + PARALLEL_PROCESS > len(dir_src_path_list):
            end = len(dir_src_path_list)
        else:
            end = index + PARALLEL_PROCESS
        processes = []
        # Generating processes
        for i in range(start, end):
            p = Process(target=_compiler_directory_multiprocess,
                        args=(directory_src_path,
                                directory_dst_path,
                                dir_src_path_list[i],
                                gpp_version,
                                cpp_version,
                                compiler_logger))
            processes.append(p)
        # Launching them
        for p in processes:
            p.start()
        # Waiting them
        for p in processes:
            p.join()

def compiler(src_path, dst_path):
    # Projecj path
    prj_path = dirname(abspath(__file__))
    # Logger
    compiler_logger = logging.getLogger('compiler')
    # Path checks
    if not isdir(src_path):
        raise Exception(src_path + " is not a directory path")
    if not isdir(dst_path):
        raise Exception(dst_path + " is not a directory path")
    # Compilation section
    compiler_logger.info("[INFO] Joining " + src_path + " directory.")
    directory = split(src_path)[1]
    # Creating destination directory
    directory_dst_path = join(dst_path, directory)
    if exists(directory_dst_path) and isdir(directory_dst_path):
        rmtree(directory_dst_path)
        compiler_logger.debug("[DEBUG] Removed " + directory_dst_path + " directory.")
    mkdir(directory_dst_path)
    compiler_logger.debug("[DEBUG] Created " + directory_dst_path + " directory.")
    # Compiling methods
    compiler_logger.info("[INFO] Compiling methods...")
    _compile_directory(src_path, directory_dst_path, compiler_logger, "public")
    compiler_logger.info("[INFO] Methods compiled.")


#!/usr/bin/env python
# coding: utf-8

# # Solución Código #

# In[29]:

def generar_dataframe_palabras(text):
    import pandas
    counts = dict()

    words = text.split()

    for word in words:
        counts[word] = counts.get(word,0) + 1
    #counts
    df = pandas.DataFrame.from_dict(counts, orient = 'index').reset_index().rename(columns={"index": "Words", "0": "count"})
    return df

neruda = """Puedo escribir los versos más tristes esta noche.
            Yo la quise, y a veces ella también me quiso.
            En las noches como ésta la tuve entre mis brazos.
            La besé tantas veces bajo el cielo infinito.

            Ella me quiso, a veces yo también la quería.
            Cómo no haber amado sus grandes ojos fijos.""".lower()  # lower hace que todas las letras sean minúsculas.

if __name__ == '__main__':
    d = generar_dataframe_palabras(neruda)
    assert isinstance(d, pandas.DataFrame), "Aun no generas un Dataframe, verifica tu algoritmo!"
    print("Excelente, has convertido un poema en un Dataframe para trabajar con el!")

# In[ ]:


#!/usr/bin/env python3.6

import types
import ssl
import os

from http.server import (
    HTTPServer,
    SimpleHTTPRequestHandler)

from ktls.utils import set_ktls_sockopt

def sendall(self, b):
    """ overwrite origin socket.sendall

    ref: cpython/Lib/socketserver.py +791
    """
    fd = self.fileno()
    os.write(fd, b)

class HTTPSServer(HTTPServer):

    def get_request(self):
        """ overwrite origin get_request for setting ktls

        ref: cpython/Lib/socketserver.py +490
        """
        conn, addr = super().get_request()

        # set ktls socket options
        conn = set_ktls_sockopt(conn)
        conn.sendall = types.MethodType(sendall, conn)
        return conn, addr

def run():

    host, port = "localhost", 4433
    cert, key = 'ktls/ca/cert.pem', 'ktls/ca/key.pem'
    handler = SimpleHTTPRequestHandler

    # prepare ssl context
    ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
    ctx.load_cert_chain(certfile=cert, keyfile=key)
    ctx.set_ciphers('ECDH-ECDSA-AES128-GCM-SHA256')

    # run the https server
    with HTTPSServer((host, port), handler) as httpd:
        httpd.socket = ctx.wrap_socket(httpd.socket,
                                       server_side=True)
        httpd.serve_forever()

try:
    run()
except KeyboardInterrupt:
    pass


import numpy as np

def compute_quantity (plan, stype):

    if stype == 'stack':
        return compute_quantity_stack (plan['packages']['children'])
    elif stype == 'shelf':
        return compute_quantity_shelf (plan['packages']['children'])
    else:
        return compute_quantity_apr (plan['packages']['children'])

def compute_quantity_stack (packages):

    counter = 0
    for row in packages:
        for stack in row['children']:
            if 'children' in stack :
                for unit in stack['children']:
                    counter += (len(unit['children']))

    return counter

def compute_quantity_apr (packages):

    counter = 0
    for row in packages:
        for stack in row['children']:
            if 'children' in stack :
                for unit in stack['children']:
                    counter += (len(unit['children']))

    return counter

def compute_quantity_shelf (packages):

    counter = 0
    for row in packages:
        for stack in row['children']:
            if 'children' in stack :
                counter += (len(stack['children']))

    return counter

def compute_rackspec (plan, stype):

    rackspec = {}
    if stype == 'stack':
        return {}

    uprights = plan['skeleton']['upright']
    beams = plan['skeleton']['beam']

    unit= uprights[0]
    beam = beams[0]

    rackspec['bay_depth'] = max( unit[1][1][0] - unit[0][0][0], unit[1][1][1] - unit[0][0][1])
    rackspec['upright_counts'] = len( uprights )
    rackspec['upright_width'] = min( unit[1][1][0] - unit[0][0][0], unit[1][1][1] - unit[0][0][1])

    return rackspec

def compute_polygon_area ( polygon, type = ""):

    pts = polygon
    x = []
    y = []

    for id in range(len(pts)):
        x.append(pts[id][0])
        y.append(pts[id][1])

    size = 0.5*np.abs(np.dot(x,np.roll(y,1))-np.dot(y,np.roll(x,1)))
    return size


import os
import re

from aiohttp import ClientSession
from aiogram import Bot, Dispatcher, executor, types
from typing import Optional
import logging

logging.basicConfig(level=logging.INFO)
bot = Bot(token=os.environ["TOKEN"], parse_mode=types.ParseMode.HTML)
dp = Dispatcher(bot)

def remove_punctuation(s: str) -> str:
    """Removes all punctuation in text excluding dashes and hyphens"""
    for sign in ",.!?<>@#$%^&*()_+=:;'\"/\\":
        s = s.replace(sign, "")
    return s

async def predict(text: str) -> Optional[dict]:
    async with ClientSession() as session:
        async with session.post(
                f"{os.environ['API_URL']}/detect_slang",
                json={
                    "text": text
                }
        ) as res:
            if res.status != 200:
                logging.error(f"Got API error while detecting slang. ({await res.text()})")
                return None
            data = await res.json()
    return data

async def get_term_definition(term: str) -> Optional[tuple[str, str]]:
    async with ClientSession() as session:
        async with session.get(f"{os.environ['API_URL']}/term/{term}") as res:
            if res.status not in {404, 200}:
                logging.error(f"Got API error while getting term definition. ({await res.text()})")
                return None
            data = await res.json()
            # print(data)
            return (data['result']['definition'], data['result']['key'])\
                if data['status'] == 'ok' else ("", "")

@dp.message_handler(commands=["start"])
async def start(message: types.Message):
    await message.answer(
        "👋 Привет, товарищ! Я найду биржевой сленг в любом предложенном тексте."
        " Чтобы начать, просто отправь его мне 😏.\n"
        "ℹ Или же отправь мне любой термин и я объясню его"
    )

@dp.message_handler()
async def echo(message: types.Message):
    await bot.send_chat_action(message.chat.id, "typing")
    text = re.split(r"\s", message.text.strip())
    definition, key = await get_term_definition(remove_punctuation(message.text.strip()))
    if definition:
        return (
            await message.answer(f"ℹ {key} – {definition}")
            if definition
            else await message.answer("😢 Определение не найдено")
        )
    result = await predict(message.text)
    if not result['result']['slang']:
        return await message.answer("🙃 Сленг не найден")
    for item in result['result']['highlight']:
        ind, method = item.split("_")
        if ":" in ind:
            s_ind, f_ind = map(int, ind.split(":"))
            # print(text[s_ind:f_ind + 1], result['result']['highlight'][item])
            text[s_ind] = f"{text[s_ind]}"
            text[f_ind] = f"{text[f_ind]}"
        else:
            ind = int(ind)
            # print(text[ind], result['result']['highlight'][item])
            if method == 'ml':
                text[ind] = f"{text[ind]}"
            elif method == 'determined':
                text[ind] = f"{text[ind]}"
    await message.answer(
        f'{" ".join(text)}\n\n'
        f'ℹ Чтоб узнать определение любого слова кликни на термин '
        f'(выделенный таким шрифтом) и отправь мне'
    )

if __name__ == "__main__":
    executor.start_polling(dp, skip_updates=True)


from tqdm import tqdm
from sklearn.metrics import roc_curve
from scipy.optimize import brentq
from scipy.interpolate import interp1d
from torch.nn import functional as F
import os
import yaml
import numpy as np

import torch
import torch.nn as nn
from torch.utils import data
from lcnn import LCNN
import scipy.io as sio
from labelID import REALID,FAKEID,POSID,NEGID
from utils import datatransform, datatransform_plus_balance_classes
from dataset import NumpyDataset

if __name__ == '__main__':
    # load yaml file & set comet_ml config
    _abspath = os.path.abspath(__file__)
    dir_yaml = os.path.splitext(_abspath)[0] + '.yaml'
    with open(dir_yaml, 'r') as f_yaml:
        parser = yaml.load(f_yaml)

    # device setting
    cuda = torch.cuda.is_available()
    device = torch.device('cuda:%s' % parser['gpu_idx'][0] if cuda else 'cpu')
    os.environ['CUDA_VISIBLE_DEVICES'] = str(parser['gpu_idx'][0])

    dev_label=parser['devlabelfile']
    ###!!!!
    dev_wavlist, dev_labellist= datatransform_plus_balance_classes(dev_label, 0)

    # define dataset generators
    devset = NumpyDataset(parser, dev_wavlist, dev_labellist, is_eval=True)
    devset_gen = data.DataLoader(devset,
                                 batch_size=parser['batch_size'],
                                 shuffle=False,
                                 drop_last=True,
                                 num_workers=parser['num_workers'])

    # set save directory
    save_dir = parser['save_dir'] + parser['name'] + '/'
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    if not os.path.exists(save_dir + 'results/'):
        os.makedirs(save_dir + 'results/')
    if not os.path.exists(save_dir + 'models/'):
        os.makedirs(save_dir + 'models/')

    f_params = open(save_dir + 'f_params.txt', 'w')
    for k, v in parser.items():
        print(k, v)
        f_params.write('{}:\t{}\n'.format(k, v))
    f_params.write('LCNN model params\n')

    # define model
    model = LCNN()
    model = model.to(device)

    # set ojbective funtions
    criterion = nn.CrossEntropyLoss()

    # set optimizer
    params = list(model.parameters())
    if parser['optimizer'].lower() == 'sgd':
        optimizer = torch.optim.SGD(params,
                                    lr=parser['lr'],
                                    momentum=parser['opt_mom'],
                                    weight_decay=parser['wd'],
                                    nesterov=bool(parser['nesterov']))

    elif parser['optimizer'].lower() == 'adam':
        optimizer = torch.optim.Adam(params,
                                     lr=parser['lr'],
                                     weight_decay=parser['wd'],
                                     betas=[0.9, 0.98],
                                     eps=1.0e-9,
                                     amsgrad=False)

    ##########################################
    # train/val################################
    ##########################################
    best_eer = 99.
    f_eer = open(save_dir + 'eers.txt', 'a', buffering=1)
    for epoch in tqdm(range(parser['epoch'])):
        f_eer.write('%d ' % epoch)

        # define dataset generators
        x_train, y_train = datatransform_plus_balance_classes(parser['trainlabelfile'], epoch)
        trnset = NumpyDataset(parser, x_train, y_train, is_eval=False)

        trnset_gen = data.DataLoader(trnset,
                                     batch_size=parser['batch_size'],
                                     shuffle=True,
                                     drop_last=True,
                                     num_workers=parser['num_workers'])

        # train phase
        model.train()
        with tqdm(total=len(trnset_gen), ncols=70) as pbar:
            for m_batch, m_label in trnset_gen:
                m_batch, m_label = m_batch.to(device=device,dtype=torch.float), m_label.to(device)

                logits, _ = model(m_batch)
                loss = criterion(logits, m_label)
                optimizer.zero_grad()
                loss.backward()
                model.parameters()
                optimizer.step()

                pbar.set_description('epoch%d:\t loss_ce:%.3f' % (epoch, loss))
                pbar.update(1)

        # validation phase
        model.eval()
        with torch.set_grad_enabled(False):
            with tqdm(total=len(devset_gen), ncols=70) as pbar:
                y_score1 = []  # score for each sample
                y1 = []  # label for each sample
                for m_batch, m_label in devset_gen:
                    m_batch= m_batch.to(device=device, dtype=torch.float)
                    y1.extend(list(m_label))
                    logits1, out1 = model(m_batch)
                    probs = F.softmax(logits1, dim=-1)
                    y_score1.extend([probs[i, FAKEID].item() for i in range(probs.size(0))])

                    pbar.update(1)

            # calculate EER
            f_res = open(save_dir + 'results/epoch%s.txt' % (epoch), 'w')
            for _s, _t in zip(y1, y_score1):
                f_res.write('{score} {target}\n'.format(score=_s, target=_t))
            f_res.close()
            fpr, tpr, thresholds = roc_curve(y1, y_score1, pos_label=POSID)
            eer = brentq(lambda x: 1. - x - interp1d(fpr, tpr)(x), 0., 1.)
            print(eer)

            f_eer.write('%f \n' % eer)

            # record best validation model
            if float(eer) < best_eer:
                print('New best EER: %f' % float(eer))
                best_eer = float(eer)
                dir_best_model_weights = save_dir + 'models/%d-%.6f.h5' % (epoch, eer)

                # save best model
                torch.save(model.state_dict(), save_dir + 'models/best.pt')
                print('-----save---')

            if not bool(parser['save_best_only']):
                # save model
                torch.save(model.state_dict(), save_dir + 'models/%d-%.6f.pt' % (epoch, eer))

    f_eer.close()


from django.core.management.base import BaseCommand, CommandError
from idlebook.book.models import Book
from idlebook.book.amazon import get_by_isbn

from django.db import connection, transaction

class Command(BaseCommand):
    args = '<None>'
    help = 'Update book format'

    def handle(self, *args, **options):
        books = Book.objects.all()
        counter = 0
        cursor = connection.cursor()

        for book in books:
            counter += 1
            # Data retrieval operation - no commit required
            cursor.execute("SELECT image FROM book_book WHERE id = %s", [book.id])
            image = cursor.fetchone()[0]

            if image and not image.startswith('books/'):
                new_image = 'books/' + image
                cursor.execute("UPDATE book_book SET image = %s WHERE id = %s", [new_image, book.id])
                transaction.commit_unless_managed()
            #    print new_image


#!/usr/bin/env python
# -*- coding: utf-8 -*- #
from __future__ import unicode_literals
from pilkit.processors import *

AUTHOR = u'Tim Krämer'
SITENAME = u'Tim Krämer - von Hamburg nach Boston'
SITEDESCRIPTION = u'Weblog von Tim Krämer. Berichte und Photos aus Boston und Hamburg'
SITEURL = ''
RELATIVE_URLS = False
TYPOGRIFY = True

TIMEZONE = 'America/New_York'

DEFAULT_LANG = u'de'

THEME = 'themes/SoMA'
PATH = 'content'
PLUGINS=['plugins.share_post', 'plugins.gallery', 'plugins.thumbnailer', 'pelican_youtube']

RESIZE = [
    ('gallery', '_thumbs', [SmartResize(180, 180)]), # Path within images to resize, Thumbnail folder suffix (Empty to overwrite), pilkit settings
]

SITEMAP = {
    'format': 'xml',
    'priorities': {
        'articles': 0.5,
        'indexes': 0.5,
        'pages': 0.5
    },
    'changefreqs': {
        'articles': 'monthly',
        'indexes': 'daily',
        'pages': 'monthly'
    }
}

AUTHORS_SAVE_AS = ''
TAGS_SAVE_AS = ''

# Feed generation is usually not desired when developing
FEED_ALL_ATOM = None
CATEGORY_FEED_ATOM = None
TRANSLATION_FEED_ATOM = None


from room.room import Room
from item.item import Item
from characters.character import Character
from handlers.characterhandler import CharacterHandler

class RoomHandler:

    rooms = []

    def __init__(self, item_handler, character_handler):
        self.characters = character_handler
        self.items = item_handler

    def setup(self):
        #Create rooms

        home = Room("House", '''a home where \
                you can get 12 units of \
                energy without spending money. However, \
                you can enter home only in the evening to \
                have a rest and leaving will be a NEW DAY.\
                You have to use Pizza or Brasserie services\
                before coming house every day''', {}, [self.characters.get_player()], [])
        yard = Room("Yard", "courtyard with a fountain in the center.", {}, [self.characters.get_character("Rapunzel")],[])
        market = Room("Market", "a place where you can buy TV, laptop, mobilephone, car and villa TO WIN THE GAME.", {}, [self.characters.get_character("Rocky")], [self.items.get_item("TV"), self.items.get_item("Laptop"), self.items.get_item("MobilePhone"), self.items.get_item("Car"), self.items.get_item("Villa")])
        university = Room("University", "You can increase your monthly salary up to 100% by using university services.", {}, [self.characters.get_character("Morgana")], [self.items.get_item("Knowledge")])
        sidewalk = Room("Sidewalk", "the sidewalk leading to the office and cityMall.", {}, [self.characters.get_character("Rapunzel")],[])
        office = Room("Office", "the only place where you can earn money.",{}, [self.characters.get_character("Bob")],[self.items.get_item("Work")])
        hall = Room("Hall", "Entrance part of CityMall on the groundfloor. From here you can access various eateries or go to the first floor.", {}, [self.characters.get_character("Bob"), self.characters.get_character("Lofty")],[])
        pizzeria = Room("Pizzeria", "You can eat different pizzas here.", {}, [self.characters.get_character("Rapunzel")],[self.items.get_item("Pizza")])
        brasserie = Room("Brasserie", "a French style restaurant.", {}, [self.characters.get_character("Rocky")], [self.items.get_item("Meal")])
        uphall = Room("Uphall", "first floor hall of CityMall. You can access to shop and pharmacy.", {}, [self.characters.get_character("Morgana")], [])
        pharm = Room("Pharmacy ", "It is recommended to take no more than 1 vitamin per day to get more energy.", {}, [], [self.items.get_item("Vitamin")])
        shop = Room("Shop", "a store where you can buy clothes and shoes. You will need to buy clothes after 7 days and shoes after 25 days.", {}, [], [self.items.get_item("Clothes"), self.items.get_item("Shoes")])

        #Create exits
        home.set_exits({"north" : None, "east" : None, "south" : None, "west" : yard})
        yard.set_exits({"north" : sidewalk, "east" : home, "south" : market, "west" : university})
        university.set_exits({"north" : None, "east" : yard, "south" : None, "west" : None})
        market.set_exits({"north" : yard, "east" : None, "south" : None, "west" : None})
        sidewalk.set_exits({"north" : hall, "east" : None, "south" : yard, "west" : office})
        office.set_exits({"north" : None, "east" : sidewalk, "south" : None, "west" : None})
        hall.set_exits({"upstairs" : uphall , "east" : pizzeria, "south" : sidewalk, "west" : brasserie})
        pizzeria.set_exits({"north" : None, "east" : None, "south" : None, "west" : hall})
        brasserie.set_exits({"north" : None, "east" : hall, "south" : None, "west" : None})
        uphall.set_exits({"downstairs" : hall, "east" : shop, "south" : None, "west" : pharm})
        pharm.set_exits({"north" : None, "east" : uphall, "south" : None, "west" : None})
        shop.set_exits({"north" : None, "east" : None, "south" : None, "west" : uphall})

        self.characters.get_player().set_room(home)
        self.characters.get_character("Bob").set_room(university)

        self.characters.get_character("Lofty").set_room(home)
        self.characters.get_character("Rapunzel").set_room(shop)
        self.characters.get_character("Rocky").set_room(pizzeria)
        self.characters.get_character("Morgana").set_room(hall)
        self.characters.get_character("James").set_room(pharm)

    def get_rooms(self):
        return self.rooms

    def get_room(self, name):
        for room in self.rooms:
            if room.name == name:
                return room

    def get_character_handler(self):
        return self.characters


"""Created by sgoswami on 6/9/17."""

class Solution(object):
    def solveNQueens(self, n):
        """
        :type n: int
        :rtype: List[List[str]]
        """
        result = []
        grid = [["." for _ in range(n)] for _ in range(n)]
        self.helper(n, 0, grid, result)
        return result

    def helper(self, n, row, grid, result):
        if n == row:
            g = grid[:]
            result.append(["".join(item) for item in g])
            return
        for col in range(n):
            if self.is_safe(row, col, grid):
                grid[row][col] = "Q"
                self.helper(n, row + 1, grid, result)
                grid[row][col] = "."

    def is_safe(self, row, col, board):
        for i in range(len(board)):
            if board[row][i] == "Q" or board[i][col] == "Q":
                return False
        i = 0
        while row - i >= 0 and col - i >= 0:
            if board[row - i][col - i] == "Q":
                return False
            i += 1
        i = 0
        while row + i < len(board) and col + i < len(board):
            if board[row + i][col - i] == "Q":
                return False
            i += 1
        i = 1
        while row + i < len(board) and col - i >= 0:
            if board[row + i][col - i] == "Q":
                return False
            i += 1
        i = 1
        while row - i >= 0 and col + i < len(board):
            if board[row - i][col + i] == "Q":
                return False
            i += 1
        return True

if __name__ == "__main__":
    solution = Solution()
    print(solution.solveNQueens(4))


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

from __future__ import absolute_import, division, print_function, unicode_literals

import logging
import shlex
import subprocess
import traceback

from asv import util
from asv.commands import common_args
from asv.commands.run import Run
from asv.console import log

def _do_build(args):
    env, conf, repo, commit_hash = args
    try:
        with log.set_level(logging.WARN):
            env.install_project(conf, repo, commit_hash)
    except util.ProcessError:
        return (env.name, False)
    return (env.name, True)

def _do_build_multiprocess(args):
    """
    multiprocessing callback to build the project in one particular
    environment.
    """
    try:
        return _do_build(args)
    except BaseException as exc:
        raise util.ParallelFailure(str(exc), exc.__class__, traceback.format_exc())

class Latest(Run):
    @classmethod
    def setup_arguments(cls, subparsers):
        parser = subparsers.add_parser(
            "latest",
            help="Run a benchmark suite on the HEAD commit",
            description="Run a benchmark suite.",
        )

        common_args.add_bench(parser)
        parser.add_argument(
            "--profile",
            "-p",
            action="store_true",
            help="""In addition to timing, run the benchmarks through
            the `cProfile` profiler and store the results.""",
        )
        common_args.add_parallel(parser)
        common_args.add_show_stderr(parser)
        parser.add_argument(
            "--quick",
            "-q",
            action="store_true",
            help="""Do a "quick" run, where each benchmark function is
            run only once.  This is useful to find basic errors in the
            benchmark functions faster.  The results are unlikely to
            be useful, and thus are not saved.""",
        )
        common_args.add_environment(parser)
        parser.add_argument(
            "--set-commit-hash",
            default=None,
            help="""Set the commit hash to use when recording benchmark
            results. This makes results to be saved also when using an
            existing environment.""",
        )
        common_args.add_launch_method(parser)
        parser.add_argument(
            "--dry-run",
            "-n",
            action="store_true",
            default=None,
            help="""Do not save any results to disk.""",
        )
        common_args.add_machine(parser)
        parser.add_argument(
            "--skip-existing-successful",
            action="store_true",
            help="""Skip running benchmarks that have previous successful
            results""",
        )
        parser.add_argument(
            "--skip-existing-failed",
            action="store_true",
            help="""Skip running benchmarks that have previous failed
            results""",
        )
        parser.add_argument(
            "--skip-existing-commits",
            action="store_true",
            help="""Skip running benchmarks for commits that have existing
            results""",
        )
        parser.add_argument(
            "--skip-existing",
            "-k",
            action="store_true",
            help="""Skip running benchmarks that have previous successful
            or failed results""",
        )
        parser.add_argument(
            "--interleave-processes",
            action="store_true",
            default=False,
            help="""Interleave benchmarks with multiple processes across
            commits. This can avoid measurement biases from commit ordering,
            can take longer.""",
        )
        parser.add_argument(
            "--no-interleave-processes",
            action="store_false",
            dest="interleave_processes",
        )
        parser.add_argument(
            "--no-pull", action="store_true", help="Do not pull the repository"
        )

        parser.set_defaults(func=cls.run_from_args)

        return parser

    @classmethod
    def run_from_conf_args(cls, conf, args, **kwargs):
        return cls.run(
            conf=conf,
            range_spec="HEAD^!",
            steps=None,
            bench=args.bench,
            attribute=args.attribute,
            parallel=args.parallel,
            show_stderr=args.show_stderr,
            quick=args.quick,
            profile=args.profile,
            env_spec=args.env_spec,
            set_commit_hash=args.set_commit_hash,
            dry_run=args.dry_run,
            machine=args.machine,
            skip_successful=args.skip_existing_successful or args.skip_existing,
            skip_failed=args.skip_existing_failed or args.skip_existing,
            skip_existing_commits=args.skip_existing_commits,
            record_samples=True,
            append_samples=True,
            pull=not args.no_pull,
            interleave_processes=args.interleave_processes,
            launch_method=args.launch_method,
            **kwargs
        )

class Batch(Run):
    @classmethod
    def setup_arguments(cls, subparsers):
        parser = subparsers.add_parser(
            "batch",
            help="Run a set of benchmark suites based on a batch file. "
            "Simply give the file name, which should be a text file "
            "containing a number of activitysim benchmark commands.",
            description="Run a set of benchmark suites based on a batch file.",
        )

        parser.add_argument(
            "file",
            action="store",
            type=str,
            help="""Set the file name to use for reading multiple commands.""",
        )

        parser.set_defaults(func=cls.run_from_args)

        return parser

    @classmethod
    def run_from_conf_args(cls, conf, args, **kwargs):
        with open(args.file, "rt") as f:
            for line in f.readlines():
                subprocess.run(["activitysim", "benchmark", *shlex.split(line)])


from django.urls import path,include
from . import views

urlpatterns = [
    path('',views.index,name='index'),
    path('login/', views.signin, name='login'),
    path('register/', views.signup, name='register'),
    path('logout/',views.logout_view,name='logout'),
    path('add/',views.add_income,name='add-income'),
    path('edit/',views.get_income,name='edit-income'),
    path('update/',views.update_income,name='update-income'),
    path('delete/',views.delete_income,name='delete-income'),
    path('plots/',views.get_plots,name='get-plots'),
]