import numpy as np
from keras.models import Sequential, Model, model_from_json
from keras.layers import Input
from keras.layers.core import Dense, Activation, Dropout, Merge, RepeatVector
from keras.layers.recurrent import LSTM
from keras.layers.embeddings import Embedding
from keras.preprocessing import sequence
from keras.utils.visualize_util import plot
import keras.backend as K
import json
import random
#from IPython.display import SVG
#from keras.utils.visualize_util import plot
from pymongo import MongoClient
from math import ceil

#fix random number for repeatability
#np.random.seed(17)

input_dim = 3 #input dimensions of fused sensor data
nb_timesteps = 2000 #maximum amount of samples/timesteps per shot
nb_output_class = 3     #slap, snap, wrist, none
nb_batch_size = 36 #number of samples per batch
train_test_ratio = 0.6
nb_input_multi = 6
model_filename = 'LSTM_silkymitties.json'
weights_filename = 'LSTM_silkymitties_weights.h5'

def load_training_data():
    client = MongoClient('localhost', 27017)
    db = client['restdb']
    handedness = []
    speed = []
    accuracy = []
    shotTypes = []
    y = []
    labelled_values = db['testlblfusedshots'].find()
    nb_input_samples = labelled_values.count()
    x_upper = np.empty(shape=[nb_input_samples,nb_timesteps,input_dim])
    x_lower = np.empty(shape=[nb_input_samples,nb_timesteps,input_dim])
    handedness = np.zeros(shape=[nb_input_samples,2], dtype='float')
    #speed = np.empty(shape=[nb_input_samples])
    #accuracy = np.empty(shape=[nb_input_samples])
    shotTypes = np.zeros(shape=[nb_input_samples,nb_output_class], dtype='float')
    #slapshotTypes = np.empty(shape=[nb_input_samples])
    #y = np.empty(shape=[nb_input_samples,nb_output_class])
    index = 0
    for target in labelled_values:
        upperTheta = np.vstack((target['upperTheta']['uthetaX'], target['upperTheta']['uthetaY'], target['upperTheta']['uthetaZ']))
        lowerTheta = np.vstack((target['lowerTheta']['lthetaX'], target['lowerTheta']['lthetaY'], target['lowerTheta']['lthetaZ']))
        normalizedUpperTheta = upperTheta / 180.0
        normalizedLowerTheta = lowerTheta / 180.0
        x_upper[index] = sequence.pad_sequences(normalizedUpperTheta, maxlen=nb_timesteps, dtype='float', padding='post', truncating='post', value=0.).T
        x_lower[index] = sequence.pad_sequences(normalizedLowerTheta, maxlen=nb_timesteps, dtype='float', padding='post', truncating='post', value=0.).T
        shotTypes[index,shotTypeToInt(target['shotType'])] = 1.0
        #slapshotTypes[index] = isSlapShot(target['shotType'])
        handedness[index,isLeftHanded(target['handedness'])] = 1.0
        #speed = nb.append(speed, target['speed']], axis=0)
        #accuracy = nb.append(accuracy, ['accuracy'], axis=0)
        index+=1

    #for size in range(20, nb_input_samples, 20):
    #    trainIndex = round(size * train_test_ratio)
    #    nb_epochs = ceil(size / nb_batch_size)
    #    trainShotTypeCompileFit(nb_epochs, handedness[0:trainIndex], handedness[trainIndex:size], \
    #            x_upper[0:trainIndex], x_lower[0:trainIndex], shotTypes[0:trainIndex,:], \
    #            x_upper[trainIndex:size], x_lower[trainIndex:size], shotTypes[trainIndex:size,:])

    #Shuffle the samples in unison to decrease data clustering
    s_handedness, s_x_upper, s_x_lower, s_shotTypes = unison_shuffle(handedness,
        x_upper, x_lower, shotTypes)

    trainIndex = round(nb_input_samples * train_test_ratio)
    nb_epochs = ceil(nb_input_samples / nb_batch_size)
    #trainSlapShotCompileFit(nb_epochs, handedness[0:trainIndex], handedness[trainIndex:], \
    #    x_upper[0:trainIndex], x_lower[0:trainIndex], slapshotTypes[0:trainIndex], \
    #    x_upper[trainIndex:], x_lower[trainIndex:], slapshotTypes[trainIndex:])
    trainShotTypeCompileFit(nb_epochs, s_handedness[0:trainIndex], s_handedness[trainIndex:], \
        s_x_upper[0:trainIndex], s_x_lower[0:trainIndex], s_shotTypes[0:trainIndex], \
        s_x_upper[trainIndex:], s_x_lower[trainIndex:], s_shotTypes[trainIndex:])

def unison_shuffle(a, b, c, d):
    p = np.random.permutation(len(a))
    return a[p], b[p], c[p], d[p]

def trainSlapShotCompileFit(epoch_count, train_handedness, test_handedness,
    x_train_upper, x_train_lower, y_train, x_test_upper, x_test_lower, y_test):
    #Upper hand LSTM input
    encoder_a = Sequential()
    encoder_a.add(LSTM(output_dim=nb_input_multi*input_dim,
        batch_input_shape=(nb_batch_size, nb_timesteps, input_dim),
        activation='sigmoid', inner_activation='hard_sigmoid'))
    #Lower hand LSTM input
    encoder_b = Sequential()
    encoder_b.add(LSTM(output_dim=nb_input_multi*input_dim,
        batch_input_shape=(nb_batch_size, nb_timesteps, input_dim),
        activation='sigmoid', inner_activation='hard_sigmoid'))
    encoded_handedness = Sequential()
    encoded_handedness.add(keras.layers.core.RepeatVector(nb_timesteps))
    #Merge both LSTM units with concatenation
    merged = Merge([encoded_handedness, encoder_a, encoder_b], mode='concat')
    decoder = Sequential()
    decoder.add(merged)
    decoder.add(Dropout(0.1))
    decoder.add(Dense(input_dim*nb_input_multi*2 + 2, activation='relu'))
    decoder.add(Dropout(0.2))
    decoder.add(Dense(input_dim*2, activation='relu'))
    decoder.add(Dropout(0.3))
    #sigmoid activation instead of softmax to avoid normalizing to 1.0
    #1 output signal for the binary classification likelihood
    decoder.add(Dense(1, activation='sigmoid'))

    decoder.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
    decoder.fit([train_handedness, x_train_upper, x_train_lower], y_train,
    batch_size=nb_batch_size, nb_epoch=epoch_count,
    validation_data=([test_handedness, x_test_upper, x_test_lower], y_test))

    printSummary(decoder,test_handedness,x_test_upper, x_test_lower, y_test)
    return decoder

def trainShotTypeCompileFit(epoch_count, train_handedness, test_handedness,
    x_train_upper, x_train_lower, y_train, x_test_upper, x_test_lower, y_test):
    #Upper hand LSTM input
    encoder_a = Sequential()
    encoder_a.add(LSTM(4*input_dim,
        batch_input_shape=(nb_batch_size, nb_timesteps, input_dim),
        activation='sigmoid', inner_activation='hard_sigmoid',
        return_sequences=True))
    encoder_a.add(Dropout(0.2))
    encoder_a.add(LSTM(4*input_dim, return_sequences=True,
        activation='sigmoid', inner_activation='hard_sigmoid'))
    encoder_a.add(Dropout(0.25))
    encoder_a.add(LSTM(8*input_dim, batch_input_shape=(nb_batch_size, nb_timesteps, input_dim),
        activation='sigmoid', inner_activation='hard_sigmoid'))

    #Lower hand LSTM input
    encoder_b = Sequential()
    encoder_b.add(LSTM(4*input_dim,
        batch_input_shape=(nb_batch_size, nb_timesteps, input_dim),
        activation='sigmoid', inner_activation='hard_sigmoid',
        return_sequences=True))
    encoder_b.add(Dropout(0.2))
    encoder_b.add(LSTM(4*input_dim, return_sequences=True,
        activation='sigmoid', inner_activation='hard_sigmoid'))
    encoder_b.add(Dropout(0.25))
    encoder_b.add(LSTM(8*input_dim, batch_input_shape=(nb_batch_size, nb_timesteps, input_dim),
        activation='sigmoid', inner_activation='hard_sigmoid'))

    encoded_handedness = Sequential()
    encoded_handedness.add(Dense(2, batch_input_shape=(nb_batch_size, 2)))

    #Merge both LSTM units with concatenation
    merged = Merge([encoded_handedness, encoder_a, encoder_b], mode='concat')
    decoder = Sequential()
    decoder.add(merged)
    #decoder.add(Dropout(0.25))
    #Use CNNs to expand then shrink to desired output signal
    decoder.add(Dropout(0.25))
    decoder.add(Dense(input_dim*8, activation='sigmoid'))
    decoder.add(Dropout(0.25))
    decoder.add(Dense(output_dim=(2*nb_output_class), activation='sigmoid'))
    decoder.add(Dropout(0.3))
    decoder.add(Dense(nb_output_class, activation='softmax'))

    decoder.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
    decoder.fit([train_handedness, x_train_upper, x_train_lower], y_train,
    batch_size=nb_batch_size, nb_epoch=epoch_count,
    validation_data=([test_handedness, x_test_upper, x_test_lower], y_test))

    printSummary(decoder, test_handedness, x_test_upper, x_test_lower, y_test)
    saveCompiledShotTypeModel(decoder)
    return decoder

def saveCompiledShotTypeModel(decoder):
    saved_model = decoder.to_json()
    with open('LSTM_silkymitties_ShotType.json', 'w') as outfile:
        json.dump(saved_model, outfile)
    decoder.save_weights('LSTM_silkymitties_ShotType_weights.h5')

def loadCompiledShotTypeModel():
    with open('LSTM_silkymitties_ShotType.json', 'r') as infile:
        architecture = json.load(infile)
    model = model_from_json(architecture)
    model.load_weights('LSTM_silkymitties_ShotType_weights.h5')
    return model

def predictShotTypeResult(fusedShotID):
    client = MongoClient('localhost', 27017)
    db = client['restdb']
    shot = db['fusedshots'].find_one(fusedShotID)
    x_upper = np.empty(shape=[1,nb_timesteps,input_dim])
    x_lower = np.empty(shape=[1,nb_timesteps,input_dim])
    raw_x_upper = np.vstack((shot['upperTheta']['uthetaX'],
            shot['upperTheta']['uthetaY'], shot['upperTheta']['uthetaZ']))
    raw_x_lower = np.vstack((shot['lowerTheta']['lthetaX'],
            shot['lowerTheta']['lthetaY'], shot['lowerTheta']['lthetaZ']))
    normalizedUpperTheta = raw_x_upper / 180.0
    normalizedLowerTheta = raw_x_lower / 180.0
    x_upper[0] = sequence.pad_sequences(normalizedUpperTheta, maxlen=nb_timesteps,
                dtype='float', padding='post', truncating='post', value=0.).T
    x_lower[0] = sequence.pad_sequences(normalizedLowerTheta, maxlen=nb_timesteps,
                dtype='float', padding='post', truncating='post', value=0.).T
    handedness = np.zeros(shape=[1,2])
    handedness[0,isLeftHanded(shot['handedness'])] = 1.0

    print("Loading Model")
    model = loadCompiledShotTypeModel()
    print("Loaded Model Succesfully")
    result = model.predict([handedness, x_upper, x_lower], batch_size=1)
    print("Result: " + str(result))
    resultIndex = result.argmax()
    print(resultIndex)
    shotTypeResult = shotTypeToString(resultIndex)
    print(shotTypeResult)
    return shotTypeResult

def trainShotSpeedCompileFit(speed, test_handedness, x_train_upper, x_train_lower, y_train, x_test_upper, x_test_lower, y_test):
    #Upper hand LSTM input
    encoder_a = Sequential()
    encoder_a.add(LSTM(output_dim=2*input_dim,
                    input_shape=(nb_timesteps, input_dim),
                    activation='sigmoid', inner_activation='hard_sigmoid'))
    #Lower hand LSTM input
    encoder_b = Sequential()
    encoder_b.add(LSTM(output_dim=2*input_dim,
                    input_shape=(nb_timesteps, input_dim),
                    activation='sigmoid', inner_activation='hard_sigmoid'))

    #Merge both LSTM units with concatenation
    merged = Merge([speed, encoder_a, encoder_b], mode='concat')
    decoder = Sequential()
    decoder.add(merged)
    #Use CNNs to reduce to desired intermediate shape
    decoder.add(Dense(output_dim=2*input_dim, activation='relu'))
    #sigmoid activation instead of softmax to avoid normalizing to 1.0
    #1 output signal for the binary classification likelihood
    decoder.add(Dense(1, activation='sigmoid'))

    decoder.compile(loss='mae', optimizer='rmsprop', metrics=['accuracy'])
    decoder.fit([x_train_upper, x_train_lower], y_train,
                batch_size=4*input_dim, nb_epoch=nb_epoch,
                validation_data=([x_test_upper, x_test_lower], y_test))

    printSummary(decoder,x_test_upper, x_test_lower, y_test)
    return decoder

def saveCompiledShotSpeedModel(decoder):
    saved_model = decoder.to_json()
    with open('LSTM_silkymitties_ShotSpeed.json', 'w') as outfile:
        json.dump(saved_model, outfile)
    decoder.save_weights('LSTM_silkymitties_ShotSpeed_weights.h5')

def loadCompiledShotSpeedModel():
    with open('LSTM_silkymitties_ShotSpeed.json', 'r') as infile:
        architecture = json.load(infile)
    model = model_from_json(architecture)
    model.load_weights('LSTM_silkymitties_ShotSpeed_weights.h5')
    return model

def trainShotAccuracyCompileFit(accuracy, test_handedness, x_train_upper,
                x_train_lower, y_train, x_test_upper, x_test_lower, y_test):
    #Upper hand LSTM input
    encoder_a = Sequential()
    encoder_a.add(LSTM(output_dim=2*input_dim,
                input_shape=(nb_timesteps, input_dim),
                activation='sigmoid', inner_activation='hard_sigmoid'))
    #Lower hand LSTM input
    encoder_b = Sequential()
    encoder_b.add(LSTM(output_dim=2*input_dim,
                input_shape=(nb_timesteps, input_dim),
                activation='sigmoid', inner_activation='hard_sigmoid'))

    #Merge both LSTM units with concatenation
    merged = Merge([accuracy, encoder_a, encoder_b], mode='concat')
    decoder = Sequential()
    decoder.add(merged)
    #Use CNNs to reduce to desired output classes
    decoder.add(Dense(output_dim=2*input_dim, activation='relu'))
    #decoder.add(Dropout(0.5))
    decoder.add(Dense(1, activation='softmax'))

    decoder.compile(loss='mse', optimizer='rmsprop', metrics=['accuracy'])
    decoder.fit([x_train_upper, x_train_lower], y_train,
                batch_size=4*input_dim, nb_epoch=nb_epoch,
                validation_data=([x_test_upper, x_test_lower], y_test))

    printSummary(decoder,x_test, y_test)
    return decoder

def saveCompiledShotAccuracyModel(decoder):
    saved_model = decoder.to_json()
    with open('LSTM_silkymitties_ShotAccuracy.json', 'w') as outfile:
        json.dump(saved_model, outfile)
    decoder.save_weights('LSTM_silkymitties_ShotAccuracy_weights.h5')

def loadCompiledShotAccuracyModel():
    with open('LSTM_silkymitties_ShotAccuracy.json', 'r') as infile:
        architecture = json.load(infile)
    model = model_from_json(architecture)
    model.load_weights('LSTM_silkymitties_ShotAccuracy_weights.h5')
    return model

def mean_pred(y_true, y_pred):
    return K.mean(y_pred)

def isLeftHanded(handedness):
    if str(handedness) == "L":
        return 1
    else:
        return 0

def isSlapShot(shotType):
    if str(shotType) == "slap" or str(shotType) == "Slap":
        return 1
    else:
        return 0

def shotTypeToInt(shotType):
    stringShot = str(shotType)
    #if stringShot == "notashot" or stringShot == "none" or stringShot == "NoShot":
    #    return 0
    if stringShot == "slap" or stringShot == "Slap":
        return 0
    elif stringShot == "wrist" or stringShot == "Wrist":
        return 1
    elif stringShot == "snap" or stringShot == "Snap":
        return 2

def shotTypeToString(shotType):
    #if stringShot == "notashot" or stringShot == "none" or stringShot == "NoShot":
    #    return 0
    if shotType == 0:
        return "Slap"
    elif shotType == 1:
        return "Wrist"
    elif shotType == 2:
        return "Snap"

def printSummary(decoder, test_handedness, x_test_upper, x_test_lower, y_test):
    #print(decoder.summary())
    scores = decoder.evaluate([test_handedness, x_test_upper, x_test_lower],
                y_test, verbose=0, batch_size=nb_batch_size)
    print("Accuracy: %2f%%" % (scores[1]*100))
    print(scores)
    plot(decoder, to_file='model.png', show_shapes=True)
    #plot(decoder, to_file='model.png')

if __name__=='__main__':
    #loadFromFile(True)
    #fuseLabelledShots()
    load_training_data()


import time

from .blob_client import GrokBlobClient
from .rest_client import GrokRestClient

class Grok:
    @staticmethod
    def create_from_env_var():
        """Initializes Grok based on keys in the environment variables.

        Returns:
            Grok: the Grok client
        """
        grok_rest_client = GrokRestClient.create_from_env_var()
        grok_blob_client = GrokBlobClient.create_from_env_var()
        return Grok(grok_rest_client, grok_blob_client)

    def __init__(
        self, grok_rest_client: GrokRestClient, grok_blob_client: GrokBlobClient
    ):
        self.grok_rest_client = grok_rest_client
        self.grok_blob_client = grok_blob_client

    def run_grok(
        self,
        src_folder_path,
        dest_folder_path,
        blob_dest_folder=None,
        cleanup=False,
        use_async=True,
    ):
        """Uploads images in the source folder to blob, sets up an indexing pipeline to run
        GROK OCR on this blob storage as a source, then dowloads the OCR output json to the destination
        folder. There resulting json files are of the same name as the original images except prefixed
        with the name of their folder on the blob storages and suffixed with the .json extension.

        Args:
            src_folder_path (str): Path to folder holding the images. This folder must only contain png or jpg files
            dest_folder_path (str): Path to folder where OCR json files will be placed
            blob_dest_folder (str, optional): Folder tag to use on the blob storage. If set to None, a hash is generated
                based on the names of files in the src folder. Defaults to None.
            cleanup (bool, optional): If set to True, the indexing pipeline is deleted, and the files uploaded to the blob are
                deleted from blob after running. Defaults to True.
            use_multiprocessing (boo, optional): If set to True, this will use multiprocessing to increase blob transfers speed.

        Returns:
            indexer_status json, blob folder name
        """
        print("uploading images to blob")
        blob_folder_name, _ = self.grok_blob_client.upload_images_to_blob(
            src_folder_path, dest_folder_name=blob_dest_folder, use_async=use_async
        )
        print(f"images upload under folder {blob_folder_name}")
        try:
            print("creating and running indexer")
            self.grok_rest_client.create_indexing_pipeline()
            time.sleep(2)

            indexer_status = self.grok_rest_client.get_indexer_status()
            if indexer_status["status"] == "error":
                raise RuntimeError(f"indexer error: {indexer_status}")

            # if not already running start the indexer
            print("indexer_status", indexer_status)
            if (
                indexer_status["lastResult"] is None
                or indexer_status["lastResult"]["status"] != "inProgress"
            ):
                self.grok_rest_client.run_indexer()

            time.sleep(1)
            print("\nrunning indexer")
            indexer_status = self.grok_rest_client.poll_indexer_till_complete()
            if indexer_status["lastResult"]["status"] == "success":
                time.sleep(30)
                print("fetching ocr json results.")
                self.grok_blob_client.get_ocr_json(
                    blob_folder_name, dest_folder_path, use_async=use_async
                )
                print(f"indexer status {indexer_status}")
                print(
                    f"finished running indexer. json files saved to {dest_folder_path}"
                )
            else:
                print("GROK failed", indexer_status["status"])
                raise RuntimeError("GROK failed", indexer_status["status"])
            return indexer_status, blob_folder_name
        finally:
            if cleanup:
                print("cleaning up indexer pipeline and blob store")
                self.cleanup(blob_folder_name)

    def cleanup(self, folder_name):
        """Deletes the indexing pipeline (index, indexer, datasource, skillset) from the search service.
        Deletes uploaded files from the blob

        Args:
            folder_name (str): blob folder name tag to remove
        """
        self.grok_blob_client.delete_blobs_folder(folder_name)
        self.grok_rest_client.delete_indexer_pipeline()


from ..app.controllers.root_controller import RootController
from ..app.controllers.calculations_controller import CalculationsController
from ..app.controllers.search_controller import SearchController

def routes(app, corpus, search_engine):

    root_controller = RootController()
    calculations_controller = CalculationsController(corpus)
    search_controller = SearchController(search_engine)

    app.add_url_rule('/', 'root__index', root_controller.index)
    app.add_url_rule('/calculations', 'calculations__index', calculations_controller.index)
    app.add_url_rule('/search', 'search__index', search_controller.index)
    app.add_url_rule('/search_results', 'search_results__index', search_controller.results_index)


# -*- coding: utf-8 -*-
'''
Local settings

- Run in Debug mode
- Use console backend for emails
- Add Django Debug Toolbar
- Add django-extensions as app
'''

from .common import *  # noqa

# DEBUG
# ------------------------------------------------------------------------------
DEBUG = bool(env.get('DJANGO_DEBUG', True))
TEMPLATES[0]['OPTIONS']['debug'] = DEBUG

# SECRET CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#secret-key
# Note: This key only used for development and testing.
SECRET_KEY = env.get("DJANGO_SECRET_KEY", 'a&35jr0qgxn-!fr4_8t!3+1ee)uq15n)%e*@v@f&u%81v22yxw')

# EMAIL
# If mailgun env variables is set, use that
if  env.get('DJANGO_MAILGUN_API_KEY', "NOTSET") != "NOTSET":
    MAILGUN_ACCESS_KEY = env.get('DJANGO_MAILGUN_API_KEY', "NOTSET")
    EMAIL_BACKEND = 'django_mailgun.MailgunBackend'
    MAILGUN_SERVER_NAME = env.get('DJANGO_MAILGUN_SERVER_NAME')
    DEFAULT_FROM_EMAIL = env.get('DJANGO_DEFAULT_FROM_EMAIL', 'No Reply <no-reply@mg.ccgapps.com.au>')
    EMAIL_SUBJECT_PREFIX = env.get("DJANGO_EMAIL_SUBJECT_PREFIX", '[BPA Track] ')
    SERVER_EMAIL = env.get('DJANGO_SERVER_EMAIL', DEFAULT_FROM_EMAIL)
else:
    # Console mail
    EMAIL_BACKEND = env.get('DJANGO_EMAIL_BACKEND', 'django.core.mail.backends.console.EmailBackend')
    EMAIL_HOST = 'localhost'
    EMAIL_PORT = 1025

# CACHING
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'LOCATION': ''
    }
}

# django-debug-toolbar
MIDDLEWARE_CLASSES += ('debug_toolbar.middleware.DebugToolbarMiddleware',)
INSTALLED_APPS += ('debug_toolbar', )

INTERNAL_IPS = ('127.0.0.1', '10.0.2.2',)

DEBUG_TOOLBAR_CONFIG = {
    'DISABLE_PANELS': [
        'debug_toolbar.panels.redirects.RedirectsPanel',
    ],
    'SHOW_TEMPLATE_CONTEXT': True,
}

# django-extensions
# ------------------------------------------------------------------------------
INSTALLED_APPS += ('django_extensions', )

# TESTING
# ------------------------------------------------------------------------------
TEST_RUNNER = 'django.test.runner.DiscoverRunner'

# Your local stuff: Below this line define 3rd party library settings


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib as mpl
from scipy.stats import linregress
from matplotlib import rcParams as rcp
import random
from utils import*

# /usr/bin/python3 /Users/tyama/tyama_exp/apps/com_pr.py

# ---------------- データ読み込み ------------------
# 重みなし
dataset_name = input("Enter the dataset name: ")
data_loader = DataLoader(dataset_name)
data_loader.load_graph()
data_loader.load_community()
G = data_loader.get_graph()

# {所属するコミュニティ：頂点idのリスト}, {頂点id:所属するコミュニティ}
c_id, id_c = data_loader.get_communities()

print(G) # グラフのノード数、エッジ数出力
print(f"community_num : {len(c_id)}") # コミュニティ数出力
print("-----------------------------------")

# 重みあり
dataset_name = input("Enter the Weight dataset name: ")
data_loader = DataLoader_W(dataset_name)
data_loader.load_graph()
Gw1 = data_loader.get_graph()
print("-----------------------------------")

dataset_name = input("Enter the Weight dataset name: ")
data_loader = DataLoader_W(dataset_name)
data_loader.load_graph()
Gw2 = data_loader.get_graph()

# ---------------------------------------------------

rwer_num = 100
walk_length = 100
d = 0.85
com_id = 0

# コミュニティ単位で PageRank を計算
com_walk_obj = ComWalk(G, c_id, id_c)
pr = com_walk_obj.com_pagerank(rwer_num, walk_length, d, com_id)
pr_sort = sorted(pr.items(), key=lambda x:x[1], reverse=True)

x_data = []
for item in pr_sort:
    x_data.append(item[0])

for i in range(5):
    print(x_data[i])
print("---------")

com_walk_obj_w1 = ComWalkWeighted(Gw1, c_id, id_c)
pr_w1 = com_walk_obj_w1.com_pagerank_weighted(rwer_num, walk_length, d, com_id)
pr_w1_sort = sorted(pr_w1.items(), key=lambda x:x[1], reverse=True)

y1_data = []
for item in pr_w1_sort:
    y1_data.append(item[0])

for i in range(5):
    print(y1_data[i])
print("---------")

com_walk_obj_w2 = ComWalkWeighted(Gw2, c_id, id_c)
pr_w2 = com_walk_obj_w2.com_pagerank_weighted(rwer_num, walk_length, d, com_id)
pr_w2_sort = sorted(pr_w2.items(), key=lambda x:x[1], reverse=True)

y2_data = []
for item in pr_w2_sort:
    y2_data.append(item[0])

for i in range(5):
    print(y2_data[i])
print("---------")

print("End")

# ---------------------------------------------------
# フォントを設定する。
rcp['font.family'] = 'sans-serif'
rcp['font.sans-serif'] = ['Hiragino Maru Gothic Pro', 'Yu Gothic', 'Meirio', 'Takao', 'IPAexGothic', 'IPAPGothic', 'VL PGothic', 'Noto Sans CJK JP']

# カラーマップを用意する。
cmap = plt.get_cmap("tab10")

# Figureを作成する。
fig = plt.figure()
# Axesを作成する。
ax = fig.add_subplot(111)

# Figureの解像度と色を設定する。
fig.set_dpi(150)
fig.set_facecolor("white")

# Axesのタイトルと色を設定する。
#ax.set_title("物品の所有率")
ax.set_facecolor("white")

# x軸とy軸のラベルを設定する。
ax.set_xlabel("Original Rank", fontsize=14)
ax.set_ylabel("PSI Rank", fontsize=14)

i = 0
for x in x_data:
    ax.plot(i, y1_data.index(x), marker='.', color="b")
    i += 1

plt.show()
# ---------------------------------------------------

# フォントを設定する。
rcp['font.family'] = 'sans-serif'
rcp['font.sans-serif'] = ['Hiragino Maru Gothic Pro', 'Yu Gothic', 'Meirio', 'Takao', 'IPAexGothic', 'IPAPGothic', 'VL PGothic', 'Noto Sans CJK JP']

# カラーマップを用意する。
cmap = plt.get_cmap("tab10")

# Figureを作成する。
fig = plt.figure()
# Axesを作成する。
ax = fig.add_subplot(111)

# Figureの解像度と色を設定する。
fig.set_dpi(150)
fig.set_facecolor("white")

# Axesのタイトルと色を設定する。
#ax.set_title("物品の所有率")
ax.set_facecolor("white")

# x軸とy軸のラベルを設定する。
ax.set_xlabel("Original Rank", fontsize=14)
ax.set_ylabel("PSI_reverse Rank", fontsize=14)

i = 0
for x in x_data:
    ax.plot(i, y2_data.index(x), marker='.', color="b")
    i += 1

plt.show()
# ---------------------------------------------------


class Marble:
    def __init__(self, location, colour):
        '''Initialises a Marble object:
            Sets location and colour'''

        self.__location = location
        self.__colour = colour

    def move(self, location):
        '''Changes the location of the marble'''

        self.set_location(location)

    def check_rolls(self, marble_locations, hop_location=None, hoppable=False):
        '''Returns the possible locations the marble can roll to
        If hoppable is true the inverse is returned, it is only used in check hops
        By default the method checks this marble's hops, a different location can be used'''

        if hop_location is None:
            hop_location = self.__location

        possible_locations = []

        ###~~~Checking surrounding neighbours~~~###
        for extra in [(1, 0), (-1, 0), (0, 1), (0, -1), (-1, 1), (1, -1)]: #[(+x), (-x), (+y), (-y), (+z), (-z)]
            x, y = hop_location

            extra_x, extra_y = extra
            neighbour_location = (x + extra_x, y + extra_y)
            #Checks the all possible rolls to see if a marble occupies that space

            if neighbour_location not in marble_locations and not hoppable:
                possible_locations.append(neighbour_location)
            if neighbour_location in marble_locations and hoppable:
                possible_locations.append((x + 2*extra_x, y + 2*extra_y)) #Hopped location
        ###~~~                               ~~~###

        return possible_locations

    def check_hops(self, marble_locations, locations, hop_location=None):
        '''Returns the possible locations the marble can hop to
        By default it checks for this marble but it can check a generic locations'''

        if hop_location is None:
            hop_location = self.__location

        x, y = self.get_location()
        mask_locations = self.__mask_locations(x, y, locations)
        allowed_locations = []

        ###~~~Remove locations with marbles~~~###
        for location in mask_locations:
            if location not in marble_locations:
                allowed_locations.append(location)
        ###~~~                             ~~~###

        possible_locations = []

        ###~~~The locations one hop away~~~###
        new_locations = self.check_rolls(marble_locations, hop_location=hop_location, hoppable=True)

        ###~~~Removes locations with marbles in them~~~###
        for location in new_locations:
            if location in allowed_locations:
                possible_locations.append(location)
        ###~~~                                      ~~~###

        ###~~~Repeat process for each new location added~~~###
        for location in possible_locations:
            new_locations = self.check_rolls(marble_locations, hop_location=location, hoppable=True)

            for new_location in new_locations:
                if new_location in allowed_locations and new_location not in possible_locations:
                    possible_locations.append(new_location)
        ###~~~                                          ~~~###
        #This terminates when no new locations are added
        #It works by adding to the array that is being looped so will continue until no more are added

        return possible_locations

    def __mask_locations(self, x_coord, y_coord, locations):
        '''Returns the locations that could be hopped to'''

        mask_locations = []

        #The locations are every second space
        #This extends from the marble location to the edges of the board

        for mask_x in range(x_coord-16, 17+x_coord, 2):
            for mask_y in range(y_coord-16, 17+y_coord, 2):
                masked = (mask_x, mask_y)

                if masked in locations:
                    mask_locations.append(masked)

        return mask_locations

    def get_colour(self):
        return self.__colour

    def get_location(self):
        return self.__location

    def set_location(self, location):
        self.__location = location

def main():
    pass

if __name__ == '__main__':
    main()


# Program to calculate PF value based on age input

def pf(a, b):
    if a>10 and a<30:
        pf = b * 0.08
        return pf
    elif a>=30 and a<60:
        pf = b * 0.1
        return pf
    else:
        print('invalid age')

age = int(input('Enter the age: '))
basic = float(input('Enter basic salary: '))

k = pf(age, basic)
print('The pf value is: ', k)


import numpy as np
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.datasets import imdb
from tensorflow.keras.preprocessing import sequence

from fast_text import FastText

def create_ngram_set(input_list, ngram_value=2):
    """
    Extract a set of n-grams from a list of integers.
    # >>> create_ngram_set([1, 4, 9, 4, 1, 4], ngram_value=2)
    {(4, 9), (4, 1), (1, 4), (9, 4)}
    # >>> create_ngram_set([1, 4, 9, 4, 1, 4], ngram_value=3)
    [(1, 4, 9), (4, 9, 4), (9, 4, 1), (4, 1, 4)]
    """
    return set(zip(*[input_list[i:] for i in range(ngram_value)]))

def add_ngram(sequences, token_indice, ngram_range=2):
    """
    Augment the input list of list (sequences) by appending n-grams values.
    Example: adding bi-gram
    # >>> sequences = [[1, 3, 4, 5], [1, 3, 7, 9, 2]]
    # >>> token_indice = {(1, 3): 1337, (9, 2): 42, (4, 5): 2017}
    # >>> add_ngram(sequences, token_indice, ngram_range=2)
    [[1, 3, 4, 5, 1337, 2017], [1, 3, 7, 9, 2, 1337, 42]]
    Example: adding tri-gram
    # >>> sequences = [[1, 3, 4, 5], [1, 3, 7, 9, 2]]
    # >>> token_indice = {(1, 3): 1337, (9, 2): 42, (4, 5): 2017, (7, 9, 2): 2018}
    # >>> add_ngram(sequences, token_indice, ngram_range=3)
    [[1, 3, 4, 5, 1337, 2017], [1, 3, 7, 9, 2, 1337, 42, 2018]]
    """
    new_sequences = []
    for input_list in sequences:
        new_list = input_list[:]
        for ngram_value in range(2, ngram_range + 1):
            for i in range(len(new_list) - ngram_value + 1):
                ngram = tuple(new_list[i:i + ngram_value])
                if ngram in token_indice:
                    new_list.append(token_indice[ngram])
        new_sequences.append(new_list)

    return new_sequences

# Set parameters:
# ngram_range = 2 will add bi-grams features
ngram_range = 1
max_features = 5000
maxlen = 400
batch_size = 32
embedding_dims = 50
epochs = 10

print('Loading data...')
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
print(len(x_train), 'train sequences')
print(len(x_test), 'test sequences')
print('Average train sequence length: {}'.format(np.mean(list(map(len, x_train)), dtype=int)))
print('Average test sequence length: {}'.format(np.mean(list(map(len, x_test)), dtype=int)))

if ngram_range > 1:
    print('Adding {}-gram features'.format(ngram_range))
    # Create set of unique n-gram from the training set.
    ngram_set = set()
    for input_list in x_train:
        for i in range(2, ngram_range + 1):
            set_of_ngram = create_ngram_set(input_list, ngram_value=i)
            ngram_set.update(set_of_ngram)

    # Dictionary mapping n-gram token to a unique integer.
    # Integer values are greater than max_features in order
    # to avoid collision with existing features.
    start_index = max_features + 1
    token_indice = {v: k + start_index for k, v in enumerate(ngram_set)}
    indice_token = {token_indice[k]: k for k in token_indice}

    # max_features is the highest integer that could be found in the dataset.
    max_features = np.max(list(indice_token.keys())) + 1

    # Augmenting x_train and x_test with n-grams features
    x_train = add_ngram(x_train, token_indice, ngram_range)
    x_test = add_ngram(x_test, token_indice, ngram_range)
    print('Average train sequence length: {}'.format(np.mean(list(map(len, x_train)), dtype=int)))
    print('Average test sequence length: {}'.format(np.mean(list(map(len, x_test)), dtype=int)))

print('Pad sequences (samples x time)...')
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
print('x_train shape:', x_train.shape)
print('x_test shape:', x_test.shape)

print('Build model...')
model = FastText(maxlen, max_features, embedding_dims)
model.compile('adam', 'binary_crossentropy', metrics=['accuracy'])

print('Train...')
early_stopping = EarlyStopping(monitor='val_accuracy', patience=3, mode='max')
model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          callbacks=[early_stopping],
          validation_data=(x_test, y_test))

print('Test...')
result = model.predict(x_test)


from sqlalchemy.orm import relationship

from aps_app import db
import datetime
from sqlalchemy import Column, Integer, DateTime, Date, String, Boolean, Float, Text, JSON, ForeignKey

class Dogs(db.Model):

    __tablename__ = "dogs"

    dog_id = Column(Integer, primary_key=True, unique=True, autoincrement=True)
    coordinator_id = Column(Integer, ForeignKey("users.user_id"), nullable=False)
    name = Column(String(20), nullable=False)
    birthdate = Column(Date, nullable=False)
    sex = Column(String(1), nullable=False)
    breed = Column(String(100), nullable=False, default="Unknown")
    size = Column(String(20), nullable=False)
    color = Column(String(100), nullable=False)
    house_trained = Column(Boolean, nullable=False)
    good_with_kids = Column(Boolean, nullable=False)
    intake_date = Column(Date, default=lambda: datetime.date.today())
    adoption_fee = Column(Float, nullable=False)
    status = Column(Integer, nullable=False, default=1)
    display_status = Column(Integer, nullable=False, default=0)
    data = Column(JSON, nullable=False)
    created_at = Column(DateTime, default=lambda: datetime.datetime.utcnow())

    coordinator = relationship("Users")

    @classmethod
    def create(cls, coordinator_id, name, birthdate, sex, breed, color, size, house_trained, good_with_kids, intake_date, adoption_fee, status, display_status, data):
        new_dog = Dogs(
            coordinator_id=coordinator_id,
            name=name,
            birthdate=birthdate,
            sex=sex,
            breed=breed,
            color=color,
            size=size,
            house_trained=house_trained,
            good_with_kids=good_with_kids,
            intake_date=intake_date,
            adoption_fee=adoption_fee,
            status=status,
            display_status=display_status,
            data=data
        )

        db.session.add(new_dog)
        db.session.commit()

        return new_dog

    @classmethod
    def __repr__(cls):
        return f'<Dog: {cls.name}, Age: {cls.age}, Sex: {cls.sex}, ID: {cls.dog_id}>'  # function ensures readable print statement for debugging

class DogImages(db.Model):

    __tablename__ = "dog_images"

    image_id = Column(Integer, primary_key=True, unique=True, autoincrement=True)
    dog_id = Column(Integer, ForeignKey("dogs.dog_id"), nullable=False)
    asset_id = Column(String(100), nullable=False)  # NOT SURE IF THIS LENGTH IS OK
    image_url = Column(Text, nullable=False)
    original_filename = Column(Text, nullable=False)
    format = Column(String(20), nullable=False)
    public_id = Column(Text, nullable=False)
    secure_url = Column(Text, nullable=False)
    signature = Column(Text, nullable=False)
    created_at = Column(DateTime, nullable=False)

    dog = relationship("Dogs")

    @classmethod
    def store_dog_image(cls, dog_id, image):
        new_img = DogImages(
            dog_id=dog_id,
            asset_id=image["asset_id"],
            image_url=image["image_url"],
            original_filename = image["original_filename"],
            format=image["format"],
            public_id=image["public_id"],
            secure_url=image["secure_url"],
            signature=image["signature"],
            created_at=image["created_at"],
        )

        db.session.add(new_img)
        db.session.commit()

        return new_img


#
'''
re.findall()
    + 按照正则表达式的规则在字符中匹配所有符合规则的元素，结果返回一个列表，如果没有找到则返回空列表

re.finditer()
    + 按照正则表达式的规则在字符中匹配所有符合规则的元素，返回一个迭代器

re.sub() 搜索替换
    + 按照正则表达式的规则，在字符串中找到需要 被替换的字符串，完成一个替换
参数：
    pattern： 正则表达式的规则，匹配需要被替换的字符串
    repl：    替换后的字符串
    string：  被替换的原始字符串

compile()
    可以直接将正则表达式定义为 正则对象，使用正则对象直接操作

'''

import re

# 定义字符串
varstr = 'iloveyou521tosimida511'

# 正则表达式
reg = '\d{3}'
# 函数调用
# res = re.findall(reg,varstr)
# res = re.finditer(reg,varstr)
# print(list(res))

# 找到数字，替换成其它
# res = re.sub(reg,'AAA',varstr)
# print(res)

# 直接定义正则表达式对象
reg = re.compile('\d{3}')
# 直接使用创建的正则对象，去调用对应的方法或函数
res = reg.findall(string=varstr)
# print(res)

lines = [
    'i love 512 you',
    'i love 521 you',
    'i love 345 you',
    'i love 543 you',
]

reg = re.compile('\d{3}')
for i in lines:
    # reg = '\d{3}'
    # res = re.search(reg,i)
    # print(res.group())

    res = reg.search(i).group()
    print(res)

爬虫 课件类/学习猿地/re/代码/3.re模块相关函数-其它函数.py

3.re模块相关函数-其它函数.py


from django.http import Http404
from blog.models import Blogs,Blogs1
from django.shortcuts import render,get_object_or_404
from django.http import HttpResponse

def home(request):
    all_blog = Blogs.objects.all()
    #return HttpResponse("Hello,welcome to bLOGGER")
    return render(request,'blog/home.html',{
        'all_blogs' : all_blog
    })

def details(request, blog_id):
    try:
        all_blog1 = Blogs.objects.get(pk =blog_id)
        blog_id1 =blog_id
    except Blogs.Does.Not.Exit:
        raise Http404("Blogs does not exist")
    return render(request,'blog/details.html',{
         'all_blog1' :all_blog1 ,'blog_id1' :blog_id1
        #'all_blog1' : Blogs.objects.filter(id =blog_id)
    })

def favorite(request,blog_id):
    all_blog1 =get_object_or_404(Blogs, pk =blog_id)
    blog_id1 =blog_id
    try:

        selected_blog =all_blog1.blogs1_set.get(pk =request.POST['x'])
    except(KeyError):
    #except(KeyError,blog1.Does.Not.Exist):
        return render(request,'blog/details.html',{
            'all_blog1': all_blog1 ,'blog_id1' :blog_id1,'error_message' : "you did not select a valid blog"
        })
    else:
        selected_blog.is_favorite =True
        selected_blog.save()
    return render(request,'blog/details.html',{
        'all_blog1': all_blog1 ,'blog_id1' :blog_id1
            })

# Create your views here.


import random

numeropote1 = random.sample(range(1, 8), 7)
numeropote2 = random.sample(range(0, 8), 8)
numeropote3 = random.sample(range(0, 8), 8)
numeropote4 = random.sample(range(0, 8), 8)

pote1 = {'Brasil': 'CMB', 'Catar': 'AFC', 'Bélgica': 'UEFA', 'França': 'UEFA', 'Argentina': 'CMB', 'Inglaterra': 'UEFA',
         'Espanha': 'UEFA', 'Portugal': 'UEFA'}
# print(pote1)
lista1 = ['Catar', 'Brasil', 'Bélgica', 'França', 'Argentina', 'Inglaterra', 'Espanha', 'Portugal']
# print(lista1)

# print(numeropote1)
lista2 = ['Holanda', 'Alemanha', 'Dinamarca', 'México', 'Estados Unidos', 'Suíça', 'Uruguai', 'Croácia']
pote2 = {'Holanda': 'UEFA', 'Alemanha': 'UEFA', 'Dinamarca': 'UEFA', 'México': 'CONCACAF',
         'Estados Unidos': 'CONCACAF', 'Suíça': 'UEFA', 'Uruguai': 'CMB', 'Croácia': 'UEFA'}
lista3 = ['Senegal', 'Irã', 'Japão', 'Sérvia', 'Polônia', 'Coreia do Sul', 'Marrocos', 'Tunísia']
pote3 = {'Senegal': 'CAF', 'Irã': 'AFC', 'Japão': 'AFC', 'Sérvia': 'UEFA', 'Polônia': 'UEFA',
         'Coreia do Sul': 'AFC', 'Marrocos': 'CAF', 'Tunísia': 'CAF'}

lista4 = ['Canadá', 'Equador', 'Arábia Saudita', 'Gana', 'Camarões', 'Europeu', 'AsiaCMB', 'OFCConcacaf']
pote4 = {}
# print(numeropote2)
grupoA = [[(lista1[0])], [lista2[(numeropote2[0])]],[lista3[numeropote3[0]]],[lista4[numeropote4[0]]]]
print(grupoA)
grupoB = [[lista1[(numeropote1[0])]], [lista2[(numeropote2[1])]],[lista3[numeropote3[1]]],[lista4[numeropote4[1]]]]
print(grupoB)
grupoC = [[lista1[(numeropote1[1])]], [lista2[(numeropote2[2])]],[lista3[numeropote3[2]]],[lista4[numeropote4[2]]]]
print(grupoC)
grupoD = [[lista1[(numeropote1[2])]], [lista2[(numeropote2[3])]],[lista3[numeropote3[3]]],[lista4[numeropote4[3]]]]
print(grupoD)
grupoE = [[lista1[(numeropote1[3])]], [lista2[(numeropote2[4])]],[lista3[numeropote3[4]]],[lista4[numeropote4[4]]]]
print(grupoE)
grupoF = [[lista1[(numeropote1[4])]], [lista2[(numeropote2[5])]],[lista3[numeropote3[5]]],[lista4[numeropote4[5]]]]
print(grupoF)
grupoG = [[lista1[(numeropote1[5])]], [lista2[(numeropote2[6])]],[lista3[numeropote3[6]]],[lista4[numeropote4[6]]]]
print(grupoG)
grupoH = [[lista1[(numeropote1[6])]], [lista2[(numeropote2[7])]],[lista3[numeropote3[7]]],[lista4[numeropote4[7]]]]
print(grupoH)


text1 = input("Enter a word: ")
text2 = input("Enter the character list: ")

text1 = ''.join(sorted(text1.lower()))
text2 = ''.join(sorted(text2.lower()))

contains = False
start_pos = 0

for char in text1:
    start_pos = text2.find(char, start_pos)
    if start_pos >= 0:
        start_pos += 1
        contains = True
    else:
        contains = False
        break

if contains:
    print('Yes')
else:
    print('No')


# Licensed to the .NET Foundation under one or more agreements.
# The .NET Foundation licenses this file to you under the Apache 2.0 License.
# See the LICENSE file in the project root for more information.

# Excel COM Interop tests

from iptest.assert_util import *
skiptest("win32", "cli64", "posix")
from iptest.cominterop_util import *

if not IsExcelInstalled():
    from sys import exit
    print("Excel is not installed.  Cannot run this test!")
    exit(1)
else:
    TryLoadExcelInteropAssembly()
    from Microsoft.Office.Interop import Excel

#------------------------------------------------------------------------------
#--HELPERS
selection_counter = 0

def selection_change_eventhandler(range):
    global selection_counter
    selection_counter = selection_counter + 1
    #print "selected range - " + range.Address[0]

def add_worksheet_event(ws):
    ws.SelectionChange += selection_change_eventhandler

def remove_worksheet_event(ws):
    ws.SelectionChange -= selection_change_eventhandler

#-----------------------------------------------------------------------------
#--TESTS
def test_excel():
    ex = None

    try:
        ex = CreateExcelApplication()
        Assert(not callable(ex))
        ex.DisplayAlerts = False
        AreEqual(ex.DisplayAlerts, False)

        for x in dir(ex):
            AreEqual(dir(ex).count(x), 1)

        #ex.Visible = True
        nb = ex.Workbooks.Add()
        ws = nb.Worksheets[1]

        AreEqual('Sheet1', ws.Name)

        #Dev10 409961
        # COM has 1-based arrays
        #AssertError(EnvironmentError, lambda: ws.Rows[0])

        for i in range(1, 10):
            for j in range(1, 10):
                ws.Cells[i, j] = i * j

        rng = ws.Range['A1', 'B3']
        AreEqual(6, rng.Count)

        co = ws.ChartObjects()
        graph = co.Add(100, 100, 200, 200)
        graph.Chart.ChartWizard(rng, Excel.XlChartType.xl3DColumn)

    finally:
        # clean up outstanding RCWs
        ws = None
        nb = None
        rng = None
        System.GC.Collect()
        System.GC.WaitForPendingFinalizers()

        if ex: ex.Quit()
        else: print("ex is %s" % ex)

def test_excel_typelibsupport():
    ex = None

    try:
        ex = CreateExcelApplication()

        typelib = clr.LoadTypeLibrary(ex)
        AreEqual(typelib.Name, 'Excel')
        typelib.Excel.__class__
        Assert('Application' in dir(typelib.Excel))
        Assert('XlSaveAction' in dir(typelib.Excel))
        Assert('xlSaveChanges' in dir(typelib.Excel.XlSaveAction))
        AreEqual(typelib.Excel.XlSaveAction.xlSaveChanges, 1)

        # verify namespace Excel is not yet available
        try:
            Excel.__class__
        except NameError: pass
        else: Fail("namespace Excel has not been imported yet")

        typelib = clr.AddReferenceToTypeLibrary(ex)
        try:
            Excel.__class__
        except NameError: pass
        else: Fail("namespace Excel has not been imported yet")

        from . import Excel
        Assert('Application' in dir(Excel))
        Assert('XlSaveAction' in dir(Excel))
        Assert('xlSaveChanges' in dir(Excel.XlSaveAction))
        AreEqual(Excel.XlSaveAction.xlSaveChanges, 1)

    finally:
        System.GC.Collect()
        System.GC.WaitForPendingFinalizers()

        if ex: ex.Quit()
        else: print("ex is %s" % ex)

def excel_events_helper(ex):
    ex.Workbooks.Add()
    ws = ex.ActiveSheet

    # test single event is firing
    add_worksheet_event(ws)
    ex.ActiveCell.Offset[1, 0].Activate()
    AreEqual(selection_counter, 1)

    # test events chaining is working
    add_worksheet_event(ws)
    ex.ActiveCell.Offset[1, 0].Activate()
    AreEqual(selection_counter, 3)

    # test removing event from a chain
    remove_worksheet_event(ws)
    ex.ActiveCell.Offset[1, 0].Activate()
    AreEqual(selection_counter, 4)

    # test removing event alltogether
    remove_worksheet_event(ws)
    ex.ActiveCell.Offset[1, 0].Activate()
    AreEqual(selection_counter, 4)

    add_worksheet_event(ws)
    ex.ActiveCell.Offset[1, 0].Activate()
    AreEqual(selection_counter, 5)

def test_excelevents():
    import gc
    ex = None
    try:
        ex = CreateExcelApplication()
        ex.DisplayAlerts = False
        #Regression for CodePlex 18614
        temp_list = dir(ex)
        for x in temp_list:
            if temp_list.count(x)!=1:
                Fail("There should be exactly one '%s' in dir(excel)" % str(x))

        #ex.Visible = True

        global selection_counter
        selection_counter = 0

        # we need all temps/locals allocated for worksheets to be in a separate function
        # in order to be collected by GC
        excel_events_helper(ex)

        gc.collect()
        System.GC.WaitForPendingFinalizers()

        ex.ActiveCell.Offset[1, 0].Activate()
        AreEqual(selection_counter, 5)

    finally:
        # clean up outstanding RCWs
        gc.collect()
        System.GC.WaitForPendingFinalizers()

        if ex: ex.Quit()
        else: print("ex is %s" % ex)

def test_cp148579():
    AssertErrorWithMessage(TypeError,
                           "Cannot create instances of Range because it is abstract",
                           Excel.Range, 1,2)

def test_cp14539():
    try:
        ex = CreateExcelApplication()
        for i in range(3):
            AreEqual(ex.Visible, False)
            ex.Visible = True
            if not is_stress:
                AreEqual(ex.Visible, True)
            ex.Visible = False

    finally:
        ex.Quit()

def test_cp24654():
    app = CreateExcelApplication()
    try:
        app.workbooks.Add()
        workbook = app.ActiveWorkbook
        worksheet = workbook.ActiveSheet
        chart = worksheet.ChartObjects
        type(chart)
        AreEqual(type(chart.__doc__), str)
    finally:
        app.Quit()

#------------------------------------------------------------------------------
run_com_test(__name__, __file__)


from datetime import datetime
import uuid
from random import getrandbits
from scapy.contrib.pfcp import CauseValues, IE_ApplyAction, IE_Cause, \
    IE_CreateFAR, IE_CreatePDR, IE_CreateURR, IE_DestinationInterface, \
    IE_DurationMeasurement, IE_EndTime, IE_EnterpriseSpecific, IE_FAR_Id, \
    IE_ForwardingParameters, IE_FSEID, IE_MeasurementMethod,IE_OuterHeaderCreation, \
    IE_NetworkInstance, IE_NodeId, IE_PDI, IE_PDR_Id, IE_Precedence, \
    IE_QueryURR, IE_RecoveryTimeStamp, IE_RedirectInformation, IE_ReportType, \
    IE_ReportingTriggers, IE_SDF_Filter, IE_SourceInterface, IE_StartTime, \
    IE_TimeQuota, IE_UE_IP_Address, IE_URR_Id, IE_UR_SEQN, IE_OuterHeaderRemoval,\
    IE_UsageReportTrigger, IE_VolumeMeasurement, IE_ApplicationId, PFCP,IE_FTEID, \
    PFCPAssociationSetupRequest, PFCPAssociationSetupResponse, \
    PFCPHeartbeatRequest, PFCPHeartbeatResponse, PFCPSessionDeletionRequest, \
    PFCPSessionDeletionResponse, PFCPSessionEstablishmentRequest, \
    PFCPSessionEstablishmentResponse, PFCPSessionModificationRequest, \
    PFCPSessionModificationResponse, PFCPSessionReportRequest
from scapy.contrib.pfcp import PFCPAssociationReleaseRequest
from scapy.layers.l2 import Ether
from scapy.layers.inet import IP, UDP, TCP
from scapy.layers.inet6 import IPv6
from scapy.packet import Raw
from scapy.all import send,sniff
import logging
import threading
import signal,os

PFCP_CP_IP_V4 = "192.168.0.115"
PFCP_UP_IP_V4 = "192.168.0.118"
N3_IP_V4 = "193.168.1.3"
GNB_IP_V4 = "193.168.1.2"
UE_IP_V4 = "10.10.10.2"

def seid():
    return uuid.uuid4().int & (1 << 64) - 1

class PfcpSkeleton(object):
    def __init__(self, pfcp_cp_ip,pfcp_up_ip):
        self.pfcp_cp_ip = pfcp_cp_ip
        self.pfcp_up_ip = pfcp_up_ip
        self.ue_ip = UE_IP_V4
        self.ts = int((datetime.now() - datetime(1900, 1, 1)).total_seconds())
        self.seq = 1
        self.nodeId = IE_NodeId(id_type=2, id="ergw")
        logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        self.logger = logging.getLogger(__name__)

    def ie_ue_ip_address(self, SD=0):
        return IE_UE_IP_Address(ipv4=self.ue_ip, V4=1, SD=SD)

    def ie_fseid(self):
        return IE_FSEID(ipv4=self.pfcp_cp_ip, v4=1, seid=self.cur_seid)

    def associate(self):
        self.chat(PFCPAssociationSetupRequest(IE_list=[
            IE_RecoveryTimeStamp(timestamp=self.ts),
            self.nodeId
            ]))

    def heartbeatRep(self):
        pkt = sniff(iface="veth0",filter="udp port 8805", count=1)
        resp = pkt[0][PFCP]
        self.logger.info("REQ: %r" % resp.message_type)
        if resp.message_type == 1:
            heartReq = resp[PFCPHeartbeatRequest]
            heartRep = PFCPHeartbeatResponse(IE_list=[
            IE_RecoveryTimeStamp(timestamp=self.ts)])
            self.chat(heartRep, resp.seq)

    def heartbeat(self):
        resp = self.chat(PFCPHeartbeatRequest(IE_list=[
            IE_RecoveryTimeStamp(timestamp=self.ts)
            ]))

    def establish_session_request(self):
        self.cur_seid = seid()
        resp = self.chat(PFCPSessionEstablishmentRequest(IE_list=[
            #UPLINK FAR=1,PDR=1
            IE_CreateFAR(IE_list=[
                IE_ApplyAction(FORW=1),
                IE_FAR_Id(id=1)
            ]),
            IE_CreatePDR(IE_list=[
                IE_FAR_Id(id=1),
                IE_PDI(IE_list=[
                    IE_NetworkInstance(instance="access"),
                    IE_SDF_Filter(
                        FD=1,
                        flow_description="permit out ip from any to assigned"),
                        IE_SourceInterface(interface="Access"),
                        self.ie_ue_ip_address(SD=0),
                    IE_FTEID(V4=1,TEID=1111,ipv4=N3_IP_V4)
                ]),
                IE_PDR_Id(id=1),
                IE_Precedence(precedence=200),
                IE_OuterHeaderRemoval(),
            ]),

            #DOWNLINK FAR=2,PDR=2
            IE_CreateFAR(IE_list=[
                IE_ApplyAction(FORW=1),
                IE_FAR_Id(id=2),
                IE_ForwardingParameters(IE_list=[
                    IE_DestinationInterface(interface="Access"),
                    IE_NetworkInstance(instance="n6"),
                    IE_OuterHeaderCreation(GTPUUDPIPV4=1,TEID=2222,ipv4=GNB_IP_V4)
                ])
            ]),
            IE_CreatePDR(IE_list=[
                IE_FAR_Id(id=2),
                IE_PDI(IE_list=[
                    IE_NetworkInstance(instance="n6"),
                    IE_SourceInterface(interface="Core"),
                    self.ie_ue_ip_address(SD=1)
                ]),
                IE_PDR_Id(id=2),
                IE_Precedence(precedence=200),
                IE_OuterHeaderRemoval(),
            ]),
            self.ie_fseid(),
            IE_NodeId(id_type=2, id="cp")
        ]), seid=self.cur_seid)

    def chat(self, pkt, seq=None,seid=None):
        self.logger.info("REQ: %r" % pkt)
        send(
            IP(src=self.pfcp_cp_ip, dst=self.pfcp_up_ip) /
            UDP(sport=8805, dport=8805) /
            PFCP(
                version=1,
                S=0 if seid is None else 1,
                seid=0 if seid is None else seid,
                seq=self.seq if seq is None else seq) /
                pkt)
        if seq is None:
            self.seq +=1
    def signal_fun(self,signum,frame):
        self.chat(PFCPAssociationReleaseRequest(IE_list=[
            self.nodeId
            ]))
        os._exit(0)

class HeartBeatThread(threading.Thread):
    def __init__(self, threadID, name, counter,ass):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
        self.ass = ass
    def run(self):
        while self.counter:
            self.ass.heartbeatRep()
            self.counter -= 1

if __name__ =="__main__":
    pfcp_client = PfcpSkeleton(PFCP_CP_IP_V4,PFCP_UP_IP_V4)
    pfcp_client.heartbeat()
    pfcp_client.establish_session_request()


"""
compute simlarity of inferred vectors between all neurosynth datasets
"""

import os,pickle
import numpy,pandas
from joblib import Parallel, delayed,load,dump

def vector_corr(i,data):
    """
    find the real image that most closely matches the index image
    ala Kay et al., 2008
    """
    corr_all=numpy.zeros(data.shape[0])
    for j in range(i,data.shape[0]):
        corr_all[j]=numpy.corrcoef(data[i,:],data[j,:])[0,1]
    return corr_all

data=pandas.read_csv('../data/neurosynth/ns_doc2vec_300dims_projection.csv',index_col=0)
data=data.values

testmode=False

if testmode:
    data=data[:5,:]

dump(data,'ns_vectors.mm')

data = load('ns_vectors.mm', mmap_mode='r')

results=Parallel(n_jobs=24)(delayed(vector_corr)(i,data) for i in range(data.shape[0]))

pickle.dump(results,open('../data/neurosynth/ns_vector_similarity_results.pkl','wb'))

os.remove('ns_vectors.mm')


import os
import glob
import sys
import time
import argparse
from manuscript_reader import DocxManuscriptReader, TxtManuscriptReader, CsvManuscriptReader
from manuscript_writer import DocxManuscriptWriter, TxtManuscriptWriter
from gpt_api_handler import GPTAPIHandler
from openai.error import RateLimitError  # Import RateLimitError

def read_prompt_file(prompt_file):
    with open(prompt_file, 'r') as file:
        return file.read()

def get_reader(file_path, start_text):
    if file_path.endswith(".docx"):
        return DocxManuscriptReader(file_path, start_text=start_text)
    elif file_path.endswith(".txt"):
        return TxtManuscriptReader(file_path, start_text=start_text)
    elif file_path.endswith(".csv"):
        return CsvManuscriptReader(file_path, start_text=start_text)
    else:
        raise ValueError(f"Unsupported file type for {file_path}")

def get_writer(file_path):
    if file_path.endswith(".docx"):
        return DocxManuscriptWriter(file_path)
    elif file_path.endswith(".txt"):
        return TxtManuscriptWriter(file_path)
    elif file_path.endswith(".csv"):
        return TxtManuscriptWriter(file_path)  # Assuming writing CSV as plain text for now
    else:
        raise ValueError(f"Unsupported file type for {file_path}")

def main(input_directory, output_directory, prompt_file, start_text=None):
    api_key = os.environ.get('OPENAI_API_KEY')
    if not api_key:
        print("Error: OPENAI_API_KEY environment variable not set.")
        sys.exit(1)

    # Read the content of the prompt file
    with open(prompt_file, 'r') as file:
        prompt = file.read()

    # Get a list of all files in the input directory
    input_files = glob.glob(os.path.join(input_directory, "*"))

    # Create a file to store all chunks
    with open('temp_output.txt', 'w+') as temp_file:
        for input_file in input_files:
            # Determine the output file path
            base_name = os.path.basename(input_file)
            output_file = os.path.join(output_directory, base_name)

            # Determine the appropriate reader and writer based on the file extension
            if input_file.lower().endswith(".docx"):
                reader = DocxManuscriptReader(input_file, start_text=start_text)
                writer = DocxManuscriptWriter(output_file)
            elif input_file.lower().endswith(".txt"):
                reader = TxtManuscriptReader(input_file, start_text=start_text)
                writer = TxtManuscriptWriter(output_file)
            elif input_file.lower().endswith(".csv"):
                reader = CsvManuscriptReader(input_file, start_text=start_text)
                writer = TxtManuscriptWriter(output_file)
            else:
                print(f"Skipping file {input_file} with unrecognized extension.")
                continue

            gpt_api = GPTAPIHandler(prompt, api_key)

            chunks = list(reader.get_chunks())
            total_chunks = len(chunks)

            for i, chunk in enumerate(chunks):
                print(f"Processing chunk {i+1} of {total_chunks} for file {input_file}")
                print(f"Sending text to GPT API: {chunk[:100]}...")

                # Modified part
                while True:
                    try:
                        edited_text = gpt_api.get_edited_text(chunk)
                        break  # Breaks the loop if the API call was successful
                    except Exception as e:
                        print(f"Error occurred: {str(e)}. Retrying after 60 seconds.")
                        time.sleep(60)  # Wait for 60 seconds before trying again

                print(f"Received edited text: {edited_text[:100]}...")

                # Write edited text to temp file
                temp_file.write(edited_text + "\n")
                temp_file.flush()  # Flush the buffer

                writer.add_paragraph(edited_text)

            # Save the document after all chunks have been processed
            writer.save()

            print(f"Edited manuscript for file {input_file} saved to {output_file}")

        print(f"All edited texts have been recorded in the file: {temp_file.name}")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Manuscript Editor")
    parser.add_argument("input_directory", help="Input manuscript directory path")
    parser.add_argument("output_directory", help="Output edited manuscript directory path")
    parser.add_argument("prompt_file", help="Prompt file path")
    parser.add_argument("--start_text", help="Optional start text to process the input manuscript from", default=None)

    args = parser.parse_args()

    main(args.input_directory, args.output_directory, args.prompt_file, args.start_text)


from keras_retinanet.models import load_model
from keras_retinanet.utils.image import read_image_bgr, preprocess_image, resize_image
import cv2
import os
import time
import matplotlib.pyplot as plt
from keras_retinanet.utils.visualization import *

def evaluate(modelPath, config):
    inferenceModel = load_model(modelPath, backbone_name=config.backbone)

    for i in range(1169, 1183):

        labels_to_names = {0: 'person', 1: 'bicycle', 2: 'car'}
        image = read_image_bgr(os.path.join("..", "Data", "images", "set00-V000-" + str(i) + ".jpg"))
        draw = image.copy()
        draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

        # preprocess image for network
        image = preprocess_image(image)
        image, scale = resize_image(image)

        # process image
        start = time.time()
        boxes, scores, labels = inferenceModel.predict_on_batch(np.expand_dims(image, axis=0))
        print("processing time: ", time.time() - start)
        print(sum(sum(boxes)))
        # correct for image scale
        boxes /= scale
        print(scores[0])
        # visualize detections
        for box, score, label in zip(boxes[0], scores[0], labels[0]):
            # scores are sorted so we can break
            print(score)
            if score < 0.5:
                break

            color = label_color(label)

            b = box.astype(int)
            draw_box(draw, b, color=color)

            caption = "{} {:.3f}".format(labels_to_names[label], score)
            draw_caption(draw, b, caption)

        plt.figure(figsize=(15, 15))
        plt.axis('off')
        plt.imshow(draw)
        plt.show()


from django.contrib.auth.admin import UserAdmin as OriginalUserAdmin
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.admin import UserAdmin
from .models import User
from django.contrib.auth.models import Group
from logging import exception
from django.contrib import admin
from django.contrib import messages
from django.db import IntegrityError
from django.http import Http404, HttpResponseRedirect
from django.urls import reverse
from . models import *
from django.urls import path
from . forms import *
from django.shortcuts import render
from django.http import HttpResponse
from django.utils.html import format_html
from math import ceil
# from django.contrib.auth.models import User
from django.contrib import messages
from import_export.admin import ExportActionMixin
from django_summernote.admin import SummernoteModelAdmin
from django_summernote.utils import get_attachment_model
from django.contrib.auth.admin import UserAdmin
from django.core.mail import send_mail

class AddressAdmin(ExportActionMixin, admin.ModelAdmin):
   #
    # env\Lib\site-packages\jazzmin\static\jazzmin\js\action_button.js
    js = ('jazzmin/js/action_button.js',)
    list_display = ('id', 'user', 'fullname','phone','locality','city','state','address','home','work','default')
    list_filter = ('city', 'user', 'state', 'pincode','home','work')
    list_per_page = 10
    search_fields = ('user', 'city', 'state', 'pincode','home','work')
    list_editable=("home",'work','default')
    # actions=['downloadCV']
    #autocomplete_fields = ['user']
    save_on_top = True
    class Meta:
        verbose_name_plural = "Customer Address"

class imageAdmin(admin.ModelAdmin):
    try:
        def image_tag(self, obj):
            return format_html(''.format(obj.image.url, "100", "100"))
        image_tag.short_description = 'Image'
        image_tag.allow_tags = True
        list_display = ['id', 'image_tag', 'action_btn']

        def action_btn(self, obj):
            html = "
 "
            try:
                html += "

".format(
                    obj.image.url, "100", "100")
            except:
                html += "

".format(
                    obj.image.url)
            html += "
"
            return format_html(html)
        action_btn.short_description = "Action"

        def get_urls(self):
            urls = super().get_urls()
            new_urls = [path('upload-csv/', self.upload_csv), ]
            return new_urls + urls

        def upload_csv(self, request):
            try:
                if request.method == "POST":
                    csv_file = request.FILES["csv_upload"]
                    # csvv=pd.read_csv(csv_file)

                    if not csv_file.name.endswith('.csv'):
                        messages.warning(request, 'Please upload csv file')
                        return HttpResponseRedirect(request.path_info)
                    file_data = csv_file.read().decode("utf-8")
                    csv_data = file_data.split("\r\n")
                    for x in csv_data:
                        fields = x.split(",")
                        try:
                            created = image.objects.create(
                                image="product/"+fields[0],)
                            created.save()
                        except IndexError:
                            pass
                        except (ValidationError, IntegrityError):
                            form = CsvImportForm()
                            data = {"form": form}
                            message = messages.warning(
                                request, 'Something went wrong! check your file again \n 1.Upload correct file \n 2.Check you data once')
                            return render(request, "admin/csv_upload.html", data)
                    url = reverse('admin:index')
                    return HttpResponseRedirect(url)
                form = CsvImportForm()
                data = {"form": form}
                return render(request, "admin/csv_upload.html", data)
            except:
                pass
    except:
        pass

class CategoryAdmin(ExportActionMixin, admin.ModelAdmin):
    try:
        def image_tag(self, obj):
            try:
                return format_html(''.format(obj.category_image.url, "100", "100"))
            except:
                return format_html('')
                #message=messages.warning('Something went wrong! check your file again \n 1.Upload correct file \n 2.Check you data once')
        image_tag.short_description = 'Brand Thumbnail'
        image_tag.allow_tags = True
        list_display = ['id', 'category', 'image_tag',
                        'is_active', 'updated_at', 'action_btn']
        #list_editable = ('slug', )
        list_editable = ('is_active',)
        list_filter = ('category', 'is_active', 'updated_at')
        list_per_page = 10
        search_fields = ('category', 'description', 'is_active', 'updated_at')
        save_on_top = True

        def action_btn(self, obj):
            html = "

"
            try:
                html += "

".format(
                    obj.category_image.url, "100", "100")
            except:
                html += "

"
            html += "
"
            return format_html(html)
        action_btn.short_description = "Action"
        #prepopulated_fields = {"slug": ("title", )}

        def get_urls(self):
            urls = super().get_urls()
            new_urls = [path('upload-csv/', self.upload_csv), ]
            return new_urls + urls

        def upload_csv(self, request):
            try:
                if request.method == "POST":
                    csv_file = request.FILES["csv_upload"]
                    if not csv_file.name.endswith('.csv'):
                        messages.warning(
                            request, 'The wrong file type was uploaded')
                        return HttpResponseRedirect(request.path_info)
                    file_data = csv_file.read().decode("utf-8")
                    csv_data = file_data.split("\r\n")
                    for x in csv_data:
                        fields = x.split(",")
                        try:
                            created = Category.objects.create(
                                brands=fields[0],
                                description=fields[1],
                                category_image="category/"+fields[2],)
                            created.save()
                        except IndexError:
                            pass
                        except (ValidationError, IntegrityError):
                            form = CsvImportForm()
                            data = {"form": form}
                            message = messages.warning(
                                request, 'Something went wrong! check your file again \n 1.Upload correct file \n 2.Check you data once')
                            return render(request, "admin/csv_upload.html", data)
                    url = reverse('admin:index')
                    return HttpResponseRedirect(url)
                form = CsvImportForm()
                data = {"form": form}
                return render(request, "admin/csv_upload.html", data)
            except:
                pass
    except exception:
        raise Http404

class BrandAdmin(ExportActionMixin, admin.ModelAdmin):
    try:
        def image_tag(self, obj):
            try:
                return format_html(''.format(obj.logo.url, "100", "100"))
            except:
                return format_html('')
                #message=messages.warning('Something went wrong! check your file again \n 1.Upload correct file \n 2.Check you data once')
        image_tag.short_description = 'Brand Thumbnail'
        image_tag.allow_tags = True
        list_display = ['id','brand_name', 'image_tag','details','date','action_btn']
        #list_editable = ('slug', )
        #list_editable = ('is_active',)
        list_filter = ('brand_name', 'date')
        #list_per_page = 10
        search_fields = ('brand_name', 'details', 'date')
        save_on_top = True

        def action_btn(self, obj):
            html = "

"
            try:
                html += "

".format(
                    obj.logo.url, "100", "100")
            except:
                html += "

"
            html += "
"
            return format_html(html)
        action_btn.short_description = "Action"
    except:
        pass

class ProductAdmin(ExportActionMixin, admin.ModelAdmin):
    try:
        #inlines = [ProductImageAdmin]
        def imagee(self, obj):
            # a=obj.image.first()
            # print(obj.image.first().image.url)
            try:
                return format_html(''.format(obj.image.first().image.url, "50", "50"))
            except:
                return format_html('')
        imagee.short_description = 'Product Image'

        def image_tag2(self, obj):
            try:
                return format_html(''.format(obj.product_image.url, "50", "50"))
            except:
                return format_html('')
        image_tag2.short_description = 'Product Thumbnail'
        image_tag2.allow_tags = True
        list_display = ['id', 'title',"type",'category','subcategory','brand',"attributes","amps","volts",'imagee', 'price', 'discounted_price','available_stocks', 'is_active',
                        'updated_at','created_at','action_btn']  # ,'is_active','is_featured'
        list_editable = ('category','attributes', 'is_active','brand','available_stocks',"type","amps","volts")
        list_filter = ('category', 'is_active', 'updated_at')
        list_per_page = 20
        #inlines = [ImagemInline]
        search_fields = ('title', 'short_description',
                         'is_active', 'updated_at')
        save_on_top = True
        actions = ['sales_discount', 'delete_offers']
        actions_list = ['sales_discount', 'delete_offers']
        actions_row = ['sales_discount', 'delete_offers']
        actions_detail = ['sales_discount', 'delete_offers']
        def action_btn(self, obj):
            html = "

"
            try:
                html += "

".format(
                    obj.image.first().image.url, "100", "100")
            except:
                html += "

"
            html += "
"
            return format_html(html)
        action_btn.short_description = "Action"

        def delete_offers(self, request, queryset):
            """ from django.db import connection
            cursor = connection.cursor()
            cursor.execute("update app1_product set discounted_price =NULL") """
            for product in queryset:
                product.discounted_price = None
                product.save(update_fields=['discounted_price'])
                if product.discounted_price == 0:
                    from django.db import connection
                    cursor = connection.cursor()
                    cursor.execute(
                        "update app1_product set discounted_price =NULL where discounted_price=0")

        def sales_discount(self, request, queryset):
            Sales = sales.objects.filter(is_active=True)
            # Sales = sales.objects.all()
            # print(Sales)
            for sale in Sales:
                discount = sale.sales_discount
                if sale.is_active == True:
                    for product in queryset:
                        multiplier = discount / 100
                        old_price = product.price
                        new_price = ceil(old_price - (old_price * multiplier))
                        product.discounted_price = new_price
                        product.save(update_fields=['discounted_price'])

                elif sale.is_active == False:
                    return messages.warning(request, 'No active Discounts')
                else:
                    print(sale.id)
                    return messages.warning(request, 'No Discounts')
        sales_discount.short_description = format_html(
            "
Apply discount
")
        def get_urls(self):
            urls = super().get_urls()
            new_urls = [path('upload-csv/', self.upload_csv), ]
            return new_urls + urls

        def upload_csv(self, request):
            # file = open('sample.csv')
            # csv_reader = csv.reader(file)
            # next(csv_reader)
            try:
                if request.method == "POST":
                    csv_file = request.FILES["csv_upload"]
                    if not csv_file.name.endswith('.csv'):
                        messages.warning(request, 'Please upload csv file')
                        return HttpResponseRedirect(request.path_info)
                    file_data = csv_file.read().decode("utf-8")
                    csv_data = file_data.split("\n")

                    for x in csv_data:
                        print(x, type(x))
                        fields = x.split(",")

                        try:
                            for i in range(len(fields)):
                                print("fields[{}]".format(i),fields[i])
                            created, k = Product.objects.update_or_create(
                                title=fields[0],
                                sku=fields[1],
                                short_description=fields[2],
                                detail_description=fields[3],
                                specification=fields[4],
                                price=fields[5],
                                discounted_price=fields[6],
                                category=Category.objects.get(pk=(fields[7])),
                                subcategory=subcategory.objects.get(pk=(fields[8])),
                                type=producttype.objects.get(pk=(fields[9])),
                                brand=Brand.objects.get(pk=(fields[10])),
                                amps=currentrating.objects.get(pk=(fields[11])),
                                volts=voltagerating.objects.get(pk=(fields[12])),
                                attributes=Attributes.objects.get(pk=(fields[13])),
                                available_stocks=fields[14],
                            )
                            n = str(x)
                            l = n.split('"')
                            if len(l) > 1:
                                k = l[1].split('"')
                                images_csv = (k[0].split('"'))
                                splited_image = (images_csv[0].split(','))

                                for i in range(len(splited_image)):
                                    iter_image = splited_image[i]

                                    imagess = image.objects.get(pk=(int(iter_image)))
                                    print("2222222",imagess)
                                    created.image.add(imagess)
                                    created.save()
                            else:
                                imagess = image.objects.get(pk=fields[15])
                                print("2222222",imagess)
                                print(imagess)
                                created.image.add(imagess)
                                created.save()
                                print("saved")
                        except IndexError:
                            pass

                        except (ValidationError, IntegrityError):
                            form = CsvImportForm()
                            data = {"form": form}
                            message = messages.warning(
                                request, "Something went wrong! check your file again \n 1.Upload correct file \n 2.Check your data once")
                            return render(request, "admin/csv_upload.html", data)
                        except TypeError as e:
                            form = CsvImportForm()
                            data = {"form": form}
                            message = messages.warning(request, e)
                            #message=messages.warning(request,"Check the category ID")
                            return render(request, "admin/csv_upload.html", data)
                        except:
                            form = CsvImportForm()
                            data = {"form": form}
                            message = messages.warning(
                                request, "category or image query doesnt exist")
                            return render(request, "admin/csv_upload.html", data)
                    url = reverse('admin:index')
                    return HttpResponseRedirect(url)

                form = CsvImportForm()
                data = {"form": form}
                return render(request, "admin/csv_upload.html", data)
            except (IntegrityError) as e:
                messages.error(request, e)
    except exception:
        pass
class subcategoryadmin(admin.ModelAdmin):
    list_display = ['id','sub_category', 'category',]
    # search_fields = ['category','Question']
    # actions = ['make_published', 'make_withdraw']
    list_editable= ['sub_category']

class AttributesAdmin(admin.ModelAdmin):
    list_display = ('id', 'Color','action_btn')
    list_filter = ('Color',)
    list_per_page = 20
    search_fields = ('Color',)
    save_on_top = True

    def action_btn(self, obj):
        html = "

"
        html += "

"
        html += "
"
        return format_html(html)
    action_btn.short_description = "Action"
class salesAdmin(admin.ModelAdmin):
    list_display = ('id', 'campaign_name', 'startdate', 'enddate',
                    'sales_discount', 'is_active', 'created_at', 'action_btn')
    list_filter = ('campaign_name', 'startdate', 'enddate', 'sales_discount')
    list_editable = ('is_active', )
    list_per_page = 20
    search_fields = ('campaign_name', 'startdate', 'enddate')
    save_on_top = True
    def action_btn(self, obj):
        html = "

"
        html += "

"
        html += "
"
        return format_html(html)
    action_btn.short_description = "Action"
class CoupenAdmin(admin.ModelAdmin):
    list_display = ['id', 'coupon', 'coupon_discount',
                    'startdate', 'enddate', 'created_at', 'action_btn']
    #list_filter = ['is_active','startdate','enddate']
    search_fields = ['created_at']
    #list_editable = ('is_active', )

    def action_btn(self, obj):
        html = "

"
        html += "

"
        html += "
"
        return format_html(html)
    action_btn.short_description = "Action"

class RatingAdmin(admin.ModelAdmin):
    list_display = ['id', 'user', 'product', 'Reviews', 'Rating',
                     'status_', 'created_date', 'action_btn']
    search_fields = ['user']
    actions = ['approved_status', 'rejected_status']

    def has_add_permission(self, request):
        return True

    def action_btn(self, obj):
        html = "
"
        return format_html(html)
    action_btn.short_description = "Delete"

    @admin.action(description='Approve')
    def approved_status(self, request, queryset):
        queryset.update(Status="Approved")

    @admin.action(description='Reject')
    def rejected_status(self, request, queryset):
        queryset.update(Status='Rejected')

class BlogAdmin(SummernoteModelAdmin):
    try:
        """ def dummy(self,obj):
            html="<object style='height: 100px; width: 100%'><param name='movie' value='https://youtu.be/HYOvEIimVzI'>"
            html+="<param name='allowFullScreen' value='true'><param name='allowScriptAccess' value='always'>"
            html+="<embed src='https://youtu.be/HYOvEIimVzI' type='application/x-shockwave-flash' allowfullscreen='true' allowScriptAccess='always' width='640' height='390'></object>"
            return format_html(html)
        dummy.allow_tags=True"""

        def imagee(self, obj):
            # a=obj.image.first()
            # print(obj.image.first().image.url)
            try:
                return format_html(''.format(obj.image.first().image.url, "100", "100"))
            except:
                return format_html('')
        imagee.short_description = 'Image'
        imagee.allow_tags = True
        def image_tag(self, obj):
            try:
                return format_html(''.format(obj.images.url, "100", "100"))
            except:
                return format_html('')
                #message=messages.warning('Something went wrong! check your file again \n 1.Upload correct file \n 2.Check you data once')
        image_tag.short_description = 'Thumbnail Image'
        image_tag.allow_tags = True

        def video_url(self, obj):
            return format_html('Play video' % (obj.url))
            # return format_html('' % (obj.url))
        video_url.allow_tags = True
        video_url.short_description = 'video'
        list_display = ['id', 'title', 'detail_description',
                        'imagee','location','uploaded_date','action_btn']
        search_fields = ['title']
        summernote_fields = ('description', )

        def action_btn(self, obj):
            html = "

"
            # html += "

"

            html += "

".format(
                    obj.image.first().image.url, "100", "100")
            html += "
"
            return format_html(html)
        action_btn.short_description = "Action"
    except:
        pass

class FAQAdmin(admin.ModelAdmin):
    list_display = ['id','category', 'Question', 'Answer',
                    'status', 'created_date', 'updated_at']
    search_fields = ['category','Question']
    actions = ['make_published', 'make_withdraw']
    list_editable= ['category']
    @admin.action(description='Publish')
    def make_published(modeladmin, request, queryset):
        queryset.update(status='p')

    @admin.action(description='Withdraw')
    def make_withdraw(modeladmin, request, queryset):
        queryset.update(status='w')

class BannerAdmin(admin.ModelAdmin):
    def image_tag(self, obj):
        try:
            return format_html(''.format(obj.image.url, "100", "100"))
        except:
            return format_html('')
            #message=messages.warning('Something went wrong! check your file again \n 1.Upload correct file \n 2.Check you data once')
    image_tag.short_description = 'Image'
    image_tag.allow_tags = True
    list_display = ['id', 'title', 'image_tag','is_active','uploaded_date', 'action_btn']
    list_editable=['is_active']
    search_fields = ['title']
    def action_btn(self, obj):
        html = "

"
        # html += "

"
        html += "

".format(
                    obj.image.url, "100", "100")
        html += "
"
        return format_html(html)
    action_btn.short_description = "Action"
from django.core.mail import EmailMessage
from django.template.loader import render_to_string,select_template
class customer_messageAdmin(admin.ModelAdmin):
    list_display = ['id', 'first_name', 'last_name', 'Phone',
                    'Email', 'Message', 'created_date', 'updated_at', 'action_btn']
    actions = ["send_message"]

    def action_btn(self, obj):
        html = "
"
        return format_html(html)
    action_btn.short_description = "Action"
    def send_message(self, request, queryset):
        print(queryset)
        # try:
        for i in queryset:
            if i.Email and i.first_name:
                html_tpl_path=("email.html",)
                context_data={"name":"sandeep"}
                email_html_template=select_template(html_tpl_path).render(context_data)
                email = i.Email
                message = "Hi {},\nyour message was recieved\nthank you ".format(i.first_name)
                a = send_mail('Prakash Electrical', email_html_template, settings.EMAIL_HOST_USER, [
                                email], fail_silently=False,),messages.success(request, "Successfully sent to {}".format(email))
    # actions = ["send_message"]
class mailadmin(admin.ModelAdmin):
    list_display = ['subject', 'message', 'send_it',
                    'created_date', 'updated_at', 'action_btnn']
    actions = ['send_message',]
    list_editable = ['send_it']

    def action_btnn(self, obj):
        html = "

"
        html += "

"
        html += "
"
        return format_html(html)
    action_btnn.short_description = "Action"

    def get_form(self, request, obj=None, **kwargs):
        # if obj.type == "1":
        self.exclude = ("send_it", )
        form = super(mailadmin, self).get_form(request, obj, **kwargs)
        return form
class cartadmin(ExportActionMixin,admin.ModelAdmin):
    list_display=['id','user','product','attributes','price','offer_price','quantity','Total_amount','amount_saved','date','updated_at']
    list_editable = ['product','attributes','quantity']
    search_fields = ['user__username','product__title']

class latestproductadmin(admin.ModelAdmin):
    list_display=['id','product','created_date','action_btnn']
    list_editable=['product']
    def action_btnn(self, obj):
        html = "

"
        html += "

"
        html += "
"
        return format_html(html)
    action_btnn.short_description = "Action"

    MAX_OBJECTS = 1

    def has_add_permission(self, request):
        if self.model.objects.count() >= 10:
            return False
        return super().has_add_permission(request)
class mostselledproductadmin(admin.ModelAdmin):
    list_display=['id','product','created_date','action_btnn']
    list_editable=['product']

    def action_btnn(self, obj):
        html = "

"
        html += "

"
        html += "
"
        return format_html(html)
    action_btnn.short_description = "Action"
    def has_add_permission(self, request):
        if self.model.objects.count() >= 10:
            return False
        return super().has_add_permission(request)
class newsletterproductadmin(admin.ModelAdmin):
    list_display=['id','Email','subscribed_date']
    # list_editable=['']
    search_fields = ['Email']
class myaccount(admin.ModelAdmin):
    list_display=['id','user','is_confirmed']
    list_editable=['is_confirmed']
    # search_fields = ['Email']
class socialmedialinksadmin(admin.ModelAdmin):
    list_display=['id','social_media','links','action_btnn']
    list_editable=['links']
    def action_btnn(self, obj):
        html = "

"
        html += "

"
        html += "
"
        return format_html(html)
    action_btnn.short_description = "Action"

class faq_enquiryAdmin(admin.ModelAdmin):
    list_display = ['id', 'name', 'Phone',
                    'Email', 'Message', 'created_date', 'updated_at', 'action_btn']
    def action_btn(self, obj):
        html = "
"
        return format_html(html)
    action_btn.short_description = "Action"
class notificationadmin(admin.ModelAdmin):
    list_display=["id",'message','created_date']
class userphotoadmin(admin.ModelAdmin):
    list_display=['id','user','photo']
class cartorderadmin(admin.ModelAdmin):
    list_display=["id","user","order_payment_id","total_price","shipping_address","is_paid","date","status"]
    list_editable=["status"]
class paymentadmin(admin.ModelAdmin):
    list_display=["id","order_id","payment_id","signature_id","amount"]
class refundadmin(admin.ModelAdmin):
    list_display=["refundId","entity","currency","amount","payment_id","created_at","status","speed_processed","speed_requested"]
class infoadmin(admin.ModelAdmin):
    list_display=['id','phone','mail','Address','about','is_active']
    list_editable=['phone',"mail","is_active"]
    MAX_OBJECTS = 1
    def has_add_permission(self, request):
        if self.model.objects.count() >= 1:
            return False
        return super().has_add_permission(request)
class typeadmin(admin.ModelAdmin):
    list_display=["id","type",]
class currentadmin(admin.ModelAdmin):
    list_display=["id","rating",]
class voltageadmin(admin.ModelAdmin):
    list_display=["id","rating",]

admin.site.register(userphoto,userphotoadmin)
admin.site.register(notification,notificationadmin)
admin.site.register(enquiryform,faq_enquiryAdmin)
admin.site.register(socialmedialinks,socialmedialinksadmin)
# admin.site.register(latest_product,latestproductadmin)
admin.site.register(most_selled_products,mostselledproductadmin)
admin.site.register(newsletter,newsletterproductadmin)
admin.site.register(Cart,cartadmin)
admin.site.register(Category, CategoryAdmin)
admin.site.register(Product, ProductAdmin)
admin.site.register(Address, AddressAdmin)
admin.site.register(Attributes, AttributesAdmin)
admin.site.register(sales, salesAdmin)
admin.site.register(image, imageAdmin)
admin.site.register(Coupon, CoupenAdmin)
admin.site.register(Rating, RatingAdmin)
admin.site.register(Blog, BlogAdmin)
admin.site.register(FAQ, FAQAdmin)
admin.site.register(customer_message, customer_messageAdmin)
admin.site.register(Banner, BannerAdmin)
admin.site.register(MailText, mailadmin)
admin.site.unregister(get_attachment_model())
admin.site.unregister(Group)
admin.site.register(Brand,BrandAdmin)
admin.site.register(my_account,myaccount)
admin.site.register(subcategory,subcategoryadmin)
admin.site.register(cart_order,cartorderadmin)
admin.site.register(payment,paymentadmin)
admin.site.register(refund,refundadmin)
admin.site.register(redeemedcoupons)
admin.site.register(info,infoadmin)
admin.site.register(producttype,typeadmin)
admin.site.register(currentrating,currentadmin)
admin.site.register(voltagerating,voltageadmin)
# admin.site.register(cart2)
# admin.site.register(notificationn)
class UserAdmin(ExportActionMixin,OriginalUserAdmin):
    list_display = ['id','username', 'email','is_staff', 'phone_no','is_confirmed','is_staff','action_btn','last_login','date_joined']
    list_editable=['is_confirmed']

    def action_btn(self, obj):
        html = "

"
        html += "

"
        html += "
"
        return format_html(html)
    action_btn.short_description = "Action"
admin.site.register(User, UserAdmin)