import numpy as np
import matplotlib.pyplot as plt
import csv

filename = "OUTPUT_5.csv"

def plot():
    reader = csv.reader(open(filename, 'rb'), delimiter = ',')

    times = []
    actuals1 = []
    setpoints1 = []
    actuals2 = []
    setpoints2 = []
    actuals3 = []
    setpoints3 = []
    actuals4 = []
    setpoints4 = []
    actuals5 = []
    setpoints5 = []
    actuals6 = []
    setpoints6 = []

    for row in reader:
        times.append(float(row[0]))
        actuals1.append(float(row[1]))
        setpoints1.append(float(row[2]))
        actuals2.append(float(row[3]))
        setpoints2.append(float(row[4]))
        actuals3.append(float(row[5]))
        setpoints3.append(float(row[6]))
        actuals4.append(float(row[7]))
        setpoints4.append(float(row[8]))
        actuals5.append(float(row[9]))
        setpoints5.append(float(row[10]))
        actuals6.append(float(row[11]))
        setpoints6.append(float(row[12]))

    times = np.array(times)
    actuals1 = np.array(actuals1)
    actuals2 = np.array(actuals2)
    actuals3 = np.array(actuals3)
    actuals4 = np.array(actuals4)
    actuals5 = np.array(actuals5)
    actuals6 = np.array(actuals6)
    setpoints1 = np.array(setpoints1)
    setpoints2 = np.array(setpoints2)
    setpoints3 = np.array(setpoints3)
    setpoints4 = np.array(setpoints4)
    setpoints5 = np.array(setpoints5)
    setpoints6 = np.array(setpoints6)

    plt.plot(times, actuals1, 'r-')
    plt.plot(times, setpoints1, 'r--')

    plt.plot(times, actuals2, 'g-')
    plt.plot(times, setpoints2, 'g--')

    plt.plot(times, actuals3, 'b-')
    plt.plot(times, setpoints3, 'b--')

    plt.plot(times, actuals4, 'c-')
    plt.plot(times, setpoints4, 'c--')

    plt.plot(times, actuals5, 'm-')
    plt.plot(times, setpoints5, 'm--')

    plt.plot(times, actuals6, 'y-')
    plt.plot(times, setpoints6, 'y--')

    plt.xlabel("time (s)")
    plt.ylabel("angle (rad)")
    plt.show()

if __name__ == "__main__":
    plot()


import json
import string
import urllib
import sys
import time
import random
import traceback
import urllib.parse
import urllib.request

def make_req(data):
    url = 'http://127.0.0.1:80'
    data = json.dumps(data)
    data = data.encode('utf8')  # data should be bytes
    req = urllib.request.Request(url, data)
    with urllib.request.urlopen(req) as response:
        response_data = response.read()
    return response_data

def send_message(user_name,gid, message_text):
    req = {"messageGuid":"null","userName": user_name, "groupID":gid, "messageContent":message_text, "messageType":"1", "msgDate": int(time.time()*1000) }
    return make_req(req)

def get_messages():
    req = { "messageType": "2" }
    return make_req(req)

def random_string(N):
    return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))

def send_messages_routine():
    user_name = random_string(7)
    gid = random_string(3)
    while True:
        try:
            print('response: ',json.loads(send_message(user_name,gid,random_string(random.randint(1,10)))))
        except Exception as e:
            print(traceback.print_exc())
            exit()
        time.sleep(random.randint(1,10))
def get_messages_routine():
    while True:
        try:
            print('response: ', json.loads(get_messages()))
        except Exception as e:
            print(traceback.print_exc())
            exit()
        time.sleep(random.randint(1, 10))

print(sys.argv[1])
if sys.argv[1]=='send':
    send_messages_routine()
elif sys.argv[1]=='get':
    get_messages_routine()
else:
    print('first argument must be "send" or "get"')
    exit()


import re
import requests_mock

DOWNSTREAM_BUSINESS_HOST = "http://datadeck-business:9081/api/v1"
DOWNSTREAM_V2_HOST = "https://middlev2.datadeck.com"

resp_datasources = [
    {'id': '423749823749', 'code': 'googleanalytics-v4',
     'name': 'googleanalytics-v4', 'description': 'googleanalytics-v4',
     'api_version': 'v4', 'published': True, 'ds_id': 1001, 'type': 'api'},
    {'id': '423749823750', 'code': 'redshift',
     'name': 'redshift', 'description': 'redshift',
     'api_version': 'v4', 'published': True, 'ds_id': 1002, 'type': 'db'},

]

@requests_mock.Mocker(kw='mock')
def test_fields(test_client, **kwargs):
    ds_connection_id = "028b02e2-3d4c-4077-89b6-30dd2701b80b"
    business_connection_id = "bc123"

    matcher0 = re.compile(DOWNSTREAM_V2_HOST + "/api/v2/users/signinValidate")

    # mock token check
    kwargs['mock'].get(matcher0,
                       json={"success": True})
    # mock datasources api
    kwargs['mock'].get(f"{DOWNSTREAM_BUSINESS_HOST}/datasources",
                       json=resp_datasources)

    kwargs['mock'].get(f"{DOWNSTREAM_V2_HOST}/api/v1/connections/config/{ds_connection_id}",
                       json={
                           "success": True, "message": None, "stackTrace": None,
                           "data": {
                               "dsConnectionId": "40382bb1-e5e1-4979-bc65-d47623d6c824",
                               "config": [
                                   {"id": 'bc123',
                                    "name": "bingxing.kang@ptmind.com",
                                    "type": "account", "index": 0},

                                   {
                                       "index": 1,
                                       "requestParams":
                                           {"id": "profile",
                                            "params": {
                                                "account.name": "bingxing.kang@ptmind.com",
                                                "account.id": "2b319713-ec09-4f57-a3ba-c0b01050df76"
                                            },
                                            "provider": "googleanalysis"
                                            },
                                       "type": "profile",
                                       "id": '["account1", "profile1"]',
                                       "name": "全部网站数据"
                                   }
                               ],
                               "dsId": 1001,
                               "uid": None,
                               "spaceId": "c9d1953a-6c97-4288-8ec2-208d2c236b76"
                           },
                           "errorCode": None,
                           "params": None
                       })

    kwargs['mock'].post(f"{DOWNSTREAM_BUSINESS_HOST}/connections/bc123/fields", json=[
        {
            "id": "folder1",
            "name": "First Folder",
            "children": [
                {
                    "id": "field1",
                    "name": "First Field",
                    "type": "STRING",
                    "allowFilter": True,
                    "filterOps": [
                        "in_list",
                        "not_in_list",
                        "str_contain",
                        "str_not_contain"
                    ]
                },
                {
                    "id": "field2",
                    "name": "Second Field",
                    "type": "NUMBER",
                    "allowAggregation": True,
                    "aggOps": [
                        "min",
                        "max",
                        "sum",
                        "average"
                    ]
                }
            ]
        }
    ])

    kwargs['mock'].post(f"{DOWNSTREAM_BUSINESS_HOST}/connections/bc123/segments", json=[
        {
            "id": "seg1234",
            "name": "New Users"
        }
    ])

    resp = test_client.get(f'/api/v1/datasources/{ds_connection_id}/fields', headers={
        "Token": "bingxing.kang@ptmind.com:eeb30f01-4434-4e61-b94a-1ce67c2e2502:prod",
        "UID": "1001592",
        "SpaceId": "c7ecdc29-bcd9-404c-80b6-a1a32329b93e",
        "TraceId": "1234567"
    })

    resp2 = test_client.get(f'/api/v1/spaces/12312313/datasources/{ds_connection_id}/segments', headers={
        "Token": "bingxing.kang@ptmind.com:eeb30f01-4434-4e61-b94a-1ce67c2e2502:prod",
        "UID": "1001592",
        "SpaceId": "c7ecdc29-bcd9-404c-80b6-a1a32329b93e",
        "TraceId": "1234567"
    })

    assert resp.status_code == 200
    j = resp.json
    assert j['success']
    assert j['data']['child'][0]['child'][0]['name'] == 'First Field'

    assert resp2.status_code == 200
    j2 = resp2.json
    assert len(j2['data'][0]) == 3, 'fields in segment not right, just 3 name-id-type'


# Databricks notebook source
def file_exists(path):
  """Generic function to check file path exists or not

  Args:
    path : dbfs or mount location

  Returns:
    Boolean True or False

  Raises:
    None
  """
  try:
    dbutils.fs.ls(path)
    return True
  except Exception as e:
    if 'java.io.FileNotFoundException' in str(e):
      return False
    else:
      raise

# COMMAND ----------

def log_trace_to_db_console(msg):
  """Generic function log info in Databricks console

  Args:
    msg : message to print

  Returns:
    Na

  Raises:
    None
  """
  print(msg)


from django.urls import path
from . import views

urlpatterns=[
    path("",views.index),
    path("/onCharPress",views.onCharPress),
    path("/onFilePress",views.onFilePress),
    path("/after_fileInput",views.after_fileInput),
    path("/after_fileInput_updt",views.after_fileInput_updt),
    path("/after_charInput",views.after_charInput),
    path("/after_charInput_updt",views.after_charInput_updt),
    path("/after_updt",views.after_updt),
    path("/show_updates",views.show_updt),

]


# External imports
import pygame

# Internal imports
from settings import Settings

# Static objects
settings = Settings.getInstance()

class BackGround(pygame.sprite.Sprite):
    """Represents and draws the background"""
    def __init__(self):
        """Inits attributes and calls parent constructor"""
        super().__init__()
        self.image = pygame.image.load(settings.background).convert_alpha()
        self.rect = self.image.get_rect()

    def draw(self, screen):
        """Draws background to screen"""
        screen.blit(self.image, self.rect)


# coding=utf-8
from PIL import Image
from util.ShowapiRequest import ShowapiRequest
from page.base_page import BasePage
import os
import pytesseract
class GetCode(BasePage):
    def __init__(self,driver,node,configPath,imgFileName=os.getcwd()+"/screenshot/code.png"):
        self.driver=driver
        self.imgFileName=imgFileName
        super().__init__(driver,node=node,configPath=configPath)
    def __get_code_image(self,key):
        self.driver.save_screenshot(self.imgFileName)
        code_el = self.find_element(key)
        left = code_el.location['x']
        top = code_el.location['y']
        right = left+code_el.size['width']
        bottom = top+code_el.size['height']
        image = Image.open(self.imgFileName).crop((left, top, right, bottom))
        image.save(self.imgFileName)

    # 获取验证码文本
    def get_code_text(self,key):
        self.__get_code_image(key)
        r = ShowapiRequest("http://route.showapi.com/184-4",
                           "62626", "d61950be50dc4dbd9969f741b8e730f5")
        r.addBodyPara("typeId", "35")
        r.addBodyPara("convert_to_jpg", "0")
        r.addBodyPara("needMorePrecise", "0")
        r.addFilePara("image", self.imgFileName)  # 文件上传时设置
        res = r.post()
        return res.json()['showapi_res_body']['Result']  # 返回信息
    def get_code_text_free(self,key):
        self.__get_code_image(key)
        codeimg=Image.open(self.imgFileName)
        return pytesseract.image_to_string(codeimg)


#This program prompts the user to enter 100 integers of either 1 or 0 and then converts these into a picture in a 10x10 grid.
#1 represents a filled or on pixel and 0 represents a blank or off pixel.

#Prompt user to input 10 10 digit binary integers made up of 1s and 0s.  Each 10 digit intger is made into a list and then inserted into an overall list.
#The final result is a list of 10 lists, each containing 10 integers of 1 or 0.
def read():
    data = []
    count = 1

    for line in range(10):
        tempString = input(f"Please enter your 10 numbers for row {count}: ")
        data.append(list(tempString))
        count += 1

    return data

#Nested loop to change contents of data structure.  1s become asterisks and 0s become empty strings.
def convert(data):
    for line in data:
        index = 0
        for item in line:
            if item == '0':
                line[index] = ' '
            else:
                line[index] = '*'
            index += 1

#Function to accept an integer to scale image by
def scale():
    return int(input("Scale? (Any whole number greater than 0): "))

#Print out picture in a 10 x 10 grid
def display(data, scale):
    for line in data:
        for item in line:
            print(item * scale, end = " ")
        print()
        if scale > 1:
            numLines = 1
            while numLines != scale:
                for item in line:
                    print(item * scale, end = " ")
                print()
                numLines += 1

#Invert data so the picture will be upside down.
def invert(data):
    invertedData = []

    for line in reversed(data):
        invertedData.append(line)

    return invertedData

#Actual program run

#Print statements
print('Hello, welcome to Icon Processing.')
print()
print('Please have your icon ready.  You will be asked to enter your 100 1s and 0s in groups of 10.')
print('\t"1s" will be interpreted as a filled pixel.')
print('\t"0s" will be interpreted as a blank pixel.')
input("Press ENTER when you are ready to continue.")
print()
print('Remember: This program requires you enter your numbers without any spaces, tabs, or commas')

#Functions
data = read()
convert(data)

scale = scale()

inverted = input("Invert? (Y/N): ")

if inverted.upper() == 'Y':
    invertedData = invert(data)
    display(invertedData, scale)
else:
    display(data, scale)


import json
import re
import random

testFile = open("isbndb-data.json")
data = json.load(testFile)

l = []
with open("data.txt", "w") as file:
    for i in range(len(data['data'])):
        # print i
        isbn13 = data['data'][i]['isbn13']
        isbn10 = data['data'][i]['isbn10']
        title = data['data'][i]['title']

        try:
            author = data['data'][i]['author_data'][0]['name']
        except:
            author = 'John Doe'
        publisher = data['data'][i]['publisher_text']
        try:
            year = int(re.search(r'\d+', data['data'][i]['edition_info']).group())

        except:
            try:
                year = int(re.search(r'\d+', data['data'][i]['publisher_text']).group())
            except:
                year = random.randint(1970,2014)
        num_copies = 1
        price = round(random.uniform(20.0, 80.0), 2)
        try:
            book_format = re.search(r'Hardcover', data['data'][i]['edition_info']).group()
        except:
            book_format = 'Paperback'
        keyword = (data['data'][i]['summary'][:250] + '...') if len(data['data'][i]['summary'])> 250 else data['data'][i]['summary']
        try:
            book_subject = data['data'][i]['subject_ids'][0]
        except:
            book_subject = 'General'

        statement = "INSERT INTO book VALUES ('" + str(isbn13)+ "','" +str(isbn10) + "','" + title + "','" + author + "','" + publisher + "'," + str(year) + "," + str(num_copies) + "," +  str(price) + ",'" +  keyword + "','" + book_subject + "','" +  book_format + "');"
        # print(statement)
        file.write(statement)
        file.write("\n")


from pydm import Display
from PyQt5.QtGui import QStandardItem
from PyQt5.QtWidgets import (QWidgetItem, QCheckBox, QPushButton, QLineEdit,
                             QGroupBox, QHBoxLayout, QMessageBox, QWidget,
                             QLabel, QFrame, QComboBox, QRadioButton)
from os import path
import subprocess
import sys
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure
import FFt_math

lastPath=''

class MplCanvas(FigureCanvasQTAgg):
# MPLCanvas is the class for the 'canvas' that plots are drawn on and then mapped to the ui
# They are Figure format described in matplotlib 2.2 documentation

    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi, tight_layout="true")
        self.axes = fig.add_subplot(111)
        super(MplCanvas, self).__init__(fig)

class MicDisp(Display):

    def __init__(self, parent=None, args=None, ui_filename="FFT_test2.ui"):
        super(MicDisp, self).__init__(parent=parent, args=args, ui_filename=ui_filename)
        self.pathHere = path.dirname(sys.modules[self.__module__].__file__)
        def getPath(fileName):
            return path.join(self.pathHere, fileName)
        self.show()
        # brings the display to the front
        self.raise_()
        # gives the display focus
        self.activateWindow()
        self.ui.AcqProg.setText("Test of printing output from subprocess  line by line")

        self.ui.StrtBut.clicked.connect(self.subCall)  # call function setGOVal when strtBut is pressed
        self.ui.AcqProg.setText("Select 1 CM and 1 cavity at a time for commissioning. \nLimit plotted waveforms to 30 sec.")

    def subCall(self):
        try:
            cmd=['python','-u','/home/user/tutorial/GitDir/Microphonics/Testy2.py']
            process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1, shell=False)
            while True:
                output = process.stdout.readline().decode()
                if output == '' and process.poll() is not None:
                    break
                if output !='':
                    print(output)
                    self.ui.AcqProg.setText(output)

            return_code = process.poll()
            if return_code==0:
        #                self.ui.AcqProg.setText("Test run completed successfully")
                print("Test run completed sucessfully")

            if return_code !=0:
        #        print(err)
                e = subprocess.CalledProcessError(return_code, cmd)
        #                    e.stdout, e.stderr = err
        #                self.ui.AcqProg.setText("Call to microphonics script failed \n"+str(e))
                print("Call returned non-zero value")

        except:
#            self.ui.AcqProg.setText("try failed \n")
            print("try failed")


"""empty message

Revision ID: 39c3c7d31751
Revises: a1d616ded8b1
Create Date: 2018-06-28 19:13:34.960297

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = '39c3c7d31751'
down_revision = 'a1d616ded8b1'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('cm_user', 'cookies_vt',
               existing_type=mysql.INTEGER(display_width=11),
               nullable=False)
    op.alter_column('cm_user', 'ip_proxy_vt',
               existing_type=mysql.INTEGER(display_width=11),
               nullable=False)
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('cm_user', 'ip_proxy_vt',
               existing_type=mysql.INTEGER(display_width=11),
               nullable=True)
    op.alter_column('cm_user', 'cookies_vt',
               existing_type=mysql.INTEGER(display_width=11),
               nullable=True)
    # ### end Alembic commands ###


import threading
import rpdb

#debugger = rpdb.Rpdb(port=4444)
rpdb.Rpdb().set_trace()

def my_func(thread_number):
    return print('my_func called by thread N°{}'.format(thread_number))

def main():
    threads = []
    for i in range(10):
        t = threading.Thread(target=my_func, args=(i,))
        threads.append(t)
        t.start()
        t.join()

if __name__ == "__main__":

   main()

Chapter09/codes chapter 9 debug and test/rpdb_code_example.py


# import logging
import unittest

# from models import *
from nova import exception
import api as db
# from oslo_serialization import jsonutils
# from nova.tests import uuidsentinel
# from nova import test
from utils import clean_rome_all_databases
# from nova.tests.unit import matchers
# import mock
# from oslo_db import exception as db_exc
#
# from nova.objects import fields
# import copy
import context as rome_context
from nova.objects import fields
from oslo_serialization import jsonutils
from nova.tests import uuidsentinel
import datetime
from oslo_utils import timeutils
from nova import quota
import api as sqlalchemy_api

def init_mock_objects(models_module):
    models_module.drop_tables()
    models_module.create_tables()
    models_module.init_objects()

def _make_compute_node(host, node, hv_type, service_id):
    compute_node_dict = dict(vcpus=2, memory_mb=1024, local_gb=2048,
                        uuid=uuidsentinel.fake_compute_node,
                        vcpus_used=0, memory_mb_used=0,
                        local_gb_used=0, free_ram_mb=1024,
                        free_disk_gb=2048, hypervisor_type=hv_type,
                        hypervisor_version=1, cpu_info="",
                        running_vms=0, current_workload=0,
                        service_id=service_id,
                        host=host,
                        disk_available_least=100,
                        hypervisor_hostname=node,
                        host_ip='127.0.0.1',
                        supported_instances='',
                        pci_stats='',
                        metrics='',
                        extra_resources='',
                        cpu_allocation_ratio=16.0,
                        ram_allocation_ratio=1.5,
                        disk_allocation_ratio=1.0,
                        stats='', numa_topology='')
    # add some random stats
    stats = dict(num_instances=3, num_proj_12345=2,
            num_proj_23456=2, num_vm_building=3)
    compute_node_dict['stats'] = jsonutils.dumps(stats)
    return compute_node_dict

def _reservation_get(context, uuid):
    @sqlalchemy_api.pick_context_manager_reader
    def doit(context):
        from models import Reservation
        return sqlalchemy_api.model_query(
            context, Reservation, read_deleted="no").filter_by(
            uuid=uuid).first()

    result = doit(context)
    if not result:
        raise exception.ReservationNotFound(uuid=uuid)

    return result

class ModelsObjectComparatorMixin(object):
    def _dict_from_object(self, obj, ignored_keys):
        if ignored_keys is None:
            ignored_keys = []
        from rome.core.utils import LazyRelationship

        # NOTE(badock): Add this block of code to prevent some tests to fail while the test should be considered as a
        # success
        irrelevant_keys = []
        for k, v in obj.items():
            if type(v) is LazyRelationship:
                irrelevant_keys += [k]

        return {k: v for k, v in obj.items()
                if k not in ignored_keys and k not in irrelevant_keys}

    def _assertEqualObjects(self, obj1, obj2, ignored_keys=None):
        obj1 = self._dict_from_object(obj1, ignored_keys)
        obj2 = self._dict_from_object(obj2, ignored_keys)

        self.assertEqual(len(obj1),
                         len(obj2),
                         "Keys mismatch: %s" %
                          str(set(obj1.keys()) ^ set(obj2.keys())))
        for key, value in obj1.items():
            self.assertEqual(value, obj2[key])

    def _assertEqualListsOfObjects(self, objs1, objs2, ignored_keys=None):
        obj_to_dict = lambda o: self._dict_from_object(o, ignored_keys)
        sort_key = lambda d: [d[k] for k in sorted(d)]
        conv_and_sort = lambda obj: sorted(map(obj_to_dict, obj), key=sort_key)

        self.assertEqual(conv_and_sort(objs1), conv_and_sort(objs2))

    def _assertEqualOrderedListOfObjects(self, objs1, objs2,
                                         ignored_keys=None):
        obj_to_dict = lambda o: self._dict_from_object(o, ignored_keys)
        conv = lambda objs: [obj_to_dict(obj) for obj in objs]

        self.assertEqual(conv(objs1), conv(objs2))

    def _assertEqualListsOfPrimitivesAsSets(self, primitives1, primitives2):
        self.assertEqual(len(primitives1), len(primitives2))
        for primitive in primitives1:
            self.assertIn(primitive, primitives2)

        for primitive in primitives2:
            self.assertIn(primitive, primitives1)

class BaseInstanceTypeTestCase(unittest.TestCase, ModelsObjectComparatorMixin):
    def setUp(self):
        super(BaseInstanceTypeTestCase, self).setUp()
        self.ctxt = rome_context.get_admin_context()
        # self.user_ctxt = context.RequestContext('user', 'user')
        self.user_ctxt = rome_context.get_admin_context()
        clean_rome_all_databases()

    def _get_base_values(self):
        return {
            'name': 'fake_name',
            'memory_mb': 512,
            'vcpus': 1,
            'root_gb': 10,
            'ephemeral_gb': 10,
            'flavorid': 'fake_flavor',
            'swap': 0,
            'rxtx_factor': 0.5,
            'vcpu_weight': 1,
            'disabled': False,
            'is_public': True
        }

    def _create_flavor(self, values, projects=None):
        v = self._get_base_values()
        v.update(values)
        return db.flavor_create(self.ctxt, v, projects)

def _quota_reserve(context, project_id, user_id):
    """Create sample Quota, QuotaUsage and Reservation objects.

    There is no method db.quota_usage_create(), so we have to use
    db.quota_reserve() for creating QuotaUsage objects.

    Returns reservations uuids.

    """
    def get_sync(resource, usage):
        def sync(elevated, project_id, user_id):
            return {resource: usage}
        return sync
    quotas = {}
    user_quotas = {}
    resources = {}
    deltas = {}
    for i in range(3):
        resource = 'resource%d' % i
        if i == 2:
            # test for project level resources
            resource = 'fixed_ips'
            quotas[resource] = db.quota_create(context,
                                               project_id,
                                               resource, i + 2).hard_limit
            user_quotas[resource] = quotas[resource]
        else:
            quotas[resource] = db.quota_create(context,
                                               project_id,
                                               resource, i + 1).hard_limit
            user_quotas[resource] = db.quota_create(context, project_id,
                                                    resource, i + 1,
                                                    user_id=user_id).hard_limit
        sync_name = '_sync_%s' % resource
        resources[resource] = quota.ReservableResource(
            resource, sync_name, 'quota_res_%d' % i)
        deltas[resource] = i
        setattr(sqlalchemy_api, sync_name, get_sync(resource, i))
        sqlalchemy_api.QUOTA_SYNC_FUNCTIONS[sync_name] = getattr(
            sqlalchemy_api, sync_name)
    return db.quota_reserve(context, resources, quotas, user_quotas, deltas,
                    timeutils.utcnow(), 1000,
                    datetime.timedelta(days=1), project_id, user_id)

class QuotaTestCase(unittest.TestCase, ModelsObjectComparatorMixin):

    """Tests for db.api.quota_* methods."""

    def setUp(self):
        super(QuotaTestCase, self).setUp()
        self.ctxt = rome_context.get_admin_context()
        clean_rome_all_databases()

    def test_quota_create(self):
        quota = db.quota_create(self.ctxt, 'project1', 'resource', 99)
        self.assertEqual(quota.resource, 'resource')
        self.assertEqual(quota.hard_limit, 99)
        self.assertEqual(quota.project_id, 'project1')

    def test_quota_get(self):
        quota = db.quota_create(self.ctxt, 'project1', 'resource', 99)
        quota_db = db.quota_get(self.ctxt, 'project1', 'resource')
        self._assertEqualObjects(quota, quota_db)

    def test_quota_get_all_by_project(self):
        for i in range(3):
            for j in range(3):
                db.quota_create(self.ctxt, 'proj%d' % i, 'resource%d' % j, j)
        for i in range(3):
            quotas_db = db.quota_get_all_by_project(self.ctxt, 'proj%d' % i)
            self.assertEqual(quotas_db, {'project_id': 'proj%d' % i,
                                                        'resource0': 0,
                                                        'resource1': 1,
                                                        'resource2': 2})

    def test_quota_get_all_by_project_and_user(self):
        for i in range(3):
            for j in range(3):
                db.quota_create(self.ctxt, 'proj%d' % i, 'resource%d' % j,
                                j - 1, user_id='user%d' % i)
        for i in range(3):
            quotas_db = db.quota_get_all_by_project_and_user(self.ctxt,
                                                             'proj%d' % i,
                                                             'user%d' % i)
            self.assertEqual(quotas_db, {'project_id': 'proj%d' % i,
                                         'user_id': 'user%d' % i,
                                                        'resource0': -1,
                                                        'resource1': 0,
                                                        'resource2': 1})

    def test_quota_update(self):
        db.quota_create(self.ctxt, 'project1', 'resource1', 41)
        db.quota_update(self.ctxt, 'project1', 'resource1', 42)
        quota = db.quota_get(self.ctxt, 'project1', 'resource1')
        self.assertEqual(quota.hard_limit, 42)
        self.assertEqual(quota.resource, 'resource1')
        self.assertEqual(quota.project_id, 'project1')

    def test_quota_update_nonexistent(self):
        self.assertRaises(exception.ProjectQuotaNotFound,
            db.quota_update, self.ctxt, 'project1', 'resource1', 42)

    def test_quota_get_nonexistent(self):
        self.assertRaises(exception.ProjectQuotaNotFound,
            db.quota_get, self.ctxt, 'project1', 'resource1')

    def test_quota_reserve_all_resources(self):
        quotas = {}
        deltas = {}
        reservable_resources = {}
        for i, resource in enumerate(quota.resources):
            if isinstance(resource, quota.ReservableResource):
                quotas[resource.name] = db.quota_create(self.ctxt, 'project1',
                                                        resource.name,
                                                        100).hard_limit
                deltas[resource.name] = i
                reservable_resources[resource.name] = resource

        usages = {'instances': 3, 'cores': 6, 'ram': 9}
        instances = []
        for i in range(3):
            instances.append(db.instance_create(self.ctxt,
                             {'vcpus': 2, 'memory_mb': 3,
                             'project_id': 'project1'}))

        usages['fixed_ips'] = 2
        network = db.network_create_safe(self.ctxt, {})
        for i in range(2):
            address = '192.168.0.%d' % i
            db.fixed_ip_create(self.ctxt, {'project_id': 'project1',
                                           'address': address,
                                           'network_id': network['id']})
            db.fixed_ip_associate(self.ctxt, address,
                                  instances[0].uuid, network['id'])

        usages['floating_ips'] = 5
        for i in range(5):
            db.floating_ip_create(self.ctxt, {'project_id': 'project1'})

        usages['security_groups'] = 3
        for i in range(3):
            db.security_group_create(self.ctxt, {'project_id': 'project1'})

        usages['server_groups'] = 4
        for i in range(4):
            db.instance_group_create(self.ctxt, {'uuid': str(i),
                                                 'project_id': 'project1'})

        reservations_uuids = db.quota_reserve(self.ctxt, reservable_resources,
                                              quotas, quotas, deltas, None,
                                              None, None, 'project1')
        resources_names = list(reservable_resources.keys())
        for reservation_uuid in reservations_uuids:
            reservation = _reservation_get(self.ctxt, reservation_uuid)
            usage = db.quota_usage_get(self.ctxt, 'project1',
                                       reservation.resource)
            self.assertEqual(usage.in_use, usages[reservation.resource],
                             'Resource: %s' % reservation.resource)
            self.assertEqual(usage.reserved, deltas[reservation.resource])
            self.assertIn(reservation.resource, resources_names)
            resources_names.remove(reservation.resource)
        self.assertEqual(len(resources_names), 0)

    def test_quota_destroy_all_by_project(self):
        reservations = _quota_reserve(self.ctxt, 'project1', 'user1')
        db.quota_destroy_all_by_project(self.ctxt, 'project1')
        self.assertEqual(db.quota_get_all_by_project(self.ctxt, 'project1'),
                            {'project_id': 'project1'})
        self.assertEqual(db.quota_get_all_by_project_and_user(self.ctxt,
                            'project1', 'user1'),
                            {'project_id': 'project1', 'user_id': 'user1'})
        self.assertEqual(db.quota_usage_get_all_by_project(
                            self.ctxt, 'project1'),
                            {'project_id': 'project1'})
        for r in reservations:
            self.assertRaises(exception.ReservationNotFound,
                            _reservation_get, self.ctxt, r)

    def test_quota_destroy_all_by_project_and_user(self):
        reservations = _quota_reserve(self.ctxt, 'project1', 'user1')
        db.quota_destroy_all_by_project_and_user(self.ctxt, 'project1',
                                                 'user1')
        self.assertEqual(db.quota_get_all_by_project_and_user(self.ctxt,
                            'project1', 'user1'),
                            {'project_id': 'project1',
                             'user_id': 'user1'})
        self.assertEqual(db.quota_usage_get_all_by_project_and_user(
                            self.ctxt, 'project1', 'user1'),
                            {'project_id': 'project1',
                             'user_id': 'user1',
                             'fixed_ips': {'in_use': 2, 'reserved': 2}})
        for r in reservations:
            self.assertRaises(exception.ReservationNotFound,
                            _reservation_get, self.ctxt, r)

    def test_quota_usage_get_nonexistent(self):
        self.assertRaises(exception.QuotaUsageNotFound, db.quota_usage_get,
            self.ctxt, 'p1', 'nonexitent_resource')

    def test_quota_usage_get(self):
        _quota_reserve(self.ctxt, 'p1', 'u1')
        quota_usage = db.quota_usage_get(self.ctxt, 'p1', 'resource0')
        expected = {'resource': 'resource0', 'project_id': 'p1',
                    'in_use': 0, 'reserved': 0, 'total': 0}
        for key, value in expected.items():
            self.assertEqual(value, quota_usage[key])

    def test_quota_usage_get_all_by_project(self):
        _quota_reserve(self.ctxt, 'p1', 'u1')
        expected = {'project_id': 'p1',
                    'resource0': {'in_use': 0, 'reserved': 0},
                    'resource1': {'in_use': 1, 'reserved': 1},
                    'fixed_ips': {'in_use': 2, 'reserved': 2}}
        self.assertEqual(expected, db.quota_usage_get_all_by_project(
                         self.ctxt, 'p1'))

    def test_quota_usage_get_all_by_project_and_user(self):
        _quota_reserve(self.ctxt, 'p1', 'u1')
        expected = {'project_id': 'p1',
                    'user_id': 'u1',
                    'resource0': {'in_use': 0, 'reserved': 0},
                    'resource1': {'in_use': 1, 'reserved': 1},
                    'fixed_ips': {'in_use': 2, 'reserved': 2}}
        self.assertEqual(expected, db.quota_usage_get_all_by_project_and_user(
                         self.ctxt, 'p1', 'u1'))

    # def test_get_project_user_quota_usages_in_order(self):
    #     _quota_reserve(self.ctxt, 'p1', 'u1')
    #
    #     # @sqlalchemy_api.pick_context_manager_reader
    #     # def test(context):
    #     #     with mock.patch.object(query.Query, 'order_by') as order_mock:
    #     #         sqlalchemy_api._get_project_user_quota_usages(
    #     #             context, 'p1', 'u1')
    #     #         self.assertTrue(order_mock.called)
    #
    #     test(self.ctxt)

    def test_quota_usage_update_nonexistent(self):
        self.assertRaises(exception.QuotaUsageNotFound, db.quota_usage_update,
            self.ctxt, 'p1', 'u1', 'resource', in_use=42)

    def test_quota_usage_update(self):
        _quota_reserve(self.ctxt, 'p1', 'u1')
        db.quota_usage_update(self.ctxt, 'p1', 'u1', 'resource0', in_use=42,
                              reserved=43)
        quota_usage = db.quota_usage_get(self.ctxt, 'p1', 'resource0', 'u1')
        expected = {'resource': 'resource0', 'project_id': 'p1',
                    'user_id': 'u1', 'in_use': 42, 'reserved': 43, 'total': 85}
        for key, value in expected.items():
            self.assertEqual(value, quota_usage[key])

    def test_quota_create_exists(self):
        db.quota_create(self.ctxt, 'project1', 'resource1', 41)
        self.assertRaises(exception.QuotaExists, db.quota_create, self.ctxt,
                          'project1', 'resource1', 42)


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

import time
import logging
import logging.config

class UTCFormatter(logging.Formatter):
    converter = time.gmtime

LOGGING = {
    "version": 1,
    "formatters": {
        "utc": {
            "()": UTCFormatter,
            "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
        },
        "local": {"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"},
    },
    "handlers": {
        "console1": {"class": "logging.StreamHandler", "formatter": "utc"},
        "console2": {"class": "logging.StreamHandler", "formatter": "local"},
    },
    "root": {"handlers": ["console1", "console2"]},
}

if __name__ == "__main__":
    # Simple logger
    logger = logging.getLogger(__name__)
    logger.addHandler(logging.FileHandler("./foo.log"))
    logger.setLevel(logging.DEBUG)
    logger.debug("foo debug log")

    logging.config.dictConfig(LOGGING)
    logging.warning("The local time is %s", time.asctime())

# Output
# 2015-11-10 19:25:20,298 - root - WARNING - The local time is Tue Nov 10 11:25:20 2015
# 2015-11-10 11:25:20,298 - root - WARNING - The local time is Tue Nov 10 11:25:20 2015


from utils.LoadStructuralData import load_struct_data
from utils.LoadFunctionalData import load_funct_data
from skrvm import RVR
from sklearn.svm import SVR
from sklearn.linear_model import Ridge
import inspect
import os
import pickle as pkl

cached_data = {}
baseline_result_dir = os.path.join(os.path.dirname(os.path.join(os.path.dirname(__file__))), 'Results',
                                   'Baselines_results')
if not os.path.exists(baseline_result_dir):
    os.makedirs(baseline_result_dir)

# utility class for storing together the hyper-parameters of a baseline model into an object
class ConfigBaselines(object):
    def __init__(self, updated_params=None):
        '''
        Initialize the object embodying the configuration of a baseline model.
        :param updated_params: specific hyper-parameters used by current baseline configuration
        '''
        self.params = {
            # model hyper-parameters
            'name': 'RVM',
            'kernel': 'rbf',
            'epsilon': 0.5,
            'gamma': 0.001,
            'C': 1.0,
            'alpha': 1.0,
            'n_iter': 500,
            'fit_intercept': False,
            'normalize': False,
            # training hyper.
            'load_specific_data': load_struct_data,
            'pers_traits_selection': ['NEO.NEOFAC_A'],
            'functional_dim': 50,
            'scan_session': 1,
            # nested CV hyper.
            'nested_CV_level': 'outer',
            'eval_fold_in': 1,
            'eval_fold_out': 4,
            'k_outer': 5,
            'k_inner': 5}

        # update the default hyper-parameters
        self.update(update_hyper=updated_params)
        if self.params['name'] == 'LR':
            self.params['model'] = Ridge
        elif self.params['name'] == 'SVR':
            self.params['model'] = SVR
        elif self.params['name'] == 'RVM':
            self.params['model'] = RVR
        else:
            raise ValueError('Possible Baselines Models: LR, SVR or RVM ')
        if self.params['nested_CV_level'] not in {'inner', 'outer'}:
            raise ValueError('Possible CV levels: inner, outer')

    def __str__(self):
        '''
         Produces a unique string identifier of the current baseline model.
        :return: str of the name of the model, including the nested CV parameters
        '''
        str_dataset = self.params['name'] + '_' + self.params['load_specific_data'].__name__.split('_')[1]
        str_dim_sess = 'DIM_%d_SESS_%d' % (self.params['functional_dim'], self.params['scan_session'])
        str_traits = 'PT_%s' % ''.join(self.params['pers_traits_selection']).replace('NEO.NEOFAC_', '')
        str_cross_val = 'CV_%d%d%s' % (self.params['eval_fold_in'], self.params['eval_fold_out'], self.params[
            'nested_CV_level'])
        str_params = [str_dataset, str_dim_sess, str_traits]
        # filter just the hyperparameters used by the current baseline when printing its name
        for arg in sorted(list(self.params.keys())):
            if arg in inspect.getfullargspec(self.params['model']).args:
                str_params.append(arg + '_' + str(self.params[arg]))
        str_params.append(str_cross_val)
        if self.params['load_specific_data'] is load_struct_data:
            str_params.remove(str_dim_sess)

        return '_'.join(str_params)

    def get_suitable_args(self):
        '''
         Inspect the signature of the constructor of the baseline class used and extract its formal parameter names.
        :return: dict of hyperparameters and their values specific to the current baseline
        '''
        args = {}
        for arg in sorted(list(self.params.keys())):
            if arg in inspect.getfullargspec(self.params['model']).args:
                args[arg] = self.params[arg]
        return args

    def get_name(self):
        '''
         Get the name of the baseline model discarding the hyper-parameters of the Nested Cross Validation.
        :return: str of the base name of the model
        '''
        import re
        return re.compile(re.escape('_CV') + '.*').sub('', re.sub(r"PT_[A-Z]_", "", str(self)))

    def update(self, update_hyper):
        '''
         Updates the default hyper-parameters of the baseline configuration object
        :param update_hyper: dict of new hyper-parameters
        :return: void, it's changing the internal state of the object
        '''
        if update_hyper is not None:
            self.params.update(update_hyper)

    def load_data(self):
        global cached_data
        loader_data = self.params['load_specific_data']
        trait_choice, = self.params['pers_traits_selection']
        if loader_data in cached_data.keys():
            if trait_choice in cached_data[loader_data].keys():
                return cached_data[loader_data][trait_choice]
            else:
                uncached_data = loader_data(self.params)
                cached_data[loader_data][trait_choice] = uncached_data
                return uncached_data
        else:
            cached_data[loader_data] = {}
            uncached_data = loader_data(self.params)
            cached_data[loader_data][trait_choice] = uncached_data
            return uncached_data

    def results_file(self):
        '''
         Retrieves the path to the results file where the evaluation data: test loss, predictions is/should be saved
        :return: str path
        '''
        return os.path.join(baseline_result_dir, 'predictions_' + str(self))

    def get_results(self):
        '''
         Retrieve the results of the model.
        :return: dict with evaluation results: losses, metrics, predictions
        '''
        results = None
        if os.path.exists(self.results_file()):
            with open(self.results_file(), 'rb') as result_fp:
                results = pkl.load(result_fp)
        return results

    @staticmethod
    def get_sampled_models(baseline_name, **kwargs):
        '''
          Samples a pre-defined number of baseline configurations for the inner CV of the nested CV phase.
        :param baseline_name: str name of the baseline
        :param kwargs: compatibility the the sampling function og GAT hyperparameters
        :return: dict of hyper-parameters choices to be converted to a Grid Search
        '''
        if baseline_name == 'LR':
            search_space = {'name': [baseline_name],
                            'fit_intercept': [False],
                            'normalize': [False],
                            'solver ': ['auto'],
                            'alpha': [10, 1.0, 0.5, 0.1]}
        elif baseline_name == 'SVR':
            search_space = {'name': [baseline_name],
                            'kernel': ['rbf', 'linear', 'poly', 'sigmoid'],
                            'epsilon': [0.1, 0.3, 0.5],
                            'gamma': [1.0, 0.1, 0.001, 0.0001],
                            'C': [1, 10, 100]}
        elif baseline_name == 'RVM':
            search_space = {'name': [baseline_name],
                            'kernel': ['rbf', 'linear'],
                            'n_iter': [10, 50, 100, 200],
                            'alpha': [1e-06, 1e-05]}
        else:
            print('Wrong choice of model name')
            return
        data_type = {'load_specific_data': [load_funct_data, load_struct_data],
                     'pers_traits_selection': [['NEO.NEOFAC_A'], ['NEO.NEOFAC_C'], ['NEO.NEOFAC_E'], ['NEO.NEOFAC_N'],
                                               ['NEO.NEOFAC_O']]}
        return {**search_space, **data_type}


from plone.app.imaging.tests.base import ImagingTestCase
from plone.app.imaging.utils import getAllowedSizes, getQuality
from unittest import defaultTestLoader

class PropertiesTests(ImagingTestCase):

    def testAllowedSizes(self):
        # test the defaults
        # for readability, pep8 is not applied to the dict below
        self.assertEqual(getAllowedSizes(), dict(
            large =   (768, 768),
            preview = (400, 400),
            mini =    (200, 200),
            thumb =   (128, 128),
            tile =    ( 64,  64),
            icon =    ( 32,  32),
            listing = ( 16,  16)))
        # override and test again
        iprops = self.portal.portal_properties.imaging_properties
        iprops.manage_changeProperties(allowed_sizes='foo 23:23')
        self.assertEqual(getAllowedSizes(), dict(foo=(23, 23)))
        # empty lines and white space should be ignored
        iprops.manage_changeProperties(allowed_sizes=['x   23 :23 ', '', ' '])
        self.assertEqual(getAllowedSizes(), dict(x=(23, 23)))
        # however, white space within the name should be fine...
        iprops.manage_changeProperties(allowed_sizes=['foo bar 23:23'])
        self.assertEqual(getAllowedSizes(), dict(foo_bar=(23, 23)))

    def testQuality(self):
        self.assertEqual(getQuality(), 88)
        # change and test again
        iprops = self.portal.portal_properties.imaging_properties
        iprops.manage_changeProperties(quality='42')
        self.assertEqual(getQuality(), 42)

def test_suite():
    return defaultTestLoader.loadTestsFromName(__name__)


#!/usr/bin/env python

# we need to save countvectorizer model, the link below shows an example.
# see: https://stackoverflow.com/questions/48924308/countvectorizer-using-same-vocabulary-in-a-second-time

import json
from pyspark.sql import SQLContext
from pyspark import SparkContext
from pyspark.sql.functions import col
from pyspark.ml.feature import RegexTokenizer, StopWordsRemover, CountVectorizer, CountVectorizerModel
from pyspark.ml.classification import LogisticRegression, LogisticRegressionModel
from pyspark.ml.feature import OneHotEncoder, StringIndexer, VectorAssembler
from pyspark.ml import Pipeline

sc =SparkContext()
sqlContext = SQLContext(sc)

##################################################
################## Prepare data ##################
##################################################
trainDataFile="./data/sanfrancisco-crime/train.csv"
data = sqlContext.read.format('com.databricks.spark.csv').options(header='true', inferschema='true').load(trainDataFile).limit(1000)
print(data.columns)

drop_list = ['Dates', 'DayOfWeek', 'PdDistrict', 'Resolution', 'Address', 'X', 'Y']

data = data.select([column for column in data.columns if column not in drop_list])
data.show(5)
data.printSchema()

##################################################
################## Transofrmers ##################
##################################################

# regular expression tokenizer
regexTokenizer = RegexTokenizer(inputCol="Descript", outputCol="words", pattern="\\W")
# stop words
add_stopwords = ["http","https","amp","rt","t","c","the"] # standard stop words
stopwordsRemover = StopWordsRemover(inputCol="words", outputCol="filtered").setStopWords(add_stopwords)
# bag of words count
countVectors = CountVectorizer(inputCol="filtered", outputCol="features", vocabSize=10000, minDF=5)
label_stringIdx = StringIndexer(inputCol = "Category", outputCol = "label")
transformers=[regexTokenizer, stopwordsRemover, countVectors, label_stringIdx]
pipeline = Pipeline(stages=transformers)
pipelineFit = pipeline.fit(data)
dataset = pipelineFit.transform(data)

### Randomly split data into training and test sets. set seed for reproducibility
(trainingData, testData) = dataset.randomSplit([0.7, 0.3], seed = 100)
print("Training Dataset Count: " + str(trainingData.count()))
print("Test Dataset Count: " + str(testData.count()))

testData.show(5)

results = testData.toJSON().map(lambda j: json.loads(j)).collect()
#print (results)
file = open('/tmp/lr.model.testdata.txt', 'w')
file.write(json.dumps(results))
file.close()

##########################################################
################## Train/load the model ##################
##########################################################
lrModel = LogisticRegressionModel.load("lr.model.savepoint")

predictions = lrModel.transform(testData)

predictions.filter(predictions['prediction'] == 7)  \
    .select("Descript","Category","probability","label","prediction") \
    .orderBy("probability", ascending=False) \
    .show(n = 10, truncate = 30)


import json

from django.core.validators import URLValidator, ValidationError
from django.http import HttpResponse, HttpResponseBadRequest
from django.middleware.csrf import get_token
from django.shortcuts import render_to_response, redirect
from django.template.response import TemplateResponse
from django.views.decorators.cache import never_cache
from django.views.decorators.http import require_POST

from django.contrib import messages

from ..core import decorators as dec
from ..relatedbugs.models import ExternalBug, ExternalBugList
from ..static.status import TestRunStatus, TestCycleStatus
from ..testexecution.models import TestRunIncludedTestCaseList
from ..users.decorators import login_redirect

from .finder import RunTestsFinder
from .models import TestCycleList, TestRunList, TestResultList

@login_redirect
@dec.finder(RunTestsFinder)
def home(request):
    return TemplateResponse(
        request,
        "runtests/home.html",
        {}
        )

@login_redirect
def finder_environments(request, run_id):
    run = TestRunList.get_by_id(run_id, auth=request.auth)

    return TemplateResponse(
        request,
        "runtests/_environment_form.html",
        {"testrun": run,
         })

@never_cache
@login_redirect
@dec.finder(RunTestsFinder)
@dec.paginate("cases")
@dec.sort("cases")
def runtests(request, testrun_id):
    # force the CSRF cookie to be set
    # @@@ replace with ensure_csrf_cookie decorator in Django 1.4
    get_token(request)

    testrun = TestRunList.get_by_id(testrun_id, auth=request.auth)

    if not testrun.environmentgroups_prefetch.match(request.environments):
        return redirect("runtests_environment", testrun_id=testrun_id)

    cycle = testrun.testCycle
    product = cycle.product

    # for prepopulating finder
    cycles = TestCycleList.ours(auth=request.auth).sort("name", "asc").filter(
        product=product, status=TestCycleStatus.ACTIVE)
    runs = TestRunList.ours(auth=request.auth).sort("name", "asc").filter(
        testCycle=cycle, status=TestRunStatus.ACTIVE)

    return TemplateResponse(
        request,
        "runtests/run.html",
        {"product": product,
         "cycle": cycle,
         "testrun": testrun,
         "cases": TestRunIncludedTestCaseList.get(auth=request.auth).filter(
                testRun=testrun),
         "finder": {
                "cycles": cycles,
                "runs": runs,
                },
         })

ACTIONS = {
    "start": [],
    "finishsucceed": [],
    "finishinvalidate": ["comment"],
    "finishfail": ["failedStepNumber", "actualResult"],
    }

@login_redirect
@require_POST
def result(request, result_id):
    result = TestResultList.get_by_id(result_id, auth=request.auth)

    action = request.POST.get("action", None)
    try:
        argnames = ACTIONS[action]
    except KeyError:
        return HttpResponseBadRequest(
            "%s is not a valid result action." % action)

    kwargs = {"auth": request.auth}

    for argname in argnames:
        try:
            kwargs[argname] = request.POST[argname]
        except KeyError:
            return HttpResponseBadRequest(
                "Required parameter %s missing." % argname)

        if not kwargs[argname]:
            messages.error(request, "The %r field is required." % argname)
            return json_response({})

    getattr(result, action)(**kwargs)

    new_bug_url = request.POST.get("related_bug", None)
    if new_bug_url:
        try:
            URLValidator()(new_bug_url)
        except ValidationError:
            messages.error(request, "The bug URL must be a valid URL.")
            return json_response({})
        bug = ExternalBug(url=new_bug_url, externalIdentifier="1")
        result.relatedbugs.post(bug)
    else:
        for bug_id in request.POST.getlist("bugs"):
            if bug_id:
                bug = ExternalBugList.get_by_id(bug_id, auth=request.auth)
                result.relatedbugs.post(bug)

    return render_to_response(
        "runtests/_run_case.html",
        {"case": result.testCase,
         "caseversion": result.testCaseVersion,
         "result": result,
         "open": action == "start",
         })

def json_response(data):
    return HttpResponse(json.dumps(data), content_type="application/json")


#calss header
class _SACRIFICE():
	def __init__(self,):
		self.name = "SACRIFICE"
		self.definitions = [u'to give up something that is valuable to you in order to help another person: ', u'to kill an animal or a person and offer them to a god or gods']

		self.parents = []
		self.childen = []
		self.properties = []
		self.jsondata = {}

		self.specie = 'verbs'

	def run(self, obj1 = [], obj2 = []):
		return self.jsondata


from yadm import fields
from yadm.documents import Document

from .test_database import BaseDatabaseTest

class SimpleSetFieldTest(BaseDatabaseTest):
    def setUp(self):
        super().setUp()

        class TestDoc(Document):
            __collection__ = 'testdoc'
            s = fields.SetField(fields.IntegerField)

        self.TestDoc = TestDoc

    def test_default(self):
        td = self.TestDoc()
        self.assertIsInstance(td.s, fields.set.Set)
        self.assertFalse(td.s)
        self.assertEqual(len(td.s), 0)
        self.assertEqual(td.s._data, set())

    def test_get(self):
        _id = self.db.db.testdoc.insert({'s': [1, 2, 3]})
        td = self.db.get_queryset(self.TestDoc).with_id(_id)

        self.assertTrue(td.s)
        self.assertEqual(len(td.s), 3)
        self.assertEqual(td.s._data, {1, 2, 3})
        self.assertEqual(set(td.s), {1, 2, 3})
        self.assertRaises(TypeError, td.s.__getitem__, 1)

    def test_add(self):
        _id = self.db.db.testdoc.insert({'s': [1, 2, 3]})
        td = self.db.get_queryset(self.TestDoc).with_id(_id)
        td.s.add(4)

        self.assertEqual(td.s, {1, 2, 3, 4})

    def test_add_typeerror(self):
        td = self.TestDoc()
        self.assertRaises(ValueError, td.s.add, 'not a number')

    def test_add_save(self):
        _id = self.db.db.testdoc.insert({'s': [1, 2, 3]})
        td = self.db.get_queryset(self.TestDoc).with_id(_id)
        td.s.add(4)
        self.db.save(td)

        data = self.db.db.testdoc.find_one({'_id': _id})
        self.assertEqual(data['s'], [1, 2, 3, 4])

    def test_remove(self):
        _id = self.db.db.testdoc.insert({'s': [1, 2, 3]})
        td = self.db.get_queryset(self.TestDoc).with_id(_id)
        td.s.remove(2)

        self.assertEqual(td.s, {1, 3})

    def test_remove_save(self):
        _id = self.db.db.testdoc.insert({'s': [1, 2, 3]})
        td = self.db.get_queryset(self.TestDoc).with_id(_id)
        td.s.remove(2)
        self.db.save(td)

        data = self.db.db.testdoc.find_one({'_id': _id})
        self.assertEqual(data['s'], [1, 3])

    def test_add_to_set(self):
        _id = self.db.db.testdoc.insert({'s': [1, 2, 3]})
        td = self.db.get_queryset(self.TestDoc).with_id(_id)
        td.s.add_to_set(4)

        self.assertEqual(td.s, {1, 2, 3, 4})

        data = self.db.db.testdoc.find_one({'_id': _id})
        self.assertEqual(data['s'], [1, 2, 3, 4])

    def test_add_to_set_typeerror(self):
        td = self.TestDoc()
        self.assertRaises(ValueError, td.s.add_to_set, 'not a number')

    def test_pull(self):
        _id = self.db.db.testdoc.insert({'s': [1, 2, 3]})
        td = self.db.get_queryset(self.TestDoc).with_id(_id)
        td.s.pull(2)

        self.assertEqual(td.s, {1, 3})

        data = self.db.db.testdoc.find_one({'_id': _id})
        self.assertEqual(data['s'], [1, 3])


import pickle
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from datetime import datetime
from sortedcontainers import SortedList

# load in the data
import os
if not os.path.exists('venv/include/user2category.json') or \
   not os.path.exists('venv/include/category2user.json') or \
   not os.path.exists('venv/include/usercategory2rating.json'):
   import preprocess2dict

with open('venv/include/user2category.json', 'rb') as f:
  user2category = pickle.load(f)

with open('venv/include/category2user.json', 'rb') as f:
  category2user = pickle.load(f)

with open('venv/include/usercategory2rating.json', 'rb') as f:
  usercategory2rating = pickle.load(f)

N = np.max(list(user2category.keys())) + 1
# the test set may contain movies the train set doesn't have data on
M = np.max(list(category2user.keys())) +1
print("N:", N, "M:", M)

if M > 251:
  print("N =", N, "are you sure you want to continue?")
  print("Comment out these lines if so...")
  exit()

# to find the user similarities, you have to do O(M^2 * N) calculations!
# in the "real-world" you'd want to parallelize this
# note: we really only have to do half the calculations, since w_ij is symmetric
K = 20 # number of neighbors we'd like to consider
limit = 5 # number of common movies users must have in common in order to consider
neighbors = [] # store neighbors in this list
averages = [] # each item's average rating for later use
deviations = [] # each item's deviation for later use

for i in range(M):
  # find the K closest items to item i
  users_i = category2user[i]
  users_i_set = set(users_i)

  # calculate avg and deviation
  ratings_i = { user:usercategory2rating[(user, i)] for user in users_i }
  avg_i = np.mean(list(ratings_i.values()))
  dev_i = { user:(rating - avg_i) for user, rating in ratings_i.items() }
  dev_i_values = np.array(list(dev_i.values()))
  sigma_i = np.sqrt(dev_i_values.dot(dev_i_values))

  # save these for later use
  averages.append(avg_i)
  deviations.append(dev_i)

  sl = SortedList()
  for j in range(M):
    # don't include yourself
    if j != i:
      users_j = category2user[j]
      users_j_set = set(users_j)
      common_users = (users_i_set & users_j_set) # intersection
      if len(common_users) > limit:
        # calculate avg and deviation
        ratings_j = { user:usercategory2rating[(user, j)] for user in users_j }
        avg_j = np.mean(list(ratings_j.values()))
        dev_j = { user:(rating - avg_j) for user, rating in ratings_j.items() }
        dev_j_values = np.array(list(dev_j.values()))
        sigma_j = np.sqrt(dev_j_values.dot(dev_j_values))

        # calculate correlation coefficient
        numerator = sum(dev_i[m]*dev_j[m] for m in common_users)
        w_ij = numerator / (sigma_i * sigma_j)

        # insert into sorted list and truncate
        # negate weight, because list is sorted ascending
        # maximum value (1) is "closest"
        sl.add((-w_ij, j))
        if len(sl) > K:
          del sl[-1]

  # store the neighbors
  neighbors.append(sl)

  # print out useful things
  if i % 1 == 0:
    print(i)

# using neighbors, calculate train and test MSE

def predict(i, u):
  # calculate the weighted sum of deviations
  numerator = 0
  denominator = 0
  for neg_w, j in neighbors[i]:
    # remember, the weight is stored as its negative
    # so the negative of the negative weight is the positive weight
    try:
      numerator += -neg_w * deviations[j][u]
      denominator += abs(neg_w)
    except KeyError:
      # neighbor may not have been rated by the same user
      # don't want to do dictionary lookup twice
      # so just throw exception
      pass

  if denominator == 0:
    prediction = averages[i]
  else:
    prediction = numerator / denominator + averages[i]
  return prediction

predictionArray = []
train_predictions = []
train_targets = []
for (u, m), target in usercategory2rating.items():
    # calculate the prediction for this movie
    prediction = predict(i, m)
    predictionArray.append([i, m, prediction])
    print("prediction", prediction, i, m)
    # save the prediction and target
    if (target == 0):
        train_predictions.append(prediction)
        train_targets.append(target)

# calculate accuracy
def mse(p, t):
  p = np.array(p)
  t = np.array(t)
  return np.mean((p - t)**2)

print('train mse:', mse(train_predictions, train_targets)),
print(len(train_predictions),len(train_targets))


#!/bin/python
# -*- coding: utf-8 -*-

def calculo(cadena):
    if len(cadena)==int(cadena):
        print(int(cadena))
        return

    result = 0
    iter = 0

    while result != len(cadena):
        result = len(cadena)
        cadena = str(result)
        iter+=1
    iter+=1
    print(iter)

def main():
    cadena = input()

    while cadena != "END":
        calculo(cadena)
        cadena = input()

if __name__ == '__main__':
    main()

CraftCoders/P08 - Palindromic Moments/solution.py


# Linear Regression With Stochastic Gradient Descent for Wine Quality

from random import seed
from utils.load_csv import load_csv
from utils.conversion_functions import str_column_to_float
from utils.normalize import dataset_minmax, normalize_dataset
from evaluation_metrics.rmse_metric import rmse_metric
from utils.evaluate_algorithm import cv_evaluate_algorithm_with_metric_algorithm

# Make a prediction with coefficients
def predict(row, coefficients):
    yhat = coefficients[0]
    for i in range(len(row)-1):
        yhat += coefficients[i + 1] * row[i]
    return yhat

# Estimate linear regression coefficients using stochastic gradient descent
def coefficients_sgd(train, l_rate, n_epoch):
    coef = [0.0 for i in range(len(train[0]))]
    for epoch in range(n_epoch):
        for row in train:
            yhat = predict(row, coef)
            error = yhat - row[-1]
            coef[0] = coef[0] - l_rate * error
            for i in range(len(row)-1):
                coef[i + 1] = coef[i + 1] - l_rate * error * row[i]
            # print(l_rate, n_epoch, error)
    return coef

# Linear Regression Algorithm With Stochastic Gradient Descent
def linear_regression_sgd(train, test, l_rate, n_epoch):
    predictions = list()
    coef = coefficients_sgd(train, l_rate, n_epoch)
    for row in test:
        yhat = predict(row, coef)
        predictions.append(yhat)
    return(predictions)

seed(1)

# load and prepare data
filename = 'datasets/winequality-white.csv'
dataset = load_csv(filename)

for i in range(len(dataset[0])):
    str_column_to_float(dataset, i)

# normalize
minmax = dataset_minmax(dataset)
normalize_dataset(dataset, minmax)

# evaluate algorithm
n_folds = 5
l_rate = 0.01
n_epoch = 50

scores = cv_evaluate_algorithm_with_metric_algorithm(dataset,
                                                     linear_regression_sgd,
                                                     rmse_metric,
                                                     n_folds,
                                                     l_rate,
                                                     n_epoch)

print('Scores: %s' % scores)
print('Mean RMSE: %.3f' % (sum(scores)/float(len(scores))))


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Jesse Rubin
"""
Heighway Dragon
Problem 220
Let D0 be the two-letter string "Fa". For n≥1, derive Dn from Dn-1 by the
string-rewriting rules:

"a" → "aRbFR"
"b" → "LFaLb"

Thus, D0 = "Fa", D1 = "FaRbFR", D2 = "FaRbFRRLFaLbFR", and so on.

These strings can be interpreted as instructions to a computer graphics
program, with "F" meaning "draw forward one unit", "L" meaning "turn left 90
degrees", "R" meaning "turn right 90 degrees", and "a" and "b" being ignored.
The initial position of the computer cursor is (0,0), pointing up
towards (0,1).

Then Dn is an exotic drawing known as the Heighway Dragon of order n. For
example, D(10) is shown below; counting each "F" as one step, the highlighted
spot at (18,16) is the position reached after 500 steps.

What is the position of the cursor after 10**12 steps in D**50 ?
Give your answer in the form x,y with no spaces.
"""
from __future__ import division
from bib import xrange
from bib.decorations import cash_it, Jasm
from bib.maths import Vuple
from itertools import combinations

# @Jasm('../txt_files/highway_dragon.txt')
@cash_it
def d(n):
    if n == 0:
        return 'Fa'
    else:
        dragon = d(n-1)
        if len(dragon) >= 10**10:
            dragon = dragon[:10**10]
        return ''.join("aRbFR" if c == 'a' else "LFaLb" if c == 'b' else c for c in dragon)

assert 'Fa' == d(0)
assert 'FaRbFR' == d(1)
assert 'FaRbFRRLFaLbFR' == d(2)

# def ddd(n):
l = []
target = 2

def highway_dragon(max_order=50, steps=10**16):
    dirs = {
        0:Vuple((0, 1)),
        1:Vuple((1, 0)),
        2:Vuple((0, -1)),
        3:Vuple((-1, 0))
    }

    def clockwise(heading):
        if heading == 3:
            return 0
        else:
            return heading +1

    def counterclockwise(heading):
        if heading == 0:
            return 3
        else:
            return heading-1

    def dee(cur_order, highway, pos=Vuple((0, 0)), heading = 0, step=0):
        for h in highway:
            # print(h)
            if h == 'F':
                pos += dirs[heading]
                step += 1
                print(step, pos, heading)
                # if step == steps:
            if h == 'L':
                heading = counterclockwise(heading)
            if h == 'R':
                heading = clockwise(heading)
            if cur_order < max_order:
                if h == 'a':
                    pos, step, heading = dee(cur_order+1, 'aRbFR', pos, heading, step)
                if h == 'b':
                    pos , step, heading = dee(cur_order+1, 'LFaLb', pos, heading, step)
        return pos, step, heading
    curlevel = 0
    start = 'Fa'
    d = ''
    dee(0, start)

highway_dragon()
    # else:

    # return 'Fa' if n == 0 else ''.join("aRbFR" if c == 'a' else "LFaLb" if c == 'b' else c for c in d(n-1))
# print(d(50))


# coding: utf-8

from __future__ import absolute_import
from datetime import date, datetime  # noqa: F401

from typing import List, Dict  # noqa: F401

from openapi_server.models.base_model_ import Model
from openapi_server import util

class RatesRates(Model):
    """NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).

    Do not edit the class manually.
    """

    def __init__(self, eur=None, usd=None):  # noqa: E501
        """RatesRates - a model defined in OpenAPI

        :param eur: The eur of this RatesRates.  # noqa: E501
        :type eur: float
        :param usd: The usd of this RatesRates.  # noqa: E501
        :type usd: float
        """
        self.openapi_types = {
            'eur': float,
            'usd': float
        }

        self.attribute_map = {
            'eur': 'eur',
            'usd': 'usd'
        }

        self._eur = eur
        self._usd = usd

    @classmethod
    def from_dict(cls, dikt) -> 'RatesRates':
        """Returns the dict as a model

        :param dikt: A dict.
        :type: dict
        :return: The rates_rates of this RatesRates.  # noqa: E501
        :rtype: RatesRates
        """
        return util.deserialize_model(dikt, cls)

    def to_dict(self, prefix=""):
        """Returns the model as a dict:

        :return: The RatesRates as a dict
        :rtype: dict
        """
        return { 'eur': self._eur,
            'usd': self._usd }

    @property
    def eur(self):
        """Gets the eur of this RatesRates.

        EUR/Crypto rate  # noqa: E501

        :return: The eur of this RatesRates.
        :rtype: float
        """
        return self._eur

    @eur.setter
    def eur(self, eur):
        """Sets the eur of this RatesRates.

        EUR/Crypto rate  # noqa: E501

        :param eur: The eur of this RatesRates.
        :type eur: float
        """

        self._eur = eur

    @property
    def usd(self):
        """Gets the usd of this RatesRates.

        USD/Crypto rate  # noqa: E501

        :return: The usd of this RatesRates.
        :rtype: float
        """
        return self._usd

    @usd.setter
    def usd(self, usd):
        """Sets the usd of this RatesRates.

        USD/Crypto rate  # noqa: E501

        :param usd: The usd of this RatesRates.
        :type usd: float
        """

        self._usd = usd


from StatisticLabSupport import statistic_lab_support
from StatisticLab import statistic_lab_toolkits
from StatisticLabVisualizer import statistic_lab_vizard
a = [7,6,1,7,9]
b = [3,2,6,9,5]
x=[-2,-1,0,1,2]
y=[-2,1,4,5,5]
z = [100, 120, 400, 100000, -1, -200000, 400, 4700]
Flight = [7.43,7.21, 8.69, 8.64, 9.76, 6.85,
          7.89, 9.3, 8.03, 7, 8.8, 6.39, 7.54]
Control = [8.65, 6.99, 8.4, 9.66, 7.62, 7.44,
           8.55,9.88, 9.94, 7.14, 9.14]
x1 = [-1,1]
y1 = [1,5]
test1 = statistic_lab_toolkits()
test_viz = statistic_lab_vizard()
#print("boxplot: {}".format(test_viz.boxplot(ctl= Control, flgt = Flight, title = "Control vs Flight", labels = ["Control", "Flights"])))
#print("Outliner1: {}".format(test1.find_outliner(Flight,plot = False, title = None, labels = None)))

#print("Outliner2: {}".format(test1.find_outliner(Flight, plot = True, title = "Flights-boxplot", labels = ["flight"])))
print("LS_regression: {}".format(test1.LS_regression(x, y, rnd = 5, x_range = None, plot = False, residual2 = True)))
#print("LS_regression2: {}".format(test1.LS_regression( S_x = 2.5033, S_y = 1.8195, Mean_x = 3.3333, Mean_y = 4.1667, R_LCC = -0.9499, x_range = (0,6), plot = False)))
#print("Linear Eqaution: {}".format(test1.LinearEquation(x_points = x1, y_points = y1,  x_range = (-1,2), plot = True )))
#print("LS_regression: {}".format(test1.LS_regression(x, y, rnd = 4, x_range = (99,110), plot = True)))
# print("Mean: {}".format(test1.mean(b)))
# print("Mode: {}".format(test1.mode(b)))
# print("Median: {}".format(test1.median(b)))
# print("Standerd Deviation: {}".format(test1.std_dev(b)))
# print("Standerd Deviation2: {}".format(test1.std_dev(a, rm_na = True)))
# print("Standerd Deviation3: {}".format(test1.std_dev(a, rm_na = False, convert_na=True)))
# print("Variance: {}".format(test1.Variance(a, rm_na = False, convert_na=True)))
# print("Z-score: {}".format(test1.z_score(187.56, 0.396, 186.62)))
#print("LCC: {}".format(test1.LCC(x, y, rnd = 4)))
# print(test1.LinearEquation(x_points = x1, y_points = y1, rnd = 2, x_range = (-10,11), plot = False ))
# print("IQR: {}".format(test1.find_IQR(y, rm_na = False, rnd = 2, convert_na = False, Output = "IQR")))
# print("IQR: {}".format(test1.find_IQR(y, rm_na = False, rnd = 2, convert_na = False, Output = "5_sum")))


# -*- coding: utf-8 -*-
"""file transfer protocol

:mod:`pcapkit.protocols.application.ftp` contains
:class:`~pcapkit.protocols.application.ftp.FTP` only,
which implements extractor for File Transfer Protocol
(FTP) [*]_.

.. [*] https://en.wikipedia.org/wiki/File_Transfer_Protocol

"""
import re

from pcapkit.const.ftp.command import Command
from pcapkit.const.ftp.return_code import ReturnCode
from pcapkit.protocols.application.application import Application
from pcapkit.utilities.exceptions import ProtocolError, UnsupportedCall

__all__ = ['FTP']

class FTP(Application):
    """This class implements File Transfer Protocol."""

    ##########################################################################
    # Properties.
    ##########################################################################

    @property
    def name(self):
        """Name of current protocol.

        :rtype: Literal['File Transfer Protocol']
        """
        return 'File Transfer Protocol'

    @property
    def length(self):
        """Header length of current protocol.

        Raises:
            UnsupportedCall: This protocol doesn't support :attr:`length`.

        """
        raise UnsupportedCall(f"'{self.__class__.__name__}' object has no attribute 'length'")

    ##########################################################################
    # Methods.
    ##########################################################################

    def read(self, length=None, **kwargs):  # pylint: disable=unused-argument
        """Read File Transfer Protocol (FTP).

        Args:
            length (Optional[int]): Length of packet data.

        Keyword Args:
            **kwargs: Arbitrary keyword arguments.

        Returns:
            Union[DataType_FTP_Request, DataType_FTP_Response]: Parsed packet data.

        Raises:
            ProtocolError: If the packet is malformed.

        """
        if length is None:
            length = len(self)

        byte = self._read_fileng(length)
        if (not byte.endswith(b'\r\n')) or (len(byte.splitlines()) > 1):
            raise ProtocolError('FTP: invalid format', quiet=True)
        text = self.decode(byte.strip())

        if re.match(r'^\d{3}', text):
            pref = int(text[:3])
            try:
                flag = text[3] == '-'
            except IndexError:
                flag = False
            suff = text[4:] or None

            code = ReturnCode.get(pref)
            ftp = dict(
                type='response',
                code=code,
                arg=suff,
                mf=flag,
                raw=byte,
            )
        else:
            temp = text.split(maxsplit=1)
            if len(temp) == 2:
                pref, suff = temp
            else:
                pref, suff = text, None

            cmmd = Command.get(pref)
            ftp = dict(
                type='request',
                command=cmmd,
                arg=suff,
                raw=byte,
            )

        return ftp

    def make(self, **kwargs):
        """Make (construct) packet data.

        Keyword Args:
            **kwargs: Arbitrary keyword arguments.

        Returns:
            bytes: Constructed packet data.

        """
        raise NotImplementedError


import unittest

from data_structures.stacks_and_queues.stack import Stack

class SubStack(Stack):
    """Class to represent stack that tracks num of elements

    Wraps Stack class
    """

    def __init__(self):
        """Initialise in same way as Stack, but setup variable to track num of
        elements in stack
        """
        super().__init__()
        self.num_elements = 0

    def push(self, data):
        """Pushes data to stack, updating num_elements

        Parameters
        ----------
        data
            Data to store in stack
        """
        super().push(data)
        self.num_elements += 1

    def pop(self):
        """Remove and return top item from stack, updating num_elements

        Returns
        -------
        data : any
            Data at top of stack. Can be any Python object.
        """
        data = super().pop()
        self.num_elements -= 1
        return data

class SetOfStacks():
    """Class to store a single stack in an array of stacks, with no stack
    bigger than a set threshold
    """

    def __init__(self, stack_capacity):
        """Set up array of stacks

        Parameters
        ----------
        capacity : int
            Max no. of elements in a single SubStack
        """
        self.stacks = []
        self.stack_capacity = stack_capacity

    def push(self, data):
        """Push an element to appropriate SubStack

        Parameters
        ----------
        data : any
            Data to store in stack. Can be any Python object.
        """
        # Handle special case of no stacks present
        if len(self.stacks) == 0:
            self.stacks.append(SubStack())
        # Check if last stack in list is at capacity
        elif self.stacks[-1].num_elements == self.stack_capacity:
            self.stacks.append(SubStack())

        # Push data onto last stack in list
        self.stacks[-1].push(data)

    def pop(self):
        """Pop item from last stack, deleting stacks as necessary

        Returns
        -------
            Data at top of stack
        """
        # Check stacks exist
        if len(self.stacks) == 0:
            raise Exception('Unable to pop, no elements in stack.')

        # Perform pop operation on last SubStack in list
        data = self.stacks[-1].pop()

        # Delete last SubStack if required
        if self.stacks[-1].num_elements == 0:
            del self.stacks[-1]

        return data

    def _pop_at(self, index):
        """Pops item from stacks array at specified index.

        Parameters
        ----------
        index : int
            Index in stacks array of SubStack to pop from
        """
        # Perform pop operation on relevant SubStack
        data = self.stacks[index].pop()

        # Delete stack we just popped from if it is now empty
        if self.stacks[index].num_elements == 0:
            del self.stacks[index]

        return data

class Test(unittest.TestCase):
    """Test cases"""
    # Define test case inputs, and expected outputs

    def test_SetOfStacks(self):
        """Test operations of SetOfStacks class
        """

        s = SetOfStacks(3)

        # Test empty stack
        with self.assertRaises(Exception):
            s.pop()

        # Test inserting elements
        try:
            s.push(2)
        except Exception:
            self.fail("push() method raised Exception unexpectedly!")
        try:
            [s.push(i) for i in range(3, 10)]
        except Exception:
            self.fail("push() method raised Exception unexpectedly!")

        # Test pop
        self.assertEqual(s.pop(), 9)
        self.assertEqual(s.pop(), 8)
        self.assertEqual(s.pop(), 7)
        self.assertEqual(s.pop(), 6)

        # Test pop_at method
        self.assertEqual(s._pop_at(0), 4)

if __name__ == '__main__':

    unittest.main()


"""
    Author: wutenghu <wutenghu@chuangxin.com>
    Date:   2018/8/15
"""
import os
import pickle
import sys
import time

import gflags
import numpy as np
from keras.applications.imagenet_utils import preprocess_input
from keras.models import Model
from keras.preprocessing import image

from feature_extraction.mobilenet import MobileNet
from feature_extraction.vgg16 import VGG16

gflags.DEFINE_string('img_dir',
                     'input_img',
                     'path of the image dir')
gflags.DEFINE_string('results_dir',
                     'results',
                     'path of the result dir')
gflags.DEFINE_string('feat_folder',
                     'extract_feature',
                     'path of the feature folder')
gflags.DEFINE_string('img_vect',
                     'img_vect',
                     'embedding img vectors')
gflags.DEFINE_string('img_name',
                     'img_name',
                     'name for images')
gflags.DEFINE_string('new_dcd_lev1',
                     'B43',
                     'version of the extracted feature')
FLAGS = gflags.FLAGS

def load_model(model_name='vgg16'):
    if model_name == 'vgg16':
        model = VGG16(include_top=True, weights='imagenet')
    elif model_name == 'vgg19':
        model = None
    elif model_name == 'mobilenet':
        base_model = MobileNet(include_top=True, weights='imagenet')
        model = Model(
            inputs=base_model.input,
            outputs=base_model.get_layer('reshape_1').output)
    return model

def main(argv):
    FLAGS(argv)

    # load-in model
    model = load_model(model_name='mobilenet')

    img_path = os.path.join(FLAGS.img_dir, FLAGS.new_dcd_lev1 + '_crop')
    print(img_path)

    start = time.time()
    for ite in os.walk(img_path):
        if len(ite[2]) == 0:
            continue
        new_dcd = ite[0].split('/')[-2]
        cate = ite[0].split('/')[-1]
        img_name_list = ite[2]

        img_vect_npy = list()
        new_img_name_list = list()
        count = 0
        total_img_num = len(img_name_list)
        for img_name in img_name_list:

            count += 1
            if np.random.rand() > 1:
                continue

            if (count % 1000) == 0:
                print('{}_{}: processed {:.4f} %'.format(new_dcd, cate, 100 * count / total_img_num))

            img_path = os.path.join(
                FLAGS.img_dir,
                FLAGS.new_dcd_lev1,
                new_dcd,
                cate,
                img_name)
            try:
                img = image.load_img(img_path, target_size=(224, 224))
                x = image.img_to_array(img)
                new_img_name_list.append(img_name.split('.')[0])
                x = np.expand_dims(x, axis=0)
                x = preprocess_input(x)
                img_vect = model.predict(x).reshape(-1)
                img_vect_npy.append(img_vect)
            except Exception as e:
                print(img_name)
                pass

        img_vect_npy = np.asarray(img_vect_npy)

        img_vect_path = os.path.join(
            FLAGS.results_dir, FLAGS.feat_folder, FLAGS.img_vect)

        img_name_path = os.path.join(
            FLAGS.results_dir, FLAGS.feat_folder, FLAGS.img_name)

        if not os.path.exists(img_vect_path):
            os.makedirs(img_vect_path)
        np.save(
            img_vect_path + "/" + FLAGS.new_dcd_lev1 + "_" + new_dcd + "_" + cate,
            img_vect_npy)

        if not os.path.exists(img_name_path):
            os.makedirs(img_name_path)
        pickle.dump(
            new_img_name_list,
            open(img_name_path + "/" + FLAGS.new_dcd_lev1 + "_" + new_dcd + "_" + cate, 'wb'))

    print("{}_{}: total cost {:.4f} seconds.".format(
        new_dcd, cate, time.time() - start))

if __name__ == '__main__':
    main(sys.argv)


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='Centro',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('cod_estado', models.BigIntegerField(verbose_name=b'C\xc3\xb3digo del estado')),
                ('estado', models.CharField(max_length=100)),
                ('cod_municipio', models.BigIntegerField(verbose_name=b'C\xc3\xb3digo del municipio')),
                ('municipio', models.CharField(max_length=100)),
                ('cod_parroquia', models.BigIntegerField(verbose_name=b'C\xc3\xb3digo de la parroquia')),
                ('parroquia', models.CharField(max_length=100)),
                ('codigo', models.BigIntegerField(unique=True, verbose_name=b'C\xc3\xb3digo del centro')),
                ('nombre', models.CharField(max_length=100, verbose_name=b'Nombre del centro')),
                ('direccion', models.TextField(verbose_name=b'Direcci\xc3\xb3n del centro', blank=True)),
                ('mesas', models.BigIntegerField(verbose_name=b'N\xc3\xbamero de mesas')),
                ('electores', models.BigIntegerField(verbose_name=b'Electores en el centro')),
                ('tecnologia', models.CharField(max_length=25, null=True, choices=[(b'INALAMBRICA', b'INALAMBRICA'), (b'ALAMBRICA', b'ALAMBRICA'), (b'INALAMBRICA/ALAMBRICA', b'INALAMBRICA/ALAMBRICA'), (b'VSAT', b'VSAT')])),
                ('observacion', models.TextField(blank=True)),
                ('fecha_registro', models.DateTimeField(auto_now_add=True, verbose_name=b'Registrado el')),
                ('fecha_actualizacion', models.DateTimeField(auto_now=True, verbose_name=b'Actualizado el')),
                ('usuario', models.ForeignKey(related_name='usuario_centro', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'ordering': ['municipio', 'parroquia', 'nombre'],
                'verbose_name_plural': 'Centros',
            },
        ),
    ]


# Copyright 2014 SAP SE
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import codecs
from pyhdb.compat import PY2, unichr

class IncrementalDecoder(codecs.BufferedIncrementalDecoder):
    # Decoder inspired by python-ftfy written by Rob Speer
    # https://github.com/LuminosoInsight/python-ftfy/blob/master/ftfy/bad_codecs/utf8_variants.py

    def _buffer_decode(self, input, errors, final):
        decoded_segments = []
        position = 0

        while True:
            decoded, consumed = self._buffer_decode_step(
                input[position:], errors, final
            )

            if consumed == 0:
                break

            decoded_segments.append(decoded)
            position += consumed

        if final and position != len(input):
            raise Exception("Final decoder doesn't decoded all bytes")

        return u''.join(decoded_segments), position

    def _buffer_decode_step(self, input, errors, final):
        # If begin of CESU-8 sequence
        if input.startswith(b'\xed'):
            if len(input) < 6:
                if final:
                    # There aren't six bytes to decode.
                    return codecs.utf_8_decode(input, errors, final)
                else:
                    # Stream is not done yet
                    return u'', 0
            elif input[3] == 237 or (PY2 and input[3] == b"\xed"):
                if PY2:
                    bytenums = [ord(b) for b in input[:6]]
                else:
                    bytenums = input

                codepoint = (
                    ((bytenums[1] & 0x0f) << 16) +
                    ((bytenums[2] & 0x3f) << 10) +
                    ((bytenums[4] & 0x0f) << 6) +
                    (bytenums[5] & 0x3f) +
                    0x10000
                )
                return unichr(codepoint), 6

        # Fallback to UTF-8
        return codecs.utf_8_decode(input, errors, final)

class IncrementalEncoder(codecs.BufferedIncrementalEncoder):

    def _buffer_encode(self, input, errors, final=False):
        encoded_segments = []
        position = 0
        input_length = len(input)

        while position + 1 <= input_length:
            encoded, consumed = self._buffer_encode_step(
                input[position], errors, final
            )

            if consumed == 0:
                break

            encoded_segments.append(encoded)
            position += consumed

        if final and position != len(input):
            raise Exception("Final encoder doesn't encode all characters")

        return b''.join(encoded_segments), position

    def _buffer_encode_step(self, char, errors, final):
        codepoint = ord(char)
        if codepoint < 65535:
            return codecs.utf_8_encode(char, errors)
        else:
            seq = bytearray(6)
            seq[0] = 0xED
            seq[1] = 0xA0 | (((codepoint & 0x1F0000) >> 16) - 1)
            seq[2] = 0x80 | (codepoint & 0xFC00) >> 10
            seq[3] = 0xED
            seq[4] = 0xB0 | ((codepoint >> 6) & 0x3F)
            seq[5] = 0x80 | (codepoint & 0x3F)
            return bytes(seq), 1

def encode(input, errors='strict'):
    return IncrementalEncoder(errors).encode(input, final=True), len(input)

def decode(input, errors='strict'):
    return IncrementalDecoder(errors).decode(input, final=True), len(input)

class StreamWriter(codecs.StreamWriter):
    encode = encode

class StreamReader(codecs.StreamReader):
    decode = decode

CESU8_CODEC_INFO = codecs.CodecInfo(
    name="cesu-8",
    encode=encode,
    decode=decode,
    incrementalencoder=IncrementalEncoder,
    incrementaldecoder=IncrementalDecoder,
    streamreader=StreamReader,
    streamwriter=StreamWriter,
)

def search_function(encoding):
    if encoding == 'cesu-8':
        return CESU8_CODEC_INFO
    else:
        return None

codecs.register(search_function)


from django.shortcuts import render

from .forms import WordCounterForm
from .helpers import count_words

def word_counter_view(request):
    if request.method == 'GET':
        form = WordCounterForm()
    else:
        form = WordCounterForm(request.POST)

        if form.is_valid():
            text = form.cleaned_data['text']
            number_words = count_words(text)
            form = WordCounterForm({'text': f'Number of words: {number_words}'})

    context = {
        'form': form
    }

    return render(request, 'word_counter/form.html', context)