import tensorflow as tf

def conv2d(x, input_filters, output_filters, kernel, strides, mode='REFLECT'):
    with tf.variable_scope('conv'):
        shape = [kernel, kernel, input_filters, output_filters]
        weight = tf.Variable(tf.truncated_normal(shape, stddev=0.1), name='weight')
        x_padded = tf.pad(x, [[0, 0], [int(kernel / 2), int(kernel / 2)], [int(kernel / 2), int(kernel / 2)], [0, 0]], mode=mode)
        return tf.nn.conv2d(x_padded, weight, strides=[1, strides, strides, 1], padding='VALID', name='conv')

def conv2d_transpose(x, input_filters, output_filters, kernel, strides):
    with tf.variable_scope('conv_transpose'):
        shape = [kernel, kernel, output_filters, input_filters]
        weight = tf.Variable(tf.truncated_normal(shape, stddev=0.1), name='weight')

        batch_size = tf.shape(x)[0]
        height = tf.shape(x)[1] * strides
        width = tf.shape(x)[2] * strides
        output_shape = tf.stack([batch_size, height, width, output_filters])
        return tf.nn.conv2d_transpose(x, weight, output_shape, strides=[1, strides, strides, 1], name='conv_transpose')

def resize_conv2d(x, input_filters, output_filters, kernel, strides, training):
    '''
    An alternative to transposed convolution where we first resize, then convolve.
    See http://distill.pub/2016/deconv-checkerboard/

    For some reason the shape needs to be statically known for gradient propagation
    through tf.image.resize_images, but we only know that for fixed image size, so we
    plumb through a "training" argument
    '''
    with tf.variable_scope('conv_transpose'):
        height = x.get_shape()[1].value if training else tf.shape(x)[1]
        width = x.get_shape()[2].value if training else tf.shape(x)[2]

        new_height = height * strides * 2
        new_width = width * strides * 2

        x_resized = tf.image.resize_images(x, [new_height, new_width], tf.image.ResizeMethod.NEAREST_NEIGHBOR)

        # shape = [kernel, kernel, input_filters, output_filters]
        # weight = tf.Variable(tf.truncated_normal(shape, stddev=0.1), name='weight')
        return conv2d(x_resized, input_filters, output_filters, kernel, strides)

def instance_norm(x):
    epsilon = 1e-9

    mean, var = tf.nn.moments(x, [1, 2], keep_dims=True)

    return tf.div(tf.subtract(x, mean), tf.sqrt(tf.add(var, epsilon)))

def batch_norm(x, size, training, decay=0.999):
    beta = tf.Variable(tf.zeros([size]), name='beta')
    scale = tf.Variable(tf.ones([size]), name='scale')
    pop_mean = tf.Variable(tf.zeros([size]))
    pop_var = tf.Variable(tf.ones([size]))
    epsilon = 1e-3

    batch_mean, batch_var = tf.nn.moments(x, [0, 1, 2])
    train_mean = tf.assign(pop_mean, pop_mean * decay + batch_mean * (1 - decay))
    train_var = tf.assign(pop_var, pop_var * decay + batch_var * (1 - decay))

    def batch_statistics():
        with tf.control_dependencies([train_mean, train_var]):
            return tf.nn.batch_normalization(x, batch_mean, batch_var, beta, scale, epsilon, name='batch_norm')

    def population_statistics():
        return tf.nn.batch_normalization(x, pop_mean, pop_var, beta, scale, epsilon, name='batch_norm')

    return tf.cond(training, batch_statistics, population_statistics)

def conditional_instance_norm(x, style_control=None):
    with tf.variable_scope('con_instance_norm'):
        batch, rows, cols, channels = [i.value for i in x.get_shape()]
        mean, var = tf.nn.moments(x, [1, 2], keep_dims=True)

        var_shape = [channels]
        # norm * gamma + beta -> z
        gamma = []
        beta = []

        for i in range(style_control.shape[0]):
            with tf.variable_scope('{0}'.format(i) + '_style'):
                beta.append(tf.get_variable('beta', shape=var_shape, initializer=tf.constant_initializer(0.)))
                gamma.append(tf.get_variable('gamma', shape=var_shape, initializer=tf.constant_initializer(1.)))
                tf.summary.histogram('beta', beta)
                tf.summary.histogram('gamma', gamma)
        gamma = tf.convert_to_tensor(gamma)
        beta = tf.convert_to_tensor(beta)
        epsilon = 1e-3
        normalized = tf.div(tf.subtract(x, mean), tf.sqrt(tf.add(var, epsilon)))

        # 得到选择style的index
        index = tf.where(tf.not_equal(style_control, tf.constant(0, dtype=tf.float32)))
        # style_control对映index的值
        style_value = tf.gather(style_control, index)
        # shift,scale 对映index的值
        beta_value = tf.gather(beta, index)
        gamma_value = tf.gather(gamma, index)
        # style_control的value和
        style_sum = tf.reduce_sum(style_control)
        #
        style_beta = tf.reduce_sum(beta_value * style_value, axis=0) / style_sum
        style_gamma = tf.reduce_sum(gamma_value * style_value, axis=0) / style_sum
        output = style_gamma * normalized + style_beta

        # for i, x in enumerate(style_control):
        #     if not x == 0:
        #         index = [i]
        # style_scale = reduce(tf.add, [scale[i]*style_control[i] for i in index]) / sum(style_control)
        # style_shift = reduce(tf.add, [shift[i]*style_control[i] for i in index]) / sum(style_control)
        # output = style_scale * normalized + style_shift
    return output

def relu(input):
    relu = tf.nn.relu(input)
    # convert nan to zero (nan != nan)
    nan_to_zero = tf.where(tf.equal(relu, relu), relu, tf.zeros_like(relu))
    return nan_to_zero

def residual(x, filters, kernel, strides, style_control):
    with tf.variable_scope('residual'):
        conv1 = conditional_instance_norm(conv2d(x, filters, filters, kernel, strides), style_control)
        conv2 = conditional_instance_norm(conv2d(relu(conv1), filters, filters, kernel, strides), style_control)
        residual = x + conv2
        return residual

def net(image, training, style_control):
    # Less border effects when padding a little before passing through ..
    image = tf.pad(image, [[0, 0], [10, 10], [10, 10], [0, 0]], mode='REFLECT')

    with tf.variable_scope('conv1'):
        conv1 = relu(conditional_instance_norm(conv2d(image, 3, 32, 9, 1), style_control))
    with tf.variable_scope('conv2'):
        conv2 = relu(conditional_instance_norm(conv2d(conv1, 32, 64, 3, 2), style_control))
    with tf.variable_scope('conv3'):
        conv3 = relu(conditional_instance_norm(conv2d(conv2, 64, 128, 3, 2), style_control))
    with tf.variable_scope('res1'):
        res1 = residual(conv3, 128, 3, 1, style_control)
    with tf.variable_scope('res2'):
        res2 = residual(res1, 128, 3, 1, style_control)
    with tf.variable_scope('res3'):
        res3 = residual(res2, 128, 3, 1, style_control)
    with tf.variable_scope('res4'):
        res4 = residual(res3, 128, 3, 1, style_control)
    with tf.variable_scope('res5'):
        res5 = residual(res4, 128, 3, 1, style_control)
    # print(res5.get_shape())
    with tf.variable_scope('deconv1'):
        # deconv1 = relu(instance_norm(conv2d_transpose(res5, 128, 64, 3, 2)))
        deconv1 = relu(conditional_instance_norm(resize_conv2d(res5, 128, 64, 3, 2, training), style_control))
    with tf.variable_scope('deconv2'):
        # deconv2 = relu(instance_norm(conv2d_transpose(deconv1, 64, 32, 3, 2)))
        deconv2 = relu(conditional_instance_norm(resize_conv2d(deconv1, 64, 32, 3, 2, training), style_control))
    with tf.variable_scope('deconv3'):
        # deconv_test = relu(instance_norm(conv2d(deconv2, 32, 32, 2, 1)))
        deconv3 = tf.nn.tanh(conditional_instance_norm(conv2d(deconv2, 32, 3, 9, 1), style_control))

    y = (deconv3 + 1) * 127.5

    # Remove border effect reducing padding.
    height = tf.shape(y)[1]
    width = tf.shape(y)[2]
    y = tf.slice(y, [0, 10, 10, 0], tf.stack([-1, height - 20, width - 20, -1]))

    return y


import socket

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:

    try:
        client.connect(("127.0.0.1", 1337))

        while True:
            datas = client.recv(2048).decode()
            print(f"Servidor: {datas}")

            dat = input("Enviar: ")
            if dat == "sair":
                break

            client.sendall(dat.encode())

    except Exception as msg:
        print(msg)

    finally:
        client.close()


#modcom.co.ke/datascience
#modcom.co.ke/flask
#modcom.co.ke/flask/datascience/banks
#modcom.co.ke/bank
import pandas as pd

df = pd.read_csv("school.csv") #df is the dataframe
# print(df)
# print(df["StudyTime"])
print(df[["StudyTime", "Gender"]])
#narrow down
subset = df[["StudyTime", "Gender"]] #example of extracting different columns in a dataframe , maybe for plotting.
#below code shows rows and columns
print(df.shape)
#below checks and maps empty collumns
# print(df.isnull().sum())
# df.dropna(inplace = True ) #removing empties
# print(df.isnull().sum())
# print(df.shape)

#imputation, filling the empties,
df["Gender"].fillna(2, inplace=True)
df["Athlete"].fillna(2, inplace=True)
df["Smoking"].fillna(3, inplace=True)
df["Rank"].fillna(5, inplace=True)

#lets do for math
medianmath = df["Math"].median()
df["Math"].fillna(medianmath, inplace=True)

mediansprint = df["Sprint"].median()
df["Sprint"].fillna(mediansprint, inplace=True)

medianreading = df["Reading"].median()
df["Reading"].fillna(medianreading, inplace=True)

mediansleeptime = df["SleepTime"].median()
df["SleepTime"].fillna(mediansleeptime, inplace=True)

medianstudytime = df["StudyTime"].median()
df["StudyTime"].fillna(medianstudytime, inplace=True)

medianwriting = df["Writing"].median()
df["Writing"].fillna(medianwriting, inplace=True)

print(df.isnull().sum())

#===========PLOTTING===========

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.scatter(df["Math"], df["Reading"], s = 30, color = "red", alpha=0.75)
ax.set_xlabel("Mathematics")
ax.set_ylabel("Reading")
ax.set_title("Distribution of students mathematics by reading scores")
plt.show()

print(df["Reading"].describe())
print(df["Math"].describe())
fig, ax = plt.subplots()
ax.scatter(df["SleepTime"], df["StudyTime"], s = 30, color = "green", alpha=0.75)
ax.set_xlabel("Sleeping Time")
ax.set_ylabel("Study Time")
ax.set_title("Distribution of students Sleeping Time by Study Time scores")
plt.show()

#Histogram
_, ax = plt.subplots()
ax.hist(df['Sprint'], color='#539caf', bins=30)#bins are not a must because the
 # Label the axes and provide a title
ax.set_title("Distribution of Students Sprint Time")
ax.set_xlabel("Sprint")
ax.set_ylabel("Frequency")
plt.show()

fig, ax = plt.subplots()
ax.hist(df['Math'], color='#539caf', bins=30)#bins are not a must because the
 # Label the axes and provide a title
ax.set_title("Distribution of Students Math Scores")
ax.set_xlabel("Mathematics")
ax.set_ylabel("Frequency")
plt.show()

#Density
fig,  ax = plt.subplots()
ax.plot(df['Weight'], color='#539caf', lw=2)
ax.set_ylabel("Frequency")
ax.set_xlabel("sprint")
ax.set_title("Weight Distribution")
ax.legend(loc='best')
plt.show()

#PieChart
df["Rank"].replace(1, "Freshmen", inplace=True)
df["Rank"].replace(2, "Sophomore", inplace=True)
df["Rank"].replace(3, "Junior", inplace=True)
df["Rank"].replace(4, "Senior", inplace=True)
fig, ax = plt.subplots()
df.groupby('Rank').size().plot(kind='pie',autopct='%1.1f%%')
ax.set_xlabel("")
ax.set_ylabel("")
ax.set_title("Distribution by Rank in %")
plt.show()

#Bar Chart
fig, ax = plt.subplots()
df.groupby('Gender')['StudyTime'].mean().plot(kind='bar', color =
"#ffcccc")
ax.set_ylabel("StudyTime")
ax.set_xlabel("Gender")
ax.set_title("studyTime Distribution by Gender")
ax.legend(loc='best')
plt.show()

#stacd bar
fig,  ax = plt.subplots()
df.groupby(['Gender', 'Rank'])['Math'].mean().unstack().plot(kind='bar',
stacked=False)
ax.set_ylabel("Math")
ax.set_xlabel("Gender")
ax.set_title("Math Distribution by Gender and Rank")
plt.show()

#Test of hypothesis
#one sample t test
#two sample t test
#Angus
#chi square
#https://modcom.co.ke/datascience
#https://modcom.co.ke/flask/DataScience/banks.csv


from django.db import models

# Create your models here.
class StatusCrm(models.Model):
    Status_name = models.CharField(max_length=200, verbose_name='Название статуса')

    def __str__(self):
        return self.Status_name

    class Meta:
        verbose_name = 'статус'
        verbose_name_plural = 'статусы'

class Enrollment(models.Model):
    enrollment_dt = models.DateTimeField(auto_now=True)
    enrollment_name = models.CharField(max_length=200, verbose_name='ФИО заказчика', default="")
    enrollment_kname = models.CharField(max_length=200, verbose_name='Имя учащегося', default="")
    enrollment_age = models.CharField(max_length=2, verbose_name='Возраст учащегося', default="")
    enrollment_phone = models.CharField(max_length=20, verbose_name='Телефон', default="")
    enrollment_section = models.CharField(max_length=100, verbose_name='Кружок', default="")
    enrollment_status = models.ForeignKey(StatusCrm, on_delete=models.PROTECT, null=True, blank=True, verbose_name='Статус')

    def __str__(self):
        return self.enrollment_name

    class Meta:
        verbose_name = 'запись'
        verbose_name_plural = 'запси'

class CommentCrm(models.Model):
    comment_binding = models.ForeignKey(Enrollment, on_delete=models.CASCADE, verbose_name='Заявка')
    comment_text = models.TextField(verbose_name='Текст комментария')
    comment_dt = models.DateTimeField(auto_now=True, verbose_name='Дата создания')

    def __str__(self):
        return self.comment_text

    class Meta:
        verbose_name = 'комментарий'
        verbose_name_plural = 'комментарии'


from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
from meals.models import Meals
from order.models import orderDetail

# Create your views here.

@login_required
def add_user_order(request):
    count_product = int(request.POST.get("count"))
    product_ID = request.POST.get("product_id")
    product = Meals.objects.filter(id=product_ID).first()
    orderDetail.objects.create(name=product.slug, price=product.price, count=count_product)
    return redirect("/meals")

def order_list(request):
    count = 0
    for order in orderDetail.objects.all():
        count += (int(float(order.price)) * int(float(order.count)))

    context= {
        "orderlist" : orderDetail.objects.all(),
        "all_food":count
    }
    return render(request, "orderlist.html", context)


from django.conf.urls import url

from rotations import views

api_urls = (
    (r'rotations', views.RotationViewSet),
    (r'requested_departments', views.RequestedDepartmentViewSet),
    (r'rotation_requests', views.RotationRequestViewSet),
    (r'rotation_requests/(?P<department_id>\d+)/(?P<month_id>\d+)', views.RotationRequestByDepartmentAndMonth, 'rotationrequest-by-d-and-m'),
    (r'rotation_request_responses', views.RotationRequestResponseViewSet),
    (r'rotation_request_forwards', views.RotationRequestForwardViewSet),
    (r'acceptance_lists', views.AcceptanceListViewSet, 'acceptancelist'),
)


from deploystream import app
from deploystream.apps.feature.lib import get_feature_info, get_all_features
from deploystream.lib.transforms import as_json
from deploystream.decorators import needs_providers

@app.route('/features', methods=['GET'])
@needs_providers
@as_json
def list_features(providers):
    features = get_all_features(providers)
    return features

@app.route('/features/<feature_id>', methods=['GET'])
@needs_providers
@as_json
def view_feature(feature_id, providers):
    feature = get_feature_info(feature_id, providers)
    return feature


# var.py
#
# Read the var files.
# NB: the f array returned is C-ordered: f[nvar, nz, ny, nx]
#     NOT Fortran as in Pencil (& IDL):  f[nx, ny, nz, nvar]
#
# Authors:
# J. Oishi (joishi@amnh.org)
# T. Gastine (tgastine@ast.obs-mip.fr)
# S. Candelaresi (iomsn1@gmail.com).
"""
Contains the read class for the VAR file reading,
some simulation attributes and the data cube.
"""

def var(*args, **kwargs):
    """
    Read VAR files from Pencil Code. If proc < 0, then load all data
    and assemble, otherwise load VAR file from specified processor.

    The file format written by output() (and used, e.g. in var.dat)
    consists of the followinig Fortran records:
    1. data(mx, my, mz, nvar)
    2. t(1), x(mx), y(my), z(mz), dx(1), dy(1), dz(1), deltay(1)
    Here nvar denotes the number of slots, i.e. 1 for one scalar field, 3
    for one vector field, 8 for var.dat in the case of MHD with entropy.
    but, deltay(1) is only there if lshear is on! need to know parameters.

    call signature:

    var(var_file='', datadir='data', proc=-1, ivar=-1, quiet=True,
        trimall=False, magic=None, sim=None, precision='f')

    Keyword arguments:
        var_file:   Name of the VAR file.
        datadir:    Directory where the data is stored.
        proc:       Processor to be read. If -1 read all and assemble to one array.
        ivar:       Index of the VAR file, if var_file is not specified.
        quiet:      Flag for switching off output.
        trimall:    Trim the data cube to exclude ghost zones.
        magic:      Values to be computed from the data, e.g. B = curl(A).
        sim:        Simulation sim object.
        precision:  Float (f) or double (d).
    """

    from ..sim import __Simulation__

    started = None

    for a in args:
        if isinstance(a, __Simulation__):
            started = a.started()
            break

    if 'sim' in kwargs.keys():
        started = kwargs['sim'].started()
    elif 'datadir' in kwargs.keys():
        from os.path import join, exists
        if exists(join(kwargs['datadir'], 'time_series.dat')):
            started = True
    else:
        from os.path import join, exists
        if exists(join('data', 'time_series.dat')):
            started = True

        #else:
        #    print('!! ERROR: No simulation of path specified..')

    if not started:
        print('ERROR: Simulation has not jet started. There are no var files.')
        return False

    var_tmp = DataCube()
    var_tmp.read(*args, **kwargs)
    return var_tmp

class DataCube(object):
    """
    DataCube -- holds Pencil Code VAR file data.
    """

    def __init__(self):
        """
        Fill members with default values.
        """

        self.t = 0.
        self.dx = 1.
        self.dy = 1.
        self.dz = 1.

    def read(self, var_file='', datadir='data', proc=-1, ivar=-1, quiet=True,
             trimall=False, magic=None, sim=None, precision='f'):
        """
        Read VAR files from Pencil Code. If proc < 0, then load all data
        and assemble, otherwise load VAR file from specified processor.

        The file format written by output() (and used, e.g. in var.dat)
        consists of the followinig Fortran records:
        1. data(mx, my, mz, nvar)
        2. t(1), x(mx), y(my), z(mz), dx(1), dy(1), dz(1), deltay(1)
        Here nvar denotes the number of slots, i.e. 1 for one scalar field, 3
        for one vector field, 8 for var.dat in the case of MHD with entropy.
        but, deltay(1) is only there if lshear is on! need to know parameters.

        call signature:

        var(var_file='', datadir='data', proc=-1, ivar=-1, quiet=True,
            trimall=False, magic=None, sim=None, precision='f')

        Keyword arguments:
            var_file:   Name of the VAR file.
            datadir:    Directory where the data is stored.
            proc:       Processor to be read. If -1 read all and assemble to one array.
            ivar:       Index of the VAR file, if var_file is not specified.
            quiet:      Flag for switching off output.
            trimall:    Trim the data cube to exclude ghost zones.
            magic:      Values to be computed from the data, e.g. B = curl(A).
            sim:        Simulation sim object.
            precision:  Float (f) or double (d).
        """

        import numpy as np
        import os
        from scipy.io import FortranFile
        from ..math.derivatives import curl, curl2
        from .. import read
        from ..sim import __Simulation__

        dim = None
        param = None
        index = None

        if isinstance(sim, __Simulation__):
            datadir = os.path.expanduser(sim.datadir)
            dim = sim.dim
            param = read.param(datadir=sim.datadir, quiet=True)
            index = read.index(datadir=sim.datadir)
        else:
            datadir = os.path.expanduser(datadir)
            if dim is None:
                if var_file[0:2].lower() == 'og':
                    dim = read.ogdim(datadir, proc)
                else:
                    dim = read.dim(datadir, proc)
            if param is None:
                param = read.param(datadir=datadir, quiet=quiet)
            if index is None:
                index = read.index(datadir=datadir)

        run2D = param.lwrite_2d

        if dim.precision == 'D':
            precision = 'd'
        else:
            precision = 'f'

        if param.lwrite_aux:
            total_vars = dim.mvar + dim.maux
        else:
            total_vars = dim.mvar

        if not var_file:
            if ivar < 0:
                var_file = 'var.dat'
            else:
                var_file = 'VAR' + str(ivar)

        if proc < 0:
            proc_dirs = self.__natural_sort(filter(lambda s: s.startswith('proc'),
                                                   os.listdir(datadir)))
        else:
            proc_dirs = ['proc' + str(proc)]

        # Set up the global array.
        if not run2D:
            f = np.zeros((total_vars, dim.mz, dim.my, dim.mx),
                         dtype=precision)
        else:
            if dim.ny == 1:
                f = np.zeros((total_vars, dim.mz, dim.mx), dtype=precision)
            else:
                f = np.zeros((total_vars, dim.my, dim.mx), dtype=precision)

        x = np.zeros(dim.mx, dtype=precision)
        y = np.zeros(dim.my, dtype=precision)
        z = np.zeros(dim.mz, dtype=precision)

        for directory in proc_dirs:
            proc = int(directory[4:])
            if var_file[0:2].lower() == 'og':
                procdim = read.ogdim(datadir, proc)
            else:
                procdim = read.dim(datadir, proc)
            if not quiet:
                print("Reading data from processor {0} of {1} ...".format( \
                      proc, len(proc_dirs)))

            mxloc = procdim.mx
            myloc = procdim.my
            mzloc = procdim.mz

            # Read the data.
            file_name = os.path.join(datadir, directory, var_file)
            infile = FortranFile(file_name)
            if not run2D:
                f_loc = infile.read_record(dtype=precision)
                f_loc = f_loc.reshape((-1, mzloc, myloc, mxloc))
            else:
                if dim.ny == 1:
                    f_loc = infile.read_record(dtype=precision)
                    f_loc = f_loc.reshape((-1, mzloc, mxloc))
                else:
                    f_loc = infile.read_record(dtype=precision)
                    f_loc = f_loc.reshape((-1, myloc, mxloc))
            raw_etc = infile.read_record(precision)
            infile.close()

            t = raw_etc[0]
            x_loc = raw_etc[1:mxloc+1]
            y_loc = raw_etc[mxloc+1:mxloc+myloc+1]
            z_loc = raw_etc[mxloc+myloc+1:mxloc+myloc+mzloc+1]
            if param.lshear:
                shear_offset = 1
                deltay = raw_etc[-1]
            else:
                shear_offset = 0

            dx = raw_etc[-3-shear_offset]
            dy = raw_etc[-2-shear_offset]
            dz = raw_etc[-1-shear_offset]

            if len(proc_dirs) > 1:
                # Calculate where the local processor will go in
                # the global array.
                #
                # Don't overwrite ghost zones of processor to the left (and
                # accordingly in y and z direction -- makes a difference on the
                # diagonals)
                #
                # Recall that in NumPy, slicing is NON-INCLUSIVE on the right end
                # ie, x[0:4] will slice all of a 4-digit array, not produce
                # an error like in idl.

                if procdim.ipx == 0:
                    i0x = 0
                    i1x = i0x + procdim.mx
                    i0xloc = 0
                    i1xloc = procdim.mx
                else:
                    i0x = procdim.ipx*procdim.nx + procdim.nghostx
                    i1x = i0x + procdim.mx - procdim.nghostx
                    i0xloc = procdim.nghostx
                    i1xloc = procdim.mx

                if procdim.ipy == 0:
                    i0y = 0
                    i1y = i0y + procdim.my
                    i0yloc = 0
                    i1yloc = procdim.my
                else:
                    i0y = procdim.ipy*procdim.ny + procdim.nghosty
                    i1y = i0y + procdim.my - procdim.nghosty
                    i0yloc = procdim.nghosty
                    i1yloc = procdim.my

                if procdim.ipz == 0:
                    i0z = 0
                    i1z = i0z+procdim.mz
                    i0zloc = 0
                    i1zloc = procdim.mz
                else:
                    i0z = procdim.ipz*procdim.nz + procdim.nghostz
                    i1z = i0z + procdim.mz - procdim.nghostz
                    i0zloc = procdim.nghostz
                    i1zloc = procdim.mz

                x[i0x:i1x] = x_loc[i0xloc:i1xloc]
                y[i0y:i1y] = y_loc[i0yloc:i1yloc]
                z[i0z:i1z] = z_loc[i0zloc:i1zloc]

                if not run2D:
                    f[:, i0z:i1z, i0y:i1y, i0x:i1x] = \
                        f_loc[:, i0zloc:i1zloc, i0yloc:i1yloc, i0xloc:i1xloc]
                else:
                    if dim.ny == 1:
                        f[:, i0z:i1z, i0x:i1x] = \
                            f_loc[:, i0zloc:i1zloc, i0xloc:i1xloc]
                    else:
                        f[:, i0y:i1y, i0x:i1x] = \
                            f_loc[:, i0yloc:i1yloc, i0xloc:i1xloc]
            else:
                f = f_loc
                x = x_loc
                y = y_loc
                z = z_loc

        if magic is not None:
            if 'bb' in magic:
                # Compute the magnetic field before doing trimall.
                aa = f[index.ax-1:index.az, ...]
                # TODO: Specify coordinate system.
                self.bb = curl(aa, dx, dy, dz, run2D=run2D)
                if trimall:
                    self.bb = self.bb[:, dim.n1:dim.n2+1,
                                      dim.m1:dim.m2+1, dim.l1:dim.l2+1]
            if 'jj' in magic:
                # Compute the electric current field before doing trimall.
                aa = f[index.ax-1:index.az, ...]
                # TODO: Specify coordinate system.
                self.jj = curl2(aa, dx, dy, dz)
                if trimall:
                    self.jj = self.jj[:, dim.n1:dim.n2+1,
                                      dim.m1:dim.m2+1, dim.l1:dim.l2+1]
            if 'vort' in magic:
                # Compute the vorticity field before doing trimall.
                uu = f[index.ux-1:index.uz, ...]
                # TODO: Specify coordinate system.
                # WL: The curl subroutine should take care of it.
                self.vort = curl(uu, dx, dy, dz, run2D=run2D)
                if trimall:
                    if run2D:
                        if dim.nz == 1:
                            self.vort = self.vort[:, dim.m1:dim.m2+1,
                                                  dim.l1:dim.l2+1]
                        else:
                            self.vort = self.vort[:, dim.n1:dim.n2+1,
                                                  dim.l1:dim.l2+1]
                    else:
                        self.vort = self.vort[:, dim.n1:dim.n2+1,
                                              dim.m1:dim.m2+1,
                                              dim.l1:dim.l2+1]

        # Trim the ghost zones of the global f-array if asked.
        if trimall:
            self.x = x[dim.l1:dim.l2+1]
            self.y = y[dim.m1:dim.m2+1]
            self.z = z[dim.n1:dim.n2+1]
            if not run2D:
                self.f = f[:, dim.n1:dim.n2+1, dim.m1:dim.m2+1, dim.l1:dim.l2+1]
            else:
                if dim.ny == 1:
                    self.f = f[:, dim.n1:dim.n2+1, dim.l1:dim.l2+1]
                else:
                    self.f = f[:, dim.m1:dim.m2+1, dim.l1:dim.l2+1]
        else:
            self.x = x
            self.y = y
            self.z = z
            self.f = f
            self.l1 = dim.l1
            self.l2 = dim.l2 + 1
            self.m1 = dim.m1
            self.m2 = dim.m2 + 1
            self.n1 = dim.n1
            self.n2 = dim.n2 + 1

        # Assign an attribute to self for each variable defined in
        # 'data/index.pro' so that e.g. self.ux is the x-velocity
        for key in index.__dict__.keys():
            if key != 'global_gg' and key != 'keys':
                value = index.__dict__[key]
                setattr(self, key, self.f[value-1, ...])
        # Special treatment for vector quantities.
        if hasattr(index, 'uu'):
            self.uu = self.f[index.ux-1:index.uz, ...]
        if hasattr(index, 'aa'):
            self.aa = self.f[index.ax-1:index.az, ...]

        self.t = t
        self.dx = dx
        self.dy = dy
        self.dz = dz
        if param.lshear:
            self.deltay = deltay

        # Do the rest of magic after the trimall (i.e. no additional curl...).
        self.magic = magic
        if self.magic is not None:
            self.magic_attributes(param)

    def __natural_sort(self, procs_list):
        """
        Sort array in a more natural way, e.g. 9VAR < 10VAR
        """

        import re

        convert = lambda text: int(text) if text.isdigit() else text.lower()
        alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
        return sorted(procs_list, key=alphanum_key)

    def magic_attributes(self, param):
        """
        Compute some additional 'magic' quantities.
        """

        import numpy as np
        import sys

        for field in self.magic:
            if field == 'rho' and not hasattr(self, 'rho'):
                if hasattr(self, 'lnrho'):
                    setattr(self, 'rho', np.exp(self.lnrho))
                else:
                    sys.exit("pb in magic!")

            if field == 'tt' and not hasattr(self, 'tt'):
                if hasattr(self, 'lnTT'):
                    tt = np.exp(self.lnTT)
                    setattr(self, 'tt', tt)
                else:
                    if hasattr(self, 'ss'):
                        if hasattr(self, 'lnrho'):
                            lnrho = self.lnrho
                        elif hasattr(self, 'rho'):
                            lnrho = np.log(self.rho)
                        else:
                            sys.exit("pb in magic: missing rho or lnrho variable")
                        cp = param.cp
                        gamma = param.gamma
                        cs20 = param.cs0**2
                        lnrho0 = np.log(param.rho0)
                        lnTT0 = np.log(cs20/(cp*(gamma-1.)))
                        lnTT = lnTT0+gamma/cp*self.ss+(gamma-1.)* \
                               (lnrho-lnrho0)
                        setattr(self, 'tt', np.exp(lnTT))
                    else:
                        sys.exit("pb in magic!")

            if field == 'ss' and not hasattr(self, 'ss'):
                cp = param.cp
                gamma = param.gamma
                cs20 = param.cs0**2
                lnrho0 = np.log(param.rho0)
                lnTT0 = np.log(cs20/(cp*(gamma-1.)))
                if hasattr(self, 'lnTT'):
                    setattr(self, 'ss', cp/gamma*(self.lnTT-lnTT0- \
                            (gamma-1.)*(self.lnrho-lnrho0)))
                elif hasattr(self, 'tt'):
                    setattr(self, 'ss', cp/gamma*(np.log(self.tt)- \
                            lnTT0-(gamma-1.)*(self.lnrho-lnrho0)))
                else:
                    sys.exit("pb in magic!")

            if field == 'pp' and not hasattr(self, 'pp'):
                cp = param.cp
                gamma = param.gamma
                cv = cp/gamma
                if hasattr(self, 'lnrho'):
                    lnrho = self.lnrho
                elif hasattr(self, 'rho'):
                    lnrho = np.log(self.rho)
                else:
                    sys.exit("pb in magic: missing rho or lnrho variable")
                if hasattr(self, 'ss'):
                    setattr(self, 'pp', np.exp(gamma*(self.ss+lnrho)))
                elif hasattr(self, 'lntt'):
                    setattr(self, 'pp', (cp-cv)*np.exp(self.lntt+lnrho))
                elif hasattr(self, 'tt'):
                    setattr(self, 'pp', (cp-cv)*self.tt*np.exp(lnrho))
                else:
                    sys.exit("pb in magic!")


import sys

def lengthen(line):
    c = 0
    esc = ''
    for c in line:
        if c == '"':
            esc += '\\"'
        elif c == '\\':
            esc += '\\\\'
        else:
            esc += c
    return '"%s"' % (esc, )

def main():
    with open(sys.argv[1]) as f:
        lines = [x.strip() for x in f.readlines()]

    total_regular = 0
    total_long = 0

    for l in lines:
        lengthened = lengthen(l)
        print('%s : %s (%d => %d)' % (l, lengthened, len(l), len(lengthened)))
        total_regular += len(l)
        total_long += len(lengthened)

    print ('Totals: (%d - %d): %d' % (total_long, total_regular, (total_long - total_regular)))

if __name__ == '__main__':
    main()


#!/usr/bin/python3

########################################################################
# This file is part of the Honeyris project made by the Astar Company: #
# https://github.com/astar-security/Honeyris                           #
# The project is published under GPLv3 license                         #
# Author: David Soria (@Sibwara)                                       #
########################################################################

import pyshark
import argparse
import logging
import logging.handlers
import signal
from scapy.all import *

blacklist = None
log = None

##################
# Whitelist part #
##################

# Get the local IPv4 addresses which will be whitelisted
def getHoneyIPAddresses(iface):
    global log
    ips = set()
    try:
        ips.add("127.0.0.1")
        ips.add(get_if_addr(iface))
        return ips
    except Exception as e:
        log.error(f"Error during local IP collection: {e}")
        return ips

# Get the local GW which will be whitelisted
def getHoneyGateway(iface):
    global log
    gws, macs = set(), set()
    try:
        # get default gw for chosen iface
        res = conf.route.route()
        if res[0] == iface :
            gws.add(res[2])
        # get the associated known MAC address
        arp = open("/proc/net/arp","r")
        table = arp.read()
        for gw in gws :
            mac = table.split(gw)[1].split("\n")[0].split()[2]
            macs.add(mac)
        return gws, macs
    except Exception as e:
        log.error(f"Error during gateway collection: {e}")
        return gws, macs

# As the DNS will be involved for the system update, it must be whitelisted
def getHoneyDNS():
    global log
    nss = set()
    try:
        # Parsing the content of "/etc/resolv.conf"
        resol = open("/etc/resolv.conf", "r")
        dns = resol.read().split("nameserver ")[1:]
        resol.close()
        for ns in dns:
            nss.add(ns.split("\n")[0])
        return nss
    except Exception as e:
        log.error(f"Error during DNS collection: {e}")
        return nss

# As the DHCP server will be joined to update leases, it must be whitelisted
def getHoneyDHCP(iface):
    global log
    dhcp = set()
    try:
        # make a DHCP discover request to identify the DHCP server
        conf.checkIPaddr=False
        localmac = get_if_hwaddr(iface)
        localmacraw = get_if_raw_hwaddr(iface)[1]
        dhcp_discover = (Ether(src=localmac, dst='ff:ff:ff:ff:ff:ff') /
            IP(src='0.0.0.0', dst='255.255.255.255') /
            UDP(dport=67, sport=68) /
            BOOTP(chaddr=localmacraw,xid=5555) /
            DHCP(options=[('message-type', 'discover'), 'end']))
        dhcp_offer = srp1(dhcp_discover,iface=iface, verbose=0, timeout=10)
        dhcp.add(dhcp_offer['IP'].src)
        return dhcp
    except Exception as e:
        log.error(f"Error during DHCP collection: {e}")
        return dhcp

# As the honeyris server will join targets to update, they must be whitelisted
#def getHoneyUpdater():

####################
# Prepare the meal #
####################

def populate(iface):
    global log
    global blacklist

    # call whitelist functions
    ips = getHoneyIPAddresses(iface)
    gws, macs = getHoneyGateway(iface)
    nss = getHoneyDNS()
    dhcp = getHoneyDHCP(iface)

    # aggregate and set the blacklist
    whitelist = ips.union(gws.union(nss.union(dhcp)))
    blacklist = set()

    # provide information about honeyris
    info = "Information about Honeyris-"\
        f"Interface:{iface}-IP:{ips}-Gateway:{gws}{macs}-NS:{nss}-DHCP:{dhcp}"
    log.info(info)

    return ips,gws,macs,nss,dhcp,whitelist

def setLog(siem):
    global log

    # default Syslog port
    port =  514

    # Set the command line logger
    log = logging.getLogger('Honeyris')
    log.setLevel(logging.INFO)
    formatter = logging.Formatter('%(name)s--%(levelname)s--%(asctime)s--%(message)s')
    console = logging.StreamHandler()
    console.setFormatter(formatter)
    log.addHandler(console)

    try:
        # check if a specific port is provided
        if ":" in siem:
            siem, port = siem.split(":")
            port = int(port)
        # set Syslog handler
        syslog = logging.handlers.SysLogHandler(address=(siem, port))
        syslog.setFormatter(formatter)
        log.addHandler(syslog)
        log.info("Logging ready")
        return 0
    except Exception as e:
        log.error(f"Error during logging initialization: {e}")
        return 1

# Clean exit after CTRL-C
def ctrlCHandler(signum, frame):
    global log
    global blacklist
    log.info(f"quitting...")
    log.info(f"Blacklisted targets were: {blacklist}")

# send CTRL-C signal to ctrlCHandler
signal.signal(signal.SIGINT, ctrlCHandler)

####################
# Serve some honey #
####################

def blacklistIP(iface, IP, ARPPing, ARPSpoof, verbose, whitelist, ips, gws, macs):
    global blacklist
    global log

    log.info("Logging initiated")
    capture = pyshark.LiveCapture(interface=iface)
    for packet in capture.sniff_continuously():
        try:
            # if an IP request does not come from a trusted IP
            if (IP and 'IP' in packet and packet['ip'].dst in ips and
                    packet['ip'].src not in whitelist):
                blacklist.add(packet['ip'].src)
                log.warning(f"{packet['ip'].src}--IP request" + ['', f"--{packet}"][verbose])

            # if a ARP request does not come from a trusted IP
            elif (ARPPing and 'ARP' in packet and packet['arp'].opcode == '1'
            and packet['arp'].dst_proto_ipv4 in ips
            and packet['arp'].src_proto_ipv4 not in whitelist):
                blacklist.add(packet['arp'].src_proto_ipv4)
                log.warning(f"{packet['arp'].src_proto_ipv4}--ARP ping" + ['', f"--{packet}"][verbose])

            # if a ARP reply of the gateway does not come from a trusted MAC
            elif (ARPSpoof and 'ARP' in packet and packet['arp'].opcode == '2'
            and packet['arp'].src_hw_mac not in macs
            and packet['arp'].src_proto_ipv4 in gws):
                blacklist.add(packet['arp'].src_proto_ipv4)
                log.warning(f"{packet['arp'].src_hw_mac}--ARP spoof" + ['', f"--{packet}"][verbose])

        except Exception as e:
            log.error(f"Error during packet capture: {e}")
            continue

def main():
    parser = argparse.ArgumentParser(description='Detect suspucious activity from '\
            'the network', add_help=True)
    parser.add_argument('--ip', action="store_true", dest="IP", default=False,
            help='Enable IP touch blacklist')
    parser.add_argument('--arpping', action="store_true", dest="ARPPing", default=False,
            help='Enable ARP ping blacklist')
    parser.add_argument('--arpspoof', action="store_true", dest="ARPSpoof", default=False,
            help='Enable ARP spoof blacklist')
    parser.add_argument('--verbose', action="store_true", dest="verbose", default=False,
            help="Include packet content received from suspicious IP, WARNING : sensitive data could be transmitted through UDP syslog (cleartext)")
    parser.add_argument('iface',
            help="the network interface to monitor")
    parser.add_argument('siem',
            help="the server to send the UDP syslog alerts, can be 127.0.0.1:514")
    args = parser.parse_args()

    setLog(args.siem)
    ips, gws, macs, nss, dhcp, whitelist = populate(args.iface)
    blacklistIP(args.iface, args.IP, args.ARPPing, args.ARPSpoof, args.verbose, whitelist, ips, gws, macs)

if __name__ == '__main__':
    main()


count = 1

while(count <= 100):
    if(count % 15 == 0):
        print("FizzBuzz")
    elif(count % 3 == 0):
        print("Fizz")
    elif(count % 5 == 0):
        print("Buzz")
    elif(count % 7 == 0):
        print("GitHub")
    else:
        print(count)
    count += 1