"""
Author: poiroot
"""

import logging
from datetime import datetime, timedelta
from collections import defaultdict
from itertools import combinations

import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error

from .metrics import Metric, getModelCoefs
from ..utils.load import load_data, load_model
from xgboost import XGBRegressor
from keras.models import Sequential
from sklearn.externals import joblib

logger = logging.getLogger(__name__)

class FortuneTeller:
    """
    The predict system

    Parameters
    ----------
    X : pandas DataFrame
        The forecast data

    name : string
        The config target name

    length : int
        The row number of history data

    mean : pandas DataFrame, shape [X.shape[1]]
        The mean of history data, i.e. the data seen by models

    std : pandas DataFrame, shape [X.shape[1]]
        The standard deviation of history data

    cov : pandas DataFrame, shape [X.shape[1], X.shape[1]]
        The covariance matrix of hostory data

    models :
        The models created by sklearn

    Attributes
    ----------
    r2 : float
        The maximum R^2 of models

    RI : float
        The confidence of predict value by system

    MD : float
        The mahalannobis distance of X

    DQIX : float
        The data quality of X computed by multivariate normal distribution

    ZScore : pandas DataFrame, shape [X.shape[1]]
        The z-score of X

    predicts : dict, shape [length of models], key : model, value : float
        The predictive values by models

    predict :float
        The predictive value by weighted average of models

    coefs : dict, shape [length of models], key : model, value : float
        The coefficients of each algorithms

    intercept : float
        The intercept of linear regression model

    error : float
        The mean absolute error of the linear regression model

    algo_name : list
        The algorithm name in config "algo_name"

    mae : float
        The mean absolute error of models

    threshold: float, optional, default 0
        Get predict values whose confidence index greater than threshold

    time : datetime object
        The predict time i.e. get feature time
    """

    def __init__(self, name, data_manager, sql):

        self.time = None
        self.X = None
        self.data_manager = data_manager
        self.config = data_manager.model_resource[name]
        self.name = name
        self.sql = sql
        self.length = self.config["train"].data.shape[0]
        self.mean = self.config["mean"]
        self.std = self.config["std"]
        self.cov = self.config["cov"]
        self.models = self.config["models"]
        self.r2 = max(self.config["algo_r2"])
        self.RI = 0
        self.MD = 0
        self.DQIX = 0
        self.ZScore = 0
        self.predicts = dict()
        self.predict = 0
        self.coefs = dict()
        self.intercept = 0
        self.error = 0
        self.algo_name = [name.split(".")[0] for name in self.config['algo_name']]
        for name in self.algo_name:
            self.coefs[name] = 1 / len(self.models)
        self.mae = 0
        self.threshold = self.config["threshold"]
        self.start_time = None
        self.target_data = None
        self.metric = Metric(self.config["train"].data, self.r2, self.config["confidence"])
        self.contribute = list()
        self.error_method = "mse"
        logger.info("Creating as instance of FortuneTeller")

    def tell(self, time):
        """
        The main method of system
        """
        self.start_time = self.start_time - timedelta(hours = 8)
        #為了能夠往回撈到lims的資料，多回推8小時

        X = self.data_manager.get_feature(self.name, time)
        self.time = time
        self.X = X
        self.MD, self.contribute = self.metric.mahalanobis(X=X, mean=self.mean, cov=self.cov, length=self.length)
        if self.config["confidence"] is True:
            self.DQIX = self.metric.quality(X=X, mean=self.mean, cov=self.cov, log=True)
            self.RI = self.metric.ri(forecast_DQIX=self.DQIX)
        logger.info("Compute indicator score")

        for name, model in zip(self.algo_name, self.models):
            if type(model) ==  type(XGBRegressor()):
                #model.get_booster().feature_names = list(X.columns)
                X = X.reindex(columns = model.get_booster().feature_names)
            if type(model) == type(Sequential()):
                X = X.values.reshape(1,4,12)
                print(X)

            predict = model.predict(X)
            print("scaled predict value:", predict)

            if type(model) == type(Sequential()):
                y_scaler = joblib.load("prep/TRL_Y_scaler_0719.sav")
                predict = y_scaler.inverse_transform(predict)

            print("your predict value :",predict)

            while True:
                if len(predict.shape) > 1:
                    predict = predict[0]
                else:
                    break
            self.predicts[name] = float(predict[0])
        logger.info("Predict")

        end_time = time
        logger.debug("target_data time: {0}~{1}".format(self.start_time, end_time))
        self.target_data = self.data_manager.get_target(self.name, self.start_time, end_time, ["SAMPLED_DATE", "RESULT_VALUE"])
        logger.info("Get target data")
        logger.debug("target_data length: {0}".format(self.target_data.shape[0]))

        self.coefs = self._updateModelCoefs(self.coefs)
        if self.config['revise'] is True:
            self._revise()

        if self.target_data.shape[0] >= self.config['revise_sample_times']:
            self.start_time = self.target_data['time'][1]

        self.predict = 0
        for name in self.algo_name:
            self.predict += self.coefs[name] * self.predicts[name]
        self.predict += self.intercept

        self.mae = self._calculateMAE()

    def _updateModelCoefs(self, weights):
        #print("START UPDATE COEF")
        #print("DATA SHAPE:", self.target_data.shape[0])
        if self.target_data.shape[0] < self.config["revise_sample_times"]:
            return weights

        error = dict()
        value = defaultdict(list)
        for name in self.algo_name:
            self.drop_index = list()
            for index in self.target_data.index:
                row = self.target_data.loc[index]
                predict_value = self._getPredictValue(row["time"],
                                                      name,
                                                      self.config["revise_minutes_low"],
                                                      self.config["revise_minutes_high"])
                if np.isinf(predict_value):
                    #print("np.isinf = T :",predict_value)
                    self.drop_index.append(index)
                    continue
                value[name].append(predict_value)
            #print(self.target_data)
            #print("DROP INDEX:", self.drop_index)

            true_value = self.target_data["value"].copy()
            true_value.drop(self.drop_index, inplace=True)
            #print("true_value :",true_value)
            #print("predict value :",value[name])
            error[name] = self._calculateError(true_value,
                                               value[name],
                                               method=self.error_method)
        #print("PREDICT ERROR :",error)

        if len(weights) == 1:
            return weights
        if sum(error.values()) == 0:
            return weights
        new_weights = getModelCoefs(error)
        #print("new_weights : ",new_weights )
        #print("END UPDATE COEF")
        return new_weights

    def _revise(self):
        """
        The method that revise the predict value by simple math

        Should check quality of y ??
        According LIMS time to get predict value then revise
        Because predict value is immediate

        """

        if self.target_data.shape[0] < self.config["revise_sample_times"]:
            return

        value = list()
        for index in self.target_data.index:
            row = self.target_data.loc[index]
            value.append(self._getPredictValue(row["time"],
                                               self.algo_name,
                                               self.config["revise_minutes_low"],
                                               self.config["revise_minutes_high"]))
        predict = list()
        for part in value:
            num = 0
            for i, algo in enumerate(self.algo_name):
                if not np.isinf(part[i]):
                    num += self.coefs[algo] * part[i]
            if num != 0:
                predict.append(num)
        true_value = self.target_data["value"].copy()
        true_value.drop(self.drop_index, inplace=True)
        self.error = self._calculateError(true_value, predict, method="mae")

        try:
            self.intercept = sum(true_value - predict) / len(predict)
        except ZeroDivisionError:
            self.intercept = 0
        logger.info("Revise")

    def _getPredictValue(self, lims_time, name=None, start_time=-30, end_time=0):
        """
        According the LIMS time and the time interval to get the mean of predictive value
        Should delete extreme predictive values??

        Parameters
        ----------
        lims_time : datetime
            The LIMS time

        name : string or list
            The algorithm name

        start_time : int, optional, default -30
            The start time of the time interval according LIMS time
            i.e. start time of the time interval = LIMS time + start_time

        end_time : int, optional, default 0
            The end time of the time interval according LIMS time
            i.e. end time of the time interval = LIMS time + end_time

        Returns
        -------
        value : float or list
            The mean of predictive values in the time interval
        """
        start = lims_time + timedelta(minutes=start_time)
        end = lims_time + timedelta(minutes=end_time)

        # get RTPMS data and mean it
        predict_df = self.sql.get_model_result(self.name, start, end)
        try:
            predict_df = predict_df[predict_df["conf_idx"] > self.threshold]
            if predict_df.shape[0] == 0:
                logger.debug("There is no {0} data's confidence index greater than threshold, Time: {1} ~ {2}".format(name, start, end))
                if type(name) is list:
                    return [np.inf for i in name]
                return np.inf
            if name is None:
                value = np.mean(predict_df["predict"])
            elif type(name) is list:
                value = list()
                for n in name:
                    value.append(np.mean(predict_df["predict_{0}".format(n)]))
            else:
                value = np.mean(predict_df["predict_{0}".format(name)])
        except KeyError:
            # There is no data in the database
            logger.error("No {0} data in database, Time: {1} ~ {2}".format(name, start, end))
            if type(name) is list:
                return [np.inf for i in name]
            return np.inf
        return value

    def _calculateError(self, true, predict, method="mse"):
        """
        Calculate error between true values and predictive values

        Parameters
        ----------
        true : list
            The true values

        predict : list
            The predictive values

        method : string, optional, default "mse"
            The method to calculate error
                "mse" : mean square error
                "mae" : mean absolute error
                "r2" : 1 - (R squared)
        """
        methods = set(["mse", "mae", "r2"])
        if method not in methods:
            raise ValueError("The method is invalid")
        if len(true) == 0 or len(predict) == 0:
            return 0

        if method == "mse":
            error = mean_squared_error(true, predict)
        elif method == "mae":
            error = mean_absolute_error(true, predict)
        elif method == "r2":
            error = 1 - r2_score(true, predict)

        return error

    def setStartTime(self, time):
        self.start_time = time

    def _calculateMAE(self):
        """
        Calculating MAE bases on the linear regression model and model difference
        """
        mae = self.error
        model_diff_mae = list()

        for indices in combinations(range(len(self.algo_name)), 2):
            diff = abs(self.predicts[self.algo_name[indices[0]]] - self.predicts[self.algo_name[indices[1]]])
            coefs = [abs(self.coefs[self.algo_name[indices[i]]]) for i in range(2)]
            ratio = min(coefs) / max(coefs)
            if ratio is np.nan:
                continue
            model_diff_mae.append(ratio * diff)
        try:
            mae += max(model_diff_mae)
        except ValueError:
            pass

        return mae

    def initialModelCoefs(self):
        time = datetime.now()
        logger.info("Intialize model coefs start")
        if self.config["target_source"] == "rtpms":
            self.target_data = self.data_manager.get_target(self.name, time - timedelta(seconds=5*self.config["predict_sleep_seconds"]), time, ["SAMPLED_DATE", "RESULT_VALUE"])
        else:
            self.target_data = self.data_manager.get_target(self.name, time - timedelta(days=1), time, ["SAMPLED_DATE", "RESULT_VALUE"])
        error = dict()
        value = defaultdict(list)
        drop_index = list()
        for index in self.target_data.index:
            row = self.target_data.loc[index]
            base_time = row["time"]
            times = [base_time + timedelta(minutes=i) for i in range(self.config["revise_minutes_low"],
                                                                     self.config["revise_minutes_high"],
                                                                     self.config["predict_sleep_seconds"] // 60)]
            predicts = defaultdict(list)
            for t in times:
                if t > time:
                    break
                feature = self.data_manager.get_feature(self.name, t)
                for name, model in zip(self.algo_name, self.models):
                    predict = model.predict(feature)
                    while True:
                        if len(predict.shape) > 1:
                            predict = predict[0]
                        else:
                            break
                    if self.config["confidence"] is True:
                        self.DQIX = self.metric.quality(X=feature, mean=self.mean, cov=self.cov, log=True)
                        self.RI = self.metric.ri(forecast_DQIX=self.DQIX)
                        if self.RI > self.threshold:
                            predicts[name].append(predict[0])
                    else:
                        predicts[name].append(predict[0])
            if len(predicts.keys()) == 0:
                drop_index.append(index)
            for name in predicts.keys():
                if len(predicts[name]) == 0:
                    drop_index.append(index)
                    break
                value[name].append(np.mean(predicts[name]))
        true_value = self.target_data["value"].copy()
        true_value.drop(drop_index, inplace=True)
        for name in self.algo_name:
            error[name] = self._calculateError(true_value,
                                               value[name],
                                               method=self.error_method)
        weights = dict()
        total_error = sum(error.values())
        if total_error == 0:
            logger.info("Initialize model coefs and intercept done by total error is zero")
            return self.coefs, 0
        if len(self.algo_name) == 1:
            name = self.algo_name[0]
            weights[name] = 1
        else:
            weights = getModelCoefs(error)
        logger.info("Initialize model coefs done")

        if true_value.shape[0] < self.config["revise_sample_times"]:
            return weights, 0

        predict = list()
        for i in range(len(true_value)):
            num = 0
            for key in value:
                num += weights[key] * value[key][i]
            predict.append(num)

        if self.config["revise"]:
            try:
                intercept = sum(true_value - predict) / len(predict)
            except ZeroDivisionError:
                intercept = 0
        else:
            intercept = 0
        logger.info("Initialize intercept done")
        return weights, intercept


'''
Trevor Urbshas
Mr. Reid
ICS 4U Area of a Triangle
October 3, 2014
October 3, 2014
Calculates the area of a triangle using Heron's formula and trigonometry.
'''
from math import sqrt,sin,cos,radians

print('This program will calculate the area of a triangle using Heron\'s formula and trigonometry')

def checkNum(message): #Create a function to check if inputs are numbers
    while True:
        temp = input(message) #Getting user input. Name is irrelevant because variable is global
        try:
            float(temp)
        except:
            print('Invalid input.')
        else:
            if float(temp) > 0.0:
                return float(temp)
            else:
                print('That is not a valid input.')

while True:
    a = checkNum('What is the length of side a of the triangle? ')
    b = checkNum('What is the length of side b of the triangle? ')
    angleC = radians(checkNum('What is the value of angle C(in degrees)? '))
    c = sqrt(a**2 + b**2 - 2*a*b*(cos(angleC))) #Don't need to ask for length of c because it can be calculated using a,b, and angleC
    s = (a + b + c)/2 #s is half the perimeter of the triangle

    heronsArea = sqrt(s*(s-a)*(s-b)*(s-c)) #Area of the triangle using Heron's formula
    trigArea = (a*b*abs(sin(angleC)))/2 #Area of the triangle using trigonometry

    print('The area of the triangle using Heron\'s formula is ',heronsArea,'.',sep='')
    print('The area of the triangle using trigonometry is ',trigArea,'.',sep='')

    close = input('Do you want to exit the program?(y or Y to close) ')
    if close == 'y' or close == 'Y':
        break

ICS4U - TUrbshas/Unit 1/U1A1Pr4(Area of a Triangle).py


import sys
import time

import tensorflow as tf

from networkcore import networkcore

# Main class
class uresnetcore(networkcore):
    '''Define a network model and run training

    U resnet implementation
    '''
    def __init__(self):
        '''initialization

        Requires a list of parameters as python dictionary

        Arguments:
            params {dict} -- Network parameters

        Raises:
            ConfigurationException -- Missing a required parameter
        '''

        # Call the base class to initialize _core_network_params:
        super(uresnetcore,self).__init__()

        # Extend the parameters to include the needed ones:

        self._core_network_params += [
            'BALANCE_LOSS',
            'NUM_LABELS',
        ]

        return

    def _initialize_input(self, dims):
        '''Initialize input parameters of the network.  Must return a dict type

        For example, paremeters of the dict can be 'image', 'label', 'weights', etc

        Arguments:
            dims {[type]} -- [description]

        Keyword Arguments:
            label_dims {[type]} -- [description] (default: {None})

        Raises:
            NotImplementedError -- [description]
        '''

        inputs = dict()

        inputs.update({
            'image' :  tf.placeholder(tf.float32, dims['image'], name="input_image")
        })

        if 'label' in dims:
            inputs['label'] =  tf.placeholder(tf.int64, dims['label'], name="label")

        if self._params['BALANCE_LOSS']:
            if 'weight' in dims:
                inputs['weight'] = tf.placeholder(tf.float32, dims['weight'], name='weight')
            else:
                raise Exception("Weights requested for loss balance, but dimension not provided.")

        return inputs


import SWiMwB as FD

fd = FD.Face_detector() # obiekt detektora
fd.get_photo() # pobieranie zdjecia z kamerki
fd.detect_face() # wykrywanie twarzy
print(fd.face)
if fd.face is not None:
    fr = FD.Face_recognitor('lbph')

    #fr.read_model('models/lbph/model_BioID_LBPH-2020-06-13_14-32-22.xml','models/lbph/subjects_BioID_LBPH-2020-06-13_14-32-22.csv')
    fr.train_model()
    (how_much, label) = fr.predict(fd.face)
    print('wykryto: ' + label)

else:
    print('nie wykryto twarzy!')

print('done!')


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon May 28 15:35:32 2018

@author: lukas
"""

import nibabel as nib
import numpy as np

nii = nib.load('/home/lukas/Documents/projects/strokeHeads/processed/logTPM/TPM_padded.nii')
d = np.array(nii.get_data(),dtype='float32')
d.dtype
np.min(d[d>0])

d[d<1e-9] = 1e-9  # Values smaller than e-10 start generating too much floating point artifacts.
np.sum(d==0)
np.min(d)
dlog = np.log(d)

img = nib.Nifti1Image(dlog, nii.affine)
nib.save(img, '/home/lukas/Documents/projects/strokeHeads/processed/logTPM/logTPM.nii')


import random
import string
from django.db import transaction
from django.shortcuts import render, redirect, get_object_or_404
from haikunator import Haikunator
from .models import Room, Member, MemberAccept
from django.views.decorators.cache import never_cache
from django.shortcuts import HttpResponseRedirect
from django.contrib.auth.decorators import login_required

from django.contrib.auth.models import User
from django.views.generic import TemplateView
from .forms import FriendForm, LabelForm
from .utils import get_room_or_error, catch_client_error
from django.core.urlresolvers import reverse

haikunator = Haikunator()

@never_cache
@login_required
def about(request):
    return render(request, "chat/about.html")
'''
@never_cache
@login_required
def new_room(request, *args,**kwargs):
    """
    Randomly create a new room, and redirect to it.
    """

    new_room = None
    while not new_room:
        with transaction.atomic():
            label = haikunator.haikunate()
            if Room.objects.filter(label=label).exists():
                continue
            new_room = Room.objects.create(label=label, current_user_id=request.user.id, private = False)

    return redirect("chat_index:chat_room", pk = new_room.pk)
    #return HttpResponseRedirect(reverse("chat_index:chat_room", pk = new_room.pk))
'''

from django.core.exceptions import PermissionDenied

@never_cache
@login_required
#@catch_client_error
def chat_room(request, pk, *args,**kwargs):
    """
    Room view - show the room, with latest messages.

    The template for this view has the WebSocket business to send and stream
    messages, so see the template for where the magic happens.
    """

    # If the room with the given label doesn't exist, automatically create it
    # upon first visit (a la etherpad).
    #room = get_object_or_404(Room, pk=pk)
    room = get_object_or_404(Room, pk=pk)
    access = False
    if room.private == True:
        memberaccepter = MemberAccept.objects.filter(acceptroom_id=pk, accepter_id = request.user.id, agree = True)
        if not memberaccepter:
            if request.user.id != room.current_user_id:
                access = False
            else:
                access = True
        else:
            access = True
    elif room.private == False:
        access = True
    else:
        access = False

    if access != True:
        #need to output with error
        raise PermissionDenied
        #return redirect('chat_index:chat_list_rooms')

    else:
        #rooms = get_room_or_error(pk, request.user)
        #room = Room.objects.get(pk=rooms.pk)

        # We want to show the last 50 messages, ordered most-recent-last
        messages = room.messages.order_by('-timestamp')[:10]
        return render(request, "chat/room.html", {
            'room': room,
            'messages': messages,
        })

from django.core.paginator import Paginator, InvalidPage
from el_pagination.decorators import page_template

@never_cache
@login_required
@page_template('chat/list_rooms_page.html')
def chat_list_rooms(request, template = 'chat/list_rooms.html',
    extra_context=None):
    #template = 'chat/list_rooms.html'
    #page_template = 'chat/list_rooms_page.html'

    context = {
        'object_list': Room.objects.all(),

    }
    if extra_context is not None:
        context.update(extra_context)

    return render(
        request, template, context)

'''
@never_cache
@login_required
def priv_room(request):
    users = User.objects.order_by('id')

    return render(request, "chat/priv_room.html",{
        "users": users,
        })
'''

class FriendView(TemplateView):
    template_name = "chat/priv_room.html"

    def get(self, request, pk, *args, **kwargs):

        room = get_object_or_404(Room, pk=pk)
        #room = Room.objects.get(pk=pk)
        if request.user.id == room.current_user_id:
            form = FriendForm()
            users = User.objects.exclude(id=request.user.id)

            query = request.GET.get("q")

            true_members=[]
            memberlist = None

            if query:
                queryset_list = User.objects.filter(username=query)

            else:
                queryset_list = None

            #your followers
            #drugs = room.who_added_user(request.user)

            #member, created = Member.objects.get_or_create(current_user=request.user, pk=pk)
            #members = member.users.all()
            members = MemberAccept.objects.filter(acceptroom_id=pk, agree=False)
            membersinroom = MemberAccept.objects.filter(acceptroom_id=pk, agree=True)

            if queryset_list:
                memberlist = MemberAccept.objects.filter(acceptroom_id=pk, accepter = queryset_list)

            '''
            for member in members:
                memberlist.append(member)

                for drug in drugs:

                    if drug.pk == member.pk:
                        true_members.append(drug)
                        drugs.remove(drug)
                        memberlist.remove(drug)
            '''

            context = {'form': form, 'users': users,
                   'members': members,
                   'membersinroom': membersinroom,
                   #'drugs': drugs,
                   'object_list': queryset_list,
                   #'true_members': true_members,
                   'memberlist': memberlist,
                   'room': room,
                   }
            return render(request,self.template_name, context)
        else:
            return redirect('home')
    def post(self, request, *args, **kwargs):
        return HttpResponseRedirect(reverse('chat_index:select_room'))

@never_cache
@login_required
def change_members(request, pk, operation, pkid):

    member = User.objects.get(pk=pkid)
    label = get_object_or_404(Room, pk=pk)
    if operation == 'add':
        M = MemberAccept.objects.filter(accepter=member, acceptroom=label)
        if M:
            MemberAccept.objects.get(accepter=member,acceptroom=label)
        else:
            MemberAccept.objects.create(accepter=member,acceptroom=label,agree=False)
        return redirect('chat_index:private_room', pk = pk)
    elif operation == 'remove':
        #Member.remove_member(request.user, label, member)
        M = get_object_or_404(MemberAccept, accepter=member,acceptroom=label)
        M.delete()
        return redirect('chat_index:private_room', pk = pk)

    elif operation == 'delete':
        if label.current_user_id == request.user.id:
            M = get_object_or_404(Room, current_user=member,id=pk)
            M.delete()
            return HttpResponseRedirect(reverse('chat_index:select_room'))
        else:
            raise PermissionDenied
    return redirect('chat_index:private_room', pk = pk)

from django.contrib import messages
@never_cache
@login_required
def get_name(request):
    #template_name = "chat/title_room.html"
    new_room = None
    form = LabelForm(request.POST or None)

    if request.method == 'POST':
        '''
        while not new_room:
            query = request.POST.get("label")
            userid = request.user.id
            #checkmember = Member.objects.filter(current_user_id=userid)
            new_room = Member.objects.create(label=query, current_user_id=userid)
        '''

        #form = LabelForm(request.POST)
        if form.is_valid():
            query = request.POST.get("label")

            #check checkbox
            userid = request.user.id
            private = request.POST.get("private_group", False)
            if private:
                new_room = Room.objects.create(label=query, current_user_id=userid, private = True)
                return HttpResponseRedirect('/chat/private/%s/' % new_room.id)
            else:
                new_room = Room.objects.create(label=query, current_user_id=userid, private = False)
                #reverse for redirecting after POST
                return HttpResponseRedirect(reverse('chat_index:select_room'))

        else:
            render(request, 'chat/title_room.html', {'form': LabelForm()})
    #else:
        #form = LabelForm()
    # if a GET (or any other method) we'll create a blank form

    return render(request, 'chat/title_room.html', {'form': form})

@never_cache
@login_required
def select_room(request, *args,**kwargs):

    rooms = Room.objects.all()
    room_members = MemberAccept.objects.filter(accepter=request.user.id, agree=True)

    #userinrooms = Room.objects.filter(users__id = request.user.id)
    room_invites = MemberAccept.objects.filter(accepter=request.user.id, agree=False)

    room_owners = Room.objects.filter(current_user_id = request.user.id)
    general_rooms = Room.objects.filter(private = 0)
    #general_room_owners = general_rooms.filter(current_user_id = request.user.id)

    '''
    room_chats = []
    for room_member in room_members:
        for room_owner in room_owners:
            room_chats.append(room_member.acceptroom)
            if room_owner.label != room_member.acceptroom:
                room_chats.append(room_owner.label)
    '''

    return render(request, "chat/select_room.html", {
        'rooms': rooms,
        'room_members': room_members,
        #'userinrooms': userinrooms,
        'room_invites': room_invites,
        'room_owners': room_owners,
        'general_rooms': general_rooms,
        #'general_room_owners': general_room_owners,

    })

@never_cache
@login_required
def accept_members(request, pk, operation, pkid):

    accepter = User.objects.get(pk=pkid)
    acceptroom = Room.objects.get(pk=pk)
    if operation == 'agree':
        if accepter.id == request.user.id:
            M = get_object_or_404(MemberAccept, accepter=accepter,acceptroom=acceptroom)
            M.agree = True
            M.save()
            return redirect('chat_index:select_room')
        else:
            raise PermissionDenied

    elif operation == 'disagree':
        if accepter.id == request.user.id:
            M = get_object_or_404(MemberAccept, accepter=accepter,acceptroom=acceptroom, agree = False)
            M.delete()
            return redirect('chat_index:select_room')
        else:
            raise PermissionDenied
    elif operation == 'leave':
        if accepter.id == request.user.id:
            M = get_object_or_404(MemberAccept, accepter=accepter,acceptroom=acceptroom, agree = True)
            M.delete()
            return redirect('chat_index:select_room')
        else:
            raise PermissionDenied
    return redirect('chat_index:select_room')


import tensorflow as tf
import argparse
# import cv2
from tensorflow.examples.tutorials.mnist.input_data import read_data_sets

#tensorboard --logdir logs --port 6789
class Config:
    def __init__(self):
        self.lr = 0.001
        self.filters = 32
        self.batch_size = 64
        self.classes = 10
        self.eps = 1e-8

        self.epoches = 5
        self.name = 'p07'
        self.save_path = './models/{name}/{name}'.format(name = self.name)
        self.sample_path = './data/MNIST_data'
        self.logdir = './logs/{name}'.format(name = self.name)

    def from_cmd_line(self):
        attrs_dict = self._get_attrs()
        parse = argparse.ArgumentParser()
        for key, value in attrs_dict.items():
            parse.add_argument('--'+key, type = type(value), default=value, help = 'set %s' % key)
        a = parse.parse_args()
        for name in attrs_dict:
            setattr(self, name, getattr(a, name))

    def __repr__(self):
        result = '{'
        for key, value in self._get_attrs().items():
            result += '%s=%s,' % (key, value)
        result = result[0:-1]
        result += '}'
        return result

    def __str__(self):
        return self.__repr__()

class Tensors:
    def __init__(self, config: Config):
        with tf.device('/gpu:0'):
            self.lr = tf.placeholder(tf.float32, [], 'lr')
            self.x = tf.placeholder(tf.float32, [None, 784], 'x')
            x = tf.reshape(self.x, [-1, 28, 28, 1])

            x = tf.layers.conv2d(x, config.filters, 3, 1, 'same', activation = tf.nn.relu, name = 'conv1') #[-1, 28, 28, 32]

            x = tf.layers.conv2d(x, 2*config.filters, 3, 1, 'same', activation = tf.nn.relu, name = 'conv2') #[-1, 28, 28, 64]
            x = tf.layers.max_pooling2d(x, 2, 2) # #[-1, 14, 14, 64]

            x = tf.layers.conv2d(x, 4*config.filters, 3, 1, 'same', activation = tf.nn.relu, name = 'conv3') #[-1, 28, 28, 128]
            x = tf.layers.max_pooling2d(x, 2, 2) # #[-1, 7, 7, 128]

            x = tf.layers.flatten(x)
            x = tf.nn.relu(x)

            x = tf.layers.dense(x, 1000, activation=tf.nn.relu, name = 'dense1')
            y_predict = tf.layers.dense(x, config.classes, name = 'dense2')
            y_predict = tf.nn.softmax(y_predict)
            y_predict = tf.maximum(y_predict, config.eps)

            self.y = tf.placeholder(tf.int32, [None], 'y')
            y = tf.one_hot(self.y, config.classes)

            self.loss = -tf.reduce_mean(y*tf.log(y_predict))

            opt = tf.train.AdamOptimizer(self.lr)
            self.train_op = opt.minimize(self.loss)

            tf.summary.scalar('loss', self.loss)
            self.summary_op = tf.summary.merge_all()

class Samples:
    def __init__(self, config: Config):
        self.ds = read_data_sets(config.sample_path)

    def next_batch(self, batch_size):
        return self.ds.train.next_batch(batch_size)

    @property
    def num(self):
        return self.ds.train.num_examples

class Mnist:
    def __init__(self, config: Config):
        self.config = config
        graph = tf.Graph()
        with graph.as_default():
            self.tensors = Tensors(config)
            conf = tf.ConfigProto()
            conf.allow_soft_placement = True
            self.session = tf.Session(graph=graph, config = conf)
            self.saver = tf.train.Saver()
            self.file_writer = tf.summary.FileWriter(config.logdir, graph = graph)
            try:
                self.saver.restore(self.session, config.save_path)
                print('resotr the model successfully!')
            except:
                print('the mdoel doese not exst, we have to train a new one')
                self.train()

    def train(self):
        self.session.run(tf.global_variables_initializer())
        self.samples = Samples(self.config)
        batches =self.samples.num // self.config.batch_size
        for epoch in range(self.config.epoches):
            for batch in range(batches):
                x, y = self.samples.next_batch(self.config.batch_size)
                feed_dic = {
                    self.tensors.x: x,
                    self.tensors.y: y,
                    self.tensors.lr: self.config.lr
                }
                _, lo, su = self.session.run([self.tensors.train_op, self.tensors.loss, self.tensors.summary_op], feed_dict=feed_dic)
                self.file_writer.add_summary(su, epoch * batches + batch)
            print('epoch=%d, loss=%f' % (epoch, lo))
        self.saver.save(self.session, self.config.save_path)

    def predict(self):
        pass

    def close(self):
        self.session.close()

if __name__ == '__main__':
    config = Config()
    mnist = Mnist(config)
    mnist.close()


import torch
import regex
import logging
import numpy as np
from math import ceil

from multiprocessing import Pool as ProcessPool
from multiprocessing.util import Finalize

from ..retriever import TfidfDocRanker
from ..retriever import DocDB

from transformers import AutoModelForQuestionAnswering, AutoTokenizer

DEFAULTS = {
    'ranker': TfidfDocRanker,
    'db': DocDB,
    'reader_model': 'distilbert-base-cased-distilled-squad',
}

logger = logging.getLogger(__name__)

# ------------------------------------------------------------------------------
# Multiprocessing functions to fetch text
# ------------------------------------------------------------------------------

PROCESS_DB = None

def init(db_class, db_opts):
    global PROCESS_DB
    PROCESS_DB = db_class(**db_opts)
    Finalize(PROCESS_DB, PROCESS_DB.close, exitpriority=100)

def fetch_text(doc_id):
    global PROCESS_DB
    return PROCESS_DB.get_doc_text(doc_id)

# ------------------------------------------------------------------------------
# Main DrQA pipeline
# ------------------------------------------------------------------------------

class DrQATransformers(object):
    def __init__(
        self,
        reader_model=None,
        use_fast_tokenizer=True,
        group_length=500,
        batch_size=32,
        cuda=True,
        num_workers=None,
        db_config=None,
        ranker_config=None,
    ):
        """Initialize the pipeline.

        Args:
            reader_model: name of the Huggingface transformer QA model.
            use_fast_tokenizer: whether to use fast tokenizer
            batch_size: batch size when processing paragraphs.
            cuda: whether to use gpu.
            num_workers: number of parallel CPU processes to use for retrieving
            db_config: config for doc db.
            ranker_config: config for ranker.
            group_length: target size for squashing short paragraphs together.
                0 = read every paragraph independently
                infty = read all paragraphs together
        """
        assert use_fast_tokenizer == True, 'Current version only support models with fast tokenizer'
        self.batch_size = batch_size
        self.device = 'cuda' if cuda else 'cpu'
        self.num_workers = num_workers
        self.group_length = group_length

        logger.info('Initializing document ranker...')
        ranker_config = ranker_config or {}
        ranker_class = ranker_config.get('class', DEFAULTS['ranker'])
        ranker_opts = ranker_config.get('options', {})
        self.ranker = ranker_class(**ranker_opts)

        logger.info('Initializing document reader & tokenizer...')
        reader_model = reader_model or DEFAULTS['reader_model']
        self.reader = AutoModelForQuestionAnswering \
            .from_pretrained(reader_model) \
            .eval() \
            .to(self.device)
        self.need_token_type = self.reader.base_model_prefix not in {
            "xlm", "roberta", "distilbert", "camembert", "bart", "longformer"
        }
        tokenizer_kwargs = {}
        if self.reader.base_model_prefix in {'mobilebert'}:
            tokenizer_kwargs['model_max_length'] = self.reader.config.max_position_embeddings
        #
        self.tokenizer = AutoTokenizer.from_pretrained(reader_model, use_fast=use_fast_tokenizer, **tokenizer_kwargs)

        logger.info('Initializing document retrievers...')
        db_config = db_config or {}
        db_class = db_config.get('class', DEFAULTS['db'])
        db_opts = db_config.get('options', {})

        self.processes = ProcessPool(
            num_workers,
            initializer=init,
            initargs=(db_class, db_opts)
        )

    def _split_doc(self, doc):
        """Given a doc, split it into chunks (by paragraph)."""
        curr = []
        curr_len = 0
        for split in regex.split(r'\n+', doc):
            split = split.strip()
            if len(split) == 0:
                continue
            curr.append(split)
            curr_len += len(split)
            # Maybe group paragraphs together until we hit a length limit
            if len(curr) > 0 and curr_len > self.group_length:
                yield ' '.join(curr)
                curr = []
                curr_len = 0
        if len(curr) > 0:
            yield ' '.join(curr)

    def process(self, query, top_n=1, n_docs=5, return_context=False):
        """Run a single query."""

        # Rank documents for query.
        ranked = [self.ranker.closest_docs(query, k=n_docs)]
        all_docids, all_doc_scores = zip(*ranked)

        # Flatten document ids and retrieve text from database.
        # We remove duplicates for processing efficiency.
        flat_docids = list({d for docids in all_docids for d in docids})
        # did2didx = {did: didx for didx, did in enumerate(flat_docids)}
        didx2did = {didx: did for didx, did in enumerate(flat_docids)}
        doc_texts = self.processes.map(fetch_text, flat_docids)

        # Split and flatten documents. Maintain a mapping from doc (index in
        # flat list) to split (index in flat list).
        flat_splits = []
        didx2sidx = []
        sidx2didx = []
        for i, text in enumerate(doc_texts):
            splits = self._split_doc(text)
            didx2sidx.append([len(flat_splits), -1])
            for split in splits:
                flat_splits.append(split)
                sidx2didx.append(i)
            didx2sidx[-1][1] = len(flat_splits)

        # Tokenize
        inputs = self.tokenizer(
            [query]*len(flat_splits),
            flat_splits,
            padding=True,
            truncation='only_second',
            stride=96,
            return_overflowing_tokens=True,
            return_attention_mask=True,
            return_token_type_ids=self.need_token_type,
            return_offsets_mapping=True,
            return_tensors='pt',
        ).to(self.device)

        # Split batches
        n_examples = inputs.input_ids.shape[0]
        n_batch = ceil(n_examples / self.batch_size)
        batches = [i*self.batch_size for i in range(n_batch)] #[0,32,64,..]
        batches.append(n_examples)

        # Feed forward batches
        outputs = []
        for i in range(n_batch):
            with torch.no_grad():
                if self.need_token_type:
                    output = self.reader(
                        inputs.input_ids[batches[i]:batches[i+1]],
                        inputs.attention_mask[batches[i]:batches[i+1]],
                        token_type_ids=inputs.token_type_ids[batches[i]:batches[i+1]],
                    )
                else:
                    output = self.reader(
                        inputs.input_ids[batches[i]:batches[i+1]],
                        inputs.attention_mask[batches[i]:batches[i+1]],
                    )
                outputs.append(output)

        # Join batch outputs
        start_logits = torch.cat([o.start_logits for o in outputs], dim=0)
        end_logits = torch.cat([o.end_logits for o in outputs], dim=0)

        # decode start-end logits to span slice & score
        start, end, score, idx_sort = self.decode_logits(start_logits, end_logits, topk=top_n)

        # Produce predictions, take top_n predictions with highest score
        all_predictions = []
        for i in range(top_n):
            split_id = inputs.overflow_to_sample_mapping[idx_sort[i]].item()
            start_char = inputs.offset_mapping[idx_sort[i], start[i], 0].item()
            end_char = inputs.offset_mapping[idx_sort[i], end[i], 1].item()
            prediction = {
                'doc_id': didx2did[sidx2didx[split_id]],
                'span': flat_splits[split_id][start_char:end_char],
                'doc_score': float(all_doc_scores[0][sidx2didx[split_id]]),
                'span_score': float(score[i]),
            }
            if return_context:
                prediction['context'] = {
                    'text': flat_splits[split_id],
                    'start': start_char,
                    'end': end_char,
                }
            all_predictions.append(prediction)

        return all_predictions

    def decode_logits(self, start_logits, end_logits, topk=1, max_answer_len=None):
        """
        Take the output of any :obj:`ModelForQuestionAnswering` and generate score for each span to be the actual answer.

        In addition, it filters out some unwanted/impossible cases like answer len being greater than max_answer_len or
        answer end position being before the starting position. The method supports output the k-best answer through
        the topk argument.

        Args:
            start_logits (:obj:`tensor`): Individual start logits for each token. # shape: batch, len(input_ids[0])
            end_logits (:obj:`tensor`): Individual end logits for each token.
            topk (:obj:`int`): Indicates how many possible answer span(s) to extract from the model output.
            max_answer_len (:obj:`int`): Maximum size of the answer to extract from the model's output.
        Output:
            starts:  top_n predicted start indices
            ends:  top_n predicted end indices
            scores:  top_n prediction scores
            idx_sort:  top_n batch element ids
        """
        start = start_logits.cpu().numpy().clip(min=0.0)
        end = end_logits.cpu().numpy().clip(min=0.0)
        max_answer_len = max_answer_len or start.shape[1]

        # Compute the score of each tuple(start, end) to be the real answer
        outer = np.matmul(np.expand_dims(start, -1), np.expand_dims(end, 1))

        # Remove candidate with end < start and end - start > max_answer_len
        candidates = np.tril(np.triu(outer), max_answer_len - 1)

        #  Inspired by Chen & al. (https://github.com/facebookresearch/DrQA)
        scores_flat = candidates.flatten()
        if topk == 1:
            idx_sort = [np.argmax(scores_flat)]
        elif len(scores_flat) < topk:
            idx_sort = np.argsort(-scores_flat)
        else:
            idx = np.argpartition(-scores_flat, topk)[0:topk]
            idx_sort = idx[np.argsort(-scores_flat[idx])]

        idx_sort, starts, ends = np.unravel_index(idx_sort, candidates.shape)
        scores = candidates[idx_sort, starts, ends]

        return starts, ends, scores, idx_sort


def xulychuoi(s): #TÁCH SỐ RA TỪ CHUỖI CON
	list = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
	n = len(s)-1
	for i in range(0, n+1): #CHO VÒNG LẶP I CHẠY, QUÉT TỪNG CHỮ CÁI CỦA CHUỖI
		s2 = s[i]
		new = []
		chuoi = ""
		if (s2 in list): #NẾU THẤY XUẤT HIỆN CÓ SỐ THÌ CỨ QUÉT
			a = i+1
		else:
			break #NẾU KHÔNG THẤY THÌ DỪNG LẠI VÀ THOÁT

	new = int(s[0:a]) #IN RA CHUỖI MÀ HẤT TỪ KÍ TỰ SỐ TRỞ VỀ BAN ĐẦU
	return -new

def tachchuoi(s): #TÁCH RA LÀM CÁC CHUỖI CON
	dem = 0
	list = s.split("-") #TẠO RA CÁC CHUỖI CON  KHI XOÁ "-"
	n = len(list)-1
	list2 = []
	for i in range(0, n+1): #LẤY RA CÁC CHUỖI CON MÀ CÓ BẮT ĐẦU LÀ SỐ
		for j in range(0, 10):
			if (list[i].startswith(str(j))):
				list2.append(list[i])
	return list2 #TRẢ VỀ CÁC CHUỖI CON ĐÓ

print("Nhap vao chuoi: ")
s = str(input())

dem = 0
ketqua1 = tachchuoi(s)
for i in ketqua1:
	dem += 1
for j in range(0, dem):
	test = xulychuoi(ketqua1[j])
	print(test,end=" ")
print(" ")


import getopt
import os
import re
import sys

import pandas as pd

import Ngrams as ng
import Utils as ut
from nlon import training_nlon

nlon, nlon_model = training_nlon()

"""
 il codice è modificato per non usare score_file
"""

def load_mairesse_features(input_arff, output_csv):
    # Read and clean arff file
    _encoding = 'utf-8'
    arff_path = input_arff
    csv_path = arff_path + 'csv'
    with open(arff_path, 'r', encoding=_encoding) as fr:
        attributes = []
        write_sw = False
        with open(csv_path, 'w', encoding=_encoding) as fw:
            for line in fr.readlines():
                if write_sw:
                    fw.write(line)
                elif '@data' in line:
                    fw.write(','.join(attributes) + '\n')
                    write_sw = True
                elif '@attribute' in line:
                    attributes.append(line.split()[1])

    # Read result as csv
    df = pd.read_csv(csv_path)
    os.remove(csv_path)
    for i in range(0, len(df)):
        df.iloc[i, df.columns.get_loc('filename')] = re.search(r'(.*\/)(.*)', df['filename'][i]).groups()[1]  # (\.txt)

    # Remove useless columns
    df.drop([list(df)[len(df.columns) - i] for i in range(1, 6)], axis=1, inplace=True)
    df.drop(['BROWN-FREQ', 'K-F-FREQ', 'K-F-NCATS', 'K-F-NSAMP', 'T-L-FREQ'], axis=1, inplace=True)

    # Replace '?' with 0
    for i in range(0, len(df)):
        df.iloc[i] = df.iloc[i].replace('?', '0')

    df.rename(columns={'filename': 'ID'}, inplace=True)
    df.to_csv(output_csv, index=False)

def load_corpus(infile):
    _dict = dict()
    df = pd.read_csv(infile, sep=';', header=0)
    for row in df.itertuples():
        key = row[1]
        value = list()
        for line in row[2].split('\n'):
            line = line.strip()
            if not line or line == '' or nlon.NLoNPredict(nlon_model, line)[0] == 'Not':
                pass
            else:  # it's natural language
                value.append(line)
        _dict[key] = value

    return _dict

if __name__ == "__main__":
    argv = sys.argv[1:]
    path = ''
    arff_file = ''
    out_file = ''
    in_file = ''
    input_ngrams_set_file = ''
    output_ngrams_set_file = ''
    ngrams_extraction = True
    nlon_cleaning = False
    task_num = '8'

    help_string = "\nUsage:\n" \
                  'python newdata_feature_extraction.py [-t task] [--nlon] -i <input-file> -a <arff_file> ' \
                  '-o <output-file> [--ngrams <file>]'
    try:
        opts, args = getopt.getopt(argv, "ht:i:o:a:n:", ["task=", "input=", "nlon", "output=", "arff=", "ngrams="])
    except getopt.GetoptError as e:
        print(e)
        print(help_string)
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print(help_string)
            sys.exit(0)
        elif opt in ("--nlon"):
            nlon_cleaning = True
        elif opt in ("-a", "--arff"):
            arff_file = arg
            if not os.path.isfile(arff_file):
                print('Arff file {0} not found'.format(arff_file))
                sys.exit(2)
        elif opt in ("-t", "--task"):
            task_num = arg
            if task_num.isdigit():
                task_num = int(task_num)
            else:
                print('Wrong task `{0}`. Must be a number 1-8'.format(task_num))
                sys.exit(2)
        elif opt in ("-n", "--ngrams"):
            input_ngrams_set_file = arg
            ngrams_extraction = False
            if not os.path.isfile(input_ngrams_set_file):
                print('N-grams file {0} not found'.format(input_ngrams_set_file))
                sys.exit(2)
        elif opt in ("-i", "--input"):
            in_file = arg
            if not os.path.isfile(in_file):
                print('Input dataset file {0} not found'.format(in_file))
                sys.exit(2)
        elif opt in ("-o", "--output"):
            out_file = arg

    if in_file:
        # load corpus as dictionary
        corpus_dict = load_corpus(in_file)
    else:
        print('No input file for the new data')
        print(help_string)
        sys.exit(2)

    # generates a file formatted as ID,cOPE,cCON,cEXT,cAGR,cNEU
    # with only IDs available
    score_file = ['classes_task', '.csv']
    if task_num in range(1, 9):  # values allowed 1-8
        pass
        ut.get_empty_score_file(sorted(corpus_dict.keys()), str(task_num).join(score_file))
    else:
        print('Wrong task number')
        print(help_string)
        sys.exit(2)

    dataset_name = in_file.split('.')[0]
    if ngrams_extraction:
        output_ngrams_set_file = os.path.join('tmp', dataset_name + '_ngrams_task' + str(task_num) + '.set')

    ngram_df = ng.get_ngram_df(corpus_dict, input_ngrams_set_file, ngrams_extraction, output_ngrams_set_file)

    feature_file = ['feature_task', '.csv']
    ut.get_feature_csv_from_arff(arff_file, str(task_num).join(feature_file), str(task_num).join(score_file))

    # Merge feature df with ngram_df
    feature_df = pd.read_csv(str(task_num).join(feature_file), sep=',')
    complete_df = pd.merge(left=ngram_df, right=feature_df, on='ID')
    complete_df.to_csv(os.path.join(out_file), index=False)

    print("Features saved into {0} file".format(out_file))
    sys.exit(0)


#-*- coding:utf-8 -*-
import os
import codecs
import re
import shutil
import time

def process_lua(file_name):
	f = open(file_name,'r', encoding='UTF-8')
	result = list()
	xx_temp = "^(?!.*--).*(?P<type>{}\.\w+)"

	for line in f.readlines():
		for x in namespaces:		#遍历所有的命名空间，找出合适的
			xx = xx_temp.format(x)
			m = re.search(xx, line)

			if 	m:
				my_type = m.groupdict()["type"]
				if not(my_type in lst):
					lst.append(my_type)
				break

def WalkAll(srcPath):
	if not os.path.isdir(srcPath) and not os.path.isfile(srcPath):
		print("path not exist" + srcPath)
		return False

	if os.path.isfile(srcPath):
		if srcPath.find(".meta") > -1 or srcPath.find("~$") >= 0:
			#print("skip file: " + srcPath)
			return

		process_lua(srcPath)
		#count = count + 1

	elif os.path.isdir(srcPath):
		#os.makedirs(dest_path)
		for x in os.listdir(srcPath):
			WalkAll(os.path.join(srcPath,x))

#_dir = "F:\Wartune.Client______\Assets\LuaScripts\cs2lua\wartune\singledunglogic.lua"
#_dir = """F:\Wartune.Client______\Assets\LuaScripts\game\modules\\vitality\\vitalitysysmanager.lua"""
_dir = """F:\Wartune.Client______\Assets\LuaScripts"""
lst = list()
namespaces = ['UnityEngine.UI.ProceduralImage',
			'UnityEngine.Profiling',
			'UnityEngine.SceneManagement',
			'UnityEngine.Events',
			'UnityEngine.EventSystems',
			'UnityEngine.Assertions',
			'UnityEngine.Serialization',
			'UnityEngine.UI',
			'UnityEngine.AI',
			'UnityEngine',
			'System.Xml',
			"Paran",
			"FlatBuffers",
			"DG.Tweening",
			#"ETCInput",
			"RimFlow",
			#"ComponentExtensions",
			"SRF",
			"ScreenFade",
			"ScreenBlur",
			"UIHerDragCtrl",
			]

black_name = ["UnityEngine.Light",
			"UnityEngine.UI.Graphic",
			"UnityEngine.Events.UnityEvent_T0",]

addition = ["XLuaExtensions"]

if __name__ == "__main__":
	WalkAll(_dir)

	for j in black_name:
		lst.remove(j)

	for x in addition:
		lst.append(x)

	lst_cs = list()
	for x in lst:
		lst_cs.append('typeof({}),'.format(x))

	print("\n".join(lst_cs))


import os.path
from result_parser.util import *
from result_parser.fio_parse.terse import FORMAT

class FioParse(object):
    """FioParse - a class to parse and display terse outputs from FIO"""
    def __init__(self, path: str = None) -> None:
        """
        The only thing needed to digest the output of a FIO test is the path
          to the terse-format results file. This file has consistent output
          between versions of FIO as the fields are standardized.
        Due to ease of parsing the output, there is no reason to read the file
          until we need to produce the results of it so the only thing stored
          is the absolute path.
        """
        logger.debug(
            ' '.join(['Received request to generate FioParse object for:',
                      '{}'.format(path)])  # Use format notation here to ensure
        )                                  # that any object logs properly
        # Validate our supplied path
        if path is None:
            logger.warning('FioParse object instantiated with no path')
            self.path = None
        elif os.path.isfile(path):
            logger.debug('Storing path: {}'.format(path))
            self.path = os.path.abspath(path)
        else:
            logger.error('FioParse object instantiated with invalid path')
            self.path = None

    @staticmethod
    def _is_zero(val: str) -> bool:
        """
        FioParse._is_zero() returns true if the provided string is a string
          representation of zero, either as the integer zero, a decimal
          equivalent of zero, or as a representation of zero percent.
        """
        val = val.strip()
        if val == '0':
            return True
        if val.strip('%').strip() == '0':
            return True
        try:
            if '.' not in val and int(val.strip('%').strip()) == 0:
                return True
            elif '.' in val and float(val.strip('%').strip()) == 0.0:
                return True
        except ValueError:
            pass
        return False

    def parse(self) -> list:
        """
        FioParse.parse() returns a list of all of the parsed results present
          in path the object was instantiated with, returning an empty list if
          self.path wasn't saved (because it was invalid).
        """
        if self.path is None:
            logger.warning(
                'Request to generate a FIO output report with no path.'
            )
            return []

        ret = []
        with open(self.path, mode='r', encoding='utf-8') as f:
            for line in f.readlines():
                # Expect that the firsts field of terse output should start
                # with the number three, as this is the version of output that
                # we support. This strictly specifies compatibility with this
                # version of terse output (currently the default version in
                # modern releases of fio, and has been since 2012)
                # https://github.com/axboe/fio/commit/3449ab8c4d2addb716105ded698438a1d3811572
                data = line.split(';')
                if '3' in data and len(data) > 1 and data[1] == '3':
                    parsed = {}
                    # Prepare our parsed output for this line by identifying
                    # the mapped block device and ensuring our return dict has
                    # a section for it.
                    blkdev_map = data[0]
                    path = data[0].split(':')[0].split('->')[0]
                    dev = data[0].split(':')[0].split('->')[-1]
                    fstype = data[0].split('::')[-1].split('->')[0]
                    # Our FORMAT dict should have all the fields neatly
                    # labelled to support directly mapping the names to indexes
                    # in our data
                    for k in FORMAT:
                        if not self._is_zero(data[k]):
                            parsed[FORMAT[k]] = best_rep(data[k])
                    logger.debug('Parsed the following disk output:')
                    logger.debug(line)
                    logger.debug('Generated:')
                    logger.debug(parsed)
                    # If there is a result with a job name that is for the same
                    # path as this, just add another job to that container
                    handled = False
                    for result in ret:
                        try:
                            if result['path'] == path:
                                result[parsed['INFO_job_name']] = parsed
                                handled = True
                                break
                        except TypeError:
                            # This is because we tried to slice into a
                            # string via dictionary key - it's fine
                            pass
                    # If one doesn't exist, we'll create a new one
                    if not handled:
                        ret.append({
                            'blkdev_map': blkdev_map,
                            'path': path,
                            'dev': dev,
                            'fstype': fstype,
                            parsed['INFO_job_name']: parsed
                        })
                else:
                    logger.warning(
                        'Cannot parse output of file {} line:'.format(
                            self.path
                        )
                    )
                    logger.warning(line.strip())
        return ret


# The clock shows 'h' hours, 'm' minutes and 's' seconds after midnight.
# Your task is to make the 'past' function return the time converted to milliseconds.
# More examples in the test cases below.

def past(h, m, s):

    # need to know how many milliseconds are in a hour/minute/second
    # convert hh into milliseconds
    hours_in_ms = h * 3600000
    # convert mm into milliseconds
    minutes_in_ms = m * 60000
    # convert ss into milliseconds
    seconds_in_ms = s * 1000
    # add hh, mm, ss millisecond values together
    # print output in milliseconds

    return hours_in_ms + minutes_in_ms + seconds_in_ms


# -*- encoding: utf-8 -*-
"""
    package.module
    ~~~~~~~~~~~~~~

    API for project pod monitor

    :copyright: (c)  2020/9/21 by Richie.Song.
    :license: OPS, see LICENSE_FILE for more details.

####
实现一个 Trie (前缀树)，包含 insert, search, 和 startsWith 这三个操作。

示例:

Trie trie = new Trie();

trie.insert("apple");
trie.search("apple");   // 返回 true
trie.search("app");     // 返回 false
trie.startsWith("app"); // 返回 true
trie.insert("app");
trie.search("app");     // 返回 true
说明:

你可以假设所有的输入都是由小写字母 a-z 构成的。
保证所有输入均为非空字符串。

"""

class Trie(object):
    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.root = {}
        self.end_of_word = "#"

    def insert(self, word):
        """
        Inserts a word into the trie.
        :type word: str
        :rtype: None
        """
        node = self.root
        for char in word:
            node = node.setdefault(char, {})
        node[self.end_of_word] = self.end_of_word

    def search(self, word):
        """
        Returns if the word is in the trie.
        :type word: str
        :rtype: bool
        """
        node = self.root
        for char in word:
            if char not in node:
                return False
            node = node[char]
        return self.end_of_word in node

    def startsWith(self, prefix):
        """
        Returns if there is any word in the trie that starts with the given prefix.
        :type prefix: str
        :rtype: bool
        """
        node = self.root
        for char in prefix:
            if char not in node:
                return False
            node = node[char]
        return True

if __name__ == '__main__':
    t = Trie()
    c = "songxiaoming"
    print(t.insert(c))
    print(t.search("song"))
    print(t.startsWith("song"))


from .Events import *
from .EMS import Event_Management_System, event_creation, Seminar_methods
from .Users import User, Student, Admin, Attendance
from .Session import session_attendees
from flask_login import login_user
from datetime import datetime

def date_converter(date_input):
    date_format = "%Y-%m-%d"
    date = datetime.strptime(date_input, date_format)
    return date

def bootstrap_system():

    system = Event_Management_System()
    b = 0
    with open("user.csv") as fp: #close file is called automatically
        for line in fp:
            if b > 0:
                l = line.split(",")

                if 'trainer' in line:
                    new_user = Admin(l[0], l[1], l[2], l[3], l[4], "secret")
                    system.add_users(new_user)
                else:
                    new_user = Student(l[0], l[1], l[2], l[3], l[4])
                    system.add_users(new_user)
            b += 1
    return system
'''
    user = Admin("Stephanie", "5133975", "z5133975@unsw.net", "pass", "trainer", "secret")
    system.add_users(user)
    Course = event_creation.create_course("course details", date_converter("2018-5-13"), date_converter("2018-5-13"), date_converter("2018-5-12"), 50, date_converter("2018-5-12"), 10, "test input", user, "UNSW", 0, 0)
    event_creation.add_course(system, Course)    # add new course in system
    user.post_event(Course)         # US1: add to admin subclass my event list
    event_creation.add_order(system, Course) # add new event in system
    Attendance.add_attending(user, Course) #add to users own record list
    Course.listAttendee().append(user)

    newSem = Seminar_methods.create_seminar('details', date_converter('2018-01-12'),date_converter('2018-01-15'), date_converter('2018-01-10'), 100, date_converter('2018-01-10'), 2, 'numSess', 2, 1, 1)
    #def create_seminar(details, start_date, end_date, deregistration, fee, early_bird, maxAttendee, SemName, numSess, typeof, eveID):
    user.post_event(newSem)
    Seminar_methods.add_seminar(system, newSem)

    s1 = Session('SessName', user, date_converter('2018-01-03'), 'Venue', 'SessDetails', 1, 0)
    newSem.add_sess(s1)
    Attendance.add_attending(user, newSem) #add to users own record list
    session_attendees.add_attendees(s1, user) #add to the session's list attendees
    newSem.listAttendee().append(user)     #aooe

    s2 = Session('SessName', user, date_converter('2018-01-04'), 'Venue', 'SessDetails', 1, 1)
    newSem.add_sess(s2)
    session_attendees.add_attendees(s2, user) #add to the session's list attendees
    '''


import numpy as np

GROUND_CLASS = {
    "NBE": None,
    "G": 0,
    'Z': 1,
    'L': 2,
    'K': 3,
    'V': 4,
    "H": 4,
    "S": 2
}

IC_MAPPING = {
    "G": 0.5,
    "Z": 2.6,
    "L": 2.95,
    "K": 4,
    "V": 100
}

GAMMA_MAPPING = {
    "G": 20,
    "Z": 20,
    "L": 20,
    "K": 17,
    "V": 11,
    "H": 11,
    "S": 20
}

MAIN_SOIL = ["G", "Z", "L", "K", "V"]
GAMMA_ARRAY = np.array([GAMMA_MAPPING[k] for k in MAIN_SOIL])

def det_u_hydrostatic(l, water_level=1.):
    """

    :param l: (array)
    :param water_level: (flt) Depth of the water level.
    :return: (array) Hydrostatic water pressure
    """
    u = (l - water_level) * 10e-3
    u[u < 0] = 0
    return u

def det_layer_weight(sp, dl):
    return dl * np.dot(GAMMA_ARRAY / 1e3, sp)

def det_ground_pressure(l, sp):
    """
    Assumes the last layer is the same thickness as the previous layer.

    Note that the l array could be like this:
     [1.2, 1.22, 1.24, ... n]

    The first values will be assumed to be 1.2 meters of the first layer.

    :param l: (array) Length
    :param sp: (array) Soil print
    :return: (array) Ground pressure
    """
    dl = np.diff(l)
    return np.cumsum(np.dot(sp, GAMMA_ARRAY * 1e-3) * np.append(l[0], dl))

def det_effective_stress(l, u2, sp):
    """

    :param l: (array) Length
    :param u2: (array) Water pressure
    :param sp: (2D array) Soil print
    :return: (array) Effective stress
    """
    return det_ground_pressure(l, sp) - u2


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

from argparse import ArgumentParser, FileType

from mpmath import mp
import numpy as np

from pyfr import __version__ as version
from pyfr.backends import get_backend
from pyfr.inifile import Inifile
from pyfr.mpiutil import register_finalize_handler
from pyfr.rank_allocator import get_rank_allocation
from pyfr.progress_bar import ProgressBar
from pyfr.readers.native import read_pyfr_data
from pyfr.solvers import get_solver

def process_run(args):
    return read_pyfr_data(args.mesh), None, Inifile.load(args.cfg)

def process_restart(args):
    mesh = read_pyfr_data(args.mesh)
    soln = read_pyfr_data(args.soln)

    # Ensure the solution is from the mesh we are using
    if soln['mesh_uuid'] != mesh['mesh_uuid']:
        raise RuntimeError('Invalid solution for mesh.')

    # Process the config file
    if args.cfg:
        cfg = Inifile.load(args.cfg)
    else:
        cfg = Inifile(soln['config'])

    return mesh, soln, cfg

@mp.workdps(60)
def main():
    from mpi4py import MPI

    ap = ArgumentParser(prog='pyfr-sim', description='Runs a PyFR simulation')
    ap.add_argument('--verbose', '-v', action='count')
    ap.add_argument('--backend', '-b', default='cuda', help='Backend to use')
    ap.add_argument('--progress', '-p', action='store_true',
                    help='show a progress bar')
    ap.add_argument('--nansweep', '-n', metavar='N', type=int,
                    help='check for NaNs every N steps')

    sp = ap.add_subparsers(help='sub-command help')

    ap_run = sp.add_parser('run', help='run --help')
    ap_run.add_argument('mesh', help='mesh file')
    ap_run.add_argument('cfg', type=FileType('r'), help='config file')
    ap_run.set_defaults(process=process_run)

    ap_restart = sp.add_parser('restart', help='restart --help')
    ap_restart.add_argument('mesh', help='mesh file')
    ap_restart.add_argument('soln', help='solution file')
    ap_restart.add_argument('cfg', nargs='?', type=FileType('r'),
                            help='new config file')
    ap_restart.set_defaults(process=process_restart)

    # Parse the arguments
    args = ap.parse_args()
    mesh, soln, cfg = args.process(args)

    # Ensure MPI is suitably cleaned up
    register_finalize_handler()

    # Create a backend
    backend = get_backend(args.backend, cfg)

    # Get the mapping from physical ranks to MPI ranks
    rallocs = get_rank_allocation(mesh, cfg)

    # Construct the solver
    solver = get_solver(backend, rallocs, mesh, soln, cfg)

    # If we are running interactively then create a progress bar
    if args.progress and MPI.COMM_WORLD.rank == 0:
        pb = ProgressBar(solver.tstart, solver.tcurr, solver.tend)

        # Register a callback to update the bar after each step
        callb = lambda intg: pb.advance_to(intg.tcurr)
        solver.completed_step_handlers.append(callb)

    # NaN sweeping
    if args.nansweep:
        def nansweep(intg):
            if intg.nsteps % args.nansweep == 0:
                if any(np.isnan(np.sum(s)) for s in intg.soln):
                    raise RuntimeError('NaNs detected at t = {}'
                                       .format(intg.tcurr))
        solver.completed_step_handlers.append(nansweep)

    # Execute!
    solver.run()

if __name__ == '__main__':
    main()


import weka.core.jvm as jvm
jvm.start()

data_dir = "D:\\Programing\\snipplets\\FraudData"
from weka.core.converters import Loader
loader = Loader(classname="weka.core.converters.ArffLoader")
data = loader.load_file(data_dir + "iris.arff")
data.class_is_last()

print(data)


from odoo import models, fields, api, exceptions
from datetime import date,datetime
from dateutil.relativedelta import relativedelta
from collections import OrderedDict
import xlsxwriter
from io import StringIO
import base64
import pandas as pd
import numpy as np
import logging

_logger = logging.getLogger(__name__)

class libro_mayor_reportes_chile(models.TransientModel):
    _inherit = 'wizard.reportes.chile'

    @api.multi
    def dic_libro_mayor(self):
        dic = OrderedDict([
                ('Fecha',''),
                ('Comprobante',''),
                ('Rut',''),
                ('Partner',''),
                ('Cuenta',''),
                ('Glosa',''),
                ('Documento',''),
                ('Debe',0.0),
                ('Haber',0.0),
                ('Saldo',0.0)
                ])
        return dic

    @api.multi
    def _libro_mayor_sql(self,wizard=False):
        if wizard:
            wiz = self.search([('id','=',wizard)])
        else:
            wiz = self
        company = wiz.company_id.id
        #if not (wiz.fecha_inicio or wiz.fecha_term):
        #    raise exceptions.Warning('Debe seleccionar al menos un periodo')
        #periodo = wiz.period_ids[0]
        fecha_inicio = wiz.fecha_inicio
        fecha_term = wiz.fecha_term
        #fiscal = periodo.fiscalyear_id.id
        #perids = set(wiz.period_ids.ids)
        #periodos= '('+",".join(map(str,perids))+')'
        cuentas = ''
        if wiz.acount_ids:
            cuentas = 'and aa.id in ('+",".join(map(str,set(wiz.acount_ids.ids)))+')'
        wiz.env.cr.execute("""
            SELECT
            null as Fecha,
            'Saldo Inicial' as comprobante,
            null,
            null,
            concat_ws(' - ', aa.code::text, aa.name::text) as cuenta,
            null,
            null,
            sum(aml.debit),
            sum(aml.credit)

            FROM
            account_move_line aml,
            account_account aa,
            account_move am

            WHERE
            aml.account_id=aa.id and
            aml.move_id=am.id and
            am.state='posted' and
            aml.company_id = %s and
            aml.date <= '%s'
            %s

            GROUP BY
            cuenta

            UNION ALL

            SELECT
            q1.fecha,
            q1.comprobante,
            q2.rut,
            q2.partner,
            q1.cuenta,
            q1.ref,
            q1.nombre,
            q1.debit,
            q1.credit

            FROM
            (
            SELECT

            aml.date as fecha,
            am.name as comprobante,
            concat_ws(' - ', aa.code::text, aa.name::text) as cuenta,
            aml.ref as ref,
            aml.name as nombre,
            aml.debit as debit,
            aml.credit as credit,
            aml.partner_id as partner_id

            FROM
            account_move_line aml,
            account_account aa,
            account_move am

            WHERE
            aml.account_id=aa.id and
            aml.move_id=am.id and
            am.state='posted' and
            aml.company_id = %s and
            aml.date >= '%s' and
            aml.date <= '%s'
            %s
            )q1

            LEFT JOIN

            (
            SELECT

            rp.id id,
            rp.document_number rut,
            rp.name partner

            FROM

            res_partner rp
            )q2

            ON
            q2.id=q1.partner_id

            ORDER BY
            cuenta, fecha NULLS FIRST, comprobante
            """ %(company,fecha_inicio,cuentas,company,fecha_inicio,fecha_term,cuentas))
        dic = wiz.dic_libro_mayor()
        lista = []
        docs = wiz.env.cr.fetchall()
        # cuentas = set([(record[9],record[4]) for record in docs])
        # for record in cuentas:
        #     lista += wiz._libro_mayor_saldo_inicial_sql(record[0],record[1])
        for record in docs:
            dicti = OrderedDict()
            dicti.update(dic)
            dicti['Fecha']=record[0]
            dicti['Comprobante']=record[1]
            dicti['Rut']=record[2]
            dicti['Partner']=record[3]
            dicti['Cuenta']=record[4]
            dicti['Glosa']=record[5]
            dicti['Documento']=record[6]
            dicti['Debe']=float(record[7])
            dicti['Haber']=float(record[8])
            lista.append(dicti)
        tabla = pd.DataFrame(lista)
        if not tabla.empty:
            tabla['Saldo'] = (tabla['Debe']-tabla['Haber'])
            tabla['Saldo'] = tabla.groupby('Cuenta')['Saldo'].transform(pd.Series.cumsum)
        return tabla


#!/usr/bin/env python

import sys
import os
import unittest
import subprocess
import shutil
import glob

SIIPSIGN = os.path.join('scripts', 'siip_sign.py')

class TestSIIPSign(unittest.TestCase):
    '''A set of integration test cases (for now)'''

    def setUp(self):
        '''Create required files'''
        pass

    def tearDown(self):
        '''Clean up generated files'''

        shutil.rmtree('extract', ignore_errors=True)
        files_to_clean = glob.glob('key*.pem')
        files_to_clean.append('payload.bin')
        files_to_clean.append('signed.bin')
        files_to_clean.append('fkm.bin')

        for f in files_to_clean:
            try:
                os.remove(f)
            except FileNotFoundError:
                pass

    def test_empty_args(self):
        '''Test empty args'''

        cmd = ['python', SIIPSIGN]
        with self.assertRaises(subprocess.CalledProcessError) as cm:
            subprocess.check_call(cmd)
        self.assertEqual(cm.exception.returncode, 2)

    def test_invalid_args(self):
        '''Test invalid args'''

        cmd = ['python', SIIPSIGN, 'build']
        with self.assertRaises(subprocess.CalledProcessError) as cm:
            subprocess.check_call(cmd)
        self.assertEqual(cm.exception.returncode, 2)

        cmd = ['python', SIIPSIGN, 'sign', '-s', 'sha_foo']
        with self.assertRaises(subprocess.CalledProcessError) as cm:
            subprocess.check_call(cmd)
        self.assertEqual(cm.exception.returncode, 2)

    def test_help(self):
        '''Test help message'''

        cmd = ['python', SIIPSIGN, '-h']
        subprocess.check_call(cmd)

    def test_version(self):
        '''Test version info'''
        cmd = ['python', SIIPSIGN, '-V']
        subprocess.check_call(cmd)

    def test_signing_rsa2k(self):
        '''Test signing'''

        hash_options = ['sha256', 'sha384', 'sha512']
        pld_file = 'payload.bin'
        out_file = 'signed.bin'

        with open(pld_file, 'wb') as pld:
            pld.write(os.urandom(1024*1024))

        # Create a new test RSA key
        cmd = ['openssl', 'genrsa', '-out', 'key.pem', '2048']
        subprocess.check_call(cmd)

        # Get public key from test key
        cmd = ['openssl', 'rsa', '-pubout', '-in',
               'key.pem', '-out', 'key.pub.pem']
        subprocess.check_call(cmd)

        for hash_alg in hash_options:
            cmd = ['python', SIIPSIGN, 'sign', '-i', pld_file, '-o', out_file,
                   '-k', 'key.pem', '-s', hash_alg]
            subprocess.check_call(cmd)

            cmd = ['python', SIIPSIGN, 'verify', '-i', out_file,
                   '-p', 'key.pub.pem', '-s', hash_alg]
            subprocess.check_call(cmd)

            cmd = ['python', SIIPSIGN, 'decompose', '-i', out_file]
            subprocess.check_call(cmd)

    def test_signing_rsa3k(self):
        '''Test signing'''

        hash_options = ['sha256', 'sha384', 'sha512']
        pld_file = 'payload.bin'
        out_file = 'signed.bin'

        with open(pld_file, 'wb') as pld:
            pld.write(os.urandom(1024*1024))

        # Create a new test RSA key
        cmd = ['openssl', 'genrsa', '-out', 'key.pem', '3072']
        subprocess.check_call(cmd)

        # Get public key from test key
        cmd = ['openssl', 'rsa', '-pubout', '-in',
               'key.pem', '-out', 'key.pub.pem']
        subprocess.check_call(cmd)

        for hash_alg in hash_options:
            cmd = ['python', SIIPSIGN, 'sign', '-i', pld_file, '-o', out_file,
                   '-k', 'key.pem', '-s', hash_alg]
            subprocess.check_call(cmd)

            cmd = ['python', SIIPSIGN, 'verify', '-i', out_file,
                   '-p', 'key.pub.pem', '-s', hash_alg]
            subprocess.check_call(cmd)

            cmd = ['python', SIIPSIGN, 'decompose', '-i', out_file]
            subprocess.check_call(cmd)

    def test_key_size_too_small(self):
        '''Test with signing key with size smaller than 2048 bit'''

        pld_file = 'payload.bin'
        out_file = 'signed.bin'

        with open(pld_file, 'wb') as pld:
            pld.write(os.urandom(1024*1024))

        # Create a new test RSA key
        cmd = ['openssl', 'genrsa', '-out', 'key.pem', '1024']
        subprocess.check_call(cmd)

        cmd = ['python', SIIPSIGN, 'sign', '-i', pld_file, '-o', out_file,
               '-k', 'key.pem', '-s', 'sha384']
        with self.assertRaises(subprocess.CalledProcessError) as cm:
            subprocess.check_call(cmd)
        self.assertEqual(cm.exception.returncode, 1)

    def test_fkm_subcommand(self):
        '''Test FKM generation and verify subcommand'''

        hash_options = ['sha256', 'sha384', 'sha512']
        out_file = 'fkm_only.bin'
        fkm_key = ('key1.pem', 'key1.pub.pem')
        pld_key = ('key2.pem', 'key2.pub.pem')

        # Create test keys
        for priv, pub in [fkm_key, pld_key]:
            cmd = ['openssl', 'genrsa', '-out', priv, '3072']
            subprocess.check_call(cmd)

            cmd = ['openssl', 'rsa', '-pubout', '-in', priv, '-out', pub]
            subprocess.check_call(cmd)

        for hash_alg in hash_options:
            cmd = ['python', SIIPSIGN, 'fkmgen',
                                       '-k', fkm_key[0],
                                       '-p', pld_key[1],  # Use public key
                                       '-s', hash_alg,
                                       '-o', out_file]
            print(" ".join(cmd))
            subprocess.check_call(cmd)

            cmd = ['python', SIIPSIGN, 'fkmcheck', '-i', out_file,
                                                   '-p', fkm_key[1],
                                                   '-t', pld_key[1]]  # pubkey
            subprocess.check_call(cmd)

            cmd = ['python', SIIPSIGN, 'decompose', '-i', out_file]
            subprocess.check_call(cmd)

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


# *-* encoding: utf-8 *-*
import argparse,json,os,sys,csv,datetime,re

def extractFollowerNetw(filein,fileout):
    counter_sk = 0
    counter_nofr = 0
    counter_nofl = 0
    counter_notw = 0
    counter_nousr = 0
    counter_line = 0
    counter_ok = 0

    writer = csv.writer(fileout,delimiter=',')
    writer.writerow(['ts','uid','friends_count','followers_count'])

    for line in filein.readlines():
        counter_line += 1
        if counter_line % 10000 == 0:
            print(counter_line,'lines treated')
        try:
            tw = json.loads(line)
        except:
            counter_sk += 1
            continue
        if 'twitter' not in tw.keys():
            counter_notw += 1
            continue

        d = [int(i) for i in re.findall(r'[0-9]+',tw['twitter']['created_at'])]
        dt = datetime.datetime(*d[0:6])
        ts = dt.timestamp()
        try:
            uid = int(tw['twitter']['user']['id'])
        except:
            counter_nousr +=1
            continue
        if 'friends_count' not in tw['twitter']['user'].keys():
            nfr = 0
            counter_nofr += 1
        else:
            nfr = int(tw['twitter']['user']['friends_count'])

        if 'followers_count' not in tw['twitter']['user'].keys():
            nfl = 0
        else:
            nfl = tw['twitter']['user']['followers_count']
        counter_ok +=1
        writer.writerow([ts,uid,nfr,nfl])

    print('Stats for file',filein.name)
    print('Total lines:\t\t',counter_line)
    print('Parsed correctly:\t',counter_ok)
    print('No json:\t\t',counter_sk)
    print('No tweet:\t\t',counter_notw)
    print('No users:\t\t',counter_nousr)
    print('No friends:\t\t',counter_nofr)
    print('No followers:\t',counter_nofl)

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-i','--input',
            type=argparse.FileType('r'),
            default=sys.stdin,
            help="Input file, tweets, json format")
    parser.add_argument('-o','--output',
            type=argparse.FileType('w'),
            default=sys.stdout,
            help="Output file, csv, <usr,nbfriends,nbfollowers>")

    args = parser.parse_args()

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)

    extractFollowerNetw(args.input,args.output)

    args.input.close()
    args.output.close()

if __name__ == '__main__':
    main()


import time
from flask import Flask, jsonify
import random
app = Flask(__name__)
list0=['motor0','motor1','motor2','motor3','sensor0','sensor1','sensor2','sensor3']
list1=['motor4','motor5','motor6','sensor4','sensor5','sensor6','sensor7']
def random_s():
    dic = {}
    for i in range(8):
        dic[str(list0[i])] = str(random.randrange(2))
    return dic

def random_n():
    dic = {}
    for i in range(7):
        dic[str(list1[i])] = str(random.randrange(2))
    return dic

@app.route('/s/status/')
def status_s():
    d=random_s()
    return jsonify(d)

@app.route('/n/status/')
def status_n():
    d=random_n()
    return jsonify(d)

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


'''
Created on 23 Dec 2016

@author: chrisdoherty
'''

from Detection import Detection
from threading import Lock

# Manages new detections using the MotionSensor.
class DetectionManager():
    # the current detection
    _detection = None

    # Lock to stop the detection from restarting.
    _lock = Lock()

    # Used when a detection finishes. Should accept 1 argument - the detection.
    new_detection = None

    # Initialises internal variables.
    def __init__(self):
        print("DetectionManager: initialised")

    # Begins a new detection based on the MotionSensor object.
    def start_detection(self):
        print("DetectionManager: start detection")
        self._lock.acquire()
        self._detection = Detection()
        self._detection.start()
        self._detection.stop()

        if self.new_detection != None:
            self.new_detection(self._detection)

        self._detection = None
        self._lock.release()
        print("DetectionManager: start deteciton function finished")

    # Ends a detection and passes it to the specified callback
    def stop_detection(self):
        print("DetectionManager: detection ended")


from lib.constants import Constants
from lib.commonConnection.commonConnection import CommonConnection
from lib.logger.logger import Logger
from lib.helpers.fileHelper import FileHelper
from lib.serverConnection.queueHandler import QueueHandler
import random
import os
import socket

class Connection:
    def startCommunicating(s, fs, sPath, queue, recvMsg, cont, v, q, lr):
        s.settimeout(1.0)
        while True:
            r = random.random()
            try:
                data, addr = s.recvfrom(Constants.bytesChunk())
                if r >= lr:
                    mode = data[0:1]
                    if mode.decode() == 'A':
                        msg = data.decode()
                        qMsg = msg + '-' + str(addr[0]) + '-' + str(addr[1])
                        recvMsg[qMsg] = True
                    Connection.process(s, fs, data, addr, sPath, queue, v, q)
            except socket.timeout:
                close = cont.get('exit', False)
                if close:
                    break
            except Exception:
                break
        return

    def process(s, f, msg, addr, pth, queue, v, q):
        mode = msg[0:1].decode()
        data = msg[1:]
        if mode == Constants.uploadProtocol():
            Connection.startUpload(s, f, data, addr, pth, queue, v, q)
        elif mode == Constants.downloadProtocol():
            Connection.startDownload(s, f, data, addr, pth, queue, v, q)
        elif mode == Constants.errorProtocol():
            Connection.processError(s, f, data, addr, v, q)
        elif mode == Constants.endProtocol():
            Connection.processEnd(s, f, data, addr, v, q)
        elif mode == Constants.ackProtocol():
            Connection.processAck(s, f, data, addr, queue, v, q)
        elif mode == Constants.fileTransferProtocol():
            Connection.processTransfer(s, f, data, addr, queue, v, q)
        return

    def startUpload(s, files, msg, addr, sPath, msgQueue, verbose, quiet):
        message = msg.decode()
        try:
            file = open(sPath+message, "wb")
            Logger.logIfVerbose(verbose, "File " + message + " opened")
            files[message] = file
            CommonConnection.sendACK(s, addr[0], addr[1], 'U', message, 0)
        except OSError:
            Logger.log("Error opening file " + message)
            msg = CommonConnection.sendError(s, message, addr[0], addr[1])
            msgQueue.put(QueueHandler.makeSimpleExpected(msg, addr))
            return
        return

    def processTransfer(s, files, data, addr, msgQueue, v, q):
        values = data[0: Constants.maxHeaderTransProtocolSize() - 1].decode()
        separatorPossition = values.find(';')
        fname = values[0:separatorPossition]
        processedData = values[separatorPossition+1:]
        separatorPossition = processedData.find(';')
        bytesRecv = int(processedData[0:separatorPossition])
        msg = data[Constants.maxHeaderTransProtocolSize() - 1:]
        try:
            f = files[fname]
            Connection.upload(s, f, fname, bytesRecv, msg, addr, v, q)
        except OSError as e:
            print(e)
            Logger.logIfNotQuiet(q, "Error processing transfer")
            msg = CommonConnection.sendError(s, fname, addr[0], addr[1])
            msgQueue.put(QueueHandler.makeSimpleExpected(msg, addr))
            return
        except Exception:
            return

    def startDownload(s, files, data, addr, sPath, msgQueue, v, q):
        filename = data.decode()
        try:
            file = open(sPath+filename, "rb")
            files[filename] = file
            data = file.read(Constants.getMaxReadSize())
            h = addr[0]
            p = addr[1]
            Logger.logIfVerbose(v, filename
                                + " begins to be sent to the client " +
                                str(addr))
            msg = CommonConnection.sendMessage(s, h, p, filename, data, 0)
            msgQueue.put(QueueHandler.makeMessageExpected(msg, addr))
        except Exception:
            Logger.log("Error opening file " + filename)
            msg = CommonConnection.sendError(s, filename, addr[0], addr[1])
            msgQueue.put(QueueHandler.makeSimpleExpected(msg, addr))
            return
        return

    def processError(s, files, data, addr, v, q):
        filename = data.decode()
        try:
            files[filename].close()
            CommonConnection.sendACK(s, addr[0], addr[1], 'F', filename, 0)
        except Exception:
            Logger.log("Error processing error")
            return
        return

    def processEnd(s, files, data, addr, v, q):
        filename = data.decode()
        try:
            files[filename].close()
            CommonConnection.sendACK(s, addr[0], addr[1], 'E', filename, 0)
            Logger.log("File " + filename + " was successfully stored")
        except Exception:
            Logger.log("Error processing end file")
            return
        return

    def processAck(s, files, msg, addr, queue, v, q):
        data = msg.decode()
        md = data[0]
        processedData = data[1:]
        if md == Constants.endProtocol():
            separatorPossition = processedData.find(';')
            fname = processedData[0:separatorPossition]
            Logger.log("File " + fname + " was successfully sent")
            return
        elif md == Constants.errorProtocol():
            return
        elif md == Constants.fileTransferProtocol():
            separatorPossition = processedData.find(';')
            fname = processedData[0:separatorPossition]
            bRecv = int(processedData[separatorPossition+1:])
            f = files[fname]
            return Connection.download(s, f, fname, bRecv, addr, queue, v, q)
        return

    def download(s, f, fname, br, addr, msgQueue, v, q):
        f.seek(br, os.SEEK_SET)
        data = f.read(Constants.getMaxReadSize())
        if len(data) == 0:
            Logger.logIfVerbose(v, "Sending EndFile to client: " + str(addr))
            msg = CommonConnection.sendEndFile(s, addr[0], addr[1], fname, 0)
            msgQueue.put(QueueHandler.makeSimpleExpected(msg, addr))
        else:
            h = addr[0]
            port = addr[1]
            Logger.logIfVerbose(v, "Sending " + str(br)
                                + " bytes to client: " + str(addr))
            msg = CommonConnection.sendMessage(s, h, port, fname, data, br)
            msgQueue.put(QueueHandler.makeMessageExpected(msg, addr))
        return

    def upload(s, f, fname, bytesRecv, msg, addr, v, q):
        filesize = FileHelper.getFileSize(f)
        if bytesRecv == filesize:
            Logger.logIfVerbose(v, "Recieved: " + str(bytesRecv) + " from " +
                                str(addr))
            f.seek(bytesRecv, os.SEEK_SET)
            Logger.logIfVerbose(v, "Writing file " + fname)
            f.write(msg)
            filesize = FileHelper.getFileSize(f)
            CommonConnection.sendACK(s, addr[0], addr[1], 'T', fname, filesize)
        elif bytesRecv < filesize:
            CommonConnection.sendACK(s, addr[0], addr[1], 'T', fname,
                                     bytesRecv + Constants.getMaxReadSize())
        return


# metabolic_robustness/utils/normdist.py

from scipy.stats import truncnorm
from scipy.stats import norm
from math import sqrt

def truncnorm_pdf(x, mean, var, min, max):
    a, b = _truncnorm_params_transform(mean, var, min, max)
    val = truncnorm.pdf(
        x, a, b,
        loc = mean,
        scale = sqrt(var),
        )
    return(val)

def truncnorm_sample(mean, var, min, max):
    a, b = _truncnorm_params_transform(mean, var, min, max)
    val = truncnorm.rvs(
        a, b,
        loc = mean,
        scale = sqrt(var),
        )
    return(val)

def _truncnorm_params_transform(mean, var, min, max):
    a = (min - mean) / sqrt(var)
    b = (max - mean) / sqrt(var)
    return(a, b)

def norm_pdf(x, mean, var):
    val = norm.pdf(x, loc=mean, scale=sqrt(var))
    return(val)

def norm_sample(mean, var):
    val = norm.rvs(
        loc = mean,
        scale = sqrt(var)
        )
    return(val)