"""
This module contains class Scenario only.
"""

from typing import TYPE_CHECKING, List, cast
import numpy
import scenario_candidates
from data_types import (
    ScenarioParameters,
    ScenarioOptions,
    OrderTypePropertyDict,
    PeerTypePropertyDict,
    EventOption,
    EventArrivalRate,
    PoissonArrivalRate,
    HawkesArrivalRate,
    ConcaveParameters,
    RandomParameter,
    AgeBasedParameters,
)

if TYPE_CHECKING:
    from message import Order

class Scenario:
    """
    The class Scenario describes our assumptions on the system setting.
    For examples, peer and order parameters, system evolving dynamics, event arrival pattern, etc.
    They describe the feature of the system, but is NOT part of our design space.
    """

    def __init__(
        self, parameters: ScenarioParameters, options: ScenarioOptions
    ) -> None:

        # unpacking parameters

        # on-chain verification speed. They are mean and var in numpy.random.lognormal()
        self.on_chain_mean: float = parameters.on_chain_verification.mean
        self.on_chain_var: float = parameters.on_chain_verification.var

        # order types and properties
        self.order_type_property: OrderTypePropertyDict = parameters.order_type_property
        # peer types and properties
        self.peer_type_property: PeerTypePropertyDict = parameters.peer_type_property

        # init period, init_size is number of peers joining the mesh at the very beginning,
        # and the birth time of such peers is randomly distributed over [0,birth_time_span]
        self.init_size: int = parameters.init_state.num_peers
        self.birth_time_span: int = parameters.init_state.birth_time_span

        # growing period (when number of peers increases)
        # An event (peer/order arrival/cancellation) happens according to some random process
        # (e.g., Poisson or Hawkes) that takes the rate as an input parameter(s).

        self.growth_rounds: int = parameters.growth_period.rounds
        self.growth_rates: List[EventArrivalRate] = [
            parameters.growth_period.peer_arrival,
            parameters.growth_period.peer_dept,
            parameters.growth_period.order_arrival,
        ]

        # stable period (number of peers and number of orders remain relatively stable)
        # We should choose the parameters such that peer arrival rate is approximately equal to
        # peer departure rate, and that order arrival rate is approximately equal to the total
        # order departure rate (due to cancellation, settlement, or expiration).

        self.stable_rounds: int = parameters.stable_period.rounds
        self.stable_rates: List[EventArrivalRate] = [
            parameters.stable_period.peer_arrival,
            parameters.stable_period.peer_dept,
            parameters.stable_period.order_arrival,
        ]

        # unpacking and setting options
        # options will determine the forms of implementations for functions in this class.
        # option_number_of_events determines event happening (peer/order arrival/dept) pattern.
        # Poisson and Hawkes processes are implemented.
        self.option_number_of_events: EventOption = options.event

    def generate_event_counts_over_time(
        self, rate: EventArrivalRate, max_time: int
    ) -> List[int]:
        """
        This method generates events according to some pattern. It reads
        self.option_number_of_events['method'] to determine the pattern, takes the expected rate
        (could be a value or a tuple of values), & the length of time slots as input, and outputs
        the number of incidents in each time slot.
        Current pattern implementations: Poisson process and Hawkes process.
        :param rate: expected rate of event happening. The type of this input depends on the
        method of generating the event.
        :param max_time: maximal time to generate events.
        :return: A realization of event happening, in terms of number of events happening in each
        slot.
        """

        if self.option_number_of_events["method"] == "Poisson":
            # check if "rate" is in the correct format
            if isinstance(rate, PoissonArrivalRate):
                return list(numpy.random.poisson(rate, max_time))
            raise TypeError(
                "Type of the rate is incorrect. Float is expected, but it is: "
                + str(type(rate))
                + "."
            )

        if self.option_number_of_events["method"] == "Hawkes":
            # Note that the rate parameters for Hawkes are a named tuple of variables,
            # explained in hawkes() function.
            if isinstance(rate, HawkesArrivalRate):
                return scenario_candidates.hawkes(rate, max_time)
            raise TypeError(
                "Type of the rate is incorrect. A tuple of 4 floats is "
                "expected, but it is: " + str(type(rate)) + "."
            )

        raise ValueError(
            f"No such option to generate events: {self.option_number_of_events['method']}"
        )

    @staticmethod
    def update_order_settled_status(order: "Order") -> None:
        """
        This method updates the is_settled status for an order.
        :param order: the order whose status is to be updated
        :return: None
        """
        if order.settlement["method"] == "ConcaveParameters":
            settlement_parameters = cast(ConcaveParameters, order.settlement)
            scenario_candidates.settle_concave(
                order,
                settlement_parameters["sensitivity"],
                settlement_parameters["max_prob"],
            )

        else:
            raise ValueError(
                f"No such method to change settlement status for orders: "
                f"{order.settlement['method']}"
            )

    @staticmethod
    def update_order_canceled_status(order: "Order", time_now) -> None:
        """
        This method updates order cancelled status.
        :param order: the order whose status is to be updated.
        :param time_now: system time.
        :return: None
        """

        if order.cancellation["method"] == "RandomParameter":
            cancellation_parameters_random = cast(RandomParameter, order.cancellation)
            scenario_candidates.cancel_random(
                order, cancellation_parameters_random["prob"]
            )
        elif order.cancellation["method"] == "AgeBasedParameters":
            cancellation_parameters_age = cast(AgeBasedParameters, order.cancellation)
            scenario_candidates.age_based_cancellation(
                order=order,
                time_now=time_now,
                sensitivity=cancellation_parameters_age["sensitivity"],
                max_prob=cancellation_parameters_age["max_prob"],
            )

        else:
            raise ValueError(
                f"No such method to change cancellation status for orders: "
                f"{order.cancellation['method']}"
            )

    def generate_server_response_time(self) -> List[int]:
        """
        This method generates a series of integers that represent Ethereum hosting server's
        response time. We use a log-normal distribution to model it.
        Log-normal distribution is a common one to model long-tail distributions. Please refer to
        https://en.wikipedia.org/wiki/Log-normal_distribution for details.
        :return: a list of response times.
        """
        float_list: List[float] = list(
            numpy.random.lognormal(
                mean=self.on_chain_mean,
                sigma=self.on_chain_var,
                # size is equal to the total length of simulation run (though we don't use the
                # time period [0, birth_time_span), we put it here for readability.
                size=self.birth_time_span + self.growth_rounds + self.stable_rounds,
            )
        )
        return_list: List[int] = [int(item) for item in float_list]
        return return_list

    def generate_server_response_time_all_zero(self) -> List[int]:
        """
        This method generates all zeros (assuming that on-chain check can finish immediately).
        This method is for comparison use only. It does not intend to simulate anything in reality.
        """
        return [0] * (self.birth_time_span + self.growth_rounds + self.stable_rounds)


import pygame
import struct

class Tile:
	def __init__(self, type, graphic, height):
		self.type = type
		self.graphic = graphic
		self.height = height

class TileType:
	STONE = 0

class Tileset:
	STONE = 0
	def __init__(self, fileName):
		image = pygame.image.load(fileName).convert()
		self.width, self.height = image.get_size()
		self.graphics = []
		self.graphics.append(image.subsurface((128, 826, 64, 32)))

class StageLoader:
	def __init__(self, fileName, tileset):
		self.fileName = fileName
		self.tileset = tileset
	def __enter__(self):
		self.open()
		return self
	def __exit__(self, type, value, traceback):
		self.close()
	def open(self):
		self.file = open(self.fileName, "rb")
		magicNumber = struct.unpack("i", self.file.read(4))[0]
		if magicNumber != 1011:
			raise "Bad magic number"
		self.width = struct.unpack("i", self.file.read(4))[0]
		self.height = struct.unpack("i", self.file.read(4))[0]
	def close(self):
		self.file.close()
	def readTile(self):
		type = struct.unpack("i", self.file.read(4))[0]
		#height = struct.unpack("i", self.file.read(4))[0]
		height = 0
		return Tile(type, self.tileset.graphics[type], height)

class Mob:
	pass


from flask import Flask, render_template, redirect
from flask_login import LoginManager
import os
import config as con
from work_with_db import db_session
from forms.user import RegisterForm, LoginForm
from work_with_db.Users import User

con.app = Flask(__name__)
con.app.config['SECRET_KEY'] = 'yandexlyceum_secret_key'
login_manager = LoginManager()
login_manager.init_app(con.app)

@login_manager.user_loader
def load_user(user_id):
    db_sess = db_session.create_session()
    return db_sess.query(User).get(user_id)

@con.app.route('/', methods=['GET', 'POST'])
def login():
    form = RegisterForm()
    form_log = LoginForm()
    if form.validate_on_submit():
        db_sess = db_session.create_session()
        if db_sess.query(User).filter(User.name == form.name.data).first():
            return render_template('login.html', title='DarkQuest',
                                   form=form,
                                   form_log=form_log,
                                   message="Такой пользователь уже есть")
        user = User(
            name=form.name.data,
            data='[]'
        )
        user.set_password(form.password.data)
        db_sess.add(user)
        db_sess.commit()
        return redirect('/fdb')
    return render_template('login.html', title='DarkQuest', form=form, form_log=form_log)

if __name__ == '__main__':
    db_session.global_init("db/base.sqlite")
    con.app.run()


#!/usr/bin/env python
#
# @copyright (c) 2012, OpenJudge Alliance <http://openjudge.net>
# @author LIU Yu <pineapple.liu@gmail.com>
# @date 2012/10/10
#
# This program source code is part of the OpenJudge Alliance Technical Report
# (TR-OJA-201209A) at <http://openjudge.net/TR/201209A>.
#

from ctypes import Structure, sizeof
from ctypes import c_uint32, c_uint64, c_int32, c_int64, c_int
from platform import system, machine
from sandbox import *

# check platform type
if system() not in ('Linux', ) or machine() not in ('x86_64', 'i686', ):
    raise AssertionError("Unsupported platform type.\n")

# sandbox with predictive out-of-quota (output) detection
class PredictiveOutputQuotaSandbox(SandboxPolicy,Sandbox):
    def __init__(self, *args, **kwds):
        # Linux system calls for fd write
        if machine() == 'x86_64': # x86_64 (multiarch) system calls
            x86_64, i686 = 0, 1
            SC_write = ((1, x86_64), (4, i686), )
            SC_pwrite64 = ((18, x86_64), (181, i686), )
            SC_writev = ((20, x86_64), (146, i686), )
            SC_pwritev = ((296, x86_64), (334, i686), )
        else: # i686 system calls
            SC_write = (4, )
            SC_pwrite64 = (181, )
            SC_writev = (146, )
            SC_pwritev = (334, )
        # table of system call rules
        self.sc_table = {}
        for entry, handler in zip((SC_write, SC_pwrite64, SC_writev, SC_pwritev), \
            (self.SYS_write, self.SYS_write, self.SYS_writev, self.SYS_writev)):
            for sc in entry:
                self.sc_table[sc] = handler
        # policy internal states
        self.written_bytes = 0
        self.pending_bytes = 0
        # initalize as a polymorphic sandbox-and-policy object
        SandboxPolicy.__init__(self)
        Sandbox.__init__(self, *args, **kwds)
        self.policy = self
        pass
    def probe(self):
        # add custom entries into the probe dict
        d = Sandbox.probe(self, False)
        d['out'] = (self.written_bytes, self.written_bytes + self.pending_bytes)
        return d
    def __call__(self, e, a):
        # handle SYSCALL/SYSRET events with local rules
        if e.type in (S_EVENT_SYSCALL, S_EVENT_SYSRET):
            sc = (e.data, e.ext0) if machine() == 'x86_64' else e.data
            if sc in self.sc_table:
                return self.sc_table[sc](e, a)
        # bypass other events to base class
        return SandboxPolicy.__call__(self, e, a)
    def SYS_write(self, e, a): # write / pwrite64
        abi64 = (machine() == 'x86_64' and e.ext0 == 0)
        ssize_t, size_t = (c_int64, c_uint64) if abi64 else (c_int32, c_uint32)
        if e.type == S_EVENT_SYSCALL:
            self.pending_bytes = size_t(e.ext3).value
        else:
            if ssize_t(e.ext1).value > 0:
                self.written_bytes += ssize_t(e.ext1).value
            self.pending_bytes = 0
        return self._output_check(e, a)
    def SYS_writev(self, e, a): # writev / pwritev
        abi64 = (machine() == 'x86_64' and e.ext0 == 0)
        ssize_t = c_int64 if abi64 else c_int32
        if e.type == S_EVENT_SYSCALL:
            # enumerate the struct iovec[] to count pending bytes
            address, iovcnt = e.ext2, c_int(e.ext3).value
            self.pending_bytes = 0
            for i in range(iovcnt):
                iovec = self._dump_iovec(abi64, address)
                self.pending_bytes += iovec.iov_len
                address += sizeof(iovec)
        else:
            if ssize_t(e.ext1).value > 0:
                self.written_bytes += ssize_t(e.ext1).value
            self.pending_bytes = 0
        return self._output_check(e, a)
    def _dump_iovec(self, abi64, address):
        # dump a struct iovec object from the given address
        typeid = T_ULONG if abi64 else T_UINT
        size_t, char_p = (c_uint64, ) * 2 if abi64 else (c_uint32, ) * 2
        class struct_iovec(Structure): # from manpage writev(2)
             _fields_ = [('iov_base', char_p), ('iov_len', size_t), ]
        return struct_iovec(self.dump(typeid, address), \
            self.dump(typeid, address + sizeof(char_p)))
    def _output_check(self, e, a):
        # compare current written + pending bytes against the quota
        if self.written_bytes + self.pending_bytes > self.quota[3]:
            return self._KILL_OL(e, a)
        return self._CONT(e, a)
    def _CONT(self, e, a): # continue
        a.type = S_ACTION_CONT
        return a
    def _KILL_RF(self, e, a): # restricted func.
        a.type, a.data = S_ACTION_KILL, S_RESULT_RF
        return a
    def _KILL_OL(self, e, a): # disk output limit exceeded
        a.type, a.data = S_ACTION_KILL, S_RESULT_OL
        return a
    pass

result_name = dict((getattr(Sandbox, 'S_RESULT_%s' % i), i) for i in \
    ('PD', 'OK', 'RF', 'RT', 'TL', 'ML', 'OL', 'AT', 'IE', 'BP'))

if __name__ == '__main__':
    s = PredictiveOutputQuotaSandbox("./write.exe", quota=dict(disk=64))
    s.run()
    print("result: %s" % result_name.get(s.result, 'NA'))
    print("out: %dB / %dB" % s.probe()['out']) # written / predicted


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('img', '0002_auto_20150113_1611'),
    ]

    operations = [
        migrations.AlterField(
            model_name='img',
            name='content_type',
            field=models.ForeignKey(related_name='related_Photo', blank=True, to='contenttypes.ContentType', null=True),
            preserve_default=True,
        ),
        migrations.AlterField(
            model_name='img',
            name='object_id',
            field=models.PositiveIntegerField(db_index=True, null=True, blank=True),
            preserve_default=True,
        ),
        migrations.AlterField(
            model_name='img',
            name='title',
            field=models.CharField(help_text='title <a> \u0437\u0430\u043f\u0438\u0441\u0438.', max_length=256, null=True, verbose_name='\u0417\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f', blank=True),
            preserve_default=True,
        ),
    ]


"""Place image views."""

from django.conf.urls import url
from django.views.generic import ListView

from ..models.place_image import PlaceImage

class PlaceImageListView(ListView):
    """List of Place images."""

    template_name = 'place_image/list.html'

    def get_queryset(self):
        place_pk = self.kwargs.get('pk')

        return PlaceImage.objects.filter(
            place__id=place_pk,
        )

urlpatterns = [
    url(
        '^images$',
        PlaceImageListView.as_view(),
        name='place_image_list'
    ),
]


class SeaofBTCapp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        #fill, let's fill based on limitations you set. Expand, let's fill everything we can
        container.pack(side='top', fill='both', expand=True)
        #0 is a size component, weight is about priority
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = []

        frame = StartPage(container, self)

        self.frames[StartPage] = frame

        #alignment and stretch (north, south, east, west)
        frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


import json
import re
import time

import jwt
import structlog

from utils import get_access_token_jwt_secret, bad_request, is_allowed_domain

def handler(event, context) -> dict:
    del context  # unused
    assert event['httpMethod'] == 'POST'

    structlog.get_logger().msg("Processing POST request", body=event)
    caller = event['requestContext']['identity']['caller']
    values = json.loads(event['body'])

    try:
        exp_in = int(values['exp_in'])

        subject = values['subject']
        assert len(subject) > 0
    except (KeyError, AssertionError):
        return bad_request('mandatory fields not present')

    if exp_in > (60 * 60 * 6):
        return bad_request('expiration too long (max 6 hours)')

    domains = set(values['domains'])
    for domain in domains:
        if not re.match(r'^[a-zA-Z0-9.-]+$', domain):
            return bad_request('', f"`{domain}` does not look like a domain name")

        if not is_allowed_domain(domain):
            return bad_request('', 'Unknown domain in request')

    now = int(time.time())
    ci_token = {
        'iat': now,
        'exp': now + exp_in,
        'azp': caller,
        'domains': list(domains),
        'sub': subject,
    }
    structlog.get_logger().msg("Issuing JWT", jwt=ci_token)
    raw_ci_token = jwt.encode(
        ci_token,
        get_access_token_jwt_secret(),
        algorithm='HS256',
    )

    return {
        'statusCode': 200,
        'headers': {
            'Content-Type': 'text/plain',
        },
        'body': raw_ci_token,
    }


import os
from flask import Flask

def create_app(test_config = None):
    #create flask instance
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY = "pripara",
        DATABASE = os.path.join(app.instance_path, 'flaskr.sqlite')
    )

    if test_config is None:
        #load the instance  config
        app.config.from_pyfile('config.py', silent = True)
    else:
        #load the test config if passed in
        app.config.from_mapping(test_config)

    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    #a simple page that says hello
    @app.route('/hello')
    def hello():
        return 'hello,world'

    return app


cars = ['audi', 'bmw', 'subaru', 'toyota']
for car in cars:
    if car == 'bmw':
        print(car.upper())
    else:
        print(car.title())

# Testing for equality is case sensitive in Python

# Conditional Operator: == != < > <= >= and or

banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users: # in / not in
    print(user.title() + ", you can post a response if you wish.")

# Boolean variables
game_active = True
can_edit = False

age = 17
if age >= 18:
    print("You are old enough to vote!")
    print("Have you registered to vote yet?")
else:
    print("Sorry, you are too young to vote.")
    print("Please register to vote as soon as you turn 18!")

age = 12
if age < 4:
    print("Your admission cost is $0.")
elif age < 18:
    print("Your admission cost is $5.")
else:
    print("Your admission cost is $10.")

age = 12
if age < 4:
    price = 0
elif age < 18:
    price = 5
else:
    price = 10

print("Your admission cost is $" + str(price) + ".")

requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings:
    print("Adding mushrooms.")
if 'pepperoni' in requested_toppings:
    print("Adding pepperoni.")
if 'extra cheese' in requested_toppings:
    print("Adding extra cheese.")
print("\nFinished making your pizza!")

# 5-3 -> 5-7
alien_color = 'red'

if alien_color == 'green':
    print("You have just earned 5 points.")

if alien_color == 'red':
    print("You have just earned 5 points.")

if alien_color == 'green':
    print("You have just earned 5 points.")
elif alien_color == 'yellow':
    print("You have just earned 10 points.")
elif alien_color == 'red':
    print("You have just earned 15 points.")

age = 21

if age < 2:
    print("you are a baby.")
elif age < 4:
    print("You are a toddler.")
elif age < 13:
    print("You are a kid.")
elif age < 20:
    print("You are a teenager.")
elif age < 65:
    print("You are an adult.")
else:
    print("You are an elder.")

favorite_fruits = ["apple", "banana", "grape"]

if "lemon" not in favorite_fruits:
    print("lemon is not my favorite.")

if "apple" in favorite_fruits:
    print("apple is my favorite.")


import parted
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtWidgets import QWidget, QLabel, QComboBox, QPushButton, QTreeWidget, QTreeWidgetItem, \
    QMessageBox, QDialog, QCheckBox, QGridLayout, QInputDialog, QHeaderView
from PyQt5.QtCore import Qt

class Disk(QWidget):
    def __init__(self, ebeveyn=None):
        super(Disk, self).__init__(ebeveyn)
        self.e = ebeveyn
        diskler_kutu = QGridLayout()
        self.setLayout(diskler_kutu)

        self.sistemDiski = ["", ""]
        self.takasDiski = ["", ""]
        self.seciliDisk = None
        self.diskler = parted.getAllDevices()
        self.disklerAcilirKutu = QComboBox()
        self.yenileButon = QPushButton()
        self.yenileButon.setIcon(QIcon("./resimler/yenile.svg"))
        self.yenileButon.pressed.connect(self.diskYenile)

        self.bolumListeKutu = QTreeWidget()
        self.bolumListeKutu.setColumnCount(4)
        self.bolumListeKutu.header().setStretchLastSection(False);
        self.bolumListeKutu.header().setSectionResizeMode(0, QHeaderView.Stretch);
        self.bolumListeKutu.header().setSectionResizeMode(1, QHeaderView.Stretch);
        self.bolumListeKutu.header().setSectionResizeMode(2, QHeaderView.Stretch);
        self.bolumListeKutu.header().setSectionResizeMode(3, QHeaderView.Stretch);

        self.disklerAcilirKutu.currentIndexChanged.connect(self.diskDegisti)

        diskler_kutu.addWidget(self.disklerAcilirKutu,0,0,1,6)
        diskler_kutu.addWidget(self.yenileButon,0,6,1,2)
        diskler_kutu.addWidget(self.bolumListeKutu,1,0,1,8)

        resim_label = QLabel()
        resim_label.setFixedSize(16,16)
        resim_label.setPixmap(QPixmap("./resimler/blank.svg"))
        diskler_kutu.addWidget(resim_label,2,0,1,1)
        self.blank_label = QLabel()
        diskler_kutu.addWidget(self.blank_label,2,1,1,1)
        resim_label = QLabel()
        resim_label.setFixedSize(16, 16)
        resim_label.setPixmap(QPixmap("./resimler/extended.svg"))
        diskler_kutu.addWidget(resim_label,2,2,1,1)
        self.extended_label = QLabel()
        diskler_kutu.addWidget(self.extended_label,2,3,1,1)
        resim_label = QLabel()
        resim_label.setFixedSize(16, 16)
        resim_label.setPixmap(QPixmap("./resimler/logical.svg"))
        diskler_kutu.addWidget(resim_label,2,4,1,1)
        self.logical_label = QLabel()
        diskler_kutu.addWidget(self.logical_label,2,5,1,1)
        resim_label = QLabel()
        resim_label.setFixedSize(16, 16)
        resim_label.setPixmap(QPixmap("./resimler/primary.svg"))
        diskler_kutu.addWidget(resim_label,2,6,1,1)
        self.primary_label = QLabel()
        diskler_kutu.addWidget(self.primary_label,2,7,1,1)

        self.bolumListeKutu.itemClicked.connect(self.bolumSecildiFonk)
        self.bolumListeKutu.itemDoubleClicked.connect(self.bolumFormatSecFonk)

        self.yeniBolumBtn = QPushButton()
        self.yeniBolumBtn.setIcon(QIcon("./resimler/yeni_bolum.svg"))
        self.yeniBolumBtn.pressed.connect(self.bolumEkleFonk)
        self.bolumSilBtn = QPushButton()
        self.bolumSilBtn.setIcon(QIcon("./resimler/bolum_sil.svg"))
        self.bolumSilBtn.pressed.connect(self.bolumSilFonk)
        diskler_kutu.addWidget(self.yeniBolumBtn,3,4,1,2)
        diskler_kutu.addWidget(self.bolumSilBtn,3,6,1,2)

        self.bolumSilBtn.setEnabled(False)
        self.diskYenile()

    def diskYenile(self):
        self.disklerAcilirKutu.clear()
        self.diskler = parted.getAllDevices()
        for disk in self.diskler:
            try:
                if parted.Disk(disk).type == "msdos" or parted.Disk(disk).type == "gpt" :
                    self.disklerAcilirKutu.addItem(
                        "{} {} GB ({})".format(disk.model, format(disk.getSize(unit="GB"), '.2f'), disk.path),
                        userData=disk.path)
            except parted.DiskLabelException:
                disk = parted.freshDisk(disk, "msdos")
                # CDROM Aygıtları için
                try:
                    disk.commit()
                except parted.IOException:
                    pass
                else:
                    disk = disk.device
                    self.disklerAcilirKutu.addItem(
                        "{} {} GB ({})".format(disk.model, format(disk.getSize(unit="GB"), '.2f'), disk.path),
                        userData=disk.path)

    def diskDegisti(self):
        if self.disklerAcilirKutu.currentData():
            self.aygit = parted.getDevice(self.disklerAcilirKutu.currentData())
            self.e.disk = parted.Disk(self.aygit)
            self.bolumListeYenile()

    def bolumListeYenile(self):
        self.extended = None
        self.bolumListeKutu.clear()
        for bolum in self.e.disk.partitions:
            _bolum = self.bolumBilgi(bolum, "GB")
            if self.sistemDiski and bolum.path == self.sistemDiski[0]:
                if self.sistemDiski[1]:
                    item = self.treeWidgetItemOlustur(_bolum["yol"], self.e.d[self.e.s_d]["Sistem Diski"], _bolum["boyut"],
                                                      "ext4", _bolum["bayraklar"], _bolum["no"])
                else:
                    item = self.treeWidgetItemOlustur(_bolum["yol"], self.e.d[self.e.s_d]["Sistem Diski"], _bolum["boyut"],
                                                      _bolum["dosyaSis"], _bolum["bayraklar"], _bolum["no"])
            elif self.takasDiski and bolum.path == self.takasDiski[0]:
                item = self.treeWidgetItemOlustur(_bolum["yol"], self.e.d[self.e.s_d]["Takas Alanı"], _bolum["boyut"],
                                                  self.e.d[self.e.s_d]["takas"], _bolum["bayraklar"], _bolum["no"])
            else:
                item = self.treeWidgetItemOlustur(_bolum["yol"], "", _bolum["boyut"], _bolum["dosyaSis"],
                                                  _bolum["bayraklar"], _bolum["no"])

            if _bolum["tur"] == parted.PARTITION_NORMAL:
                item.setIcon(0, QIcon("./resimler/primary.svg"))
            elif _bolum["tur"] == parted.PARTITION_EXTENDED:
                item.setIcon(0, QIcon("./resimler/extended.svg"))
                self.extended = item
            elif _bolum["tur"] == parted.PARTITION_LOGICAL:
                item.setIcon(0, QIcon("./resimler/logical.svg"))
                self.extended.addChild(item)
                self.extended.setExpanded(True)
            self.bolumListeKutu.addTopLevelItem(item)

        for bosBolum in self.e.disk.getFreeSpacePartitions():
            _toplam = 0
            _bolum = self.bolumBilgi(bosBolum, "GB")
            if float(_bolum["boyut"]) > 1:
                if _bolum["tur"] == 5:
                    uzatilmisKalan = self.treeWidgetItemOlustur("", self.e.d[self.e.s_d]["Uzatılmış Bölüm Kalan"], _bolum["boyut"],
                                                                "", "", "ayrilmamis")
                    uzatilmisKalan.setIcon(0, QIcon("./resimler/blank.svg"))
                    self.extended.addChild(uzatilmisKalan)
                    self.extended.setExpanded(True)
                if _bolum["tur"] == parted.PARTITION_FREESPACE:
                    _toplam = _toplam + float(_bolum["boyut"])
                ayrilmamis = self.treeWidgetItemOlustur("", self.e.d[self.e.s_d]["Ayrılmamış Bölüm"], _toplam, "", "", "ayrilmamis")
                ayrilmamis.setIcon(0, QIcon("./resimler/blank.svg"))
                self.bolumListeKutu.addTopLevelItem(ayrilmamis)

    def treeWidgetItemOlustur(self, bolum, kullanim, boyut, format, islev, bolumno):
        item = QTreeWidgetItem()
        item.setText(0, str(bolum))
        item.setText(1, str(kullanim))
        item.setText(2, str(boyut) + " GB ")
        item.setText(3, str(format))
        item.setData(0,Qt.UserRole, bolumno)
        return item

    def bolumSecildiFonk(self, tiklanan):
        if tiklanan.data(0,Qt.UserRole) != "ayrilmamis":
            self.bolumSilBtn.setEnabled(True)
        else:
            self.bolumSilBtn.setEnabled(False)

    def bolumFormatSecFonk(self, tiklanan):
        if tiklanan.data(0,Qt.UserRole) != "ayrilmamis":
            self.seciliDisk = tiklanan
            diskOzellikPencere = diskOzellikleriSinif(self)
            diskOzellikPencere.exec_()
            if self.sistemDiski[0] != "":
                self.e.milis_ayarlar["disk_bolum"] = self.sistemDiski[0]
                self.e.milis_ayarlar["disk_format"] = self.sistemDiski[1]
            if self.takasDiski[0] != "":
                self.e.milis_ayarlar["disk_takasbolum"] = self.takasDiski[0]
            else:
                self.e.milis_ayarlar["disk_takasbolum"] = ""

            if self.sistemDiski[0] == "":
                pass
            elif self.sistemDiski[0] != "" and self.takasDiski[0] == "":
                QMessageBox.information(self, self.e.d[self.e.s_d]["Bilgi"], self.e.d[self.e.s_d]["Takas Alanı Belirtmediniz Takas alanı ram miktarınızın düşük olduğu durumlarda ram yerine disk kullanarak işlemlerin devam etmesini sağlar."])
                self.e.ileri_dugme.setDisabled(False)
                self.bolumListeYenile()
            elif self.sistemDiski[0] != "" and self.takasDiski[0] != "":
                if self.sistemDiski[0] == self.takasDiski[0]:
                    QMessageBox.warning(self, self.e.d[self.e.s_d]["Hata"], self.takasDiski[0] + self.e.d[self.e.s_d][" diskini hem sistem hem takas için seçtiniz. Aynı diski hem sistem hem takas olarak kullanmazsınız."])
                    self.e.ileri_dugme.setDisabled(True)
                else:
                    self.e.ileri_dugme.setDisabled(False)
                    self.bolumListeYenile()

    def bolumEkleFonk(self):
        if self._en_buyuk_bos_alan():
            alan = self._en_buyuk_bos_alan()
            birincilSayi = len(self.e.disk.getPrimaryPartitions())
            uzatilmisSayi = ext_count = 1 if self.e.disk.getExtendedPartition() else 0
            parts_avail = self.e.disk.maxPrimaryPartitionCount - (birincilSayi + uzatilmisSayi)
            if not parts_avail and not ext_count:
                QMessageBox.warning(self, self.e.d[self.e.s_d]["Uyarı"],self.e.d[self.e.s_d]["Eğer dörtten fazla disk bölümü oluşturmak istiyorsanız birincil bölümlerden birini silip uzatılmış bölüm oluşturun.Bu durumda oluşturduğunuz uzatılmış bölümleri işletim sistemi kurmak için kullanamayacağınızı aklınızda bulundurun."])
            else:
                if parts_avail:
                    if not uzatilmisSayi and parts_avail > 1:
                        self.bolumOlustur(alan, parted.PARTITION_NORMAL)
                        self.bolumListeYenile()
                    elif parts_avail == 1:
                        self.bolumOlustur(alan, parted.PARTITION_EXTENDED)
                        self.bolumListeYenile()
                if uzatilmisSayi:
                    ext_part = self.e.disk.getExtendedPartition()
                    try:
                        alan = ext_part.geometry.intersect(alan)
                    except ArithmeticError:
                        QMessageBox.critical(self, self.e.d[self.e.s_d]["Hata"], self.e.d[self.e.s_d]["Yeni disk bölümü oluşturmak için yeterli alan yok ! Uzatılmış bölümün boyutunu arttırmayı deneyiniz."])
                    else:
                        self.bolumOlustur(alan, parted.PARTITION_LOGICAL)
                        self.bolumListeYenile()

    def bolumSilFonk(self):
        if self.bolumListeKutu.currentItem().data(0,Qt.UserRole) != "ayrilmamis":
            bolumNo = int(self.bolumListeKutu.currentItem().data(0,Qt.UserRole))
            for bolum in self.e.disk.partitions:
                if bolum.number == bolumNo:
                    try:
                        self.e.disk.deletePartition(bolum)
                        self.bolumListeYenile()
                    except parted.PartitionException:
                        QMessageBox.warning(self, self.e.d[self.e.s_d]["Uyarı"], self.e.d[self.e.s_d]["Lütfen uzatılmış bölümleri silmeden önce mantıksal bölümleri siliniz."])
            self.bolumSilBtn.setDisabled(True)

    def bolumBilgi(self, bolum, birim):
        _bolum = {}
        _bolum["yol"] = bolum.path
        if birim == "GB":
            _bolum["boyut"] = format(bolum.getSize(unit=birim), '.2f')
        else:
            _bolum["boyut"] = bolum.getSize(unit=birim)
        _bolum["dosyaSis"] = "Bilinmeyen"

        if bolum.fileSystem:
            if bolum.fileSystem.type.startswith('linux-swap'):
                _bolum["dosyaSis"] = "takas"
            else:
                _bolum["dosyaSis"] = bolum.fileSystem.type
        try:
            _bolum["bayraklar"] = bolum.getFlagsAsString()
        except:
            pass
        _bolum["no"] = bolum.number
        _bolum["tur"] = bolum.type
        return _bolum

    def _en_buyuk_bos_alan(self):
        maks_boyut = -1
        alan = None
        alignment = self.aygit.optimumAlignment

        for _alan in self.e.disk.getFreeSpaceRegions():
            if _alan.length > maks_boyut and _alan.length > alignment.grainSize:
                alan = _alan
                maks_boyut = _alan.length
        return alan

    def bolumOlustur(self, alan, bolumTur):
        if bolumTur == parted.PARTITION_NORMAL or bolumTur == parted.PARTITION_EXTENDED:
            for bosBolum in self.e.disk.getFreeSpacePartitions():
                _bolum = self.bolumBilgi(bosBolum, "GB")
                if _bolum["tur"] == parted.PARTITION_FREESPACE:
                    maksBoyut = float(_bolum["boyut"])
        elif bolumTur == bolumTur == parted.PARTITION_LOGICAL:
            for bosBolum in self.e.disk.getFreeSpacePartitions():
                _bolum = self.bolumBilgi(bosBolum, "GB")
                if _bolum["tur"] == 5:
                    maksBoyut = float(_bolum["boyut"])

        alignment = self.aygit.optimalAlignedConstraint
        constraint = self.aygit.getConstraint()
        data = {
            'start': constraint.startAlign.alignUp(alan, alan.start),
            'end': constraint.endAlign.alignDown(alan, alan.end),
        }

        boyut, ok = QInputDialog().getDouble(self, self.e.d[self.e.s_d]['Bölüm oluştur'], self.e.d[self.e.s_d]['GB cinsinden boyut'], min=0.001,
                                             value=1, max=maksBoyut, decimals=3)

        if ok:
            data["end"] = int(data["start"]) + int(parted.sizeToSectors(float(boyut), "GiB", self.aygit.sectorSize))
            try:
                geometry = parted.Geometry(device=self.aygit, start=int(data["start"]), end=int(data["end"]))
                partition = parted.Partition(
                    disk=self.e.disk,
                    type=bolumTur,
                    geometry=geometry,
                )

                self.e.disk.addPartition(partition=partition, constraint=constraint)
            except (parted.PartitionException, parted.GeometryException, parted.CreateException) as e:
                raise RuntimeError(e.message)

    def showEvent(self, event):
        self.e.setWindowTitle(self.e.d[self.e.s_d]["Disk Yapılandırma"])
        self.yenileButon.setText(self.e.d[self.e.s_d]["  Yenile"])
        self.bolumListeKutu.headerItem().setText(0, self.e.d[self.e.s_d]["Bölüm"])
        self.bolumListeKutu.headerItem().setText(1, self.e.d[self.e.s_d]["Kullanım Şekli"])
        self.bolumListeKutu.headerItem().setText(2, self.e.d[self.e.s_d]["Boyut"])
        self.bolumListeKutu.headerItem().setText(3, self.e.d[self.e.s_d]["Dosya Sistemi"])
        self.yeniBolumBtn.setText(self.e.d[self.e.s_d]["  Yeni Bölüm Ekle"])
        self.bolumSilBtn.setText(self.e.d[self.e.s_d]["  Bölümü Sil"])
        self.blank_label.setText(self.e.d[self.e.s_d]["Birincil Bölüm"])
        self.extended_label.setText(self.e.d[self.e.s_d]["Uzatılmış Bölüm"])
        self.logical_label.setText(self.e.d[self.e.s_d]["Mantıksal Bölüm"])
        self.primary_label.setText(self.e.d[self.e.s_d]["Ayrılmamış Bölüm"])
        self.e.ileri_dugme.setDisabled(True)

class diskOzellikleriSinif(QDialog):
    def __init__(self, ebeveyn=None):
        super(diskOzellikleriSinif, self).__init__(ebeveyn)
        self.e = ebeveyn
        disk_ = self.e.seciliDisk
        self.baslik_ = disk_.text(0)
        self.boyut_ = float(disk_.text(2).replace(" GB",""))
        format_ = disk_.text(3)
        self.setWindowTitle(self.baslik_)
        diskOzellikKutu = QGridLayout()
        self.setLayout(diskOzellikKutu)
        self.secenekAcilirListe = QComboBox()
        self.secenekAcilirListe.addItem(self.e.e.d[self.e.e.s_d]["Sistem Diski"])
        self.secenekAcilirListe.addItem(self.e.e.d[self.e.e.s_d]["Takas Alanı"])
        diskOzellikKutu.addWidget(self.secenekAcilirListe, 0, 0, 1, 1)
        self.diskBicimlendirKutu = QCheckBox(self.e.e.d[self.e.e.s_d]["Diski Biçimlendir"])
        if format_ != "ext4":
            self.diskBicimlendirKutu.setChecked(True)
            self.diskBicimlendirKutu.setDisabled(True)
        diskOzellikKutu.addWidget(self.diskBicimlendirKutu, 1, 0, 1, 1)
        tamamDugme = QPushButton(self.e.e.d[self.e.e.s_d]["Tamam"])
        tamamDugme.pressed.connect(self.tamamBasildiFonk)
        diskOzellikKutu.addWidget(tamamDugme, 2, 0, 1, 1)

    def tamamBasildiFonk(self):
        if self.secenekAcilirListe.currentText() == self.e.e.d[self.e.e.s_d]["Sistem Diski"]:
            komut = "LC_ALL=C df -h / | awk '{ print $3 }' | tail -n 1 | sed 's/G//'g"
            boyut = self.e.e.komutCalistirFonksiyon(komut)
            if self.boyut_ >= round(float(boyut)):
                self.e.sistemDiski = [self.baslik_]
                if self.diskBicimlendirKutu.isChecked():
                    self.e.sistemDiski.append(True)
                else:
                    self.e.sistemDiski.append(False)
            else:
                    QMessageBox.critical(self, self.e.e.d[self.e.e.s_d]["Hata"], self.e.e.d[self.e.e.s_d]["Bu bölüm sistem diski oluşturmak için çok küçük ! Sistem diski seçilecek bölüm en azından {} GB boyutunda olmalıdır."].format(round(float(boyut))))
        elif self.secenekAcilirListe.currentText() == self.e.e.d[self.e.e.s_d]["Takas Alanı"]:
            self.e.takasDiski = [self.baslik_]
            if self.diskBicimlendirKutu.isChecked():
                self.e.takasDiski.append(True)
            else:
                self.e.takasDiski.append(False)
        QDialog.accept(self)


from textblob import TextBlob
from flask import request
import re
import random

from flask import Flask
app = Flask(__name__)

@app.route('/',methods = ['POST'])
def get_tweet_polarity():
    tweet = request.get_json()
    print(tweet)
    text=tweet["text"]
    cleanedTweet=' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)|(RT)", " ", text).split())
    analysis=TextBlob(cleanedTweet)
    tweet["polarity"] = analysis.polarity
    #tweet["polarity"] = random.uniform(-1, 1)
    return tweet

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


#!/usr/bin/env python

# Copyright (c) 2014, Palo Alto Networks
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

# Author: Brian Torres-Gil <btorres-gil@paloaltonetworks.com>

"""Policies module contains policies and rules that exist in the 'Policies' tab in the firewall GUI"""

# import modules
import logging
from base import PanObject, Root, MEMBER, ENTRY
from base import VarPath as Var

# import other parts of this pandevice package
import errors as err

# set logging to nullhandler to prevent exceptions if logging not enabled
logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())

class Rulebase(PanObject):
    """Rulebase for a Firewall

    Firewall only.  For Panorama, use :class:`pandevice.policies.PreRulebase` or
    :class:`pandevice.policies.PostRulebase`.

    """
    ROOT = Root.VSYS
    XPATH = "/rulebase"
    CHILDTYPES = (
        "policies.SecurityRule",
    )

class PreRulebase(Rulebase):
    """Pre-rulebase for a Panorama

    Panorama only.  For Firewall, use :class:`pandevice.policies.Rulebase`.

    """
    XPATH = "/pre-rulebase"

class PostRulebase(Rulebase):
    """Pre-rulebase for a Panorama

    Panorama only.  For Firewall, use :class:`pandevice.policies.Rulebase`.

    """
    XPATH = "/post-rulebase"

class SecurityRule(PanObject):
    """Security Rule

    Firewall only.  Use SecurityPreRule or SecurityPostRule with Panorama.

    Args:
        name (str): Name of the rule
        fromzone (list): From zones
        tozone (list): To zones
        source (list): Source addresses
        destination (list): Destination addresses
        application (list): Applications
        service (list): Destination services (ports) (Default: application-default)
        category (list): Destination URL Categories
        action (str): Action to take (deny, allow, drop, reset-client, reset-server, reset-both)
            Note: Not all options are available on all PAN-OS versions.
        log_setting (str): Log forwarding profile
        log_start (bool): Log at session start
        log_end (bool): Log at session end
        description (str): Description of this rule
        type (str): 'universal', 'intrazone', or 'intrazone' (Default: universal)
        tag (list): Administrative tags
        negate_source (bool): Match on the reverse of the 'source' attribute
        negate_destination (bool): Match on the reverse of the 'destination' attribute
        disabled (bool): Disable this rule
        schedule (str): Schedule Profile
        icmp_unreachable (bool): Send ICMP Unreachable
        disable_server_response_inspection (bool): Disable server response inspection
        group (str): Security Profile Group
        virus (str): Antivirus Security Profile
        spyware (str): Anti-Spyware Security Profile
        vulnerability (str): Vulnerability Protection Security Profile
        url_filtering (str): URL Filtering Security Profile
        file_blocking (str): File Blocking Security Profile
        wildfire_analysis (str): Wildfire Analysis Security Profile
        data_filtering (str): Data Filtering Security Profile

    """
    # TODO: Add QoS variables
    XPATH = "/security/rules"
    SUFFIX = ENTRY

    @classmethod
    def variables(cls):
        return (
            Var("from", "fromzone", vartype="member", default=("any",)),
            Var("to", "tozone", vartype="member", default=("any",)),
            Var("source", vartype="member", default=("any",)),
            Var("source-user", vartype="member", default=("any",)),
            Var("hip-profiles", vartype="member", default=("any",)),
            Var("destination", vartype="member", default=("any",)),
            Var("application", vartype="member", default=("any",)),
            Var("service", vartype="member", default=("application-default",)),
            Var("category", vartype="member", default=("any",)),
            Var("action"),
            Var("log-setting"),
            Var("log-start", vartype="bool"),
            Var("log-end", vartype="bool"),
            Var("description"),
            Var("rule-type", "type", default="universal"),
            Var("tag", vartype="member"),
            Var("negate-source", vartype="bool"),
            Var("negate-destination", vartype="bool"),
            Var("disabled", vartype="bool"),
            Var("schedule"),
            Var("icmp-unreachable"),
            Var("option/disable-server-response-inspection", vartype="bool"),
            Var("profile-setting/group", vartype="member"),
            Var("profile-setting/profiles/virus", vartype="member"),
            Var("profile-setting/profiles/spyware", vartype="member"),
            Var("profile-setting/profiles/vulnerability", vartype="member"),
            Var("profile-setting/profiles/url-filtering", vartype="member"),
            Var("profile-setting/profiles/file-blocking", vartype="member"),
            Var("profile-setting/profiles/wildfire-analysis", vartype="member"),
            Var("profile-setting/profiles/data-filtering", vartype="member"),

        )


import tensorflow as tf
import numpy as np
import pandas as pd
import os, sys

# read data
# print(os.listdir(os.getcwd()))
data = pd.read_csv(os.path.join(os.getcwd(), 'train.csv'))
# print(data.groupby(["Sex"]).size())
data.drop("Ticket", axis=1, inplace=True)
data.drop("Cabin", axis=1, inplace=True)
# print(data.describe())
print(data.isna().sum())
data["Age"].fillna(data["Age"].median(), inplace=True)
print(data.isna().sum())
data.dropna(inplace=True)
print(data.isna().sum())
# print(data.describe())
# print(data.to_string())

num_category = 3

converted_data = pd.DataFrame()
names = []
for name in data["Name"]:
    if "Mr" in name:
        names.append(0)
    elif "Miss" in name:
        names.append(1)
    elif "Mrs" in name:
        names.append(2)
    elif "Dr" in name:
        names.append(3)
    elif "Master" in name:
        names.append(4)
    else:
        names.append(5)
data["Name"] = names

sexes = []
for sex in data["Sex"]:
    if "female" in sex:
        sexes.append(0)
    else:
        sexes.append(1)
data["Sex"] = sexes

embarkeds = []
for embarked in data["Embarked"]:
    if "S" in embarked:
        embarkeds.append(0)
    elif "C" in embarked:
        embarkeds.append(1)
    elif "Q" in embarked:
        embarkeds.append(2)
    else:
        embarkeds.append(0)
data["Embarked"] = embarkeds

data = data.values

# data preprocess
train_features = data[:710]
train_features = train_features[:, 1:-1]
# print(len(train_features[0]))
# exit()

train_labels = data[:710]
train_labels = train_labels[:, -1:]
# print(train_labels[61])
train_labels = np.reshape(train_labels, newshape=[-1])

# print(train_labels)
# exit()
#

# exit()
# print(train_labels)

# sys.exit()
# train_features = np.reshape(train_features, newshape=[-1, 13])

# print(train_labels)

val_features = data[710:]
val_features = val_features[:, 1:-1]

val_labels = data[710:]
val_labels = val_labels[:, -1:]

val_labels = np.reshape(val_labels, newshape=[-1])

# normalize
max_values = np.max(train_features, axis=0)
min_values = np.min(train_features, axis=0)

norm_train_data = (train_features - min_values)/(max_values - min_values)
norm_val_data = (val_features - min_values)/(max_values - min_values)

# max_label = np.max(train_labels, axis=0)
# min_label = np.min(train_labels, axis=0)
#
# norm_train_label = (train_labels - min_label)/(max_label - min_label)
# norm_val_label = (val_labels - min_label)/(max_label - min_label)

# construction
x = tf.placeholder(dtype=tf.float32, shape=[None, 8], name='x')
y = tf.placeholder(dtype=tf.float32, shape=[None], name='y')
y_1hot = tf.one_hot(y, depth=num_category)
lr = tf.placeholder(dtype=tf.float32, shape=[], name='lr')

layer0 = tf.layers.dense(x, units=64, activation=tf.nn.sigmoid)
layer1 = tf.layers.dense(layer0, units=64, activation=tf.nn.sigmoid)
layer2 = tf.layers.dense(layer1, units=num_category, activation = tf.nn.softmax)

logits_1hot = tf.identity(layer2, name="logits_1hot")
logits = tf.argmax(logits_1hot, name="logits")
print(logits)
exit()

#tf.nn.sparse_softmax_cross_entropy_with_logits(logits = onehot , labels = # not onehot)
loss = tf.losses.mean_squared_error(labels=y, predictions=logits)

floored_preds = tf.cast(tf.math.round(logits), tf.int32)
acc = tf.reduce_mean(tf.cast(tf.equal(y, floored_preds), tf.float32))

train_op = tf.train.GradientDescentOptimizer(lr).minimize(loss)

tf.summary.scalar('loss', loss)
tf.summary.scalar('accuracy', acc)

train_writer = tf.summary.FileWriter('tensorboard/train')
val_writer = tf.summary.FileWriter('tensorboard/val')

merge_all = tf.summary.merge_all()
train_writer.add_graph(tf.get_default_graph())

# executer
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# y_1hot_ = sess.run(y_1hot)

print('start training...')
for i in range(10000):
    merge_all_, train_op_ = sess.run(
        [merge_all, train_op],
        feed_dict={x: norm_train_data, y: train_labels, lr: 0.01}
    )
    train_writer.add_summary(merge_all_, global_step=i)

    if i % 100 == 0:
        merge_all_, val_loss_, acc_ = sess.run(
            [merge_all, loss, acc],
            feed_dict={x: norm_val_data, y: val_labels, lr: 0.01}
        )
        val_writer.add_summary(merge_all_, global_step=i/100)
        print(val_loss_, acc_)

print('end training...')

#model save and restore
# saver = tf.train.Saver()
# saver.save(sess, save_path='boston/model')


'''
    1. get header
    2. get @From field, save as folder name
    3. check if folder name existence:
        1: check if email exists
        0: create folder
    4. copy email to corresponding folder
'''
from argparse import ArgumentParser, FileType
from email import message_from_file
import os
import quopri
import base64
import shutil
__description__ = "Util to classify email"
__authors__ = "Minh Dinh"
__date__ = 111111

#constant

def get_list_of_incoming_emails(current_eml_path):
    '''
        return a list of email in a specified folder path
    '''
    email_list = []
    for each_element in os.listdir():
        if each_element.endswith(".eml"):
            email_list.append(each_element)
    if not email_list:
        return None
    return email_list

def get_eml_header(input_file_path):
    '''
        return full email_header in input_file
    '''
    input_file_as_path = open(input_file_path,"r")
    return message_from_file(input_file_as_path)._headers

def print_full_header(eml_header):
    for key, value in eml_header:
        print(" {} : {} ".format(key,value))

def get_value_by_key(eml_header,key_to_search):
    '''
        return value of key_to_search,
        return None if not found
    '''
    for key,value in eml_header:
        if key == key_to_search:
            return value
    return None

def get_email_from_obfuscated_string(obfuscated_string):
    '''
        extract email_address from a obfuscated string
        email should be wrapped between < and >
        return None if notfound
    '''
    email_address = []
    start_point = obfuscated_string.index("<")+1
    stop_point = obfuscated_string.index(">")
    for i in range(start_point,stop_point):
        email_address.append(obfuscated_string[i])

    if not email_address:
        print("\n[ERR]none email address found in input string\n")
        return None
    return ''.join(email_address)

def create_email_storing_folder_if_not_exists(folder_name,folder_path):
    '''
        create a folder to store email from a specific address
        folder_name = @From field in eml-header
    '''
    if os.path.isdir(os.path.join(folder_path,folder_name)):
        print("folder %s already exists"%folder_name)
        #check for duplicated file and copy
    else:
        os.mkdir(os.path.join(folder_path,folder_name))
        print("newly created folder %s "%folder_name)

def copy_email_to_storing_folder(src,dst,email_file_name):
    '''
        if email doestn't exist in storing folder, copy it
    '''
    #check if email exists
    if not os.path.isfile(os.path.join(dst,email_file_name)):
        try:
            shutil.copy2(os.path.join(src,email_file_name),os.path.join(dst,email_file_name))
            print("successfully copied new email file")
        except shutil.Error as e:
            print("duplicated file ",email_file_name)
    else:
        print("email file: %s already exist "%(email_file_name))

def move_copied_email_to_treasure(src,treasure_name,email_file_name,dst = None):
    '''
        move copied email to treasure, if exists, delete it
    '''
    create_email_storing_folder_if_not_exists(treasure_name,src)
    if dst is not None:
        if not os.path.isfile(dst + os.sep + treasure_name):
            try:
                shutil.move(os.path.join(src,email_file_name),os.path.join(dst,treasure_name))
            except shutil.Error as e:
                print("duplicated file~!",email_file_name   )
    else:
        if not os.path.isdir(os.path.join(src+ os.sep + treasure_name)):
            try:
                shutil.move(os.path.join(src,email_file_name),os.path.join(src,treasure_name))
            except shutil.Error as e:
                print("duplicated file~!",email_file_name)

def get_email_object(path_to_eml_file,file_name):
    return message_from_file(open(path_to_eml_file+os.sep+file_name,"r",encoding="ISO-8859-1"))

def extract_from_address_in_payload(email_object):
    '''
        extract value of 'from' address in the payload of provided email_obj
    '''
    start_point = 0
    stop_point = 0
    email_payload_as_string = email_object['From']

    #process the value of 'From' key in header
    print("raw value:",email_payload_as_string)
    try:
        start_point = email_payload_as_string.index('<')
        stop_point = email_payload_as_string.index('>')
    except ValueError as verr:
        return email_payload_as_string

    extracted_from_address = []
    for i in range(start_point+1,stop_point):
        extracted_from_address.append(email_payload_as_string[i])

    return ''.join(extracted_from_address)

def main(path):
    current_path = path
    treasure_name = "treasure"
    eml_key_to_search = "From"

    print("current emails in %s:"%path)
    try:
        email_list = get_list_of_incoming_emails(current_path)
        email_quantity = 0
        #print(email_list)

        for each_mail in email_list:
            #test the header
            #eml_header = get_eml_header(current_path + os.sep + each_mail)
            #print_full_header(eml_header)
            #print("====================")
            #from_addr = get_email_from_obfuscated_string(get_value_by_key(eml_header,eml_key_to_search))

            #test mail name
            from_addr = extract_from_address_in_payload(get_email_object(current_path,each_mail))
            #email_quantity += 1
            create_email_storing_folder_if_not_exists(from_addr,current_path)
            copy_email_to_storing_folder(current_path, os.path.join(current_path,from_addr), each_mail )
            move_copied_email_to_treasure(current_path,os.path.join(current_path,treasure_name),each_mail)
        print(email_quantity)
    except TypeError as err:
        print("empty email list")

if __name__ == '__main__':
    parser = ArgumentParser(
        description = __description__,
        epilog = "developed by {} on {}".format(", ".join(__authors__),__date__)
    )
    parser.add_argument("EML_FILE",help = "Path to EML File")
    args = parser.parse_args()

    main(args.EML_FILE)


from flaskps.db import get_db

class Role(object):
    @classmethod
    def all(cls):
        sql = """
            SELECT *
            FROM rol
        """
        try:
            dbconn = get_db()
            with dbconn.cursor() as cursor:
                cursor.execute(sql)
        finally:
            dbconn.cursor().close()
        return cursor.fetchall()


# use Python 3 print function and division
from __future__ import print_function

# modules
import time
import timeit

# example function, could be anything, in this case it just pauses for 2.8 s
def procedure():
    time.sleep(2.8)

# measure process time, use this for benchmarking code or algorithms
ti = time.clock()
procedure()
tf = time.clock()
print(tf-ti, 'seconds time.clock')

# measure wall time
ti = time.time()
procedure()
tf = time.time()
print(tf-ti, 'seconds time.time')

# measure with timeit
ti = timeit.default_timer()
procedure()
tf = timeit.default_timer()
print(tf-ti, 'seconds timeit')

#--- Python 3 where time.clock() is deprecated since v3.3

# measure with process_time
ti = time.process_time()
procedure()
tf = time.process_time()
print(tf-ti, 'seconds process_time')

# measure with perf_counter
ti = time.perf_counter()
procedure()
tf = time.perf_counter()
print(tf-ti, 'seconds perf_counter')

# so it looks like the following are similar:
#   time.clock() <-> process_time()         is cpu clock time
#   time.time()  <-> time.perf_counter()    is wall time


class Meta:
    def __init__(self, meta_name):
        print('init Meta in')
        self.metaname = meta_name
        print('init Meta out')

class A(Meta):
    def __init__(self, nameA):
        print('init A in')
        super().__init__(nameA)
        self.nameA = nameA + "A"
        print('init A out')
    def funcA(self):
        print('func A '+ self.nameA)

class B(Meta):
    def __init__(self, nameB):
        print('init B in')
        super().__init__(nameB)
        self.nameB = nameB + "B"
        print('init B out')
    def funcB(self):
        print('func B '+ self.nameB)

class C(A, B):
    def __init__(self, name, sex):
        print(C.__mro__)
        print('init C in')
        # only one super needed since A and B are all inherited from Meta
        super().__init__(name)
        print('init C out')

c = C('myname', 'male')
# Using super to make mro BFS, like this
# (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class '__main__.Meta'>, <class 'object'>)

print(c.nameB)
c.funcA()
c.funcB()

InheritanceTEST/GrandParents Init Inheritance with super.py


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 13 17:13:01 2018

@author: shashi
"""
import pickle #pickle to sae and load binary file
import sys #for command lines
from DFA_CLASS import DFA as NFA #class file for DFA
from DFA_CLASS import DFA

def check_syntax(line, length, string):
    if inputstring[line][0:length] != string: #checking for syntax
        print("SYNTAX ERROR IN LINE "+str(line+1)+"\n")
        sys.exit()

if __name__ == "__main__":
    if sys.argv[1][-4:] != '.nfa':
        print("UNEXPECTED INPUT FILE FORMAT\n")
        sys.exit()
    nfa = NFA() #creates an object of class nfa
    inputfile = open(sys.argv[1], 'r') #opens input text file give first command line argument
    inputstring = inputfile.read().split('\n\n'); #splits the tuple
    check_syntax(0,4,"Q = ") #checking for syntax
    try:
        nfa.Q = inputstring[0][4:].split() #No. of states checking if integer
    except:
        print("SYNTAX ERROR IN LINE 1\n")
        sys.exit()
    check_syntax(1,11,"Alphabet = ")
    try:
        nfa.Alphabet = inputstring[1][11:].split() #Alphabets
    except:
        print("SYNTAX ERROR IN LINE 2\n")
        sys.exit()
    check_syntax(2,19,"Transition Table =\n")
    try:
        nfa.Transition = {} #construction of transition table
        inputstring[2] = inputstring[2][19:].split('\n')
        inputstring[2][0] = inputstring[2][0].split('\t')
        for i in range(1, len(nfa.Q)+1):
            inputstring[2][i] = inputstring[2][i].split('\t')
            transition = {}
            j = 1;
            for x in inputstring[2][0][1:]:
                transition[x] = inputstring[2][i][j]
                j = j + 1
            nfa.Transition[inputstring[2][i][0]] = transition
    except:
        print("SYNTAX ERROR IN TRANSITION TABLE\n")
        sys.exit()
    check_syntax(3,8,"Start = ")
    try:
        nfa.Start = inputstring[3][8:] #start state
        if nfa.Start not in nfa.Q:
            raise Exception()
    except:
        print("SYNTAX ERROR IN LINE 4\n")
        sys.exit()
    check_syntax(4,8,"Final = ")
    try:
        nfa.Final = inputstring[4][8:].split() #final states
    except:
        print("SYNTAX ERROR IN LINE 5\n")
        sys.exit()
    #Compute EClose of all the states
    eclose = {}
    for x in nfa.Q:
        eclose[x] = [x]
        flag = True
        while flag :
            flag = False
            for y in eclose[x]:
                next_states = nfa.Transition[y]['eps'];
                if next_states!='':
                    next_states = next_states.split();
                    for z in next_states:
                        if z not in eclose[x]:
                            eclose[x].append(z)
                            flag = True
    eclose[''] = ['']
    for x in eclose:
        eclose[x].sort()
#%%
    #Lazy evaluation starts here
    dfa = DFA()
    dfa.Start = '{' + '-'.join(eclose[nfa.Start]) + '}'
    dfa.Q.append(dfa.Start)
    dfa.Alphabet = nfa.Alphabet;
    while True:
        finished = True
        print(dfa.Q)
        for x in dfa.Q:
            print(x)
            if dfa.Transition.get(x) == None:
                if x != '{}':
                    finished = False
                    current_state = x[1:-1].split('-')
                    dfa.Transition['{'+'-'.join(current_state)+'}'] = {}
                    break
                else:
                    transition = {};
                    finished = False
                    for y in dfa.Alphabet:
                        transition[y] ='{}'
                    dfa.Transition[x] = transition
                    break;
        if finished == True:
            break;
        for y in nfa.Alphabet:
            new_state = ''
            for x in current_state:
                for ex in eclose[x]:
                    transition = ''
                    for z in nfa.Transition[ex][y].split():
                        if z not in new_state:
                            if transition == '':
                                transition = z
                            else:
                                transition = transition + '-' +z
                    if new_state != '' and transition != '':
                        new_state = new_state +'-'+ transition
                    elif transition!='':
                        new_state = transition
            new_state = new_state.split('-')
            temp = []
            for z in new_state:
                temp = temp + eclose[z]
            new_state = temp
            new_state.sort()
            new_state = '-'.join(new_state)
            if new_state not in [y[1:-1] for y in dfa.Q]:
                dfa.Q.append('{'+new_state+'}')
            dfa.Transition['{'+'-'.join(current_state)+'}'][y] = '{'+new_state+'}'
    for x in dfa.Q:
        for y in nfa.Final:
            if y in x:
                dfa.Final = dfa.Final + eclose[y]
    #write into a file
#%%
    with open(sys.argv[2],'w') as outputfile:
        outputfile.write('Q = ' + ' '.join(dfa.Q) + '\n\n')
        outputfile.write('Alphabet = ' + ' '.join(dfa.Alphabet) + '\n\n')
        outputfile.write('Transition Table =\n\t' + '\t'.join(dfa.Alphabet) + '\n')
        for x in dfa.Q:
            outputfile.write(x)
            for y in dfa.Alphabet:
                outputfile.write('\t'+dfa.Transition[x][y])
            outputfile.write('\n')
        outputfile.write('\n')
        outputfile.write('Start = ' + dfa.Start + '\n\n')
        outputfile.write('Final = ' + ' '.join(dfa.Final) + '\n')


"""

class Connector

"""

from .port import *

class Connector:

    def __init__(self):
        self.nodes=[]
        self.index=0

    def getnodevalue(self, node,port):
        """
           the node is the connector of two ports ,so the node may get values from all of two ports
            the values is the union set of the not-none values within two ports
        """
        if node[0].index is None and port.index is not None:
            node[0].index = port.index
        if node[0].p is None and port.p is not None:
            node[0].p = port.p
        if node[0].t is None and port.t is not None:
            node[0].t = port.t
        if node[0].h is None and port.h is not None:
            node[0].h = port.h
        if node[0].s is None and port.s is not None:
            node[0].s = port.s
        if node[0].x is None and port.x is not None:
            node[0].x = port.x
        if node[0].mdot is None and port.mdot is not None:
            node[0].mdot = port.mdot

    def AddConnector(self, tupConnector,comps):
        comp0, port0 =tupConnector[0]
        comp1, port1 =tupConnector[1]
        # 1 get the index of port in nodes
        comps[comp0].portdict[port0][0].index = self.index
        # 2 add port0 into nodes
        self.nodes.append(comps[comp0].portdict[port0])
        # 3 join port1 info into  nodes[self.index]
        self.getnodevalue(self.nodes[self.index],comps[comp1].portdict[port1][0])
        # 4 send back nodes[self.index] to port1
        comps[comp1].portdict[port1][0] =self.nodes[self.index][0]

        self.index += 1


from datetime import datetime
import pandas_datareader.data as wb
import requests
import pandas_datareader as pdr
import pandas as pd
from bs4 import BeautifulSoup

print("시작년도")
y = int(input())
print("시작월")
m = int(input())
print("시작일")
d = int(input())

start = str(datetime(y,m,d))

print("끝년도")
e_y = int(input())
print("끝월")
e_m = int(input())
print("끝일")
e_d = int(input())

end = datetime(e_y,e_m,e_d)

stock_type = {
'kospi': 'stockMkt',
'kosdaq': 'kosdaqMkt'
}
# 회사명으로 주식 종목 코드를 획득할 수 있도록 하는 함수
def get_code(df, name):

    code = df.query("name=='{}'".format(name))['code'].to_string(index=False)
# 위와같이 code명을 가져오면 앞에 공백이 붙어있는 상황이 발생하여 앞뒤로 sript() 하여 공백 제거
    code = code.strip()
    return code
# download url 조합

def get_download_stock(market_type=None):
    market_type = stock_type[market_type]
    download_link = 'http://kind.krx.co.kr/corpgeneral/corpList.do'
    download_link = download_link + '?method=download'
    download_link = download_link + '&marketType=' + market_type
    df = pd.read_html(download_link, header=0)[0]
    return df
    # kospi 종목코드 목록 다운로드
def get_download_kospi():
    df = get_download_stock('kospi')
    df.종목코드 = df.종목코드.map('{:06d}.KS'.format)
    return df
# kosdaq 종목코드 목록 다운로드
def get_download_kosdaq():
    df = get_download_stock('kosdaq')
    df.종목코드 = df.종목코드.map('{:06d}.KQ'.format)
    return df
# kospi, kosdaq 종목코드 각각 다운로드
kospi_df = get_download_kospi()
kosdaq_df = get_download_kosdaq()
    # data frame merge
code_df = pd.concat([kospi_df, kosdaq_df])
    # data frame정리
code_df = code_df[['회사명', '종목코드']]
    # data frame title 변경 '회사명' = name, 종목코드 = 'code'
code_df = code_df.rename(columns={'회사명': 'name', '종목코드': 'code'})
print("회사이름을 입력하세요")
com_name = input()
code = get_code(code_df, com_name)

df = wb.DataReader(code, 'yahoo',start,end)
print(df)


import numpy as np
from snc.agents.hedgehog.asymptotic_workload_cov.\
    estimate_asymptotic_cov import EstimateAsymptoticWorkloadCovBatchMeans

def test_get_batch():
    data = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                     [.1, .2, .3, .4, .5, .6, .7, .8, .9, 1]])

    b12 = EstimateAsymptoticWorkloadCovBatchMeans.get_batch_sum(data, batch_len=2, index=0)
    assert np.all(b12 == np.sum(np.array([[1, 2], [.1, .2]]), axis=1))
    b22 = EstimateAsymptoticWorkloadCovBatchMeans.get_batch_sum(data, batch_len=2, index=1)
    assert np.all(b22 == np.sum(np.array([[3, 4], [.3, .4]]), axis=1))
    b32 = EstimateAsymptoticWorkloadCovBatchMeans.get_batch_sum(data, batch_len=2, index=2)
    assert np.all(b32 == np.sum(np.array([[5, 6], [.5, .6]]), axis=1))
    b42 = EstimateAsymptoticWorkloadCovBatchMeans.get_batch_sum(data, batch_len=2, index=3)
    assert np.all(b42 == np.sum(np.array([[7, 8], [.7, .8]]), axis=1))
    b52 = EstimateAsymptoticWorkloadCovBatchMeans.get_batch_sum(data, batch_len=2, index=4)
    assert np.all(b52 == np.sum(np.array([[9, 10], [.9, 1]]), axis=1))

    b13 = EstimateAsymptoticWorkloadCovBatchMeans.get_batch_sum(data, batch_len=3, index=0)
    assert np.all(b13 == np.sum(np.array([[1, 2, 3], [.1, .2, .3]]), axis=1))
    b23 = EstimateAsymptoticWorkloadCovBatchMeans.get_batch_sum(data, batch_len=3, index=1)
    assert np.all(b23 == np.sum(np.array([[4, 5, 6], [.4, .5, .6]]), axis=1))
    b33 = EstimateAsymptoticWorkloadCovBatchMeans.get_batch_sum(data, batch_len=3, index=2)
    assert np.all(b33 == np.sum(np.array([[7, 8, 9], [.7, .8, .9]]), axis=1))

def test_get_noise_process_single_server():
    state_process = np.array([[0, 2, 3, 6, 4, 2, 2]])
    action_process = np.array([[1, 0, 0, 0, 1, 1, 1]])
    load = np.array([0.9])
    workload_mat = 0.5 * np.ones((1, 1))
    buffer_processing_matrix = -2 * np.ones((1, 1))

    test_noise = np.array([[1.1, -0.4, 0.6, -1.9, -0.9, 0.1]])

    noise_process = EstimateAsymptoticWorkloadCovBatchMeans.get_noise_process(
        workload_mat, state_process, action_process, load, buffer_processing_matrix)

    np.testing.assert_almost_equal(noise_process, test_noise)

def test_get_noise_process_two_serially_connected_resources_one_buffer_each():
    """We test multiple dimension processes: 2 buffers, 2 actions."""
    state_process = np.array([[0, 2, 3, 6, 4, 2, 2],
                              [1, 3, 4, 7, 5, 3, 3]])
    action_process = np.array([[1, 0, 0, 0, 1, 1, 1],
                               [0, 1, 1, 0, 1, 0, 1]])
    load = np.array([[0.9], [0.9]])
    workload_mat = np.array([[0.5, 0.5],
                             [0.1, 0.1]])
    buffer_processing_matrix = np.array([[-1.5, 0],
                                         [1.5, -0.5]])  # mu1 = 1.5, mu2 = 0.5

    test_noise = np.array([[1.1, 0.35, 2.35, -2.9, -2.65, -0.9],
                           [-0.5, -0.65, -0.25, -1.3, -1.25, -0.9]])

    noise_process = EstimateAsymptoticWorkloadCovBatchMeans.get_noise_process(
        workload_mat, state_process, action_process, load, buffer_processing_matrix)

    np.testing.assert_almost_equal(noise_process, test_noise)

def test_compute_workload_cov():
    """Simple correctness test."""
    noise = np.array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                      [.1, .2, .3, .4, .5, .6, .7, .8, .9, 1]])
    b12 = np.sum(np.array([[1, 2], [.1, .2]]), axis=1)
    b22 = np.sum(np.array([[3, 4], [.3, .4]]), axis=1)
    b32 = np.sum(np.array([[5, 6], [.5, .6]]), axis=1)
    b42 = np.sum(np.array([[7, 8], [.7, .8]]), axis=1)
    b52 = np.sum(np.array([[9, 10], [.9, 1]]), axis=1)
    test_cov = np.outer(b12, b12) + np.outer(b22, b22) + np.outer(b32, b32) + np.outer(b42, b42) \
        + np.outer(b52, b52)
    test_cov /= 10
    workload_cov = EstimateAsymptoticWorkloadCovBatchMeans.compute_workload_cov(noise, 2)
    np.testing.assert_almost_equal(workload_cov, test_cov)


from threading import Thread
from ctypes import Structure, c_uint, sizeof, windll, byref
import config

#debugging only
from time import sleep

class IdleHandler(Thread):
  def __init__(self, idleCallback, activeCallback):
    super(IdleHandler, self).__init__()

    self.daemon = True
    self.maxTicks = 3
    self.maxIdleTime = config.maxIdleTime
    self.previousIdleTime = 0
    self.isIdle = False
    self.currentTicks = 0
    self.idleCallback = idleCallback
    self.activeCallback = activeCallback

  class LASTINPUTINFO(Structure):
    _fields_ = [
      ('cbSize', c_uint),
      ('dwTime', c_uint),
    ]

  def getIdleTime(self):
    lastInputInfo = self.LASTINPUTINFO()
    lastInputInfo.cbSize = sizeof(lastInputInfo)
    windll.user32.GetLastInputInfo(byref(lastInputInfo))

    return (windll.kernel32.GetTickCount() - lastInputInfo.dwTime) / 1000.0

  def run(self):
    print('%s Started.' % __class__.__name__)
    while True:
      newIdleTime = self.getIdleTime()
      delta = abs(self.previousIdleTime - newIdleTime)
      if newIdleTime > self.maxIdleTime and not self.isIdle:
        self.isIdle = True
        self.idleCallback()
#        print("Starting job")
      elif self.isIdle:
        if delta < 0.5:
          if self.currentTicks > self.maxTicks:
            self.currentTicks = 0
            self.isIdle = False
            self.activeCallback()
#            print('Stopping job')
          else:
#            print('Activity tick')
            self.currentTicks += 1
        else:
          self.currentTicks = 0
          self.isIdle = True
      else:
#        print('User is active')
        self.currentTicks = 0
        self.isIdle = False

      self.previousIdleTime = newIdleTime
      sleep(1)


from flask import Flask, url_for
from view_classes import BasicView, IndexView
from nose.tools import *

app = Flask("common")
BasicView.register(app)
IndexView.register(app)

client = app.test_client()

def test_index_url():
    with app.test_request_context():
        url = url_for("IndexView:index")
        eq_("/", url)

def test_basic_index_url():
    with app.test_request_context():
        url = url_for("BasicView:index")
        eq_("/basic/", url)

def test_custom_endpoint_url():
    with app.test_request_context():
        url = url_for("basic_endpoint")
        eq_("/basic/endpoint/", url)


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

from django import template
from django.utils.safestring import mark_safe
import os

register = template.Library()

@register.simple_tag
def homework_download_button(homework_path, course_id, day_num, student_id=None):
    course_id = str(course_id)
    day_num = str(day_num)
    if student_id:
        student_id = str(student_id)
        file_path = homework_path + '/' + course_id + '/' + day_num + '/' + student_id
        buttoncontent = '作业下载'
        if os.path.exists(file_path):
            if not os.listdir(file_path):
                result = "<button class='btn'>未交作业</button>"
            else:
                result = "<button class='btn btn-info'>{}</button>".format(file_path,buttoncontent)
        else:
            result = "<button class='btn'>未交作业</button>"
    else:
        file_path = homework_path + '/' + course_id + '/' + day_num
        buttoncontent = '全班作业下载'
        if os.path.exists(file_path):
            # result = "<button class='homework_download btn btn-info'>{}</button>".format(file_path,buttoncontent)
            result = "<button class='homework_download btn btn-info' homework_path='{}'>{}</button>".format(file_path, buttoncontent)
        else:
            result = "<button class='btn'>未交作业</button>"
    return mark_safe(result)


from entity_linkage.typing.entity_typing import entity_typing
import preprocess
from task import Task
import logging
from utils import logging_utils, data_utils, embedding_utils, pkl_utils
from model_param_space import param_space_dict
import datetime
import config

class NFETC(entity_typing):

	def __init__(self):
		self.task = None

	def initi_task(self, model_name, data_name, epoch_num) :
		time_str = datetime.datetime.now().isoformat()
		logname = "Final_[Model@%s]_[Data@%s]_%s.log" % (model_name, data_name, time_str)
		logger = logging_utils._get_logger(config.LOG_DIR, logname)
		params_dict = param_space_dict[model_name]
		task = Task(model_name, data_name, epoch_num, params_dict, logger) # default:cv_run=5
		return task

	def preprocess_helper(self, data_name, extension, input_file_path):
		if extension == "txt":
			preprocess.preprocess(data_name, input_file_path)
		elif extension == "tsv":
			preprocess.tsv_preprocess(data_name, input_file_path)

	def read_dataset(self, file_path, options={}):

		data_name = options.get("data_name", "others") # default = "others"
		ratio = options.get("ratio", (0.7, 0.15, 0.15))
		model_name = options.get("model_name", "best_nfetc_wiki") # default = "best_nfetc_wiki"
		epoch_num = options.get("epoch_num", 5) # default = 5 epoch

		print(">> Initiate Task")
		self.task = self.initi_task(model_name, data_name, epoch_num)

		return (self.task.train_set, self.task.full_test_set)

	def train(self, train_data, options={}) :

		print(">> In train, train_data size: (", len(train_data), ")")

		if(len(train_data) == 0):
			print(">> There is NOT enough data to train.")
		else:
			print(">> There is Enough data to train.")
			self.task.add_save(train_data)
			print(">> Training is Done ! ")

		return

	def predict(self, test_data, model_details=None, options={}) :

		print(">> In predict, test_data size: (", len(test_data),  ")")

		if(len(test_data) == 0):
			print(">> There is NOT enough data to predict.")
			prediction_data = None
		else:
			print(">> There is Enough data to predict.")
			prediction_data = self.task.add_evaluate(test_data)
			print(">> prediction_data size: (", len(prediction_data),  ")")
			print(">> Prediction is Done ! ")

		return prediction_data

	def evaluate(self, test_data, prediction_data=None,  options={}) :

		print(">> In evaluate, test_data size: (", len(test_data), ")")

		if(len(test_data) == 0):
			print(">> There is NOT enough data to evaluate.")
			scores = (0.0, 0.0, 0.0)
		else:
			print(">> There is Enough data to evaluate.")
			if prediction_data is None:
				prediction_data = self.predict(test_data, None, options)

			acc, macro, micro = self.task.add_get_scores(test_data, prediction_data, True) # save = True
			scores = (acc, macro, micro)

			print(">> Evaluation is Done ! ")

		return scores

	def split_data_tsv(self, file_path, data_name, split_ratio=(0.7, 0.15, 0.15)): # train.tsv, test.tsv

		split_r = split_ratio[0]+split_ratio[1] # Since [train + dev]/[test]
		print("> Split all.tsv to cleat_train, clean_test data with ratio (", split_r, ",",  split_ratio[2], ")")

		docs = []
		with open(file_path, 'r') as f:
			for line in f:
				docs.append((line.strip()))

		train_dev_split_idx = int(len(docs) * split_r)

		data = {}
		data["train"] = docs[:train_dev_split_idx]
		data["test"] = docs[train_dev_split_idx:]

		folder_path = "others"
		if data_name == "wiki":
			folder_path = config.WIKI_DATA_PATH
		elif data_name == "wikim":
			folder_path = config.WIKIM_DATA_PATH
		elif data_name == "ontonotes":
			folder_path = config.ONTONOTES_DATA_PATH
		else:
			folder_path = config.OTHERS_DATA_PATH

		for key in data:
			with open(folder_path+"/"+key+"_clean.tsv", 'w') as f:
				for line in data[key]:
					f.write(line+"\n")

		print(">> Finished! ")

	def split_data_txt(self, file_path, data_name, split_ratio=(0.7, 0.15, 0.15)): # train.txt, dev.txt, test.txt

		docs = []
		with open(file_path, 'r') as f:
			for line in f:
				docs.append((line.strip()))

		train_dev_split_idx = int(len(docs) * split_ratio[0])
		dev_test_split_idx = int(len(docs) * (split_ratio[0] + split_ratio[1]))

		data = {}
		data["train"] = docs[:train_dev_split_idx]
		data["dev"] = docs[train_dev_split_idx:dev_test_split_idx]
		data["test"] = docs[dev_test_split_idx:]

		folder_path = "others"
		if data_name == "wiki":
			folder_path = config.WIKI_DATA_PATH
		elif data_name == "wikim":
			folder_path = config.WIKIM_DATA_PATH
		elif data_name == "ontonotes":
			folder_path = config.ONTONOTES_DATA_PATH
		else:
			folder_path = config.OTHERS_DATA_PATH

		for key in data:
			with open(folder_path+"/"+key+".txt", 'w') as f:
				for line in data[key]:
					f.write(line+"\n")

		print(">> Finished! ")

	def save_model(self, file=None):

		file = config.CHECKPOINT_DIR
		print("save model to ", file)
		self.task.save()
		return

	def load_model(self, file=None):

		file = config.CHECKPOINT_DIR
		print("load model from ", file)
		self.task.load()

		return


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

"""
This script contains classes and functions that
authenticate escience users.

@author: Ioannis Stenos, Nick Vrionis
"""

import logging
from kamaki.clients.astakos import AstakosClient
from kamaki.clients import ClientError
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import BasePermission
from backend.models import Token
from rest_framework import exceptions
from cluster_errors_constants import *

# Constants
AUTHENTICATED = 1
NOT_AUTHENTICATED = 0
# Only method that escience token authentication is not required.
SAFE_METHODS = ['POST']

class EscienceTokenAuthentication(TokenAuthentication):
    """
    Class that inherit the rest_framework default Token authentication class.
    Overrides the Token model used and authenticate_credentials method.
    """

    model = Token

    def authenticate_credentials(self, key):
        try:
            token = self.model.objects.get(key=key)
        except self.model.DoesNotExist:
            raise exceptions.AuthenticationFailed('Invalid token')

        return (token.user, token)

class IsAuthenticatedOrIsCreation(BasePermission):
    """
    Class for permissions. Only POST method will be allowed without
    Token authentication. Every other method will have to add
    as a request header the escience authentication token.
    """

    def has_permission(self, request, view):
        return (
            request.method in SAFE_METHODS or
            request.user and
            request.user.is_authenticated()
            )

class IsAuthenticated(BasePermission):
    """
    Class for permissions for celery jobs view. Every method will have to add
    as a request header the escience authentication token.
    """

    def has_permission(self, request, view):
        return (
            request.user and
            request.user.is_authenticated()
            )

def check_user_credentials(token, auth_url='https://accounts.okeanos.grnet.gr'
                      '/identity/v2.0'):
    """Identity,Account/Astakos. Test ~okeanos authentication credentials"""
    logging.info(' Test the credentials')
    try:
        auth = AstakosClient(auth_url, token)
        auth.authenticate()
        logging.info(' Authentication verified')
        return AUTHENTICATED
    except ClientError:
        logging.error('Authentication failed with url %s and token %s' % (
                      auth_url, token))
        return NOT_AUTHENTICATED


guess = "penguin"
print("Hello ready to play a guessing game?")
print("Try guessing the animal, the animal is know to be a flyles bird livin in cold areas!")
awnser1 = input("ready?").lower()
while awnser1 == "yes" :
    awnser2 = input("whats your guess?").lower()
    if awnser2[0] == "q":
       awnser1 = "no"
    elif awnser2 == guess :
        print("well done, your a genius thanks for playing")
        awnser1 = "no"
    else:
        print("Good guess but it is incorrect :( another hint is that they live in the south pole")


import numpy as np
import cv2
import pydicom
import torch
from smart_open import smart_open
from torch.utils.data import Dataset, DataLoader
from albumentations import  Compose, Normalize, Resize
from albumentations.pytorch import ToTensorV2
from utils.dicom_utils import get_plane, extract_multiwindow_image

torch.set_num_threads(1)
cv2.setNumThreads(1)

NUM_WORKERS = 2
SIZE = 256
INTER = cv2.INTER_CUBIC
MEAN = [0.4984]
SD = [0.2483]
WINDOWS = ((60, 40), (40, 40), (40, 80))  # (blood, tissue, brain) *start with blood for model compatibility

def gen_ct_array(dicom_paths, windows=WINDOWS):
    images = []
    for file_path in dicom_paths:
        try:
            data = pydicom.dcmread(smart_open(file_path))
            if get_plane(data) == 'Axial':
                images.append(extract_multiwindow_image(data, windows))
        except:
            pass

    if len(images) > 0:
        ct_array = np.stack([img for img, _ in sorted(images, key=lambda x: x[1])], axis=0)
    else:
        ct_array = np.array([])

    return ct_array

def make_transform(size=SIZE, inter=INTER):
    trans = Compose([
                     Resize(size, size, interpolation=inter),
                     Normalize(MEAN, SD),
                     ToTensorV2(),
                    ])
    return trans

def get_flip_ct_array_loader(dicom_paths, windows=WINDOWS, batch_size=16, shuffle=False):
    X = gen_ct_array(dicom_paths, windows=windows)
    transform = make_transform(size=SIZE, inter=INTER)
    ds = FlipBrainCTArray(X, transform)
    return DataLoader(ds, batch_size=batch_size, shuffle=shuffle, num_workers=NUM_WORKERS)

class FlipBrainCTArray(Dataset):
    def __init__(self, X, transform=None):
        super(FlipBrainCTArray, self).__init__()
        self.X = X
        self.transform = transform

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

    def __getitem__(self, i):
        img = self.X[i]
        img_f = img[:, ::-1]
        img = np.concatenate([img, img_f], axis=2)

        if self.transform:
            _res = self.transform(image=img)
            img = _res['image']

        return img

# # FOR TEST
# if __name__ == '__main__':
#     import matplotlib.pyplot as plt
#     dicom_dir = r'D:\ai4strokesubtypes\Benchmark\1151438'
#     data_loader = get_flip_ct_array_loader(dicom_dir)
#     print(data_loader.dataset.X.shape)
#     for x in data_loader:
#         print(x.shape)
#     fig, axs = plt.subplots(x.shape[1], 1)
#     for i in range(x.shape[1]):
#         axs[i].imshow(x[10][i])
#     plt.show()


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

import subprocess

SERVERS = {
	"trident1.vlab.cs.hioa.no" : "128.39.120.89",
	"trident2.vlab.cs.hioa.no" : "128.39.120.90",
	"trident3.vlab.cs.hioa.no" : "128.39.120.91"
}

SERVER_MESSAGE = {
	"trident1.vlab.cs.hioa.no" : 0.0,
	"trident2.vlab.cs.hioa.no" : 0.0,
	"trident3.vlab.cs.hioa.no" : 0.0
}

CLIENT_MESSAGE = {
	"request" : {
		"login" : False,
		"temperature" : False,
		"migration" : False
	},
	"vm" : {
		"mac" : "",
		"target" : "",
		"load" : 0,
		"deltaTemp" : 0.0
	}
}

def getHostName():
	hostName = ""
	for key, value in SERVERS.items():
		cmd = "sudo traceroute -n %s | tail -n+2 | awk '{ print $2 }' | wc -l" % (value)
		try:
			if(int(subprocess.check_output(cmd, shell=True).decode('UTF-8').rstrip("\n")) == 1):
				hostName = key
				break
		except subprocess.CalledProcessError as e:
			print("ERROR: : {reason}".format(reason=e))
	return hostName

def getHostIp(hostName):
	hostIp = ""
	if hostName in SERVERS:
		hostIp = SERVERS[hostName]
	return hostIp

def getVmMac():
	try:
		mac = subprocess.check_output("sudo ifconfig | grep 'HWaddr' | awk '{print $NF}'", shell=True).decode('UTF-8').rstrip("\n")
		return mac.lower()
	except subprocess.CalledProcessError as e:
		print("ERROR: : {reason}".format(reason=e))
	return ""

def getLoad():
	try:
		load = subprocess.check_output("sudo ps | grep stress-ng | head -1 | awk '{print $NF}'", shell=True).decode('UTF-8').rstrip("\n")
		print("Load = {}".format(load))
		return int(load)
	except subprocess.CalledProcessError as e:
		print("ERROR: : {reason}".format(reason=e))
	return 0