import matplotlib.pyplot as plt
import numpy as np
import tkinter as tk

root=tk.Tk()
root.title("PYPT graphs")
root.geometry("400x1000")

def enable_dark_mode():
 choice=var2.get()
 if choice==1:
  plt.style.use('dark_background')
 elif choice==0:
  plt.style.use('default')

def another_graph():
 global label6,label7,label8,label9,entry6,entry7,entry8,entry9
 choice=var.get()
 if choice==1:

  label6=tk.Label(root, text="Enter y-values", font=("Times New Roman",10))
  label6.pack(pady=10)

  entry6=tk.Entry(root,width=48, borderwidth=4, font=("Times New Roman", 10))
  entry6.pack(pady=10)

  label7=tk.Label(root, text="Enter error bar for y values", font=("Times New Roman",10))
  label7.pack(pady=10)

  entry7=tk.Entry(root,width=48, borderwidth=4, font=("Times New Roman", 10))
  entry7.pack(pady=10)

  label8=tk.Label(root, text="enter title for line 1", font=("Times New Roman",10))
  label8.pack(pady=10)

  entry8=tk.Entry(root,width=48, borderwidth=4, font=("Times New Roman", 10))
  entry8.pack(pady=10)

  label9=tk.Label(root, text="enter title for line 2", font=("Times New Roman",10))
  label9.pack(pady=10)

  entry9=tk.Entry(root,width=48, borderwidth=4, font=("Times New Roman", 10))
  entry9.pack(pady=10)

  button2=tk.Button(root, text="create graph with now two lines", command=graph2)
  button2.pack()

def graph2():
 x=[]
 y=[]
 y2=[]
 a = entry1.get().split(",")
 for i in range(len(a)):
  x.append(float(a[i]))

 b = entry2.get().split(",")
 for i in range(len(b)):
   y.append(float(b[i]))
 y_err = float(entry3.get())

 c = entry6.get().split(",")
 for i in range(len(c)):
   y2.append(float(c[i]))

 y2_err = float(entry7.get())
 fig, ax = plt.subplots()

 ax.errorbar(x, y, yerr=y_err,label=entry8.get() ,color="red", fmt='o-', markersize=4, capsize=5, capthick=1)
 ax.errorbar(x, y2, yerr=y2_err, label=entry9.get(),color="blue", fmt='o-', markersize=4, capsize=5, capthick=1)
 ax.set_xlabel(entry4.get())
 ax.set_ylabel(entry5.get())

 ax.legend()
 plt.show()

def graph():
 x=[]
 y=[]
 x_vals = entry1.get().split(",")
 for i in range(len(x_vals)):
  x.append(float(x_vals[i]))

 y_vals = entry2.get().split(",")
 for i in range(len(y_vals)):
   y.append(float(y_vals[i]))
 y_err = float(entry3.get())

 fig, ax = plt.subplots()

 ax.errorbar(x, y, yerr=y_err ,color="red", fmt='o-', markersize=4, capsize=5, capthick=1)
 ax.set_xlabel(entry4.get())
 ax.set_ylabel(entry5.get())

 ax.legend()
 plt.show()

label1=tk.Label(root, text="Enter x-values seperated by a comma", font=("Times New Roman",10))
label1.pack(pady=10)

entry1=tk.Entry(root,width=48, borderwidth=4, font=("Times New Roman", 10))
entry1.pack(pady=10)

label2=tk.Label(root, text="Enter y-values seperated by a comma", font=("Times New Roman",10))
label2.pack(pady=10)

entry2=tk.Entry(root,width=48, borderwidth=4, font=("Times New Roman", 10))
entry2.pack(pady=10)

label3=tk.Label(root, text="Enter error bars for y", font=("Times New Roman",10))
label3.pack(pady=10)

entry3=tk.Entry(root,width=48, borderwidth=4, font=("Times New Roman", 10))
entry3.pack(pady=10)

label4=tk.Label(root, text="Enter title for x values", font=("Times New Roman",10))
label4.pack(pady=10)

entry4=tk.Entry(root,width=48, borderwidth=4, font=("Times New Roman", 10))
entry4.pack(pady=10)

label5=tk.Label(root, text="Enter title for y values", font=("Times New Roman",10))
label5.pack(pady=10)

entry5=tk.Entry(root,width=48, borderwidth=4, font=("Times New Roman", 10))
entry5.pack(pady=10)

button=tk.Button(root, text="create graph with one line", command=graph)
button.pack()
var = tk.IntVar()
checkbutton = tk.Checkbutton(root, text="Enter another graph", variable=var, onvalue=1, offvalue=0,command=another_graph)
checkbutton.pack()
var2 = tk.IntVar()
checkbutton2 = tk.Checkbutton(root,text="Dark Mode", variable=var2, onvalue=1, offvalue=0,command=enable_dark_mode)
checkbutton2.pack()
root.mainloop()


from abc import ABCMeta, abstractmethod
from copy import deepcopy
from typing import List, Optional, Tuple

import numpy as np
import pandas as pd

from ..exceptions import InvalidRecordsError
from ..record import RecordsInterface
from ..record.data_frame_shaper import DataFrameShaper, Strip

class PathBase(metaclass=ABCMeta):
    """Base class for Latency."""

    def __init__(self) -> None:
        self.__records_cache: Optional[RecordsInterface] = None

    def to_records(self) -> RecordsInterface:
        """
        Convert to records.

        Returns
        -------
        RecordsInterface
            Information for each delay.

        """
        return self.__records.clone()

    @abstractmethod
    def _to_records_core(self) -> RecordsInterface:
        """
        Convert to records.

        Returns
        -------
        RecordsInterface
            Information for each delay.

        """

    def clear_cache(self) -> None:
        self.__records_cache = None

    @property
    def __records(self) -> RecordsInterface:
        if self.__records_cache is None:
            self.__records_cache = self._to_records_core()
        return self.__records_cache

    @property
    def column_names(self) -> List[str]:
        """
        Get column names.

        Returns
        -------
        List[str]
            column names

        """
        return deepcopy(self.__records.columns)

    def to_dataframe(
        self,
        remove_dropped=False,
        treat_drop_as_delay=False,
        lstrip_s: float = 0,
        rstrip_s: float = 0,
        *,
        shaper: Optional[DataFrameShaper] = None,
    ) -> pd.DataFrame:
        """
        Convert to dataframe.

        Parameters
        ----------
        remove_dropped: bool
            If true, eliminate the records that caused the drop.
        treat_drop_as_delay: bool
            Convert dropped records as a delay.
            Valid only when remove_dropped=false.
        lstrip: Optional[float]
            Remove from beginning. [s]
        rstrip: Optional[float]
            Remove from end [s]

        Returns
        -------
        pandas.DataFrame
            Information for each delay.

        """
        records = self.to_records()
        column_names = self.column_names

        if remove_dropped is False and treat_drop_as_delay:
            records.bind_drop_as_delay()

        df = records.to_dataframe()
        for column in column_names:
            if column in df.columns:
                continue
            df[column] = np.nan
        df = df[column_names]

        if lstrip_s > 0 or rstrip_s > 0:
            strip = Strip(lstrip_s, rstrip_s)
            df = strip.execute(df)
        if shaper:
            df = shaper.execute(df)

        if remove_dropped:
            df.dropna(inplace=True)

        for missing_column in set(column_names) - set(df.columns):
            df[missing_column] = np.nan

        return df

    def to_timeseries(
        self,
        remove_dropped=False,
        treat_drop_as_delay=False,
        lstrip_s: float = 0,
        rstrip_s: float = 0,
        *,
        shaper: Optional[DataFrameShaper] = None,
    ) -> Tuple[np.array, np.array]:
        """
        Convert to timeseries data.

        Parameters
        ----------
        remove_dropped : bool
            If true, eliminate the records that caused the drop.
        treat_drop_as_delay : bool
            Convert dropped records as a delay.
            Valid only when remove_dropped=false.
        lstrip: Optional[float]
            Remove from beginning. [s]
        rstrip: Optional[float]
            Remove from end [s]

        Returns
        -------
        pandas.DataFrame
            Information for each delay.

        """
        df = self.to_dataframe(
            remove_dropped, treat_drop_as_delay, lstrip_s, rstrip_s, shaper=shaper)

        if len(df.columns) == 0:
            msg = 'Failed to calculate time series latency.'
            msg += 'There is a possibility that records are dummy data.'
            raise InvalidRecordsError(msg)

        if len(df) == 0:
            msg = 'Failed to find any records that went through the path.'
            msg += 'There is a possibility that all records are lost.'
            raise InvalidRecordsError(msg)
        source_stamps_ns = np.array(df.iloc[:, 0].values)
        dest_stamps_ns = np.array(df.iloc[:, -1].values)
        t = source_stamps_ns

        latency_ns = dest_stamps_ns - source_stamps_ns
        if remove_dropped:
            t = t.astype('int64')
            latency_ns = latency_ns.astype('int64')
        return t, latency_ns

    def to_histogram(
        self,
        binsize_ns: int = 1000000,
        treat_drop_as_delay=False,
        lstrip_s: float = 0,
        rstrip_s: float = 0,
        *,
        shaper: Optional[DataFrameShaper] = None,
    ) -> Tuple[np.array, np.array]:
        """
        Convert to histogram data.

        Parameters
        ----------
        binsize_ns : int
            bin size for histogram. default 1ms.
        treat_drop_as_delay : bool
            Convert dropped records as a delay.
        lstrip: Optional[float]
            Remove from beginning. [s]
        rstrip: Optional[float]
            Remove from end [s]

        Returns
        -------
        pandas.DataFrame
            Information for each delay.

        """
        import math

        _, latency_ns = self.to_timeseries(
            True, treat_drop_as_delay, lstrip_s, rstrip_s, shaper=shaper)
        range_min = math.floor(min(latency_ns) / binsize_ns) * binsize_ns
        range_max = math.ceil(max(latency_ns) / binsize_ns) * binsize_ns
        bin_num = math.ceil((range_max - range_min) / binsize_ns)
        return np.histogram(latency_ns, bins=bin_num, range=(range_min, range_max))


from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.

def index(request):
    if request.method == 'GET':
        return render(request, 'matrix_size_input.html')
    elif request.method == 'POST':
        if request.POST['form_submitted'] == 'matrix_size':
            m = int(request.POST['m'])
            n = int(request.POST['n'])
            request.session['m'] = m # Need to run python manage.py migrate before deploying
            request.session['n'] = n
            return render(request, 'matrix_input.html', context={'range_m':range(m),'range_n':range(n)})
        elif request.POST['form_submitted'] == 'matrix':
            m = request.session['m']
            n = request.session['n']
            # store the matrix in the variable A
            A = []
            for i in range(m):
                row = []
                for j in range(n):
                    row.append(request.POST.dict()['a_'+str(i)+'_'+str(j)])
                A.append(row)
            from .linear_equation_solver import find_solution
            return render(request, 'display_row_operations.html', context={'row_op':find_solution(A)})


from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.messages import constants
from django.shortcuts import redirect, render
from django.urls import reverse
from django_q.tasks import async_task

from dfirtrack_main.async_messages import message_user
from dfirtrack_main.forms import CaseCreatorForm
from dfirtrack_main.logger.default_logger import debug_logger, info_logger
from dfirtrack_main.models import Case, System

@login_required(login_url="/login")
def case_creator(request):
    """function to assign many cases to systems and vice versa (helper function to call the real function)"""

    # form was valid to post
    if request.method == 'POST':
        # get form
        form = CaseCreatorForm(request.POST)

        # form was valid
        if form.is_valid():
            # get objects from request object
            request_post = request.POST
            request_user = request.user

            # show immediate message for user
            messages.success(request, 'Case creator started')

            # call async function
            async_task(
                "dfirtrack_main.creator.case_creator.case_creator_async",
                request_post,
                request_user,
            )

            # return directly to case list
            return redirect(reverse('case_list'))

    # show empty form
    else:
        form = CaseCreatorForm()

        # call logger
        debug_logger(str(request.user), ' CASE_CREATOR_ENTERED')

    return render(request, 'dfirtrack_main/case/case_creator.html', {'form': form})

def case_creator_async(request_post, request_user):
    """function to assign many cases to systems and vice versa"""

    # call logger
    debug_logger(str(request_user), ' CASE_CREATOR_START')

    # extract cases (list results from request object via multiple choice field)
    cases = request_post.getlist('case')

    # extract systems (list results from request object via multiple choice field)
    systems = request_post.getlist('system')

    # set cases_affected_counter (needed for messages)
    cases_affected_counter = 0

    # set systems_affected_counter (needed for messages)
    systems_affected_counter = 0

    # iterate over systems
    for system_id in systems:
        # autoincrement counter
        systems_affected_counter += 1

        # iterate over cases
        for case_id in cases:
            # create form with request data
            form = CaseCreatorForm(request_post)

            # create relation
            if form.is_valid():
                """object creation"""

                # get objects
                system = System.objects.get(system_id=system_id)
                case = Case.objects.get(case_id=case_id)

                # add case to system
                system.case.add(case)

                """ object counter / log """

                # autoincrement counter
                cases_affected_counter += 1

                # call logger
                system.logger(str(request_user), ' CASE_CREATOR_EXECUTED')

    """ finish case creator """

    # fix case number (other meaning than with tag creator
    cases_affected_counter = int(cases_affected_counter / systems_affected_counter)

    # call final message
    message_user(
        request_user,
        f'{cases_affected_counter} cases assigned to {systems_affected_counter} systems.',
        constants.SUCCESS,
    )

    # call logger
    info_logger(
        str(request_user),
        f' CASE_CREATOR_STATUS'
        f' cases_affected:{cases_affected_counter}'
        f'|systems_affected:{systems_affected_counter}',
    )

    # call logger
    debug_logger(str(request_user), ' CASE_CREATOR_END')


#	FileName : Chapter1_15.py
'''
通过某个字段将字典、实例的序列进行分组：
关键点：
1、先通过sorted或者序列自带sort函数，利用lambda/itemgetter/attrgetter根据某字段进行排序
2、然后用itertools.groupby函数进行分组后返回迭代对象
'''
from operator import itemgetter
from itertools import groupby

if __name__ == '__main__':
	rows = [
		{'address': '5412 N CLARK', 'date': '07/01/2012'},
		{'address': '5148 N CLARK', 'date': '07/04/2012'},
		{'address': '5800 E 58TH', 'date': '07/02/2012'},
		{'address': '2122 N CLARK', 'date': '07/03/2012'},
		{'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'},
		{'address': '1060 W ADDISON', 'date': '07/02/2012'},
		{'address': '4801 N BROADWAY', 'date': '07/01/2012'},
		{'address': '1039 W GRANVILLE', 'date': '07/04/2012'},
	]
	rows.sort(key = itemgetter('date'))
	for data, items in groupby(rows, key=itemgetter('date')):
		print(data, ':')
		for item in items:
			print(' ', item)


from django.shortcuts import render, get_object_or_404
from django.views import generic
from django_filters.views import FilterView
from django.contrib.auth.decorators import login_required
from django.views.generic import ListView, TemplateView
from braces.views import LoginRequiredMixin
from braces.views import GroupRequiredMixin

from django.core.exceptions import ObjectDoesNotExist
from django.db.models.query_utils import Q
from django.http import JsonResponse, HttpResponse, HttpResponseForbidden
from django.shortcuts import get_list_or_404, get_object_or_404, render, render_to_response
from django_tables2 import SingleTableView, RequestConfig
from django_tables2.export.views import ExportMixin

from dal import autocomplete
from django.db.models import Count, Case,Sum, When, IntegerField, CharField
from django.db.models.functions import Substr
from django.db.models.expressions import Value
from django.contrib.auth.decorators import user_passes_test

import datetime
import decimal
from itertools import chain

# from django.http import HttpResponse

from harvest.models.strategy import Strategy, Stock, Watchlist, Order, Signal, Ledger
from harvest.tables.harvest import LedgerTable, LedgerFilter, OrderTable, OrderFilter, SignalTable, SignalFilter
from harvest.forms.harvest import LedgerFormHelper, OrderFormHelper, SignalFormHelper
from harvest.services import signal, ledger

class HarvestView(generic.ListView):
    template_name = 'home/index.html'
    context_object_name = 'user_strategy_list'

    def get_queryset(self):
        """Return the last five items."""
        return Strategy.objects.order_by('-name')  # [:5]

class StrategyView(generic.ListView):
    template_name = 'strategy/index.html'
    context_object_name = 'user_strategy_list'

    def get_queryset(self):
        return Strategy.objects.annotate(prof=Sum('watchlist__profit_earned'))

class StockView(generic.ListView):
    template_name = 'stock/index.html'
    context_object_name = 'user_stock_list'

    def get_queryset(self):
        return Stock.objects.order_by('stock')

# class SignalView(generic.ListView):
#     template_name = 'signal/index.html'
#     context_object_name = 'user_stock_list'

#     def get_queryset(self):
#         return Signal.objects.order_by('-time')

# class OrderView(generic.ListView):
#     template_name = 'order/index.html'
#     context_object_name = 'user_stock_list'

#     def get_queryset(self):
#         return Order.objects.order_by('-time')

class PagedFilteredTableView(ExportMixin, SingleTableView):
    filter_class = None
    formhelper_class = None
    context_filter_name = 'filter'
    # exclude_column = ('x_edit','x_transfer','x_promote','x_terminate')

    def get_queryset(self, **kwargs):
      qs = super(PagedFilteredTableView, self).get_queryset()
      self.filter = self.filter_class(self.request.GET, queryset=qs)
      self.filter.form.helper = self.formhelper_class()
      return self.filter.qs

    def get_context_data(self, **kwargs):
      context = super(PagedFilteredTableView, self).get_context_data()
      context[self.context_filter_name] = self.filter
      return context

class LedgerListView(PagedFilteredTableView):
    model = Ledger
    template_name = 'ledger/index.html'
    context_object_name = 'ledger_list'
    # ordering = ['e_name']
    # group_required = u'area_apms'
    table_class = LedgerTable
    filter_class = LedgerFilter
    formhelper_class = LedgerFormHelper

    def get_queryset(self):
        qs = super(LedgerListView, self).get_queryset()
        return qs

    def post(self, request, *args, **kwargs):
        return PagedFilteredTableView.as_view()(request)

    def get_context_data(self, **kwargs):
        context = super(LedgerListView, self).get_context_data(**kwargs)
        context['nav_customer'] = True
        search_query = self.get_queryset()
        # print(search_query.query)
        table = LedgerTable(search_query)
        RequestConfig(self.request, paginate={'per_page': 40}).configure(table)
        context['table'] = table
        context['total_rows'] = search_query.count()
        # print(context['total_rows'])
        return context

class OrderListView(PagedFilteredTableView):
    model = Order
    template_name = 'order/index.html'
    context_object_name = 'ledger_list'
    # ordering = ['e_name']
    # group_required = u'area_apms'
    table_class = OrderTable
    filter_class = OrderFilter
    formhelper_class = OrderFormHelper

    def get_queryset(self):
        qs = super(OrderListView, self).get_queryset()
        return qs

    def post(self, request, *args, **kwargs):
        return PagedFilteredTableView.as_view()(request)

    def get_context_data(self, **kwargs):
        context = super(OrderListView, self).get_context_data(**kwargs)
        context['nav_customer'] = True
        search_query = self.get_queryset()
        # print(search_query.query)
        table = OrderTable(search_query)
        RequestConfig(self.request, paginate={'per_page': 40}).configure(table)
        context['table'] = table
        context['total_rows'] = search_query.count()
        # print(context['total_rows'])
        return context

class SignalListView(PagedFilteredTableView):
    model = Signal
    template_name = 'signal/index.html'
    context_object_name = 'ledger_list'
    # ordering = ['e_name']
    # group_required = u'area_apms'
    table_class = SignalTable
    filter_class = SignalFilter
    formhelper_class = SignalFormHelper

    def get_queryset(self):
        qs = super(SignalListView, self).get_queryset()
        return qs

    def post(self, request, *args, **kwargs):
        return PagedFilteredTableView.as_view()(request)

    def get_context_data(self, **kwargs):
        context = super(SignalListView, self).get_context_data(**kwargs)
        context['nav_customer'] = True
        search_query = self.get_queryset()
        # print(search_query.query)
        table = SignalTable(search_query)
        RequestConfig(self.request, paginate={'per_page': 40}).configure(table)
        context['table'] = table
        context['total_rows'] = search_query.count()
        # print(context['total_rows'])
        return context

def watchlist(request, strategy_id):
    total_gain = 0
    total_unreal = 0
    total_fund_avail = 0
    strategy = Strategy.objects.get(id=strategy_id)
    try:
        watchlist_ls = Watchlist.objects.filter(strategy=strategy_id).order_by('-signal_status','profit_earned')
        for item in watchlist_ls:
            total_gain = total_gain + item.profit_earned
            if item.signal_status=='OPEN':
                item.unreal_gain = (item.stock.close - item.last_transact_price) * item.last_qty
                total_unreal = total_unreal + item.unreal_gain
            else:
                item.unreal_gain=''
        total_fund_avail = ledger.get_current_avail(strategy.name)

    except Watchlist.DoesNotExist:
        raise Http404("watchlist does not exist")
    return render(request, 'watchlist/'+strategy.name+'_index.html', {'user_watchlist': watchlist_ls,
            'total_gain':total_gain,
            'total_unreal':total_unreal,
            'total_fund_avail':total_fund_avail,
            'strategy':strategy
            })

def close_signal(request, watchlist_id):
    response = {"success":False}
    try:
        watchlist = Watchlist.objects.get(id=watchlist_id)
        signal.close(watchlist, watchlist.stock.close)
        response = {"success": True}
    except Exception as e:
        raise e
    return JsonResponse(response, safe=False)

def add_fund(request, strategy_id, amt):
    amt = decimal.Decimal(amt)
    response = {"success":False}
    try:
        strategy = Strategy.objects.get(id=strategy_id)
        response = ledger.add_fund(strategy, amt)
    except Exception as e:
        raise e
    return JsonResponse(response, safe=False)

def pull_fund(request, strategy_id, amt):
    amt = decimal.Decimal(amt)
    response = {"success":False}
    try:
        strategy = Strategy.objects.get(id=strategy_id)
        response = ledger.pull_fund(strategy, amt)
    except Exception as e:
        raise e
    return JsonResponse(response, safe=False)

# class WatchlistView(generic.ListView):
#     template_name = 'watchlist/index.html'
#     context_object_name = 'user_watchlist'

#     def get_queryset(self):
#         # """Return the last five items."""
#         print(str(self.que))
#         Watchlist.objects.filter(strategy=self.request.strategy_id).order_by('norm_score')
#         # # [:5]

# def index(request):
#     user_strategy_list = Strategy.objects.order_by('-name')[:5]
#     context = {
#         'user_strategy_list': user_strategy_list,
#     }
#     return render(request, 'strategy/index.html', context)

    # stock_list = get_object_or_404(Watchlist, strategy=strategy_id)
    # return render(request, 'watchlist/index.html', {'user_watchlist':
    # stock_list})


from typing import List

class Solution:
    def wordBreak(self, s: str, wordDict: List[str]) -> bool:
        l = len(s)

        dp = [False]*(l+1)
        dp[0] = True
        for i in range(1,l+1):
            for j in range(i):
                if dp[j] and s[j:i] in wordDict:
                    dp[i] = True
                    break;
        return dp[l]

#https://leetcode.com/problems/word-break/description/?envType=study-plan-v2&envId=dynamic-programming
#Given a string s and a dictionary of strings wordDict, return true if s can be segmented into a space-separated
# sequence of one or more dictionary words.Note that the same word in the dictionary may be reused multiple times in the segmentation.


from django.shortcuts import render, redirect, HttpResponseRedirect, HttpResponse
from django.http import JsonResponse
from django.contrib import messages
import razorpay
from django.contrib.auth.decorators import login_required
from .models import Order, OrderItem
from home.models import Restraunt, Menu, Branch, Dish, FoodItem, Category
from address.models import Address, AddressList
# Create your views here.

client = razorpay.Client(auth=('rzp_test_7DRR93ecd1hzdM', 'Dyfy8kCYQaYkDRUma72Rbh6I'))

def cartPageView(request):
    order = OrderItem.objects.all()

def addItem(request, id):
    print(id)
    if id < 0:
        dish_id = -1 * id
    else:
        dish_id = id
    customer = request.user
    order, created = Order.objects.get_or_create(customer=customer, is_complete=False, status='pending')

    if request.method == 'GET':
        try:
            branch = Branch.objects.filter(name='Branch_1')
            item = Dish.objects.get(id=dish_id)
            #print(item)

            # create or update current order and quantity of new dishes
            orderItem, created = OrderItem.objects.get_or_create(order=order, orderItem=item)
            dishTotal = orderItem.get_total     # total price of dishes : quantity * dishes cost

            if id < 0:
                orderItem.quantity -= 1
                orderItem.save()
                print("removed")
            else:
                orderItem.quantity += 1
                orderItem.save()
                print('added')

            if orderItem.quantity <= 0:
                Order.delete(orderItem)
                print('deleted')
            #print('QUANTITY', order.orderitem_set.all())
        except:
            pass
        finally:
            # fetch all the dishes in current order from OrderItem Table/ Model
            orderItems = OrderItem.objects.values().filter(order=order)
            #print(list(orderItems))
            orderedDishDict = {}
            orderedDishDictList = []

            for item in list(orderItems):
                dishId = item["orderItem_id"]
                dish = list(Dish.objects.values().filter(id=dishId))
                dish = dish[0]

                orderedDishDict["dishId"] = dishId

                dishName = dish["dish"]
                orderedDishDict["dishName"] = dishName

                dishCost = dish["cost"]
                orderedDishDict["dishCost"] = dishCost

                dishAvailability = dish["availability"]
                orderedDishDict["dishAvailability"] = dishAvailability

                dishType = dish["type"]
                orderedDishDict["dishType"] = dishType

                dishQuantity = item["quantity"]
                orderedDishDict["quantity"] = dishQuantity

                dishTotal = OrderItem.objects.get(id = item["id"]).get_total
                orderedDishDict["dishTotal"] = dishTotal

                # print("Dish Name :", dishName)
                # print("Dish Cost :", dishCost)
                # print("Dish Availability :", dishAvailability)
                # print("Dish Type :", dishType)

                orderedDishDictList.append(orderedDishDict)
                orderedDishDict = {}

            print("Order Total :", order.get_bill)
            print(orderedDishDictList)
            orderedDishDictList.append({"orderTotal" : order.get_bill})

            return JsonResponse(orderedDishDictList, safe=False)

def completeOrder(request):
    user = request.user
    context = {}
    if user.is_authenticated:
        order = Order.objects.get(customer=user, is_complete=False, status='pending')

        response = client.order.create(dict(amount=order.get_bill*100,
                                            currency="INR",
                                            receipt="order_" + str(order.id),
                                            notes={},
                                            payment_capture='0'
                                            ))

        order_id = response['id']
        print(order_id)
        order_status = response['status']

        if order_status == 'created':
            context['order'] = order
            context['price'] = order.get_bill
            context['name'] = user.first_name + user.last_name
            context['phone'] = user.phone_no
            context['email'] = user.email

            context['order_id'] = order_id

            return render(request, 'order/payment.html', context)

            # print('\n\n\nresponse: ',response, type(response))
        return HttpResponse('
Error in  create order function
')

def payment_status(request):
    user = request.user
    context = {}
    order = Order.objects.get(customer=user, is_complete=False)

    response = request.POST

    params_dict = {
        'razorpay_payment_id' : response['razorpay_payment_id'],
        'razorpay_order_id' : response['razorpay_order_id'],
        'razorpay_signature' : response['razorpay_signature']
    }

    # VERIFYING SIGNATURE
    try:
        status = client.utility.verify_payment_signature(params_dict)

        order.status = 'payed'
        order.is_complete = True
        print(order.id)
        order.save()
        messages.success(request, 'order completed!')
        return render(request, 'order/order_summary.html', {'status': 'Payment Successful'})
    except:
        order.is_complete = True
        print(order.id)
        order.save()
        messages.success(request, 'Failed order completed!')
        return render(request, 'order/order_summary.html', {'status': 'Payment Faliure!!!'})

def orderStatusView(request, _id):
    user = request.user
    dishDict = {}
    dishList = []

    if not user.is_authenticated:
        return redirect('login')

    order = Order.objects.all(customer=user, is_complete=False, id=_id, status='payed')
    orderItems = OrderItem.objects.filter(order=order)
    address = order.address

    if request.method == "GET":
        for item in list(orderItems):
            dishId = item.orderItem.id
            dishName = item.orderItem.dish
            dishCost = item.orderItem.cost
            dishQuantity = item.quantity
            dishTotal = item.get_total

            dishDict["dishId"] = dishId
            dishDict["dishName"] = dishName
            dishDict["dishCost"] = dishCost
            dishDict["dishQuantity"] = dishQuantity
            dishDict["dishTotal"] = dishTotal
            dishList.append(dishDict)
            dishDict = {}
            # print("dishId : ", dishId, " dishName : ", dishName, " dishCost : ", dishCost, " dishQuantity : ",
            # dishQuantity)
        dishDict["totalBill"] = order.get_bill
        dishList.append(dishDict)

        context = {
            'order': order,
            'dishes': dishList,
            'address': address,
            'name': f'{user.first_name} {user.last_name}',
            'order_id': order.id,
        }

def orderView(request, id):
    user = request.user
    dishDict = {}
    dishList = []

    if not user.is_authenticated:
        return redirect('login')

    order = Order.objects.get(id=id)
    orderItems = OrderItem.objects.filter(order=order)

    for item in list(orderItems):
        dishDict['dishName'] = item.orderItem.dish
        dishDict['quantity'] = item.quantity
        dishDict['dishCost'] = item.orderItem.cost
        dishDict['dishTotal'] = item.get_total
        dishList.append(dishDict)
        dishDict = {}

    context = {
        'order': order,
        'customer': user,
        'dishes': dishList,
        'total': order.get_bill,
        'address': order.address,
    }

    return render(request, 'order/order-view.html', context)


from openai import OpenAI
from dotenv import load_dotenv
import os

load_dotenv()
openai_api_key = os.getenv('OPENAI_API_KEY')

client = OpenAI(
    api_key = openai_api_key
)

def summarize_text(text):
    response = client.chat.completions.create(
    model="gpt-3.5-turbo-1106",
    messages=[
        {
            "role": "user",
            "content": f"""
            Sos un abogado especialista en justicia federal. Necesito que analices la siguiente sentencia de la Corte Superema de Justicia de la Nación, me la resumas en no más de 100 palabras y me digas que fue lo que se resolvió.
            Además quiero que lo devuelvas en un formato como el siguiente:
            1. *Título de la sentencia*:
            2. *Involucrados en la causa*:
            3. *Resolución*:
            4. *Resumen ejecutivo*:
            : {text}"""
        }
    ]
    )

    return response.choices[0].message.content


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('varer', '0012_auto_20141222_0524'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='salgsvarepris',
            name='kassenr',
        ),
        migrations.AddField(
            model_name='salgsvare',
            name='kassenr',
            field=models.PositiveSmallIntegerField(blank=True, help_text='Nr i varekatalog i kassa', null=True),
            preserve_default=True,
        ),
    ]


from flask import Flask, render_template, request, flash, redirect, session
app = Flask(__name__)
app.secret_key = "secret_key"

@app.route('/')

def index():
  return render_template("index.html")

@app.route('/check', methods=['POST'])

def check():
	if len(request.form['name']) == 0:
		flash('Name must not be blank')

	if len(request.form['comment']) > 120:
		flash('Comment must not be over 120 characters')

	if len(request.form['name']) == 0 or len(request.form['comment']) > 120:
		return redirect('/')

	session['all_info']	= request.form
	return redirect('/result')

@app.route('/result')

def result():
	return render_template('result.html', result=session['all_info'])

app.run(debug=True)


import re
import io
import time
import struct

import numpy as np

import ncep_wave.terminal as term

# '<Field ID>' <n freqs> <n dirs> <n points> '<grid name>'
#
# Indent 1: <freqs (Hz)> X nfreqs
#
# Indent 2: <directions (rads)> X ndirs
#
# <Time>
# '<station number>' <lat>-<lon> <d> <u10> <u10 dir> <crnt speed> <crnt dir>
#
# Indent 1: E(f, theta) X nfreqs*ndirs

def indent(f):
    ident = 0
    place = f.tell()
    char = f.read(1)
    while char == " " and char != "\n":
        ident += 1
        char = f.read(1)
    f.seek(place)
    return ident

class Spectrum:

    nfreqs = None
    ndirs = None
    freqs = None
    dirs = None
    # records = None

    class Record:

        def __init__(self, rtime, pid, lat, lon, d, UA, UD, crnt, crnt_dir, freqs, dfreqs, dirs):
            self.rtime = rtime
            self.pid = pid
            self.lat = lat
            self.lon = lon
            self.depth = d      # Depth
            self.UA = UA        # Wind speed at 10m
            self.UD = UD        # Wind dir
            self.current = crnt
            self.crnt_dir = crnt_dir
            self.freqs = freqs
            self.dfreqs = dfreqs
            self.dirs = dirs
            self.data = None

        @property
        def hs(self):
            """ Hs is calculated as 4 * sqrt(E), where E is the spectrum integral

            (see Nondirectional and Directional Wave Data Analysis Procedures, sec 3.2.11)
            """
            # Integrate across directions
            dir_int = self.data.sum(axis=0) * 2*np.pi/len(self.dirs)

            # Bandwidths for the frequency bands
            bw = 0.5 * (self.dfreqs - 1/self.dfreqs) * self.freqs
            bw[-1] *= 0.5
            tailf = 0.25 * self.freqs[-1]

            E = np.dot(dir_int, bw) + tailf * dir_int[-1]
            return 4 * np.sqrt(E)

    def __init__(self, fspec):
        if isinstance(fspec, str):
            term.message(f"Parsing data in {fspec}")
            self.fspec = open(fspec)
        elif isinstance(fspec, io.BufferedReader):
            self.fspec = fspec
        else:
            raise ValueError(f"Cannot parse {type(fspec)} into a Spectrum")
        self._records = []
        self._parse_header()

    @property
    def location(self):
        """ Returns the location of the station
        """
        if len(self._records) == 0:
            record = next(self.records)
        else:
            record = self._records[0]
        return record.lat, record.lon

    @property
    def station(self):
        if len(self._records) == 0:
            record = next(self.records)
        else:
            record = self._records[0]
        return record.pid

    @property
    def records(self):
        while True:
            rec = self._parse_record()
            if not rec:
                return
            self._records.append(rec)
            yield rec

    def read_all(self):
        while True:
            rec = self._parse_record()
            if not rec:
                return self._records
            self._records.append(rec)

    def _parse_header(self):
        if self.fspec.mode == "rb":
            self._parse_binary_header()
        else:
            self._parse_ascii_header()

    def _parse_record(self):
        if self.fspec.mode == "rb":
            return self._parse_binary_record()
        else:
            return self._parse_ascii_record()

    def _parse_ascii_header(self):
        """ Parses the header

        This captures:
        * n freqs
        * n dirs
        * n points (grid name is ignored)
        * Frequencies, and derives df
        * Directions
        """
        # Capture metadata
        line = self.fspec.readline().strip()
        m = re.fullmatch(r"'WAVEWATCH III SPECTRA' +([0-9]+) +([0-9]+) +([0-9]+).*", line)
        if not m:
            raise ValueError(f"Bad header format: {line}")
        self.nfreqs, self.ndirs, self.npoints = [int(val) for val in m.groups()]

        # Capture frequencies
        freqs = []
        while indent(self.fspec) == 1:
            freqs += [float(val) for val in self.fspec.readline().split()]
        if len(freqs) != self.nfreqs:
            raise ValueError(f"Wrong number of frequencies: {len(freqs)} != {self.nfreqs}\n {freqs}")
        self.freqs = np.array(freqs)

        # Calculate dfreqs
        df = self.freqs[1:]/self.freqs[:-1]
        self.df = np.append(df, df[-1])

        # Capture directions
        dirs = []
        while indent(self.fspec) == 2:
            dirs += [float(val) for val in self.fspec.readline().split()]
        if len(dirs) != self.ndirs:
            raise ValueError(f"Wrong number of directions: {len(dirs)} != {self.ndirs}\n{dirs}")
        self.dirs = np.array(dirs)

    def _parse_binary_header(self):
        """
        | Form     | Value         | format      | unit |              n |
        |----------+---------------+-------------+------+----------------|
        |          | sname length  | uint8       |      |              1 |
        |          | station name  | string      |      |       variable |
        |          | latitude      | float       | deg  |              1 |
        |          | longitude     | float       | deg  |              1 |
        | Header   | n freqs       | uint16      |      |              1 |
        |          | freqs         | float array | Hz   |        n freqs |
        |          | n dirs        | uint16      |      |              1 |
        |          | directions    | float array | rads |         n dirs |
        """
        sname_len = self.fspec.read(1)[0]
        self.station_name = str(self.fspec.read(sname_len))
        self.lat, self.lon = struct.unpack("ff", self.fspec.read(8))  # read 2*float32

        self.nfreqs = struct.unpack("H", self.fspec.read(2))[0]
        freqs = struct.unpack("f" * self.nfreqs, self.fspec.read(4 * self.nfreqs))
        self.freqs = np.array(freqs)

        # Calculate dfreqs
        df = self.freqs[1:]/self.freqs[:-1]
        self.df = np.append(df, df[-1])

        self.ndirs = struct.unpack("H", self.fspec.read(2))[0]
        dirs = struct.unpack("f" * self.ndirs, self.fspec.read(4 * self.ndirs))
        self.dirs = np.array(dirs)

    def _parse_ascii_record(self):
        """ Parses a single record

        A record consists of:
        * The record time YYYYMMDD HH0000
        * Station name
        * Station latitude
        * Station longitude
        * Water depth
        * Wind speed (m/s)
        * Wind direction (deg)
        * Current speed (m/s)
        * Current direction (deg)
        * ndirs*nfreqs data points
        """
        # Read time
        timeline = self.fspec.readline()

        if not timeline:
            return None

        rtime = time.mktime(time.strptime(timeline.strip(), "%Y%m%d %H%M%S"))

        # Read Summary
        rsum = self.fspec.readline().strip()
        m = re.fullmatch(r"'([^ ]+) *' +(-?[0-9.]+) *(-?[0-9.]+) +([0-9.]+) +([0-9.]+) +([0-9.]+) +([0-9.]+) +([0-9.]+)",
                         rsum)
        if not m:
            raise ValueError(f"Bad record header: {rsum}")
        params = [m.groups()[0]] + [float(val) for val in m.groups()[1:]]
        record = Spectrum.Record(rtime, *params, self.freqs, self.df, self.dirs)

        data = []
        while indent(self.fspec) == 2:
            data += [float(val) for val in self.fspec.readline().split()]

        if len(data) != self.nfreqs * self.ndirs:
            raise ValueError(f"Received an unexpected amount of data: \n{data}\n\n{len(data)} elements")

        record.data = np.array(data).reshape((self.ndirs, self.nfreqs))
        return record

    def _parse_binary_record(self):
        """
        | Form     | Value         | format              | unit |              n |
        |----------+---------------+---------------------+------+----------------|
        | Record 1 | time          | uint32              | s    |              1 |
        |          | water depth   | float               | m    |              1 |
        |          | wind speed    | float               | m/s  |              1 |
        |          | wind dir      | float               | rads |              1 |
        |          | current speed | float               | m/s  |              1 |
        |          | current dir   | float               | rads |              1 |
        |          | spectrum      | counted float array |      | n freqs*n dirs |
        |----------+---------------+---------------------+------+----------------|
        """
        try:
            rtime, depth, UA, UD, crnt, crnt_dir = struct.unpack("Ifffff", self.fspec.read(4 * 6))
            spec_len = struct.unpack("I", self.fspec.read(4))[0]
            exp_spec_len = self.nfreqs * self.ndirs
            if spec_len != exp_spec_len:
                raise ValueError(f"Received unexpected amount of data: \ngot {spec_len} values, expected {exp_spec_len}")
            data = struct.unpack("f" * spec_len, self.fspec.read(4 * spec_len))

            record = Spectrum.Record(rtime,
                                     self.station_name,
                                     self.lat,
                                     self.lon,
                                     depth,
                                     UA, UD,
                                     crnt, crnt_dir,
                                     self.freqs, self.df,
                                     self.dirs)
            record.data = np.array(data).reshape((self.ndirs, self.nfreqs))

            return record
        except struct.error:
            return None


from django.conf.urls.defaults import patterns, url

# Uncomment the next two lines to enable the admin:
#from django.contrib import admin
#admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'wiki.views.home', name='home'),
    url(r'^manage$', 'wiki.views.manage',name='manage'),
    url(r'^view/(?P<page_id>\d+)/$', 'wiki.views.view',name='view'),
    url(r'^new$', 'wiki.views.new', name='new'),
    url(r'^add/(?P<page_id>\d+)/$', 'wiki.views.add', name='add'),
    url(r'^edit/(?P<page_id>\d+)/$', 'wiki.views.edit', name='edit'),
    url(r'^save$', 'wiki.views.save', name='save'),

    url(r'^del/(?P<page_id>\d+)/$', 'wiki.views.delete', name='delete'),
    url(r'^login$', 'django.contrib.auth.views.login', {'template_name': 'web2/login.html'}),
    url(r'^logout$', 'django.contrib.auth.views.logout', {'next_page':"/"}),

    url(r'^api/$', 'wiki.views.api', name='api'),
    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    #url(r'^admin/', include(admin.site.urls)),
)


from numbers import Real
from typing import Optional

import numpy as np

import mygrad._utils.graph_tracking as _tracking
from mygrad.nnet.activations import softmax
from mygrad.operation_base import Operation
from mygrad.tensor_base import Tensor, asarray
from mygrad.typing import ArrayLike

from ._utils import check_loss_inputs

__all__ = ["softmax_focal_loss", "focal_loss"]

class FocalLoss(Operation):
    r"""Returns the per-datum focal loss as described in https://arxiv.org/abs/1708.02002
    which is given by -ɑ(1-p)ˠlog(p).

    Extended Description
    --------------------
    The focal loss is given by

    .. math::
        \frac{1}{N}\sum\limits_{1}^{N}-\alpha \hat{y}_i(1-p_i)^\gamma\log(p_i)

    where :math:`N` is the number of elements in `x` and `y` and :math:`\hat{y}_i` is
    one where :math:`i` is the label of the element :math:`y_i` and 0 elsewhere. That is,
    if the label :math:`y_k` is 1 and there are four possible label values, then
    :math:`\hat{y}_k = (0, 1, 0, 0)`.

    It is recommended in the paper that you normalize by the number of foreground samples.
    """

    def __call__(self, class_probs, targets, alpha, gamma):
        """
        Parameters
        ----------
        class_probs : mygrad.Tensor, shape=(N, C)
            The C class scores for each of the N pieces of data.

        targets : Union[mygrad.Tensor, ArrayLike], shape=(N,)
            The correct class indices, in [0, C), for each datum.

        alpha : Real
            The ɑ weighting factor in the loss formulation.

        gamma : Real
            The ɣ focusing parameter.

        Returns
        -------
        numpy.ndarray
            The per-datum focal loss.
        """
        if isinstance(targets, Tensor):  # pragma: nocover
            targets = targets.data

        check_loss_inputs(class_probs, targets)

        self.variables = (class_probs,)
        self.label_locs = (range(len(class_probs)), targets)

        class_probs = asarray(class_probs)
        pc = class_probs[self.label_locs]
        one_m_pc = np.clip(1 - pc, a_min=0, a_max=1)
        log_pc = np.log(pc)

        one_m_pc_gamma = one_m_pc**gamma
        loss = -(alpha * one_m_pc_gamma * log_pc)

        if not _tracking.TRACK_GRAPH:
            return loss

        self.back = np.zeros(class_probs.shape, dtype=np.float64)

        if np.isclose(gamma, 0, atol=1e-15):
            self.back[self.label_locs] -= alpha / pc
            return loss

        # dL/dp = -alpha * ( (1 - p)**g / p - g * (1 - p)**(g - 1) * log(p) )
        #
        # term 1: (1 - p)**g / p
        term1 = one_m_pc_gamma / pc  # (1 - p)**g / p

        # term 2: - g * (1 - p)**(g - 1) * log(p)
        if np.isclose(gamma, 1, rtol=1e-15):
            term2 = -log_pc
        elif gamma < 1:
            # For g < 1 and p -> 1, the 2nd term -> 0 via L'Hôpital's rule
            term2 = np.zeros(pc.shape, dtype=class_probs.dtype)
            pc_not_1 = ~np.isclose(one_m_pc, 0, atol=1e-25)
            term2[pc_not_1] = (
                -gamma * one_m_pc[pc_not_1] ** (gamma - 1) * log_pc[pc_not_1]
            )
        else:
            term2 = -gamma * one_m_pc ** (gamma - 1) * log_pc

        self.back[self.label_locs] -= alpha * (term1 + term2)
        return loss

    def backward_var(self, grad, index, **kwargs):
        self.back[self.label_locs] *= grad
        return self.back

def focal_loss(
    class_probs: ArrayLike,
    targets: ArrayLike,
    *,
    alpha: float = 1,
    gamma: float = 0,
    constant: Optional[bool] = None,
) -> Tensor:
    r"""Return the per-datum focal loss.

    Parameters
    ----------
    class_probs : ArrayLike, shape=(N, C)
        The C class probabilities for each of the N pieces of data.
        Each value is expected to lie on (0, 1]

    targets : ArrayLike, shape=(N,)
        The correct class indices, in [0, C), for each datum.

    alpha : Real, optional (default=1)
        The ɑ weighting factor in the loss formulation.

    gamma : Real, optional (default=0)
        The ɣ focusing parameter. Note that for Ɣ=0 and ɑ=1, this is cross-entropy loss.
        Must be a non-negative value.

    constant : Optional[bool]
        If ``True``, the returned tensor is a constant (it
        does not back-propagate a gradient)

    Returns
    -------
    mygrad.Tensor, shape=(N,)
        The per-datum focal loss.

    Notes
    -----
    The formulation for the focal loss introduced in https://arxiv.org/abs/1708.02002.
    It is given by -ɑ(1-p)ˠlog(p).

    The focal loss for datum-:math:`i` is given by

    .. math::
        -\alpha \hat{y}_i(1-p_i)^\gamma\log(p_i)

    where :math:`\hat{y}_i` is one in correspondence to the label associated with the
    datum and 0 elsewhere. That is, if the label :math:`y_k` is 2 and
    there are four possible label values, then :math:`\hat{y}_k = (0, 0, 1, 0)`.

    It is recommended in the paper that you normalize by the number of foreground samples.
    """
    if not isinstance(gamma, Real) or gamma < 0:
        raise ValueError(f"`gamma` must be a non-negative number, got: {gamma}")

    return Tensor._op(
        FocalLoss, class_probs, op_args=(targets, alpha, gamma), constant=constant
    )

def softmax_focal_loss(
    scores: ArrayLike,
    targets: ArrayLike,
    *,
    alpha: float = 1,
    gamma: float = 0,
    constant: Optional[bool] = None,
) -> Tensor:
    r"""
    Applies the softmax normalization to the input scores before computing the
    per-datum focal loss.

    Parameters
    ----------
    scores : ArrayLike, shape=(N, C)
        The C class scores for each of the N pieces of data.

    targets : ArrayLike, shape=(N,)
        The correct class indices, in [0, C), for each datum.

    alpha : Real, optional (default=1)
        The ɑ weighting factor in the loss formulation.

    gamma : Real, optional (default=0)
        The ɣ focusing parameter. Note that for Ɣ=0 and ɑ=1, this is cross-entropy loss.
        Must be a non-negative value.

    constant : Optional[bool]
        If ``True``, the returned tensor is a constant (it
        does not back-propagate a gradient)

    Returns
    -------
    mygrad.Tensor, shape=(N,)
        The per-datum focal loss.

    Notes
    -----
    The formulation for the focal loss introduced in https://arxiv.org/abs/1708.02002.
    It is given by -ɑ(1-p)ˠlog(p).

    The focal loss for datum-:math:`i` is given by

    .. math::
        -\alpha \hat{y}_i(1-p_i)^\gamma\log(p_i)

    where :math:`\hat{y}_i` is one in correspondence to the label associated with the
    datum and 0 elsewhere. That is, if the label :math:`y_k` is 2 and
    there are four possible label values, then :math:`\hat{y}_k = (0, 0, 1, 0)`.

    It is recommended in the paper that you normalize by the number of foreground samples.
    """
    return focal_loss(
        softmax(scores), targets=targets, alpha=alpha, gamma=gamma, constant=constant
    )


from datetime import datetime

from typing import Iterable

class Message_INNER:
    FMT_BEGIN = ">"
    TS_FIELD = "ts"
    FIELDS = [{"name": "status", "fmt": "H", "ru": "Статус"},
              {f"name": "EMPTY", "fmt": "H", "ru": "EMPTY"},
              {"name": "actual_turnover", "fmt": "f", "ru": "Фактические обороты"},
              {"name": "generator_voltage", "fmt": "f", "ru": "Упр. Напряжением. На генераторе"},
              {"name": "brake_degree", "fmt": "f", "ru": "Степень торможения"},
              {"name": "body_temperature", "fmt": "f", "ru": "Температура на корпусе"},
              {"name": "windings_temperature", "fmt": "f", "ru": "Температура на обмотках"},
              {"name": "vibration_speed", "fmt": "f", "ru": "Виброскорость"},
              {"name": "specified_frequency", "fmt": "f", "ru": "Заданная частота"},
              {"name": "acceleration_frequency", "fmt": "f", "ru": "Частота разгона"},
              {"name": "current_consumption", "fmt": "f", "ru": "Ток потребления"},
              {"name": "output_voltage", "fmt": "f", "ru": "Выходное напряжение"},
              {"name": "output_power", "fmt": "f", "ru": "Выходная мощность"},
              {"name": "power_coefficient", "fmt": "f", "ru": "Коэффициент мощности"},
              {"name": "IGBT_temperature", "fmt": "H", "ru": "Температура IGBT модуля"},
              {"name": "start_stop", "fmt": "H", "ru": "Пуск/Стоп"}, ]

    def __init__(self, state: Iterable):
        self.ts = datetime.now().timestamp()
        self.state = [v for i, v in enumerate(state) if i not in self._empty_indexes()]
        self.FIELDS = [v for i, v in enumerate(self.FIELDS) if i not in self._empty_indexes()]

    @classmethod
    def _empty_indexes(cls):
        return [i for i, v in enumerate(cls.FIELDS) if v["name"] == "EMPTY"]

    @classmethod
    def build_fmt(cls):
        fmt = [cls.FMT_BEGIN]
        for field in cls.FIELDS:
            fmt.append(field["fmt"])
        return "".join(fmt)

    @classmethod
    def get_cols(cls):
        cols = [cls.TS_FIELD]
        cols.extend([val['name'] for i, val in enumerate(cls.FIELDS) if i not in cls._empty_indexes()])
        return cols

    @classmethod
    def dict_from_csv_row(cls, row: str):
        values = row.strip().split(',')
        cols = cls.get_cols()
        return {cols[i]: float(val) for i, val in enumerate(values)}

    def get_csv_row(self):
        """
        Format message as csv row
        :return: 'message.ts,val1,...valN'
        """
        return f"{self.ts}, {','.join([str(v) for v in self.state])}"

    def pretty_print(self):
        for i, v in enumerate(self.FIELDS):
            print(v["ru"], self.state[i])


# to convert to 8-bit TIFF files that have been accidentally saved as 12-bit
# usage: python bitCut.py /path/or/paths/to/images

import numpy as np
from PIL import Image
from os import path, walk, mkdir
import sys

basePath = sys.argv[1:]
for bp in basePath:
    for root, dirs, files in walk(bp):
        files = [f for f in files if f.split(".")[-1] == "tif"]
        OPdir = path.join(root,"bit_fix")
        try:
            mkdir(OPdir)
        except OSError:
            nop=True
        for f in files:
            IPpath = path.join(root,f)
            OPpath = path.join(OPdir,f)
            im = Image.open(IPpath)
            img = np.array(im).astype(np.uint16)
            img = np.floor(np.divide(img,2**4)).astype(np.uint8)
            im = Image.fromarray(img)
            im.save(OPpath)


import time
import os

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

EMAIL = os.environ.get("EMAIL")
PASS = os.environ.get("PASS")

url = "https://tinder.com"
decline_cookies_xpath = '//*[@id="u-1238065328"]/main/div[2]/div/div/div[1]/div[2]/button/div[2]/div[2]'
next_xpath = '//*[@id="identifierNext"]/div/button/span'

options = Options()
options.add_experimental_option("detach", True)
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)

# stay_open.add_argument("--headless=new")
# stay_open.add_argument("--remote-debugging-port=9222")
driver = webdriver.Chrome(options)
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
driver.get("https://tinder.com/404")
time.sleep(1)

driver.get(url)
print(driver.current_window_handle)
time.sleep(3)

original_window = driver.current_window_handle

driver.find_element(By.LINK_TEXT, "Log in").click()
time.sleep(2)
driver.find_element(By.XPATH, decline_cookies_xpath).click()
driver.find_element(By.TAG_NAME, "iframe").click()

wait = WebDriverWait(driver, 10)
wait.until(EC.number_of_windows_to_be(2))

for window_handle in driver.window_handles:
    if window_handle != original_window:
        driver.switch_to.window(window_handle)
print(driver.current_window_handle)

driver.find_element(By.TAG_NAME, "input").send_keys(EMAIL)
driver.find_element(By.XPATH, next_xpath).click()
time.sleep(1)
driver.find_element(By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input').send_keys(PASS, Keys.ENTER)
time.sleep(2)
keep_going = input("Hit ENTER to continue.")
driver.switch_to.window(original_window)
# Allow location
driver.find_element(By.XPATH, '//*[@id="u-1238065328"]/main/div/div/div/div[3]/button[1]/div[2]/div[2]').click()
#Don't allow notifications
driver.find_element(By.XPATH, '//*[@id="u-1238065328"]/main/div/div/div/div[3]/button[2]/div[2]/div[2]').click()
time.sleep(5)
# Start smashing those hearts
for n in range(10):
    driver.find_element(By.XPATH,
                        '//*[@id="u490315748"]/div/div[1]/div/div/main/div/div/div[1]/div/div[3]/div/div[4]/button/'
                        'span/span/svg/path')\
        .click()
    time.sleep(1)


from django.core.files import File
import os
import logging
from tabmgnt.settings import MEDIA_ROOT
import boto
from django.conf import settings
from boto.s3.connection import S3Connection, Bucket, Key

logging.basicConfig()
log = logging.getLogger(__name__)

def get_base_url():
    if not settings.ENVIRONMENT_PRODUCTION:
        return ""
    else:
        return "media/"

def convert_toS3_url(url):
    return "http://"+str(settings.AWS_STORAGE_BUCKET_NAME)+".s3.amazonaws.com/"+url if settings.ENVIRONMENT_PRODUCTION else url

def get_event_upload_path(instance, filename):
    return get_base_url() + "event/" + str(instance.event.uuid) + "/" + str(instance.id) + '-' + filename

def get_userImage_upload_path(instance, filename):
    return get_base_url() + "user/" + str(instance.user.uuid) + "/" + "images/" + filename

def get_main_home_upload_directory(instance):
    return get_base_url() + "homePage/" + str(instance.id)

def get_main_home_upload_path(instance, filename):
    uploadDirectory=get_main_home_upload_directory(instance)
    return uploadDirectory+ "/main/" + str(filename)

def get_siteInfo_image_upload_path(instance, filename):
    return get_base_url() + "homePage/" + str(instance.homePage.id) + "/siteInfo/" + filename

def remove_s3_file(filePath):
    if settings.ENVIRONMENT_PRODUCTION:
        conn = S3Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
        bucket = Bucket(conn,settings.AWS_STORAGE_BUCKET_NAME)
        for key in bucket.list(prefix=filePath):
            key.delete()
        # print("-------------------"+filePath)
        # bucket.delete_key(filePath)

def createTestFile(content, name):
    print(MEDIA_ROOT + '/test/' + name)
    try:
        with open(MEDIA_ROOT + '/test/' + name, "wb") as f:
            f.write("" + content + "")
            f.close()
    except Exception as e:
        log.error("===================" + str(e))

def createFixedDepositHtmlFile(content, name, fixedDepositScrape):
    name = name.replace(' ', '')
    try:
        with open(MEDIA_ROOT + '/' + name, "wb") as f:
            f.write("" + content + "")
        django_file = File(open(MEDIA_ROOT + '/' + name, "rb"))
        fixedDepositScrape.fixedDepositFile.save(name, django_file, save=True)
        fixedDepositScrape.save()
        os.remove(MEDIA_ROOT + '/' + name)

    except Exception as e:
        log.error("===================" + str(e))


from ..utils import _file_handle, location_normalization
from ..datatypes.arraycgh import ArrayCGH

# filter valid return only valid probes
def ucsc_mapping(ucsc_file, filter_valid=False):
    """Reads an input file describing a chip and returns a dictionary mapping the probes included in the specified chip to their relative base pairs.

    The input file must contain rows organized in 5 tab-separated columns, representing:

    #. Indexing field to speed chromosome range queries.
    #. Reference sequence chromosome or scaffold
    #. Start position in chromosome
    #. End position in chromosome
    #. Name of the probe

    Example: http://hgdownload.cse.ucsc.edu/goldenpath/hg19/database/agilentCgh4x44k.txt.gz

    Parameters
    ----------

    ucsc_file : str
        The path to the file containing the chip specification.

    filter_valid : bool, optional (default: False)
        Whether to include or not lines with possibly missing values. If set to False (default), returns a list of 4-tuples (chromosome number, start base, end base, a flag indicating if the probe has been correctly read), if set to True in each tuple the last value is omitted.

    Returns
    -------

    ucsc_mapping : list
        The list of tuples representing the probes on the chip.

    """

    ucsc_mapping = dict()
    for line in _file_handle(ucsc_file):
        _, chr, sb, eb, id = line.split()
        value = location_normalization(chr, int(sb)+1, int(eb))
        if not filter_valid:
            ucsc_mapping[id] = value
        elif not value[3]: # Only If valid
            ucsc_mapping[id] = value[:3]

    return ucsc_mapping


import numpy as np
import pandas as pd
from datetime import datetime
init_time = datetime.now()
# CONSTANTS

IS_DEV = True
TREAT_OUTLIERS = False

if(IS_DEV == True):
    TRAIN_FILE = '../input/train.csv'
    TEST_FILE = '../input/test.csv'
    COMPRESSION = None
    SKIP_ROWS = range(1, 170000000)
else:
    TRAIN_FILE = '../input/train.csv.zip'
    TEST_FILE = '../input/test.csv.zip'
    COMPRESSION = 'zip'

TRAIN_SAMPLE_FILE = '../input/' + 'train_sample.csv'

LEARNING_RATE_INIT = 0.001
LEARNING_RATE_END = 0.0001
BATCH_SIZE = 20000
EPOCHS = 2

EMBEDDING_N = 50
DENSE_N = 1024

SPATIAL_DROPOUT_1D = 0.2
DROPOUT_1 = 0.2
DROPOUT_2 = 0.2
dtypes = {
    'ip': 'uint32',
    'app': 'uint16',
    'device': 'uint16',
    'os': 'uint16',
    'channel': 'uint16',
    'is_attributed': 'uint8',
    'click_id': 'uint32'
}
# Ensure file path is correct.
train_sample = pd.read_csv(
    TRAIN_SAMPLE_FILE,
    dtype=dtypes,
    usecols=['ip', 'app', 'device', 'os', 'channel', 'click_time', 'is_attributed'],
    compression = None,
    header=0,
    #skiprows=range(1, 181886954),  # 2016-01-01)
    engine='c'
)
train_sample.head()
# Load Data
print('Loading data...')
start_time = datetime.now()
train = pd.read_csv(
    TRAIN_FILE,
    dtype=dtypes,
    usecols=['ip', 'app', 'device', 'os', 'channel', 'click_time', 'is_attributed'],
    compression = COMPRESSION,
    header=0,
    skiprows=SKIP_ROWS,  # 2016-01-01)
    engine='c'
)

test = pd.read_csv(
    TEST_FILE,
    dtype=dtypes,
    usecols=['ip', 'app', 'device', 'os', 'channel', 'click_time', 'click_id'],
    compression = COMPRESSION,
    header=0,
    #skiprows=range(1, 17886954),  # 2016-01-01)
    engine='c'
)

print('End of Loading Data: {time_taken}'.format(time_taken=datetime.now() - start_time))
train.head()
if(False):

    import seaborn as sns
    import matplotlib.pyplot as plt

    from mlxtend.preprocessing import minmax_scaling
    from scipy import stats

    positive_is_attributed = train.is_attributed.loc[train.is_attributed > 0]

    # Normalize
    normalized_is_attributed = stats.boxcox(positive_is_attributed)[0]
    fig, ax = plt.subplots(1, 3)
    sns.distplot(train.is_attributed, ax=ax[0])
    ax[0].set_title('Original Data')
    sns.distplot(normalized_is_attributed[0], ax=ax[1])
    ax[1].set_title('Normalized Data')
# Remove Outliers
print(len(train))

class Outliers():

    data = None
    def __init__(self, data):
        """
        INPUT
        data - Dataframe having date column in MM-DD-YYYY format
        """
        self.data = data

    def treat_outliers_by_iqr(self, colName, what_sd=1.5):
        mean_value = np.mean(self.data[colName].values)
        sd_value = np.std(self.data[colName].values)

        max_value = mean_value + what_sd * sd_value
        min_value = mean_value - what_sd * sd_value

        new_data = self.data[(self.data[colName] >= min_value) & (self.data[colName] <= max_value)]
        outliers = self.data[(self.data[colName] > max_value) | (self.data[colName] < min_value)]

        print('%Outliers: {outlier_percentage:<8} Mean: {mean_value:<8} SD: {sd_value:<8} IQR Min: {iqr_min:<8} IQR Max: {iqr_max:<8} Min: {mini:<8} Max: {maxi:<8}  Outliers: {outliers:<8} #App: {app_count:<8} #Obs: {size}'.format(
            outlier_percentage=np.around((len(outliers)/len(self.data))*100, decimals=2),
            mean_value=np.around(mean_value, decimals=2), sd_value=np.around(sd_value, decimals=2),
            iqr_min=np.around(min_value, decimals=2),
            iqr_max=np.around(max_value, decimals=2),
            mini=np.around(min(self.data[colName].values), decimals=2),
            maxi=np.around(max(self.data[colName].values), decimals=2),
            outliers=len(outliers),
            app_count=len(new_data.app.unique()),
            size=len(new_data)
        ))

        return outliers, new_data

if(TREAT_OUTLIERS == True):
    print('Remove Outliers...')
    start_time = datetime.now()

    outs = Outliers(train)
    outliers, train = outs.treat_outliers_by_iqr('is_attributed')

    print('End of Outlier Removal: {time_taken}'.format(time_taken=datetime.now() - start_time))
train_size = len(train)
train = train.append(test)
del test

import gc
gc.collect()
# Datetime Object
print('Making Hour, Day and Weekday Columns...')
start_time = datetime.now()

date_time = pd.to_datetime(train.click_time)

train['hour'] = date_time.dt.hour.astype('uint8')
train['day'] = date_time.dt.day.astype('uint8')
train['wday'] = date_time.dt.dayofweek.astype('uint8')

print('End of Making HDWd: {time_taken}'.format(time_taken=datetime.now() - start_time))
print('Making number of channels by IP, Day, HOUR...')
group_ip_dhc = train[['ip', 'day', 'hour', 'channel']].groupby(by=['ip', 'day', 'hour'])
group_ip_dhc = group_ip_dhc[['channel']].count().reset_index().rename(index=str, columns={'channel': 'qty'})
train = train.merge(group_ip_dhc, on=['ip', 'day', 'hour'], how='left')

del group_ip_dhc
gc.collect

train.head()

print('End of Making channel count by IDH: {time_taken}'.format(time_taken=datetime.now() - start_time))
# Group by IP, APP, OS Combination
print('Making number of channels by IP, APP...')
start_time = datetime.now()

group_ip_ac = train[['ip', 'app', 'channel']].groupby(by=['ip', 'app'])
group_ip_ac = group_ip_ac[['channel']].count().reset_index().rename(index=str, columns={'channel': 'ip_app_count'})
train = train.merge(group_ip_ac, on=['ip', 'app'], how='left')

del group_ip_ac
gc.collect()

train.head()

print('End of Making channel count by IA: {time_taken}'.format(time_taken=datetime.now() - start_time))
print('Making number of channels by IP, APP, OS...')
start_time = datetime.now()

group_ip_aoc = train[['ip', 'app', 'os', 'channel']].groupby(by=['ip', 'app', 'os'])
group_ip_aoc = group_ip_aoc[['channel']].count().reset_index().rename(index=str, columns={'channel': 'ip_app_os_count'})
train = train.merge(group_ip_aoc, on=['ip', 'app', 'os'], how='left')

del group_ip_aoc
gc.collect()

print('End of  Making channel count by IAO: {time_taken}'.format(time_taken=datetime.now() - start_time))
from sklearn.preprocessing import LabelEncoder
print('Label encoding and fit transformation...')

start_time = datetime.now()
train[['app', 'device', 'os', 'channel', 'hour', 'day', 'wday']].apply(LabelEncoder().fit_transform)

print('End of  Making channel count by IAO: {time_taken}'.format(time_taken=datetime.now() - start_time))
# train.head()
print('Splitting train and test data...')
start_time = datetime.now()

test = train[train_size:]
print(len(test))
train = train[:train_size]

y_train = train['is_attributed'].values
print(len(train))
train.drop(['click_id', 'click_time', 'ip', 'is_attributed'], 1, inplace=True)

print('End of Splitting Train and Test Dataset: {time_taken}'.format(time_taken=datetime.now() - start_time))
# Neural Network
from keras.layers import Input, Embedding, Dense, Flatten, Dropout, concatenate
from keras.layers import BatchNormalization, SpatialDropout1D, GRU
from keras.callbacks import Callback
from keras.models import Model
from keras.optimizers import Adam
max_app = np.max([train['app'].max(), test['app'].max()]) + 1
max_channel = np.max([train['channel'].max(), test['channel'].max()]) + 1
max_device = np.max([train['device'].max(), test['device'].max()]) + 1
max_os = np.max([train['os'].max(), test['os'].max()]) + 1
max_hour = np.max([train['hour'].max(), test['hour'].max()]) + 1
max_day = np.max([train['day'].max(), test['day'].max()]) + 1
max_wday = np.max([train['wday'].max(), test['wday'].max()]) + 1
max_qty = np.max([train['qty'].max(), test['qty'].max()]) + 1
max_c1 = np.max([train['ip_app_count'].max(), test['ip_app_count'].max()]) + 1
max_c2 = np.max([train['ip_app_os_count'].max(), test['ip_app_os_count'].max()]) + 1

print('Preparing dataset for training...')
start_time = datetime.now()

def get_keras_data(dataset):
    X = {
        'app': np.array(dataset.app),
        'channel': np.array(dataset.channel),
        'device': np.array(dataset.device),
        'os': np.array(dataset.os),
        'hour': np.array(dataset.hour),
        'day': np.array(dataset.day),
        'wday': np.array(dataset.wday),
        'qty': np.array(dataset.qty),
        'c1': np.array(dataset.ip_app_count),
        'c2': np.array(dataset.ip_app_os_count)
    }
    return X

train = get_keras_data(train)

in_app = Input(shape=[1], name='app')
emb_app = Embedding(max_app, EMBEDDING_N)(in_app)

in_channel = Input(shape=[1], name='channel')
emb_channel = Embedding(max_channel, EMBEDDING_N)(in_channel)

in_device = Input(shape=[1], name='device')
emb_device = Embedding(max_device, EMBEDDING_N)(in_device)

in_os = Input(shape=[1], name='os')
emb_os = Embedding(max_os, EMBEDDING_N)(in_os)

in_hour = Input(shape=[1], name='hour')
emb_hour = Embedding(max_hour, EMBEDDING_N)(in_hour)

in_day = Input(shape=[1], name='day')
emb_day = Embedding(max_day, EMBEDDING_N)(in_day)

in_wday = Input(shape=[1], name='wday')
emb_wday = Embedding(max_wday, EMBEDDING_N)(in_wday)

in_qty = Input(shape=[1], name='qty')
emb_qty = Embedding(max_qty, EMBEDDING_N)(in_qty)

in_c1 = Input(shape=[1], name='c1')
emb_c1 = Embedding(max_c1, EMBEDDING_N)(in_c1)

in_c2 = Input(shape=[1], name='c2')
emb_c2 = Embedding(max_c2, EMBEDDING_N)(in_c2)

print('End of Dataset Preparation: {time_taken}'.format(time_taken=datetime.now() - start_time))
print('Create RNN Layers...')
start_time = datetime.now()

rnn_layer_1 = GRU(16)(emb_app)
rnn_layer_2 = GRU(8)(emb_channel)
rnn_layer_3 = GRU(8)(emb_device)
rnn_layer_4 = GRU(8)(emb_os)

fe = concatenate([
    Flatten()(emb_app),
    Flatten()(emb_channel),
    Flatten()(emb_device),
    Flatten()(emb_os),
    Flatten()(emb_hour),
    Flatten()(emb_day),
    Flatten()(emb_wday),
    Flatten()(emb_qty),
    Flatten()(emb_c1),
    Flatten()(emb_c2),
    rnn_layer_1,
    rnn_layer_2,
    rnn_layer_3,
    rnn_layer_4
])
print('End of RNN Layer Creation: {time_taken}'.format(time_taken=datetime.now() - start_time))
print('Building TF architecture...')
start_time = datetime.now()

#s_dout = SpatialDropout1D(SPATIAL_DROPOUT_1D)(fe)
#x = Flatten()(s_dout)
x = Dropout(DROPOUT_1)(Dense(DENSE_N, activation='relu')(fe))
x = Dropout(DROPOUT_1)(Dense(512, activation='relu')(x))
x = Dropout(DROPOUT_2)(Dense(256, activation='relu')(fe))
x = Dropout(DROPOUT_2)(Dense(128, activation='relu')(x))
outp = Dense(1,activation='sigmoid')(x)
model = Model(inputs=[in_app, in_channel, in_device, in_os,
    in_hour, in_day, in_wday, in_qty, in_c1, in_c2], outputs=outp)

print('End of Building TF Architecture: {time_taken}'.format(time_taken=datetime.now() - start_time))
exp_decay = lambda init, fin, steps: (init/fin)**(1/(steps -1)) - 1
steps = int(len(train) / BATCH_SIZE) * EPOCHS
lr_init, lr_fin = LEARNING_RATE_INIT, LEARNING_RATE_END
lr_decay = exp_decay(lr_init, lr_fin, steps)
print('Creating the model...')
start_time = datetime.now()

optimizer_adam = Adam(lr=0.001, decay=lr_decay)
model.compile(loss='binary_crossentropy', optimizer=optimizer_adam, metrics=['accuracy'])
model.summary()

print('End of Creating the Model: {time_taken}'.format(time_taken=datetime.now() - start_time))
print('Training...')
start_time = datetime.now()

class_weight = {0:0.01, 1:0.99}
model.fit(train, y_train, batch_size=BATCH_SIZE, epochs=EPOCHS, class_weight=class_weight, shuffle=True, verbose=1)
del train, y_train
gc.collect()

print('End of Training: {time_taken}'.format(time_taken=datetime.now() - start_time))
print('Preparing test dataset...')
start_time = datetime.now()

submit = pd.DataFrame()
submit['click_id'] = test['click_id'].astype('int')
test.drop(['click_id', 'click_time', 'ip', 'is_attributed'], 1, inplace=True)
test = get_keras_data(test)

print('End of Test Dataset: {time_taken}'.format(time_taken=datetime.now() - start_time))
print('Preparing predicted output...')
start_time = datetime.now()

submit['is_attributed'] = model.predict(test, batch_size=BATCH_SIZE, verbose=2)
del test
gc.collect()
submit.to_csv('output.csv', index=False)

print('End of Predicted Output: {time_taken}'.format(time_taken=datetime.now() - start_time))
print('The END: {time_taken}'.format(time_taken=datetime.now() - init_time))
if(IS_DEV == False):
    import h5py
    model.save_weights('../output/weights.h5')
    model.save('../output/model.h5')


import os
import re
from sklearn.utils import shuffle
import pandas as pd
import nltk
from sklearn.model_selection import KFold
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem.porter import PorterStemmer

def read_text_file(file_path):

    file = ""
    with open(file_path, 'r') as f:
        file+=f.read()
    return file

def read_files(path):

    files = []
    for file in os.listdir():
        if file.endswith(".txt"):
            file_path = f"{path}\{file}"
            files.append(read_text_file(file_path))
    return files

def load_dataset() :

    negative_dataset_path = "D:\college material\\4th year\\2nd term\\NLP\\Sentiment-analysis-of-movies-reviews\\txt_sentoken\\neg"
    positive_dataset_path = "D:\college material\\4th year\\2nd term\\NLP\\Sentiment-analysis-of-movies-reviews\\txt_sentoken\\pos"
    os.chdir(negative_dataset_path)
    negative_dataset = read_files(negative_dataset_path)
    os.chdir(positive_dataset_path)
    positive_dataset = read_files(positive_dataset_path)

    return positive_dataset,negative_dataset

def remove_stop_words(dataset):

    stop_words = set(stopwords.words("english"))
    for i in range(len(dataset)):
        dataset[i] = [word for word in dataset[i] if word.casefold() not in stop_words]
    return dataset

def remove_numbers(dataset):
    for i in range(len(dataset)):
        dataset[i] = re.sub("\d+",' ', dataset[i])
        #dataset[i] = word_tokenize(dataset[i])
    return dataset

def remove_punctuation(dataset):

    for i in range(len(dataset)):
        dataset[i] = re.sub("[^a-zA-z | ^\w+'t]",' ', dataset[i])
        dataset[i] = word_tokenize(dataset[i])
    return dataset

def stemming(dataset):

    porter_stemmer = PorterStemmer()
    for i in range(len(dataset)):
        dataset[i] = [porter_stemmer.stem(word) for word in dataset[i]]
        dataset[i] = ' '.join(dataset[i])
    return dataset

def preprocess(dataset):

    dataset = remove_numbers(dataset)
    dataset = remove_punctuation(dataset)
    dataset = remove_stop_words(dataset)
    dataset = stemming(dataset)
    return dataset

def prepare_dataset():

    positive_dataset, negative_dataset = load_dataset()
    positive_dataset = preprocess(positive_dataset)
    positive_dataset = pd.DataFrame(positive_dataset)
    positive_dataset.columns = ['Review']
    positive_dataset['Label'] = ["pos" for i in range(1000)]
    negative_dataset = preprocess(negative_dataset)
    negative_dataset = pd.DataFrame(negative_dataset)
    negative_dataset.columns = ['Review']
    negative_dataset['Label'] = ["neg" for i in range(1000)]
    dataset = pd.concat([positive_dataset,negative_dataset],ignore_index=True,sort=False)
    return dataset


#!/usr/bin/env python3

from PIL import Image
from IPython.display import display
from data import *
import random
from colorGen import genColor
import json
'''
Each avatar will be unique and have 5 traits
'''

TOTAL_IMAGES = 50

all_images = []

def create_new_images():

	new_image = {}

	new_image["FACE"] = random.choices(face,face_weights)[0]
	new_image["EARS"] = random.choices(ears,ears_weights)[0]
	new_image["EYES"] = random.choices(eyes,eyes_weights)[0]
	new_image["HAIR"] = random.choices(hair, hair_weights)[0]
	new_image["MOUTH"] = random.choices(mouth, mouth_weights)[0]
	new_image["NOSE"] = random.choices(nose, nose_weights)[0]

	if new_image in all_images:
		return create_new_images()
	else:
		return new_image

for i in range(TOTAL_IMAGES):

    new_trait_image = create_new_images()

    all_images.append(new_trait_image)

def all_images_unique(all_images):
	seen = list()
	#print([i in seen or seen.append(i) for i in all_images])
	return any(i in seen or seen.append(i) for i in all_images)

i=0

for item in all_images:
	item["token_id"]=f"{i}"
	i+=1

''' Getting trait counts '''
mouth_count = {}
for item in mouth:
	mouth_count[item] = 0

face_count = {}
for item in face:
	face_count[item] = 0

eyes_count = {}
for item in eyes:
	eyes_count[item] = 0

nose_count = {}
for item in nose:
	nose_count[item] = 0

ears_count = {}
for item in ears:
	ears_count[item] = 0

hair_count = {}
for item in hair:
	hair_count[item] = 0

for image in all_images:
	face_count[image["FACE"]] += 1
	ears_count[image["EARS"]] += 1
	eyes_count[image["EYES"]] += 1
	hair_count[image["HAIR"]] += 1
	mouth_count[image["MOUTH"]] += 1
	nose_count[image["NOSE"]] += 1

for item in all_images:
	color = genColor()
	im1 = Image.open(f'substrapunks-master/scripts/face_parts/face/{face_files[item["FACE"]]}.png')
	im2 = Image.open(f'substrapunks-master/scripts/face_parts/eyes/{eyes_files[item["EYES"]]}.png')
	im3 = Image.open(f'substrapunks-master/scripts/face_parts/ears/{ears_files[item["EARS"]]}.png')
	im4 = Image.open(f'substrapunks-master/scripts/face_parts/hair/{hair_files[item["HAIR"]]}.png')
	im5 = Image.open(f'substrapunks-master/scripts/face_parts/mouth/{mouth_files[item["MOUTH"]]}.png')
	im6 = Image.open(f'substrapunks-master/scripts/face_parts/nose/{nose_files[item["NOSE"]]}.png')
	bg = Image.new("RGBA",im5.size,color=color)

	item["background"] = color

	com = Image.alpha_composite(bg,im1)
	com1 = Image.alpha_composite(com,im2)
	com2 = Image.alpha_composite(com1,im3)
	com3 = Image.alpha_composite(com2,im4)
	com4 = Image.alpha_composite(com3,im5)
	com5 = Image.alpha_composite(com4,im6)

	rgb_im = com5.convert("RGB")
	file_name = str(item["token_id"]) + ".png"

	rgb_im.save("./images/"+file_name)

METADATA_FILE = "./metadata/all_traits.json"

with open(METADATA_FILE,"w") as file:
	json.dump(all_images,file,indent=3)