import json
import os
import sys
import time
from datetime import datetime
from textwrap import TextWrapper

import pytz

class Tables(object):
    """parent class for all tables."""

    schema = None
    alias = None

    def __init__(self, music, logger):
        """Initializer. Accepts target host list, port, and path."""

        self.music = music
        self.logger = logger
        self.key = None
        self.keyspace = music.keyspace
        self.tz = pytz.timezone('America/New_York')

    @classmethod
    def table_alias(cls):
        s = cls.__name__
        s = s[0].lower() + s[1:]
        return [s] + cls.alias

    @staticmethod
    def option_choices():
        choices = []
        aliases = None
        for table in Tables.__subclasses__():
            choices = choices + table.table_alias()
            for alias in choices:
                if aliases:
                    aliases = aliases + ' ' + alias
                else:
                    aliases = alias

        choices.append('all')

        return {
            "alias":  TextWrapper(subsequent_indent="    ", initial_indent="    ", width=79).fill(aliases),
            "choices": choices
        }

    def table(self):
        """Return tables name (same as class name but all lc)."""

        s = type(self).__name__
        return s[0].lower() + s[1:]

    def utc2local(self, utc):
        """Change utc time to local time for readability."""

        return " (" + datetime.fromtimestamp(utc, self.tz).strftime('%Y-%m-%d %I:%M:%S %Z%z') + ")"

    def get_rows(self, ids=None, raw=False):
        """ get_rows read table, or rows by id and return rows array """

        # TODO if ids == None and hasattr(self, 'ids'): ids = self.ids
        key = None if (ids is None) else self.key
        rows = []

        for row_id in ids or [None]:
            if raw:
                rows.append(json.dumps(self.music.read_row(self.keyspace, self.table(), key, row_id), sort_keys=True, indent=4))
                continue

            result = self.music.read_row(self.keyspace, self.table(), key, row_id)["result"]

            # strip "Row n"
            for _, data in sorted(result.items()):
                rows.append(data)

        if raw:
            return rows

        if len(rows) == 1:
            rows = rows[0]  # one row? not a list

        return rows

    def read(self, ids=None, json_file=None, raw=False, rows=None, names=None):
        """ read rows (array or single dict) to stdout or to a file """

        if rows is None:
            rows = self.get_rows(ids, raw)

        if raw:
            if names:
                print("\n" + self.table())
            for row in rows:
                print(row)
            return

        if isinstance(rows, list):
            for row in rows:
                if not ("timestamp" in row or "expire_time" in row):
                    break
                for key in (["timestamp", "expire_time"]):
                    if (not (key in row)) or (row[key] is None):
                        continue
                    try:
                        row[key] = row[key] + self.utc2local(float(row[key])/1000)
                    except ValueError:
                        row[key] = "Error: "+ row[key]

        else:
            row = rows
            for key in (["timestamp", "expire_time"]):
                if (not (key in row)) or (row[key] is None):
                    continue
                try:
                    row[key] = row[key] + self.utc2local(float(row[key])/1000)
                except ValueError:
                    row[key] = "Error: "+ row[key]

        if json_file is None:
            if names:
                print("\n" + self.table())
            print (json.dumps(rows, sort_keys=True, indent=4))
            return

        fh = open(json_file, "w")
        fh.write(json.dumps(rows, sort_keys=True, indent=4))
        fh.close()

    def create(self, json_file=None):
        """ add records from a file to db """

        if json_file and os.path.exists(json_file):
                inf = open(json_file)
                f = json_file
        else:
                inf = sys.stdin
                f = "stdin"

        self.logger.info("Create " + self.table() + " from: " + f)
        self.insert(json.loads(inf.read()))

    def insert(self, data):
        """ add records """

        self.logger.debug(data)

        if isinstance(data, list):
            for row in data:
                if "timestamp" in row:
                    row['timestamp'] = int(round(time.time() * 1000))
                self.music.create_row(self.keyspace, self.table(), row)
        else:
            row = data
            if "timestamp" in row:
                row['timestamp'] = int(round(time.time() * 1000))
            self.music.create_row(self.keyspace, self.table(), row)

    def create_table(self):
        """ create table """

        self.logger.info(self.schema)
        self.music.create_table(self.keyspace, self.table(), self.schema)

    def update(self, json_file):
        """Update a row. Not atomic."""

        self.logger.info("Update " + self.table() + " from: " + json_file)
        data = json.loads(open(json_file).read())
        self.logger.debug(data)

        if isinstance(data, list):
            for row in data:
                self.music.update_row_eventually(self.keyspace, self.table(), row)
        else:
            self.music.update_row_eventually(self.keyspace, self.table(), data)

    def delete(self, ids):
        """ delete records db based on id """

        for row_id in ids:
            self.logger.info("Delete from" + self.table() + " id: " + row_id)
            self.music.delete_row_eventually(self.keyspace, self.table(), self.key, row_id)

    def clean_table(self):
        """ delete all records in table """

        ids = []
        rows = self.get_rows()

        if isinstance(rows, list):
            for row in rows:
                ids.append(row[self.key])
        else:
            row = rows
            ids.append(row[self.key])

        for row_id in ids:
            self.music.delete_row_eventually(self.keyspace, self.table(), self.key, row_id)

#  Subclasses of Tables:

class Requests(Tables):
    alias = ["req", "q"]
    key = "request_id"
    schema = json.loads('{ "request_id": "text", "timestamp": "text", "request": "text", "PRIMARY KEY": "(request_id)" }')

    def __init__(self, music, logger):
        Tables.__init__(self, music, logger)
        self.key = Requests.key

class Results(Tables):
    alias = ["resu", "u"]
    key = "request_id"
    schema = json.loads('{ "request_id": "text", "status": "text", "timestamp": "text", "result": "text", "PRIMARY KEY": "(request_id)" }')

    def __init__(self, music, logger):
        Tables.__init__(self, music, logger)
        self.key = Results.key

class Group_rules(Tables):
    alias = ["rule", "gr"]
    key = "id"
    schema = json.loads('{ "id": "text", "app_scope": "text", "type": "text", "level": "text", "members": "text", "description": "text", "groups": "text", "status": "text", "timestamp": "text", "PRIMARY KEY": "(id)" }')

    def __init__(self, music, logger):
        Tables.__init__(self, music, logger)
        self.key = Group_rules.key

class Stacks(Tables):
    alias = ["stack", "s"]
    key = "id"
    schema = json.loads('{ "id": "text", "last_status": "text", "datacenter": "text", "stack_name": "text", "uuid": "text", "tenant_id": "text", "metadata": "text", "servers": "text", "prior_servers": "text", "state": "text", "prior_State": "text", "timestamp": "text", "PRIMARY KEY": "(id)" }')

    def __init__(self, music, logger):
        Tables.__init__(self, music, logger)
        self.key = Stacks.key

class Stack_id_map(Tables):
    alias = ["map", "m"]
    key = "request_id"
    schema = json.loads('{ "request_id": "text", "stack_id": "text", "timestamp": "text", "PRIMARY KEY": "(request_id)" }')

    def __init__(self, music, logger):
        Tables.__init__(self, music, logger)
        self.key = Stack_id_map.key

class Resources(Tables):
    alias = ["reso", "o"]
    key = "id"
    schema = json.loads('{ "id": "text", "url": "text", "resource": "text", "timestamp": "text", "requests": "text", "PRIMARY KEY": "(id)" }')

    def __init__(self, music, logger):
        Tables.__init__(self, music, logger)
        self.key = Resources.key

class Regions(Tables):
    alias = ["reg", "i", "lock"]
    key = "region_id"
    schema = json.loads('{ "region_id ": "text", "timestamp": "text", "last_updated ": "text", "keystone_url": "text", "locked_by": "text", "locked_time ": "text", "expire_time": "text", "PRIMARY KEY": "(region_id)" }')

    def __init__(self, music, logger):
        Tables.__init__(self, music, logger)
        self.key = Regions.key

class Groups(Tables):
    alias = ["group", "g"]
    key = "id"
    schema = json.loads('{ "id ": "text", "uuid": "text", "type ": "text", "level": "text", "factory": "text", "rule_id ": "text", "metadata ": "text", "server_list": "text", "member_hosts": "text", "status": "text", "PRIMARY KEY": "(id)" }')

    def __init__(self, music, logger):
        Tables.__init__(self, music, logger)
        self.key = Groups.key

if __name__ == "__main__":

    print(Tables.option_choices())


from PySide6.QtWidgets import QLabel,QLineEdit,QGroupBox
from PySide6.QtCore import Qt
import keyboard
import asyncio
import time

class GroupBox(QGroupBox):
    num_of_group = 0
    group_list = []
    width = 1000
    height = 100
    def __init__(self):
        super().__init__()
        self.setCheckable(True)
        self.setChecked(False)

        self.InitHotKeyLabel()
        self.InitHotKeyLine()

        self.InitRepeatCountLabel()
        self.InitRepeatCountLine()

        self.InitIntervalDelayLabel()
        self.InitIntervalDelayLine()

        self.InitKeyListLabel()
        self.InitKeyListLine()
    def InitHotKeyLabel(self):
        self.hot_key_label = GroupBox.Label(self)
        self.hot_key_label.setFixedWidth(105)
        self.hot_key_label.setText("Hot Key (max : {}) :".format(GroupBox.HotKeyLine.hot_key_max_count))
        self.hot_key_label.move(0, 20)
    def InitHotKeyLine(self):
        self.hot_key_line = GroupBox.HotKeyLine(self)
        self.hot_key_line.setFixedWidth(250)
        self.hot_key_line.move(self.hot_key_label.x() + self.hot_key_label.width(),self.hot_key_label.y())
    def InitRepeatCountLabel(self):
        self.repeat_count_label = GroupBox.Label(self)
        self.repeat_count_label.setFixedWidth(142)
        self.repeat_count_label.setText("Repeat Count (max : {}) :".format(GroupBox.RepeatCountLine.max_repeat_count))
        self.repeat_count_label.move(self.hot_key_line.x() + self.hot_key_line.width() + 10,self.hot_key_line.y())
    def InitRepeatCountLine(self):
        self.repeat_count_line = GroupBox.RepeatCountLine(self)
        self.repeat_count_line.setFixedWidth(100)
        self.repeat_count_line.move(self.repeat_count_label.x() + self.repeat_count_label.width(),self.repeat_count_label.y())
    def InitIntervalDelayLabel(self):
        self.interval_delay_label = GroupBox.Label(self)
        self.interval_delay_label.setFixedWidth(112)
        self.interval_delay_label.setText("Interval Delay (sec) :")
        self.interval_delay_label.move(self.repeat_count_line.x() + self.repeat_count_line.width() + 10,self.repeat_count_line.y())
    def InitIntervalDelayLine(self):
        self.interval_delay_line = GroupBox.IntervalDelayLine(self)
        self.interval_delay_line.move(self.interval_delay_label.x() + self.interval_delay_label.width(),self.interval_delay_label.y())
    def InitKeyListLabel(self):
        self.key_list_label = GroupBox.Label(self)
        self.key_list_label.setFixedWidth(110)
        self.key_list_label.setText("Key List (max : {}) :".format(GroupBox.KeyListLine.key_list_max_count))
        self.key_list_label.move(self.hot_key_label.x(),self.hot_key_label.y() + 35)
    def InitKeyListLine(self):
        self.key_list_line = GroupBox.KeyListLine(self)
        self.key_list_line.setFixedWidth(500)
        self.key_list_line.move(self.key_list_label.x() + self.key_list_label.width(),self.key_list_label.y())
    class HotKeyLine(QLineEdit):
        hot_key_max_count = 3
        def __init__(self,parent):
            super().__init__(parent)
            self.hot_key_list = []
        def enterEvent(self, event):
            from ___Threads___ import HotKeyInputThread
            from ___Menu___ import file_menubar
            file_menubar.setEnabled(False)
            self.setReadOnly(False)
            self.input_thread = HotKeyInputThread()
            self.input_thread.start()
        def leaveEvent(self,event):
            keyboard.unhook(self.input_thread.key_hook)
            from ___Menu___ import file_menubar
            file_menubar.setEnabled(True)
            self.setReadOnly(True)
            self.deselect()
            self.input_thread.stop()
            self.input_thread.join()
    class RepeatCountLine(QLineEdit):
        max_repeat_count = 10
        def __init__(self,parent):
            super().__init__(parent)
            self.repeat_count = ""
        def enterEvent(self,event):
            from ___Threads___ import RepeatCountInputThread
            from ___Menu___ import file_menubar
            file_menubar.setEnabled(False)
            self.setReadOnly(False)
            self.input_thread = RepeatCountInputThread()
            self.input_thread.start()
        def leaveEvent(self,event):
            keyboard.unhook(self.input_thread.key_hook)
            from ___Menu___ import file_menubar
            file_menubar.setEnabled(True)
            self.setReadOnly(True)
            self.deselect()
            self.input_thread.stop()
            self.input_thread.join()
    class IntervalDelayLine(QLineEdit):
        def __init__(self,parent):
            super().__init__(parent)
            self.interval_delay = ""
            self.setMaxLength(20)
        def enterEvent(self, event):
            self.setReadOnly(False)
            from ___Threads___ import IntervalDelayInputThread
            from ___Menu___ import file_menubar
            file_menubar.setEnabled(False)
            self.setReadOnly(False)
            self.input_thread = IntervalDelayInputThread()
            self.input_thread.start()
        def leaveEvent(self, event):
            keyboard.unhook(self.input_thread.key_hook)
            from ___Menu___ import file_menubar
            file_menubar.setEnabled(True)
            self.setReadOnly(True)
            self.deselect()
            self.input_thread.stop()
            self.input_thread.join()
    class KeyListLine(QLineEdit):
        key_list_max_count = 10
        def __init__(self,parent):
            super().__init__(parent)
            self.key_list = []
        def enterEvent(self,event):
            from ___Threads___ import KeyListInputThread
            from ___Menu___ import file_menubar
            file_menubar.setEnabled(False)
            self.setReadOnly(False)
            self.input_thread = KeyListInputThread()
            self.input_thread.start()
        def leaveEvent(self,event):
            keyboard.unhook(self.input_thread.key_hook)
            from ___Menu___ import file_menubar
            file_menubar.setEnabled(True)
            self.setReadOnly(True)
            self.deselect()
            self.input_thread.stop()
            self.input_thread.join()
    class Label(QLabel):
        def __init__(self,parent):
            super().__init__(parent)


#!/usr/bin/env python3
"""Set up OLM cache dirs for unit tests"""

import sys
from pathlib import Path
from os import listdir, remove, geteuid
from common import FREAD_CACHE_DIR, FWRITE_CACHE_DIR, FREAD_FILE, FREAD_FILE2,\
        FCACHES_FREAD_VALID_FILES, FCACHES_FREAD_INVALID_FILES,\
        FCACHES_FREAD_EMPTY_FILE, random_string_generator

if geteuid() != 0:
    print("Run as root")
    sys.exit(1)

# make cache dirs
Path(FREAD_CACHE_DIR).mkdir(parents=True, exist_ok=True)
Path(FWRITE_CACHE_DIR).mkdir(parents=True, exist_ok=True)

# clean cache dirs
for f in listdir(FREAD_CACHE_DIR):
    remove(FREAD_CACHE_DIR + f)
for f in listdir(FWRITE_CACHE_DIR):
    remove(FWRITE_CACHE_DIR + f)

# make short fread test file
with open(FREAD_CACHE_DIR + FREAD_FILE, "w") as fptr:
    fptr.write("This is a test. ")

# make fread test file for block SDO transfer testing
with open(FREAD_CACHE_DIR + FREAD_FILE2, "w") as fptr:
    fptr.write(random_string_generator(2000))

# make files for file caches tests
for f in FCACHES_FREAD_VALID_FILES:
    with open(FREAD_CACHE_DIR + f, "w") as fptr:
        fptr.write(random_string_generator(100))
for f in FCACHES_FREAD_INVALID_FILES:
    with open(FREAD_CACHE_DIR + f, "w") as fptr:
        fptr.write(random_string_generator(100))

# make empty file for file caches tests
Path(FREAD_CACHE_DIR + FCACHES_FREAD_EMPTY_FILE).touch()

print("Now start the oresat-linux-manager and then pytest")


import pytest
from unittest import mock

import os
from pydantic import BaseModel, root_validator
import tempfile

from csvmodel import errors
from csvmodel.types import SchemaSpec, ValidationResult
from csvmodel.csvfile import CsvFile
from csvmodel.validator import get_validator, JsonSchemaValidator, PydanticValidator

@pytest.fixture
def inlinespec():
    return SchemaSpec(type='inline', details='{"type": "object"}')

def test_get_validator_for_jsonschema(inlinespec):
    assert isinstance(
        get_validator('jsonschema', inlinespec),
        JsonSchemaValidator,
    )

def test_error_for_requesting_validator():
    with pytest.raises(errors.ConfigError):
        get_validator('validator', inlinespec)

@pytest.fixture
def raw_csv():
    with mock.patch('csvmodel.csvfile.open') as m:
        yield m.return_value.__enter__

class TestJsonSchemaValidator:
    @pytest.fixture
    def validator(self):
        return JsonSchemaValidator(
            {
                'type': 'object',
                'properties': {
                    'col1': {'type': 'string'},
                    'col2': {'type': 'number'},
                },
                'required': ['col1', 'col2'],
            },
            line_limit=1000,
        )

    @pytest.fixture
    def validator2(self):
        return JsonSchemaValidator(
            {
                'type': 'object',
                'properties': {
                    'col1': {'type': 'number'},
                    'col2': {'type': 'integer'},
                },
                'required': ['col1', 'col2'],
            },
            line_limit=1000,
        )

    def test_check_ok_file_gives_no_error(self, validator, raw_csv):
        raw_csv.return_value = [
            'col1,col2',
            'a,1',
        ]
        expected = ValidationResult(ok=True, messages=[])
        assert validator.check(CsvFile('any_file.csv')) == expected

    def test_check_error_messages(self, validator, raw_csv):
        raw_csv.return_value = [
            'col1,col2',
            'a,a'
        ]
        res = validator.check(CsvFile('any_file.csv'))

        assert not res.ok
        assert all([
            msg.startswith('any_file.csv')
            for msg in res.messages
        ])
        assert len(res.messages) == 1
        assert res.messages[0] == "any_file.csv:2: 'a' is not of type 'number'"

    def test_check_missing_values(self, validator, raw_csv):
        raw_csv.return_value = [
            'col1,col2',
            'a'
        ]
        res = validator.check(CsvFile('any_file.csv'))

        assert not res.ok
        assert all([
            msg.startswith('any_file.csv')
            for msg in res.messages
        ])
        assert len(res.messages) == 1
        assert res.messages[0] == "any_file.csv:2: 'col2' is a required property"

    def test_check_integer_works(self, validator2, raw_csv):
        raw_csv.return_value = [
            'col1,col2',
            '1.1,1',  # ok
            '1,1.1',  # no ok
        ]
        res = validator2.check(CsvFile('any_file.csv'))
        assert not res.ok
        assert res.messages == [
            "any_file.csv:3: '1.1' is not of type 'integer'",
        ]

    def test_correct_line_numbers(self, validator, raw_csv):
        raw_csv.return_value = [
            'col1,col2',
            'a',
            'a,1',
            'a,a'
        ]
        res = validator.check(CsvFile('any_file.csv'))
        assert not res.ok
        assert all([
            msg.startswith('any_file.csv')
            for msg in res.messages
        ])
        assert len(res.messages) == 2
        assert res.messages[0] == "any_file.csv:2: 'col2' is a required property"
        assert res.messages[1] == "any_file.csv:4: 'a' is not of type 'number'"

    def testerrors_for_deep_schema(self, raw_csv):
        raw_csv.return_value = [
            'col1,col2',
            'a,1',
        ]
        validator = JsonSchemaValidator(
            {
                'type': 'object',
                'properties': {
                    'col1': {'type': 'string'},
                    'col2': {
                        'type': 'object',
                        'properties': {
                            'a': {'type': 'string'},
                            'b': {'type': 'number'},
                         }
                    }
                }
            },
            line_limit=1000,
        )
        with pytest.raises(ValueError):
            validator.check(CsvFile('any_file.csv'))

class TestPydanticValidator:
    @pytest.fixture
    def model(self):

        class Data(BaseModel):
            col1: str
            col2: int

        return Data

    @pytest.fixture
    def model2(self):

        class Data(BaseModel):
            col1: str
            col2: int
            col3: float

        return Data

    def test_ok_data(self, model, raw_csv):
        raw_csv.return_value = [
            'col1,col2',
            'a,1',
        ]
        validator = PydanticValidator(model, line_limit=1000)
        res = validator.check(CsvFile('any_file.csv'))
        assert res.ok

    def test_with_line_limit(self, model, raw_csv):
        raw_csv.return_value = ['col1,col2'] + ['a,1']*19 + ['a,a']
        validator = PydanticValidator(model, line_limit=20)
        res = validator.check(CsvFile('any_file.csv'))
        assert res.ok

    def test_with_line_limit_failure(self, model, raw_csv):
        raw_csv.return_value = ['col1,col2'] + ['a,1']*18 + ['a,a']
        validator = PydanticValidator(model, line_limit=20)
        res = validator.check(CsvFile('any_file.csv'))
        assert not res.ok

    def test_with_single_violation(self, model, raw_csv):
        raw_csv.return_value = [
            'col1,col2',
            'a,a',
        ]
        validator = PydanticValidator(model, line_limit=1000)
        res = validator.check(CsvFile('any_file.csv'))
        assert not res.ok
        assert res.messages == [
            'any_file.csv:2: Issue in column col2: value is not a valid integer'
        ]

    def test_with_multiple_violations(self, model2, raw_csv):
        raw_csv.return_value = [
            'col1,col2,col3',
            'a,1.1,a',
        ]
        validator = PydanticValidator(model2, line_limit=1000)
        res = validator.check(CsvFile('any_file.csv'))
        assert not res.ok
        assert res.messages == [
            'any_file.csv:2: Issue in column col2: value is not a valid integer',
            'any_file.csv:2: Issue in column col3: value is not a valid float'
        ]

    def test_load_schema_from_file(self, raw_csv):
        with tempfile.TemporaryDirectory() as tdir:
            fname = os.path.join(tdir, 'model.py')
            with open(fname, 'w') as f:
                f.write('\n'.join([
                    'from pydantic import BaseModel',
                    '',
                    'class Model(BaseModel):',
                    '    col1: str',
                    '    col2: int',
                    '    col3: float',
                ]))

            validator = PydanticValidator.from_schema(
                SchemaSpec(
                    type='file',
                    details=':'.join([fname, 'Model'])
                ),
            )

        raw_csv.return_value = [
            'col1,col2,col3',
            'a,1,1',
            'a,1.1,a',
            'a,1',
        ]

        res = validator.check(CsvFile('any_file.csv'))

        assert not res.ok
        assert res.messages == [
            'any_file.csv:3: Issue in column col2: value is not a valid integer',
            'any_file.csv:3: Issue in column col3: value is not a valid float',
            'any_file.csv:4: Issue in column col3: field required',
        ]

    def test_missing_field(self, model, raw_csv):
        raw_csv.return_value = [
            'col1,col2',
            'a',
        ]
        validator = PydanticValidator(model, line_limit=1000)
        res = validator.check(CsvFile('any_file.csv'))
        assert not res.ok
        assert res.messages == [
            'any_file.csv:2: Issue in column col2: field required'
        ]

    def test_root_validator(self, raw_csv):

        class Model(BaseModel):
            col1: int
            col2: int

            @root_validator(pre=False)
            def only_one_nonzero(cls, values):
                col1 = int(values.get('col1'))
                col2 = int(values.get('col2'))
                if col1*col2 != 0:
                    raise ValueError('Either col1 or col2 should be 0')
                return values

        raw_csv.return_value = [
            'col1,col2',
            '1,1',
            '3,0',
        ]

        validator = PydanticValidator(Model, line_limit=1000)
        res = validator.check(CsvFile('any_file.csv'))

        assert not res.ok
        assert res.messages == [
            'any_file.csv:2: Issue in column __root__: Either col1 or col2 should be 0',
        ]

    def test_validator_from_module(self, raw_csv):
        validator = PydanticValidator.from_schema(
            SchemaSpec(
                type='module',
                details='csvmodel.testing:ExampleModel',
            )
        )

        raw_csv.return_value = [
            'col1,col2,col3',
            '1,a,3.2',
            '3.3,a,b',
        ]

        res = validator.check(CsvFile('any_file.csv'))
        assert not res.ok
        assert res.messages == [
            'any_file.csv:3: Issue in column col1: value is not a valid integer',
            'any_file.csv:3: Issue in column col3: value is not a valid float',
        ]

    def test_validator_from_nonexistant_file(self):
        with pytest.raises(errors.NoSchemaError):
            PydanticValidator.from_schema(
                SchemaSpec(
                    type='file',
                    details='non_existant_file.py:Model',
                )
            )

    def test_nonexistant_model(self):
        with pytest.raises(errors.NoSchemaError):
            PydanticValidator.from_schema(
                SchemaSpec(
                    type='module',
                    details='csvmodel.testing:DoesNotExist',
                )
            )

    def test_pydantic_dataclasses(self, raw_csv):
        with tempfile.TemporaryDirectory() as tdir:
            fname = os.path.join(tdir, 'model.py')
            with open(fname, 'w') as f:
                f.write('\n'.join([
                    'from pydantic.dataclasses import dataclass',
                    '',
                    '@dataclass',
                    'class Model:',
                    '    col1: str',
                    '    col2: int',
                    '    col3: float',
                ]))

            validator = PydanticValidator.from_schema(
                SchemaSpec(
                    type='file',
                    details=':'.join([fname, 'Model'])
                )
            )

        raw_csv.return_value = [
            'col1,col2,col3',
            'a,1,1',
            'a,1.1,a',
            'a,1',
        ]

        res = validator.check(CsvFile('any_file.csv'))

        assert not res.ok
        assert res.messages == [
            'any_file.csv:3: Issue in column col2: value is not a valid integer',
            'any_file.csv:3: Issue in column col3: value is not a valid float',
            "any_file.csv:4: missing 1 required positional argument: 'col3'",
        ]


# coding=utf8

from typing import List
from overrides import overrides
from allennlp.data.tokenizers import Token, WordTokenizer
from allennlp.data.tokenizers.word_splitter import WordSplitter

class PrologWordSplitter(WordSplitter):
    @overrides
    def split_words(self, logical_form: str) -> List[Token]:
        normalized_lf = logical_form.replace(" ", "::")
        replacements = [
            ('(', ' ( '),
            (')', ' ) '),
            (',', ' , '),
            ("\\+", " \\+ "),
        ]
        for a, b in replacements:
            normalized_lf = normalized_lf.replace(a, b)
        tokens = [Token(t) if "::" not in t else Token(
            t.replace("::", " ")) for t in normalized_lf.split()]
        return tokens

class PrologWordSplitter2(WordSplitter):
    @overrides
    def split_words(self, logical_form: str) -> List[Token]:
        normalized_lf = logical_form.replace(" ", "::")
        replacements = [
            ('(', ' ( '),
            (')', ' ) '),
            (',', ' , '),
            ("\\+", " \\+ "),
            ("'", " ' "),
        ]
        for a, b in replacements:
            normalized_lf = normalized_lf.replace(a, b)
        tokens = list()
        for t in normalized_lf.split():
            if "::" not in t:
                tokens.append(Token(t))
            else:
                tokens += [Token(_t) for _t in t.replace("::", " ").split()]
        return tokens

class FunQLWordSplitter(WordSplitter):
    @overrides
    def split_words(self, logical_form: str) -> List[Token]:
        normalized_lf = logical_form.replace(" ", "::")
        replacements = [
            ('(', ' ( '),
            (')', ' ) '),
            (',', ' , '),
            ("\\+", " \\+ "),
        ]
        for a, b in replacements:
            normalized_lf = normalized_lf.replace(a, b)
        tokens = [Token(t) if "::" not in t else Token(
            t.replace("::", " ")) for t in normalized_lf.split()]
        return tokens

class FunQLWordSplitter2(WordSplitter):
    @overrides
    def split_words(self, logical_form: str) -> List[Token]:
        normalized_lf = logical_form.replace(" ", "::")
        replacements = [
            ('(', ' ( '),
            (')', ' ) '),
            (',', ' , '),
            ("\\+", " \\+ "),
            ("'", " ' "),
        ]
        for a, b in replacements:
            normalized_lf = normalized_lf.replace(a, b)
        tokens = list()
        for t in normalized_lf.split():
            if "::" not in t:
                tokens.append(Token(t))
            else:
                tokens += [Token(_t) for _t in t.replace("::", " ").split()]
        return tokens

class SQLWordSplitter(WordSplitter):
    @overrides
    def split_words(self, logical_form: str) -> List[Token]:
        normalized_sql = logical_form
        replacements = [
            ('(', ' ( '),
            (')', ' ) '),
            (',', ' , '),
            ('.', ' . '),
            ("'", " \\' ")
        ]
        for a, b in replacements:
            normalized_sql = normalized_sql.replace(a, b)
        tokens = [Token(t) for t in normalized_sql.split()]
        return tokens

class LambdaCalculusWordSplitter(WordSplitter):
    @overrides
    def split_words(self, logical_form: str) -> List[Token]:
        normalized_lc = logical_form
        replacements = [
            ('(', ' ( '),
            (')', ' ) '),
            ("'", " \\' ")
        ]
        for a, b in replacements:
            normalized_lc = normalized_lc.replace(a, b)
        tokens = [Token(t) for t in normalized_lc.split()]
        return tokens

def get_logical_tokenizer(language: str) -> WordTokenizer:
    splitter = None
    if language == 'prolog':
        splitter = PrologWordSplitter()
    elif language == 'prolog2':
        splitter = PrologWordSplitter2()
    elif language == 'funql':
        splitter = FunQLWordSplitter()
    elif language == 'funql2':
        splitter = FunQLWordSplitter2()
    elif language == 'sql':
        splitter = SQLWordSplitter()
    elif language == 'lambda':
        splitter = LambdaCalculusWordSplitter()
    assert splitter is not None
    return splitter

if __name__ == '__main__':
    prolog = "(lambda $0:e (and (job $0) (language $0 perl) (company $0 \'Lockheed Martin Aeronautics\') (loc $0 colorado)))"
    spliiter = get_logical_tokenizer('lambda')
    tokenizer = WordTokenizer(spliiter)
    tokens = tokenizer.tokenize(prolog)
    print(tokens)


import json
import os
from pathlib import Path
from typing import Mapping

from preload.data import AbstractPreloadInstance
from preload.generator import (
    get_json_template,
    get_or_create_template,
    AbstractPreloadGenerator,
)
from preload.model import VnfModule, Port

THIS_DIR = os.path.dirname(os.path.abspath(__file__))
DATA_DIR = os.path.join(THIS_DIR, "grapi_data")

def get_or_create_network_template(network, vm_networks):
    """
    If the network role already exists in vm_networks, then
    return that otherwise create a blank template and return that
    """
    return get_or_create_template(
        DATA_DIR, "network-role", network, vm_networks, "vm-network"
    )

class GrApiPreloadGenerator(AbstractPreloadGenerator):
    @classmethod
    def supports_output_passing(cls):
        return True

    @classmethod
    def format_name(cls):
        return "GR-API"

    @classmethod
    def output_sub_dir(cls):
        return "grapi"

    def generate_module(
        self,
        vnf_module: VnfModule,
        preload_data: AbstractPreloadInstance,
        output_dir: Path,
    ):
        self.module_incomplete = False
        template = get_json_template(DATA_DIR, "preload_template")
        self._populate(template, preload_data, vnf_module)
        incomplete = (
            "_incomplete"
            if preload_data.flag_incompletes and self.module_incomplete
            else ""
        )
        filename = "{}{}.json".format(preload_data.preload_basename, incomplete)
        outfile = output_dir.joinpath(filename)
        with outfile.open("w") as f:
            json.dump(template, f, indent=4)

    def _populate(
        self,
        template: Mapping,
        preload_data: AbstractPreloadInstance,
        vnf_module: VnfModule,
    ):
        self._add_vnf_metadata(template, preload_data)
        self._add_availability_zones(template, preload_data, vnf_module)
        self._add_vnf_networks(template, preload_data, vnf_module)
        self._add_vms(template, preload_data, vnf_module)
        self._add_parameters(template, preload_data, vnf_module)

    def _add_vnf_metadata(self, template: Mapping, preload: AbstractPreloadInstance):
        topology = template["input"]["preload-vf-module-topology-information"]
        vnf_meta = topology["vnf-topology-identifier-structure"]
        vnf_meta["vnf-name"] = self.normalize(preload.vnf_name, "vnf_name")
        vnf_meta["vnf-type"] = self.normalize(
            preload.vnf_type,
            "vnf-type",
            "VALUE FOR: Concatenation of <Service Name>/"
            "<VF Instance Name> MUST MATCH SDC",
        )
        module_meta = topology["vf-module-topology"]["vf-module-topology-identifier"]
        module_meta["vf-module-name"] = self.normalize(
            preload.vf_module_name, "vf_module_name"
        )
        module_meta["vf-module-type"] = self.normalize(
            preload.vf_module_model_name,
            "vf-module-model-name",
            "VALUE FOR: <vfModuleModelName> from CSAR or SDC",
        )

    def _add_availability_zones(
        self, template: Mapping, preload: AbstractPreloadInstance, vnf_module: VnfModule
    ):
        zones = template["input"]["preload-vf-module-topology-information"][
            "vnf-resource-assignments"
        ]["availability-zones"]["availability-zone"]
        for i, zone_param in enumerate(vnf_module.availability_zones):
            zone = preload.get_availability_zone(i, zone_param)
            zones.append(self.normalize(zone, zone_param, index=i))

    def _add_vnf_networks(
        self, template: Mapping, preload: AbstractPreloadInstance, vnf_module: VnfModule
    ):
        networks = template["input"]["preload-vf-module-topology-information"][
            "vnf-resource-assignments"
        ]["vnf-networks"]["vnf-network"]
        for network in vnf_module.networks:
            network_data = {
                "network-role": network.network_role,
                "network-name": self.normalize(
                    preload.get_network_name(network.network_role, network.name_param),
                    network.name_param,
                    "VALUE FOR: network name of {}".format(network.name_param),
                ),
            }
            if network.subnet_params:
                network_data["subnets-data"] = {"subnet-data": []}
                subnet_data = network_data["subnets-data"]["subnet-data"]
                for subnet in network.subnet_params:
                    data = {}
                    subnet_id = preload.get_subnet_id(
                        network.network_role, subnet.ip_version, subnet.param_name
                    )
                    if subnet_id:
                        data["subnet-id"] = self.normalize(subnet_id, subnet.param_name)
                    else:
                        subnet_name = preload.get_subnet_name(
                            network.network_role, subnet.ip_version, ""
                        )
                        data["subnet-name"] = self.normalize(
                            subnet_name,
                            subnet.param_name,
                            alt_message="VALUE FOR: name of {}".format(
                                subnet.param_name
                            ),
                        )
                    subnet_data.append(data)
            networks.append(network_data)

    def add_floating_ips(
        self, network_template: dict, port: Port, preload: AbstractPreloadInstance
    ):
        for ip in port.floating_ips:
            key = "floating-ip-v4" if ip.ip_version == 4 else "floating-ip-v6"
            ips = network_template["floating-ips"][key]
            value = self.normalize(
                preload.get_floating_ip(
                    port.vm.vm_type, port.network.network_role, ip.ip_version, ip.param
                ),
                ip.param,
            )
            ips.append(value)

    def add_fixed_ips(
        self, network_template: dict, port: Port, preload: AbstractPreloadInstance
    ):
        items = network_template["network-information-items"][
            "network-information-item"
        ]
        ipv4s = next(item for item in items if item["ip-version"] == "4")
        ipv6s = next(item for item in items if item["ip-version"] == "6")
        if port.uses_dhcp:
            ipv4s["use-dhcp"] = "Y"
            ipv6s["use-dhcp"] = "Y"
        for index, ip in port.fixed_ips_with_index:
            target = ipv4s if ip.ip_version == 4 else ipv6s
            ips = target["network-ips"]["network-ip"]
            if ip.param not in ips:
                ips.append(
                    self.normalize(
                        preload.get_fixed_ip(
                            port.vm.vm_type,
                            port.network.network_role,
                            ip.ip_version,
                            index,
                            ip.param,
                        ),
                        ip.param,
                        index=index
                    )
                )
            target["ip-count"] += 1

    def _add_vms(
        self, template: Mapping, preload: AbstractPreloadInstance, vnf_module: VnfModule
    ):
        vms = template["input"]["preload-vf-module-topology-information"][
            "vf-module-topology"
        ]["vf-module-assignments"]["vms"]["vm"]
        for vm in vnf_module.virtual_machine_types:
            vm_template = get_json_template(DATA_DIR, "vm")
            vms.append(vm_template)
            vm_template["vm-type"] = vm.vm_type
            for i, param in enumerate(sorted(vm.names)):
                name = preload.get_vm_name(vm.vm_type, i, param)
                value = self.normalize(name, param, index=i)
                vm_template["vm-names"]["vm-name"].append(value)
            vm_template["vm-count"] = vm.vm_count
            vm_networks = vm_template["vm-networks"]["vm-network"]
            for port in vm.ports:
                role = port.network.network_role
                network_template = get_or_create_network_template(role, vm_networks)
                network_template["network-role"] = role
                network_template["network-role-tag"] = role
                self.add_fixed_ips(network_template, port, preload)
                self.add_floating_ips(network_template, port, preload)

    def _add_parameters(
        self, template: Mapping, preload: AbstractPreloadInstance, vnf_module: VnfModule
    ):
        params = [
            {
                "name": key,
                "value": self.normalize(preload.get_vnf_parameter(key, value), key),
            }
            for key, value in vnf_module.preload_parameters.items()
        ]
        for key, value in preload.get_additional_parameters().items():
            params.append(
                {
                    "name": key,
                    "value": value,
                }
            )

        template["input"]["preload-vf-module-topology-information"][
            "vf-module-topology"
        ]["vf-module-parameters"]["param"].extend(params)


import datetime
class Person:
    def __init__(self,name,birthdate):
        self.name = name
        self.birthdate = birthdate
        self._age = None
        self._age_last_calculated = None
        self.recalculate_age()
    def recalculate_age(self):
        today = datetime.date.today()
        age = today.year - self.birthdate.year
        if today < datetime.date(today.year,self.birthdate.month,self.birthdate.day):
            age -= 1

        self.age = age
        self.age_last_calculated = today
    def age(self):
        if(datetime.date.today() > self._age_last_calculated):
            self.recalculate_age()

        return self._age
    def __str__(self):
        return "string"
person = Person(
            "Jane",
            datetime.date(1990,7,26)
        )
print(person.__str__())
print(person.name)
print(person.age)


import data

DEVICE = 'cuda'
EPOCHS = 30
BATCH_SIZE = 64
LR = 0.0001
ENCODER_IN_CH = len(data.title_field.vocab)
DECODER_IN_CH = len(data.desc_field.vocab)
ENCODER_EMBED_DIM = 100
OUTPUT_SIZE = len(data.desc_field.vocab)
ENCODER_HIDDEN_SIZE = 1024
DECODER_HIDDEN_SIZE = 1024
ENCODER_NUM_LAYERS = 1
DECODER_NUM_LAYERS = 1
E_DROP = 0.5
D_DROP = 0.4


#!/usr/bin/python3
"""Importing Flask to run the web app"""
from flask import Flask, render_template
from models import storage
from models.state import State

app = Flask(__name__)

@app.route("/states_list", strict_slashes=False)
def display_states():
    """Render state_list html page to display States created"""
    states = storage.all()
    return render_template('7-states_list.html', states=states)

@app.teardown_appcontext
def teardown(self):
    """Method to remove current SQLAlchemy Session"""
    storage.close()

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


import logging
import os
from pathlib import Path

class StaticGtfsValidator:
    VALIDATOR_VERSION = "3.1.1"
    VALIDATOR_JAR = (
        Path(__file__).parent.absolute()
        / f"../include/gtfs-validator-{VALIDATOR_VERSION}-cli.jar"
    )

    @staticmethod
    def validate(gtfs_zip_file_path: str, *, output_path: str) -> None:
        """
        Validates the gtfs zip file and stores the result in the output_path

        Args:
            gtfs_zip_file_path - The path to the zip file that points to the Static GTFS file to be validated
            output_path - The path where you want the results of the validation to be stored
        """
        # Call validator with cmd: java -jar {gtfsValidtorName} -i {gtfs_zip_file_path} -o {output_path}
        # check to see if both the paths exist
        try:
            if not os.path.exists(gtfs_zip_file_path) or not os.path.exists(
                output_path
            ):
                raise Exception("Invalid path")

            validator_cmd_string = f"java -jar {StaticGtfsValidator.VALIDATOR_JAR} -i {gtfs_zip_file_path} -o {output_path} &> /dev/null"
            os.system(validator_cmd_string)

        except OSError as e:
            logging.error(f"OSError: {e}")
            raise e
        except Exception as e:
            logging.exception(f"Exception while validating zip file: {e}")
            raise e

        report_path = f"{output_path}/report.json"

        if not os.path.exists(output_path):
            return

        try:
            with open(report_path, "rb") as validation_output:
                return validation_output.read()
        except Exception as e:
            logging.error(e)


#!/usr/bin/env python

import logging
import colorlog

def get_logger(name: str, level=logging.INFO):
    log_format = "%(log_color)s[%(levelname)s] %(name)s:%(message)s"
    handler = colorlog.StreamHandler()
    handler.setFormatter(colorlog.ColoredFormatter(log_format))

    logger = colorlog.getLogger(name)
    logger.addHandler(handler)
    logger.setLevel(level)
    return logger


import logging
from logging.handlers import RotatingFileHandler
import pathlib

folder_path = pathlib.Path("logs")
folder_path.mkdir(parents=True, exist_ok=True)
log_file = pathlib.Path(folder_path, "app.log")
log_file.parent.mkdir(parents=True, exist_ok=True)
format = "%(asctime)s - %(levelname)s - %(message)s"
formatter = logging.Formatter(format)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
file_handler = RotatingFileHandler(log_file, maxBytes=1024 * 1024, backupCount=3)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)


# -----------------------------------------------------------
# CSCI-720-Big-Data-Analytics
# Assignment: HW02
#
# Author: Zizhun Guo
# Email: zg2808@cs.rit.edu
#
# RIT, Rochester, NY
#
# ZG@All rights researved
# -----------------------------------------------------------

import numpy as np # fundamental scientific computing package
import pandas as pd # data analysis and manipulation package
import matplotlib.pyplot as plt # plotting package
from datetime import datetime # python std data package
import sys # for using command line argument

def hist(data, bin_size, type):
    """ Draw histogram on dataset
    Paras:
        @data: a list signed dataset
        @bin_size: an int size of each bin
        @type: a string name of dataset
    Return:
        void
    """
    data_pos = data[data > 0]                               # dataset of Class: Bhutan
    data_neg = - data[data < 0]                             # dataset of Class: Assam
    num_bins = (np.max(data_pos)                            # # of bins
                + bin_size
                - np.min(data_pos)) \
                / bin_size
    plt.hist([data_neg, data_pos],                          # plot histogram
            bins= int(num_bins),                            # # of bins
            density=False,
            stacked = True)                                 # stack two classes
    plt.title('Data for Silver-Backed Abominable Snowfolks') # sets title
    plt.xlabel(type + ' Feature based Distribution')         # sets x label
    plt.legend(['Assam', 'Bhutan'])                          # sets legend
    plt.show()                                               # render the graph

def ROC_curve(TPR_FPR_tuples, roc_points_with_lowest_cost):
    """ Draw Receiver Operating Characteristics(ROC) Curve
    Paras:
        @TPR_FPR_tuples: a list of tuples containing pairs of TPR-FPR rates
        @roc_points_with_lowest_cost: a list of list which contains tuples of
                                    the coordinates of points of lowest costs
    Return:
        void
    """
    # Traverse all tuple to scatter and connects on the graph
    for TPR_FPR_tuple in TPR_FPR_tuples:
        plt.scatter(*zip(*TPR_FPR_tuple), s = 20)
        plt.plot(*zip(*TPR_FPR_tuple))

    # Mark the rate point with its matching threshold annotated
    # Data structure [[(x1, y1), threshold1],...,[(xn, yn), thresholdn]]
    for roc_point_with_lowest_cost in roc_points_with_lowest_cost:
        for roc_point_with_lowest_cost_duplicate in roc_point_with_lowest_cost:
            plt.plot(roc_point_with_lowest_cost_duplicate[0],
                    roc_point_with_lowest_cost_duplicate[1],
                    'ro')
            plt.annotate(roc_point_with_lowest_cost_duplicate[2],
                        (roc_point_with_lowest_cost_duplicate[0],
                        roc_point_with_lowest_cost_duplicate[1]))

    plt.xlim(0, 1)                                      # limits range of x-axis
    plt.ylim(0, 1)                                      # limits range of y-axis
    plt.gca().set_aspect('equal', adjustable='box')     # Axes squared
    plt.xticks(np.arange(0, 1.0, 0.1))                  # sets x ticks step size as 0.1
    plt.yticks(np.arange(0, 1.0, 0.1))                  # sets y ticks step size as 0.1
    plt.grid(True, linewidth = 0.5)                     # plot gird
    plt.title('ROC curve')                              # sets title
    plt.xlabel("False Positive Rate / FPR ->")          # sets x labels
    plt.ylabel('<- True Positive Rate / TPR')           # sets y labels
    plt.legend(['Age', 'Height', 'best thresholds',])   # sets legends
    plt.show()                                          # render the graph

def mixed_costs_threshold(thresholds, mixed_costs, best_thresholds, step, type):
    """ Draw mixed costs based on each thresholds with highlighted best thresholds
    Paras:
        @thresholds: a list of all possible thresholds
        @mixed_costs: a list of all possible mixed costs
        @best_thresholds: a list of best thresholds (one or multiple)
        @step: an int of bin size
        @type: a string of current attribute name
    Return:
        void
    """
    plt.plot(thresholds, mixed_costs)                           # plot the line graph
    for best_threshold in best_thresholds:                      # highlight the best threshold
        plt.axvline(best_threshold, color = 'r')
    plt.xticks(np.arange                                        # sets the x ticks size as a step
                    (np.min(thresholds),
                    np.max(thresholds) + step,
                    step))
    for label_to_hide in plt.axes().xaxis.get_ticklabels()[::2]:        # hide x labels in every 2 steps
        label_to_hide.set_visible(False)
    plt.grid(True, linewidth = 0.5)                             # plot grid
    plt.title('Mixed Cost based on Attribute Thresholds')     # sets title
    plt.xlabel(type + ' Thresholds')                             # sets x label
    plt.ylabel('Mixed Costs')                 # sets y label
    plt.show()                                                  # render the graph

def binary_classifier_1d(data, step, classfication_rule_A_to_B):
    """ One Dimensional Binary Classifier
    Paras:
        @data: a list of signed dataset
        @step: an int of bin size
        @classfication_rule_A_to_B: domain knowledge to regulate direction
                                    of how to classify classes:
                                    True: left is Assam, right is Bhutan
                                    False: left is Bhutan, right is Assam
    Return:
        thresholds: a list of all possible thresholds
        mixed_costs: a list of all possible mixed costs
        best_thresholds: a list of best thresholds (one or multiple)
        TPR_FPR_tuples: a list of tuples containing pairs of TPR-FPR rates
        roc_point_with_lowest_cost: a list of tuples of the coordinates of
                                    points of lowest costs with matching
                                    threshold
    """
    start = np.floor(np.min(np.abs(data)))                  # sets start threshold
    end = np.ceil(np.max(np.abs(data)))                     # sets end threshold

    TPR_FPR_tuples = []                                     # initialization
    mixed_costs = []                                        # ...
    thresholds = []                                         # ...

    best_thresholds = []                                    # ...
    lowest_cost = np.inf                                      # ...
    roc_point_with_lowest_cost = []                         # ...

    # while loop traversing all thresholds & classify datasets into two classes
    # based on the current threshold
    while start <= end:
        Class_A = data[np.abs(data) < start] \
            if classfication_rule_A_to_B \
            else data[np.abs(data) >= start]
        Class_B = data[np.abs(data) >= start] \
            if classfication_rule_A_to_B \
            else data[np.abs(data) < start]

        FN = np.size(Class_A[Class_A > 0])                  # calculate the False Negative
        FP = np.size(Class_B[Class_B < 0])                  # calculate the False Positives
        mixed_cost = FN + FP                                # calculate the mixed cost by adding up
        mixed_costs.append(mixed_cost)                      # store the current mixed cost
        thresholds.append(start)                            # store the current threshold
        # print(mixed_cost)
        TP = np.size(Class_B[Class_B > 0])                      # calculate the True Positive
        TPR_FPR_tuples.append((FP / np.size(data[data < 0]),    # calculate the ROC & store as tuple
                                TP / np.size(data[data > 0])))
        # Minimize the mistakes
        if mixed_cost < lowest_cost:
            lowest_cost = mixed_cost                              # update the minimal cost
            best_thresholds = [start]                           # update the best threshold
            roc_point_with_lowest_cost = [                      # update the roc point with lowest cost
                    (FP / np.size(data[data < 0]),
                    TP / np.size(data[data > 0]),
                    start)]

        if mixed_cost == lowest_cost:                             # for repeating minimal cost
            best_thresholds.append(start)                       #   store the new threshold
            roc_point_with_lowest_cost.append(                  #   update the roc point into list
                (FP / np.size(data[data < 0]),
                TP / np.size(data[data > 0]),
                start))

        start += step                                           # move to the next treshold
    # roc_point_with_lowest_cost.append((0.4583333333333333, 0.8142857142857143, 'test'))
    print("Best cost = " + str(lowest_cost)
            + "  Best threshold = "
            + str(best_thresholds))
    return thresholds, lowest_cost, mixed_costs, best_thresholds, \
            TPR_FPR_tuples, roc_point_with_lowest_cost

def write_file(best_thresholds_ages, best_thresholds_heights,
                lowest_cost_ages, lowest_cost_heights):
    """ Writes a trained
    Paras:
        @best_thresholds_ages: a list of best thresholds of Age dataset
        @best_thresholds_heights: a list of best thresholds of Height dataset
        @lowest_cost_ages: an int of the lowest cost of Age dataset
        @lowest_cost_heights: an int of the lowest cost of Height dataset
    Return:
        void
    """
    # select attribute by comparing the lowest cost to decide the classify rule
    attribute = 'Age' \
        if lowest_cost_ages < lowest_cost_heights \
        else 'Height'
    threshold = best_thresholds_ages \
        if lowest_cost_ages < lowest_cost_heights \
        else best_thresholds_heights

    # initialize a string to contains codes for dumping in the trained program
    lines = ""

    # add packages
    lines += f"import numpy as np"
    lines += f"\nimport pandas as pd"
    lines += f"\nfrom datetime import datetime"
    lines += f"\nfrom datetime import datetime"
    lines += f"\nimport sys"

    # add main functions
    # read validation csv file into data and print the created time & current running time
    lines += f"\n\ndef main():"
    lines += f"\n\tdf_snowfolks_data_raw = pd.read_csv(\'../\' + sys.argv[1])"
    lines += f"\n\tcreateDate = \"{datetime.now()}\""
    lines += f"\n\tprint(f\"File Created date:    {{createDate}}\")       # time and date when this file created"
    lines += f"\n\tprint(f\"Current running date: {{datetime.now()}}\")   # current time and data"

    # assign the selected attribute & threshold to the string as the classifier
    lines += f"\n\tattribute = \"{attribute}\""
    lines += f"\n\tthreshold = {threshold}"

    # select the attribute based on given attribute
    lines += f"\n\tdata = df_snowfolks_data_raw[attribute]"

    # classification based on attribute and domain knowledge
    lines += f"\n\tif attribute == 'Age':"
    lines += f"\n\t\tfor val in data:"
    lines += f"\n\t\t\tif val <= threshold[0]:"
    lines += f"\n\t\t\t\tprint('- 1')"
    lines += f"\n\t\t\telse:"
    lines +=  f"\n\t\t\t\tprint('+ 1')"

    lines += f"\n\tif attribute == 'Height':"
    lines += f"\n\t\tfor val in data:"
    lines += f"\n\t\t\tif val <= threshold[np.size(threshold) - 1]:"
    lines += f"\n\t\t\t\tprint('+ 1')"
    lines += f"\n\t\t\telse:"
    lines += f"\n\t\t\t\tprint('- 1')"

    lines += f"\n\nif __name__ == \"__main__\":"
    lines += f"\n\tmain()"

    # for testing usage
    print(lines)

    # create a file with given name "filepath"
    f = open('HW02_GUO_Zizhun_trained.py', "w")
    # write string to the filepath
    f.write(lines)
    f.close()

def main():
    """ Main function running the program
    """
    # read & convert the csv file into data DataFrame
    # df_snowfolks_data_raw = pd.read_csv('../Abominable_Data_For_1D_Classification__v93_HW3_720_final.csv')
    df_snowfolks_data_raw = pd.read_csv('../' + sys.argv[1])

    # Initialization for the public ROC tuples and ROC coordinates (with lowest cost) for ROC curve graph
    TPR_FPR_tuples = []
    roc_points_with_lowest_cost = []

    ####################################### 1. Age ###########################################

    # Initialization from for all necessary data strutures for training and plotting
    bin_size_ages = 2
    data_ages= []
    thresholds_ages= []
    lowest_cost_ages = np.inf
    mixed_costs_ages = []
    best_thresholds_ages = []
    TPR_FPR_tuples_age = []
    roc_point_with_lowest_cost_ages = []

    # Quantize the dataset using Bining Method
    data_ages = np.floor(df_snowfolks_data_raw['Age'] /bin_size_ages) \
                    * bin_size_ages  \
                    * df_snowfolks_data_raw['Class']

    # Threshold Setting for Age Data
    thresholds_ages, lowest_cost_ages, mixed_costs_ages, \
    best_thresholds_ages, TPR_FPR_tuples_age, \
    roc_point_with_lowest_cost_ages \
            = binary_classifier_1d(data_ages, bin_size_ages, True)

    # Store ROC tuple
    TPR_FPR_tuples.append(TPR_FPR_tuples_age)

    # Store the coordinate of the ROC point with matching thresholds
    roc_points_with_lowest_cost.append(roc_point_with_lowest_cost_ages)

    ######################################## 2. Heights ########################################

    # Initialization from for all necessary data strutures for training and plotting
    bin_size_heights = 5
    data_heights = []
    thresholds_heights= []
    lowest_cost_heights = np.inf
    mixed_costs_heights = []
    best_thresholds_heights = np.inf
    TPR_FPR_tuples_heights = []
    roc_point_with_lowest_cost_heights = []

    # Quantize the dataset using Bining Method
    data_heights= np.floor(df_snowfolks_data_raw['Height'] /bin_size_heights) \
                        * bin_size_heights \
                        * df_snowfolks_data_raw['Class']

    # Threshold Setting for Age Data
    thresholds_heights, lowest_cost_heights, mixed_costs_heights, \
    best_thresholds_heights, TPR_FPR_tuples_heights, \
    roc_point_with_lowest_cost_heights \
        = binary_classifier_1d(data_heights, bin_size_heights, False)

    # Store ROC tuple
    TPR_FPR_tuples.append(TPR_FPR_tuples_heights)

    # Store the coordinate of the ROC point
    roc_points_with_lowest_cost.append(roc_point_with_lowest_cost_heights)

    ##################################### 3. Writing File ####################################

    write_file(best_thresholds_ages, best_thresholds_heights,
                lowest_cost_ages, lowest_cost_heights)

    ###################################### 4. Rendering ######################################

    # call for histgram redering on "Age" data
    hist(data_ages, bin_size_ages, 'Age')

    # call for mixed cost thresholds redering on "Age" data
    mixed_costs_threshold(thresholds_ages,
                            mixed_costs_ages,
                            best_thresholds_ages,
                            bin_size_ages,
                            'Age')
    # call for histgram redering on Height data
    hist(data_heights, bin_size_heights, 'Height')

    # call for mixed cost thresholds redering on Height data
    mixed_costs_threshold(thresholds_heights,
                            mixed_costs_heights,
                            best_thresholds_heights,
                            bin_size_heights,
                            'Height')

    # call for ROC curve redering on Height data
    ROC_curve(TPR_FPR_tuples, roc_points_with_lowest_cost)

if __name__ == "__main__":
    main()


import pytest

from surebets_finder.raw_content.aplication.clients.betclick_client import BetClickClient
from surebets_finder.raw_content.aplication.clients.efortuna_client import EFortunaClient
from surebets_finder.raw_content.aplication.clients.lvbet_client import LvBetClient

@pytest.mark.slow
def test_efortuna_client_can_fetch_raw_data() -> None:
    # given
    client = EFortunaClient(urls=["https://www.efortuna.pl/zaklady-bukmacherskie/mma/ksw#"])  # type: ignore

    # when
    raw_html_content = client.get_raw_data()

    # then
    assert "main-content" in raw_html_content

@pytest.mark.slow
def test_betclick_client_can_fetch_raw_data() -> None:
    # given
    client = BetClickClient(
        urls=[
            "https://offer.cdn.begmedia.com/api/pub/v4/events?application=2048&countrycode=pl&fetchMultipleDefaultMarkets=true&language=pa&limit=1000&offset=0&sitecode=plpa&sortBy=ByLiveRankingPreliveDate&sportIds=16"
        ]
    )  # type: ignore

    # when
    raw_content = client.get_raw_data()

    # then
    assert "selections" in raw_content

@pytest.mark.slow
def test_lvbet_client_can_fetch_raw_data() -> None:
    # given
    client = LvBetClient(
        urls=["https://app.lvbet.pl/_api/v1/offer/matches/?is_live=false&sports_groups_ids=683&lang=pl"]
    )  # type: ignore

    # when
    raw_content = client.get_raw_data()

    # then
    assert "selections" in raw_content


from io import BytesIO
import json
from pathlib import Path
import mimetypes
import re
from os import environ

from tiled_maps.database import get_connection
from tiled_maps.raster import render_tilemap
from tiled_maps.tilegen import generate

from fastapi import FastAPI, HTTPException
from fastapi import Depends
from fastapi.responses import Response

app = FastAPI()

CHUNK_REGEX = re.compile(r".+chunk_(-?\d+)_(-?\d+).json")
WORLD_CENTER_X = int(environ["WORLD_CENTER_X"])
WORLD_CENTER_Y = int(environ["WORLD_CENTER_Y"])
GAME_ZOOM_LEVEL = int(environ["GAME_ZOOM_LEVEL"])
TILE_RESOLUTION = int(environ["TILE_RESOLUTION"])
CELL_PIXEL_SIZE = int(environ["CELL_PIXEL_SIZE"])

@app.get("/maps/generated/world.world")
def get_world_file():
    game_world_data = {
        "patterns": [
            {
                "regexp": "chunk_(\\-?\\d+)_(\\-?\\d+)\\.json",
                "multiplierX": TILE_RESOLUTION * CELL_PIXEL_SIZE,
                "multiplierY": TILE_RESOLUTION * CELL_PIXEL_SIZE,
                "offsetX": 0,
                "offsetY": 0,
            }
        ],
        "type": "world",
    }
    with open("demo_tilegame2/maps/generated/world.world", "w") as fw:
        json.dump(game_world_data, fw, indent=2)
    return game_world_data

@app.get("/zxy_gamified/{z}/{x}/{y}.{ext}")
def generate_raster_tile(
    z: int, x: int, y: int, ext: str, conn=Depends(get_connection)
):
    import time

    start = time.time()
    # path is fake, this is not going to be persisted
    tm = generate.generate_map(Path("/fake"), x, y, z, conn, tiles=TILE_RESOLUTION)
    print(f"Time for pure generation: {time.time() - start:.2f}")
    data_repr = tm.to_dict()

    if ext == "json":
        return data_repr
    elif ext == "png":
        out_image = render_tilemap(tm)
        ret_data = BytesIO()
        out_image.save(ret_data, "PNG")
        return Response(content=ret_data.getvalue(), media_type="image/png")
    else:
        return HTTPException(400, f"Unknown extension {ext}")

@app.get("/{file_path:path}")
def get_path(file_path: str):
    base_folder = Path("demo_tilegame2")
    p = base_folder / file_path
    assert p.is_relative_to(base_folder)
    if p.exists() and not p.is_dir():
        raw_data = p.read_bytes()
        return Response(content=raw_data, media_type=mimetypes.guess_type(file_path)[0])
    # not there, was it a chunk request?
    if CHUNK_REGEX.match(file_path) is None:
        print("Cannot find ", p)
        raise HTTPException(404, "File not found")
    # it was, generate it on the fly
    # get the tiled world coordinates
    x, y = (int(e) for e in CHUNK_REGEX.match(file_path).groups())
    geo_x = WORLD_CENTER_X + x
    geo_y = WORLD_CENTER_Y + y
    p = base_folder / f"maps/generated/chunk_{x}_{y}.json"
    # if already there, read it and that's it
    if p.exists():
        with open(p) as fr:
            return json.load(fr)
    print(f"Chunk {y, y} means XYZ {geo_x, geo_y, GAME_ZOOM_LEVEL}")
    import time

    start = time.time()
    with get_connection() as conn:
        tm = generate.generate_map(
            p, geo_x, geo_y, GAME_ZOOM_LEVEL, conn, tiles=TILE_RESOLUTION
        )
    print(f"Time for pure generation: {time.time() - start:.2f}")
    # cache the file
    data_repr = tm.to_dict()
    with open(p, "w") as fw:
        json.dump(data_repr, fw)
    for relpath, content in tm.get_event_files():
        with open(p.parent / relpath, "w") as fw:
            fw.write(content)
    return data_repr


from gpiozero import LED
from time import sleep

a = LED(25)
b = LED(17)
c = LED(13)
d = LED(20)

while True:
    a.on()
    sleep(1)
    a.off()
    sleep(1)


import os, requests, pprint, re
from bs4 import BeautifulSoup
if __name__ == '__main__':
    url = 'https://pic.sogou.com/pics?query=%B5%E7%C4%D4%B1%DA%D6%BD&di=2&_asf=pic.sogou.com&w=05009900'
    os.makedirs('Pic', exist_ok=True)
    print('Prepare for download .......')
    res = requests.get(url)
    res.raise_for_status()
    soup = BeautifulSoup(res.text, 'html.parser')
    pprint.pprint(soup.prettify())
    linkElems = soup.find_all('img',  class_='img-hover')
    pprint.pprint(linkElems)
    num = 0
    for link in linkElems:
        comUrl = link.get('src')
        print('Downloading image %s...' % (comUrl))
        res = requests.get(comUrl)
        if res.status_code != 200:
            continue
        num += 1
        imageFile = open(os.path.join('Pic', str(num)+'.jpg'), 'wb')
        imageFile.write(res.content)
        imageFile.close()
    print('You have downloaded ' + str(num) + ' Pictures\n')


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

import os
import streamlit.components.v1 as components

import socket

_RELEASE = True

if not _RELEASE:
	# local dev server via `npm run start`
	_lorem_ipsum = components.declare_component(
		"lorem_ipsum",
		url=f"http://{socket.getfqdn()}:3001",
	)
else:
	# release build via `npm run build`
	parent_dir = os.path.dirname(os.path.abspath(__file__))
	build_dir = os.path.join(parent_dir, "frontend/build")
	_lorem_ipsum = components.declare_component("lorem_ipsum", path=build_dir)

# Wrapper function for the component
def lorem_ipsum(key=None):
	"""Create a new instance of "lorem_ipsum".

	Parameters
	----------
	key: str or None
	    An optional key that uniquely identifies this component. If this is
	    None, and the component's arguments are changed, the component will
	    be re-mounted in the Streamlit frontend and lose its current state.

	Returns
	-------
	None

	"""
	return _lorem_ipsum(key=key)

# Add some test code to play with the component while it's in development.
# During development, we can run this just as we would any other Streamlit
# app: `$ streamlit run ExtractiveReaderAnswer/__init__.py`
if not _RELEASE:
	import streamlit as st

	st.subheader("Test Component `lorem_ipsum()`")

	"Hello World, this is LoremIpsum component!"

	lorem_ipsum()

# EOF


import requests

# response = requests.get(url="http://api.open-notify.org/iss-now.json")
# response.raise_for_status()

# longitude = response.json()["iss_position"]["longitude"]
# latitude = response.json()["iss_position"]["latitude"]

# position = (longitude, latitude)
# print(position)

parameters = {
    "lat": 12.169174,
    "lng": -61.726143,
}

response = requests.get("https://api.sunrise-sunset.org/json", params=parameters)
response.raise_for_status()
data = response.json()
sunrise = data["results"]["sunrise"]
sunset = data["results"]["sunset"]

print(sunrise, sunset)


# _*_ coding:utf-8 _*_
# author: zizle
# last edited: 2018/10/10

import sys
from PyQt5.QtWidgets import (
    QApplication, QVBoxLayout,
    QWidget, QLabel, QPushButton
)
from PyQt5.QtGui import QPixmap

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.initWindow()

    def initWindow(self):
        # 设置按钮
        btn = QPushButton('立即查看', self)
        btn.setFixedSize(60,20)
        btn.setCheckable(True)
        # 连接到方法，通过clicked信号操作一个布尔值
        btn.clicked[bool].connect(self.setImage)

        # 纵向布局
        vbox = QVBoxLayout(self)
        # 标签
        self.lbl = QLabel(self)
        # 设置label的大小
        self.lbl.setFixedWidth(500)
        self.lbl.setFixedHeight(400)
        self.lbl.setScaledContents(True)

        vbox.addWidget(btn)
        vbox.addWidget(self.lbl)

        # 窗口显示
        self.setLayout(vbox)
        self.move(30, 30)
        self.setWindowTitle('看看你是谁？')
        self.show()

    def setImage(self, pressed):
        if pressed:
            # 图像对象
            pixMap = QPixmap("data/pig.jpg")
        else:
            pixMap = QPixmap()
            # 设置图像
        self.lbl.setPixmap(pixMap)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec_())


import chispa

import main.config.constants as Constants
import main.processors.processor as Processor

class TestProcessor(object):

    def test_group_visits_by_video(self, spark):
        source_data = [
            (1234, 11111),
            (1234, 22222),
            (5678, 33333),
        ]
        source_df = spark.spark_session.createDataFrame(
            source_data,
            [Constants.VISITS_VIDEO_ID, Constants.VISITS_LOCATION_ID]
        )
        actual_df = Processor.group_visits_by_video(source_df)
        expected_data = [
            (1234, 2),
            (5678, 1)
        ]
        expected_df = spark.spark_session.createDataFrame(
            expected_data,
            [Constants.VISITSXVIDEO_VIDEO_ID, Constants.VISITSXVIDEO_COUNT]
        )
        chispa.assert_df_equality(actual_df, expected_df, ignore_nullable=True)

    def test_add_video_info(self, spark):
        source_data = [
            (1234, 2),
            (5678, 1),
        ]
        source_df = spark.spark_session.createDataFrame(
            source_data,
            [Constants.VISITSXVIDEO_VIDEO_ID, Constants.VISITSXVIDEO_COUNT]
        )

        source_ref_data = [
            (1234, "Hello"),
            (5678, "All ok!"),
        ]
        source_ref_df = spark.spark_session.createDataFrame(
            source_ref_data,
            [Constants.VISITSXVIDEO_VIDEO_ID, "somecol"]
        )

        actual_df = Processor.add_video_info(source_df, source_ref_df)
        expected_data = [
            (1234, 2, "Hello"),
            (5678, 1, "All ok!"),
        ]
        expected_df = spark.spark_session.createDataFrame(
            expected_data,
            [Constants.VISITSXVIDEO_VIDEO_ID, Constants.VISITSXVIDEO_COUNT, "somecol"]
        )
        chispa.assert_df_equality(actual_df, expected_df, ignore_nullable=True)


from dajax.core import Dajax
from dajaxice.decorators import dajaxice_register

from django.template.loader import render_to_string
from django.conf import settings
from home.forms import ProfileLocationForm, ProfilePresentationForm
from home.forms import ProfilePersonalForm, ProfileSpeakerForm, ProfileSocialForm, ProfileNotificationsForm
from home.views import get_presentations
from home.models import Location, Presentation, UserProfile
from home.models.userprofile import RELATIONSHIP_FOLLOWING
from home.tasks import convert_to_pdf, geocode

import logging

logger = logging.getLogger('home.views')

@dajaxice_register
def send_form(request, form, form_id):

  dajax = Dajax()
  logger.error(form_id)

  if form_id == "id-profile-personal-form":
    form = ProfilePersonalForm(form, instance = request.user.profile)

    if form.is_valid():
      dajax.remove_css_class('#%s div' % form_id,'error')
      dajax.clear('#%s-alert' % form_id, 'innerHTML')
      user_profile = form.save()
      dajax.append('#%s-alert' % form_id, 'innerHTML', '
Personal Information Saved
')
      result = "success"

    else:
      dajax.remove_css_class('#%s div' % form_id, 'error')
      dajax.clear('#%s-alert' % form_id, 'innerHTML')
      dajax.append('#%s-alert' % form_id, 'innerHTML', '
Please fix the errors below
')
      for error in form.errors:
        dajax.add_css_class('#div_id_%s' % error, 'error')
      result = "failed"

    return dajax.json()

  elif form_id == "id-profile-speaker-form":
    form = ProfileSpeakerForm(form, instance = request.user.profile)

    if form.is_valid():
      dajax.remove_css_class('#%s div' % form_id,'error')
      dajax.clear('#%s-alert' % form_id, 'innerHTML')
      user_profile = form.save()
      dajax.append('#%s-alert' % form_id, 'innerHTML', '
Speaker Information Saved
')
      result = "success"

    else:
      dajax.remove_css_class('#%s div' % form_id, 'error')
      dajax.clear('#%s-alert' % form_id, 'innerHTML')
      dajax.append('#%s-alert' % form_id, 'innerHTML', '
Please fix the errors below
')
      for error in form.errors:
        dajax.add_css_class('#div_id_%s' % error, 'error')
      result = "failed"

    return dajax.json()

  elif form_id == "id-profile-social-form":
    form = ProfileSocialForm(form, instance = request.user.profile)

    if form.is_valid():
      dajax.remove_css_class('#%s div' % form_id,'error')
      dajax.clear('#%s-alert' % form_id, 'innerHTML')
      user_profile = form.save()
      dajax.append('#%s-alert' % form_id, 'innerHTML', '
Social Networking Information Saved
')
      result = "success"

    else:
      dajax.remove_css_class('#%s div' % form_id, 'error')
      dajax.clear('#%s-alert' % form_id, 'innerHTML')
      dajax.append('#%s-alert' % form_id, 'innerHTML', '
Please fix the errors below
')
      for error in form.errors:
        dajax.add_css_class('#div_id_%s' % error, 'error')
      result = "failed"

    return dajax.json()

  elif form_id == "id-profile-notifications-form":
    form = ProfileNotificationsForm(form, instance = request.user.profile)

    if form.is_valid():
      dajax.remove_css_class('#%s div' % form_id,'error')
      dajax.clear('#%s-alert' % form_id, 'innerHTML')
      user_profile = form.save()
      dajax.append('#%s-alert' % form_id, 'innerHTML', '
Notification Settings Saved
')
      result = "success"

    else:
      dajax.remove_css_class('#%s div' % form_id, 'error')
      dajax.clear('#%s-alert' % form_id, 'innerHTML')
      dajax.append('#%s-alert' % form_id, 'innerHTML', '
Please fix the errors below
')
      for error in form.errors:
        dajax.add_css_class('#div_id_%s' % error, 'error')
      result = "failed"

    return dajax.json()

  else:
    pass

#  if form_id == "id-presentation-form":
#    logger.error("Presentation Form")

#    # Check for the presentation slides hidden value
#    if len(form['presentation_slides']) <= 0:
#        dajax.append('#%s-alert' % form_id, 'innerHTML', '
Please upload a presenation.
')
#        result = "failed"
#        dajax.add_data({'result': result}, 'js_callback')
#        return dajax.json()
#    else:
#        dajax.clear('#%s-alert' % form_id, 'innerHTML')
#
#    logger.error(form)
#    form = ProfilePresentationForm(form, request.FILES)
#
#  elif form_id == "id-location-form":
#    form = ProfileLocationForm(form)
#
#  elif form_id == "id-personal-form":
#    form = ProfilePersonalForm(form, instance = request.user.profile)
#
#  else:
#    result = "failed"
#    return dajax.json()
#
#  if form.is_valid():
#    dajax.remove_css_class('#%s div' % form_id,'error')
#    dajax.clear('#%s-alert' % form_id, 'innerHTML')
#
#    # Save the personal information to the profile
#    if form_id == "id-personal-form":
#      user_profile = form.save(commit=False)
#      user_profile.generate_hash()
#      user_profile.new_profile = False
#      user_profile.save()
#      form.save_m2m()
#
#    elif form_id == "id-location-form":
#      location = form.save()
#      request.user.profile.locations.add(location)
#      geocode.delay(location)

#    elif form_id == "id-presentation-form":
#      presentation = form.save(commit=False)
#      presentation.presentation_slides = form.cleaned_data['presentation_slides']
#      presentation.save()
#      form.save_m2m()
#      request.user.profile.presentations.add(presentation)
#
#      # Kick off the Celery job to convert to PDF and generate the thumbnail
#      #convert_to_pdf.delay("%s/%s" % (settings.CURRENT_DIR, presentation_slides), request.user, presentation)
#      convert_to_pdf.delay(request.user, presentation)
#
#    else:
#      pass
#
#    result = "success"

#  else:
#    logger.error("%s Form not valid" % form_id)
#    dajax.remove_css_class('#%s div' % form_id,'error')
#    dajax.append('#%s-alert' % form_id, 'innerHTML', '
Please fix the errors below
')
#    logger.error(form.__dict__)
#    for error in form.errors:
#      logger.error(error)
#      dajax.add_css_class('#div_id_%s' % error, 'error')
#    result = "failed"
#
#  dajax.add_data({'result': result, 'form_id': form_id}, 'js_callback')
#  return dajax.json()

def save_profile_personal_form(request, form, form_id, dajax):
  pass

def save_profile_speaker_form(request, form):
  pass

def save_profile_social_form(request, form):
  pass

def save_profile_notifications_form(request, form):
  pass

@dajaxice_register
def presentation_pagination(request, p, speakerprofile):

  logger.error(speakerprofile)

  try:
    page = int(p)
  except:
    page = 1

  presentations = get_presentations(speakerprofile, page)
  logger.error(presentations.paginator.num_pages)

  render = render_to_string('PresentationList.html', { 'presentations': presentations })

  dajax = Dajax()
  dajax.assign('#presentations', 'innerHTML', render)

  return dajax.json()

@dajaxice_register
def speaker_follow(request, speaker):
  dajax = Dajax()

  logger.error(request.user.profile)
  speaker = UserProfile.objects.get(user_hash=speaker)
  request.user.profile.add_relationship(speaker, RELATIONSHIP_FOLLOWING)

  return dajax.json()

@dajaxice_register
def speaker_unfollow(request, speaker):
  dajax = Dajax()

  logger.error(request.user.profile)
  speaker = UserProfile.objects.get(user_hash=speaker)
  request.user.profile.remove_relationship(speaker, RELATIONSHIP_FOLLOWING)

  return dajax.json()


import json
import csv

def json_to_csv(input_json_file, output_csv_file):
    try:
        # Read JSON data from input file using utf-8-sig encoding to handle BOM
        with open(input_json_file, 'r', encoding='utf-8-sig') as json_file:
            data = json.load(json_file)

        # Get the keys from the first JSON object to use as column headers
        headers = list(data[0].keys())

        # Write CSV data to output file
        with open(output_csv_file, 'w', newline='') as csv_file:
            writer = csv.DictWriter(csv_file, fieldnames=headers)

            # Write column headers to CSV file
            writer.writeheader()

            # Write JSON data to CSV file
            for row in data:
                writer.writerow(row)

        print(f"Conversion from JSON to CSV completed successfully. CSV file saved as {output_csv_file}")

    except Exception as e:
        print(f"An error occurred: {str(e)}")

# Example usage
input_json_file = 'path_to_input.json'
output_csv_file = 'path_to_output.csv'

json_to_csv(input_json_file, output_csv_file)


from typing import Dict, Optional, List, Union

import torch
import torch.nn as nn
from allennlp import common, data
from allennlp import nn as allen_nn
from allennlp.common import checks
from allennlp.modules import feedforward
from allennlp.nn import Activation

from combo.models import utils

class Predictor(nn.Module, common.Registrable):
    default_implementation = "feedforward_predictor_from_vocab"

    def forward(self,
                x: Union[torch.Tensor, List[torch.Tensor]],
                mask: Optional[torch.BoolTensor] = None,
                labels: Optional[Union[torch.Tensor, List[torch.Tensor]]] = None,
                sample_weights: Optional[Union[torch.Tensor, List[torch.Tensor]]] = None) -> Dict[str, torch.Tensor]:
        raise NotImplementedError()

class Linear(nn.Linear, common.FromParams):

    def __init__(self,
                 in_features: int,
                 out_features: int,
                 activation: Optional[allen_nn.Activation] = lambda x: x,
                 dropout_rate: Optional[float] = 0.0):
        super().__init__(in_features, out_features)
        self.activation = activation
        self.dropout = nn.Dropout(p=dropout_rate) if dropout_rate else lambda x: x

    def forward(self, x: torch.FloatTensor) -> torch.FloatTensor:
        x = super().forward(x)
        x = self.activation(x)
        return self.dropout(x)

    def get_output_dim(self) -> int:
        return self.out_features

@Predictor.register("feedforward_predictor")
@Predictor.register("feedforward_predictor_from_vocab", constructor="from_vocab")
class FeedForwardPredictor(Predictor):
    """Feedforward predictor. Should be used on top of Seq2Seq encoder."""

    def __init__(self, feedforward_network: feedforward.FeedForward):
        super().__init__()
        self.feedforward_network = feedforward_network

    def forward(self,
                x: Union[torch.Tensor, List[torch.Tensor]],
                mask: Optional[torch.BoolTensor] = None,
                labels: Optional[Union[torch.Tensor, List[torch.Tensor]]] = None,
                sample_weights: Optional[Union[torch.Tensor, List[torch.Tensor]]] = None) -> Dict[str, torch.Tensor]:
        if mask is None:
            mask = x.new_ones(x.size()[:-1])

        x = self.feedforward_network(x)
        output = {
            "prediction": x.argmax(-1),
            "probability": x
        }

        if labels is not None:
            if sample_weights is None:
                sample_weights = labels.new_ones([mask.size(0)])
            output["loss"] = self._loss(x, labels, mask, sample_weights)

        return output

    def _loss(self,
              pred: torch.Tensor,
              true: torch.Tensor,
              mask: torch.BoolTensor,
              sample_weights: torch.Tensor) -> torch.Tensor:
        BATCH_SIZE, _, CLASSES = pred.size()
        valid_positions = mask.sum()
        pred = pred.reshape(-1, CLASSES)
        true = true.reshape(-1)
        mask = mask.reshape(-1)
        loss = utils.masked_cross_entropy(pred, true, mask)
        loss = loss.reshape(BATCH_SIZE, -1) * sample_weights.unsqueeze(-1)
        return loss.sum() / valid_positions

    @classmethod
    def from_vocab(cls,
                   vocab: data.Vocabulary,
                   vocab_namespace: str,
                   input_dim: int,
                   num_layers: int,
                   hidden_dims: List[int],
                   activations: Union[Activation, List[Activation]],
                   dropout: Union[float, List[float]] = 0.0,
                   ):
        if len(hidden_dims) + 1 != num_layers:
            raise checks.ConfigurationError(
                f"len(hidden_dims) ({len(hidden_dims):d}) + 1 != num_layers ({num_layers:d})"
            )

        assert vocab_namespace in vocab.get_namespaces(), \
            f"There is not {vocab_namespace} in created vocabs, check if this field has any values to predict!"
        hidden_dims = hidden_dims + [vocab.get_vocab_size(vocab_namespace)]

        return cls(feedforward.FeedForward(
            input_dim=input_dim,
            num_layers=num_layers,
            hidden_dims=hidden_dims,
            activations=activations,
            dropout=dropout))


#!/usr/bin/env python3
""" This is a utility file that starts a dictionary based in-memory database
with a given timeseries length and a given number of vantage points.
It is meant to be an example of how to start our system and also
servers as a way for us to test our code.

Takes two arguments:
    -len : length of timeseries
    -vps : number of vantage points

IF YOU EDIT THIS FILE TESTS MIGHT BREAK. IT IS MEANT AS A DEMO ONLY.
"""

from tsdb import TSDBServer, DictDB
import timeseries as ts
import argparse
import sys

identity = lambda x: x
to_int = lambda x:int(x)
to_float = lambda x:float(x)
to_bool = lambda x:bool(x)

schema = {
  'pk': {'convert': identity, 'index': None},  #will be indexed anyways
  'ts': {'convert': identity, 'index': None},
  'order': {'convert': to_int, 'index': 1},
  'blarg': {'convert': to_int, 'index': 1},
  'useless': {'convert': identity, 'index': None},
  'mean': {'convert': to_float, 'index': 1},
  'std': {'convert': to_float, 'index': 1},
  'vp': {'convert': to_bool, 'index': 1},
  'vp_num': {'convert': to_int, 'index': 1},
}

TS_LENGTH = 100
NUMVPS = 5

def main():
    # we augment the schema by adding columns for 5 vantage points
    for i in range(NUMVPS):
        schema["d_vp-{}".format(i)] = {'convert': float, 'index': 1}
    db = DictDB(schema, 'pk',TS_LENGTH)
    server = TSDBServer(db)
    server.run()

if __name__=='__main__':
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('-len', help="length of timeseies",
                        type=int,default=TS_LENGTH,required=False)
    parser.add_argument('-vps', help="number of vantage points",
                        type=int,default=NUMVPS,required=False)
    args = parser.parse_args(sys.argv[1:])

    TS_LENGTH = args.len
    NUMVPS = args.vps

    main()


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Article',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, verbose_name='ID', serialize=False)),
                ('title', models.CharField(max_length=100)),
                ('content', models.TextField()),
                ('pub_date', models.DateField(auto_now_add=True)),
                ('change_date', models.DateTimeField(auto_now=True)),
            ],
            options={
                'ordering': ['-id'],
            },
        ),
        migrations.CreateModel(
            name='Tag',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, verbose_name='ID', serialize=False)),
                ('ParentTag', models.ForeignKey(to='myBlog.Tag')),
            ],
        ),
        migrations.AddField(
            model_name='article',
            name='tag',
            field=models.ForeignKey(to='myBlog.Tag'),
        ),
    ]


from flask import Blueprint, make_response, request, render_template
from flask_login import login_required, current_user
import constants
from app.models import User, Pantry
from app import db

bp = Blueprint('pantry', __name__, template_folder='templates')

@bp.route('/', methods=constants.http_verbs)
@login_required
def view_pantry():
    user = User.query.filter_by(id=current_user.id).first()
    if request.method == 'POST':
        payload = dict()
        pantry_items = list()
        for items in request.json:
            pantry_items.append(items['value'])
        for item in pantry_items:
            new_item = Pantry(owner=current_user.id, name=item)
            db.session.add(new_item)
            db.session.commit()
        res = make_response()
        return '', 200
    if request.method == 'GET':
        payload = dict()
        pantry_items = Pantry.query.filter_by(owner=current_user.id).all()
        if pantry_items:
            payload['pantry'] = pantry_items
        else:
            payload['message'] = "You don't have any items in your pantry. Would you like to add some?"
        return render_template('pantry_list.html', title='My Pantry', user=user, payload=payload)

@bp.route('/<item_id>', methods=constants.http_verbs)
@login_required
def delete_item(item_id):
    if request.method == 'DELETE':
        # Find the user
        user = User.query.filter_by(id=current_user.id).first()
        item = Pantry.query.filter_by(id=item_id).first()

        if item.owner == user.id:
            db.session.delete(item)
            db.session.commit()
            return "Successful deletion."
        else:
            return "Unable to delete this recipe."


'''
	Consolidate all CoNLL files
	into one CoNLL file for each video

	Author - Oyesh Mann Singh
	Date - 01/15/2020

	How to run:
		python utility/conlltodataset.py

    Output Directory structure:
       |_ Avenues_khabar
          |_ 0S8tX4eRa6M.conll
          |_ 3XI16CXFcJA.conll
          |_ .
          |_ .
       |_ Canada_Nepal
          |_ 7aSSjVrkYmI.conll
          |_ 7I4HfcAnYzA.conll
          |_ .
          |_ .
'''

import os
import argparse
import subprocess
import shutil

parser = argparse.ArgumentParser(add_help=True, description=('CoNLL to Final Dataset'))
parser.add_argument('--input_dir', '-idir', default='../brat/data/annotated/', metavar='PATH', help='Input path directory')
parser.add_argument('--output_dir', '-odir', default='./data/dataset', metavar='PATH', help='Output path directory')
parser.add_argument('--unicode', '-u', default='utf8', choices=['utf8','utf16'], metavar='UTF', help='Encoding format')

args = parser.parse_args()

def main():
    input_dir = args.input_dir
    output_dir = args.output_dir
    encoding = args.unicode

    # Create output directory
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

#   Delete channel directory in output folder if exists
    for channels in next(os.walk(input_dir))[1]:
        output_folder = os.path.join(output_dir, channels)
        if os.path.exists(output_folder):
            shutil.rmtree(output_folder)

    # Go through each folder
    # Create a conll file based on video name
    total_file = os.path.join(output_dir, 'total.conll')
    with open(total_file, 'w', encoding=encoding) as t_f:
        for root, dirs, files in os.walk(input_dir):
            for f in files:
                filename = os.path.join(root, f)
                if filename.split('.')[-1] == 'conll':
                    pathlist = filename.split('/')
                    channel_name = pathlist[-3]
                    output_folder = os.path.join(output_dir, channel_name)

                    if not os.path.exists(output_folder):
                        os.mkdir(output_folder)

                    video_name = pathlist[-2]
                    output_file = os.path.join(output_folder, video_name + '.conll')

                    with open(output_file, 'a', encoding=encoding) as o_f:
                        i_f = open(filename, 'r', encoding=encoding)
                        data = i_f.read()
                        o_f.write(data)
                        t_f.write(data)
                        o_f.write('\n')
                        t_f.write('\n')

if __name__ == "__main__":
	main()


import pygame.font
from pygame.sprite import Group

class Gameover():
    def __init__(self, ai_settings, screen):
        self.screen = screen
        self.screen_rect = screen.get_rect()
        self.ai_settings = ai_settings
        self.text_color = (255, 0, 00)
        self.font = pygame.font.SysFont(None, 96)
        self.prep_gameover()

    def prep_gameover(self):
        self.gameover_image = self.font.render("Game over", True, self.text_color, None)
        self.score_rect = self.gameover_image.get_rect()
        self.gameover_rect = self.gameover_image.get_rect()
        self.gameover_rect.centerx = self.screen_rect.centerx
        self.gameover_rect.top = self.screen_rect.top + 400

    def show_gameover(self):
        self.screen.blit(self.gameover_image, self.gameover_rect)


####CICD Tests

import unittest

import requests

class TestEndpoint(unittest.TestCase):

    def setUp(self):

        self.endpoint_url = '<YOUR ENDPOINT URL>'

        self.data = {'input': [1, 2, 3, 4]}

    def test_endpoint_response(self):

        response = requests.post(self.endpoint_url, json=self.data)

        self.assertEqual(response.status_code, 200)

    def test_endpoint_prediction(self):

        response = requests.post(self.endpoint_url, json=self.data)

        prediction = response.json()['prediction']

        self.assertIsInstance(prediction, list)

if __name__ == '__main__':

    unittest.main()


from flask import Flask,render_template,request
import pandas as pd

import csv,datetime
app=Flask(__name__)

from textblob import TextBlob as tb

@app.route('/')
def dashboard():
    return render_template('dashboard.html')

@app.route('/user')
def user():
    return render_template('user.html')

@app.route('/tables', methods=['POST','GET'])
def table():
    df2 = pd.read_csv("link/Dataset2.csv")
    df2 = df2.dropna()
    list_apps = sorted(set(df2["App"]))
    if request.method == "POST":
        app_name = request.form['app_name']
        dictionary = {
            "postitive" : [],
            "negative" : [],
            "neutral" : []
        }
        df = df2.loc[df2["App"]== app_name]

        df_pos = df[df["Sentiment"] == "Positive"]
        df_neg = df[df["Sentiment"] == "Negative"]
        df_neu = df[df["Sentiment"] == "Neutral"]

        dictionary['positive'] = list(df_pos["Translated_Review"])
        dictionary['negative'] = list(df_neg["Translated_Review"])
        dictionary['neutral'] = list(df_neu["Translated_Review"])

        total = len(df)
        per_pos = len(dictionary['positive'])/total * 100
        per_neg = len(dictionary['negative'])/total * 100
        per_neut = len(dictionary['neutral'])/total * 100

        return render_template('tables.html',ppos=per_pos,pneg=per_neg,pneu=per_neut,app_name=app_name,list_apps=list_apps,
                           pos=dictionary['positive'],neg=dictionary['negative'],neu=dictionary['neutral'])

    return render_template('tables.html',list_apps=list_apps)

@app.route('/add_data',methods=['GET','POST'])
def data_1():
    money=0
    message='Data Added'
    df=pd.read_csv('updated_proj_new.csv')
    app = request.form['App_name']
    cat = request.form['Category']
    rating = request.form['Rating']
    review = request.form['Review']
    size = request.form['Size']
    Installs = request.form.get('Installs')
    Type = request.form.get('Type')
    if Type == 'Paid':
        money= '$'+str(request.form['price'])
    content = request.form.get('content_rating')
    Genre = request.form.get('Genre')
    Last = datetime.datetime.strptime(request.form['Last'], '%Y-%m-%d')
    Last=Last.strftime('%d-%b-%Y')
    Current = request.form.get('Current')
    Android = request.form.get('Android')

    fields=[len(df)-1,app,cat,rating,review,size,Installs,Type,money,content,Genre,Last,Current,Android]
    with open(r'updated_proj_new.csv', 'a') as f:
        writer = csv.writer(f)
        writer.writerow(fields)

    return render_template('user.html',message = message)

@app.route('/add_data_1',methods=['GET','POST'])
def data_2():
    message='Data Added'
    app = request.form['app-name']
    rev= request.form['enter-review']

    sentiment=tb(rev)
    sent='Neutral'
    pol_val = sentiment.polarity

    if pol_val > 0.0:
        sent = 'Positive'
    elif pol_val<0.0:
        sent='Negative'

    sent_sub = sentiment.subjectivity

    fields=[app,rev,sent,pol_val,sent_sub]

    with open(r'updated_project_d2.csv', 'a') as f:
        writer = csv.writer(f)
        writer.writerow(fields)

    return render_template('user.html',message=message)

if __name__ == '__main__':
    app.run(debug=True)